Skip to content

Commit

Permalink
feat(abstracts): add AbstractMangoRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed May 27, 2021
1 parent ea6708a commit 7ac2164
Show file tree
Hide file tree
Showing 14 changed files with 867 additions and 51 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ module.exports = {
}
},
{
files: ['src/abstracts/__tests__/__fixtures__/cars-finder.fixture.ts'],
files: [
'src/abstracts/__tests__/__fixtures__/cars-finder.fixture.ts',
'src/abstracts/__tests__/__fixtures__/cars-repo.fixture.ts'
],
rules: {
'@typescript-eslint/no-unused-vars': 0
}
Expand Down
4 changes: 2 additions & 2 deletions __tests__/__fixtures__/cars.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Car implements ICar {

export const CARS_UID: CarUID = 'vin'

const CARS_ROOT = {
export const CARS_ROOT = {
'5b38c222-bf0c-4972-9810-d8cd7e399a56': {
make: 'Mitsubishi',
model: '3000GT',
Expand Down Expand Up @@ -79,7 +79,7 @@ export const CARS_MOCK_CACHE: MangoCacheFinder<ICar> = {
collection: Object.freeze(Object.values(CARS_ROOT))
}

export const CARS_FINDER_OPTIONS: MangoFinderOptionsDTO<ICar, CarUID> = {
export const CARS_MANGO_OPTIONS: MangoFinderOptionsDTO<ICar, CarUID> = {
cache: CARS_MOCK_CACHE as MangoFinderOptionsDTO<ICar>['cache'],
mingo: { idKey: CARS_UID }
}
40 changes: 40 additions & 0 deletions src/abstracts/__tests__/__fixtures__/cars-repo.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { CreateEntityDTO, EntityDTO, PatchEntityDTO } from '@/dtos'
import type { UID } from '@/types'
import type { OneOrMany, OrPromise, Path } from '@flex-development/tutils'
import type {
CarParams,
CarQuery,
CarUID,
ICar
} from '@tests/fixtures/cars.fixture'
import AbstractMangoRepository from '../../mango-repo.abstract'

/**
* @file Test Fixture - CarsRepo
* @module abstracts/tests/fixtures/cars-repo.fixture
*/

export default class CarsRepo extends AbstractMangoRepository<
ICar,
CarUID,
CarParams,
CarQuery
> {
create<F extends Path<ICar>>(dto: CreateEntityDTO<ICar, F>): OrPromise<ICar> {
throw new Error('Method not implemented')
}

patch<F extends Path<ICar>>(
uid: UID,
dto: PatchEntityDTO<ICar, F>,
rfields?: string[]
): OrPromise<ICar> {
throw new Error('Method not implemented')
}

save<F extends Path<ICar>>(
dto: OneOrMany<EntityDTO<ICar, F>>
): OrPromise<ICar[]> {
throw new Error('Method not implemented')
}
}
49 changes: 27 additions & 22 deletions src/abstracts/__tests__/mango-finder.abstract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import { PlainObject } from '@flex-development/exceptions/types'
import type { ObjectPlain } from '@flex-development/tutils'
import type { CarUID, ICar } from '@tests/fixtures/cars.fixture'
import {
CARS_FINDER_OPTIONS as OPTIONS,
CARS_MOCK_CACHE_EMPTY,
CARS_UID
CARS_MANGO_OPTIONS as OPTIONS,
CARS_MOCK_CACHE_EMPTY
} from '@tests/fixtures/cars.fixture'
import faker from 'faker'
import TestSubjectAbstract from '../mango-finder.abstract'
Expand All @@ -38,8 +37,8 @@ describe('unit:abstracts/AbstractMangoFinder', () => {
const MOPTIONS = OPTIONS.mingo as MingoOptions<CarUID>

const DOCUMENT = COLLECTION[3]
const UID = DOCUMENT[CARS_UID]
const UIDS = [UID, COLLECTION[0][CARS_UID], COLLECTION[2][CARS_UID]]
const UID = DOCUMENT[MOPTIONS.idKey]
const UIDS = [UID, COLLECTION[0][MOPTIONS.idKey]]
const FUID = `vin-0${faker.datatype.number(5)}`

describe('constructor', () => {
Expand Down Expand Up @@ -77,9 +76,11 @@ describe('unit:abstracts/AbstractMangoFinder', () => {
}

// Expect
expect(exception.code).toBe(ExceptionStatusCode.BAD_REQUEST)
expect(exception.data).toMatchObject({ pipeline: [] })
expect(exception.message).toBe(error_message)
expect(exception.toJSON()).toMatchObject({
code: ExceptionStatusCode.BAD_REQUEST,
data: { pipeline: [] },
message: error_message
})
})

describe('runs pipeline', () => {
Expand Down Expand Up @@ -140,7 +141,7 @@ describe('unit:abstracts/AbstractMangoFinder', () => {

it('should handle query criteria', () => {
// Arrange
const params = { [CARS_UID]: UID }
const params = { [MOPTIONS.idKey]: UID }

// Act
TestSubject.find(params, COLLECTION, MOPTIONS, mockMingo)
Expand All @@ -152,7 +153,7 @@ describe('unit:abstracts/AbstractMangoFinder', () => {

it('should sort results', () => {
// Arrange
const options = { sort: { [CARS_UID]: SortOrder.ASCENDING } }
const options = { sort: { [MOPTIONS.idKey]: SortOrder.ASCENDING } }

// Act
TestSubject.find({ options }, COLLECTION, MOPTIONS, mockMingo)
Expand Down Expand Up @@ -204,9 +205,11 @@ describe('unit:abstracts/AbstractMangoFinder', () => {
}

// Expect
expect(exception.code).toBe(ExceptionStatusCode.BAD_REQUEST)
expect(exception.data).toMatchObject({ params: {} })
expect(exception.message).toBe(error_message)
expect(exception.toJSON()).toMatchObject({
code: ExceptionStatusCode.BAD_REQUEST,
data: { params: {} },
message: error_message
})
})
})

Expand Down Expand Up @@ -273,7 +276,7 @@ describe('unit:abstracts/AbstractMangoFinder', () => {

it('should return document', () => {
// Arrange
const eparams = { [CARS_UID]: UID }
const eparams = { [MOPTIONS.idKey]: UID }
spy_find.mockReturnValueOnce([DOCUMENT] as ObjectPlain[])

// Act
Expand All @@ -287,7 +290,7 @@ describe('unit:abstracts/AbstractMangoFinder', () => {

it('should return null if document is not found', () => {
// Arrange
const eparams = { [CARS_UID]: FUID }
const eparams = { [MOPTIONS.idKey]: FUID }
spy_find.mockReturnValueOnce([])

// Act
Expand Down Expand Up @@ -328,10 +331,12 @@ describe('unit:abstracts/AbstractMangoFinder', () => {
}

// Expect
expect(exception.code).toBe(ExceptionStatusCode.NOT_FOUND)
expect(exception.data).toMatchObject({ params: {} })
expect((exception.errors as ObjectPlain)[CARS_UID]).toBe(FUID)
expect(exception.message).toMatch(new RegExp(`"${FUID}" does not exist`))
expect(exception.toJSON()).toMatchObject({
code: ExceptionStatusCode.NOT_FOUND,
data: { params: {} },
errors: { [MOPTIONS.idKey]: FUID },
message: `Document with ${MOPTIONS.idKey} "${FUID}" does not exist`
})
})
})

Expand Down Expand Up @@ -455,7 +460,7 @@ describe('unit:abstracts/AbstractMangoFinder', () => {
const spy_findOne = jest.spyOn(TestSubjectAbstract, 'findOne')

beforeEach(() => {
Subject.queryOne(Subject.cache.collection[0][CARS_UID])
Subject.queryOne(Subject.cache.collection[0][MOPTIONS.idKey])
})

it('should call #mparser.params', () => {
Expand All @@ -476,7 +481,7 @@ describe('unit:abstracts/AbstractMangoFinder', () => {

beforeEach(() => {
spy_findOneOrFail.mockReturnValueOnce(DOCUMENT as ObjectPlain)
Subject.queryOneOrFail(DOCUMENT[CARS_UID])
Subject.queryOneOrFail(DOCUMENT[MOPTIONS.idKey])
})

it('should call #mparser.params', () => {
Expand Down Expand Up @@ -517,7 +522,7 @@ describe('unit:abstracts/AbstractMangoFinder', () => {

describe('#uid', () => {
it('should return name of document uid field', () => {
expect(Subject.uid()).toBe(CARS_UID)
expect(Subject.uid()).toBe(MOPTIONS.idKey)
})
})
})
Loading

0 comments on commit 7ac2164

Please sign in to comment.