@@ -6,13 +6,14 @@ import (
66 "github.com/gitql/gitql/mem"
77 "github.com/gitql/gitql/sql"
88
9+ "github.com/gitql/gitql/sql/expression"
910 "github.com/stretchr/testify/assert"
1011)
1112
1213func TestCatalog_Database (t * testing.T ) {
1314 assert := assert .New (t )
1415
15- c := sql.Catalog {}
16+ c := sql .NewCatalog ()
1617 db , err := c .Database ("foo" )
1718 assert .EqualError (err , "database not found: foo" )
1819 assert .Nil (db )
@@ -28,7 +29,7 @@ func TestCatalog_Database(t *testing.T) {
2829func TestCatalog_Table (t * testing.T ) {
2930 assert := assert .New (t )
3031
31- c := sql.Catalog {}
32+ c := sql .NewCatalog ()
3233
3334 table , err := c .Table ("foo" , "bar" )
3435 assert .EqualError (err , "database not found: foo" )
@@ -48,3 +49,144 @@ func TestCatalog_Table(t *testing.T) {
4849 assert .NoError (err )
4950 assert .Equal (mytable , table )
5051}
52+
53+ func TestCatalog_RegisterFunction_NoArgs (t * testing.T ) {
54+ assert := assert .New (t )
55+
56+ c := sql .NewCatalog ()
57+ name := "func"
58+ var expected sql.Expression = expression .NewStar ()
59+ err := c .RegisterFunction (name , func () sql.Expression {
60+ return expected
61+ })
62+ assert .Nil (err )
63+
64+ f , err := c .Function (name )
65+ assert .Nil (err )
66+
67+ e , err := f .Build ()
68+ assert .Nil (err )
69+ assert .Equal (expected , e )
70+
71+ e , err = f .Build (expression .NewStar ())
72+ assert .NotNil (err )
73+ assert .Nil (e )
74+
75+ e , err = f .Build (expression .NewStar (), expression .NewStar ())
76+ assert .NotNil (err )
77+ assert .Nil (e )
78+ }
79+
80+ func TestCatalog_RegisterFunction_OneArg (t * testing.T ) {
81+ assert := assert .New (t )
82+
83+ c := sql .NewCatalog ()
84+ name := "func"
85+ var expected sql.Expression = expression .NewStar ()
86+ err := c .RegisterFunction (name , func (sql.Expression ) sql.Expression {
87+ return expected
88+ })
89+ assert .Nil (err )
90+
91+ f , err := c .Function (name )
92+ assert .Nil (err )
93+
94+ e , err := f .Build ()
95+ assert .NotNil (err )
96+ assert .Nil (e )
97+
98+ e , err = f .Build (expression .NewStar ())
99+ assert .Nil (err )
100+ assert .Equal (expected , e )
101+
102+ e , err = f .Build (expression .NewStar (), expression .NewStar ())
103+ assert .NotNil (err )
104+ assert .Nil (e )
105+ }
106+
107+ func TestCatalog_RegisterFunction_Variadic (t * testing.T ) {
108+ assert := assert .New (t )
109+
110+ c := sql .NewCatalog ()
111+ name := "func"
112+ var expected sql.Expression = expression .NewStar ()
113+ err := c .RegisterFunction (name , func (... sql.Expression ) sql.Expression {
114+ return expected
115+ })
116+ assert .Nil (err )
117+
118+ f , err := c .Function (name )
119+ assert .Nil (err )
120+
121+ e , err := f .Build ()
122+ assert .Nil (err )
123+ assert .Equal (expected , e )
124+
125+ e , err = f .Build (expression .NewStar ())
126+ assert .Nil (err )
127+ assert .Equal (expected , e )
128+
129+ e , err = f .Build (expression .NewStar (), expression .NewStar ())
130+ assert .Nil (err )
131+ assert .Equal (expected , e )
132+ }
133+
134+ func TestCatalog_RegisterFunction_OneAndVariadic (t * testing.T ) {
135+ assert := assert .New (t )
136+
137+ c := sql .NewCatalog ()
138+ name := "func"
139+ var expected sql.Expression = expression .NewStar ()
140+ err := c .RegisterFunction (name , func (sql.Expression , ... sql.Expression ) sql.Expression {
141+ return expected
142+ })
143+ assert .Nil (err )
144+
145+ f , err := c .Function (name )
146+ assert .Nil (err )
147+
148+ e , err := f .Build ()
149+ assert .NotNil (err )
150+ assert .Nil (e )
151+
152+ e , err = f .Build (expression .NewStar ())
153+ assert .Nil (err )
154+ assert .Equal (expected , e )
155+
156+ e , err = f .Build (expression .NewStar (), expression .NewStar ())
157+ assert .Nil (err )
158+ assert .Equal (expected , e )
159+ }
160+
161+ func TestCatalog_RegisterFunction_Invalid (t * testing.T ) {
162+ assert := assert .New (t )
163+
164+ c := sql .NewCatalog ()
165+ name := "func"
166+ err := c .RegisterFunction (name , func (sql.Table ) sql.Expression {
167+ return nil
168+ })
169+ assert .NotNil (err )
170+
171+ err = c .RegisterFunction (name , func (sql.Expression ) sql.Table {
172+ return nil
173+ })
174+ assert .NotNil (err )
175+
176+ err = c .RegisterFunction (name , func (sql.Expression ) (sql.Table , error ) {
177+ return nil , nil
178+ })
179+ assert .NotNil (err )
180+
181+ err = c .RegisterFunction (name , 1 )
182+ assert .NotNil (err )
183+ }
184+
185+ func TestCatalog_Function_NotExists (t * testing.T ) {
186+ assert := assert .New (t )
187+
188+ c := sql .NewCatalog ()
189+ f , err := c .Function ("func" )
190+ assert .NotNil (err )
191+ assert .Nil (f )
192+ }
0 commit comments