-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathFilesystemParseSpec.hs
More file actions
174 lines (148 loc) · 5.44 KB
/
FilesystemParseSpec.hs
File metadata and controls
174 lines (148 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
module FilesystemParseSpec
( spec
)
where
import Prelude
import Common
import Data.String.Conversions (cs)
import Data.Text (Text)
import Data.Time.Clock (UTCTime)
import Database.Schema.Migrations.Filesystem
( FilesystemStoreSettings (..)
, migrationFromFile
)
import Database.Schema.Migrations.Migration
import System.FilePath ((</>))
import Test.Hspec
spec :: Spec
spec = do
describe "migrationFromFile" $ do
let
testStorePath :: FilePath
testStorePath = testFile "migration_parsing"
fp :: FilePath -> FilePath
fp = (testStorePath </>)
migrationFromFile' :: Text -> IO (Either String Migration)
migrationFromFile' =
migrationFromFile (FSStore {storePath = testStorePath}) . cs
it "fully valid" $ do
migrationFromFile' "valid_full" `shouldReturn` Right validFull
it "fully valid with fractional seconds in timestamp" $ do
migrationFromFile' "valid_full_fractional_ts"
`shouldReturn` Right (validFullFractionalTs {mId = "valid_full_fractional_ts"})
it "comments" $ do
migrationFromFile' "valid_with_comments"
`shouldReturn` Right (validFull {mId = "valid_with_comments"})
it "comments (2)" $ do
migrationFromFile' "valid_with_comments2"
`shouldReturn` Right (validFullComments {mId = "valid_with_comments2"})
it "colon" $ do
migrationFromFile' "valid_with_colon"
`shouldReturn` Right (validFullColon {mId = "valid_with_colon"})
it "multi-line deps" $ do
migrationFromFile' "valid_with_multiline_deps"
`shouldReturn` Right
( validFull
{ mId = "valid_with_multiline_deps"
, mDeps = ["one", "two", "three"]
}
)
it "no deps" $ do
migrationFromFile' "valid_no_depends"
`shouldReturn` Right (validFull {mId = "valid_no_depends", mDeps = []})
it "no description" $ do
migrationFromFile' "valid_no_desc"
`shouldReturn` Right (validFull {mId = "valid_no_desc", mDesc = Nothing})
it "no revert" $ do
migrationFromFile' "valid_no_revert"
`shouldReturn` Right (validFull {mId = "valid_no_revert", mRevert = Nothing})
it "no timestamp" $ do
migrationFromFile' "valid_no_timestamp"
`shouldReturn` Right (validFull {mId = "valid_no_timestamp", mTimestamp = Nothing})
context "invalid" $ do
it "missing required fields" $ do
pendingWith "Aeson 2.x changes the message format"
migrationFromFile' "invalid_missing_required_fields"
`shouldReturn` Left
( "Could not parse migration "
<> fp "invalid_missing_required_fields"
<> ":Error in "
<> show (fp "invalid_missing_required_fields")
<> ": missing required field(s): "
<> "[\"Depends\"]"
)
it "unrecognized field name" $ do
pendingWith "Aeson 2.x changes the message format"
migrationFromFile' "invalid_field_name"
`shouldReturn` Left
( "Could not parse migration "
<> fp "invalid_field_name"
<> ":Error in "
<> show (fp "invalid_field_name")
<> ": unrecognized field found"
)
it "invalid syntax" $ do
migrationFromFile' "invalid_syntax"
`shouldReturn` Left
( "Could not parse migration "
<> fp "invalid_syntax"
<> ":InvalidYaml (Just (YamlParseException {yamlProblem = \"could not find expected ':'\", yamlContext = \"while scanning a simple key\", yamlProblemMark = YamlMark {yamlIndex = 130, yamlLine = 6, yamlColumn = 0}}))"
)
it "invalid timestamp" $ do
pendingWith "Aeson 2.x changes the message format"
migrationFromFile' "invalid_timestamp"
`shouldReturn` Left
( "Could not parse migration "
<> fp "invalid_timestamp"
<> ":Error in "
<> show (fp "invalid_timestamp")
<> ": unrecognized field found"
)
validFull :: Migration
validFull =
Migration
{ mTimestamp = Just ts
, mId = "valid_full"
, mDesc = Just "A valid full migration."
, mDeps = ["another_migration"]
, mApply = "CREATE TABLE test ( a int );"
, mRevert = Just "DROP TABLE test;"
}
validFullComments :: Migration
validFullComments =
Migration
{ mTimestamp = Just ts
, mId = "valid_full"
, mDesc = Just "A valid full migration."
, mDeps = ["another_migration"]
, mApply =
"\n-- Comment on a line\nCREATE TABLE test (\n a int -- comment inline\n);\n"
, mRevert = Just "DROP TABLE test;"
}
validFullColon :: Migration
validFullColon =
Migration
{ mTimestamp = Just ts
, mId = "valid_full"
, mDesc = Just "A valid full migration."
, mDeps = ["another_migration"]
, mApply =
"\n-- Comment on a line with a colon:\nCREATE TABLE test (\n a int\n);\n"
, mRevert = Just "DROP TABLE test;"
}
ts :: UTCTime
ts = read tsStr
tsStr :: String
tsStr = "2009-04-15 10:02:06 UTC"
validFullFractionalTs :: Migration
validFullFractionalTs =
Migration
{ mTimestamp = Just tsFractional
, mId = "valid_full_fractional_ts"
, mDesc = Just "A valid full migration with fractional seconds."
, mDeps = ["another_migration"]
, mApply = "CREATE TABLE test ( a int );"
, mRevert = Just "DROP TABLE test;"
}
tsFractional :: UTCTime
tsFractional = read "2009-04-15 10:02:06.123456 UTC"