Example #1
0
func TestAll(t *testing.T) {
	SetupDriver(fake.NewDriver())

	type Person struct {
		ModelMetadata `tablename:"people"`

		ID   int    `rebecca:"id" rebecca_primary:"true"`
		Name string `rebecca:"name"`
		Age  int    `rebecca:"age"`
	}

	p1 := &Person{Name: "John", Age: 37}
	p2 := &Person{Name: "Sarah", Age: 26}
	p3 := &Person{Name: "James", Age: 33}
	people := []*Person{p1, p2, p3}

	for _, p := range people {
		if err := Save(p); err != nil {
			t.Fatal(err)
		}
	}

	expected := []Person{*p1, *p2, *p3}
	actual := []Person{}
	if err := All(&actual); err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("Expected %+v to equal to %+v", actual, expected)
	}
}
Example #2
0
func TestSaveUpdates(t *testing.T) {
	SetupDriver(fake.NewDriver())

	type Person struct {
		ModelMetadata `tablename:"people"`

		ID   int    `rebecca:"id" rebecca_primary:"true"`
		Name string `rebecca:"name"`
		Age  int    `rebecca:"age"`
	}

	p := &Person{Name: "John Smith", Age: 31}
	if err := Save(p); err != nil {
		t.Fatal(err)
	}

	expected := &Person{}
	if err := Get(expected, p.ID); err != nil {
		t.Fatal(err)
	}

	expected.Name = "John Smith Jr"
	if err := Save(expected); err != nil {
		t.Fatal(err)
	}

	actual := &Person{}
	if err := Get(actual, p.ID); err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(expected, actual) {
		t.Errorf("Expected %+v to equal %+v", actual, expected)
	}
}
Example #3
0
func TestExec(t *testing.T) {
	d := fake.NewDriver()
	SetupDriver(d)

	if err := Exec("SOME QUERY $1, $2", 42, "hello"); err != nil {
		t.Fatal(err)
	}

	actual := d.ReceivedExec()
	expected := fake.ReceivedExec{
		Tx:    nil,
		Query: "SOME QUERY $1, $2",
		Args:  []interface{}{42, "hello"},
	}
	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("Expected driver to receive exec %#v, but got %#v", expected, actual)
	}
}
Example #4
0
func TestDoubleCommit(t *testing.T) {
	rebecca.SetupDriver(fake.NewDriver())

	tx, err := rebecca.Begin()
	if err != nil {
		t.Fatal(err)
	}

	if err := tx.Commit(); err != nil {
		t.Fatal(err)
	}

	err = tx.Commit()
	if err == nil {
		t.Fatal("Expected transaction to no being able to be committed twice")
	}

	expected := `Unable to commit transaction - Current transaction is already finished`
	actual := err.Error()
	if actual != expected {
		t.Errorf("Expected %s to equal %s", actual, expected)
	}
}
Example #5
0
func TestTransactions(t *testing.T) {
	d := fake.NewDriver()
	rebecca.SetupDriver(d)

	d.RegisterWhere("title = $1", func(record []field.Field, args ...interface{}) (bool, error) {
		for _, f := range record {
			if f.DriverName == "title" {
				return f.Value.(string) == args[0].(string), nil
			}
		}

		return false, fmt.Errorf("record %+v does not have title field", record)
	})

	txa, err := rebecca.Begin()
	if err != nil {
		t.Fatal(err)
	}
	defer txa.Rollback()

	txb, err := rebecca.Begin()
	if err != nil {
		t.Fatal(err)
	}
	defer txb.Rollback()

	pa := &Post{Title: "Hello world", Content: "Content of Hello World, many hellos", CreatedAt: time.Now()}
	if err := txa.Save(pa); err != nil {
		t.Fatal(err)
	}

	pa2 := &Post{Title: "Hello Blog", Content: "More hellos here!", CreatedAt: time.Now()}
	if err := txa.Save(pa2); err != nil {
		t.Fatal(err)
	}

	actual := &Post{}
	if err := txa.Get(pa.ID, actual); err != nil {
		t.Fatal(err)
	}

	actual = &Post{}
	if err := txb.Get(pa.ID, actual); err == nil {
		t.Errorf(
			"Expected transaction B not to find record saved in transaction A, but got: %+v",
			actual,
		)
	}

	pb := &Post{Title: "Super Post", Content: "Super Content", CreatedAt: time.Now()}
	if err := txb.Save(pb); err != nil {
		t.Fatal(err)
	}

	actual = &Post{}
	if err := txa.Get(pb.ID, actual); err == nil {
		t.Errorf(
			"Expected transaction A not to find record saved in transaction B, but got: %+v",
			actual,
		)
	}

	expecteds := []Post{*pa, *pa2}
	actuals := []Post{}
	if err := txa.All(&actuals); err != nil {
		t.Fatal(err)
	}

	if !equalPosts(actuals, expecteds) {
		t.Errorf("Expected %+v to equal %+v", actuals, expecteds)
	}

	expecteds = []Post{*pa}
	actuals = []Post{}
	if err := txa.Where(&actuals, "title = $1", "Hello world"); err != nil {
		t.Fatal(err)
	}

	if !equalPosts(actuals, expecteds) {
		t.Errorf("Expected %+v to equal %+v", actuals, expecteds)
	}

	expecteds = []Post{*pa2}
	actuals = []Post{}
	if err := txa.Where(&actuals, "title = $1", "Hello Blog"); err != nil {
		t.Fatal(err)
	}

	if !equalPosts(actuals, expecteds) {
		t.Errorf("Expected %+v to equal %+v", actuals, expecteds)
	}

	expecteds = []Post{}
	actuals = []Post{}
	if err := txa.Where(&actuals, "title = $1", "Super Post"); err != nil {
		t.Fatal(err)
	}

	if !equalPosts(actuals, expecteds) {
		t.Errorf("Expected %+v to equal %+v", actuals, expecteds)
	}

	expecteds = []Post{*pb}
	actuals = []Post{}
	if err := txb.All(&actuals); err != nil {
		t.Fatal(err)
	}

	if !equalPosts(actuals, expecteds) {
		t.Errorf("Expected %+v to equal %+v", actuals, expecteds)
	}

	if err := txa.Commit(); err != nil {
		t.Fatal(err)
	}

	txb.Rollback()

	txc, err := rebecca.Begin()
	if err != nil {
		t.Fatal(err)
	}

	actual = &Post{}
	if err := txc.Get(pa.ID, actual); err != nil {
		t.Fatal(err)
	}

	if !actual.Equal(pa) {
		t.Errorf("Expected %+v to equal %+v", actual, pa)
	}

	expecteds = []Post{}
	actuals = []Post{}
	if err := txc.Where(&actuals, "title = $1", "Super Post"); err != nil {
		t.Fatal(err)
	}

	if !equalPosts(actuals, expecteds) {
		t.Errorf("Expected %+v to equal %+v", actuals, expecteds)
	}

	expected := pa2
	actual = &Post{}
	if err := txc.First(actual, "title = $1", "Hello Blog"); err != nil {
		t.Fatal(err)
	}

	if !actual.Equal(expected) {
		t.Errorf("Expected %+v to equal %+v", actual, expected)
	}

	txd, err := rebecca.Begin()
	if err != nil {
		t.Fatal(err)
	}

	if err := txd.Remove(pa); err != nil {
		t.Fatal(err)
	}

	expecteds = []Post{*pa2}
	actuals = []Post{}
	if err := txd.All(&actuals); err != nil {
		t.Fatal(err)
	}

	if !equalPosts(actuals, expecteds) {
		t.Errorf("Expected %+v to equal %+v", actuals, expecteds)
	}

	expecteds = []Post{*pa, *pa2}
	actuals = []Post{}
	if err := txc.All(&actuals); err != nil {
		t.Fatal(err)
	}

	if !equalPosts(actuals, expecteds) {
		t.Errorf("Expected %+v to equal %+v", actuals, expecteds)
	}

	expecteds = []Post{*pa, *pa2}
	actuals = []Post{}
	if err := rebecca.All(&actuals); err != nil {
		t.Fatal(err)
	}

	if !equalPosts(actuals, expecteds) {
		t.Errorf("Expected %+v to equal %+v", actuals, expecteds)
	}

	if err := txd.Commit(); err != nil {
		t.Fatal(err)
	}

	expecteds = []Post{*pa2}
	actuals = []Post{}
	if err := rebecca.All(&actuals); err != nil {
		t.Fatal(err)
	}

	if !equalPosts(actuals, expecteds) {
		t.Errorf("Expected %+v to equal %+v", actuals, expecteds)
	}
}
Example #6
0
func TestFirst(t *testing.T) {
	d := fake.NewDriver()
	SetupDriver(d)

	d.RegisterWhere("age < $1", func(record []field.Field, args ...interface{}) (bool, error) {
		for _, f := range record {
			if f.DriverName == "age" {
				return f.Value.(int) < args[0].(int), nil
			}
		}

		return false, fmt.Errorf("record %+v does not have age field", record)
	})

	d.RegisterWhere("age >= $1", func(record []field.Field, args ...interface{}) (bool, error) {
		for _, f := range record {
			if f.DriverName == "age" {
				return f.Value.(int) >= args[0].(int), nil
			}
		}

		return false, fmt.Errorf("record %+v does not have age field", record)
	})

	type Person struct {
		ModelMetadata `tablename:"people"`

		ID   int    `rebecca:"id" rebecca_primary:"true"`
		Name string `rebecca:"name"`
		Age  int    `rebecca:"age"`
	}

	p1 := &Person{Name: "John", Age: 9}
	p2 := &Person{Name: "Sarah", Age: 27}
	p3 := &Person{Name: "James", Age: 11}
	p4 := &Person{Name: "Monika", Age: 12}
	people := []*Person{p1, p2, p3, p4}

	for _, p := range people {
		if err := Save(p); err != nil {
			t.Fatal(err)
		}
	}

	expected := p1
	actual := &Person{}
	if err := First(&actual, "age < $1", 12); err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("Expected %+v to equal to %+v", actual, expected)
	}

	expected = p2
	actual = &Person{}
	if err := First(&actual, "age >= $1", 12); err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("Expected %+v to equal to %+v", actual, expected)
	}
}