Best Python code snippet using localstack_python
serializers.py
Source:serializers.py
...13 self.user = User.objects.create_user(14 username="test",15 password="test",16 )17 def create_serializer(self, data, assignment=False, assignment_args={}, article=None):18 """19 Create serializer with data and context20 """21 if not article:22 article = self.article23 if assignment:24 self.create_assignment(article, **assignment_args)25 now = datetime.datetime.now()26 before = now - datetime.timedelta(minutes=5)27 data["metadata"] = data.get("metadata", None) or json.dumps({28 "start_time": str(before),29 "end_time": str(now),30 })31 return ArticleLabelSerializer(32 data=data, context={'article': article, 'user': self.user}33 )34 def create_assignment(self, article, num_comments=None,**kwargs):35 """36 Creates assignment for user and article37 """38 assignment = self.user.assignment_set.create(article=article, **kwargs)39 if num_comments:40 comments_to_label = self.article.comment_set.all()[:num_comments]41 assignment.set_comments(comments_to_label)42 return assignment43 def test_is_not_valid_if_no_assignment(self):44 """45 If no assignment, then it is not valid46 """47 serializer = self.create_serializer({48 "is_interesting": False,49 })50 assert not serializer.is_valid()51 def test_is_valid_if_not_interesting(self):52 """53 If is not interesting, it should be valid54 """55 serializer = self.create_serializer({56 "is_interesting": False,57 }, assignment=True)58 assert serializer.is_valid()59 def test_is_not_valid_if_not_interesting_but_not_skippable(self):60 """61 If is not interesting, it should be valid62 """63 serializer = self.create_serializer({64 "is_interesting": False,65 }, assignment=True, assignment_args={"skippable": False})66 assert not serializer.is_valid()67 def test_not_valid_if_already_labeled(self):68 """69 If is not interesting, it should be valid70 """71 serializer = self.create_serializer({72 "is_interesting": False,73 }, assignment=True)74 another_serializer = self.create_serializer({75 "is_interesting": False,76 })77 serializer.is_valid()78 serializer.save()79 assert not another_serializer.is_valid()80 def test_not_valid_if_not_interesting_and_has_labels(self):81 """82 Not valid if has labels and not interesting83 """84 serializer = self.create_serializer({85 "is_interesting": False,86 "comment_labels": [87 comment_label(comm.id) for comm in self.article.comment_set.all()88 ],89 }, assignment=True)90 assert not serializer.is_valid()91 def test_valid_if_interesting_and_has_labels_for_every_comment(self):92 """93 Valid if interesting and labels for every comment94 """95 serializer = self.create_serializer({96 "is_interesting": True,97 "comment_labels": [{98 "is_hateful": True,99 "types": ["MUJER"],100 "comment": comm.id,101 "calls_for_action": False102 } for comm in self.article.comment_set.all()103 ],104 }, assignment=True)105 assert serializer.is_valid()106 def test_not_valid_if_lacks_calls_for_action(self):107 """108 Valid if interesting and labels for every comment109 """110 serializer = self.create_serializer({111 "is_interesting": True,112 "comment_labels": [{113 "is_hateful": True,114 "comment": comm.id,115 "types": ["LGBTI"],116 } for comm in self.article.comment_set.all()117 ],118 }, assignment=True)119 assert not serializer.is_valid()120 def test_not_valid_if_lacks_type(self):121 """122 Valid if interesting and labels for every comment123 """124 serializer = self.create_serializer({125 "is_interesting": True,126 "comment_labels": [{127 "is_hateful": True,128 "comment": comm.id,129 "calls_for_action": False,130 } for comm in self.article.comment_set.all()131 ],132 }, assignment=True)133 assert not serializer.is_valid()134 def test_not_valid_if_type_empty_when_hateful(self):135 """136 Not valid if hateful but type is empty137 """138 serializer = self.create_serializer({139 "is_interesting": True,140 "comment_labels": [141 {"is_hateful": True, "comment": comm.id, "types": []}142 for comm in self.article.comment_set.all()143 ],144 }, assignment=True)145 assert not serializer.is_valid()146 def test_not_valid_if_type_non_empty_when_not_hateful(self):147 """148 Not Valid if not hateful but non empty type149 """150 serializer = self.create_serializer({151 "is_interesting": True,152 "comment_labels": [153 {"is_hateful": False, "comment": comm.id, "types": ['RACISMO']}154 for comm in self.article.comment_set.all()155 ],156 }, assignment=True)157 assert not serializer.is_valid()158 def test_valid_with_two_different_comments(self):159 """160 Test for two comments161 """162 article = ArticleFactory(create_comments__num_comments=2)163 comments = list(article.comment_set.all())164 serializer = self.create_serializer({165 "is_interesting": True,166 "comment_labels": [167 comment_label(comments[0].id, is_hateful=False),168 comment_label(comments[1].id, is_hateful=True, types=["RACISMO"]),169 ],170 }, assignment=True, article=article)171 assert serializer.is_valid()172 def test_not_valid_if_interesting_but_lacks_label_for_comment(self):173 """174 Not valid if lacks a label175 """176 serializer = self.create_serializer({177 "is_interesting": True,178 "comment_labels": [179 comment_label(comm.id)180 for comm in self.article.comment_set.all()[:3]181 ],182 }, assignment=True)183 assert not serializer.is_valid()184 def test_valid_if_labels_only_selected_comments(self):185 """186 Valid if labels only specified comments in assignment187 """188 num_comments = 3189 serializer = self.create_serializer({190 "is_interesting": True,191 "comment_labels": [192 comment_label(comm.id)193 for comm in self.article.comment_set.all()[:num_comments]194 ],195 }, assignment=True, assignment_args={"num_comments": num_comments})196 assert serializer.is_valid()197 def test_not_valid_if_specified_comments_but_labels_less(self):198 """199 Valid if labels only specified comments in assignment200 """201 num_comments = 3202 serializer = self.create_serializer({203 "is_interesting": True,204 "comment_labels": [205 comment_label(comm.id)206 for comm in self.article.comment_set.all()[:num_comments-1]207 ],208 }, assignment=True, assignment_args={"num_comments": num_comments})209 assert not serializer.is_valid()210 def test_not_valid_if_specified_comments_but_labels_more(self):211 """212 Valid if labels only specified comments in assignment213 """214 num_comments = 3215 serializer = self.create_serializer({216 "is_interesting": True,217 "comment_labels": [218 comment_label(comm.id)219 for comm in self.article.comment_set.all()[:num_comments+1]220 ],221 }, assignment=True, assignment_args={"num_comments": num_comments})222 assert not serializer.is_valid()223 def test_create_for_not_interesting(self):224 """225 Test creation for not interesting article226 """227 serializer = self.create_serializer({228 "is_interesting": False,229 "feedback": "Esto apesta",230 }, assignment=True)231 assert serializer.is_valid()232 article_label = serializer.save()233 self.assertEqual(article_label.feedback, "Esto apesta")234 assert article_label.comment_labels.count() == 0235 def test_create_for_interesting(self):236 """237 Test creation for interesting article238 """239 serializer = self.create_serializer({240 "is_interesting": True,241 "feedback": "Esto apesta",242 "comment_labels": [243 comment_label(comm.id, is_hateful=bool(i % 2))244 for i, comm in enumerate(self.article.comment_set.all())245 ],246 }, assignment=True)247 assert serializer.is_valid()248 article_label = serializer.save()249 self.assertEqual(article_label.feedback, "Esto apesta")250 self.assertEqual(251 article_label.comment_labels.count(),252 self.article.comment_set.count()253 )254 def test_check_metadata_is_saved(self):255 """256 Test metadata257 """258 serializer = self.create_serializer({259 "is_interesting": False,260 "metadata": "{some_random: 'metadata'}"261 }, assignment=True)262 assert serializer.is_valid()263 article_label = serializer.save()264 assert article_label.metadata == "{some_random: 'metadata'}"265 def test_check_types(self):266 """267 Test creation for interesting article268 """269 serializer = self.create_serializer({270 "is_interesting": True,271 "comment_labels": [272 comment_label(comm.id, is_hateful=bool(i % 2), types=[273 "MUJER", "LGBTI", "POLITICA"])274 for i, comm in enumerate(self.article.comment_set.all())275 ],276 }, assignment=True)277 serializer.is_valid()278 article_label = serializer.save()279 # reload280 article_label = self.article.labels.first()281 for label in article_label.comment_labels.all():282 if not label.is_hateful:283 continue284 assert label.against_women285 assert label.against_lgbti286 assert label.against_political287 assert not label.against_race288 assert not label.against_poor289 assert not label.against_disabled290 def test_assignment_is_done_after_creating(self):291 """292 Test creation for interesting article293 """294 serializer = self.create_serializer({295 "is_interesting": True,296 "comment_labels": [297 comment_label(comm.id) for comm in self.article.comment_set.all()298 ],299 }, assignment=True)300 assert serializer.is_valid()301 serializer.save()302 assignment = self.article.assignment_set.get(user=self.user)303 assert assignment.done304class CommentLabelForCreationSerializerTest(TestCase):305 """306 Tests for validation and save of ArticleLabelSerializer307 """308 def setUp(self):...
test_serializers.py
Source:test_serializers.py
...131class TestBlockSerializer(TestBlockSerializerBase):132 """133 Tests the BlockSerializer class, which returns a list of blocks.134 """135 def create_serializer(self, context=None):136 """137 creates a BlockSerializer138 """139 if context is None:140 context = self.serializer_context141 return BlockSerializer(142 context['block_structure'], many=True, context=context,143 )144 def test_basic(self):145 serializer = self.create_serializer()146 for serialized_block in serializer.data:147 self.assert_basic_block(serialized_block['id'], serialized_block)148 self.assertEqual(len(serializer.data), 28)149 def test_additional_requested_fields(self):150 self.add_additional_requested_fields()151 serializer = self.create_serializer()152 for serialized_block in serializer.data:153 self.assert_extended_block(serialized_block)154 self.assertEqual(len(serializer.data), 28)155 def test_staff_fields(self):156 """157 Test fields accessed by a staff user158 """159 context = self.create_staff_context()160 self.add_additional_requested_fields(context)161 serializer = self.create_serializer(context)162 for serialized_block in serializer.data:163 self.assert_extended_block(serialized_block)164 self.assert_staff_fields(serialized_block)165 self.assertEqual(len(serializer.data), 29)166class TestBlockDictSerializer(TestBlockSerializerBase):167 """168 Tests the BlockDictSerializer class, which returns a dict of blocks key-ed by its block_key.169 """170 def create_serializer(self, context=None):171 """172 creates a BlockDictSerializer173 """174 if context is None:175 context = self.serializer_context176 return BlockDictSerializer(177 context['block_structure'], many=False, context=context,178 )179 def test_basic(self):180 serializer = self.create_serializer()181 # verify root182 self.assertEqual(serializer.data['root'], six.text_type(self.block_structure.root_block_usage_key))183 # verify blocks184 for block_key_string, serialized_block in six.iteritems(serializer.data['blocks']):185 self.assertEqual(serialized_block['id'], block_key_string)186 self.assert_basic_block(block_key_string, serialized_block)187 self.assertEqual(len(serializer.data['blocks']), 28)188 def test_additional_requested_fields(self):189 self.add_additional_requested_fields()190 serializer = self.create_serializer()191 for serialized_block in six.itervalues(serializer.data['blocks']):192 self.assert_extended_block(serialized_block)193 self.assertEqual(len(serializer.data['blocks']), 28)194 def test_staff_fields(self):195 """196 Test fields accessed by a staff user197 """198 context = self.create_staff_context()199 self.add_additional_requested_fields(context)200 serializer = self.create_serializer(context)201 for serialized_block in six.itervalues(serializer.data['blocks']):202 self.assert_extended_block(serialized_block)203 self.assert_staff_fields(serialized_block)...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!