-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgenerator_test.go
More file actions
127 lines (110 loc) · 3.43 KB
/
Copy pathgenerator_test.go
File metadata and controls
127 lines (110 loc) · 3.43 KB
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
125
126
127
package xml
import (
"encoding/xml"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/vmkteam/mfd-generator/generators/testdata"
"github.com/vmkteam/mfd-generator/mfd"
"github.com/dizzyfool/genna/model"
. "github.com/smartystreets/goconvey/convey"
)
// todo: error if .mfd file has already existed but .xml files was deleted
func TestGenerator_Generate(t *testing.T) {
dbdsn, exists := os.LookupEnv("DB_DSN")
if !exists {
dbdsn = "postgres://postgres:postgres@localhost:5432/newsportal?sslmode=disable"
}
Convey("TestGenerator_Generate", t, func() {
Convey("Check correct generate", func() {
generator := New()
generator.options.Def()
generator.options.URL = dbdsn
generator.options.Output = testdata.PathActualMFD
generator.options.CustomTypes = model.CustomTypeMapping{"uuid": {
PGType: "uuid",
GoType: "uuid.UUID",
GoImport: "github.com/google/uuid",
}}
generator.options.Packages = parseNamespacesFlag("portal:news,categories,tags;geo:countries,regions,cities;vfs:vfsFiles,vfsFolders;card:encryptionKeys;common:siteUsers,loginCodes")
t.Log("Generate xml")
So(generator.Generate(), ShouldBeNil)
})
Convey("Check generated files", func() {
expectedFilenames := map[string]struct{}{
"portal.xml": {},
"geo.xml": {},
"card.xml": {},
"common.xml": {},
"newsportal.mfd": {},
}
for f := range expectedFilenames {
t.Logf("Check %s file", f)
content, err := os.ReadFile(filepath.Join(testdata.PathActual, f))
if err != nil {
t.Fatal(err)
}
expectedContent, err := os.ReadFile(filepath.Join(testdata.PathExpected, f))
if err != nil {
t.Fatal(err)
}
So(string(content), ShouldResemble, string(expectedContent))
}
})
})
}
func helperLoadBytes(t *testing.T, path string) []byte {
bytes, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
return bytes
}
func TestGenerator_TableModel(t *testing.T) {
actualDir := t.TempDir()
mfdPathInActual := filepath.Join(actualDir, filepath.Base(testdata.PathExpectedMFD))
Convey("TestGenerator_TableModel", t, func() {
Convey("Check equal generate tableModels", func() {
var project, projectActual mfd.Project
err := xml.Unmarshal(helperLoadBytes(t, testdata.PathExpectedMFD), &project)
So(err, ShouldBeNil)
project.TableMapping = mfd.TableMapping{
Entries: []mfd.Entry{
{
XMLName: xml.Name{Local: "portal"},
Value: "news,categories,tags",
},
},
}
err = mfd.SaveMFD(mfdPathInActual, &project)
So(err, ShouldBeNil)
err = xml.Unmarshal(helperLoadBytes(t, mfdPathInActual), &projectActual)
So(err, ShouldBeNil)
So(reflect.DeepEqual(projectActual.TableMapping, project.TableMapping), ShouldBeTrue)
})
Convey("Check equal Packages data", func() {
generator := New()
generator.options.Def()
generator.options.Packages = parseNamespacesFlag("common:users;vfs:vfsFiles,vfsFolders;news:news,categories,tags")
project := mfd.Project{TableMapping: mfd.TableMapping{
Entries: []mfd.Entry{
{
XMLName: xml.Name{Local: "common"},
Value: "users",
},
{
XMLName: xml.Name{Local: "vfs"},
Value: "vfsFiles,vfsFolders",
},
{
XMLName: xml.Name{Local: "news"},
Value: "news,categories,tags",
},
},
}}
project.TableMapping.Packages()
So(reflect.DeepEqual(generator.options.Packages, project.TableMapping.Packages()), ShouldBeTrue)
})
})
}