From 7c23b42c1161c400b9311fbb4a2f239b378c325e Mon Sep 17 00:00:00 2001 From: "Idiakose O. Sunday" Date: Mon, 7 Oct 2024 22:40:52 +0100 Subject: [PATCH] chore: integrate sqllite tests chore: cleanup --- .github/workflows/go.yml | 1 - .github/workflows/linter.yml | 1 - Makefile | 4 +-- datastore/sql/ingest_test.go | 26 ++++++++++++++++++- .../20240120125102_create_url_tables.down.sql | 0 .../20240120125102_create_url_tables.up.sql | 0 ...20165926_create_http_ingest_table.down.sql | 0 ...0120165926_create_http_ingest_table.up.sql | 0 ...4834_create_users_and_plans_table.down.sql | 0 ...184834_create_users_and_plans_table.up.sql | 0 datastore/{postgres => sql}/postgres_test.go | 13 +++++----- datastore/{sqlite => sql}/sqlite_test.go | 11 ++++---- .../testdata/fixtures/urls.yml | 0 .../testdata/fixtures/users.yml | 0 datastore/sql/url_test.go | 6 ++--- datastore/sql/user_test.go | 4 +-- 16 files changed, 45 insertions(+), 21 deletions(-) rename datastore/{postgres => sql}/migrations/20240120125102_create_url_tables.down.sql (100%) rename datastore/{postgres => sql}/migrations/20240120125102_create_url_tables.up.sql (100%) rename datastore/{postgres => sql}/migrations/20240120165926_create_http_ingest_table.down.sql (100%) rename datastore/{postgres => sql}/migrations/20240120165926_create_http_ingest_table.up.sql (100%) rename datastore/{postgres => sql}/migrations/20240130184834_create_users_and_plans_table.down.sql (100%) rename datastore/{postgres => sql}/migrations/20240130184834_create_users_and_plans_table.up.sql (100%) rename datastore/{postgres => sql}/postgres_test.go (85%) rename datastore/{sqlite => sql}/sqlite_test.go (80%) rename datastore/{postgres => sql}/testdata/fixtures/urls.yml (100%) rename datastore/{postgres => sql}/testdata/fixtures/users.yml (100%) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index e53ded7..6e0fdd1 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -5,7 +5,6 @@ on: push: branches: - main - - feat/sqllite3_support jobs: diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 4d575e4..47fea8b 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -1,7 +1,6 @@ name: golangci-lint on: pull_request: - push: jobs: golangci: name: lint diff --git a/Makefile b/Makefile index eb7cd5d..b6f4a1f 100644 --- a/Makefile +++ b/Makefile @@ -2,10 +2,10 @@ tidy_dependencies: go mod tidy migrate: - migrate -path ./datastore/postgres/migrations/ -database "postgres://sdump:sdump@localhost:3432/sdump?sslmode=disable" up + migrate -path ./datastore/sql/migrations/ -database "postgres://sdump:sdump@localhost:3432/sdump?sslmode=disable" up migrate-down: - migrate -path ./datastore/postgres/migrations/ -database "postgres://sdump:sdump@localhost:3432/sdump?sslmode=disable" down + migrate -path ./datastore/sql/migrations/ -database "postgres://sdump:sdump@localhost:3432/sdump?sslmode=disable" down run-http: go run cmd/*.go diff --git a/datastore/sql/ingest_test.go b/datastore/sql/ingest_test.go index a801607..c228269 100644 --- a/datastore/sql/ingest_test.go +++ b/datastore/sql/ingest_test.go @@ -12,7 +12,7 @@ import ( ) func TestIngestRepository_Create(t *testing.T) { - client, teardownFunc := setupDatabase(t) + client, teardownFunc := setupPostgresDatabase(t) defer teardownFunc() ingestStore := NewIngestRepository(client) @@ -31,3 +31,27 @@ func TestIngestRepository_Create(t *testing.T) { }, })) } + +func TestIngestRepository_Sqllite_Create(t *testing.T) { + client, teardownFunc := setupSqlliteDatabase(t) + defer teardownFunc() + + ingestStore := NewIngestRepository(client) + + urlStore := NewURLRepositoryTable(client) + + // the models use uuid, tricky bit now is setting up sqlite3 uuid support + // This currently fails as the migrations fail + + endpoint, err := urlStore.Get(context.Background(), &sdump.FindURLOptions{ + Reference: "cmltfm6g330l5l1vq110", // see fixtures/urls.yml + }) + require.NoError(t, err) + + require.NoError(t, ingestStore.Create(context.Background(), &sdump.IngestHTTPRequest{ + UrlID: endpoint.ID, + Request: sdump.RequestDefinition{ + Body: "{}", + }, + })) +} diff --git a/datastore/postgres/migrations/20240120125102_create_url_tables.down.sql b/datastore/sql/migrations/20240120125102_create_url_tables.down.sql similarity index 100% rename from datastore/postgres/migrations/20240120125102_create_url_tables.down.sql rename to datastore/sql/migrations/20240120125102_create_url_tables.down.sql diff --git a/datastore/postgres/migrations/20240120125102_create_url_tables.up.sql b/datastore/sql/migrations/20240120125102_create_url_tables.up.sql similarity index 100% rename from datastore/postgres/migrations/20240120125102_create_url_tables.up.sql rename to datastore/sql/migrations/20240120125102_create_url_tables.up.sql diff --git a/datastore/postgres/migrations/20240120165926_create_http_ingest_table.down.sql b/datastore/sql/migrations/20240120165926_create_http_ingest_table.down.sql similarity index 100% rename from datastore/postgres/migrations/20240120165926_create_http_ingest_table.down.sql rename to datastore/sql/migrations/20240120165926_create_http_ingest_table.down.sql diff --git a/datastore/postgres/migrations/20240120165926_create_http_ingest_table.up.sql b/datastore/sql/migrations/20240120165926_create_http_ingest_table.up.sql similarity index 100% rename from datastore/postgres/migrations/20240120165926_create_http_ingest_table.up.sql rename to datastore/sql/migrations/20240120165926_create_http_ingest_table.up.sql diff --git a/datastore/postgres/migrations/20240130184834_create_users_and_plans_table.down.sql b/datastore/sql/migrations/20240130184834_create_users_and_plans_table.down.sql similarity index 100% rename from datastore/postgres/migrations/20240130184834_create_users_and_plans_table.down.sql rename to datastore/sql/migrations/20240130184834_create_users_and_plans_table.down.sql diff --git a/datastore/postgres/migrations/20240130184834_create_users_and_plans_table.up.sql b/datastore/sql/migrations/20240130184834_create_users_and_plans_table.up.sql similarity index 100% rename from datastore/postgres/migrations/20240130184834_create_users_and_plans_table.up.sql rename to datastore/sql/migrations/20240130184834_create_users_and_plans_table.up.sql diff --git a/datastore/postgres/postgres_test.go b/datastore/sql/postgres_test.go similarity index 85% rename from datastore/postgres/postgres_test.go rename to datastore/sql/postgres_test.go index 4513376..7527f6a 100644 --- a/datastore/postgres/postgres_test.go +++ b/datastore/sql/postgres_test.go @@ -1,7 +1,7 @@ //go:build integration // +build integration -package postgres +package sql import ( "context" @@ -9,6 +9,7 @@ import ( "fmt" "testing" + sdumpPostgres "github.com/adelowo/sdump/datastore/postgres" testfixtures "github.com/go-testfixtures/testfixtures/v3" "github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4/database/postgres" @@ -19,7 +20,7 @@ import ( "github.com/uptrace/bun" ) -func prepareTestDatabase(t *testing.T, dsn string) { +func preparePostgresTestDatabase(t *testing.T, dsn string) { t.Helper() var err error @@ -50,9 +51,9 @@ func prepareTestDatabase(t *testing.T, dsn string) { require.NoError(t, fixtures.Load()) } -// setupDatabase spins up a new Postgres container and returns a closure +// setupPostgresDatabase spins up a new Postgres container and returns a closure // please always make sure to call the closure as it is the teardown function -func setupDatabase(t *testing.T) (*bun.DB, func()) { +func setupPostgresDatabase(t *testing.T) (*bun.DB, func()) { t.Helper() var dsn string @@ -83,9 +84,9 @@ func setupDatabase(t *testing.T) (*bun.DB, func()) { dsn = fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", "sdump", "sdump", fmt.Sprintf("localhost:%s", port.Port()), "sdumptest") - prepareTestDatabase(t, dsn) + preparePostgresTestDatabase(t, dsn) - client, err := New(dsn, false) + client, err := sdumpPostgres.New(dsn, false) require.NoError(t, err) return client, func() { diff --git a/datastore/sqlite/sqlite_test.go b/datastore/sql/sqlite_test.go similarity index 80% rename from datastore/sqlite/sqlite_test.go rename to datastore/sql/sqlite_test.go index 84b393b..9246746 100644 --- a/datastore/sqlite/sqlite_test.go +++ b/datastore/sql/sqlite_test.go @@ -1,13 +1,14 @@ //go:build integration // +build integration -package sqlite +package sql import ( "database/sql" "fmt" "testing" + sdumpSqllite "github.com/adelowo/sdump/datastore/sqlite" testfixtures "github.com/go-testfixtures/testfixtures/v3" "github.com/stretchr/testify/require" "github.com/uptrace/bun" @@ -17,7 +18,7 @@ import ( "github.com/golang-migrate/migrate/v4/database/sqlite3" ) -func prepareTestDatabase(t *testing.T, dsn string) { +func prepareSqlliteTestDatabase(t *testing.T, dsn string) { t.Helper() var err error @@ -48,14 +49,14 @@ func prepareTestDatabase(t *testing.T, dsn string) { require.NoError(t, fixtures.Load()) } -func setupDatabase(t *testing.T) (*bun.DB, func()) { +func setupSqlliteDatabase(t *testing.T) (*bun.DB, func()) { t.Helper() dsn := "file::memory:?cache=shared" - prepareTestDatabase(t, dsn) + prepareSqlliteTestDatabase(t, dsn) - client, err := New(dsn, false) + client, err := sdumpSqllite.New(dsn, false) require.NoError(t, err) return client, func() { diff --git a/datastore/postgres/testdata/fixtures/urls.yml b/datastore/sql/testdata/fixtures/urls.yml similarity index 100% rename from datastore/postgres/testdata/fixtures/urls.yml rename to datastore/sql/testdata/fixtures/urls.yml diff --git a/datastore/postgres/testdata/fixtures/users.yml b/datastore/sql/testdata/fixtures/users.yml similarity index 100% rename from datastore/postgres/testdata/fixtures/users.yml rename to datastore/sql/testdata/fixtures/users.yml diff --git a/datastore/sql/url_test.go b/datastore/sql/url_test.go index 15b4a5a..e74bf75 100644 --- a/datastore/sql/url_test.go +++ b/datastore/sql/url_test.go @@ -16,7 +16,7 @@ import ( var userID = uuid.MustParse("8511ac86-5079-42ae-a030-cb46e6dbfbda") func TestURLRepositoryTable_Create(t *testing.T) { - client, teardownFunc := setupDatabase(t) + client, teardownFunc := setupPostgresDatabase(t) defer teardownFunc() urlStore := NewURLRepositoryTable(client) @@ -26,7 +26,7 @@ func TestURLRepositoryTable_Create(t *testing.T) { } func TestURLRepositoryTable_Get(t *testing.T) { - client, teardownFunc := setupDatabase(t) + client, teardownFunc := setupPostgresDatabase(t) defer teardownFunc() urlStore := NewURLRepositoryTable(client) @@ -44,7 +44,7 @@ func TestURLRepositoryTable_Get(t *testing.T) { } func TestURLRepositoryTable_Latest(t *testing.T) { - client, teardownFunc := setupDatabase(t) + client, teardownFunc := setupPostgresDatabase(t) defer teardownFunc() urlStore := NewURLRepositoryTable(client) diff --git a/datastore/sql/user_test.go b/datastore/sql/user_test.go index 67c76bf..7205515 100644 --- a/datastore/sql/user_test.go +++ b/datastore/sql/user_test.go @@ -15,7 +15,7 @@ import ( // var userID = uuid.MustParse("8511ac86-5079-42ae-a030-cb46e6dbfbda") func TestUserRepository_Create(t *testing.T) { - client, teardownFunc := setupDatabase(t) + client, teardownFunc := setupPostgresDatabase(t) defer teardownFunc() userStore := NewUserRepositoryTable(client) @@ -27,7 +27,7 @@ func TestUserRepository_Create(t *testing.T) { } func TestUserRepository_Find(t *testing.T) { - client, teardownFunc := setupDatabase(t) + client, teardownFunc := setupPostgresDatabase(t) defer teardownFunc() userStore := NewUserRepositoryTable(client)