diff --git a/dao/persondao.js b/dao/persondao.js index 987542b21d3ceef0e2423ce506a08a2323fbcea9..7d522361dfc59a592fce6a9b69b1f66b253bf343 100644 --- a/dao/persondao.js +++ b/dao/persondao.js @@ -21,4 +21,22 @@ module.exports = class PersonDao extends Dao { callback ); } + + updateOne(json, callback) { + var val = [json.navn, json.adresse, json.alder, json.id]; + super.query( + "update person set navn = ?, adresse = ?, alder = ? where id=?", + val, + callback + ); + } + + deleteOne(json, callback) { + var val = [json.id]; + super.query( + "delete from person where id =?", + val, + callback + ); + } }; diff --git a/dao/persondao.test.js b/dao/persondao.test.js index 79f786bff02a1e925f42f400f2d811203a9188d0..62864469c71fd775b58606f855e92891f7e18c32 100644 --- a/dao/persondao.test.js +++ b/dao/persondao.test.js @@ -5,14 +5,24 @@ const runsqlfile = require("./runsqlfile.js"); // GitLab CI Pool var pool = mysql.createPool({ - connectionLimit: 1, - host: "mysql", - user: "root", - password: "secret", - database: "supertestdb", - debug: false, - multipleStatements: true + connectionLimit: 2, + host: "mysql", + user: "root", + password: "secret", + database: "supertestdb", + debug: false, + multipleStatements: true }); +/* +var pool = mysql.createPool({ + connectionLimit: 2, + host: "mysql.stud.iie.ntnu.no", + user: "nilstesd", + password: "lqqWcMzq", + database: "nilstesd", + debug: false +}); + */ let personDao = new PersonDao(pool); @@ -77,3 +87,38 @@ test("get all persons from db", done => { personDao.getAll(callback); }); + +test("Update one person in db", done => { + function callback(status, data) { + console.log( + "Test callback: status=" + status + ", data=" + JSON.stringify(data) + ); + expect(data.length).toBe(0); + done(); + } + + personDao.updateOne({ navn: "Oppdatert Nilsen", alder: 99, adresse: "Gata 3", id:0 }, callback); +}); + +test("get all persons from db", done => { + function callback(status, data) { + console.log( + "Test callback: status=" + status + ", data.length=" + data.length + ); + expect(data.length).toBeGreaterThanOrEqual(2); + done(); + } + + personDao.getAll(callback); +}); + +test("delete person from db", done => { + function callback(status, data) { + console.log( + "Test callback: status=" + status + ", data=" + JSON.stringify(data) + ); + expect(data.affectedRows).toBeGreaterThanOrEqual(1); + done(); + } + personDao.deleteOne(0, callback); +});