From 6e1112e77528b379fb0bb24d960bd37dd97ad837 Mon Sep 17 00:00:00 2001
From: Rudolf Copi Eckelberg <rce16@inf.ufpr.br>
Date: Tue, 4 Oct 2016 12:01:09 -0300
Subject: [PATCH] More simulation testing

---
 src/libs/routes/simulation.js |  2 +-
 src/test/test.js              | 50 +++++++++++++++++++++++++++++++++++
 test_config.json.example      | 21 +++++++++++++++
 3 files changed, 72 insertions(+), 1 deletion(-)
 create mode 100644 test_config.json.example

diff --git a/src/libs/routes/simulation.js b/src/libs/routes/simulation.js
index d3fcc2b5..9b97e7bb 100644
--- a/src/libs/routes/simulation.js
+++ b/src/libs/routes/simulation.js
@@ -59,7 +59,7 @@ simulationApp.get('/:id', (req, res) => {
         if (!simulation) {
             res.send({ success: false, msg: 'Entry not found' });
         } else {
-            res.send(JSON.stringify(simulation));
+            res.send(simulation);
         }
     });
 });
diff --git a/src/test/test.js b/src/test/test.js
index 90103f0a..092780a1 100644
--- a/src/test/test.js
+++ b/src/test/test.js
@@ -16,6 +16,9 @@ const should = chai.should(); // actually call the function
 
 const server = require('../libs/app');
 
+const mongoose = require('../libs/db/mongoose');
+const Simulation = require('../libs/models/simulation');
+
 chai.use(chaiHttp);
 
 describe('request enrollments', () => {
@@ -164,6 +167,12 @@ describe('request cities', () => {
 });
 
 describe('Create new sim', () => {
+    let newSimulation;
+
+    beforeEach(() => {
+        Simulation.remove();
+    });
+
     it('should return a new simulation id', (done) => {
         chai.request(server)
             .post('/api/v1/simulation')
@@ -178,4 +187,45 @@ describe('Create new sim', () => {
                 done();
             });
     });
+    it('should find an existing simulation', (done) => {
+        newSimulation = new Simulation();
+        newSimulation.name = 'test';
+        newSimulation.save((err, sim) => {
+            let id = sim._id;
+            chai.request(server)
+                .get(`/api/v1/simulation/${id}`)
+                .end((err, res) => {
+                    res.should.have.status(200);
+                    res.should.be.json;
+                    res.body.should.have.property('_id');
+                    res.body._id.should.be.a('string');
+                    res.body.should.have.property('name');
+                    res.body._id.should.be.a('string');
+                    done();
+                });
+        });
+    });
+    it('should update an existing simulation', (done) => {
+        newSimulation = new Simulation();
+        newSimulation.name = 'test';
+        newSimulation.save((err, sim) => {
+            let id = sim._id;
+            chai.request(server)
+                .post(`/api/v1/simulation/${id}`)
+                .send({location: 5})
+                .end((err, res) => {
+                    res.should.have.status(200);
+                    res.should.be.json;
+                    res.body.should.have.property('id');
+                    res.body.id.should.be.a('string');
+                    Simulation.findById(res.body.id, (err, simulation) => {
+                        simulation.should.have.property('name');
+                        simulation.name.should.be.a('string');
+                        simulation.should.have.property('location');
+                        simulation.location.should.be.a('number');
+                    });
+                    done();
+                });
+        });
+    });
 });
diff --git a/test_config.json.example b/test_config.json.example
new file mode 100644
index 00000000..0a8d9146
--- /dev/null
+++ b/test_config.json.example
@@ -0,0 +1,21 @@
+{
+    "port": 3000,
+    "ip": "127.0.0.1",
+    "debug" : true,
+    "monetdb": {
+        "host": "simcaqdb1",
+        "port": 50000,
+        "dbname": "simcaq_dev",
+        "user": "monetdb",
+        "password":"monetdb",
+        "nrConnections": "16"
+    },
+    "mongodb" : {
+        "uri": "mongodb://localhost/test_users"
+    },
+    "default": {
+        "api": {
+            "version" : "v1"
+        }
+    }
+}
-- 
GitLab