Repository: scieloorg/articles_meta

Issue:
[scieloci] Erro em data de publicação de 
### Descrição do problema

http://articlemeta.scielo.org/api/v1/article/?collection=scl&code=S0001-37652008000100006&format=xmlwos

Gerou year com valor 0000.

```xml
<ref id="B25"><element-citation publication-type="conference"><conf-name>Seminário Internacional de Manejoem Floresta Tropical-SEMA/WWF</conf-name><conf-loc>Serra dos Carajás</conf-loc><source>Presentation</source><date><day>29</day><month>01</month><year>0000</year></date><person-group><name><surname>FEARNSIDE</surname><given-names>PM</given-names></name></person-group></element-citation></ref>
```

No entanto, no registro há a data correta, no campo v64. O correto é usar `raw.publication_date` no lugar de `raw.date` entrega a data de acordo com o tipo de referência bibliográfica, ou seja, se é tese, a data da tese, se é link, a data do acesso, e assim por diante. No entanto, a data que interessa na referência bibliográfica é a data de publicação do documento.

https://github.com/scieloorg/articles_meta/blob/05743b335f07dd97b828c375c5240e2833e58657/articlemeta/export_sci.py#L230



Discussion:
joffilyfe: > Sugiro criar testes.
> Considerar que v65 = "20120000", por exemplo.

Refiz o PR @robertatakenaka, resolvi alterar o código do pipeline para algo mais versátil e adicionei testes para os casos requeridos.
robertatakenaka: @joffilyfe só para garantir, crie 1 teste pelo menos para quando há dia e mês.
joffilyfe: > @joffilyfe só para garantir, crie 1 teste pelo menos para quando há dia e mês.

Eles já existem @robertatakenaka, por exemplo: https://github.com/scieloorg/articles_meta/blob/master/tests/test_export_sci.py#L296-L297

Patch:
diff --git a/articlemeta/export_rsps.py b/articlemeta/export_rsps.py
index d8cba54..17feeed 100644
--- a/articlemeta/export_rsps.py
+++ b/articlemeta/export_rsps.py
@@ -210,30 +210,28 @@ class XMLCitation(object):
         def precond(data):
             raw, xml = data
 
-            if not raw.date:
+            if not raw.publication_date:
                 raise plumber.UnmetPrecondition()
 
         @plumber.precondition(precond)
         def transform(self, data):
             raw, xml = data
 
-            pdate = ET.Element('date')
+            pdate = ET.Element("date")
 
-            if raw.date[8:10]:
-                day = ET.Element('day')
-                day.text = raw.date[8:10]
-                pdate.append(day)
-
-            if raw.date[5:7]:
-                month = ET.Element('month')
-                month.text = raw.date[5:7]
-                pdate.append(month)
+            date = {
+                "year": raw.publication_date[0:4],
+                "month": raw.publication_date[5:7],
+                "day": raw.publication_date[8:10],
+            }
 
-            year = ET.Element('year')
-            year.text = raw.date[0:4]
-            pdate.append(year)
+            for name, value in date.items():
+                if len(value) > 0 and value.isdigit() and int(value) > 0:
+                    date_element = ET.Element(name)
+                    date_element.text = value
+                    pdate.append(date_element)
 
-            xml.find('./element-citation').append(pdate)
+            xml.find("./element-citation").append(pdate)
 
             return data
 
diff --git a/articlemeta/export_sci.py b/articlemeta/export_sci.py
index 3a2f4c4..d914fe5 100644
--- a/articlemeta/export_sci.py
+++ b/articlemeta/export_sci.py
@@ -227,30 +227,28 @@ class XMLCitation(object):
         def precond(data):
             raw, xml = data
 
-            if not raw.date:
+            if not raw.publication_date:
                 raise plumber.UnmetPrecondition()
 
         @plumber.precondition(precond)
         def transform(self, data):
             raw, xml = data
 
-            pdate = ET.Element('date')
+            pdate = ET.Element("date")
 
-            if raw.date[8:10]:
-                day = ET.Element('day')
-                day.text = raw.date[8:10]
-                pdate.append(day)
+            date = {
+                "year": raw.publication_date[0:4],
+                "month": raw.publication_date[5:7],
+                "day": raw.publication_date[8:10],
+            }
 
-            if raw.date[5:7]:
-                month = ET.Element('month')
-                month.text = raw.date[5:7]
-                pdate.append(month)
+            for name, value in date.items():
+                if len(value) > 0 and value.isdigit() and int(value) > 0:
+                    date_element = ET.Element(name)
+                    date_element.text = value
+                    pdate.append(date_element)
 
-            year = ET.Element('year')
-            year.text = raw.date[0:4]
-            pdate.append(year)
-
-            xml.find('./element-citation').append(pdate)
+            xml.find("./element-citation").append(pdate)
 
             return data
 


Test patch:
diff --git a/tests/test_export_rsps.py b/tests/test_export_rsps.py
index b3dc281..8d9bdd8 100644
--- a/tests/test_export_rsps.py
+++ b/tests/test_export_rsps.py
@@ -167,6 +167,76 @@ class XMLCitationTests(unittest.TestCase):
         self.assertEqual(u'04', expected_month)
         self.assertEqual(u'30', expected_day)
 
