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

var _ = Describe("Updater", func() {
	Describe("Update", func() {
		var (
			conn          *mocks.Connection
			database      *mocks.Database
			templatesRepo *mocks.TemplatesRepo
			updater       services.TemplateUpdater
		)

		BeforeEach(func() {
			conn = mocks.NewConnection()
			database = mocks.NewDatabase()
			database.ConnectionCall.Returns.Connection = conn
			templatesRepo = mocks.NewTemplatesRepo()

			updater = services.NewTemplateUpdater(templatesRepo)
		})

		It("Inserts templates into the templates repo", func() {
			err := updater.Update(database, "my-awesome-id", models.Template{
				Name: "gobble template",
				Text: "gobble",
				HTML: "<p>gobble</p>",
			})
			Expect(err).ToNot(HaveOccurred())
			_, err = repo.Insert(conn, models.Template{
				Name:     "some-template",
				ClientID: "some-client-id",
			})
			Expect(err).NotTo(HaveOccurred())

			templates, err := repo.List(conn, "some-client-id")
			Expect(err).NotTo(HaveOccurred())
			Expect(templates).To(HaveLen(1))
			Expect(templates[0].ClientID).To(Equal("some-client-id"))
			Expect(templates[0].Name).To(Equal("some-template"))
		})

		Context("failure cases", func() {
			It("returns not found error if it happens", func() {
				connection := mocks.NewConnection()
				connection.SelectCall.Returns.Error = errors.New("an error")
				_, err := repo.List(connection, "client-id")
				Expect(err).To(MatchError(errors.New("an error")))
			})
		})
	})

	Describe("Get", func() {
		It("fetches the template given a template_id", func() {
			createdTemplate, err := repo.Insert(conn, models.Template{
				Name:     "some-template",
				ClientID: "some-client-id",
			})
			Expect(err).NotTo(HaveOccurred())
			})
			Expect(err).NotTo(HaveOccurred())
		})

		It("should return the counts of each message status", func() {
			messageCounts, err := repo.CountByStatus(conn, "some-campaign-id")
			Expect(err).NotTo(HaveOccurred())
			Expect(messageCounts.Retry).To(Equal(1))
			Expect(messageCounts.Failed).To(Equal(1))
			Expect(messageCounts.Delivered).To(Equal(2))
			Expect(messageCounts.Queued).To(Equal(1))
		})

		Context("when an error occurs", func() {
			It("should return an error", func() {
				connection := mocks.NewConnection()
				connection.SelectCall.Returns.Error = errors.New("some connection error")

				_, err := repo.CountByStatus(connection, "some-campaign-id")
				Expect(err).To(MatchError(errors.New("some connection error")))
			})
		})
	})

	Describe("MostRecentlyUpatedByCampaignID", func() {
		var anotherUpdatedAt time.Time
		BeforeEach(func() {
			var err error

			updatedAt, err := time.Parse(time.RFC3339, "2014-12-31T12:05:05+07:00")
			Expect(err).NotTo(HaveOccurred())
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Campaign Status Updater", func() {
	var (
		campaignStatusUpdater postal.CampaignStatusUpdater
		messagesRepo          *mocks.MessagesRepository
		campaignsRepo         *mocks.CampaignsRepository
		pollInterval          time.Duration
		database              *mocks.Database
	)

	BeforeEach(func() {
		database = mocks.NewDatabase()
		database.ConnectionCall.Returns.Connection = mocks.NewConnection()
		messagesRepo = mocks.NewMessagesRepository()
		campaignsRepo = mocks.NewCampaignsRepository()
		pollInterval = 1 * time.Millisecond

		campaignStatusUpdater = postal.NewCampaignStatusUpdater(database.Connection(),
			messagesRepo,
			campaignsRepo,
			pollInterval)
	})

	Describe("Run", func() {
		It("starts the worker to gather campaign ids", func() {
			campaignStatusUpdater.Run()

			Eventually(func() int {
var _ = Describe("CampaignTypesCollection", func() {
	var (
		campaignTypesCollection     collections.CampaignTypesCollection
		fakeCampaignTypesRepository *mocks.CampaignTypesRepository
		fakeSendersRepository       *mocks.SendersRepository
		fakeTemplatesRepository     *mocks.TemplatesRepository
		fakeDatabaseConnection      *mocks.Connection
	)

	BeforeEach(func() {
		fakeCampaignTypesRepository = mocks.NewCampaignTypesRepository()
		fakeSendersRepository = mocks.NewSendersRepository()
		fakeTemplatesRepository = mocks.NewTemplatesRepository()

		campaignTypesCollection = collections.NewCampaignTypesCollection(fakeCampaignTypesRepository, fakeSendersRepository, fakeTemplatesRepository)
		fakeDatabaseConnection = mocks.NewConnection()
	})

	Describe("Delete", func() {
		BeforeEach(func() {
			fakeCampaignTypesRepository.GetCall.Returns.CampaignType = models.CampaignType{
				ID:          "some-campaign-id",
				Name:        "My cool campaign type",
				Description: "description",
				Critical:    false,
				TemplateID:  "",
				SenderID:    "mysender",
			}

			fakeSendersRepository.GetCall.Returns.Sender = models.Sender{
				ID:       "mysender",