jet/sqlbuilder/table_test.go

189 lines
4.1 KiB
Go
Raw Normal View History

2019-03-31 09:17:28 +02:00
// +build disabled
package sqlbuilder
import (
"bytes"
gc "gopkg.in/check.v1"
)
type TableSuite struct {
}
var _ = gc.Suite(&TableSuite{})
// NOTE: tables / columns are defined in statement_test.go
func (s *TableSuite) TestBasicColumns(c *gc.C) {
cols := table1.Columns()
c.Assert(len(cols), gc.Equals, 4)
c.Assert(cols[0], gc.Equals, table1Col1)
c.Assert(cols[1], gc.Equals, table1ColFloat)
c.Assert(cols[2], gc.Equals, table1Col3)
2019-05-29 14:03:38 +02:00
c.Assert(cols[3], gc.Equals, table1ColTime)
}
func (s *TableSuite) TestCValidLookup(c *gc.C) {
col := table1.C("col1")
buf := &bytes.Buffer{}
err := col.SerializeSql(buf)
c.Assert(err, gc.IsNil)
sql := buf.String()
2019-03-09 09:52:03 +01:00
c.Assert(sql, gc.Equals, "table1.col1")
}
func (s *TableSuite) TestCInvalidLookup(c *gc.C) {
col := table1.C("foo")
buf := &bytes.Buffer{}
err := col.SerializeSql(buf)
c.Assert(err, gc.NotNil)
}
func (s *TableSuite) TestJoinNilLeftTable(c *gc.C) {
join := InnerJoinOn(nil, table2, EqL(table2Col3, 123))
buf := &bytes.Buffer{}
err := join.serialize("", buf)
c.Assert(err, gc.NotNil)
}
func (s *TableSuite) TestJoinNilRightTable(c *gc.C) {
join := InnerJoinOn(table1, nil, EqL(table2Col3, 123))
buf := &bytes.Buffer{}
err := join.serialize("", buf)
c.Assert(err, gc.NotNil)
}
func (s *TableSuite) TestJoinNilOnCondition(c *gc.C) {
join := InnerJoinOn(table1, table2, nil)
buf := &bytes.Buffer{}
err := join.serialize("", buf)
c.Assert(err, gc.NotNil)
}
func (s *TableSuite) TestInnerJoin(c *gc.C) {
2019-05-29 14:03:38 +02:00
join := table1.InnerJoinOn(table2, EQ(table1Col3, table2Col3))
buf := &bytes.Buffer{}
2019-03-09 09:52:03 +01:00
err := join.SerializeSql(buf)
c.Assert(err, gc.IsNil)
sql := buf.String()
c.Assert(
sql,
gc.Equals,
2019-03-09 09:52:03 +01:00
"db.table1 JOIN db.table2 ON table1.col3=table2.col3")
}
func (s *TableSuite) TestLeftJoin(c *gc.C) {
2019-05-29 14:03:38 +02:00
join := table1.LEFT_JOIN(table2, EQ(table1Col3, table2Col3))
buf := &bytes.Buffer{}
err := join.serialize("", buf)
c.Assert(err, gc.IsNil)
sql := buf.String()
c.Assert(
sql,
gc.Equals,
2019-03-09 09:52:03 +01:00
"db.table1 LEFT JOIN db.table2 "+
"ON table1.col3=table2.col3")
}
func (s *TableSuite) TestRightJoin(c *gc.C) {
2019-05-29 14:03:38 +02:00
join := table1.RIGHT_JOIN(table2, EQ(table1Col3, table2Col3))
buf := &bytes.Buffer{}
err := join.serialize("", buf)
c.Assert(err, gc.IsNil)
sql := buf.String()
c.Assert(
sql,
gc.Equals,
2019-03-09 09:52:03 +01:00
"db.table1 RIGHT JOIN db.table2 "+
"ON table1.col3=table2.col3")
}
2019-05-05 13:49:24 +02:00
//func (s *TableSuite) TestJoinColumns(c *gc.C) {
2019-05-29 14:03:38 +02:00
// join := table1.RIGHT_JOIN(table2, EQ(table1Col3, table2Col3))
2019-05-05 13:49:24 +02:00
//
// cols := join.Columns()
// c.Assert(len(cols), gc.Equals, 6)
// c.Assert(cols[0], gc.Equals, table1Col1)
// c.Assert(cols[1], gc.Equals, table1ColFloat)
2019-05-05 13:49:24 +02:00
// c.Assert(cols[2], gc.Equals, table1Col3)
2019-05-29 14:03:38 +02:00
// c.Assert(cols[3], gc.Equals, table1ColTime)
2019-05-05 13:49:24 +02:00
// c.Assert(cols[4], gc.Equals, table2Col3)
// c.Assert(cols[5], gc.Equals, table2Col4)
//}
func (s *TableSuite) TestNestedInnerJoin(c *gc.C) {
2019-05-29 14:03:38 +02:00
join1 := table1.InnerJoinOn(table2, EQ(table1Col3, table2Col3))
join2 := join1.InnerJoinOn(table3, EQ(table1Col1, table3Col1))
buf := &bytes.Buffer{}
2019-03-09 09:52:03 +01:00
err := join2.SerializeSql(buf)
c.Assert(err, gc.IsNil)
sql := buf.String()
c.Assert(
sql,
gc.Equals,
2019-03-09 09:52:03 +01:00
"db.table1 "+
"JOIN db.table2 ON table1.col3=table2.col3 "+
"JOIN db.table3 ON table1.col1=table3.col1")
}
func (s *TableSuite) TestNestedLeftJoin(c *gc.C) {
2019-05-29 14:03:38 +02:00
join1 := table1.InnerJoinOn(table2, EQ(table1Col3, table2Col3))
join2 := join1.LeftJoinOn(table3, EQ(table1Col1, table3Col1))
buf := &bytes.Buffer{}
2019-03-09 09:52:03 +01:00
err := join2.SerializeSql(buf)
c.Assert(err, gc.IsNil)
sql := buf.String()
c.Assert(
sql,
gc.Equals,
2019-03-09 09:52:03 +01:00
"db.table1 "+
"JOIN db.table2 ON table1.col3=table2.col3 "+
"LEFT JOIN db.table3 ON table1.col1=table3.col1")
}
func (s *TableSuite) TestNestedRightJoin(c *gc.C) {
2019-05-29 14:03:38 +02:00
join1 := table1.InnerJoinOn(table2, EQ(table1Col3, table2Col3))
join2 := join1.RightJoinOn(table3, EQ(table1Col1, table3Col1))
buf := &bytes.Buffer{}
2019-03-09 09:52:03 +01:00
err := join2.SerializeSql(buf)
c.Assert(err, gc.IsNil)
sql := buf.String()
c.Assert(
sql,
gc.Equals,
2019-03-09 09:52:03 +01:00
"db.table1 "+
"JOIN db.table2 ON table1.col3=table2.col3 "+
"RIGHT JOIN db.table3 ON table1.col1=table3.col1")
}