From 55cac3bd137690e6a2dc619ea545432de1c4e1fa Mon Sep 17 00:00:00 2001
From: Geert Kapteijns <ghkapteijns@gmail.com>
Date: Sat, 6 May 2017 15:15:07 +0200
Subject: [PATCH] add tests for DOICaller

---
 scipost/services.py      | 39 ++++++++++++++++++++-------------------
 scipost/test_services.py | 30 ++++++++++++++++++++++++------
 2 files changed, 44 insertions(+), 25 deletions(-)

diff --git a/scipost/services.py b/scipost/services.py
index 56c403df8..78c38e849 100644
--- a/scipost/services.py
+++ b/scipost/services.py
@@ -2,6 +2,7 @@
 import feedparser
 import requests
 import re
+import datetime
 
 from django.template import Template, Context
 from .behaviors import ArxivCallable
@@ -26,8 +27,16 @@ class DOICaller:
         journal = data['container-title'][0]
         volume = data.get('volume', '')
         pages = self._get_pages(data)
-        pub_data = self._get_pub_date(data)
+        pub_date = self._get_pub_date(data)
 
+        self.data = {
+            'pub_title': pub_title,
+            'authorlist': authorlist,
+            'journal': journal,
+            'volume': volume,
+            'pages': pages,
+            'pub_date': pub_date,
+        }
 
     def _get_pages(self, data):
         # For Physical Review
@@ -37,25 +46,17 @@ class DOICaller:
         return pages
 
     def _get_pub_date(self, data):
-        date_parts = data.get('issued', {}).get('date_parts', {})
-        date_parts = data['issued']['date-parts'][0]
-        year = date_parts[0]
-        month = date_parts[1]
-        day = date_parts[2]
-        pub_date = "{}-{}-{}".format(year, month, day)
-
-        pub_date = ''
-        try:
-            pub_date = (str(data['message']['issued']['date-parts'][0][0]) + '-' +
-                        str(data['message']['issued']['date-parts'][0][1]))
-            try:
-                pub_date += '-' + str(
-                    data['message']['issued']['date-parts'][0][2])
-            except (IndexError, KeyError):
-                pass
-        except (IndexError, KeyError):
-            pass
+        date_parts = data.get('issued', {}).get('date-parts', {})
+        if date_parts:
+            date_parts = date_parts[0]
+            year = date_parts[0]
+            month = date_parts[1]
+            day = date_parts[2]
+            pub_date = datetime.date(year, month, day).isoformat()
+        else:
+            pub_date = ''
 
+        return pub_date
 
 
 
diff --git a/scipost/test_services.py b/scipost/test_services.py
index d2aca0066..ca8de44fb 100644
--- a/scipost/test_services.py
+++ b/scipost/test_services.py
@@ -47,9 +47,27 @@ class ArxivCallerTest(TestCase):
 
 
 class DOICallerTest(TestCase):
-    def setUp(self):
-        self.doi_string = '10.1103/PhysRevB.92.214427'
-        self.caller = DOICaller(self.doi_string)
-
-    def test_collects_data(self):
-        print(self.caller.crossref_data)
+    def test_works_for_physrev_doi(self):
+        caller = DOICaller('10.1103/PhysRevB.92.214427')
+        correct_data = {
+            'pub_date': '2015-12-18',
+            'journal': 'Physical Review B',
+            'pages': '',
+            'authorlist': [
+                'R. Vlijm', 'M. Ganahl', 'D. Fioretto', 'M. Brockmann', 'M. Haque', 'H. G. Evertz', 'J.-S. Caux'],
+            'volume': '92',
+            'pub_title': 'Quasi-soliton scattering in quantum spin chains'
+        }
+        self.assertEqual(caller.data, correct_data)
+
+    def test_works_for_scipost_doi(self):
+        caller = DOICaller('10.21468/SciPostPhys.2.2.012')
+        correct_data = {
+            'pub_date': '2017-04-04',
+            'journal': 'SciPost Physics',
+            'pub_title': 'One-particle density matrix of trapped one-dimensional impenetrable  bosons from conformal invariance',
+            'pages': '',
+            'volume': '2',
+            'authorlist': ['Yannis Brun', 'Jerome Dubail']
+        }
+        self.assertEqual(caller.data, correct_data)
-- 
GitLab