Skip to content

Commit 34238e7

Browse files
committed
Reduce dependencies on common code in test cases
This reduces the amount of dependencies on code in the dbhub.io/common module in the test cases. The only remaining place where it is used is for the diff function. Eventually this should also be reworked so we can completely eliminate the import of the common module and have an independent test here (i.e. not asking the server to do something, then calling server code to verify its behaviour).
1 parent 43be0da commit 34238e7

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

dbhub_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55
"crypto/tls"
66
"io"
77
"log"
8+
"math/rand"
89
"net/http"
910
"os"
1011
"path/filepath"
1112
"testing"
13+
"time"
1214

1315
"github.com/docker/docker/pkg/fileutils"
1416
sqlite "github.com/gwenn/gosqlite"
@@ -193,7 +195,7 @@ func TestDiff(t *testing.T) {
193195
})
194196

195197
// Copy the database file to a temp location so we can make some changes
196-
newFile := filepath.Join(t.TempDir(), "diff-"+common.RandomString(8)+".sqlite")
198+
newFile := filepath.Join(t.TempDir(), "diff-"+randomString(8)+".sqlite")
197199
_, err = fileutils.CopyFile(dbFile, newFile)
198200
if err != nil {
199201
t.Error(err)
@@ -270,7 +272,7 @@ func TestDiff(t *testing.T) {
270272
assert.Len(t, diffs.Diff, 1)
271273
assert.Equal(t, "foo", diffs.Diff[0].ObjectName)
272274
assert.Equal(t, "table", diffs.Diff[0].ObjectType)
273-
assert.Equal(t, common.DiffType("add"), diffs.Diff[0].Schema.ActionType)
275+
assert.Equal(t, DiffType("add"), diffs.Diff[0].Schema.ActionType)
274276
assert.Equal(t, "CREATE TABLE foo (first integer);", diffs.Diff[0].Schema.Sql)
275277
assert.Equal(t, "", diffs.Diff[0].Schema.Before)
276278
assert.Equal(t, "CREATE TABLE foo (first integer)", diffs.Diff[0].Schema.After)
@@ -288,7 +290,7 @@ func TestDiff(t *testing.T) {
288290
assert.Len(t, diffs.Diff, 1)
289291
assert.Equal(t, "foo", diffs.Diff[0].ObjectName)
290292
assert.Equal(t, "table", diffs.Diff[0].ObjectType)
291-
assert.Equal(t, common.DiffType("add"), diffs.Diff[0].Schema.ActionType)
293+
assert.Equal(t, DiffType("add"), diffs.Diff[0].Schema.ActionType)
292294
assert.Equal(t, "CREATE TABLE foo (first integer);", diffs.Diff[0].Schema.Sql)
293295
assert.Equal(t, "", diffs.Diff[0].Schema.Before)
294296
assert.Equal(t, "CREATE TABLE foo (first integer)", diffs.Diff[0].Schema.After)
@@ -306,7 +308,7 @@ func TestDiff(t *testing.T) {
306308
assert.Len(t, diffs.Diff, 1)
307309
assert.Equal(t, "foo", diffs.Diff[0].ObjectName)
308310
assert.Equal(t, "table", diffs.Diff[0].ObjectType)
309-
assert.Equal(t, common.DiffType("add"), diffs.Diff[0].Schema.ActionType)
311+
assert.Equal(t, DiffType("add"), diffs.Diff[0].Schema.ActionType)
310312
assert.Equal(t, "", diffs.Diff[0].Schema.Sql)
311313
assert.Equal(t, "", diffs.Diff[0].Schema.Before)
312314
assert.Equal(t, "CREATE TABLE foo (first integer)", diffs.Diff[0].Schema.After)
@@ -572,7 +574,7 @@ func TestUploadLive(t *testing.T) {
572574
t.Error(err)
573575
return
574576
}
575-
dbB := filepath.Join(t.TempDir(), "diff-"+common.RandomString(8)+".sqlite")
577+
dbB := filepath.Join(t.TempDir(), "diff-"+randomString(8)+".sqlite")
576578
err = os.WriteFile(dbB, data, 0750)
577579
if err != nil {
578580
t.Error(err)
@@ -617,6 +619,17 @@ func TestWebpage(t *testing.T) {
617619
assert.Equal(t, "https://docker-dev.dbhub.io:9443/default/Assembly Election 2017.sqlite", pageData.WebPage)
618620
}
619621

622+
// randomString generates a random alphanumeric string of the desired length
623+
func randomString(length int) string {
624+
rand.Seed(time.Now().UnixNano())
625+
const alphaNum = "abcdefghijklmnopqrstuvwxyz0123456789"
626+
randomString := make([]byte, length)
627+
for i := range randomString {
628+
randomString[i] = alphaNum[rand.Intn(len(alphaNum))]
629+
}
630+
return string(randomString)
631+
}
632+
620633
// serverConnection is a utility function that sets up the API connection object to the test server, ready for use
621634
func serverConnection(apiKey string) Connection {
622635
// Create a new DBHub.io API object

0 commit comments

Comments
 (0)