Skip to content

Commit eb5ddd5

Browse files
Shared tests between sqlite and postgres
1 parent 83e2daa commit eb5ddd5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+132
-21861
lines changed

internal/autotag/integration_test.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,43 +45,49 @@ func testTeardown(db database.Database) {
4545
}
4646
}
4747

48-
func getNewDB(databaseFile string) database.Database {
49-
if dbUrl, valid := os.LookupEnv("PGSQL_TEST"); valid {
48+
func IsPostgresTest() *string {
49+
if val, ok := os.LookupEnv("PGSQL_TEST"); ok {
50+
return &val
51+
}
52+
return nil
53+
}
54+
55+
func getNewDB() {
56+
if val := IsPostgresTest(); val != nil {
5057
fmt.Printf("Postgres backend for tests detected\n")
5158
db = postgres.NewDatabase()
5259

53-
if err := db.Open(dbUrl); err != nil {
60+
if err := db.Open(*val); err != nil {
5461
panic(fmt.Sprintf("Could not initialize database: %s", err.Error()))
5562
}
56-
return db
57-
}
63+
} else {
64+
fmt.Printf("SQLite backend for tests detected\n")
65+
db = sqlite.NewDatabase()
66+
67+
// create the database file
68+
f, err := os.CreateTemp("", "*.sqlite")
69+
if err != nil {
70+
panic(fmt.Sprintf("Could not create temporary file: %s", err.Error()))
71+
}
5872

59-
fmt.Printf("SQLite backend for tests detected\n")
60-
db = sqlite.NewDatabase()
73+
f.Close()
74+
databaseFile := f.Name()
6175

62-
if err := db.Open(databaseFile); err != nil {
63-
panic(fmt.Sprintf("Could not initialize database: %s", err.Error()))
76+
if err := db.Open(databaseFile); err != nil {
77+
panic(fmt.Sprintf("Could not initialize database: %s", err.Error()))
78+
}
6479
}
65-
return db
6680
}
6781

6882
func runTests(m *testing.M) int {
69-
// create the database file
70-
f, err := os.CreateTemp("", "*.sqlite")
71-
if err != nil {
72-
panic(fmt.Sprintf("Could not create temporary file: %s", err.Error()))
73-
}
74-
75-
f.Close()
76-
databaseFile := f.Name()
77-
db = getNewDB(databaseFile)
83+
getNewDB()
7884

7985
r = db.Repository()
8086

8187
// defer close and delete the database
8288
defer testTeardown(db)
8389

84-
err = populateDB()
90+
err := populateDB()
8591
if err != nil {
8692
panic(fmt.Sprintf("Could not populate database: %s", err.Error()))
8793
} else {

pkg/postgres/anonymise_test.go renamed to pkg/database/anonymise_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
//go:build pg_integration
2-
// +build pg_integration
1+
//go:build db_integration
2+
// +build db_integration
33

4-
package postgres_test
4+
package database_test
55

66
import (
77
"context"
88
"os"
99
"testing"
1010

1111
"github.com/stashapp/stash/pkg/postgres"
12+
"github.com/stashapp/stash/pkg/sqlite"
1213
)
1314

15+
func IsPostgresTest() *string {
16+
if val, ok := os.LookupEnv("PGSQL_TEST"); ok {
17+
return &val
18+
}
19+
return nil
20+
}
21+
1422
func TestAnonymiser_Anonymise(t *testing.T) {
1523
f, err := os.CreateTemp("", "*.sqlite")
1624
if err != nil {
@@ -22,7 +30,13 @@ func TestAnonymiser_Anonymise(t *testing.T) {
2230
defer os.Remove(f.Name())
2331

2432
// use existing database
25-
anonymiser, err := postgres.NewAnonymiser(db, f.Name())
33+
var anonymiser *sqlite.Anonymiser
34+
if val := IsPostgresTest(); val != nil {
35+
anonymiser, err = postgres.NewAnonymiser(db.(*postgres.Database), f.Name())
36+
} else {
37+
anonymiser, err = sqlite.NewAnonymiser(db.(*sqlite.Database), f.Name())
38+
}
39+
2640
if err != nil {
2741
t.Errorf("Could not create anonymiser: %v", err)
2842
return

pkg/postgres/blob_test.go renamed to pkg/database/blob_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//go:build pg_integration
2-
// +build pg_integration
1+
//go:build db_integration
2+
// +build db_integration
33

4-
package postgres_test
4+
package database_test
55

66
import (
77
"context"

pkg/sqlite/custom_fields_test.go renamed to pkg/database/custom_fields_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//go:build integration
2-
// +build integration
1+
//go:build db_integration
2+
// +build db_integration
33

4-
package sqlite_test
4+
package database_test
55

66
import (
77
"context"

pkg/postgres/file_filter_test.go renamed to pkg/database/file_filter_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//go:build pg_integration
2-
// +build pg_integration
1+
//go:build db_integration
2+
// +build db_integration
33

4-
package postgres_test
4+
package database_test
55

66
import (
77
"context"

pkg/postgres/file_test.go renamed to pkg/database/file_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//go:build pg_integration
2-
// +build pg_integration
1+
//go:build db_integration
2+
// +build db_integration
33

4-
package postgres_test
4+
package database_test
55

66
import (
77
"context"

pkg/postgres/folder_filter_test.go renamed to pkg/database/folder_filter_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//go:build pg_integration
2-
// +build pg_integration
1+
//go:build db_integration
2+
// +build db_integration
33

4-
package postgres_test
4+
package database_test
55

66
import (
77
"context"

pkg/postgres/folder_test.go renamed to pkg/database/folder_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//go:build pg_integration
2-
// +build pg_integration
1+
//go:build db_integration
2+
// +build db_integration
33

4-
package postgres_test
4+
package database_test
55

66
import (
77
"context"

pkg/postgres/gallery_chapter_test.go renamed to pkg/database/gallery_chapter_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//go:build pg_integration
2-
// +build pg_integration
1+
//go:build db_integration
2+
// +build db_integration
33

4-
package postgres_test
4+
package database_test
55

66
import (
77
"context"

pkg/postgres/gallery_test.go renamed to pkg/database/gallery_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//go:build pg_integration
2-
// +build pg_integration
1+
//go:build db_integration
2+
// +build db_integration
33

4-
package postgres_test
4+
package database_test
55

66
import (
77
"context"
@@ -506,7 +506,7 @@ func Test_galleryQueryBuilder_UpdatePartial(t *testing.T) {
506506
CreatedAt: createdAt,
507507
UpdatedAt: updatedAt,
508508
SceneIDs: models.NewRelatedIDs([]int{sceneIDs[sceneIdxWithGallery]}),
509-
TagIDs: models.NewRelatedIDs([]int{tagIDs[tagIdx1WithGallery], tagIDs[tagIdx1WithDupName]}),
509+
TagIDs: models.NewRelatedIDs([]int{tagIDs[tagIdx1WithDupName], tagIDs[tagIdx1WithGallery]}),
510510
PerformerIDs: models.NewRelatedIDs([]int{performerIDs[performerIdx1WithGallery], performerIDs[performerIdx1WithDupName]}),
511511
},
512512
false,

0 commit comments

Comments
 (0)