Skip to content

Commit

Permalink
Implement extendLifetime() method for shared diff repository
Browse files Browse the repository at this point in the history
  • Loading branch information
pbu88 committed Sep 17, 2019
1 parent 486a055 commit 2923511
Show file tree
Hide file tree
Showing 8 changed files with 835 additions and 787 deletions.
1,568 changes: 784 additions & 784 deletions backend/package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions backend/src/v2/SharedDiffRepository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SharedDiff } from './SharedDiff';

export interface SharedDiffRepository {
insert: (diff: SharedDiff) => Promise<SharedDiff>;
fetchById: (id: string) => Promise<SharedDiff>;
deleteById: (id: string) => Promise<number>;
insert: (diff: SharedDiff) => Promise<SharedDiff>;
fetchById: (id: string) => Promise<SharedDiff>;
deleteById: (id: string) => Promise<number>;
extendLifetime: (id: string, noOfDays: number) => Promise<SharedDiff>;
}
19 changes: 19 additions & 0 deletions backend/src/v2/SharedDiffRepository/MongoSharedDiffRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ export class MongoSharedDiffRepository implements SharedDiffRepository {
.then(doc => ({id: doc._id, ...doc}));
}

extendLifetime(id: string, hours: number): Promise<SharedDiff> {
return this.fetchById(id)
.then(sharedDiff => new Date(sharedDiff.expiresAt.getTime() + (hours * 60 * 60 * 1000)))
.then(newExpiredDate => {
this.client
.then(client => client.db(this.db_name))
.then(db => db.collection(COLLECTION_NAME))
.then(collection => collection.updateOne(
{ "_id": id },
{
$set: {
expiresAt: newExpiredDate
}
}
))
})
.then(updateResult => this.fetchById(id));
}

// returns a promise of how many items where deleted
deleteById(id: string): Promise<number> {
return this.client
Expand Down
2 changes: 2 additions & 0 deletions backend/tests/v2/CreateSharedDiffAPIAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ index 1456e89..e1da2da 100644
insert: jest.fn(diff => Promise.resolve(diff)),
fetchById: (id: string) => null,
deleteById: (id: string) => Promise.resolve(0),
extendLifetime: jest.fn(diff => Promise.resolve(diff)),
};
const action = new CreateSharedDiffAPIAction(repo, metrics);
expect(action).toBeDefined();
Expand Down Expand Up @@ -45,6 +46,7 @@ index 1456e89..e1da2da 100644
insert: jest.fn((diff) => Promise.reject(new Error('fake error'))),
fetchById: (id: string) => null,
deleteById: (id: string) => Promise.resolve(0),
extendLifetime: jest.fn(diff => Promise.reject("random err")),
};
const action = new CreateSharedDiffAPIAction(repo, metrics);
expect(action).toBeDefined();
Expand Down
3 changes: 3 additions & 0 deletions backend/tests/v2/CreateSharedDiffAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ index 1456e89..e1da2da 100644
insert: jest.fn(diff => Promise.resolve(diff)),
fetchById: (id: string) => null,
deleteById: (id: string) => Promise.resolve(0),
extendLifetime: (id: string, noOfDays: number) => Promise.reject("random err"),
};
const action = new CreateSharedDiffAction(repo, metrics);
expect(action).toBeDefined();
Expand Down Expand Up @@ -45,6 +46,7 @@ index 1456e89..e1da2da 100644
insert: jest.fn((diff) => Promise.reject(new Error('fake error'))),
fetchById: (id: string) => null,
deleteById: (id: string) => Promise.resolve(0),
extendLifetime: (id: string, noOfDays: number) => Promise.reject("random err"),
};
const action = new CreateSharedDiffAction(repo, metrics);
expect(action).toBeDefined();
Expand Down Expand Up @@ -72,6 +74,7 @@ index 1456e89..e1da2da 100644
insert: jest.fn().mockReturnValueOnce(new Promise(() => {})),
fetchById: (id: string) => null,
deleteById: (id: string) => Promise.resolve(0),
extendLifetime: (id: string, noOfDays: number) => Promise.reject("random err"),
};
const action = new CreateSharedDiffAction(repo, metrics);
const is_valid = action.isValidRawDiff(raw_diff);
Expand Down
1 change: 1 addition & 0 deletions backend/tests/v2/DeleteSharedDiffAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ test('should create a DeleteSharedDiffAction and delete a SharedDiff by id', ()
insert: jest.fn(),
fetchById: jest.fn(),
deleteById: (id: string) => Promise.resolve(1),
extendLifetime: (id: string, noOfDays: number) => Promise.reject("random err"),
}
const action = new DeleteSharedDiffAction(repo, metrics);
expect(action).toBeDefined();
Expand Down
1 change: 1 addition & 0 deletions backend/tests/v2/GetSharedDiffAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ index 1456e89..e1da2da 100644
insert: jest.fn(),
fetchById: (id: string) => Promise.resolve({id, ...makeSharedDiff(raw_diff)}),
deleteById: (id: string) => Promise.resolve(0),
extendLifetime: (id: string, noOfDays: number) => Promise.reject("random err"),
}
const action = new GetSharedDiffAction(repo, metrics);
expect(action).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,25 @@ describe('MongoSharedDiff tests', () => {
.then(stored_diff => repo.deleteById(stored_diff.id))
.then(deletedCount => expect(deletedCount).toEqual(1));
});

test('Mongo test: extend lifetime of a SharedDiff', () => {
const raw_diff = `
diff --git a/file.json b/file.json
index 1456e89..e1da2da 100644
--- a/file.json
+++ b/file.json
@@ -1,1 +1,1 @@
-a
+b
`
const shared_diff = makeSharedDiff(raw_diff);
const msPerDay = 24 * 60 * 60 * 1000;
return repo.insert(shared_diff)
.then((diff) => {
expect(diff.expiresAt.getTime() - diff.created.getTime()).toBeLessThan(2 * msPerDay);
return diff;
})
.then(stored_diff => repo.extendLifetime(stored_diff.id, 24))
.then((diff) => expect(diff.expiresAt.getTime() - diff.created.getTime()).toBeGreaterThanOrEqual(2 * msPerDay));
});
});

0 comments on commit 2923511

Please sign in to comment.