Skip to content

Commit

Permalink
test(factory): add tests for ConnectionFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed May 15, 2024
1 parent 8fd919b commit afb0d41
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 38 deletions.
60 changes: 30 additions & 30 deletions src/factories/ConnectionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,6 @@ export class ConnectionFactory {
return new Driver(con)
}

/**
* Parse connection config name if is default
*/
private static parseConName(con: string): string {
if (con === 'default') {
return Config.get('queue.default')
}

return con
}

/**
* Get the connection configuration of config/queue file.
*/
private static getConnectionDriver(con: string): string {
const config = Config.get(`queue.connections.${con}`)

if (!config) {
throw new NotImplementedConfigException(con)
}

if (!this.drivers.has(config.driver)) {
throw new NotFoundDriverException(config.driver)
}

return config.driver
}

/**
* Verify if client is present on a driver connection.
*/
Expand All @@ -110,7 +82,7 @@ export class ConnectionFactory {
* Set connection client on driver.
*/
public static setClient(con: string, client: any): void {
const connection = this.connections.get(con)
const connection = this.connections.get(con) || {}

connection.client = client

Expand All @@ -123,7 +95,7 @@ export class ConnectionFactory {
public static availableDrivers() {
const availableDrivers = []

for (const key of this.connections.keys()) {
for (const key of this.drivers.keys()) {
availableDrivers.push(key)
}

Expand All @@ -142,4 +114,32 @@ export class ConnectionFactory {

return availableConnections
}

/**
* Parse connection config name if is default
*/
private static parseConName(con: string): string {
if (con === 'default') {
return Config.get('queue.default')
}

return con
}

/**
* Get the connection configuration of config/queue file.
*/
private static getConnectionDriver(con: string): string {
const config = Config.get(`queue.connections.${con}`)

if (!config) {
throw new NotImplementedConfigException(con)
}

if (!this.drivers.has(config.driver)) {
throw new NotFoundDriverException(config.driver)
}

return config.driver
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from '#src/annotations/Worker'

export * from '#src/queue/QueueImpl'
export * from '#src/drivers/Driver'
export * from '#src/drivers/FakeDriver'
export * from '#src/drivers/VanillaDriver'
export * from '#src/drivers/DatabaseDriver'
export * from '#src/factories/ConnectionFactory'
Expand Down
61 changes: 61 additions & 0 deletions tests/unit/factories/ConnectionFactoryTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,64 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { Path } from '@athenna/common'
import { AfterEach, BeforeEach, Test, type Context } from '@athenna/test'
import { ConnectionFactory, FakeDriver, DatabaseDriver, VanillaDriver } from '#src'

export class ConnectionFactoryTest {
@BeforeEach()
public async beforeEach() {
await Config.loadAll(Path.fixtures('config'))
ConnectionFactory.connections = new Map()
}

@AfterEach()
public async afterEach() {
Config.clear()
}

@Test()
public async shouldBeAbleToGetAllAvailableDrivers({ assert }: Context) {
const availableDrivers = ConnectionFactory.availableDrivers()

assert.deepEqual(availableDrivers, ['fake', 'vanilla', 'database'])
}

@Test()
public async shouldBeAbleToGetAllAvailableConnections({ assert }: Context) {
const availableConnections = ConnectionFactory.availableConnections()

assert.deepEqual(availableConnections, [])
}

@Test()
public async shouldBeAbleToGetAllAvailableConnectionsWhenTheyExist({ assert }: Context) {
ConnectionFactory.setClient('test', {})

const availableConnections = ConnectionFactory.availableConnections()

assert.deepEqual(availableConnections, ['test'])
}

@Test()
public async shouldBeAbleToFabricateNewConnectionsAndReturnFakeDriverInstance({ assert }: Context) {
const driver = ConnectionFactory.fabricate('fake')

assert.deepEqual(driver, FakeDriver)
}

@Test()
public async shouldBeAbleToFabricateNewConnectionsAndReturnVanillaDriverInstance({ assert }: Context) {
const driver = ConnectionFactory.fabricate('vanilla')

assert.instanceOf(driver, VanillaDriver)
}

@Test()
public async shouldBeAbleToFabricateNewConnectionsAndReturnDatabaseDriverInstance({ assert }: Context) {
const driver = ConnectionFactory.fabricate('database')

assert.instanceOf(driver, DatabaseDriver)
}
}
8 changes: 0 additions & 8 deletions tests/unit/queue/QueueImplTest.ts

This file was deleted.

0 comments on commit afb0d41

Please sign in to comment.