From f6f8749223b7b8a1a4d679445899072341c1f1fe Mon Sep 17 00:00:00 2001 From: Maciej Matuszczak Date: Thu, 13 Feb 2020 15:30:28 +0100 Subject: [PATCH] [#121] Added school model, refactor code, fixtures and simple test --- package.json | 7 ++--- src/db/api.js | 14 +++++----- src/db/apply_fixture.js | 18 ------------- src/db/models.js | 16 +++--------- src/db/school_types.js | 6 +++++ src/db/ttt.js | 18 ------------- src/test/factories/schoolFactory.js | 22 ++++++++++++++++ src/test/factories/schoolTypeFactory.js | 26 ++++++------------- src/test/fixtures/apply_school_types.js | 8 ++++++ src/test/fixtures/apply_schools.js | 10 +++++++ src/test/models/test_school_model.js | 23 ++++++++++++++++ .../{test.js => test_school_type_model.js} | 8 +++--- src/test/truncate.js | 6 ++--- 13 files changed, 99 insertions(+), 83 deletions(-) delete mode 100644 src/db/apply_fixture.js create mode 100644 src/db/school_types.js delete mode 100644 src/db/ttt.js create mode 100644 src/test/factories/schoolFactory.js create mode 100644 src/test/fixtures/apply_school_types.js create mode 100644 src/test/fixtures/apply_schools.js create mode 100644 src/test/models/test_school_model.js rename src/test/models/{test.js => test_school_type_model.js} (72%) diff --git a/package.json b/package.json index c2f0d2d..88a1fb5 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,9 @@ "build": "webpack --mode production && electron-builder --windows --linux", "test": "jest", "rebuild": "electron-rebuild -f -w sqlite3", - "apply_fixture": "node src/db/apply_fixture.js", - "apply_fixture2": "node src/db/ttt.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", @@ -50,8 +51,8 @@ "@babel/plugin-transform-runtime": "^7.5.5", "@babel/preset-env": "^7.5.5", "@babel/preset-react": "^7.0.0", - "@testing-library/react": "^9.3.2", "@svgr/webpack": "^4.3.3", + "@testing-library/react": "^9.3.2", "babel-core": "^7.0.0-bridge.0", "babel-eslint": "10.0.1", "babel-jest": "^24.9.0", diff --git a/src/db/api.js b/src/db/api.js index 91c234b..a09da89 100644 --- a/src/db/api.js +++ b/src/db/api.js @@ -3,7 +3,7 @@ const { SchoolType, School } = require("./models"); const createSchoolType = schoolType => { SchoolType.sync({ force: true }).then(() => { return SchoolType.create({ - schoolType: schoolType + name: schoolType }); }); }; @@ -12,14 +12,14 @@ const getSchoolTypeList = () => { return SchoolType.findAll(); }; -const createSchool = type => { +const createSchool = ({ name, city, postCode, street, type }) => { School.sync({ force: true }).then(() => School.create({ - name: "TEST", - city: "Poznan", - postCode: "61-666", - street: "Mostowa 4", - schooltypeSchoolType: type || "przedszkole" + name: name, + city: city, + postCode: postCode, + street: street, + SchoolTypeName: type }) ); }; diff --git a/src/db/apply_fixture.js b/src/db/apply_fixture.js deleted file mode 100644 index 7a5f6b3..0000000 --- a/src/db/apply_fixture.js +++ /dev/null @@ -1,18 +0,0 @@ -const { createSchoolType, createSchool } = require("./api"); -const schoolTypeList = [ - "przedszkole", - "szkoła podstawowa", - "szkoła ponadpodstawowa", - "liceum ogólnokształcące" -]; - -const insertSchoolTypes = schoolTypeList => { - schoolTypeList.map(schoolType => createSchoolType(schoolType)); -}; - -const insertSchool = () => { - createSchool(); -}; - -insertSchoolTypes(schoolTypeList); -// insertSchool(); diff --git a/src/db/models.js b/src/db/models.js index e975ffc..2018f52 100644 --- a/src/db/models.js +++ b/src/db/models.js @@ -3,15 +3,10 @@ const Sequelize = require("sequelize"); const sequelize = require("./db_config").sequelize; const SchoolType = sequelize.define( - "schooltype", + "SchoolType", { - // id: { - // type: Sequelize.INTEGER, - // autoIncrement: true, - // primaryKey: true - // }, - schoolType: { - type: Sequelize.STRING, + name: { + type: Sequelize.STRING(80), allowNull: false, unique: true, primaryKey: true @@ -41,10 +36,7 @@ const School = sequelize.define( }, postCode: { type: Sequelize.STRING(6), - allowNull: false, - validation: { - postCodeFormatValidation: value => {} - } + allowNull: false }, street: { type: Sequelize.STRING(80), diff --git a/src/db/school_types.js b/src/db/school_types.js new file mode 100644 index 0000000..0fd1bf8 --- /dev/null +++ b/src/db/school_types.js @@ -0,0 +1,6 @@ +module.exports = [ + "przedszkole", + "szkoła podstawowa", + "szkoła zawodowa", + "liceum ogólnokształcące" +]; diff --git a/src/db/ttt.js b/src/db/ttt.js deleted file mode 100644 index 93d8a86..0000000 --- a/src/db/ttt.js +++ /dev/null @@ -1,18 +0,0 @@ -const { createSchoolType, createSchool } = require("./api"); -const schoolTypeList = [ - "przedszkole", - "szkoła podstawowa", - "szkoła ponadpodstawowa", - "liceum ogólnokształcące" -]; - -const insertSchoolTypes = schoolTypeList => { - schoolTypeList.map(schoolType => createSchoolType(schoolType)); -}; - -const insertSchool = () => { - createSchool(); -}; - -// insertSchoolTypes(schoolTypeList); -insertSchool(); diff --git a/src/test/factories/schoolFactory.js b/src/test/factories/schoolFactory.js new file mode 100644 index 0000000..1be931e --- /dev/null +++ b/src/test/factories/schoolFactory.js @@ -0,0 +1,22 @@ +const { School } = require("../../db/models"); +const schoolTypeList = require("../../db/school_types"); +const faker = require("faker/locale/pl"); + +const schoolData = () => { + const schoolType = faker.random.arrayElement(schoolTypeList); + return { + type: schoolType, + name: `${schoolType} nr. ${faker.random.number(200)}`, + city: faker.address.city(), + postCode: faker.address.zipCode(), + street: faker.address.streetAddress() + }; +}; + +const schoolFixture = async (props = {}) => { + const schoolData = schoolData(); + return await School.create({ schoolData, ...props }); +}; + +module.exports = schoolFixture; +module.exports.schoolData = schoolData; diff --git a/src/test/factories/schoolTypeFactory.js b/src/test/factories/schoolTypeFactory.js index 456f302..510cbd8 100644 --- a/src/test/factories/schoolTypeFactory.js +++ b/src/test/factories/schoolTypeFactory.js @@ -1,23 +1,13 @@ -const SchoolType = require("../../db/models"); +const { SchoolType } = require("../../db/models"); +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 => + await SchoolType.create({ name: name || schoolTypeData() }); module.exports = schoolTypeFixture; +module.exports.schoolTypeData = schoolTypeData; diff --git a/src/test/fixtures/apply_school_types.js b/src/test/fixtures/apply_school_types.js new file mode 100644 index 0000000..007e09d --- /dev/null +++ b/src/test/fixtures/apply_school_types.js @@ -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); diff --git a/src/test/fixtures/apply_schools.js b/src/test/fixtures/apply_schools.js new file mode 100644 index 0000000..dff943b --- /dev/null +++ b/src/test/fixtures/apply_schools.js @@ -0,0 +1,10 @@ +const { createSchool } = require("../../db/api"); +const { schoolData } = require("../factories/schoolFactory"); + +const insertSchool = () => { + Array(10) + .fill(0) + .forEach(() => createSchool(schoolData())); +}; + +insertSchool(); diff --git a/src/test/models/test_school_model.js b/src/test/models/test_school_model.js new file mode 100644 index 0000000..723287d --- /dev/null +++ b/src/test/models/test_school_model.js @@ -0,0 +1,23 @@ +const { sequelize } = require("../../db/db_config"); +const schoolFactory = require("../factories/schoolFactory"); +const truncate = require("../truncate"); +const { School } = require("../../db/models"); + +describe("School model", () => { + beforeEach(async () => { + await truncate(School); + }); + + afterEach(async () => { + await truncate(School); + }); + + it("should be created", async () => { + const school = await sequelize + .sync({ force: true }) + .then(() => schoolFactory({ name: "TEST" })); + + expect(school).toBeTruthy(); + expect(school.dataValues.name).toBe("TEST"); + }); +}); diff --git a/src/test/models/test.js b/src/test/models/test_school_type_model.js similarity index 72% rename from src/test/models/test.js rename to src/test/models/test_school_type_model.js index 3bb8027..5f143d8 100644 --- a/src/test/models/test.js +++ b/src/test/models/test_school_type_model.js @@ -1,21 +1,23 @@ const { sequelize } = require("../../db/db_config"); const schoolTypeFactory = require("../factories/schoolTypeFactory"); const truncate = require("../truncate"); +const { SchoolType } = require("../../db/models"); describe("SchoolType model", () => { beforeEach(async () => { - await truncate(); + await truncate(SchoolType); }); afterEach(async () => { - await truncate(); + await truncate(SchoolType); }); it("should be created", async () => { const schoolType = await sequelize .sync({ force: true }) .then(() => schoolTypeFactory("mock school type")); + expect(schoolType).toBeTruthy(); - expect(schoolType.dataValues.schoolType).toBe("mock school type"); + expect(schoolType.dataValues.name).toBe("mock school type"); }); }); diff --git a/src/test/truncate.js b/src/test/truncate.js index a3bedb1..0c6723c 100644 --- a/src/test/truncate.js +++ b/src/test/truncate.js @@ -1,5 +1,3 @@ -const SchoolType = require("../db/models"); - -module.exports = async function truncate() { - SchoolType.destroy({ where: {}, force: true }); +module.exports = async function truncate(model) { + model.destroy({ where: {}, force: true }); };