Skip to content

Commit a146193

Browse files
removed support for exclusion constraint, more tests
1 parent c67e0ee commit a146193

File tree

5 files changed

+140
-130
lines changed

5 files changed

+140
-130
lines changed

constraint.go

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ type ConstraintOption func(*Constraint)
3131
type Constraint struct {
3232
Type ConstraintType
3333
Where, Check string
34-
Exclude Exclude
3534
Table, ReferenceTable *Table
3635
Columns, ReferenceColumns Columns
3736
Attribute []*Attribute
@@ -95,29 +94,18 @@ func Check(table *Table, check string, columns ...*Column) *Constraint {
9594
}
9695
}
9796

98-
// Exclusion constraint ensure that if any two rows are compared on the specified columns
99-
// or expressions using the specified operators,
100-
// at least one of these operator comparisons will return false or null.
101-
func Exclusion(table *Table, exclude Exclude, columns ...*Column) *Constraint {
102-
return &Constraint{
103-
Type: ConstraintTypeExclusion,
104-
Table: table,
105-
Exclude: exclude,
106-
Columns: columns,
107-
}
108-
}
109-
110-
// Exclude ...
111-
type Exclude struct {
112-
Using string
113-
Elements []Elements
114-
}
115-
116-
// Elements ...
117-
type Elements struct {
118-
Selector string
119-
Operator string
120-
}
97+
//
98+
//// Exclusion constraint ensure that if any two rows are compared on the specified columns
99+
//// or expressions using the specified operators,
100+
//// at least one of these operator comparisons will return false or null.
101+
//func Exclusion(table *Table, exclude Exclude, columns ...*Column) *Constraint {
102+
// return &Constraint{
103+
// Type: ConstraintTypeExclusion,
104+
// Table: table,
105+
// Exclude: exclude,
106+
// Columns: columns,
107+
// }
108+
//}
121109

