|
| 1 | +package database |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/flashbots/go-template/common" |
| 8 | + "github.com/flashbots/go-template/database/migrations" |
| 9 | + "github.com/flashbots/go-template/database/vars" |
| 10 | + "github.com/jmoiron/sqlx" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + runDBTests = os.Getenv("RUN_DB_TESTS") == "1" //|| true |
| 16 | + testDBDSN = common.GetEnv("TEST_DB_DSN", "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable") |
| 17 | +) |
| 18 | + |
| 19 | +func resetDatabase(t *testing.T) *DatabaseService { |
| 20 | + t.Helper() |
| 21 | + if !runDBTests { |
| 22 | + t.Skip("Skipping database tests") |
| 23 | + } |
| 24 | + |
| 25 | + // Wipe test database |
| 26 | + _db, err := sqlx.Connect("postgres", testDBDSN) |
| 27 | + require.NoError(t, err) |
| 28 | + _, err = _db.Exec(`DROP SCHEMA public CASCADE; CREATE SCHEMA public;`) |
| 29 | + require.NoError(t, err) |
| 30 | + |
| 31 | + db, err := NewDatabaseService(testDBDSN) |
| 32 | + require.NoError(t, err) |
| 33 | + return db |
| 34 | +} |
| 35 | + |
| 36 | +func TestMigrations(t *testing.T) { |
| 37 | + db := resetDatabase(t) |
| 38 | + query := `SELECT COUNT(*) FROM ` + vars.TableMigrations + `;` |
| 39 | + rowCount := 0 |
| 40 | + err := db.DB.QueryRow(query).Scan(&rowCount) |
| 41 | + require.NoError(t, err) |
| 42 | + require.Len(t, migrations.Migrations.Migrations, rowCount) |
| 43 | +} |
| 44 | + |
| 45 | +func Test_DB1(t *testing.T) { |
| 46 | + db := resetDatabase(t) |
| 47 | + x, err := db.SomeQuery() |
| 48 | + require.NoError(t, err) |
| 49 | + require.Equal(t, uint64(0), x) |
| 50 | +} |
0 commit comments