Skip to content

Commit 7bdd416

Browse files
igalklebanovkoskimas
authored andcommitted
test logging.
1 parent 8259d75 commit 7bdd416

File tree

2 files changed

+188
-1
lines changed

2 files changed

+188
-1
lines changed

Diff for: test/node/src/log-once.test.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ describe('logOnce', () => {
77
const sandbox = createSandbox()
88

99
before(() => {
10-
logSpy = sandbox.spy(console, 'log')
10+
logSpy = sandbox.stub(console, 'log')
11+
})
12+
13+
after(() => {
14+
sandbox.restore()
1115
})
1216

1317
it('should log each message once.', () => {

Diff for: test/node/src/logging.test.ts

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import { SinonSandbox, SinonSpy, createSandbox } from 'sinon'
2+
import { Database, expect } from './test-setup'
3+
import {
4+
DatabaseConnection,
5+
Driver,
6+
DummyDriver,
7+
Kysely,
8+
LogConfig,
9+
PostgresDialect,
10+
} from '../../..'
11+
12+
describe('logging', () => {
13+
let sandbox: SinonSandbox
14+
let errorSpy: SinonSpy
15+
let logSpy: SinonSpy
16+
17+
beforeEach(() => {
18+
sandbox = createSandbox()
19+
errorSpy = sandbox.stub(console, 'error')
20+
logSpy = sandbox.stub(console, 'log')
21+
})
22+
23+
afterEach(() => {
24+
sandbox.restore()
25+
})
26+
27+
describe('when query execution succeeds', () => {
28+
describe('when query logging is disabled', () => {
29+
describe('when error logging is disabled', () => {
30+
const db = getKysely([])
31+
32+
it('should not log query', async () => {
33+
await run(db)
34+
expect(logSpy.called).to.be.false
35+
})
36+
37+
it('should not log error', async () => {
38+
await run(db)
39+
expect(errorSpy.called).to.be.false
40+
})
41+
})
42+
43+
describe('when error logging is enabled', () => {
44+
const db = getKysely(['error'])
45+
46+
it('should not log query', async () => {
47+
await run(db)
48+
expect(logSpy.called).to.be.false
49+
})
50+
51+
it('should not log error', async () => {
52+
await run(db)
53+
expect(errorSpy.called).to.be.false
54+
})
55+
})
56+
})
57+
58+
describe('when query logging is enabled', () => {
59+
describe('when error logging is disabled', () => {
60+
const db = getKysely(['query'])
61+
62+
it('should log query', async () => {
63+
await run(db)
64+
expect(logSpy.called).to.be.true
65+
})
66+
67+
it('should not log error', async () => {
68+
await run(db)
69+
expect(errorSpy.called).to.be.false
70+
})
71+
})
72+
73+
describe('when error logging is enabled', () => {
74+
const db = getKysely(['query', 'error'])
75+
76+
it('should log query', async () => {
77+
await run(db)
78+
expect(logSpy.called).to.be.true
79+
})
80+
81+
it('should not log error', async () => {
82+
await run(db)
83+
expect(errorSpy.called).to.be.false
84+
})
85+
})
86+
})
87+
})
88+
89+
describe('when query execution fails', () => {
90+
const executeQuery = () => Promise.reject('oops')
91+
92+
describe('when query logging is disabled', () => {
93+
describe('when error logging is disabled', () => {
94+
const db = getKysely([], executeQuery)
95+
96+
it('should not log query', async () => {
97+
await run(db)
98+
expect(logSpy.called).to.be.false
99+
})
100+
101+
it('should not log error', async () => {
102+
await run(db)
103+
expect(errorSpy.called).to.be.false
104+
})
105+
})
106+
107+
describe('when error logging is enabled', () => {
108+
const db = getKysely(['error'], executeQuery)
109+
110+
it('should not log query', async () => {
111+
await run(db)
112+
expect(logSpy.called).to.be.false
113+
})
114+
115+
it('should log error', async () => {
116+
await run(db)
117+
expect(errorSpy.called).to.be.true
118+
})
119+
})
120+
})
121+
122+
describe('when query logging is enabled', () => {
123+
describe('when error logging is disabled', () => {
124+
const db = getKysely(['query'], executeQuery)
125+
126+
it('should not log query', async () => {
127+
await run(db)
128+
expect(logSpy.called).to.be.false
129+
})
130+
131+
it('should not log error', async () => {
132+
await run(db)
133+
expect(errorSpy.called).to.be.false
134+
})
135+
})
136+
137+
describe('when error logging is enabled', () => {
138+
const db = getKysely(['query', 'error'], executeQuery)
139+
140+
it('should not log query', async () => {
141+
await run(db)
142+
expect(logSpy.called).to.be.false
143+
})
144+
145+
it('should log error', async () => {
146+
await run(db)
147+
expect(errorSpy.called).to.be.true
148+
})
149+
})
150+
})
151+
})
152+
})
153+
154+
function getKysely(
155+
log: LogConfig,
156+
executeQuery: DatabaseConnection['executeQuery'] = () =>
157+
Promise.resolve({ rows: [] })
158+
): Kysely<Database> {
159+
return new Kysely({
160+
dialect: new (class extends PostgresDialect {
161+
constructor() {
162+
super({ pool: {} as any })
163+
}
164+
createDriver(): Driver {
165+
return new (class extends DummyDriver {
166+
acquireConnection(): Promise<DatabaseConnection> {
167+
return Promise.resolve({
168+
executeQuery,
169+
streamQuery: (async () => {}) as any,
170+
})
171+
}
172+
})()
173+
}
174+
})(),
175+
log,
176+
})
177+
}
178+
179+
async function run(db: Kysely<Database>) {
180+
try {
181+
await db.selectFrom('person').selectAll().execute()
182+
} catch (err) {}
183+
}

0 commit comments

Comments
 (0)