1 | var chai = require("chai")
|
2 | var should = chai.should();
|
3 |
|
4 | chai.use(require("chai-http"));
|
5 |
|
6 | var request = chai.request(require("./server"))
|
7 | var db = require("mongojs")("test")
|
8 |
|
9 | describe("CRUD Handler", () => {
|
10 | before(done => {
|
11 | done()
|
12 | })
|
13 | beforeEach(done => {
|
14 | db.dropDatabase(done);
|
15 | })
|
16 |
|
17 | after(done => {
|
18 | db.dropDatabase(done)
|
19 | })
|
20 |
|
21 | it("should save a document", (done) => {
|
22 | var data = {
|
23 | name: "test"
|
24 | }
|
25 |
|
26 | request.post("/test")
|
27 | .send(data)
|
28 | .end((err, res) => {
|
29 | should.not.exist(err)
|
30 | res.should.have.status(201);
|
31 | res.body.should.be.an("object")
|
32 | res.body.should.have.keys(['error', 'data'])
|
33 | res.body.error.should.not.be.ok;
|
34 | res.body.data.name.should.be.eql("test")
|
35 | res.body.data.should.have.property("_id");
|
36 |
|
37 | done()
|
38 | })
|
39 | })
|
40 | it("should list all documents", (done ) => {
|
41 | var list = [
|
42 | {name: 'test21'}, {name: 'test22'}
|
43 | ]
|
44 |
|
45 | db.collection("test").insert(list, (err, result) => {
|
46 | request.get("/test")
|
47 | .end((err, res) => {
|
48 | should.not.exist(err)
|
49 | res.should.have.status(200)
|
50 | res.body.should.be.an("object")
|
51 | res.body.should.have.keys(['error', 'data'])
|
52 | res.body.error.should.not.be.ok;
|
53 | res.body.data.should.have.keys(['count', 'list'])
|
54 | res.body.data.count.should.be.eql(2)
|
55 | res.body.data.list.length.should.be.eql(2)
|
56 |
|
57 | done()
|
58 | })
|
59 | })
|
60 | })
|
61 | it("should get a single document", done => {
|
62 | var doc = {
|
63 | name: 'test3'
|
64 | }
|
65 |
|
66 | db.collection('test').insert(doc, (err, result ) => {
|
67 | request.get("/test/" + result._id)
|
68 | .end((err, res) => {
|
69 | should.not.exist(err)
|
70 | res.should.have.status(200)
|
71 | res.body.should.be.an("object")
|
72 | res.body.should.have.keys(['error', 'data'])
|
73 | res.body.error.should.not.be.ok;
|
74 | res.body.data.should.have.keys(['_id', 'name'])
|
75 | res.body.data.name.should.be.eql(doc.name);
|
76 |
|
77 | done()
|
78 | })
|
79 | })
|
80 | })
|
81 | it("should update an existing document", done => {
|
82 | var doc = {
|
83 | name: 'test3'
|
84 | }
|
85 |
|
86 | db.collection('test').insert(doc, (err, result ) => {
|
87 | result.name = "test3_updated";
|
88 |
|
89 | request.put("/test/" + result._id)
|
90 | .send(result)
|
91 | .end((err, res) => {
|
92 | should.not.exist(err)
|
93 | res.should.have.status(202)
|
94 | res.body.should.be.an("object")
|
95 | res.body.should.have.keys(['error', 'data'])
|
96 | res.body.error.should.not.be.ok;
|
97 |
|
98 | db.collection('test').findOne({_id: db.ObjectId(result._id)}, (err, result1) => {
|
99 | should.not.exist(err)
|
100 | result1.name.should.be.eql(result.name);
|
101 |
|
102 | done()
|
103 | })
|
104 |
|
105 | })
|
106 | })
|
107 | })
|
108 | it("should remove a document", done => {
|
109 | var doc = {
|
110 | name: 'test3'
|
111 | }
|
112 |
|
113 | db.collection('test').insert(doc, (err, result ) => {
|
114 | request.delete("/test/" + result._id)
|
115 | .end((err, res) => {
|
116 | should.not.exist(err)
|
117 | res.should.have.status(202)
|
118 | res.body.should.be.an("object")
|
119 | res.body.should.have.keys(['error', 'data'])
|
120 | res.body.error.should.not.be.ok;
|
121 |
|
122 | db.collection('test').findOne({_id: db.ObjectId(result._id)}, (err, result1) => {
|
123 | should.not.exist(err)
|
124 | should.not.exist(result1);
|
125 |
|
126 | done()
|
127 | })
|
128 |
|
129 | })
|
130 | })
|
131 | })
|
132 | }) |
\ | No newline at end of file |