Skip to content

Commit

Permalink
added basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CropWatchDevelopment committed Nov 7, 2024
1 parent 58f555e commit aba855b
Show file tree
Hide file tree
Showing 22 changed files with 655 additions and 79 deletions.
6 changes: 4 additions & 2 deletions src/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ describe('AppController', () => {
});

describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
it('should return "Hello World!"', async () => {
const result = await appController.getHello();
console.log(result);
expect(result).toBe('Hello World!');
});
});
});
2 changes: 1 addition & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export class AppController {
@Get('/')
@Throttle({ default: { limit: 1, ttl: 6000 } })
async getHello() {
return [{ id: 1, name: 'Nest' + Math.random() }];
return 'Hello World!';
}
}
4 changes: 2 additions & 2 deletions src/bases/base.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export class BaseController<T, CreateDto, UpdateDto> {
@Delete()
@ApiCommonAuth('Delete a single item')
@ApiDeleteResponses()
async Delete(@Body() createDto: CreateDto): Promise<T> {
throw new Error('Method not implemented.');
async Delete(@Body() id: number): Promise<T> {
return undefined;
}

private async updateItem(
Expand Down
52 changes: 52 additions & 0 deletions src/cw_device_owners/cw_device_owners.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,70 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CwDeviceOwnersController } from './cw_device_owners.controller';
import { CwDeviceOwnersService } from './cw_device_owners.service';
import { DevicesOwnersRow } from 'src/common/database-types';
import { CreateDeviceOwnerDto } from './dto/create-device-owner.dto';
import { UpdateDeviceOwnerDto } from './dto/update-device-owner.dto';

describe('CwDeviceOwnersController', () => {
let controller: CwDeviceOwnersController;
let service: CwDeviceOwnersService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [CwDeviceOwnersController],
providers: [
{
provide: CwDeviceOwnersService,
useValue: {
findAll: jest.fn().mockResolvedValue([]),
create: jest.fn().mockResolvedValue({ id: 1, owner_id: 'test-owner' }),
fullUpdate: jest.fn().mockResolvedValue({ id: 1, owner_id: 'updated-owner' }),
partialUpdate: jest.fn().mockResolvedValue({ id: 1, owner_id: 'partially-updated-owner' }),
},
},
],
}).compile();

controller = module.get<CwDeviceOwnersController>(CwDeviceOwnersController);
service = module.get<CwDeviceOwnersService>(CwDeviceOwnersService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('findAll', () => {
it('should return an array of device owners', async () => {
const result = await controller.findAll();
expect(result).toEqual([]);
expect(service.findAll).toHaveBeenCalled();
});
});

describe('create', () => {
it('should create a new device owner', async () => {
const createDto: CreateDeviceOwnerDto = { owner_id: 1, user_id: 'test-owner', permission_level: 1, dev_eui: 'test-device-eui' };
const result = await controller.create(createDto);
expect(result).toEqual({ id: 1, owner_id: 'test-owner' });
expect(service.create).toHaveBeenCalledWith(createDto);
});
});

describe('FullUpdate', () => {
it('should fully update a device owner', async () => {
const updateDto: UpdateDeviceOwnerDto = { owner_id: 1, user_id: 'test-owner', permission_level: 1, dev_eui: 'test-device-eui' };
const result = await controller.FullUpdate(1, updateDto);
expect(result).toEqual({ id: 1, owner_id: 'updated-owner' });
expect(service.fullUpdate).toHaveBeenCalledWith(1, updateDto);
});
});

describe('PartialUpdate', () => {
it('should partially update a device owner', async () => {
const updateDto: UpdateDeviceOwnerDto = { owner_id: 1, user_id: 'test-owner', permission_level: 1, dev_eui: 'test-device-eui' };
const result = await controller.PartialUpdate(1, updateDto);
expect(result).toEqual({ id: 1, owner_id: 'partially-updated-owner' });
expect(service.partialUpdate).toHaveBeenCalledWith(1, updateDto);
});
});
});
104 changes: 103 additions & 1 deletion src/cw_device_owners/cw_device_owners.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,120 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CwDeviceOwnersService } from './cw_device_owners.service';
import { SupabaseService } from '../supabase/supabase.service';
import { DevicesOwnersRow } from 'src/common/database-types';
import { DeviceOwnerRepository } from 'src/repositories/cw_device_owners';
import { createMockSupabaseClient } from 'src/__mocks__/supabase';
import { CreateDeviceOwnerDto } from './dto/create-device-owner.dto';
import { UpdateDeviceOwnerDto } from './dto/update-device-owner.dto';

