Skip to content

Commit a020810

Browse files
author
Andres Pinto
committed
chore(usage): added tests to usage service
1 parent 5d63a7e commit a020810

File tree

1 file changed

+254
-0
lines changed

1 file changed

+254
-0
lines changed
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { v4 } from 'uuid';
3+
import { createMock } from '@golevelup/ts-jest';
4+
import { UsageService } from './usage.service';
5+
import { SequelizeUsageRepository } from './usage.repository';
6+
import { newUser, newFile, newUsage } from '../../../test/fixtures';
7+
import { UsageType } from './usage.domain';
8+
import { Time } from '../../lib/time';
9+
10+
describe('UsageService', () => {
11+
let service: UsageService;
12+
let usageRepository: SequelizeUsageRepository;
13+
14+
beforeEach(async () => {
15+
const module: TestingModule = await Test.createTestingModule({
16+
providers: [UsageService],
17+
})
18+
.useMocker(() => createMock())
19+
.compile();
20+
21+
service = module.get<UsageService>(UsageService);
22+
usageRepository = module.get<SequelizeUsageRepository>(
23+
SequelizeUsageRepository,
24+
);
25+
});
26+
27+
afterEach(() => {
28+
jest.clearAllMocks();
29+
Time.resumeTime();
30+
});
31+
32+
it('should be defined', () => {
33+
expect(service).toBeDefined();
34+
});
35+
36+
describe('addDailyUsageChangeOnFileSizeChange', () => {
37+
const user = newUser();
38+
const oldFile = newFile({ attributes: { size: BigInt(100) } });
39+
40+
it('When no existing usage found, then should return null', async () => {
41+
const newFileData = newFile({ attributes: { size: BigInt(200) } });
42+
43+
jest
44+
.spyOn(usageRepository, 'getMostRecentMonthlyOrYearlyUsage')
45+
.mockResolvedValue(null);
46+
47+
const result = await service.addDailyUsageChangeOnFileSizeChange(
48+
user,
49+
oldFile,
50+
newFileData,
51+
);
52+
53+
expect(result).toBeNull();
54+
expect(
55+
usageRepository.getMostRecentMonthlyOrYearlyUsage,
56+
).toHaveBeenCalledWith(user.uuid);
57+
});
58+
59+
it('When file size delta is zero, then should return null', async () => {
60+
const existingUsage = newUsage();
61+
const sameFile = newFile({ attributes: { size: BigInt(100) } });
62+
63+
jest
64+
.spyOn(usageRepository, 'getMostRecentMonthlyOrYearlyUsage')
65+
.mockResolvedValue(existingUsage);
66+
67+
const result = await service.addDailyUsageChangeOnFileSizeChange(
68+
user,
69+
oldFile,
70+
sameFile,
71+
);
72+
73+
expect(result).toBeNull();
74+
});
75+
76+
it('When file is created today, then should return null', async () => {
77+
const existingUsage = newUsage();
78+
const todayFile = newFile({
79+
attributes: {
80+
size: BigInt(200),
81+
createdAt: new Date(),
82+
},
83+
});
84+
85+
jest
86+
.spyOn(usageRepository, 'getMostRecentMonthlyOrYearlyUsage')
87+
.mockResolvedValue(existingUsage);
88+
89+
const result = await service.addDailyUsageChangeOnFileSizeChange(
90+
user,
91+
oldFile,
92+
todayFile,
93+
);
94+
95+
expect(result).toBeNull();
96+
});
97+
98+
it('When file size increased and not created today, then should create daily usage with positive delta', async () => {
99+
const existingUsage = newUsage();
100+
const yesterdayFile = newFile({
101+
attributes: {
102+
size: BigInt(200),
103+
createdAt: new Date(Date.now() - 24 * 60 * 60 * 1000),
104+
},
105+
});
106+
const expectedUsage = newUsage({
107+
attributes: {
108+
userId: user.uuid,
109+
delta: 100,
110+
type: UsageType.Daily,
111+
},
112+
});
113+
114+
jest
115+
.spyOn(usageRepository, 'getMostRecentMonthlyOrYearlyUsage')
116+
.mockResolvedValue(existingUsage);
117+
jest.spyOn(usageRepository, 'create').mockResolvedValue(expectedUsage);
118+
119+
const result = await service.addDailyUsageChangeOnFileSizeChange(
120+
user,
121+
oldFile,
122+
yesterdayFile,
123+
);
124+
125+
expect(result).toEqual(expectedUsage);
126+
expect(usageRepository.create).toHaveBeenCalledWith(
127+
expect.objectContaining({
128+
userId: user.uuid,
129+
delta: 100,
130+
type: UsageType.Daily,
131+
}),
132+
);
133+
});
134+
135+
it('When file size decreased and not created today, then should create daily usage with negative delta', async () => {
136+
const existingUsage = newUsage();
137+
const yesterdayFile = newFile({
138+
attributes: {
139+
size: BigInt(50),
140+
createdAt: new Date(Date.now() - 24 * 60 * 60 * 1000),
141+
},
142+
});
143+
const expectedUsage = newUsage({
144+
attributes: {
145+
userId: user.uuid,
146+
delta: -50,
147+
type: UsageType.Daily,
148+
},
149+
});
150+
151+
jest
152+
.spyOn(usageRepository, 'getMostRecentMonthlyOrYearlyUsage')
153+
.mockResolvedValue(existingUsage);
154+
jest.spyOn(usageRepository, 'create').mockResolvedValue(expectedUsage);
155+
156+
const result = await service.addDailyUsageChangeOnFileSizeChange(
157+
user,
158+
oldFile,
159+
yesterdayFile,
160+
);
161+
162+
expect(result).toEqual(expectedUsage);
163+
expect(usageRepository.create).toHaveBeenCalledWith(
164+
expect.objectContaining({
165+
userId: user.uuid,
166+
delta: -50,
167+
type: UsageType.Daily,
168+
}),
169+
);
170+
});
171+
});
172+
173+
describe('getUserMostRecentUsage', () => {
174+
it('When called, then it should return the most recent usage', async () => {
175+
const user = newUser();
176+
const usage = newUsage();
177+
178+
jest
179+
.spyOn(usageRepository, 'getMostRecentMonthlyOrYearlyUsage')
180+
.mockResolvedValue(usage);
181+
182+
const result = await service.getUserMostRecentUsage(user.uuid);
183+
184+
expect(result).toEqual(usage);
185+
expect(
186+
usageRepository.getMostRecentMonthlyOrYearlyUsage,
187+
).toHaveBeenCalledWith(user.uuid);
188+
});
189+
});
190+
191+
describe('createFirstUsageCalculation', () => {
192+
it('When called, then it should call the repository with expected arguments and return the first created usage', async () => {
193+
const user = newUser();
194+
const usage = newUsage();
195+
196+
jest
197+
.spyOn(usageRepository, 'createFirstUsageCalculation')
198+
.mockResolvedValue(usage);
199+
200+
const result = await service.createFirstUsageCalculation(user.uuid);
201+
202+
expect(result).toEqual(usage);
203+
expect(usageRepository.createFirstUsageCalculation).toHaveBeenCalledWith(
204+
user.uuid,
205+
);
206+
});
207+
});
208+
209+
describe('createMonthlyUsage', () => {
210+
it('When called, then it should create the monthly usage with expected arguments', async () => {
211+
const userId = v4();
212+
const period = new Date();
213+
const delta = 1000;
214+
const usage = newUsage({ attributes: { type: UsageType.Monthly } });
215+
216+
jest.spyOn(usageRepository, 'create').mockResolvedValue(usage);
217+
218+
const result = await service.createMonthlyUsage(userId, period, delta);
219+
220+
expect(result).toEqual(usage);
221+
expect(usageRepository.create).toHaveBeenCalledWith(
222+
expect.objectContaining({
223+
userId,
224+
period,
225+
delta,
226+
type: UsageType.Monthly,
227+
}),
228+
);
229+
});
230+
});
231+
232+
describe('createDailyUsage', () => {
233+
it('When called, then it should create daily usage with expected arguments', async () => {
234+
const userId = 'user-id';
235+
const period = new Date();
236+
const delta = 500;
237+
const usage = newUsage({ attributes: { type: UsageType.Daily } });
238+
239+
jest.spyOn(usageRepository, 'create').mockResolvedValue(usage);
240+
241+
const result = await service.createDailyUsage(userId, period, delta);
242+
243+
expect(result).toEqual(usage);
244+
expect(usageRepository.create).toHaveBeenCalledWith(
245+
expect.objectContaining({
246+
userId,
247+
period,
248+
delta,
249+
type: UsageType.Daily,
250+
}),
251+
);
252+
});
253+
});
254+
});

0 commit comments

Comments
 (0)