Skip to content

Commit 49deca6

Browse files
committed
test: split dev-phase test files by feature
1 parent c705929 commit 49deca6

7 files changed

Lines changed: 622 additions & 1 deletion

File tree

‎tests/test_callable.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pytest
2+
3+
from assertpy2 import assert_that
4+
5+
6+
class TestIsCallable:
7+
def test_callable_lambda(self):
8+
assert_that(lambda: None).is_callable()
9+
10+
def test_callable_function(self):
11+
assert_that(print).is_callable()
12+
13+
def test_callable_class(self):
14+
assert_that(int).is_callable()
15+
16+
def test_callable_method(self):
17+
assert_that("foo".upper).is_callable()
18+
19+
def test_callable_failure(self):
20+
with pytest.raises(AssertionError) as exc_info:
21+
assert_that(42).is_callable()
22+
assert_that(str(exc_info.value)).is_equal_to("Expected <42> to be callable, but was not.")
23+
24+
def test_callable_string_failure(self):
25+
with pytest.raises(AssertionError) as exc_info:
26+
assert_that("foo").is_callable()
27+
assert_that(str(exc_info.value)).is_equal_to("Expected <foo> to be callable, but was not.")
28+
29+
30+
class TestIsNotCallable:
31+
def test_not_callable_int(self):
32+
assert_that(42).is_not_callable()
33+
34+
def test_not_callable_string(self):
35+
assert_that("foo").is_not_callable()
36+
37+
def test_not_callable_none(self):
38+
assert_that(None).is_not_callable()
39+
40+
def test_not_callable_failure(self):
41+
with pytest.raises(AssertionError) as exc_info:
42+
assert_that(print).is_not_callable()
43+
assert_that(str(exc_info.value)).contains("to not be callable, but was.")
44+
45+
def test_not_callable_lambda_failure(self):
46+
with pytest.raises(AssertionError) as exc_info:
47+
assert_that(lambda: None).is_not_callable()
48+
assert_that(str(exc_info.value)).contains("to not be callable, but was.")

‎tests/test_chaining.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import datetime
2+
3+
from assertpy2 import assert_that
4+
5+
6+
class TestChaining:
7+
def test_callable_chain(self):
8+
assert_that(print).is_callable().is_not_none()
9+
10+
def test_satisfy_chain(self):
11+
assert_that([1, 2, 3]).any_satisfy(lambda x: x > 2).none_satisfy(lambda x: x < 0)
12+
13+
def test_contains_exactly_chain(self):
14+
assert_that([1, 2, 3]).contains_exactly(1, 2, 3).is_length(3)
15+
16+
def test_contains_in_order_chain(self):
17+
assert_that([1, 2, 3]).contains_in_order(1, 3).is_not_empty()
18+
19+
def test_datetime_chain(self):
20+
d1 = datetime.datetime(2020, 1, 1)
21+
d2 = datetime.datetime(2020, 12, 31)
22+
assert_that(d1).is_before_or_equal_to(d2).is_after_or_equal_to(d1)

