Skip to content

Commit f644dd4

Browse files
committed
#5 Implement the isFile where statement
1 parent b9562d3 commit f644dd4

4 files changed

Lines changed: 173 additions & 2 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ export interface IDirEntry {
8989

9090
`select * from root where isDirectory <> false`
9191

92+
`select * from root where isFile = true`
93+
94+
`select * from root where isFile = false`
95+
96+
`select * from root where isFile <> true`
97+
98+
`select * from root where isFile <> false`
99+
92100
### TODO
93101

94102
`select * from root where name = 'root.txt'`

mod_test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,46 @@ Deno.test("if 'select * from root where isDirectory <> true' works", async () =>
6565
assertEquals(result[0].name, "root.txt");
6666
assertEquals(result[0].isDirectory, false);
6767
assertEquals(result[0].isFile, true);
68+
});
69+
70+
Deno.test("if 'select * from root where isFile = true' works", async () => {
71+
const result = await fsselect("select * from root where isFile = true");
72+
assert(result.length === 1);
73+
assertEquals(result[0].name, "root.txt");
74+
assertEquals(result[0].isDirectory, false);
75+
assertEquals(result[0].isFile, true);
76+
});
77+
78+
Deno.test("if 'select * from root where isFile = false' works", async () => {
79+
const result = await fsselect("select * from root where isFile = false");
80+
assert(result.length === 3);
81+
82+
const names = result.map((i) => i.name as string);
83+
const expectedNames = [
84+
"test_folder_with_file",
85+
"test_folder_with_files",
86+
"test_folder_with_folder",
87+
];
88+
assertArrayIncludes<string>(names, expectedNames);
89+
});
90+
91+
Deno.test("if 'select * from root where isFile <> true' works", async () => {
92+
const result = await fsselect("select * from root where isFile <> true");
93+
assert(result.length === 3);
94+
95+
const names = result.map((i) => i.name as string);
96+
const expectedNames = [
97+
"test_folder_with_file",
98+
"test_folder_with_files",
99+
"test_folder_with_folder",
100+
];
101+
assertArrayIncludes<string>(names, expectedNames);
102+
});
103+
104+
Deno.test("if 'select * from root where isFile <> false' works", async () => {
105+
const result = await fsselect("select * from root where isFile <> false");
106+
assert(result.length === 1);
107+
assertEquals(result[0].name, "root.txt");
108+
assertEquals(result[0].isDirectory, false);
109+
assertEquals(result[0].isFile, true);
68110
});

select_test.ts

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
assert,
3+
assertArrayIncludes,
34
assertEquals,
4-
assertArrayIncludes
55
} from "https://deno.land/std@0.82.0/testing/asserts.ts";
66

77
import { IQuery } from "./types.ts";
@@ -204,4 +204,108 @@ Deno.test("if 3 folders are returned when the where clause has isDirectory <> tr
204204
assertEquals(result[0].name, "root.txt");
205205
assertEquals(result[0].isDirectory, false);
206206
assertEquals(result[0].isFile, true);
207-
});
207+
});
208+
209+
Deno.test("if 3 folders are returned when the where clause has isFile = true", async () => {
210+
const query: IQuery = {
211+
type: "select",
212+
fields: ["*"],
213+
from: "root",
214+
where: {
215+
conditions: [
216+
{
217+
left: "isFile",
218+
op: "Equal",
219+
right: "true",
220+
},
221+
],
222+
},
223+
};
224+
225+
const result = await select(query);
226+
227+
assert(result.length === 1);
228+
assertEquals(result[0].name, "root.txt");
229+
assertEquals(result[0].isDirectory, false);
230+
assertEquals(result[0].isFile, true);
231+
});
232+
233+
Deno.test("if the 'root.txt' is returned when the where clause has isFile = false", async () => {
234+
const query: IQuery = {
235+
type: "select",
236+
fields: ["*"],
237+
from: "root",
238+
where: {
239+
conditions: [
240+
{
241+
left: "isFile",
242+
op: "Equal",
243+
right: "false",
244+
},
245+
],
246+
},
247+
};
248+
249+
const result = await select(query);
250+
251+
assert(result.length === 3);
252+
const names = result.map((i) => i.name as string);
253+
const expectedNames = [
254+
"test_folder_with_file",
255+
"test_folder_with_files",
256+
"test_folder_with_folder",
257+
];
258+
assertArrayIncludes<string>(names, expectedNames);
259+
});
260+
261+
Deno.test("if the 'root.txt' is returned when the where clause has isFile <> false", async () => {
262+
const query: IQuery = {
263+
type: "select",
264+
fields: ["*"],
265+
from: "root",
266+
where: {
267+
conditions: [
268+
{
269+
left: "isFile",
270+
op: "Different",
271+
right: "false",
272+
},
273+
],
274+
},
275+
};
276+
277+
const result = await select(query);
278+
279+
assert(result.length === 1);
280+
assertEquals(result[0].name, "root.txt");
281+
assertEquals(result[0].isDirectory, false);
282+
assertEquals(result[0].isFile, true);
283+
});
284+
285+
Deno.test("if 3 folders are returned when the where clause has isFile <> true", async () => {
286+
const query: IQuery = {
287+
type: "select",
288+
fields: ["*"],
289+
from: "root",
290+
where: {
291+
conditions: [
292+
{
293+
left: "isFile",
294+
op: "Different",
295+
right: "true",
296+
},
297+
],
298+
},
299+
};
300+
301+
const result = await select(query);
302+
303+
assert(result.length === 3);
304+
const names = result.map((i) => i.name as string);
305+
const expectedNames = [
306+
"test_folder_with_file",
307+
"test_folder_with_files",
308+
"test_folder_with_folder",
309+
];
310+
assertArrayIncludes<string>(names, expectedNames);
311+
});

where.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ function meet(entry: IDirEntry, condition: IWhereCondition): boolean {
5252
}
5353

5454
return operation(entry.isDirectory, JSON.parse(right as string));
55+
} else if (left === "isFile") {
56+
const operations = {
57+
"GreaterThan": null,
58+
"LessThan": null,
59+
"Equal": equalBoolean,
60+
"Different": differentBoolean,
61+
"Like": null,
62+
};
63+
64+
const operation = operations[op];
65+
if (!operation) {
66+
throw new Error(
67+
`The operation '${op}' is not supported for the '${left}' property`,
68+
);
69+
}
70+
71+
return operation(entry.isFile, JSON.parse(right as string));
5572
}
5673

5774
return false;

0 commit comments

Comments
 (0)