Skip to content

Commit 7c482f4

Browse files
committed
chore: add bootstrap version tests
1 parent 8084880 commit 7c482f4

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

db/postgres/postgres.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (stri
7070
}
7171
}
7272

73+
var debugEnabled = false
74+
7375
func InitializePostgres(ctx context.Context) {
7476
connection := postgres.Open(fmt.Sprintf(
7577
"sslmode=disable host=%s port=%d user=%s dbname=%s password=%s",
@@ -96,6 +98,10 @@ func InitializePostgres(ctx context.Context) {
9698

9799
db = dbInit
98100

101+
if debugEnabled {
102+
db = db.Debug()
103+
}
104+
99105
dbCache = cache.New(time.Second*5, time.Second*10)
100106

101107
// TODO Create search indexes
@@ -133,3 +139,11 @@ func DBCtx(ctx context.Context) *gorm.DB {
133139
func ClearCache() {
134140
dbCache.Flush()
135141
}
142+
143+
func EnableDebug() {
144+
if db != nil {
145+
db = db.Debug()
146+
}
147+
148+
debugEnabled = true
149+
}

tests/announcements_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66
"github.com/MarvinJWendt/testza"
77

88
"github.com/satisfactorymodding/smr-api/config"
9+
"github.com/satisfactorymodding/smr-api/db/postgres"
910
"github.com/satisfactorymodding/smr-api/generated"
1011
"github.com/satisfactorymodding/smr-api/migrations"
1112
)
1213

1314
func init() {
1415
migrations.SetMigrationDir("../migrations")
1516
config.SetConfigDir("../")
17+
postgres.EnableDebug()
1618
}
1719

1820
func TestAnnouncements(t *testing.T) {

tests/bootstrap_versions_test.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package tests
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
7+
"github.com/MarvinJWendt/testza"
8+
9+
"github.com/satisfactorymodding/smr-api/config"
10+
"github.com/satisfactorymodding/smr-api/db/postgres"
11+
"github.com/satisfactorymodding/smr-api/generated"
12+
"github.com/satisfactorymodding/smr-api/migrations"
13+
)
14+
15+
func init() {
16+
migrations.SetMigrationDir("../migrations")
17+
config.SetConfigDir("../")
18+
postgres.EnableDebug()
19+
}
20+
21+
func TestBootstrapVersions(t *testing.T) {
22+
ctx, client, stop := setup()
23+
defer stop()
24+
25+
token, err := makeUser(ctx)
26+
testza.AssertNoError(t, err)
27+
28+
// Run Twice to detect any cache issues
29+
for i := 0; i < 2; i++ {
30+
version := strconv.Itoa(i+1) + ".0.0"
31+
32+
// Create
33+
createBootstrapVersion := authRequest(`mutation ($version: String!) {
34+
createBootstrapVersion(bootstrapVersion: {
35+
version: $version,
36+
satisfactory_version: 12345,
37+
stability: beta,
38+
link: "example.com",
39+
changelog: "Hello World",
40+
date: "2006-01-02T15:04:05Z"
41+
}) {
42+
id
43+
}
44+
}`, token)
45+
createBootstrapVersion.Var("version", version)
46+
47+
var createBootstrapVersionResponse struct {
48+
CreateBootstrapVersion generated.BootstrapVersion
49+
}
50+
testza.AssertNoError(t, client.Run(ctx, createBootstrapVersion, &createBootstrapVersionResponse))
51+
testza.AssertNotEqual(t, "", createBootstrapVersionResponse.CreateBootstrapVersion.ID)
52+
53+
// Query One
54+
queryBootstrapVersion := authRequest(`query ($id: BootstrapVersionID!) {
55+
getBootstrapVersion(bootstrapVersionID: $id) {
56+
id
57+
version
58+
satisfactory_version
59+
stability
60+
link
61+
changelog
62+
date
63+
}
64+
}`, token)
65+
queryBootstrapVersion.Var("id", createBootstrapVersionResponse.CreateBootstrapVersion.ID)
66+
67+
var queryBootstrapVersionResponse struct {
68+
GetBootstrapVersion generated.BootstrapVersion
69+
}
70+
testza.AssertNoError(t, client.Run(ctx, queryBootstrapVersion, &queryBootstrapVersionResponse))
71+
testza.AssertEqual(t, createBootstrapVersionResponse.CreateBootstrapVersion.ID, queryBootstrapVersionResponse.GetBootstrapVersion.ID)
72+
testza.AssertEqual(t, version, queryBootstrapVersionResponse.GetBootstrapVersion.Version)
73+
testza.AssertEqual(t, 12345, queryBootstrapVersionResponse.GetBootstrapVersion.SatisfactoryVersion)
74+
testza.AssertEqual(t, generated.VersionStabilitiesBeta, queryBootstrapVersionResponse.GetBootstrapVersion.Stability)
75+
testza.AssertEqual(t, "example.com", queryBootstrapVersionResponse.GetBootstrapVersion.Link)
76+
testza.AssertEqual(t, "Hello World", queryBootstrapVersionResponse.GetBootstrapVersion.Changelog)
77+
78+
// Update
79+
updateBootstrapVersion := authRequest(`mutation ($id: BootstrapVersionID!) {
80+
updateBootstrapVersion(
81+
bootstrapVersionId: $id,
82+
bootstrapVersion: {
83+
changelog: "Foo Bar",
84+
}
85+
) {
86+
id
87+
}
88+
}`, token)
89+
updateBootstrapVersion.Var("id", createBootstrapVersionResponse.CreateBootstrapVersion.ID)
90+
91+
var updateBootstrapVersionResponse struct {
92+
UpdateBootstrapVersion generated.BootstrapVersion
93+
}
94+
testza.AssertNoError(t, client.Run(ctx, updateBootstrapVersion, &updateBootstrapVersionResponse))
95+
96+
// Query Many
97+
queryBootstrapVersions := authRequest(`query {
98+
getBootstrapVersions {
99+
count
100+
bootstrap_versions {
101+
id
102+
version
103+
satisfactory_version
104+
stability
105+
link
106+
changelog
107+
date
108+
}
109+
}
110+
}`, token)
111+
112+
var queryBootstrapVersionsResponse struct {
113+
GetBootstrapVersions generated.GetBootstrapVersions
114+
}
115+
testza.AssertNoError(t, client.Run(ctx, queryBootstrapVersions, &queryBootstrapVersionsResponse))
116+
testza.AssertEqual(t, 1, queryBootstrapVersionsResponse.GetBootstrapVersions.Count)
117+
testza.AssertEqual(t, 1, len(queryBootstrapVersionsResponse.GetBootstrapVersions.BootstrapVersions))
118+
testza.AssertEqual(t, createBootstrapVersionResponse.CreateBootstrapVersion.ID, queryBootstrapVersionsResponse.GetBootstrapVersions.BootstrapVersions[0].ID)
119+
testza.AssertEqual(t, version, queryBootstrapVersionsResponse.GetBootstrapVersions.BootstrapVersions[0].Version)
120+
testza.AssertEqual(t, 12345, queryBootstrapVersionsResponse.GetBootstrapVersions.BootstrapVersions[0].SatisfactoryVersion)
121+
testza.AssertEqual(t, generated.VersionStabilitiesBeta, queryBootstrapVersionsResponse.GetBootstrapVersions.BootstrapVersions[0].Stability)
122+
testza.AssertEqual(t, "example.com", queryBootstrapVersionsResponse.GetBootstrapVersions.BootstrapVersions[0].Link)
123+
testza.AssertEqual(t, "Foo Bar", queryBootstrapVersionsResponse.GetBootstrapVersions.BootstrapVersions[0].Changelog)
124+
125+
// Delete
126+
deleteBootstrapVersion := authRequest(`mutation ($id: BootstrapVersionID!) {
127+
deleteBootstrapVersion(bootstrapVersionId: $id)
128+
}`, token)
129+
deleteBootstrapVersion.Var("id", createBootstrapVersionResponse.CreateBootstrapVersion.ID)
130+
131+
var deleteBootstrapVersionResponse struct {
132+
DeleteBootstrapVersion bool
133+
}
134+
testza.AssertNoError(t, client.Run(ctx, deleteBootstrapVersion, &deleteBootstrapVersionResponse))
135+
testza.AssertTrue(t, deleteBootstrapVersionResponse.DeleteBootstrapVersion)
136+
}
137+
}

0 commit comments

Comments
 (0)