Toggle navigation
Toggle navigation
This project
Loading...
Sign in
fe
/
yohobuywap-node
·
Commits
Go to a project
GitLab
Go to group
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
0
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Email Patches
Plain Diff
Browse Files
Authored by
Feng
9 years ago
Commit
5d54e56ab5c81157dcd1304092888dce960b143c
1 parent
00efd137
添加部分单元测试
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
216 additions
and
50 deletions
.eslintrc
apps/product/controllers/detail.js
library/api.js
package.json
test/library/api.test.js
test/library/cache.test.js
test/library/logger.test.js
.eslintrc
View file @
5d54e56
{
"extends"
:
"yoho"
,
"rules"
:
{
"no-unused-vars"
:
[
"error"
,
{
"args"
:
"after-used"
,
"vars"
:
"all"
}
]
},
"parserOptions"
:
{
"sourceType"
:
"module"
}
...
...
apps/product/controllers/detail.js
View file @
5d54e56
...
...
@@ -19,7 +19,7 @@ exports.index = (req, res) => {
id
:
req
.
params
.
id
,
uid
:
uid
,
vipLevel
:
vipLevel
,
ua
:
req
.
get
(
'user-agent'
)
||
''
ua
:
req
.
get
(
'user-agent'
)
||
''
}).
then
((
result
)
=>
{
res
.
render
(
'detail'
,
{
resultShow
:
JSON
.
stringify
(
result
,
null
,
4
),
...
...
library/api.js
View file @
5d54e56
...
...
@@ -18,12 +18,11 @@ const api = config.domains.api;
const
serviceApi
=
config
.
domains
.
service
;
const
searchApi
=
config
.
domains
.
search
;
let
ApiUrl
;
class
API
{
class
Http
{
constructor
()
{
ApiUrl
=
api
;
constructor
(
baseUrl
)
{
this
.
ApiUrl
=
baseUrl
;
}
/**
...
...
@@ -38,10 +37,11 @@ class API {
*/
_requestFromAPI
(
options
,
cacheOption
,
reqId
)
{
let
timer
=
new
Timer
();
let
method
=
options
.
method
||
'get'
;
timer
.
put
(
'getApi'
);
// 统计时间开始
log
.
info
(
`
get
api
:
$
{
options
.
url
}?
$
{
qs
.
stringify
(
options
.
qs
)}
`
);
log
.
info
(
`
$
{
method
}
api
:
$
{
options
.
url
}?
$
{
qs
.
stringify
(
options
.
qs
)}
`
);
return
rp
(
options
).
then
((
result
)
=>
{
let
duration
=
timer
.
put
(
'getApi'
);
// 统计时间结束
...
...
@@ -60,10 +60,11 @@ class API {
}
}
return
result
;
}).
catch
((
error
)
=>
{
}).
catch
((
error
)
=>
{
let
duration
=
timer
.
put
(
'getApi'
);
// 统计时间结束
log
.
error
(
`
get
api
fail
:
use
:
$
{
duration
}
ms
,
statusCode
:
$
{
error
.
statusCode
},
error
:
$
{
error
.
message
}
`
);
log
.
error
(
`
$
{
method
}
api
fail
:
use
:
$
{
duration
}
ms
,
statusCode
:
$
{
error
.
statusCode
},
error
:
$
{
error
.
message
}
`
);
// 使用缓存的时候,读取二级缓存
if
(
config
.
useCache
)
{
...
...
@@ -116,12 +117,14 @@ class API {
*/
get
(
url
,
data
,
cacheOption
)
{
let
options
=
{
url
:
`
$
{
ApiUrl
}
$
{
url
}
`
,
url
:
`
$
{
this
.
ApiUrl
}
$
{
url
}
`
,
qs
:
data
,
json
:
true
,
timeout
:
3000
};
console
.
log
(
'in api : '
+
config
.
useCache
);
// 从缓存获取数据
if
(
config
.
useCache
&&
cacheOption
)
{
return
this
.
_requestFromCache
(
options
);
...
...
@@ -137,7 +140,7 @@ class API {
*/
post
(
url
,
data
)
{
let
options
=
{
url
:
`
$
{
ApiUrl
}
$
{
url
}
`
,
url
:
`
$
{
this
.
ApiUrl
}
$
{
url
}
`
,
form
:
data
,
method
:
'post'
,
json
:
true
,
...
...
@@ -150,22 +153,27 @@ class API {
all
(
list
)
{
if
(
_
.
isArray
(
list
))
{
return
Promise
.
all
(
list
);
}
else
{
return
Promise
.
reject
(
Error
(
'the parameters of api all method should be Array!'
));
}
throw
Error
(
'the parameters of api all method should be Array!'
);
}
}
class
ServiceAPI
extends
API
{
class
API
extends
Http
{
constructor
()
{
super
(
api
);
}
}
class
ServiceAPI
extends
Http
{
constructor
()
{
super
();
ApiUrl
=
serviceApi
;
super
(
serviceApi
);
}
}
class
SearchAPI
extends
API
{
class
SearchAPI
extends
Http
{
constructor
()
{
super
();
ApiUrl
=
searchApi
;
super
(
searchApi
);
}
}
...
...
package.json
View file @
5d54e56
...
...
@@ -79,6 +79,7 @@
"postcss-sprites"
:
"^3.1.2"
,
"postcss-use"
:
"^2.0.2"
,
"precss"
:
"^1.4.0"
,
"rewire"
:
"^2.5.1"
,
"shelljs"
:
"^0.7.0"
,
"stylelint"
:
"^6.3.3"
,
"stylelint-config-yoho"
:
"^1.2.3"
,
...
...
test/library/api.test.js
View file @
5d54e56
...
...
@@ -4,19 +4,104 @@
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 2016/05/17
*/
'use strict'
;
const
test
=
require
(
'ava'
);
//const rewire = require('rewire');
//const shelljs = require('shelljs');
const
sign
=
require
(
'../../library/sign'
);
const
API
=
require
(
'../../library/api'
).
ServiceAPI
;
const
api
=
new
API
();
//let config = rewire('../../config/common');
const
API
=
require
(
'../../library/api'
).
API
;
const
ServiceAPI
=
require
(
'../../library/api'
).
ServiceAPI
;
const
SearchAPI
=
require
(
'../../library/api'
).
SearchAPI
;
const
getUrl
=
'operations/api/v6/category/getCategory'
;
test
(
'api get test'
,
(
t
)
=>
{
return
api
.
get
(
'operations/api/v6/category/getCategory'
,
sign
.
apiSign
({})).
then
(
result
=>
{
if
(
result
&&
result
.
code
===
200
)
{
//test.before('create log folder', (t) => {
// shelljs.mkdir('log');
// t.pass();
//});
//
//test.after('delete log folder', (t) => {
// shelljs.rm('-rf', 'log');
// t.pass();
//});
test
(
'api constructor test'
,
(
t
)
=>
{
let
api
=
new
ServiceAPI
();
let
api2
=
new
API
();
let
api3
=
new
SearchAPI
();
t
.
true
(
api
!==
null
);
t
.
true
(
api2
!==
null
);
t
.
true
(
api3
!==
null
);
});
test
(
'api get test'
,
t
=>
{
let
api
=
new
ServiceAPI
();
return
api
.
get
(
getUrl
,
sign
.
apiSign
({})).
then
(
result
=>
{
if
(
result
&&
(
result
.
code
===
200
||
result
.
code
===
500
))
{
t
.
pass
();
}
else
{
t
.
fail
();
}
});
});
test
(
'api get use cache test'
,
t
=>
{
let
api
=
new
ServiceAPI
();
return
api
.
get
(
getUrl
,
sign
.
apiSign
({}),
true
).
then
(
result
=>
{
if
(
result
&&
(
result
.
code
===
200
||
result
.
code
===
500
))
{
t
.
pass
();
}
else
{
t
.
fail
();
}
});
});
test
(
'api post test'
,
t
=>
{
let
api
=
new
ServiceAPI
();
return
api
.
post
(
getUrl
,
sign
.
apiSign
({})).
then
(
result
=>
{
if
(
result
&&
(
result
.
code
===
200
||
result
.
code
===
500
))
{
t
.
pass
();
}
else
{
t
.
fail
();
}
});
});
test
(
'api multiple call test'
,
(
t
)
=>
{
let
api
=
new
ServiceAPI
();
let
multi
=
[
api
.
get
(
getUrl
,
sign
.
apiSign
({})),
api
.
get
(
getUrl
,
sign
.
apiSign
({}))];
return
api
.
all
(
multi
).
then
(
result
=>
{
if
(
result
.
length
===
2
)
{
t
.
pass
();
}
else
{
t
.
fail
();
}
});
});
test
(
'api multiple fail call test'
,
(
t
)
=>
{
let
api
=
new
ServiceAPI
();
return
api
.
all
(
1
).
catch
((
e
)
=>
{
if
(
e
)
{
t
.
pass
();
}
else
{
t
.
fail
();
}
});
});
...
...
test/library/cache.test.js
0 → 100644
View file @
5d54e56
/**
* cache 测试
*
* @author: jf<jeff.jiang@yoho.cn>
* @date: 2016/5/18
*/
'use strict'
;
import
test
from
'ava'
;
import
cache
from
'../../library/cache'
;
let
testKey
=
'test_unit_key:'
+
(
new
Date
()).
getTime
();
let
testValue
=
'anotherValue'
;
let
anotherKey
=
'test_unit_key2:'
+
(
new
Date
()).
getTime
();
let
anotherValue
=
{
a
:
1
};
let
slaveTestKey
=
'test_unit_key3:'
+
(
new
Date
()).
getTime
();
let
slaveTestValue
=
'anotherValue3'
;
test
.
before
(
'set test key'
,
(
t
)
=>
{
cache
.
set
(
testKey
,
testValue
);
cache
.
set
(
anotherKey
,
anotherValue
);
t
.
pass
();
});
test
.
after
(
'del test key'
,
(
t
)
=>
{
cache
.
del
(
testKey
);
cache
.
del
(
anotherKey
);
t
.
pass
();
});
test
(
'cache get test'
,
(
t
)
=>
{
return
cache
.
get
(
testKey
).
then
((
v
)
=>
{
t
.
is
(
v
,
testValue
);
});
});
test
(
'cache get multi test'
,
(
t
)
=>
{
cache
.
set
(
anotherKey
,
anotherValue
);
return
cache
.
getMulti
([
testKey
,
anotherKey
]).
then
((
values
)
=>
{
console
.
log
(
values
);
t
.
is
(
values
[
testKey
],
testValue
);
t
.
is
(
values
[
anotherKey
],
JSON
.
stringify
(
anotherValue
));
});
});
test
(
'cache get from slave test'
,
(
t
)
=>
{
return
cache
.
getFromSlave
(
testKey
).
then
((
v
)
=>
{
t
.
is
(
v
,
testValue
);
});
});
test
(
'cache get multi from slave test'
,
(
t
)
=>
{
cache
.
set
(
anotherKey
,
anotherValue
);
return
cache
.
getMultiFromSlave
([
testKey
,
anotherKey
]).
then
((
values
)
=>
{
t
.
is
(
values
[
testKey
],
testValue
);
t
.
is
(
values
[
anotherKey
],
JSON
.
stringify
(
anotherValue
));
});
});
test
(
'cache set to slave'
,
(
t
)
=>
{
return
cache
.
setSlave
(
slaveTestKey
,
slaveTestValue
).
then
(()
=>
{
return
cache
.
getFromSlave
(
slaveTestKey
);
}).
then
((
v
)
=>
{
t
.
is
(
v
,
slaveTestValue
);
cache
.
del
(
slaveTestKey
);
});
});
...
...
test/library/logger.test.js
View file @
5d54e56
...
...
@@ -6,31 +6,42 @@ const test = require('ava');
const
shelljs
=
require
(
'shelljs'
);
const
logger
=
require
(
'../../library/logger'
);
// const today = () => {
// let now = new Date();
// let s = now.getFullYear();
const
today
=
()
=>
{
let
now
=
new
Date
();
let
s
=
now
.
getFullYear
();
// if (now.getMonth() < 10) {
// s += '-0' + now.getMonth();
// } else {
// s += now.getMonth();
// }
// if (now.getDay() < 10) {
// s += '-0' + now.getDay();
// } else {
// s += now.getDay();
// }
// };
if
(
now
.
getMonth
()
<
10
)
{
s
+=
'-0'
+
now
.
getMonth
();
}
else
{
s
+=
now
.
getMonth
();
}
if
(
now
.
getDay
()
<
10
)
{
s
+=
'-0'
+
now
.
getDay
();
}
else
{
s
+=
now
.
getDay
();
}
return
s
;
};
//test.before('create log folder', t => {
// shelljs.mkdir('log');
// t.pass();
//});
//
//test.after('clean test log file ', t => {
// shelljs.rm('-rf', 'log');
// t.pass();
//});
test
.
cb
(
'logger test'
,
t
=>
{
shelljs
.
rm
(
'-f'
,
'log/*.log.*'
);
logger
.
info
(
'xxx'
,
()
=>
{
shelljs
.
ls
(
'info.log.*'
).
forEach
(
s
=>
console
.
log
(
'generate log file:'
+
s
));
shelljs
.
ls
(
'log/info.log.*'
).
some
(
s
=>
{
console
.
log
(
'generate log file:'
+
s
);
return
s
===
'info.log.'
+
today
();
});
t
.
end
();
});
});
test
.
after
(
'clean test log file '
,
t
=>
{
shelljs
.
rm
(
'-f'
,
'log/*.log.*'
);
t
.
pass
();
});
...
...
Please
register
or
login
to post a comment