Skip to content

Commit

Permalink
Introduce integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucapette committed May 19, 2017
1 parent 078593f commit 2f3b2df
Show file tree
Hide file tree
Showing 23 changed files with 1,183 additions and 12 deletions.
9 changes: 5 additions & 4 deletions cmd/fakedata.go
Expand Up @@ -33,7 +33,8 @@ func getFormatter(format string) (f fakedata.Formatter) {
return f
}

func generatorsHelp(generators []fakedata.Generator) string {
func generatorsHelp() string {
generators := fakedata.Generators()
var buffer bytes.Buffer

var max int
Expand All @@ -46,7 +47,7 @@ func generatorsHelp(generators []fakedata.Generator) string {

pattern := fmt.Sprintf("%%-%ds%%s\n", max+2) //+2 makes the output more readable
for _, gen := range generators {
buffer.WriteString(fmt.Sprintf(pattern, gen.Name, gen.Desc))
fmt.Fprintf(&buffer, pattern, gen.Name, gen.Desc)
}

return buffer.String()
Expand All @@ -59,7 +60,7 @@ func main() {
}

if *generatorsFlag {
fmt.Print(generatorsHelp(fakedata.Generators()))
fmt.Print(generatorsHelp())
os.Exit(0)
}

Expand All @@ -85,7 +86,7 @@ func main() {

func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [option ...] field...\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Usage: fakedata [option ...] field...\n\n")
flag.PrintDefaults()
}
flag.Parse()
Expand Down
112 changes: 112 additions & 0 deletions integration/cli_test.go
@@ -0,0 +1,112 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"testing"

"reflect"

"github.com/kr/pretty"
)

var update = flag.Bool("update", false, "update golden files")

var binaryName = "fakedata"

func diff(expected, actual interface{}) []string {
return pretty.Diff(expected, actual)
}

func fixturePath(t *testing.T, fixture string) string {
_, filename, _, ok := runtime.Caller(0)
if !ok {
t.Fatalf("problems recovering caller information")
}

return filepath.Join(filepath.Dir(filename), fixture)
}

func writeFixture(t *testing.T, fixture string, content []byte) {
err := ioutil.WriteFile(fixturePath(t, fixture), content, 0644)
if err != nil {
t.Fatal(err)
}
}

func loadFixture(t *testing.T, fixture string) string {
content, err := ioutil.ReadFile(fixturePath(t, fixture))
if err != nil {
t.Fatal(err)
}

return string(content)
}

func TestCliArgs(t *testing.T) {
tests := []struct {
name string
args []string
fixture string
}{
{"no arguments", []string{}, "help.golden"},
{"list generators", []string{"-g"}, "generators.golden"},
{"default format", []string{"int,42..42", "enum,foo..foo"}, "default-format.golden"},
{"default format with limit short", []string{"-l=5", "int,42..42", "enum,foo..foo"}, "default-format-with-limit.golden"},
{"default format with limit", []string{"--limit=5", "int,42..42", "enum,foo..foo"}, "default-format-with-limit.golden"},
{"csv format short", []string{"-f=csv", "int,42..42", "enum,foo..foo"}, "csv-format.golden"},
{"csv format", []string{"--format=csv", "int,42..42", "enum,foo..foo"}, "csv-format.golden"},
{"tab format", []string{"-f=tab", "int,42..42", "enum,foo..foo"}, "tab-format.golden"},
{"sql format", []string{"-f=sql", "int,42..42", "enum,foo..foo"}, "sql-format.golden"},
{"sql format with table name", []string{"-f=sql", "-t=USERS", "int,42..42", "enum,foo..foo"}, "sql-format-with-table-name.golden"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}

cmd := exec.Command(path.Join(dir, binaryName), tt.args...)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}

if *update {
writeFixture(t, tt.fixture, output)
}

actual := string(output)

expected := loadFixture(t, tt.fixture)

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("diff: %v", diff(expected, actual))
}
})
}
}

