How to use build_message method in Lemoncheesecake

Best Python code snippet using lemoncheesecake

test_protorpc.py

Source:test_protorpc.py Github

copy

Full Screen

...31 assert hasattr(protorpc, 'pmessage_types')32 assert hasattr(protorpc, 'build_message')33 assert hasattr(protorpc, 'ProtoRPCKey')34 assert hasattr(protorpc, 'ProtoRPCModel')35 def test_build_message(self):36 """ Test `build_message` with a basic `Model` """37 class SimpleModel(model.Model):38 """ Simple model message """39 string = basestring40 integer = int41 message_class = protorpc.build_message(SimpleModel)42 assert hasattr(message_class, 'string')43 assert hasattr(message_class, 'integer')44 assert message_class.__name__ == SimpleModel.kind()45 _model = SimpleModel(string='hi', integer=5)46 message = message_class(string='hi', integer=5)47 assert _model.string == message.string48 assert _model.integer == message.integer49 def test_build_message_default(self):50 """ Test `build_message` with a property that has a default value """51 class SimpleModel(model.Model):52 """ Simple model message """53 string = basestring54 integer = int, {'default': 10}55 message_class = protorpc.build_message(SimpleModel)56 assert hasattr(message_class, 'string')57 assert hasattr(message_class, 'integer')58 assert message_class.__name__ == SimpleModel.kind()59 _model = SimpleModel(string='hi')60 message = message_class(string='hi')61 assert _model.string == message.string62 assert _model.integer == message.integer63 def test_build_message_required(self):64 """ Test `build_message` with a required property """65 class SimpleModel(model.Model):66 """ Simple model message """67 string = basestring68 integer = int, {'required': True}69 message_class = protorpc.build_message(SimpleModel)70 assert hasattr(message_class, 'string')71 assert hasattr(message_class, 'integer')72 assert message_class.__name__ == SimpleModel.kind()73 _model = SimpleModel(string='hi')74 message = message_class(string='hi')75 assert _model.string == message.string76 assert _model.integer == message.integer77 with self.assertRaises(exceptions.PropertyRequired):78 _model.put()79 with self.assertRaises(messages.ValidationError):80 message.check_initialized()81 def test_build_message_repeated(self):82 """ Test `build_message` with a repeated property """83 class SimpleModel(model.Model):84 """ Simple model message """85 string = basestring86 integer = int, {'repeated': True}87 message_class = protorpc.build_message(SimpleModel)88 assert hasattr(message_class, 'string')89 assert hasattr(message_class, 'integer')90 assert message_class.integer.repeated is True91 assert message_class.__name__ == SimpleModel.kind()92 _model = SimpleModel(string='hi', integer=[1, 2, 3])93 message = message_class(string='hi', integer=[1, 2, 3])94 assert _model.string == message.string95 assert type(_model.integer) is list96 assert type(message.integer) is messages.FieldList97 assert len(_model.integer) == 3 and len(message.integer) == 398 def test_build_message_explicit_implementation_field(self):99 """ Test `build_message` with a valid explicit implementation field """100 class SimpleModel(model.Model):101 """ Simple model message """102 string = basestring103 integer = float, {'field': 'IntegerField'}104 message_class = protorpc.build_message(SimpleModel)105 assert hasattr(message_class, 'string')106 assert hasattr(message_class, 'integer')107 assert message_class.__name__ == SimpleModel.kind()108 _model = SimpleModel(string='hi', integer=5.5)109 message = message_class(string='hi', integer=5)110 assert _model.string == message.string111 assert isinstance(_model.integer, float)112 assert isinstance(message.integer, int)113 def test_build_message_invalid_implementation_field(self):114 """ Test `build_message` with an invalid explicit implementation field """115 class SimpleModel(model.Model):116 """ Simple model message """117 string = basestring118 integer = int, {'field': 'NoSuchField'}119 with self.assertRaises(ValueError):120 protorpc.build_message(SimpleModel)121 def test_build_message_skip_field(self):122 """ Test `build_message` with an indication to skip a field """123 class SimpleModel(model.Model):124 """ Simple model message """125 string = basestring126 integer = float, {'field': False}127 message_class = protorpc.build_message(SimpleModel)128 assert hasattr(message_class, 'string')129 assert not hasattr(message_class, 'integer')130 assert message_class.__name__ == SimpleModel.kind()131 _model = SimpleModel(string='hi', integer=5.5)132 message = message_class(string='hi')133 assert _model.string == message.string134 with self.assertRaises(AttributeError):135 message.integer136 def test_build_message_explicit_field_args(self):137 """ Test `build_message` with implementation field args """138 class SimpleEnum(messages.Enum):139 """ Enumerates colors! """140 RED = 0x0141 BLUE = 0x1142 GREEN = 0x2143 class SimpleModel(model.Model):144 """ Simple model message """145 string = basestring146 color = basestring, {'field': ('EnumField', (SimpleEnum,))}147 message_class = protorpc.build_message(SimpleModel)148 assert hasattr(message_class, 'string')149 assert hasattr(message_class, 'color')150 assert message_class.color.type is SimpleEnum151 def test_build_message_with_enum(self):152 """ Test `build_message` with a message that contains an enum """153 class Color(struct.BidirectionalEnum):154 """ Sample enumeration of a bunch of colors. """155 BLUE = 0x0156 RED = 0x1157 GREEN = 0x2158 class SomeModel(model.Model):159 """ Something involving colors. """160 color = Color161 name = str162 message_class = protorpc.build_message(SomeModel)163 assert hasattr(message_class, 'color')164 assert hasattr(message_class, 'name')165 assert message_class.color.type.BLUE is Color.BLUE166 def test_build_message_explicit_field_args_kwargs(self):167 """ Test `build_message` with an implementation of field args + kwargs """168 class SimpleEnum(messages.Enum):169 """ Enumerates colors! """170 RED = 0x0171 BLUE = 0x1172 GREEN = 0x2173 class SimpleModel(model.Model):174 """ Simple model message """175 string = basestring176 color = basestring, {177 'field': ('EnumField', (SimpleEnum,), {'default': SimpleEnum.RED})}178 message_class = protorpc.build_message(SimpleModel)179 assert hasattr(message_class, 'string')180 assert hasattr(message_class, 'color')181 assert message_class.color.type is SimpleEnum182 assert message_class().color is SimpleEnum.RED183 def test_build_message_bogus_explicit_field(self):184 """ Test `build_message` with a bogus field value """185 class SimpleModel(model.Model):186 """ Simple model message """187 string = basestring188 color = basestring, {'field': 5.5}189 with self.assertRaises(TypeError):190 protorpc.build_message(SimpleModel)191 def test_build_message_submodel(self):192 """ Test `build_message` with an embedded submodel """193 class SimpleModel(model.Model):194 """ Simple model message """195 string = basestring196 integer = int197 class SimpleContainer(model.Model):198 """ Simple container message """199 model = SimpleModel200 message_class = protorpc.build_message(SimpleContainer)201 assert hasattr(message_class, 'model')202 assert message_class.__name__ == SimpleContainer.kind()203 assert message_class.model.message_type.__name__ == SimpleModel.kind()204 def test_build_message_variant(self):205 """ Test `build_message` with a variant property """206 class SimpleModel(model.Model):207 """ Simple model message """208 string = basestring209 integer = int210 data = dict211 message_class = protorpc.build_message(SimpleModel)212 assert hasattr(message_class, 'string')213 assert hasattr(message_class, 'integer')214 assert hasattr(message_class, 'data')215 assert message_class.__name__ == SimpleModel.kind()216 _model = SimpleModel(string='hi', integer=5, data={'hi': 'sup'})217 message = message_class(string='hi', integer=5, data={'hi': 'sup'})218 assert _model.string == message.string219 assert _model.integer == message.integer220 assert _model.data['hi'] == message.data['hi']221 def test_build_message_variant_vanilla_model(self):222 """ Test `build_message` with a variant property (by vanilla `Model`) """223 class SimpleModel(model.Model):224 """ Simple model message """225 string = basestring226 integer = int227 data = model.Model228 some_model = SimpleModel(string='hithere')229 message_class = protorpc.build_message(SimpleModel)230 assert hasattr(message_class, 'string')231 assert hasattr(message_class, 'integer')232 assert hasattr(message_class, 'data')233 assert message_class.__name__ == SimpleModel.kind()234 _model = SimpleModel(string='hi', integer=5, data=some_model)235 message = message_class(string='hi', integer=5, data=some_model.to_dict())236 assert _model.string == message.string237 assert _model.integer == message.integer238 assert _model.data.string == message.data['string']239 def test_build_message_key(self):240 """ Test `build_message` with a `Key` property """241 class SimpleModel(model.Model):242 """ Simple model message """243 string = basestring244 integer = int245 ref = model.Key246 message_class = protorpc.build_message(SimpleModel)247 assert hasattr(message_class, 'string')248 assert hasattr(message_class, 'integer')249 assert hasattr(message_class, 'ref')250 assert message_class.__name__ == SimpleModel.kind()251 _model = SimpleModel(string='hi', integer=5, ref=model.Key('hi', 1))252 message = message_class(string='hi', integer=5, ref=model.Key('hi', 1).to_message())253 _msg_from_obj = _model.to_message()254 assert _model.string == message.string255 assert _model.integer == message.integer256 assert _model.ref.urlsafe() == message.ref.encoded257 assert _msg_from_obj.ref.encoded == _model.ref.urlsafe()258 assert _msg_from_obj.ref.id == 1259 def test_build_message_hook(self):260 """ Test `build_message` with a target object hook """261 class SomeProperty(model.Property):262 """ Simple property with __message__ hook """263 _basetype = basestring264 @classmethod265 def __message__(cls, *args, **kwargs):266 """ Return an IntegerField implementation... """267 return messages.IntegerField(*args, **kwargs)268 class SimpleModel(model.Model):269 """ Simple model message """270 string = SomeProperty271 message_class = protorpc.build_message(SimpleModel)272 assert hasattr(message_class, 'string')273 assert message_class.__name__ == SimpleModel.kind()274 _model = SimpleModel(string='5') # it's a basestring...275 message = message_class(string=5) # jk its an int! lol276 assert int(_model.string) == message.string277class ProtoRPCAdaptedKeyTest(test.FrameworkTest):278 """ Tests for the ProtoRPC `Key` mixin """279 def test_key_to_message(self):280 """ Test converting a `Key` to a message """281 key = model.Key('Hi', 5)282 message = key.to_message()283 assert message.id == key.id284 assert message.kind == key.kind285 assert message.encoded == key.urlsafe()...

