Skip to content

Commit a1c659c

Browse files
committed
#4 Introduce where isDirectory equals/different to true/false
1 parent d38b23b commit a1c659c

7 files changed

Lines changed: 197 additions & 17 deletions

File tree

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,16 @@ export interface IDirEntry {
8181

8282
`select * from root where size <> 1000000`
8383

84-
### TODO
85-
8684
`select * from root where isDirectory = true`
8785

86+
`select * from root where isDirectory = false`
87+
88+
`select * from root where isDirectory <> true`
89+
90+
`select * from root where isDirectory <> false`
91+
92+
### TODO
93+
8894
`select * from root where name = 'root.txt'`
8995

9096
`select * from root where name like '%txt'`

deps.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export {
66
assert,
77
assertExists,
88
assertEquals,
9-
assertThrows
9+
assertThrows,
10+
assertArrayIncludes
1011
} from "https://deno.land/std@0.82.0/testing/asserts.ts";

mod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import { IDirEntry } from "./types.ts";
44

55
import { select } from "./select.ts";
66

7-
export async function fsselect(query: string): Promise<IDirEntry[]> {
8-
return await select(parser.parse(query))
7+
export function fsselect(query: string): Promise<IDirEntry[]> {
8+
return select(parser.parse(query))
99
}

mod_test.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {assert } from "./deps.ts"
1+
import { assert, assertArrayIncludes, assertEquals } from "./deps.ts";
22

33
import { fsselect } from "./mod.ts";
44

@@ -18,7 +18,51 @@ Deno.test("if 'select * from root/test_folder_with_file' works", async () => {
1818
});
1919

2020
Deno.test("if 'select * from root/test_folder_with_files where size > 1000000' works", async () => {
21-
const result = await fsselect("select * from root/test_folder_with_files where size > 1000000");
21+
const result = await fsselect(
22+
"select * from root/test_folder_with_files where size > 1000000",
23+
);
2224
assert(result.length === 1);
23-
assert(result[0].name === 'b-file-1MB.txt');
25+
assert(result[0].name === "b-file-1MB.txt");
26+
});
27+
28+
Deno.test("if 'select * from root where isDirectory = true' works", async () => {
29+
const result = await fsselect("select * from root where isDirectory = true");
30+
assert(result.length === 3);
31+
32+
const names = result.map((i) => i.name as string);
33+
const expectedNames = [
34+
"test_folder_with_file",
35+
"test_folder_with_files",
36+
"test_folder_with_folder",
37+
];
38+
assertArrayIncludes<string>(names, expectedNames);
39+
});
40+
41+
Deno.test("if 'select * from root where isDirectory = false' works", async () => {
42+
const result = await fsselect("select * from root where isDirectory = false");
43+
assert(result.length === 1);
44+
assertEquals(result[0].name, "root.txt");
45+
assertEquals(result[0].isDirectory, false);
46+
assertEquals(result[0].isFile, true);
47+
});
48+
49+
Deno.test("if 'select * from root where isDirectory <> false' works", async () => {
50+
const result = await fsselect("select * from root where isDirectory <> false");
51+
assert(result.length === 3);
52+
53+
const names = result.map((i) => i.name as string);
54+
const expectedNames = [
55+
"test_folder_with_file",
56+
"test_folder_with_files",
57+
"test_folder_with_folder",
58+
];
59+
assertArrayIncludes<string>(names, expectedNames);
60+
});
61+
62+
Deno.test("if 'select * from root where isDirectory <> true' works", async () => {
63+
const result = await fsselect("select * from root where isDirectory <> true");
64+
assert(result.length === 1);
65+
assertEquals(result[0].name, "root.txt");
66+
assertEquals(result[0].isDirectory, false);
67+
assertEquals(result[0].isFile, true);
2468
});

select_test.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
assert,
33
assertEquals,
4+
assertArrayIncludes
45
} from "https://deno.land/std@0.82.0/testing/asserts.ts";
56

67
import { IQuery } from "./types.ts";
@@ -100,3 +101,107 @@ Deno.test("if a 1MB file is returned when the where clause is correct", async ()
100101
assert(result[0].createdAt);
101102
assert(result[0].modifiedAt);
102103
});
104+
105+
Deno.test("if 3 folders are returned when the where clause has isDirectory = true", async () => {
106+
const query: IQuery = {
107+
type: "select",
108+
fields: ["*"],
109+
from: "root",
110+
where: {
111+
conditions: [
112+
{
113+
left: "isDirectory",
114+
op: "Equal",
115+
right: "true",
116+
},
117+
],
118+
},
119+
};
120+
121+
const result = await select(query);
122+
123+
assert(result.length === 3);
124+
const names = result.map((i) => i.name as string);
125+
const expectedNames = [
126+
"test_folder_with_file",
127+
"test_folder_with_files",
128+
"test_folder_with_folder",
129+
];
130+
assertArrayIncludes<string>(names, expectedNames);
131+
});
132+
133+
Deno.test("if the 'root.txt' is returned when the where clause has isDirectory = false", async () => {
134+
const query: IQuery = {
135+
type: "select",
136+
fields: ["*"],
137+
from: "root",
138+
where: {
139+
conditions: [
140+
{
141+
left: "isDirectory",
142+
op: "Equal",
143+
right: "false",
144+
},
145+
],
146+
},
147+
};
148+
149+
const result = await select(query);
150+
151+
assert(result.length === 1);
152+
assertEquals(result[0].name, "root.txt");
153+
assertEquals(result[0].isDirectory, false);
154+
assertEquals(result[0].isFile, true);
155+
});
156+
157+
Deno.test("if the 'root.txt' is returned when the where clause has isDirectory <> false", async () => {
158+
const query: IQuery = {
159+
type: "select",
160+
fields: ["*"],
161+
from: "root",
162+
where: {
163+
conditions: [
164+
{
165+
left: "isDirectory",
166+
op: "Different",
167+
right: "false",
168+
},
169+
],
170+
},
171+
};
172+
173+
const result = await select(query);
174+
175+
assert(result.length === 3);
176+
const names = result.map((i) => i.name as string);
177+
const expectedNames = [
178+
"test_folder_with_file",
179+
"test_folder_with_files",
180+
"test_folder_with_folder",
181+
];
182+
assertArrayIncludes<string>(names, expectedNames);
183+
});
184+
185+
Deno.test("if 3 folders are returned when the where clause has isDirectory <> true", async () => {
186+
const query: IQuery = {
187+
type: "select",
188+
fields: ["*"],
189+
from: "root",
190+
where: {
191+
conditions: [
192+
{
193+
left: "isDirectory",
194+
op: "Different",
195+
right: "true",
196+
},
197+
],
198+
},
199+
};
200+
201+
const result = await select(query);
202+
203+
assert(result.length === 1);
204+
assertEquals(result[0].name, "root.txt");
205+
assertEquals(result[0].isDirectory, false);
206+
assertEquals(result[0].isFile, true);
207+
});

