Skip to content

Commit

Permalink
Models started
Browse files Browse the repository at this point in the history
  • Loading branch information
heynemann committed Feb 14, 2017
1 parent 285d684 commit 80c8fdb
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 20 deletions.
16 changes: 1 addition & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,58 +51,44 @@ start-deps:
stop-deps:
@env MY_IP=${MY_IP} docker-compose --project-name offers down

test: deps drop-test unit integration acceptance test-coverage-func
test: deps drop-test migrate-test unit integration acceptance test-coverage-func

clear-coverage-profiles:
@find . -name '*.coverprofile' -delete

unit: clear-coverage-profiles unit-run gather-unit-profiles

unit-run:
@echo 'Before unit'
@ginkgo -cover -r -randomizeAllSpecs -randomizeSuites -skipMeasurements ${TEST_PACKAGES}
@echo 'After unit'

gather-unit-profiles:
@echo 'Before gather unit profiles'
@mkdir -p _build
@echo "mode: count" > _build/coverage-unit.out
@bash -c 'for f in $$(find . -name "*.coverprofile"); do tail -n +2 $$f >> _build/coverage-unit.out; done'
@echo 'After gather unit profiles'

integration int: clear-coverage-profiles integration-run gather-integration-profiles

integration-run:
@echo 'Before integration'
@ginkgo -tags integration -cover -r -randomizeAllSpecs -randomizeSuites -skipMeasurements ${TEST_PACKAGES}
@echo 'After integration'

gather-integration-profiles:
@echo 'Before gather integration profiles'
@mkdir -p _build
@echo "mode: count" > _build/coverage-integration.out
@bash -c 'for f in $$(find . -name "*.coverprofile"); do tail -n +2 $$f >> _build/coverage-integration.out; done'
@echo 'After gather integration profiles'

acceptance acc: clear-coverage-profiles acceptance-run

acceptance-run:
@echo 'Before acceptance'
@mkdir -p _build
@rm -f _build/coverage-acceptance.out
@cd features && go test -cover -covermode=count -coverprofile=../_build/coverage-acceptance.out
@echo 'After acceptance'

merge-profiles:
@echo 'Before merge profiles'
@mkdir -p _build
@gocovmerge _build/*.out > _build/coverage-all.out
@echo 'After merge profiles'

test-coverage-func coverage-func: merge-profiles
@echo 'Before coverage func'
@go tool cover -func=_build/coverage-all.out
@echo 'After coverage func'

test-coverage-html coverage-html: merge-profiles
@go tool cover -html=_build/coverage-all.out
11 changes: 11 additions & 0 deletions fixtures/games.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#offers api
#https://github.com/topfreegames/offers

#Licensed under the MIT license:
#http://www.opensource.org/licenses/mit-license
#Copyright © 2017 Top Free Games <backend@tfgco.com>

-
id: 3393cd15-5b5a-4cfb-9725-ddbde660a727
name: game-1
created_at: 2016-01-01 12:30:12
6 changes: 4 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ import:
- sqlx-runner
- package: github.com/cenkalti/backoff
version: ^1.0.0
- package: github.com/go-testfixtures/testfixtures
version: ^2.0.0
2 changes: 1 addition & 1 deletion migrations/0001-CreateGamesTable.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE games (
id UUID PRIMARY KEY,
id UUID PRIMARY KEY DEFAULT uuid_generate_v1(),
name varchar(255) NOT NULL,
metadata JSONB NOT NULL DEFAULT '{}'::JSONB,
created_at timestamp NOT NULL DEFAULT NOW(),
Expand Down
4 changes: 2 additions & 2 deletions migrations/migrations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions models/game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// offers api
// https://github.com/topfreegames/offers
//
// Licensed under the MIT license:
// http://www.opensource.org/licenses/mit-license
// Copyright © 2017 Top Free Games <backend@tfgco.com>

package models

import (
"github.com/mgutz/dat"

"github.com/jmoiron/sqlx/types"
)

//Game represents a tenant in offers API
type Game struct {
ID string `db:"id"`
Name string `db:"name"`
Metadata types.JSONText `db:"metadata"`
CreatedAt dat.NullTime `db:"created_at"`
UpdatedAt dat.NullTime `db:"updated_at"`
}
55 changes: 55 additions & 0 deletions models/game_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// offers api
// https://github.com/topfreegames/offers
//
// Licensed under the MIT license:
// http://www.opensource.org/licenses/mit-license
// Copyright © 2017 Top Free Games <backend@tfgco.com>

package models_test

import (
"github.com/jmoiron/sqlx/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
uuid "github.com/satori/go.uuid"
"github.com/topfreegames/offers/models"
)

var _ = Describe("Games Model", func() {
Describe("Game Instance", func() {
It("Shoud load a game", func() {
var game models.Game
err := db.
Select("*").
From("games").
Where("id = $1", "3393cd15-5b5a-4cfb-9725-ddbde660a727").
QueryStruct(&game)

Expect(game.ID).To(Equal("3393cd15-5b5a-4cfb-9725-ddbde660a727"))
Expect(game.Name).To(Equal("game-1"))
Expect(game.Metadata.String()).To(BeEquivalentTo("{}"))
Expect(err).NotTo(HaveOccurred())
})

It("Should create game", func() {
meta := types.JSONText(
[]byte(`
{"qwe": 123}
`),
)

game := models.Game{
Name: uuid.NewV4().String(),
Metadata: meta,
}
err := db.
InsertInto("games").
Columns("name", "metadata").
Record(&game).
Returning("id", "created_at", "updated_at").
QueryStruct(&game)

Expect(err).NotTo(HaveOccurred())
})
})
})
19 changes: 19 additions & 0 deletions models/models_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,32 @@
package models_test

import (
runner "github.com/mgutz/dat/sqlx-runner"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"

oTesting "github.com/topfreegames/offers/testing"
)

var db *runner.DB

func TestApi(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Offers API - Models Suite")
}

var _ = BeforeSuite(func() {
var err error
db, err = oTesting.GetTestDB()
Expect(err).NotTo(HaveOccurred())

err = oTesting.LoadFixtures(db)
Expect(err).NotTo(HaveOccurred())
})

var _ = AfterSuite(func() {
err := db.DB.Close()
Expect(err).NotTo(HaveOccurred())
})
47 changes: 47 additions & 0 deletions testing/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// offers api
// https://github.com/topfreegames/offers
//
// Licensed under the MIT license:
// http://www.opensource.org/licenses/mit-license
// Copyright © 2017 Top Free Games <backend@tfgco.com>

package testing

import (
"github.com/go-testfixtures/testfixtures"
runner "github.com/mgutz/dat/sqlx-runner"
"github.com/topfreegames/offers/models"
)

var (
fixtures *testfixtures.Context
)

//GetTestDB returns a connection to the test database
func GetTestDB() (*runner.DB, error) {
return models.GetDB(
"localhost", "offers_test", 8585, "disable",
"offers_test", "",
10, 10,
)
}

//LoadFixtures into the DB
func LoadFixtures(db *runner.DB) error {
var err error

if fixtures == nil {
// creating the context that hold the fixtures
// see about all compatible databases in this page below
fixtures, err = testfixtures.NewFolder(db.DB.DB, &testfixtures.PostgreSQL{}, "../fixtures")
if err != nil {
return err
}
}

if err := fixtures.Load(); err != nil {
return err
}

return nil
}

0 comments on commit 80c8fdb

Please sign in to comment.