コード例 #1
0
ファイル: postgres_test.go プロジェクト: shyrobbiani/aspect
func TestPostGres(t *testing.T) {
	// Connect to the database specified in the test db.json config
	// Default to the Travis CI settings if no file is found
	conf, err := config.ParseTestConfig("./db.json")
	if err != nil {
		t.Fatalf(
			"postgres: failed to parse test configuration, test aborted: %s",
			err,
		)
	}

	db, err := aspect.Connect(conf.Driver, conf.Credentials())
	require.Nil(t, err)
	defer db.Close()

	_, err = db.Execute(users.Drop().IfExists())
	require.Nil(t, err)

	// Perform test twice
	for i := 0; i < 2; i++ {
		tx, err := db.Begin()
		require.Nil(t, err)

		// Start a fake transaction that will implement the connection passed
		// to resources / controllers
		fakeTX := aspect.FakeTx(tx)

		innerTX, err := fakeTX.Begin()
		require.Nil(t, err)

		_, err = innerTX.Execute(users.Create())
		require.Nil(t, err)
		innerTX.Commit()

		// Rollback the real transaction
		tx.Rollback()
	}
}
コード例 #2
0
ファイル: with.go プロジェクト: shyrobbiani/aspect
// WithConfig returns a database connection pool and transaction using the
// given config.Database path. It will fallback to a Travis CI spec if no
// file is found.
func WithConfig(t *testing.T, path string) (*aspect.DB, aspect.Transaction) {
	c, err := config.ParseTestConfig(path)
	require.Nil(t, err)
	return with(t, c)
}