@@ -4,7 +4,8 @@ import java.io.File
44import java .nio .file .Files
55import akka .util .ByteString
66import io .iohk .ethereum .ObjectGenerators
7- import io .iohk .ethereum .db .dataSource .DataSource
7+ import io .iohk .ethereum .db .dataSource .{DataSource , DataSourceUpdate }
8+ import io .iohk .ethereum .db .dataSource .DataSource .{Key , Namespace , Value }
89import org .scalatest .FlatSpec
910import org .scalatestplus .scalacheck .ScalaCheckPropertyChecks
1011
@@ -30,12 +31,19 @@ trait DataSourceIntegrationTestBehavior extends ScalaCheckPropertyChecks with Ob
3031 }
3132 }
3233
34+ def prepareUpdate (
35+ namespace : Namespace = OtherNamespace ,
36+ toRemove : Seq [Key ] = Nil ,
37+ toUpsert : Seq [(Key , Value )] = Nil
38+ ): Seq [DataSourceUpdate ] =
39+ Seq (DataSourceUpdate (namespace, toRemove, toUpsert))
40+
3341 def updateInSeparateCalls (
34- dataSource : DataSource ,
35- toUpsert : Seq [(ByteString , ByteString )]
36- ): DataSource = {
37- toUpsert.foldLeft(dataSource) { case (recDB, keyValuePair) =>
38- recDB .update(OtherNamespace , Seq (), Seq (keyValuePair))
42+ dataSource : DataSource ,
43+ toUpsert : Seq [(ByteString , ByteString )]
44+ ): Unit = {
45+ toUpsert.foreach { keyValuePair =>
46+ dataSource .update(prepareUpdate(toUpsert = Seq (keyValuePair) ))
3947 }
4048 }
4149
@@ -45,8 +53,9 @@ trait DataSourceIntegrationTestBehavior extends ScalaCheckPropertyChecks with Ob
4553 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
4654 withDir { path =>
4755 val keyList = unFilteredKeyList.take(KeyNumberLimit )
48- val db = updateInSeparateCalls(
49- dataSource = createDataSource(path),
56+ val db = createDataSource(path)
57+ updateInSeparateCalls(
58+ dataSource = db,
5059 toUpsert = keyList.zip(keyList)
5160 )
5261 keyList.foreach { key =>
@@ -62,11 +71,8 @@ trait DataSourceIntegrationTestBehavior extends ScalaCheckPropertyChecks with Ob
6271 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
6372 withDir { path =>
6473 val keyList = unFilteredKeyList.take(KeyNumberLimit )
65- val db = createDataSource(path).update(
66- OtherNamespace ,
67- Seq (),
68- keyList.zip(keyList)
69- )
74+ val db = createDataSource(path)
75+ db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
7076
7177 keyList.foreach { key =>
7278 assert(db.get(OtherNamespace , key).contains(key))
@@ -81,21 +87,18 @@ trait DataSourceIntegrationTestBehavior extends ScalaCheckPropertyChecks with Ob
8187 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
8288 withDir { path =>
8389 val keyList = unFilteredKeyList.take(KeyNumberLimit )
84- val db = createDataSource(path).update(
85- OtherNamespace ,
86- Seq (),
87- keyList.zip(keyList)
88- )
90+ val db = createDataSource(path)
91+ db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
8992
9093 val keyListWithExtraByte = keyList.map(1 .toByte +: _)
91- val dbAfterUpdate =
92- updateInSeparateCalls(db, keyList.zip(keyListWithExtraByte))
94+ updateInSeparateCalls(db, keyList.zip(keyListWithExtraByte))
9395
94- keyList.zip(keyListWithExtraByte).foreach { case (key, value) =>
95- assert(dbAfterUpdate.get(OtherNamespace , key).contains(value))
96+ keyList.zip(keyListWithExtraByte).foreach {
97+ case (key, value) =>
98+ assert(db.get(OtherNamespace , key).contains(value))
9699 }
97100
98- dbAfterUpdate .destroy()
101+ db .destroy()
99102 }
100103 }
101104 }
@@ -104,24 +107,18 @@ trait DataSourceIntegrationTestBehavior extends ScalaCheckPropertyChecks with Ob
104107 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
105108 withDir { path =>
106109 val keyList = unFilteredKeyList.take(KeyNumberLimit )
107- val db = createDataSource(path).update(
108- OtherNamespace ,
109- Seq (),
110- keyList.zip(keyList)
111- )
110+ val db = createDataSource(path)
111+ db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
112112
113113 val keyListWithExtraByte = keyList.map(1 .toByte +: _)
114- val dbAfterUpdate = db.update(
115- OtherNamespace ,
116- Seq (),
117- keyList.zip(keyListWithExtraByte)
118- )
114+ db.update(prepareUpdate(toUpsert = keyList.zip(keyListWithExtraByte)))
119115
120- keyList.zip(keyListWithExtraByte).foreach { case (key, value) =>
121- assert(dbAfterUpdate.get(OtherNamespace , key).contains(value))
116+ keyList.zip(keyListWithExtraByte).foreach {
117+ case (key, value) =>
118+ assert(db.get(OtherNamespace , key).contains(value))
122119 }
123120
124- dbAfterUpdate .destroy()
121+ db .destroy()
125122 }
126123 }
127124 }
@@ -131,12 +128,8 @@ trait DataSourceIntegrationTestBehavior extends ScalaCheckPropertyChecks with Ob
131128 withDir { path =>
132129 val keyList = unFilteredKeyList.take(KeyNumberLimit )
133130 val db = createDataSource(path)
134- .update(
135- namespace = OtherNamespace ,
136- toRemove = Seq (),
137- toUpsert = keyList.zip(keyList)
138- )
139- .clear
131+ db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
132+ db.clear()
140133
141134 keyList.foreach { key =>
142135 assert(db.get(OtherNamespace , key).isEmpty)
@@ -151,11 +144,8 @@ trait DataSourceIntegrationTestBehavior extends ScalaCheckPropertyChecks with Ob
151144 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
152145 withDir { path =>
153146 val keyList = unFilteredKeyList.take(KeyNumberLimit )
154- val db = createDataSource(path).update(
155- namespace = OtherNamespace ,
156- toRemove = Seq (),
157- toUpsert = keyList.zip(keyList)
158- )
147+ val db = createDataSource(path)
148+ db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
159149 db.close()
160150
161151 val dbAfterClose = createDataSource(path)
@@ -172,14 +162,11 @@ trait DataSourceIntegrationTestBehavior extends ScalaCheckPropertyChecks with Ob
172162 withDir { path =>
173163 forAll(seqByteStringOfNItemsGen(KeySizeWithoutPrefix )) { unFilteredKeyList : Seq [ByteString ] =>
174164 val keyList = unFilteredKeyList.take(KeyNumberLimit )
175- val db = createDataSource(path).update(
176- namespace = OtherNamespace ,
177- toRemove = Seq (),
178- toUpsert = keyList.zip(keyList)
179- )
165+ val db = createDataSource(path)
166+ db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
180167 db.destroy()
181168
182- assert(! new File (path ).exists())
169+ assert(! new File (" /tmp/iodbDestroy " ).exists())
183170
184171 val dbAfterDestroy = createDataSource(path)
185172 keyList.foreach { key =>
@@ -199,15 +186,15 @@ trait DataSourceIntegrationTestBehavior extends ScalaCheckPropertyChecks with Ob
199186 val db = createDataSource(path)
200187
201188 val valList1 = keyList.map(1 .toByte +: _)
202- db.update(OtherNamespace , Seq (), keyList.zip(valList1))
189+ db.update(prepareUpdate(namespace = OtherNamespace , toUpsert = keyList.zip(valList1) ))
203190
204191 val valList2 = keyList.map(2 .toByte +: _)
205- db.update(OtherNamespace2 , Seq (), keyList.zip(valList2))
192+ db.update(prepareUpdate(namespace = OtherNamespace2 , toUpsert = keyList.zip(valList2) ))
206193
207- keyList.zip(valList1).foreach { case (key, value) =>
208- assert(db.get(OtherNamespace , key).contains(value))
194+ keyList.zip(valList1).foreach {
195+ case (key, value) =>
196+ assert(db.get(OtherNamespace , key).contains(value))
209197 }
210-
211198 keyList.zip(valList2).foreach { case (key, value) =>
212199 assert(db.get(OtherNamespace2 , key).contains(value))
213200 }
@@ -225,31 +212,31 @@ trait DataSourceIntegrationTestBehavior extends ScalaCheckPropertyChecks with Ob
225212 val db = createDataSource(path)
226213
227214 val valList1 = keyList.map(1 .toByte +: _)
228- db.update(OtherNamespace , Seq (), keyList.zip(valList1))
215+ db.update(prepareUpdate(namespace = OtherNamespace , toUpsert = keyList.zip(valList1) ))
229216
230217 val valList2 = keyList.map(2 .toByte +: _)
231- db.update(OtherNamespace2 , Seq (), keyList.zip(valList2))
218+ db.update(prepareUpdate(namespace = OtherNamespace2 , toUpsert = keyList.zip(valList2) ))
232219
233220 // Removal of keys from the OtherNamespace namespace
234- db.update(OtherNamespace , keyList, Nil )
221+ db.update(prepareUpdate(namespace = OtherNamespace , toRemove = keyList) )
235222
236223 keyList.foreach { key =>
237224 assert(db.get(OtherNamespace , key).isEmpty)
238225 }
239- keyList.zip(valList2).foreach { case (key, value) =>
240- assert(db.get(OtherNamespace2 , key).contains(value))
226+ keyList.zip(valList2).foreach {
227+ case (key, value) =>
228+ assert(db.get(OtherNamespace2 , key).contains(value))
241229 }
242230
243231 // Removal of keys from the OtherNamespace2 namespace
244- db.update(OtherNamespace2 , keyList, Nil )
232+ db.update(prepareUpdate(namespace = OtherNamespace2 , toRemove = keyList) )
245233
246234 keyList.foreach { key =>
247235 assert(db.get(OtherNamespace , key).isEmpty)
248236 }
249237 keyList.foreach { key =>
250238 assert(db.get(OtherNamespace2 , key).isEmpty)
251239 }
252-
253240 db.destroy()
254241 }
255242 }
0 commit comments