| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-17 21:55:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const	async = require('async'); | 
					
						
							|  |  |  | const assert = require('assert'); | 
					
						
							|  |  |  | const db = require('../mocks/databasemock'); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | describe('Key methods', () => { | 
					
						
							|  |  |  | 	beforeEach((done) => { | 
					
						
							| 
									
										
										
										
											2015-06-04 17:14:22 -04:00
										 |  |  | 		db.set('testKey', 'testValue', done); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should set a key without error', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		db.set('testKey', 'testValue', function (err) { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert(arguments.length < 2); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should get a key without error', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		db.get('testKey', function (err, value) { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 			assert.equal(arguments.length, 2); | 
					
						
							|  |  |  | 			assert.strictEqual(value, 'testValue'); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return null if key does not exist', (done) => { | 
					
						
							|  |  |  | 		db.get('doesnotexist', (err, value) => { | 
					
						
							| 
									
										
										
										
											2018-03-03 12:31:20 -05:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(value, null); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return true if key exist', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		db.exists('testKey', function (err, exists) { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 			assert.equal(arguments.length, 2); | 
					
						
							|  |  |  | 			assert.strictEqual(exists, true); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return false if key does not exist', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		db.exists('doesnotexist', function (err, exists) { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 			assert.equal(arguments.length, 2); | 
					
						
							|  |  |  | 			assert.strictEqual(exists, false); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should work for an array of keys', (done) => { | 
					
						
							|  |  |  | 		db.exists(['testKey', 'doesnotexist'], (err, exists) => { | 
					
						
							| 
									
										
										
										
											2018-11-21 20:40:40 -05:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(exists, [true, false]); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('scan', () => { | 
					
						
							|  |  |  | 		it('should scan keys for pattern', async () => { | 
					
						
							| 
									
										
										
										
											2020-07-02 20:11:53 -04:00
										 |  |  | 			await db.sortedSetAdd('ip:123:uid', 1, 'a'); | 
					
						
							|  |  |  | 			await db.sortedSetAdd('ip:123:uid', 2, 'b'); | 
					
						
							|  |  |  | 			await db.sortedSetAdd('ip:124:uid', 2, 'b'); | 
					
						
							|  |  |  | 			await db.sortedSetAdd('ip:1:uid', 1, 'a'); | 
					
						
							|  |  |  | 			await db.sortedSetAdd('ip:23:uid', 1, 'a'); | 
					
						
							|  |  |  | 			const data = await db.scan({ match: 'ip:1*' }); | 
					
						
							|  |  |  | 			assert.equal(data.length, 3); | 
					
						
							|  |  |  | 			assert(data.includes('ip:123:uid')); | 
					
						
							|  |  |  | 			assert(data.includes('ip:124:uid')); | 
					
						
							|  |  |  | 			assert(data.includes('ip:1:uid')); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should delete a key without error', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		db.delete('testKey', function (err) { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert(arguments.length < 2); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			db.get('testKey', (err, value) => { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 				assert.equal(false, !!value); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should return false if key was deleted', (done) => { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		db.delete('testKey', function (err) { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert(arguments.length < 2); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			db.exists('testKey', (err, exists) => { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2015-06-04 17:14:22 -04:00
										 |  |  | 				assert.strictEqual(exists, false); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should delete all keys passed in', (done) => { | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 		async.parallel([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 				db.set('key1', 'value1', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 				db.set('key2', 'value2', next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		], (err) => { | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return done(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			db.deleteAll(['key1', 'key2'], function (err) { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 				assert.equal(arguments.length, 1); | 
					
						
							| 
									
										
										
										
											2015-01-15 15:02:50 -05:00
										 |  |  | 				async.parallel({ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					key1exists: function (next) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 						db.exists('key1', next); | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					key2exists: function (next) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 						db.exists('key2', next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				}, (err, results) => { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 					assert.equal(results.key1exists, false); | 
					
						
							|  |  |  | 					assert.equal(results.key2exists, false); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should delete all sorted set elements', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-18 16:59:32 -04:00
										 |  |  | 		async.parallel([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				db.sortedSetAdd('deletezset', 1, 'value1', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				db.sortedSetAdd('deletezset', 2, 'value2', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		], (err) => { | 
					
						
							| 
									
										
										
										
											2017-05-18 16:59:32 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return done(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			db.delete('deletezset', (err) => { | 
					
						
							| 
									
										
										
										
											2017-05-18 16:59:32 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				async.parallel({ | 
					
						
							|  |  |  | 					key1exists: function (next) { | 
					
						
							|  |  |  | 						db.isSortedSetMember('deletezset', 'value1', next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					key2exists: function (next) { | 
					
						
							|  |  |  | 						db.isSortedSetMember('deletezset', 'value2', next); | 
					
						
							|  |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				}, (err, results) => { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-05-18 16:59:32 -04:00
										 |  |  | 					assert.equal(results.key1exists, false); | 
					
						
							|  |  |  | 					assert.equal(results.key2exists, false); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('increment', () => { | 
					
						
							|  |  |  | 		it('should initialize key to 1', (done) => { | 
					
						
							|  |  |  | 			db.increment('keyToIncrement', (err, value) => { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 				assert.strictEqual(parseInt(value, 10), 1); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should increment key to 2', (done) => { | 
					
						
							|  |  |  | 			db.increment('keyToIncrement', (err, value) => { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 				assert.strictEqual(parseInt(value, 10), 2); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-01-03 14:24:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should set then increment a key', (done) => { | 
					
						
							|  |  |  | 			db.set('myIncrement', 1, (err) => { | 
					
						
							| 
									
										
										
										
											2018-01-03 14:24:07 -05:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.increment('myIncrement', (err, value) => { | 
					
						
							| 
									
										
										
										
											2018-01-03 14:24:07 -05:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(value, 2); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					db.get('myIncrement', (err, value) => { | 
					
						
							| 
									
										
										
										
											2018-01-03 14:24:07 -05:00
										 |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert.equal(value, 2); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-11-07 15:04:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return the correct value', (done) => { | 
					
						
							|  |  |  | 			db.increment('testingCache', (err) => { | 
					
						
							| 
									
										
										
										
											2018-11-07 15:04:56 -05:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.get('testingCache', (err, value) => { | 
					
						
							| 
									
										
										
										
											2018-11-07 15:04:56 -05:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(value, 1); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					db.increment('testingCache', (err) => { | 
					
						
							| 
									
										
										
										
											2018-11-07 15:04:56 -05:00
										 |  |  | 						assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 						db.get('testingCache', (err, value) => { | 
					
						
							| 
									
										
										
										
											2018-11-07 15:04:56 -05:00
										 |  |  | 							assert.ifError(err); | 
					
						
							|  |  |  | 							assert.equal(value, 2); | 
					
						
							|  |  |  | 							done(); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('rename', () => { | 
					
						
							|  |  |  | 		it('should rename key to new name', (done) => { | 
					
						
							|  |  |  | 			db.set('keyOldName', 'renamedKeyValue', (err) => { | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return done(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				db.rename('keyOldName', 'keyNewName', function (err) { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(arguments.length < 2); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					db.get('keyNewName', (err, value) => { | 
					
						
							| 
									
										
										
										
											2017-10-23 18:31:16 -04:00
										 |  |  | 						assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 						assert.equal(value, 'renamedKeyValue'); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-09-26 15:02:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should rename multiple keys', (done) => { | 
					
						
							|  |  |  | 			db.sortedSetAdd('zsettorename', [1, 2, 3], ['value1', 'value2', 'value3'], (err) => { | 
					
						
							| 
									
										
										
										
											2018-09-26 15:02:57 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.rename('zsettorename', 'newzsetname', (err) => { | 
					
						
							| 
									
										
										
										
											2018-09-26 15:02:57 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					db.exists('zsettorename', (err, exists) => { | 
					
						
							| 
									
										
										
										
											2018-09-26 15:02:57 -04:00
										 |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert(!exists); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 						db.getSortedSetRange('newzsetname', 0, -1, (err, values) => { | 
					
						
							| 
									
										
										
										
											2018-09-26 15:02:57 -04:00
										 |  |  | 							assert.ifError(err); | 
					
						
							|  |  |  | 							assert.deepEqual(['value1', 'value2', 'value3'], values); | 
					
						
							|  |  |  | 							done(); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not error if old key does not exist', (done) => { | 
					
						
							|  |  |  | 			db.rename('doesnotexist', 'anotherdoesnotexist', (err) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.exists('anotherdoesnotexist', (err, exists) => { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(!exists); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('type', () => { | 
					
						
							|  |  |  | 		it('should return null if key does not exist', (done) => { | 
					
						
							|  |  |  | 			db.type('doesnotexist', (err, type) => { | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.strictEqual(type, null); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return hash as type', (done) => { | 
					
						
							|  |  |  | 			db.setObject('typeHash', { foo: 1 }, (err) => { | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.type('typeHash', (err, type) => { | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(type, 'hash'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return zset as type', (done) => { | 
					
						
							|  |  |  | 			db.sortedSetAdd('typeZset', 123, 'value1', (err) => { | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.type('typeZset', (err, type) => { | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(type, 'zset'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return set as type', (done) => { | 
					
						
							|  |  |  | 			db.setAdd('typeSet', 'value1', (err) => { | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.type('typeSet', (err, type) => { | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(type, 'set'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return list as type', (done) => { | 
					
						
							|  |  |  | 			db.listAppend('typeList', 'value1', (err) => { | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.type('typeList', (err, type) => { | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(type, 'list'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return string as type', (done) => { | 
					
						
							|  |  |  | 			db.set('typeString', 'value1', (err) => { | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.type('typeString', (err, type) => { | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(type, 'string'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-09-27 22:10:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should expire a key using seconds', (done) => { | 
					
						
							|  |  |  | 			db.expire('testKey', 86400, (err) => { | 
					
						
							| 
									
										
										
										
											2020-09-27 22:10:46 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.ttl('testKey', (err, ttl) => { | 
					
						
							| 
									
										
										
										
											2020-09-27 22:10:46 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(Math.round(86400 / 1000), Math.round(ttl / 1000)); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should expire a key using milliseconds', (done) => { | 
					
						
							|  |  |  | 			db.pexpire('testKey', 86400000, (err) => { | 
					
						
							| 
									
										
										
										
											2020-09-27 22:10:46 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.pttl('testKey', (err, pttl) => { | 
					
						
							| 
									
										
										
										
											2020-09-27 22:10:46 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(Math.round(86400000 / 1000000), Math.round(pttl / 1000000)); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-10-20 13:51:02 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | }); |