Best Python code snippet using molecule_python
test_fields.py
Source:test_fields.py
...60 try:61 foo = CharField(model_attr='foo')62 except:63 self.fail()64 def test_prepare(self):65 mock = MockModel()66 mock.user = 'daniel'67 author = CharField(model_attr='user')68 self.assertEqual(author.prepare(mock), u'daniel')69 # Do a lookup through the relation.70 mock_tag = MockTag.objects.create(name='primary')71 mock = MockModel()72 mock.tag = mock_tag73 tag_name = CharField(model_attr='tag__name')74 self.assertEqual(tag_name.prepare(mock), u'primary')75 # Use the default.76 mock = MockModel()77 author = CharField(model_attr='author', default='')78 self.assertEqual(author.prepare(mock), u'')79 # Simulate failed lookups.80 mock_tag = MockTag.objects.create(name='primary')81 mock = MockModel()82 mock.tag = mock_tag83 tag_slug = CharField(model_attr='tag__slug')84 self.assertRaises(SearchFieldError, tag_slug.prepare, mock)85 # Simulate default='foo'.86 mock = MockModel()87 default = CharField(default='foo')88 self.assertEqual(default.prepare(mock), 'foo')89 # Simulate null=True.90 mock = MockModel()91 empty = CharField(null=True)92 self.assertEqual(empty.prepare(mock), None)93 mock = MockModel()94 mock.user = None95 author = CharField(model_attr='user', null=True)96 self.assertEqual(author.prepare(mock), None)97class NgramFieldTestCase(TestCase):98 def test_init(self):99 try:100 foo = NgramField(model_attr='foo')101 except:102 self.fail()103 self.assertRaises(SearchFieldError, NgramField, faceted=True)104 def test_prepare(self):105 mock = MockModel()106 mock.user = 'daniel'107 author = NgramField(model_attr='user')108 self.assertEqual(author.prepare(mock), u'daniel')109 # Do a lookup through the relation.110 mock_tag = MockTag.objects.create(name='primary')111 mock = MockModel()112 mock.tag = mock_tag113 tag_name = NgramField(model_attr='tag__name')114 self.assertEqual(tag_name.prepare(mock), u'primary')115 # Use the default.116 mock = MockModel()117 author = NgramField(model_attr='author', default='')118 self.assertEqual(author.prepare(mock), u'')119 # Simulate failed lookups.120 mock_tag = MockTag.objects.create(name='primary')121 mock = MockModel()122 mock.tag = mock_tag123 tag_slug = NgramField(model_attr='tag__slug')124 self.assertRaises(SearchFieldError, tag_slug.prepare, mock)125 # Simulate default='foo'.126 mock = MockModel()127 default = NgramField(default='foo')128 self.assertEqual(default.prepare(mock), 'foo')129 # Simulate null=True.130 mock = MockModel()131 empty = NgramField(null=True)132 self.assertEqual(empty.prepare(mock), None)133 mock = MockModel()134 mock.user = None135 author = NgramField(model_attr='user', null=True)136 self.assertEqual(author.prepare(mock), None)137class EdgeNgramFieldTestCase(TestCase):138 def test_init(self):139 try:140 foo = EdgeNgramField(model_attr='foo')141 except:142 self.fail()143 self.assertRaises(SearchFieldError, EdgeNgramField, faceted=True)144 def test_prepare(self):145 mock = MockModel()146 mock.user = 'daniel'147 author = EdgeNgramField(model_attr='user')148 self.assertEqual(author.prepare(mock), u'daniel')149 # Do a lookup through the relation.150 mock_tag = MockTag.objects.create(name='primary')151 mock = MockModel()152 mock.tag = mock_tag153 tag_name = EdgeNgramField(model_attr='tag__name')154 self.assertEqual(tag_name.prepare(mock), u'primary')155 # Use the default.156 mock = MockModel()157 author = EdgeNgramField(model_attr='author', default='')158 self.assertEqual(author.prepare(mock), u'')159 # Simulate failed lookups.160 mock_tag = MockTag.objects.create(name='primary')161 mock = MockModel()162 mock.tag = mock_tag163 tag_slug = EdgeNgramField(model_attr='tag__slug')164 self.assertRaises(SearchFieldError, tag_slug.prepare, mock)165 # Simulate default='foo'.166 mock = MockModel()167 default = EdgeNgramField(default='foo')168 self.assertEqual(default.prepare(mock), 'foo')169 # Simulate null=True.170 mock = MockModel()171 empty = EdgeNgramField(null=True)172 self.assertEqual(empty.prepare(mock), None)173 mock = MockModel()174 mock.user = None175 author = EdgeNgramField(model_attr='user', null=True)176 self.assertEqual(author.prepare(mock), None)177class IntegerFieldTestCase(TestCase):178 def test_init(self):179 try:180 foo = IntegerField(model_attr='foo')181 except:182 self.fail()183 def test_prepare(self):184 mock = MockModel()185 mock.pk = 1186 pk = IntegerField(model_attr='pk')187 self.assertEqual(pk.prepare(mock), 1)188 # Simulate failed lookups.189 mock_tag = MockTag.objects.create(name='primary')190 mock = MockModel()191 mock.tag = mock_tag192 tag_count = IntegerField(model_attr='tag__count')193 self.assertRaises(SearchFieldError, tag_count.prepare, mock)194 # Simulate default=1.195 mock = MockModel()196 default = IntegerField(default=1)197 self.assertEqual(default.prepare(mock), 1)198 # Simulate null=True.199 mock = MockModel()200 pk_none = IntegerField(model_attr='pk', null=True)201 self.assertEqual(pk_none.prepare(mock), None)202class FloatFieldTestCase(TestCase):203 def test_init(self):204 try:205 foo = FloatField(model_attr='foo')206 except:207 self.fail()208 def test_prepare(self):209 mock = MockModel()210 mock.floaty = 12.5211 floaty = FloatField(model_attr='floaty')212 self.assertEqual(floaty.prepare(mock), 12.5)213 # Simulate default=1.5.214 mock = MockModel()215 default = FloatField(default=1.5)216 self.assertEqual(default.prepare(mock), 1.5)217 # Simulate null=True.218 mock = MockModel()219 floaty_none = FloatField(null=True)220 self.assertEqual(floaty_none.prepare(mock), None)221class DecimalFieldTestCase(TestCase):222 def test_init(self):223 try:224 foo = DecimalField(model_attr='foo')225 except:226 self.fail()227 def test_prepare(self):228 mock = MockModel()229 mock.floaty = Decimal('12.5')230 floaty = DecimalField(model_attr='floaty')231 self.assertEqual(floaty.prepare(mock), '12.5')232 # Simulate default=1.5.233 mock = MockModel()234 default = DecimalField(default='1.5')235 self.assertEqual(default.prepare(mock), '1.5')236 # Simulate null=True.237 mock = MockModel()238 floaty_none = DecimalField(null=True)239 self.assertEqual(floaty_none.prepare(mock), None)240class BooleanFieldTestCase(TestCase):241 def test_init(self):242 try:243 foo = BooleanField(model_attr='foo')244 except:245 self.fail()246 def test_prepare(self):247 mock = MockModel()248 mock.active = True249 is_active = BooleanField(model_attr='active')250 self.assertEqual(is_active.prepare(mock), True)251 # Simulate default=True.252 mock = MockModel()253 default = BooleanField(default=True)254 self.assertEqual(default.prepare(mock), True)255 # Simulate null=True.256 mock = MockModel()257 booly_none = BooleanField(null=True)258 self.assertEqual(booly_none.prepare(mock), None)259class DateFieldTestCase(TestCase):260 def test_init(self):261 try:262 foo = DateField(model_attr='foo')263 except:264 self.fail()265 def test_convert(self):266 pub_date = DateField()267 self.assertEqual(pub_date.convert('2016-02-16'), datetime.date(2016, 2, 16))268 def test_prepare(self):269 mock = MockModel()270 mock.pub_date = datetime.date(2009, 2, 13)271 pub_date = DateField(model_attr='pub_date')272 self.assertEqual(pub_date.prepare(mock), datetime.date(2009, 2, 13))273 # Simulate default=datetime.date(2000, 1, 1).274 mock = MockModel()275 default = DateField(default=datetime.date(2000, 1, 1))276 self.assertEqual(default.prepare(mock), datetime.date(2000, 1, 1))277 def test_prepare_from_string(self):278 mock = MockModel()279 mock.pub_date = datetime.date(2016, 2, 16)280 pub_date = DateField(model_attr='pub_date')281 self.assertEqual(pub_date.prepare(mock), datetime.date(2016, 2, 16))282class DateTimeFieldTestCase(TestCase):283 def test_init(self):284 try:285 foo = DateTimeField(model_attr='foo')286 except:287 self.fail()288 def test_convert(self):289 pub_date = DateTimeField()290 self.assertEqual(pub_date.convert('2016-02-16T10:02:03'),291 datetime.datetime(2016, 2, 16, 10, 2, 3))292 def test_prepare(self):293 mock = MockModel()294 mock.pub_date = datetime.datetime(2009, 2, 13, 10, 1, 0)295 pub_date = DateTimeField(model_attr='pub_date')296 self.assertEqual(pub_date.prepare(mock), datetime.datetime(2009, 2, 13, 10, 1, 0))297 # Simulate default=datetime.datetime(2009, 2, 13, 10, 01, 00).298 mock = MockModel()299 default = DateTimeField(default=datetime.datetime(2000, 1, 1, 0, 0, 0))300 self.assertEqual(default.prepare(mock), datetime.datetime(2000, 1, 1, 0, 0, 0))301 def test_prepare_from_string(self):302 mock = MockModel()303 mock.pub_date = '2016-02-16T10:01:02Z'304 pub_date = DateTimeField(model_attr='pub_date')305 self.assertEqual(pub_date.prepare(mock), datetime.datetime(2016, 2, 16, 10, 1, 2))306class MultiValueFieldTestCase(TestCase):307 def test_init(self):308 try:309 foo = MultiValueField(model_attr='foo')310 except:311 self.fail()312 self.assertRaises(SearchFieldError, MultiValueField, use_template=True)313 def test_prepare(self):314 mock = MockModel()315 mock.sites = ['3', '4', '5']316 sites = MultiValueField(model_attr='sites')317 self.assertEqual(sites.prepare(mock), ['3', '4', '5'])318 # Simulate default=[1].319 mock = MockModel()320 default = MultiValueField(default=[1])321 self.assertEqual(default.prepare(mock), [1])322 # Simulate null=True.323 mock = MockModel()324 multy_none = MultiValueField(null=True)325 self.assertEqual(multy_none.prepare(mock), None)326 def test_convert_with_single_string(self):327 field = MultiValueField()328 self.assertEqual(['String'], field.convert('String'))329 def test_convert_with_single_int(self):330 field = MultiValueField()331 self.assertEqual([1], field.convert(1))332 def test_convert_with_list_of_strings(self):333 field = MultiValueField()334 self.assertEqual(['String 1', 'String 2'], field.convert(['String 1', 'String 2']))335 def test_convert_with_list_of_ints(self):336 field = MultiValueField()337 self.assertEqual([1, 2, 3], field.convert([1, 2, 3]))338class CharFieldWithTemplateTestCase(TestCase):339 def test_init(self):340 try:341 foo = CharField(use_template=True)342 except:343 self.fail()344 try:345 foo = CharField(use_template=True, template_name='foo.txt')346 except:347 self.fail()348 foo = CharField(use_template=True, template_name='foo.txt')349 self.assertEqual(foo.template_name, 'foo.txt')350 # Test the select_template usage.351 foo = CharField(use_template=True, template_name=['bar.txt', 'foo.txt'])352 self.assertEqual(foo.template_name, ['bar.txt', 'foo.txt'])353 def test_prepare(self):354 mock = MockModel()355 mock.pk = 1356 mock.user = 'daniel'357 template1 = CharField(use_template=True)358 self.assertRaises(SearchFieldError, template1.prepare, mock)359 template2 = CharField(use_template=True)360 template2.instance_name = 'template_x'361 self.assertRaises(TemplateDoesNotExist, template2.prepare, mock)362 template3 = CharField(use_template=True)363 template3.instance_name = 'template'364 self.assertEqual(template3.prepare(mock), u'Indexed!\n1')365 template4 = CharField(use_template=True, template_name='search/indexes/foo.txt')366 template4.instance_name = 'template'367 self.assertEqual(template4.prepare(mock), u'FOO!\n')368 template5 = CharField(use_template=True, template_name=['foo.txt', 'search/indexes/bar.txt'])369 template5.instance_name = 'template'370 self.assertEqual(template5.prepare(mock), u'BAR!\n')371##############################################################################372# The following tests look like they don't do much, but it's important because373# we need to verify that the faceted variants behave like the field they374# emulate. The old-broke behavior was convert everything to string.375##############################################################################376class FacetFieldTestCase(TestCase):377 def test_init(self):378 # You shouldn't use the FacetField itself.379 try:380 foo = FacetField(model_attr='foo')381 self.fail()382 except:383 pass384 try:385 foo_exact = FacetField(facet_for='bar')386 self.fail()387 except:388 pass389class FacetCharFieldTestCase(TestCase):390 def test_init(self):391 try:392 foo = FacetCharField(model_attr='foo')393 foo_exact = FacetCharField(facet_for='bar')394 except:395 self.fail()396 self.assertEqual(foo.facet_for, None)397 self.assertEqual(foo_exact.null, True)398 self.assertEqual(foo_exact.facet_for, 'bar')399 def test_prepare(self):400 mock = MockModel()401 mock.user = 'daniel'402 author = FacetCharField(model_attr='user')403 self.assertEqual(author.prepare(mock), u'daniel')404class FacetIntegerFieldTestCase(TestCase):405 def test_init(self):406 try:407 foo = FacetIntegerField(model_attr='foo')408 foo_exact = FacetIntegerField(facet_for='bar')409 except:410 self.fail()411 self.assertEqual(foo.facet_for, None)412 self.assertEqual(foo_exact.null, True)413 self.assertEqual(foo_exact.facet_for, 'bar')414 def test_prepare(self):415 mock = MockModel()416 mock.user = 'daniel'417 mock.view_count = 13418 view_count = FacetIntegerField(model_attr='view_count')419 self.assertEqual(view_count.prepare(mock), 13)420class FacetFloatFieldTestCase(TestCase):421 def test_init(self):422 try:423 foo = FacetFloatField(model_attr='foo')424 foo_exact = FacetFloatField(facet_for='bar')425 except:426 self.fail()427 self.assertEqual(foo.facet_for, None)428 self.assertEqual(foo_exact.null, True)429 self.assertEqual(foo_exact.facet_for, 'bar')430 def test_prepare(self):431 mock = MockModel()432 mock.user = 'daniel'433 mock.price = 25.65434 price = FacetFloatField(model_attr='price')435 self.assertEqual(price.prepare(mock), 25.65)436class FacetBooleanFieldTestCase(TestCase):437 def test_init(self):438 try:439 foo = FacetBooleanField(model_attr='foo')440 foo_exact = FacetBooleanField(facet_for='bar')441 except:442 self.fail()443 self.assertEqual(foo.facet_for, None)444 self.assertEqual(foo_exact.null, True)445 self.assertEqual(foo_exact.facet_for, 'bar')446 def test_prepare(self):447 mock = MockModel()448 mock.user = 'daniel'449 mock.is_active = True450 is_active = FacetBooleanField(model_attr='is_active')451 self.assertEqual(is_active.prepare(mock), True)452class FacetDateFieldTestCase(TestCase):453 def test_init(self):454 try:455 foo = FacetDateField(model_attr='foo')456 foo_exact = FacetDateField(facet_for='bar')457 except:458 self.fail()459 self.assertEqual(foo.facet_for, None)460 self.assertEqual(foo_exact.null, True)461 self.assertEqual(foo_exact.facet_for, 'bar')462 def test_prepare(self):463 mock = MockModel()464 mock.user = 'daniel'465 mock.created = datetime.date(2010, 10, 30)466 created = FacetDateField(model_attr='created')467 self.assertEqual(created.prepare(mock), datetime.date(2010, 10, 30))468class FacetDateTimeFieldTestCase(TestCase):469 def test_init(self):470 try:471 foo = FacetDateTimeField(model_attr='foo')472 foo_exact = FacetDateTimeField(facet_for='bar')473 except:474 self.fail()475 self.assertEqual(foo.facet_for, None)476 self.assertEqual(foo_exact.null, True)477 self.assertEqual(foo_exact.facet_for, 'bar')478 def test_prepare(self):479 mock = MockModel()480 mock.user = 'daniel'481 mock.created = datetime.datetime(2010, 10, 30, 3, 14, 25)482 created = FacetDateTimeField(model_attr='created')483 self.assertEqual(created.prepare(mock), datetime.datetime(2010, 10, 30, 3, 14, 25))484class FacetMultiValueFieldTestCase(TestCase):485 def test_init(self):486 try:487 foo = FacetMultiValueField(model_attr='foo')488 foo_exact = FacetMultiValueField(facet_for='bar')489 except:490 self.fail()491 self.assertEqual(foo.facet_for, None)492 self.assertEqual(foo_exact.null, True)493 self.assertEqual(foo_exact.facet_for, 'bar')494 def test_prepare(self):495 mock = MockModel()496 mock.user = 'daniel'497 mock.sites = [1, 3, 4]498 sites = FacetMultiValueField(model_attr='sites')...
fields.py
Source:fields.py
...9 foo = CharField(model_attr='foo')10 except:11 self.fail()12 13 def test_prepare(self):14 mock = MockModel()15 mock.user = 'daniel'16 author = CharField(model_attr='user')17 18 self.assertEqual(author.prepare(mock), u'daniel')19 20 # Do a lookup through the relation.21 mock_tag = MockTag(name='primary')22 mock = MockModel()23 mock.tag = mock_tag24 tag_name = CharField(model_attr='tag__name')25 26 self.assertEqual(tag_name.prepare(mock), u'primary')27 28 # Use the default.29 mock = MockModel()30 author = CharField(model_attr='author', default='')31 32 self.assertEqual(author.prepare(mock), u'')33 34 # Simulate failed lookups.35 mock_tag = MockTag(name='primary')36 mock = MockModel()37 mock.tag = mock_tag38 tag_slug = CharField(model_attr='tag__slug')39 40 self.assertRaises(SearchFieldError, tag_slug.prepare, mock)41 42 # Simulate default='foo'.43 mock = MockModel()44 default = CharField(default='foo')45 46 self.assertEqual(default.prepare(mock), 'foo')47 48 # Simulate null=True.49 mock = MockModel()50 empty = CharField(null=True)51 52 self.assertEqual(empty.prepare(mock), None)53 54 mock = MockModel()55 mock.user = None56 author = CharField(model_attr='user', null=True)57 58 self.assertEqual(author.prepare(mock), None)59class IntegerFieldTestCase(TestCase):60 def test_init(self):61 try:62 foo = IntegerField(model_attr='foo')63 except:64 self.fail()65 66 def test_prepare(self):67 mock = MockModel()68 mock.pk = 169 pk = IntegerField(model_attr='pk')70 71 self.assertEqual(pk.prepare(mock), 1)72 73 # Simulate failed lookups.74 mock_tag = MockTag(name='primary')75 mock = MockModel()76 mock.tag = mock_tag77 tag_count = IntegerField(model_attr='tag__count')78 79 self.assertRaises(SearchFieldError, tag_count.prepare, mock)80 81 # Simulate default=1.82 mock = MockModel()83 default = IntegerField(default=1)84 85 self.assertEqual(default.prepare(mock), 1)86 87 # Simulate null=True.88 mock = MockModel()89 pk_none = IntegerField(model_attr='pk', null=True)90 91 self.assertEqual(pk_none.prepare(mock), None)92class FloatFieldTestCase(TestCase):93 def test_init(self):94 try:95 foo = FloatField(model_attr='foo')96 except:97 self.fail()98 99 def test_prepare(self):100 mock = MockModel()101 mock.floaty = 12.5102 floaty = FloatField(model_attr='floaty')103 104 self.assertEqual(floaty.prepare(mock), 12.5)105 106 # Simulate default=1.5.107 mock = MockModel()108 default = FloatField(default=1.5)109 110 self.assertEqual(default.prepare(mock), 1.5)111 112 # Simulate null=True.113 mock = MockModel()114 floaty_none = FloatField(null=True)115 116 self.assertEqual(floaty_none.prepare(mock), None)117class BooleanFieldTestCase(TestCase):118 def test_init(self):119 try:120 foo = BooleanField(model_attr='foo')121 except:122 self.fail()123 124 def test_prepare(self):125 mock = MockModel()126 mock.active = True127 is_active = BooleanField(model_attr='active')128 129 self.assertEqual(is_active.prepare(mock), True)130 131 # Simulate default=True.132 mock = MockModel()133 default = BooleanField(default=True)134 135 self.assertEqual(default.prepare(mock), True)136 137 # Simulate null=True.138 mock = MockModel()139 booly_none = BooleanField(null=True)140 141 self.assertEqual(booly_none.prepare(mock), None)142class DateFieldTestCase(TestCase):143 def test_init(self):144 try:145 foo = DateField(model_attr='foo')146 except:147 self.fail()148 149 def test_prepare(self):150 mock = MockModel()151 mock.pub_date = datetime.date(2009, 2, 13)152 pub_date = DateField(model_attr='pub_date')153 154 self.assertEqual(pub_date.prepare(mock), datetime.date(2009, 2, 13))155 156 # Simulate default=datetime.date(2000, 1, 1).157 mock = MockModel()158 default = DateField(default=datetime.date(2000, 1, 1))159 160 self.assertEqual(default.prepare(mock), datetime.date(2000, 1, 1))161class DateTimeFieldTestCase(TestCase):162 def test_init(self):163 try:164 foo = DateTimeField(model_attr='foo')165 except:166 self.fail()167 168 def test_prepare(self):169 mock = MockModel()170 mock.pub_date = datetime.datetime(2009, 2, 13, 10, 01, 00)171 pub_date = DateTimeField(model_attr='pub_date')172 173 self.assertEqual(pub_date.prepare(mock), datetime.datetime(2009, 2, 13, 10, 01, 00))174 175 # Simulate default=datetime.datetime(2009, 2, 13, 10, 01, 00).176 mock = MockModel()177 default = DateTimeField(default=datetime.datetime(2000, 1, 1, 0, 0, 0))178 179 self.assertEqual(default.prepare(mock), datetime.datetime(2000, 1, 1, 0, 0, 0))180class MultiValueFieldTestCase(TestCase):181 def test_init(self):182 try:183 foo = MultiValueField(model_attr='foo')184 except:185 self.fail()186 187 def test_prepare(self):188 mock = MockModel()189 mock.sites = ['3', '4', '5']190 sites = MultiValueField(model_attr='sites')191 192 self.assertEqual(sites.prepare(mock), ['3', '4', '5'])193 194 # Simulate default=[1].195 mock = MockModel()196 default = MultiValueField(default=[1])197 198 self.assertEqual(default.prepare(mock), [1])199 200 # Simulate null=True.201 mock = MockModel()202 multy_none = MultiValueField(null=True)203 204 self.assertEqual(multy_none.prepare(mock), None)205class CharFieldWithTemplateTestCase(TestCase):206 def test_init(self):207 try:208 foo = CharField(use_template=True)209 except:210 self.fail()211 212 try:213 foo = CharField(use_template=True, template_name='foo.txt')214 except:215 self.fail()216 217 foo = CharField(use_template=True, template_name='foo.txt')218 self.assertEqual(foo.template_name, 'foo.txt')219 220 # Test the select_template usage.221 foo = CharField(use_template=True, template_name=['bar.txt', 'foo.txt'])222 self.assertEqual(foo.template_name, ['bar.txt', 'foo.txt'])223 224 def test_prepare(self):225 mock = MockModel()226 mock.pk = 1227 mock.user = 'daniel'228 template1 = CharField(use_template=True)229 230 self.assertRaises(SearchFieldError, template1.prepare, mock)231 232 template2 = CharField(use_template=True)233 template2.instance_name = 'template_x'234 self.assertRaises(TemplateDoesNotExist, template2.prepare, mock)235 236 template3 = CharField(use_template=True)237 template3.instance_name = 'template'238 self.assertEqual(template3.prepare(mock), u'Indexed!\n1')239 240 template4 = CharField(use_template=True, template_name='search/indexes/foo.txt')241 template4.instance_name = 'template'242 self.assertEqual(template4.prepare(mock), u'FOO!\n')243 244 template5 = CharField(use_template=True, template_name=['foo.txt', 'search/indexes/bar.txt'])245 template5.instance_name = 'template'246 self.assertEqual(template5.prepare(mock), u'BAR!\n')247##############################################################################248# The following tests look like they don't do much, but it's important because249# we need to verify that the faceted variants behave like the field they250# emulate. The old-broke behavior was convert everything to string.251##############################################################################252class FacetFieldTestCase(TestCase):253 def test_init(self):254 # You shouldn't use the FacetField itself.255 try:256 foo = FacetField(model_attr='foo')257 self.fail()258 except:259 pass260 261 try:262 foo_exact = FacetField(facet_for='bar')263 self.fail()264 except:265 pass266class FacetCharFieldTestCase(TestCase):267 def test_init(self):268 try:269 foo = FacetCharField(model_attr='foo')270 foo_exact = FacetCharField(facet_for='bar')271 except:272 self.fail()273 274 self.assertEqual(foo.facet_for, None)275 self.assertEqual(foo_exact.null, True)276 self.assertEqual(foo_exact.facet_for, 'bar')277 278 def test_prepare(self):279 mock = MockModel()280 mock.user = 'daniel'281 author = FacetCharField(model_attr='user')282 283 self.assertEqual(author.prepare(mock), u'daniel')284class FacetIntegerFieldTestCase(TestCase):285 def test_init(self):286 try:287 foo = FacetIntegerField(model_attr='foo')288 foo_exact = FacetIntegerField(facet_for='bar')289 except:290 self.fail()291 292 self.assertEqual(foo.facet_for, None)293 self.assertEqual(foo_exact.null, True)294 self.assertEqual(foo_exact.facet_for, 'bar')295 296 def test_prepare(self):297 mock = MockModel()298 mock.user = 'daniel'299 mock.view_count = 13300 view_count = FacetIntegerField(model_attr='view_count')301 302 self.assertEqual(view_count.prepare(mock), 13)303class FacetFloatFieldTestCase(TestCase):304 def test_init(self):305 try:306 foo = FacetFloatField(model_attr='foo')307 foo_exact = FacetFloatField(facet_for='bar')308 except:309 self.fail()310 311 self.assertEqual(foo.facet_for, None)312 self.assertEqual(foo_exact.null, True)313 self.assertEqual(foo_exact.facet_for, 'bar')314 315 def test_prepare(self):316 mock = MockModel()317 mock.user = 'daniel'318 mock.price = 25.65319 price = FacetFloatField(model_attr='price')320 321 self.assertEqual(price.prepare(mock), 25.65)322class FacetBooleanFieldTestCase(TestCase):323 def test_init(self):324 try:325 foo = FacetBooleanField(model_attr='foo')326 foo_exact = FacetBooleanField(facet_for='bar')327 except:328 self.fail()329 330 self.assertEqual(foo.facet_for, None)331 self.assertEqual(foo_exact.null, True)332 self.assertEqual(foo_exact.facet_for, 'bar')333 334 def test_prepare(self):335 mock = MockModel()336 mock.user = 'daniel'337 mock.is_active = True338 is_active = FacetBooleanField(model_attr='is_active')339 340 self.assertEqual(is_active.prepare(mock), True)341class FacetDateFieldTestCase(TestCase):342 def test_init(self):343 try:344 foo = FacetDateField(model_attr='foo')345 foo_exact = FacetDateField(facet_for='bar')346 except:347 self.fail()348 349 self.assertEqual(foo.facet_for, None)350 self.assertEqual(foo_exact.null, True)351 self.assertEqual(foo_exact.facet_for, 'bar')352 353 def test_prepare(self):354 mock = MockModel()355 mock.user = 'daniel'356 mock.created = datetime.date(2010, 10, 30)357 created = FacetDateField(model_attr='created')358 359 self.assertEqual(created.prepare(mock), datetime.date(2010, 10, 30))360class FacetDateTimeFieldTestCase(TestCase):361 def test_init(self):362 try:363 foo = FacetDateTimeField(model_attr='foo')364 foo_exact = FacetDateTimeField(facet_for='bar')365 except:366 self.fail()367 368 self.assertEqual(foo.facet_for, None)369 self.assertEqual(foo_exact.null, True)370 self.assertEqual(foo_exact.facet_for, 'bar')371 372 def test_prepare(self):373 mock = MockModel()374 mock.user = 'daniel'375 mock.created = datetime.datetime(2010, 10, 30, 3, 14, 25)376 created = FacetDateTimeField(model_attr='created')377 378 self.assertEqual(created.prepare(mock), datetime.datetime(2010, 10, 30, 3, 14, 25))379class FacetMultiValueFieldTestCase(TestCase):380 def test_init(self):381 try:382 foo = FacetMultiValueField(model_attr='foo')383 foo_exact = FacetMultiValueField(facet_for='bar')384 except:385 self.fail()386 387 self.assertEqual(foo.facet_for, None)388 self.assertEqual(foo_exact.null, True)389 self.assertEqual(foo_exact.facet_for, 'bar')390 391 def test_prepare(self):392 mock = MockModel()393 mock.user = 'daniel'394 mock.sites = [1, 3, 4]395 sites = FacetMultiValueField(model_attr='sites')396 ...
test_venvs.py
Source:test_venvs.py
1from __future__ import annotations2from pathlib import Path3import sys4import platform5from mypythontools.misc import delete_files6from mypythontools.system import is_wsl7sys.path.insert(0, Path(__file__).parents[1].as_posix()) # pylint: disable=no-member8from conftest import prepare_test9from mypythontools_cicd import venvs10def test_prepare_venvs():11 if platform.system() == "Windows" and not is_wsl():12 delete_files(["venv/test_prepare/3.7", "venv/test_prepare /wsl-3.7"])13 venvs.prepare_venvs(path="venv/test_prepare", versions=["3.7", "3.10", "wsl-3.7", "wsl-3.10"])14 for i in ["3.7", "3.10"]:15 assert venvs.Venv(f"venv/test_prepare/{i}").installed16 for i in ["venv/test_prepare/wsl-3.7", "venv/test_prepare/wsl-3.10"]:17 assert (Path(i) / "bin").exists()18 else:19 delete_files(["venv/test_prepare/3.7"])20 venvs.prepare_venvs(path="venv/test_prepare", versions=["3.7"])21 assert venvs.Venv(f"venv/test_prepare/3.7").installed22 delete_files(["venv/test_prepare/3.7"])23if __name__ == "__main__":24 # Find paths and add to sys.path to be able to import local modules25 prepare_test()...
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!!