| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2015-01-03 20:08:49 -05:00
										 |  |  | /*global require, after*/ | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-17 20:20:42 -07:00
										 |  |  | var	async = require('async'); | 
					
						
							|  |  |  | var assert = require('assert'); | 
					
						
							|  |  |  | var db = require('../mocks/databasemock'); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | describe('Key methods', function () { | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	beforeEach(function (done) { | 
					
						
							| 
									
										
										
										
											2015-06-04 17:14:22 -04:00
										 |  |  | 		db.set('testKey', 'testValue', done); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	it('should set a key without error', function (done) { | 
					
						
							|  |  |  | 		db.set('testKey', 'testValue', function (err) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 			assert.equal(err, null); | 
					
						
							|  |  |  | 			assert.equal(arguments.length, 1); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	it('should get a key without error', function (done) { | 
					
						
							|  |  |  | 		db.get('testKey', function (err, value) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 			assert.equal(err, null); | 
					
						
							|  |  |  | 			assert.equal(arguments.length, 2); | 
					
						
							|  |  |  | 			assert.strictEqual(value, 'testValue'); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	it('should return true if key exist', function (done) { | 
					
						
							|  |  |  | 		db.exists('testKey', function (err, exists) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 			assert.equal(err, null); | 
					
						
							|  |  |  | 			assert.equal(arguments.length, 2); | 
					
						
							|  |  |  | 			assert.strictEqual(exists, true); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	it('should return false if key does not exist', function (done) { | 
					
						
							|  |  |  | 		db.exists('doesnotexist', function (err, exists) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 			assert.equal(err, null); | 
					
						
							|  |  |  | 			assert.equal(arguments.length, 2); | 
					
						
							|  |  |  | 			assert.strictEqual(exists, false); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	it('should delete a key without error', function (done) { | 
					
						
							|  |  |  | 		db.delete('testKey', function (err) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 			assert.equal(err, null); | 
					
						
							|  |  |  | 			assert.equal(arguments.length, 1); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			db.get('testKey', function (err, value) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							|  |  |  | 				assert.equal(false, !!value); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	it('should return false if key was deleted', function (done) { | 
					
						
							|  |  |  | 		db.delete('testKey', function (err) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 			assert.equal(err, null); | 
					
						
							| 
									
										
										
										
											2015-06-04 17:14:22 -04:00
										 |  |  | 			assert.equal(arguments.length, 1); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			db.exists('testKey', function (err, exists) { | 
					
						
							| 
									
										
										
										
											2015-06-04 17:14:22 -04:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							|  |  |  | 				assert.strictEqual(exists, false); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	it('should delete all keys passed in', function (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
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		], function (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) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							|  |  |  | 				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
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				}, function (err, results) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 					assert.equal(err, null); | 
					
						
							|  |  |  | 					assert.equal(results.key1exists, false); | 
					
						
							|  |  |  | 					assert.equal(results.key2exists, false); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('increment', function () { | 
					
						
							|  |  |  | 		it('should initialize key to 1', function (done) { | 
					
						
							|  |  |  | 			db.increment('keyToIncrement', function (err, value) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							|  |  |  | 				assert.strictEqual(parseInt(value, 10), 1); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should increment key to 2', function (done) { | 
					
						
							|  |  |  | 			db.increment('keyToIncrement', function (err, value) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							|  |  |  | 				assert.strictEqual(parseInt(value, 10), 2); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('rename', function () { | 
					
						
							|  |  |  | 		it('should rename key to new name', function (done) { | 
					
						
							|  |  |  | 			db.set('keyOldName', 'renamedKeyValue', function (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) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 					assert.equal(err, null); | 
					
						
							|  |  |  | 					assert.equal(arguments.length, 1); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					db.get('keyNewName', function (err, value) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:36:03 -05:00
										 |  |  | 						assert.equal(err, null); | 
					
						
							|  |  |  | 						assert.equal(value, 'renamedKeyValue'); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	after(function (done) { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		db.emptydb(done); | 
					
						
							| 
									
										
										
										
											2014-12-25 01:53:28 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }); |