-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from garrett-green/feature/120-add-expireAt
Add 'expireAt' To Set TTL With Date Object
- Loading branch information
Showing
6 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import '../../helpers/custom-matchers' | ||
|
||
import { redis } from '../helpers/mock-redis' | ||
|
||
import { Client } from '$lib/client' | ||
import { RedisOmError } from '$lib/error' | ||
|
||
const today = new Date(); | ||
const tomorrow = new Date(today); | ||
tomorrow.setDate(tomorrow.getDate() + 1); | ||
const yesterday = new Date(today); | ||
yesterday.setDate(yesterday.getDate() - 1); | ||
|
||
describe("Client", () => { | ||
|
||
let client: Client | ||
|
||
beforeEach(() => { client = new Client() }) | ||
|
||
describe("#expire", () => { | ||
describe("when called on an open client", () => { | ||
beforeEach(async () => { | ||
await client.open() | ||
}) | ||
|
||
it("passes the command to redis", async () => { | ||
await client.expireAt('foo', tomorrow) | ||
expect(redis.expireAt).toHaveBeenCalledWith('foo', tomorrow) | ||
}) | ||
}) | ||
|
||
describe("when called on a closed client", () => { | ||
beforeEach(async () => { | ||
await client.open() | ||
await client.close() | ||
}) | ||
|
||
it("errors when called on a closed client", () => | ||
expect(async () => await client.expireAt('foo', tomorrow)) | ||
.rejects.toThrowErrorOfType(RedisOmError, "Redis connection needs to be open.")) | ||
}) | ||
|
||
it("errors when called on a new client", async () => | ||
expect(async () => await client.expireAt('foo', tomorrow)) | ||
.rejects.toThrowErrorOfType(RedisOmError, "Redis connection needs to be open.")) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import '../helpers/mock-client'; | ||
|
||
import { Client } from '$lib/client'; | ||
import { Repository } from '$lib/repository'; | ||
import { Schema } from '$lib/schema'; | ||
|
||
const simpleSchema = new Schema('SimpleEntity', {}, { dataStructure: 'HASH' }); | ||
|
||
const today = new Date(); | ||
const tomorrow = new Date(today); | ||
tomorrow.setDate(tomorrow.getDate() + 1); | ||
const yesterday = new Date(today); | ||
yesterday.setDate(yesterday.getDate() - 1); | ||
|
||
describe('Repository', () => { | ||
describe('#expireAt', () => { | ||
let client: Client; | ||
let repository: Repository; | ||
|
||
beforeAll(() => { | ||
client = new Client(); | ||
}); | ||
beforeEach(() => { | ||
repository = new Repository(simpleSchema, client); | ||
}); | ||
|
||
it('expires a single entity', async () => { | ||
await repository.expireAt('foo', tomorrow); | ||
expect(client.expireAt).toHaveBeenCalledWith( | ||
'SimpleEntity:foo', | ||
tomorrow | ||
); | ||
}); | ||
|
||
it('expires multiple entities', async () => { | ||
await repository.expireAt(['foo', 'bar', 'baz'], tomorrow); | ||
expect(client.expireAt).toHaveBeenNthCalledWith( | ||
1, | ||
'SimpleEntity:foo', | ||
tomorrow | ||
); | ||
expect(client.expireAt).toHaveBeenNthCalledWith( | ||
2, | ||
'SimpleEntity:bar', | ||
tomorrow | ||
); | ||
expect(client.expireAt).toHaveBeenNthCalledWith( | ||
3, | ||
'SimpleEntity:baz', | ||
tomorrow | ||
); | ||
}); | ||
|
||
it('throws an error when provided invalid/past date', async () => { | ||
let caughtError: any; | ||
await repository.expireAt('foo', yesterday).catch((error) => { | ||
caughtError = error; | ||
}); | ||
expect(client.expireAt).toHaveBeenCalledTimes(0); | ||
expect(caughtError).toBeDefined(); | ||
expect(caughtError!.message).toEqual( | ||
`${yesterday.toString()} is invalid. Expiration date must be in the future.` | ||
); | ||
}); | ||
}); | ||
}); |