Column reference from sub queries.
This commit is contained in:
parent
e727fc3d4f
commit
e772768180
13 changed files with 507 additions and 470 deletions
|
|
@ -1,210 +1,14 @@
|
|||
// +build disabled
|
||||
|
||||
package sqlbuilder
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
import "testing"
|
||||
|
||||
gc "gopkg.in/check.v1"
|
||||
)
|
||||
func TestColumn(t *testing.T) {
|
||||
column := newColumn("col", "", nil)
|
||||
column.expressionInterfaceImpl.parent = &column
|
||||
|
||||
func Test(t *testing.T) {
|
||||
gc.TestingT(t)
|
||||
}
|
||||
|
||||
type ColumnSuite struct {
|
||||
}
|
||||
|
||||
var _ = gc.Suite(&ColumnSuite{})
|
||||
|
||||
//
|
||||
// tests for baseColumn and columns that extends baseColumn
|
||||
//
|
||||
|
||||
func (s *ColumnSuite) TestRealColumnName(c *gc.C) {
|
||||
col := IntColumn("col", Nullable)
|
||||
|
||||
c.Assert(col.Name(), gc.Equals, "col")
|
||||
}
|
||||
|
||||
func (s *ColumnSuite) TestRealColumnSerializeSqlForColumnList(c *gc.C) {
|
||||
col := IntColumn("col", Nullable)
|
||||
|
||||
// Without tableName name
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
err := col.SerializeSqlForColumnList(buf)
|
||||
c.Assert(err, gc.IsNil)
|
||||
|
||||
sql := buf.String()
|
||||
c.Assert(sql, gc.Equals, "col")
|
||||
|
||||
// With tableName name
|
||||
err = col.setTableName("foo")
|
||||
c.Assert(err, gc.IsNil)
|
||||
|
||||
buf = &bytes.Buffer{}
|
||||
|
||||
err = col.SerializeSqlForColumnList(buf)
|
||||
c.Assert(err, gc.IsNil)
|
||||
|
||||
sql = buf.String()
|
||||
c.Assert(sql, gc.Equals, "foo.col")
|
||||
}
|
||||
|
||||
func (s *ColumnSuite) TestRealColumnSerializeSql(c *gc.C) {
|
||||
col := IntColumn("col", Nullable)
|
||||
|
||||
// Without tableName name
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
err := col.SerializeSql(buf)
|
||||
c.Assert(err, gc.IsNil)
|
||||
|
||||
sql := buf.String()
|
||||
c.Assert(sql, gc.Equals, "col")
|
||||
|
||||
// With tableName name
|
||||
err = col.setTableName("foo")
|
||||
c.Assert(err, gc.IsNil)
|
||||
|
||||
buf = &bytes.Buffer{}
|
||||
|
||||
err = col.SerializeSql(buf)
|
||||
c.Assert(err, gc.IsNil)
|
||||
|
||||
sql = buf.String()
|
||||
c.Assert(sql, gc.Equals, "foo.col")
|
||||
}
|
||||
|
||||
//
|
||||
// tests for AliasCoulmns
|
||||
//
|
||||
|
||||
func (s *ColumnSuite) TestAliasColumnName(c *gc.C) {
|
||||
col := Alias("foo", SqlFunc("max", table1Col1))
|
||||
|
||||
c.Assert(col.Name(), gc.Equals, "foo")
|
||||
}
|
||||
|
||||
func (s *ColumnSuite) TestAliasColumnSerializeSqlForColumnList(c *gc.C) {
|
||||
col := Alias("foo", SqlFunc("max", table1Col1))
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
err := col.SerializeSqlForColumnList(buf)
|
||||
c.Assert(err, gc.IsNil)
|
||||
|
||||
sql := buf.String()
|
||||
c.Assert(err, gc.IsNil)
|
||||
|
||||
c.Assert(sql, gc.Equals, "(max(table1.col1)) AS \"foo\"")
|
||||
}
|
||||
|
||||
func (s *ColumnSuite) TestAliasColumnSerializeSqlForColumnListNilExpr(c *gc.C) {
|
||||
col := Alias("foo", nil)
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
err := col.SerializeSqlForColumnList(buf)
|
||||
c.Assert(err, gc.NotNil)
|
||||
}
|
||||
|
||||
func (s *ColumnSuite) TestAliasColumnSerializeSqlForColumnListInvalidAlias(
|
||||
c *gc.C) {
|
||||
|
||||
col := Alias("1234", SqlFunc("max", table1Col1))
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
err := col.SerializeSqlForColumnList(buf)
|
||||
c.Assert(err, gc.NotNil)
|
||||
}
|
||||
|
||||
func (s *ColumnSuite) TestAliasColumnSerializeSql(c *gc.C) {
|
||||
col := Alias("foo", SqlFunc("max", table1Col1))
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
err := col.SerializeSql(buf)
|
||||
c.Assert(err, gc.IsNil)
|
||||
|
||||
sql := buf.String()
|
||||
c.Assert(sql, gc.Equals, "`foo`")
|
||||
}
|
||||
|
||||
func (s *ColumnSuite) TestAliasColumnSetTableName(c *gc.C) {
|
||||
col := Alias("foo", SqlFunc("max", table1Col1))
|
||||
|
||||
// should always error
|
||||
err := col.setTableName("test")
|
||||
c.Assert(err, gc.NotNil)
|
||||
}
|
||||
|
||||
//
|
||||
// tests for deferredLookkupColumnName
|
||||
//
|
||||
|
||||
func (s *ColumnSuite) TestDeferredLookupColumnName(c *gc.C) {
|
||||
col := table1.C("foo")
|
||||
|
||||
c.Assert(col.Name(), gc.Equals, "foo")
|
||||
}
|
||||
|
||||
func (s *ColumnSuite) TestDeferredLookupColumnSerializeSqlForColumnList(
|
||||
c *gc.C) {
|
||||
|
||||
col := table1.C("col1")
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
err := col.SerializeSql(buf)
|
||||
c.Assert(err, gc.IsNil)
|
||||
|
||||
sql := buf.String()
|
||||
c.Assert(sql, gc.Equals, "table1.col1")
|
||||
|
||||
// check cached lookup
|
||||
buf = &bytes.Buffer{}
|
||||
|
||||
err = col.SerializeSql(buf)
|
||||
c.Assert(err, gc.IsNil)
|
||||
|
||||
sql = buf.String()
|
||||
c.Assert(sql, gc.Equals, "table1.col1")
|
||||
}
|
||||
|
||||
func (s *ColumnSuite) TestDeferredLookupColumnSerializeSqlForColumnListInvalidName(
|
||||
c *gc.C) {
|
||||
col := table1.C("foo")
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
err := col.SerializeSql(buf)
|
||||
c.Assert(err, gc.NotNil)
|
||||
}
|
||||
|
||||
func (s *ColumnSuite) TestDeferredLookupColumnSerializeSql(c *gc.C) {
|
||||
col := table1.C("col1")
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
err := col.SerializeSql(buf)
|
||||
c.Assert(err, gc.IsNil)
|
||||
|
||||
sql := buf.String()
|
||||
c.Assert(sql, gc.Equals, "table1.col1")
|
||||
}
|
||||
|
||||
func (s *ColumnSuite) TestDeferredLookupColumnSerializeSqlInvalidName(c *gc.C) {
|
||||
col := table1.C("foo")
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
err := col.SerializeSql(buf)
|
||||
c.Assert(err, gc.NotNil)
|
||||
}
|
||||
|
||||
func (s *ColumnSuite) TestDeferredLookupColumnSetTableName(c *gc.C) {
|
||||
col := table1.C("col1")
|
||||
|
||||
err := col.setTableName("foo")
|
||||
c.Assert(err, gc.NotNil)
|
||||
assertClauseSerialize(t, column, "col")
|
||||
column.setTableName("table1")
|
||||
assertClauseSerialize(t, column, "table1.col")
|
||||
assertProjectionSerialize(t, column.defaultAliasProjection(), `table1.col AS "table1.col"`)
|
||||
assertProjectionSerialize(t, column.AS("alias1"), `table1.col AS "alias1"`)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue