aboutsummaryrefslogtreecommitdiff
path: root/src/models/users_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/models/users_test.go')
-rw-r--r--src/models/users_test.go153
1 files changed, 153 insertions, 0 deletions
diff --git a/src/models/users_test.go b/src/models/users_test.go
index db6ca4c..212c86f 100644
--- a/src/models/users_test.go
+++ b/src/models/users_test.go
@@ -494,6 +494,84 @@ func testUsersInsertWhitelist(t *testing.T) {
}
}
+func testUserToManyComments(t *testing.T) {
+ var err error
+ ctx := context.Background()
+ tx := MustTx(boil.BeginTx(ctx, nil))
+ defer func() { _ = tx.Rollback() }()
+
+ var a User
+ var b, c Comment
+
+ seed := randomize.NewSeed()
+ if err = randomize.Struct(seed, &a, userDBTypes, true, userColumnsWithDefault...); err != nil {
+ t.Errorf("Unable to randomize User struct: %s", err)
+ }
+
+ if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
+ t.Fatal(err)
+ }
+
+ if err = randomize.Struct(seed, &b, commentDBTypes, false, commentColumnsWithDefault...); err != nil {
+ t.Fatal(err)
+ }
+ if err = randomize.Struct(seed, &c, commentDBTypes, false, commentColumnsWithDefault...); err != nil {
+ t.Fatal(err)
+ }
+
+ b.UserID = a.ID
+ c.UserID = a.ID
+
+ if err = b.Insert(ctx, tx, boil.Infer()); err != nil {
+ t.Fatal(err)
+ }
+ if err = c.Insert(ctx, tx, boil.Infer()); err != nil {
+ t.Fatal(err)
+ }
+
+ check, err := a.Comments().All(ctx, tx)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ bFound, cFound := false, false
+ for _, v := range check {
+ if v.UserID == b.UserID {
+ bFound = true
+ }
+ if v.UserID == c.UserID {
+ cFound = true
+ }
+ }
+
+ if !bFound {
+ t.Error("expected to find b")
+ }
+ if !cFound {
+ t.Error("expected to find c")
+ }
+
+ slice := UserSlice{&a}
+ if err = a.L.LoadComments(ctx, tx, false, (*[]*User)(&slice), nil); err != nil {
+ t.Fatal(err)
+ }
+ if got := len(a.R.Comments); got != 2 {
+ t.Error("number of eager loaded records wrong, got:", got)
+ }
+
+ a.R.Comments = nil
+ if err = a.L.LoadComments(ctx, tx, true, &a, nil); err != nil {
+ t.Fatal(err)
+ }
+ if got := len(a.R.Comments); got != 2 {
+ t.Error("number of eager loaded records wrong, got:", got)
+ }
+
+ if t.Failed() {
+ t.Logf("%#v", check)
+ }
+}
+
func testUserToManyPosts(t *testing.T) {
var err error
ctx := context.Background()
@@ -650,6 +728,81 @@ func testUserToManyTags(t *testing.T) {
}
}
+func testUserToManyAddOpComments(t *testing.T) {
+ var err error
+
+ ctx := context.Background()
+ tx := MustTx(boil.BeginTx(ctx, nil))
+ defer func() { _ = tx.Rollback() }()
+
+ var a User
+ var b, c, d, e Comment
+
+ seed := randomize.NewSeed()
+ if err = randomize.Struct(seed, &a, userDBTypes, false, strmangle.SetComplement(userPrimaryKeyColumns, userColumnsWithoutDefault)...); err != nil {
+ t.Fatal(err)
+ }
+ foreigners := []*Comment{&b, &c, &d, &e}
+ for _, x := range foreigners {
+ if err = randomize.Struct(seed, x, commentDBTypes, false, strmangle.SetComplement(commentPrimaryKeyColumns, commentColumnsWithoutDefault)...); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
+ t.Fatal(err)
+ }
+ if err = b.Insert(ctx, tx, boil.Infer()); err != nil {
+ t.Fatal(err)
+ }
+ if err = c.Insert(ctx, tx, boil.Infer()); err != nil {
+ t.Fatal(err)
+ }
+
+ foreignersSplitByInsertion := [][]*Comment{
+ {&b, &c},
+ {&d, &e},
+ }
+
+ for i, x := range foreignersSplitByInsertion {
+ err = a.AddComments(ctx, tx, i != 0, x...)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ first := x[0]
+ second := x[1]
+
+ if a.ID != first.UserID {
+ t.Error("foreign key was wrong value", a.ID, first.UserID)
+ }
+ if a.ID != second.UserID {
+ t.Error("foreign key was wrong value", a.ID, second.UserID)
+ }
+
+ if first.R.User != &a {
+ t.Error("relationship was not added properly to the foreign slice")
+ }
+ if second.R.User != &a {
+ t.Error("relationship was not added properly to the foreign slice")
+ }
+
+ if a.R.Comments[i*2] != first {
+ t.Error("relationship struct slice not set to correct value")
+ }
+ if a.R.Comments[i*2+1] != second {
+ t.Error("relationship struct slice not set to correct value")
+ }
+
+ count, err := a.Comments().Count(ctx, tx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if want := int64((i + 1) * 2); count != want {
+ t.Error("want", want, "got", count)
+ }
+ }
+}
func testUserToManyAddOpPosts(t *testing.T) {
var err error