Completed
Push — develop ( b745b5...362316 )
by Wu
9s
created

test_session()   C

Complexity

Conditions 8

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
c 1
b 0
f 0
dl 0
loc 55
rs 6.2085

2 Methods

Rating   Name   Duplication   Size   Complexity  
A first() 0 6 2
A second() 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
2
3
import werobot
4
import werobot.utils
5
import werobot.testing
6
from werobot.session import filestorage, mongodbstorage, redisstorage
7
from werobot.session import SessionStorage
8
from werobot.utils import to_binary
9
10
import pymongo
11
import redis
12
import os
13
from nose.tools import raises
14
15
16
def remove_session(session):
17
    try:
18
        del session[to_binary("fromUser")]
19
    except:
20
        pass
21
22
23
def test_session():
24
    robot = werobot.WeRoBot(token=werobot.utils.generate_token(),
25
                            enable_session=True)
26
27
    @robot.text
28
    def first(message, session):
29
        if 'last' in session:
30
            return
31
        session['last'] = message.content
32
        return message.content
33
34
    @robot.text
35
    def second(_, session):
36
        return session['last']
37
38
    tester = werobot.testing.WeTest(robot)
39
    xml_1 = """
40
        <xml>
41
        <ToUserName><![CDATA[toUser]]></ToUserName>
42
        <FromUserName><![CDATA[fromUser]]></FromUserName>
43
        <CreateTime>1348831860</CreateTime>
44
        <MsgType><![CDATA[text]]></MsgType>
45
        <Content><![CDATA[ss]]></Content>
46
        <MsgId>1234567890123456</MsgId>
47
        </xml>
48
    """
49
    xml_2 = """
50
        <xml>
51
        <ToUserName><![CDATA[toUser]]></ToUserName>
52
        <FromUserName><![CDATA[fromUser]]></FromUserName>
53
        <CreateTime>1348831860</CreateTime>
54
        <MsgType><![CDATA[text]]></MsgType>
55
        <Content><![CDATA[dd]]></Content>
56
        <MsgId>1234567890123456</MsgId>
57
        </xml>
58
    """
59
60
    try:
61
        os.remove(os.path.abspath("werobot_session"))
62
    except OSError:
63
        pass
64
    session_storages = [
65
        filestorage.FileStorage(),
66
        mongodbstorage.MongoDBStorage(pymongo.MongoClient().t.t),
67
        redisstorage.RedisStorage(redis.Redis()),
68
    ]
69
70
    for session_storage in session_storages:
71
        remove_session(session_storage)
72
        robot.session_storage = session_storage
73
        reply_1 = tester.send_xml(xml_1)._args['content']
74
        assert reply_1 == 'ss', (reply_1, session_storage)
75
        reply_2 = tester.send_xml(xml_2)._args['content']
76
        assert reply_2 == 'ss', (reply_2, session_storage)
77
        remove_session(session_storage)
78
79
80
@raises(NotImplementedError)
81
def test_session_storage_get():
82
    session = SessionStorage()
83
    session.get('s')
84
85
86
@raises(NotImplementedError)
87
def test_session_storage_set():
88
    session = SessionStorage()
89
    session.set('s', {})
90
91
92
@raises(NotImplementedError)
93
def test_session_storage_delete():
94
    session = SessionStorage()
95
    session.delete('s')
96