| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-17 21:55:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -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-29 16:20:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | describe('List methods', function () { | 
					
						
							|  |  |  | 	describe('listAppend()', function () { | 
					
						
							|  |  |  | 		it('should append to a list', function (done) { | 
					
						
							|  |  |  | 			db.listAppend('testList1', 5, function (err) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							|  |  |  | 				assert.equal(arguments.length, 1); | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('listPrepend()', function () { | 
					
						
							|  |  |  | 		it('should prepend to a list', function (done) { | 
					
						
							|  |  |  | 			db.listPrepend('testList2', 3, function (err) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							|  |  |  | 				assert.equal(arguments.length, 1); | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should prepend 2 more elements to a list', function (done) { | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 			async.series([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 					db.listPrepend('testList2', 2, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 					db.listPrepend('testList2', 1, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			], function (err) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('getListRange()', function () { | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 			async.series([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 					db.listAppend('testList3', 7, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 					db.listPrepend('testList3', 3, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 					db.listAppend('testList4', 5, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should return an empty list', function (done) { | 
					
						
							|  |  |  | 			db.getListRange('doesnotexist', 0, -1, function (err, list) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							|  |  |  | 				assert.equal(arguments.length, 2); | 
					
						
							|  |  |  | 				assert.equal(Array.isArray(list), true); | 
					
						
							|  |  |  | 				assert.equal(list.length, 0); | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should return a list with one element', function (done) { | 
					
						
							|  |  |  | 			db.getListRange('testList4', 0, 0, function (err, list) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							| 
									
										
										
										
											2015-01-15 15:06:02 -05:00
										 |  |  | 				assert.equal(Array.isArray(list), true); | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 				assert.equal(list[0], 5); | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should return a list with 2 elements 3, 7', function (done) { | 
					
						
							|  |  |  | 			db.getListRange('testList3', 0, -1, function (err, list) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							|  |  |  | 				assert.equal(Array.isArray(list), true); | 
					
						
							|  |  |  | 				assert.equal(list.length, 2); | 
					
						
							|  |  |  | 				assert.deepEqual(list, ['3', '7']); | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('listRemoveLast()', function () { | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 			async.series([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 					db.listAppend('testList4', 12, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 					db.listPrepend('testList4', 9, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should remove the last element of list and return it', function (done) { | 
					
						
							|  |  |  | 			db.listRemoveLast('testList4', function (err, lastElement) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							|  |  |  | 				assert.equal(arguments.length, 2); | 
					
						
							|  |  |  | 				assert.equal(lastElement, '12'); | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('listRemoveAll()', function () { | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 			async.series([ | 
					
						
							|  |  |  | 				async.apply(db.listAppend, 'testList5', 1), | 
					
						
							|  |  |  | 				async.apply(db.listAppend, 'testList5', 1), | 
					
						
							|  |  |  | 				async.apply(db.listAppend, 'testList5', 1), | 
					
						
							|  |  |  | 				async.apply(db.listAppend, 'testList5', 2), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				async.apply(db.listAppend, 'testList5', 5), | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should remove all the matching elements of list', function (done) { | 
					
						
							|  |  |  | 			db.listRemoveAll('testList5', '1', function (err) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							|  |  |  | 				assert.equal(arguments.length, 1); | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				db.getListRange('testList5', 0, -1, function (err, list) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 					assert.equal(err, null); | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 					assert.equal(Array.isArray(list), true); | 
					
						
							|  |  |  | 					assert.equal(list.length, 2); | 
					
						
							|  |  |  | 					assert.equal(list.indexOf('1'), -1); | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('listTrim()', function () { | 
					
						
							|  |  |  | 		it('should trim list to a certain range', function (done) { | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 			var list = ['1', '2', '3', '4', '5']; | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			async.eachSeries(list, function (value, next) { | 
					
						
							| 
									
										
										
										
											2015-01-15 14:02:51 -05:00
										 |  |  | 				db.listAppend('testList6', value, next); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			}, function (err) { | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return done(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				db.listTrim('testList6', 0, 2, function (err) { | 
					
						
							| 
									
										
										
										
											2015-01-15 15:33:22 -05:00
										 |  |  | 					assert.equal(err, null); | 
					
						
							|  |  |  | 					assert.equal(arguments.length, 1); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					db.getListRange('testList6', 0, -1, function (err, list) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 						assert.equal(err, null); | 
					
						
							| 
									
										
										
										
											2015-01-15 15:33:22 -05:00
										 |  |  | 						assert.equal(list.length, 3); | 
					
						
							|  |  |  | 						assert.deepEqual(list, ['1', '2', '3']); | 
					
						
							| 
									
										
										
										
											2014-12-29 17:34:56 -05:00
										 |  |  | 						done(); | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	after(function (done) { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		db.emptydb(done); | 
					
						
							| 
									
										
										
										
											2014-12-29 16:20:35 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }); |