forked from corestoreio/pkg
/
tests.go
85 lines (72 loc) · 2.65 KB
/
tests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package dmlgen
import (
"fmt"
"strconv"
"github.com/corestoreio/pkg/util/codegen"
)
func (g *Generator) fnTestMainOther(testGen *codegen.Go, tbls tables) {
// Test Header
lenBefore := testGen.Len()
var codeWritten int
testGen.Pln(`func TestNewDBManagerNonDB_` + tbls.nameID() + `(t *testing.T) {`)
{
testGen.Pln(`ps := pseudo.MustNewService(0, &pseudo.Options{Lang: "de",MaxFloatDecimals:6})`)
// If some features haven't been enabled, then there are no tests so
// assign ps to underscore to avoid the unused variable error.
// Alternatively figure out how not to print the whole test function at
// all.
testGen.Pln(`_ = ps`)
for _, t := range tbls {
codeWritten += t.generateTestOther(testGen, g)
}
}
testGen.Pln(`}`) // end TestNewDBManager
if codeWritten == 0 {
testGen.Truncate(lenBefore)
}
}
func (g *Generator) fnTestMainDB(testGen *codegen.Go, tbls tables) {
if !tbls.hasFeature(g, FeatureDB) {
return
}
// Test Header
testGen.Pln(`func TestNewDBManagerDB_` + tbls.nameID() + `(t *testing.T) {`)
{
testGen.Pln(`db := dmltest.MustConnectDB(t)`)
testGen.Pln(`defer dmltest.Close(t, db)`)
if g.TestSQLDumpGlobPath != "" {
testGen.Pln(`defer dmltest.SQLDumpLoad(t,`, strconv.Quote(g.TestSQLDumpGlobPath), `, &dmltest.SQLDumpOptions{
SkipDBCleanup: true,
}).Deferred()`)
}
testGen.Pln(`ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)`)
testGen.Pln(`defer cancel()`)
testGen.Pln(`tbls, err := NewDBManager(ctx, &DBMOption{TableOptions: []ddl.TableOption{ddl.WithConnPool(db)}} )`)
testGen.Pln(`assert.NoError(t, err)`)
testGen.Pln(`tblNames := tbls.Tables.Tables()`)
testGen.Pln(`sort.Strings(tblNames)`)
testGen.Pln(`assert.Exactly(t, `, fmt.Sprintf("%#v", g.sortedTableNames()), `, tblNames)`)
testGen.Pln(`err = tbls.Validate(ctx)`)
testGen.Pln(`assert.NoError(t, err)`)
testGen.Pln(`var ps *pseudo.Service`)
testGen.Pln(`ps = pseudo.MustNewService(0, &pseudo.Options{Lang: "de",MaxFloatDecimals:6},`)
testGen.In()
testGen.Pln(`pseudo.WithTagFakeFunc("website_id", func(maxLen int) interface{} {`)
testGen.Pln(` return 1`)
testGen.Pln(`}),`)
testGen.Pln(`pseudo.WithTagFakeFunc("store_id", func(maxLen int) interface{} {`)
testGen.Pln(` return 1`)
testGen.Pln(`}),`)
if fn, ok := g.customCode["pseudo.MustNewService.Option"]; ok {
fn(g, nil, testGen)
}
testGen.Out()
testGen.Pln(`)`)
for _, t := range tbls {
t.generateTestDB(testGen)
} // end for tables
}
testGen.C(`Uncomment the next line for debugging to see all the queries.`)
testGen.Pln(`// t.Logf("queries: %#v", tbls.ConnPool.CachedQueries())`)
testGen.Pln(`}`) // end TestNewDBManager
}