‎tests/test_datetime.py‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,3 +506,74 @@ def test_is_equal_to_ignoring_seconds_each_component_mismatch():
506506
):
507507
with pytest.raises(AssertionError):
508508
assert_that(base).is_equal_to_ignoring_seconds(other)
509+
510+
511+
class TestIsBeforeOrEqualTo:
512+
def test_before(self):
513+
d1 = datetime.datetime(2020, 1, 1)
514+
d2 = datetime.datetime(2020, 1, 2)
515+
assert_that(d1).is_before_or_equal_to(d2)
516+
517+
def test_equal(self):
518+
d1 = datetime.datetime(2020, 1, 1, 12, 0, 0)
519+
assert_that(d1).is_before_or_equal_to(d1)
520+
521+
def test_failure(self):
522+
d1 = datetime.datetime(2020, 1, 2)
523+
d2 = datetime.datetime(2020, 1, 1)
524+
with pytest.raises(AssertionError) as exc_info:
525+
assert_that(d1).is_before_or_equal_to(d2)
526+
assert_that(str(exc_info.value)).contains("to be before or equal to")
527+
528+
def test_bad_val_type(self):
529+
with pytest.raises(TypeError) as exc_info:
530+
assert_that("foo").is_before_or_equal_to(datetime.datetime.now())
531+
assert_that(str(exc_info.value)).contains("val must be datetime")
532+
533+
def test_bad_arg_type(self):
534+
with pytest.raises(TypeError) as exc_info:
535+
assert_that(datetime.datetime.now()).is_before_or_equal_to("foo")
536+
assert_that(str(exc_info.value)).contains("given arg must be datetime")
537+
538+
def test_date_not_datetime_val(self):
539+
with pytest.raises(TypeError) as exc_info:
540+
assert_that(datetime.date(2020, 1, 1)).is_before_or_equal_to(datetime.datetime.now())
541+
assert_that(str(exc_info.value)).contains("val must be datetime")
542+
543+
def test_date_not_datetime_arg(self):
544+
with pytest.raises(TypeError) as exc_info:
545+
assert_that(datetime.datetime.now()).is_before_or_equal_to(datetime.date(2020, 1, 1))
546+
assert_that(str(exc_info.value)).contains("given arg must be datetime")
547+
548+
549+
class TestIsAfterOrEqualTo:
550+
def test_after(self):
551+
d1 = datetime.datetime(2020, 1, 2)
552+
d2 = datetime.datetime(2020, 1, 1)
553+
assert_that(d1).is_after_or_equal_to(d2)
554+
555+
def test_equal(self):
556+
d1 = datetime.datetime(2020, 1, 1, 12, 0, 0)
557+
assert_that(d1).is_after_or_equal_to(d1)
558+
559+
def test_failure(self):
560+
d1 = datetime.datetime(2020, 1, 1)
561+
d2 = datetime.datetime(2020, 1, 2)
562+
with pytest.raises(AssertionError) as exc_info:
563+
assert_that(d1).is_after_or_equal_to(d2)
564+
assert_that(str(exc_info.value)).contains("to be after or equal to")
565+
566+
def test_bad_val_type(self):
567+
with pytest.raises(TypeError) as exc_info:
568+
assert_that("foo").is_after_or_equal_to(datetime.datetime.now())
569+
assert_that(str(exc_info.value)).contains("val must be datetime")
570+
571+
def test_bad_arg_type(self):
572+
with pytest.raises(TypeError) as exc_info:
573+
assert_that(datetime.datetime.now()).is_after_or_equal_to("foo")
574+
assert_that(str(exc_info.value)).contains("given arg must be datetime")
575+
576+
def test_date_not_datetime_val(self):
577+
with pytest.raises(TypeError) as exc_info:
578+
assert_that(datetime.date(2020, 1, 1)).is_after_or_equal_to(datetime.datetime.now())
579+
assert_that(str(exc_info.value)).contains("val must be datetime")

‎tests/test_expected_exception.py‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from assertpy2 import WarningLoggingAdapter, assert_that, assert_warn
6+
from assertpy2 import WarningLoggingAdapter, assert_that, assert_warn, soft_assertions
77

88

99
def test_expected_exception():
@@ -177,3 +177,52 @@ def test_returned_without_return_value_fails():
177177
with pytest.raises(TypeError) as exc_info:
178178
assert_that(func_no_arg).raises(RuntimeError).when_called_with().returned()
179179
assert_that(str(exc_info.value)).contains("no return value captured")
180+
181+
182+
class TestDoesNotRaise:
183+
def test_no_exception(self):
184+
def safe_func(x):
185+
return x + 1
186+
187+
assert_that(safe_func).does_not_raise(ValueError).when_called_with(1)
188+
189+
def test_different_exception(self):
190+
def raises_type_error():
191+
raise TypeError("oops")
192+
193+
assert_that(raises_type_error).does_not_raise(ValueError).when_called_with()
194+
195+
def test_raises_expected_failure(self):
196+
def raises_value_error():
197+
raise ValueError("bad value")
198+
199+
with pytest.raises(AssertionError) as exc_info:
200+
assert_that(raises_value_error).does_not_raise(ValueError).when_called_with()
201+
assert_that(str(exc_info.value)).contains("to not raise <ValueError>")
202+
assert_that(str(exc_info.value)).contains("but did raise")
203+
204+
def test_raises_subclass_failure(self):
205+
def raises_file_not_found():
206+
raise FileNotFoundError("missing")
207+
208+
with pytest.raises(AssertionError) as exc_info:
209+
assert_that(raises_file_not_found).does_not_raise(OSError).when_called_with()
210+
assert_that(str(exc_info.value)).contains("to not raise <OSError>")
211+
212+
def test_not_callable_failure(self):
213+
with pytest.raises(TypeError) as exc_info:
214+
assert_that(42).does_not_raise(ValueError)
215+
assert_that(str(exc_info.value)).is_equal_to("val must be callable")
216+
217+
def test_not_exception_failure(self):
218+
with pytest.raises(TypeError) as exc_info:
219+
assert_that(lambda: None).does_not_raise(str)
220+
assert_that(str(exc_info.value)).is_equal_to("given arg must be exception")
221+
222+
def test_raises_expected_soft_mode(self):
223+
def raises_value_error():
224+
raise ValueError("bad value")
225+
226+
with pytest.raises(AssertionError) as exc_info, soft_assertions():
227+
assert_that(raises_value_error).does_not_raise(ValueError).when_called_with()
228+
assert_that(str(exc_info.value)).contains("to not raise <ValueError>")