types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ export interface IWhereClause {
2323
export interface IWhereCondition {
2424
left: string;
2525
op: 'Different' | 'GreaterThan' | 'LessThan' | 'Equal' | 'Like';
26-
right: string | number;
26+
right: string | number | boolean;
2727
}

where.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,51 @@ function meet(entry: IDirEntry, condition: IWhereCondition): boolean {
3434
);
3535
}
3636

37-
return operation(entry.size, right);
37+
return operation(entry.size, <number>right);
38+
} else if (left === "isDirectory") {
39+
const operations = {
40+
"GreaterThan": null,
41+
"LessThan": null,
42+
"Equal": equalBoolean,
43+
"Different": differentBoolean,
44+
"Like": null,
45+
};
46+
47+
const operation = operations[op];
48+
if (!operation) {
49+
throw new Error(
50+
`The operation '${op}' is not supported for the '${left}' property`,
51+
);
52+
}
53+
54+
return operation(entry.isDirectory, JSON.parse(right as string));
3855
}
3956

4057
return false;
4158
}
4259

43-
function greaterThan(
44-
left: number | undefined,
45-
right: number | string,
46-
): boolean {
60+
/* Number */
61+
function greaterThan(left: number | undefined, right: number): boolean {
4762
return typeof left !== "undefined" && left > right;
4863
}
4964

50-
function lessThan(left: number | undefined, right: number | string): boolean {
65+
function lessThan(left: number | undefined, right: number): boolean {
5166
return typeof left !== "undefined" && left < right;
5267
}
5368

54-
function equal(left: number | undefined, right: number | string): boolean {
69+
function equal(left: number | undefined, right: number): boolean {
5570
return typeof left !== "undefined" && left === right;
5671
}
5772

58-
function different(left: number | undefined, right: number | string): boolean {
73+
function different(left: number | undefined, right: number): boolean {
5974
return typeof left !== "undefined" && left !== right;
6075
}
76+
77+
/* Boolean */
78+
function equalBoolean(left: boolean | undefined, right: boolean): boolean {
79+
return typeof left !== "undefined" && left === right;
80+
}
81+
82+
function differentBoolean(left: boolean | undefined, right: boolean): boolean {
83+
return typeof left !== "undefined" && left !== right;
84+
}

0 commit comments

Comments
 (0)