Skip to content

Commit

Permalink
test: improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 8, 2024
1 parent 527dca3 commit d8367e3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const createKeys = require('./keys')
const createPlans = require('./plans')

module.exports = ({ serialize = JSONB.stringify, deserialize = JSONB.parse, redis = new Map() } = {}) => {
if (!redis) throw TypeError('The argument `store` is required.')
const plans = createPlans({ serialize, deserialize, redis })
const keys = createKeys({ serialize, deserialize, redis, plans })
return { keys, plans }
Expand Down
3 changes: 2 additions & 1 deletion src/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ module.exports = ({ serialize, deserialize, plans, redis } = {}) => {
assert(plan === null, `The key \`${keyId}\` is associated with the plan \`${getKey.plan}\``)
}
const isDeleted = (await redis.del(getKey(keyId, { verify: true }))) === 1
return assert(isDeleted, `The key \`${keyId}\` does not exist.`) || isDeleted
assert(isDeleted, `The key \`${keyId}\` does not exist.`)
return isDeleted
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ module.exports = ({ serialize, deserialize, redis } = {}) => {
*/
const del = async planId => {
const isDeleted = (await redis.del(getKey(planId, { validate: true }))) === 1
return assert(isDeleted, `The plan \`${planId}\` does not exist.`) || isDeleted
assert(isDeleted, `The plan \`${planId}\` does not exist.`)
return isDeleted
}

/**
Expand Down
15 changes: 14 additions & 1 deletion test/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const { keys, plans } = openkey({ redis: new Redis() })

test.beforeEach(async () => {
const keys = await redis.keys(`${KEY_PREFIX}*`)

if (keys.length > 0) await redis.del(keys)
})

Expand Down Expand Up @@ -130,6 +129,20 @@ test('.update # error if key does not exist', async t => {
t.is(error.name, 'TypeError')
})

test('.update # error if plan is invalid', async t => {
const { id } = await keys.create({ name: '[email protected]' })
const error = await t.throwsAsync(keys.update(id, { plan: 'id' }))
t.is(error.message, 'The id `id` must to start with `plan_`.')
t.is(error.name, 'TypeError')
})

test('.update # error if plan does not exist', async t => {
const { id } = await keys.create({ name: '[email protected]' })
const error = await t.throwsAsync(keys.update(id, { plan: 'plan_id' }))
t.is(error.message, 'The plan `plan_id` does not exist.')
t.is(error.name, 'TypeError')
})

test('.update # add a plan', async t => {
const plan = await plans.create({
name: 'free tier',
Expand Down
5 changes: 3 additions & 2 deletions test/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ test('.create', async t => {
t.deepEqual(plan.throttle, { burstLimit: 1000, rateLimit: 10 })
})

test('.retrieve # a plan not previosuly created', async t => {
test('.retrieve # a plan not previously created', async t => {
t.is(await plans.retrieve('plan_1'), null)
})

test('.retrieve # a plan previosuly created', async t => {
test('.retrieve # a plan previously created', async t => {
const { id } = await plans.create({
name: 'free tier',
quota: { limit: 3000, period: 'day' }
Expand Down Expand Up @@ -276,6 +276,7 @@ test('.del', async t => {

test('.del # error if plan does not exist', async t => {
const error = await t.throwsAsync(plans.del('plan_id'))

t.is(error.message, 'The plan `plan_id` does not exist.')
t.is(error.name, 'TypeError')
})

0 comments on commit d8367e3

Please sign in to comment.