Repository: MITLibraries/oastats-backend

Issue:
Add combined author/mit id field to solr during summary
Changes in the summary script depend on a string field with `author:mitid`. This is in addition to the two separate `author` and `mitid` fields.

Discussion:

Patch:
diff --git a/pipeline/summary.py b/pipeline/summary.py
index 982c7f5..c3a4360 100644
--- a/pipeline/summary.py
+++ b/pipeline/summary.py
@@ -29,6 +29,7 @@ def index(requests, solr_url):
                                       request.get('authors', []))),
                 'author_name': list(map(itemgetter('name'),
                                         request.get('authors', []))),
+                'author': list(map(join_author, request.get('authors', [])))
             }
             solr.write(doc)
 
@@ -209,4 +210,8 @@ def split_author(author):
     except ValueError:
         return
     if mitid and name:
-        return {'mitid': int(mitid), 'name': name}
+        return {'mitid': mitid, 'name': name}
+
+
+def join_author(author):
+    return "%s:%s" % (author.get('mitid', ''), author.get('name', ''))


Test patch:
diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py
index 34933b2..757d504 100644
--- a/tests/integration/test_cli.py
+++ b/tests/integration/test_cli.py
@@ -62,7 +62,13 @@ def test_index_adds_mongo_records_to_solr(runner, solr_port, mongo_port):
     r = solr.search('*:*')
     assert len(r) == 2
     doc = next(iter(r))
-    assert doc['title'] == 'The Foobar'
+    assert doc == {'title': 'The Foobar', 'handle': 'foobar', 'country': 'USA',
+                   'time': '2015-01-01T00:00:00Z',
+                   'dlc_display': ['Stuff', 'Things'],
+                   'dlc_canonical': ['Dept of Stuff', 'Dept of Things'],
+                   'author_id': ['1234', '5678'],
+                   'author_name': ['Bar, Foo', 'Baz, Foo'],
+                   'author': ['1234:Bar, Foo', '5678:Baz, Foo']}
 
 
 @pytest.mark.usefixtures('load_mongo_records', 'load_solr_records')
diff --git a/tests/unit/test_summary.py b/tests/unit/test_summary.py
index b86c192..034d163 100644
--- a/tests/unit/test_summary.py
+++ b/tests/unit/test_summary.py
@@ -75,7 +75,8 @@ def test_index_maps_mongo_request_to_solr(solr, mongo_req):
         'dlc_display': ['Stuff n Such', 'Other Things'],
         'dlc_canonical': ['Dept of Stuff', 'Dept of Things'],
         'author_id': ['1234', '7890'],
-        'author_name': ['Captain Snuggles', 'Muffinpants']
+        'author_name': ['Captain Snuggles', 'Muffinpants'],
+        'author': ['1234:Captain Snuggles', '7890:Muffinpants']
     }]
 
 
@@ -126,7 +127,7 @@ def test_authors_filters_out_authors_with_empty_mitid(mongo):
 
 
 def test_get_author_returns_solr_query():
-    assert get_author({'mitid': 1234, 'name': 'Fluffy'}) == \
+    assert get_author({'mitid': '1234', 'name': 'Fluffy'}) == \
         ('author_id:"1234"', {'rows': 0, 'group': 'true',
                               'group.field': 'handle', 'group.ngroups': 'true'})
 
@@ -181,8 +182,8 @@ def test_create_handle_creates_mongo_insert(handle_result):
                           {'country': 'ISL', 'downloads': 4}],
             'dates': [{'date': '2015-01-01', 'downloads': 3},
                       {'date': '2015-02-01', 'downloads': 5}],
-            'parents': [{'mitid': 1234, 'name': 'Fluffy'},
-                        {'mitid': 5678, 'name': 'Captain Snuggles'}]
+            'parents': [{'mitid': '1234', 'name': 'Fluffy'},
+                        {'mitid': '5678', 'name': 'Captain Snuggles'}]
         }
     }
 
@@ -214,9 +215,17 @@ def test_dictify_converts_facet_count_to_dictionary():
 
 
 def test_split_author_turns_string_into_compound_field():
-    assert split_author('1234:Foo Bar') == {'mitid': 1234, 'name': 'Foo Bar'}
+    assert split_author('1234:Foo Bar') == {'mitid': '1234', 'name': 'Foo Bar'}
 
 
 def test_split_author_returns_none_for_invalid_author():
     assert split_author(':Foo Bar') is None
     assert split_author('Foo Bar') is None
+
+
+def test_join_author_joins_author_parts():
+    assert join_author({'name': 'Cat, Lucy', 'mitid': '1234'}) == \
+        '1234:Cat, Lucy'
+
+def test_join_author_uses_empty_string_for_missing_items():
+    assert join_author({'name': 'Cat, Lucy'}) == ':Cat, Lucy'