Skip to content

Commit ce8f39d

Browse files
authored
test: standardize database credentials with environment variables and refine test group names (#100)
1 parent e68df4e commit ce8f39d

14 files changed

Lines changed: 84 additions & 34 deletions

File tree

packages/bentocache/.env.example

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,19 @@
22
REDIS_HOST=localhost
33
REDIS_PORT=6379
44

5+
## Valkey
6+
VALKEY_HOST=localhost
7+
VALKEY_PORT=6380
8+
9+
## MySQL
10+
MYSQL_USER=root
11+
MYSQL_PASSWORD=root
12+
MYSQL_DATABASE=mysql
13+
MYSQL_PORT=3306
14+
15+
## PostgreSQL
16+
POSTGRES_USER=postgres
17+
POSTGRES_PASSWORD=postgres
18+
519
## DynamoDB
620
DYNAMODB_ENDPOINT=http://localhost:8000

packages/bentocache/src/drivers/file/cleaner.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ export async function pruneExpiredFiles({ directory, onError }) {
4545
for (const dirEntry of dirEntries) {
4646
if (dirEntry.isDirectory()) continue
4747

48-
const filePath = join(dirEntry.parentPath, dirEntry.name)
48+
/**
49+
* "parentPath" was added in Node.js v20.12.0.
50+
* We fallback to "path" for older versions of Node.js.
51+
*/
52+
const basePath = typeof dirEntry.parentPath === 'string' ? dirEntry.parentPath : dirEntry.path
53+
54+
const filePath = join(basePath, dirEntry.name)
4955
await deleteFileIfExpired({ filePath, onError })
5056
}
5157
}

packages/bentocache/tests/bus/bus.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ test.group('Bus synchronization', () => {
320320
assert.deepEqual(result, 'bar')
321321
})
322322

323-
test('binary encoding/decoding should works fine', async ({ assert, cleanup }, done) => {
323+
test('binary encoding/decoding should work fine', async ({ assert, cleanup }, done) => {
324324
const bus1 = redisBusDriver({ connection: REDIS_CREDENTIALS })
325325
.factory(null as any)
326326
.setId('foo')
@@ -383,7 +383,7 @@ test.group('Bus synchronization', () => {
383383
.waitForDone()
384384
.disableTimeout()
385385

386-
test('works with utf8 characters', async ({ assert }, done) => {
386+
test('works with utf8 characters', async ({ assert, cleanup }, done) => {
387387
const bus1 = redisBusDriver({ connection: REDIS_CREDENTIALS })
388388
.factory(null as any)
389389
.setId('foo')
@@ -392,6 +392,11 @@ test.group('Bus synchronization', () => {
392392
.factory(null as any)
393393
.setId('bar')
394394

395+
cleanup(async () => {
396+
await bus1.disconnect()
397+
await bus2.disconnect()
398+
})
399+
395400
const data = {
396401
keys: ['foo', '1', '2', 'bar', 'key::test', '🚀'],
397402
type: CacheBusMessageType.Set,
@@ -406,10 +411,6 @@ test.group('Bus synchronization', () => {
406411

407412
await bus2.publish('foo', data)
408413

409-
await bus1.disconnect()
410-
411-
await bus2.disconnect()
412-
413414
await sleep(200)
414415
}).waitForDone()
415416

packages/bentocache/tests/cache/one_tier_local.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { sleep } from '@julr/utils/misc'
44
import { CacheFactory } from '../../factories/cache_factory.js'
55
import { throwingFactory, slowFactory } from '../helpers/index.js'
66

7-
test.group('One tier tests', () => {
7+
test.group('One tier cache', () => {
88
test('get() returns deserialized value', async ({ assert }) => {
99
const { cache } = new CacheFactory().withMemoryL1().create()
1010

packages/bentocache/tests/cache/two_tier.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { CacheFactory } from '../../factories/cache_factory.js'
1010
import { L2CacheError, UndefinedValueError } from '../../src/errors.js'
1111
import { throwingFactory, slowFactory, REDIS_CREDENTIALS } from '../helpers/index.js'
1212

13-
test.group('Cache', () => {
13+
test.group('Two tier cache', () => {
1414
test('get() returns null if null is stored', async ({ assert }) => {
1515
const { cache } = new CacheFactory().withL1L2Config().create()
1616

@@ -674,7 +674,7 @@ test.group('Cache', () => {
674674
assert.deepEqual(r2?.entry.getValue(), { foo: 'bar' })
675675
})
676676

677-
test('when local and remote hitted items are logically it should prioritize remote', async ({
677+
test('should prioritize remote value when both local and remote items are logically expired', async ({
678678
assert,
679679
}) => {
680680
const { cache, local, remote, stack } = new CacheFactory()

packages/bentocache/tests/drivers/database.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { sleep } from '@julr/utils/misc'
55
import { DatabaseDriver } from '../../src/drivers/database/database.js'
66
import { KnexAdapter } from '../../src/drivers/database/adapters/knex.js'
77

8-
test.group('Database', () => {
8+
test.group('Database driver', () => {
99
test('should prune expired items every x seconds', async ({ assert, cleanup }) => {
1010
const db = knex({
1111
client: 'better-sqlite3',

packages/bentocache/tests/drivers/knex/mysql.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,35 @@ import { test } from '@japa/runner'
33
import { sleep } from '@julr/utils/misc'
44

55
import { createKnexStore } from './helpers.js'
6+
import { MYSQL_CREDENTIALS } from '../../helpers/index.js'
67
import { registerCacheDriverTestSuite } from '../../helpers/driver_test_suite.js'
78

8-
test.group('Knex | MySQL driver', (group) => {
9+
test.group('Knex | Mysql driver', (group) => {
910
registerCacheDriverTestSuite({
1011
test,
1112
group,
1213
supportsMilliseconds: false,
1314
createDriver: (options) => {
1415
const db = knex({
1516
client: 'mysql2',
16-
connection: { user: 'root', password: 'root', database: 'mysql', port: 3306 },
17+
connection: { ...MYSQL_CREDENTIALS },
1718
})
1819

1920
return createKnexStore({ connection: db, prefix: 'japa', ...options })
2021
},
2122
})
2223

23-
test('should not throw error when disconnecting immediately', async () => {
24+
test('should not throw error when disconnecting immediately', async ({ cleanup }) => {
2425
const db = knex({
2526
client: 'mysql2',
26-
connection: { user: 'root', password: 'root', database: 'mysql', port: 3306 },
27+
connection: { ...MYSQL_CREDENTIALS },
2728
})
29+
cleanup(() => db.destroy())
2830

2931
const store = createKnexStore({ connection: db, prefix: 'japa' })
3032

3133
await store.disconnect()
3234
await sleep(1000)
3335
await store.disconnect()
34-
await db.destroy()
3536
})
3637
})

packages/bentocache/tests/drivers/knex/postgres.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import knex from 'knex'
22
import { test } from '@japa/runner'
33

44
import { createKnexStore } from './helpers.js'
5+
import { POSTGRES_CREDENTIALS } from '../../helpers/index.js'
56
import { registerCacheDriverTestSuite } from '../../helpers/driver_test_suite.js'
67

7-
test.group('Knex | MySQL driver', (group) => {
8+
test.group('Knex | Postgres driver', (group) => {
89
registerCacheDriverTestSuite({
910
test,
1011
group,
1112
supportsMilliseconds: false,
1213
createDriver: (options) => {
1314
const db = knex({
1415
client: 'pg',
15-
connection: { user: 'postgres', password: 'postgres' },
16+
connection: { ...POSTGRES_CREDENTIALS },
1617
})
1718

1819
return createKnexStore({ connection: db, prefix: 'japa', ...options })

packages/bentocache/tests/drivers/kysely/mysql.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createPool } from 'mysql2'
33
import { Kysely, MysqlDialect } from 'kysely'
44

55
import { createKyselyStore } from './helpers.js'
6+
import { MYSQL_CREDENTIALS } from '../../helpers/index.js'
67
import { registerCacheDriverTestSuite } from '../../../src/test_suite.js'
78

89
test.group('Kysely | Mysql driver', (group) => {
@@ -13,7 +14,7 @@ test.group('Kysely | Mysql driver', (group) => {
1314
createDriver: (options) => {
1415
const db = new Kysely<any>({
1516
dialect: new MysqlDialect({
16-
pool: createPool({ user: 'root', password: 'root', database: 'mysql', port: 3306 }),
17+
pool: createPool({ ...MYSQL_CREDENTIALS }),
1718
}),
1819
})
1920

packages/bentocache/tests/drivers/kysely/postgres.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { test } from '@japa/runner'
33
import { Kysely, PostgresDialect } from 'kysely'
44

55
import { createKyselyStore } from './helpers.js'
6+
import { POSTGRES_CREDENTIALS } from '../../helpers/index.js'
67
import { registerCacheDriverTestSuite } from '../../../src/test_suite.js'
78

89
test.group('Kysely | Postgres driver', (group) => {
@@ -13,7 +14,7 @@ test.group('Kysely | Postgres driver', (group) => {
1314
createDriver: (options) => {
1415
const db = new Kysely<any>({
1516
dialect: new PostgresDialect({
16-
pool: new pg.Pool({ user: 'postgres', password: 'postgres' }),
17+
pool: new pg.Pool({ ...POSTGRES_CREDENTIALS }),
1718
}),
1819
})
1920

0 commit comments

Comments
 (0)