-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
121 lines (85 loc) · 2.38 KB
/
test.js
File metadata and controls
121 lines (85 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* test module
*
*/
var debug = require('debug')('monpoco:entity'),
co = require('co'),
db = require('..')('mysql');
var config = {
pool: 5,
user : 'root',
database:'monpy_test'
};
//TEST Table
var CREATE_SQL = "CREATE TABLE IF NOT EXISTS `user` ( "
+ "`id` int(11) unsigned NOT NULL AUTO_INCREMENT, "
+ "`login` varchar(32) NOT NULL DEFAULT '', "
+ "`name` varchar(64) NOT NULL DEFAULT '', "
+ "`password` varchar(128) NOT NULL DEFAULT '', "
+ "`sex` tinyint(4) NOT NULL DEFAULT '0', "
+ "`created_at` datetime DEFAULT NULL, "
+ "`updated_at` datetime DEFAULT NULL, "
+ "PRIMARY KEY (`id`), "
+ "KEY `UK_USER_LOGIN` (`login`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
// DB Connection
db.connect(config);
/* TEST Model Class */
function User(){
db.BaseEntity.apply(this, arguments);
}
db.inherits(User, db.BaseEntity);
// Test code.
// Success case
co(function *(){
// Create Table;
yield db.query(CREATE_SQL);
var model = new User();
// truncate table
yield model.truncate();
var from = new Date();
var to = new Date(from.getTime() + (3600000 * 24) );
var data = {
login: 'usr_' + Math.floor(new Date().getTime()/1000),
name: 'Test Name',
password: 'password',
sex: 1,
created_at:new Date()
};
var ret = yield model.save(data);
var id = ret.insertId;
var insertedUser = yield model.getById(id);
console.log('-- INSERT Data ------');
console.log(insertedUser);
yield model.updateById(id, {
updated_at: new Date()
});
var updatedUser = yield model.getById(id);
console.log('-- UPDATE Data ------');
console.log(updatedUser);
yield model.deleteById(id);
var deleteUser = yield model.getById(id);
console.log('-- Delete Data ------');
console.log(deleteUser);
for(var i = 0; i < 30; i++) {
data = {
login: 'usr_' + i,
name: 'Test Name' + i,
password: 'password' + i,
sex: (i%2),
created_at:new Date()
};
yield model.insert(data);
}
var rows = yield model.where('sex', '=', 0).paginate(0, 10);
console.log('-- Paginate Data (page index:0, page size: 20) ------');
console.log(rows);
console.log('-- Paginate Data (page index:1, page size: 20) ------');
rows = yield model.where('sex', '=', 0).paginate(1, 10);
console.log(rows);
db.end();
process.exit(0);
}).catch(function(err){
console.error(err.message);
console.error(err.stack);
process.exit(1);
});