Skip to content
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 14 commits into from
Apr 24, 2020
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
"electron:dev": "MODE=dev electron .",
"start": "concurrently \"cross-env BROWSER=none npm run webpack:dev\" \"wait-on http://localhost:9000 && npm run electron:dev\"",
"build": "webpack --mode production && electron-builder --windows --linux",
"test": "jest",
"test": "cross-env MODE=dev jest",
"rebuild": "electron-rebuild -f -w sqlite3",
"apply_fixture": "node src/db/apply_fixture.js"
"apply_school_types": "node src/test/fixtures/apply_school_types.js",
"apply_schools": "node src/test/fixtures/apply_schools.js",
"apply_fixtures": "npm run apply_school_types && npm run apply_schools"
},
"dependencies": {
"@babel/runtime": "^7.8.3",
Expand All @@ -40,7 +42,7 @@
"react-final-form": "^6.3.3",
"react-final-form-arrays": "^3.1.1",
"sequelize": "5.21.2",
"sqlite3": "4.1.0"
"sqlite3": "^4.1.0"
Copy link
Contributor

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?

Copy link
Member Author

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.

},
"devDependencies": {
"@babel/cli": "^7.5.5",
Expand Down Expand Up @@ -81,7 +83,6 @@
"prettier": "1.17.1",
"react-test-renderer": "16.8.0",
"sass-loader": "^7.1.0",
"sqlite3": "4.1.0",
"style-loader": "^0.23.1",
"url-loader": "^2.3.0",
"wait-on": "^3.2.0",
Expand Down
35 changes: 32 additions & 3 deletions src/db/api.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const SchoolType = require("./models");
const { SchoolType, School } = require("./models");

const createSchoolType = schoolType => {
SchoolType.sync({ force: true }).then(() => {
return SchoolType.create({
schoolType: schoolType
name: schoolType
});
});
};
Expand All @@ -12,4 +12,33 @@ const getSchoolTypeList = () => {
return SchoolType.findAll();
};

module.exports = { createSchoolType, getSchoolTypeList };
const createSchool = ({
name,
city,
postCode,
street,
SchoolTypeName,
postOffice
}) => {
School.sync({ force: true }).then(() =>
School.create({
name,
city,
postCode,
street,
SchoolTypeName,
postOffice
})
);
};

const getSchoolList = () => {
return School.findAll();
};

module.exports = {
createSchoolType,
getSchoolTypeList,
createSchool,
getSchoolList
};
13 changes: 0 additions & 13 deletions src/db/apply_fixture.js

This file was deleted.

3 changes: 2 additions & 1 deletion src/db/db_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const sequelize = new Sequelize({
storage: path.resolve(__dirname, "../alinka.db"),
dialectOptions: {
requestTimeout: 300000
}
},
logging: false
});

sequelize
Expand Down
19 changes: 0 additions & 19 deletions src/db/models.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/db/models/index.js
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
};
61 changes: 61 additions & 0 deletions src/db/models/school.js
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;
22 changes: 22 additions & 0 deletions src/db/models/school_type.js
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;
6 changes: 6 additions & 0 deletions src/db/school_types.js
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"
];
24 changes: 24 additions & 0 deletions src/test/factories/schoolFactory.js
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;
27 changes: 9 additions & 18 deletions src/test/factories/schoolTypeFactory.js
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;
8 changes: 8 additions & 0 deletions src/test/fixtures/apply_school_types.js
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);
10 changes: 10 additions & 0 deletions src/test/fixtures/apply_schools.js
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();
69 changes: 69 additions & 0 deletions src/test/models/school_model.test.js
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");
});
});
});
Loading