Skip to content

Commit 048e84c

Browse files
committed
テスト導入
1 parent 4b73ff0 commit 048e84c

File tree

6 files changed

+3146
-74
lines changed

6 files changed

+3146
-74
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ The easiest way to get mastodon token: https://takahashim.github.io/mastodon-acc
2727
Create your Twitter app : https://developer.twitter.com/
2828

2929
2. Run `docker-compose up`
30+
31+
## Testing
32+
33+
Run `docker-compose run --rm -e NODE_ENV=development node npm run test`

TTRelation.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const util = require('util')
2+
3+
class TTRelation {
4+
5+
constructor(redis, config){
6+
this.prefix = config.redis_prefix
7+
var rdc = redis.createClient("redis://redis")
8+
rdc.on("error", err => {
9+
throw new Error("RedisError " + err)
10+
})
11+
this.setAsync = util.promisify(rdc.set).bind(rdc)
12+
this.getAsync = util.promisify(rdc.get).bind(rdc)
13+
this.keysAsync = util.promisify(rdc.keys).bind(rdc)
14+
}
15+
16+
set(k,v){
17+
console.log('TTRelation.set key,value:', k, v)
18+
return this.setAsync(this.prefix + k, v, 'EX', 3600).then(() => {
19+
return this.list()
20+
})
21+
}
22+
23+
get(k){
24+
console.log('TTRelation.get key:', k)
25+
return this.getAsync(this.prefix + k)
26+
}
27+
28+
list(){
29+
return this.keysAsync('*')
30+
}
31+
32+
}
33+
34+
module.exports = TTRelation

TTRelation.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const TTRelation = require('./TTRelation.js'),
2+
redis = require('redis-mock'),
3+
config = require('config')
4+
5+
test('コンストラクタが機能する', () => {
6+
let ttr = new TTRelation(redis, config)
7+
console.log(config)
8+
expect(ttr.prefix).toBe(config.redis_prefix)
9+
expect(ttr.setAsync).toBeDefined()
10+
expect(ttr.getAsync).toBeDefined()
11+
expect(ttr.keysAsync).toBeDefined()
12+
})

index.js

+3-37
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,11 @@ const https = require('https'),
44
htmlDecode = require('unescape'),
55
redis = require("redis"),
66
util = require('util'),
7-
Twitter = require('twitter')
7+
Twitter = require('twitter'),
8+
TTRelation = require('./TTRelation.js')
89

910
TARGET_ACCT = config.target_acct
1011

11-
class TTRelation {
12-
13-
constructor(prefix = null){
14-
if(prefix == null){
15-
this.prefix = config.redis_prefix
16-
}else{
17-
this.prefix = prefix
18-
}
19-
var rdc = redis.createClient("redis://redis")
20-
rdc.on("error", err => {
21-
throw new Error("RedisError " + err)
22-
})
23-
this.setAsync = util.promisify(rdc.set).bind(rdc)
24-
this.getAsync = util.promisify(rdc.get).bind(rdc)
25-
this.keysAsync = util.promisify(rdc.keys).bind(rdc)
26-
}
27-
28-
set(k,v){
29-
console.log('TTRelation.set key,value:', k, v)
30-
return this.setAsync(this.prefix + k, v, 'EX', 3600).then(() => {
31-
return this.list()
32-
})
33-
}
34-
35-
get(k){
36-
console.log('TTRelation.get key:', k)
37-
return this.getAsync(this.prefix + k)
38-
}
39-
40-
list(){
41-
return this.keysAsync('*')
42-
}
43-
44-
}
45-
4612
class WatchDog {
4713

4814
constructor(){
@@ -77,7 +43,7 @@ var session = (wd) => {
7743
access_token_secret: config.access_token_secret
7844
})
7945
let wsc = new WebSocketClient()
80-
let ttr = new TTRelation()
46+
let ttr = new TTRelation(redis, config)
8147

8248
wsc.on('connectFailed', e => {
8349
console.log('Connection Error: ' + e.toString())

0 commit comments

Comments
 (0)