Skip to content

Commit edd3f76

Browse files
committed
moved core tests shared utilities
1 parent a1a90a8 commit edd3f76

3 files changed

Lines changed: 158 additions & 113 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.224",
3+
"version": "1.0.241",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

test/core/built-in-commands.test.ts

Lines changed: 22 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -2,118 +2,28 @@
22
* built-in-commands.test.ts - test sqlitecloud built-in commands
33
*/
44

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'
11727

11828
describe.each([
11929
['', true],

test/core/shared.ts

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

0 commit comments

Comments
 (0)