Skip to content

Commit e0915fe

Browse files
committed
test: increase test coverage for ConnectionString
1 parent bd9fa97 commit e0915fe

File tree

3 files changed

+110
-3
lines changed

3 files changed

+110
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"build": "tsc",
1818
"lint": "eslint ./src ./test",
1919
"test": "tsx --test test/**/*.test.ts",
20-
"test:coverage": "c8 --reporter=cobertura -- tsx --test test/**/*.test.ts",
20+
"test:coverage": "c8 --reporter=cobertura -- tsx --test test/**/*.test.ts test/*.test.ts",
2121
"test:workflow": "npm run test --silent"
2222
},
2323
"keywords": [

src/connection-string.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class ConnectionString implements ReadonlyMap<string, string> {
4444
// it would be really nice to be able to make this a generice (eg: get<string>) and that would then coerce the value
4545
// see typia library for an example of something similar
4646
get<T extends CoerceType = 'string'>(key: string, coerceType?: T): CoerceTypeMap[T] | undefined {
47-
const val = this.#parsed.get(key);
47+
const val = this.#parsed.get(key.toLowerCase());
4848
const actualType = coerceType ?? 'string';
4949
if (typeof val === 'undefined' || actualType === 'string') {
5050
return val as CoerceTypeMap[T];
@@ -96,7 +96,7 @@ export class ConnectionString implements ReadonlyMap<string, string> {
9696
// try to find the property
9797
const prop = [key, ...aliases ?? []].find((k) => this.has(k));
9898
if (prop || includeMissing) {
99-
props.push([key, prop ? this.get(prop, type) : defaultValue] as [keyof T, CoerceType]);
99+
props.push([key, prop ? this.get(prop.toLowerCase(), type) : defaultValue] as [keyof T, CoerceType]);
100100
}
101101
return props;
102102
}, [] as [keyof T, CoerceType][])) as InferSchema<T>;

test/connection-string.test.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import * as assert from 'node:assert';
2+
import { describe, it } from 'node:test';
3+
import { ConnectionString } from '../src/connection-string';
4+
5+
describe('connection-string', () => {
6+
const connectionString = 'keyword=value;isTrue=true;isYes=yes;isFalse=false;isNo=no;is0=0;is1=1';
7+
describe('.has()', () => {
8+
it('returns true for existing keys', () => {
9+
const parsed = new ConnectionString(connectionString);
10+
assert.equal(parsed.has('keyword'), true);
11+
assert.equal(parsed.has('isTrue'), true);
12+
assert.equal(parsed.has('isYes'), true);
13+
assert.equal(parsed.has('isFalse'), true);
14+
assert.equal(parsed.has('isNo'), true);
15+
assert.equal(parsed.has('is0'), true);
16+
assert.equal(parsed.has('is1'), true);
17+
});
18+
it('returns false for missing keys', () => {
19+
const parsed = new ConnectionString(connectionString);
20+
assert.equal(parsed.has('missing'), false);
21+
});
22+
});
23+
describe('.get()', () => {
24+
it('coerces a string', () => {
25+
const parsed = new ConnectionString(connectionString);
26+
assert.equal(parsed.get('keyword'), 'value');
27+
assert.equal(parsed.get('keyword', 'string'), 'value');
28+
});
29+
it('coerces a string to a number', () => {
30+
const parsed = new ConnectionString(connectionString);
31+
assert.equal(parsed.get('keyword', 'number'), Number.NaN);
32+
assert.equal(parsed.get('is0', 'number'), 0);
33+
assert.equal(parsed.get('is1', 'number'), 1);
34+
});
35+
it('coerces a string to a boolean', () => {
36+
const parsed = new ConnectionString(connectionString);
37+
assert.equal(parsed.get('keyword', 'boolean'), true);
38+
});
39+
it('coerces a boolean', () => {
40+
const parsed = new ConnectionString(connectionString);
41+
assert.equal(parsed.get('isTrue', 'boolean'), true);
42+
assert.equal(parsed.get('isFalse', 'boolean'), false);
43+
assert.equal(parsed.get('isYes', 'boolean'), true);
44+
assert.equal(parsed.get('isNo', 'boolean'), false);
45+
assert.equal(parsed.get('is0', 'boolean'), false);
46+
assert.equal(parsed.get('is1', 'boolean'), true);
47+
});
48+
it('returns undefined for missing value', () => {
49+
const parsed = new ConnectionString(connectionString);
50+
assert.equal(parsed.get('missing'), undefined);
51+
});
52+
});
53+
describe('.size', () => {
54+
it('returns the number of properties', () => {
55+
const parsed = new ConnectionString(connectionString);
56+
assert.equal(parsed.size, 7);
57+
});
58+
});
59+
describe('.keys()', () => {
60+
it('returns the keys', () => {
61+
const parsed = new ConnectionString(connectionString);
62+
assert.deepEqual(Array.from(parsed.keys()), [
63+
'keyword',
64+
'istrue',
65+
'isyes',
66+
'isfalse',
67+
'isno',
68+
'is0',
69+
'is1',
70+
]);
71+
});
72+
});
73+
describe('.values()', () => {
74+
it('returns the values', () => {
75+
const parsed = new ConnectionString(connectionString);
76+
assert.deepEqual(Array.from(parsed.values()), [
77+
'value',
78+
'true',
79+
'yes',
80+
'false',
81+
'no',
82+
'0',
83+
'1',
84+
]);
85+
});
86+
});
87+
describe('.entries()', () => {
88+
it('returns the entries', () => {
89+
const parsed = new ConnectionString(connectionString);
90+
assert.deepEqual(Array.from(parsed.entries()), [
91+
['keyword', 'value'],
92+
['istrue', 'true'],
93+
['isyes', 'yes'],
94+
['isfalse', 'false'],
95+
['isno', 'no'],
96+
['is0', '0'],
97+
['is1', '1'],
98+
]);
99+
});
100+
});
101+
describe('.toString()', () => {
102+
it('returns the connection string', () => {
103+
const parsed = new ConnectionString(connectionString);
104+
assert.equal(parsed.toString(), connectionString);
105+
});
106+
});
107+
});

0 commit comments

Comments
 (0)