diff options
Diffstat (limited to 'src/models/comments_test.go')
| -rw-r--r-- | src/models/comments_test.go | 1795 |
1 files changed, 1795 insertions, 0 deletions
diff --git a/src/models/comments_test.go b/src/models/comments_test.go new file mode 100644 index 0000000..5d92a60 --- /dev/null +++ b/src/models/comments_test.go @@ -0,0 +1,1795 @@ +// Code generated by SQLBoiler 4.16.1 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package models + +import ( + "bytes" + "context" + "reflect" + "testing" + + "github.com/volatiletech/randomize" + "github.com/volatiletech/sqlboiler/v4/boil" + "github.com/volatiletech/sqlboiler/v4/queries" + "github.com/volatiletech/strmangle" +) + +var ( + // Relationships sometimes use the reflection helper queries.Equal/queries.Assign + // so force a package dependency in case they don't. + _ = queries.Equal +) + +func testComments(t *testing.T) { + t.Parallel() + + query := Comments() + + if query.Query == nil { + t.Error("expected a query, got nothing") + } +} + +func testCommentsDelete(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + if rowsAff, err := o.Delete(ctx, tx); err != nil { + t.Error(err) + } else if rowsAff != 1 { + t.Error("should only have deleted one row, but affected:", rowsAff) + } + + count, err := Comments().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 0 { + t.Error("want zero records, got:", count) + } +} + +func testCommentsQueryDeleteAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + if rowsAff, err := Comments().DeleteAll(ctx, tx); err != nil { + t.Error(err) + } else if rowsAff != 1 { + t.Error("should only have deleted one row, but affected:", rowsAff) + } + + count, err := Comments().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 0 { + t.Error("want zero records, got:", count) + } +} + +func testCommentsSliceDeleteAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + slice := CommentSlice{o} + + if rowsAff, err := slice.DeleteAll(ctx, tx); err != nil { + t.Error(err) + } else if rowsAff != 1 { + t.Error("should only have deleted one row, but affected:", rowsAff) + } + + count, err := Comments().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 0 { + t.Error("want zero records, got:", count) + } +} + +func testCommentsExists(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + e, err := CommentExists(ctx, tx, o.ID) + if err != nil { + t.Errorf("Unable to check if Comment exists: %s", err) + } + if !e { + t.Errorf("Expected CommentExists to return true, but got false.") + } +} + +func testCommentsFind(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + commentFound, err := FindComment(ctx, tx, o.ID) + if err != nil { + t.Error(err) + } + + if commentFound == nil { + t.Error("want a record, got nil") + } +} + +func testCommentsBind(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + if err = Comments().Bind(ctx, tx, o); err != nil { + t.Error(err) + } +} + +func testCommentsOne(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + if x, err := Comments().One(ctx, tx); err != nil { + t.Error(err) + } else if x == nil { + t.Error("expected to get a non nil record") + } +} + +func testCommentsAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + commentOne := &Comment{} + commentTwo := &Comment{} + if err = randomize.Struct(seed, commentOne, commentDBTypes, false, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + if err = randomize.Struct(seed, commentTwo, commentDBTypes, false, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = commentOne.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + if err = commentTwo.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + slice, err := Comments().All(ctx, tx) + if err != nil { + t.Error(err) + } + + if len(slice) != 2 { + t.Error("want 2 records, got:", len(slice)) + } +} + +func testCommentsCount(t *testing.T) { + t.Parallel() + + var err error + seed := randomize.NewSeed() + commentOne := &Comment{} + commentTwo := &Comment{} + if err = randomize.Struct(seed, commentOne, commentDBTypes, false, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + if err = randomize.Struct(seed, commentTwo, commentDBTypes, false, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = commentOne.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + if err = commentTwo.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + count, err := Comments().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 2 { + t.Error("want 2 records, got:", count) + } +} + +func commentBeforeInsertHook(ctx context.Context, e boil.ContextExecutor, o *Comment) error { + *o = Comment{} + return nil +} + +func commentAfterInsertHook(ctx context.Context, e boil.ContextExecutor, o *Comment) error { + *o = Comment{} + return nil +} + +func commentAfterSelectHook(ctx context.Context, e boil.ContextExecutor, o *Comment) error { + *o = Comment{} + return nil +} + +func commentBeforeUpdateHook(ctx context.Context, e boil.ContextExecutor, o *Comment) error { + *o = Comment{} + return nil +} + +func commentAfterUpdateHook(ctx context.Context, e boil.ContextExecutor, o *Comment) error { + *o = Comment{} + return nil +} + +func commentBeforeDeleteHook(ctx context.Context, e boil.ContextExecutor, o *Comment) error { + *o = Comment{} + return nil +} + +func commentAfterDeleteHook(ctx context.Context, e boil.ContextExecutor, o *Comment) error { + *o = Comment{} + return nil +} + +func commentBeforeUpsertHook(ctx context.Context, e boil.ContextExecutor, o *Comment) error { + *o = Comment{} + return nil +} + +func commentAfterUpsertHook(ctx context.Context, e boil.ContextExecutor, o *Comment) error { + *o = Comment{} + return nil +} + +func testCommentsHooks(t *testing.T) { + t.Parallel() + + var err error + + ctx := context.Background() + empty := &Comment{} + o := &Comment{} + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, o, commentDBTypes, false); err != nil { + t.Errorf("Unable to randomize Comment object: %s", err) + } + + AddCommentHook(boil.BeforeInsertHook, commentBeforeInsertHook) + if err = o.doBeforeInsertHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doBeforeInsertHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected BeforeInsertHook function to empty object, but got: %#v", o) + } + commentBeforeInsertHooks = []CommentHook{} + + AddCommentHook(boil.AfterInsertHook, commentAfterInsertHook) + if err = o.doAfterInsertHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doAfterInsertHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected AfterInsertHook function to empty object, but got: %#v", o) + } + commentAfterInsertHooks = []CommentHook{} + + AddCommentHook(boil.AfterSelectHook, commentAfterSelectHook) + if err = o.doAfterSelectHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doAfterSelectHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected AfterSelectHook function to empty object, but got: %#v", o) + } + commentAfterSelectHooks = []CommentHook{} + + AddCommentHook(boil.BeforeUpdateHook, commentBeforeUpdateHook) + if err = o.doBeforeUpdateHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doBeforeUpdateHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected BeforeUpdateHook function to empty object, but got: %#v", o) + } + commentBeforeUpdateHooks = []CommentHook{} + + AddCommentHook(boil.AfterUpdateHook, commentAfterUpdateHook) + if err = o.doAfterUpdateHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doAfterUpdateHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected AfterUpdateHook function to empty object, but got: %#v", o) + } + commentAfterUpdateHooks = []CommentHook{} + + AddCommentHook(boil.BeforeDeleteHook, commentBeforeDeleteHook) + if err = o.doBeforeDeleteHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doBeforeDeleteHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected BeforeDeleteHook function to empty object, but got: %#v", o) + } + commentBeforeDeleteHooks = []CommentHook{} + + AddCommentHook(boil.AfterDeleteHook, commentAfterDeleteHook) + if err = o.doAfterDeleteHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doAfterDeleteHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected AfterDeleteHook function to empty object, but got: %#v", o) + } + commentAfterDeleteHooks = []CommentHook{} + + AddCommentHook(boil.BeforeUpsertHook, commentBeforeUpsertHook) + if err = o.doBeforeUpsertHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doBeforeUpsertHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected BeforeUpsertHook function to empty object, but got: %#v", o) + } + commentBeforeUpsertHooks = []CommentHook{} + + AddCommentHook(boil.AfterUpsertHook, commentAfterUpsertHook) + if err = o.doAfterUpsertHooks(ctx, nil); err != nil { + t.Errorf("Unable to execute doAfterUpsertHooks: %s", err) + } + if !reflect.DeepEqual(o, empty) { + t.Errorf("Expected AfterUpsertHook function to empty object, but got: %#v", o) + } + commentAfterUpsertHooks = []CommentHook{} +} + +func testCommentsInsert(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + count, err := Comments().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 1 { + t.Error("want one record, got:", count) + } +} + +func testCommentsInsertWhitelist(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Whitelist(commentColumnsWithoutDefault...)); err != nil { + t.Error(err) + } + + count, err := Comments().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 1 { + t.Error("want one record, got:", count) + } +} + +func testCommentToManyReplyComments(t *testing.T) { + var err error + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a Comment + var b, c Comment + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment 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) + } + + queries.Assign(&b.ReplyID, a.ID) + queries.Assign(&c.ReplyID, 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.ReplyComments().All(ctx, tx) + if err != nil { + t.Fatal(err) + } + + bFound, cFound := false, false + for _, v := range check { + if queries.Equal(v.ReplyID, b.ReplyID) { + bFound = true + } + if queries.Equal(v.ReplyID, c.ReplyID) { + cFound = true + } + } + + if !bFound { + t.Error("expected to find b") + } + if !cFound { + t.Error("expected to find c") + } + + slice := CommentSlice{&a} + if err = a.L.LoadReplyComments(ctx, tx, false, (*[]*Comment)(&slice), nil); err != nil { + t.Fatal(err) + } + if got := len(a.R.ReplyComments); got != 2 { + t.Error("number of eager loaded records wrong, got:", got) + } + + a.R.ReplyComments = nil + if err = a.L.LoadReplyComments(ctx, tx, true, &a, nil); err != nil { + t.Fatal(err) + } + if got := len(a.R.ReplyComments); got != 2 { + t.Error("number of eager loaded records wrong, got:", got) + } + + if t.Failed() { + t.Logf("%#v", check) + } +} + +func testCommentToManyVotes(t *testing.T) { + var err error + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a Comment + var b, c Vote + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + if err := a.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + + if err = randomize.Struct(seed, &b, voteDBTypes, false, voteColumnsWithDefault...); err != nil { + t.Fatal(err) + } + if err = randomize.Struct(seed, &c, voteDBTypes, false, voteColumnsWithDefault...); err != nil { + t.Fatal(err) + } + + queries.Assign(&b.CommentID, a.ID) + queries.Assign(&c.CommentID, 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.Votes().All(ctx, tx) + if err != nil { + t.Fatal(err) + } + + bFound, cFound := false, false + for _, v := range check { + if queries.Equal(v.CommentID, b.CommentID) { + bFound = true + } + if queries.Equal(v.CommentID, c.CommentID) { + cFound = true + } + } + + if !bFound { + t.Error("expected to find b") + } + if !cFound { + t.Error("expected to find c") + } + + slice := CommentSlice{&a} + if err = a.L.LoadVotes(ctx, tx, false, (*[]*Comment)(&slice), nil); err != nil { + t.Fatal(err) + } + if got := len(a.R.Votes); got != 2 { + t.Error("number of eager loaded records wrong, got:", got) + } + + a.R.Votes = nil + if err = a.L.LoadVotes(ctx, tx, true, &a, nil); err != nil { + t.Fatal(err) + } + if got := len(a.R.Votes); got != 2 { + t.Error("number of eager loaded records wrong, got:", got) + } + + if t.Failed() { + t.Logf("%#v", check) + } +} + +func testCommentToManyAddOpReplyComments(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a Comment + var b, c, d, e Comment + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, commentDBTypes, false, strmangle.SetComplement(commentPrimaryKeyColumns, commentColumnsWithoutDefault)...); 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.AddReplyComments(ctx, tx, i != 0, x...) + if err != nil { + t.Fatal(err) + } + + first := x[0] + second := x[1] + + if !queries.Equal(a.ID, first.ReplyID) { + t.Error("foreign key was wrong value", a.ID, first.ReplyID) + } + if !queries.Equal(a.ID, second.ReplyID) { + t.Error("foreign key was wrong value", a.ID, second.ReplyID) + } + + if first.R.Reply != &a { + t.Error("relationship was not added properly to the foreign slice") + } + if second.R.Reply != &a { + t.Error("relationship was not added properly to the foreign slice") + } + + if a.R.ReplyComments[i*2] != first { + t.Error("relationship struct slice not set to correct value") + } + if a.R.ReplyComments[i*2+1] != second { + t.Error("relationship struct slice not set to correct value") + } + + count, err := a.ReplyComments().Count(ctx, tx) + if err != nil { + t.Fatal(err) + } + if want := int64((i + 1) * 2); count != want { + t.Error("want", want, "got", count) + } + } +} + +func testCommentToManySetOpReplyComments(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a Comment + var b, c, d, e Comment + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, commentDBTypes, false, strmangle.SetComplement(commentPrimaryKeyColumns, commentColumnsWithoutDefault)...); 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) + } + + err = a.SetReplyComments(ctx, tx, false, &b, &c) + if err != nil { + t.Fatal(err) + } + + count, err := a.ReplyComments().Count(ctx, tx) + if err != nil { + t.Fatal(err) + } + if count != 2 { + t.Error("count was wrong:", count) + } + + err = a.SetReplyComments(ctx, tx, true, &d, &e) + if err != nil { + t.Fatal(err) + } + + count, err = a.ReplyComments().Count(ctx, tx) + if err != nil { + t.Fatal(err) + } + if count != 2 { + t.Error("count was wrong:", count) + } + + if !queries.IsValuerNil(b.ReplyID) { + t.Error("want b's foreign key value to be nil") + } + if !queries.IsValuerNil(c.ReplyID) { + t.Error("want c's foreign key value to be nil") + } + if !queries.Equal(a.ID, d.ReplyID) { + t.Error("foreign key was wrong value", a.ID, d.ReplyID) + } + if !queries.Equal(a.ID, e.ReplyID) { + t.Error("foreign key was wrong value", a.ID, e.ReplyID) + } + + if b.R.Reply != nil { + t.Error("relationship was not removed properly from the foreign struct") + } + if c.R.Reply != nil { + t.Error("relationship was not removed properly from the foreign struct") + } + if d.R.Reply != &a { + t.Error("relationship was not added properly to the foreign struct") + } + if e.R.Reply != &a { + t.Error("relationship was not added properly to the foreign struct") + } + + if a.R.ReplyComments[0] != &d { + t.Error("relationship struct slice not set to correct value") + } + if a.R.ReplyComments[1] != &e { + t.Error("relationship struct slice not set to correct value") + } +} + +func testCommentToManyRemoveOpReplyComments(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a Comment + var b, c, d, e Comment + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, commentDBTypes, false, strmangle.SetComplement(commentPrimaryKeyColumns, commentColumnsWithoutDefault)...); 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) + } + + err = a.AddReplyComments(ctx, tx, true, foreigners...) + if err != nil { + t.Fatal(err) + } + + count, err := a.ReplyComments().Count(ctx, tx) + if err != nil { + t.Fatal(err) + } + if count != 4 { + t.Error("count was wrong:", count) + } + + err = a.RemoveReplyComments(ctx, tx, foreigners[:2]...) + if err != nil { + t.Fatal(err) + } + + count, err = a.ReplyComments().Count(ctx, tx) + if err != nil { + t.Fatal(err) + } + if count != 2 { + t.Error("count was wrong:", count) + } + + if !queries.IsValuerNil(b.ReplyID) { + t.Error("want b's foreign key value to be nil") + } + if !queries.IsValuerNil(c.ReplyID) { + t.Error("want c's foreign key value to be nil") + } + + if b.R.Reply != nil { + t.Error("relationship was not removed properly from the foreign struct") + } + if c.R.Reply != nil { + t.Error("relationship was not removed properly from the foreign struct") + } + if d.R.Reply != &a { + t.Error("relationship to a should have been preserved") + } + if e.R.Reply != &a { + t.Error("relationship to a should have been preserved") + } + + if len(a.R.ReplyComments) != 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.ReplyComments[1] != &d { + t.Error("relationship to d should have been preserved") + } + if a.R.ReplyComments[0] != &e { + t.Error("relationship to e should have been preserved") + } +} + +func testCommentToManyAddOpVotes(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a Comment + var b, c, d, e Vote + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, commentDBTypes, false, strmangle.SetComplement(commentPrimaryKeyColumns, commentColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + foreigners := []*Vote{&b, &c, &d, &e} + for _, x := range foreigners { + if err = randomize.Struct(seed, x, voteDBTypes, false, strmangle.SetComplement(votePrimaryKeyColumns, voteColumnsWithoutDefault)...); 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 := [][]*Vote{ + {&b, &c}, + {&d, &e}, + } + + for i, x := range foreignersSplitByInsertion { + err = a.AddVotes(ctx, tx, i != 0, x...) + if err != nil { + t.Fatal(err) + } + + first := x[0] + second := x[1] + + if !queries.Equal(a.ID, first.CommentID) { + t.Error("foreign key was wrong value", a.ID, first.CommentID) + } + if !queries.Equal(a.ID, second.CommentID) { + t.Error("foreign key was wrong value", a.ID, second.CommentID) + } + + if first.R.Comment != &a { + t.Error("relationship was not added properly to the foreign slice") + } + if second.R.Comment != &a { + t.Error("relationship was not added properly to the foreign slice") + } + + if a.R.Votes[i*2] != first { + t.Error("relationship struct slice not set to correct value") + } + if a.R.Votes[i*2+1] != second { + t.Error("relationship struct slice not set to correct value") + } + + count, err := a.Votes().Count(ctx, tx) + if err != nil { + t.Fatal(err) + } + if want := int64((i + 1) * 2); count != want { + t.Error("want", want, "got", count) + } + } +} + +func testCommentToManySetOpVotes(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a Comment + var b, c, d, e Vote + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, commentDBTypes, false, strmangle.SetComplement(commentPrimaryKeyColumns, commentColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + foreigners := []*Vote{&b, &c, &d, &e} + for _, x := range foreigners { + if err = randomize.Struct(seed, x, voteDBTypes, false, strmangle.SetComplement(votePrimaryKeyColumns, voteColumnsWithoutDefault)...); 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.SetVotes(ctx, tx, false, &b, &c) + if err != nil { + t.Fatal(err) + } + + count, err := a.Votes().Count(ctx, tx) + if err != nil { + t.Fatal(err) + } + if count != 2 { + t.Error("count was wrong:", count) + } + + err = a.SetVotes(ctx, tx, true, &d, &e) + if err != nil { + t.Fatal(err) + } + + count, err = a.Votes().Count(ctx, tx) + if err != nil { + t.Fatal(err) + } + if count != 2 { + t.Error("count was wrong:", count) + } + + if !queries.IsValuerNil(b.CommentID) { + t.Error("want b's foreign key value to be nil") + } + if !queries.IsValuerNil(c.CommentID) { + t.Error("want c's foreign key value to be nil") + } + if !queries.Equal(a.ID, d.CommentID) { + t.Error("foreign key was wrong value", a.ID, d.CommentID) + } + if !queries.Equal(a.ID, e.CommentID) { + t.Error("foreign key was wrong value", a.ID, e.CommentID) + } + + if b.R.Comment != nil { + t.Error("relationship was not removed properly from the foreign struct") + } + if c.R.Comment != nil { + t.Error("relationship was not removed properly from the foreign struct") + } + if d.R.Comment != &a { + t.Error("relationship was not added properly to the foreign struct") + } + if e.R.Comment != &a { + t.Error("relationship was not added properly to the foreign struct") + } + + if a.R.Votes[0] != &d { + t.Error("relationship struct slice not set to correct value") + } + if a.R.Votes[1] != &e { + t.Error("relationship struct slice not set to correct value") + } +} + +func testCommentToManyRemoveOpVotes(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a Comment + var b, c, d, e Vote + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, commentDBTypes, false, strmangle.SetComplement(commentPrimaryKeyColumns, commentColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + foreigners := []*Vote{&b, &c, &d, &e} + for _, x := range foreigners { + if err = randomize.Struct(seed, x, voteDBTypes, false, strmangle.SetComplement(votePrimaryKeyColumns, voteColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + } + + if err := a.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + + err = a.AddVotes(ctx, tx, true, foreigners...) + if err != nil { + t.Fatal(err) + } + + count, err := a.Votes().Count(ctx, tx) + if err != nil { + t.Fatal(err) + } + if count != 4 { + t.Error("count was wrong:", count) + } + + err = a.RemoveVotes(ctx, tx, foreigners[:2]...) + if err != nil { + t.Fatal(err) + } + + count, err = a.Votes().Count(ctx, tx) + if err != nil { + t.Fatal(err) + } + if count != 2 { + t.Error("count was wrong:", count) + } + + if !queries.IsValuerNil(b.CommentID) { + t.Error("want b's foreign key value to be nil") + } + if !queries.IsValuerNil(c.CommentID) { + t.Error("want c's foreign key value to be nil") + } + + if b.R.Comment != nil { + t.Error("relationship was not removed properly from the foreign struct") + } + if c.R.Comment != nil { + t.Error("relationship was not removed properly from the foreign struct") + } + if d.R.Comment != &a { + t.Error("relationship to a should have been preserved") + } + if e.R.Comment != &a { + t.Error("relationship to a should have been preserved") + } + + if len(a.R.Votes) != 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.Votes[1] != &d { + t.Error("relationship to d should have been preserved") + } + if a.R.Votes[0] != &e { + t.Error("relationship to e should have been preserved") + } +} + +func testCommentToOnePostUsingPost(t *testing.T) { + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var local Comment + var foreign Post + + seed := randomize.NewSeed() + if err := randomize.Struct(seed, &local, commentDBTypes, false, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + if err := randomize.Struct(seed, &foreign, postDBTypes, false, postColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Post struct: %s", err) + } + + if err := foreign.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + + local.PostID = foreign.ID + if err := local.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + + check, err := local.Post().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 + AddPostHook(boil.AfterSelectHook, func(ctx context.Context, e boil.ContextExecutor, o *Post) error { + ranAfterSelectHook = true + return nil + }) + + slice := CommentSlice{&local} + if err = local.L.LoadPost(ctx, tx, false, (*[]*Comment)(&slice), nil); err != nil { + t.Fatal(err) + } + if local.R.Post == nil { + t.Error("struct should have been eager loaded") + } + + local.R.Post = nil + if err = local.L.LoadPost(ctx, tx, true, &local, nil); err != nil { + t.Fatal(err) + } + if local.R.Post == nil { + t.Error("struct should have been eager loaded") + } + + if !ranAfterSelectHook { + t.Error("failed to run AfterSelect hook for relationship") + } +} + +func testCommentToOneCommentUsingReply(t *testing.T) { + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var local Comment + var foreign Comment + + seed := randomize.NewSeed() + if err := randomize.Struct(seed, &local, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + if err := randomize.Struct(seed, &foreign, commentDBTypes, false, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + if err := foreign.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + + queries.Assign(&local.ReplyID, foreign.ID) + if err := local.Insert(ctx, tx, boil.Infer()); err != nil { + t.Fatal(err) + } + + check, err := local.Reply().One(ctx, tx) + if err != nil { + t.Fatal(err) + } + + if !queries.Equal(check.ID, foreign.ID) { + t.Errorf("want: %v, got %v", foreign.ID, check.ID) + } + + ranAfterSelectHook := false + AddCommentHook(boil.AfterSelectHook, func(ctx context.Context, e boil.ContextExecutor, o *Comment) error { + ranAfterSelectHook = true + return nil + }) + + slice := CommentSlice{&local} + if err = local.L.LoadReply(ctx, tx, false, (*[]*Comment)(&slice), nil); err != nil { + t.Fatal(err) + } + if local.R.Reply == nil { + t.Error("struct should have been eager loaded") + } + + local.R.Reply = nil + if err = local.L.LoadReply(ctx, tx, true, &local, nil); err != nil { + t.Fatal(err) + } + if local.R.Reply == nil { + t.Error("struct should have been eager loaded") + } + + if !ranAfterSelectHook { + t.Error("failed to run AfterSelect hook for relationship") + } +} + +func testCommentToOneUserUsingUser(t *testing.T) { + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var local Comment + var foreign User + + seed := randomize.NewSeed() + if err := randomize.Struct(seed, &local, commentDBTypes, false, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment 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 := CommentSlice{&local} + if err = local.L.LoadUser(ctx, tx, false, (*[]*Comment)(&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 testCommentToOneSetOpPostUsingPost(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a Comment + var b, c Post + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, commentDBTypes, false, strmangle.SetComplement(commentPrimaryKeyColumns, commentColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + if err = randomize.Struct(seed, &b, postDBTypes, false, strmangle.SetComplement(postPrimaryKeyColumns, postColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + if err = randomize.Struct(seed, &c, postDBTypes, false, strmangle.SetComplement(postPrimaryKeyColumns, postColumnsWithoutDefault)...); 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 []*Post{&b, &c} { + err = a.SetPost(ctx, tx, i != 0, x) + if err != nil { + t.Fatal(err) + } + + if a.R.Post != x { + t.Error("relationship struct not set to correct value") + } + + if x.R.Comments[0] != &a { + t.Error("failed to append to foreign relationship struct") + } + if a.PostID != x.ID { + t.Error("foreign key was wrong value", a.PostID) + } + + zero := reflect.Zero(reflect.TypeOf(a.PostID)) + reflect.Indirect(reflect.ValueOf(&a.PostID)).Set(zero) + + if err = a.Reload(ctx, tx); err != nil { + t.Fatal("failed to reload", err) + } + + if a.PostID != x.ID { + t.Error("foreign key was wrong value", a.PostID, x.ID) + } + } +} +func testCommentToOneSetOpCommentUsingReply(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a Comment + var b, c Comment + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, commentDBTypes, false, strmangle.SetComplement(commentPrimaryKeyColumns, commentColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + if err = randomize.Struct(seed, &b, commentDBTypes, false, strmangle.SetComplement(commentPrimaryKeyColumns, commentColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + if err = randomize.Struct(seed, &c, 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) + } + + for i, x := range []*Comment{&b, &c} { + err = a.SetReply(ctx, tx, i != 0, x) + if err != nil { + t.Fatal(err) + } + + if a.R.Reply != x { + t.Error("relationship struct not set to correct value") + } + + if x.R.ReplyComments[0] != &a { + t.Error("failed to append to foreign relationship struct") + } + if !queries.Equal(a.ReplyID, x.ID) { + t.Error("foreign key was wrong value", a.ReplyID) + } + + zero := reflect.Zero(reflect.TypeOf(a.ReplyID)) + reflect.Indirect(reflect.ValueOf(&a.ReplyID)).Set(zero) + + if err = a.Reload(ctx, tx); err != nil { + t.Fatal("failed to reload", err) + } + + if !queries.Equal(a.ReplyID, x.ID) { + t.Error("foreign key was wrong value", a.ReplyID, x.ID) + } + } +} + +func testCommentToOneRemoveOpCommentUsingReply(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a Comment + var b Comment + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, commentDBTypes, false, strmangle.SetComplement(commentPrimaryKeyColumns, commentColumnsWithoutDefault)...); err != nil { + t.Fatal(err) + } + if err = randomize.Struct(seed, &b, 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 = a.SetReply(ctx, tx, true, &b); err != nil { + t.Fatal(err) + } + + if err = a.RemoveReply(ctx, tx, &b); err != nil { + t.Error("failed to remove relationship") + } + + count, err := a.Reply().Count(ctx, tx) + if err != nil { + t.Error(err) + } + if count != 0 { + t.Error("want no relationships remaining") + } + + if a.R.Reply != nil { + t.Error("R struct entry should be nil") + } + + if !queries.IsValuerNil(a.ReplyID) { + t.Error("foreign key value should be nil") + } + + if len(b.R.ReplyComments) != 0 { + t.Error("failed to remove a from b's relationships") + } +} + +func testCommentToOneSetOpUserUsingUser(t *testing.T) { + var err error + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + + var a Comment + var b, c User + + seed := randomize.NewSeed() + if err = randomize.Struct(seed, &a, commentDBTypes, false, strmangle.SetComplement(commentPrimaryKeyColumns, commentColumnsWithoutDefault)...); 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.Comments[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 testCommentsReload(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + if err = o.Reload(ctx, tx); err != nil { + t.Error(err) + } +} + +func testCommentsReloadAll(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + slice := CommentSlice{o} + + if err = slice.ReloadAll(ctx, tx); err != nil { + t.Error(err) + } +} + +func testCommentsSelect(t *testing.T) { + t.Parallel() + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + slice, err := Comments().All(ctx, tx) + if err != nil { + t.Error(err) + } + + if len(slice) != 1 { + t.Error("want one record, got:", len(slice)) + } +} + +var ( + commentDBTypes = map[string]string{`ID`: `integer`, `Comment`: `text`, `UserID`: `integer`, `PostID`: `integer`, `ReplyID`: `integer`, `State`: `enum.postable_state('hidden','visible')`, `CreatedAt`: `timestamp with time zone`, `UpdatedAt`: `timestamp with time zone`} + _ = bytes.MinRead +) + +func testCommentsUpdate(t *testing.T) { + t.Parallel() + + if 0 == len(commentPrimaryKeyColumns) { + t.Skip("Skipping table with no primary key columns") + } + if len(commentAllColumns) == len(commentPrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + count, err := Comments().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 1 { + t.Error("want one record, got:", count) + } + + if err = randomize.Struct(seed, o, commentDBTypes, true, commentPrimaryKeyColumns...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + if rowsAff, err := o.Update(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } else if rowsAff != 1 { + t.Error("should only affect one row but affected", rowsAff) + } +} + +func testCommentsSliceUpdateAll(t *testing.T) { + t.Parallel() + + if len(commentAllColumns) == len(commentPrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + + seed := randomize.NewSeed() + var err error + o := &Comment{} + if err = randomize.Struct(seed, o, commentDBTypes, true, commentColumnsWithDefault...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Insert(ctx, tx, boil.Infer()); err != nil { + t.Error(err) + } + + count, err := Comments().Count(ctx, tx) + if err != nil { + t.Error(err) + } + + if count != 1 { + t.Error("want one record, got:", count) + } + + if err = randomize.Struct(seed, o, commentDBTypes, true, commentPrimaryKeyColumns...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + // Remove Primary keys and unique columns from what we plan to update + var fields []string + if strmangle.StringSliceMatch(commentAllColumns, commentPrimaryKeyColumns) { + fields = commentAllColumns + } else { + fields = strmangle.SetComplement( + commentAllColumns, + commentPrimaryKeyColumns, + ) + } + + value := reflect.Indirect(reflect.ValueOf(o)) + typ := reflect.TypeOf(o).Elem() + n := typ.NumField() + + updateMap := M{} + for _, col := range fields { + for i := 0; i < n; i++ { + f := typ.Field(i) + if f.Tag.Get("boil") == col { + updateMap[col] = value.Field(i).Interface() + } + } + } + + slice := CommentSlice{o} + if rowsAff, err := slice.UpdateAll(ctx, tx, updateMap); err != nil { + t.Error(err) + } else if rowsAff != 1 { + t.Error("wanted one record updated but got", rowsAff) + } +} + +func testCommentsUpsert(t *testing.T) { + t.Parallel() + + if len(commentAllColumns) == len(commentPrimaryKeyColumns) { + t.Skip("Skipping table with only primary key columns") + } + + seed := randomize.NewSeed() + var err error + // Attempt the INSERT side of an UPSERT + o := Comment{} + if err = randomize.Struct(seed, &o, commentDBTypes, true); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + ctx := context.Background() + tx := MustTx(boil.BeginTx(ctx, nil)) + defer func() { _ = tx.Rollback() }() + if err = o.Upsert(ctx, tx, false, nil, boil.Infer(), boil.Infer()); err != nil { + t.Errorf("Unable to upsert Comment: %s", err) + } + + count, err := Comments().Count(ctx, tx) + if err != nil { + t.Error(err) + } + if count != 1 { + t.Error("want one record, got:", count) + } + + // Attempt the UPDATE side of an UPSERT + if err = randomize.Struct(seed, &o, commentDBTypes, false, commentPrimaryKeyColumns...); err != nil { + t.Errorf("Unable to randomize Comment struct: %s", err) + } + + if err = o.Upsert(ctx, tx, true, nil, boil.Infer(), boil.Infer()); err != nil { + t.Errorf("Unable to upsert Comment: %s", err) + } + + count, err = Comments().Count(ctx, tx) + if err != nil { + t.Error(err) + } + if count != 1 { + t.Error("want one record, got:", count) + } +} |
