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 (

	_ = queries.Equal
)

func testCustomerAddresses(t *testing.T) {
	t.Parallel()

	query := CustomerAddresses()

	if query.Query == nil {
		t.Error("expected a query, got nothing")
	}
}

func testCustomerAddressesDelete(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if count != 0 {
		t.Error("want zero records, got:", count)
	}
}

func testCustomerAddressesQueryDeleteAll(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().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 := CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if count != 0 {
		t.Error("want zero records, got:", count)
	}
}

func testCustomerAddressesSliceDeleteAll(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddressSlice{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 := CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if count != 0 {
		t.Error("want zero records, got:", count)
	}
}

func testCustomerAddressesExists(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddressExists(ctx, tx, o.ID)
	if err != nil {
		t.Errorf("Unable to check if CustomerAddress exists: %s", err)
	}
	if !e {
		t.Errorf("Expected CustomerAddressExists to return true, but got false.")
	}
}

func testCustomerAddressesFind(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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)
	}

	customerAddressFound, err := FindCustomerAddress(ctx, tx, o.ID)
	if err != nil {
		t.Error(err)
	}

	if customerAddressFound == nil {
		t.Error("want a record, got nil")
	}
}

func testCustomerAddressesBind(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 = CustomerAddresses().Bind(ctx, tx, o); err != nil {
		t.Error(err)
	}
}

func testCustomerAddressesOne(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().One(ctx, tx); err != nil {
		t.Error(err)
	} else if x == nil {
		t.Error("expected to get a non nil record")
	}
}

func testCustomerAddressesAll(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	customerAddressOne := &CustomerAddress{}
	customerAddressTwo := &CustomerAddress{}
	if err = randomize.Struct(seed, customerAddressOne, customerAddressDBTypes, false, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}
	if err = randomize.Struct(seed, customerAddressTwo, customerAddressDBTypes, false, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()
	if err = customerAddressOne.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Error(err)
	}
	if err = customerAddressTwo.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Error(err)
	}

	slice, err := CustomerAddresses().All(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if len(slice) != 2 {
		t.Error("want 2 records, got:", len(slice))
	}
}

func testCustomerAddressesCount(t *testing.T) {
	t.Parallel()

	var err error
	seed := randomize.NewSeed()
	customerAddressOne := &CustomerAddress{}
	customerAddressTwo := &CustomerAddress{}
	if err = randomize.Struct(seed, customerAddressOne, customerAddressDBTypes, false, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}
	if err = randomize.Struct(seed, customerAddressTwo, customerAddressDBTypes, false, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()
	if err = customerAddressOne.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Error(err)
	}
	if err = customerAddressTwo.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Error(err)
	}

	count, err := CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if count != 2 {
		t.Error("want 2 records, got:", count)
	}
}

func customerAddressBeforeInsertHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressAfterInsertHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressAfterSelectHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressBeforeUpdateHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressAfterUpdateHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressBeforeDeleteHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressAfterDeleteHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressBeforeUpsertHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressAfterUpsertHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func testCustomerAddressesHooks(t *testing.T) {
	t.Parallel()

	var err error

	ctx := context.Background()
	empty := &CustomerAddress{}
	o := &CustomerAddress{}

	seed := randomize.NewSeed()
	if err = randomize.Struct(seed, o, customerAddressDBTypes, false); err != nil {
		t.Errorf("Unable to randomize CustomerAddress object: %s", err)
	}

	AddCustomerAddressHook(boil.BeforeInsertHook, customerAddressBeforeInsertHook)
	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)
	}
	customerAddressBeforeInsertHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.AfterInsertHook, customerAddressAfterInsertHook)
	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)
	}
	customerAddressAfterInsertHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.AfterSelectHook, customerAddressAfterSelectHook)
	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)
	}
	customerAddressAfterSelectHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.BeforeUpdateHook, customerAddressBeforeUpdateHook)
	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)
	}
	customerAddressBeforeUpdateHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.AfterUpdateHook, customerAddressAfterUpdateHook)
	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)
	}
	customerAddressAfterUpdateHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.BeforeDeleteHook, customerAddressBeforeDeleteHook)
	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)
	}
	customerAddressBeforeDeleteHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.AfterDeleteHook, customerAddressAfterDeleteHook)
	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)
	}
	customerAddressAfterDeleteHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.BeforeUpsertHook, customerAddressBeforeUpsertHook)
	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)
	}
	customerAddressBeforeUpsertHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.AfterUpsertHook, customerAddressAfterUpsertHook)
	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)
	}
	customerAddressAfterUpsertHooks = []CustomerAddressHook{}
}

func testCustomerAddressesInsert(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if count != 1 {
		t.Error("want one record, got:", count)
	}
}

func testCustomerAddressesInsertWhitelist(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()
	if err = o.Insert(ctx, tx, boil.Whitelist(customerAddressColumnsWithoutDefault...)); err != nil {
		t.Error(err)
	}

	count, err := CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if count != 1 {
		t.Error("want one record, got:", count)
	}
}

