Skip to content

Commit

Permalink
Merge branch 'CodeForPoznan#121' of github.com:Maciek246/alinka-elect…
Browse files Browse the repository at this point in the history
…ron into CodeForPoznan#121
  • Loading branch information
Maciek246 committed Mar 4, 2020
2 parents a7e64e2 + cac2bd3 commit 3a780a7
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 100 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": "cross-env 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
20 changes: 14 additions & 6 deletions src/db/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ const getSchoolTypeList = () => {
return SchoolType.findAll();
};

const createSchool = ({ name, city, postCode, street, type }) => {
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
name,
city,
postCode,
street,
SchoolTypeName,
postOffice
})
);
};
Expand Down
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
59 changes: 0 additions & 59 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: {
isValidaPolishPostCode(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;
12 changes: 7 additions & 5 deletions src/test/factories/schoolFactory.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
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: faker.address.city(),
city: city,
postCode: faker.address.zipCode(),
street: faker.address.streetAddress()
street: faker.address.streetAddress(),
postOffice: city
};
};

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;
63 changes: 44 additions & 19 deletions src/test/models/school_model.test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,61 @@
const { sequelize } = require("../../db/db_config");
const schoolFactory = require("../factories/schoolFactory");
const {schoolData} = 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 schoolTypeFactory("przedszkole")
beforeEach(() => {
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.dataValues.name).toBe("TEST");
expect(school.name).toEqual("TEST");
});

it("should return concatenated data by calling `address`", async () => {
it("should validate `postCode`", async () => {
const school = await sequelize
.sync({ force: true })
.then(() => schoolFactory({ street: "TEST STREET", type: "przedszkole" }));

expect(school).toBeTruthy();
expect(school.dataValues.address).toBe("TEST STREET");
.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");
});
});
});
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
9 changes: 3 additions & 6 deletions src/test/truncate.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
module.exports = async function truncate(models) {
if (models instanceof Array){
models.map(
model => model.destroy({ where: {}, force: true })
);
}
else {
if (models instanceof Array) {
models.map(model => model.destroy({ where: {}, force: true }));
} else {
models.destroy({ where: {}, force: true });
}
};

0 comments on commit 3a780a7

Please sign in to comment.