Skip to content

Commit

Permalink
Add first test
Browse files Browse the repository at this point in the history
  • Loading branch information
physikerwelt committed Jan 7, 2024
1 parent f147e86 commit 9f9ae3e
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 66 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test = [
"requests_mock",
"python-dateutil",
"pytest-coverage",
"xmldiff"
]

[tool.setuptools]
Expand Down
Binary file removed src/zbmath_rest2oai/.getWithSwagger.py.swp
Binary file not shown.
102 changes: 36 additions & 66 deletions src/zbmath_rest2oai/getWithSwagger.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
import os.path

import swagger_client
from zbmath_rest2oai.xml_writer import create_document

import json

with open('./output mapping - Copy.json') as f:
with open(os.path.join(os.path.dirname(__file__), './output mapping - Copy.json')) as f:
d = json.load(f)
api_instance = swagger_client.DocumentApi(swagger_client.ApiClient())
res = api_instance.get_document_by_zbmath_id_document_id_get(id="6383667")
doc = res.result
root_doc = create_document(doc)

ron = root_doc.createElement("oai_zb_preview:zbmath")
ron.setAttributeNS(
"xmls",
"xmlns:oai_zb_preview",
"https://zbmath.org/OAI/2.0/oai_zb_preview/",
)
ron.setAttributeNS(
"xmls",
"xmlns:zbmath",
"https://zbmath.org/zbmath/elements/1.0/",
)
ron.setAttributeNS(
"xmls",
"xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance",
)


def append_text_child(xmld, parent, name, value):
Expand All @@ -46,7 +27,7 @@ def append_text_child(xmld, parent, name, value):
return parent


