Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
goemen committed May 10, 2024
1 parent 194e3ff commit f30c951
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 190 deletions.
85 changes: 33 additions & 52 deletions backend/src/v1/routes/external-consumer-routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import router from './external-consumer-routes';
import { faker } from '@faker-js/faker';

const mockCount = jest.fn();
const mockFindMany = jest.fn();
const mockQueryRaw = jest.fn();
jest.mock('../prisma/prisma-client-readonly-replica', () => {
return {
__esModule: true,
default: {
$replica: () => {
return {
pay_transparency_report: {
count: () => mockCount(),
findMany: (...args) => mockFindMany(...args),
},
$queryRaw: (...args) => mockQueryRaw(...args),
};
},
},
Expand All @@ -34,31 +31,21 @@ const REPORT = {
report_start_date: faker.date.past(),
report_end_date: faker.date.past(),
report_status: 'Published',
pay_transparency_company: {
company_name: faker.company.name(),
province: faker.location.state(),
bceid_business_guid: faker.string.uuid(),
country: faker.location.country(),
city: faker.location.city(),
postal_code: faker.location.zipCode(),
address_line1: faker.location.streetAddress(),
address_line2: faker.location.streetAddress(),
},
employee_count_range: {
employee_count_range: '50-299',
},
naics_code_pay_transparency_report_naics_codeTonaics_code: {
naics_code: '11',
naics_label: faker.lorem.words(3),
},
pay_transparency_calculated_data: [
{
value: faker.number.float(),
is_suppressed: false,
calculation_code: { calculation_code: `${faker.number.int()}` },
},
],
report_history: [],
company_name: faker.company.name(),
province: faker.location.state(),
bceid_business_guid: faker.string.uuid(),
country: faker.location.country(),
city: faker.location.city(),
postal_code: faker.location.zipCode(),
address_line1: faker.location.streetAddress(),
address_line2: faker.location.streetAddress(),

employee_count_range: '50-299',
naics_label: faker.lorem.words(3),

value: faker.number.float(),
is_suppressed: false,
calculation_code: faker.number.int(),
};

describe('external-consumer-routes', () => {
Expand All @@ -71,52 +58,46 @@ describe('external-consumer-routes', () => {
describe('/ GET', () => {
it('should return data if user doeas not send query params', () => {
mockCount.mockReturnValue(1);
mockFindMany.mockReturnValue([REPORT]);
mockQueryRaw.mockReturnValue([REPORT]);
return request(app)
.get('')
.expect(200)
.expect(({ body }) => {
expect(body).toEqual({
page: 0,
pageSize: 1000,
history: [],
records: [
{
calculated_data: [
{
calculation_code:
REPORT.pay_transparency_calculated_data[0]
.calculation_code.calculation_code,
REPORT.calculation_code,
is_suppressed:
REPORT.pay_transparency_calculated_data[0].is_suppressed,
value: REPORT.pay_transparency_calculated_data[0].value,
REPORT.is_suppressed,
value: REPORT.value,
},
],
company_address_line1:
REPORT.pay_transparency_company.address_line1,
REPORT.address_line1,
company_address_line2:
REPORT.pay_transparency_company.address_line2,
REPORT.address_line2,
company_bceid_business_guid:
REPORT.pay_transparency_company.bceid_business_guid,
company_city: REPORT.pay_transparency_company.city,
company_country: REPORT.pay_transparency_company.country,
REPORT.bceid_business_guid,
company_city: REPORT.city,
company_country: REPORT.country,
company_id: REPORT.company_id,
company_name: REPORT.pay_transparency_company.company_name,
company_name: REPORT.company_name,
company_postal_code:
REPORT.pay_transparency_company.postal_code,
company_province: REPORT.pay_transparency_company.province,
REPORT.postal_code,
company_province: REPORT.province,
create_date: REPORT.create_date.toISOString(),
data_constraints: REPORT.data_constraints,
employee_count_range:
REPORT.employee_count_range.employee_count_range,
REPORT.employee_count_range,
naics_code:
REPORT
.naics_code_pay_transparency_report_naics_codeTonaics_code
.naics_code,
REPORT.naics_code,
naics_code_label:
REPORT
.naics_code_pay_transparency_report_naics_codeTonaics_code
.naics_label,
REPORT.naics_label,
report_end_date: REPORT.report_end_date.toISOString(),
report_id: REPORT.report_id,
report_start_date: REPORT.report_start_date.toISOString(),
Expand All @@ -141,7 +122,7 @@ describe('external-consumer-routes', () => {
});
});
it('should fail if request fails to get reports', () => {
mockFindMany.mockRejectedValue({});
mockQueryRaw.mockRejectedValue({});
return request(app).get('').expect(200);
});
});
Expand Down
174 changes: 36 additions & 138 deletions backend/src/v1/services/external-consumer-service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { faker } from '@faker-js/faker';
import { externalConsumerService } from './external-consumer-service';
const mockCount = jest.fn();
const mockFindMany = jest.fn();

const mockQueryRaw = jest.fn();
jest.mock('../prisma/prisma-client-readonly-replica', () => {
return {
__esModule: true,
default: {
$replica: () => {
return {
pay_transparency_report: {
count: () => mockCount(),
findMany: (...args) => mockFindMany(...args),
},
$queryRaw: (...args) => mockQueryRaw(...args),
};
},
},
Expand All @@ -30,69 +27,20 @@ const testData = {
report_start_date: faker.date.past(),
report_end_date: faker.date.past(),
report_status: 'Published',
pay_transparency_company: {
company_name: faker.company.name(),
province: faker.location.state(),
bceid_business_guid: faker.string.uuid(),
country: faker.location.country(),
city: faker.location.city(),
postal_code: faker.location.zipCode(),
address_line1: faker.location.streetAddress(),
address_line2: faker.location.streetAddress(),
},
employee_count_range: {
employee_count_range: '50-299',
},
naics_code_pay_transparency_report_naics_codeTonaics_code: {
naics_code: '11',
naics_label: faker.lorem.words(3),
},
pay_transparency_calculated_data: [
{
value: faker.number.float(),
is_suppressed: false,
calculation_code: { calculation_code: `${faker.number.int()}` },
},
],
report_history: [
{
report_id: faker.string.uuid(),
company_id: faker.string.uuid(),
naics_code: '11',
create_date: faker.date.past(),
update_date: faker.date.past(),
data_constraints: faker.lorem.sentence(),
user_comment: faker.lorem.sentence(),
revision: '12',
report_start_date: faker.date.past(),
report_end_date: faker.date.past(),
report_status: 'Published',
pay_transparency_company: {
company_name: faker.company.name(),
province: faker.location.state(),
bceid_business_guid: faker.string.uuid(),
country: faker.location.country(),
city: faker.location.city(),
postal_code: faker.location.zipCode(),
address_line1: faker.location.streetAddress(),
address_line2: faker.location.streetAddress(),
},
employee_count_range: {
employee_count_range: '50-299',
},
naics_code_report_history_naics_codeTonaics_code: {
naics_code: '11',
naics_label: faker.lorem.words(3),
},
calculated_data_history: [
{
value: faker.number.float(),
is_suppressed: false,
calculation_code: { calculation_code: `${faker.number.int()}` },
},
],
},
],
reporting_year: 2024,
company_name: faker.company.name(),
province: faker.location.state(),
bceid_business_guid: faker.string.uuid(),
country: faker.location.country(),
city: faker.location.city(),
postal_code: faker.location.zipCode(),
address_line1: faker.location.streetAddress(),
address_line2: faker.location.streetAddress(),
employee_count_range: '50-299',
naics_label: faker.lorem.words(3),
value: faker.number.float(),
is_suppressed: false,
calculation_code: faker.number.int()
};

describe('external-consumer-service', () => {
Expand All @@ -101,99 +49,51 @@ describe('external-consumer-service', () => {
});

it('should return reports with defaults values', async () => {
mockCount.mockReturnValue(1);
mockFindMany.mockReturnValue([testData]);
mockQueryRaw.mockReturnValue([testData]);
const results = await externalConsumerService.exportDataWithPagination();
expect(results.page).toBe(0);
expect(results.records[0]).toStrictEqual({
calculated_data: [
{
is_suppressed:
testData.pay_transparency_calculated_data[0].is_suppressed,
value: testData.pay_transparency_calculated_data[0].value,
testData.is_suppressed,
value: testData.value,
calculation_code:
testData.pay_transparency_calculated_data[0].calculation_code
.calculation_code,
testData.calculation_code,
},
],
company_address_line1: testData.pay_transparency_company.address_line1,
company_address_line2: testData.pay_transparency_company.address_line2,
company_address_line1: testData.address_line1,
company_address_line2: testData.address_line2,
company_bceid_business_guid:
testData.pay_transparency_company.bceid_business_guid,
company_city: testData.pay_transparency_company.city,
company_country: testData.pay_transparency_company.country,
testData.bceid_business_guid,
company_city: testData.city,
company_country: testData.country,
company_id: testData.company_id,
company_name: testData.pay_transparency_company.company_name,
company_postal_code: testData.pay_transparency_company.postal_code,
company_province: testData.pay_transparency_company.province,
company_name: testData.company_name,
company_postal_code: testData.postal_code,
company_province: testData.province,
create_date: testData.create_date,
data_constraints: testData.data_constraints,
employee_count_range: testData.employee_count_range.employee_count_range,
employee_count_range: testData.employee_count_range,
naics_code:
testData.naics_code_pay_transparency_report_naics_codeTonaics_code
testData
.naics_code,
naics_code_label:
testData.naics_code_pay_transparency_report_naics_codeTonaics_code
testData
.naics_label,
report_end_date: testData.report_end_date,
report_id: testData.report_id,
report_start_date: testData.report_start_date,
report_status: testData.report_status,
reporting_year: testData.reporting_year,
revision: testData.revision,
update_date: testData.update_date,
user_comment: testData.user_comment,
});

expect(results.history[0]).toStrictEqual({
calculated_data: [
{
is_suppressed:
testData.report_history[0].calculated_data_history[0].is_suppressed,
value: testData.report_history[0].calculated_data_history[0].value,
calculation_code:
testData.report_history[0].calculated_data_history[0]
.calculation_code.calculation_code,
},
],
company_address_line1:
testData.report_history[0].pay_transparency_company.address_line1,
company_address_line2:
testData.report_history[0].pay_transparency_company.address_line2,
company_bceid_business_guid:
testData.report_history[0].pay_transparency_company.bceid_business_guid,
company_city: testData.report_history[0].pay_transparency_company.city,
company_country:
testData.report_history[0].pay_transparency_company.country,
company_id: testData.report_history[0].company_id,
company_name:
testData.report_history[0].pay_transparency_company.company_name,
company_postal_code:
testData.report_history[0].pay_transparency_company.postal_code,
company_province:
testData.report_history[0].pay_transparency_company.province,
create_date: testData.report_history[0].create_date,
data_constraints: testData.report_history[0].data_constraints,
employee_count_range:
testData.report_history[0].employee_count_range.employee_count_range,
naics_code:
testData.report_history[0]
.naics_code_report_history_naics_codeTonaics_code.naics_code,
naics_code_label:
testData.report_history[0]
.naics_code_report_history_naics_codeTonaics_code.naics_label,
report_end_date: testData.report_history[0].report_end_date,
report_id: testData.report_history[0].report_id,
report_start_date: testData.report_history[0].report_start_date,
report_status: testData.report_history[0].report_status,
revision: testData.report_history[0].revision,
update_date: testData.report_history[0].update_date,
user_comment: testData.report_history[0].user_comment,
});
});

it('should parse date strings', async () => {
mockCount.mockReturnValue(1);
mockFindMany.mockReturnValue([testData]);
mockQueryRaw.mockReturnValue([testData]);
const results = await externalConsumerService.exportDataWithPagination(
'2024-01-01',
'2024-01-01',
Expand All @@ -204,8 +104,7 @@ describe('external-consumer-service', () => {
});

it('should fail parse invalid date strings', async () => {
mockCount.mockReturnValue(1);
mockFindMany.mockReturnValue([testData]);
mockQueryRaw.mockReturnValue([testData]);
try {
await externalConsumerService.exportDataWithPagination(
'20241-01-01',
Expand All @@ -220,8 +119,7 @@ describe('external-consumer-service', () => {
}
});
it('should fail when endDate is before the startDate', async () => {
mockCount.mockReturnValue(1);
mockFindMany.mockReturnValue([testData]);
mockQueryRaw.mockReturnValue([testData]);
try {
await externalConsumerService.exportDataWithPagination(
'2024-01-01',
Expand Down

0 comments on commit f30c951

Please sign in to comment.