Exemple #1
0
import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/aodin/aspect"
)

// The sql dialect must implement the dialect interface
var _ aspect.Dialect = &Sqlite3{}

var things = aspect.Table("things",
	aspect.Column("name", aspect.String{Length: 32, NotNull: true}),
	aspect.Column(
		"created_at", Datetime{NotNull: true, Default: CurrentTimestamp},
	),
)

type thing struct {
	Name      string    `db:"name"`
	CreatedAt time.Time `db:"created_at,omitempty"`
}

var users = aspect.Table("users",
	aspect.Column("id", aspect.Integer{NotNull: true}),
	aspect.Column("name", aspect.String{Length: 32, NotNull: true}),
	aspect.Column("password", aspect.String{Length: 128, NotNull: true}),
	aspect.PrimaryKey("id"),
Exemple #2
0
	result, err = tx.Execute(users.Delete())
	require.Nil(t, err)

	rowsAffected, err = result.RowsAffected()
	assert.Nil(t, err)
	assert.EqualValues(t, 3, rowsAffected)
}

type u1 struct {
	ID   int64  `db:"id,omitempty"`
	Name string `db:"name"`
}

var u1s = aspect.Table("u1s",
	aspect.Column("id", Serial{PrimaryKey: true, NotNull: true}),
	aspect.Column("name", aspect.String{NotNull: true}),
)

func TestSelect_Omitempty(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
	conn, tx := dbtest.WithConfig(t, "./db.json")

	// Perform all tests in a transaction and always rollback
	defer conn.Close()
	defer tx.Rollback()

	_, err := tx.Execute(u1s.Create())
	require.Nil(t, err)
Exemple #3
0
package postgis

import (
	"testing"

	"github.com/aodin/aspect"
	"github.com/aodin/aspect/postgres"
)

// TODO Schemas should live in one file

var shapes = aspect.Table("shapes",
	aspect.Column("pt", Geometry{Geom: Point{}}),
	aspect.Column("area", Geometry{Polygon{}, 4326}),
)

func TestLatLong(t *testing.T) {
	expect := aspect.NewTester(t, &postgres.PostGres{})

	// TODO parameterization?
	expect.SQL(
		`ST_SetSRID(ST_Point(-104.984722, 39.739167), 4326)::geography`,
		LatLong{39.739167, -104.984722},
	)
}

func TestWithin(t *testing.T) {
	expect := aspect.NewTester(t, &postgres.PostGres{})
	expect.SQL(
		`ST_Within(ST_Point(-104.984722, 39.739167), "shapes"."area")`,
		Within(shapes.C["area"], Point{-104.984722, 39.739167}),
Exemple #4
0
import (
	"time"

	"github.com/aodin/aspect"
)

type user struct {
	ID        int64     `db:"id"`
	Name      string    `db:"name"`
	Password  string    `db:"password"`
	IsActive  bool      `db:"is_active"`
	CreatedAt time.Time `db:"created_at,omitempty"`
}

var users = aspect.Table("users",
	aspect.Column("id", Serial{NotNull: true}),
	aspect.Column("name", aspect.String{Length: 32, NotNull: true}),
	aspect.Column("password", aspect.String{Length: 128}),
	aspect.Column("is_active", aspect.Boolean{Default: aspect.True}),
	aspect.Column("created_at", aspect.Timestamp{Default: Now}),
	aspect.PrimaryKey("id"),
)

type hasUUID struct {
	UUID string `db:"uuid,omitempty"`
	Name string `db:"name"`
}

var hasUUIDs = aspect.Table("has_uuids",
	aspect.Column("uuid", UUID{NotNull: true, Default: GenerateV4}),
	aspect.Column("name", aspect.String{Length: 32, NotNull: true}),