def test_session(): robot = werobot.WeRoBot(token=werobot.utils.generate_token(), enable_session=True) @robot.text def first(message, session): if 'last' in session: return session['last'] = message.content return message.content @robot.text def second(message, session): return session['last'] tester = werobot.testing.WeTest(robot) message_1 = werobot.testing.make_text_message('ss') message_2 = werobot.testing.make_text_message('dd') session_storages = [ filestorage.FileStorage(), mongodbstorage.MongoDBStorage(pymongo.MongoClient().t.t), redisstorage.RedisStorage(redis.Redis()), ] for session_storage in session_storages: robot.session_storage = session_storage assert tester.send(message_1) == tester.send(message_2) == 'ss',\ session_storage remove_session(session_storage)
def test_session(): robot = werobot.WeRoBot(token=werobot.utils.generate_token(), enable_session=True) @robot.text def first(message, session): if 'last' in session: return session['last'] = message.content return message.content @robot.text def second(_, session): return session['last'] tester = werobot.testing.WeTest(robot) xml_1 = """ <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[ss]]></Content> <MsgId>1234567890123456</MsgId> </xml> """ xml_2 = """ <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[dd]]></Content> <MsgId>1234567890123456</MsgId> </xml> """ try: os.remove(os.path.abspath("werobot_session")) except OSError: pass session_storages = [ filestorage.FileStorage(), mongodbstorage.MongoDBStorage(pymongo.MongoClient().t.t), redisstorage.RedisStorage(redis.Redis()), ] for session_storage in session_storages: remove_session(session_storage) robot.session_storage = session_storage reply_1 = tester.send_xml(xml_1)._args['content'] assert reply_1 == 'ss', (reply_1, session_storage) reply_2 = tester.send_xml(xml_2)._args['content'] assert reply_2 == 'ss', (reply_2, session_storage) remove_session(session_storage)
] self.db.execute(*args, **kwargs) else: self.db.execute(*args, **kwargs) def fetchone(self): return self.cache_result def commit(self): return self.db.commit() @pytest.mark.parametrize("storage", [ filestorage.FileStorage(), mongodbstorage.MongoDBStorage(mongomock.MongoClient().t.t), redisstorage.RedisStorage(mockredis.mock_redis_client()), sqlitestorage.SQLiteStorage(), mysqlstorage.MySQLStorage(MockPyMySQL()) ]) def test_storage(storage): assert storage.get("喵") == {} storage.set("喵", "喵喵") assert storage.get("喵") == u"喵喵" storage.delete("喵") assert storage.get("喵") == {} assert storage["榴莲"] == {} storage["榴莲"] = "榴莲" assert storage["榴莲"] == u"榴莲" del storage["榴莲"] assert storage["榴莲"] == {}
def test_session_storage_get(): session = SessionStorage() with pytest.raises(NotImplementedError): session.get('s') def test_session_storage_set(): session = SessionStorage() with pytest.raises(NotImplementedError): session.set('s', {}) def test_session_storage_delete(): session = SessionStorage() with pytest.raises(NotImplementedError): session.delete('s') @pytest.mark.parametrize("storage", [ filestorage.FileStorage(), mongodbstorage.MongoDBStorage(pymongo.MongoClient().t.t), redisstorage.RedisStorage(redis.Redis()), sqlitestorage.SQLiteStorage(), ]) def test_storage(storage): assert storage.get("喵") == {} storage.set("喵", "喵喵") assert storage.get("喵") == u"喵喵" storage.delete("喵") assert storage.get("喵") == {}