122110
// Reference ...
123111
type Reference struct {
@@ -190,10 +178,10 @@ func IsCheck(c string) bool {
190178
return strings.HasSuffix(c, string(ConstraintTypeCheck))
191179
}
192180

193-
// IsExclusion returns true if string has suffix "_excl".
194-
func IsExclusion(c string) bool {
195-
return strings.HasSuffix(c, string(ConstraintTypeExclusion))
196-
}
181+
//// IsExclusion returns true if string has suffix "_excl".
182+
//func IsExclusion(c string) bool {
183+
// return strings.HasSuffix(c, string(ConstraintTypeExclusion))
184+
//}
197185

198186
// IsIndex returns true if string has suffix "_idx".
199187
func IsIndex(c string) bool {

constraint_test.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,6 @@ import (
88
"github.com/piotrkowalczuk/pqt"
99
)
1010

11-
func TestExclusion(t *testing.T) {
12-
col1 := pqt.NewColumn("a", pqt.TypeIntegerBig())
13-
col2 := pqt.NewColumn("b", pqt.TypeIntegerBig())
14-
tbl := pqt.NewTable("table").AddColumn(col1).AddColumn(col2)
15-
got := pqt.Exclusion(tbl, pqt.Exclude{
16-
Using: "gist",
17-
Elements: []pqt.Elements{
18-
{
19-
Operator: "=",
20-
Selector: "a",
21-
},
22-
},
23-
}, col1, col2)
24-
25-
if len(got.Exclude.Elements) != 1 {
26-
t.Error("expected one exclude element")
27-
}
28-
}
29-
3011
func TestConstraint_Name(t *testing.T) {
3112
id := pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey())
3213
success := map[string]*pqt.Constraint{

example/app/internal/model/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func populateNews(t testing.TB, r *model.NewsRepositoryBase, nb int) {
109109
Content: fmt.Sprintf("content-%d", i),
110110
Lead: sql.NullString{String: fmt.Sprintf("lead-%d", i), Valid: true},
111111
Continue: true,
112-
Score: 10.11,
112+
Score: 10.11,
113113
})
114114
if err != nil {
115115
t.Fatalf("unexpected error #%d: %s", i, err.Error())

table_test.go

Lines changed: 31 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -203,88 +203,37 @@ func TestTable_AddRelationship_oneToMany(t *testing.T) {
203203
}
204204
}
205205

206-
//func TestTable_AddRelationship_manyToMany(t *testing.T) {
207-
// user := pqt.NewTable("user").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey()))
208-
// group := pqt.NewTable("group").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey()))
209-
// userGroups := pqt.NewTable("user_groups")
210-
// user.AddRelationship(pqt.ManyToMany(
211-
// group,
212-
// userGroups,
213-
// pqt.WithInversedName("users"),
214-
// pqt.WithOwnerName("groups"),
215-
// ))
216-
//
217-
// if len(user.Relationships) != 1 {
218-
// t.Fatalf("user should have 1 relationship, but has %d", len(user.Relationships))
219-
// }
220-
//
221-
// if user.Relationships[0].OwnerName != "groups" {
222-
// t.Errorf("user relationship to group should be mapped by groups")
223-
// }
224-
//
225-
// if user.Relationships[0].OwnerTable != group {
226-
// t.Errorf("user relationship to group should be mapped by group table")
227-
// }
228-
//
229-
// if user.Relationships[0].Type != pqt.RelationshipTypeManyToMany {
230-
// t.Errorf("user relationship to group should be many to many")
231-
// }
232-
//
233-
// if len(group.Relationships) != 1 {
234-
// t.Fatalf("group should have 1 relationship, but has %d", len(group.Relationships))
235-
// }
236-
//
237-
// if group.Relationships[0].InversedName != "users" {
238-
// t.Errorf("group relationship to user should be mapped by users")
239-
// }
240-
//
241-
// if group.Relationships[0].InversedTable != user {
242-
// t.Errorf("group relationship to user should be mapped by user table")
243-
// }
244-
//
245-
// if group.Relationships[0].Type != pqt.RelationshipTypeManyToMany {
246-
// t.Errorf("group relationship to user should be %d, but is %d", pqt.RelationshipTypeManyToMany, group.Relationships[0].Type)
247-
// }
248-
//}
249-
//
250-
//func TestTable_AddRelationship_manyToManySelfReferencing(t *testing.T) {
251-
// friendship := pqt.NewTable("friendship")
252-
// user := pqt.NewTable("user").
253-
// AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey())).
254-
// AddRelationship(pqt.ManyToManySelfReferencing(
255-
// friendship,
256-
// pqt.WithInversedName("friends_with_me"),
257-
// pqt.WithOwnerName("my_friends"),
258-
// ))
259-
//
260-
// if len(user.Relationships) != 2 {
261-
// t.Fatalf("user should have 2 relationships, but has %d", len(user.Relationships))
262-
// }
263-
//
264-
// if user.Relationships[0].OwnerName != "my_friends" {
265-
// t.Errorf("user relationship to user should be mapped by my_friends")
266-
// }
267-
//
268-
// if user.Relationships[0].OwnerTable != user {
269-
// t.Errorf("user relationship to group should be mapped by group table")
270-
// }
271-
//
272-
// if user.Relationships[0].Type != pqt.RelationshipTypeManyToManySelfReferencing {
273-
// t.Errorf("user relationship to group should be many to many")
274-
// }
275-
//
276-
// if user.Relationships[1].InversedName != "friends_with_me" {
277-
// t.Errorf("user relationship to user should be mapped by friends_with_me")
278-
// }
279-
//
280-
// if user.Relationships[1].InversedTable != user {
281-
// t.Errorf("user relationship to user should be mapped by user table")
282-
// }
283-
//
284-
// if user.Relationships[1].Type != pqt.RelationshipTypeManyToManySelfReferencing {
285-
// t.Errorf("user relationship to user should be %d, but is %d", pqt.RelationshipTypeManyToManySelfReferencing, user.Relationships[1].Type)
286-
// }
287-
//}
206+
func TestTable_AddRelationship_manyToMany(t *testing.T) {
207+
user := pqt.NewTable("user").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey()))
208+
group := pqt.NewTable("group").AddColumn(pqt.NewColumn("id", pqt.TypeSerial(), pqt.WithPrimaryKey()))
209+
userGroups := pqt.NewTable("user_groups")
210+
user.AddRelationship(pqt.ManyToMany(
211+
group,
212+
userGroups,
213+
pqt.WithInversedName("users"),
214+
pqt.WithOwnerName("groups"),
215+
))
216+
217+
if len(user.OwnedRelationships) != 1 {
218+
t.Fatalf("user should have 1 relationship, but has %d", len(user.OwnedRelationships))
219+
}
220+
221+
if user.OwnedRelationships[0].OwnerName != "groups" {
222+
t.Errorf("user relationship to group should be mapped by groups")
223+
}
224+
225+
if user.OwnedRelationships[0].OwnerTable != group {
226+
t.Errorf("user relationship to group should be mapped by group table")
227+
}
228+
229+
if user.OwnedRelationships[0].Type != pqt.RelationshipTypeManyToMany {
230+
t.Errorf("user relationship to group should be many to many")
231+
}
232+
233+
if len(group.InversedRelationships) != 0 {
234+
t.Fatalf("group should have 0 relationship, but has %d", len(group.InversedRelationships))
235+
}
236+
}
288237

