Skip to content

Commit 5a63edd

Browse files
committed
lint cleanup and unit test changes to accommodate recent bug fixes
1 parent 5f43570 commit 5a63edd

14 files changed

+309
-204
lines changed

server/feed16/src/app.module.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { Module } from '@nestjs/common';
22
import { ParticipantApiController } from './participant/participant.controller';
33
import { OutboundApiController } from './outbound/outbound.controller';
4-
import { ParticipantService, OutboundService, InboundService } from './participant/participant.service';
4+
import {
5+
ParticipantService,
6+
OutboundService,
7+
InboundService,
8+
} from './participant/participant.service';
59
import { OutboundService as OutboundSearchService } from './outbound/outbound.service';
610
import { RedisModule } from './redis.module';
711
import { SearchModule } from './elasticsearch.module';
812
import { MySqlModule } from './mysql.module';
913
import { ParticipantRepository } from './entity/participant';
1014
import { FriendRepository } from './entity/friend';
11-
import { TypeOrmModule } from '@nestjs/typeorm';
1215
import { participantProviders, friendProviders } from './database.providers';
1316

1417
@Module({
@@ -17,12 +20,12 @@ import { participantProviders, friendProviders } from './database.providers';
1720
providers: [
1821
...participantProviders,
1922
...friendProviders,
20-
ParticipantRepository,
23+
ParticipantRepository,
2124
FriendRepository,
22-
OutboundService,
23-
InboundService,
24-
OutboundSearchService,
25-
ParticipantService,
25+
OutboundService,
26+
InboundService,
27+
OutboundSearchService,
28+
ParticipantService,
2629
],
2730
})
2831
export class AppModule {}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { DataSource } from 'typeorm';
32
import { Participant } from './entity/participant';
43
import { Friend } from './entity/friend';
@@ -9,7 +8,7 @@ export const databaseProviders = [
98
useFactory: async () => {
109
const dataSource = new DataSource({
1110
type: 'mysql',
12-
host: process.env["MYSQL_HOST"] ?? 'localhost',
11+
host: process.env['MYSQL_HOST'] ?? 'localhost',
1312
port: 3306,
1413
username: 'feed',
1514
password: 'feed1234',
@@ -24,21 +23,21 @@ export const databaseProviders = [
2423
];
2524

2625
export const participantProviders = [
27-
{
28-
provide: 'PARTICIPANT_REPOSITORY',
29-
useFactory: (dataSource: DataSource) => {
30-
return dataSource.getRepository(Participant);
31-
},
32-
inject: ['feedDataSource'],
26+
{
27+
provide: 'PARTICIPANT_REPOSITORY',
28+
useFactory: (dataSource: DataSource) => {
29+
return dataSource.getRepository(Participant);
3330
},
31+
inject: ['feedDataSource'],
32+
},
3433
];
3534

3635
export const friendProviders = [
37-
{
38-
provide: 'FRIEND_REPOSITORY',
39-
useFactory: (dataSource: DataSource) => {
40-
return dataSource.getRepository(Friend);
41-
},
42-
inject: ['feedDataSource'],
36+
{
37+
provide: 'FRIEND_REPOSITORY',
38+
useFactory: (dataSource: DataSource) => {
39+
return dataSource.getRepository(Friend);
4340
},
41+
inject: ['feedDataSource'],
42+
},
4443
];

server/feed16/src/elasticsearch.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { SearchService } from './search.service';
88
imports: [
99
ElasticsearchModule.registerAsync({
1010
useFactory: () => ({
11-
node: `http://${process.env["SEARCH_HOST"] ?? 'localhost'}:9200`,
11+
node: `http://${process.env['SEARCH_HOST'] ?? 'localhost'}:9200`,
1212
maxRetries: 10,
1313
requestTimeout: 60000,
1414
pingTimeout: 60000,
@@ -23,4 +23,4 @@ import { SearchService } from './search.service';
2323
providers: [SearchService],
2424
exports: [SearchService],
2525
})
26-
export class SearchModule {}
26+
export class SearchModule {}

server/feed16/src/entity/friend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ export class Friend {
1212

1313
@Column({ name: 'ToParticipantID' })
1414
toParticipantId: number;
15-
}
15+
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Entity, Column, PrimaryGeneratedColumn, Repository } from 'typeorm';
22

3-
export class ParticipantRepository extends Repository<Participant> {
4-
}
3+
export class ParticipantRepository extends Repository<Participant> {}
54

65
@Entity('Participant')
76
export class Participant {
@@ -10,4 +9,4 @@ export class Participant {
109

1110
@Column({ name: 'Moniker' })
1211
moniker: string;
13-
}
12+
}

server/feed16/src/mysql.module.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
21
import { Module } from '@nestjs/common';
3-
import { TypeOrmModule } from '@nestjs/typeorm';
4-
import { Friend } from './entity/friend';
5-
import { Participant } from './entity/participant';
62
import { databaseProviders } from './database.providers';
73

84
@Module({

server/feed16/src/outbound/outbound.controller.spec.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,38 @@ import { OutboundApiController } from './outbound.controller';
44
import { OutboundService } from './outbound.service';
55

66
describe('OutboundApiController', () => {
7-
let appController: OutboundApiController | null = null;
8-
9-
beforeEach(async () => {
10-
const app: TestingModule = await Test.createTestingModule({
11-
imports: [],
12-
controllers: [OutboundApiController],
13-
providers: [
7+
let appController: OutboundApiController | null = null;
8+
9+
beforeEach(async () => {
10+
const app: TestingModule = await Test.createTestingModule({
11+
imports: [],
12+
controllers: [OutboundApiController],
13+
providers: [
14+
{
15+
provide: SearchService,
16+
useValue: {
17+
search: jest.fn().mockImplementation(() => {
18+
return Promise.resolve([
1419
{
15-
provide: SearchService,
16-
useValue: {
17-
search: jest.fn().mockImplementation(() => {
18-
return Promise.resolve([{
19-
sender: '/participant/1',
20-
subject: 'test subject',
21-
story: 'test story',
22-
}]);
23-
}),
24-
},
20+
sender: '/participant/1',
21+
subject: 'test subject',
22+
story: 'test story',
2523
},
26-
OutboundService,
27-
],
28-
}).compile();
29-
30-
appController = app.get<OutboundApiController>(OutboundApiController);
31-
});
32-
describe('search api', () => {
33-
it('should return results from keyword search', async () => {
34-
const result = await appController?.searchOutbound('test');
35-
expect(result?.length).toBe(1);
36-
expect(result).toEqual(['/participant/1']);
37-
});
24+
]);
25+
}),
26+
},
27+
},
28+
OutboundService,
29+
],
30+
}).compile();
31+
32+
appController = app.get<OutboundApiController>(OutboundApiController);
33+
});
34+
describe('search api', () => {
35+
it('should return results from keyword search', async () => {
36+
const result = await appController?.searchOutbound('test');
37+
expect(result?.length).toBe(1);
38+
expect(result).toEqual(['/participant/1']);
3839
});
39-
});
40+
});
41+
});

server/feed16/src/outbound/outbound.controller.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ export class OutboundApiController {
88
constructor(private readonly outboundService: OutboundService) {}
99

1010
@Get()
11-
@ApiOperation({ summary: 'Retrieve participants who posted stories containing these keywords' })
11+
@ApiOperation({
12+
summary:
13+
'Retrieve participants who posted stories containing these keywords',
14+
})
1215
@ApiResponse({
1316
status: 200,
1417
description: 'Successful operation',
1518
type: [String],
1619
})
17-
public async searchOutbound(@Query('keywords') keywords: string): Promise<string[]> {
20+
public async searchOutbound(
21+
@Query('keywords') keywords: string,
22+
): Promise<string[]> {
1823
return await this.outboundService.searchOutbound(keywords);
1924
}
20-
21-
}
25+
}
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { Injectable, Inject, NotFoundException } from '@nestjs/common';
1+
import { Injectable, NotFoundException } from '@nestjs/common';
22
import { SearchService, SearchDocument } from '../search.service';
33

44
@Injectable()
55
export class OutboundService {
6-
constructor(private readonly searchService: SearchService) {}
7-
public async searchOutbound(keywords: string): Promise<string[]> {
8-
const outbound: (SearchDocument | null)[] = await this.searchService.search(keywords);
9-
if (!outbound) {
10-
throw new NotFoundException(`no match for keywords: ${keywords}`);
11-
}
12-
return outbound.filter((doc) => doc !== null).map((doc) => doc.sender);
6+
constructor(private readonly searchService: SearchService) {}
7+
public async searchOutbound(keywords: string): Promise<string[]> {
8+
const outbound: (SearchDocument | null)[] =
9+
await this.searchService.search(keywords);
10+
if (!outbound) {
11+
throw new NotFoundException(`no match for keywords: ${keywords}`);
1312
}
14-
}
13+
return outbound.filter((doc) => doc !== null).map((doc) => doc.sender);
14+
}
15+
}

server/feed16/src/participant/participant.controller.spec.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import { Test, TestingModule } from '@nestjs/testing';
2-
import { ParticipantApiController, Participant, Friend, Inbound, Outbound } from './participant.controller';
2+
import {
3+
ParticipantApiController,
4+
Participant,
5+
Friend,
6+
Inbound,
7+
Outbound,
8+
} from './participant.controller';
39
import { Friend as FriendEntity } from '../entity/friend';
410
import { Participant as ParticipantEntity } from '../entity/participant';
5-
import { ParticipantService, OutboundService, InboundService, InboundModel, OutboundModel, ParticipantModel } from './participant.service';
11+
import {
12+
ParticipantService,
13+
OutboundService,
14+
InboundService,
15+
InboundModel,
16+
OutboundModel,
17+
ParticipantModel,
18+
} from './participant.service';
619
import { SearchService } from '../search.service';
720

821
describe('ParticipantApiController', () => {
@@ -23,8 +36,19 @@ describe('ParticipantApiController', () => {
2336
fe.toParticipantId = 2;
2437
const pm1: ParticipantModel = new ParticipantModel(1, 'Hello World!');
2538
const pm2: ParticipantModel = new ParticipantModel(2, 'foo bar');
26-
const im: InboundModel = new InboundModel(pm1, pm2, nowAsDate, 'test subject', 'test story');
27-
const om: OutboundModel = new OutboundModel(pm1, nowAsDate, 'test subject', 'test story');
39+
const im: InboundModel = new InboundModel(
40+
pm1,
41+
pm2,
42+
nowAsDate,
43+
'test subject',
44+
'test story',
45+
);
46+
const om: OutboundModel = new OutboundModel(
47+
pm1,
48+
nowAsDate,
49+
'test subject',
50+
'test story',
51+
);
2852
const app: TestingModule = await Test.createTestingModule({
2953
imports: [],
3054
controllers: [ParticipantApiController],
@@ -38,17 +62,17 @@ describe('ParticipantApiController', () => {
3862
},
3963
},
4064
{
41-
provide: 'ParticipantRepository',
65+
provide: 'PARTICIPANT_REPOSITORY',
4266
useValue: {
4367
findOneBy: jest.fn().mockReturnValue(Promise.resolve(pe)),
4468
save: jest.fn().mockImplementation(() => {
4569
participantSaved++;
4670
return Promise.resolve(pe);
47-
})
48-
}
71+
}),
72+
},
4973
},
5074
{
51-
provide: 'FriendRepository',
75+
provide: 'FRIEND_REPOSITORY',
5276
useValue: {
5377
save: jest.fn().mockImplementation(() => {
5478
friendSaved++;
@@ -75,7 +99,7 @@ describe('ParticipantApiController', () => {
7599
useValue: {
76100
get: jest.fn().mockReturnValue(Promise.resolve([im])),
77101
save: jest.fn().mockImplementation(() => {
78-
inboundSaved++;nowAsDate
102+
inboundSaved++;
79103
return Promise.resolve(im);
80104
}),
81105
},
@@ -89,8 +113,7 @@ describe('ParticipantApiController', () => {
89113
},
90114
ParticipantService,
91115
],
92-
})
93-
.compile();
116+
}).compile();
94117

95118
appController = app.get<ParticipantApiController>(ParticipantApiController);
96119
});
@@ -105,6 +128,7 @@ describe('ParticipantApiController', () => {
105128
const p: Participant = new Participant(1, 'Hello World!');
106129
const t: Participant | undefined = await appController?.addParticipant(p);
107130
expect(participantSaved).toBeGreaterThan(0);
131+
expect(t).toStrictEqual(p);
108132
});
109133
});
110134
describe('friend', () => {
@@ -145,9 +169,12 @@ describe('ParticipantApiController', () => {
145169
});
146170
it('add should call underlying repository add', async () => {
147171
const p1: Participant = new Participant(1, 'Hello World!');
148-
const p2: Participant = new Participant(2, 'foo bar');
149-
const f: Friend = new Friend(1, p1.link, p2.link);
150-
const o: Outbound = new Outbound(p1.link, nowAsDate, 'test subject', 'test story');
172+
const o: Outbound = new Outbound(
173+
p1.link,
174+
nowAsDate,
175+
'test subject',
176+
'test story',
177+
);
151178
const t: Outbound | undefined = await appController?.addOutbound(1, o);
152179
expect(outboundSaved).toBeGreaterThan(0);
153180
expect(inboundSaved).toBeGreaterThan(0);

0 commit comments

Comments
 (0)