Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciek246 committed Feb 14, 2020
1 parent 1ce1e30 commit 878703a
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 116 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"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": "MODE=dev jest",
"rebuild": "electron-rebuild -f -w sqlite3",
"apply_school_types": "node src/test/fixtures/apply_school_types.js",
"apply_schools": "node src/test/fixtures/apply_schools.js",
Expand Down
14 changes: 7 additions & 7 deletions src/db/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const getSchoolTypeList = () => {
return SchoolType.findAll();
};

const createSchool = ({ name, city, postCode, street, type, postOffice }) => {
const createSchool = ({ name, city, postCode, street, SchoolTypeName, postOffice }) => {
School.sync({ force: true }).then(() =>
School.create({
name: name,
city: city,
postCode: postCode,
street: street,
SchoolTypeName: type,
postOffice: postOffice,
name,
city,
postCode,
street,
SchoolTypeName,
postOffice
})
);
};
Expand Down
72 changes: 0 additions & 72 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
}
55 changes: 55 additions & 0 deletions src/db/models/school.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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
},
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: 3 additions & 3 deletions src/test/factories/schoolFactory.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { School } = require("../../db/models");
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 {
type: schoolType,
SchoolTypeName: schoolType,
name: `${schoolType} nr. ${faker.random.number(200)}`,
city: city,
postCode: faker.address.zipCode(),
Expand All @@ -16,7 +16,7 @@ const schoolData = type => {
};

const schoolFixture = async (props = {}) => {
const data = schoolData(props.type);
const data = schoolData(props.SchoolTypeName);
return await School.create({ ...data, ...props });
};

Expand Down
7 changes: 4 additions & 3 deletions src/test/factories/schoolTypeFactory.js
Original file line number Diff line number Diff line change
@@ -1,13 +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 = () => {
return faker.random.arrayElement(schoolTypeList);
};

const schoolTypeFixture = async name =>
await SchoolType.create({ name: name || schoolTypeData() });
const schoolTypeFixture = async name =>{
return await SchoolType.create({ name: name || schoolTypeData() });
}

module.exports = schoolTypeFixture;
module.exports.schoolTypeData = schoolTypeData;
50 changes: 20 additions & 30 deletions src/test/models/school_model.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
const { sequelize } = require("../../db/db_config");
const schoolFactory = require("../factories/schoolFactory");
const schoolTypeFactory = require("../factories/schoolTypeFactory");
const truncate = require("../truncate");
const { School, SchoolType } = require("../../db/models");
const { schoolData } = require("../factories/schoolFactory");
const { School } = require("../../db/models");

describe("School model", () => {
beforeEach(async () => {
await truncate([School, SchoolType]);
await SchoolType.sync({ force: true });
await schoolTypeFactory("przedszkole")
beforeAll(() => {
return sequelize
.sync({ force: true })
.then(() => schoolTypeFactory("mock"));
});

afterEach(async () => {
await truncate([School, SchoolType]);
afterAll(async () => {
await sequelize.drop();
});

it("should be created", async () => {
const school = await sequelize
.sync({ force: true })
.then(() => schoolFactory({ name: "TEST", type: "przedszkole" }));
const school = await School.create({ ...schoolData("mock"), name: "TEST" });

expect(school).toBeTruthy();
expect(school.name).toEqual("TEST");
Expand All @@ -30,31 +28,23 @@ describe("School model", () => {
// .then(() => schoolFactory({ name: "TEST", type: "przedszkole" }));
// })

describe("schould has virtual methods which", () => {
describe("has virtual methods which", () => {
it("return concatenated address data by calling `address`", async () => {
const school = await sequelize
.sync({ force: true })
.then(() =>
schoolFactory({
street: "Testowa 2",
city: "Poznan",
type: "przedszkole"
})
);
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 sequelize
.sync({ force: true })
.then(() =>
schoolFactory({
postCode: "12-345",
postOffice: "Poznan",
type: "przedszkole"
})
);
const school = await schoolFactory({
postCode: "12-345",
postOffice: "Poznan",
SchoolTypeName: "mock"
});

expect(school.post).toEqual("12-345 Poznan");
});
Expand Down
4 changes: 4 additions & 0 deletions src/test/models/school_type_model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ describe("SchoolType model", () => {
await truncate(SchoolType);
});

afterAll(async () => {
await SchoolType.drop()
})

it("should be created", async () => {
const schoolType = await sequelize
.sync({ force: true })
Expand Down

0 comments on commit 878703a

Please sign in to comment.