func testCustomerAddressToManyDefaultAddressCustomers(t *testing.T) {
	var err error
	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()

	var a CustomerAddress
	var b, c Customer

	seed := randomize.NewSeed()
	if err = randomize.Struct(seed, &a, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Fatal(err)
	}

	if err = randomize.Struct(seed, &b, customerDBTypes, false, customerColumnsWithDefault...); err != nil {
		t.Fatal(err)
	}
	if err = randomize.Struct(seed, &c, customerDBTypes, false, customerColumnsWithDefault...); err != nil {
		t.Fatal(err)
	}

	queries.Assign(&b.DefaultAddressID, a.ID)
	queries.Assign(&c.DefaultAddressID, 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.DefaultAddressCustomers().All(ctx, tx)
	if err != nil {
		t.Fatal(err)
	}

	bFound, cFound := false, false
	for _, v := range check {
		if queries.Equal(v.DefaultAddressID, b.DefaultAddressID) {
			bFound = true
		}
		if queries.Equal(v.DefaultAddressID, c.DefaultAddressID) {
			cFound = true
		}
	}

	if !bFound {
		t.Error("expected to find b")
	}
	if !cFound {
		t.Error("expected to find c")
	}

	slice := CustomerAddressSlice{&a}
	if err = a.L.LoadDefaultAddressCustomers(ctx, tx, false, (*[]*CustomerAddress)(&slice), nil); err != nil {
		t.Fatal(err)
	}
	if got := len(a.R.DefaultAddressCustomers); got != 2 {
		t.Error("number of eager loaded records wrong, got:", got)
	}

	a.R.DefaultAddressCustomers = nil
	if err = a.L.LoadDefaultAddressCustomers(ctx, tx, true, &a, nil); err != nil {
		t.Fatal(err)
	}
	if got := len(a.R.DefaultAddressCustomers); got != 2 {
		t.Error("number of eager loaded records wrong, got:", got)
	}

	if t.Failed() {
		t.Logf("%#v", check)
	}
}

func testCustomerAddressToManyAddOpDefaultAddressCustomers(t *testing.T) {
	var err error

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()

	var a CustomerAddress
	var b, c, d, e Customer

	seed := randomize.NewSeed()
	if err = randomize.Struct(seed, &a, customerAddressDBTypes, false, strmangle.SetComplement(customerAddressPrimaryKeyColumns, customerAddressColumnsWithoutDefault)...); err != nil {
		t.Fatal(err)
	}
	foreigners := []*Customer{&b, &c, &d, &e}
	for _, x := range foreigners {
		if err = randomize.Struct(seed, x, customerDBTypes, false, strmangle.SetComplement(customerPrimaryKeyColumns, customerColumnsWithoutDefault)...); 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 := [][]*Customer{
		{&b, &c},
		{&d, &e},
	}

	for i, x := range foreignersSplitByInsertion {
		err = a.AddDefaultAddressCustomers(ctx, tx, i != 0, x...)
		if err != nil {
			t.Fatal(err)
		}

		first := x[0]
		second := x[1]

		if !queries.Equal(a.ID, first.DefaultAddressID) {
			t.Error("foreign key was wrong value", a.ID, first.DefaultAddressID)
		}
		if !queries.Equal(a.ID, second.DefaultAddressID) {
			t.Error("foreign key was wrong value", a.ID, second.DefaultAddressID)
		}

		if first.R.DefaultAddress != &a {
			t.Error("relationship was not added properly to the foreign slice")
		}
		if second.R.DefaultAddress != &a {
			t.Error("relationship was not added properly to the foreign slice")
		}

		if a.R.DefaultAddressCustomers[i*2] != first {
			t.Error("relationship struct slice not set to correct value")
		}
		if a.R.DefaultAddressCustomers[i*2+1] != second {
			t.Error("relationship struct slice not set to correct value")
		}

		count, err := a.DefaultAddressCustomers().Count(ctx, tx)
		if err != nil {
			t.Fatal(err)
		}
		if want := int64((i + 1) * 2); count != want {
			t.Error("want", want, "got", count)
		}
	}
}

func testCustomerAddressToManySetOpDefaultAddressCustomers(t *testing.T) {
	var err error

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()

	var a CustomerAddress
	var b, c, d, e Customer

	seed := randomize.NewSeed()
	if err = randomize.Struct(seed, &a, customerAddressDBTypes, false, strmangle.SetComplement(customerAddressPrimaryKeyColumns, customerAddressColumnsWithoutDefault)...); err != nil {
		t.Fatal(err)
	}
	foreigners := []*Customer{&b, &c, &d, &e}
	for _, x := range foreigners {
		if err = randomize.Struct(seed, x, customerDBTypes, false, strmangle.SetComplement(customerPrimaryKeyColumns, customerColumnsWithoutDefault)...); 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.SetDefaultAddressCustomers(ctx, tx, false, &b, &c)
	if err != nil {
		t.Fatal(err)
	}

	count, err := a.DefaultAddressCustomers().Count(ctx, tx)
	if err != nil {
		t.Fatal(err)
	}
	if count != 2 {
		t.Error("count was wrong:", count)
	}

	err = a.SetDefaultAddressCustomers(ctx, tx, true, &d, &e)
	if err != nil {
		t.Fatal(err)
	}

	count, err = a.DefaultAddressCustomers().Count(ctx, tx)
	if err != nil {
		t.Fatal(err)
	}
	if count != 2 {
		t.Error("count was wrong:", count)
	}

	if !queries.IsValuerNil(b.DefaultAddressID) {
		t.Error("want b's foreign key value to be nil")
	}
	if !queries.IsValuerNil(c.DefaultAddressID) {
		t.Error("want c's foreign key value to be nil")
	}
	if !queries.Equal(a.ID, d.DefaultAddressID) {
		t.Error("foreign key was wrong value", a.ID, d.DefaultAddressID)
	}
	if !queries.Equal(a.ID, e.DefaultAddressID) {
		t.Error("foreign key was wrong value", a.ID, e.DefaultAddressID)
	}

	if b.R.DefaultAddress != nil {
		t.Error("relationship was not removed properly from the foreign struct")
	}
	if c.R.DefaultAddress != nil {
		t.Error("relationship was not removed properly from the foreign struct")
	}
	if d.R.DefaultAddress != &a {
		t.Error("relationship was not added properly to the foreign struct")
	}
	if e.R.DefaultAddress != &a {
		t.Error("relationship was not added properly to the foreign struct")
	}

	if a.R.DefaultAddressCustomers[0] != &d {
		t.Error("relationship struct slice not set to correct value")
	}
	if a.R.DefaultAddressCustomers[1] != &e {
		t.Error("relationship struct slice not set to correct value")
	}
}

func testCustomerAddressToManyRemoveOpDefaultAddressCustomers(t *testing.T) {
	var err error

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()

	var a CustomerAddress
	var b, c, d, e Customer

	seed := randomize.NewSeed()
	if err = randomize.Struct(seed, &a, customerAddressDBTypes, false, strmangle.SetComplement(customerAddressPrimaryKeyColumns, customerAddressColumnsWithoutDefault)...); err != nil {
		t.Fatal(err)
	}
	foreigners := []*Customer{&b, &c, &d, &e}
	for _, x := range foreigners {
		if err = randomize.Struct(seed, x, customerDBTypes, false, strmangle.SetComplement(customerPrimaryKeyColumns, customerColumnsWithoutDefault)...); err != nil {
			t.Fatal(err)
		}
	}

	if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Fatal(err)
	}

	err = a.AddDefaultAddressCustomers(ctx, tx, true, foreigners...)
	if err != nil {
		t.Fatal(err)
	}

	count, err := a.DefaultAddressCustomers().Count(ctx, tx)
	if err != nil {
		t.Fatal(err)
	}
	if count != 4 {
		t.Error("count was wrong:", count)
	}

	err = a.RemoveDefaultAddressCustomers(ctx, tx, foreigners[:2]...)
	if err != nil {
		t.Fatal(err)
	}

	count, err = a.DefaultAddressCustomers().Count(ctx, tx)
	if err != nil {
		t.Fatal(err)
	}
	if count != 2 {
		t.Error("count was wrong:", count)
	}

	if !queries.IsValuerNil(b.DefaultAddressID) {
		t.Error("want b's foreign key value to be nil")
	}
	if !queries.IsValuerNil(c.DefaultAddressID) {
		t.Error("want c's foreign key value to be nil")
	}

	if b.R.DefaultAddress != nil {
		t.Error("relationship was not removed properly from the foreign struct")
	}
	if c.R.DefaultAddress != nil {
		t.Error("relationship was not removed properly from the foreign struct")
	}
	if d.R.DefaultAddress != &a {
		t.Error("relationship to a should have been preserved")
	}
	if e.R.DefaultAddress != &a {
		t.Error("relationship to a should have been preserved")
	}

	if len(a.R.DefaultAddressCustomers) != 2 {
		t.Error("should have preserved two relationships")
	}

	if a.R.DefaultAddressCustomers[1] != &d {
		t.Error("relationship to d should have been preserved")
	}
	if a.R.DefaultAddressCustomers[0] != &e {
		t.Error("relationship to e should have been preserved")
	}
}

func testCustomerAddressToOneCustomerUsingCustomer(t *testing.T) {
	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()

	var local CustomerAddress
	var foreign Customer

	seed := randomize.NewSeed()
	if err := randomize.Struct(seed, &local, customerAddressDBTypes, false, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}
	if err := randomize.Struct(seed, &foreign, customerDBTypes, false, customerColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize Customer struct: %s", err)
	}

	if err := foreign.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Fatal(err)
	}

	local.CustomerID = foreign.ID
	if err := local.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Fatal(err)
	}

	check, err := local.Customer().One(ctx, tx)
	if err != nil {
		t.Fatal(err)
	}

	if check.ID != foreign.ID {
		t.Errorf("want: %v, got %v", foreign.ID, check.ID)
	}

	slice := CustomerAddressSlice{&local}
	if err = local.L.LoadCustomer(ctx, tx, false, (*[]*CustomerAddress)(&slice), nil); err != nil {
		t.Fatal(err)
	}
	if local.R.Customer == nil {
		t.Error("struct should have been eager loaded")
	}

	local.R.Customer = nil
	if err = local.L.LoadCustomer(ctx, tx, true, &local, nil); err != nil {
		t.Fatal(err)
	}
	if local.R.Customer == nil {
		t.Error("struct should have been eager loaded")
	}
}

