-
Notifications
You must be signed in to change notification settings - Fork 1
/
FunctionalBigtableDataClientSpec.scala
641 lines (561 loc) · 19.2 KB
/
FunctionalBigtableDataClientSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
* Copyright 2022 Permutive
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.permutive.google.bigtable.data
import cats.data.{NonEmptyMap, NonEmptySet}
import cats.effect.IO
import cats.effect.kernel.Resource
import com.google.cloud.bigtable.data.v2.models.Filters.FILTERS
import com.google.cloud.bigtable.data.v2.models._
import com.google.common.primitives.Longs
import com.google.protobuf.ByteString
import com.permutive.google.gcp.types.ProjectId
import com.permutive.testkit.munit.bigtable.BigtableSuite
import com.comcast.ip4s._
import fs2.Chunk
import scala.jdk.CollectionConverters._
class FunctionalBigtableDataClientSpec extends BigtableSuite {
val emptyStringTable = ""
val testTable = "test-table"
val familyA = "a"
val familyB = "b"
val qualifierA = "a"
val key = "key"
val keyByteString = ByteString.copyFromUtf8(key)
val emptyKey = ""
val emptyKeyByteString = ByteString.copyFromUtf8(emptyKey)
lazy val config = BigtableDataClientSettings[IO](
projectId = ProjectId.fromString(projectId).get,
instanceId = instanceId
).withEndpoint(
EndpointSettings.emulator(
bigtableHost,
Port.fromInt(bigtablePort(bigtableEmulator)).get
)
)
lazy val sutResource: Resource[IO, FunctionalBigtableDataClient[IO]] =
FunctionalBigtableDataClient.resource(config)
override def tablesAndColumnFamilies: NonEmptyMap[String, NonEmptySet[String]] =
NonEmptyMap.of(
testTable -> NonEmptySet.of(familyA, familyB),
emptyStringTable -> NonEmptySet.of(familyA, familyB)
)
test(
"FunctionalBigtableDataClient.exists(String) should return false if the row does not exist"
) {
val program: IO[Boolean] =
sutResource.use(sut => sut.exists(testTable, key))
assertIO(program, false)
}
test(
"FunctionalBigtableDataClient.exists(String) should return true if the row exists"
) {
val program: IO[Boolean] =
sutResource.use(sut =>
for {
_ <- IO.delay(
bigtableDataClient.mutateRow(
RowMutation.create(testTable, key).setCell(familyA, qualifierA, 1)
)
)
res <- sut.exists(testTable, key)
} yield res
)
assertIO(program, true)
}
test(
"FunctionalBigtableDataClient.exists(String) should not raise an error if the table is an empty string"
) {
val program: IO[Boolean] =
sutResource.use(sut => sut.exists(emptyStringTable, key))
assertIO(program, false)
}
test(
"FunctionalBigtableDataClient.exists(String) should not raise an error if the key is an empty string"
) {
val program: IO[Boolean] =
sutResource.use(sut => sut.exists(testTable, emptyKey))
assertIO(program, false)
}
test(
"FunctionalBigtableDataClient.exists(ByteString) should return false if the row does not exist"
) {
val program: IO[Boolean] =
sutResource.use(sut => sut.exists(testTable, keyByteString))
assertIO(program, false)
}
test(
"FunctionalBigtableDataClient.exists(ByteString) should return true if the row exists"
) {
val program: IO[Boolean] =
sutResource.use(sut =>
for {
_ <- IO.delay(
bigtableDataClient.mutateRow(
RowMutation.create(testTable, key).setCell(familyA, qualifierA, 1)
)
)
res <- sut.exists(testTable, key)
} yield res
)
assertIO(program, true)
}
test(
"FunctionalBigtableDataClient.exists(ByteString) not raise an error if the table is an empty string"
) {
val program: IO[Boolean] =
sutResource.use(sut => sut.exists(emptyStringTable, keyByteString))
assertIO(program, false)
}
test(
"FunctionalBigtableDataClient.exists(ByteString) not raise an error if the key is an empty string"
) {
val program: IO[Boolean] =
sutResource.use(sut => sut.exists(testTable, emptyKeyByteString))
assertIO(program, false)
}
test(
"FunctionalBigtableDataClient.readRow(String) should return None if the row does not exist"
) {
val program: IO[Option[Row]] =
sutResource.use(sut => sut.readRow(testTable, key, None))
assertIO(program, None)
}
test(
"FunctionalBigtableDataClient.readRow(String) should return None if the row exists but is filtered out"
) {
val program: IO[Option[Row]] =
sutResource.use(sut =>
for {
_ <- IO.delay(
bigtableDataClient.mutateRow(
RowMutation.create(testTable, key).setCell(familyA, qualifierA, 1)
)
)
res <- sut.readRow(
testTable,
key,
Some(FILTERS.key().exactMatch("DO-NOT-MATCH"))
)
} yield res
)
assertIO(program, None)
}
test(
"FunctionalBigtableDataClient.readRow(String) should return the Row if it exists"
) {
sutResource.use(sut =>
for {
_ <- IO.delay(
bigtableDataClient.mutateRow(
RowMutation.create(testTable, key).setCell(familyA, qualifierA, 1)
)
)
res <- sut.readRow(testTable, key, None)
} yield {
assert(clue(res).isDefined, "Row was not found")
assertEquals(res.get.getKey.toStringUtf8, key)
}
)
}
test(
"FunctionalBigtableDataClient.readRow(String) should return None if the table is an empty string"
) {
val program: IO[Option[Row]] =
sutResource.use(sut => sut.readRow(emptyStringTable, key, None))
assertIO(program, None)
}
test(
"FunctionalBigtableDataClient.readRow(String) should return None if the key is an empty string"
) {
val program: IO[Option[Row]] =
sutResource.use(sut => sut.readRow(testTable, emptyKey, None))
assertIO(program, None)
}
test(
"FunctionalBigtableDataClient.readRow(ByteString) should return None if the row does not exist"
) {
val program: IO[Option[Row]] =
sutResource.use(sut => sut.readRow(testTable, keyByteString, None))
assertIO(program, None)
}
test(
"FunctionalBigtableDataClient.readRow(ByteString) should return None if the row exists but is filtered out"
) {
val program: IO[Option[Row]] =
sutResource.use(sut =>
for {
_ <- IO.delay(
bigtableDataClient.mutateRow(
RowMutation.create(testTable, key).setCell(familyA, qualifierA, 1)
)
)
res <- sut.readRow(
testTable,
keyByteString,
Some(FILTERS.key().exactMatch("DO-NOT-MATCH"))
)
} yield res
)
assertIO(program, None)
}
test(
"FunctionalBigtableDataClient.readRow(ByteString) should return the Row if it exists"
) {
sutResource.use(sut =>
for {
_ <- IO.delay(
bigtableDataClient.mutateRow(
RowMutation.create(testTable, key).setCell(familyA, qualifierA, 1)
)
)
res <- sut.readRow(testTable, keyByteString, None)
} yield {
assert(clue(res).isDefined, "Row was not found")
assertEquals(res.get.getKey.toStringUtf8, key)
}
)
}
test(
"FunctionalBigtableDataClient.readRow(ByteString) should return None if the table is an empty string"
) {
val program: IO[Option[Row]] =
sutResource.use(sut => sut.readRow(emptyStringTable, keyByteString, None))
assertIO(program, None)
}
test(
"FunctionalBigtableDataClient.readRow(ByteString) should return None if the key is an empty string"
) {
val program: IO[Option[Row]] =
sutResource.use(sut => sut.readRow(testTable, emptyKeyByteString, None))
assertIO(program, None)
}
test(
"FunctionalBigtableDataClient.readRows should return no rows if none exist"
) {
val program: IO[List[Row]] =
sutResource.use(
_.readRows(
Query.create(testTable).rowKey(key).rowKey("another key"),
streamChunkSize = 128
).compile.toList
)
assertIO(program, List.empty)
}
test(
"FunctionalBigtableDataClient.readRows should return rows which exists"
) {
sutResource.use(sut =>
for {
_ <- IO.delay(
bigtableDataClient.mutateRow(
RowMutation.create(testTable, key).setCell(familyA, qualifierA, 1)
)
)
res <- sut
.readRows(
Query.create(testTable).rowKey(key).rowKey("another key"),
streamChunkSize = 128
)
.compile
.toList
} yield {
assertEquals(clue(res).size, 1, "Incorrect number of rows returned")
assertEquals(res.head.getKey.toStringUtf8, key)
}
)
}
test(
"FunctionalBigtableDataClient.sampleRowKeys should return no rows if none exist"
) {
val program: IO[Chunk[KeyOffset]] =
sutResource.use(_.sampleRowKeys(testTable))
assertIO(program, Chunk.empty)
}
test(
"FunctionalBigtableDataClient.sampleRowKeys should return rows which exists"
) {
sutResource.use(sut =>
for {
_ <- IO.delay(
bigtableDataClient.mutateRow(
RowMutation.create(testTable, key).setCell(familyA, qualifierA, 1)
)
)
res <- sut.sampleRowKeys(testTable)
} yield {
assertEquals(clue(res).size, 1, "Incorrect number of sampled row keys")
assertEquals(res.head.get.getKey.toStringUtf8, key)
}
)
}
test("FunctionalBigtableDataClient.mutateRow should edit a row") {
val keyB = "keyB"
sutResource.use(sut =>
for {
beforeA <- IO.delay(Option(bigtableDataClient.readRow(testTable, key)))
beforeB <- IO.delay(Option(bigtableDataClient.readRow(testTable, keyB)))
_ <- sut.bulkMutateRows(
BulkMutation
.create(testTable)
.add(key, Mutation.create().setCell(familyA, qualifierA, 1))
)
_ <- sut.bulkMutateRows(
BulkMutation
.create(testTable)
.add(keyB, Mutation.create().setCell(familyB, qualifierA, 1))
)
afterA <- IO.delay(Option(bigtableDataClient.readRow(testTable, key)))
afterB <- IO.delay(Option(bigtableDataClient.readRow(testTable, keyB)))
} yield {
assertEquals(beforeA, Option.empty, "Row A existed already")
assertEquals(beforeB, Option.empty, "Row B existed already")
assertNotEquals(afterA, Option.empty[Row], "Row A didn't exist after")
assertEquals(afterA.get.getKey.toStringUtf8, key)
assertNotEquals(afterB, Option.empty[Row], "Row B didn't exist after")
assertEquals(afterB.get.getKey.toStringUtf8, keyB)
}
)
}
test(
"FunctionalBigtableDataClient.bulkMutateRows should edit multiple rows"
) {
sutResource.use(sut =>
for {
before <- IO.delay(Option(bigtableDataClient.readRow(testTable, key)))
_ <- sut.mutateRow(
RowMutation.create(testTable, key).setCell(familyA, qualifierA, 1)
)
after <- IO.delay(Option(bigtableDataClient.readRow(testTable, key)))
} yield {
assertEquals(before, Option.empty, "Row existed already")
assertNotEquals(after, Option.empty[Row], "Row didn't exist after")
assertEquals(after.get.getKey.toStringUtf8, key)
}
)
}
test(
"FunctionalBigtableDataClient.bulkMutateRowsBatcher should edit multiple rows"
) {
val keyB = "keyB"
sutResource
.flatMap(_.bulkMutateRowsBatcher(testTable))
.use(batcher =>
for {
beforeA <- IO
.delay(Option(bigtableDataClient.readRow(testTable, key)))
beforeB <- IO
.delay(Option(bigtableDataClient.readRow(testTable, keyB)))
awaitA <- batcher
.run(RowMutationEntry.create(key).setCell(familyA, qualifierA, 1))
awaitB <- batcher
.run(RowMutationEntry.create(keyB).setCell(familyB, qualifierA, 1))
midA <- IO.delay(Option(bigtableDataClient.readRow(testTable, key)))
midB <- IO.delay(Option(bigtableDataClient.readRow(testTable, keyB)))
_ <- awaitA
_ <- awaitB
afterA <- IO.delay(Option(bigtableDataClient.readRow(testTable, key)))
afterB <- IO
.delay(Option(bigtableDataClient.readRow(testTable, keyB)))
} yield {
assertEquals(beforeA, Option.empty, "Row A existed already")
assertEquals(beforeB, Option.empty, "Row B existed already")
assertEquals(midA, Option.empty, "Row A existed before await")
assertEquals(midB, Option.empty, "Row B existed before await")
assertNotEquals(afterA, Option.empty[Row], "Row A didn't exist after")
assertEquals(afterA.get.getKey.toStringUtf8, key)
assertNotEquals(afterB, Option.empty[Row], "Row B didn't exist after")
assertEquals(afterB.get.getKey.toStringUtf8, keyB)
}
)
}
test(
"FunctionalBigtableDataClient.bulkReadRowsBatcher should read rows, returning None if the row does not exist"
) {
sutResource
.flatMap(_.bulkReadRowsBatcher(testTable, None))
.use(batcher =>
for {
before <- batcher.run(keyByteString).flatten
_ <- IO.delay(
bigtableDataClient.mutateRow(
RowMutation.create(testTable, key).setCell(familyA, qualifierA, 1)
)
)
res <- batcher.run(keyByteString).flatten
} yield {
assertEquals(before, None, "Row existed already")
assert(clue(res).isDefined, "Row was not found")
assertEquals(res.get.getKey.toStringUtf8, key)
}
)
}
test(
"FunctionalBigtableDataClient.bulkReadRowsBatcher should work with a filter"
) {
sutResource
.flatMap(
_.bulkReadRowsBatcher(
testTable,
Some(FILTERS.key().exactMatch("DO-NOT-MATCH"))
)
)
.use(batcher =>
for {
before <- batcher.run(keyByteString).flatten
_ <- IO.delay(
bigtableDataClient.mutateRow(
RowMutation.create(testTable, key).setCell(familyA, qualifierA, 1)
)
)
res <- batcher.run(keyByteString).flatten
} yield {
assertEquals(before, None, "Row existed already")
assertEquals(res, None, "Filter did not work")
}
)
}
test(
"FunctionalBigtableDataClient.checkAndMutateRow should function as expected"
) {
sutResource.use(sut =>
for {
_ <- IO.delay(
bigtableDataClient.mutateRow(
RowMutation
.create(testTable, key)
.setCell(familyA, qualifierA, "initial-value")
)
)
// Update the value of the cell (the condition should match). If condition doesn't match delete
_ <- sut.checkAndMutateRow(
ConditionalRowMutation
.create(testTable, key)
.condition(
FILTERS.value().exactMatch("updated-value")
) // value is currently `initial-value`
.`then`(Mutation.create().deleteRow())
.otherwise(
Mutation.create().setCell(familyA, qualifierA, "updated-value")
)
)
// Now delete the row using another conditional mutation
mid <- IO.delay(Option(bigtableDataClient.readRow(testTable, key)))
_ <- sut.checkAndMutateRow(
ConditionalRowMutation
.create(testTable, key)
.condition(FILTERS.value().exactMatch("updated-value"))
.`then`(Mutation.create().deleteRow())
)
after <- IO.delay(Option(bigtableDataClient.readRow(testTable, key)))
} yield {
assert(clue(mid).isDefined, "Row was not found")
val row = mid.get
assertEquals(row.getKey.toStringUtf8, key)
val cells = row.getCells.asScala.toList
assertEquals(
clue(cells).size,
2
) // 2 cells as we updated the value, first one is the newest
val firstCell :: secondCell :: Nil = cells
assertEquals(firstCell.getValue.toStringUtf8, "updated-value")
assertEquals(secondCell.getValue.toStringUtf8, "initial-value")
assertEquals(after, None)
}
)
}
test(
"FunctionalBigtableDataClient.readModifyWriteRow should function as expected"
) {
sutResource.use { sut =>
val updateOperation: IO[Row] =
sut.readModifyWriteRow(
ReadModifyWriteRow
.create(testTable, key)
.append(familyA, qualifierA, "foo")
.increment(familyB, qualifierA, 1L)
)
for {
before <- IO.delay(Option(bigtableDataClient.readRow(testTable, key)))
firstModification <- updateOperation
secondModification <- updateOperation
// Read only the latest cell from each column
after <- IO.delay(
Option(
bigtableDataClient.readRow(
testTable,
key,
FILTERS.limit().cellsPerColumn(1)
)
)
)
} yield {
assertEquals(before, None, "Row existed already")
val firstCells = firstModification.getCells.asScala.toList
assertEquals(
clue(firstCells).size,
2,
"Wrong number of cells after first modification"
)
val firstCellA :: firstCellB :: Nil = firstCells
assertEquals(
(
firstCellA.getFamily,
firstCellA.getQualifier.toStringUtf8,
firstCellA.getValue.toStringUtf8
),
(familyA, qualifierA, "foo"),
"First cell from first modification does not have expected data"
)
assertEquals(
(
firstCellB.getFamily,
firstCellB.getQualifier.toStringUtf8,
Longs.fromByteArray(firstCellB.getValue.toByteArray)
),
(familyB, qualifierA, 1L),
"Second cell from first modification does not have expected data"
)
val secondCells = secondModification.getCells.asScala.toList
assertEquals(
clue(secondCells).size,
2,
"Wrong number of cells after second modification"
)
val secondCellA :: secondCellB :: Nil = secondCells
assertEquals(
(
secondCellA.getFamily,
secondCellA.getQualifier.toStringUtf8,
secondCellA.getValue.toStringUtf8
),
(familyA, qualifierA, "foofoo"),
"Second cell from second modification does not have expected data"
)
assertEquals(
(
secondCellB.getFamily,
secondCellB.getQualifier.toStringUtf8,
Longs.fromByteArray(secondCellB.getValue.toByteArray)
),
(familyB, qualifierA, 2L),
"Second cell from second modification does not have expected data"
)
assert(clue(after).isDefined, "Row was not found from read")
assertEquals(secondModification, after.get)
}
}
}
}