1
|
|
|
"""Integration tests to ensure a mapped object's signature is unchanged.""" |
2
|
|
|
|
3
|
|
|
# pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned |
4
|
|
|
|
5
|
|
|
import pytest |
|
|
|
|
6
|
|
|
from expecter import expect |
|
|
|
|
7
|
|
|
|
8
|
|
|
from yorm import decorators |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class SampleWithMagicMethods: |
12
|
|
|
|
13
|
|
|
def __init__(self): |
14
|
|
|
self.values = [] |
15
|
|
|
|
16
|
|
|
def __setattr__(self, name, value): |
17
|
|
|
if name == 'foobar': |
18
|
|
|
self.values.append(('__setattr__', name, value)) |
19
|
|
|
super().__setattr__(name, value) |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
@pytest.yield_fixture |
23
|
|
|
def mapped(): |
24
|
|
|
import yorm |
25
|
|
|
yorm.settings.fake = True |
26
|
|
|
yield decorators.sync(SampleWithMagicMethods(), "sample.yml") |
27
|
|
|
yorm.settings.fake = False |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
@pytest.fixture |
31
|
|
|
def unmapped(): |
32
|
|
|
return SampleWithMagicMethods() |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def fuzzy_repr(obj): |
36
|
|
|
return repr(obj).split(" object at ")[0] + ">" |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def describe_mapped_object(): |
40
|
|
|
|
41
|
|
|
def it_retains_representation(mapped, unmapped): |
42
|
|
|
expect(fuzzy_repr(mapped)) == fuzzy_repr(unmapped) |
43
|
|
|
|
44
|
|
|
def it_retains_docstring(mapped, unmapped): |
45
|
|
|
expect(mapped.__doc__) == unmapped.__doc__ |
46
|
|
|
|
47
|
|
|
def it_retains_class(mapped, unmapped): |
48
|
|
|
expect(mapped.__class__) == unmapped.__class__ |
49
|
|
|
|
50
|
|
|
def it_retains_magic_methods(mapped): |
51
|
|
|
setattr(mapped, 'foobar', 42) |
52
|
|
|
expect(mapped.values) == [('__setattr__', 'foobar', 42)] |
53
|
|
|
|
54
|
|
|
def it_does_not_affect_unmapped_objects(mapped, unmapped): |
55
|
|
|
expect(hasattr(mapped, '__mapper__')) is True |
56
|
|
|
expect(hasattr(unmapped, '__mapper__')) is False |
57
|
|
|
expect(hasattr(mapped.__setattr__, '_modified_by_yorm')) is True |
58
|
|
|
expect(hasattr(unmapped.__setattr__, '_modified_by_yorm')) is False |
59
|
|
|
|