Skip to content

Commit

Permalink
Add generator tests for field types and argument restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleolivo committed Jul 14, 2017
1 parent 90ed919 commit a8c856b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
6 changes: 3 additions & 3 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (g *Generator) WithField(fieldName, fieldType string, fieldOpts interface{}
if bounds, ok := fieldOpts.([2]int); ok {
min, max := bounds[0], bounds[1]
if max < min {
fmt.Printf("max %d cannot be less than min %d\n", max, min)
inform("max %d cannot be less than min %d\n", max, min)
}

g.fields[fieldName] = &IntegerField{min: min, max: max}
Expand All @@ -69,7 +69,7 @@ func (g *Generator) WithField(fieldName, fieldType string, fieldOpts interface{}
if bounds, ok := fieldOpts.([2]float64); ok {
min, max := bounds[0], bounds[1]
if max < min {
fmt.Printf("max %d cannot be less than min %d\n", max, min)
inform("max %d cannot be less than min %d\n", max, min)
}
g.fields[fieldName] = &FloatField{min: min, max: max}
} else {
Expand All @@ -80,7 +80,7 @@ func (g *Generator) WithField(fieldName, fieldType string, fieldOpts interface{}
min, max := bounds[0], bounds[1]
field := &DateField{min: min, max: max}
if !field.ValidBounds() {
fmt.Printf("max %s cannot be before min %s\n", max, min)
inform("max %s cannot be before min %s\n", max, min)
}
g.fields[fieldName] = field
} else {
Expand Down
74 changes: 74 additions & 0 deletions generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,62 @@ func TestWithFieldCreatesCorrectFields(t *testing.T) {
}
}

func TestIntegerRangeIsCorrect(t *testing.T) {
saved := inform
defer func() { inform = saved }()

messageLogged := false

inform = func(message string, values ...interface{}) {
messageLogged = true
}

g := NewGenerator("thing")
g.WithField("age", "integer", [2]int{4, 2})

if !messageLogged {
t.Error("Field 'age' had invalid range, but not logged")
}
}

func TestDecimalRangeIsCorrect(t *testing.T) {
saved := inform
defer func() { inform = saved }()

messageLogged := false

inform = func(message string, values ...interface{}) {
messageLogged = true
}

g := NewGenerator("thing")
timeMin, _ := time.Parse("2006-01-02", "1945-01-01")
timeMax, _ := time.Parse("2006-01-02", "1945-01-02")
g.WithField("dob", "date", [2]time.Time{timeMax, timeMin})

if !messageLogged {
t.Error("Field 'dob' had invalid range, but not logged")
}
}

func TestDateRangeIsCorrect(t *testing.T) {
saved := inform
defer func() { inform = saved }()

messageLogged := false

inform = func(message string, values ...interface{}) {
messageLogged = true
}

g := NewGenerator("thing")
g.WithField("stars", "decimal", [2]float64{4.4, 2.0})

if !messageLogged {
t.Error("Field 'stars' had invalid range, but not logged")
}
}

func TestDuplicatedFieldIsLogged(t *testing.T) {
saved := inform
defer func() { inform = saved }()
Expand Down Expand Up @@ -162,6 +218,24 @@ func TestGenerateProducesCorrectJSON(t *testing.T) {
}

g := NewGenerator("thing")
timeMin, _ := time.Parse("2006-01-02", "1945-01-01")
timeMax, _ := time.Parse("2006-01-02", "1945-01-02")
g.WithField("a", "string", 2)
g.WithField("b", "integer", [2]int{2, 4})
g.WithField("c", "decimal", [2]float64{2.85, 4.50})
g.WithField("d", "date", [2]time.Time{timeMin, timeMax})
g.WithField("e", "dict", "last_name")
g.WithField("f", "dict", "first_name")
g.WithField("g", "dict", "city")
g.WithField("h", "dict", "country")
g.WithField("i", "dict", "state")
g.WithField("j", "dict", "street")
g.WithField("k", "dict", "address")
g.WithField("l", "dict", "email")
g.WithField("m", "dict", "zip_code")
g.WithField("n", "dict", "full_name")
g.WithField("o", "dict", "random_string")
g.WithField("p", "dict", "invalid_type")
g.Generate(1)

if fileCreated != "thing.json" {
Expand Down

0 comments on commit a8c856b

Please sign in to comment.