Skip to content
Snippets Groups Projects
Commit 7f1576b7 authored by Ingelin Garmann's avatar Ingelin Garmann
Browse files

Added update and delete + tests

parent c3bcd161
Branches
No related tags found
No related merge requests found
Pipeline #22876 passed
CREATE TABLE person(
id int(11) NOT NULL AUTO_INCREMENT,
navn varchar(256) NOT NULL,
navn varchar(50) NOT NULL,
alder int(3) DEFAULT NULL,
adresse varchar(256) NOT NULL,
adresse varchar(255) NOT NULL,
bilde_base64 longtext,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
)
ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO person (id, navn, alder, adresse) VALUES
(1, 'Hei Sveisen', 21, 'Gata 1'),
(2, 'Hei Heisen', 22, 'Gata 2');
(NULL,'Hei Sveisen', 21, 'Gata 1'),
(NULL,'Hei Heisen', 22, 'Gata 2'),
(NULL,'Hei', 21, 'Gata 1'),
(NULL,'Nei Peisen', 22, 'Gata 2'),
(NULL,'Lei Feisen', 21, 'Gata 1'),
(NULL,'Sei Seisen', 22, 'Gata 2');
DROP TABLE IF EXISTS person;
......@@ -21,4 +21,21 @@ module.exports = class PersonDao extends Dao {
callback
);
}
updateOne(id, json, callback){
var info = [json.navn, json.adresse, json.alder, id];
super.query(
"update person set navn=?,adresse=?,alder=? where id=?",
info,
callback
);
}
deleteOne(id,callback){
super.query(
"delete from person where id=?",
[id],
callback
);
}
};
......@@ -4,6 +4,7 @@ const PersonDao = require("./persondao.js");
const runsqlfile = require("./runsqlfile.js");
// GitLab CI Pool
/*
var pool = mysql.createPool({
connectionLimit: 1,
host: "mysql",
......@@ -13,26 +14,41 @@ var pool = mysql.createPool({
debug: false,
multipleStatements: true
});
*/
var pool = mysql.createPool({
connectionLimit: 2,
host: "mysql.stud.iie.ntnu.no",
user: "ingelig",
password: "ZYC27GCD",
database: "ingelig",
debug: false
});
let personDao = new PersonDao(pool);
beforeAll(done => {
runsqlfile("dao/drop_tables.sql", pool, () => {
runsqlfile("dao/create_tables.sql", pool, () => {
runsqlfile("dao/create_testdata.sql", pool, done);
});
});
});
test("get one person from db", done => {
afterAll(() => {
pool.end();
});
test("get one person from db", async (done) => {
function callback(status, data) {
console.log(
"Test callback: status=" + status + ", data=" + JSON.stringify(data)
);
expect(data.length).toBe(1);
expect(data[0].navn).toBe("Hei Sveisen");
expect(data[0].navn).toBe("Hei Heisen");
done();
}
personDao.getOne(1, callback);
personDao.getOne(2, callback);
});
test("get unknown person from db", done => {
......@@ -73,3 +89,28 @@ test("get all persons from db", done => {
personDao.getAll(callback);
});
test("update one person in db", async (done) => {
function callback(status, data) {
console.log(
"Test callback: status=" + status + ", data=" + JSON.stringify(data)
);
expect(data.changedRows).toBe(1);
//expect(data[0].navn).toBe("Nytt Navn");
done();
}
personDao.updateOne(3, {navn: "Nytt Navn", adresse: "Ny adresse", alder: "100"},
callback
);
});
test("delete one person from db", done =>{
function callback(status, data){
console.log("Test callback: status="+status+", data.length="+data.length);
expect(data.affectedRows).toBe(1);
done();
}
personDao.deleteOne(1, callback);
});
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment