Example #1
0
func TestUpgrade(t *testing.T) {

	// Happy path...
	if err := db.Connect(host, port, schema, user, password); nil != err {
		t.Errorf("Unable to connect to database: %v", err)
	}
	db.Execute("DROP SCHEMA sample")
	if err := db.Execute("CREATE SCHEMA sample DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"); nil != err {
		t.Errorf("Unable to create database: %v", err)
	}
	if err := db.Connect(host, port, schema, user, password); nil != err {
		t.Errorf("Unable to reconnect to database: %v", err)
	}

	if err := Upgrade(); nil != err {
		t.Errorf("Upgrade failed: %v", err)
	}

	// Error path...
	db.Execute("DROP SCHEMA sample")
	if err := db.Execute("CREATE SCHEMA sample DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"); nil != err {
		t.Errorf("Unable to create database: %v", err)
	}
	if err := db.Connect(host, port, schema, user, password); nil != err {
		t.Errorf("Unable to reconnect to database: %v", err)
	}

	if err := db.Execute(usersql.CreateEncryptionFunction); nil != err {
		t.Errorf("Unable to add pre-existing function: %v", err)
	}

	if err := Upgrade(); nil == err {
		t.Error("Upgrade succeeded when it should have failed")
	}

	// Restore database...
	db.Execute("DROP SCHEMA sample")
	if err := db.Execute("CREATE SCHEMA sample DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"); nil != err {
		t.Errorf("Unable to create database: %v", err)
	}
}
Example #2
0
func TestAll(t *testing.T) {
	settings.Get.Storage.Folder = "testfolder/"

	// Setup for testing.
	if err := db.Connect(host, port, schema, user, password); nil != err {
		t.Errorf("Failed to connect to database with: %v", err)
	}
	if err := upgrade.All(); nil != err {
		t.Errorf("Database failed to upgrade with: %v", err)
	}

	testPut(t)
	testGet(t)
	testDelete(t)

	if err := os.RemoveAll("testfolder/"); nil != err {
		t.Errorf("Failed to delete folder: %v", err)
	}
}
Example #3
0
// WithSettings run the requested operation.
func WithSettings(
	settingsFile string,
	operation func(...string) error,
	args ...string,
) error {

	// Parse settings.
	if err := settings.Parse(settingsFile); nil != err {
		return err
	}

	// Connect to database.
	if err := db.Connect(
		settings.Get.Database.Host,
		settings.Get.Database.Port,
		settings.Get.Database.Name,
		settings.Get.Database.User,
		settings.Get.Database.Password,
	); nil != err {
		return err
	}

	return operation(args...)
}