add dataValidation to Submission object

This commit is contained in:
Andrzej Stepien 2023-09-09 11:32:51 +02:00
parent 96101b11b1
commit fb64b41043
2 changed files with 75 additions and 2 deletions

View File

@ -1,8 +1,43 @@
import Entity from "./Entity.mjs"; import Entity from "./Entity.mjs";
import dataValidation from "./dataValidation.mjs";
export default class Submission extends Entity{ export default class Submission extends Entity{
set _story_id(prop){
if(prop){
if(!Number.isInteger(prop)){throw new TypeError("story_id must be an integer")}
this.story_id=prop
}
}
set _pub_id(prop){
if(prop){
if(!Number.isInteger(prop)){throw new TypeError("pub_id must be an integer")}
this.pub_id=prop
}
}
set _response_id(prop){
if(prop){
if(!Number.isInteger(prop)){throw new TypeError("response_id must be an integer")}
this.response_id=prop
}
}
set _date_submitted(prop){
if(prop){
if(!dataValidation.dateStringIsValid(prop)){throw new TypeError("response_id must be a valid date in YYYY-MM-DD format")}
this.date_submitted=prop
}
}
set _date_responded(prop){
if(prop){
if(!dataValidation.dateStringIsValid(prop)){throw new TypeError("date_responded must be a valid date in YYYY-MM-DD format")}
this.date_responded=prop
}
}
constructor(data){ constructor(data){
super(data) super(data)
this._story_id=data?.story_id
this._pub_id=data?.pub_id
this._response_id=data?.response_id
this._date_submitted=data?.date_submitted
this._date_responded=data?.date_responded
} }
} }

View File

@ -6,5 +6,43 @@ import { testDb as db } from "../db.mjs";
import Submission from "../objects/Submission.mjs" import Submission from "../objects/Submission.mjs"
chai.use(chaiAsPromised) chai.use(chaiAsPromised)
describe("testing Submission object",function(){ describe("testing Submission object",function(){
it("should throw if passed invalid") it("should throw if passed invalid story_id", function(){
expect(()=>{new Submission({story_id:"string"})}).to.throw(TypeError)
expect(()=>{new Submission({story_id:[]})}).to.throw(TypeError)
expect(()=>{new Submission({story_id:{}})}).to.throw(TypeError)
})
it("should throw if passed invalid pub_id", function(){
expect(()=>{new Submission({pub_id:"string"})}).to.throw(TypeError)
expect(()=>{new Submission({pub_id:[]})}).to.throw(TypeError)
expect(()=>{new Submission({pub_id:{}})}).to.throw(TypeError)
})
it("should throw if passed invalid response_id", function(){
expect(()=>{new Submission({response_id:"string"})}).to.throw(TypeError)
expect(()=>{new Submission({response_id:[]})}).to.throw(TypeError)
expect(()=>{new Submission({response_id:{}})}).to.throw(TypeError)
})
it("should throw if passed invalid date_submitted", function(){
expect(()=>{new Submission({date_submitted:"01-01-9999"})}).to.throw(TypeError)
expect(()=>{new Submission({date_submitted:"not even a date"})}).to.throw(TypeError)
expect(()=>{new Submission({date_submitted:{}})}).to.throw(TypeError)
})
it("should throw if passed invalid date_resonded", function(){
expect(()=>{new Submission({date_responded:"01-01-9999"})}).to.throw(TypeError)
expect(()=>{new Submission({date_responded:"not even a date"})}).to.throw(TypeError)
expect(()=>{new Submission({date_responded:{}})}).to.throw(TypeError)
})
it("should create an object with enumerable props equal to it's input if passed correct data", function(){
const goodData = {
pub_id:1,
story_id:1,
response_id:1,
date_submitted:'1999-01-01',
date_responded: '1999-01-02'
}
const submission = new Submission(goodData)
expect(submission).to.eql(goodData)
})
}) })