Skip to content

Commit

Permalink
fix: use correct ami percentage in csv (bloom-housing#4520) (#827)
Browse files Browse the repository at this point in the history
* fix: use correct ami percentage in csv

* fix: add unit test
  • Loading branch information
ludtkemorgan authored Jan 13, 2025
1 parent dd7b622 commit 7de1c79
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
4 changes: 1 addition & 3 deletions api/src/services/listing-csv-export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Logger,
StreamableFile,
} from '@nestjs/common';
import { SchedulerRegistry } from '@nestjs/schedule';
import {
Request as ExpressRequest,
Response as ExpressResponse,
Expand Down Expand Up @@ -67,7 +66,6 @@ export class ListingCsvExporterService implements CsvExporterServiceInterface {
private prisma: PrismaService,
@Inject(Logger)
private logger = new Logger(ListingCsvExporterService.name),
private schedulerRegistry: SchedulerRegistry,
) {}

/**
Expand Down Expand Up @@ -845,7 +843,7 @@ export class ListingCsvExporterService implements CsvExporterServiceInterface {
label: 'AMI Chart',
},
{
path: 'unit.amiChart.items.0.percentOfAmi',
path: 'unit.amiPercentage',
label: 'AMI Level',
},
{
Expand Down
82 changes: 82 additions & 0 deletions api/test/unit/services/listing-csv-export.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Test, TestingModule } from '@nestjs/testing';
import { Logger } from '@nestjs/common';
import { randomUUID } from 'crypto';
import fs from 'fs';
import { ListingCsvExporterService } from '../../../src/services/listing-csv-export.service';
import { PrismaService } from '../../../src/services/prisma.service';
import Listing from '../../../src/dtos/listings/listing.dto';

describe('Testing listing csv export service', () => {
let service: ListingCsvExporterService;
let writeStream;

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PrismaService, ListingCsvExporterService, Logger],
}).compile();

service = module.get<ListingCsvExporterService>(ListingCsvExporterService);
});

beforeEach(() => {
writeStream = fs.createWriteStream('sampleFile.csv');
jest.spyOn(fs, 'createWriteStream').mockReturnValue(writeStream);
});

afterEach(() => {
writeStream.end();
fs.unlink('sampleFile.csv', () => {
// do nothing
});
jest.restoreAllMocks();
});

describe('createUnitCsv', () => {
it('should create the unit csv', async () => {
const unit = {
number: 1,
numBathrooms: 2,
floor: 3,
sqFeet: 1200,
minOccupancy: 1,
maxOccupancy: 8,
amiPercentage: 80,
monthlyRentAsPercentOfIncome: null,
monthlyRent: 4000,
unitTypes: { id: randomUUID(), name: 'studio' },
amiChart: { id: randomUUID(), name: 'Ami Chart Name' },
};
const mockListing = {
id: 'listing1-ID',
name: `listing1-Name`,
units: [unit],
};
const mockListing2 = {
id: 'listing2-ID',
name: `listing2-Name`,
units: [
{
...unit,
monthlyRentAsPercentOfIncome: 30.0,
unitTypes: { id: randomUUID(), name: 'twoBdrm' },
},
],
};
await service.createUnitCsv('sampleFile.csv', [
mockListing as unknown as Listing,
mockListing2 as unknown as Listing,
]);
expect(writeStream.bytesWritten).toBeGreaterThan(0);
const content = fs.readFileSync('sampleFile.csv', 'utf8');
expect(content).toContain(
'Listing Id,Listing Name,Unit Number,Unit Type,Number of Bathrooms,Unit Floor,Square Footage,Minimum Occupancy,Max Occupancy,AMI Chart,AMI Level,Rent Type',
);
expect(content).toContain(
'listing1-ID,listing1-Name,1,studio,2,3,1200,1,8,Ami Chart Name,80,Fixed amount',
);
expect(content).toContain(
'listing2-ID,listing2-Name,1,twoBdrm,2,3,1200,1,8,Ami Chart Name,80,% of income',
);
});
});
});

0 comments on commit 7de1c79

Please sign in to comment.