@@ -2,1079 +2,1139 @@ module Test.Main where
22
33import Prelude
44
5−import Control.Monad.Aff (Aff, launchAff, launchAff_, forkAff, delay, attempt)
6−import Control.Monad.Aff.AVar (AVAR, AVar, makeVar, makeEmptyVar, readVar, putVar, takeVar)
7−import Control.Monad.Aff.Console (log)
8−import Control.Monad.Eff (Eff)
9−import Control.Monad.Eff.Class (liftEff)
10−import Control.Monad.Eff.Exception (EXCEPTION, name)
11−import Control.Monad.Eff.Now (NOW, now)
12−import Data.Date as Date
13−import Data.DateTime as DateTime
14−import Data.DateTime (DateTime(..), Time(..))
15−import Data.DateTime.Instant (toDateTime)
16−import Data.Either (Either(..))
17−import Data.Enum (toEnum)
18−import Data.Maybe (Maybe(..), isNothing, maybe)
19−import Data.Array (head, drop)
20−import Data.Time.Duration (Milliseconds(..))
21−import Data.Traversable (traverse)
22−import Data.Tuple (Tuple(..), uncurry)
23−import Test.Spec (describe, describeOnly, it)
24−import Test.Spec.Assertions (shouldEqual, fail)
25−import Test.Spec.Mocha (MOCHA, runMocha)
26−
27−import Database.IndexedDB.Core
28−import Database.IndexedDB.IDBKey
29−import Database.IndexedDB.IDBCursor as IDBCursor
30−import Database.IndexedDB.IDBDatabase as IDBDatabase
31−import Database.IndexedDB.IDBFactory as IDBFactory
32−import Database.IndexedDB.IDBIndex as IDBIndex
33−import Database.IndexedDB.IDBKeyRange as IDBKeyRange
34−import Database.IndexedDB.IDBObjectStore as IDBObjectStore
35−import Database.IndexedDB.IDBTransaction as IDBTransaction
36−
5+import Data.Array (head, drop)
6+import Data.Date as Date
7+import Data.DateTime (DateTime(..), Time(..))
8+import Data.DateTime.Instant (toDateTime)
9+import Data.Either (Either(..))
10+import Data.Enum (toEnum)
11+import Data.Maybe (Maybe(..), isNothing, maybe)
12+import Data.Time.Duration (Milliseconds(..))
13+import Data.Traversable (traverse)
14+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)
33+import Test.Spec.Assertions (shouldEqual, fail)
34+import Test.Spec.Mocha (runMocha)
3735
3836infixr 7 Tuple as :+:
3937
40−launchAff' :: forall a e. Aff e a -> Eff e Unit
38+launchAff' :: forall a. Aff a -> Effect Unit
4139launchAff' =
42− launchAff_
40+ launchAff_ <<< void
41+
42+modifyAVar :: forall a. (a -> a) -> AVar a -> Aff Unit
43+modifyAVar fn v = do
44+ val <- AVar.take v
45+ AVar.put (fn val) v
46+
47+main :: Effect Unit
48+main =
49+ runMocha
50+ do
51+ describe "IDBFactory" do
52+ let
53+ tearDown name version db = do
54+ IDBDatabase.close db
55+ version' <- IDBFactory.deleteDatabase name
56+ liftEffect $ log $ show version'
57+ version' `shouldEqual` version
58+
59+ it "open default" do
60+ let
61+ name = "db-default"
62+ version = 1
63+ db <- IDBFactory.open name Nothing
64+ { onUpgradeNeeded: Nothing
65+ , onBlocked: Nothing
66+ }
67+ IDBDatabase.name db `shouldEqual` name
68+ tearDown name version db
69+
70+ it "open specific version" do
71+ let
72+ name = "db-specific"
73+ version = 14
74+ db <- IDBFactory.open name (Just version)
75+ { onUpgradeNeeded: Nothing
76+ , onBlocked: Nothing
77+ }
78+ IDBDatabase.name db `shouldEqual` name
79+ tearDown name version db
80+
81+ it "open specific version -> close -> open latest" do
82+ let
83+ name = "db-latest"
84+ version = 14
85+ db01 <- IDBFactory.open name (Just version)
86+ { onUpgradeNeeded: Nothing
87+ , onBlocked: Nothing
88+ }
89+ IDBDatabase.name db01 `shouldEqual` name
90+ IDBDatabase.close db01
91+ db02 <- IDBFactory.open name Nothing
92+ { onUpgradeNeeded: Nothing
93+ , onBlocked: Nothing
94+ }
95+ tearDown name version db02
96+
97+ it "open + onUpgradeNeed" do
98+ let
99+ name = "db-upgrade-needed"
100+ version = 1
101+ callback (Tuple varName varVersion) db _ { oldVersion } = do
102+ _ <- launchAff $ modifyAVar (const $ IDBDatabase.name db) varName
103+ _ <- launchAff $ modifyAVar (const $ oldVersion) varVersion
104+ pure unit
105+ varName <- AVar.new "-"
106+ varVersion <- AVar.new (-1)
107+ db <- IDBFactory.open name Nothing
108+ { onUpgradeNeeded: Just (callback (Tuple varName varVersion))
109+ , onBlocked: Nothing
110+ }
111+ name' <- AVar.read varName
112+ version' <- AVar.read varVersion
113+ name' `shouldEqual` name
114+ version' `shouldEqual` 0
115+ tearDown name version db
116+
117+ it "open + onBlocked" do
118+ let
119+ name = "db-blocked"
120+ version = 14
121+ callback var = do
122+ _ <- launchAff $ modifyAVar (const $ "db-blocked") var
123+ pure unit
124+
125+ var <- AVar.new "-"
126+ db01 <- IDBFactory.open name Nothing
127+ { onUpgradeNeeded: Nothing
128+ , onBlocked: Nothing
129+ }
130+ _ <- forkAff do
131+ delay (Milliseconds 100.0)
132+ IDBDatabase.close db01
43133
134+ db02 <- IDBFactory.open name (Just version)
135+ { onUpgradeNeeded: Nothing
136+ , onBlocked: Just (callback var)
137+ }
138+ name' <- AVar.read var
139+ name' `shouldEqual` name
140+ tearDown name version db02
141+
142+ describe "IDBKeyRange" do
143+ it "only(int)" do
144+ let
145+ key = 14
146+ range = IDBKeyRange.only key
147+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
44148
45−modifyVar :: forall eff a. (a -> a) -> AVar a -> Aff (avar :: AVAR | eff) Unit
46−modifyVar fn v = do
47− val <- takeVar v
48− putVar (fn val) v
149+ it "only(string)" do
150+ let
151+ key = "patate"
152+ range = IDBKeyRange.only key
153+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
49154
155+ it "only(float)" do
156+ let
157+ key = 14.42
158+ range = IDBKeyRange.only key
159+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
50160
51−-- main :: forall eff. Eff (now :: NOW, mocha :: MOCHA, idb :: IDB, exception :: EXCEPTION, avar :: AVAR | eff) Unit
52−main = runMocha do
53− describe "IDBFactory" do
54− let
55− tearDown name version db = do
56− IDBDatabase.close db
57− version' <- IDBFactory.deleteDatabase name
58− version' `shouldEqual` version
59−
60− it "open default" do
61− let name = "db-default"
62− version = 1
63− db <- IDBFactory.open name Nothing
64− { onUpgradeNeeded : Nothing
65− , onBlocked : Nothing
66− }
67− IDBDatabase.name db `shouldEqual` name
68− tearDown name version db
69−
70−
71− it "open specific version" do
72− let name = "db-specific"
73− version = 14
74− db <- IDBFactory.open name (Just version)
75− { onUpgradeNeeded : Nothing
76− , onBlocked : Nothing
77− }
78− IDBDatabase.name db `shouldEqual` name
79− tearDown name version db
80−
81−
82− it "open specific version -> close -> open latest" do
83− let name = "db-latest"
84− version = 14
85− db01 <- IDBFactory.open name (Just version)
86− { onUpgradeNeeded : Nothing
87− , onBlocked : Nothing
88− }
89− IDBDatabase.name db01 `shouldEqual` name
90− IDBDatabase.close db01
91− db02 <- IDBFactory.open name Nothing
92− { onUpgradeNeeded : Nothing
93− , onBlocked : Nothing
94− }
95− tearDown name version db02
96−
97− it "open + onUpgradeNeed" do
98− let name = "db-upgrade-needed"
99− version = 1
100− callback (Tuple varName varVersion) db _ { oldVersion } = do
101− _ <- launchAff $ modifyVar (const $ IDBDatabase.name db) varName
102− _ <- launchAff $ modifyVar (const $ oldVersion) varVersion
103− pure unit
104− varName <- makeVar "-"
105− varVersion <- makeVar (-1)
106− db <- IDBFactory.open name Nothing
107− { onUpgradeNeeded : Just (callback (Tuple varName varVersion))
108− , onBlocked : Nothing
109− }
110− name' <- readVar varName
111− version' <- readVar varVersion
112− name' `shouldEqual` name
113− version' `shouldEqual` 0
114− tearDown name version db
115−
116− it "open + onBlocked" do
117− let name = "db-blocked"
118− version = 14
119− callback var = do
120− _ <- launchAff $ modifyVar (const $ "db-blocked") var
121− pure unit
161+ it "only(date)" do
162+ let
163+ mkey = DateTime
164+ <$> (Date.canonicalDate <$> toEnum 2017 <*> toEnum 6 <*> toEnum 23)
165+ <*> (Time <$> toEnum 17 <*> toEnum 59 <*> toEnum 34 <*> toEnum 42)
166+
167+ case mkey of
168+ Nothing ->
169+ fail "unable to create datetime"
170+ Just key -> do
171+ let range = IDBKeyRange.only key
172+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
173+
174+ it "only([int])" do
175+ let
176+ key = [ 14, 42 ]
177+ range = IDBKeyRange.only key
178+ IDBKeyRange.includes range (toKey key) `shouldEqual` true
122179
123− var <- makeVar "-"
124− db01 <- IDBFactory.open name Nothing
125− { onUpgradeNeeded : Nothing
126− , onBlocked : Nothing
127− }
128− _ <- forkAff do
129− delay (Milliseconds 100.0)
130− IDBDatabase.close db01
131−
132− db02 <- IDBFactory.open name (Just version)
133− { onUpgradeNeeded : Nothing
134− , onBlocked : Just (callback var)
135− }
136− name' <- readVar var
137− name' `shouldEqual` name
138− tearDown name version db02
139−
140− describe "IDBKeyRange" do
141− it "only(int)" do
142− let key = 14
143− range = IDBKeyRange.only key
144− IDBKeyRange.includes range (toKey key) `shouldEqual` true
145−
146− it "only(string)" do
147− let key = "patate"
148− range = IDBKeyRange.only key
149− IDBKeyRange.includes range (toKey key) `shouldEqual` true
150−
151− it "only(float)" do
152− let key = 14.42
153− range = IDBKeyRange.only key
154− IDBKeyRange.includes range (toKey key) `shouldEqual` true
155−
156− it "only(date)" do
157− let mkey = DateTime
158− <$> (Date.canonicalDate <$> toEnum 2017 <*> toEnum 6 <*> toEnum 23)
159− <*> (Time <$> toEnum 17 <*> toEnum 59 <*> toEnum 34 <*> toEnum 42)
160−
161− case mkey of
162− Nothing ->
163− fail "unable to create datetime"
164− Just key -> do
165− let range = IDBKeyRange.only key
180+ it "only([string])" do
181+ let
182+ key = [ "patate", "autruche" ]
183+ range = IDBKeyRange.only key
166184 IDBKeyRange.includes range (toKey key) `shouldEqual` true
167185
168− it "only([int])" do
169− let key = [14, 42]
170− range = IDBKeyRange.only key
171− IDBKeyRange.includes range (toKey key) `shouldEqual` true
172−
173− it "only([string])" do
174− let key = ["patate", "autruche"]
175− range = IDBKeyRange.only key
176− IDBKeyRange.includes range (toKey key) `shouldEqual` true
177−
178− it "lowerBound(14, open)" do
179− let key = 14
180− open = true
181− range = IDBKeyRange.lowerBound key open
182− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
183− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
184− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
185−
186− it "lowerBound(14, close)" do
187− let key = 14
188− open = false
189− range = IDBKeyRange.lowerBound key open
190− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
191− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
192− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
193−
194− it "upperBound(14, open)" do
195− let key = 14
196− open = true
197− range = IDBKeyRange.upperBound key open
198− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
199− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
200− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
201−
202− it "upperBound(14, close)" do
203− let key = 14
204− open = false
205− range = IDBKeyRange.upperBound key open
206− IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
207− IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
208− IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
209−
210− it "bound(42, 14, open, open) => Nothing" do
211− let lower = 42
212− upper = 14
213− lowerOpen = true
214− upperOpen = true
215− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
216− isNothing mrange `shouldEqual` true
217−
218− it "bound(14, 42, open, open)" do
219− let lower = 14
220− upper = 42
221− lowerOpen = true
222− upperOpen = true
223− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
224− case mrange of
225− Nothing ->
226− fail "invalid range provided"
227− Just range -> do
228− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
229− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
230− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
231− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
232− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
233− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
234−
235− it "bound(14, 42, open, close)" do
236− let lower = 14
237− upper = 42
238− lowerOpen = true
239− upperOpen = false
240− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
241− case mrange of
242− Nothing ->
243− fail "invalid range provided"
244− Just range -> do
245− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
246− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
247− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
248− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
249− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
250− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
251−
252− it "bound(14, 42, close, open)" do
253− let lower = 14
254− upper = 42
255− lowerOpen = false
256− upperOpen = true
257− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
258− case mrange of
259− Nothing ->
260− fail "invalid range provided"
261− Just range -> do
262− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
263− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
264− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
265− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
266− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
267− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
268−
269− it "bound(14, 42, close, close)" do
270− let lower = 14
271− upper = 42
272− lowerOpen = false
273− upperOpen = false
274− mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
275− case mrange of
276− Nothing ->
277− fail "invalid range provided"
278− Just range -> do
279− IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
280− IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
281− IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
282− IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
283− IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
284− IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
285−
286− it "can access attributes of a range" do
287− let range = IDBKeyRange.lowerBound 14 false
288− IDBKeyRange.lower range `shouldEqual` (Just $ toKey 14)
289− IDBKeyRange.upper range `shouldEqual` none
290− IDBKeyRange.lowerOpen range `shouldEqual` false
291− IDBKeyRange.upperOpen range `shouldEqual` true
292−
293− describe "IDBDatabase" do
294− let
295− tearDown db = do
296− IDBDatabase.close db
297− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
298− pure unit
186+ it "lowerBound(14, open)" do
187+ let
188+ key = 14
189+ open = true
190+ range = IDBKeyRange.lowerBound key open
191+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
192+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
193+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
194+
195+ it "lowerBound(14, close)" do
196+ let
197+ key = 14
198+ open = false
199+ range = IDBKeyRange.lowerBound key open
200+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` true
201+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
202+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` false
203+
204+ it "upperBound(14, open)" do
205+ let
206+ key = 14
207+ open = true
208+ range = IDBKeyRange.upperBound key open
209+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
210+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
211+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
212+
213+ it "upperBound(14, close)" do
214+ let
215+ key = 14
216+ open = false
217+ range = IDBKeyRange.upperBound key open
218+ IDBKeyRange.includes range (toKey (key + 1)) `shouldEqual` false
219+ IDBKeyRange.includes range (toKey key) `shouldEqual` (not open)
220+ IDBKeyRange.includes range (toKey (key - 1)) `shouldEqual` true
221+
222+ it "bound(42, 14, open, open) => Nothing" do
223+ let
224+ lower = 42
225+ upper = 14
226+ lowerOpen = true
227+ upperOpen = true
228+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
229+ isNothing mrange `shouldEqual` true
230+
231+ it "bound(14, 42, open, open)" do
232+ let
233+ lower = 14
234+ upper = 42
235+ lowerOpen = true
236+ upperOpen = true
237+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
238+ case mrange of
239+ Nothing ->
240+ fail "invalid range provided"
241+ Just range -> do
242+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
243+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
244+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
245+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
246+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
247+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
248+
249+ it "bound(14, 42, open, close)" do
250+ let
251+ lower = 14
252+ upper = 42
253+ lowerOpen = true
254+ upperOpen = false
255+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
256+ case mrange of
257+ Nothing ->
258+ fail "invalid range provided"
259+ Just range -> do
260+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
261+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
262+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
263+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
264+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
265+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
266+
267+ it "bound(14, 42, close, open)" do
268+ let
269+ lower = 14
270+ upper = 42
271+ lowerOpen = false
272+ upperOpen = true
273+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
274+ case mrange of
275+ Nothing ->
276+ fail "invalid range provided"
277+ Just range -> do
278+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
279+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
280+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
281+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
282+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
283+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
284+
285+ it "bound(14, 42, close, close)" do
286+ let
287+ lower = 14
288+ upper = 42
289+ lowerOpen = false
290+ upperOpen = false
291+ mrange = IDBKeyRange.bound { lower, upper, lowerOpen, upperOpen }
292+ case mrange of
293+ Nothing ->
294+ fail "invalid range provided"
295+ Just range -> do
296+ IDBKeyRange.includes range (toKey (lower + 1)) `shouldEqual` true
297+ IDBKeyRange.includes range (toKey (upper - 1)) `shouldEqual` true
298+ IDBKeyRange.includes range (toKey (lower - 1)) `shouldEqual` false
299+ IDBKeyRange.includes range (toKey (upper + 1)) `shouldEqual` false
300+ IDBKeyRange.includes range (toKey lower) `shouldEqual` (not lowerOpen)
301+ IDBKeyRange.includes range (toKey upper) `shouldEqual` (not upperOpen)
302+
303+ it "can access attributes of a range" do
304+ let range = IDBKeyRange.lowerBound 14 false
305+ IDBKeyRange.lower range `shouldEqual` (Just $ toKey 14)
306+ IDBKeyRange.upper range `shouldEqual` none
307+ IDBKeyRange.lowerOpen range `shouldEqual` false
308+ IDBKeyRange.upperOpen range `shouldEqual` true
309+
310+ describe "IDBDatabase" do
311+ let
312+ tearDown db = do
313+ IDBDatabase.close db
314+ _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
315+ pure unit
299316
300− setup storeParams = do
301− let onUpgradeNeeded var db _ _ = launchAff' do
317+ setup storeParams = do
318+ let
319+ onUpgradeNeeded var db _ _ = launchAff' do
302320 store <- IDBDatabase.createObjectStore db "store" storeParams
303− _ <- putVar { db, store } var
321+ _ <- AVar.put { db, store } var
304322 pure unit
305323
306− var <- makeEmptyVar
307− db <- IDBFactory.open "db" Nothing
308− { onUpgradeNeeded : Just (onUpgradeNeeded var)
309− , onBlocked : Nothing
310− }
311−
312− takeVar var
313−
314− it "createObjectStore (keyPath: [], autoIncrement: true)" do
315− { db, store } <- setup { keyPath: [], autoIncrement: true }
316− IDBObjectStore.name store `shouldEqual` "store"
317− IDBObjectStore.keyPath store `shouldEqual` []
318− IDBObjectStore.autoIncrement store `shouldEqual` true
319− IDBObjectStore.indexNames store `shouldEqual` []
320− tearDown db
321−
322− it "createObjectStore (keyPath: [\"patate\"], autoIncrement: true)" do
323− { db, store } <- setup { keyPath: ["patate"], autoIncrement: true }
324− IDBObjectStore.name store `shouldEqual` "store"
325− IDBObjectStore.keyPath store `shouldEqual` ["patate"]
326− IDBObjectStore.autoIncrement store `shouldEqual` true
327− IDBObjectStore.indexNames store `shouldEqual` []
328− tearDown db
329−
330− it "createObjectStore (keyPath: [\"a\", \"b\"], autoIncrement: false)" do
331− { db, store } <- setup { keyPath: ["patate", "autruche"], autoIncrement: false }
332− IDBObjectStore.name store `shouldEqual` "store"
333− IDBObjectStore.keyPath store `shouldEqual` ["patate", "autruche"]
334− IDBObjectStore.autoIncrement store `shouldEqual` false
335− IDBObjectStore.indexNames store `shouldEqual` []
336− tearDown db
337−
338− it "deleteObjectStore" do
339− let onUpgradeNeeded var db _ _ = launchAff' do
340− _ <- IDBDatabase.deleteObjectStore db "store"
341− putVar true var
342−
343− var <- makeEmptyVar
344− { db, store } <- setup IDBDatabase.defaultParameters
345− IDBDatabase.close db
346− db' <- IDBFactory.open "db" (Just 999) { onUpgradeNeeded : Just (onUpgradeNeeded var)
347− , onBlocked : Nothing
348− }
349− deleted <- takeVar var
350− deleted `shouldEqual` true
351− tearDown db'
352−
353−
354− describe "IDBObjectStore" do
355− let
356− tearDown db = do
324+ var <- AVar.empty
325+ _ <- IDBFactory.open "db" Nothing
326+ { onUpgradeNeeded: Just (onUpgradeNeeded var)
327+ , onBlocked: Nothing
328+ }
329+
330+ AVar.take var
331+
332+ it "createObjectStore (keyPath: [], autoIncrement: true)" do
333+ { db, store } <- setup { keyPath: [], autoIncrement: true }
334+ IDBObjectStore.name store `shouldEqual` "store"
335+ IDBObjectStore.keyPath store `shouldEqual` []
336+ IDBObjectStore.autoIncrement store `shouldEqual` true
337+ IDBObjectStore.indexNames store `shouldEqual` []
338+ tearDown db
339+
340+ it "createObjectStore (keyPath: [\"patate\"], autoIncrement: true)" do
341+ { db, store } <- setup { keyPath: [ "patate" ], autoIncrement: true }
342+ IDBObjectStore.name store `shouldEqual` "store"
343+ IDBObjectStore.keyPath store `shouldEqual` [ "patate" ]
344+ IDBObjectStore.autoIncrement store `shouldEqual` true
345+ IDBObjectStore.indexNames store `shouldEqual` []
346+ tearDown db
347+
348+ it "createObjectStore (keyPath: [\"a\", \"b\"], autoIncrement: false)" do
349+ { db, store } <- setup { keyPath: [ "patate", "autruche" ], autoIncrement: false }
350+ IDBObjectStore.name store `shouldEqual` "store"
351+ IDBObjectStore.keyPath store `shouldEqual` [ "patate", "autruche" ]
352+ IDBObjectStore.autoIncrement store `shouldEqual` false
353+ IDBObjectStore.indexNames store `shouldEqual` []
354+ tearDown db
355+
356+ it "deleteObjectStore" do
357+ let
358+ onUpgradeNeeded var db _ _ = launchAff' do
359+ _ <- IDBDatabase.deleteObjectStore db "store"
360+ AVar.put true var
361+
362+ var <- AVar.empty
363+ { db } <- setup IDBDatabase.defaultParameters
357364 IDBDatabase.close db
358− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
359− pure unit
365+ db' <- IDBFactory.open "db" (Just 999)
366+ { onUpgradeNeeded: Just (onUpgradeNeeded var)
367+ , onBlocked: Nothing
368+ }
369+ deleted <- AVar.take var
370+ deleted `shouldEqual` true
371+ tearDown db'
372+
373+ describe "IDBObjectStore" do
374+ let
375+ tearDown db = do
376+ IDBDatabase.close db
377+ _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
378+ pure unit
360379
361− setup { storeParams, onUpgradeNeeded } = do
362− let onUpgradeNeeded' var db _ _ = launchAff' do
380+ setup { storeParams, onUpgradeNeeded } = do
381+ let
382+ onUpgradeNeeded' var db _ _ = launchAff' do
363383 store <- IDBDatabase.createObjectStore db "store" storeParams
364− liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure store)
365− putVar { db, store } var
366−
367− var <- makeEmptyVar
368− db <- IDBFactory.open "db" Nothing
369− { onUpgradeNeeded : Just (onUpgradeNeeded' var)
370− , onBlocked : Nothing
384+ liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure store)
385+ AVar.put { db, store } var
386+
387+ var <- AVar.empty
388+ _ <- IDBFactory.open "db" Nothing
389+ { onUpgradeNeeded: Just (onUpgradeNeeded' var)
390+ , onBlocked: Nothing
391+ }
392+
393+ AVar.take var
394+
395+ it "add()" do
396+ date <- liftEffect $ toDateTime <$> now
397+ { db } <- setup
398+ { storeParams: { autoIncrement: true, keyPath: [] }
399+ , onUpgradeNeeded: Just $ \_ store -> launchAff' do
400+ -- no key
401+ key <- IDBObjectStore.add store "patate" none
402+ (toKey 1) `shouldEqual` key
403+
404+ -- int key
405+ key' <- IDBObjectStore.add store "patate" (Just 14)
406+ (toKey 14) `shouldEqual` key'
407+
408+ -- number key
409+ key'' <- IDBObjectStore.add store "patate" (Just 14.42)
410+ (toKey 14.42) `shouldEqual` key''
411+
412+ -- string key
413+ key''' <- IDBObjectStore.add store "patate" (Just "key")
414+ (toKey "key") `shouldEqual` key'''
415+
416+ -- date key
417+ key'''' <- IDBObjectStore.add store "patate" (Just date)
418+ (toKey date) `shouldEqual` key''''
419+
420+ -- array key
421+ key''''' <- IDBObjectStore.add store "patate" (Just $ toKey [ 14, 42 ])
422+ (toKey [ 14, 42 ]) `shouldEqual` key'''''
371423 }
372−
373− takeVar var
374−
375− it "add()" do
376− date <- liftEff $ toDateTime <$> now
377− { db } <- setup
378− { storeParams: { autoIncrement: true, keyPath: [] }
379− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
380− -- no key
381− key <- IDBObjectStore.add store "patate" none
382− (toKey 1) `shouldEqual` key
383−
384− -- int key
385− key' <- IDBObjectStore.add store "patate" (Just 14)
386− (toKey 14) `shouldEqual` key'
387−
388− -- number key
389− key'' <- IDBObjectStore.add store "patate" (Just 14.42)
390− (toKey 14.42) `shouldEqual` key''
391−
392− -- string key
393− key''' <- IDBObjectStore.add store "patate" (Just "key")
394− (toKey "key") `shouldEqual` key'''
395−
396− -- date key
397− key'''' <- IDBObjectStore.add store "patate" (Just date)
398− (toKey date) `shouldEqual` key''''
399−
400− -- array key
401− key''''' <- IDBObjectStore.add store "patate" (Just $ toKey [14, 42])
402− (toKey [14, 42]) `shouldEqual` key'''''
403− }
404− tearDown db
405−
406− it "clear()" do
407− { db } <- setup
408− { storeParams: IDBDatabase.defaultParameters
409− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
410− key <- IDBObjectStore.add store "patate" (Just 14)
411− _ <- IDBObjectStore.clear store
412− val <- IDBObjectStore.get store (IDBKeyRange.only key)
413− val `shouldEqual` (Nothing :: Maybe String)
414− }
415− tearDown db
416−
417− it "count()" do
418− { db } <- setup
419− { storeParams: IDBDatabase.defaultParameters
420− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
421− _ <- IDBObjectStore.add store "patate" (Just 14)
422− _ <- IDBObjectStore.add store "autruche" (Just 42)
423− n <- IDBObjectStore.count store Nothing
424− n `shouldEqual` 2
425− }
426− tearDown db
427−
428− it "getKey()" do
429− { db } <- setup
430− { storeParams: IDBDatabase.defaultParameters
431− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
432− key <- IDBObjectStore.add store "patate" (Just 14)
433− mkey <- IDBObjectStore.getKey store (IDBKeyRange.only 14)
434− mkey `shouldEqual` (Just key)
435−
436− mkey' <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
437− mkey' `shouldEqual` none
438− }
439− tearDown db
440−
441− it "getAllKeys()" do
442− { db } <- setup
443− { storeParams: IDBDatabase.defaultParameters
444− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
445− key1 <- IDBObjectStore.add store "patate" (Just 14)
446− key2 <- IDBObjectStore.add store "autruche" (Just 42)
447− key3 <- IDBObjectStore.add store 14 (Just 1337)
448−
449− -- no bounds
450− keys <- IDBObjectStore.getAllKeys store Nothing Nothing
451− keys `shouldEqual` [key1, key2, key3]
452−
453− -- lower bound
454− keys' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
455− keys' `shouldEqual` [key2, key3]
456−
457− -- upper bound
458− keys'' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
459− keys'' `shouldEqual` [key1, key2]
460−
461− -- count
462− keys''' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
463− keys''' `shouldEqual` [key1, key2]
464− }
465− tearDown db
466−
467− it "openCursor()" do
468− let cb =
469− { onSuccess : const $ pure unit
470− , onError : show >>> fail >>> launchAff'
471− , onComplete: pure unit
424+ tearDown db
425+
426+ it "clear()" do
427+ { db } <- setup
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)
472434 }
473− { db } <- setup
474− { storeParams: IDBDatabase.defaultParameters
475− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
476− _ <- IDBObjectStore.openCursor store Nothing Next cb
477− _ <- IDBObjectStore.openCursor store Nothing NextUnique cb
478− _ <- IDBObjectStore.openCursor store Nothing Prev cb
479− _ <- IDBObjectStore.openCursor store Nothing PrevUnique cb
480− _ <- IDBObjectStore.openCursor store (Just $ IDBKeyRange.upperBound 1 true) Next cb
481− pure unit
482− }
483− tearDown db
484−
485− it "openKeyCursor()" do
486− let cb =
487− { onSuccess : const $ pure unit
488− , onError : show >>> fail >>> launchAff'
489− , onComplete: pure unit
435+ tearDown db
436+
437+ it "count()" do
438+ { db } <- setup
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
444+ n `shouldEqual` 2
490445 }
491− { db } <- setup
492− { storeParams: IDBDatabase.defaultParameters
493− , onUpgradeNeeded: Just $ \_ store -> launchAff' do
494− _ <- IDBObjectStore.openKeyCursor store Nothing Next cb
495− _ <- IDBObjectStore.openKeyCursor store Nothing NextUnique cb
496− _ <- IDBObjectStore.openKeyCursor store Nothing Prev cb
497− _ <- IDBObjectStore.openKeyCursor store Nothing PrevUnique cb
498− _ <- IDBObjectStore.openKeyCursor store (Just $ IDBKeyRange.lowerBound 1 true) Next cb
446+ tearDown db
447+
448+ it "getKey()" do
449+ { db } <- setup
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)
455+
456+ mkey' <- IDBObjectStore.getKey store (IDBKeyRange.only 42)
457+ mkey' `shouldEqual` none
458+ }
459+ tearDown db
460+
461+ it "getAllKeys()" do
462+ { db } <- setup
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)
468+
469+ -- no bounds
470+ keys <- IDBObjectStore.getAllKeys store Nothing Nothing
471+ keys `shouldEqual` [ key1, key2, key3 ]
472+
473+ -- lower bound
474+ keys' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 14 true) Nothing
475+ keys' `shouldEqual` [ key2, key3 ]
476+
477+ -- upper bound
478+ keys'' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.upperBound 42 false) Nothing
479+ keys'' `shouldEqual` [ key1, key2 ]
480+
481+ -- count
482+ keys''' <- IDBObjectStore.getAllKeys store (Just $ IDBKeyRange.lowerBound 1 true) (Just 2)
483+ keys''' `shouldEqual` [ key1, key2 ]
484+ }
485+ tearDown db
486+
487+ it "openCursor()" do
488+ let
489+ cb =
490+ { onSuccess: const $ pure unit
491+ , onError: show >>> fail >>> launchAff'
492+ , onComplete: pure unit
493+ }
494+ { db } <- setup
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
502+ pure unit
503+ }
504+ tearDown db
505+
506+ it "openKeyCursor()" do
507+ let
508+ cb =
509+ { onSuccess: const $ pure unit
510+ , onError: show >>> fail >>> launchAff'
511+ , onComplete: pure unit
512+ }
513+ { db } <- setup
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
521+ pure unit
522+ }
523+ tearDown db
524+
525+ describe "IDBIndex" do
526+ let
527+ tearDown db = do
528+ IDBDatabase.close db
529+ _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
499530 pure unit
500− }
501− tearDown db
502531
503− describe "IDBIndex" do
504− let
505− tearDown db = do
506− IDBDatabase.close db
507− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
508− pure unit
509−
510− setup :: forall value e e'.
511− { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
512− , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
513− , values :: Array (Tuple value (Maybe Key))
514− , keyPath :: Array String
515− , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
516− } -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
517− setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
518− let onUpgradeNeeded' var db tx _ = launchAff' do
532+ setup
533+ :: forall value
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)
539+ }
540+ -> Aff { db :: Database, index :: Index, store :: ObjectStore }
541+ setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
542+ let
543+ onUpgradeNeeded' var db tx _ = launchAff' do
519544 store <- IDBDatabase.createObjectStore db "store" storeParams
520− _ <- traverse (uncurry (IDBObjectStore.add store)) values
545+ _ <- traverse (uncurry (IDBObjectStore.add store)) values
521546 index <- IDBObjectStore.createIndex store "index" keyPath indexParams
522− liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
523− putVar { db, index, store } var
547+ liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
548+ AVar.put { db, index, store } var
549+
550+ var <- AVar.empty
551+ _ <- IDBFactory.open "db" Nothing
552+ { onUpgradeNeeded: Just (onUpgradeNeeded' var)
553+ , onBlocked: Nothing
554+ }
555+ AVar.take var
556+
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
562+ , keyPath: []
563+ , values: []
564+ }
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
569+ tearDown db
570+
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)
573+ txVar <- AVar.empty
574+ dbVar <- AVar.empty
575+ res <- attempt $ setup
576+ { storeParams: IDBDatabase.defaultParameters
577+ , indexParams:
578+ { unique: true
579+ , multiEntry: false
580+ }
581+ , keyPath: [ "indexedProperty" ]
582+ , values:
583+ [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
584+ , { indexedProperty: "bar" } :+: (Just $ toKey 2)
585+ ]
586+ , onUpgradeNeeded: Just $ \db tx _ -> launchAff' do
587+ IDBTransaction.onAbort tx (onAbort txVar)
588+ IDBDatabase.onAbort db (onAbort dbVar)
589+ }
590+ case res of
591+ Right _ ->
592+ fail "expected abort"
593+ _ -> do
594+ shouldEqual true =<< AVar.take txVar
595+ shouldEqual true =<< AVar.take dbVar
596+
597+ it "the index is usable right after being made" do
598+ { db } <- setup
599+ { storeParams:
600+ { keyPath: [ "key" ]
601+ , autoIncrement: false
602+ }
603+ , indexParams: IDBObjectStore.defaultParameters
604+ , keyPath: [ "indexedProperty" ]
605+ , values:
606+ [ { key: "key1", indexedProperty: "indexed_1" } :+: Nothing
607+ , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
608+ , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
609+ ]
610+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
611+ val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
612+ ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
613+ }
614+ tearDown db
615+
616+ it "empty keyPath" do
617+ { db } <- setup
618+ { storeParams: IDBDatabase.defaultParameters
619+ , indexParams: IDBObjectStore.defaultParameters
620+ , keyPath: []
621+ , values:
622+ [ "object_1" :+: (Just $ toKey 1)
623+ , "object_2" :+: (Just $ toKey 2)
624+ , "object_3" :+: (Just $ toKey 3)
625+ ]
626+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
627+ val <- IDBIndex.get index (IDBKeyRange.only "object_3")
628+ val `shouldEqual` (Just "object_3")
629+ }
630+ tearDown db
631+
632+ it "index can be valid keys [date]" do
633+ date <- liftEffect $ toDateTime <$> now
634+ { db } <- setup
635+ { storeParams:
636+ { keyPath: [ "key" ]
637+ , autoIncrement: false
638+ }
639+ , indexParams: IDBObjectStore.defaultParameters
640+ , keyPath: [ "i" ]
641+ , values:
642+ [ { key: "date", i: (toKey date) } :+: Nothing
643+ ]
644+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
645+ val <- IDBIndex.get index (IDBKeyRange.only date)
646+ ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
647+ }
648+ tearDown db
649+
650+ it "index can be valid keys [num]" do
651+ let num = 14
652+ { db } <- setup
653+ { storeParams:
654+ { keyPath: [ "key" ]
655+ , autoIncrement: false
656+ }
657+ , indexParams: IDBObjectStore.defaultParameters
658+ , keyPath: [ "i" ]
659+ , values:
660+ [ { key: "num", i: (toKey num) } :+: Nothing
661+ ]
662+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
663+ val <- IDBIndex.get index (IDBKeyRange.only num)
664+ ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
665+ }
666+ tearDown db
667+
668+ it "index can be valid keys [array]" do
669+ let array = [ "patate", "autruche" ]
670+ { db } <- setup
671+ { storeParams:
672+ { keyPath: [ "key" ]
673+ , autoIncrement: false
674+ }
675+ , indexParams: IDBObjectStore.defaultParameters
676+ , keyPath: [ "i" ]
677+ , values:
678+ [ { key: "array", i: (toKey array) } :+: Nothing
679+ ]
680+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
681+ val <- IDBIndex.get index (IDBKeyRange.only array)
682+ ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
683+ }
684+ tearDown db
685+
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" ]
691+ , values:
692+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
693+ ]
694+ , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
695+ let
696+ cb =
697+ { onSuccess: const $ pure unit
698+ , onError: show >>> fail >>> launchAff'
699+ , onComplete: pure unit
700+ }
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
705+ case cursor of
706+ Right _ -> fail "expected InvalidStateError"
707+ Left err -> name err `shouldEqual` "InvalidStateError"
708+ }
709+ case res of
710+ Right _ -> fail "expected InvalidStateError"
711+ _ -> pure unit
712+
713+ it "openKeyCursor() - throw TransactionInactiveError on aborted transaction" do
714+ { db } <- setup
715+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
716+ , indexParams: IDBObjectStore.defaultParameters
717+ , keyPath: [ "indexedProperty" ]
718+ , values:
719+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
720+ ]
721+ , onUpgradeNeeded: Nothing
722+ }
723+ let
724+ cb =
725+ { onSuccess: const $ pure unit
726+ , onError: show >>> fail >>> launchAff'
727+ , onComplete: pure unit
728+ }
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
735+ case cursor of
736+ Right _ -> fail "expected TransactionInactiveError"
737+ Left err -> name err `shouldEqual` "TransactionInactiveError"
738+
739+ tearDown db
740+
741+ it "openKeyCursor() - throw InvalidStateError when the index is deleted" do
742+ { db } <- setup
743+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
744+ , indexParams: IDBObjectStore.defaultParameters
745+ , keyPath: [ "indexedProperty" ]
746+ , values:
747+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
748+ ]
749+ , onUpgradeNeeded: Just $ \_ tx index -> launchAff' do
750+ let
751+ cb =
752+ { onSuccess: const $ pure unit
753+ , onError: show >>> fail >>> launchAff'
754+ , onComplete: pure unit
755+ }
756+ store <- IDBTransaction.objectStore tx "store"
757+ IDBObjectStore.deleteIndex store "index"
758+ cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
759+ case cursor of
760+ Right _ -> fail "expected InvalidStateError"
761+ Left err -> name err `shouldEqual` "InvalidStateError"
762+ }
524763
525− var <- makeEmptyVar
526− db <- IDBFactory.open "db" Nothing
527− { onUpgradeNeeded : Just (onUpgradeNeeded' var)
528− , onBlocked : Nothing
764+ tearDown db
765+
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" ]
771+ , values:
772+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
773+ ]
774+ , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
775+ let
776+ cb =
777+ { onSuccess: const $ pure unit
778+ , onError: show >>> fail >>> launchAff'
779+ , onComplete: pure unit
780+ }
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
785+ case cursor of
786+ Right _ -> fail "expected InvalidStateError"
787+ Left err -> name err `shouldEqual` "InvalidStateError"
529788 }
530− takeVar var
531−
532−
533− it "returns an IDBIndex and the properties are set correctly" do
534− { db, index } <- setup
535− { storeParams : IDBDatabase.defaultParameters
536− , indexParams : IDBObjectStore.defaultParameters
537− , onUpgradeNeeded : Nothing
538− , keyPath : []
539− , values : []
540− }
541− IDBIndex.name index `shouldEqual` "index"
542− IDBIndex.keyPath index `shouldEqual` []
543− IDBIndex.unique index `shouldEqual` IDBObjectStore.defaultParameters.unique
544− IDBIndex.multiEntry index `shouldEqual` IDBObjectStore.defaultParameters.multiEntry
545− tearDown db
546−
547− it "attempt to create an index that requires unique values on an object store already contains duplicates" do
548− let onAbort var = launchAff' (putVar true var)
549− txVar <- makeEmptyVar
550− dbVar <- makeEmptyVar
551− res <- attempt $ setup
552− { storeParams : IDBDatabase.defaultParameters
553− , indexParams : { unique : true
554− , multiEntry : false
555− }
556− , keyPath : ["indexedProperty"]
557− , values : [ { indexedProperty: "bar" } :+: (Just $ toKey 1)
558− , { indexedProperty: "bar" } :+: (Just $ toKey 2)
559− ]
560− , onUpgradeNeeded : Just $ \db tx _ -> launchAff' do
561− IDBTransaction.onAbort tx (onAbort txVar)
562− IDBDatabase.onAbort db (onAbort dbVar)
563− }
564− case res of
565− Right _ ->
566− fail "expected abort"
567− _ -> do
568− shouldEqual true =<< takeVar txVar
569− shouldEqual true =<< takeVar dbVar
570−
571−
572− it "the index is usable right after being made" do
573− { db } <- setup
574− { storeParams : { keyPath : ["key"]
575− , autoIncrement : false
576− }
577− , indexParams : IDBObjectStore.defaultParameters
578− , keyPath : ["indexedProperty"]
579− , values : [ { key: "key1", indexedProperty: "indexed_1" } :+: Nothing
580− , { key: "key2", indexedProperty: "indexed_2" } :+: Nothing
581− , { key: "key3", indexedProperty: "indexed_3" } :+: Nothing
582− ]
583− , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
584− val <- IDBIndex.get index (IDBKeyRange.only "indexed_2")
585− ((\r -> r.key) <$> val) `shouldEqual` (Just $ toKey "key2")
586− }
587− tearDown db
588−
589− it "empty keyPath" do
590− { db } <- setup
591− { storeParams : IDBDatabase.defaultParameters
592− , indexParams : IDBObjectStore.defaultParameters
593− , keyPath : []
594− , values : [ "object_1" :+: (Just $ toKey 1)
595− , "object_2" :+: (Just $ toKey 2)
596− , "object_3" :+: (Just $ toKey 3)
597− ]
598− , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
599− val <- IDBIndex.get index (IDBKeyRange.only "object_3")
600− val `shouldEqual` (Just "object_3")
601− }
602− tearDown db
603−
604− it "index can be valid keys [date]" do
605− date <- liftEff $ toDateTime <$> now
606− { db } <- setup
607− { storeParams : { keyPath: ["key"]
608− , autoIncrement: false
609− }
610− , indexParams : IDBObjectStore.defaultParameters
611− , keyPath : ["i"]
612− , values : [ { key: "date", i: (toKey date) } :+: Nothing
613− ]
614− , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
615− val <- IDBIndex.get index (IDBKeyRange.only date)
616− ((\r -> r.key) <$> val) `shouldEqual` (Just "date")
617− }
618− tearDown db
619−
620− it "index can be valid keys [num]" do
621− let num = 14
622− { db } <- setup
623− { storeParams : { keyPath: ["key"]
624− , autoIncrement: false
625− }
626− , indexParams : IDBObjectStore.defaultParameters
627− , keyPath : ["i"]
628− , values : [ { key: "num", i: (toKey num) } :+: Nothing
629− ]
630− , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
631− val <- IDBIndex.get index (IDBKeyRange.only num)
632− ((\r -> r.key) <$> val) `shouldEqual` (Just "num")
633− }
634− tearDown db
635−
636− it "index can be valid keys [array]" do
637− let array = ["patate", "autruche"]
638− { db } <- setup
639− { storeParams : { keyPath: ["key"]
640− , autoIncrement: false
641− }
642− , indexParams : IDBObjectStore.defaultParameters
643− , keyPath : ["i"]
644− , values : [ { key: "array", i: (toKey array) } :+: Nothing
645− ]
646− , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
647− val <- IDBIndex.get index (IDBKeyRange.only array)
648− ((\r -> r.key) <$> val) `shouldEqual` (Just "array")
649− }
650− tearDown db
651−
652− it "openKeyCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
653− res <- attempt $ setup
654− { storeParams : { keyPath: ["key"], autoIncrement: false }
655− , indexParams : IDBObjectStore.defaultParameters
656− , keyPath : ["indexedProperty"]
657− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
658− ]
659− , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
660− let cb =
661− { onSuccess : const $ pure unit
662− , onError : show >>> fail >>> launchAff'
663− , onComplete: pure unit
664− }
665− IDBTransaction.onAbort tx (pure unit)
666− IDBDatabase.onAbort db (pure unit)
667− IDBTransaction.abort tx
668− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
669− case cursor of
670− Right _ -> fail "expected InvalidStateError"
671− Left err -> name err `shouldEqual` "InvalidStateError"
672− }
673− case res of
674− Right _ -> fail "expected InvalidStateError"
675− _ -> pure unit
676−
677− it "openKeyCursor() - throw TransactionInactiveError on aborted transaction" do
678− { db } <- setup
679− { storeParams : { keyPath: ["key"], autoIncrement: false }
680− , indexParams : IDBObjectStore.defaultParameters
681− , keyPath : ["indexedProperty"]
682− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
683− ]
684− , onUpgradeNeeded : Nothing
685− }
686− let cb =
687− { onSuccess : const $ pure unit
688− , onError : show >>> fail >>> launchAff'
689− , onComplete: pure unit
789+ case res of
790+ Right _ -> fail "expected InvalidStateError"
791+ _ -> pure unit
792+
793+ it "openCursor() - throw TransactionInactiveError on aborted transaction" do
794+ { db } <- setup
795+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
796+ , indexParams: IDBObjectStore.defaultParameters
797+ , keyPath: [ "indexedProperty" ]
798+ , values:
799+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
800+ ]
801+ , onUpgradeNeeded: Nothing
690802 }
691− tx <- IDBDatabase.transaction db ["store"] ReadOnly
692− store <- IDBTransaction.objectStore tx "store"
693− index <- IDBObjectStore.index store "index"
694− IDBTransaction.onAbort tx (pure unit)
695− IDBTransaction.abort tx
696− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
697− case cursor of
698− Right _ -> fail "expected TransactionInactiveError"
699− Left err -> name err `shouldEqual` "TransactionInactiveError"
700−
701− tearDown db
702−
703− it "openKeyCursor() - throw InvalidStateError when the index is deleted" do
704− { db } <- setup
705− { storeParams : { keyPath: ["key"], autoIncrement: false }
706− , indexParams : IDBObjectStore.defaultParameters
707− , keyPath : ["indexedProperty"]
708− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
709− ]
710− , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
711− let cb =
712− { onSuccess : const $ pure unit
713− , onError : show >>> fail >>> launchAff'
714− , onComplete: pure unit
715− }
716− store <- IDBTransaction.objectStore tx "store"
717− IDBObjectStore.deleteIndex store "index"
718− cursor <- attempt $ IDBIndex.openKeyCursor index Nothing Next cb
719− case cursor of
720− Right _ -> fail "expected InvalidStateError"
721− Left err -> name err `shouldEqual` "InvalidStateError"
722− }
723−
724− tearDown db
725−
726− it "openCursor() - throw InvalidStateError on index deleted by aborted upgrade" do
727− res <- attempt $ setup
728− { storeParams : { keyPath: ["key"], autoIncrement: false }
729− , indexParams : IDBObjectStore.defaultParameters
730− , keyPath : ["indexedProperty"]
731− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
732− ]
733− , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
734− let cb =
735− { onSuccess : const $ pure unit
736− , onError : show >>> fail >>> launchAff'
737− , onComplete: pure unit
738− }
739− IDBTransaction.onAbort tx (pure unit)
740− IDBDatabase.onAbort db (pure unit)
741− IDBTransaction.abort tx
742− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
743− case cursor of
744− Right _ -> fail "expected InvalidStateError"
745− Left err -> name err `shouldEqual` "InvalidStateError"
746− }
747− case res of
748− Right _ -> fail "expected InvalidStateError"
749− _ -> pure unit
750−
751− it "openCursor() - throw TransactionInactiveError on aborted transaction" do
752− { db } <- setup
753− { storeParams : { keyPath: ["key"], autoIncrement: false }
754− , indexParams : IDBObjectStore.defaultParameters
755− , keyPath : ["indexedProperty"]
756− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
757− ]
758− , onUpgradeNeeded : Nothing
759− }
760− let cb =
761− { onSuccess : const $ pure unit
762− , onError : show >>> fail >>> launchAff'
763− , onComplete: pure unit
803+ let
804+ cb =
805+ { onSuccess: const $ pure unit
806+ , onError: show >>> fail >>> launchAff'
807+ , onComplete: pure unit
808+ }
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
815+ case cursor of
816+ Right _ -> fail "expected TransactionInactiveError"
817+ Left err -> name err `shouldEqual` "TransactionInactiveError"
818+
819+ tearDown db
820+
821+ it "openCursor() - throw InvalidStateError when the index is deleted" do
822+ { db } <- setup
823+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
824+ , indexParams: IDBObjectStore.defaultParameters
825+ , keyPath: [ "indexedProperty" ]
826+ , values:
827+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
828+ ]
829+ , onUpgradeNeeded: Just $ \db tx index -> launchAff' do
830+ let
831+ cb =
832+ { onSuccess: const $ pure unit
833+ , onError: show >>> fail >>> launchAff'
834+ , onComplete: pure unit
835+ }
836+ store <- IDBTransaction.objectStore tx "store"
837+ IDBObjectStore.deleteIndex store "index"
838+ cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
839+ case cursor of
840+ Right _ -> fail "expected InvalidStateError"
841+ Left err -> name err `shouldEqual` "InvalidStateError"
764842 }
765− tx <- IDBDatabase.transaction db ["store"] ReadOnly
766− store <- IDBTransaction.objectStore tx "store"
767− index <- IDBObjectStore.index store "index"
768− IDBTransaction.onAbort tx (pure unit)
769− IDBTransaction.abort tx
770− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
771− case cursor of
772− Right _ -> fail "expected TransactionInactiveError"
773− Left err -> name err `shouldEqual` "TransactionInactiveError"
774−
775− tearDown db
776−
777− it "openCursor() - throw InvalidStateError when the index is deleted" do
778− { db } <- setup
779− { storeParams : { keyPath: ["key"], autoIncrement: false }
780− , indexParams : IDBObjectStore.defaultParameters
781− , keyPath : ["indexedProperty"]
782− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
783− ]
784− , onUpgradeNeeded : Just $ \db tx index -> launchAff' do
785− let cb =
786− { onSuccess : const $ pure unit
787− , onError : show >>> fail >>> launchAff'
788− , onComplete: pure unit
789− }
790− store <- IDBTransaction.objectStore tx "store"
791− IDBObjectStore.deleteIndex store "index"
792− cursor <- attempt $ IDBIndex.openCursor index Nothing Next cb
793− case cursor of
794− Right _ -> fail "expected InvalidStateError"
795− Left err -> name err `shouldEqual` "InvalidStateError"
796− }
797−
798− tearDown db
799−
800− it "getKey() - multiEntry - adding keys" do
801− { db } <- setup
802− { storeParams : IDBDatabase.defaultParameters
803− , indexParams : { unique: false, multiEntry: true }
804− , keyPath : ["name"]
805− , values : [ { name: ["patate", "autruche"] } :+: (Just $ toKey 1)
806− , { name: ["bob"] } :+: (Just $ toKey 2)
807− ]
808− , onUpgradeNeeded : Just $ \_ _ index -> launchAff' do
809− key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
810− key `shouldEqual` (Just $ toKey 1)
811−
812− key' <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
813− key' `shouldEqual` (Just $ toKey 1)
814−
815− key'' <- IDBIndex.getKey index (IDBKeyRange.only "bob")
816− key'' `shouldEqual` (Just $ toKey 2)
817− }
818− tearDown db
819−
820−
821− it "get() - returns the record with the first key in the range" do
822− { db } <- setup
823− { storeParams : { keyPath: ["key"], autoIncrement: false }
824− , indexParams : IDBObjectStore.defaultParameters
825− , keyPath : ["indexedProperty"]
826− , values : [ { key: 14, indexedProperty: "patate" } :+: Nothing
827− , { key: 42, indexedProperty: "autruche" } :+: Nothing
828− , { key: 1337, indexedProperty: "baguette" } :+: Nothing
829− ]
830− , onUpgradeNeeded : Nothing
831− }
832−
833− tx <- IDBDatabase.transaction db ["store"] ReadOnly
834− store <- IDBTransaction.objectStore tx "store"
835− index <- IDBObjectStore.index store "index"
836− val <- IDBIndex.get index (IDBKeyRange.lowerBound "autruche" false)
837− ((\r -> r.key) <$> val) `shouldEqual` (Just 42)
838−
839− tearDown db
840−
841− describe "IDBCursor" do
842− let
843− tearDown db = do
844− IDBDatabase.close db
845− _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
846− pure unit
847−
848− setup :: forall value e e'.
849− { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
850− , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
851− , values :: Array (Tuple value (Maybe Key))
852− , keyPath :: Array String
853− , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Eff (idb :: IDB, avar :: AVAR, exception :: EXCEPTION | e') Unit)
854− } -> Aff (idb :: IDB, avar :: AVAR | e) { db :: Database, index :: Index, store :: ObjectStore }
855− setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
856− let onUpgradeNeeded' var db tx _ = launchAff' do
857− store <- IDBDatabase.createObjectStore db "store" storeParams
858− _ <- traverse (uncurry (IDBObjectStore.add store)) values
859− index <- IDBObjectStore.createIndex store "index" keyPath indexParams
860− liftEff $ maybe (pure unit) id (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
861− putVar { db, index, store } var
862843
863− var <- makeEmptyVar
864− db <- IDBFactory.open "db" Nothing
865− { onUpgradeNeeded : Just (onUpgradeNeeded' var)
866− , onBlocked : Nothing
844+ tearDown db
845+
846+ it "getKey() - multiEntry - adding keys" do
847+ { db } <- setup
848+ { storeParams: IDBDatabase.defaultParameters
849+ , indexParams: { unique: false, multiEntry: true }
850+ , keyPath: [ "name" ]
851+ , values:
852+ [ { name: [ "patate", "autruche" ] } :+: (Just $ toKey 1)
853+ , { name: [ "bob" ] } :+: (Just $ toKey 2)
854+ ]
855+ , onUpgradeNeeded: Just $ \_ _ index -> launchAff' do
856+ key <- IDBIndex.getKey index (IDBKeyRange.only "patate")
857+ key `shouldEqual` (Just $ toKey 1)
858+
859+ key' <- IDBIndex.getKey index (IDBKeyRange.only "autruche")
860+ key' `shouldEqual` (Just $ toKey 1)
861+
862+ key'' <- IDBIndex.getKey index (IDBKeyRange.only "bob")
863+ key'' `shouldEqual` (Just $ toKey 2)
864+ }
865+ tearDown db
866+
867+ it "get() - returns the record with the first key in the range" do
868+ { db } <- setup
869+ { storeParams: { keyPath: [ "key" ], autoIncrement: false }
870+ , indexParams: IDBObjectStore.defaultParameters
871+ , keyPath: [ "indexedProperty" ]
872+ , values:
873+ [ { key: 14, indexedProperty: "patate" } :+: Nothing
874+ , { key: 42, indexedProperty: "autruche" } :+: Nothing
875+ , { key: 1337, indexedProperty: "baguette" } :+: Nothing
876+ ]
877+ , onUpgradeNeeded: Nothing
867878 }
868− takeVar var
869−
870− it "continue() - iterate to the next record" do
871− { db } <- setup
872− { storeParams : IDBDatabase.defaultParameters
873− , indexParams : IDBObjectStore.defaultParameters
874− , keyPath : []
875− , values : [ "cupcake" :+: (Just $ toKey 4)
876− , "pancake" :+: (Just $ toKey 2)
877− , "pie" :+: (Just $ toKey 1)
878− , "pie" :+: (Just $ toKey 3)
879− ]
880− , onUpgradeNeeded: Nothing
881− }
882− let cb vdone vvals =
883− { onComplete: launchAff' do
884− putVar unit vdone
885−
886− , onError: \error -> launchAff' do
887− fail $ "unexpected error: " <> show error
888−
889− , onSuccess: \cursor -> launchAff' do
890− vals <- takeVar vvals
891− pure (IDBCursor.value cursor) >>= shouldEqual (maybe "" _.v $ head vals)
892− IDBCursor.primaryKey cursor >>= shouldEqual (maybe (toKey 0) _.k $ head vals)
893− putVar (drop 1 vals) vvals
894− IDBCursor.continue cursor none
879+
880+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
881+ store <- IDBTransaction.objectStore tx "store"
882+ index <- IDBObjectStore.index store "index"
883+ val <- IDBIndex.get index (IDBKeyRange.lowerBound "autruche" false)
884+ ((\r -> r.key) <$> val) `shouldEqual` (Just 42)
885+
886+ tearDown db
887+
888+ describe "IDBCursor" do
889+ let
890+ tearDown db = do
891+ IDBDatabase.close db
892+ _ <- IDBFactory.deleteDatabase (IDBDatabase.name db)
893+ pure unit
894+
895+ setup
896+ :: forall value
897+ . { storeParams :: { keyPath :: Array String, autoIncrement :: Boolean }
898+ , indexParams :: { unique :: Boolean, multiEntry :: Boolean }
899+ , values :: Array (Tuple value (Maybe Key))
900+ , keyPath :: Array String
901+ , onUpgradeNeeded :: Maybe (Database -> Transaction -> Index -> Effect Unit)
902+ }
903+ -> Aff { db :: Database, index :: Index, store :: ObjectStore }
904+ setup { storeParams, indexParams, values, keyPath, onUpgradeNeeded } = do
905+ let
906+ onUpgradeNeeded' var db tx _ = launchAff' do
907+ store <- IDBDatabase.createObjectStore db "store" storeParams
908+ _ <- traverse (uncurry (IDBObjectStore.add store)) values
909+ index <- IDBObjectStore.createIndex store "index" keyPath indexParams
910+ liftEffect $ maybe (pure unit) identity (onUpgradeNeeded <*> pure db <*> pure tx <*> pure index)
911+ AVar.put { db, index, store } var
912+
913+ var <- AVar.empty
914+ db <- IDBFactory.open "db" Nothing
915+ { onUpgradeNeeded: Just (onUpgradeNeeded' var)
916+ , onBlocked: Nothing
917+ }
918+ AVar.take var
919+
920+ it "continue() - iterate to the next record" do
921+ { db } <- setup
922+ { storeParams: IDBDatabase.defaultParameters
923+ , indexParams: IDBObjectStore.defaultParameters
924+ , keyPath: []
925+ , values:
926+ [ "cupcake" :+: (Just $ toKey 4)
927+ , "pancake" :+: (Just $ toKey 2)
928+ , "pie" :+: (Just $ toKey 1)
929+ , "pie" :+: (Just $ toKey 3)
930+ ]
931+ , onUpgradeNeeded: Nothing
895932 }
896− vdone <- makeEmptyVar
897− vvals <- makeVar
898− [ { v: "pie" , k: toKey 1 }
899− , { v: "pancake", k: toKey 2 }
900− , { v: "pie" , k: toKey 3 }
901− , { v: "cupcake", k: toKey 4 }
902− ]
903− tx <- IDBDatabase.transaction db ["store"] ReadOnly
904− store <- IDBTransaction.objectStore tx "store"
905− IDBObjectStore.openCursor store Nothing Next (cb vdone vvals)
906− takeVar vdone
907− tearDown db
908−
909− it "continue() - attempt to iterate in the wrong direction" do
910− { db } <- setup
911− { storeParams : IDBDatabase.defaultParameters
912− , indexParams : IDBObjectStore.defaultParameters
913− , keyPath : []
914− , values : [ "cupcake" :+: (Just $ toKey 4)
915− , "pancake" :+: (Just $ toKey 2)
916− , "pie" :+: (Just $ toKey 1)
917− , "pie" :+: (Just $ toKey 3)
918− ]
919− , onUpgradeNeeded: Nothing
920− }
921− let cb vdone =
922− { onComplete: launchAff' do
923− fail $ "shouldn't complete"
924−
925− , onError: \error -> launchAff' do
926− fail $ "unexpected error: " <> show error
927−
928− , onSuccess: \cursor -> launchAff' do
929− res <- attempt $ IDBCursor.continue cursor (Just 1)
930− case res of
931− Left err -> do
932− name err `shouldEqual` "DataError"
933− putVar unit vdone
934− Right _ -> do
935− fail "expected continue to fail"
933+ let
934+ cb vdone vvals =
935+ { onComplete: launchAff' do
936+ AVar.put unit vdone
937+
938+ , onError: \error -> launchAff' do
939+ fail $ "unexpected error: " <> show error
940+
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
947+ }
948+ vdone <- AVar.empty
949+ vvals <- AVar.new
950+ [ { v: "pie", k: toKey 1 }
951+ , { v: "pancake", k: toKey 2 }
952+ , { v: "pie", k: toKey 3 }
953+ , { v: "cupcake", k: toKey 4 }
954+ ]
955+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
956+ store <- IDBTransaction.objectStore tx "store"
957+ IDBObjectStore.openCursor store Nothing Next (cb vdone vvals)
958+ AVar.take vdone
959+ tearDown db
960+
961+ it "continue() - attempt to iterate in the wrong direction" do
962+ { db } <- setup
963+ { storeParams: IDBDatabase.defaultParameters
964+ , indexParams: IDBObjectStore.defaultParameters
965+ , keyPath: []
966+ , values:
967+ [ "cupcake" :+: (Just $ toKey 4)
968+ , "pancake" :+: (Just $ toKey 2)
969+ , "pie" :+: (Just $ toKey 1)
970+ , "pie" :+: (Just $ toKey 3)
971+ ]
972+ , onUpgradeNeeded: Nothing
936973 }
937− vdone <- makeEmptyVar
938− tx <- IDBDatabase.transaction db ["store"] ReadOnly
939− store <- IDBTransaction.objectStore tx "store"
940− IDBObjectStore.openCursor store Nothing Next (cb vdone)
941− takeVar vdone
942− tearDown db
943−
944−
945− it "advance() - iterate cursor number of times specified by count" do
946− { db } <- setup
947− { storeParams : { keyPath: ["pKey"], autoIncrement: false }
948− , indexParams : IDBObjectStore.defaultParameters
949− , keyPath : []
950− , values : [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
951− , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
952− , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
953− , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
954− ]
955− , onUpgradeNeeded: Nothing
956− }
957− let cb vdone vjump =
958− { onComplete: launchAff' do
959− putVar unit vdone
960−
961− , onError: \error -> launchAff' do
962− fail $ "unexpected error: " <> show error
963−
964− , onSuccess: \cursor -> launchAff' do
965− jump <- takeVar vjump
966− if jump
967− then do
968− IDBCursor.advance cursor 3
969− else do
970− let value = IDBCursor.value cursor
971− value.pKey `shouldEqual` "pkey_3"
972− value.iKey `shouldEqual` "ikey_3"
973− IDBCursor.continue cursor none
974− putVar false vjump
974+ let
975+ cb vdone =
976+ { onComplete: launchAff' do
977+ fail $ "shouldn't complete"
978+
979+ , onError: \error -> launchAff' do
980+ fail $ "unexpected error: " <> show error
981+
982+ , onSuccess: \cursor -> launchAff' do
983+ res <- attempt $ IDBCursor.continue cursor (Just 1)
984+ case res of
985+ Left err -> do
986+ name err `shouldEqual` "DataError"
987+ AVar.put unit vdone
988+ Right _ -> do
989+ fail "expected continue to fail"
990+ }
991+ vdone <- AVar.empty
992+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
993+ store <- IDBTransaction.objectStore tx "store"
994+ IDBObjectStore.openCursor store Nothing Next (cb vdone)
995+ AVar.take vdone
996+ tearDown db
997+
998+ it "advance() - iterate cursor number of times specified by count" do
999+ { db } <- setup
1000+ { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1001+ , indexParams: IDBObjectStore.defaultParameters
1002+ , keyPath: []
1003+ , values:
1004+ [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1005+ , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
1006+ , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
1007+ , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
1008+ ]
1009+ , onUpgradeNeeded: Nothing
9751010 }
976− vdone <- makeEmptyVar
977− vjump <- makeVar true
978− tx <- IDBDatabase.transaction db ["store"] ReadOnly
979− store <- IDBTransaction.objectStore tx "store"
980− IDBObjectStore.openCursor store Nothing Next (cb vdone vjump)
981− takeVar vdone
982− tearDown db
983−
984− it "delete() - remove a record from the object store" do
985− { db } <- setup
986− { storeParams : { keyPath: ["pKey"], autoIncrement: false }
987− , indexParams : IDBObjectStore.defaultParameters
988− , keyPath : []
989− , values : [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
990− , { pKey: "pkey_1", iKey: "ikey_1" } :+: Nothing
991− , { pKey: "pkey_2", iKey: "ikey_2" } :+: Nothing
992− , { pKey: "pkey_3", iKey: "ikey_3" } :+: Nothing
993− ]
994− , onUpgradeNeeded: Nothing
995− }
996− let cb vdone store =
997− { onComplete: launchAff' do
998− mval <- map _.pKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
999− mval `shouldEqual` (Nothing :: Maybe String)
1000− putVar unit vdone
1001−
1002− , onError: \error -> launchAff' do
1003− fail $ "unexpected error: " <> show error
1004−
1005− , onSuccess: \cursor -> launchAff' do
1006− let value = IDBCursor.value cursor
1007− value.pKey `shouldEqual` "pkey_0"
1008− IDBCursor.delete cursor
1009− IDBCursor.advance cursor 4
1011+ let
1012+ cb vdone vjump =
1013+ { onComplete: launchAff' do
1014+ AVar.put unit vdone
1015+
1016+ , onError: \error -> launchAff' do
1017+ fail $ "unexpected error: " <> show error
1018+
1019+ , onSuccess: \cursor -> launchAff' do
1020+ jump <- AVar.take vjump
1021+ if jump then do
1022+ IDBCursor.advance cursor 3
1023+ else do
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
1029+ }
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)
1035+ AVar.take vdone
1036+ tearDown db
1037+
1038+ it "delete() - remove a record from the object store" do
1039+ { db } <- setup
1040+ { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1041+ , indexParams: IDBObjectStore.defaultParameters
1042+ , keyPath: []
1043+ , values:
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
1048+ ]
1049+ , onUpgradeNeeded: Nothing
10101050 }
1011− vdone <- makeEmptyVar
1012− tx <- IDBDatabase.transaction db ["store"] ReadWrite
1013− store <- IDBTransaction.objectStore tx "store"
1014− IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1015− takeVar vdone
1016− tearDown db
1017−
1018− it "update() - modify a record in the object store" do
1019− { db } <- setup
1020− { storeParams : { keyPath: ["pKey"], autoIncrement: false }
1021− , indexParams : IDBObjectStore.defaultParameters
1022− , keyPath : []
1023− , values : [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1024− ]
1025− , onUpgradeNeeded: Nothing
1026− }
1027− let cb vdone store =
1028− { onComplete: launchAff' do
1029− mval <- map _.iKey <$> IDBIndex.get store (IDBKeyRange.only "pkey_0")
1030− mval `shouldEqual` (Just "patate")
1031− putVar unit vdone
1032−
1033− , onError: \error -> launchAff' do
1034− fail $ "unexpected error: " <> show error
1035−
1036− , onSuccess: \cursor -> launchAff' do
1037− let value = IDBCursor.value cursor
1038− value.pKey `shouldEqual` "pkey_0"
1039− key <- IDBCursor.update cursor { pKey: "pkey_0", iKey: "patate" }
1040− key `shouldEqual` toKey "pkey_0"
1041− IDBCursor.advance cursor 4
1051+ let
1052+ cb vdone store =
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
1057+
1058+ , onError: \error -> launchAff' do
1059+ fail $ "unexpected error: " <> show error
1060+
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
1066+ }
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)
1071+ AVar.take vdone
1072+ tearDown db
1073+
1074+ it "update() - modify a record in the object store" do
1075+ { db } <- setup
1076+ { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1077+ , indexParams: IDBObjectStore.defaultParameters
1078+ , keyPath: []
1079+ , values:
1080+ [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1081+ ]
1082+ , onUpgradeNeeded: Nothing
10421083 }
1043− vdone <- makeEmptyVar
1044− tx <- IDBDatabase.transaction db ["store"] ReadWrite
1045− store <- IDBTransaction.objectStore tx "store"
1046− IDBObjectStore.openCursor store Nothing Next (cb vdone store)
1047− takeVar vdone
1048− tearDown db
1049−
1050− it "update() - throw ReadOnlyError after update on ReadOnly transaction" do
1051− { db } <- setup
1052− { storeParams : { keyPath: ["pKey"], autoIncrement: false }
1053− , indexParams : IDBObjectStore.defaultParameters
1054− , keyPath : []
1055− , values : [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1056− ]
1057− , onUpgradeNeeded: Nothing
1058− }
1059− let cb vdone =
1060− { onComplete: launchAff' do
1061− fail $ "shouldn't complete"
1062−
1063− , onError: \error -> launchAff' do
1064− fail $ "unexpected error: " <> show error
1065−
1066− , onSuccess: \cursor -> launchAff' do
1067− res <- attempt $ IDBCursor.update cursor "patate"
1068− case res of
1069− Left err -> do
1070− name err `shouldEqual` "ReadOnlyError"
1071− putVar unit vdone
1072− Right _ ->
1073− fail $ "expected ReadOnlyError"
1084+ let
1085+ cb vdone store =
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
1090+
1091+ , onError: \error -> launchAff' do
1092+ fail $ "unexpected error: " <> show error
1093+
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
1100+ }
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)
1105+ AVar.take vdone
1106+ tearDown db
1107+
1108+ it "update() - throw ReadOnlyError after update on ReadOnly transaction" do
1109+ { db } <- setup
1110+ { storeParams: { keyPath: [ "pKey" ], autoIncrement: false }
1111+ , indexParams: IDBObjectStore.defaultParameters
1112+ , keyPath: []
1113+ , values:
1114+ [ { pKey: "pkey_0", iKey: "ikey_0" } :+: Nothing
1115+ ]
1116+ , onUpgradeNeeded: Nothing
10741117 }
1075− vdone <- makeEmptyVar
1076− tx <- IDBDatabase.transaction db ["store"] ReadOnly
1077− store <- IDBTransaction.objectStore tx "store"
1078− IDBObjectStore.openCursor store Nothing Next (cb vdone)
1079− takeVar vdone
1080− tearDown db
1118+ let
1119+ cb vdone =
1120+ { onComplete: launchAff' do
1121+ fail $ "shouldn't complete"
1122+
1123+ , onError: \error -> launchAff' do
1124+ fail $ "unexpected error: " <> show error
1125+
1126+ , onSuccess: \cursor -> launchAff' do
1127+ res <- attempt $ IDBCursor.update cursor "patate"
1128+ case res of
1129+ Left err -> do
1130+ name err `shouldEqual` "ReadOnlyError"
1131+ AVar.put unit vdone
1132+ Right _ ->
1133+ fail $ "expected ReadOnlyError"
1134+ }
1135+ vdone <- AVar.empty
1136+ tx <- IDBDatabase.transaction db [ "store" ] ReadOnly
1137+ store <- IDBTransaction.objectStore tx "store"
1138+ IDBObjectStore.openCursor store Nothing Next (cb vdone)
1139+ AVar.take vdone
1140+ tearDown db