|
2 | 2 | * built-in-commands.test.ts - test sqlitecloud built-in commands |
3 | 3 | */ |
4 | 4 |
|
5 | | -import { SQLiteCloudError, SQLiteCloudRowset } from '../../src/index' |
6 | | -import { SQLiteCloudConnection } from '../../src/drivers/connection' |
7 | | -import { SQLiteCloudTlsConnection } from '../../src/drivers/connection-tls' |
8 | | -import { CHINOOK_DATABASE_URL } from '../shared' |
9 | | -import { parseconnectionstring } from '../../src/drivers/utilities' |
10 | | - |
11 | | -const _ = undefined // to use undefined as empty argument |
12 | | - |
13 | | -function getConnection() { |
14 | | - return new SQLiteCloudTlsConnection({ connectionstring: CHINOOK_DATABASE_URL }, error => { |
15 | | - if (error) { |
16 | | - console.error(`getChinookTlsConnection - returned error: ${error}`) |
17 | | - } |
18 | | - expect(error).toBeNull() |
19 | | - }) |
20 | | -} |
21 | | - |
22 | | -const connUsername = parseconnectionstring(CHINOOK_DATABASE_URL).username |
23 | | - |
24 | | -//utils |
25 | | -const randomName = (length: number = 7): string => |
26 | | - Array(length + 1) |
27 | | - .join((Math.random().toString(36) + '00000000000000000').slice(2, 18)) |
28 | | - .slice(0, length) |
29 | | -const randomDate = (fromTime = new Date(new Date().getTime() + 4 * 60 * 60 * 1000).getTime()) => |
30 | | - new Date(fromTime + Math.random()) |
31 | | - .toISOString() |
32 | | - .replace('T', ' ') |
33 | | - .replace(/\.\d{3}Z/, '') |
34 | | -const randomBool = (): boolean => Math.random() < 0.5 |
35 | | -const date = () => expect.stringMatching(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/) |
36 | | -const ip = () => expect.stringMatching(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) |
37 | | -const uuid = () => expect.stringMatching(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/) |
38 | | -const bool = () => expect.any(Number) |
39 | | -const colseq = () => expect.stringMatching(/^(BINARY|RTRIM|NOCASE)$/) |
40 | | -const screaming_snake_case = () => expect.stringMatching(/^[A-Z]+[_]*[A-Z]*$/) |
41 | | -const regex_IP_UUID_N = /(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)|(^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$)|[0-9]/ |
42 | | - |
43 | | -/* const sqlOk = async (command: string, database: SQLiteCloudTlsConnection, done: jest.DoneCallback, ok: boolean) => { //testing with .sql |
44 | | - try { |
45 | | - const results = await database.sql(command) |
46 | | - console.log(results) |
47 | | - } catch (error) { |
48 | | - expect(error).toBeInstanceOf(SQLiteCloudError) |
49 | | - console.log(error) |
50 | | - } |
51 | | -} */ |
52 | | - |
53 | | -const test = (done: jest.DoneCallback, chinook: SQLiteCloudConnection, ok: boolean, expectedResult: any = 'OK', callback?: Function) => { |
54 | | - return (error: SQLiteCloudError | Error | null, results: any) => { |
55 | | - try { |
56 | | - if (ok) { |
57 | | - expect(error).toBeNull() |
58 | | - if (results && results.constructor) { |
59 | | - switch (results.constructor) { |
60 | | - case Array: |
61 | | - if (typeof expectedResult === 'number') { |
62 | | - expect(results).toHaveLength(expectedResult) |
63 | | - } else { |
64 | | - expectedResult.forEach((expRes: any) => expect(results).toContainEqual(expRes)) |
65 | | - } |
66 | | - break |
67 | | - case String: |
68 | | - expect(results).toMatch(expectedResult) |
69 | | - break |
70 | | - case Object: |
71 | | - case Number: |
72 | | - case Buffer: |
73 | | - expect(results).toEqual(expectedResult) |
74 | | - break |
75 | | - case SQLiteCloudRowset: |
76 | | - if (expectedResult instanceof Array) { |
77 | | - expect(results).toEqual(expectedResult) |
78 | | - } else { |
79 | | - expect(results).toContainEqual(expectedResult) |
80 | | - } |
81 | | - break |
82 | | - default: |
83 | | - expect(results).toBe(expectedResult) |
84 | | - } |
85 | | - } else { |
86 | | - expect(results).toBe(expectedResult) |
87 | | - } |
88 | | - } else { |
89 | | - try { |
90 | | - expect(results).not.toContainEqual(expectedResult) |
91 | | - } catch { |
92 | | - try { |
93 | | - expect(results).toEqual([]) |
94 | | - } catch { |
95 | | - try { |
96 | | - expect(error).toBeInstanceOf(SQLiteCloudError) |
97 | | - expect((error as SQLiteCloudError).message).toMatch( |
98 | | - /(not found|doesn\'t exist|does not exist|invalid|unable|fail|cannot|must be unique|unknown|undefined|error|no such|not available|try again later|wrong|has no|is read-only)/i |
99 | | - ) |
100 | | - expect(results).toBeUndefined() |
101 | | - } catch { |
102 | | - expect(results).toBeFalsy() |
103 | | - expect(error).toBeFalsy() |
104 | | - } |
105 | | - } |
106 | | - } |
107 | | - } |
108 | | - if (callback) callback(results, error) |
109 | | - done() |
110 | | - } catch (error) { |
111 | | - done(error) |
112 | | - } finally { |
113 | | - chinook.close() |
114 | | - } |
115 | | - } |
116 | | -} |
| 5 | +import { |
| 6 | + _, |
| 7 | + SQLiteCloudError, |
| 8 | + SQLiteCloudRowset, |
| 9 | + SQLiteCloudConnection, |
| 10 | + SQLiteCloudTlsConnection, |
| 11 | + CHINOOK_DATABASE_URL, |
| 12 | + parseconnectionstring, |
| 13 | + getConnection, |
| 14 | + connUsername, |
| 15 | + randomName, |
| 16 | + randomDate, |
| 17 | + randomBool, |
| 18 | + date, |
| 19 | + ip, |
| 20 | + uuid, |
| 21 | + bool, |
| 22 | + colseq, |
| 23 | + screaming_snake_case, |
| 24 | + regex_IP_UUID_N, |
| 25 | + test |
| 26 | +} from './shared' |
117 | 27 |
|
118 | 28 | describe.each([ |
119 | 29 | ['', true], |
|
0 commit comments