Started adding unit tests
the build failed Details

This commit is contained in:
konrad 2018-01-16 11:41:11 +01:00 committed by kolaente
parent 6868833854
commit aa6bc74405
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
7 changed files with 160 additions and 0 deletions

View File

@ -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

View File

@ -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; }

35
models/author_test.go Normal file
View File

@ -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)
}

7
models/main_test.go Normal file
View File

@ -0,0 +1,7 @@
package models
import "testing"
func TestMain(m *testing.M) {
MainTest(m, "..")
}

19
models/test_fixtures.go Normal file
View File

@ -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()
}

55
models/unit_tests.go Normal file
View File

@ -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()
}

30
vendor/vendor.json vendored
View File

@ -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"