2022-11-22 18:40:24 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/sequtils,
|
|
|
|
testutils/unittests,
|
|
|
|
chronos
|
|
|
|
import
|
2023-06-22 09:27:40 +00:00
|
|
|
../../../waku/common/databases/db_sqlite,
|
2023-08-09 17:11:50 +00:00
|
|
|
../../../waku/waku_archive,
|
|
|
|
../../../waku/waku_archive/driver/sqlite_driver,
|
|
|
|
../../../waku/waku_core,
|
2023-02-13 10:43:49 +00:00
|
|
|
../testlib/common,
|
2023-04-05 14:01:51 +00:00
|
|
|
../testlib/wakucore
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc newTestDatabase(): SqliteDatabase =
|
|
|
|
SqliteDatabase.new(":memory:").tryGet()
|
|
|
|
|
|
|
|
proc newTestSqliteDriver(): ArchiveDriver =
|
|
|
|
let db = newTestDatabase()
|
|
|
|
SqliteDriver.new(db).tryGet()
|
|
|
|
|
|
|
|
|
|
|
|
suite "SQLite driver":
|
|
|
|
|
|
|
|
test "init driver and database":
|
|
|
|
## Given
|
|
|
|
let database = newTestDatabase()
|
|
|
|
|
|
|
|
## When
|
|
|
|
let driverRes = SqliteDriver.new(database)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
driverRes.isOk()
|
|
|
|
|
|
|
|
let driver: ArchiveDriver = driverRes.tryGet()
|
|
|
|
check:
|
|
|
|
not driver.isNil()
|
|
|
|
|
|
|
|
## Cleanup
|
2023-05-25 15:34:34 +00:00
|
|
|
(waitFor driver.close()).expect("driver to close")
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
test "insert a message":
|
|
|
|
## Given
|
|
|
|
const contentTopic = "test-content-topic"
|
|
|
|
|
|
|
|
let driver = newTestSqliteDriver()
|
|
|
|
|
|
|
|
let msg = fakeWakuMessage(contentTopic=contentTopic)
|
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
let putRes = waitFor driver.put(DefaultPubsubTopic, msg, computeDigest(msg), msg.timestamp)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
putRes.isOk()
|
|
|
|
|
2023-05-25 15:34:34 +00:00
|
|
|
let storedMsg = (waitFor driver.getAllMessages()).tryGet()
|
2022-11-22 18:40:24 +00:00
|
|
|
check:
|
|
|
|
storedMsg.len == 1
|
|
|
|
storedMsg.all do (item: auto) -> bool:
|
|
|
|
let (pubsubTopic, msg, digest, storeTimestamp) = item
|
|
|
|
msg.contentTopic == contentTopic and
|
|
|
|
pubsubTopic == DefaultPubsubTopic
|
|
|
|
|
|
|
|
## Cleanup
|
2023-05-25 15:34:34 +00:00
|
|
|
(waitFor driver.close()).expect("driver to close")
|