-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#9 - метод /user/info - тесты (подключил sqlite-базу в памяти для прогона тестов)
- Loading branch information
Alexander Pavlov
committed
Aug 10, 2018
1 parent
f7e7a7a
commit ebd0a53
Showing
11 changed files
with
333 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
|
||
npm-debug.log | ||
.env | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}); | ||
|
||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.