| Conditions | 1 |
| Paths | 1 |
| Total Lines | 212 |
| Code Lines | 150 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* global it describe before */ |
||
| 18 | describe('deliveries', () => { |
||
| 19 | before(() => { |
||
| 20 | db.run("DELETE FROM deliveries", (err) => { |
||
| 21 | if (err) { |
||
| 22 | console.log("Could not empty test DB table deliveries", err.message); |
||
|
|
|||
| 23 | } |
||
| 24 | }); |
||
| 25 | }); |
||
| 26 | |||
| 27 | describe('GET /deliveries', () => { |
||
| 28 | it('should get 401 as we do not provide valid api_key', (done) => { |
||
| 29 | chai.request(server) |
||
| 30 | .get("/v1/deliveries") |
||
| 31 | .end((err, res) => { |
||
| 32 | res.should.have.status(401); |
||
| 33 | res.body.should.be.an("object"); |
||
| 34 | res.body.errors.status.should.be.equal(401); |
||
| 35 | done(); |
||
| 36 | }); |
||
| 37 | }); |
||
| 38 | |||
| 39 | it('should get 200 HAPPY PATH FROM GETTING API KEY', (done) => { |
||
| 40 | chai.request(server) |
||
| 41 | .get("/v1/[email protected]") |
||
| 42 | .end((err, res) => { |
||
| 43 | res.should.have.status(200); |
||
| 44 | res.body.should.be.an("object"); |
||
| 45 | res.body.data.should.be.an("object"); |
||
| 46 | res.body.data.should.have.property("key"); |
||
| 47 | |||
| 48 | apiKey = res.body.data.key; |
||
| 49 | |||
| 50 | done(); |
||
| 51 | }); |
||
| 52 | }); |
||
| 53 | |||
| 54 | it('should get 200 HAPPY PATH getting no deliveries', (done) => { |
||
| 55 | chai.request(server) |
||
| 56 | .get("/v1/deliveries?api_key=" + apiKey) |
||
| 57 | .end((err, res) => { |
||
| 58 | res.should.have.status(200); |
||
| 59 | res.body.should.be.an("object"); |
||
| 60 | res.body.data.should.be.an("array"); |
||
| 61 | res.body.data.length.should.be.equal(0); |
||
| 62 | |||
| 63 | done(); |
||
| 64 | }); |
||
| 65 | }); |
||
| 66 | }); |
||
| 67 | |||
| 68 | describe('POST /delivery', () => { |
||
| 69 | it('should get 500 as we do not supply id', (done) => { |
||
| 70 | let delivery = { |
||
| 71 | // id: 1, |
||
| 72 | product_id: 1, |
||
| 73 | amount: 10, |
||
| 74 | delivery_date: "2018-02-01", |
||
| 75 | comment: "Leverans av skruvar", |
||
| 76 | api_key: apiKey |
||
| 77 | }; |
||
| 78 | |||
| 79 | chai.request(server) |
||
| 80 | .post("/v1/delivery") |
||
| 81 | .send(delivery) |
||
| 82 | .end((err, res) => { |
||
| 83 | res.should.have.status(500); |
||
| 84 | res.body.should.be.an("object"); |
||
| 85 | res.body.should.have.property("errors"); |
||
| 86 | res.body.errors.should.have.property("status"); |
||
| 87 | res.body.errors.status.should.be.equal(500); |
||
| 88 | res.body.errors.should.have.property("detail"); |
||
| 89 | |||
| 90 | done(); |
||
| 91 | }); |
||
| 92 | }); |
||
| 93 | |||
| 94 | it('should get 500 as we do not supply productId', (done) => { |
||
| 95 | let delivery = { |
||
| 96 | id: 1, |
||
| 97 | // product_id: 1, |
||
| 98 | amount: 10, |
||
| 99 | delivery_date: "2018-02-01", |
||
| 100 | comment: "Leverans av skruvar", |
||
| 101 | api_key: apiKey |
||
| 102 | }; |
||
| 103 | |||
| 104 | chai.request(server) |
||
| 105 | .post("/v1/delivery") |
||
| 106 | .send(delivery) |
||
| 107 | .end((err, res) => { |
||
| 108 | res.should.have.status(500); |
||
| 109 | res.body.should.be.an("object"); |
||
| 110 | res.body.should.have.property("errors"); |
||
| 111 | res.body.errors.should.have.property("status"); |
||
| 112 | res.body.errors.status.should.be.equal(500); |
||
| 113 | res.body.errors.should.have.property("detail"); |
||
| 114 | |||
| 115 | done(); |
||
| 116 | }); |
||
| 117 | }); |
||
| 118 | |||
| 119 | it('should get 500 as we do not supply amount', (done) => { |
||
| 120 | let delivery = { |
||
| 121 | id: 1, |
||
| 122 | product_id: 1, |
||
| 123 | // amount: 10, |
||
| 124 | delivery_date: "2018-02-01", |
||
| 125 | comment: "Leverans av skruvar", |
||
| 126 | api_key: apiKey |
||
| 127 | }; |
||
| 128 | |||
| 129 | chai.request(server) |
||
| 130 | .post("/v1/delivery") |
||
| 131 | .send(delivery) |
||
| 132 | .end((err, res) => { |
||
| 133 | res.should.have.status(500); |
||
| 134 | res.body.should.be.an("object"); |
||
| 135 | res.body.should.have.property("errors"); |
||
| 136 | res.body.errors.should.have.property("status"); |
||
| 137 | res.body.errors.status.should.be.equal(500); |
||
| 138 | res.body.errors.should.have.property("detail"); |
||
| 139 | |||
| 140 | done(); |
||
| 141 | }); |
||
| 142 | }); |
||
| 143 | |||
| 144 | it('should get 500 as we do not supply delivery_date', (done) => { |
||
| 145 | let delivery = { |
||
| 146 | id: 1, |
||
| 147 | product_id: 1, |
||
| 148 | amount: 10, |
||
| 149 | // delivery_date: "2018-02-01", |
||
| 150 | comment: "Leverans av skruvar", |
||
| 151 | api_key: apiKey |
||
| 152 | }; |
||
| 153 | |||
| 154 | chai.request(server) |
||
| 155 | .post("/v1/delivery") |
||
| 156 | .send(delivery) |
||
| 157 | .end((err, res) => { |
||
| 158 | res.should.have.status(500); |
||
| 159 | res.body.should.be.an("object"); |
||
| 160 | res.body.should.have.property("errors"); |
||
| 161 | res.body.errors.should.have.property("status"); |
||
| 162 | res.body.errors.status.should.be.equal(500); |
||
| 163 | res.body.errors.should.have.property("detail"); |
||
| 164 | |||
| 165 | done(); |
||
| 166 | }); |
||
| 167 | }); |
||
| 168 | |||
| 169 | it('should get 201 HAPPY PATH', (done) => { |
||
| 170 | let delivery = { |
||
| 171 | id: 1, |
||
| 172 | product_id: 1, |
||
| 173 | amount: 10, |
||
| 174 | delivery_date: "2018-02-01", |
||
| 175 | comment: "Leverans av skruvar", |
||
| 176 | api_key: apiKey |
||
| 177 | }; |
||
| 178 | |||
| 179 | chai.request(server) |
||
| 180 | .post("/v1/delivery") |
||
| 181 | .send(delivery) |
||
| 182 | .end((err, res) => { |
||
| 183 | res.should.have.status(201); |
||
| 184 | res.body.should.be.an("object"); |
||
| 185 | res.body.should.have.property("data"); |
||
| 186 | |||
| 187 | done(); |
||
| 188 | }); |
||
| 189 | }); |
||
| 190 | |||
| 191 | it('should get 200 HAPPY PATH getting the one product we just created', (done) => { |
||
| 192 | chai.request(server) |
||
| 193 | .get("/v1/deliveries?api_key=" + apiKey) |
||
| 194 | .end((err, res) => { |
||
| 195 | res.should.have.status(200); |
||
| 196 | res.body.should.be.an("object"); |
||
| 197 | res.body.data.should.be.an("array"); |
||
| 198 | res.body.data.length.should.be.equal(1); |
||
| 199 | |||
| 200 | done(); |
||
| 201 | }); |
||
| 202 | }); |
||
| 203 | |||
| 204 | it('should get 500 UNIQUE CONSTRAINT', (done) => { |
||
| 205 | let delivery = { |
||
| 206 | id: 1, |
||
| 207 | product_id: 1, |
||
| 208 | amount: 10, |
||
| 209 | delivery_date: "2018-02-01", |
||
| 210 | comment: "Leverans av skruvar", |
||
| 211 | api_key: apiKey |
||
| 212 | }; |
||
| 213 | |||
| 214 | chai.request(server) |
||
| 215 | .post("/v1/delivery") |
||
| 216 | .send(delivery) |
||
| 217 | .end((err, res) => { |
||
| 218 | res.should.have.status(500); |
||
| 219 | res.body.should.be.an("object"); |
||
| 220 | res.body.should.have.property("errors"); |
||
| 221 | res.body.errors.should.have.property("status"); |
||
| 222 | res.body.errors.status.should.be.equal(500); |
||
| 223 | res.body.errors.should.have.property("detail"); |
||
| 224 | |||
| 225 | done(); |
||
| 226 | }); |
||
| 227 | }); |
||
| 228 | }); |
||
| 229 | }); |
||
| 230 |