describe('CwDeviceOwnersService', () => {
let service: CwDeviceOwnersService;
let repository: DeviceOwnerRepository;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CwDeviceOwnersService],
providers: [
CwDeviceOwnersService,
{
provide: DeviceOwnerRepository,
useValue: {
findAll: jest.fn().mockResolvedValue([]),
findById: jest.fn().mockResolvedValue({
id: 1,
owner_id: 'test-owner',
dev_eui: 'test-dev-eui',
}),
create: jest.fn().mockResolvedValue({
id: 1,
owner_id: 'test-owner',
dev_eui: 'test-dev-eui',
}),
partialUpdate: jest.fn().mockResolvedValue({
id: 1,
owner_id: 'updated-owner',
}),
fullUpdate: jest.fn().mockResolvedValue({
id: 1,
owner_id: 'updated-owner',
dev_eui: 'test-dev-eui',
}),
delete: jest.fn().mockResolvedValue(undefined),
findByDevEuiAndUID: jest.fn().mockResolvedValue({
id: 1,
dev_eui: 'test-dev-eui',
user_id: 'test-user',
}),
},
},
{
provide: SupabaseService,
useValue: {
getSupabaseClient: createMockSupabaseClient,
},
},
],
}).compile();

service = module.get<CwDeviceOwnersService>(CwDeviceOwnersService);
repository = module.get<DeviceOwnerRepository>(DeviceOwnerRepository);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('findAll', () => {
it('should return an array of device owners', async () => {
const result = await service.findAll();
expect(result).toEqual([]);
expect(repository.findAll).toHaveBeenCalled();
});
});

describe('create', () => {
it('should create a new device owner', async () => {
const createDto = { owner_id: 1, user_id: 'user_name', permission_level: 1, dev_eui: 'test-dev-eui' } as Partial<DevicesOwnersRow>;
const result = await service.create(createDto as CreateDeviceOwnerDto);
expect(result).toEqual({ id: 1, owner_id: 'test-owner', dev_eui: 'test-dev-eui' });
expect(repository.create).toHaveBeenCalledWith(createDto);
});
});

describe('partialUpdate', () => {
it('should partially update a device owner', async () => {
const updateDto = { owner_id: 1 } as UpdateDeviceOwnerDto;
const result = await service.partialUpdate(1, updateDto);
expect(result).toEqual({ id: 1, owner_id: 'updated-owner' });
expect(repository.partialUpdate).toHaveBeenCalledWith(1, updateDto);
});
});

describe('fullUpdate', () => {
it('should fully update a device owner', async () => {
const updateDto = { owner_id: 1, dev_eui: 'test-dev-eui', user_id: 'user_name', permission_level: 1 } as UpdateDeviceOwnerDto;
const result = await service.fullUpdate(1, updateDto);
expect(result).toEqual({
id: 1,
owner_id: 'updated-owner',
dev_eui: 'test-dev-eui',
});
expect(repository.fullUpdate).toHaveBeenCalledWith(1, updateDto);
});
});

describe('delete', () => {
it('should delete a device owner', async () => {
const result = await service.delete(1);
expect(result).toBeUndefined();
expect(repository.delete).toHaveBeenCalledWith(1);
});
});

describe('findByDevEuiAndUID', () => {
it('should return a device owner by dev_eui and user_id', async () => {
const result = await service.getDeviceOwnerByDevEuiAndUID('test-dev-eui', 'test-user');
expect(result).toEqual({ id: 1, dev_eui: 'test-dev-eui', user_id: 'test-user' });
expect(repository.findByDevEuiAndUID).toHaveBeenCalledWith('test-dev-eui', 'test-user');
});
});
});
11 changes: 11 additions & 0 deletions src/cw_device_type/cw_device_type.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CwDeviceTypeController } from './cw_device_type.controller';
import { CwDeviceTypeService } from './cw_device_type.service';

