aboutsummaryrefslogtreecommitdiff
path: root/src/models/posts_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/models/posts_test.go')
-rw-r--r--src/models/posts_test.go433
1 files changed, 432 insertions, 1 deletions
diff --git a/src/models/posts_test.go b/src/models/posts_test.go
index 0df4258..578d483 100644
--- a/src/models/posts_test.go
+++ b/src/models/posts_test.go
@@ -494,6 +494,437 @@ func testPostsInsertWhitelist(t *testing.T) {
}
}
+func testPostToManyTags(t *testing.T) {
+ var err error
+ ctx := context.Background()
+ tx := MustTx(boil.BeginTx(ctx, nil))
+ defer func() { _ = tx.Rollback() }()
+
+ var a Post
+ var b, c Tag
+
+ seed := randomize.NewSeed()
+ if err = randomize.Struct(seed, &a, postDBTypes, true, postColumnsWithDefault...); err != nil {
+ t.Errorf("Unable to randomize Post struct: %s", err)
+ }
+
+ if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
+ t.Fatal(err)
+ }
+
+ if err = randomize.Struct(seed, &b, tagDBTypes, false, tagColumnsWithDefault...); err != nil {
+ t.Fatal(err)
+ }
+ if err = randomize.Struct(seed, &c, tagDBTypes, false, tagColumnsWithDefault...); 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)
+ }
+
+ _, err = tx.Exec("insert into \"post_tags\" (\"post_id\", \"tag_id\") values ($1, $2)", a.ID, b.ID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, err = tx.Exec("insert into \"post_tags\" (\"post_id\", \"tag_id\") values ($1, $2)", a.ID, c.ID)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ check, err := a.Tags().All(ctx, tx)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ bFound, cFound := false, false
+ for _, v := range check {
+ if v.ID == b.ID {
+ bFound = true
+ }
+ if v.ID == c.ID {
+ cFound = true
+ }
+ }
+
+ if !bFound {
+ t.Error("expected to find b")
+ }
+ if !cFound {
+ t.Error("expected to find c")
+ }
+
+ slice := PostSlice{&a}
+ if err = a.L.LoadTags(ctx, tx, false, (*[]*Post)(&slice), nil); err != nil {
+ t.Fatal(err)
+ }
+ if got := len(a.R.Tags); got != 2 {
+ t.Error("number of eager loaded records wrong, got:", got)
+ }
+
+ a.R.Tags = nil
+ if err = a.L.LoadTags(ctx, tx, true, &a, nil); err != nil {
+ t.Fatal(err)
+ }
+ if got := len(a.R.Tags); got != 2 {
+ t.Error("number of eager loaded records wrong, got:", got)
+ }
+
+ if t.Failed() {
+ t.Logf("%#v", check)
+ }
+}
+
+func testPostToManyAddOpTags(t *testing.T) {
+ var err error
+
+ ctx := context.Background()
+ tx := MustTx(boil.BeginTx(ctx, nil))
+ defer func() { _ = tx.Rollback() }()
+
+ var a Post
+ var b, c, d, e Tag
+
+ seed := randomize.NewSeed()
+ if err = randomize.Struct(seed, &a, postDBTypes, false, strmangle.SetComplement(postPrimaryKeyColumns, postColumnsWithoutDefault)...); err != nil {
+ t.Fatal(err)
+ }
+ foreigners := []*Tag{&b, &c, &d, &e}
+ for _, x := range foreigners {
+ if err = randomize.Struct(seed, x, tagDBTypes, false, strmangle.SetComplement(tagPrimaryKeyColumns, tagColumnsWithoutDefault)...); 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 := [][]*Tag{
+ {&b, &c},
+ {&d, &e},
+ }
+
+ for i, x := range foreignersSplitByInsertion {
+ err = a.AddTags(ctx, tx, i != 0, x...)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ first := x[0]
+ second := x[1]
+
+ if first.R.Posts[0] != &a {
+ t.Error("relationship was not added properly to the slice")
+ }
+ if second.R.Posts[0] != &a {
+ t.Error("relationship was not added properly to the slice")
+ }
+
+ if a.R.Tags[i*2] != first {
+ t.Error("relationship struct slice not set to correct value")
+ }
+ if a.R.Tags[i*2+1] != second {
+ t.Error("relationship struct slice not set to correct value")
+ }
+
+ count, err := a.Tags().Count(ctx, tx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if want := int64((i + 1) * 2); count != want {
+ t.Error("want", want, "got", count)
+ }
+ }
+}
+
+func testPostToManySetOpTags(t *testing.T) {
+ var err error
+
+ ctx := context.Background()
+ tx := MustTx(boil.BeginTx(ctx, nil))
+ defer func() { _ = tx.Rollback() }()
+
+ var a Post
+ var b, c, d, e Tag
+
+ seed := randomize.NewSeed()
+ if err = randomize.Struct(seed, &a, postDBTypes, false, strmangle.SetComplement(postPrimaryKeyColumns, postColumnsWithoutDefault)...); err != nil {
+ t.Fatal(err)
+ }
+ foreigners := []*Tag{&b, &c, &d, &e}
+ for _, x := range foreigners {
+ if err = randomize.Struct(seed, x, tagDBTypes, false, strmangle.SetComplement(tagPrimaryKeyColumns, tagColumnsWithoutDefault)...); 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)
+ }
+
+ err = a.SetTags(ctx, tx, false, &b, &c)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ count, err := a.Tags().Count(ctx, tx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if count != 2 {
+ t.Error("count was wrong:", count)
+ }
+
+ err = a.SetTags(ctx, tx, true, &d, &e)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ count, err = a.Tags().Count(ctx, tx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if count != 2 {
+ t.Error("count was wrong:", count)
+ }
+
+ // The following checks cannot be implemented since we have no handle
+ // to these when we call Set(). Leaving them here as wishful thinking
+ // and to let people know there's dragons.
+ //
+ // if len(b.R.Posts) != 0 {
+ // t.Error("relationship was not removed properly from the slice")
+ // }
+ // if len(c.R.Posts) != 0 {
+ // t.Error("relationship was not removed properly from the slice")
+ // }
+ if d.R.Posts[0] != &a {
+ t.Error("relationship was not added properly to the slice")
+ }
+ if e.R.Posts[0] != &a {
+ t.Error("relationship was not added properly to the slice")
+ }
+
+ if a.R.Tags[0] != &d {
+ t.Error("relationship struct slice not set to correct value")
+ }
+ if a.R.Tags[1] != &e {
+ t.Error("relationship struct slice not set to correct value")
+ }
+}
+
+func testPostToManyRemoveOpTags(t *testing.T) {
+ var err error
+
+ ctx := context.Background()
+ tx := MustTx(boil.BeginTx(ctx, nil))
+ defer func() { _ = tx.Rollback() }()
+
+ var a Post
+ var b, c, d, e Tag
+
+ seed := randomize.NewSeed()
+ if err = randomize.Struct(seed, &a, postDBTypes, false, strmangle.SetComplement(postPrimaryKeyColumns, postColumnsWithoutDefault)...); err != nil {
+ t.Fatal(err)
+ }
+ foreigners := []*Tag{&b, &c, &d, &e}
+ for _, x := range foreigners {
+ if err = randomize.Struct(seed, x, tagDBTypes, false, strmangle.SetComplement(tagPrimaryKeyColumns, tagColumnsWithoutDefault)...); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
+ t.Fatal(err)
+ }
+
+ err = a.AddTags(ctx, tx, true, foreigners...)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ count, err := a.Tags().Count(ctx, tx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if count != 4 {
+ t.Error("count was wrong:", count)
+ }
+
+ err = a.RemoveTags(ctx, tx, foreigners[:2]...)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ count, err = a.Tags().Count(ctx, tx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if count != 2 {
+ t.Error("count was wrong:", count)
+ }
+
+ if len(b.R.Posts) != 0 {
+ t.Error("relationship was not removed properly from the slice")
+ }
+ if len(c.R.Posts) != 0 {
+ t.Error("relationship was not removed properly from the slice")
+ }
+ if d.R.Posts[0] != &a {
+ t.Error("relationship was not added properly to the foreign struct")
+ }
+ if e.R.Posts[0] != &a {
+ t.Error("relationship was not added properly to the foreign struct")
+ }
+
+ if len(a.R.Tags) != 2 {
+ t.Error("should have preserved two relationships")
+ }
+
+ // Removal doesn't do a stable deletion for performance so we have to flip the order
+ if a.R.Tags[1] != &d {
+ t.Error("relationship to d should have been preserved")
+ }
+ if a.R.Tags[0] != &e {
+ t.Error("relationship to e should have been preserved")
+ }
+}
+
+func testPostToOneUserUsingUser(t *testing.T) {
+ ctx := context.Background()
+ tx := MustTx(boil.BeginTx(ctx, nil))
+ defer func() { _ = tx.Rollback() }()
+
+ var local Post
+ var foreign User
+
+ seed := randomize.NewSeed()
+ if err := randomize.Struct(seed, &local, postDBTypes, false, postColumnsWithDefault...); err != nil {
+ t.Errorf("Unable to randomize Post struct: %s", err)
+ }
+ if err := randomize.Struct(seed, &foreign, userDBTypes, false, userColumnsWithDefault...); err != nil {
+ t.Errorf("Unable to randomize User struct: %s", err)
+ }
+
+ if err := foreign.Insert(ctx, tx, boil.Infer()); err != nil {
+ t.Fatal(err)
+ }
+
+ local.UserID = foreign.ID
+ if err := local.Insert(ctx, tx, boil.Infer()); err != nil {
+ t.Fatal(err)
+ }
+
+ check, err := local.User().One(ctx, tx)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if check.ID != foreign.ID {
+ t.Errorf("want: %v, got %v", foreign.ID, check.ID)
+ }
+
+ ranAfterSelectHook := false
+ AddUserHook(boil.AfterSelectHook, func(ctx context.Context, e boil.ContextExecutor, o *User) error {
+ ranAfterSelectHook = true
+ return nil
+ })
+
+ slice := PostSlice{&local}
+ if err = local.L.LoadUser(ctx, tx, false, (*[]*Post)(&slice), nil); err != nil {
+ t.Fatal(err)
+ }
+ if local.R.User == nil {
+ t.Error("struct should have been eager loaded")
+ }
+
+ local.R.User = nil
+ if err = local.L.LoadUser(ctx, tx, true, &local, nil); err != nil {
+ t.Fatal(err)
+ }
+ if local.R.User == nil {
+ t.Error("struct should have been eager loaded")
+ }
+
+ if !ranAfterSelectHook {
+ t.Error("failed to run AfterSelect hook for relationship")
+ }
+}
+
+func testPostToOneSetOpUserUsingUser(t *testing.T) {
+ var err error
+
+ ctx := context.Background()
+ tx := MustTx(boil.BeginTx(ctx, nil))
+ defer func() { _ = tx.Rollback() }()
+
+ var a Post
+ var b, c User
+
+ seed := randomize.NewSeed()
+ if err = randomize.Struct(seed, &a, postDBTypes, false, strmangle.SetComplement(postPrimaryKeyColumns, postColumnsWithoutDefault)...); err != nil {
+ t.Fatal(err)
+ }
+ if err = randomize.Struct(seed, &b, userDBTypes, false, strmangle.SetComplement(userPrimaryKeyColumns, userColumnsWithoutDefault)...); err != nil {
+ t.Fatal(err)
+ }
+ if err = randomize.Struct(seed, &c, userDBTypes, false, strmangle.SetComplement(userPrimaryKeyColumns, userColumnsWithoutDefault)...); 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)
+ }
+
+ for i, x := range []*User{&b, &c} {
+ err = a.SetUser(ctx, tx, i != 0, x)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if a.R.User != x {
+ t.Error("relationship struct not set to correct value")
+ }
+
+ if x.R.Posts[0] != &a {
+ t.Error("failed to append to foreign relationship struct")
+ }
+ if a.UserID != x.ID {
+ t.Error("foreign key was wrong value", a.UserID)
+ }
+
+ zero := reflect.Zero(reflect.TypeOf(a.UserID))
+ reflect.Indirect(reflect.ValueOf(&a.UserID)).Set(zero)
+
+ if err = a.Reload(ctx, tx); err != nil {
+ t.Fatal("failed to reload", err)
+ }
+
+ if a.UserID != x.ID {
+ t.Error("foreign key was wrong value", a.UserID, x.ID)
+ }
+ }
+}
+
func testPostsReload(t *testing.T) {
t.Parallel()
@@ -568,7 +999,7 @@ func testPostsSelect(t *testing.T) {
}
var (
- postDBTypes = map[string]string{`ID`: `integer`, `Title`: `character varying`, `URL`: `character varying`, `Description`: `text`, `CreatedAt`: `timestamp with time zone`, `UpdatedAt`: `timestamp with time zone`}
+ postDBTypes = map[string]string{`ID`: `integer`, `Title`: `character varying`, `Description`: `text`, `URL`: `character varying`, `UserID`: `integer`, `CreatedAt`: `timestamp with time zone`, `UpdatedAt`: `timestamp with time zone`}
_ = bytes.MinRead
)