Exemple #1
0
func NewDatabase(sqlDB *sql.DB, config Config) DatabaseInterface {
	database := db.NewDatabase(sqlDB, db.Config{
		DefaultTemplatePath: config.DefaultTemplatePath,
	})
	Setup(database)

	return database
}
func fetchDatabases() (*db.DB, *gobble.DB) {
	env := application.NewEnvironment()
	sqlDB, err := sql.Open("mysql", env.DatabaseURL)
	if err != nil {
		ginkgo.Fail(err.Error(), 1)
	}

	database := db.NewDatabase(sqlDB, db.Config{DefaultTemplatePath: path.Join(env.RootPath, "templates", "default.json")})
	gobbleDB := gobble.NewDatabase(sqlDB)

	return database, gobbleDB
}
	"github.com/cloudfoundry-incubator/notifications/v2/models"
	"github.com/nu7hatch/gouuid"

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

var _ = Describe("TemplatesRepo", func() {
	var (
		repo          models.TemplatesRepository
		conn          db.ConnectionInterface
		guidGenerator *mocks.GUIDGenerator
	)

	BeforeEach(func() {
		database := db.NewDatabase(sqlDB, db.Config{})
		helpers.TruncateTables(database)

		guid1 := uuid.UUID([16]byte{0xDE, 0xAD, 0xBE, 0xEF, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55})
		guid2 := uuid.UUID([16]byte{0xDE, 0xAD, 0xBE, 0xEF, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11, 0x22, 0x33, 0x44, 0x56})
		guidGenerator = mocks.NewGUIDGenerator()
		guidGenerator.GenerateCall.Returns.GUIDs = []*uuid.UUID{&guid1, &guid2}

		repo = models.NewTemplatesRepository(guidGenerator.Generate)
		conn = database.Connection()
	})

	Describe("Insert", func() {
		It("returns the data", func() {
			createdTemplate, err := repo.Insert(conn, models.Template{
				Name:     "some-template",
	"github.com/cloudfoundry-incubator/notifications/v2/models"

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

var _ = Describe("Messages Repository", func() {
	var (
		repo  models.MessagesRepository
		conn  *db.Connection
		clock *mocks.Clock
	)

	BeforeEach(func() {
		database := models.NewDatabase(sqlDB, models.Config{})
		helpers.TruncateTables(db.NewDatabase(sqlDB, db.Config{}))
		conn = database.Connection().(*db.Connection)
		conn.AddTableWithName(models.Message{}, "messages")
		clock = mocks.NewClock()

		repo = models.NewMessagesRepository(clock)
	})

	Describe("CountByStatus", func() {
		BeforeEach(func() {
			err := conn.Insert(&models.Message{
				ID:         "message-id-123",
				CampaignID: "some-campaign-id",
				Status:     postal.StatusDelivered,
			})
			Expect(err).NotTo(HaveOccurred())
	. "github.com/onsi/gomega"
)

var _ = Describe("DatabaseMigrator", func() {
	var (
		database            *db.DB
		connection          *db.Connection
		dbMigrator          models.DatabaseMigrator
		defaultTemplatePath string
	)

	BeforeEach(func() {
		env := application.NewEnvironment()
		defaultTemplatePath = env.RootPath + "/templates/default.json"
		database = db.NewDatabase(sqlDB, db.Config{
			DefaultTemplatePath: defaultTemplatePath,
		})
		helpers.TruncateTables(database)
		connection = database.Connection().(*db.Connection)
		dbMigrator = models.DatabaseMigrator{}
	})

	Describe("migrating the database", func() {
		It("has the correct tables", func() {
			err := connection.Db.Ping()
			Expect(err).To(BeNil())

			rows, err := connection.Db.Query("SHOW TABLES")
			Expect(err).To(BeNil())

			tables := []string{}