Example #1
0
func TestSaveUpdates(t *testing.T) {
	setup(t)

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

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

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

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

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

	p1 := &Post{Title: "Post 1", Content: "Content 1", CreatedAt: time.Now()}
	p2 := &Post{Title: "Post 2", Content: "Content 2", CreatedAt: time.Now()}
	p3 := &Post{Title: "Post 3", Content: "Content 3", CreatedAt: time.Now()}
	p4 := &Post{Title: "Post 4", Content: "Content 4", CreatedAt: time.Now()}
	posts := []*Post{p1, p2, p3, p4}

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

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

	if !equalPosts(actual, expected) {
		t.Errorf("Expected %+v to equal %+v", actual, expected)
	}
}
Example #3
0
func TestRemove(t *testing.T) {
	setup(t)

	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 := rebecca.Save(p); err != nil {
			t.Fatal(err)
		}
	}

	if err := rebecca.Remove(p3); err != nil {
		t.Fatal(err)
	}

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

	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("Expected %+v to equal %+v", actual, expected)
	}
}
Example #4
0
func TestFirst(t *testing.T) {
	setup(t)

	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 := rebecca.Save(p); err != nil {
			t.Fatal(err)
		}
	}

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

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

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

	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("Expected %+v to equal %+v", actual, expected)
	}
}
Example #5
0
func ExampleSave() {
	// Lets first define our model:
	type Person struct {
		// the table name is people
		rebecca.ModelMetadata `tablename:"people"`

		// ID is of type int, in database it is mapped to `id` and it is a primary
		// key.
		// Name is of type string, in database it is mapped to `name`.
		// Age is of type int, in database it is mapped to `age`.
		ID   int    `rebecca:"id" rebecca_primary:"true"`
		Name string `rebecca:"name"`
		Age  int    `rebecca:"age"`
	}

	// And now, lets create new record:
	person := &Person{Name: "John", Age: 28}
	// And finally, lets save it to the database
	if err := rebecca.Save(person); err != nil {
		panic(err)
	}

	// At this point, record was saved to database as a new record and its ID
	// field was updated accordingly.
	fmt.Print(person)

	// Now lets modify our record:
	person.Name = "John Smith"
	// And save it
	if err := rebecca.Save(person); err != nil {
		panic(err)
	}

	// At this point, original record was update with new data.
	fmt.Print(person)
}
Example #6
0
func TestOrder(t *testing.T) {
	setup(t)

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

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

	ctx := &rebecca.Context{Order: "age DESC"}
	expected := []Person{*p3, *p2, *p6, *p5, *p4, *p1}
	actual := []Person{}
	if err := ctx.All(&actual); err != nil {
		t.Fatal(err)
	}

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

	expected = []Person{*p6, *p5, *p4, *p1}
	actual = []Person{}
	if err := ctx.Where(&actual, "age <= $1", 21); err != nil {
		t.Fatal(err)
	}

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

	expectedOne := p5
	actualOne := &Person{}
	if err := ctx.First(actualOne, "age < $1", 21); err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(actualOne, expectedOne) {
		t.Errorf("Expected %+v to equal %+v", actualOne, expectedOne)
	}
}
Example #7
0
func TestSaveCreates(t *testing.T) {
	setup(t)

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

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

	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("Expected %+v to equal %+v", actual, expected)
	}
}
Example #8
0
func TestGroup(t *testing.T) {
	setup(t)

	p1 := &Person{Name: "John", Age: 9}
	p2 := &Person{Name: "Sarah", Age: 27}
	p3 := &Person{Name: "Bruce", Age: 27}
	p4 := &Person{Name: "James", Age: 11}
	p5 := &Person{Name: "Monika", Age: 11}
	p6 := &Person{Name: "Peter", Age: 21}
	p7 := &Person{Name: "Brad", Age: 11}
	people := []*Person{p1, p2, p3, p4, p5, p6, p7}

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

	type PersonByAge struct {
		rebecca.ModelMetadata `tablename:"people"`

		Age   int `rebecca:"age" rebecca_primary:"true"`
		Count int `rebecca:"count(distinct(id))"`
	}

	ctx := rebecca.Context{Group: "age"}
	expected := []PersonByAge{
		{Age: 9, Count: 1},
		{Age: 11, Count: 3},
		{Age: 21, Count: 1},
		{Age: 27, Count: 2},
	}
	actual := []PersonByAge{}
	if err := ctx.All(&actual); err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("Expected %+v to equal %+v", actual, expected)
	}
}
Example #9
0
func TestSkip(t *testing.T) {
	setup(t)

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

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

	examples := map[string]struct {
		ctx *rebecca.Context
	}{
		"when using Skip option it works": {
			ctx: &rebecca.Context{Skip: 2},
		},

		"when using Offset option it should be the same": {
			ctx: &rebecca.Context{Offset: 2},
		},
	}

	for _, e := range examples {
		ctx := e.ctx

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

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

		expected = []Person{*p5, *p6}
		actual = []Person{}
		if err := ctx.Where(&actual, "age > $1", 11); err != nil {
			t.Fatal(err)
		}

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

		expectedOne := p4
		actualOne := &Person{}
		if err := ctx.First(&actualOne, "age > $1", 10); err != nil {
			t.Fatal(err)
		}

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