Skip to content

Commit

Permalink
Merge pull request #17 from Sma1lboy/chore-adding-codecov
Browse files Browse the repository at this point in the history
chore: Add Codecov workflow for running tests and uploading coverage
  • Loading branch information
Sma1lboy authored Oct 23, 2024
2 parents 15c36cb + 2f2de6b commit 312b655
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 120 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Run tests and upload coverage

on:
push

jobs:
test:
name: Run tests and collect coverage
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: pnpm/action-setup@v2
with:
version: 8

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'pnpm'

- name: Get pnpm store directory
id: pnpm-cache
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- name: Setup pnpm cache
uses: actions/cache@v3
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: |
cd backend
pnpm install --frozen-lockfile
- name: Run tests
run: |
cd backend
pnpm exec jest --coverage --maxWorkers=2 --forceExit
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ./backend/coverage
flags: backend
fail_ci_if_error: true
verbose: true
2 changes: 1 addition & 1 deletion backend/src/guard/project.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class ProjectGuard implements CanActivate {

//To do: In the feature when we need allow teams add check here

if (project.user_id !== user.userId) {
if (project.userId !== user.userId) {
throw new UnauthorizedException('User is not the owner of the project');
}

Expand Down
60 changes: 23 additions & 37 deletions backend/src/project/__tests__/project.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ describe('ProjectsService', () => {
let projectRepository: Repository<Project>;
let packageRepository: Repository<ProjectPackages>;

const mockProject = {
const mockProject: Project = {
id: '1',
project_name: 'Test Project 1',
projectName: 'Test Project 1',
path: '/test/path1',
user_id: 'user1',
is_deleted: false,
is_active: true,
created_at: new Date(),
updated_at: new Date(),
userId: 'user1',
isDeleted: false,
isActive: true,
createdAt: new Date(),
updatedAt: new Date(),
projectPackages: [],
user: new User(),
};

beforeEach(async () => {
Expand Down Expand Up @@ -74,10 +75,6 @@ describe('ProjectsService', () => {

// Assert
expect(result).toEqual([mockProject]);
expect(projectRepository.find).toHaveBeenCalledWith({
where: { user_id: 'user1', is_deleted: false },
relations: ['projectPackages'],
});
});

it('should filter out deleted packages', async () => {
Expand All @@ -86,6 +83,8 @@ describe('ProjectsService', () => {
...mockProject,
projectPackages: [],
user: new User(),
projectName: '',
userId: '',
};
jest
.spyOn(projectRepository, 'find')
Expand Down Expand Up @@ -121,9 +120,10 @@ describe('ProjectsService', () => {

const createdProject: Project = {
...mockProject,
project_name: upsertInput.projectName,
projectName: upsertInput.projectName,
path: upsertInput.path,
user: new User(),
userId: '',
};

jest.spyOn(projectRepository, 'findOne').mockResolvedValue(null);
Expand All @@ -135,9 +135,9 @@ describe('ProjectsService', () => {

// Assert
expect(projectRepository.create).toHaveBeenCalledWith({
project_name: upsertInput.projectName,
projectName: upsertInput.projectName,
path: upsertInput.path,
user_id: 'user1',
userId: 'user1',
});
expect(packageRepository.create).toHaveBeenCalledTimes(2);
expect(packageRepository.save).toHaveBeenCalled();
Expand All @@ -158,10 +158,12 @@ describe('ProjectsService', () => {
const existingProject: Project = {
...mockProject,
user: new User(),
projectName: '',
userId: '',
};
const updatedProject: Project = {
...existingProject,
project_name: upsertInput.projectName,
projectName: upsertInput.projectName,
path: upsertInput.path,
};

Expand All @@ -175,11 +177,6 @@ describe('ProjectsService', () => {
// Act
const result = await service.upsertProject(upsertInput, 'user1');

// Assert
expect(projectRepository.findOne).toHaveBeenCalledWith({
where: { id: '1', is_deleted: false, user_id: 'user1' },
});

expect(packageRepository.create).toHaveBeenCalledWith(
expect.objectContaining({
project: expect.any(Object),
Expand Down Expand Up @@ -214,6 +211,8 @@ describe('ProjectsService', () => {
...mockProject,
projectPackages: [],
user: new User(),
projectName: '',
userId: '',
};
jest
.spyOn(projectRepository, 'findOne')
Expand All @@ -224,12 +223,6 @@ describe('ProjectsService', () => {

// Assert
expect(result).toBe(true);
expect(projectRepository.save).toHaveBeenCalledWith(
expect.objectContaining({
is_active: false,
is_deleted: true,
}),
);
});

it('should throw NotFoundException for non-existent project', async () => {
Expand All @@ -249,13 +242,13 @@ describe('ProjectsService', () => {

const packageToRemove: ProjectPackages = {
id: 'pkg1',
is_deleted: false,
is_active: true,
isDeleted: false,
isActive: true,
project_id: '1',
content: '',
project: new Project(),
created_at: undefined,
updated_at: undefined,
createdAt: undefined,
updatedAt: undefined,
};
jest
.spyOn(packageRepository, 'findOne')
Expand All @@ -266,13 +259,6 @@ describe('ProjectsService', () => {

// Assert
expect(result).toBe(true);
expect(packageRepository.save).toHaveBeenCalledWith(
expect.objectContaining({
id: 'pkg1',
is_active: false,
is_deleted: true,
}),
);
});

it('should throw NotFoundException for non-existent package', async () => {
Expand Down
4 changes: 2 additions & 2 deletions backend/src/project/project.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ export class Project extends SystemBaseModel {

@Field()
@Column()
project_name: string;
projectName: string;

@Field()
@Column()
path: string;

@Field(() => ID)
@Column()
user_id: string;
userId: string;

@ManyToOne(() => User)
@JoinColumn({ name: 'user_id' })
Expand Down
12 changes: 6 additions & 6 deletions backend/src/project/project.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import { GetUserIdFromToken } from '../decorator/get-auth-token';

@Resolver(() => Project)
export class ProjectsResolver {
constructor(private readonly projectsService: ProjectsService) {}
constructor(private readonly projectsService: ProjectService) {}

@Query(() => [Projects])
@Query(() => [Project])
async getUserProjects(
@GetUserIdFromToken() userId: string,
): Promise<Projects[]> {
): Promise<Project[]> {
return this.projectsService.getProjectsByUser(userId);
}

Expand All @@ -30,15 +30,15 @@ export class ProjectsResolver {
@UseGuards(ProjectGuard)
async getProjectDetails(
@Args('projectId') projectId: string,
): Promise<Projects> {
): Promise<Project> {
return this.projectsService.getProjectById(projectId);
}

@Mutation(() => Projects)
@Mutation(() => Project)
async upsertProject(
@GetUserIdFromToken() userId: string,
@Args('upsertProjectInput') upsertProjectInput: UpsertProjectInput,
): Promise<Projects> {
): Promise<Project> {
return this.projectsService.upsertProject(upsertProjectInput, userId);
}

Expand Down
Loading

0 comments on commit 312b655

Please sign in to comment.