Full Screen

Full Screen

tests.py

Source:tests.py Github

copy

Full Screen

...23 def test_initial_messages(self, ws_mock):24 self.uut.connect()25 ws_mock.WebSocketApp().on_open()26 ws_mock.WebSocketApp().send.assert_has_calls([27 call(AnyRunClientTest.build_message({"msg": "connect", "version": "1", "support": ["1", "pre2", "pre1"]})),28 call(AnyRunClientTest.build_message({"msg": "method", "method": "host", "params": [], "id": "1"})),29 call(AnyRunClientTest.build_message({"msg": "method", "method": "getPrefix", "params": [], "id": "2"})),30 call(AnyRunClientTest.build_message({31 'msg': 'sub',32 'id': AnyRunClientTest.generate_token_stub(),33 'name': 'meteor.loginServiceConfiguration',34 'params': []})),35 call(AnyRunClientTest.build_message({36 'msg': 'sub',37 'id': AnyRunClientTest.generate_token_stub(),38 'name': 'meteor_autoupdate_clientVersions',39 'params': []})),40 call(AnyRunClientTest.build_message({41 'msg': 'sub',42 'id': AnyRunClientTest.generate_token_stub(),43 'name': 'activeTasks',44 'params': []}45 )),46 call(AnyRunClientTest.build_message({47 'msg': 'sub',48 'id': AnyRunClientTest.generate_token_stub(),49 'name': 'settings',50 'params': []}51 )),52 call(AnyRunClientTest.build_message({53 'msg': 'sub',54 'id': AnyRunClientTest.generate_token_stub(),55 'name': 'teams',56 'params': []}57 )),58 call(AnyRunClientTest.build_message({59 'msg': 'sub',60 'id': AnyRunClientTest.generate_token_stub(),61 'name': 'files.avatars',62 'params': []}63 )),64 call(AnyRunClientTest.build_message({65 'msg': 'sub',66 'id': AnyRunClientTest.generate_token_stub(),67 'name': 'statisticsDayVeridct',68 'params': []}69 )),70 call(AnyRunClientTest.build_message({71 'msg': 'sub',72 'id': AnyRunClientTest.generate_token_stub(),73 'name': 'statisticsDayCountry',74 'params': []}75 )),76 call(AnyRunClientTest.build_message({77 'msg': 'sub',78 'id': AnyRunClientTest.generate_token_stub(),79 'name': 'statisticsDayTags',80 'params': []}81 )),82 call(AnyRunClientTest.build_message({83 'msg': 'sub',84 'id': AnyRunClientTest.generate_token_stub(),85 'name': 'statisticsDayTime',86 'params': []}87 )),88 call(AnyRunClientTest.build_message({89 'msg': 'sub',90 'id': AnyRunClientTest.generate_token_stub(),91 'name': 'statisticsDayIOC',92 'params': []}93 )),94 call(AnyRunClientTest.build_message({95 'msg': 'sub',96 'id': AnyRunClientTest.generate_token_stub(),97 'name': 'publicTasks',98 'params': [99 50, 0, {100 "isPublic": True,101 "hash": "",102 "major": "",103 "bit": "",104 "runtype": [],105 "name": "",106 "verdict": [],107 "ext": [],108 "tag": "",109 "significant": False,110 }]}111 )),112 call(AnyRunClientTest.build_message({113 'msg': 'sub',114 'id': AnyRunClientTest.generate_token_stub(),115 'name': 'changeLog',116 'params': []}117 )),118 call(AnyRunClientTest.build_message({119 'msg': 'sub',120 'id': AnyRunClientTest.generate_token_stub(),121 'name': 'activeTasksCounter',122 'params': []}123 )),124 call(AnyRunClientTest.build_message({125 'msg': 'sub',126 'id': AnyRunClientTest.generate_token_stub(),127 'name': 'interestingTask',128 'params': []}129 )),130 ])131 def test_on_message_server_sends_empty_msg(self, ws_mock):132 self.uut.connect()133 self.uut.on_message('a')134 self.cb_mock.assert_not_called()135 ws_mock.WebSocketApp().send.assert_not_called()136 def test_on_message_server_sends_ping(self, ws_mock):137 self.uut.connect()138 self.uut.on_message('a' + AnyRunClientTest.build_message({'msg': 'ping'}))139 ws_mock.WebSocketApp().send.assert_called_once_with(AnyRunClientTest.build_message({'msg': 'pong'}))140 def test_on_message_server_sends_msg(self, _):141 self.uut.connect()142 self.uut.on_message('a' + AnyRunClientTest.build_message({'msg': 'test'}))143 self.cb_mock.assert_called_once_with({'msg': 'test'})144 def test_on_message_without_cb(self, ws_mock):145 self.uut = AnyRunClient(None)146 self.uut.connect()147 self.uut.on_message('a' + AnyRunClientTest.build_message({'msg': 'test'}))148 self.cb_mock.assert_not_called()149 ws_mock.WebSocketApp().send.assert_not_called()150 def test_call_run_forever(self, ws_mock):151 self.uut.connect()152 self.uut.run_forever()153 self.assertEqual(ws_mock.WebSocketApp().run_forever.call_count, 1)154 def test_call_on_error_raises_exception(self, _):155 with self.assertRaises(AnyRunException):156 self.uut.on_error('error')157 def test_call_on_error_not_raises_exception(self, _):158 self.uut.on_error(KeyboardInterrupt)159 @staticmethod160 def build_message(msg) -> str:161 return json.dumps([json.dumps(msg)])162 @staticmethod163 def generate_token_stub() -> str:164 return 'test'165 @staticmethod166 def generate_id_stub() -> int:...