def func_get_doc_to_xml(obj, xml):
def func_get_doc_to_xml(obj, xml, root_doc):
swagger_client_dicttype_list = [
swagger_client.models.all_ofzbmath_api_data_models_display_documents_result_id_result.AllOfzbmathApiDataModelsDisplayDocumentsResultIDResult,
swagger_client.models.all_of_document_contributors.AllOfDocumentContributors,
Expand Down Expand Up @@ -116,7 +97,7 @@ def func_get_doc_to_xml(obj, xml):
print(0)

elif type(obj[i]) in all_iter_list:
func_get_doc_to_xml(obj[i], xml)
func_get_doc_to_xml(obj[i], xml, root_doc)

if type(obj) is dict:
new_obj = {}
Expand Down Expand Up @@ -240,50 +221,39 @@ def func_get_doc_to_xml(obj, xml):
else:
xml = append_text_child(root_doc, xml, key, "")

func_get_doc_to_xml(new_obj[key], xml)
func_get_doc_to_xml(new_obj[key], xml, root_doc)
return xml


final_xml = func_get_doc_to_xml(res.result, ron)

l = final_xml.childNodes

# print([node.nodeName for node in l])
# print(final_xml.getElementsByTagName('zbmath:references'))
####

## STRATEGY TO SOLVE the situation with inner indentation. IF elem in Happening node, append child and recurs func ELSE, happend node normally
##
# list_nodes_remove = []
# for i in range(len(l)):
# if l[i].localName in ['_contributors','_authors', '_aliases', '_checked', '_author_references', '_reviewer', '_editors', '_author_codes']:
# list_nodes_remove.append(l[i])

# for node in list_nodes_remove:
# final_xml.removeChild(node)
def get_final_xml(de: str):
api_instance = swagger_client.DocumentApi(swagger_client.ApiClient())
res = api_instance.get_document_by_zbmath_id_document_id_get(id=de)
doc = res.result
root_doc = create_document(doc)

ron = root_doc.createElement("oai_zb_preview:zbmath")
ron.setAttributeNS(
"xmls",
"xmlns:oai_zb_preview",
"https://zbmath.org/OAI/2.0/oai_zb_preview/",
)
ron.setAttributeNS(
"xmls",
"xmlns:zbmath",
"https://zbmath.org/zbmath/elements/1.0/",
)
ron.setAttributeNS(
"xmls",
"xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance",
)
return func_get_doc_to_xml(
res.result,
ron,
root_doc
)


final_xml = get_final_xml("6383667")

print(final_xml.parentNode.parentNode.toprettyxml())

### PART OF THE CODE TO SORT THE FINAL XML
ron1 = root_doc.createElement("oai_zb_preview:zbmath")
ron1.setAttributeNS(
"xmls",
"xmlns:oai_zb_preview",
"https://zbmath.org/OAI/2.0/oai_zb_preview/",
)
ron1.setAttributeNS(
"xmls",
"xmlns:zbmath",
"https://zbmath.org/zbmath/elements/1.0/",
)
ron1.setAttributeNS(
"xmls",
"xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance",
)

# sorted_dict = dict(sorted(dict(zip([i.localName for i in l],l)).items()))
# for key in sorted_dict.keys():
# ron1.appendChild(sorted_dict[key])

# print(ron1.toprettyxml())
172 changes: 172 additions & 0 deletions tests/data/reference.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<oai_zb_preview:zbmath xmlns:oai_zb_preview="https://zbmath.org/OAI/2.0/oai_zb_preview/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:zbmath="https://zbmath.org/zbmath/elements/1.0/">
<zbmath:author>Maynard, James</zbmath:author>
<zbmath:author_ids>
<zbmath:author_id>maynard.james</zbmath:author_id>
</zbmath:author_ids>
<zbmath:classifications>
<zbmath:classification>11N05</zbmath:classification>
<zbmath:classification>11N36</zbmath:classification>
</zbmath:classifications>
<zbmath:document_id>6383667</zbmath:document_id>
<zbmath:document_title>Small gaps between primes</zbmath:document_title>
<zbmath:document_type>j</zbmath:document_type>
<zbmath:doi>10.4007/annals.2015.181.1.7</zbmath:doi>
<zbmath:keywords>
<zbmath:keyword>prime number</zbmath:keyword>
<zbmath:keyword>small gap</zbmath:keyword>
<zbmath:keyword>sieve method</zbmath:keyword>
<zbmath:keyword>\(k\)-tuples conjecture</zbmath:keyword>
<zbmath:keyword>admissible set</zbmath:keyword>
<zbmath:keyword>Selberg sieve</zbmath:keyword>
<zbmath:keyword>symmetric polynomial</zbmath:keyword>
<zbmath:keyword>symmetric matrix</zbmath:keyword>
</zbmath:keywords>
<zbmath:language>English</zbmath:language>
<zbmath:pagination>383-413</zbmath:pagination>
<zbmath:publication_year>2015</zbmath:publication_year>
<zbmath:source>Ann. Math. (2) 181, No. 1, 383-413 (2015).</zbmath:source>
<zbmath:spelling>Maynard, James</zbmath:spelling>
<zbmath:time>2015-01-06T13:15:02Z</zbmath:time>
<zbmath:zbl_id>1306.11073</zbmath:zbl_id>
<zbmath:review>
<zbmath:review_language>English</zbmath:review_language>
<zbmath:review_sign>Jonas Šiaulys (Vilnius)</zbmath:review_sign>
<zbmath:review_text>The prime \(k\)-tuples and small gaps between prime numbers are considered. Using a
refinement of the Goldston-Pintz-Yildirim sieve method [\textit{D. A. Goldston} et al., Ann. Math. (2) 170,
No. 2, 819--862 (2009; Zbl 1207.11096)] the author proves, for instance, the following estimates
\[

\liminf_{n\to\infty}\,(p_{n+m}-p_n)\ll m^3\text{{e}}^{4m}, \quad \liminf_{n\to\infty}\,(p_{n+1}-p_n)\leq 600

\]
with an absolute constant in sign \(\ll\). Here \(m\) is a natural number, and \(p_{\,l}\) denote the
\(l\)-th prime number.
</zbmath:review_text>
<zbmath:review_type>review</zbmath:review_type>
<zbmath:reviewer>11807</zbmath:reviewer>
<zbmath:reviewer_id>siaulys.jonas</zbmath:reviewer_id>
</zbmath:review>
<zbmath:serial>
<zbmath:serial_publisher>Princeton University, Mathematics Department, Princeton, NJ</zbmath:serial_publisher>
<zbmath:serial_title>Annals of Mathematics. Second Series</zbmath:serial_title>
</zbmath:serial>
<zbmath:references>
<zbmath:reference>
<zbmath:text>P. D. T. A. Elliott and H. Halberstam, ''A conjecture in prime number theory,'' in Symposia
Mathematica, Vol. IV, London: Academic Press, 1970, pp. 59-72.
</zbmath:text>
<zbmath:ref_id>3377327</zbmath:ref_id>
<zbmath:ref_classifications>
<zbmath:ref_classification>11N35</zbmath:ref_classification>
<zbmath:ref_classification>11N13</zbmath:ref_classification>
</zbmath:ref_classifications>
</zbmath:reference>
<zbmath:reference>
<zbmath:text>J. Friedlander and A. Granville, ''Limitations to the equi-distribution of primes. I,'' Ann. of
Math., vol. 129, iss. 2, pp. 363-382, 1989.
</zbmath:text>
<zbmath:ref_id>4097497</zbmath:ref_id>
<zbmath:ref_classifications>
<zbmath:ref_classification>11N05</zbmath:ref_classification>
<zbmath:ref_classification>11N13</zbmath:ref_classification>
<zbmath:ref_classification>11N35</zbmath:ref_classification>
</zbmath:ref_classifications>
</zbmath:reference>
<zbmath:reference>
<zbmath:text>D. A. Goldston, S. W. Graham, J. Pintz, and C. Y. Yildirim, ''Small gaps between products of
two primes,'' Proc. Lond. Math. Soc., vol. 98, iss. 3, pp. 741-774, 2009.
</zbmath:text>
<zbmath:ref_id>5551831</zbmath:ref_id>
<zbmath:ref_classifications>
<zbmath:ref_classification>11N25</zbmath:ref_classification>
<zbmath:ref_classification>11N36</zbmath:ref_classification>
</zbmath:ref_classifications>
</zbmath:reference>
<zbmath:reference>
<zbmath:text>D. A. Goldston, J. Pintz, and C. Y. Yildirim, ''Primes in tuples. III. On the difference
\(p_{n+\nu}-p_n\),'' Funct. Approx. Comment. Math., vol. 35, pp. 79-89, 2006.
</zbmath:text>
<zbmath:ref_id>5135166</zbmath:ref_id>
<zbmath:ref_classifications>
<zbmath:ref_classification>11N05</zbmath:ref_classification>
<zbmath:ref_classification>11N13</zbmath:ref_classification>
</zbmath:ref_classifications>
</zbmath:reference>
<zbmath:reference>
<zbmath:text>D. A. Goldston, J. Pintz, and C. Y. Yildirim, ''Primes in tuples. I,'' Ann. of Math., vol. 170,
iss. 2, pp. 819-862, 2009.
</zbmath:text>
<zbmath:ref_id>5610431</zbmath:ref_id>
<zbmath:ref_classifications>
<zbmath:ref_classification>11N05</zbmath:ref_classification>
<zbmath:ref_classification>11N36</zbmath:ref_classification>
<zbmath:ref_classification>11N13</zbmath:ref_classification>
</zbmath:ref_classifications>
</zbmath:reference>
<zbmath:reference>
<zbmath:text>D. A. Goldston and C. Y. Yildirim, ''Higher correlations of divisor sums related to primes.
III. Small gaps between primes,'' Proc. Lond. Math. Soc., vol. 95, iss. 3, pp. 653-686, 2007.
</zbmath:text>
<zbmath:ref_id>5170700</zbmath:ref_id>
<zbmath:ref_classifications>
<zbmath:ref_classification>11N05</zbmath:ref_classification>
<zbmath:ref_classification>11N37</zbmath:ref_classification>
</zbmath:ref_classifications>
</zbmath:reference>
<zbmath:reference>
<zbmath:text>D. H. J. Polymath, New equidistribution estimates of Zhang type, and bounded gaps between
primes.
</zbmath:text>
<zbmath:ref_id>6587992</zbmath:ref_id>
<zbmath:ref_classifications>
<zbmath:ref_classification>11N35</zbmath:ref_classification>
<zbmath:ref_classification>11N05</zbmath:ref_classification>
</zbmath:ref_classifications>
</zbmath:reference>
<zbmath:reference>
<zbmath:text>A. Selberg, Collected Papers. Vol. II, New York: Springer-Verlag, 1991.</zbmath:text>
<zbmath:ref_id>195021</zbmath:ref_id>
<zbmath:ref_classifications>
<zbmath:ref_classification>11-03</zbmath:ref_classification>
<zbmath:ref_classification>01A75</zbmath:ref_classification>
<zbmath:ref_classification>32-03</zbmath:ref_classification>
<zbmath:ref_classification>11M06</zbmath:ref_classification>
<zbmath:ref_classification>11M41</zbmath:ref_classification>
<zbmath:ref_classification>11N35</zbmath:ref_classification>
<zbmath:ref_classification>11N36</zbmath:ref_classification>
<zbmath:ref_classification>11F72</zbmath:ref_classification>
<zbmath:ref_classification>32N05</zbmath:ref_classification>
<zbmath:ref_classification>32N15</zbmath:ref_classification>
</zbmath:ref_classifications>
</zbmath:reference>
<zbmath:reference>
<zbmath:text>Y. Zhang, ''Bounded gaps between primes,'' Ann. of Math., vol. 179, iss. 3, pp. 1121-1174,
2014.
</zbmath:text>
<zbmath:ref_id>6302171</zbmath:ref_id>
<zbmath:ref_classifications>
<zbmath:ref_classification>11N05</zbmath:ref_classification>
<zbmath:ref_classification>11N13</zbmath:ref_classification>
<zbmath:ref_classification>11N35</zbmath:ref_classification>
<zbmath:ref_classification>11N36</zbmath:ref_classification>
<zbmath:ref_classification>11L07</zbmath:ref_classification>
</zbmath:ref_classifications>
</zbmath:reference>
</zbmath:references>
<zbmath:links>
<zbmath:link>https://arxiv.org/abs/1311.4600</zbmath:link>
</zbmath:links>
<zbmath:rights>Content generated by zbMATH Open, such as reviews,
classifications, software, or author disambiguation data,
are distributed under CC-BY-SA 4.0. This defines the license for the
whole dataset, which also contains non-copyrighted bibliographic
metadata and reference data derived from I4OC (CC0). Note that the API
only provides a subset of the data in the zbMATH Open Web interface. In
several cases, third-party information, such as abstracts, cannot be
made available under a suitable license through the API. In those cases,
we replaced the data with the string 'zbMATH Open Web Interface contents
unavailable due to conflicting licenses.'
</zbmath:rights>
</oai_zb_preview:zbmath>
21 changes: 21 additions & 0 deletions tests/test_oai_preview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import unittest
from xml.dom.minidom import parse
from xmldiff import main
from zbmath_rest2oai import getWithSwagger


class MyTestCase(unittest.TestCase):
def test_something(self):
real = getWithSwagger.get_final_xml("6383667")
real_string = real.parentNode.parentNode.toprettyxml()
ref_location = os.path.join(os.path.dirname(__file__), './data/reference.xml')
with open(ref_location) as f:
dom = parse(f)
expected_string = dom.toprettyxml()
diff = main.diff_texts(expected_string, real_string)
self.assertEqual(len(diff), 0) # add assertion here


if __name__ == '__main__':
unittest.main()

0 comments on commit 9f9ae3e

Please sign in to comment.