Skip to content

Commit

Permalink
update testing
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Dec 29, 2023
1 parent ea23b0b commit 95d0491
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 28 deletions.
3 changes: 1 addition & 2 deletions block/block_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func TestMigrateBlockSQlite(t *testing.T) {
}

func TestMigrateBlockPostgres(t *testing.T) {
db, err := getPostgresDB(t)
assert.NoError(t, err)
db := getPostgresDB(t)

assert.NoError(t, block.MigrateBlockDB(db, types.DATABASE_POSTGRES))
}
3 changes: 1 addition & 2 deletions block/block_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

func setupPostgress(t *testing.T) (block.BlockRepository, *sql.DB) {
db, err := getPostgresDB(t)
assert.NoError(t, err)
db := getPostgresDB(t)

// Cleanup any previous data
db.Exec("delete from blocks")
Expand Down
7 changes: 5 additions & 2 deletions block/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (

_ "github.com/lib/pq"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func getPostgresDB(t *testing.T) (*sql.DB, error) {
func getPostgresDB(t *testing.T) *sql.DB {
if os.Getenv("PGHOST") == "" {
t.SkipNow()
}
Expand All @@ -23,7 +24,9 @@ func getPostgresDB(t *testing.T) (*sql.DB, error) {
os.Getenv("PGHOST"),
os.Getenv("PGDATABASE"))

return sql.Open("postgres", connStr)
db, err := sql.Open("postgres", connStr)
assert.NoError(t, err)
return db
}

type testingLogWriter struct {
Expand Down
9 changes: 6 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@ services:
POSTGRES_PASSWORD: enter
volumes:
- "postgres:/var/lib/postgresql/data"
command: -c port=9432

integration-test:
image: golang:1.21.4
depends_on:
- postgres
volumes:
- "go_dir:/go"
- "go_cache:/.cache"
- ".:/data"
environment:
- PGUSER=postgres
- PGPASSWORD=enter
- PGHOST=postgres
- PGPORT=5432
- PGDATABASE=postgres
- PGPORT=9432
- LOGLEVEL=debug
- CGO_ENABLED=0
- GO_FLAGS=-count=1
working_dir: /data
command: ["go", "test", "./...", "-v", "-cover"]
command: ["go", "test", "./...", "-cover"]

volumes:
postgres: {}
go_cache: {}
go_dir: {}
5 changes: 5 additions & 0 deletions mod_storage/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ func TestMigrateModStorageSQlite(t *testing.T) {

assert.NoError(t, mod_storage.MigrateModStorageDB(db, types.DATABASE_SQLITE))
}

func TestMigrateModStoragePostgres(t *testing.T) {
db := getPostgresDB(t)
assert.NoError(t, mod_storage.MigrateModStorageDB(db, types.DATABASE_POSTGRES))
}
14 changes: 7 additions & 7 deletions mod_storage/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type modStoragePostgresRepository struct {
}

func (repo *modStoragePostgresRepository) Get(modname string, key []byte) (*ModStorageEntry, error) {
row := repo.db.QueryRow("select modname,key,value from entries where modname = $1 and key = $2", modname, key)
row := repo.db.QueryRow("select modname,key,value from mod_storage where modname = $1 and key = $2", modname, key)
entry := &ModStorageEntry{}
err := row.Scan(&entry.ModName, &entry.Key, &entry.Value)
if err == sql.ErrNoRows {
Expand All @@ -23,22 +23,22 @@ func (repo *modStoragePostgresRepository) Get(modname string, key []byte) (*ModS
}

func (repo *modStoragePostgresRepository) Create(entry *ModStorageEntry) error {
_, err := repo.db.Exec("insert into entries(modname,key,value) values($1,$2,$3)", entry.ModName, entry.Key, entry.Value)
_, err := repo.db.Exec("insert into mod_storage(modname,key,value) values($1,$2,$3)", entry.ModName, entry.Key, entry.Value)
return err
}

func (repo *modStoragePostgresRepository) Update(entry *ModStorageEntry) error {
_, err := repo.db.Exec("update entries set value = $1 where modname = $2 and key = $3", entry.Value, entry.ModName, entry.Key)
_, err := repo.db.Exec("update mod_storage set value = $1 where modname = $2 and key = $3", entry.Value, entry.ModName, entry.Key)
return err
}

func (repo *modStoragePostgresRepository) Delete(modname string, key []byte) error {
_, err := repo.db.Exec("delete from entries where modname = $1 and key = $2", modname, key)
_, err := repo.db.Exec("delete from mod_storage where modname = $1 and key = $2", modname, key)
return err
}

func (repo *modStoragePostgresRepository) Count() (int64, error) {
row := repo.db.QueryRow("select count(*) from entries")
row := repo.db.QueryRow("select count(*) from mod_storage")
count := int64(0)
err := row.Scan(&count)
return count, err
Expand All @@ -51,7 +51,7 @@ func (repo *modStoragePostgresRepository) Export(z *zip.Writer) error {
}
enc := json.NewEncoder(w)

rows, err := repo.db.Query("select modname,key,value from entries")
rows, err := repo.db.Query("select modname,key,value from mod_storage")
if err != nil {
return err
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (repo *modStoragePostgresRepository) Import(z *zip.Reader) error {
return err
}

_, err := repo.db.Exec("insert into entries(modname,key,value) values($1,$2,$3) on conflict set value = $3", e.ModName, e.Key, e.Value)
_, err := repo.db.Exec("insert into mod_storage(modname,key,value) values($1,$2,$3) on conflict set value = $3", e.ModName, e.Key, e.Value)
if err != nil {
return err
}
Expand Down
39 changes: 39 additions & 0 deletions mod_storage/postgres_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package mod_storage_test

import (
"testing"

"github.com/minetest-go/mtdb/mod_storage"
"github.com/minetest-go/mtdb/types"
"github.com/stretchr/testify/assert"
)

func TestModStoragePostgresRepo(t *testing.T) {
// open db
db := getPostgresDB(t)
repo := mod_storage.NewModStorageRepository(db, types.DATABASE_POSTGRES)
assert.NotNil(t, repo)

// cleanup
_, err := db.Exec("delete from mod_storage")
assert.NoError(t, err)

// create
entry := &mod_storage.ModStorageEntry{
ModName: "mymod",
Key: []byte("mykey"),
Value: []byte("myvalue"),
}
assert.NoError(t, repo.Create(entry))

// count
entry_count, err := repo.Count()
assert.NoError(t, err)
assert.Equal(t, int64(1), entry_count)

// delete
assert.NoError(t, repo.Delete("mymod", []byte("mykey")))
entry, err = repo.Get("mymod", []byte("mykey"))
assert.NoError(t, err)
assert.Nil(t, entry)
}
22 changes: 22 additions & 0 deletions mod_storage/util_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
package mod_storage_test

import (
"database/sql"
"fmt"
"io"
"os"
"testing"

_ "github.com/lib/pq"
"github.com/stretchr/testify/assert"
)

func getPostgresDB(t *testing.T) *sql.DB {
if os.Getenv("PGHOST") == "" {
t.SkipNow()
}

connStr := fmt.Sprintf(
"user=%s password=%s port=%s host=%s dbname=%s sslmode=disable",
os.Getenv("PGUSER"),
os.Getenv("PGPASSWORD"),
os.Getenv("PGPORT"),
os.Getenv("PGHOST"),
os.Getenv("PGDATABASE"))

db, err := sql.Open("postgres", connStr)
assert.NoError(t, err)
return db
}

func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions worldconfig/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
CONFIG_AUTH_BACKEND = "auth_backend"
CONFIG_STORAGE_BACKEND = "mod_storage_backend"
CONFIG_PLAYER_BACKEND = "player_backend"
CONFIG_MOD_STORAGE_BACKEND = "mod_storage_backend"
CONFIG_PSQL_PLAYER_CONNECTION = "pgsql_player_connection"
CONFIG_PSQL_MAP_CONNECTION = "pgsql_connection"
CONFIG_PSQL_AUTH_CONNECTION = "pgsql_auth_connection"
Expand Down
16 changes: 4 additions & 12 deletions worldconfig/parse_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package worldconfig_test

import (
"fmt"
"testing"

"github.com/minetest-go/mtdb/worldconfig"
Expand All @@ -17,20 +16,13 @@ func TestInvalidFile(t *testing.T) {
func TestParseSqlite(t *testing.T) {
cfg, err := worldconfig.Parse("./testdata/world.mt.sqlite")
assert.NoError(t, err)
if cfg[worldconfig.CONFIG_AUTH_BACKEND] != worldconfig.BACKEND_SQLITE3 {
t.Fatal("not sqlite3")
}
assert.Equal(t, worldconfig.BACKEND_SQLITE3, cfg[worldconfig.CONFIG_AUTH_BACKEND])
}

func TestParsePostgres(t *testing.T) {
cfg, err := worldconfig.Parse("./testdata/world.mt.postgres")
assert.NoError(t, err)
fmt.Println(cfg)
if cfg[worldconfig.CONFIG_AUTH_BACKEND] != worldconfig.BACKEND_POSTGRES {
t.Fatal("not postgres")
}

if cfg[worldconfig.CONFIG_PSQL_AUTH_CONNECTION] != "host=/var/run/postgresql user=postgres password=enter dbname=postgres" {
t.Fatal("param err")
}
assert.Equal(t, worldconfig.BACKEND_POSTGRES, cfg[worldconfig.CONFIG_AUTH_BACKEND])
assert.Equal(t, "host=/var/run/postgresql user=postgres password=enter dbname=postgres", cfg[worldconfig.CONFIG_PSQL_AUTH_CONNECTION])
assert.Equal(t, "host=postgres port=5432 user=postgres password=enter dbname=postgres", cfg[worldconfig.CONFIG_PSQL_MOD_STORAGE_CONNECTION])
}
2 changes: 2 additions & 0 deletions worldconfig/testdata/world.mt.postgres
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
gameid = minetest
backend = postgresql
auth_backend = postgresql
mod_storage_backend = postgresql
creative_mode = false
enable_damage = true
player_backend = postgresql
pgsql_connection = host=postgres port=5432 user=postgres password=enter dbname=postgres
pgsql_player_connection = host=postgres port=5432 user=postgres password=enter dbname=postgres
pgsql_auth_connection = host=/var/run/postgresql user=postgres password=enter dbname=postgres
pgsql_mod_storage_connection = host=postgres port=5432 user=postgres password=enter dbname=postgres

0 comments on commit 95d0491

Please sign in to comment.