@@ -172,9 +172,49 @@ describe('SqlAlias', () => {
172172 } ) ;
173173
174174 describe ( '.create' , ( ) => {
175- expect (
176- SqlAlias . create ( SqlAlias . create ( SqlColumn . create ( 'X' ) , 'name1' ) , 'name2' ) . toString ( ) ,
177- ) . toEqual ( '"X" AS "name2"' ) ;
175+ it ( 'overwrites existing alias when aliasing an already aliased expression' , ( ) => {
176+ expect (
177+ SqlAlias . create ( SqlAlias . create ( SqlColumn . create ( 'X' ) , 'name1' ) , 'name2' ) . toString ( ) ,
178+ ) . toEqual ( '"X" AS "name2"' ) ;
179+ } ) ;
180+
181+ it ( 'creates a simple alias with string column and string alias' , ( ) => {
182+ expect ( SqlAlias . create ( SqlColumn . create ( 'col1' ) , 'alias1' ) . toString ( ) ) . toEqual (
183+ '"col1" AS "alias1"' ,
184+ ) ;
185+ } ) ;
186+
187+ it ( 'creates an alias with RefName object as alias' , ( ) => {
188+ const refName = RefName . create ( 'myAlias' , true ) ;
189+ expect ( SqlAlias . create ( SqlColumn . create ( 'col1' ) , refName ) . toString ( ) ) . toEqual (
190+ '"col1" AS "myAlias"' ,
191+ ) ;
192+ } ) ;
193+
194+ it ( 'auto-quotes aliases that are reserved keywords' , ( ) => {
195+ expect ( SqlAlias . create ( SqlColumn . create ( 'col1' ) , 'select' ) . toString ( ) ) . toEqual (
196+ '"col1" AS "select"' ,
197+ ) ;
198+ } ) ;
199+
200+ it ( 'forces quotes when forceQuotes is true' , ( ) => {
201+ expect ( SqlAlias . create ( SqlColumn . create ( 'col1' ) , 'normal' , true ) . toString ( ) ) . toEqual (
202+ '"col1" AS "normal"' ,
203+ ) ;
204+ } ) ;
205+
206+ it ( 'adds parentheses to SqlQuery expressions' , ( ) => {
207+ const query = SqlQuery . create ( 'tbl' ) ;
208+ const aliasedQuery = SqlAlias . create ( query , 'subq' ) ;
209+ const result = aliasedQuery . toString ( ) ;
210+
211+ // Check that the result contains the main components rather than exact formatting
212+ expect ( result ) . toContain ( '(' ) ;
213+ expect ( result ) . toContain ( ')' ) ;
214+ expect ( result ) . toContain ( 'SELECT' ) ;
215+ expect ( result ) . toContain ( 'FROM "tbl"' ) ;
216+ expect ( result ) . toContain ( 'AS "subq"' ) ;
217+ } ) ;
178218 } ) ;
179219
180220 describe ( '#changeAlias' , ( ) => {
0 commit comments