-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_creating_builder.py
121 lines (102 loc) · 2.46 KB
/
test_creating_builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import pytest
import gqlrequests
def get_new_sometype():
class SomeType(gqlrequests.QueryBuilder):
id: int
return SomeType
def test_adding_field_by_setattr():
correct_string = """
{
id
name
}
"""[1:]
SomeType = get_new_sometype()
SomeType.name = str
assert SomeType().build() == correct_string
def test_removing_field_by_setattr():
correct_string = """
{
id
}
"""[1:]
SomeType = get_new_sometype()
SomeType.name = None
assert SomeType().build() == correct_string
def test_adding_field_by_add_field():
correct_string = """
{
id
name
}
"""[1:]
SomeType = get_new_sometype()
SomeType.add_field("name", str)
assert SomeType().build() == correct_string
def test_removing_field_by_remove_field():
correct_string = """
{
id
}
"""[1:]
SomeType = get_new_sometype()
SomeType.add_field("name", str)
SomeType.remove_field("name")
assert SomeType().build() == correct_string
def test_removing_invalid_field_just_does_nothing():
correct_string = """
{
id
}
"""[1:]
SomeType = get_new_sometype()
SomeType.remove_field("name")
assert SomeType().build() == correct_string
def test_adding_existing_field_overwrites_type():
correct_string = """
{
id
name
}
"""[1:]
SomeType = get_new_sometype()
SomeType.name = int
assert SomeType()._resolved_fields["name"] == int
SomeType.name = str
assert SomeType().build() == correct_string
assert SomeType()._resolved_fields["name"] == str
class SomeClass(gqlrequests.QueryBuilder):
pass
class InvalidType(gqlrequests.QueryBuilder):
invalidProperty: SomeClass
def test_invalid_type_throws_value_error():
with pytest.raises(ValueError) as e:
InvalidType().build()
def test_string_representation_of_class_shows_graphql_syntax():
class EveryType(gqlrequests.QueryBuilder):
id: int
age: int
money: float
name: str
company: bool
correct_string = """
type EveryType {
id: int
age: int
money: float
name: str
company: bool
}
"""[1:]
assert str(EveryType) == correct_string
def test_string_representation_of_nested_class_shows_graphql_syntax():
correct_string = """
type NestedType {
something: SomethingType
}
"""[1:]
class SomethingType(gqlrequests.QueryBuilder):
id: int
class NestedType(gqlrequests.QueryBuilder):
something: SomethingType
assert str(NestedType) == correct_string