Skip to content

Commit aa04197

Browse files
file updated with lecture theory statements for better understanding.
Added screenshot of API path.
1 parent 6eb498e commit aa04197

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
BASE_URI = 'http://0.0.0.0:5000/api/people'
2-
COVID_TRACKER_HOST = 'http://127.0.0.1:3000'
1+
BASE_URI = 'http://127.0.0.1:5000/api/people'
2+
COVID_TRACKER_HOST = 'http://127.0.0.1:3000'

tests/covid_test.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,49 @@
11
import requests
22
from assertpy import assert_that
33
from lxml import etree
4-
54
from config import COVID_TRACKER_HOST
65
from utils.print_helpers import pretty_print
76

87

98
def test_covid_cases_have_crossed_a_million():
9+
"""
10+
Learn more about 'lxml' at : https://lxml.de/xpathxslt.html
11+
like : how to deal with xml response.
12+
If you're dealing with an XML request, a common approach to deal with that, we can store our xml into a file,
13+
and read it from there, and then use the same method to convert it into an elementary object and then modify as per
14+
our need (maybe again convert into a string). So, this library offers lots of flexibility for your use cases.
15+
Making a request to dummy service, running on my local host : http://127.0.0.1:3000/
16+
"""
1017
response = requests.get(f'{COVID_TRACKER_HOST}/api/v1/summary/latest')
1118
pretty_print(response.headers)
12-
19+
# getting XML text
1320
response_xml = response.text
21+
"""
22+
In immediate below code line we're deserializing it into an XML Object, which we can work with.
23+
this ensures that you're able to convert your string into a actual tree that you can work with.
24+
But, the main use case with XML is typically you want to verify a whether a certain node has a certain value.
25+
So, for that we're using : xml_tree.xpath('pass your xpath for the node that you're actually looking for') below
26+
in next code line.
27+
"""
1428
xml_tree = etree.fromstring(bytes(response_xml, encoding='utf8'))
15-
16-
# use .xpath on xml_tree object to evaluate the expression
29+
""" use .xpath on xml_tree object to evaluate the expression
30+
this below Xpath is coming from the 'Covid Tracker' API in 'people-api' project. For more detail check Postman.
31+
Screenshot attached for reference.
32+
"""
1733
total_cases = xml_tree.xpath("//data/summary/total_cases")[0].text
34+
pretty_print(total_cases)
1835
assert_that(int(total_cases)).is_greater_than(1000000)
1936

2037

2138
def test_overall_covid_cases_match_sum_of_total_cases_by_country():
2239
response = requests.get(f'{COVID_TRACKER_HOST}/api/v1/summary/latest')
2340
pretty_print(response.headers)
24-
2541
response_xml = response.text
2642
xml_tree = etree.fromstring(bytes(response_xml, encoding='utf8'))
27-
2843
overall_cases = int(xml_tree.xpath("//data/summary/total_cases")[0].text)
29-
# Another way to specify XPath first and then use to evaluate
30-
# on an XML tree
44+
# Another way to specify XPath first and then use to evaluate on an XML tree
3145
search_for = etree.XPath("//data//regions//total_cases")
3246
cases_by_country = 0
3347
for region in search_for(xml_tree):
3448
cases_by_country += int(region.text)
35-
3649
assert_that(overall_cases).is_greater_than(cases_by_country)
37-
Loading

0 commit comments

Comments
 (0)