Beispiel #1
0
package models_test

import (
	"github.com/cloudfoundry-incubator/notifications/v1/models"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Kind", func() {
	var kind models.Kind

	Describe("TemplateToUse", func() {
		Context("when the template is set", func() {
			BeforeEach(func() {
				kind.TemplateID = "template-id"
			})

			It("returns the template value", func() {
				Expect(kind.TemplateToUse()).To(Equal("template-id"))
			})
		})

		Context("when the template is not set", func() {
			BeforeEach(func() {
				kind.TemplateID = ""
			})

			It("returns the default template value", func() {
				Expect(kind.TemplateToUse()).To(Equal(models.DefaultTemplateID))
			})
			})

			Context("on updating the client", func() {
				It("Returns the error", func() {
					clientsRepo.UpdateCall.Returns.Error = errors.New("database fail")

					err := assigner.AssignToClient(database, "my-client", "my-template")
					Expect(err).To(HaveOccurred())
				})
			})

		})
	})

	Describe("AssignToNotification", func() {
		var kind models.Kind

		BeforeEach(func() {
			clientsRepo.FindCall.Returns.Client = models.Client{
				ID: "my-client",
			}

			_, err := templatesRepo.Create(conn, models.Template{
				ID: "default",
			})
			Expect(err).NotTo(HaveOccurred())

			kind = models.Kind{
				ID:       "my-kind",
				ClientID: "my-client",
			}
	)

	BeforeEach(func() {

		repo = models.NewKindsRepo()
		database := db.NewDatabase(sqlDB, db.Config{})
		helpers.TruncateTables(database)
		conn = database.Connection().(*db.Connection)
	})

	Describe("Update", func() {
		Context("when the template id is meant to be set", func() {
			It("updates the record in the database", func() {
				kind := models.Kind{
					ID:         "my-kind",
					ClientID:   "my-client",
					TemplateID: "my-template",
				}

				kind, err := repo.Upsert(conn, kind)
				if err != nil {
					panic(err)
				}

				primary := kind.Primary
				createdAt := kind.CreatedAt

				kind.Description = "My Kind"
				kind.Critical = true
				kind.Primary = 42069
				kind.TemplateID = "new-template"