-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add more tests and another way to register for custom seed name, also…
… allow programmatic access to seed execution entry
- Loading branch information
1 parent
d694992
commit 0e85906
Showing
9 changed files
with
292 additions
and
27 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
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,10 @@ | ||
[ | ||
{ | ||
"id": "1", | ||
"name": "{\"en\": \"Push\"}" | ||
}, | ||
{ | ||
"id": "2", | ||
"name": "{\"en\": \"Pull\"}" | ||
} | ||
] |
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,65 @@ | ||
package goseeder | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestFindString(t *testing.T) { | ||
input := []string{ | ||
"dandy", | ||
"trout", | ||
"fish", | ||
"more", | ||
"fish", | ||
"ok", | ||
} | ||
|
||
pos, found := findString(input, "fish") | ||
require.Equal(t, 2, pos) | ||
require.Equal(t, true, found) | ||
} | ||
|
||
func TestPrepareStatement(t *testing.T) { | ||
table := "categories" | ||
data := map[string]string{ | ||
"id": "100", | ||
"name": "common", | ||
} | ||
sb, args := prepareStatement(table, data) | ||
|
||
require.Equal( | ||
t, | ||
"insert into categories (id, name) values (?, ?)", | ||
sb.String(), | ||
) | ||
|
||
require.Equal( | ||
t, | ||
[]interface{}{ | ||
int64(100), | ||
"common"}, | ||
args, | ||
) | ||
} | ||
|
||
var testCases = []struct { | ||
name string | ||
value string | ||
expected interface{} | ||
}{ | ||
{"bool", "true", true}, | ||
{"bool", "false", false}, | ||
{"int", "12", int64(12)}, | ||
{"float", "12.77", 12.770000457763672}, | ||
{"string", "justastring", "justastring"}, | ||
{"string_with_nr", "12justastring", "12justastring"}, | ||
} | ||
|
||
func TestParseValue(t *testing.T) { | ||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require.Equal(t, tt.expected, parseValue(tt.value)) | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,27 +1,161 @@ | ||
package goseeder | ||
|
||
import ( | ||
"database/sql" | ||
"flag" | ||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/stretchr/testify/require" | ||
"os" | ||
"testing" | ||
) | ||
|
||
var testCases = []struct { | ||
name string | ||
value string | ||
expected interface{} | ||
}{ | ||
{"bool", "true", true}, | ||
{"bool", "false", false}, | ||
{"int", "12", int64(12)}, | ||
{"float", "12.77", 12.770000457763672}, | ||
{"string", "justastring", "justastring"}, | ||
{"string_with_nr", "12justastring", "12justastring"}, | ||
} | ||
|
||
func TestParseValue(t *testing.T) { | ||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require.Equal(t, tt.expected, parseValue(tt.value)) | ||
}) | ||
type mockStatus struct { | ||
callCount int | ||
callParams []interface{} | ||
} | ||
|
||
func newFnMock() (func(), *mockStatus) { | ||
mockStatus := mockStatus{ | ||
callCount: 0, | ||
callParams: []interface{}{}, | ||
} | ||
fn := func() { | ||
mockStatus.callCount++ | ||
} | ||
|
||
return fn, &mockStatus | ||
} | ||
|
||
func newSeedFnMock() (func(s Seeder), *mockStatus) { | ||
mockStatus := mockStatus{ | ||
callCount: 0, | ||
callParams: []interface{}{}, | ||
} | ||
fnSeeder := func(s Seeder) { | ||
mockStatus.callCount++ | ||
} | ||
|
||
return fnSeeder, &mockStatus | ||
} | ||
|
||
var conProvider = func() *sql.DB { | ||
db, _, _ := sqlmock.New() | ||
return db | ||
} | ||
|
||
func TestWithSeeder_not_seeding(t *testing.T) { | ||
clientMain, mockStatus := newFnMock() | ||
WithSeeder(conProvider, clientMain) | ||
require.Equal(t, 1, mockStatus.callCount) | ||
|
||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) | ||
} | ||
|
||
func TestWithSeeder_seed_all(t *testing.T) { | ||
clientMain, mockFnStatus := newFnMock() | ||
mockSeedFn, mockSeedStatus := newSeedFnMock() | ||
|
||
Registration{ | ||
Name: "test_seed", | ||
Env: "", | ||
}.Complete(mockSeedFn) | ||
|
||
os.Args = []string{ | ||
"", | ||
"--gseed", | ||
} | ||
|
||
WithSeeder(conProvider, clientMain) | ||
require.Equal(t, 0, mockFnStatus.callCount) | ||
require.Equal(t, 1, mockSeedStatus.callCount) | ||
|
||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) | ||
} | ||
|
||
func TestWithSeeder_seed_named(t *testing.T) { | ||
clientMain, mockFnStatus := newFnMock() | ||
mockSeedFn, mockSeedStatus := newSeedFnMock() | ||
mockSeed2Fn, mockSeed2Status := newSeedFnMock() | ||
|
||
Registration{ | ||
Name: "test_seed", | ||
Env: "", | ||
}.Complete(mockSeedFn) | ||
Registration{ | ||
Name: "another_weird_seed", | ||
Env: "", | ||
}.Complete(mockSeed2Fn) | ||
|
||
os.Args = []string{ | ||
"", | ||
"--gseed", | ||
"--gsnames=another_weird_seed", | ||
} | ||
|
||
WithSeeder(conProvider, clientMain) | ||
require.Equal(t, 0, mockFnStatus.callCount) | ||
require.Equal(t, 0, mockSeedStatus.callCount) | ||
require.Equal(t, 1, mockSeed2Status.callCount) | ||
|
||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) | ||
} | ||
|
||
func TestWithSeeder_seed_for_env(t *testing.T) { | ||
clientMain, mockFnStatus := newFnMock() | ||
mockCommonSeedFn, mockCommonSeedStatus := newSeedFnMock() | ||
mockSeedSecretFn, mockSeedSecretStatus := newSeedFnMock() | ||
mockSeedStageFn, mockSeedStageStatus := newSeedFnMock() | ||
|
||
Registration{ | ||
Name: "common_seed", | ||
Env: "", | ||
}.Complete(mockCommonSeedFn) | ||
Registration{ | ||
Name: "secret_env_seed", | ||
Env: "secret", | ||
}.Complete(mockSeedSecretFn) | ||
Registration{ | ||
Name: "stage_env_seed", | ||
Env: "stage", | ||
}.Complete(mockSeedStageFn) | ||
|
||
os.Args = []string{ | ||
"", | ||
"--gseed", | ||
"--gsenv=secret", | ||
} | ||
|
||
WithSeeder(conProvider, clientMain) | ||
require.Equal(t, 0, mockFnStatus.callCount) | ||
require.Equal(t, 0, mockCommonSeedStatus.callCount) | ||
require.Equal(t, 1, mockSeedSecretStatus.callCount) | ||
require.Equal(t, 0, mockSeedStageStatus.callCount) | ||
|
||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) | ||
} | ||
|
||
func TestRegister(t *testing.T) { | ||
Register(dummySeeder) | ||
a := seeders[len(seeders)-1] | ||
require.Equal(t, "", a.env) | ||
require.Equal(t, "dummySeeder", a.name) | ||
require.NotEmpty(t, a.cb) | ||
} | ||
|
||
func TestRegisterForTest(t *testing.T) { | ||
RegisterForTest(dummySeeder) | ||
a := seeders[len(seeders)-1] | ||
require.Equal(t, "test", a.env) | ||
require.Equal(t, "dummySeeder", a.name) | ||
require.NotEmpty(t, a.cb) | ||
} | ||
|
||
func TestRegisterForEnv(t *testing.T) { | ||
RegisterForEnv("mySuperEnv", dummySeeder) | ||
a := seeders[len(seeders)-1] | ||
require.Equal(t, "mySuperEnv", a.env) | ||
require.Equal(t, "dummySeeder", a.name) | ||
require.NotEmpty(t, a.cb) | ||
} | ||
|
||
func dummySeeder(s Seeder) {} |
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
Oops, something went wrong.