+    def test_xml_citation_should_not_includes_month_when_number_is_zero(self):
+        fakexylosearticle = Article(
+            {"article": {}, "title": {}, "citations": [{"v65": [{"_": "20060000"}]}]}
+        ).citations[0]
+
+        pxml = ET.Element("ref")
+        pxml.append(ET.Element("element-citation"))
+
+        data = [fakexylosearticle, pxml]
+
+        raw, xml = self._xmlcitation.DatePipe().transform(data)
+
+        expected_year = xml.find("./element-citation/date/year")
+        not_expected_month = xml.find("./element-citation/date/month")
+
+        self.assertEqual(u"2006", expected_year.text)
+        self.assertIsNone(not_expected_month)
+
+    def test_xml_citation_should_not_includes_day_when_number_is_zero(self):
+        fakexylosearticle = Article(
+            {"article": {}, "title": {}, "citations": [{"v65": [{"_": "20060000"}]}]}
+        ).citations[0]
+
+        pxml = ET.Element("ref")
+        pxml.append(ET.Element("element-citation"))
+
+        data = [fakexylosearticle, pxml]
+
+        raw, xml = self._xmlcitation.DatePipe().transform(data)
+
+        expected_year = xml.find("./element-citation/date/year")
+        not_expected_day = xml.find("./element-citation/date/day")
+
+        self.assertEqual(u"2006", expected_year.text)
+        self.assertIsNone(not_expected_day)
+
+    def test_xml_citation_should_not_includes_month_when_number_is_zero(self):
+        fakexylosearticle = Article(
+            {"article": {}, "title": {}, "citations": [{"v65": [{"_": "20060000"}]}]}
+        ).citations[0]
+
+        pxml = ET.Element("ref")
+        pxml.append(ET.Element("element-citation"))
+
+        data = [fakexylosearticle, pxml]
+
+        raw, xml = self._xmlcitation.DatePipe().transform(data)
+
+        expected_year = xml.find("./element-citation/date/year")
+        not_expected_month = xml.find("./element-citation/date/month")
+
+        self.assertEqual(u"2006", expected_year.text)
+        self.assertIsNone(not_expected_month)
+
+    def test_xml_citation_should_not_includes_year_when_number_is_zero(self):
+        fakexylosearticle = Article(
+            {"article": {}, "title": {}, "citations": [{"v65": [{"_": "00000000"}]}]}
+        ).citations[0]
+
+        pxml = ET.Element("ref")
+        pxml.append(ET.Element("element-citation"))
+
+        data = [fakexylosearticle, pxml]
+
+        raw, xml = self._xmlcitation.DatePipe().transform(data)
+
+        not_expected_year = xml.find("./element-citation/date/year")
+
+        self.assertIsNone(not_expected_year)
+
     def test_xml_citation_date_without_data_pipe(self):
 
         fakexylosearticle = Article({'article': {},
diff --git a/tests/test_export_sci.py b/tests/test_export_sci.py
index 58807e3..8c5e49b 100644
--- a/tests/test_export_sci.py
+++ b/tests/test_export_sci.py
@@ -296,6 +296,76 @@ class XMLCitationTests(unittest.TestCase):
         self.assertEqual(u'04', expected_month)
         self.assertEqual(u'30', expected_day)
 
+    def test_xml_citation_should_not_includes_month_when_number_is_zero(self):
+        fakexylosearticle = Article(
+            {"article": {}, "title": {}, "citations": [{"v65": [{"_": "20060000"}]}]}
+        ).citations[0]
+
+        pxml = ET.Element("ref")
+        pxml.append(ET.Element("element-citation"))
+
+        data = [fakexylosearticle, pxml]
+
+        raw, xml = self._xmlcitation.DatePipe().transform(data)
+
+        expected_year = xml.find("./element-citation/date/year")
+        not_expected_month = xml.find("./element-citation/date/month")
+
+        self.assertEqual(u"2006", expected_year.text)
+        self.assertIsNone(not_expected_month)
+
+    def test_xml_citation_should_not_includes_day_when_number_is_zero(self):
+        fakexylosearticle = Article(
+            {"article": {}, "title": {}, "citations": [{"v65": [{"_": "20060000"}]}]}
+        ).citations[0]
+
+        pxml = ET.Element("ref")
+        pxml.append(ET.Element("element-citation"))
+
+        data = [fakexylosearticle, pxml]
+
+        raw, xml = self._xmlcitation.DatePipe().transform(data)
+
+        expected_year = xml.find("./element-citation/date/year")
+        not_expected_day = xml.find("./element-citation/date/day")
+
+        self.assertEqual(u"2006", expected_year.text)
+        self.assertIsNone(not_expected_day)
+
+    def test_xml_citation_should_not_includes_month_when_number_is_zero(self):
+        fakexylosearticle = Article(
+            {"article": {}, "title": {}, "citations": [{"v65": [{"_": "20060000"}]}]}
+        ).citations[0]
+
+        pxml = ET.Element("ref")
+        pxml.append(ET.Element("element-citation"))
+
+        data = [fakexylosearticle, pxml]
+
+        raw, xml = self._xmlcitation.DatePipe().transform(data)
+
+        expected_year = xml.find("./element-citation/date/year")
+        not_expected_month = xml.find("./element-citation/date/month")
+
+        self.assertEqual(u"2006", expected_year.text)
+        self.assertIsNone(not_expected_month)
+
+    def test_xml_citation_should_not_includes_year_when_number_is_zero(self):
+        fakexylosearticle = Article(
+            {"article": {}, "title": {}, "citations": [{"v65": [{"_": "00000000"}]}]}
+        ).citations[0]
+
+        pxml = ET.Element("ref")
+        pxml.append(ET.Element("element-citation"))
+
+        data = [fakexylosearticle, pxml]
+
+        raw, xml = self._xmlcitation.DatePipe().transform(data)
+
+        not_expected_year = xml.find("./element-citation/date/year")
+
+        self.assertIsNone(not_expected_year)
+
     def test_xml_citation_date_without_data_pipe(self):
 
         fakexylosearticle = Article({'article': {},