Skip to content

Commit

Permalink
[CodeForPoznan#121] Added school model, refactor code, fixtures and s…
Browse files Browse the repository at this point in the history
…imple test
  • Loading branch information
Maciek246 committed Feb 13, 2020
1 parent 754a96a commit f6f8749
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 83 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions src/db/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { SchoolType, School } = require("./models");
const createSchoolType = schoolType => {
SchoolType.sync({ force: true }).then(() => {
return SchoolType.create({
schoolType: schoolType
name: schoolType
});
});
};
Expand All @@ -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
})
);
};
Expand Down
18 changes: 0 additions & 18 deletions src/db/apply_fixture.js

This file was deleted.

16 changes: 4 additions & 12 deletions src/db/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
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"
];
18 changes: 0 additions & 18 deletions src/db/ttt.js

This file was deleted.

22 changes: 22 additions & 0 deletions src/test/factories/schoolFactory.js
Original file line number Diff line number Diff line change
@@ -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;
26 changes: 8 additions & 18 deletions src/test/factories/schoolTypeFactory.js
Original file line number Diff line number Diff line change
@@ -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;
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 = () => {
Array(10)
.fill(0)
.forEach(() => createSchool(schoolData()));
};

insertSchool();
23 changes: 23 additions & 0 deletions src/test/models/test_school_model.js
Original file line number Diff line number Diff line change
@@ -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");
});
});
Original file line number Diff line number Diff line change
@@ -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");
});
});
6 changes: 2 additions & 4 deletions src/test/truncate.js
Original file line number Diff line number Diff line change
@@ -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 });
};

0 comments on commit f6f8749

Please sign in to comment.