Move dataclass tests for 3.7 to separate file

This commit is contained in:
Aly Sivji 2018-11-12 15:36:16 -06:00
parent 4e99c80425
commit 2bffd6829e
5 changed files with 96 additions and 66 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -551,78 +551,47 @@ class TestAssert_reprcompare(object):
class TestAssert_reprcompare_dataclass(object): class TestAssert_reprcompare_dataclass(object):
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+") @pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+")
def test_dataclasses(self): def test_dataclasses(self, testdir):
from dataclasses import dataclass, field p = testdir.copy_example("dataclasses/test_compare_dataclasses.py")
result = testdir.runpytest(p)
@dataclass result.assert_outcomes(failed=1, passed=0)
class SimpleDataObject(object): result.stdout.fnmatch_lines(
field_a: int = field() [
field_b: int = field() "*Omitting 1 identical items, use -vv to show*",
"*Differing attributes:*",
left = SimpleDataObject(1, "b") "*SimpleDataObject(field_b='b') != SimpleDataObject(field_b='c')*",
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
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+") @pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+")
def test_dataclasses_verbose(self): def test_dataclasses_verbose(self, testdir):
from dataclasses import dataclass, field p = testdir.copy_example("dataclasses/test_compare_dataclasses_verbose.py")
result = testdir.runpytest(p, "-vv")
@dataclass result.assert_outcomes(failed=1, passed=0)
class SimpleDataObject(object): result.stdout.fnmatch_lines(
field_a: int = field() [
field_b: int = field() "*Common attributes:*",
"*['field_a']*",
left = SimpleDataObject(1, "b") "*Differing attributes:*",
right = SimpleDataObject(1, "c") "*SimpleDataObject(field_b='b') != SimpleDataObject(field_b='c')*",
]
lines = callequal(left, right, verbose=2) )
assert lines[1].startswith("Common attributes:")
assert "Omitting" not in lines[1]
assert lines[2] == "['field_a']"
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+") @pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+")
def test_dataclasses_with_attribute_comparison_off(self): def test_dataclasses_with_attribute_comparison_off(self, testdir):
from dataclasses import dataclass, field p = testdir.copy_example(
"dataclasses/test_compare_dataclasses_field_comparison_off.py"
@dataclass )
class SimpleDataObject(object): result = testdir.runpytest(p, "-vv")
field_a: int = field() result.assert_outcomes(failed=0, passed=1)
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
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+") @pytest.mark.skipif(sys.version_info < (3, 7), reason="Dataclasses in Python3.7+")
def test_comparing_two_different_data_classes(self): def test_comparing_two_different_data_classes(self, testdir):
from dataclasses import dataclass, field p = testdir.copy_example(
"dataclasses/test_compare_two_different_dataclasses.py"
@dataclass )
class SimpleDataObjectOne(object): result = testdir.runpytest(p, "-vv")
field_a: int = field() result.assert_outcomes(failed=0, passed=1)
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
class TestAssert_reprcompare_attrsclass(object): class TestAssert_reprcompare_attrsclass(object):