Skip to content

Commit

Permalink
Merge pull request #1303 from globaldothealth/upload
Browse files Browse the repository at this point in the history
Fix upload sorting take 2
  • Loading branch information
allysonjp715 authored Oct 1, 2020
2 parents 976908a + 4e7f2de commit 9dd023a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default class UploadsController {
Source.aggregate([
{ $unwind: '$uploads' },
...changesOnlyMatcher,
{ $sort: { 'upload.created': -1, sourceName: -1 } },
{ $sort: { 'uploads.created': -1, name: -1 } },
{ $skip: limit * (page - 1) },
{ $limit: limit + 1 },
{
Expand Down
6 changes: 3 additions & 3 deletions verification/curator-service/api/src/model/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
automationParsingValidator,
automationSchema,
} from './automation';
import { DateFilterDocument, dateFilterSchema } from './date-filter';
import { OriginDocument, originSchema } from './origin';
import { UploadDocument, uploadSchema } from './upload';

import mongoose from 'mongoose';
import { uploadSchema, UploadDocument } from './upload';
import { dateFilterSchema, DateFilterDocument } from './date-filter';

const sourceSchema = new mongoose.Schema({
name: {
Expand Down Expand Up @@ -49,7 +49,7 @@ export type SourceDocument = mongoose.Document & {
origin: OriginDocument;
format: string;
automation: AutomationDocument;
uploads: [UploadDocument];
uploads: UploadDocument[];
dateFilter: DateFilterDocument;
notificationRecipients: string[];

Expand Down
85 changes: 83 additions & 2 deletions verification/curator-service/api/test/uploads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('GET', () => {
expect(res.body.nextPage).toBeUndefined();
expect(res.body.total).toEqual(15);
});
it('should filter for changes only and sort by created date', async () => {
it('should filter for changes only', async () => {
const sourceWithError = new Source(fullSource);
sourceWithError.uploads[0].created = new Date(2020, 2, 3);
await sourceWithError.save();
Expand Down Expand Up @@ -165,10 +165,91 @@ describe('GET', () => {
sourceWithError.uploads[0]._id.toString(),
);
expect(res.body.uploads[1].upload._id).toEqual(
sourceWithUpdatedUploads.uploads[0]._id.toString(),
);
expect(res.body.uploads[2].upload._id).toEqual(
sourceWithCreatedUploads.uploads[0]._id.toString(),
);
});
it('should sort by created date then source name', async () => {
const source1 = new Source(fullSource);
source1.name = 'source1';
source1.uploads = [
new Upload({
status: 'ERROR',
summary: new UploadSummary(),
created: new Date(2020, 2, 1),
}),
new Upload({
status: 'SUCCESS',
summary: new UploadSummary({ numCreated: 3 }),
created: new Date(2020, 2, 6),
}),
];
await source1.save();

const source2 = new Source(fullSource);
source2.name = 'source2';
source2.uploads = [
new Upload({
status: 'SUCCESS',
summary: new UploadSummary({ numUpdated: 3 }),
created: new Date(2020, 2, 5),
}),
new Upload({
status: 'ERROR',
summary: new UploadSummary(),
created: new Date(2020, 2, 3),
}),
];
await source2.save();

const source3 = new Source(fullSource);
source3.name = 'source3';
source3.uploads = [
new Upload({
status: 'SUCCESS',
summary: new UploadSummary({ numCreated: 3 }),
created: new Date(2020, 2, 3),
}),
new Upload({
status: 'SUCCESS',
summary: new UploadSummary({ numCreated: 3 }),
created: new Date(2020, 2, 4),
}),
];
await source3.save();

const sourceNoChanges = new Source(fullSource);
sourceNoChanges.uploads = [
new Upload({
status: 'SUCCESS',
summary: new UploadSummary(),
created: new Date(2020, 2, 7),
}),
];
await sourceNoChanges.save();

const res = await curatorRequest
.get('/api/sources/uploads?page=1&limit=5&changes_only=true')
.expect('Content-Type', /json/)
.expect(200);

expect(res.body.uploads).toHaveLength(5);
expect(res.body.uploads[0].upload._id).toEqual(
source1.uploads[1]._id.toString(),
);
expect(res.body.uploads[1].upload._id).toEqual(
source2.uploads[0]._id.toString(),
);
expect(res.body.uploads[2].upload._id).toEqual(
sourceWithUpdatedUploads.uploads[0]._id.toString(),
source3.uploads[1]._id.toString(),
);
expect(res.body.uploads[3].upload._id).toEqual(
source3.uploads[0]._id.toString(),
);
expect(res.body.uploads[4].upload._id).toEqual(
source2.uploads[1]._id.toString(),
);
});
it('rejects negative page param', (done) => {
Expand Down

0 comments on commit 9dd023a

Please sign in to comment.