Exemplo n.º 1
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)
	}
}
Exemplo n.º 2
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)
	}
}
Exemplo n.º 3
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)
	}
}