From aa6bc744057157a54bbf3d5da85601e0d23aa2df Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 16 Jan 2018 11:41:11 +0100 Subject: [PATCH] Started adding unit tests --- .drone.yml | 10 ++++++++ Makefile | 4 +++ models/author_test.go | 35 ++++++++++++++++++++++++++ models/main_test.go | 7 ++++++ models/test_fixtures.go | 19 ++++++++++++++ models/unit_tests.go | 55 +++++++++++++++++++++++++++++++++++++++++ vendor/vendor.json | 30 ++++++++++++++++++++++ 7 files changed, 160 insertions(+) create mode 100644 models/author_test.go create mode 100644 models/main_test.go create mode 100644 models/test_fixtures.go create mode 100644 models/unit_tests.go diff --git a/.drone.yml b/.drone.yml index e34740b..74e999a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -37,6 +37,16 @@ pipeline: when: event: [ push, tag, pull_request ] + test-backend: + image: webhippie/golang:edge + pull: true + environment: + GOPATH: /srv/app + commands: + - make test + when: + event: [ push, tag, pull_request ] + # Build a release when tagging before-static-build: image: karalabe/xgo-latest:latest diff --git a/Makefile b/Makefile index 0057278..73487e5 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,10 @@ clean: go clean -i ./... rm -rf $(EXECUTABLE) $(DIST) $(BINDATA) +.PHONY: test +test: + go test -cover $(PACKAGES) + required-gofmt-version: @go version | grep -q '\(1.7\|1.8\|1.9\)' || { echo "We require go version 1.7 or 1.8 or 1.9 to format code" >&2 && exit 1; } diff --git a/models/author_test.go b/models/author_test.go new file mode 100644 index 0000000..d24bba3 --- /dev/null +++ b/models/author_test.go @@ -0,0 +1,35 @@ +package models + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestAddOrUpdateAuthor(t *testing.T) { + // Create test database + assert.NoError(t, PrepareTestDatabase()) + + // Bootstrap our test author + testauthor := Author{Forename: "test", Lastname: "tsting"} + + // Create a new author + author1, err := AddOrUpdateAuthor(testauthor) + assert.NoError(t, err) + + // Get the new author + author2, exists, err := GetAuthorByID(author1.ID) + assert.NoError(t, err) + assert.True(t, exists) + assert.Equal(t, author2.Forename, testauthor.Forename) + + // Pass an empty author to see if it fails + _, err = AddOrUpdateAuthor(Author{}) + assert.Error(t, err) + + // Update the author + testauthor.ID = 1 + testauthor.Forename = "Lorem Ipsum" + author1updated, err := AddOrUpdateAuthor(testauthor) + assert.NoError(t, err) + assert.Equal(t, testauthor.Forename, author1updated.Forename) +} diff --git a/models/main_test.go b/models/main_test.go new file mode 100644 index 0000000..b5a71ab --- /dev/null +++ b/models/main_test.go @@ -0,0 +1,7 @@ +package models + +import "testing" + +func TestMain(m *testing.M) { + MainTest(m, "..") +} \ No newline at end of file diff --git a/models/test_fixtures.go b/models/test_fixtures.go new file mode 100644 index 0000000..4560210 --- /dev/null +++ b/models/test_fixtures.go @@ -0,0 +1,19 @@ +package models + +import ( + "gopkg.in/testfixtures.v2" +) + +var fixtures *testfixtures.Context + +// InitFixtures initialize test fixtures for a test database +func InitFixtures(helper testfixtures.Helper, dir string) (err error) { + testfixtures.SkipDatabaseNameCheck(true) + fixtures, err = testfixtures.NewFolder(x.DB().DB, helper, dir) + return err +} + +// LoadFixtures load fixtures for a test database +func LoadFixtures() error { + return fixtures.Load() +} diff --git a/models/unit_tests.go b/models/unit_tests.go new file mode 100644 index 0000000..aaca690 --- /dev/null +++ b/models/unit_tests.go @@ -0,0 +1,55 @@ +package models + +import ( + "github.com/go-xorm/xorm" + "github.com/go-xorm/core" + "os" + "gopkg.in/testfixtures.v2" + "testing" + "path/filepath" + "fmt" +) + +func MainTest(m *testing.M, pathToRoot string) { + var err error + fixturesDir := filepath.Join(pathToRoot, "models") + if err = createTestEngine(fixturesDir); err != nil { + fmt.Fprintf(os.Stderr, "Error creating test engine: %v\n", err) + os.Exit(1) + } + + os.Exit(m.Run()) +} + +func createTestEngine(fixturesDir string) error { + var err error + x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared") + if err != nil { + return err + } + x.SetMapper(core.GonicMapper{}) + + // Sync dat shit + x.Sync(&Book{}) + x.Sync(&User{}) + x.Sync(&Publisher{}) + x.Sync(&Author{}) + x.Sync(&AuthorBook{}) + x.Sync(&Status{}) + x.Sync(&Quantity{}) + x.Sync(&quantityRelation{}) + x.Sync(&Item{}) + x.Sync(&UserLog{}) + + // Show SQL-Queries if nessecary + if os.Getenv("UNIT_TESTS_VERBOSE") == "1" { + x.ShowSQL(true) + } + + return InitFixtures(&testfixtures.SQLite{}, fixturesDir) +} + +// PrepareTestDatabase load test fixtures into test database +func PrepareTestDatabase() error { + return LoadFixtures() +} \ No newline at end of file diff --git a/vendor/vendor.json b/vendor/vendor.json index 39592a7..d693c8c 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -6,6 +6,12 @@ "path": "appengine/cloudsql", "revision": "" }, + { + "checksumSHA1": "mrz/kicZiUaHxkyfvC/DyQcr8Do=", + "path": "github.com/davecgh/go-spew/spew", + "revision": "ecdeabc65495df2dec95d7c4a4c3e021903035e5", + "revisionTime": "2017-10-02T20:02:53Z" + }, { "checksumSHA1": "GXOurDGgsLmJs0wounpdWZZRSGw=", "path": "github.com/dgrijalva/jwt-go", @@ -148,6 +154,18 @@ "revision": "d5ffb5c0cca8778699a929b236766f4a7af674e8", "revisionTime": "2017-11-22T00:24:37Z" }, + { + "checksumSHA1": "LuFv4/jlrmFNnDb/5SCSEPAM9vU=", + "path": "github.com/pmezard/go-difflib/difflib", + "revision": "792786c7400a136282c1664665ae0a8db921c6c2", + "revisionTime": "2016-01-10T10:55:54Z" + }, + { + "checksumSHA1": "uyVU5fRfEK17QZuODRjeZX8zDqg=", + "path": "github.com/stretchr/testify/assert", + "revision": "87b1dfb5b2fa649f52695dd9eae19abe404a4308", + "revisionTime": "2017-12-31T12:27:32Z" + }, { "checksumSHA1": "LTOa3BADhwvT0wFCknPueQALm8I=", "path": "github.com/valyala/bytebufferpool", @@ -199,6 +217,18 @@ { "path": "gopkg.in/ini.v1.28.2", "revision": "" + }, + { + "checksumSHA1": "ob4ZeUbmT7SgWIStHaqq0+vjLdI=", + "path": "gopkg.in/testfixtures.v2", + "revision": "f79bf941e2785516a66bcf4de9a21b0b827ed716", + "revisionTime": "2017-10-30T10:15:45Z" + }, + { + "checksumSHA1": "qOmvuDm+F+2nQQecUZBVkZrTn6Y=", + "path": "gopkg.in/yaml.v2", + "revision": "d670f9405373e636a5a2765eea47fac0c9bc91a4", + "revisionTime": "2018-01-09T11:43:31Z" } ], "rootPath": "git.mowie.cc/konrad/Library"