Skip to content

Commit

Permalink
fix(test): postgres (#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos authored Nov 20, 2023
1 parent c0aa511 commit f0fbe62
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/...

4 changes: 3 additions & 1 deletion waku/persistence/postgres/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions waku/persistence/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 19 additions & 19 deletions waku/persistence/utils/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f0fbe62

Please sign in to comment.