Пример #1
0
func TestSelectByPK(t *testing.T) {
	t.Parallel()

	tx := begin(t)
	defer tx.Rollback()

	customer, err := data.SelectCustomerByPK(tx, -1)
	if err != data.ErrNotFound {
		t.Fatalf("Expected SelectCustomerByPK to return err data.ErrNotFound but it was: %v", err)
	}

	insertedRow := data.Customer{
		FirstName: data.String{Value: "John", Status: data.Present},
		LastName:  data.String{Value: "Smith", Status: data.Present},
		BirthDate: data.Time{Status: data.Null},
	}

	err = data.InsertCustomer(tx, &insertedRow)
	if err != nil {
		t.Fatalf("InsertCustomer unexpectedly failed: %v", err)
	}

	customer, err = data.SelectCustomerByPK(tx, insertedRow.ID.Value)
	if err != nil {
		t.Fatalf("SelectCustomerByPK unexpectedly failed: %v", err)
	}

	if customer.FirstName != insertedRow.FirstName {
		t.Errorf("Expected FirstName to be %v, but it was %v", data.String{Value: "John", Status: data.Present}, customer.FirstName)
	}
	if customer.LastName != insertedRow.LastName {
		t.Errorf("Expected LastName to be %v, but it was %v", data.String{Value: "Smith", Status: data.Present}, customer.LastName)
	}
}
Пример #2
0
func TestCount(t *testing.T) {
	t.Parallel()

	tx := begin(t)
	defer tx.Rollback()

	customerCount, err := data.CountCustomer(tx)
	if err != nil {
		t.Fatalf("CountCustomer unexpectedly failed: %v", err)
	}
	if customerCount != 0 {
		t.Fatalf("Expected CountCustomer to return %v, but is was %v", 0, customerCount)
	}

	err = data.InsertCustomer(tx, &data.Customer{
		FirstName: data.String{Value: "John", Status: data.Present},
		LastName:  data.String{Value: "Smith", Status: data.Present},
		BirthDate: data.Time{Status: data.Null},
	})
	if err != nil {
		t.Fatalf("InsertCustomer unexpectedly failed: %v", err)
	}

	customerCount, err = data.CountCustomer(tx)
	if err != nil {
		t.Fatalf("CountCustomer unexpectedly failed: %v", err)
	}
	if customerCount != 1 {
		t.Fatalf("Expected CountCustomer to return %v, but is was %v", 1, customerCount)
	}
}
Пример #3
0
func TestDelete(t *testing.T) {
	t.Parallel()

	tx := begin(t)
	defer tx.Rollback()

	insertedRow := data.Customer{
		FirstName: data.String{Value: "John", Status: data.Present},
		LastName:  data.String{Value: "Smith", Status: data.Present},
	}

	err := data.InsertCustomer(tx, &insertedRow)
	if err != nil {
		t.Fatalf("InsertCustomer unexpectedly failed: %v", err)
	}

	_, err = data.SelectCustomerByPK(tx, insertedRow.ID.Value)
	if err != nil {
		t.Fatalf("SelectCustomerByPK unexpectedly failed: %v", err)
	}

	err = data.DeleteCustomer(tx, insertedRow.ID.Value)
	if err != nil {
		t.Fatalf("DeleteCustomer unexpectedly failed: %v", err)
	}

	_, err = data.SelectCustomerByPK(tx, insertedRow.ID.Value)
	if err != data.ErrNotFound {
		t.Fatalf("Expected SelectCustomerByPK to return err data.ErrNotFound but it was: %v", err)
	}
}
Пример #4
0
func TestInsertOverridingPK(t *testing.T) {
	t.Parallel()

	tx := begin(t)
	defer tx.Rollback()

	insertedRow := data.Customer{
		ID:        data.Int32{Value: -2, Status: data.Present},
		FirstName: data.String{Value: "John", Status: data.Present},
		LastName:  data.String{Value: "Smith", Status: data.Present},
	}

	err := data.InsertCustomer(tx, &insertedRow)
	if err != nil {
		t.Fatalf("InsertCustomer unexpectedly failed: %v", err)
	}

	customer, err := data.SelectCustomerByPK(tx, -2)
	if err != nil {
		t.Fatalf("SelectCustomerByPK unexpectedly failed: %v", err)
	}

	if customer.FirstName != insertedRow.FirstName {
		t.Errorf("Expected FirstName to be %v, but it was %v", data.String{Value: "John", Status: data.Present}, customer.FirstName)
	}
	if customer.LastName != insertedRow.LastName {
		t.Errorf("Expected LastName to be %v, but it was %v", data.String{Value: "Smith", Status: data.Present}, customer.LastName)
	}
}
Пример #5
0
func TestSelectAll(t *testing.T) {
	t.Parallel()

	tx := begin(t)
	defer tx.Rollback()

	customers, err := data.SelectAllCustomer(tx)
	if err != nil {
		t.Fatalf("SelectAllCustomer unexpectedly failed: %v", err)
	}
	if len(customers) != 0 {
		t.Fatalf("Expected SelectAllCustomer to return %d rows, but is was %d", 0, len(customers))
	}

	insertedRow := data.Customer{
		FirstName: data.String{Value: "John", Status: data.Present},
		LastName:  data.String{Value: "Smith", Status: data.Present},
		BirthDate: data.Time{Status: data.Null},
	}

	err = data.InsertCustomer(tx, &insertedRow)
	if err != nil {
		t.Fatalf("InsertCustomer unexpectedly failed: %v", err)
	}

	customers, err = data.SelectAllCustomer(tx)
	if err != nil {
		t.Fatalf("SelectAllCustomer unexpectedly failed: %v", err)
	}
	if len(customers) != 1 {
		t.Fatalf("Expected SelectAllCustomer to return %d rows, but is was %d", 1, len(customers))
	}

	if customers[0].FirstName != insertedRow.FirstName {
		t.Errorf("Expected FirstName to be %v, but it was %v", insertedRow.FirstName, customers[0].FirstName)
	}
	if customers[0].LastName != insertedRow.LastName {
		t.Errorf("Expected LastName to be %v, but it was %v", insertedRow.LastName, customers[0].LastName)
	}
}