|
| 1 | +import {describe, it as test} from "https://deno.land/ [email protected]/testing/bdd.ts"; |
| 2 | +import {assertEquals} from "https://deno.land/ [email protected]/assert/assert_equals.ts"; |
| 3 | +import {Query, QueryTypes} from "../src/query.ts"; |
| 4 | + |
| 5 | +type BasicFilterQueryTest = { |
| 6 | + description: string; |
| 7 | + value: QueryTypes; |
| 8 | + expectedValues: string; |
| 9 | +} |
| 10 | + |
| 11 | +const tests: BasicFilterQueryTest[] = [ |
| 12 | + { |
| 13 | + description: 'with a string', |
| 14 | + value: 's', |
| 15 | + expectedValues: '["s"]' |
| 16 | + }, |
| 17 | + { |
| 18 | + description: 'with a integer', |
| 19 | + value: 1, |
| 20 | + expectedValues: '[1]' |
| 21 | + }, |
| 22 | + { |
| 23 | + description: 'with a double', |
| 24 | + value: 1.2, |
| 25 | + expectedValues: '[1.2]' |
| 26 | + }, |
| 27 | + { |
| 28 | + description: 'with a whole number double', |
| 29 | + value: 1.0, |
| 30 | + expectedValues: '[1]' |
| 31 | + }, |
| 32 | + { |
| 33 | + description: 'with a bool', |
| 34 | + value: false, |
| 35 | + expectedValues: '[false]' |
| 36 | + }, |
| 37 | + { |
| 38 | + description: 'with a list', |
| 39 | + value: ['a', 'b', 'c'], |
| 40 | + expectedValues: '["a","b","c"]' |
| 41 | + } |
| 42 | +]; |
| 43 | + |
| 44 | +describe('Query', () => { |
| 45 | + describe('basic filter equal', () => { |
| 46 | + for (const t of tests) { |
| 47 | + test(t.description, () => |
| 48 | + assertEquals( |
| 49 | + Query.equal("attr", t.value), |
| 50 | + `equal("attr", ${t.expectedValues})`, |
| 51 | + ) |
| 52 | + ) |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + describe('basic filter notEqual', () => { |
| 57 | + for (const t of tests) { |
| 58 | + test(t.description, () => |
| 59 | + assertEquals( |
| 60 | + Query.notEqual("attr", t.value), |
| 61 | + `notEqual("attr", ${t.expectedValues})`, |
| 62 | + ) |
| 63 | + ) |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + describe('basic filter lessThan', () => { |
| 68 | + for (const t of tests) { |
| 69 | + test(t.description, () => |
| 70 | + assertEquals( |
| 71 | + Query.lessThan("attr", t.value), |
| 72 | + `lessThan("attr", ${t.expectedValues})`, |
| 73 | + ) |
| 74 | + ) |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + describe('basic filter lessThanEqual', () => { |
| 79 | + for (const t of tests) { |
| 80 | + test(t.description, () => |
| 81 | + assertEquals( |
| 82 | + Query.lessThanEqual("attr", t.value), |
| 83 | + `lessThanEqual("attr", ${t.expectedValues})`, |
| 84 | + ) |
| 85 | + ) |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + describe('basic filter greaterThan', () => { |
| 90 | + for (const t of tests) { |
| 91 | + test(t.description, () => |
| 92 | + assertEquals( |
| 93 | + Query.greaterThan("attr", t.value), |
| 94 | + `greaterThan("attr", ${t.expectedValues})`, |
| 95 | + ) |
| 96 | + ) |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + describe('basic filter greaterThanEqual', () => { |
| 101 | + for (const t of tests) { |
| 102 | + test(t.description, () => |
| 103 | + assertEquals( |
| 104 | + Query.greaterThanEqual("attr", t.value), |
| 105 | + `greaterThanEqual("attr", ${t.expectedValues})`, |
| 106 | + ) |
| 107 | + ) |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + test('search', () => assertEquals( |
| 112 | + Query.search('attr', 'keyword1 keyword2'), |
| 113 | + 'search("attr", ["keyword1 keyword2"])', |
| 114 | + )); |
| 115 | + |
| 116 | + test('isNull', () => assertEquals( |
| 117 | + Query.isNull('attr'), |
| 118 | + 'isNull("attr")', |
| 119 | + )); |
| 120 | + |
| 121 | + test('isNotNull', () => assertEquals( |
| 122 | + Query.isNotNull('attr'), |
| 123 | + 'isNotNull("attr")', |
| 124 | + )); |
| 125 | + |
| 126 | + describe('between', () => { |
| 127 | + test('with integers', () => assertEquals( |
| 128 | + Query.between('attr', 1, 2), |
| 129 | + 'between("attr", [1,2])' |
| 130 | + )); |
| 131 | + test('with doubles', () => assertEquals( |
| 132 | + Query.between('attr', 1.2, 2.2), |
| 133 | + 'between("attr", [1.2,2.2])' |
| 134 | + )); |
| 135 | + test('with strings', () => assertEquals( |
| 136 | + Query.between('attr', "a", "z"), |
| 137 | + 'between("attr", ["a","z"])' |
| 138 | + )); |
| 139 | + }); |
| 140 | + |
| 141 | + test('select', () => assertEquals( |
| 142 | + Query.select(['attr1', 'attr2']), |
| 143 | + 'select(["attr1","attr2"])', |
| 144 | + )); |
| 145 | + |
| 146 | + test('orderAsc', () => assertEquals( |
| 147 | + Query.orderAsc('attr'), |
| 148 | + 'orderAsc("attr")', |
| 149 | + )); |
| 150 | + |
| 151 | + test('orderDesc', () => assertEquals( |
| 152 | + Query.orderDesc('attr'), |
| 153 | + 'orderDesc("attr")', |
| 154 | + )); |
| 155 | + |
| 156 | + test('cursorBefore', () => assertEquals( |
| 157 | + Query.cursorBefore('attr'), |
| 158 | + 'cursorBefore("attr")', |
| 159 | + )); |
| 160 | + |
| 161 | + test('cursorAfter', () => assertEquals( |
| 162 | + Query.cursorAfter('attr'), |
| 163 | + 'cursorAfter("attr")', |
| 164 | + )); |
| 165 | + |
| 166 | + test('limit', () => assertEquals( |
| 167 | + Query.limit(1), |
| 168 | + 'limit(1)' |
| 169 | + )); |
| 170 | + |
| 171 | + test('offset', () => assertEquals( |
| 172 | + Query.offset(1), |
| 173 | + 'offset(1)' |
| 174 | + )); |
| 175 | +}) |
0 commit comments