|
1 | | -from lxml import etree |
2 | | - |
3 | 1 | import requests |
| 2 | +import pytest |
4 | 3 |
|
5 | 4 |
|
6 | 5 | # 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(): |
36 | 11 |
|
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(): |
44 | 12 | 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 } }'} |
48 | 15 | ) |
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 | +] |
51 | 34 |
|
52 | 35 |
|
53 | 36 | # 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 | + |
76 | 56 |
|
| 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): |
77 | 59 |
|
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) |
84 | 60 | 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 | + } |
88 | 68 | ) |
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 |
0 commit comments