Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:just-dandi/dandi into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchaffer committed Jun 15, 2019
2 parents 0fd5100 + 6ae96fb commit 997933b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
67 changes: 40 additions & 27 deletions packages/dandi-contrib/data-pg/src/pg-db-pool-client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PgDbPool, poolClientFactory } from '@dandi-contrib/data-pg'
import { Disposable } from '@dandi/common'
import { PgDbPool, PgDbPoolClient, poolClientFactory } from '@dandi-contrib/data-pg'

import { expect } from 'chai'
import { createStubInstance, stub } from 'sinon'
Expand All @@ -15,52 +14,66 @@ describe('poolClientFactory', function() {
const client = {}
this.pool.connect.resolves(client)

expect(await poolClientFactory(this.pool)).to.equal(client)
await poolClientFactory(this.pool)

})
expect(this.pool.connect).to.have.been.calledOnce

it('returns the client directly if it is already a disposable', async function() {
})

const client = {
dispose: stub(),
}
const makeDisposable = stub(Disposable, 'makeDisposable')
it('returns an instance of PgDbPoolClient', async function() {

const client = {}
this.pool.connect.resolves(client)

expect(await poolClientFactory(this.pool)).to.equal(client)
expect(Disposable.makeDisposable).to.not.have.been.called

makeDisposable.restore()
expect(await poolClientFactory(this.pool)).to.be.instanceof(PgDbPoolClient)

})

it('makes the client disposable by adding a dispose() function', async function() {
})

describe('PgDbPoolClient', function() {

const client: any = {
beforeEach(function() {
this.poolClient = {
query: stub(),
release: stub(),
}
this.client = new PgDbPoolClient(this.poolClient)
})

this.pool.connect.resolves(client)
describe('query', function() {

await poolClientFactory(this.pool)
it('passes the cmd and args params through to the pg PoolClient instance', function() {

const query = 'SELECT foo FROM bar WHERE id = $1'
const args = ['1']
this.client.query(query, args)

expect(this.poolClient.query).to.have.been.calledOnceWithExactly(query, args)

})

it('returns the result from the pg PoolClient instance', async function() {

const query = 'SELECT foo FROM bar WHERE id = $1'
const args = ['1']
const result = { id: 1 }
this.poolClient.query.resolves(result)

expect(client.dispose).to.be.a('function')
await expect(this.poolClient.query(query, args)).to.become(result)

})

})

it(`the added dispose() function calls the client's release() function`, async function() {
describe('dispose', function() {

const client: any = {
release: stub(),
}
this.pool.connect.resolves(client)
it('calls the release() function of the pg PoolClient instance', function() {

await poolClientFactory(this.pool)
this.client.dispose()

await client.dispose()
expect(this.poolClient.release).to.have.been.calledOnce

expect(client.release).to.have.been.called
})

})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { PgDbPoolClient } from '@dandi-contrib/data-pg'
import { Options, Provider } from '@dandi/core'
import { Options } from '@dandi/core'
import { TestProvider } from '@dandi/core/testing'

import { QueryResult } from 'pg'
import { stub } from 'sinon'

export class PgDbPoolClientFixture implements PgDbPoolClient {
export class PgDbPoolClientFixture extends PgDbPoolClient {

public static result(result: Options<QueryResult> | Error): void {
this.currentResult = result
Expand All @@ -22,13 +22,15 @@ export class PgDbPoolClientFixture implements PgDbPoolClient {
return {
provide: PgDbPoolClient,
useFactory: () => new PgDbPoolClientFixture(),
singleton,
underTest: !singleton,
}
}

private static currentResult: Options<QueryResult> | Error

constructor() {
super(undefined)
stub(this, 'query').callsFake(() => {
if (PgDbPoolClientFixture.currentResult instanceof Error) {
return Promise.reject(PgDbPoolClientFixture.currentResult)
Expand All @@ -38,12 +40,4 @@ export class PgDbPoolClientFixture implements PgDbPoolClient {
stub(this, 'dispose')
}

public query(cmd: string, args: any[]): Promise<QueryResult> {
return undefined
}

public dispose(reason: string): void | Promise<void> {
return undefined
}

}
2 changes: 1 addition & 1 deletion packages/dandi/common/src/async-mutex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class AsyncMutex<T extends object> implements Disposable {
}
}

public async runLocked<TResult>(fn: (lock?: LockedObject<T>) => Promise<TResult>) {
public async runLocked<TResult>(fn: (lock?: LockedObject<T>) => Promise<TResult>): Promise<TResult> {
return Disposable.useAsync(this.getLock(), fn)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/dandi/core/testing/src/test-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class TestHarness implements TestInjector, Disposable {
let repo: Repository
let globalRepoStub: SinonStub

// eslint-disable-next-line typescript/class-name-casing
// eslint-disable-next-line @typescript-eslint/class-name-casing
class __TestSanityChecker {}

beforeEach(function() {
Expand Down Expand Up @@ -108,7 +108,7 @@ export class TestHarness implements TestInjector, Disposable {
}
}

private singletonizeProviders(providers: any[]) {
private singletonizeProviders(providers: any[]): Provider<any>[] {
return providers.map(provider => {
// allow forcing singletons for providers that don't allow singletons
if (!isFactoryProvider(provider) || provider.singleton || (provider as TestProvider<any>).underTest) {
Expand Down

0 comments on commit 997933b

Please sign in to comment.