func (c Climb) Create(db *sqlx.DB, m *climb.Model) error { if m.RockID == 0 { r := Rock{} rm := rock.Model{} err := r.Create(db, &rm) if err != nil { log.Fatal(err) } m.RockID = rm.ID } stmt, err := db.PrepareNamed(`INSERT INTO climbs (rock_id, name, rating, description) VALUES (:rock_id, :name, :rating, :description) RETURNING *`) if err != nil { log.Fatal(err) } err = stmt.Get(m, m) return err }
request.Json = fmt.Sprintf(`{"rockId":%v,"name": "Zion", "rating": 3, "description": "Awesome!"}`, rm.ID) request.Path = fmt.Sprintf("/rocks/%v/climbs", rm.ID) request.Expected = fmt.Sprintf(`{"id":\d+,"rockId":%v,"name":"Zion","rating":3,"description":"Awesome!","createdAt":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","updatedAt":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z"}`, rm.ID) }) testCreate(&request) }) Describe("Update", func() { rm := rock.Model{} BeforeEach(func() { err := rockFactory.Create(db, &rm) Expect(err).To(BeNil()) m.RockID = rm.ID err = climbFactory.Create(db, &m) Expect(err).To(BeNil()) request.Json = `{"name": "Joe's Valley", "rating": 5, "description": "Another description!"}` request.Path = fmt.Sprintf("/climbs/%v", m.ID) request.Expected = fmt.Sprintf(`{"id":%v,"rockId":\d+,"name":"Joe's Valley","rating":5,"description":"Another description!","createdAt":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","updatedAt":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z"}`, m.ID) }) testUpdate(&request) }) Describe("Destroy", func() { rm := rock.Model{}
Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(repository.NoRows{Msg: "sql: no rows in result set"})) }) }) }) Describe("Create", func() { Context("When no duplicate", func() { BeforeEach(func() { err := rockFactory.Create(db, &rm) if err != nil { Fail(err.Error()) } m.RockID = rm.ID }) It("returns struct", func() { rm, err := climbRepo.Create(reader) received := rm.(climb.Model) Expect(err).To(BeNil()) Expect(received.ID).To(BeNumerically(">", 0)) Expect(received.Name).To(Equal(m.Name)) Expect(received.Rating).To(Equal(m.Rating)) Expect(received.Description).To(Equal(m.Description)) }) }) })