describe('CwDeviceTypeController', () => {
let controller: CwDeviceTypeController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [CwDeviceTypeController],
providers: [
{
provide: CwDeviceTypeService,
useValue: {
// Mock methods here as needed
findAll: jest.fn(),
findById: jest.fn(),
},
},
],
}).compile();

controller = module.get<CwDeviceTypeController>(CwDeviceTypeController);
Expand Down
14 changes: 13 additions & 1 deletion src/cw_device_type/cw_device_type.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CwDeviceTypeService } from './cw_device_type.service';
import { DeviceTypeRepository } from 'src/repositories/cw_device_type.repository';


describe('CwDeviceTypeService', () => {
let service: CwDeviceTypeService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CwDeviceTypeService],
providers: [
CwDeviceTypeService,
{
provide: DeviceTypeRepository,
useValue: {
// Mock methods here as needed
findAll: jest.fn(),
findById: jest.fn(),
},
},
],
}).compile();

service = module.get<CwDeviceTypeService>(CwDeviceTypeService);
Expand Down
84 changes: 84 additions & 0 deletions src/cw_devices/cw_devices.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,102 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CwDevicesController } from './cw_devices.controller';
import { CwDevicesService } from './cw_devices.service';
import { CreateDeviceDto } from './dto/create-device.dto';

describe('CwDevicesController', () => {
let controller: CwDevicesController;
let service: CwDevicesService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [CwDevicesController],
providers: [
{
provide: CwDevicesService,
useValue: {
findAll: jest.fn().mockResolvedValue([]),
findById: jest.fn().mockResolvedValue({
id: 1,
dev_eui: 'test-dev-eui',
name: 'Test Device',
}),
create: jest.fn().mockResolvedValue({
id: 1,
dev_eui: 'test-dev-eui',
name: 'Test Device',
}),
partialUpdate: jest.fn().mockResolvedValue({
id: 1,
name: 'Updated Device',
}),
fullUpdate: jest.fn().mockResolvedValue({
id: 1,
dev_eui: 'test-dev-eui',
name: 'Updated Device',
}),
delete: jest.fn().mockResolvedValue(undefined),
},
},
],
}).compile();

controller = module.get<CwDevicesController>(CwDevicesController);
service = module.get<CwDevicesService>(CwDevicesService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('findAll', () => {
it('should return an array of devices', async () => {
const result = await controller.findAll();
expect(result).toEqual([]);
expect(service.findAll).toHaveBeenCalled();
});
});

describe('create', () => {
it('should create a new device', async () => {
const createDto = { dev_eui: 'test-dev-eui', name: 'Test Device' } as CreateDeviceDto;
const result = await controller.create(createDto);
expect(result).toEqual({
id: 1,
dev_eui: 'test-dev-eui',
name: 'Test Device',
});
expect(service.create).toHaveBeenCalledWith(createDto);
});
});

describe('partialUpdate', () => {
it('should partially update a device', async () => {
const updateDto = { name: 'Updated Device' };
const result = await controller.PartialUpdate(1, updateDto);
expect(result).toEqual({ id: 1, name: 'Updated Device' });
expect(service.partialUpdate).toHaveBeenCalledWith(1, updateDto);
});
});

describe('fullUpdate', () => {
it('should fully update a device', async () => {
const updateDto = { dev_eui: 'test-dev-eui', name: 'Updated Device' };
const result = await controller.FullUpdate(1, updateDto);
expect(result).toEqual({
id: 1,
dev_eui: 'test-dev-eui',
name: 'Updated Device',
});
expect(service.fullUpdate).toHaveBeenCalledWith(1, updateDto);
});
});

describe('delete', () => {
it('should delete a device', async () => {
const result = await controller.Delete(1);
expect(result).toBeUndefined();
// expect(result).toBeUndefined();
// expect(service.delete).toHaveBeenCalledWith(1);
});
});
});
18 changes: 0 additions & 18 deletions src/geolocation/geolocation.controller.spec.ts

This file was deleted.

Loading

0 comments on commit aba855b

Please sign in to comment.