-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathroller.tests.js
121 lines (105 loc) · 3.8 KB
/
roller.tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* @author EmmanuelOlaojo
* @since 8/13/16
*/
module.exports = describe("Roller", function(){
after(require("./cleanup"));
describe("#roll", function(){
it("should throw original error on failure", function(){
task.save(TestMdlA, {name: "BoJack Horseman", age: 34})
.save(TestMdlB, {name: "Puss in Boots", age: 26})
.update(TestMdlA, {name: "BoJack Horseman"}, {name: "Samurai Jack", age: 300})
.update(TEST_COLLECTION_B, {name: "Puss in Boots"}, {name: "Aristocat", age: 6})
.save(TEST_COLLECTION_A, {_id: ["fail"]}, {name: "fail"});
return expect(task.run())
.to.eventually.be
.rejectedWith(/can't use an array for _id/);
});
it("should rollback save", function(){
return task.save(TestMdlA, {name: "Arya Stark", age: 34})
.save(TestMdlA, {_id: ["fail"]}, {name: "fail"})
.run()
.then(failure)
.catch(function(){
return expect(TestMdlA.find({name: "Arya Stark"}).exec())
.to.eventually.have.length(0);
});
});
it("should rollback update", function(){
return task.save(TestMdlA, {name: "Tyrion Lannister", age: 34})
.run()
.then(function(result){
// Tyrion's id
var id = result[0].ops[0]._id;
return task.update(TestMdlA, {_id: id}, {name: "Jamie", $inc: {age: 1}})
.save(TestMdlA, {_id: ["fail"]}, {name: "fail"})
.run()
.then(failure)
.catch(function(){
return expect(TestMdlA.find({name: "Tyrion Lannister"}).exec())
.to.eventually.have.length(1);
});
})
});
it("should rollback remove", function(){
return task.remove(TestMdlA, {name: "Tyrion Lannister"})
.save(TestMdlA, {_id: ["fail"]}, {name: "fail"})
.run()
.then(failure)
.catch(function(){
return expect(TestMdlA.find({name: "Tyrion Lannister"}).exec()).to.eventually.have.length(1);
});
});
it("should rollback save, update and remove with mongoose", function(){
var dog1 = new TestMdlC({name: "dog1", age: 2});
var dog2 = new TestMdlC({name: "dog2", age: 3});
var dog2b = {_id: dog2._id, name: dog2.name, age: 5};
var dog3 = new TestMdlC({age: 4});
return task.save(dog1)
.save(dog2)
.remove(dog1)
.update(dog2, dog2b)
.options({viaSave: true})
.save(dog3)
.run({useMongoose: true})
.then(failure)
.catch(function(){
return expect(TestMdlC.find().exec()).to.eventually.have.length(0);
});
});
it("should rollback file save", function () {
var gfs = Grid(mongoose.connection.db);
var id = dbUtils.generateId();
return task.saveFile(TEST_FILE_PATH, {_id: id})
.save(TestMdlA, {_id: ["fail"]}, {name: "fail"})
.run()
.then(failure)
.catch(function () {
return expect(dbUtils.fileExists(id, gfs)).to.eventually.equal(false);
});
});
it("should rollback file remove", function (done) {
var gfs = Grid(mongoose.connection.db);
var id = dbUtils.generateId();
var writeStream = gfs.createWriteStream({_id: id});
writeStream.on("close", function () {
task.removeFile({_id: id})
.save(TestMdlA, {_id: ["fail"]}, {name: "fail"})
.run()
.then(failure)
.catch(function () {
dbUtils.fileExists(id, gfs).then(function (exists) {
if (exists) dbUtils.removeFile(id, gfs);
expect(exists).to.equal(true);
done();
});
});
});
require("fs").createReadStream(TEST_FILE_PATH).pipe(writeStream);
});
});
});
function failure(){
// TestMdlA.find().exec(console.log);
throw new Error("failed");
}