From 2bffd6829eb38a44f9e7523d044dad7339830691 Mon Sep 17 00:00:00 2001 From: Aly Sivji Date: Mon, 12 Nov 2018 15:36:16 -0600 Subject: [PATCH] Move dataclass tests for 3.7 to separate file --- .../dataclasses/test_compare_dataclasses.py | 14 +++ ...ompare_dataclasses_field_comparison_off.py | 14 +++ .../test_compare_dataclasses_verbose.py | 14 +++ .../test_compare_two_different_dataclasses.py | 19 ++++ testing/test_assertion.py | 101 ++++++------------ 5 files changed, 96 insertions(+), 66 deletions(-) create mode 100644 testing/example_scripts/dataclasses/test_compare_dataclasses.py create mode 100644 testing/example_scripts/dataclasses/test_compare_dataclasses_field_comparison_off.py create mode 100644 testing/example_scripts/dataclasses/test_compare_dataclasses_verbose.py create mode 100644 testing/example_scripts/dataclasses/test_compare_two_different_dataclasses.py diff --git a/testing/example_scripts/dataclasses/test_compare_dataclasses.py b/testing/example_scripts/dataclasses/test_compare_dataclasses.py new file mode 100644 index 000000000..3bbebe2aa --- /dev/null +++ b/testing/example_scripts/dataclasses/test_compare_dataclasses.py @@ -0,0 +1,14 @@ +from dataclasses import dataclass +from dataclasses import field + + +def test_dataclasses(): + @dataclass + class SimpleDataObject(object): + field_a: int = field() + field_b: int = field() + + left = SimpleDataObject(1, "b") + right = SimpleDataObject(1, "c") + + assert left == right diff --git a/testing/example_scripts/dataclasses/test_compare_dataclasses_field_comparison_off.py b/testing/example_scripts/dataclasses/test_compare_dataclasses_field_comparison_off.py new file mode 100644 index 000000000..63b9f534e --- /dev/null +++ b/testing/example_scripts/dataclasses/test_compare_dataclasses_field_comparison_off.py @@ -0,0 +1,14 @@ +from dataclasses import dataclass +from dataclasses import field + + +def test_dataclasses_with_attribute_comparison_off(): + @dataclass + class SimpleDataObject(object): + field_a: int = field() + field_b: int = field(compare=False) + + left = SimpleDataObject(1, "b") + right = SimpleDataObject(1, "c") + + assert left == right diff --git a/testing/example_scripts/dataclasses/test_compare_dataclasses_verbose.py b/testing/example_scripts/dataclasses/test_compare_dataclasses_verbose.py new file mode 100644 index 000000000..17835c0c3 --- /dev/null +++ b/testing/example_scripts/dataclasses/test_compare_dataclasses_verbose.py @@ -0,0 +1,14 @@ +from dataclasses import dataclass +from dataclasses import field + + +def test_dataclasses_verbose(): + @dataclass + class SimpleDataObject(object): + field_a: int = field() + field_b: int = field() + + left = SimpleDataObject(1, "b") + right = SimpleDataObject(1, "c") + + assert left == right diff --git a/testing/example_scripts/dataclasses/test_compare_two_different_dataclasses.py b/testing/example_scripts/dataclasses/test_compare_two_different_dataclasses.py new file mode 100644 index 000000000..24f185d8a --- /dev/null +++ b/testing/example_scripts/dataclasses/test_compare_two_different_dataclasses.py @@ -0,0 +1,19 @@ +from dataclasses import dataclass +from dataclasses import field + + +def test_comparing_two_different_data_classes(): + @dataclass + class SimpleDataObjectOne(object): + field_a: int = field() + field_b: int = field() + + @dataclass + class SimpleDataObjectTwo(object): + field_a: int = field() + field_b: int = field() + + left = SimpleDataObjectOne(1, "b") + right = SimpleDataObjectTwo(1, "c") + + assert left != right diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 4a09187b7..2a55f70bc 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -551,78 +551,47 @@ class TestAssert_reprcompare(object): class TestAssert_reprcompare_dataclass(object): @pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+") - def test_dataclasses(self): - from dataclasses import dataclass, field - - @dataclass - class SimpleDataObject(object): - field_a: int = field() - field_b: int = field() - - left = SimpleDataObject(1, "b") - right = SimpleDataObject(1, "c") - - lines = callequal(left, right) - assert lines[1].startswith("Omitting 1 identical item") - assert "Common attributes" not in lines - for line in lines[1:]: - assert "field_a" not in line + def test_dataclasses(self, testdir): + p = testdir.copy_example("dataclasses/test_compare_dataclasses.py") + result = testdir.runpytest(p) + result.assert_outcomes(failed=1, passed=0) + result.stdout.fnmatch_lines( + [ + "*Omitting 1 identical items, use -vv to show*", + "*Differing attributes:*", + "*SimpleDataObject(field_b='b') != SimpleDataObject(field_b='c')*", + ] + ) @pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+") - def test_dataclasses_verbose(self): - from dataclasses import dataclass, field - - @dataclass - class SimpleDataObject(object): - field_a: int = field() - field_b: int = field() - - left = SimpleDataObject(1, "b") - right = SimpleDataObject(1, "c") - - lines = callequal(left, right, verbose=2) - assert lines[1].startswith("Common attributes:") - assert "Omitting" not in lines[1] - assert lines[2] == "['field_a']" + def test_dataclasses_verbose(self, testdir): + p = testdir.copy_example("dataclasses/test_compare_dataclasses_verbose.py") + result = testdir.runpytest(p, "-vv") + result.assert_outcomes(failed=1, passed=0) + result.stdout.fnmatch_lines( + [ + "*Common attributes:*", + "*['field_a']*", + "*Differing attributes:*", + "*SimpleDataObject(field_b='b') != SimpleDataObject(field_b='c')*", + ] + ) @pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+") - def test_dataclasses_with_attribute_comparison_off(self): - from dataclasses import dataclass, field - - @dataclass - class SimpleDataObject(object): - field_a: int = field() - field_b: int = field(compare=False) - - left = SimpleDataObject(1, "b") - right = SimpleDataObject(1, "b") - - lines = callequal(left, right, verbose=2) - assert lines[1].startswith("Common attributes:") - assert "Omitting" not in lines[1] - assert lines[2] == "['field_a']" - for line in lines[2:]: - assert "field_b" not in line + def test_dataclasses_with_attribute_comparison_off(self, testdir): + p = testdir.copy_example( + "dataclasses/test_compare_dataclasses_field_comparison_off.py" + ) + result = testdir.runpytest(p, "-vv") + result.assert_outcomes(failed=0, passed=1) @pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+") - def test_comparing_two_different_data_classes(self): - from dataclasses import dataclass, field - - @dataclass - class SimpleDataObjectOne(object): - field_a: int = field() - field_b: int = field() - - @dataclass - class SimpleDataObjectTwo(object): - field_a: int = field() - field_b: int = field() - - left = SimpleDataObjectOne(1, "b") - right = SimpleDataObjectTwo(1, "c") - - lines = callequal(left, right) - assert lines is None + def test_comparing_two_different_data_classes(self, testdir): + p = testdir.copy_example( + "dataclasses/test_compare_two_different_dataclasses.py" + ) + result = testdir.runpytest(p, "-vv") + result.assert_outcomes(failed=0, passed=1) class TestAssert_reprcompare_attrsclass(object):