Skip to content

Commit 4eafe24

Browse files
committed
Remove XML exercises, update GraphQL exercise
1 parent b2ebffb commit 4eafe24

File tree

10 files changed

+147
-479
lines changed

10 files changed

+147
-479
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ jobs:
3434
run: pytest ./answers/answers_02.py
3535

3636
- name: Run Answers 03
37-
run: pytest ./answers/answers_03.py
37+
run: pytest ./answers/answers_03.py
38+
39+
- name: Run Answers 04
40+
run: pytest ./answers/answers_04.py

answers/answers_04.py

Lines changed: 57 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,71 @@
1-
from lxml import etree
2-
31
import requests
2+
import pytest
43

54

65
# Exercise 4.1
7-
# Create a function create_xml_body_from_string()
8-
# that returns a docstring (with triple double quotes)
9-
# containing the following XML document:
10-
# <payee>
11-
# <name>John Smith</name>
12-
# <address>
13-
# <street>My street</street>
14-
# <city>My city</city>
15-
# <state>My state</state>
16-
# <zipCode>90210</zipCode>
17-
# </address>
18-
# <phoneNumber>0123456789</phoneNumber>
19-
# <accountNumber>12345</accountNumber>
20-
# </payee>
21-
def create_xml_body_from_string():
22-
return """
23-
<payee>
24-
<name>John Smith</name>
25-
<address>
26-
<street>My street</street>
27-
<city>My city</city>
28-
<state>My state</state>
29-
<zipCode>90210</zipCode>
30-
</address>
31-
<phoneNumber>0123456789</phoneNumber>
32-
<accountNumber>12345</accountNumber>
33-
</payee>
34-
"""
35-
6+
# Create a new GraphQL query as a String with value { company { name ceo coo } }
7+
# POST this object to https://spacex-production.up.railway.app/
8+
# Assert that the name of the CEO is Elon Musk
9+
# The name can be found using ['data']['company']['ceo']
10+
def test_get_company_data_check_ceo_should_be_elon_musk():
3611

37-
# Exercise 4.2
38-
# Write a test that POSTs the object created in 4.1
39-
# to https://parabank.parasoft.com/parabank/services/bank/billpay?accountId=12345&amount=500
40-
# Set the request header 'Content-Type' to 'application/xml'
41-
# Then check that the response status code is 200
42-
# and that the value of the response header 'Content-Type' is also equal to 'application/xml'
43-
def test_send_xml_body_from_docstring_check_status_code_is_200_and_name_is_correct():
4412
response = requests.post(
45-
"https://parabank.parasoft.com/parabank/services/bank/billpay?accountId=12345&amount=500",
46-
headers={"Content-Type": "application/xml"},
47-
data=create_xml_body_from_string(),
13+
'https://spacex-production.up.railway.app/',
14+
json={'query': '{ company { name ceo coo } }'}
4815
)
49-
assert response.status_code == 200
50-
assert response.headers["Content-Type"] == "application/xml"
16+
response_body = response.json()
17+
assert response_body['data']['company']['ceo'] == 'Elon Musk'
18+
19+
20+
# Exercise 4.2
21+
# Create a test data source (a list of test data tuples)
22+
# containing the following test data:
23+
# --------------------------------------------------------------------------
24+
# rocket id | rocket name | country
25+
# --------------------------------------------------------------------------
26+
# 5e9d0d95eda69955f709d1eb | Falcon 1 | Republic of the Marshall Islands
27+
# 5e9d0d95eda69974db09d1ed | Falcon Heavy | United States
28+
# 5e9d0d96eda699382d09d1ee | Starship | United States
29+
test_data_rockets = [
30+
('5e9d0d95eda69955f709d1eb', 'Falcon 1', 'Republic of the Marshall Islands'),
31+
('5e9d0d95eda69974db09d1ed', 'Falcon Heavy', 'United States'),
32+
('5e9d0d96eda699382d09d1ee', 'Starship', 'United States')
33+
]
5134

5235

5336
# Exercise 4.3
54-
# Write a method create_xml_body_using_elementtree() that returns
55-
# the same request body as in Exercise 4.1, but now uses the
56-
# etree library from lxml (I've imported that for you already, it's available as 'etree')
57-
# Make your life a little easier by specifying all element values as strings
58-
def create_xml_body_using_elementtree():
59-
payee = etree.Element("payee")
60-
name = etree.SubElement(payee, "name")
61-
name.text = "John Smith"
62-
address = etree.SubElement(payee, "address")
63-
street = etree.SubElement(address, "street")
64-
street.text = "My street"
65-
city = etree.SubElement(address, "city")
66-
city.text = "My city"
67-
state = etree.SubElement(address, "state")
68-
state.text = "My state"
69-
zip_code = etree.SubElement(address, "zipCode")
70-
zip_code.text = "90210"
71-
phone_number = etree.SubElement(payee, "phoneNumber")
72-
phone_number.text = "0123456789"
73-
account_number = etree.SubElement(payee, "accountNumber")
74-
account_number.text = "12345"
75-
return payee
37+
# Write a test that POSTs the given parameterized GraphQL query to
38+
# https://spacex-production.up.railway.app/, together with the rocket id as
39+
# the value for the id variable, for all test cases in the test data source.
40+
#
41+
# Assert that the name of the rocket is equal to the value in the data source
42+
# Use ['data']['rocket']['name'] to extract it from the JSON response body.
43+
#
44+
# Assert that the country where the rocket was launched is equal to the value in the data source
45+
# Use ['data']['rocket']['country'] to extract it from the JSON response body.
46+
query_rocket_parameterized = """
47+
query getRocketData($id: ID!)
48+
{
49+
rocket(id: $id) {
50+
name
51+
country
52+
}
53+
}
54+
"""
55+
7656

57+
@pytest.mark.parametrize('rocket_id, rocket_name, country', test_data_rockets)
58+
def test_get_rocket_data_check_name_and_country_should_equal_expected(rocket_id, rocket_name, country):
7759

78-
# Exercise 4.4
79-
# Repeat Exercise 4.2, but now use the XML document created in Exercise 4.3
80-
# Don't forget to convert the XML document to a string before sending it!
81-
def test_send_xml_body_from_elementtree_check_status_code_is_200_and_name_is_correct():
82-
xml = create_xml_body_using_elementtree()
83-
xml_as_string = etree.tostring(xml)
8460
response = requests.post(
85-
"https://parabank.parasoft.com/parabank/services/bank/billpay?accountId=12345&amount=500",
86-
headers={"Content-Type": "application/xml"},
87-
data=xml_as_string,
61+
'https://spacex-production.up.railway.app/',
62+
json={
63+
'query': query_rocket_parameterized,
64+
'variables': {
65+
'id': rocket_id
66+
}
67+
}
8868
)
89-
print(response.request.body)
90-
assert response.status_code == 200
91-
assert response.headers["Content-Type"] == "application/xml"
69+
response_body = response.json()
70+
assert response_body['data']['rocket']['name'] == rocket_name
71+
assert response_body['data']['rocket']['country'] == country

answers/answers_05.py

Lines changed: 0 additions & 74 deletions
This file was deleted.

answers/answers_06.py

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)