func testCustomerAddressToOneSetOpCustomerUsingCustomer(t *testing.T) {
	var err error

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()

	var a CustomerAddress
	var b, c Customer

	seed := randomize.NewSeed()
	if err = randomize.Struct(seed, &a, customerAddressDBTypes, false, strmangle.SetComplement(customerAddressPrimaryKeyColumns, customerAddressColumnsWithoutDefault)...); err != nil {
		t.Fatal(err)
	}
	if err = randomize.Struct(seed, &b, customerDBTypes, false, strmangle.SetComplement(customerPrimaryKeyColumns, customerColumnsWithoutDefault)...); err != nil {
		t.Fatal(err)
	}
	if err = randomize.Struct(seed, &c, customerDBTypes, false, strmangle.SetComplement(customerPrimaryKeyColumns, customerColumnsWithoutDefault)...); 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 []*Customer{&b, &c} {
		err = a.SetCustomer(ctx, tx, i != 0, x)
		if err != nil {
			t.Fatal(err)
		}

		if a.R.Customer != x {
			t.Error("relationship struct not set to correct value")
		}

		if x.R.CustomerAddresses[0] != &a {
			t.Error("failed to append to foreign relationship struct")
		}
		if a.CustomerID != x.ID {
			t.Error("foreign key was wrong value", a.CustomerID)
		}

		zero := reflect.Zero(reflect.TypeOf(a.CustomerID))
		reflect.Indirect(reflect.ValueOf(&a.CustomerID)).Set(zero)

		if err = a.Reload(ctx, tx); err != nil {
			t.Fatal("failed to reload", err)
		}

		if a.CustomerID != x.ID {
			t.Error("foreign key was wrong value", a.CustomerID, x.ID)
		}
	}
}

