Skip to content

Commit 74ab989

Browse files
committed
backend: add some sample tests
Not comprehensive but enough of a starting point for further modifications.
1 parent a668e5b commit 74ab989

File tree

5 files changed

+279
-1
lines changed

5 files changed

+279
-1
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,23 @@ cd backend
1515
python3 -m venv venv
1616
source venv/bin/activate
1717
pip install -r requirements.txt
18+
```
19+
20+
Seed the database
21+
22+
```
1823
python seed.py
24+
```
25+
26+
Run tests (optional)
27+
28+
```
29+
pytest
30+
```
31+
32+
Start backend server
33+
34+
```
1935
flask run
2036
```
2137

backend/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
backref)
44
from sqlalchemy.ext.declarative import declarative_base
55

6-
engine = create_engine('sqlite:///database.sqlite3', convert_unicode=True)
6+
engine = create_engine('sqlite:///database.sqlite3')
77
db_session = scoped_session(sessionmaker(autocommit=False,
88
autoflush=False,
99
bind=engine))

backend/requirements.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
aniso8601==7.0.0
2+
attrs==19.3.0
23
click==7.1.2
34
Flask==1.1.2
45
Flask-GraphQL==2.0.1
@@ -7,14 +8,23 @@ graphene-sqlalchemy==2.3.0
78
graphql-core==2.3.2
89
graphql-relay==2.0.1
910
graphql-server-core==1.2.0
11+
importlib-metadata==1.7.0
1012
install==1.3.3
1113
itsdangerous==1.1.0
1214
Jinja2==2.11.2
1315
MarkupSafe==1.1.1
16+
more-itertools==8.4.0
17+
packaging==20.4
18+
pluggy==0.13.1
1419
promise==2.3
20+
py==1.9.0
21+
pyparsing==2.4.7
22+
pytest==5.4.3
1523
python-dotenv==0.14.0
1624
Rx==1.6.1
1725
singledispatch==3.4.0.3
1826
six==1.15.0
1927
SQLAlchemy==1.3.18
28+
wcwidth==0.2.5
2029
Werkzeug==1.0.1
30+
zipp==3.1.0

backend/test_models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from models import User, Todo
2+
3+
4+
def test_basic():
5+
me = User.query.filter(User.name == 'me').first()
6+
assert me.todos.count() == 2
7+
assert me.todos.filter(Todo.complete == True).count() == 1
8+
assert [todo.text for todo in me.todos.all()] == ['Taste JavaScript', 'Buy a unicorn']
9+
10+
other = User.query.filter(User.name == 'other').first()
11+
assert other.todos.count() == 2
12+
assert other.todos.filter(Todo.complete == True).count() == 1
13+
assert [todo.text for todo in other.todos.all()] == ['This should be hidden', 'Secret task']

backend/test_schema.py

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
from graphene.test import Client
2+
from schema import schema
3+
4+
5+
def test_basic_query():
6+
client = Client(schema)
7+
executed = client.execute('''
8+
query {
9+
user (id: "VXNlcjox") {
10+
name
11+
}
12+
}
13+
''')
14+
assert executed == {
15+
'data': {
16+
'user': {
17+
'name': 'me',
18+
},
19+
},
20+
}
21+
22+
23+
def test_all_users_query():
24+
client = Client(schema)
25+
executed = client.execute('''
26+
query {
27+
allUsers {
28+
edges {
29+
node {
30+
id
31+
name
32+
}
33+
}
34+
}
35+
}
36+
''')
37+
assert executed == {
38+
'data': {
39+
'allUsers': {
40+
'edges': [{
41+
'node': {
42+
'id': 'VXNlcjox',
43+
'name': 'me'
44+
}
45+
}, {
46+
'node': {
47+
'id': 'VXNlcjoy',
48+
'name': 'other'
49+
}
50+
}]
51+
},
52+
},
53+
}
54+
55+
56+
def test_viewer_query():
57+
client = Client(schema)
58+
executed = client.execute('''
59+
query {
60+
viewer {
61+
totalCount
62+
completedCount
63+
todos {
64+
edges {
65+
node {
66+
id
67+
complete
68+
text
69+
}
70+
}
71+
}
72+
anyTodos: todos (status: "any") {
73+
edges {
74+
node {
75+
id
76+
complete
77+
text
78+
}
79+
}
80+
}
81+
completedTodos: todos (status: "completed") {
82+
edges {
83+
node {
84+
id
85+
complete
86+
text
87+
}
88+
}
89+
}
90+
}
91+
}
92+
''')
93+
completed_todo = {
94+
'node': {
95+
'id': 'VG9kbzox',
96+
'complete': True,
97+
'text': 'Taste JavaScript',
98+
},
99+
}
100+
remaining_todo = {
101+
'node': {
102+
'id': 'VG9kbzoy',
103+
'complete': False,
104+
'text': 'Buy a unicorn',
105+
},
106+
}
107+
assert executed == {
108+
'data': {
109+
'viewer': {
110+
'totalCount': 2,
111+
'completedCount': 1,
112+
'todos': {
113+
'edges': [
114+
completed_todo,
115+
remaining_todo,
116+
],
117+
},
118+
'anyTodos': {
119+
'edges': [
120+
completed_todo,
121+
remaining_todo,
122+
],
123+
},
124+
'completedTodos': {
125+
'edges': [
126+
completed_todo,
127+
],
128+
},
129+
},
130+
},
131+
}
132+
133+
134+
def test_mutation():
135+
client = Client(schema)
136+
executed = client.execute('''
137+
query {
138+
user (id: "VXNlcjox") {
139+
completedCount
140+
}
141+
todo (id: "VG9kbzox") {
142+
complete
143+
text
144+
}
145+
}
146+
''')
147+
assert executed == {
148+
'data': {
149+
'user': {
150+
'completedCount': 1,
151+
},
152+
'todo': {
153+
'complete': True,
154+
'text': 'Taste JavaScript',
155+
},
156+
},
157+
}
158+
159+
variables = {
160+
'input': {
161+
'id': 'VG9kbzox',
162+
'complete': False,
163+
'clientMutationId': 0,
164+
}
165+
}
166+
executed = client.execute('''
167+
mutation ($input: ChangeTodoStatusInput!) {
168+
changeTodoStatus(input: $input) {
169+
todo { id complete }
170+
viewer { id completedCount }
171+
}
172+
}''', variable_values=variables)
173+
assert executed == {
174+
'data': {
175+
'changeTodoStatus': {
176+
'todo': {
177+
'id': 'VG9kbzox',
178+
'complete': False,
179+
},
180+
'viewer': {
181+
'id': 'VXNlcjox',
182+
'completedCount': 0,
183+
},
184+
},
185+
},
186+
}
187+
188+
executed = client.execute('''
189+
query {
190+
user (id: "VXNlcjox") {
191+
completedCount
192+
}
193+
todo (id: "VG9kbzox") {
194+
complete
195+
text
196+
}
197+
}
198+
''')
199+
assert executed == {
200+
'data': {
201+
'user': {
202+
'completedCount': 0,
203+
},
204+
'todo': {
205+
'complete': False,
206+
'text': 'Taste JavaScript',
207+
},
208+
},
209+
}
210+
211+
# revert changes
212+
variables = {
213+
'input': {
214+
'id': 'VG9kbzox',
215+
'complete': True,
216+
'clientMutationId': 0,
217+
},
218+
}
219+
executed = client.execute('''
220+
mutation ($input: ChangeTodoStatusInput!) {
221+
changeTodoStatus(input: $input) {
222+
todo { id complete }
223+
viewer { id completedCount }
224+
}
225+
}''', variable_values=variables)
226+
assert executed == {
227+
'data': {
228+
'changeTodoStatus': {
229+
'todo': {
230+
'id': 'VG9kbzox',
231+
'complete': True,
232+
},
233+
'viewer': {
234+
'id': 'VXNlcjox',
235+
'completedCount': 1,
236+
},
237+
},
238+
},
239+
}

0 commit comments

Comments
 (0)