|
1 | 1 | import requests
|
2 | 2 | from assertpy import assert_that
|
3 | 3 | from lxml import etree
|
4 |
| - |
5 | 4 | from config import COVID_TRACKER_HOST
|
6 | 5 | from utils.print_helpers import pretty_print
|
7 | 6 |
|
8 | 7 |
|
9 | 8 | 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 | + """ |
10 | 17 | response = requests.get(f'{COVID_TRACKER_HOST}/api/v1/summary/latest')
|
11 | 18 | pretty_print(response.headers)
|
12 |
| - |
| 19 | + # getting XML text |
13 | 20 | 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 | + """ |
14 | 28 | 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 | + """ |
17 | 33 | total_cases = xml_tree.xpath("//data/summary/total_cases")[0].text
|
| 34 | + pretty_print(total_cases) |
18 | 35 | assert_that(int(total_cases)).is_greater_than(1000000)
|
19 | 36 |
|
20 | 37 |
|
21 | 38 | def test_overall_covid_cases_match_sum_of_total_cases_by_country():
|
22 | 39 | response = requests.get(f'{COVID_TRACKER_HOST}/api/v1/summary/latest')
|
23 | 40 | pretty_print(response.headers)
|
24 |
| - |
25 | 41 | response_xml = response.text
|
26 | 42 | xml_tree = etree.fromstring(bytes(response_xml, encoding='utf8'))
|
27 |
| - |
28 | 43 | 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 |
31 | 45 | search_for = etree.XPath("//data//regions//total_cases")
|
32 | 46 | cases_by_country = 0
|
33 | 47 | for region in search_for(xml_tree):
|
34 | 48 | cases_by_country += int(region.text)
|
35 |
| - |
36 | 49 | assert_that(overall_cases).is_greater_than(cases_by_country)
|
37 |
| - |
|
0 commit comments