forked from bloom-housing/bloom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use correct ami percentage in csv (bloom-housing#4520) (#827)
* fix: use correct ami percentage in csv * fix: add unit test
- Loading branch information
1 parent
dd7b622
commit 7de1c79
Showing
2 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
); | ||
}); | ||
}); | ||
}); |