func TestMain(m *testing.M) {
err := os.Chdir("..")
if err != nil {
fmt.Printf("could not change dir: %v", err)
os.Exit(1)
}
make := exec.Command("make")
err = make.Run()
if err != nil {
fmt.Printf("could not make binary for %s: %v", binaryName, err)
os.Exit(1)
}

os.Exit(m.Run())
}
10 changes: 10 additions & 0 deletions integration/csv-format.golden
@@ -0,0 +1,10 @@
42,foo
42,foo
42,foo
42,foo
42,foo
42,foo
42,foo
42,foo
42,foo
42,foo
5 changes: 5 additions & 0 deletions integration/default-format-with-limit.golden
@@ -0,0 +1,5 @@
42 foo
42 foo
42 foo
42 foo
42 foo
10 changes: 10 additions & 0 deletions integration/default-format.golden
@@ -0,0 +1,10 @@
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
27 changes: 27 additions & 0 deletions integration/generators.golden
@@ -0,0 +1,27 @@
color one word color
country Full country name
country.code 2-digit country code
date YYYY-MM-DD. Accepts a range in the format YYYY-MM-DD..YYYY-MM-DD. By default, it generates dates in the last year.
domain domain
domain.name example|test
domain.tld name|info|com|org|me|us
double double number
email email
enum a random value from an enum. Defaults to "foo..bar..baz"
event.action clicked|purchased|viewed|watched
http.method DELETE|GET|HEAD|OPTION|PATCH|POST|PUT
int positive integer. Accepts range mix..max (default: 1..1000).
ipv4 ipv4
ipv6 ipv6
latitude latitude
longitude longitude
mac.address mac address
name name.first + " " + name.last
name.first capitalized first name
name.last capitalized last name
product.category Beauty|Games|Movies|Tools|..
product.name invented product name
state Full US state name
state.code 2-digit US state name
timezone tz in the form Area/City
username username using the pattern \w+
7 changes: 7 additions & 0 deletions integration/help.golden
@@ -0,0 +1,7 @@
Usage: fakedata [option ...] field...

-f, --format string generators rows in f format. Available formats: csv|tab|sql
-g, --generators lists available generators
-l, --limit int limits rows up to n (default 10)
-t, --table string table name of the sql format (default "TABLE")
-v, --version shows version information
10 changes: 10 additions & 0 deletions integration/sql-format-with-table-name.golden
@@ -0,0 +1,10 @@
INSERT INTO USERS (int,enum) VALUES ('42','foo');
INSERT INTO USERS (int,enum) VALUES ('42','foo');
INSERT INTO USERS (int,enum) VALUES ('42','foo');
INSERT INTO USERS (int,enum) VALUES ('42','foo');
INSERT INTO USERS (int,enum) VALUES ('42','foo');
INSERT INTO USERS (int,enum) VALUES ('42','foo');
INSERT INTO USERS (int,enum) VALUES ('42','foo');
INSERT INTO USERS (int,enum) VALUES ('42','foo');
INSERT INTO USERS (int,enum) VALUES ('42','foo');
INSERT INTO USERS (int,enum) VALUES ('42','foo');
10 changes: 10 additions & 0 deletions integration/sql-format.golden
@@ -0,0 +1,10 @@
INSERT INTO TABLE (int,enum) VALUES ('42','foo');
INSERT INTO TABLE (int,enum) VALUES ('42','foo');
INSERT INTO TABLE (int,enum) VALUES ('42','foo');
INSERT INTO TABLE (int,enum) VALUES ('42','foo');
INSERT INTO TABLE (int,enum) VALUES ('42','foo');
INSERT INTO TABLE (int,enum) VALUES ('42','foo');
INSERT INTO TABLE (int,enum) VALUES ('42','foo');
INSERT INTO TABLE (int,enum) VALUES ('42','foo');
INSERT INTO TABLE (int,enum) VALUES ('42','foo');
INSERT INTO TABLE (int,enum) VALUES ('42','foo');
10 changes: 10 additions & 0 deletions integration/tab-format.golden
@@ -0,0 +1,10 @@
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
16 changes: 8 additions & 8 deletions pkg/fakedata/generator.go
Expand Up @@ -115,7 +115,7 @@ var mac = func(column Column) string {
return fmt.Sprintf("%x:%x:%x:%x:%x:%x", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255))
}

var latitute = func(column Column) string {
var latitude = func(column Column) string {
return strconv.FormatFloat((rand.Float64()*180)-90, 'f', 6, 64)
}

Expand Down Expand Up @@ -191,7 +191,7 @@ func init() {
}

generators["domain.name"] = Generator{
Name: "domain.tld",
Name: "domain.name",
Desc: "example|test",
Func: withEnum([]string{"example", "test"}),
}
Expand Down Expand Up @@ -234,13 +234,13 @@ func init() {

generators["name.first"] = Generator{
Name: "name.first",
Desc: "capilized first name",
Desc: "capitalized first name",
Func: withDictKey("name.first"),
}

generators["name.last"] = Generator{
Name: "name.last",
Desc: "capilized last name",
Desc: "capitalized last name",
Func: withDictKey("name.last"),
}

Expand Down Expand Up @@ -301,10 +301,10 @@ func init() {
Desc: "mac address",
Func: mac}

generators["latitute"] = Generator{
Name: "latitute",
Desc: "latitute",
Func: latitute,
generators["latitude"] = Generator{
Name: "latitude",
Desc: "latitude",
Func: latitude,
}

generators["longitude"] = Generator{
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/kr/pretty/License

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

9 changes: 9 additions & 0 deletions vendor/github.com/kr/pretty/Readme

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

0 comments on commit 2f3b2df

Please sign in to comment.