-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,183 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
42,foo | ||
42,foo | ||
42,foo | ||
42,foo | ||
42,foo | ||
42,foo | ||
42,foo | ||
42,foo | ||
42,foo | ||
42,foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo | ||
42 foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.