Skip to content

Commit

Permalink
test: handle clean correctly
Browse files Browse the repository at this point in the history
Signed-off-by: Arnav Gupta <championswimmer@gmail.com>
  • Loading branch information
championswimmer committed Feb 18, 2024
1 parent 20b9e05 commit 27265ed
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,4 @@ fabric.properties
/events.db
/events.db.wal
/duckdb-driver
/GeoLite2-City.mmdb
/GeoLite2-City.mmdb
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,20 @@ build_all: $(BUILDDEPS)
@echo "Building windows amd64 binary..."
@GOOS=windows GOARCH=amd64 go build -o "bin/$(BINARY_NAME)-windows-amd64.exe" src/main.go

test_unit:
test_clean:
@echo "Cleaning test databases..."
@rm -f app.db
@rm -f events.db
@rm -f events.db.wal
@echo "Cleaning test results..."
@rm -f coverage.unit.out
@rm -f coverage.e2e.out

test_unit: test_clean
@echo "Running unit tests..."
@@GOOS=$(OS) GOARCH=$(ARCH) ENV=test go test -count 1 -timeout 10s -race -coverprofile=coverage.unit.out -covermode=atomic -v -coverpkg=./src/... ./src/...

test_e2e:
test_e2e: test_clean
@echo "Running end-to-end tests..."
@@GOOS=$(OS) GOARCH=$(ARCH) ENV=test go test -count 1 -timeout 10s -race -coverprofile=coverage.e2e.out -covermode=atomic -v -coverpkg=./src/... ./tests/...

Expand Down
1 change: 0 additions & 1 deletion src/security/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package security
import (
"onepixel_backend/src/db/models"
"onepixel_backend/src/utils/applogger"
_ "onepixel_backend/tests/providers"
"testing"

"github.com/stretchr/testify/assert"
Expand Down
15 changes: 8 additions & 7 deletions tests/providers/test_db_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ import (
"time"
)

var removeDbOnce sync.Once
var warnDbExistOnce sync.Once

func init() {
db.InjectDBProvider("sqlite", ProvideSqliteDB)
db.InjectDBProvider("duckdb", ProvideDuckDB)

// Remove existing test databases
removeDbOnce.Do(func() {
warnDbExistOnce.Do(func() {
cwd := lo.Must(os.Getwd())
appDbPath := path.Join(cwd, config.DBUrl)
eventDbPath := path.Join(cwd, config.EventDBUrl)
applogger.Debug("App: Removing existing test database file at", appDbPath)
lo.Must0(os.RemoveAll(appDbPath))
applogger.Debug("Events: Removing existing test database file at", eventDbPath)
lo.Must0(os.RemoveAll(eventDbPath))
lo.Must0(os.RemoveAll(eventDbPath + ".wal"))
if _, err := os.Stat(appDbPath); err == nil {
applogger.Error("Test: app.db already exists")
}
if _, err := os.Stat(eventDbPath); err == nil {
applogger.Error("Test: event.db already exists")
}
time.Sleep(1 * time.Second)
})

Expand Down

0 comments on commit 27265ed

Please sign in to comment.