Skip to content

Commit

Permalink
Merge pull request #50 from dpordomingo/kallaxTests
Browse files Browse the repository at this point in the history
Kallax tests
  • Loading branch information
dpordomingo committed Jan 31, 2017
2 parents 004ee3d + fab522f commit 335d899
Show file tree
Hide file tree
Showing 11 changed files with 1,460 additions and 939 deletions.
9 changes: 9 additions & 0 deletions generator/templates/model.tgo
Expand Up @@ -26,15 +26,20 @@ func (r *{{.Name}}) Value(col string) (interface{}, error) {
}

func (r *{{.Name}}) NewRelationshipRecord(field string) (kallax.Record, error) {
{{if .Relationships -}}
switch field {
{{range .Relationships}}case "{{.Name}}":
return new({{$.GenTypeName .}}), nil
{{end}}
}
return nil, fmt.Errorf("kallax: model {{.Name}} has no relationship %s", field)
{{- else -}}
return nil, fmt.Errorf("kallax: model {{.Name}} has no relationships")
{{- end}}
}

func (r *{{.Name}}) SetRelationship(field string, rel interface{}) error {
{{if .Relationships -}}
switch field {
{{range .Relationships}}{{if not .IsOneToManyRelationship}}case "{{.Name}}":
val, ok := rel.(*{{$.GenTypeName .}})
Expand Down Expand Up @@ -64,6 +69,9 @@ func (r *{{.Name}}) SetRelationship(field string, rel interface{}) error {
{{end}}{{end}}
}
return fmt.Errorf("kallax: model {{.Name}} has no relationship %s", field)
{{- else -}}
return fmt.Errorf("kallax: model {{.Name}} has no relationships")
{{- end}}
}

// {{.StoreName}} is the entity to access the records of the type {{.Name}}
Expand Down Expand Up @@ -125,6 +133,7 @@ func (s *{{.StoreName}}) Insert(record *{{.Name}}) error {
{{end}}
{{if .Events.Has "BeforeInsert"}}
if err := record.BeforeInsert(); err != nil {
return err
}
{{end}}
{{if .HasRelationships}}
Expand Down
24 changes: 23 additions & 1 deletion tests/common_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"os"
"reflect"

"github.com/stretchr/testify/suite"
)
Expand Down Expand Up @@ -83,11 +84,32 @@ func (s *BaseTestSuite) QueryFails(queries ...string) bool {

func (s *BaseTestSuite) resetSchema() bool {
return s.QuerySucceed(
fmt.Sprintf(`DROP SCHEMA %s CASCADE;`, database),
fmt.Sprintf(`DROP SCHEMA IF EXISTS %s CASCADE;`, database),
fmt.Sprintf(`CREATE SCHEMA %s;`, database),
)
}

func (s *BaseTestSuite) resultOrError(res interface{}, err error) bool {
if !reflect.ValueOf(res).Elem().IsValid() {
res = nil
}

if err == nil && res == nil {
s.Fail(
`FindOne should return an error or a document, but nothing was returned
TODO: https://github.com/src-d/go-kallax/issues/49`,
)
return false
}

if err != nil && res != nil {
s.Fail("FindOne should return only an error or a document, but it was returned both")
return false
}

return true
}

func envOrDefault(key string, def string) string {
v := os.Getenv(key)
if v == "" {
Expand Down
82 changes: 82 additions & 0 deletions tests/events.go
@@ -1 +1,83 @@
package tests

import "github.com/src-d/go-kallax"

type EventsFixture struct {
kallax.Model `table:"event"`
Checks map[string]bool
MustFailBefore error
MustFailAfter error
}

func newEventsFixture() *EventsFixture {
return &EventsFixture{
Checks: make(map[string]bool, 0),
}
}

func (s *EventsFixture) BeforeInsert() error {
if s.MustFailBefore != nil {
return s.MustFailBefore
}

s.Checks["BeforeInsert"] = true
return nil
}

func (s *EventsFixture) AfterInsert() error {
if s.MustFailAfter != nil {
return s.MustFailAfter
}

s.Checks["AfterInsert"] = true
return nil
}

func (s *EventsFixture) BeforeUpdate() error {
if s.MustFailBefore != nil {
return s.MustFailBefore
}

s.Checks["BeforeUpdate"] = true
return nil
}

func (s *EventsFixture) AfterUpdate() error {
if s.MustFailAfter != nil {
return s.MustFailAfter
}

s.Checks["AfterUpdate"] = true
return nil
}

type EventsSaveFixture struct {
kallax.Model `table:"event"`
Checks map[string]bool
MustFailBefore error
MustFailAfter error
}

func newEventsSaveFixture() *EventsSaveFixture {
return &EventsSaveFixture{
Checks: make(map[string]bool, 0),
}
}

func (s *EventsSaveFixture) BeforeSave() error {
if s.MustFailBefore != nil {
return s.MustFailBefore
}

s.Checks["BeforeSave"] = true
return nil
}

func (s *EventsSaveFixture) AfterSave() error {
if s.MustFailAfter != nil {
return s.MustFailAfter
}

s.Checks["AfterSave"] = true
return nil
}
41 changes: 30 additions & 11 deletions tests/events_test.go
@@ -1,6 +1,13 @@
package tests

/*
import (
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/suite"
)

type EventsSuite struct {
BaseTestSuite
}
Expand All @@ -17,13 +24,26 @@ func TestEventsSuite(t *testing.T) {
suite.Run(t, &EventsSuite{BaseTestSuite{initQueries: schema}})
}

type eventsCheck map[string]bool

func assertEventsPassed(s *EventsSuite, expected eventsCheck, received eventsCheck) {
for expectedEvent, expectedSign := range expected {
receivedSign, ok := received[expectedEvent]
if s.True(ok, fmt.Sprintf("Event '%s' was not received", expectedEvent)) {
s.Equal(expectedSign, receivedSign, expectedEvent)
}
}

s.Equal(len(expected), len(received))
}

func (s *EventsSuite) TestEventsInsert() {
store := NewEventsFixtureStore(s.db)

doc := NewEventsFixture()
err := store.Insert(doc)
s.Nil(err)
s.Equal(map[string]bool{
assertEventsPassed(s, map[string]bool{
"BeforeInsert": true,
"AfterInsert": true,
}, doc.Checks)
Expand All @@ -40,7 +60,7 @@ func (s *EventsSuite) TestEventsUpdate() {
updatedRows, err := store.Update(doc)
s.Nil(err)
s.True(updatedRows > 0)
s.Equal(map[string]bool{
assertEventsPassed(s, map[string]bool{
"BeforeUpdate": true,
"AfterUpdate": true,
}, doc.Checks)
Expand All @@ -55,12 +75,12 @@ func (s *EventsSuite) TestEventsUpdateError() {

doc.MustFailAfter = errors.New("kallax: after")
updatedRows, err := store.Update(doc)
s.True(updatedRows == 0)
s.Equal(int64(0), updatedRows)
s.Equal(doc.MustFailAfter, err)

doc.MustFailBefore = errors.New("kallax: before")
updatedRows, err = store.Update(doc)
s.True(updatedRows == 0)
s.Equal(int64(0), updatedRows)
s.Equal(doc.MustFailBefore, err)
}

Expand All @@ -71,7 +91,7 @@ func (s *EventsSuite) TestEventsSaveOnInsert() {
updated, err := store.Save(doc)
s.Nil(err)
s.False(updated)
s.Equal(map[string]bool{
assertEventsPassed(s, map[string]bool{
"BeforeInsert": true,
"AfterInsert": true,
}, doc.Checks)
Expand All @@ -87,7 +107,7 @@ func (s *EventsSuite) TestEventsSaveOnUpdate() {
updated, err := store.Save(doc)
s.Nil(err)
s.True(updated)
s.Equal(map[string]bool{
assertEventsPassed(s, map[string]bool{
"BeforeUpdate": true,
"AfterUpdate": true,
}, doc.Checks)
Expand All @@ -99,7 +119,7 @@ func (s *EventsSuite) TestEventsSaveInsert() {
doc := NewEventsSaveFixture()
err := store.Insert(doc)
s.Nil(err)
s.Equal(map[string]bool{
assertEventsPassed(s, map[string]bool{
"BeforeSave": true,
"AfterSave": true,
}, doc.Checks)
Expand All @@ -116,7 +136,7 @@ func (s *EventsSuite) TestEventsSaveUpdate() {
updatedRows, err := store.Update(doc)
s.Nil(err)
s.True(updatedRows > 0)
s.Equal(map[string]bool{
assertEventsPassed(s, map[string]bool{
"BeforeSave": true,
"AfterSave": true,
}, doc.Checks)
Expand All @@ -132,10 +152,9 @@ func (s *EventsSuite) TestEventsSaveSave() {
updated, err := store.Save(doc)
s.Nil(err)
s.True(updated)
s.Equal(map[string]bool{
assertEventsPassed(s, map[string]bool{
"AfterInsert": true,
"BeforeSave": true,
"AfterSave": true,
}, doc.Checks)
}
*/

0 comments on commit 335d899

Please sign in to comment.