func testCustomerAddressesReload(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 testCustomerAddressesReloadAll(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddressSlice{o}

	if err = slice.ReloadAll(ctx, tx); err != nil {
		t.Error(err)
	}
}

func testCustomerAddressesSelect(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().All(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if len(slice) != 1 {
		t.Error("want one record, got:", len(slice))
	}
}

var (
	customerAddressDBTypes = map[string]string{`ID`: `bigint`, `CustomerID`: `bigint`, `Address1`: `varchar`, `Address2`: `varchar`, `City`: `varchar`, `Company`: `varchar`, `Country`: `varchar`, `CountryCode`: `varchar`, `FirstName`: `varchar`, `IsDefault`: `tinyint`, `LastName`: `varchar`, `Name`: `varchar`, `Phone`: `varchar`, `Province`: `varchar`, `ProvinceCode`: `varchar`, `Zip`: `varchar`}
	_                      = bytes.MinRead
)

func testCustomerAddressesUpdate(t *testing.T) {
	t.Parallel()

	if 0 == len(customerAddressPrimaryKeyColumns) {
		t.Skip("Skipping table with no primary key columns")
	}
	if len(customerAddressAllColumns) == len(customerAddressPrimaryKeyColumns) {
		t.Skip("Skipping table with only primary key columns")
	}

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().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, customerAddressDBTypes, true, customerAddressPrimaryKeyColumns...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 testCustomerAddressesSliceUpdateAll(t *testing.T) {
	t.Parallel()

	if len(customerAddressAllColumns) == len(customerAddressPrimaryKeyColumns) {
		t.Skip("Skipping table with only primary key columns")
	}

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().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, customerAddressDBTypes, true, customerAddressPrimaryKeyColumns...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	var fields []string
	if strmangle.StringSliceMatch(customerAddressAllColumns, customerAddressPrimaryKeyColumns) {
		fields = customerAddressAllColumns
	} else {
		fields = strmangle.SetComplement(
			customerAddressAllColumns,
			customerAddressPrimaryKeyColumns,
		)
	}

	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 := CustomerAddressSlice{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 testCustomerAddressesUpsert(t *testing.T) {
	t.Parallel()

	if len(customerAddressAllColumns) == len(customerAddressPrimaryKeyColumns) {
		t.Skip("Skipping table with only primary key columns")
	}
	if len(mySQLCustomerAddressUniqueColumns) == 0 {
		t.Skip("Skipping table with no unique columns to conflict on")
	}

	seed := randomize.NewSeed()
	var err error

	o := CustomerAddress{}
	if err = randomize.Struct(seed, &o, customerAddressDBTypes, false); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()
	if err = o.Upsert(ctx, tx, boil.Infer(), boil.Infer()); err != nil {
		t.Errorf("Unable to upsert CustomerAddress: %s", err)
	}

	count, err := CustomerAddresses().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, customerAddressDBTypes, false, customerAddressPrimaryKeyColumns...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	if err = o.Upsert(ctx, tx, boil.Infer(), boil.Infer()); err != nil {
		t.Errorf("Unable to upsert CustomerAddress: %s", err)
	}

	count, err = CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}
	if count != 1 {
		t.Error("want one record, got:", count)
	}
}
// Code generated by SQLBoiler 4.7.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 testCustomerAddresses(t *testing.T) {
	t.Parallel()

	query := CustomerAddresses()

	if query.Query == nil {
		t.Error("expected a query, got nothing")
	}
}

func testCustomerAddressesDelete(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if count != 0 {
		t.Error("want zero records, got:", count)
	}
}

func testCustomerAddressesQueryDeleteAll(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().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 := CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if count != 0 {
		t.Error("want zero records, got:", count)
	}
}

func testCustomerAddressesSliceDeleteAll(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddressSlice{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 := CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if count != 0 {
		t.Error("want zero records, got:", count)
	}
}

func testCustomerAddressesExists(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddressExists(ctx, tx, o.ID)
	if err != nil {
		t.Errorf("Unable to check if CustomerAddress exists: %s", err)
	}
	if !e {
		t.Errorf("Expected CustomerAddressExists to return true, but got false.")
	}
}

func testCustomerAddressesFind(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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)
	}

	customerAddressFound, err := FindCustomerAddress(ctx, tx, o.ID)
	if err != nil {
		t.Error(err)
	}

	if customerAddressFound == nil {
		t.Error("want a record, got nil")
	}
}

func testCustomerAddressesBind(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 = CustomerAddresses().Bind(ctx, tx, o); err != nil {
		t.Error(err)
	}
}

func testCustomerAddressesOne(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().One(ctx, tx); err != nil {
		t.Error(err)
	} else if x == nil {
		t.Error("expected to get a non nil record")
	}
}

func testCustomerAddressesAll(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	customerAddressOne := &CustomerAddress{}
	customerAddressTwo := &CustomerAddress{}
	if err = randomize.Struct(seed, customerAddressOne, customerAddressDBTypes, false, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}
	if err = randomize.Struct(seed, customerAddressTwo, customerAddressDBTypes, false, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()
	if err = customerAddressOne.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Error(err)
	}
	if err = customerAddressTwo.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Error(err)
	}

	slice, err := CustomerAddresses().All(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if len(slice) != 2 {
		t.Error("want 2 records, got:", len(slice))
	}
}

func testCustomerAddressesCount(t *testing.T) {
	t.Parallel()

	var err error
	seed := randomize.NewSeed()
	customerAddressOne := &CustomerAddress{}
	customerAddressTwo := &CustomerAddress{}
	if err = randomize.Struct(seed, customerAddressOne, customerAddressDBTypes, false, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}
	if err = randomize.Struct(seed, customerAddressTwo, customerAddressDBTypes, false, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()
	if err = customerAddressOne.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Error(err)
	}
	if err = customerAddressTwo.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Error(err)
	}

	count, err := CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if count != 2 {
		t.Error("want 2 records, got:", count)
	}
}

func customerAddressBeforeInsertHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressAfterInsertHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressAfterSelectHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressBeforeUpdateHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressAfterUpdateHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressBeforeDeleteHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressAfterDeleteHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressBeforeUpsertHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func customerAddressAfterUpsertHook(ctx context.Context, e boil.ContextExecutor, o *CustomerAddress) error {
	*o = CustomerAddress{}
	return nil
}

func testCustomerAddressesHooks(t *testing.T) {
	t.Parallel()

	var err error

	ctx := context.Background()
	empty := &CustomerAddress{}
	o := &CustomerAddress{}

	seed := randomize.NewSeed()
	if err = randomize.Struct(seed, o, customerAddressDBTypes, false); err != nil {
		t.Errorf("Unable to randomize CustomerAddress object: %s", err)
	}

	AddCustomerAddressHook(boil.BeforeInsertHook, customerAddressBeforeInsertHook)
	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)
	}
	customerAddressBeforeInsertHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.AfterInsertHook, customerAddressAfterInsertHook)
	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)
	}
	customerAddressAfterInsertHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.AfterSelectHook, customerAddressAfterSelectHook)
	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)
	}
	customerAddressAfterSelectHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.BeforeUpdateHook, customerAddressBeforeUpdateHook)
	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)
	}
	customerAddressBeforeUpdateHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.AfterUpdateHook, customerAddressAfterUpdateHook)
	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)
	}
	customerAddressAfterUpdateHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.BeforeDeleteHook, customerAddressBeforeDeleteHook)
	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)
	}
	customerAddressBeforeDeleteHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.AfterDeleteHook, customerAddressAfterDeleteHook)
	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)
	}
	customerAddressAfterDeleteHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.BeforeUpsertHook, customerAddressBeforeUpsertHook)
	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)
	}
	customerAddressBeforeUpsertHooks = []CustomerAddressHook{}

	AddCustomerAddressHook(boil.AfterUpsertHook, customerAddressAfterUpsertHook)
	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)
	}
	customerAddressAfterUpsertHooks = []CustomerAddressHook{}
}

