Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 07f01bb

Browse files
Lorak-mmkdkropachev
authored andcommitted
Replace unitttest assertRaises with pytest.raises
Replaces all uses of self.assertRaises and self.assertRaisesRegex with pytest.raises. I started this commit with command `unittest2pytest -f self_assert ./tests -w -n`. After it finished, I manually reviewed all differences, and performed fixes where necessary / useful. There were two types of fixes: - When using output value of context manager (with ... as X), unittest exposes the thrown exception as X.exception, while pytest as X.value. - In few places there were calls like `self.assertRaises(T, func, **{'arg': val, ...}`. Passing args as dict with ** is not pretty, so I desugared this into directly passing named args.
1 parent ff4d392 commit 07f01bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+719
-516
lines changed

‎tests/integration/cqlengine/columns/test_container_columns.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def tearDownClass(cls):
7373
drop_table(TestSetModel)
7474

7575
def test_add_none_fails(self):
76-
self.assertRaises(ValidationError, TestSetModel.create, **{'int_set': set([None])})
76+
with pytest.raises(ValidationError):
77+
TestSetModel.create(int_set=set([None]))
7778

7879
def test_empty_set_initial(self):
7980
"""
@@ -127,7 +128,8 @@ def test_type_validation(self):
127128
"""
128129
Tests that attempting to use the wrong types will raise an exception
129130
"""
130-
self.assertRaises(ValidationError, TestSetModel.create, **{'int_set': set(('string', True)), 'text_set': set((1, 3.0))})
131+
with pytest.raises(ValidationError):
132+
TestSetModel.create(int_set=set(('string', True)), text_set=set((1, 3.0)))
131133

132134
def test_element_count_validation(self):
133135
"""
@@ -144,7 +146,8 @@ def test_element_count_validation(self):
144146
except OperationTimedOut:
145147
#This will happen if the host is remote
146148
assert not CASSANDRA_IP.startswith("127.0.0.")
147-
self.assertRaises(ValidationError, TestSetModel.create, **{'text_set': set(str(uuid4()) for i in range(65536))})
149+
with pytest.raises(ValidationError):
150+
TestSetModel.create(text_set=set(str(uuid4()) for i in range(65536)))
148151

149152
def test_partial_updates(self):
150153
""" Tests that partial udpates work as expected """
@@ -244,7 +247,8 @@ def test_type_validation(self):
244247
"""
245248
Tests that attempting to use the wrong types will raise an exception
246249
"""
247-
self.assertRaises(ValidationError, TestListModel.create, **{'int_list': ['string', True], 'text_list': [1, 3.0]})
250+
with pytest.raises(ValidationError):
251+
TestListModel.create(int_list=['string', True], text_list=[1, 3.0])
248252

249253
def test_element_count_validation(self):
250254
"""
@@ -258,7 +262,8 @@ def test_element_count_validation(self):
258262
ex_type, ex, tb = sys.exc_info()
259263
log.warning("{0}: {1} Backtrace: {2}".format(ex_type.__name__, ex, traceback.extract_tb(tb)))
260264
del tb
261-
self.assertRaises(ValidationError, TestListModel.create, **{'text_list': [str(uuid4()) for _ in range(65536)]})
265+
with pytest.raises(ValidationError):
266+
TestListModel.create(text_list=[str(uuid4()) for _ in range(65536)])
262267

263268
def test_partial_updates(self):
264269
""" Tests that partial udpates work as expected """
@@ -332,7 +337,8 @@ def test_update_from_non_empty_to_empty(self):
332337

333338
def test_insert_none(self):
334339
pkey = uuid4()
335-
self.assertRaises(ValidationError, TestListModel.create, **{'partition': pkey, 'int_list': [None]})
340+
with pytest.raises(ValidationError):
341+
TestListModel.create(partition=pkey, int_list=[None])
336342

337343
def test_blind_list_updates_from_none(self):
338344
""" Tests that updates from None work as expected """
@@ -374,7 +380,8 @@ def test_empty_default(self):
374380
tmp.int_map['blah'] = 1
375381

376382
def test_add_none_as_map_key(self):
377-
self.assertRaises(ValidationError, TestMapModel.create, **{'int_map': {None: uuid4()}})
383+
with pytest.raises(ValidationError):
384+
TestMapModel.create(int_map={None: uuid4()})
378385

379386
def test_empty_retrieve(self):
380387
tmp = TestMapModel.create()
@@ -416,7 +423,8 @@ def test_type_validation(self):
416423
"""
417424
Tests that attempting to use the wrong types will raise an exception
418425
"""
419-
self.assertRaises(ValidationError, TestMapModel.create, **{'int_map': {'key': 2, uuid4(): 'val'}, 'text_map': {2: 5}})
426+
with pytest.raises(ValidationError):
427+
TestMapModel.create(int_map={'key': 2, uuid4(): 'val'}, text_map={2: 5})
420428

421429
def test_element_count_validation(self):
422430
"""
@@ -430,7 +438,8 @@ def test_element_count_validation(self):
430438
ex_type, ex, tb = sys.exc_info()
431439
log.warning("{0}: {1} Backtrace: {2}".format(ex_type.__name__, ex, traceback.extract_tb(tb)))
432440
del tb
433-
self.assertRaises(ValidationError, TestMapModel.create, **{'text_map': dict((str(uuid4()), i) for i in range(65536))})
441+
with pytest.raises(ValidationError):
442+
TestMapModel.create(text_map=dict((str(uuid4()), i) for i in range(65536)))
434443

