Skip to content

Commit

Permalink
Don't return duplicates on /unique
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Oct 26, 2024
1 parent 99b9150 commit 43120fe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/courses/courses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class CoursesService {
)));
}

return db.sql`SELECT *, to_json("offered") as offered FROM ${'Course'} WHERE ${where} ORDER BY ${'id'} ASC`.compile();
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();
}

getFirstCourseZapatosQuery(parameters?: FindFirstCourseParameters) {
Expand Down
31 changes: 29 additions & 2 deletions src/tests/courses/courses.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,40 @@ test.serial('returns unique courses (by CRSE & subject)', async t => {
});

const allUniqueCoursesQuery = await service.getUniqueCoursesQuery(pool);
t.is((await pool.query(allUniqueCoursesQuery)).rowCount, 3);
t.is((await pool.query(allUniqueCoursesQuery)).rowCount, 2);

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

const uniqueCoursesAfter2000Query = await service.getUniqueCoursesQuery(pool, {startYear: 2000} as any);
t.is((await pool.query(uniqueCoursesAfter2000Query)).rowCount, 2);
t.is((await pool.query(uniqueCoursesAfter2000Query)).rowCount, 1);
});

test.serial('getUniqueCourses does not return duplicate courses', async t => {
const {service, prisma, pool} = await getTestService(CoursesService, {
seedCourses: true
});

const {id, ...courseWithoutId} = await prisma.course.findFirstOrThrow();
t.is(courseWithoutId.year, 2000);

await prisma.course.createMany({
data: [
// Create same course in different term...
{
...courseWithoutId,
semester: Semester.SUMMER,
year: 2001
},
]
});

const allUniqueCoursesQuery = await service.getUniqueCoursesQuery(pool, {startYear: 1999} as any);
const results = await pool.query(allUniqueCoursesQuery);

const cs1110Results = results.rows.filter(row => row.subject === 'CS' && row.crse === '1110');
t.is(cs1110Results.length, 1); // Should not be duplicate results
t.is(cs1110Results[0].year, 2001);// Contains the latest version
});

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

0 comments on commit 43120fe

Please sign in to comment.