func testCustomerAddressesInsert(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if count != 1 {
		t.Error("want one record, got:", count)
	}
}

func testCustomerAddressesInsertWhitelist(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()
	if err = o.Insert(ctx, tx, boil.Whitelist(customerAddressColumnsWithoutDefault...)); err != nil {
		t.Error(err)
	}

	count, err := CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if count != 1 {
		t.Error("want one record, got:", count)
	}
}

func testCustomerAddressToManyDefaultAddressCustomers(t *testing.T) {
	var err error
	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()

	var a CustomerAddress
	var b, c Customer

	seed := randomize.NewSeed()
	if err = randomize.Struct(seed, &a, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Fatal(err)
	}

	if err = randomize.Struct(seed, &b, customerDBTypes, false, customerColumnsWithDefault...); err != nil {
		t.Fatal(err)
	}
	if err = randomize.Struct(seed, &c, customerDBTypes, false, customerColumnsWithDefault...); err != nil {
		t.Fatal(err)
	}

	queries.Assign(&b.DefaultAddressID, a.ID)
	queries.Assign(&c.DefaultAddressID, 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.DefaultAddressCustomers().All(ctx, tx)
	if err != nil {
		t.Fatal(err)
	}

	bFound, cFound := false, false
	for _, v := range check {
		if queries.Equal(v.DefaultAddressID, b.DefaultAddressID) {
			bFound = true
		}
		if queries.Equal(v.DefaultAddressID, c.DefaultAddressID) {
			cFound = true
		}
	}

	if !bFound {
		t.Error("expected to find b")
	}
	if !cFound {
		t.Error("expected to find c")
	}

	slice := CustomerAddressSlice{&a}
	if err = a.L.LoadDefaultAddressCustomers(ctx, tx, false, (*[]*CustomerAddress)(&slice), nil); err != nil {
		t.Fatal(err)
	}
	if got := len(a.R.DefaultAddressCustomers); got != 2 {
		t.Error("number of eager loaded records wrong, got:", got)
	}

	a.R.DefaultAddressCustomers = nil
	if err = a.L.LoadDefaultAddressCustomers(ctx, tx, true, &a, nil); err != nil {
		t.Fatal(err)
	}
	if got := len(a.R.DefaultAddressCustomers); got != 2 {
		t.Error("number of eager loaded records wrong, got:", got)
	}

	if t.Failed() {
		t.Logf("%#v", check)
	}
}

func testCustomerAddressToManyAddOpDefaultAddressCustomers(t *testing.T) {
	var err error

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()

	var a CustomerAddress
	var b, c, d, e Customer

	seed := randomize.NewSeed()
	if err = randomize.Struct(seed, &a, customerAddressDBTypes, false, strmangle.SetComplement(customerAddressPrimaryKeyColumns, customerAddressColumnsWithoutDefault)...); err != nil {
		t.Fatal(err)
	}
	foreigners := []*Customer{&b, &c, &d, &e}
	for _, x := range foreigners {
		if err = randomize.Struct(seed, x, customerDBTypes, false, strmangle.SetComplement(customerPrimaryKeyColumns, customerColumnsWithoutDefault)...); 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 := [][]*Customer{
		{&b, &c},
		{&d, &e},
	}

	for i, x := range foreignersSplitByInsertion {
		err = a.AddDefaultAddressCustomers(ctx, tx, i != 0, x...)
		if err != nil {
			t.Fatal(err)
		}

		first := x[0]
		second := x[1]

		if !queries.Equal(a.ID, first.DefaultAddressID) {
			t.Error("foreign key was wrong value", a.ID, first.DefaultAddressID)
		}
		if !queries.Equal(a.ID, second.DefaultAddressID) {
			t.Error("foreign key was wrong value", a.ID, second.DefaultAddressID)
		}

		if first.R.DefaultAddress != &a {
			t.Error("relationship was not added properly to the foreign slice")
		}
		if second.R.DefaultAddress != &a {
			t.Error("relationship was not added properly to the foreign slice")
		}

		if a.R.DefaultAddressCustomers[i*2] != first {
			t.Error("relationship struct slice not set to correct value")
		}
		if a.R.DefaultAddressCustomers[i*2+1] != second {
			t.Error("relationship struct slice not set to correct value")
		}

		count, err := a.DefaultAddressCustomers().Count(ctx, tx)
		if err != nil {
			t.Fatal(err)
		}
		if want := int64((i + 1) * 2); count != want {
			t.Error("want", want, "got", count)
		}
	}
}

func testCustomerAddressToManySetOpDefaultAddressCustomers(t *testing.T) {
	var err error

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()

	var a CustomerAddress
	var b, c, d, e Customer

	seed := randomize.NewSeed()
	if err = randomize.Struct(seed, &a, customerAddressDBTypes, false, strmangle.SetComplement(customerAddressPrimaryKeyColumns, customerAddressColumnsWithoutDefault)...); err != nil {
		t.Fatal(err)
	}
	foreigners := []*Customer{&b, &c, &d, &e}
	for _, x := range foreigners {
		if err = randomize.Struct(seed, x, customerDBTypes, false, strmangle.SetComplement(customerPrimaryKeyColumns, customerColumnsWithoutDefault)...); 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.SetDefaultAddressCustomers(ctx, tx, false, &b, &c)
	if err != nil {
		t.Fatal(err)
	}

	count, err := a.DefaultAddressCustomers().Count(ctx, tx)
	if err != nil {
		t.Fatal(err)
	}
	if count != 2 {
		t.Error("count was wrong:", count)
	}

	err = a.SetDefaultAddressCustomers(ctx, tx, true, &d, &e)
	if err != nil {
		t.Fatal(err)
	}

	count, err = a.DefaultAddressCustomers().Count(ctx, tx)
	if err != nil {
		t.Fatal(err)
	}
	if count != 2 {
		t.Error("count was wrong:", count)
	}

	if !queries.IsValuerNil(b.DefaultAddressID) {
		t.Error("want b's foreign key value to be nil")
	}
	if !queries.IsValuerNil(c.DefaultAddressID) {
		t.Error("want c's foreign key value to be nil")
	}
	if !queries.Equal(a.ID, d.DefaultAddressID) {
		t.Error("foreign key was wrong value", a.ID, d.DefaultAddressID)
	}
	if !queries.Equal(a.ID, e.DefaultAddressID) {
		t.Error("foreign key was wrong value", a.ID, e.DefaultAddressID)
	}

	if b.R.DefaultAddress != nil {
		t.Error("relationship was not removed properly from the foreign struct")
	}
	if c.R.DefaultAddress != nil {
		t.Error("relationship was not removed properly from the foreign struct")
	}
	if d.R.DefaultAddress != &a {
		t.Error("relationship was not added properly to the foreign struct")
	}
	if e.R.DefaultAddress != &a {
		t.Error("relationship was not added properly to the foreign struct")
	}

	if a.R.DefaultAddressCustomers[0] != &d {
		t.Error("relationship struct slice not set to correct value")
	}
	if a.R.DefaultAddressCustomers[1] != &e {
		t.Error("relationship struct slice not set to correct value")
	}
}

func testCustomerAddressToManyRemoveOpDefaultAddressCustomers(t *testing.T) {
	var err error

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()

	var a CustomerAddress
	var b, c, d, e Customer

	seed := randomize.NewSeed()
	if err = randomize.Struct(seed, &a, customerAddressDBTypes, false, strmangle.SetComplement(customerAddressPrimaryKeyColumns, customerAddressColumnsWithoutDefault)...); err != nil {
		t.Fatal(err)
	}
	foreigners := []*Customer{&b, &c, &d, &e}
	for _, x := range foreigners {
		if err = randomize.Struct(seed, x, customerDBTypes, false, strmangle.SetComplement(customerPrimaryKeyColumns, customerColumnsWithoutDefault)...); err != nil {
			t.Fatal(err)
		}
	}

	if err := a.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Fatal(err)
	}

	err = a.AddDefaultAddressCustomers(ctx, tx, true, foreigners...)
	if err != nil {
		t.Fatal(err)
	}

	count, err := a.DefaultAddressCustomers().Count(ctx, tx)
	if err != nil {
		t.Fatal(err)
	}
	if count != 4 {
		t.Error("count was wrong:", count)
	}

	err = a.RemoveDefaultAddressCustomers(ctx, tx, foreigners[:2]...)
	if err != nil {
		t.Fatal(err)
	}

	count, err = a.DefaultAddressCustomers().Count(ctx, tx)
	if err != nil {
		t.Fatal(err)
	}
	if count != 2 {
		t.Error("count was wrong:", count)
	}

	if !queries.IsValuerNil(b.DefaultAddressID) {
		t.Error("want b's foreign key value to be nil")
	}
	if !queries.IsValuerNil(c.DefaultAddressID) {
		t.Error("want c's foreign key value to be nil")
	}

	if b.R.DefaultAddress != nil {
		t.Error("relationship was not removed properly from the foreign struct")
	}
	if c.R.DefaultAddress != nil {
		t.Error("relationship was not removed properly from the foreign struct")
	}
	if d.R.DefaultAddress != &a {
		t.Error("relationship to a should have been preserved")
	}
	if e.R.DefaultAddress != &a {
		t.Error("relationship to a should have been preserved")
	}

	if len(a.R.DefaultAddressCustomers) != 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.DefaultAddressCustomers[1] != &d {
		t.Error("relationship to d should have been preserved")
	}
	if a.R.DefaultAddressCustomers[0] != &e {
		t.Error("relationship to e should have been preserved")
	}
}

func testCustomerAddressToOneCustomerUsingCustomer(t *testing.T) {
	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()

	var local CustomerAddress
	var foreign Customer

	seed := randomize.NewSeed()
	if err := randomize.Struct(seed, &local, customerAddressDBTypes, false, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}
	if err := randomize.Struct(seed, &foreign, customerDBTypes, false, customerColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize Customer struct: %s", err)
	}

	if err := foreign.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Fatal(err)
	}

	local.CustomerID = foreign.ID
	if err := local.Insert(ctx, tx, boil.Infer()); err != nil {
		t.Fatal(err)
	}

	check, err := local.Customer().One(ctx, tx)
	if err != nil {
		t.Fatal(err)
	}

	if check.ID != foreign.ID {
		t.Errorf("want: %v, got %v", foreign.ID, check.ID)
	}

	slice := CustomerAddressSlice{&local}
	if err = local.L.LoadCustomer(ctx, tx, false, (*[]*CustomerAddress)(&slice), nil); err != nil {
		t.Fatal(err)
	}
	if local.R.Customer == nil {
		t.Error("struct should have been eager loaded")
	}

	local.R.Customer = nil
	if err = local.L.LoadCustomer(ctx, tx, true, &local, nil); err != nil {
		t.Fatal(err)
	}
	if local.R.Customer == nil {
		t.Error("struct should have been eager loaded")
	}
}

func testCustomerAddressToOneSetOpCustomerUsingCustomer(t *testing.T) {
	var err error

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()

	var a CustomerAddress
	var b, c Customer

	seed := randomize.NewSeed()
	if err = randomize.Struct(seed, &a, customerAddressDBTypes, false, strmangle.SetComplement(customerAddressPrimaryKeyColumns, customerAddressColumnsWithoutDefault)...); err != nil {
		t.Fatal(err)
	}
	if err = randomize.Struct(seed, &b, customerDBTypes, false, strmangle.SetComplement(customerPrimaryKeyColumns, customerColumnsWithoutDefault)...); err != nil {
		t.Fatal(err)
	}
	if err = randomize.Struct(seed, &c, customerDBTypes, false, strmangle.SetComplement(customerPrimaryKeyColumns, customerColumnsWithoutDefault)...); 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 []*Customer{&b, &c} {
		err = a.SetCustomer(ctx, tx, i != 0, x)
		if err != nil {
			t.Fatal(err)
		}

		if a.R.Customer != x {
			t.Error("relationship struct not set to correct value")
		}

		if x.R.CustomerAddresses[0] != &a {
			t.Error("failed to append to foreign relationship struct")
		}
		if a.CustomerID != x.ID {
			t.Error("foreign key was wrong value", a.CustomerID)
		}

		zero := reflect.Zero(reflect.TypeOf(a.CustomerID))
		reflect.Indirect(reflect.ValueOf(&a.CustomerID)).Set(zero)

		if err = a.Reload(ctx, tx); err != nil {
			t.Fatal("failed to reload", err)
		}

		if a.CustomerID != x.ID {
			t.Error("foreign key was wrong value", a.CustomerID, x.ID)
		}
	}
}

func testCustomerAddressesReload(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 testCustomerAddressesReloadAll(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddressSlice{o}

	if err = slice.ReloadAll(ctx, tx); err != nil {
		t.Error(err)
	}
}

func testCustomerAddressesSelect(t *testing.T) {
	t.Parallel()

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().All(ctx, tx)
	if err != nil {
		t.Error(err)
	}

	if len(slice) != 1 {
		t.Error("want one record, got:", len(slice))
	}
}

var (
	customerAddressDBTypes = map[string]string{`ID`: `bigint`, `CustomerID`: `bigint`, `Address1`: `varchar`, `Address2`: `varchar`, `City`: `varchar`, `Company`: `varchar`, `Country`: `varchar`, `CountryCode`: `varchar`, `FirstName`: `varchar`, `IsDefault`: `tinyint`, `LastName`: `varchar`, `Name`: `varchar`, `Phone`: `varchar`, `Province`: `varchar`, `ProvinceCode`: `varchar`, `Zip`: `varchar`}
	_                      = bytes.MinRead
)

func testCustomerAddressesUpdate(t *testing.T) {
	t.Parallel()

	if 0 == len(customerAddressPrimaryKeyColumns) {
		t.Skip("Skipping table with no primary key columns")
	}
	if len(customerAddressAllColumns) == len(customerAddressPrimaryKeyColumns) {
		t.Skip("Skipping table with only primary key columns")
	}

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().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, customerAddressDBTypes, true, customerAddressPrimaryKeyColumns...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 testCustomerAddressesSliceUpdateAll(t *testing.T) {
	t.Parallel()

	if len(customerAddressAllColumns) == len(customerAddressPrimaryKeyColumns) {
		t.Skip("Skipping table with only primary key columns")
	}

	seed := randomize.NewSeed()
	var err error
	o := &CustomerAddress{}
	if err = randomize.Struct(seed, o, customerAddressDBTypes, true, customerAddressColumnsWithDefault...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress 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 := CustomerAddresses().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, customerAddressDBTypes, true, customerAddressPrimaryKeyColumns...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	// Remove Primary keys and unique columns from what we plan to update
	var fields []string
	if strmangle.StringSliceMatch(customerAddressAllColumns, customerAddressPrimaryKeyColumns) {
		fields = customerAddressAllColumns
	} else {
		fields = strmangle.SetComplement(
			customerAddressAllColumns,
			customerAddressPrimaryKeyColumns,
		)
	}

	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 := CustomerAddressSlice{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 testCustomerAddressesUpsert(t *testing.T) {
	t.Parallel()

	if len(customerAddressAllColumns) == len(customerAddressPrimaryKeyColumns) {
		t.Skip("Skipping table with only primary key columns")
	}
	if len(mySQLCustomerAddressUniqueColumns) == 0 {
		t.Skip("Skipping table with no unique columns to conflict on")
	}

	seed := randomize.NewSeed()
	var err error
	// Attempt the INSERT side of an UPSERT
	o := CustomerAddress{}
	if err = randomize.Struct(seed, &o, customerAddressDBTypes, false); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	ctx := context.Background()
	tx := MustTx(boil.BeginTx(ctx, nil))
	defer func() { _ = tx.Rollback() }()
	if err = o.Upsert(ctx, tx, boil.Infer(), boil.Infer()); err != nil {
		t.Errorf("Unable to upsert CustomerAddress: %s", err)
	}

	count, err := CustomerAddresses().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, customerAddressDBTypes, false, customerAddressPrimaryKeyColumns...); err != nil {
		t.Errorf("Unable to randomize CustomerAddress struct: %s", err)
	}

	if err = o.Upsert(ctx, tx, boil.Infer(), boil.Infer()); err != nil {
		t.Errorf("Unable to upsert CustomerAddress: %s", err)
	}

	count, err = CustomerAddresses().Count(ctx, tx)
	if err != nil {
		t.Error(err)
	}
	if count != 1 {
		t.Error("want one record, got:", count)
	}
}
