@@ -881,5 +881,94 @@ describe('Client Queries', function () {
881
881
} ) ;
882
882
} ) ;
883
883
} ) ;
884
+ describe ( 'Case statement with functions' , ( ) => {
885
+ it . skip ( 'Should work when the case statement does not have backticks' , async ( ) => {
886
+ const queryText = `
887
+ SELECT
888
+ o.item,
889
+ o.notes,
890
+ o.specialChars,
891
+ (case
892
+ WHEN o.item = "pecans"
893
+ THEN 'Y'
894
+ ELSE 'N'
895
+ END)
896
+ as OriginalExecutive
897
+ FROM orders o
898
+ where id=2` ;
899
+ const parsedQuery = SQLParser . makeMongoAggregate ( queryText ) ;
900
+ const pipeline = [
901
+ { $match : { id : { $eq : 2 } } } ,
902
+ { $project : { o : '$$ROOT' } } ,
903
+ {
904
+ $project : {
905
+ item : '$o.item' ,
906
+ notes : '$o.notes' ,
907
+ specialChars : '$o.specialChars' ,
908
+ OriginalExecutive : {
909
+ $switch : {
910
+ branches : [
911
+ {
912
+ case : { $eq : [ '$o.item' , 'pecans' ] } ,
913
+ then : 'Y' ,
914
+ } ,
915
+ ] ,
916
+ default : { $literal : 'N' } ,
917
+ } ,
918
+ } ,
919
+ } ,
920
+ } ,
921
+ ] ;
922
+ assert (
923
+ parsedQuery . pipeline [ 2 ] . $project . OriginalExecutive . $switch
924
+ . branches [ 0 ] . case . $eq [ 0 ] === '$o.item'
925
+ ) ;
926
+ const results = await mongoClient
927
+ . db ( _dbName )
928
+ . collection ( parsedQuery . collections [ 0 ] )
929
+ . aggregate ( parsedQuery . pipeline )
930
+ . toArray ( ) ;
931
+ assert ( results ) ;
932
+ assert ( results [ 0 ] . OriginalExecutive === 'Y' ) ;
933
+ } ) ;
934
+ } ) ;
935
+ describe ( 'select in select' , ( ) => {
936
+ it ( 'Should work without an order by on a local field' , async ( ) => {
937
+ const queryText = `
938
+ SELECT Address,
939
+ SELECT \`Film Title\` from Inventory where inventoryId=1 as latestFilm
940
+ FROM stores
941
+ where _id=1` ;
942
+ const parsedQuery = SQLParser . makeMongoAggregate ( queryText ) ;
943
+ const results = await mongoClient
944
+ . db ( _dbName )
945
+ . collection ( parsedQuery . collections [ 0 ] )
946
+ . aggregate ( parsedQuery . pipeline )
947
+ . toArray ( ) ;
948
+ assert ( results ) ;
949
+ console . log ( results ) ;
950
+ } ) ;
951
+ it ( 'Should work without an order by on another table' , async ( ) => {
952
+ const queryText = `SELECT c.*,cn.* FROM customers c inner join (select * from \`customer-notes\` where id > 2) cn on cn.id=c.id` ;
953
+ const parsedQuery = SQLParser . makeMongoAggregate ( queryText ) ;
954
+ const results = await mongoClient
955
+ . db ( _dbName )
956
+ . collection ( parsedQuery . collections [ 0 ] )
957
+ . aggregate ( parsedQuery . pipeline )
958
+ . toArray ( ) ;
959
+ assert ( results ) ;
960
+ } ) ;
961
+ it ( 'Should work with an order by on another table' , async ( ) => {
962
+ const queryText = `SELECT c.*,cn.* FROM customers c inner join (select * from \`customer-notes\` ORDER BY id DESC limit 1) cn on cn.id=c.id` ;
963
+ const parsedQuery = SQLParser . makeMongoAggregate ( queryText ) ;
964
+ const results = await mongoClient
965
+ . db ( _dbName )
966
+ . collection ( parsedQuery . collections [ 0 ] )
967
+ . aggregate ( parsedQuery . pipeline )
968
+ . toArray ( ) ;
969
+ assert ( results ) ;
970
+ assert ( results [ 0 ] . cn [ 0 ] . id === 5 ) ;
971
+ } ) ;
972
+ } ) ;
884
973
} ) ;
885
974
} ) ;
0 commit comments