From f0fbe62b8dd7244bab1763c177292c57a4fde7fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rich=CE=9Brd?= Date: Mon, 20 Nov 2023 08:51:29 -0400 Subject: [PATCH] fix(test): postgres (#912) --- Makefile | 4 +-- waku/persistence/postgres/mock.go | 4 ++- waku/persistence/postgres/postgres_test.go | 5 ++- waku/persistence/utils/store_test.go | 38 +++++++++++----------- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 8d64a429c..71caed28b 100644 --- a/Makefile +++ b/Makefile @@ -215,8 +215,8 @@ test-onchain-with-race: test-postgres: PG_BUILD_TAGS = ${BUILD_TAGS} include_postgres_tests test-postgres: - ${GOBIN} test -p 1 -v -count 1 -tags="${PG_BUILD_TAGS}" github.com/waku-org/go-waku/waku/persistence + ${GOBIN} test -p 1 -v -count 1 -tags="${PG_BUILD_TAGS}" github.com/waku-org/go-waku/waku/persistence/... test-postgres-with-race: - ${GOBIN} test -race -p 1 -v -count 1 -tags="${PG_BUILD_TAGS}" github.com/waku-org/go-waku/waku/persistence + ${GOBIN} test -race -p 1 -v -count 1 -tags="${PG_BUILD_TAGS}" github.com/waku-org/go-waku/waku/persistence/... diff --git a/waku/persistence/postgres/mock.go b/waku/persistence/postgres/mock.go index ed9457bc7..df601a532 100644 --- a/waku/persistence/postgres/mock.go +++ b/waku/persistence/postgres/mock.go @@ -35,7 +35,9 @@ func ResetDefaultTestPostgresDB(dropDBUrl string) error { func NewMockPgDB() *sql.DB { mockPgDBPort := os.Getenv("TEST_DB_PORT") - + if mockPgDBPort == "" { + mockPgDBPort = "5432" + } // dropDBUrl := fmt.Sprintf(dbUrlTemplate, mockPgDBPort, "template1") if err := ResetDefaultTestPostgresDB(dropDBUrl); err != nil { diff --git a/waku/persistence/postgres/postgres_test.go b/waku/persistence/postgres/postgres_test.go index 32ddc8dc4..d13e9128d 100644 --- a/waku/persistence/postgres/postgres_test.go +++ b/waku/persistence/postgres/postgres_test.go @@ -7,11 +7,10 @@ import ( "testing" "github.com/stretchr/testify/require" - "github.com/waku-org/go-waku/waku/persistence" ) func TestQueries(t *testing.T) { - db := persistence.NewMockPgDB() + db := NewMockPgDB() queries, err := NewQueries("test_queries", db) require.NoError(t, err) @@ -45,7 +44,7 @@ func TestQueries(t *testing.T) { } func TestCreateTable(t *testing.T) { - db := persistence.NewMockPgDB() + db := NewMockPgDB() err := CreateTable(db, "test_create_table") require.NoError(t, err) diff --git a/waku/persistence/utils/store_test.go b/waku/persistence/utils/store_test.go index 6b7b8bbad..d8896b81d 100644 --- a/waku/persistence/utils/store_test.go +++ b/waku/persistence/utils/store_test.go @@ -20,6 +20,7 @@ import ( "github.com/waku-org/go-waku/waku/v2/protocol/store/pb" "github.com/waku-org/go-waku/waku/v2/timesource" "github.com/waku-org/go-waku/waku/v2/utils" + "google.golang.org/protobuf/proto" ) func TestStore(t *testing.T) { @@ -62,7 +63,7 @@ func testDbStore(t *testing.T, db *sql.DB, migrationFn func(*sql.DB) error) { require.NoError(t, err) require.Empty(t, res) - err = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test", utils.GetUnixEpoch()), utils.GetUnixEpoch(), "test")) + err = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test", utils.GetUnixEpoch()), *utils.GetUnixEpoch(), "test")) require.NoError(t, err) res, err = store.GetAll() @@ -78,19 +79,18 @@ func testStoreRetention(t *testing.T, db *sql.DB, migrationFn func(*sql.DB) erro require.NoError(t, err) insertTime := time.Now() - ////////////////////////////////// - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test1", insertTime.Add(-70*time.Second).UnixNano()), insertTime.Add(-70*time.Second).UnixNano(), "test")) - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test2", insertTime.Add(-60*time.Second).UnixNano()), insertTime.Add(-60*time.Second).UnixNano(), "test")) - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test3", insertTime.Add(-50*time.Second).UnixNano()), insertTime.Add(-50*time.Second).UnixNano(), "test")) - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test4", insertTime.Add(-40*time.Second).UnixNano()), insertTime.Add(-40*time.Second).UnixNano(), "test")) - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test5", insertTime.Add(-30*time.Second).UnixNano()), insertTime.Add(-30*time.Second).UnixNano(), "test")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test1", proto.Int64(insertTime.Add(-70*time.Second).UnixNano())), insertTime.Add(-70*time.Second).UnixNano(), "test")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test2", proto.Int64(insertTime.Add(-60*time.Second).UnixNano())), insertTime.Add(-60*time.Second).UnixNano(), "test")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test3", proto.Int64(insertTime.Add(-50*time.Second).UnixNano())), insertTime.Add(-50*time.Second).UnixNano(), "test")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test4", proto.Int64(insertTime.Add(-40*time.Second).UnixNano())), insertTime.Add(-40*time.Second).UnixNano(), "test")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test5", proto.Int64(insertTime.Add(-30*time.Second).UnixNano())), insertTime.Add(-30*time.Second).UnixNano(), "test")) dbResults, err := store.GetAll() require.NoError(t, err) require.Len(t, dbResults, 5) - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test6", insertTime.Add(-20*time.Second).UnixNano()), insertTime.Add(-20*time.Second).UnixNano(), "test")) - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test7", insertTime.Add(-10*time.Second).UnixNano()), insertTime.Add(-10*time.Second).UnixNano(), "test")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test6", proto.Int64(insertTime.Add(-20*time.Second).UnixNano())), insertTime.Add(-20*time.Second).UnixNano(), "test")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test7", proto.Int64(insertTime.Add(-10*time.Second).UnixNano())), insertTime.Add(-10*time.Second).UnixNano(), "test")) // This step simulates starting go-waku again from scratch @@ -118,11 +118,11 @@ func testQuery(t *testing.T, db *sql.DB, migrationFn func(*sql.DB) error) { insertTime := time.Now() ////////////////////////////////// - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test1", insertTime.Add(-40*time.Second).UnixNano()), insertTime.Add(-10*time.Second).UnixNano(), "test")) - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test2", insertTime.Add(-30*time.Second).UnixNano()), insertTime.Add(-10*time.Second).UnixNano(), "test")) - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test3", insertTime.Add(-20*time.Second).UnixNano()), insertTime.Add(-10*time.Second).UnixNano(), "test")) - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test3", insertTime.Add(-20*time.Second).UnixNano()), insertTime.Add(-10*time.Second).UnixNano(), "test2")) - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test4", insertTime.Add(-10*time.Second).UnixNano()), insertTime.Add(-10*time.Second).UnixNano(), "test")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test1", proto.Int64(insertTime.Add(-40*time.Second).UnixNano())), insertTime.Add(-10*time.Second).UnixNano(), "test")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test2", proto.Int64(insertTime.Add(-30*time.Second).UnixNano())), insertTime.Add(-10*time.Second).UnixNano(), "test")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test3", proto.Int64(insertTime.Add(-20*time.Second).UnixNano())), insertTime.Add(-10*time.Second).UnixNano(), "test")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test3", proto.Int64(insertTime.Add(-20*time.Second).UnixNano())), insertTime.Add(-10*time.Second).UnixNano(), "test2")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test4", proto.Int64(insertTime.Add(-10*time.Second).UnixNano())), insertTime.Add(-10*time.Second).UnixNano(), "test")) // Range [startTime-endTime] // Check: matching ContentTopics and pubsubTopic, and ts of msg in range @@ -135,13 +135,13 @@ func testQuery(t *testing.T, db *sql.DB, migrationFn func(*sql.DB) error) { {ContentTopic: "test3"}, }, PagingInfo: &pb.PagingInfo{PageSize: 10}, - StartTime: insertTime.Add(-40 * time.Second).UnixNano(), - EndTime: insertTime.Add(-20 * time.Second).UnixNano(), + StartTime: proto.Int64(insertTime.Add(-40 * time.Second).UnixNano()), + EndTime: proto.Int64(insertTime.Add(-20 * time.Second).UnixNano()), }) require.NoError(t, err) require.Len(t, msgs, 3) - _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test5", insertTime.UnixNano()), insertTime.Add(-10*time.Second).UnixNano(), "test")) + _ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test5", proto.Int64(insertTime.UnixNano())), insertTime.Add(-10*time.Second).UnixNano(), "test")) // Range [cursor-endTime] // Check: matching ContentTopic,pubsubTopic, pageSize @@ -157,7 +157,7 @@ func testQuery(t *testing.T, db *sql.DB, migrationFn func(*sql.DB) error) { {ContentTopic: "test5"}, }, PagingInfo: &pb.PagingInfo{Cursor: cursor, PageSize: uint64(pageSize), Direction: pb.PagingInfo_FORWARD}, - EndTime: insertTime.Add(1 * time.Second).UnixNano(), + EndTime: proto.Int64(insertTime.Add(1 * time.Second).UnixNano()), }) require.NoError(t, err) require.Len(t, msgs, pageSize) // due to pageSize @@ -180,7 +180,7 @@ func testQuery(t *testing.T, db *sql.DB, migrationFn func(*sql.DB) error) { {ContentTopic: "test4"}, }, PagingInfo: &pb.PagingInfo{Cursor: cursor2, PageSize: 4, Direction: pb.PagingInfo_BACKWARD}, - StartTime: insertTime.Add(-39 * time.Second).UnixNano(), + StartTime: proto.Int64(insertTime.Add(-39 * time.Second).UnixNano()), }) require.NoError(t, err) require.Len(t, msgs, 3) // due to pageSize