Full Screen

Full Screen

controller.py

Source:controller.py Github

copy

Full Screen

2from flask import jsonify3import vocab4import chat_util as cu5import turing_bot as tb6def build_message(content):7 if content:8 return jsonify(9 command='msg',10 msg_content=content11 )12 else:13 return jsonify(command=None)14def build_message_multimsgs(msgs):15 return jsonify(16 command='msgs',17 msgs=msgs18 )19def handle_chat(game, chat_obj):20 speaker_name = chat_obj['name']21 if cu.is_self(speaker_name):22 pass23 elif chat_obj['isAt'] == 'True':24 content = cu.rm_At(chat_obj['Content'])25 if content:26 return handle_targeted_chat(game,27 content,28 speaker_name)29 else:30 return build_message(vocab.en.gen())31 elif game.current_speaker is not None:32 if game.current_speaker.name == speaker_name:33 game.append_comment(chat_obj['Content'])34 return build_message(None)35 else:36 return build_message(None)37 elif game.director_name == speaker_name:38 return handle_director_chat(game, chat_obj['Content'])39 else:40 return build_message(None)41def handle_director_chat(game, content):42 if '发言' in content:43 if '@' in content:44 _atu = cu.retrieve_At(content)45 _message = _atu + '记得发言格式哦\n'46 _message += 'discubot.com/help'47 return build_message(_message)48 else:49 return build_message(None)50 else:51 return build_message(None)52def handle_targeted_chat(game, content, speaker_name):53 base_url_sheet = 'https://docs.google.com/spreadsheets/d/'54 base_url_doc = 'https://docs.google.com/document/d/'55 params = '/edit?usp=sharing'56 if '行程' in content:57 return build_message(base_url_doc +58 '14gxFL5sz5VrVPJQC3HdT7DuBYm3XkG-HTTMHAxDoiB4' +59 params)60 elif '账单' in content:61 return build_message(base_url_sheet +62 '1g9BnJHIdUvwHl-OOB6meMDJKOWq0aE-57Ktl5SeGFBM' +63 params)64 elif game.recruiting:65 if '1' in content:66 game.init_player(speaker_name, '?')67 elif '0' in content:68 game.remove_player(name=speaker_name)69 return build_message('报名表已更新:discubot.com/players')70 elif 'help' in content:71 return build_message('discubot.com/help')72 elif 'players' in content:73 return build_message('discubot.com/players')74 elif '发言整理' in content or '整理发言' in content:75 return build_message('discubot.com/comments')76 elif '开始报名' in content or '报名开始' in content:77 if game.started:78 game.reset()79 game.recruiting = True80 game.set_director(speaker_name)81 _msg_contents = [vocab.recruit_start.gen(),82 '你已被钦定为导演[Sly]',83 '管理界面在discubot.com/director/players']84 _users = [None, speaker_name, speaker_name]85 _msgs = [{'user': u, 'contents': c} for u, c in zip(_users, _msg_contents)]86 return build_message_multimsgs(_msgs)87 elif '开局' in content:88 if game.recruiting:89 game.recruiting = False90 else:91 game.reset()92 game.set_director(speaker_name)93 game.start()94 return build_message(vocab.game_start.gen())95 elif '天亮' in content:96 if speaker_name == game.director_name:97 game.dawn()98 game.set_current_speaker(None)99 return build_message(vocab.dawn.gen())100 elif '天黑' in content:101 if speaker_name == game.director_name:102 game.dusk()103 game.set_current_speaker(None)104 return build_message(vocab.dusk.gen())105 elif '号发言' in content and not ("结束" in content):106 return init_comment(game, content, speaker_name)107 elif '号自爆' in content:108 if not game.isNight:109 game.set_current_speaker(None)110 player_i = cu.get_ints(content.split('号自爆')[0])[0]111 content_trimed = content.split('号自爆')[1]112 player = game.init_player(speaker_name, player_i)113 player_i.is_dead = True114 comment = Comment(player, game.day, content_trimed, is_suicide=True)115 game.add_comment(comment)116 game.set_current_speaker(None)117 game.dusk()118 _message = f'{player_i}号自爆了,夜幕又降临在这个诡异的微信群🌚'119 return build_message(_message)120 else:121 _message = f'如果我没有记错的话,现在是晚上不能自爆...'122 return build_message(_message)123 elif '过' == content.strip() or '发言结束' in content:124 try:125 player_i = game.current_speaker.index126 game.set_current_speaker(None)127 link_str = 'discubot.com/comment/' + str(game.comments[-1].id)128 _message = str(player_i) + '号的发言记录好了:' + link_str129 except:130 _message = '发言前先@我并说 x号发言, 否则我记录不到[Sweat]'131 finally:132 return build_message(_message)133 else:134 tb_r = tb.get_response(content)135 rply_content = tb_r or 'echo:' + content136 return build_message(rply_content)137def init_comment(game, content, speaker_name):138 player_i = cu.get_ints(content.split('号发言')[0])[0]139 content_trimed = content.split('号发言')[1]140 player = game.init_player(speaker_name, player_i)141 game.set_current_speaker(player)142 comment = Comment(player, game.day, content_trimed)143 if game.day <= 1:144 if not game.shreff:145 player.is_shreff_cadidate = True146 comment.is_shreff_run = True147 game.add_comment(comment)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Lemoncheesecake automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful