Skip to content

Commit d369979

Browse files
author
Gaurav Singh
committed
Added another test to show couple of ways we can get values from XML response using Xpath
1 parent 657cf98 commit d369979

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

config.py

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

tests/covid_test.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,27 @@ def test_covid_cases_have_crossed_a_million():
1111
pretty_print(response.headers)
1212

1313
response_xml = response.text
14-
tree = etree.fromstring(bytes(response_xml, encoding='utf8'))
15-
total_cases = tree.xpath("//data/summary/total_cases")[0].text
14+
xml_tree = etree.fromstring(bytes(response_xml, encoding='utf8'))
1615

16+
# use .xpath on xml_tree object to evaluate the expression
17+
total_cases = xml_tree.xpath("//data/summary/total_cases")[0].text
1718
assert_that(int(total_cases)).is_greater_than(1000000)
19+
20+
21+
def test_overall_covid_cases_match_sum_of_total_cases_by_country():
22+
response = requests.get(f'{COVID_TRACKER_HOST}/api/v1/summary/latest')
23+
pretty_print(response.headers)
24+
25+
response_xml = response.text
26+
xml_tree = etree.fromstring(bytes(response_xml, encoding='utf8'))
27+
28+
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
31+
search_for = etree.XPath("//data//regions//total_cases")
32+
cases_by_country = 0
33+
for region in search_for(xml_tree):
34+
cases_by_country += int(region.text)
35+
36+
assert_that(overall_cases).is_greater_than(cases_by_country)
37+

0 commit comments

Comments
 (0)