示例#1
0
func with(t *testing.T, c config.Database) (*aspect.DB, aspect.Transaction) {
	// Connect to the database
	conn, err := aspect.Connect(c.Driver, c.Credentials())
	require.Nil(t, err)

	// Start a transaction
	tx, err := conn.Begin()
	require.Nil(t, err)
	return conn, tx
}
示例#2
0
func TestResultTypes(t *testing.T) {
	conn, err := aspect.Connect("sqlite3", ":memory:")
	require.Nil(t, err, "Failed to connect to in-memory sqlite3 instance")
	defer conn.Close()

	// Create the users table
	_, err = conn.Execute(users.Create())
	require.Nil(t, err, "Failed to create users table")

	admin := embedUser{
		id:       id{ID: 1},
		fullname: fullname{Name: "admin"},
		Password: "******",
	}
	_, err = conn.Execute(users.Insert().Values(admin))
	require.Nil(t, err, "Inserting a single user should not error")

	// Tagless destination
	var untagged tagless
	conn.MustQueryOne(
		aspect.Select(users.C["id"], users.C["name"]).Limit(1),
		&untagged,
	)
	assert.EqualValues(t, 1, untagged.ID)
	assert.Equal(t, "admin", untagged.Name)

	var untaggeds []tagless
	conn.MustQueryAll(
		aspect.Select(users.C["id"], users.C["name"]),
		&untaggeds,
	)
	require.Equal(t, 1, len(untaggeds))
	assert.EqualValues(t, 1, untaggeds[0].ID)
	assert.Equal(t, "admin", untaggeds[0].Name)

	// Tagless insert - number of columns must match numebr of exported fields
	noTag := fullTagless{
		ID:   2,
		Name: "tagless",
		// Password is a blank string
	}
	_, err = conn.Execute(users.Insert().Values(noTag))
	require.Nil(t, err, "Inserting a single tagless user should not error")

	// Embedded destination
	var embed embedUser
	conn.MustQueryOne(
		users.Select().Where(users.C["id"].Equals(1)).Limit(1),
		&embed,
	)
	assert.EqualValues(t, 1, embed.id.ID)
	assert.Equal(t, "admin", embed.fullname.Name)
	assert.Equal(t, "secret", embed.Password)
}
示例#3
0
func TestColumnTypes(t *testing.T) {
	conn, err := aspect.Connect("sqlite3", ":memory:")
	require.Nil(t, err, "Failed to connect to in-memory sqlite3 instance")
	defer conn.Close()

	// Create the things table which has a sqlite3.Datetime column type
	_, err = conn.Execute(things.Create())
	require.Nil(t, err, "Failed to create things table")

	// Sqlite3 only has per second resolution
	before := time.Now().Add(-time.Second)
	newThingy := thing{Name: "A Thing"}
	conn.MustExecute(things.Insert().Values(newThingy))
	after := time.Now().Add(time.Second)

	var thingy thing
	conn.MustQueryOne(things.Select(), &thingy)
	assert.Equal(t, "A Thing", thingy.Name)
	assert.True(t, thingy.CreatedAt.After(before))
	assert.True(t, thingy.CreatedAt.Before(after))
}
示例#4
0
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()
	}
}
示例#5
0
// Connect to an in-memory sqlite3 instance and execute some statements.
func TestConnect(t *testing.T) {
	assert := assert.New(t)

	conn, err := aspect.Connect("sqlite3", ":memory:")
	require.Nil(t, err, "Failed to connect to in-memory sqlite3 instance")
	defer conn.Close()

	// Create the users table
	_, err = conn.Execute(users.Create())
	require.Nil(t, err, "Failed to create users table")

	// Insert a user
	admin := user{
		ID:       1,
		Name:     "admin",
		Password: "******",
	}
	_, err = conn.Execute(users.Insert().Values(admin))
	require.Nil(t, err, "Inserting a single user should not error")

	// Insert multiple users
	clients := []user{
		user{ID: 2, Name: "client1"},
		user{ID: 3, Name: "client2"},
	}
	_, err = conn.Execute(users.Insert().Values(clients))
	require.Nil(t, err, "Inserting multiple users should not error")

	// Select a user - Query must be given a pointer
	var u user
	require.Nil(t, conn.QueryOne(users.Select(), &u))
	assert.Equal(admin.ID, u.ID)
	assert.Equal(admin.Name, u.Name)
	assert.Equal(admin.Password, u.Password)

	// Select an incomplete, embedded struct
	var embed embedded
	require.Nil(t, conn.QueryOne(users.Select(), &embed))
	assert.Equal(admin.ID, embed.id.ID)
	assert.Equal(admin.Name, embed.Name)

	// Select a wrapped struct
	var wrap wrapped
	require.Nil(t, conn.QueryOne(users.Select(), &wrap))
	assert.Equal(admin.ID, wrap.user.ID)
	assert.Equal(admin.Name, wrap.user.Name)
	assert.Equal(admin.Password, wrap.user.Password)
	assert.EqualValues(0, wrap.ID)

	// Select multiple users
	var us []user
	require.Nil(t, conn.QueryAll(users.Select().OrderBy(users.C["id"]), &us))
	require.Equal(t, 3, len(us))
	assert.Equal(admin.ID, us[0].ID)
	assert.Equal(admin.Name, us[0].Name)
	assert.Equal(admin.Password, us[0].Password)

	// Select multiple users with embedding
	var embeds []embedded
	require.Nil(t,
		conn.QueryAll(users.Select().OrderBy(users.C["id"]), &embeds),
	)
	require.Equal(t, 3, len(us))
	assert.Equal(admin.ID, embeds[0].id.ID)
	assert.Equal(admin.Name, embeds[0].Name)

	// Select multiple embedded users into a slice that is pre-populated
	embeds = []embedded{
		{Name: "a"},
		{Name: "b"},
		{Name: "c"},
	}
	require.Nil(t, conn.QueryAll(
		aspect.Select(users.C["id"]).OrderBy(users.C["id"]), &embeds,
	))
	require.Equal(t, 3, len(us))
	assert.EqualValues(1, embeds[0].id.ID)
	assert.Equal("a", embeds[0].Name)

	// Drop the table
	_, err = conn.Execute(users.Drop())
	assert.Nil(err, "Dropping the users table should not fail")
}