<commit_before>package collections_test

import (
	"errors"

	"github.com/cloudfoundry-incubator/notifications/collections"
	"github.com/cloudfoundry-incubator/notifications/fakes"
	"github.com/cloudfoundry-incubator/notifications/models"

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

var _ = Describe("CampaignTypesCollection", func() {
	var (
		campaignTypesCollection     collections.CampaignTypesCollection
		fakeCampaignTypesRepository *fakes.CampaignTypesRepository
		fakeSendersRepository       *fakes.SendersRepository
		fakeDatabaseConnection      *fakes.Connection
	)

	BeforeEach(func() {
		fakeCampaignTypesRepository = fakes.NewCampaignTypesRepository()
		fakeSendersRepository = fakes.NewSendersRepository()

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

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

			fakeSendersRepository.GetCall.ReturnSender = models.Sender{
				ID:       "mysender",
				Name:     "some-name",
				ClientID: "some-client-random-id",
			}
		})

		Context("when the clientID and senderID are valid", func() {
			It("deletes the given campaign", func() {
				Expect(campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "mysender", "some-client-random-id")).To(Succeed())

				Expect(fakeSendersRepository.GetCall.Conn).To(Equal(fakeDatabaseConnection))
				Expect(fakeSendersRepository.GetCall.SenderID).To(Equal("mysender"))

				Expect(fakeCampaignTypesRepository.GetCall.Connection).To(Equal(fakeDatabaseConnection))
				Expect(fakeCampaignTypesRepository.GetCall.CampaignTypeID).To(Equal("some-campaign-id"))

				Expect(fakeCampaignTypesRepository.DeleteCall.CampaignType.ID).To(Equal("some-campaign-id"))
				Expect(fakeCampaignTypesRepository.DeleteCall.Connection).To(Equal(fakeDatabaseConnection))
			})
		})

		Context("when an error occurs", func() {
			Context("when the sender does not match the client ID", func() {
				It("returns an error", func() {
					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "mysender", "some-other-client-id")
					Expect(err).To(MatchError(collections.NewNotFoundError("sender mysender not found")))
				})
			})

			Context("when the campaign type does not belong to the sender", func() {
				It("returns an error", func() {
					fakeSendersRepository.GetCall.ReturnSender = models.Sender{
						ID:       "othersender",
						Name:     "some-name",
						ClientID: "some-other-client-id",
					}

					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "othersender", "some-other-client-id")
					Expect(err).To(MatchError(collections.NewNotFoundError("campaign type some-campaign-id not found")))
				})
			})

			Context("when the campaign type does not exist", func() {
				It("returns an error", func() {
					originalError := models.RecordNotFoundError("record not found")
					fakeCampaignTypesRepository.GetReturn.CampaignType = models.CampaignType{}
					fakeCampaignTypesRepository.GetReturn.Err = originalError

					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-bad-campaign-id", "mysender", "some-client-random-id")
					Expect(err).To(MatchError(collections.NewNotFoundErrorWithOriginalError("campaign type some-bad-campaign-id not found", originalError)))
				})
			})

			Context("when the sender returns an error", func() {
				It("returns an error", func() {
					originalError := models.RecordNotFoundError("record not found")
					fakeSendersRepository.GetCall.ReturnSender = models.Sender{}
					fakeSendersRepository.GetCall.Err = originalError
					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "not-found", "")
					Expect(err).To(MatchError(collections.NewNotFoundErrorWithOriginalError("sender not-found not found", originalError)))
				})
			})

			Context("when the database connection returns some other error while deleting", func() {
				It("returns the error", func() {
					fakeCampaignTypesRepository.DeleteCall.Err = errors.New("indeletable")

					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "mysender", "some-client-random-id")
					Expect(err).To(MatchError("indeletable"))
				})
			})

			Context("when the database connection returns some other error while getting sender", func() {
				It("returns the error", func() {
					fakeSendersRepository.GetCall.ReturnSender = models.Sender{}
					fakeSendersRepository.GetCall.Err = errors.New("nope")

					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "not-found", "")
					Expect(err).To(MatchError(collections.PersistenceError{errors.New("nope")}))
				})
			})

			Context("when the database connection returns some other error while getting campaign type", func() {
				It("returns the error", func() {
					fakeCampaignTypesRepository.GetReturn.Err = errors.New("undeletable")

					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "mysender", "some-client-random-id")
					Expect(err).To(MatchError(collections.PersistenceError{errors.New("undeletable")}))
				})
			})
		})
	})<commit_msg>Key fields that were unkeyed<commit_after>package collections_test

