-
Notifications
You must be signed in to change notification settings - Fork 0
/
origin_test.go
124 lines (111 loc) · 2.54 KB
/
origin_test.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package psqlfront_test
import (
"context"
"testing"
psqlfront "github.com/mashiike/psql-front"
"github.com/samber/lo"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
type DummyOrigin struct {
id string
tables []*psqlfront.Table
}
func (o *DummyOrigin) ID() string {
return o.id
}
func (o *DummyOrigin) GetTables(ctx context.Context) ([]*psqlfront.Table, error) {
return o.tables, nil
}
func (o *DummyOrigin) RefreshCache(ctx context.Context, w psqlfront.CacheWriter) error {
table := w.TargetTable()
if err := w.DeleteRows(ctx); err != nil {
return err
}
row := make([]interface{}, 0, len(table.Columns))
for _, c := range table.Columns {
row = append(row, "value_"+c.Name)
}
w.AppendRows(ctx, [][]interface{}{row})
return nil
}
type DummyOriginConfig struct {
Schema string `yaml:"schema"`
Tables []string `yaml:"tables"`
}
const DummyOriginType = "Dummy"
func (cfg *DummyOriginConfig) Type() string {
return DummyOriginType
}
func (cfg *DummyOriginConfig) Restrict() error {
return nil
}
func (cfg *DummyOriginConfig) NewOrigin(id string) (psqlfront.Origin, error) {
return &DummyOrigin{
id: id,
tables: lo.Map(cfg.Tables, func(table string, _ int) *psqlfront.Table {
l := int(256)
return &psqlfront.Table{
SchemaName: cfg.Schema,
RelName: table,
Columns: []*psqlfront.Column{
{
Name: "id",
DataType: "varchar",
Length: &l,
},
},
}
}),
}, nil
}
func TestDummyOriginConfigUnmarshalSuccess(t *testing.T) {
psqlfront.RegisterOriginType(DummyOriginType, func() psqlfront.OriginConfig {
return &DummyOriginConfig{}
})
defer psqlfront.UnregisterOriginType(DummyOriginType)
cfgString := []byte(`
id: hoge
type: Dummy
schema: psqlfront_test
tables:
- dummy
- hoge
`)
var cfg psqlfront.CommonOriginConfig
err := yaml.Unmarshal(cfgString, &cfg)
require.NoError(t, err)
require.EqualValues(t, DummyOriginType, cfg.Type)
require.EqualValues(t, DummyOriginType, cfg.OriginConfig.Type())
origin, err := cfg.NewOrigin()
require.NoError(t, err)
l := int(256)
expected := &DummyOrigin{
id: "hoge",
tables: []*psqlfront.Table{
{
SchemaName: "psqlfront_test",
RelName: "dummy",
Columns: []*psqlfront.Column{
{
Name: "id",
DataType: "varchar",
Length: &l,
},
},
},
{
SchemaName: "psqlfront_test",
RelName: "hoge",
Columns: []*psqlfront.Column{
{
Name: "id",
DataType: "varchar",
Length: &l,
},
},
},
},
}
require.EqualValues(t, expected, origin)
}