‎tests/test_list.py‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,73 @@ def test_list_of_dicts():
412412
class Person:
413413
def __init__(self, name):
414414
self.name = name
415+
416+
417+
class TestContainsExactly:
418+
def test_list(self):
419+
assert_that([1, 2, 3]).contains_exactly(1, 2, 3)
420+
421+
def test_tuple(self):
422+
assert_that((1, 2, 3)).contains_exactly(1, 2, 3)
423+
424+
def test_string_chars(self):
425+
assert_that("abc").contains_exactly("a", "b", "c")
426+
427+
def test_wrong_order_failure(self):
428+
with pytest.raises(AssertionError) as exc_info:
429+
assert_that([1, 2, 3]).contains_exactly(1, 3, 2)
430+
assert_that(str(exc_info.value)).contains("to contain exactly")
431+
432+
def test_missing_items_failure(self):
433+
with pytest.raises(AssertionError) as exc_info:
434+
assert_that([1, 2, 3]).contains_exactly(1, 2)
435+
assert_that(str(exc_info.value)).contains("to contain exactly")
436+
437+
def test_extra_items_failure(self):
438+
with pytest.raises(AssertionError) as exc_info:
439+
assert_that([1, 2]).contains_exactly(1, 2, 3)
440+
assert_that(str(exc_info.value)).contains("to contain exactly")
441+
442+
def test_empty_args_failure(self):
443+
with pytest.raises(ValueError) as exc_info:
444+
assert_that([1]).contains_exactly()
445+
assert_that(str(exc_info.value)).is_equal_to("one or more args must be given")
446+
447+
def test_not_iterable_failure(self):
448+
with pytest.raises(TypeError) as exc_info:
449+
assert_that(42).contains_exactly(1)
450+
assert_that(str(exc_info.value)).is_equal_to("val is not iterable")
451+
452+
453+
class TestContainsInOrder:
454+
def test_all_present(self):
455+
assert_that([1, 5, 2, 8, 3]).contains_in_order(1, 2, 3)
456+
457+
def test_contiguous(self):
458+
assert_that([1, 2, 3]).contains_in_order(1, 2, 3)
459+
460+
def test_strings(self):
461+
assert_that(["a", "x", "b", "y", "c"]).contains_in_order("a", "b", "c")
462+
463+
def test_single_item(self):
464+
assert_that([1, 2, 3]).contains_in_order(2)
465+
466+
def test_wrong_order_failure(self):
467+
with pytest.raises(AssertionError) as exc_info:
468+
assert_that([1, 2, 3]).contains_in_order(3, 1)
469+
assert_that(str(exc_info.value)).contains("in order, but did not")
470+
471+
def test_missing_item_failure(self):
472+
with pytest.raises(AssertionError) as exc_info:
473+
assert_that([1, 2, 3]).contains_in_order(1, 4)
474+
assert_that(str(exc_info.value)).contains("in order, but did not")
475+
476+
def test_empty_args_failure(self):
477+
with pytest.raises(ValueError) as exc_info:
478+
assert_that([1]).contains_in_order()
479+
assert_that(str(exc_info.value)).is_equal_to("one or more args must be given")
480+
481+
def test_not_iterable_failure(self):
482+
with pytest.raises(TypeError) as exc_info:
483+
assert_that(42).contains_in_order(1)
484+
assert_that(str(exc_info.value)).is_equal_to("val is not iterable")

0 commit comments

Comments
 (0)