-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTermModel_test.go
186 lines (143 loc) · 5.97 KB
/
TermModel_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package tags
// func TestUpdateFieldTermById_Category(t *testing.T) {
// assert := assert.New(t)
// app := GetAppInstance()
// var categoryFieldCfg = NewCategoryFieldConfiguration("category", "content", "cat")
// // var tagFieldCfg = NewTagFieldConfiguration("tags", "content", "tagis")
// t.Run("Should Return error if try to associate with a not existent category", func(t *testing.T) {
// modelId, termText := "10", "somethingUnknow"
// term, assoc, err := UpdateFieldTermById(modelId, termText, &categoryFieldCfg)
// assert.NotNil(err)
// assert.Equal("UpdateFieldTermById term dont exists and cant create for this field", err.Error())
// assert.Nil(term)
// assert.Nil(assoc)
// })
// t.Run("Should associate model field with a valid category", func(t *testing.T) {
// modelId, termText := "2", "Gaming"
// savedCat := TermModel{
// Text: termText,
// VocabularyName: categoryFieldCfg.VocabularyName,
// }
// err := savedCat.Save()
// assert.Nil(err)
// term, assoc, err := UpdateFieldTermById(modelId, termText, &categoryFieldCfg)
// assert.Nil(err)
// assert.NotNil(term)
// assert.NotNil(assoc)
// assert.Equal(savedCat.ID, term.ID)
// assert.Equal(savedCat.Text, term.Text)
// assert.Equal(savedCat.ID, *assoc.TermID)
// })
// t.Run("Should remove old association and associate the new one", func(t *testing.T) {
// modelId, termText, oldTermText := "3", "Health", "Tech"
// savedCat := TermModel{
// Text: termText,
// VocabularyName: categoryFieldCfg.VocabularyName,
// }
// err := savedCat.Save()
// assert.Nil(err)
// savedCat2 := TermModel{
// Text: oldTermText,
// VocabularyName: categoryFieldCfg.VocabularyName,
// }
// err = savedCat2.Save()
// assert.Nil(err)
// assocSaved := ModelstermsModel{
// ModelName: categoryFieldCfg.ModelName,
// ModelID: 3,
// Field: categoryFieldCfg.FieldName,
// VocabularyName: categoryFieldCfg.VocabularyName,
// TermID: &savedCat2.ID,
// }
// err = assocSaved.Save()
// assert.Nil(err)
// var oldSavedTerm TermModel
// err = TermFindOneInRecord(categoryFieldCfg.ModelName, categoryFieldCfg.FieldName, modelId, &oldSavedTerm)
// assert.Nil(err)
// assert.Equal(oldTermText, oldSavedTerm.Text)
// assert.Equal(savedCat2.ID, oldSavedTerm.ID)
// term, assoc, err := UpdateFieldTermById(modelId, termText, &categoryFieldCfg)
// assert.Nil(err)
// assert.NotNil(term)
// assert.NotNil(assoc)
// assert.Equal(savedCat.ID, term.ID)
// assert.Equal(savedCat.Text, term.Text)
// assert.Equal(savedCat.ID, *assoc.TermID)
// })
// t.Cleanup(func() {
// db := app.DB
// r := db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&ModelstermsModel{})
// if r.Error != nil {
// log.Println("Error on delete db modelsTerms", r.Error, r.RowsAffected)
// }
// })
// }
// func TestUpdateFieldTermById_Term(t *testing.T) {
// assert := assert.New(t)
// app := GetAppInstance()
// var cfg = NewTagFieldConfiguration("tags", "content", "tagis")
// t.Run("Should Return create a new term and associate", func(t *testing.T) {
// modelId := "11"
// terms := []string{"somethingNew1", "meToo2"}
// err := UpdateFieldTermsById(modelId, terms, &cfg)
// assert.Nil(err)
// afterSaveTerms := []TermModel{}
// err = TermFindManyInRecord(cfg.VocabularyName, cfg.ModelName, cfg.FieldName, modelId, &afterSaveTerms)
// assert.Nil(err)
// assert.Equal(2, len(afterSaveTerms))
// assert.Equal(terms[0], afterSaveTerms[0].Text)
// assert.Equal(terms[1], afterSaveTerms[1].Text)
// })
// t.Run("Should remove 1 terms and add 2 new ones", func(t *testing.T) {
// modelId := "12"
// oldTerms := []string{"oldOne1"}
// terms := []string{"somethingNew2", "meToo3"}
// err := UpdateFieldTermsById(modelId, oldTerms, &cfg)
// assert.Nil(err)
// afterSave1Terms := []TermModel{}
// err = TermFindManyInRecord(cfg.VocabularyName, cfg.ModelName, cfg.FieldName, modelId, &afterSave1Terms)
// assert.Nil(err)
// assert.Equal(1, len(afterSave1Terms))
// assert.Equal(oldTerms[0], afterSave1Terms[0].Text)
// err = UpdateFieldTermsById(modelId, terms, &cfg)
// assert.Nil(err)
// afterSave2Terms := []TermModel{}
// err = TermFindManyInRecord(cfg.VocabularyName, cfg.ModelName, cfg.FieldName, modelId, &afterSave2Terms)
// assert.Nil(err)
// assert.Equal(2, len(afterSave2Terms))
// assert.Equal(terms[0], afterSave2Terms[0].Text)
// assert.Equal(terms[1], afterSave2Terms[1].Text)
// })
// t.Run("Should remove 2, keep 2 terms and add 2 new ones", func(t *testing.T) {
// modelId := "13"
// oldTerms := []string{"oldOne0", "oldOne1", "oldOne2", "oldOne3"}
// terms := []string{"oldOne2", "oldOne3", "somethingNew2", "meToo3"}
// err := UpdateFieldTermsById(modelId, oldTerms, &cfg)
// assert.Nil(err)
// afterSave1Terms := []TermModel{}
// err = TermFindManyInRecord(cfg.VocabularyName, cfg.ModelName, cfg.FieldName, modelId, &afterSave1Terms)
// assert.Nil(err)
// assert.Equal(4, len(afterSave1Terms))
// assert.Equal(oldTerms[0], afterSave1Terms[0].Text)
// assert.Equal(oldTerms[1], afterSave1Terms[1].Text)
// assert.Equal(oldTerms[2], afterSave1Terms[2].Text)
// assert.Equal(oldTerms[3], afterSave1Terms[3].Text)
// err = UpdateFieldTermsById(modelId, terms, &cfg)
// assert.Nil(err)
// afterSave2Terms := []TermModel{}
// err = TermFindManyInRecord(cfg.VocabularyName, cfg.ModelName, cfg.FieldName, modelId, &afterSave2Terms)
// assert.Nil(err)
// assert.Equal(4, len(afterSave2Terms))
// assert.Equal(terms[0], afterSave2Terms[0].Text)
// assert.Equal(terms[1], afterSave2Terms[1].Text)
// assert.Equal(terms[2], afterSave2Terms[2].Text)
// assert.Equal(terms[3], afterSave2Terms[3].Text)
// })
// t.Cleanup(func() {
// db := app.DB
// r := db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&ModelstermsModel{})
// if r.Error != nil {
// log.Println("Error on delete db modelsTerms", r.Error, r.RowsAffected)
// }
// })
// }