Commit 9eb99caa authored by Lucas Fernandes de Oliveira's avatar Lucas Fernandes de Oliveira

Merge branch '24-inserir-resposta-no-banco' into 'develop'

Resolve "Inserir resposta no banco"

Closes #24

See merge request !20
parents a575e0ec 1bea58f0
Pipeline #20511 passed with stages
in 1 minute and 6 seconds
......@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## 0.0.18 - 25-05-2019
### Added
- Create readFormAnswer method to read formAnswer from database #24 (Horstmann)
- Create writeFormAnswer method to insert formAnswer into database #24 (Horstmann)
- Create TestHandler to tests FormAnswers
### Changed
- Fix OptHandler to return id in inputAnswer
## 0.0.17 - 25-05-2019
### Added
......
{
"name": "form-creator-api",
"version": "0.0.16",
"version": "0.0.18",
"description": "RESTful API used to manage and answer forms.",
"main": "index.js",
"scripts": {
......
......@@ -22,7 +22,9 @@ import { series } from "async";
import { expect } from "chai";
import { QueryResult } from "pg";
import { Form, FormOptions } from "../core/form";
import { FormAnswer, FormAnswerOptions } from "../core/formAnswer";
import { Input, InputOptions, Validation } from "../core/input";
import { InputAnswer, InputAnswerDict, InputAnswerOptions, InputAnswerOptionsDict } from "../core/inputAnswer";
import { configs } from "./config";
import { DbHandler, QueryOptions } from "./dbHandler";
import { InputType, ValidationType } from "./enumHandler";
......@@ -490,4 +492,100 @@ describe("Read and Write on Database", () => {
});
it("should read an existent form Answer", (done) => {
const inputAnswersOpt1: InputAnswerOptions = {
id: 5
, idInput: 1
, placement: 0
, value: "Answer to Question 1 Form 1"
};
const inputAnswersOpt2: InputAnswerOptions = {
id: 6
, idInput: 2
, placement: 0
, value: "Answer to Question 2 Form 1"
};
const inputAnswersOpt3: InputAnswerOptions = {
id: 7
, idInput: 3
, placement: 0
, value: "Answer to Question 3 Form 1"
};
const inputAnswerOptionsDict: InputAnswerOptionsDict = {1: [inputAnswersOpt1], 2: [inputAnswersOpt2], 3: [inputAnswersOpt3]};
const data: Date = new Date("2019-02-21 12:10:25");
dbhandler.readForm(1, (error: Error, form: Form) => {
const formAnswerOptions: FormAnswerOptions = {
id: 3
, form
, timestamp: data
, inputsAnswerOptions: inputAnswerOptionsDict
};
dbhandler.readFormAnswer(3, (err: Error, formAnswer: FormAnswer) => {
TestHandler.testFormAnswer(formAnswer, new FormAnswer(OptHandler.formAnswer(formAnswerOptions)));
done();
});
});
});
it("should read a non existent form Answer", (done) => {
dbhandler.readFormAnswer(25, (err: Error, formAnswer: FormAnswer) => {
expect(err).to.not.equal(null);
expect(formAnswer).to.be.undefined;
done();
});
});
it("should write form Answer", (done) => {
const inputAnswersOpt1: InputAnswerOptions = {
id: 5
, idInput: 1
, placement: 0
, value: "Answer to Question 1 Form 1"
};
const inputAnswersOpt2: InputAnswerOptions = {
id: 6
, idInput: 2
, placement: 0
, value: "Answer to Question 2 Form 1"
};
const inputAnswersOpt3: InputAnswerOptions = {
id: 7
, idInput: 3
, placement: 0
, value: "Answer to Question 3 Form 1"
};
const inputAnswerOptionsDict: InputAnswerOptionsDict = {1: [inputAnswersOpt1], 2: [inputAnswersOpt2], 3: [inputAnswersOpt3]};
const data: Date = new Date(2019, 6, 4);
dbhandler.readForm(1, (error: Error, form: Form) => {
const formAnswerOptions: FormAnswerOptions = {
form
, timestamp: data
, inputsAnswerOptions: inputAnswerOptionsDict
};
const formAnswer = new FormAnswer(OptHandler.formAnswer(formAnswerOptions));
dbhandler.writeFormAnswer(formAnswer, (err: Error, formAnswerResult: FormAnswer) => {
expect(err).to.be.a("null");
expect(formAnswerResult.id).to.be.equal(7);
let inputAnswerId: number = 15;
for (const key of Object.keys(formAnswerResult.inputAnswers)){
for (const inputAnswer of formAnswerResult.inputAnswers[parseInt(key, 10)]){
expect(inputAnswer.id).to.be.equal(inputAnswerId);
inputAnswerId++;
}
}
done();
});
});
});
});
This diff is collapsed.
......@@ -144,7 +144,8 @@
}
const option: InputAnswerOptions = {
idInput: obj.idInput
id: obj.id
, idInput: obj.idInput
, placement: obj.placement
, value: obj.value
};
......
......@@ -21,7 +21,9 @@
import { expect } from "chai";
import { Form } from "../core/form";
import { FormAnswer} from "../core/formAnswer";
import { Input, Validation } from "../core/input";
import { InputAnswer } from "../core/inputAnswer";
import { EnumHandler, InputType, ValidationType } from "./enumHandler";
/**
......@@ -69,4 +71,36 @@
}
}
/**
* Verify if two formAnswers are semantically equal;
* @param formAnswer - Form Answer that should be tested
* @param stub - A model formAnswer that first param itend to be.
* @returns - True if formAnswers are equal else false
*/
public static testFormAnswer(formAnswer: FormAnswer, stub: FormAnswer){
expect(formAnswer.id).to.be.equal(stub.id);
TestHandler.testForm(formAnswer.form, stub.form);
for (const key of Object.keys(formAnswer.inputAnswers)){
formAnswer.inputAnswers[parseInt(key, 10)].forEach((inputAnswer, i) => {
TestHandler.testInputAnswer(inputAnswer, stub.inputAnswers[parseInt(key, 10)][i]);
});
}
expect(formAnswer.timestamp.toISOString()).to.be.equal(stub.timestamp.toISOString());
}
/**
* Verify if two inputAnswer are semantically equal;
* @param inputAnswer - inputAnswer that should be tested
* @param stub - A model inputAnswer that first param itend to be.
* @returns - True if inputAnswer are equal else false
*/
public static testInputAnswer(inputAnswer: InputAnswer, stub: InputAnswer){
expect(inputAnswer.id).to.be.equal(stub.id);
expect(inputAnswer.idInput).to.be.equal(stub.idInput);
expect(inputAnswer.placement).to.be.equal(stub.placement);
expect(inputAnswer.value).to.be.equal(stub.value);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment