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) } }
func ExampleGet() { type Person struct { // ... } person := &Person{} if err := rebecca.Get(25, person); err != nil { panic(err) } // At this point person contains record with primary key equal to 25. fmt.Print(person) }
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) } }
func ExampleRemove() { type Person struct { // ... } // First lets find person with primary key = 25 person := &Person{} if err := rebecca.Get(25, person); err != nil { panic(err) } // And then, lets remove it if err := rebecca.Remove(person); err != nil { panic(err) } // At this point person with primary key = 25 was removed from database. }
func TestTransact(t *testing.T) { now := time.Now() var p *Post examples := map[string]struct { handler func(tx *rebecca.Transaction) error verify func(err error) error }{ "when handler succeeds": { handler: func(tx *rebecca.Transaction) error { p = &Post{Title: "Hello", Content: "World", CreatedAt: now} return tx.Save(p) }, verify: func(err error) error { if err != nil { return fmt.Errorf("Expected handler to not fail, but got: %s", err) } actual := &Post{} if err := rebecca.Get(actual, p.ID); err != nil { return fmt.Errorf("Expected rebecca.Get(actual, %d) to not fail, but got: %s", p.ID, err) } if !actual.Equal(p) { return fmt.Errorf("Expected %#v to equal %#v", actual, p) } return nil }, }, "when handler fails": { handler: func(tx *rebecca.Transaction) error { p = &Post{Title: "Hello", Content: "World", CreatedAt: now} tx.Save(p) return errors.New("I have failed") }, verify: func(err error) error { if err == nil { return errors.New("Expected handler to fail, but got nil") } if err.Error() != "I have failed" { return fmt.Errorf("Expected error to be 'I have failed', but got: '%s'", err) } actual := &Post{} if err := rebecca.Get(actual, p.ID); err == nil { return fmt.Errorf("Expected rebecca.Get(actual, %d) to fail, but got nil", p.ID) } return nil }, }, "when handler panics": { handler: func(tx *rebecca.Transaction) error { p = &Post{Title: "Hello", Content: "World", CreatedAt: now} tx.Save(p) panic("I have a panic!") }, verify: func(err error) error { if err == nil { return errors.New("Expected handler to fail, but got nil") } if err.Error() != "I have a panic! (recovered)" { return fmt.Errorf("Expected error to be 'I have a panic! (recovered)', but got: '%s'", err) } actual := &Post{} if err := rebecca.Get(actual, p.ID); err == nil { return fmt.Errorf("Expected rebecca.Get(actual, %d) to fail, but got nil", p.ID) } return nil }, }, } for info, e := range examples { t.Log(info) p = nil if err := e.verify(rebecca.Transact(e.handler)); err != nil { t.Error(err) } } }