Ejemplo n.º 1
0
func TestNewTableQuery(t *testing.T) {
	id, err := sql.NewOptions("name=id type=int(11) auto-increment=100 unique unsigned primary-key")
	assert.Nil(t, err)

	key, err := sql.NewOptions("varchar(30) required default='yolo' name=key")
	assert.Nil(t, err)

	bio, err := sql.NewOptions("text name=bio")
	assert.Nil(t, err)

	ignored, err := sql.NewOptions("-")
	assert.Nil(t, err)

	query := sql.NewTableQuery("user", []*sql.Options{id, key, bio, ignored}, false)
	lines := strings.Split(query, "\n")
	assert.Equal(t, len(lines), 6)
	assert.Equal(t, lines[0], "CREATE TABLE `user` (")
	assert.Equal(t, lines[1], "  `id` int(11) UNSIGNED UNIQUE AUTO_INCREMENT,")
	assert.Equal(t, lines[2], "  `key` varchar(30) NOT NULL DEFAULT 'yolo',")
	assert.Equal(t, lines[3], "  `bio` text,")
	assert.Equal(t, lines[4], "  PRIMARY KEY (`id`)")
	assert.Equal(t, lines[5], ") AUTO_INCREMENT=100;")

	query = sql.NewTableQuery("user", []*sql.Options{id, key, bio, ignored}, true)
	lines = strings.Split(query, "\n")
	assert.Equal(t, lines[0], "CREATE TABLE IF NOT EXISTS `user` (")
}
Ejemplo n.º 2
0
func TestCustomTypes(t *testing.T) {
	o, err := sql.NewOptions("text")
	assert.Nil(t, err)
	assert.Equal(t, o.Type, "text")
	assert.False(t, o.IsAutoIncrementing)

	o, err = sql.NewOptions("auto_increment type=bigint(16)")
	assert.Nil(t, err)
	assert.Equal(t, o.Type, "bigint")
	assert.Equal(t, o.Length, 16)
	assert.Equal(t, o.AutoIncrement, 1)
	assert.True(t, o.IsAutoIncrementing)
}
Ejemplo n.º 3
0
func TestNewFieldQuery(t *testing.T) {
	f, err := sql.NewOptions("varchar(30) required default='yolo' name=key")
	assert.Nil(t, err)
	query := sql.NewFieldQuery(f)
	assert.Equal(t, query, "  `key` varchar(30) NOT NULL DEFAULT 'yolo'")

	f, err = sql.NewOptions("name=id type=int(11) auto-increment unique unsigned")
	assert.Nil(t, err)
	query = sql.NewFieldQuery(f)
	assert.Equal(t, query, "  `id` int(11) UNSIGNED UNIQUE AUTO_INCREMENT")

	f, err = sql.NewOptions("text name=bio")
	assert.Nil(t, err)
	query = sql.NewFieldQuery(f)
	assert.Equal(t, query, "  `bio` text")
}
Ejemplo n.º 4
0
func TestDefaultValues(t *testing.T) {
	o, err := sql.NewOptions("int autoincrement")
	assert.Nil(t, err)
	assert.Equal(t, o.AutoIncrement, 1)
	assert.True(t, o.IsAutoIncrementing)
	assert.Equal(t, o.Type, "int")
	assert.Equal(t, o.Length, 11)
}
Ejemplo n.º 5
0
func TestAllOptions(t *testing.T) {
	o, err := sql.NewOptions("varchar(255) auto-increment=25 primary-key required default='yolo' name=id")
	assert.Nil(t, err)
	assert.Equal(t, o.Name, "id")
	assert.Equal(t, o.Type, "varchar")
	assert.Equal(t, o.Length, 255)
	assert.Equal(t, o.AutoIncrement, 25)
	assert.True(t, o.IsAutoIncrementing)
	assert.Equal(t, o.IsPrimaryKey, true)
	assert.Equal(t, o.IsRequired, true)
	assert.Equal(t, o.DefaultValue, "'yolo'")
}
Ejemplo n.º 6
0
func TestNewFieldQueries(t *testing.T) {
	id, err := sql.NewOptions("name=id type=int(11) auto-increment unique unsigned")
	assert.Nil(t, err)

	key, err := sql.NewOptions("varchar(30) required default='yolo' name=key")
	assert.Nil(t, err)

	bio, err := sql.NewOptions("text name=bio")
	assert.Nil(t, err)

	ignored, err := sql.NewOptions("-")
	assert.Nil(t, err)

	queries := sql.NewFieldQueries([]*sql.Options{id, key, bio, ignored})
	assert.Nil(t, err)

	lines := strings.Split(queries, "\n")
	assert.Equal(t, len(lines), 3)
	assert.Equal(t, lines[0], "  `id` int(11) UNSIGNED UNIQUE AUTO_INCREMENT,")
	assert.Equal(t, lines[1], "  `key` varchar(30) NOT NULL DEFAULT 'yolo',")
	assert.Equal(t, lines[2], "  `bio` text")
}
Ejemplo n.º 7
0
func (iteration *FieldIteration) SQLOptions() (*sql.Options, error) {
	result, err := sql.NewOptions(iteration.TypeField().Tag.Get("sql"))
	if err != nil {
		return nil, err
	}

	if len(result.Name) == 0 {
		result.Name = snakecase.SnakeCase(iteration.Name())
	}

	if len(result.Type) == 0 {
		sqlType, err := sql.MatchType(iteration.TypeField().Type.String())
		if err != nil {
			return nil, err
		}

		result.Type = sqlType
		result.Length = sql.Types[result.Type]
	}

	return result, nil
}
Ejemplo n.º 8
0
func TestFailing(t *testing.T) {
	_, err := sql.NewOptions("yolo")
	assert.NotNil(t, err)
}
Ejemplo n.º 9
0
func TestIgnoring(t *testing.T) {
	o, err := sql.NewOptions("-")
	assert.Nil(t, err)
	assert.Equal(t, o.Ignore, true)
}