289238
func TestTable_FullName(t *testing.T) {
290239
tbl := pqt.NewTable("table")

type_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,48 @@ package pqt_test
33
import (
44
"testing"
55

6+
"reflect"
7+
68
"github.com/piotrkowalczuk/pqt"
79
)
810

11+
func TestTypeSerialSmall(t *testing.T) {
12+
assertType(t, "SMALLSERIAL", pqt.TypeSerialSmall())
13+
}
14+
15+
func TestTypeSerialBig(t *testing.T) {
16+
assertType(t, "BIGSERIAL", pqt.TypeSerialBig())
17+
}
18+
19+
func TestTypeUUID(t *testing.T) {
20+
assertType(t, "UUID", pqt.TypeUUID())
21+
}
22+
23+
func TestTypeCharacter(t *testing.T) {
24+
assertType(t, "CHARACTER[100]", pqt.TypeCharacter(100))
25+
}
26+
27+
func TestTypeBytea(t *testing.T) {
28+
assertType(t, "BYTEA", pqt.TypeBytea())
29+
}
30+
31+
func TestTypeTimestamp(t *testing.T) {
32+
assertType(t, "TIMESTAMP", pqt.TypeTimestamp())
33+
}
34+
func TestTypeTimestampTZ(t *testing.T) {
35+
assertType(t, "TIMESTAMPTZ", pqt.TypeTimestampTZ())
36+
}
37+
func TestTypeJSON(t *testing.T) {
38+
assertType(t, "JSON", pqt.TypeJSON())
39+
}
40+
func TestTypeJSONB(t *testing.T) {
41+
assertType(t, "JSONB", pqt.TypeJSONB())
42+
}
43+
44+
func TestTypeIntegerSmall(t *testing.T) {
45+
assertType(t, "SMALLINT", pqt.TypeIntegerSmall())
46+
}
47+
948
func TestTypeIntegerSmallArray_zero(t *testing.T) {
1049
expected := "SMALLINT[]"
1150
got := pqt.TypeIntegerSmallArray(0)
@@ -104,8 +143,61 @@ func TestTypeDoubleArray(t *testing.T) {
104143
assertType(t, expected, got)
105144
}
106145

146+
func TestTypeReal(t *testing.T) {
147+
expected := "REAL"
148+
got := pqt.TypeReal()
149+
assertType(t, expected, got)
150+
}
151+
152+
func TestTypeDoublePrecision(t *testing.T) {
153+
expected := "DOUBLE PRECISION"
154+
got := pqt.TypeDoublePrecision()
155+
assertType(t, expected, got)
156+
}
157+
158+
func TestTypeTextArray_zero(t *testing.T) {
159+
expected := "TEXT[]"
160+
got := pqt.TypeTextArray(0)
161+
assertType(t, expected, got)
162+
}
163+
164+
func TestTypeTextArray(t *testing.T) {
165+
expected := "TEXT[100]"
166+
got := pqt.TypeTextArray(100)
167+
assertType(t, expected, got)
168+
}
169+
170+
func TestTypeVarchar_zero(t *testing.T) {
171+
expected := "VARCHAR"
172+
got := pqt.TypeVarchar(0)
173+
assertType(t, expected, got)
174+
}
175+
176+
func TestTypeVarchar(t *testing.T) {
177+
expected := "VARCHAR(100)"
178+
got := pqt.TypeVarchar(100)
179+
assertType(t, expected, got)
180+
}
181+
107182
func assertType(t *testing.T, expected string, got pqt.Type) {
108183
if got.String() != expected {
109184
t.Errorf("unexpected sql representation, expected %s got %s", expected, got.String())
110185
}
111186
}
187+
188+
func TestBaseType_Fingerprint(t *testing.T) {
189+
if pqt.TypeText().Fingerprint() != "base: TEXT" {
190+
t.Errorf("wrong fingerprint: %s", pqt.TypeText().Fingerprint())
191+
}
192+
}
193+
194+
func TestTypeEnumerated(t *testing.T) {
195+
given := pqt.TypeEnumerated("pets", "cat", "dog", "pig")
196+
assertType(t, "pets", given)
197+
if !reflect.DeepEqual(given.Enums, []string{"cat", "dog", "pig"}) {
198+
t.Errorf("wrong set of enums: %v", given.Enums)
199+
}
200+
if given.Fingerprint() != "enumarated: pets" {
201+
t.Errorf("wrong fingerprint: %s", given.Fingerprint())
202+
}
203+
}

0 commit comments

Comments
 (0)