-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#121] Added model of school #134
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
16ca881
[#121] POC School model
Maciek246 754a96a
[#121] Remove db file
Maciek246 f6f8749
[#121] Added school model, refactor code, fixtures and simple test
Maciek246 e5d3548
[#121] Virtual field [WiP]
Maciek246 1ce1e30
[#121] Added more tests
Maciek246 878703a
[#121] Tests
Maciek246 9b0a16e
[#121] Lint
Maciek246 d07986f
[#121] cross-env for jest
Maciek246 cac2bd3
[#121] Added validation of postCode
Maciek246 a7e64e2
Merge branch 'master' of https://github.com/CodeForPoznan/alinka-elec…
Maciek246 3a780a7
Merge branch '#121' of github.com:Maciek246/alinka-electron into #121
Maciek246 5146bba
[#121] Temporary test solution
Maciek246 e0b02f9
Fixed typo
Maciek246 d99e915
Post review changes
Maciek246 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const SchoolType = require("./school_type"); | ||
const School = require("./school"); | ||
|
||
module.exports = { | ||
SchoolType, | ||
School | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const Sequelize = require("sequelize"); | ||
const { DataTypes } = require("sequelize"); | ||
|
||
const sequelize = require("../db_config").sequelize; | ||
|
||
const School = sequelize.define( | ||
"School", | ||
{ | ||
id: { | ||
type: Sequelize.INTEGER, | ||
unique: true, | ||
autoIncrement: true, | ||
primaryKey: true | ||
}, | ||
name: { | ||
type: Sequelize.STRING(80), | ||
allowNull: false | ||
}, | ||
city: { | ||
type: Sequelize.STRING(80), | ||
allowNull: false | ||
}, | ||
postCode: { | ||
type: Sequelize.STRING(6), | ||
allowNull: false, | ||
validate: { | ||
isValidPolishPostCode(value) { | ||
const regex = /^[0-9]{2}-[0-9]{3}$/; | ||
if (!regex.test(value)) throw new Error("Invalid polish postal code"); | ||
} | ||
} | ||
}, | ||
postOffice: { | ||
type: Sequelize.STRING(80), | ||
allowNull: false | ||
}, | ||
street: { | ||
type: Sequelize.STRING(80), | ||
allowNull: false | ||
}, | ||
address: { | ||
type: Sequelize.VIRTUAL(DataTypes.STRING, ["city", "street"]), | ||
get() { | ||
return `${this.getDataValue("city")}, ${this.getDataValue("street")}`; | ||
} | ||
}, | ||
post: { | ||
type: Sequelize.VIRTUAL(DataTypes.STRING, ["postCode", "postOffice"]), | ||
get() { | ||
const { postOffice, city, postCode } = this; | ||
const postLocation = postOffice === city ? city : postOffice; | ||
return `${postCode} ${postLocation}`; | ||
} | ||
} | ||
}, | ||
{ | ||
// options | ||
} | ||
); | ||
|
||
module.exports = School; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const Sequelize = require("sequelize"); | ||
const sequelize = require("../db_config").sequelize; | ||
const School = require("./school"); | ||
|
||
const SchoolType = sequelize.define( | ||
"SchoolType", | ||
{ | ||
name: { | ||
type: Sequelize.STRING(80), | ||
allowNull: false, | ||
unique: true, | ||
primaryKey: true | ||
} | ||
}, | ||
{ | ||
// options | ||
} | ||
); | ||
|
||
SchoolType.hasMany(School, { foreignKey: { allowNull: false } }); | ||
|
||
module.exports = SchoolType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = [ | ||
"przedszkole", | ||
"szkoła podstawowa", | ||
"szkoła zawodowa", | ||
"liceum ogólnokształcące" | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const School = require("../../db/models/school"); | ||
const schoolTypeList = require("../../db/school_types"); | ||
const faker = require("faker/locale/pl"); | ||
|
||
const schoolData = type => { | ||
const schoolType = type || faker.random.arrayElement(schoolTypeList); | ||
const city = faker.address.city(); | ||
return { | ||
SchoolTypeName: schoolType, | ||
name: `${schoolType} nr. ${faker.random.number(200)}`, | ||
city: city, | ||
postCode: faker.address.zipCode(), | ||
street: faker.address.streetAddress(), | ||
postOffice: city | ||
}; | ||
}; | ||
|
||
const schoolFixture = async (props = {}) => { | ||
const data = schoolData(props.SchoolTypeName); | ||
return await School.create({ ...data, ...props }); | ||
}; | ||
|
||
module.exports = schoolFixture; | ||
module.exports.schoolData = schoolData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,14 @@ | ||
const SchoolType = require("../../db/models"); | ||
const SchoolType = require("../../db/models/school_type"); | ||
const schoolTypeList = require("../../db/school_types"); | ||
const faker = require("faker/locale/pl"); | ||
|
||
const schoolTypeData = (props = {}) => { | ||
const schoolTypeList = [ | ||
"przedszkole", | ||
"szkoła podstawowa", | ||
"szkoła zawodowa", | ||
"liceum ogólnokształcące" | ||
]; | ||
|
||
const getSchoolType = array => { | ||
const randomArrayItemNumber = Math.floor(Math.random() * array.length); | ||
const school_type = schoolTypeList[randomArrayItemNumber]; | ||
return school_type; | ||
}; | ||
|
||
return { schoolType: props || getSchoolType(schoolTypeList) }; | ||
const schoolTypeData = () => { | ||
return faker.random.arrayElement(schoolTypeList); | ||
}; | ||
|
||
const schoolTypeFixture = async (props = {}) => | ||
await SchoolType.create(schoolTypeData(props)); | ||
const schoolTypeFixture = async name => { | ||
return await SchoolType.create({ name: name || schoolTypeData() }); | ||
}; | ||
|
||
module.exports = schoolTypeFixture; | ||
module.exports.schoolTypeData = schoolTypeData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const { createSchoolType } = require("../../db/api"); | ||
const schoolTypeList = require("../../db/school_types"); | ||
|
||
const insertSchoolTypes = schoolTypeList => { | ||
schoolTypeList.map(schoolType => createSchoolType(schoolType)); | ||
}; | ||
|
||
insertSchoolTypes(schoolTypeList); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const { createSchool } = require("../../db/api"); | ||
const { schoolData } = require("../factories/schoolFactory"); | ||
|
||
const insertSchool = numberOfSchools => { | ||
Array(numberOfSchools || 10) | ||
.fill(0) | ||
.forEach(() => createSchool(schoolData())); | ||
}; | ||
|
||
insertSchool(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const { sequelize } = require("../../db/db_config"); | ||
const schoolFactory = require("../factories/schoolFactory"); | ||
const schoolTypeFactory = require("../factories/schoolTypeFactory"); | ||
const { schoolData } = require("../factories/schoolFactory"); | ||
const { School } = require("../../db/models"); | ||
|
||
describe("School model", () => { | ||
beforeAll(() => { | ||
return sequelize | ||
.sync({ force: true }) | ||
.then(() => schoolTypeFactory("mock")); | ||
}); | ||
|
||
beforeEach(() => { | ||
// I know, this is the same as code in `beforeAll` but | ||
// this is temporary solution until problem with creating DB in tests should be resolved | ||
return sequelize | ||
.sync({ force: true }) | ||
.then(() => schoolTypeFactory("mock")); | ||
}); | ||
|
||
afterAll(async () => { | ||
await sequelize.drop(); | ||
}); | ||
|
||
it("should be created", async () => { | ||
const school = await School.create({ ...schoolData("mock"), name: "TEST" }); | ||
|
||
expect(school).toBeTruthy(); | ||
expect(school.name).toEqual("TEST"); | ||
}); | ||
|
||
it("should validate `postCode`", async () => { | ||
const school = await sequelize | ||
.sync({ force: true }) | ||
.then(() => | ||
schoolFactory({ | ||
type: "mock", | ||
postCode: "invalid code", | ||
SchoolTypeName: "mock" | ||
}) | ||
) | ||
.catch(err => err.message); | ||
|
||
expect(school).toEqual("Validation error: Invalid polish postal code"); | ||
}); | ||
|
||
describe("has virtual methods which", () => { | ||
it("return concatenated address data by calling `address`", async () => { | ||
const school = await schoolFactory({ | ||
street: "Testowa 2", | ||
city: "Poznan", | ||
SchoolTypeName: "mock" | ||
}); | ||
|
||
expect(school.address).toEqual("Poznan, Testowa 2"); | ||
}); | ||
|
||
it("return concatenated post data by calling `post`", async () => { | ||
const school = await schoolFactory({ | ||
postCode: "12-345", | ||
postOffice: "Poznan", | ||
SchoolTypeName: "mock" | ||
}); | ||
|
||
expect(school.post).toEqual("12-345 Poznan"); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you think it's better to have fixed version of this so vital libraries, to be consistent with node version and so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I do. Probably range mark has occurred when I've synchronized with master and I've used
npm install
. It will be fixed.