backends.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2015 The MemDB Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. var P = require('bluebird');
  17. var should = require('should');
  18. var backends = require('../../app/backends');
  19. var env = require('../env');
  20. describe('backends test', function(){
  21. beforeEach(env.flushdb);
  22. var testFunc = function(backend){
  23. var item1 = {name : 'test', id : 1, doc : {_id : 1, k : 1}};
  24. var item2 = {name : 'test2', id : 1, doc : {_id : 1, k2 : 1}};
  25. return P.try(function(){
  26. return backend.start();
  27. })
  28. .then(function(){
  29. return backend.setMulti([item1, item2]);
  30. })
  31. .then(function(){
  32. return backend.get(item1.name, item1.id)
  33. .then(function(ret){
  34. ret.should.eql(item1.doc);
  35. });
  36. })
  37. .then(function(){
  38. return backend.get(item2.name, item2.id)
  39. .then(function(ret){
  40. ret.should.eql(item2.doc);
  41. });
  42. })
  43. .then(function(){
  44. // update item1
  45. item1.doc.k = 2;
  46. // remove item2
  47. item2.doc = null;
  48. return backend.setMulti([item1, item2]);
  49. })
  50. .then(function(){
  51. return backend.get(item1.name, item1.id)
  52. .then(function(ret){
  53. ret.should.eql(item1.doc);
  54. });
  55. })
  56. .then(function(){
  57. return backend.get(item2.name, item2.id)
  58. .then(function(ret){
  59. (ret === null).should.be.true; // jshint ignore:line
  60. });
  61. })
  62. .then(function(){
  63. return backend.drop();
  64. })
  65. .finally(function(){
  66. return backend.stop();
  67. });
  68. };
  69. it('mongo backend', function(cb){
  70. var opts = {
  71. engine : 'mongodb',
  72. url : env.config.backend.url,
  73. shardId : 's1',
  74. };
  75. var backend = backends.create(opts);
  76. return testFunc(backend)
  77. .nodeify(cb);
  78. });
  79. it('redis backend', function(cb){
  80. var opts = {
  81. engine : 'redis',
  82. host : env.config.locking.host,
  83. port : env.config.locking.port,
  84. db : env.config.locking.db,
  85. shardId : 's1',
  86. };
  87. var backend = backends.create(opts);
  88. return testFunc(backend)
  89. .nodeify(cb);
  90. });
  91. });