435444
def test_partial_updates(self):
436445
""" Tests that partial udpates work as expected """
@@ -636,9 +645,12 @@ def test_type_validation(self):
636645
637646
@test_category object_mapper
638647
"""
639-
self.assertRaises(ValidationError, TestTupleModel.create, **{'int_tuple': ('string', True), 'text_tuple': ('test', 'test'), 'mixed_tuple': ('one', 2, 'three')})
640-
self.assertRaises(ValidationError, TestTupleModel.create, **{'int_tuple': ('string', 'string'), 'text_tuple': (1, 3.0), 'mixed_tuple': ('one', 2, 'three')})
641-
self.assertRaises(ValidationError, TestTupleModel.create, **{'int_tuple': ('string', 'string'), 'text_tuple': ('test', 'test'), 'mixed_tuple': (1, "two", 3)})
648+
with pytest.raises(ValidationError):
649+
TestTupleModel.create(int_tuple=('string', True), text_tuple=('test', 'test'), mixed_tuple=('one', 2, 'three'))
650+
with pytest.raises(ValidationError):
651+
TestTupleModel.create(int_tuple=('string', 'string'), text_tuple=(1, 3.0), mixed_tuple=('one', 2, 'three'))
652+
with pytest.raises(ValidationError):
653+
TestTupleModel.create(int_tuple=('string', 'string'), text_tuple=('test', 'test'), mixed_tuple=(1, "two", 3))
642654

643655
def test_instantiation_with_column_class(self):
644656
"""
@@ -854,12 +866,18 @@ def test_type_validation(self):
854866
set_tuple_bad_tuple_value = set((("text", "text"), ("text", "text"), ("text", "text")))
855867
set_tuple_not_set = ['This', 'is', 'not', 'a', 'set']
856868

857-
self.assertRaises(ValidationError, TestNestedModel.create, **{'list_list': list_list_bad_list_context})
858-
self.assertRaises(ValidationError, TestNestedModel.create, **{'list_list': list_list_no_list})
859-
self.assertRaises(ValidationError, TestNestedModel.create, **{'map_list': map_list_bad_value})
860-
self.assertRaises(ValidationError, TestNestedModel.create, **{'map_list': map_list_bad_key})
861-
self.assertRaises(ValidationError, TestNestedModel.create, **{'set_tuple': set_tuple_bad_tuple_value})
862-
self.assertRaises(ValidationError, TestNestedModel.create, **{'set_tuple': set_tuple_not_set})
869+
with pytest.raises(ValidationError):
870+
TestNestedModel.create(list_list=list_list_bad_list_context)
871+
with pytest.raises(ValidationError):
872+
TestNestedModel.create(list_list=list_list_no_list)
873+
with pytest.raises(ValidationError):
874+
TestNestedModel.create(map_list=map_list_bad_value)
875+
with pytest.raises(ValidationError):
876+
TestNestedModel.create(map_list=map_list_bad_key)
877+
with pytest.raises(ValidationError):
878+
TestNestedModel.create(set_tuple=set_tuple_bad_tuple_value)
879+
with pytest.raises(ValidationError):
880+
TestNestedModel.create(set_tuple=set_tuple_not_set)
863881

