// currenttable. is used because we always need a refrence to table we are fetching FROM.
// so when implementing we need to remember to put FROM ${TableName} AS currenttable
it("Properly handles lookup method by creating left join",()=>{
constquery=newQuery();
constlookup_id=query.lookup({
from:"RefrenceTable",
localField:"id",
foreignField:"id",
});
query.match({
[`${lookup_id}.id`]:{
$in:[1,5,6,7],
},
});
constpipeline=query.toPipeline();
assert.equal(
pipeline,
`LEFT JOIN RefrenceTable ON currenttable.id = ${lookup_id}.id WHERE ${lookup_id}.id IN (1, 5, 6, 7)`
);
});
it("Properly handles lookup method by creating left join and where condition to field from refenced table",()=>{
constquery=newQuery();
constlookup_id=query.lookup({
from:"RefrenceTable",
localField:"id",
foreignField:"id",
});
query.match({
[`${lookup_id}.differentfields`]:{
$eq:"Test",
},
});
constpipeline=query.toPipeline();
assert.equal(
pipeline,
`LEFT JOIN RefrenceTable ON currenttable.id = ${lookup_id}.id WHERE ${lookup_id}.differentfields = 'Test'`
);
});
it("Properly handles lookup method by creating multiple left joins",()=>{
constquery=newQuery();
constlookup_id_first=query.lookup({
from:"RefrenceTableFirst",
localField:"id",
foreignField:"firstid",
});
constlookup_id_second=query.lookup({
from:"RefrenceTableSecond",
localField:"id",
foreignField:"secondid",
});
query.match({
[`${lookup_id_first}.differentfields`]:{
$eq:`${lookup_id_second}.differentfields`,
},
});
constpipeline=query.toPipeline();
assert.equal(
pipeline,
`LEFT JOIN RefrenceTableFirst ON currenttable.id = ${lookup_id_first}.firstid LEFT JOIN RefrenceTableSecond ON currenttable.id = ${lookup_id_second}.secondid WHERE ${lookup_id_first}.differentfields = ${lookup_id_second}.differentfields`
);
});
it("Properly maps where condition for IN clause with multiple values",()=>{
constpipeline=Query.fromSingleMatch({
id:{$in:[5,6]},
}).toPipeline();
assert.equal(pipeline,"WHERE id IN (5, 6)");
});
it("Properly maps where condition for IN clause with string values",()=>{
constpipeline=Query.fromSingleMatch({
testcolumn:{$in:["Test 1","Test 2"]},
}).toPipeline();
assert.equal(pipeline,"WHERE testcolumn IN ('Test 1', 'Test 2')");
});
it("Properly maps AND condition",()=>{
constpipeline=newQueries.And(
Query.fromSingleMatch({id:{$lte:5}}),
Query.fromSingleMatch({id:{$gt:1}})
).toPipeline();
assert.equal(pipeline,"WHERE (id <= 5 AND id > 1)");
});
it("Properly maps OR condition",()=>{
constpipeline=newQueries.Or(
Query.fromSingleMatch({id:{$lte:5}}),
Query.fromSingleMatch({id:{$gt:1}})
).toPipeline();
assert.equal(pipeline,"WHERE (id <= 5 OR id > 1)");
});
it("Properly maps NOT condition with AND",()=>{
constpipeline=newQueries.Not(
newQueries.And(
Query.fromSingleMatch({id:{$lte:5}}),
Query.fromSingleMatch({id:{$gt:1}})
)
).toPipeline();
assert.equal(pipeline,"WHERE (NOT (id <= 5 AND id > 1))");
});
it("Properly maps NOT condition with OR",()=>{
constpipeline=newQueries.Not(
newQueries.Or(
Query.fromSingleMatch({id:{$lte:5}}),
Query.fromSingleMatch({id:{$gt:1}})
)
).toPipeline();
assert.equal(pipeline,"WHERE (NOT (id <= 5 OR id > 1))");
});
it("Properly maps AND conditions with nested NOTs",()=>{