コード例 #1
0
func TestChannelAPI(t *testing.T) {
	conf := getConfig()

	Convey("Given a clean database and an API instance", t, func() {
		db, err := OpenDatabase(conf.PostgresDSN)
		So(err, ShouldBeNil)
		mustResetDB(db)

		ctx := Context{
			DB: db,
		}
		api := NewChannelAPI(ctx)

		Convey("Given a channel-list in the database", func() {
			cl := models.ChannelList{
				Name: "test set",
			}
			So(createChannelList(db, &cl), ShouldBeNil)

			c1 := models.Channel{
				ChannelListID: cl.ID,
				Channel:       3,
				Frequency:     868100000,
			}
			c2 := models.Channel{
				ChannelListID: cl.ID,
				Channel:       5,
				Frequency:     868200000,
			}

			Convey("When calling Create", func() {
				So(api.Create(c1, &c1.ID), ShouldBeNil)

				Convey("Then the channel has been created", func() {
					var c models.Channel
					So(api.Get(c1.ID, &c), ShouldBeNil)
					So(c, ShouldResemble, c1)

					Convey("Then the channel can be updated", func() {
						c1.Frequency = 868300000
						So(api.Update(c1, &c1.ID), ShouldBeNil)
						So(api.Get(c1.ID, &c), ShouldBeNil)
						So(c, ShouldResemble, c1)
					})

					Convey("Then the channel can be deleted", func() {
						So(api.Delete(c1.ID, &c1.ID), ShouldBeNil)
						So(api.Get(c1.ID, &c), ShouldNotBeNil)
					})

					Convey("When creating a second channel", func() {
						So(api.Create(c2, &c2.ID), ShouldBeNil)

						Convey("Then GetForChannelList returns two channels", func() {
							var channels []models.Channel
							So(api.GetForChannelList(cl.ID, &channels), ShouldBeNil)
							So(channels, ShouldHaveLength, 2)
						})
					})
				})
			})
		})
	})
}