1+ from unittest import TestCase
2+ from unittest .mock import MagicMock , PropertyMock , patch
3+ from lxml import etree
4+ from packtools .sps .pid_provider .xml_sps_lib import XMLWithPre
5+
6+
7+ class XMLWithPreTestMixin :
8+ """Mixin com helper para criar XML de artigo SciELO."""
9+
10+ def _make_xml (
11+ self ,
12+ issn_epub = None ,
13+ issn_ppub = None ,
14+ acron = "abc" ,
15+ vol = None ,
16+ num = None ,
17+ suppl = None ,
18+ elocation = None ,
19+ fpage = None ,
20+ fpage_seq = None ,
21+ lpage = None ,
22+ doi = None ,
23+ order = None ,
24+ v2 = None ,
25+ ):
26+ issn_parts = []
27+ if issn_epub :
28+ issn_parts .append (f'<issn pub-type="epub">{ issn_epub } </issn>' )
29+ if issn_ppub :
30+ issn_parts .append (f'<issn pub-type="ppub">{ issn_ppub } </issn>' )
31+ issns = "" .join (issn_parts ) or '<issn pub-type="epub">0000-0000</issn>'
32+
33+ vol_tag = f"<volume>{ vol } </volume>" if vol else ""
34+ num_tag = f"<issue>{ num } </issue>" if num else ""
35+ suppl_tag = f"<supplement>{ suppl } </supplement>" if suppl else ""
36+ eloc_tag = f"<elocation-id>{ elocation } </elocation-id>" if elocation else ""
37+
38+ fpage_attr = f' seq="{ fpage_seq } "' if fpage_seq else ""
39+ fpage_tag = f"<fpage{ fpage_attr } >{ fpage } </fpage>" if fpage else ""
40+ lpage_tag = f"<lpage>{ lpage } </lpage>" if lpage else ""
41+
42+ doi_tag = f'<article-id pub-id-type="doi">{ doi } </article-id>' if doi else ""
43+ order_tag = f'<article-id pub-id-type="other">{ order } </article-id>' if order else ""
44+ v2_tag = f'<article-id specific-use="scielo-v2" pub-id-type="publisher-id">{ v2 } </article-id>' if v2 else ""
45+
46+ xml_content = f"""<?xml version="1.0" encoding="UTF-8"?>
47+ <!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Publishing DTD v1.1 20151215//EN" "JATS-journalpublishing1.dtd">
48+ <article xmlns:xlink="http://www.w3.org/1999/xlink" article-type="research-article" xml:lang="en">
49+ <front>
50+ <journal-meta>
51+ <journal-id journal-id-type="publisher-id">{ acron } </journal-id>
52+ { issns }
53+ </journal-meta>
54+ <article-meta>
55+ { doi_tag }
56+ { v2_tag }
57+ { order_tag }
58+ { vol_tag }
59+ { num_tag }
60+ { suppl_tag }
61+ { eloc_tag }
62+ { fpage_tag }
63+ { lpage_tag }
64+ </article-meta>
65+ </front>
66+ </article>"""
67+ for item in XMLWithPre .create (xml_content = xml_content ):
68+ return item
69+
70+
71+ class TestSPSPkgNameSuppl (XMLWithPreTestMixin , TestCase ):
72+ """Testes para sps_pkg_name_suppl e deprecated_sps_pkg_name_suppl"""
73+
74+ def test_sps_pkg_name_suppl_none (self ):
75+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" )
76+ self .assertIsNone (xml_with_pre .suppl )
77+ self .assertIsNone (xml_with_pre .sps_pkg_name_suppl )
78+
79+ def test_sps_pkg_name_suppl_zero (self ):
80+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , suppl = "0" )
81+ self .assertEqual (xml_with_pre .suppl , "0" )
82+ self .assertEqual (xml_with_pre .sps_pkg_name_suppl , "suppl" )
83+
84+ def test_sps_pkg_name_suppl_numeric (self ):
85+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , suppl = "1" )
86+ self .assertEqual (xml_with_pre .suppl , "1" )
87+ self .assertEqual (xml_with_pre .sps_pkg_name_suppl , "s1" )
88+
89+ def test_sps_pkg_name_suppl_numeric_two_digits (self ):
90+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , suppl = "12" )
91+ self .assertEqual (xml_with_pre .suppl , "12" )
92+ self .assertEqual (xml_with_pre .sps_pkg_name_suppl , "s12" )
93+
94+ def test_sps_pkg_name_suppl_text (self ):
95+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , suppl = "A" )
96+ self .assertEqual (xml_with_pre .suppl , "A" )
97+ self .assertEqual (xml_with_pre .sps_pkg_name_suppl , "sA" )
98+
99+ def test_deprecated_sps_pkg_name_suppl_none (self ):
100+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" )
101+ self .assertIsNone (xml_with_pre .deprecated_sps_pkg_name_suppl )
102+
103+ def test_deprecated_sps_pkg_name_suppl_zero (self ):
104+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , suppl = "0" )
105+ self .assertEqual (xml_with_pre .deprecated_sps_pkg_name_suppl , "suppl" )
106+
107+ def test_deprecated_sps_pkg_name_suppl_numeric (self ):
108+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , suppl = "1" )
109+ self .assertEqual (xml_with_pre .deprecated_sps_pkg_name_suppl , "1" )
110+
111+ def test_deprecated_sps_pkg_name_suppl_text (self ):
112+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , suppl = "A" )
113+ self .assertEqual (xml_with_pre .deprecated_sps_pkg_name_suppl , "A" )
114+
115+
116+ class TestSPSPkgNameFpage (XMLWithPreTestMixin , TestCase ):
117+ """Testes para sps_pkg_name_fpage e deprecated_sps_pkg_name_fpage"""
118+
119+ def test_sps_pkg_name_fpage_none (self ):
120+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , elocation = "e123" )
121+ self .assertIsNone (xml_with_pre .fpage )
122+ self .assertIsNone (xml_with_pre .sps_pkg_name_fpage )
123+
124+ def test_sps_pkg_name_fpage_zero (self ):
125+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , fpage = "0" , lpage = "0" )
126+ self .assertIsNone (xml_with_pre .fpage )
127+ self .assertIsNone (xml_with_pre .sps_pkg_name_fpage )
128+
129+ def test_sps_pkg_name_fpage_simple (self ):
130+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , fpage = "123" , lpage = "130" )
131+ self .assertEqual (xml_with_pre .fpage , "123" )
132+ self .assertEqual (xml_with_pre .sps_pkg_name_fpage , "123" )
133+
134+ def test_sps_pkg_name_fpage_with_seq (self ):
135+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , fpage = "123" , fpage_seq = "a" , lpage = "130" )
136+ self .assertEqual (xml_with_pre .fpage , "123" )
137+ self .assertEqual (xml_with_pre .fpage_seq , "a" )
138+ self .assertEqual (xml_with_pre .sps_pkg_name_fpage , "123_a" )
139+
140+ def test_sps_pkg_name_fpage_same_fpage_lpage_with_v2 (self ):
141+ xml_with_pre = self ._make_xml (
142+ vol = "10" , num = "2" , fpage = "123" , lpage = "123" ,
143+ v2 = "S0101-01011999000100123"
144+ )
145+ self .assertEqual (xml_with_pre .fpage , "123" )
146+ self .assertEqual (xml_with_pre .lpage , "123" )
147+ self .assertEqual (xml_with_pre .sps_pkg_name_fpage , "123_00123" )
148+
149+ def test_sps_pkg_name_fpage_same_fpage_lpage_without_v2 (self ):
150+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , fpage = "123" , lpage = "123" )
151+ self .assertEqual (xml_with_pre .fpage , "123" )
152+ self .assertEqual (xml_with_pre .lpage , "123" )
153+ self .assertEqual (xml_with_pre .sps_pkg_name_fpage , "123" )
154+
155+ def test_sps_pkg_name_fpage_alphanumeric (self ):
156+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , fpage = "e123" , lpage = "e130" )
157+ self .assertEqual (xml_with_pre .fpage , "e123" )
158+ self .assertEqual (xml_with_pre .sps_pkg_name_fpage , "e123" )
159+
160+ def test_deprecated_sps_pkg_name_fpage_none (self ):
161+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , elocation = "e123" )
162+ self .assertIsNone (xml_with_pre .deprecated_sps_pkg_name_fpage )
163+
164+ def test_deprecated_sps_pkg_name_fpage_zero (self ):
165+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , fpage = "0" , lpage = "0" )
166+ self .assertIsNone (xml_with_pre .deprecated_sps_pkg_name_fpage )
167+
168+ def test_deprecated_sps_pkg_name_fpage_simple (self ):
169+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , fpage = "123" , lpage = "130" )
170+ self .assertEqual (xml_with_pre .deprecated_sps_pkg_name_fpage , "123" )
171+
172+ def test_deprecated_sps_pkg_name_fpage_with_seq (self ):
173+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , fpage = "123" , fpage_seq = "a" , lpage = "130" )
174+ self .assertEqual (xml_with_pre .deprecated_sps_pkg_name_fpage , "123a" )
175+
176+
177+ class TestSPSPkgName (XMLWithPreTestMixin , TestCase ):
178+ """Testes para sps_pkg_name e deprecated_sps_pkg_name"""
179+
180+ def test_sps_pkg_name_complete (self ):
181+ xml_with_pre = self ._make_xml (
182+ issn_epub = "1234-5678" ,
183+ issn_ppub = "8765-4321" ,
184+ acron = "abc" ,
185+ vol = "10" ,
186+ num = "2" ,
187+ elocation = "e12345" ,
188+ )
189+ self .assertEqual (xml_with_pre .sps_pkg_name , "1234-5678-abc-10-02-e12345" )
190+
191+ def test_sps_pkg_name_with_suppl (self ):
192+ xml_with_pre = self ._make_xml (
193+ issn_epub = "1234-5678" ,
194+ acron = "abc" ,
195+ vol = "10" ,
196+ num = "2" ,
197+ suppl = "1" ,
198+ fpage = "100" ,
199+ lpage = "110" ,
200+ )
201+ self .assertEqual (xml_with_pre .sps_pkg_name , "1234-5678-abc-10-02-s1-100" )
202+
203+ def test_sps_pkg_name_ppub_fallback (self ):
204+ xml_with_pre = self ._make_xml (
205+ issn_ppub = "8765-4321" ,
206+ acron = "xyz" ,
207+ vol = "5" ,
208+ elocation = "e001" ,
209+ )
210+ self .assertEqual (xml_with_pre .sps_pkg_name , "8765-4321-xyz-5-e001" )
211+
212+ def test_sps_pkg_name_no_volume_no_number (self ):
213+ xml_with_pre = self ._make_xml (
214+ issn_epub = "1111-2222" ,
215+ acron = "rev" ,
216+ elocation = "e999" ,
217+ )
218+ self .assertEqual (xml_with_pre .sps_pkg_name , "1111-2222-rev-e999" )
219+
220+ def test_deprecated_sps_pkg_name_with_suppl (self ):
221+ xml_with_pre = self ._make_xml (
222+ issn_epub = "1234-5678" ,
223+ acron = "abc" ,
224+ vol = "10" ,
225+ num = "2" ,
226+ suppl = "1" ,
227+ fpage = "100" ,
228+ lpage = "110" ,
229+ )
230+ self .assertEqual (xml_with_pre .deprecated_sps_pkg_name , "1234-5678-abc-10-02-1-100" )
231+
232+ def test_deprecated_sps_pkg_name_suppl_zero (self ):
233+ xml_with_pre = self ._make_xml (
234+ issn_epub = "1234-5678" ,
235+ acron = "abc" ,
236+ vol = "10" ,
237+ num = "2" ,
238+ suppl = "0" ,
239+ fpage = "100" ,
240+ lpage = "110" ,
241+ )
242+ self .assertEqual (xml_with_pre .deprecated_sps_pkg_name , "1234-5678-abc-10-02-suppl-100" )
243+
244+
245+ class TestSPSPkgNameSuffix (XMLWithPreTestMixin , TestCase ):
246+ """Testes para sps_pkg_name_suffix e alternative_sps_pkg_name_suffix"""
247+
248+ def test_sps_pkg_name_suffix_elocation_id (self ):
249+ xml_with_pre = self ._make_xml (
250+ vol = "10" , num = "2" ,
251+ elocation = "e12345" ,
252+ fpage = "100" , lpage = "110" ,
253+ doi = "10.1590/1234" ,
254+ )
255+ self .assertEqual (xml_with_pre .sps_pkg_name_suffix , "e12345" )
256+
257+ def test_sps_pkg_name_suffix_fpage (self ):
258+ xml_with_pre = self ._make_xml (
259+ vol = "10" , num = "2" ,
260+ fpage = "100" , lpage = "110" ,
261+ doi = "10.1590/1234" ,
262+ )
263+ self .assertEqual (xml_with_pre .sps_pkg_name_suffix , "100" )
264+
265+ def test_sps_pkg_name_suffix_doi (self ):
266+ xml_with_pre = self ._make_xml (
267+ vol = "10" , num = "2" ,
268+ doi = "10.1590/0001-3714.2020.v1.n2.1234" ,
269+ )
270+ self .assertEqual (xml_with_pre .sps_pkg_name_suffix , "0001-3714-2020-v1-n2-1234" )
271+
272+ def test_sps_pkg_name_suffix_doi_simple (self ):
273+ xml_with_pre = self ._make_xml (
274+ vol = "10" , num = "2" ,
275+ doi = "10.1590/abc123" ,
276+ )
277+ self .assertEqual (xml_with_pre .sps_pkg_name_suffix , "abc123" )
278+
279+ def test_sps_pkg_name_suffix_none (self ):
280+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" )
281+ self .assertIsNone (xml_with_pre .sps_pkg_name_suffix )
282+
283+ def test_alternative_sps_pkg_name_suffix_order (self ):
284+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" , order = "00001" )
285+ self .assertEqual (xml_with_pre .alternative_sps_pkg_name_suffix , "00001" )
286+
287+ def test_alternative_sps_pkg_name_suffix_filename (self ):
288+ xml_with_pre = self ._make_xml (vol = "10" , num = "2" )
289+ xml_with_pre .filename = "article.xml"
290+ self .assertEqual (xml_with_pre .alternative_sps_pkg_name_suffix , "article.xml" )
291+
292+
293+ if __name__ == "__main__" :
294+ import unittest
295+ unittest .main ()
0 commit comments