import (
	"errors"

	"github.com/cloudfoundry-incubator/notifications/collections"
	"github.com/cloudfoundry-incubator/notifications/fakes"
	"github.com/cloudfoundry-incubator/notifications/models"

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

var _ = Describe("CampaignTypesCollection", func() {
	var (
		campaignTypesCollection     collections.CampaignTypesCollection
		fakeCampaignTypesRepository *fakes.CampaignTypesRepository
		fakeSendersRepository       *fakes.SendersRepository
		fakeDatabaseConnection      *fakes.Connection
	)

	BeforeEach(func() {
		fakeCampaignTypesRepository = fakes.NewCampaignTypesRepository()
		fakeSendersRepository = fakes.NewSendersRepository()

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

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

			fakeSendersRepository.GetCall.ReturnSender = models.Sender{
				ID:       "mysender",
				Name:     "some-name",
				ClientID: "some-client-random-id",
			}
		})

		Context("when the clientID and senderID are valid", func() {
			It("deletes the given campaign", func() {
				Expect(campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "mysender", "some-client-random-id")).To(Succeed())

				Expect(fakeSendersRepository.GetCall.Conn).To(Equal(fakeDatabaseConnection))
				Expect(fakeSendersRepository.GetCall.SenderID).To(Equal("mysender"))

				Expect(fakeCampaignTypesRepository.GetCall.Connection).To(Equal(fakeDatabaseConnection))
				Expect(fakeCampaignTypesRepository.GetCall.CampaignTypeID).To(Equal("some-campaign-id"))

				Expect(fakeCampaignTypesRepository.DeleteCall.CampaignType.ID).To(Equal("some-campaign-id"))
				Expect(fakeCampaignTypesRepository.DeleteCall.Connection).To(Equal(fakeDatabaseConnection))
			})
		})

		Context("when an error occurs", func() {
			Context("when the sender does not match the client ID", func() {
				It("returns an error", func() {
					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "mysender", "some-other-client-id")
					Expect(err).To(MatchError(collections.NewNotFoundError("sender mysender not found")))
				})
			})

			Context("when the campaign type does not belong to the sender", func() {
				It("returns an error", func() {
					fakeSendersRepository.GetCall.ReturnSender = models.Sender{
						ID:       "othersender",
						Name:     "some-name",
						ClientID: "some-other-client-id",
					}

					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "othersender", "some-other-client-id")
					Expect(err).To(MatchError(collections.NewNotFoundError("campaign type some-campaign-id not found")))
				})
			})

			Context("when the campaign type does not exist", func() {
				It("returns an error", func() {
					originalError := models.RecordNotFoundError("record not found")
					fakeCampaignTypesRepository.GetReturn.CampaignType = models.CampaignType{}
					fakeCampaignTypesRepository.GetReturn.Err = originalError

					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-bad-campaign-id", "mysender", "some-client-random-id")
					Expect(err).To(MatchError(collections.NewNotFoundErrorWithOriginalError("campaign type some-bad-campaign-id not found", originalError)))
				})
			})

			Context("when the sender returns an error", func() {
				It("returns an error", func() {
					originalError := models.RecordNotFoundError("record not found")
					fakeSendersRepository.GetCall.ReturnSender = models.Sender{}
					fakeSendersRepository.GetCall.Err = originalError
					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "not-found", "")
					Expect(err).To(MatchError(collections.NewNotFoundErrorWithOriginalError("sender not-found not found", originalError)))
				})
			})

			Context("when the database connection returns some other error while deleting", func() {
				It("returns the error", func() {
					fakeCampaignTypesRepository.DeleteCall.Err = errors.New("indeletable")

					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "mysender", "some-client-random-id")
					Expect(err).To(MatchError("indeletable"))
				})
			})

			Context("when the database connection returns some other error while getting sender", func() {
				It("returns the error", func() {
					fakeSendersRepository.GetCall.ReturnSender = models.Sender{}
					fakeSendersRepository.GetCall.Err = errors.New("nope")

					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "not-found", "")
					Expect(err).To(MatchError(collections.PersistenceError{Err: errors.New("nope")}))
				})
			})

			Context("when the database connection returns some other error while getting campaign type", func() {
				It("returns the error", func() {
					fakeCampaignTypesRepository.GetReturn.Err = errors.New("undeletable")

					err := campaignTypesCollection.Delete(fakeDatabaseConnection, "some-campaign-id", "mysender", "some-client-random-id")
					Expect(err).To(MatchError(collections.PersistenceError{Err: errors.New("undeletable")}))
				})
			})
		})
	})
{'fraction_non_alphanumeric': 0.12611637347767254, 'fraction_numerical': 0.0, 'mean_word_length': 4.717380092831356, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7467102', 'n_tokens_mistral': 3713, 'n_tokens_neox': 3106, 'n_words': 554}