Skip to content

Commit

Permalink
#3 - метод /user/update
Browse files Browse the repository at this point in the history
#9 - метод /user/info
- тесты (подключил sqlite-базу в памяти для прогона тестов)
  • Loading branch information
Alexander Pavlov committed Aug 10, 2018
1 parent f7e7a7a commit ebd0a53
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 107 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

npm-debug.log
.env
*.iml
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ services:
depends_on:
- database
container_name: server
environment:
- MYSQL_USER=${DATABASE_USER}
- MYSQL_PASSWORD=${DATABASE_PASSWORD}
- MYSQL_DATABASE=${DATABASE_SCHEMA}
- PORT=3000
- SOCKETS_PORT=8443
build:
context: .
dockerfile: server/Dockerfile
Expand Down
7 changes: 6 additions & 1 deletion server/database.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const Sequelize = require('sequelize');

const database = new Sequelize('mysql://toor:hei6voYi@database:3306/circles', {});
let username = process.env.MYSQL_USER || '';
let password = process.env.MYSQL_PASSWORD || '';
let host = 'database';
let dbName = process.env.MYSQL_DATABASE || '';

const database = new Sequelize('mysql://' + username + ':' + password + '@' + host + ':3306/' + dbName, {});

function connection() {
return database;
Expand Down
92 changes: 92 additions & 0 deletions server/databaseModels/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const makeUserModel = function (database, types) {
return database().define('user', {
id: {
type: types.INTEGER,
primaryKey: true,
autoIncrement: true
},
uuid: {
type: types.STRING(36),
allowNull: true,
defaultValue: null
},
dateRegistered: {
type: 'TIMESTAMP',
allowNull: false,
defaultValue: types.literal('CURRENT_TIMESTAMP')
},
name: {
type: types.STRING(255),
allowNull: true,
defaultValue: null
},
email: {
type: types.STRING(255),
allowNull: true,
defaultValue: null
},
birthday: {
type: 'TIMESTAMP',
allowNull: true,
defaultValue: null
},
sex: {
type: types.STRING(6),
allowNull: true,
defaultValue: null
},
status: {
type: types.STRING(11),
allowNull: true,
defaultValue: null
},
aim: {
type: types.STRING(6),
allowNull: true,
defaultValue: null
},
education: {
type: types.STRING(6),
allowNull: true,
defaultValue: null
},
phone: {
type: types.INTEGER(10),
allowNull: true,
defaultValue: null
},
about: {
type: types.TEXT,
allowNull: true,
defaultValue: null
},
needSex: {
type: types.STRING(6),
allowNull: true,
defaultValue: null
},
needStatus: {
type: types.STRING(11),
allowNull: true,
defaultValue: null
},
needAim: {
type: types.STRING(6),
allowNull: true,
defaultValue: null
},
needEducation: {
type: types.STRING(6),
allowNull: true,
defaultValue: null
}
},
{
timestamps: true,
createdAt: 'dateRegistered',
updatedAt: false,
deletedAt: false
});
};

module.exports = makeUserModel;
62 changes: 62 additions & 0 deletions server/mappers/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
let makeUserModel = require('../databaseModels/User.js');

function deepClone(objectToClone) {
return JSON.parse(JSON.stringify(objectToClone));
}

function UserMapper(database, types) {
this.database = database;
this.types = types;

this.createTable = function () {
let user = makeUserModel(database, types);
return user.sync();
};

this.create = function (uuid, userProps) {
let createProps = deepClone(userProps);
createProps.uuid = uuid;

let user = makeUserModel(this.database, this.types);

return new Promise(function (resolve, reject) {
user.create(createProps)
.then(function () {
return user.find({where: {uuid: uuid}});
})
.then(function (foundUser) {
resolve(foundUser);
})
.catch(function (error) {
reject(error);
});
});
};

this.update = function (uuid, propsToUpdate) {
let updateProps = deepClone(propsToUpdate);
let user = makeUserModel(this.database, this.types);

return new Promise(function (resolve, reject) {
user.find({where: {uuid: uuid}})
.then(function (foundUser) {
return foundUser.updateAttributes(updateProps);
})
.then(function (updatedUser) {
resolve(updatedUser);
})
.catch(function (error) {
reject(error);
});
});
};

this.find = function (uuid) {
let user = makeUserModel(this.database, this.types);
return user.find({where: {uuid: uuid}});
}
}

module.exports = function (database, types) {
return new UserMapper(database, types);
};
97 changes: 97 additions & 0 deletions server/mappers/User.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const makeUserMapper = require('./User');
const database = require('../testDatabase.js');
const types = require('sequelize');

beforeAll(function () {
let userMapper = makeUserMapper(database, types);
return userMapper.createTable();
});

test('Метод create', function () {
let userMapper = makeUserMapper(database, types);
let uuid = "49642b99-edd1-4a4d-ae7f-407f36d665dc";
let userProps = {
name: "Константин Константинопольский"
};

return new Promise(function (resolve, reject) {
userMapper.create(uuid, userProps)
.then(function (createdUser) {
try {
expect(createdUser.id).toBeGreaterThan(0);
expect(createdUser.name).toEqual(userProps.name);
resolve();
} catch (exception)
{
reject(exception);
}
});
});
});

test('Метод update', function () {
let userMapper = makeUserMapper(database, types);
let uuid = "f99ac8bf-9ed4-4504-8eb3-413d01c89969";
let initialProps = {
name: "Константин Константинопольский"
};

let updatedProps = {
name: "Сергей Сергиевский"
};

let newUserId = false;

return new Promise(function (resolve, reject) {
userMapper.create(uuid, initialProps)
.then(function (createdUser) {
try {
newUserId = createdUser.id;
expect(createdUser.id).toBeGreaterThan(0);
expect(createdUser.name).toEqual(initialProps.name);
} catch (exception)
{
reject(exception);
}
})
.then(function () {
return userMapper.update(uuid, updatedProps);
})
.then(function (updatedUser) {
try {
expect(updatedUser.id).toEqual(newUserId);
expect(updatedUser.name).toEqual(updatedProps.name);
resolve();
} catch (exception)
{
reject(exception);
}
});
});
});

test('Метод find', function () {
let userMapper = makeUserMapper(database, types);
let uuid = "e27b3196-18fd-48a0-84e0-25afaa8160a1";
let userProps = {
name: "Константин Константинопольский"
};

return new Promise(function (resolve, reject) {
userMapper.create(uuid, userProps)
.then(function () {
return userMapper.find(uuid);
})
.then(function (foundUser) {
try {
expect(foundUser.uuid).toEqual(uuid);
expect(foundUser.name).toEqual(userProps.name);
resolve();
}
catch (error) {
reject(error);
}
});
});

});
93 changes: 0 additions & 93 deletions server/mappers/user.js

This file was deleted.

8 changes: 6 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
},
"devDependencies": {
"jest": "^23.4.0",
"nodemon": "^1.17.5"
"nodemon": "^1.17.5",
"sqlite3": "^4.0.2"
},
"jest": {
"modulePaths": ["./"]
"modulePaths": [
"./"
],
"testURL": "http://localhost"
}
}
Loading

0 comments on commit ebd0a53

Please sign in to comment.