Skip to content

Commit

Permalink
test: try to fix codecov coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fasenderos committed Sep 18, 2023
1 parent f8c3228 commit fc7ebf3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 132 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ jobs:
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./packages/api-gateway/coverage/lcov.info
files: ./packages/api-gateway/coverage/clover.xml
fail_ci_if_error: true
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<p align="center">
<a href="https://github.com/fasenderos/bitify/blob/main/LICENSE" target="_blank"><img src="https://img.shields.io/badge/License-Apache_2.0-blue.svg" alt="Package License"></a>
<a href="https://codecov.io/github/fasenderos/bitify" target="_blank"><img src="https://img.shields.io/codecov/c/github/fasenderos/bitify" alt="Codecov"></a>
<a href="https://github.com/fasenderos/bitify"><img src="https://badgen.net/badge/icon/typescript?icon=typescript&label" alt="Built with TypeScript"></a>
</p>

> <b>WARNING</b>: This software is not ready yet, please don't use in production. There are a [lot of things to do](#to-do) and is under active development. APIs and table schemas are subject to change without notice. Technical support is unavailable at this time.
<h3 align="center">Bitify Trading Platform</h3>
Expand Down
2 changes: 1 addition & 1 deletion packages/api-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"lint:fix": "eslint --fix",
"pretest": "node ./test/before-all-tests.js",
"test": "tap --ts",
"test:ci": "pnpm pretest && tap --ts --coverage --coverage-report=lcov",
"test:ci": "tap --ts --coverage --coverage-report=clover",
"test:cov": "pnpm pretest && tap --ts --coverage",
"test:dev": "pnpm pretest && tap --ts --watch --coverage"
},
Expand Down
33 changes: 1 addition & 32 deletions packages/api-gateway/src/base/base.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import {
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
import { BaseEntity } from './base.entity';
import { IBaseService } from './interfaces/base-service.interface';
import {
InternalServerErrorException,
UnprocessableEntityException,
} from '@nestjs/common';
import { UnprocessableEntityException } from '@nestjs/common';

export abstract class BaseService<
Entity extends BaseEntity,
Expand All @@ -27,10 +24,6 @@ export abstract class BaseService<
* @returns The entity
*/
createEntity(data: CreateDTO, userId: string): Entity {
if (!data || !userId)
throw new InternalServerErrorException(
'Missing data or userId for createEntity method',
);
// Instantiating the entity before saving so hooks run
return this.repo.create({ ...data, userId: userId });
}
Expand All @@ -41,8 +34,6 @@ export abstract class BaseService<
* @returns The created resource
*/
save(data: Entity): Promise<Entity> {
if (!data)
throw new InternalServerErrorException('Missing data for save method');
return this.repo.save(data);
}

Expand All @@ -66,10 +57,6 @@ export abstract class BaseService<
filter: FindOptionsWhere<Entity>,
unselected = false,
): Promise<Entity | null> {
if (!filter)
throw new InternalServerErrorException(
'Missing filter for findOne method',
);
if (unselected === true) {
return this.repo.findOne({
select: this.getAllTableColumns(),
Expand All @@ -86,8 +73,6 @@ export abstract class BaseService<
* @returns The entity that match the conditions or null.
*/
findById(id: string, unselected = false): Promise<Entity | null> {
if (!id)
throw new InternalServerErrorException('Missing id for findById method');
return this.findOne({ id } as FindOptionsWhere<Entity>, unselected);
}

Expand All @@ -101,10 +86,6 @@ export abstract class BaseService<
filter: FindOptionsWhere<Entity>,
data: UpdateDTO,
): Promise<void> {
if (!filter || !data)
throw new InternalServerErrorException(
'Missing filter or data for update method',
);
// @ts-expect-error Dto should not have userId, but we check anyway at runtime
if (data.userId)
throw new UnprocessableEntityException('Ownership can not be changed');
Expand All @@ -124,10 +105,6 @@ export abstract class BaseService<
data: UpdateDTO,
userId?: string,
): Promise<void> {
if (!id || !data)
throw new InternalServerErrorException(
'Missing id or data for updateById method',
);
// @ts-expect-error Dto should not have userId, but we check anyway at runtime
if (data.userId)
throw new UnprocessableEntityException('Ownership can not be changed');
Expand All @@ -149,10 +126,6 @@ export abstract class BaseService<
* @param {boolean} soft When true a soft delete is performed otherwise a real delete.
*/
async delete(filter: FindOptionsWhere<Entity>, soft = true): Promise<void> {
if (!filter)
throw new InternalServerErrorException(
'Missing filter for delete method',
);
await this.repo[soft ? 'softDelete' : 'delete'](filter);
}

Expand All @@ -165,10 +138,6 @@ export abstract class BaseService<
* @param {boolean} soft When true a soft delete is performed otherwise a real delete.
*/
async deleteById(id: string, userId?: string, soft = true): Promise<void> {
if (!id)
throw new InternalServerErrorException(
'Missing id for deleteById method',
);
await this.repo[soft ? 'softDelete' : 'delete']({
id,
...(userId ? { userId: userId } : {}),
Expand Down
100 changes: 2 additions & 98 deletions packages/api-gateway/test/base/base-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { test } from 'tap';
import { buildServer, createUser } from '../helper';
import {
InternalServerErrorException,
UnprocessableEntityException,
} from '@nestjs/common';
import { UnprocessableEntityException } from '@nestjs/common';
import { RecoveryTokensService } from '../../src/recovery-tokens/recovery-tokens.service';
import { RecoveryToken } from '../../src/recovery-tokens/entities/recovery-token.entity';
import { randomUUID } from 'crypto';
Expand All @@ -25,31 +22,11 @@ test('base service creatEntity() and save() method', async ({
token: 'somerecoverytoken',
expiresAt: new Date(),
};
// Test createEntity() method without required params
try {
// @ts-expect-error we now that userId is required
service.createEntity(mockBody);
} catch (error) {
equal(error instanceof InternalServerErrorException, true);
}
try {
// @ts-expect-error we now that dto is required
service.createEntity();
} catch (error) {
equal(error instanceof InternalServerErrorException, true);
}

// Test createEntity() method with right params
const tokenEntity = service.createEntity(mockBody, user.id);
equal(tokenEntity instanceof RecoveryToken, true);

// Test save() method without required param
try {
// @ts-expect-error we now that dto is required
service.save();
} catch (error) {
equal(error instanceof InternalServerErrorException, true);
}

// test save() method with right param
const token = await service.save(tokenEntity);
equal(token instanceof RecoveryToken, true);
Expand Down Expand Up @@ -107,16 +84,6 @@ test('base service findOne() method', async ({ equal, teardown }) => {
const tokenEntity = service.createEntity(mockBody, user.id);
const token = await service.save(tokenEntity);

// Test findOne() method without params
{
try {
// @ts-expect-error we now that a filter is required in findOne
await service.findOne();
} catch (error) {
equal(error instanceof InternalServerErrorException, true);
}
}

// Test findOne() method invalid ID
{
const res = await service.findOne({ id: randomUUID() });
Expand Down Expand Up @@ -145,16 +112,6 @@ test('base service findById() method', async ({ equal, teardown }) => {
const tokenEntity = service.createEntity(mockBody, user.id);
const token = await service.save(tokenEntity);

// Test findById() method without params
{
try {
// @ts-expect-error we now that id is required
await service.findById();
} catch (error) {
equal(error instanceof InternalServerErrorException, true);
}
}

// Test findById() method invalid ID
{
const res = await service.findById(randomUUID());
Expand Down Expand Up @@ -183,23 +140,6 @@ test('base service update() method', async ({ equal, teardown }) => {
const tokenEntity = service.createEntity(mockBody, user.id);
const token = await service.save(tokenEntity);

// Test update() method without params
{
try {
// @ts-expect-error we now that filter is required
await service.update();
} catch (error) {
equal(error instanceof InternalServerErrorException, true);
}

try {
// @ts-expect-error we now that dto is required
await service.update({ id: token.id });
} catch (error) {
equal(error instanceof InternalServerErrorException, true);
}
}

// Test update() method with right params
{
await service.update({ id: token.id }, { token: 'newtokenUpdate' });
Expand Down Expand Up @@ -232,22 +172,6 @@ test('base service updateById() method', async ({ equal, teardown }) => {
};
const tokenEntity = service.createEntity(mockBody, user.id);
const token = await service.save(tokenEntity);
// Test updateById() method without required params
{
try {
// @ts-expect-error we now that filter is required
await service.updateById();
} catch (error) {
equal(error instanceof InternalServerErrorException, true);
}

try {
// @ts-expect-error we now that dto is required
await service.updateById(token.id);
} catch (error) {
equal(error instanceof InternalServerErrorException, true);
}
}

// Test updatedById() method with right params
{
Expand Down Expand Up @@ -296,16 +220,6 @@ test('base service delete() method', async ({ equal, teardown }) => {
const tokenEntity = service.createEntity(mockBody, user.id);
const token = await service.save(tokenEntity);

// Test delete() method without params
{
try {
// @ts-expect-error we now that filter is required
await service.delete();
} catch (error) {
equal(error instanceof InternalServerErrorException, true);
}
}

// Test delete() method with right params
{
await service.delete({ id: token.id });
Expand Down Expand Up @@ -349,16 +263,6 @@ test('base service deleteById() method', async ({ equal, teardown }) => {
const tokenEntity = service.createEntity(mockBody, user.id);
const token = await service.save(tokenEntity);

// Test deleteById() method without required params
{
try {
// @ts-expect-error we now that filter is required
await service.deleteById();
} catch (error) {
equal(error instanceof InternalServerErrorException, true);
}
}

// Test deleteById() method without passing userId
{
await service.deleteById(token.id);
Expand Down

0 comments on commit fc7ebf3

Please sign in to comment.