Skip to content

Commit

Permalink
chore(store-archive): Remove duplicated code (#2234)
Browse files Browse the repository at this point in the history
* Refactor utility functions for store and archive test.
  • Loading branch information
AlejandroCabeza authored Nov 27, 2023
1 parent c301e88 commit 38e100e
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 196 deletions.
8 changes: 3 additions & 5 deletions tests/common/test_sqlite_migrations.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ import
stew/results,
testutils/unittests
import
../../waku/common/databases/db_sqlite {.all.}
../../waku/common/databases/db_sqlite {.all.},
../waku_archive/archive_utils


proc newTestDatabase(): SqliteDatabase =
SqliteDatabase.new(":memory:").tryGet()

template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]


suite "SQLite - migrations":

test "set and get user version":
## Given
let database = newTestDatabase()
let database = newSqliteDatabase()

## When
let setRes = database.setUserVersion(5)
Expand Down
2 changes: 0 additions & 2 deletions tests/node/test_wakunode_filter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import
testutils
]

let FUTURE_TIMEOUT = 1.seconds

suite "Waku Filter - End to End":
var client {.threadvar.}: WakuNode
var clientPeerId {.threadvar.}: PeerId
Expand Down
56 changes: 56 additions & 0 deletions tests/waku_archive/archive_utils.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{.used.}

import
std/options,
chronos,
chronicles,
libp2p/crypto/crypto

import
../../../waku/[
node/peer_manager,
waku_core,
waku_archive,
waku_archive/common,
waku_archive/driver/sqlite_driver,
common/databases/db_sqlite,
waku_store
],
../testlib/[
common,
wakucore
]


proc newSqliteDatabase*(): SqliteDatabase =
SqliteDatabase.new(":memory:").tryGet()


proc newSqliteArchiveDriver*(): ArchiveDriver =
let database = newSqliteDatabase()
SqliteDriver.new(database).tryGet()


proc newWakuArchive*(driver: ArchiveDriver): WakuArchive =
WakuArchive.new(driver).get()


proc computeArchiveCursor*(pubsubTopic: PubsubTopic, message: WakuMessage): ArchiveCursor =
ArchiveCursor(
pubsubTopic: pubsubTopic,
senderTime: message.timestamp,
storeTime: message.timestamp,
digest: waku_archive.computeDigest(message)
)


proc newArchiveDriverWithMessages*(pubsubTopic: PubSubTopic, msgList: seq[WakuMessage]): ArchiveDriver =
let driver = newSqliteArchiveDriver()

for msg in msgList:
let
msgDigest = waku_archive.computeDigest(msg)
msgHash = computeMessageHash(pubsubTopic, msg)
discard waitFor driver.put(pubsubTopic, msg, msgDigest, msgHash, msg.timestamp)

return driver
13 changes: 3 additions & 10 deletions tests/waku_archive/test_driver_sqlite.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,16 @@ import
../../../waku/waku_archive,
../../../waku/waku_archive/driver/sqlite_driver,
../../../waku/waku_core,
../waku_archive/archive_utils,
../testlib/common,
../testlib/wakucore


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()
let database = newSqliteDatabase()

## When
let driverRes = SqliteDriver.new(database)
Expand All @@ -45,7 +38,7 @@ suite "SQLite driver":
## Given
const contentTopic = "test-content-topic"

let driver = newTestSqliteDriver()
let driver = newSqliteArchiveDriver()

let msg = fakeWakuMessage(contentTopic=contentTopic)

Expand Down
Loading

0 comments on commit 38e100e

Please sign in to comment.