@@ -2,1140 +2,647 @@ module Test.Main where
3import Prelude
3import Prelude
5−import Data.Array (head, drop)
5+import Control.Monad.Aff (Aff, launchAff, forkAff, delay, attempt)
6+import Control.Monad.Aff.AVar (AVAR, makeVar, makeVar', modifyVar, peekVar, putVar, takeVar)
7+import Control.Monad.Eff (Eff)
8+import Control.Monad.Eff.Class (liftEff)
9+import Control.Monad.Eff.Exception (EXCEPTION)
10+import Control.Monad.Eff.Now (NOW, now)
6−import Data.Date as Date
11+import Data.Date as Date
7−import Data.DateTime (DateTime(..), Time(..))
12+import Data.DateTime as DateTime
13+import Data.DateTime (DateTime(..), Time(..))
8−import Data.DateTime.Instant (toDateTime)
14+import Data.DateTime.Instant (toDateTime)
9−import Data.Either (Either(..))
15+import Data.Either (Either(..))
10−import Data.Enum (toEnum)
16+import Data.Enum (toEnum)
11−import Data.Maybe (Maybe(..), isNothing, maybe)
17+import Data.Maybe (Maybe(..), isNothing, maybe)
12−import Data.Time.Duration (Milliseconds(..))
18+import Data.Time.Duration (Milliseconds(..))
13−import Data.Traversable (traverse)
19+import Data.Traversable (traverse)
14−import Data.Tuple (Tuple(..), uncurry)
20+import Data.Tuple (Tuple(..), uncurry)
15−import Database.IndexedDB.Core (CursorDirection(..), Database, Index, ObjectStore, Transaction, TransactionMode(..))
16−import Database.IndexedDB.IDBCursor as IDBCursor
17−import Database.IndexedDB.IDBDatabase as IDBDatabase
18−import Database.IndexedDB.IDBFactory as IDBFactory
19−import Database.IndexedDB.IDBIndex as IDBIndex
20−import Database.IndexedDB.IDBKey (Key, none, toKey)
21−import Database.IndexedDB.IDBKeyRange as IDBKeyRange
22−import Database.IndexedDB.IDBObjectStore as IDBObjectStore
23−import Database.IndexedDB.IDBTransaction as IDBTransaction
24−import Effect (Effect)
25−import Effect.Aff (Aff, attempt, delay, forkAff, launchAff, launchAff_)
26−import Effect.Aff.AVar (AVar)
27−import Effect.Aff.AVar as AVar
28−import Effect.Class (liftEffect)
29−import Effect.Console (log)
30−import Effect.Exception (name)
31−import Effect.Now (now)
32−import Test.Spec (describe, it)
21+import Test.Spec (describe, describeOnly, it, itOnly)
33−import Test.Spec.Assertions (shouldEqual, fail)
22+import Test.Spec.Assertions (shouldEqual, fail)
34−import Test.Spec.Reporter (consoleReporter)
23+import Test.Spec.Mocha (MOCHA, runMocha)
35−import Test.Spec.Runner (runSpec)
24+
25+import Database.IndexedDB.Core
26+import Database.IndexedDB.IDBKey
27+import Database.IndexedDB.IDBDatabase as IDBDatabase
28+import Database.IndexedDB.IDBFactory as IDBFactory
29+import Database.IndexedDB.IDBIndex as IDBIndex
30+import Database.IndexedDB.IDBKeyRange as IDBKeyRange
31+import Database.IndexedDB.IDBObjectStore as IDBObjectStore
32+import Database.IndexedDB.IDBTransaction as IDBTransaction
39−launchAff' :: forall a. Aff a -> Effect Unit
35+infixr 7 Tuple as :+:
43−modifyAVar :: forall a. (a -> a) -> AVar a -> Aff Unit
37+launchAff' :: forall a e. Aff e a -> Eff (exception :: EXCEPTION | e) Unit
44−modifyAVar fn v = do
38+launchAff' aff =
45− val <- AVar.take v
39+ pure unit <* (launchAff aff)
48−main :: Effect Unit
41+main :: forall eff. Eff (now :: NOW, mocha :: MOCHA, idb :: IDB, exception :: EXCEPTION, avar :: AVAR | eff) Unit
49−main = do
42+main = runMocha do
50− launchAff_ $ runSpec [ consoleReporter ] $ do
51− describe "IDBFactory" do
43+ describe "IDBFactory" do
53 tearDown name version db = do
45 tearDown name version db = do
54 IDBDatabase.close db
46 IDBDatabase.close db
55 version' <- IDBFactory.deleteDatabase name
47 version' <- IDBFactory.deleteDatabase name
56− liftEffect $ log $ show version'
57 version' `shouldEqual` version
48 version' `shouldEqual` version
59− it "open default" do
50+ it "open default" do
61− name = "db-default"
51+ let name = "db-default"
62 version = 1
52 version = 1
63− db <- IDBFactory.open name Nothing
53+ db <- IDBFactory.open name Nothing
64− { onUpgradeNeeded: Nothing
54+ { onUpgradeNeeded : Nothing
65− , onBlocked: Nothing
55+ , onBlocked : Nothing
67− IDBDatabase.name db `shouldEqual` name
57+ IDBDatabase.name db `shouldEqual` name
68− tearDown name version db
58+ tearDown name version db
70− it "open specific version" do
74− db <- IDBFactory.open name (Just version)
75− { onUpgradeNeeded: Nothing
78− IDBDatabase.name db `shouldEqual` name
79− tearDown name version db
81− it "open specific version -> close -> open latest" do
61+ it "open specific version" do
83− name = "db-latest"
62+ let name = "db-specific"
84 version = 14
63 version = 14
85− db01 <- IDBFactory.open name (Just version)
64+ db <- IDBFactory.open name (Just version)
86− { onUpgradeNeeded: Nothing
65+ { onUpgradeNeeded : Nothing
87− , onBlocked: Nothing
66+ , onBlocked : Nothing
89− IDBDatabase.name db01 `shouldEqual` name
68+ IDBDatabase.name db `shouldEqual` name
90− IDBDatabase.close db01
91− db02 <- IDBFactory.open name Nothing
92− { onUpgradeNeeded: Nothing
95− tearDown name version db02
69+ tearDown name version db
72+ it "open specific version -> close -> open latest" do
73+ let name = "db-latest"
75+ db <- IDBFactory.open name (Just version)
76+ { onUpgradeNeeded : Nothing
77+ , onBlocked : Nothing
79+ IDBDatabase.name db `shouldEqual` name
81+ db <- IDBFactory.open name Nothing
82+ { onUpgradeNeeded : Nothing
83+ , onBlocked : Nothing
85+ tearDown name version db
97− it "open + onUpgradeNeed" do
88+ it "open + onUpgradeNeed" do
99− name = "db-upgrade-needed"
89+ let name = "db-upgrade-needed"
100 version = 1
90 version = 1
101− callback (Tuple varName varVersion) db _ { oldVersion } = do
91+ callback var db _ = do
102− _ <- launchAff $ modifyAVar (const $ IDBDatabase.name db) varName
92+ _ <- launchAff $ modifyVar (const $ IDBDatabase.name db) var
103− _ <- launchAff $ modifyAVar (const $ oldVersion) varVersion
104 pure unit
93 pure unit
105− varName <- AVar.new "-"
94+ var <- makeVar' "-"
106− varVersion <- AVar.new (-1)
107− db <- IDBFactory.open name Nothing
95+ db <- IDBFactory.open name Nothing
108− { onUpgradeNeeded: Just (callback (Tuple varName varVersion))
96+ { onUpgradeNeeded : Just (callback var)
109− , onBlocked: Nothing
97+ , onBlocked : Nothing
111− name' <- AVar.read varName
99+ name <- peekVar var
112− version' <- AVar.read varVersion
113− name' `shouldEqual` name
100+ name `shouldEqual` name
114− version' `shouldEqual` 0
115− tearDown name version db
101+ tearDown name version db
117− it "open + onBlocked" do
104+ it "open + onBlocked" do
119− name = "db-blocked"
105+ let name = "db-blocked"
120 version = 14
106 version = 14
121 callback var = do
107 callback var = do
122− _ <- launchAff $ modifyAVar (const $ "db-blocked") var
108+ _ <- launchAff $ modifyVar (const $ "db-blocked") var
123 pure unit
109 pure unit
125− var <- AVar.new "-"
111+ var <- makeVar' "-"
126− db01 <- IDBFactory.open name Nothing
112+ db01 <- IDBFactory.open name Nothing
127− { onUpgradeNeeded: Nothing
113+ { onUpgradeNeeded : Nothing
128− , onBlocked: Nothing
114+ , onBlocked : Nothing
130− _ <- forkAff do
116+ _ <- forkAff do
131− delay (Milliseconds 100.0)
117+ delay (Milliseconds 100.0)
132− IDBDatabase.close db01
118+ IDBDatabase.close db01
134− db02 <- IDBFactory.open name (Just version)
120+ db02 <- IDBFactory.open name (Just version)
135− { onUpgradeNeeded: Nothing
121+ { onUpgradeNeeded : Nothing
136− , onBlocked: Just (callback var)
122+ , onBlocked : Just (callback var)
138− name' <- AVar.read var
124+ name <- peekVar var
139− name' `shouldEqual` name
125+ name `shouldEqual` name
140− tearDown name version db02
126+ tearDown name version db02
142− describe "IDBKeyRange" do
128+ describe "IDBKeyRange" do
143− it "only(int)" do
129+ it "only(int)" do
145− key = 14
130+ let key = 14
146 range = IDBKeyRange.only key
131 range = IDBKeyRange.only key
147− IDBKeyRange.includes range (toKey key) `shouldEqual` true
132+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
149− it "only(string)" do
134+ it "only(string)" do
151− key = "patate"
135+ let key = "patate"
152 range = IDBKeyRange.only key
136 range = IDBKeyRange.only key
153− IDBKeyRange.includes range (toKey key) `shouldEqual` true
137+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
155− it "only(float)" do
139+ it "only(float)" do
157− key = 14.42
140+ let key = 14.42
158 range = IDBKeyRange.only key
141 range = IDBKeyRange.only key
159− IDBKeyRange.includes range (toKey key) `shouldEqual` true
142+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
161− it "only(date)" do
144+ it "only(date)" do
163− mkey = DateTime
145+ let mkey = DateTime
164 <$> (Date.canonicalDate <$> toEnum 2017 <*> toEnum 6 <*> toEnum 23)
146 <$> (Date.canonicalDate <$> toEnum 2017 <*> toEnum 6 <*> toEnum 23)
165 <*> (Time <$> toEnum 17 <*> toEnum 59 <*> toEnum 34 <*> toEnum 42)
147 <*> (Time <$> toEnum 17 <*> toEnum 59 <*> toEnum 34 <*> toEnum 42)
167− case mkey of
149+ case mkey of
168− Nothing ->
150+ Nothing ->
169− fail "unable to create datetime"
151+ fail "unable to create datetime"
170− Just key -> do
152+ Just key -> do
171− let range = IDBKeyRange.only key
153+ let range = IDBKeyRange.only key
172− IDBKeyRange.includes range (toKey key) `shouldEqual` true
154+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
174− it "only([int])" do
156+ it "only([int])" do
176− key = [ 14, 42 ]
157+ let key = [14, 42]
177 range = IDBKeyRange.only key
158 range = IDBKeyRange.only key
178− IDBKeyRange.includes range (toKey key) `shouldEqual` true
159+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
180− it "only([string])" do
161+ it "only([string])" do
182− key = [ "patate", "autruche" ]
162+ let key = ["patate", "autruche"]
183 range = IDBKeyRange.only key
163 range = IDBKeyRange.only key
184− IDBKeyRange.includes range (toKey key) `shouldEqual` true
164+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
186− it "lowerBound(14, open)" do
166+ it "lowerBound(14, open)" do
188− key = 14
167+ let key = 14
189− open = true
168+ open = true
190 range = IDBKeyRange.lowerBound key open
169 range = IDBKeyRange.lowerBound key open
191− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
170+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
192− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
171+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
193− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
172+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
195− it "lowerBound(14, close)" do
174+ it "lowerBound(14, close)" do
197− key = 14
175+ let key = 14
198− open = false
176+ open = false
199 range = IDBKeyRange.lowerBound key open
177 range = IDBKeyRange.lowerBound key open
200− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
178+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
201− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
179+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
202− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
180+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
204− it "upperBound(14, open)" do
182+ it "upperBound(14, open)" do
206− key = 14
183+ let key = 14
207− open = true
184+ open = true
208 range = IDBKeyRange.upperBound key open
185 range = IDBKeyRange.upperBound key open
209− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
186+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
210− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
187+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
211− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
188+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
213− it "upperBound(14, close)" do
190+ it "upperBound(14, close)" do
215− key = 14
191+ let key = 14
216− open = false
192+ open = false
217 range = IDBKeyRange.upperBound key open
193 range = IDBKeyRange.upperBound key open
218− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
194+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
219− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
195+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
220− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
196+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
222− it "bound(42, 14, open, open) => Nothing" do
198+ it "bound(42, 14, open, open) => Nothing" do
224− lower = 42
199+ let lower = 42
225− upper = 14
200+ upper = 14
226 lowerOpen = true
201 lowerOpen = true
227 upperOpen = true
202 upperOpen = true
228− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
203+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
229− isNothing mrange `shouldEqual` true
204+ isNothing mrange `shouldEqual` true
231− it "bound(14, 42, open, open)" do
206+ it "bound(14, 42, open, open)" do
233− lower = 14
207+ let lower = 14
234− upper = 42
208+ upper = 42
235 lowerOpen = true
209 lowerOpen = true
236 upperOpen = true
210 upperOpen = true
237− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
211+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
238− case mrange of
212+ case mrange of
239− Nothing ->
213+ Nothing ->
240− fail "invalid range provided"
214+ fail "invalid range provided"
241− Just range -> do
215+ Just range -> do
242− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
216+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
243− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
217+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
244− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
218+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
245− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
219+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
246− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
220+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
247− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
221+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
249− it "bound(14, 42, open, close)" do
223+ it "bound(14, 42, open, close)" do
251− lower = 14
224+ let lower = 14
252− upper = 42
225+ upper = 42
253 lowerOpen = true
226 lowerOpen = true
254 upperOpen = false
227 upperOpen = false
255− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
228+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
256− case mrange of
229+ case mrange of
257− Nothing ->
230+ Nothing ->
258− fail "invalid range provided"
231+ fail "invalid range provided"
259− Just range -> do
232+ Just range -> do
260− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
233+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
261− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
234+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
262− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
235+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
263− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
236+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
264− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
237+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
265− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
238+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
267− it "bound(14, 42, close, open)" do
240+ it "bound(14, 42, close, open)" do
269− lower = 14
241+ let lower = 14
270− upper = 42
242+ upper = 42
271 lowerOpen = false
243 lowerOpen = false
272 upperOpen = true
244 upperOpen = true
273− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
245+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
274− case mrange of
246+ case mrange of
275− Nothing ->
247+ Nothing ->
276− fail "invalid range provided"
248+ fail "invalid range provided"
277− Just range -> do
249+ Just range -> do
278− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
250+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
279− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
251+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
280− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
252+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
281− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
253+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
282− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
254+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
283− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
255+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
285− it "bound(14, 42, close, close)" do
257+ it "bound(14, 42, close, close)" do
287− lower = 14
258+ let lower = 14
288− upper = 42
259+ upper = 42
289 lowerOpen = false
260 lowerOpen = false
290 upperOpen = false
261 upperOpen = false
291− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
262+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
292− case mrange of
263+ case mrange of
293− Nothing ->
264+ Nothing ->
294− fail "invalid range provided"
265+ fail "invalid range provided"
295− Just range -> do
266+ Just range -> do
296− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
267+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
297− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
268+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
298− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
269+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
299− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
270+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
300− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
271+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
301− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
272+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
303− it "can access attributes of a range" do
274+ it "can access attributes of a range" do
304− let range = IDBKeyRange.lowerBound 14 false
275+ let range = IDBKeyRange.lowerBound 14 false
305− IDBKeyRange.lower range `shouldEqual` (Just $ toKey 14)
276+ IDBKeyRange.lower range `shouldEqual` (Just $ toKey 14)
306− IDBKeyRange.upper range `shouldEqual` none
277+ IDBKeyRange.upper range `shouldEqual` (Nothing :: Maybe Key)
307− IDBKeyRange.lowerOpen range `shouldEqual` false
278+ IDBKeyRange.lowerOpen range `shouldEqual` false
308− IDBKeyRange.upperOpen range `shouldEqual` true
279+ IDBKeyRange.upperOpen range `shouldEqual` true
310− describe "IDBDatabase" do
281+ describe "IDBDatabase" do
312 tearDown db = do
283 tearDown db = do
313 IDBDatabase.close db
284 IDBDatabase.close db
314 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
285 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
315 pure unit
286 pure unit
317 setup storeParams = do
288 setup storeParams = do
319− onUpgradeNeeded var db _ _ = launchAff' do
289+ let onUpgradeNeeded var db _ = launchAff' do
320− store <- IDBDatabase.createObjectStore db "store" storeParams
290+ store <- IDBDatabase.createObjectStore db "store" storeParams
321− _ <- AVar.put { db, store } var
291+ _ <- putVar var { db, store }
322− pure unit
292+ pure unit
324− var <- AVar.empty
294+ var <- makeVar
325− _ <- IDBFactory.open "db" Nothing
295+ db <- IDBFactory.open "db" Nothing
326− { onUpgradeNeeded: Just (onUpgradeNeeded var)
296+ { onUpgradeNeeded : Just (onUpgradeNeeded var)
327− , onBlocked: Nothing
297+ , onBlocked : Nothing
330− AVar.take var
300+ takeVar var
332− it "createObjectStore (keyPath: [], autoIncrement: true)" do
302+ it "createObjectStore (keyPath: [], autoIncrement: true)" do
333− { db, store } <- setup { keyPath: [], autoIncrement: true }
303+ { db, store } <- setup { keyPath: [], autoIncrement: true }
334− IDBObjectStore.name store `shouldEqual` "store"
304+ IDBObjectStore.name store `shouldEqual` "store"
335− IDBObjectStore.keyPath store `shouldEqual` []
305+ IDBObjectStore.keyPath store `shouldEqual` []
336− IDBObjectStore.autoIncrement store `shouldEqual` true
306+ IDBObjectStore.autoIncrement store `shouldEqual` true
337− IDBObjectStore.indexNames store `shouldEqual` []
307+ IDBObjectStore.indexNames store `shouldEqual` []
338− tearDown db
308+ tearDown db
340− it "createObjectStore (keyPath: [\"patate\"], autoIncrement: true)" do
310+ it "createObjectStore (keyPath: [\"patate\"], autoIncrement: true)" do
341− { db, store } <- setup { keyPath: [ "patate" ], autoIncrement: true }
311+ { db, store } <- setup { keyPath: ["patate"], autoIncrement: true }
342− IDBObjectStore.name store `shouldEqual` "store"
312+ IDBObjectStore.name store `shouldEqual` "store"
343− IDBObjectStore.keyPath store `shouldEqual` [ "patate" ]
313+ IDBObjectStore.keyPath store `shouldEqual` ["patate"]
344− IDBObjectStore.autoIncrement store `shouldEqual` true
314+ IDBObjectStore.autoIncrement store `shouldEqual` true
345− IDBObjectStore.indexNames store `shouldEqual` []
315+ IDBObjectStore.indexNames store `shouldEqual` []
346− tearDown db
316+ tearDown db
348− it "createObjectStore (keyPath: [\"a\", \"b\"], autoIncrement: false)" do
318+ it "createObjectStore (keyPath: [\"a\", \"b\"], autoIncrement: false)" do
349− { db, store } <- setup { keyPath: [ "patate", "autruche" ], autoIncrement: false }
319+ { db, store } <- setup { keyPath: ["patate", "autruche"], autoIncrement: false }
350− IDBObjectStore.name store `shouldEqual` "store"
320+ IDBObjectStore.name store `shouldEqual` "store"
351− IDBObjectStore.keyPath store `shouldEqual` [ "patate", "autruche" ]
321+ IDBObjectStore.keyPath store `shouldEqual` ["patate", "autruche"]
352− IDBObjectStore.autoIncrement store `shouldEqual` false
322+ IDBObjectStore.autoIncrement store `shouldEqual` false
353− IDBObjectStore.indexNames store `shouldEqual` []
323+ IDBObjectStore.indexNames store `shouldEqual` []
354− tearDown db
324+ tearDown db
356− it "deleteObjectStore" do
326+ it "deleteObjectStore" do
358− onUpgradeNeeded var db _ _ = launchAff' do
327+ let onUpgradeNeeded var db _ = launchAff' do
359 _ <- IDBDatabase.deleteObjectStore db "store"
328 _ <- IDBDatabase.deleteObjectStore db "store"
360− AVar.put true var
329+ putVar var true
363− { db } <- setup IDBDatabase.defaultParameters
364− IDBDatabase.close db
365− db' <- IDBFactory.open "db" (Just 999)
366− { onUpgradeNeeded: Just (onUpgradeNeeded var)
367− , onBlocked: Nothing
369− deleted <- AVar.take var
370− deleted `shouldEqual` true
373− describe "IDBObjectStore" do
331+ var <- makeVar
374− let
332+ { db, store } <- setup IDBObjectStore.defaultParameters
376− IDBDatabase.close db
333+ IDBDatabase.close db
377− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
380− setup { storeParams, onUpgradeNeeded } = do
382− onUpgradeNeeded' var db _ _ = launchAff' do
383− store <- IDBDatabase.createObjectStore db "store" storeParams
384− liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure store)
385− AVar.put { db, store } var
388− _ <- IDBFactory.open "db" Nothing
389− { onUpgradeNeeded: Just (onUpgradeNeeded' var)
334+ db <- IDBFactory.open "db" (Just 999) { onUpgradeNeeded : Just (onUpgradeNeeded var)
390− , onBlocked: Nothing
335+ , onBlocked : Nothing
392−
337+ deleted <- takeVar var
393− AVar.take var
338+ deleted `shouldEqual` true
396− date <- liftEffect $ toDateTime <$> now
398− { storeParams: { autoIncrement: true, keyPath: [] }
399− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
401− key <- IDBObjectStore.add store "patate" none
402− (toKey 1) `shouldEqual` key
405− key' <- IDBObjectStore.add store "patate" (Just 14)
406− (toKey 14) `shouldEqual` key'
409− key'' <- IDBObjectStore.add store "patate" (Just 14.42)
410− (toKey 14.42) `shouldEqual` key''
413− key''' <- IDBObjectStore.add store "patate" (Just "key")
414− (toKey "key") `shouldEqual` key'''
417− key'''' <- IDBObjectStore.add store "patate" (Just date)
418− (toKey date) `shouldEqual` key''''
421− key''''' <- IDBObjectStore.add store "patate" (Just $ toKey [ 14, 42 ])
422− (toKey [ 14, 42 ]) `shouldEqual` key'''''
428− { storeParams: IDBDatabase.defaultParameters
429− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
430− key <- IDBObjectStore.add store "patate" (Just 14)
431− _ <- IDBObjectStore.clear store
432− val <- IDBObjectStore.get store (IDBKeyRange.only key)
433− val `shouldEqual` (Nothing :: Maybe String)
439− { storeParams: IDBDatabase.defaultParameters
440− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
441− _ <- IDBObjectStore.add store "patate" (Just 14)
442− _ <- IDBObjectStore.add store "autruche" (Just 42)
443− n <- IDBObjectStore.count store Nothing
450− { storeParams: IDBDatabase.defaultParameters
451− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
452− key <- IDBObjectStore.add store "patate" (Just 14)
453− mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
454− mkey `shouldEqual` (Just key)
456− mkey' <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
457− mkey' `shouldEqual` none
461− it "getAllKeys()" do
463− { storeParams: IDBDatabase.defaultParameters
464− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
465− key1 <- IDBObjectStore.add store "patate" (Just 14)
466− key2 <- IDBObjectStore.add store "autruche" (Just 42)
467− key3 <- IDBObjectStore.add store 14 (Just 1337)
470− keys <- IDBObjectStore.getAllKeys store Nothing Nothing
471− keys `shouldEqual` [ key1, key2, key3 ]
474− keys' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
475− keys' `shouldEqual` [ key2, key3 ]
478− keys'' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
479− keys'' `shouldEqual` [ key1, key2 ]
482− keys''' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
483− keys''' `shouldEqual` [ key1, key2 ]
487− it "openCursor()" do
490− { onSuccess: const $ pure unit
491− , onError: show >>> fail >>> launchAff'
492− , onComplete: pure unit
495− { storeParams: IDBDatabase.defaultParameters
496− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
497− _ <- IDBObjectStore.openCursor store Nothing Next cb
498− _ <- IDBObjectStore.openCursor store Nothing NextUnique cb
499− _ <- IDBObjectStore.openCursor store Nothing Prev cb
500− _ <- IDBObjectStore.openCursor store Nothing PrevUnique cb
501− _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next cb
506− it "openKeyCursor()" do
509− { onSuccess: const $ pure unit
510− , onError: show >>> fail >>> launchAff'
511− , onComplete: pure unit
514− { storeParams: IDBDatabase.defaultParameters
515− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
516− _ <- IDBObjectStore.openKeyCursor store Nothing Next cb
517− _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique cb
518− _ <- IDBObjectStore.openKeyCursor store Nothing Prev cb
519− _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique cb
520− _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next cb
525− describe "IDBIndex" do
342+ describe "IDBObjectStore" do
527 tearDown db = do
344 tearDown db = do
528 IDBDatabase.close db
345 IDBDatabase.close db
529 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
346 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
530 pure unit
347 pure unit
532− setup
349+ setup { storeParams, onUpgradeNeeded } = do
534− . { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
535− , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
536− , values :: Array (Tuple value (Maybe Key))
537− , keyPath :: Array String
538− , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Effect Unit)
540− -> Aff { db :: Database, index :: Index, store :: ObjectStore }
541− setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
543− onUpgradeNeeded' var db tx _ = launchAff' do
350+ let onUpgradeNeeded' var db _ = launchAff' do
544− store <- IDBDatabase.createObjectStore db "store" storeParams
351+ store <- IDBDatabase.createObjectStore db "store" storeParams
545− _ <- traverse (uncurry (IDBObjectStore.add store)) values
546− index <- IDBObjectStore.createIndex store "index" keyPath indexParams
547− liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
352+ liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
548− AVar.put { db, index, store } var
353+ putVar var { db, store }
550− var <- AVar.empty
355+ var <- makeVar
551− _ <- IDBFactory.open "db" Nothing
356+ db <- IDBFactory.open "db" Nothing
552− { onUpgradeNeeded: Just (onUpgradeNeeded' var)
357+ { onUpgradeNeeded : Just (onUpgradeNeeded' var)
553− , onBlocked: Nothing
358+ , onBlocked : Nothing
557− it "returns an IDBIndex and the properties are set correctly" do
558− { db, index } <- setup
559− { storeParams: IDBDatabase.defaultParameters
560− , indexParams: IDBObjectStore.defaultParameters
561− , onUpgradeNeeded: Nothing
565− IDBIndex.name index `shouldEqual` "index"
566− IDBIndex.keyPath index `shouldEqual` []
567− IDBIndex.unique index `shouldEqual` IDBObjectStore.defaultParameters.unique
568− IDBIndex.multiEntry index `shouldEqual` IDBObjectStore.defaultParameters.multiEntry
571− it "attempt to create an index that requires unique values on an object store already contains duplicates" do
572− let onAbort var = launchAff' (AVar.put true var)
575− res <- attempt $ setup
576− { storeParams: IDBDatabase.defaultParameters
581− , keyPath: [ "indexedProperty" ]
583− [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
584− , { indexedProperty: "bar" } :+: (Just $ toKey 2)
586− , onUpgradeNeeded: Just $ \db tx _ -> launchAff' do
587− IDBTransaction.onAbort tx (onAbort txVar)
588− IDBDatabase.onAbort db (onAbort dbVar)
592− fail "expected abort"
594− shouldEqual true =<< AVar.take txVar
595− shouldEqual true =<< AVar.take dbVar
597− it "the index is usable right after being made" do
600− { keyPath: [ "key" ]
601− , autoIncrement: false
603− , indexParams: IDBObjectStore.defaultParameters
604− , keyPath: [ "indexedProperty" ]
606− [ { key: "key1", indexedProperty: "indexed_1" } :+: Nothing
607− , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
608− , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
610− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
611− val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
612− ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
616− it "empty keyPath" do
618− { storeParams: IDBDatabase.defaultParameters
619− , indexParams: IDBObjectStore.defaultParameters
622− [ "object_1" :+: (Just $ toKey 1)
623− , "object_2" :+: (Just $ toKey 2)
624− , "object_3" :+: (Just $ toKey 3)
626− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
627− val <- IDBIndex.get index (IDBKeyRange.only "object_3")
628− val `shouldEqual` (Just "object_3")
632− it "index can be valid keys [date]" do
633− date <- liftEffect $ toDateTime <$> now
636− { keyPath: [ "key" ]
637− , autoIncrement: false
639− , indexParams: IDBObjectStore.defaultParameters
642− [ { key: "date", i: (toKey date) } :+: Nothing
644− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
645− val <- IDBIndex.get index (IDBKeyRange.only date)
646− ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
650− it "index can be valid keys [num]" do
654− { keyPath: [ "key" ]
655− , autoIncrement: false
657− , indexParams: IDBObjectStore.defaultParameters
660− [ { key: "num", i: (toKey num) } :+: Nothing
662− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
663− val <- IDBIndex.get index (IDBKeyRange.only num)
664− ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
668− it "index can be valid keys [array]" do
669− let array = [ "patate", "autruche" ]
672− { keyPath: [ "key" ]
673− , autoIncrement: false
675− , indexParams: IDBObjectStore.defaultParameters
678− [ { key: "array", i: (toKey array) } :+: Nothing
680− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
681− val <- IDBIndex.get index (IDBKeyRange.only array)
682− ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
686− it "openKeyCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
687− res <- attempt $ setup
688− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
689− , indexParams: IDBObjectStore.defaultParameters
690− , keyPath: [ "indexedProperty" ]
692− [ { key: 14, indexedProperty: "patate" } :+: Nothing
694− , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
697− { onSuccess: const $ pure unit
698− , onError: show >>> fail >>> launchAff'
699− , onComplete: pure unit
701− IDBTransaction.onAbort tx (pure unit)
702− IDBDatabase.onAbort db (pure unit)
703− IDBTransaction.abort tx
704− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
706− Right _ -> fail "expected InvalidStateError"
707− Left err -> name err `shouldEqual` "InvalidStateError"
710− Right _ -> fail "expected InvalidStateError"
713− it "openKeyCursor() - throw TransactionInactiveError on aborted transaction" do
715− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
716− , indexParams: IDBObjectStore.defaultParameters
717− , keyPath: [ "indexedProperty" ]
719− [ { key: 14, indexedProperty: "patate" } :+: Nothing
721− , onUpgradeNeeded: Nothing
725− { onSuccess: const $ pure unit
726− , onError: show >>> fail >>> launchAff'
727− , onComplete: pure unit
729− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
730− store <- IDBTransaction.objectStore tx "store"
731− index <- IDBObjectStore.index store "index"
732− IDBTransaction.onAbort tx (pure unit)
733− IDBTransaction.abort tx
734− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
736− Right _ -> fail "expected TransactionInactiveError"
737− Left err -> name err `shouldEqual` "TransactionInactiveError"
741− it "openKeyCursor() - throw InvalidStateError when the index is deleted" do
743− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
744− , indexParams: IDBObjectStore.defaultParameters
745− , keyPath: [ "indexedProperty" ]
747− [ { key: 14, indexedProperty: "patate" } :+: Nothing
749− , onUpgradeNeeded: Just $ \_ tx index -> launchAff' do
752− { onSuccess: const $ pure unit
753− , onError: show >>> fail >>> launchAff'
754− , onComplete: pure unit
756− store <- IDBTransaction.objectStore tx "store"
757− IDBObjectStore.deleteIndex store "index"
758− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
760− Right _ -> fail "expected InvalidStateError"
761− Left err -> name err `shouldEqual` "InvalidStateError"
766− it "openCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
767− res <- attempt $ setup
768− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
769− , indexParams: IDBObjectStore.defaultParameters
770− , keyPath: [ "indexedProperty" ]
772− [ { key: 14, indexedProperty: "patate" } :+: Nothing
774− , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
777− { onSuccess: const $ pure unit
778− , onError: show >>> fail >>> launchAff'
779− , onComplete: pure unit
781− IDBTransaction.onAbort tx (pure unit)
782− IDBDatabase.onAbort db (pure unit)
783− IDBTransaction.abort tx
784− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
786− Right _ -> fail "expected InvalidStateError"
787− Left err -> name err `shouldEqual` "InvalidStateError"
790− Right _ -> fail "expected InvalidStateError"
793− it "openCursor() - throw TransactionInactiveError on aborted transaction" do
795− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
796− , indexParams: IDBObjectStore.defaultParameters
797− , keyPath: [ "indexedProperty" ]
799− [ { key: 14, indexedProperty: "patate" } :+: Nothing
801− , onUpgradeNeeded: Nothing
805− { onSuccess: const $ pure unit
806− , onError: show >>> fail >>> launchAff'
807− , onComplete: pure unit
809− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
810− store <- IDBTransaction.objectStore tx "store"
811− index <- IDBObjectStore.index store "index"
812− IDBTransaction.onAbort tx (pure unit)
813− IDBTransaction.abort tx
814− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
816− Right _ -> fail "expected TransactionInactiveError"
817− Left err -> name err `shouldEqual` "TransactionInactiveError"
821− it "openCursor() - throw InvalidStateError when the index is deleted" do
823− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
824− , indexParams: IDBObjectStore.defaultParameters
825− , keyPath: [ "indexedProperty" ]
827− [ { key: 14, indexedProperty: "patate" } :+: Nothing
829− , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
832− { onSuccess: const $ pure unit
833− , onError: show >>> fail >>> launchAff'
834− , onComplete: pure unit
836− store <- IDBTransaction.objectStore tx "store"
837− IDBObjectStore.deleteIndex store "index"
838− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
840− Right _ -> fail "expected InvalidStateError"
841− Left err -> name err `shouldEqual` "InvalidStateError"
844− tearDown db
361+ takeVar var
846− it "getKey() - multiEntry - adding keys" do
363+ it "add()" do
847− { db } <- setup
364+ date <- liftEff $ toDateTime <$> now
848− { storeParams: IDBDatabase.defaultParameters
849− , indexParams: { unique: false, multiEntry: true }
850− , keyPath: [ "name" ]
852− [ { name: [ "patate", "autruche" ] } :+: (Just $ toKey 1)
853− , { name: [ "bob" ] } :+: (Just $ toKey 2)
855− , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
856− key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
857− key `shouldEqual` (Just $ toKey 1)
859− key' <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
860− key' `shouldEqual` (Just $ toKey 1)
862− key'' <- IDBIndex.getKey index (IDBKeyRange.only "bob")
863− key'' `shouldEqual` (Just $ toKey 2)
867− it "get() - returns the record with the first key in the range" do
868− { db } <- setup
365+ { db } <- setup
869− { storeParams: { keyPath: [ "key" ], autoIncrement: false }
366+ { storeParams: { autoIncrement: true, keyPath: [] }
367+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
369+ key <- IDBObjectStore.add store "patate" (Nothing :: Maybe Key)
370+ (toKey 1) `shouldEqual` key
373+ key <- IDBObjectStore.add store "patate" (Just 14)
374+ (toKey 14) `shouldEqual` key
377+ key <- IDBObjectStore.add store "patate" (Just 14.42)
378+ (toKey 14.42) `shouldEqual` key
381+ key <- IDBObjectStore.add store "patate" (Just "key")
382+ (toKey "key") `shouldEqual` key
385+ key <- IDBObjectStore.add store "patate" (Just date)
386+ (toKey date) `shouldEqual` key
389+ key <- IDBObjectStore.add store "patate" (Just $ toKey [14, 42])
390+ (toKey [14, 42]) `shouldEqual` key
396+ { storeParams: IDBObjectStore.defaultParameters
397+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
398+ key <- IDBObjectStore.add store "patate" (Just 14)
399+ _ <- IDBObjectStore.clear store
400+ val <- IDBObjectStore.get store (IDBKeyRange.only key)
401+ val `shouldEqual` (Nothing :: Maybe String)
407+ { storeParams: IDBObjectStore.defaultParameters
408+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
409+ key <- IDBObjectStore.add store "patate" (Just 14)
410+ key <- IDBObjectStore.add store "autruche" (Just 42)
411+ n <- IDBObjectStore.count store Nothing
870− , indexParams: IDBObjectStore.defaultParameters
418+ { storeParams: IDBObjectStore.defaultParameters
871− , keyPath: [ "indexedProperty" ]
419+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
872− , values:
420+ key <- IDBObjectStore.add store "patate" (Just 14)
873− [ { key: 14, indexedProperty: "patate" } :+: Nothing
421+ mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
874− , { key: 42, indexedProperty: "autruche" } :+: Nothing
422+ mkey `shouldEqual` (Just key)
875− , { key: 1337, indexedProperty: "baguette" } :+: Nothing
423+
876− ]
424+ mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
877− , onUpgradeNeeded: Nothing
425+ mkey `shouldEqual` (Nothing :: Maybe Key)
880− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
428+
881− store <- IDBTransaction.objectStore tx "store"
429+ it "getAllKeys()" do
431+ { storeParams: IDBObjectStore.defaultParameters
432+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
433+ key1 <- IDBObjectStore.add store "patate" (Just 14)
434+ key2 <- IDBObjectStore.add store "autruche" (Just 42)
435+ key3 <- IDBObjectStore.add store 14 (Just 1337)
438+ keys <- IDBObjectStore.getAllKeys store Nothing Nothing
439+ keys `shouldEqual` [key1, key2, key3]
442+ keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
443+ keys `shouldEqual` [key2, key3]
446+ keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
447+ keys `shouldEqual` [key1, key2]
450+ keys <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
451+ keys `shouldEqual` [key1, key2]
455+ it "openCursor()" do
457+ { storeParams: IDBObjectStore.defaultParameters
458+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
459+ _ <- IDBObjectStore.openCursor store Nothing Next
460+ _ <- IDBObjectStore.openCursor store Nothing NextUnique
882− index <- IDBObjectStore.index store "index"
461+ _ <- IDBObjectStore.openCursor store Nothing Prev
883− val <- IDBIndex.get index (IDBKeyRange.lowerBound "autruche" false)
462+ _ <- IDBObjectStore.openCursor store Nothing PrevUnique
884− ((\r -> r.key) <$> val) `shouldEqual` (Just 42)
463+ _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next
468+ it "openKeyCursor()" do
470+ { storeParams: IDBObjectStore.defaultParameters
471+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
472+ _ <- IDBObjectStore.openKeyCursor store Nothing Next
473+ _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique
474+ _ <- IDBObjectStore.openKeyCursor store Nothing Prev
475+ _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique
476+ _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next
886− tearDown db
479+ tearDown db
888− describe "IDBCursor" do
481+ describe "IDBIndex" do
890 tearDown db = do
483 tearDown db = do
891 IDBDatabase.close db
484 IDBDatabase.close db
892 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
485 _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
893 pure unit
486 pure unit
896− :: forall value
488+ setup :: forall value e e'.
897− . { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
489+ { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
898− , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
490+ , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
899− , values :: Array (Tuple value (Maybe Key))
491+ , values :: Array (Tuple value (Maybe Key))
900− , keyPath :: Array String
492+ , keyPath :: Array String
901− , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Effect Unit)
493+ , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
903− -> Aff { db :: Database, index :: Index, store :: ObjectStore }
494+ } -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
904 setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
495 setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
906− onUpgradeNeeded' var db tx _ = launchAff' do
496+ let onUpgradeNeeded' var db tx = launchAff' do
907− store <- IDBDatabase.createObjectStore db "store" storeParams
497+ store <- IDBDatabase.createObjectStore db "store" storeParams
908− _ <- traverse (uncurry (IDBObjectStore.add store)) values
498+ _ <- traverse (uncurry (IDBObjectStore.add store)) values
909− index <- IDBObjectStore.createIndex store "index" keyPath indexParams
499+ index <- IDBObjectStore.createIndex store "index" keyPath indexParams
910− liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
500+ liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
911− AVar.put { db, index, store } var
501+ putVar var { db, index, store }
913− var <- AVar.empty
503+ var <- makeVar
914− db <- IDBFactory.open "db" Nothing
504+ db <- IDBFactory.open "db" Nothing
915− { onUpgradeNeeded: Just (onUpgradeNeeded' var)
505+ { onUpgradeNeeded : Just (onUpgradeNeeded' var)
916− , onBlocked: Nothing
506+ , onBlocked : Nothing
920− it "continue() - iterate to the next record" do
922− { storeParams: IDBDatabase.defaultParameters
923− , indexParams: IDBObjectStore.defaultParameters
926− [ "cupcake" :+: (Just $ toKey 4)
927− , "pancake" :+: (Just $ toKey 2)
928− , "pie" :+: (Just $ toKey 1)
929− , "pie" :+: (Just $ toKey 3)
931− , onUpgradeNeeded: Nothing
935− { onComplete: launchAff' do
938− , onError: \error -> launchAff' do
939− fail $ "unexpected error: " <> show error
941− , onSuccess: \cursor -> launchAff' do
942− vals <- AVar.take vvals
943− pure (IDBCursor.value cursor) >>= shouldEqual (maybe "" _.v $ head vals)
944− IDBCursor.primaryKey cursor >>= shouldEqual (maybe (toKey 0) _.k $ head vals)
945− AVar.put (drop 1 vals) vvals
946− IDBCursor.continue cursor none
950− [ { v: "pie", k: toKey 1 }
951− , { v: "pancake", k: toKey 2 }
952− , { v: "pie", k: toKey 3 }
953− , { v: "cupcake", k: toKey 4 }
955− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
956− store <- IDBTransaction.objectStore tx "store"
957− IDBObjectStore.openCursor store Nothing Next (cb vdone vvals)
961− it "continue() - attempt to iterate in the wrong direction" do
963− { storeParams: IDBDatabase.defaultParameters
964− , indexParams: IDBObjectStore.defaultParameters
967− [ "cupcake" :+: (Just $ toKey 4)
968− , "pancake" :+: (Just $ toKey 2)
969− , "pie" :+: (Just $ toKey 1)
970− , "pie" :+: (Just $ toKey 3)
972− , onUpgradeNeeded: Nothing
976− { onComplete: launchAff' do
977− fail $ "shouldn't complete"
979− , onError: \error -> launchAff' do
980− fail $ "unexpected error: " <> show error
982− , onSuccess: \cursor -> launchAff' do
983− res <- attempt $ IDBCursor.continue cursor (Just 1)
986− name err `shouldEqual` "DataError"
989− fail "expected continue to fail"
991− vdone <- AVar.empty
508+ takeVar var
992− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
509+
993− store <- IDBTransaction.objectStore tx "store"
510+
994− IDBObjectStore.openCursor store Nothing Next (cb vdone)
511+ it "returns an IDBIndex and the properties are set correctly" do
998− it "advance() - iterate cursor number of times specified by count" do
999− { db } <- setup
512+ { db, index } <- setup
1000− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
513+ { storeParams : IDBObjectStore.defaultParameters
1001− , indexParams: IDBObjectStore.defaultParameters
514+ , indexParams : IDBIndex.defaultParameters
515+ , onUpgradeNeeded : Nothing
1002− , keyPath: []
516+ , keyPath : []
1003− , values:
517+ , values : []
1004− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
518+ }
1005− , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
519+ IDBIndex.name index `shouldEqual` "index"
1006− , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
520+ IDBIndex.keyPath index `shouldEqual` []
1007− , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
521+ IDBIndex.unique index `shouldEqual` IDBIndex.defaultParameters.unique
1008− ]
522+ IDBIndex.multiEntry index `shouldEqual` IDBIndex.defaultParameters.multiEntry
1009− , onUpgradeNeeded: Nothing
523+ tearDown db
1011− let
525+ it "attempt to create an index that requires unique values on an object store already contains duplicates" do
1012− cb vdone vjump =
526+ let onAbort var = launchAff' (putVar var true)
1013− { onComplete: launchAff' do
527+ txVar <- makeVar
1014− AVar.put unit vdone
528+ dbVar <- makeVar
1016− , onError: \error -> launchAff' do
1017− fail $ "unexpected error: " <> show error
1019− , onSuccess: \cursor -> launchAff' do
1020− jump <- AVar.take vjump
1022− IDBCursor.advance cursor 3
1024− let value = IDBCursor.value cursor
1025− value.pKey `shouldEqual` "pkey_3"
1026− value.iKey `shouldEqual` "ikey_3"
1027− IDBCursor.continue cursor none
1028− AVar.put false vjump
1030− vdone <- AVar.empty
1031− vjump <- AVar.new true
1032− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
1033− store <- IDBTransaction.objectStore tx "store"
1034− IDBObjectStore.openCursor store Nothing Next (cb vdone vjump)
1038− it "delete() - remove a record from the object store" do
1040− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1041− , indexParams: IDBObjectStore.defaultParameters
1044− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1045− , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
1046− , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
1047− , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
1049− , onUpgradeNeeded: Nothing
1053− { onComplete: launchAff' do
1054− mval <- map _.pKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
1055− mval `shouldEqual` (Nothing :: Maybe String)
1056− AVar.put unit vdone
1058− , onError: \error -> launchAff' do
1059− fail $ "unexpected error: " <> show error
1061− , onSuccess: \cursor -> launchAff' do
1062− let value = IDBCursor.value cursor
1063− value.pKey `shouldEqual` "pkey_0"
1064− IDBCursor.delete cursor
1065− IDBCursor.advance cursor 4
1067− vdone <- AVar.empty
1068− tx <- IDBDatabase.transaction db [ "store" ] ReadWrite
1069− store <- IDBTransaction.objectStore tx "store"
1070− IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1074− it "update() - modify a record in the object store" do
1076− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1077− , indexParams: IDBObjectStore.defaultParameters
1080− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1082− , onUpgradeNeeded: Nothing
1086− { onComplete: launchAff' do
1087− mval <- map _.iKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
1088− mval `shouldEqual` (Just "patate")
1089− AVar.put unit vdone
1091− , onError: \error -> launchAff' do
1092− fail $ "unexpected error: " <> show error
1094− , onSuccess: \cursor -> launchAff' do
1095− let value = IDBCursor.value cursor
1096− value.pKey `shouldEqual` "pkey_0"
1097− key <- IDBCursor.update cursor { pKey: "pkey_0", iKey: "patate" }
1098− key `shouldEqual` toKey "pkey_0"
1099− IDBCursor.advance cursor 4
1101− vdone <- AVar.empty
1102− tx <- IDBDatabase.transaction db [ "store" ] ReadWrite
1103− store <- IDBTransaction.objectStore tx "store"
1104− IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1108− it "update() - throw ReadOnlyError after update on ReadOnly transaction" do
1109− { db } <- setup
529+ res <- attempt $ setup
1110− { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
530+ { storeParams : IDBObjectStore.defaultParameters
1111− , indexParams: IDBObjectStore.defaultParameters
531+ , indexParams : { unique : true
532+ , multiEntry : false
1112− , keyPath: []
534+ , keyPath : ["indexedProperty"]
1113− , values:
535+ , values : [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
1114− [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
536+ , { indexedProperty: "bar" } :+: (Just $ toKey 2)
1116− , onUpgradeNeeded: Nothing
538+ , onUpgradeNeeded : Just $ \db tx _ -> launchAff' do
1117− }
539+ IDBTransaction.onAbort tx (onAbort txVar)
1118− let
540+ IDBDatabase.onAbort db (onAbort dbVar)
1120− { onComplete: launchAff' do
1121− fail $ "shouldn't complete"
1123− , onError: \error -> launchAff' do
1124− fail $ "unexpected error: " <> show error
1126− , onSuccess: \cursor -> launchAff' do
1127− res <- attempt $ IDBCursor.update cursor "patate"
1128− case res of
542+ case res of
1130− name err `shouldEqual` "ReadOnlyError"
1131− AVar.put unit vdone
1132− Right _ ->
543+ Right _ ->
1133− fail $ "expected ReadOnlyError"
544+ fail "expected abort"
1135− vdone <- AVar.empty
546+ shouldEqual true =<< takeVar txVar
1136− tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
547+ shouldEqual true =<< takeVar dbVar
1137− store <- IDBTransaction.objectStore tx "store"
548+
1138− IDBObjectStore.openCursor store Nothing Next (cb vdone)
549+
1139− AVar.take vdone
550+ it "the index is usable right after being made" do
552+ { storeParams : { keyPath : ["key"]
553+ , autoIncrement : false
555+ , indexParams : IDBIndex.defaultParameters
556+ , keyPath : ["indexedProperty"]
557+ , values : [ { key: "key1", indexedProperty: "indexed_1" } :+: Nothing
558+ , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
559+ , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
561+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
562+ val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
563+ ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
567+ it "empty keyPath" do
569+ { storeParams : IDBObjectStore.defaultParameters
570+ , indexParams : IDBIndex.defaultParameters
572+ , values : [ "object_1" :+: (Just $ toKey 1)
573+ , "object_2" :+: (Just $ toKey 2)
574+ , "object_3" :+: (Just $ toKey 3)
576+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
577+ val <- IDBIndex.get index (IDBKeyRange.only "object_3")
578+ val `shouldEqual` (Just "object_3")
582+ it "index can be valid keys [date]" do
583+ date <- liftEff $ toDateTime <$> now
585+ { storeParams : { keyPath: ["key"]
586+ , autoIncrement: false
588+ , indexParams : IDBIndex.defaultParameters
590+ , values : [ { key: "date", i: (toKey date) } :+: Nothing
592+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
593+ val <- IDBIndex.get index (IDBKeyRange.only date)
594+ ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
598+ it "index can be valid keys [num]" do
601+ { storeParams : { keyPath: ["key"]
602+ , autoIncrement: false
604+ , indexParams : IDBIndex.defaultParameters
606+ , values : [ { key: "num", i: (toKey num) } :+: Nothing
608+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
609+ val <- IDBIndex.get index (IDBKeyRange.only num)
610+ ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
614+ it "index can be valid keys [array]" do
615+ let array = ["patate", "autruche"]
617+ { storeParams : { keyPath: ["key"]
618+ , autoIncrement: false
620+ , indexParams : IDBIndex.defaultParameters
622+ , values : [ { key: "array", i: (toKey array) } :+: Nothing
624+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
625+ val <- IDBIndex.get index (IDBKeyRange.only array)
626+ ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
630+ it "multiEntry - adding keys" do
632+ { storeParams : IDBObjectStore.defaultParameters
633+ , indexParams : { unique: false, multiEntry: true }
634+ , keyPath : ["name"]
635+ , values : [ { name: ["patate", "autruche"] } :+: (Just $ toKey 1)
636+ , { name: ["bob"] } :+: (Just $ toKey 2)
638+ , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
639+ key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
640+ key `shouldEqual` (Just $ toKey 1)
642+ key <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
643+ key `shouldEqual` (Just $ toKey 1)
645+ key <- IDBIndex.getKey index (IDBKeyRange.only "bob")
646+ key `shouldEqual` (Just $ toKey 2)
1140− tearDown db
648+ tearDown db