Skip to content

Commit 43120fe

Browse files
committed
Don't return duplicates on /unique
1 parent 99b9150 commit 43120fe

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/courses/courses.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class CoursesService {
5454
)));
5555
}
5656

57-
return db.sql`SELECT *, to_json("offered") as offered FROM ${'Course'} WHERE ${where} ORDER BY ${'id'} ASC`.compile();
57+
return db.sql`SELECT DISTINCT ON (subject, crse) *, to_json("offered") as offered FROM ${'Course'} WHERE ${where} ORDER BY ${'subject'} ASC, ${'crse'} ASC, ${'year'} DESC, ${'semester'} DESC`.compile();
5858
}
5959

6060
getFirstCourseZapatosQuery(parameters?: FindFirstCourseParameters) {

src/tests/courses/courses.service.test.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,40 @@ test.serial('returns unique courses (by CRSE & subject)', async t => {
9797
});
9898

9999
const allUniqueCoursesQuery = await service.getUniqueCoursesQuery(pool);
100-
t.is((await pool.query(allUniqueCoursesQuery)).rowCount, 3);
100+
t.is((await pool.query(allUniqueCoursesQuery)).rowCount, 2);
101101

102102
const fallUniqueCoursesQuery = await service.getUniqueCoursesQuery(pool, {semester: Semester.FALL} as any);
103103
t.is((await pool.query(fallUniqueCoursesQuery)).rowCount, 2);
104104

105105
const uniqueCoursesAfter2000Query = await service.getUniqueCoursesQuery(pool, {startYear: 2000} as any);
106-
t.is((await pool.query(uniqueCoursesAfter2000Query)).rowCount, 2);
106+
t.is((await pool.query(uniqueCoursesAfter2000Query)).rowCount, 1);
107+
});
108+
109+
test.serial('getUniqueCourses does not return duplicate courses', async t => {
110+
const {service, prisma, pool} = await getTestService(CoursesService, {
111+
seedCourses: true
112+
});
113+
114+
const {id, ...courseWithoutId} = await prisma.course.findFirstOrThrow();
115+
t.is(courseWithoutId.year, 2000);
116+
117+
await prisma.course.createMany({
118+
data: [
119+
// Create same course in different term...
120+
{
121+
...courseWithoutId,
122+
semester: Semester.SUMMER,
123+
year: 2001
124+
},
125+
]
126+
});
127+
128+
const allUniqueCoursesQuery = await service.getUniqueCoursesQuery(pool, {startYear: 1999} as any);
129+
const results = await pool.query(allUniqueCoursesQuery);
130+
131+
const cs1110Results = results.rows.filter(row => row.subject === 'CS' && row.crse === '1110');
132+
t.is(cs1110Results.length, 1); // Should not be duplicate results
133+
t.is(cs1110Results[0].year, 2001);// Contains the latest version
107134
});
108135

109136
test.serial('returns unique courses with updatedSince', async t => {

0 commit comments

Comments
 (0)