864882
def test_instantiation_with_column_class(self):
865883
"""

‎tests/integration/cqlengine/columns/test_validation.py

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
from tests.integration import PROTOCOL_VERSION, CASSANDRA_VERSION, greaterthanorequalcass30, greaterthanorequalcass3_11
3535
from tests.integration.cqlengine.base import BaseCassEngTestCase
36+
import pytest
3637

3738

3839
class TestDatetime(BaseCassEngTestCase):
@@ -90,7 +91,7 @@ def test_datetime_none(self):
9091

9192
def test_datetime_invalid(self):
9293
dt_value= 'INVALID'
93-
with self.assertRaises(TypeError):
94+
with pytest.raises(TypeError):
9495
self.DatetimeTest.objects.create(test_id=4, created_at=dt_value)
9596

9697
def test_datetime_timestamp(self):
@@ -185,7 +186,7 @@ def test_varint_io(self):
185186
int2 = self.VarIntTest.objects(test_id=0).first()
186187
assert int1.bignum == int2.bignum
187188

188-
with self.assertRaises(ValidationError):
189+
with pytest.raises(ValidationError):
189190
self.VarIntTest.objects.create(test_id=0, bignum="not_a_number")
190191

191192

@@ -541,22 +542,22 @@ def test_min_length(self):
541542
Ascii(min_length=5).validate('kevin')
542543
Ascii(min_length=5).validate('kevintastic')
543544

544-
with self.assertRaises(ValidationError):
545+
with pytest.raises(ValidationError):
545546
Ascii(min_length=1).validate('')
546547

547-
with self.assertRaises(ValidationError):
548+
with pytest.raises(ValidationError):
548549
Ascii(min_length=1).validate(None)
549550

550-
with self.assertRaises(ValidationError):
551+
with pytest.raises(ValidationError):
551552
Ascii(min_length=6).validate('')
552553

553-
with self.assertRaises(ValidationError):
554+
with pytest.raises(ValidationError):
554555
Ascii(min_length=6).validate(None)
555556

556-
with self.assertRaises(ValidationError):
557+
with pytest.raises(ValidationError):
557558
Ascii(min_length=6).validate('kevin')
558559

559-
with self.assertRaises(ValueError):
560+
with pytest.raises(ValueError):
560561
Ascii(min_length=-1)
561562

562563
def test_max_length(self):
@@ -573,13 +574,13 @@ def test_max_length(self):
573574
Ascii(max_length=5).validate('b')
574575
Ascii(max_length=5).validate('blake')
575576

576-
with self.assertRaises(ValidationError):
577+
with pytest.raises(ValidationError):
577578
Ascii(max_length=0).validate('b')
578579

579-
with self.assertRaises(ValidationError):
580+
with pytest.raises(ValidationError):
580581
Ascii(max_length=5).validate('blaketastic')
581582

582-
with self.assertRaises(ValueError):
583+
with pytest.raises(ValueError):
583584
Ascii(max_length=-1)
584585

585586
def test_length_range(self):
@@ -588,30 +589,30 @@ def test_length_range(self):
588589
Ascii(min_length=10, max_length=10)
589590
Ascii(min_length=10, max_length=11)
590591

591-
with self.assertRaises(ValueError):
592+
with pytest.raises(ValueError):
592593
Ascii(min_length=10, max_length=9)
593594

594-
with self.assertRaises(ValueError):
595+
with pytest.raises(ValueError):
595596
Ascii(min_length=1, max_length=0)
596597

597598
def test_type_checking(self):
598599
Ascii().validate('string')
599600
Ascii().validate(u'unicode')
600601
Ascii().validate(bytearray('bytearray', encoding='ascii'))
601602

602-
with self.assertRaises(ValidationError):
603+
with pytest.raises(ValidationError):
603604
Ascii().validate(5)
604605

605-
with self.assertRaises(ValidationError):
606+
with pytest.raises(ValidationError):
606607
Ascii().validate(True)
607608

608609
Ascii().validate("!#$%&\'()*+,-./")
609610

610-
with self.assertRaises(ValidationError):
611+
with pytest.raises(ValidationError):
611612
Ascii().validate('Beyonc' + chr(233))
612613

613614
if sys.version_info < (3, 1):
614-
with self.assertRaises(ValidationError):
615+
with pytest.raises(ValidationError):
615616
Ascii().validate(u'Beyonc' + unichr(233))
616617

617618
def test_unaltering_validation(self):
@@ -629,26 +630,26 @@ def test_required_validation(self):
629630
""" Tests that validation raise on none and blank values if value required. """
630631
Ascii(required=True).validate('k')
631632

632-
with self.assertRaises(ValidationError):
633+
with pytest.raises(ValidationError):
633634
Ascii(required=True).validate('')
634635

635-
with self.assertRaises(ValidationError):
636+
with pytest.raises(ValidationError):
636637
Ascii(required=True).validate(None)
637638

638639
# With min_length set.
639640
Ascii(required=True, min_length=0).validate('k')
640641
Ascii(required=True, min_length=1).validate('k')
641642

642-
with self.assertRaises(ValidationError):
643+
with pytest.raises(ValidationError):
643644
Ascii(required=True, min_length=2).validate('k')
644645

645646
# With max_length set.
646647
Ascii(required=True, max_length=1).validate('k')
647648

648-
with self.assertRaises(ValidationError):
649+
with pytest.raises(ValidationError):
649650
Ascii(required=True, max_length=2).validate('kevin')
650651

651-
with self.assertRaises(ValueError):
652+
with pytest.raises(ValueError):
652653
Ascii(required=True, max_length=0)
653654

654655

@@ -668,22 +669,22 @@ def test_min_length(self):
668669
Text(min_length=5).validate('blake')
669670
Text(min_length=5).validate('blaketastic')
670671

671-
with self.assertRaises(ValidationError):
672+
with pytest.raises(ValidationError):
672673
Text(min_length=1).validate('')
673674

674-
with self.assertRaises(ValidationError):
675+
with pytest.raises(ValidationError):
675676
Text(min_length=1).validate(None)
676677

677-
with self.assertRaises(ValidationError):
678+
with pytest.raises(ValidationError):
678679
Text(min_length=6).validate('')
679680

680-
with self.assertRaises(ValidationError):
681+
with pytest.raises(ValidationError):
681682
Text(min_length=6).validate(None)
682683

683-
with self.assertRaises(ValidationError):
684+
with pytest.raises(ValidationError):
684685
Text(min_length=6).validate('blake')
685686

686-
with self.assertRaises(ValueError):
687+
with pytest.raises(ValueError):
687688
Text(min_length=-1)
688689

689690
def test_max_length(self):
@@ -700,13 +701,13 @@ def test_max_length(self):
700701
Text(max_length=5).validate('b')
701702
Text(max_length=5).validate('blake')
702703

703-
with self.assertRaises(ValidationError):
704+
with pytest.raises(ValidationError):
704705
Text(max_length=0).validate('b')
705706

706-
with self.assertRaises(ValidationError):
707+
with pytest.raises(ValidationError):
707708
Text(max_length=5).validate('blaketastic')
708709

709-
with self.assertRaises(ValueError):
710+
with pytest.raises(ValueError):
710711
Text(max_length=-1)
711712

712713
def test_length_range(self):
@@ -715,21 +716,21 @@ def test_length_range(self):
715716
Text(min_length=10, max_length=10)
716717
Text(min_length=10, max_length=11)
717718

718-
with self.assertRaises(ValueError):
719+
with pytest.raises(ValueError):
719720
Text(min_length=10, max_length=9)
720721

721-
with self.assertRaises(ValueError):
722+
with pytest.raises(ValueError):
722723
Text(min_length=1, max_length=0)
723724

724725
def test_type_checking(self):
725726
Text().validate('string')
726727
Text().validate(u'unicode')
727728
Text().validate(bytearray('bytearray', encoding='ascii'))
728729

729-
with self.assertRaises(ValidationError):
730+
with pytest.raises(ValidationError):
730731
Text().validate(5)
731732

732-
with self.assertRaises(ValidationError):
733+
with pytest.raises(ValidationError):
733734
Text().validate(True)
734735

735736
Text().validate("!#$%&\'()*+,-./")
@@ -752,26 +753,26 @@ def test_required_validation(self):
752753
""" Tests that validation raise on none and blank values if value required. """
753754
Text(required=True).validate('b')
754755

755-
with self.assertRaises(ValidationError):
756+
with pytest.raises(ValidationError):
756757
Text(required=True).validate('')
757758

758-
with self.assertRaises(ValidationError):
759+
with pytest.raises(ValidationError):
759760
Text(required=True).validate(None)
760761

761762
# With min_length set.
762763
Text(required=True, min_length=0).validate('b')
763764
Text(required=True, min_length=1).validate('b')
764765

765-
with self.assertRaises(ValidationError):
766+
with pytest.raises(ValidationError):
766767
Text(required=True, min_length=2).validate('b')
767768

768769
# With max_length set.
769770
Text(required=True, max_length=1).validate('b')
770771

771-
with self.assertRaises(ValidationError):
772+
with pytest.raises(ValidationError):
772773
Text(required=True, max_length=2).validate('blake')
773774

774-
with self.assertRaises(ValueError):
775+
with pytest.raises(ValueError):
775776
Text(required=True, max_length=0)
776777

777778

@@ -781,7 +782,7 @@ class TestModel(Model):
781782
id = UUID(primary_key=True, default=uuid4)
782783

783784
def test_extra_field(self):
784-
with self.assertRaises(ValidationError):
785+
with pytest.raises(ValidationError):
785786
self.TestModel.create(bacon=5000)
786787

787788

@@ -834,5 +835,5 @@ def test_inet_saves(self):
834835

835836
def test_non_address_fails(self):
836837
# TODO: presently this only tests that the server blows it up. Is there supposed to be local validation?
837-
with self.assertRaises(InvalidRequest):
838+
with pytest.raises(InvalidRequest):
838839
self.InetTestModel.create(address="what is going on here?")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /