Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Fix #17
Browse files Browse the repository at this point in the history
  • Loading branch information
pafonta committed Aug 20, 2018
1 parent 6d4a0a7 commit edbb35b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
17 changes: 12 additions & 5 deletions nat/zotero_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,18 @@ def reference_creator_surnames(self, index):
# book (as opposed to the level of a chapter).
creators = self.reference_data(index)["creators"]
creator_types = [x["creatorType"] for x in creators]
if "author" in creator_types:
return [x["lastName"] for x in creators if x["creatorType"] == "author"]
else:
return [x["lastName"] for x in creators]
# 'name' (not split) might be used instead of 'firstName' and 'lastName'.
try:
if "author" in creator_types:
return [x["lastName"] for x in creators if x["creatorType"] == "author"]
else:
return [x["lastName"] for x in creators]
except KeyError:
return []

def reference_creator_surnames_str(self, index):
"""Return as a string the surnames of the reference creators (locally defined)."""
# NB: str.join() returns an empty string for an empty list.
return ", ".join(self.reference_creator_surnames(index))

def reference_date(self, index):
Expand Down Expand Up @@ -249,8 +254,10 @@ def reference_creators_citation(self, ref_id):
# FIXME Delayed refactoring. Use an index instead of an ID.
index = self.reference_index(ref_id)
creators = self.reference_creator_surnames(index)
year = self.reference_year(index)
creator_count = len(creators)
if creator_count == 0:
return ""
year = self.reference_year(index)
if creator_count == 1:
return "{} ({})".format(creators[0], year)
elif creator_count == 2:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from setuptools import setup

VERSION = "0.4.2"
VERSION = "0.4.3"

HERE = os.path.abspath(os.path.dirname(__file__))

Expand Down
10 changes: 7 additions & 3 deletions tests/zotero/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@
{
"creatorType": "editor",
"firstName": "EditorFirstA",
"lastName": "EditorLastD"
"lastName": "EditorLastA"
},
{
"creatorType": "editor",
"firstName": "EditorFirst B",
"lastName": "EditorLast-E"
}
"lastName": "EditorLast-B"
},
{
"creatorType": "author",
"name": "AuthorLastD AuthorFirstD"
},
]


Expand Down
43 changes: 31 additions & 12 deletions tests/zotero/test_zotero_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,9 @@ def test_reference_title(self, zw0):
@mark.parametrize("value, expected", [
param(CREATORS[:1], ["AuthorLastA"], id="one_author"),
param(CREATORS[:2], ["AuthorLastA", "AuthorLast-B"], id="two_authors"),
param(CREATORS[3:], ["EditorLastD", "EditorLast-E"], id="several_not_authors"),
param(CREATORS, ["AuthorLastA", "AuthorLast-B", "AuthorLastC"], id="several_mixed")
param(CREATORS[3:-1], ["EditorLastA", "EditorLast-B"], id="several_not_authors"),
param(CREATORS[:-1], ["AuthorLastA", "AuthorLast-B", "AuthorLastC"], id="several_mixed"),
param(CREATORS[-1:], [], id="no_last_name"),
])
def test_reference_creator_surnames(self, zw0, value, expected):
"""Test reference_creator_surnames().
Expand All @@ -375,18 +376,25 @@ def test_reference_creator_surnames(self, zw0, value, expected):
When a reference has two creators of type 'author'.
When a reference has several creators which aren't of type 'author'.
When a reference has several creators of type 'author' and not.
When a reference has one creator of type 'author' described with 'name'.
"""
init(zw0, "creators", value)
assert zw0.reference_creator_surnames(0) == expected

# reference_creator_surnames_str

@mark.parametrize("value, expected", [
param(CREATORS[:1], "AuthorLastA", id="one"),
param(CREATORS[:2], "AuthorLastA, AuthorLast-B", id="two")
param(CREATORS[:1], "AuthorLastA", id="one_author"),
param(CREATORS[:2], "AuthorLastA, AuthorLast-B", id="two_authors"),
param(CREATORS[-1:], "", id="no_last_name"),
])
def test_reference_creator_surnames_str(self, zw0, value, expected):
"""When a reference has one or two creator(s) of type 'author'."""
"""Test reference_creator_surnames_str().
When a reference has one creator of type 'author'.
When a reference has two creators of type 'author'.
When a reference has one creator of type 'author' described with 'name'.
"""
init(zw0, "creators", value)
assert zw0.reference_creator_surnames_str(0) == expected

Expand Down Expand Up @@ -458,20 +466,31 @@ def test_reference_index_not_found(self, zw0):
# reference_creators_citation

@mark.parametrize("value, expected", [
param(CREATORS[:1], "AuthorLastA (2017)", id="one"),
param(CREATORS[:2], "AuthorLastA and AuthorLast-B (2017)", id="two"),
param(CREATORS[:3], "AuthorLastA et al. (2017)", id="three")
param(CREATORS[:1], "AuthorLastA (2017)", id="one_author"),
param(CREATORS[:2], "AuthorLastA and AuthorLast-B (2017)", id="two_authors"),
param(CREATORS[:3], "AuthorLastA et al. (2017)", id="three_authors"),
param(CREATORS[-1:], "", id="no_last_name"),
])
def test_reference_creators_citation(self, zw0, value, expected):
"""When a reference has an ID, a date, and 1, 2, or 3 creator(s)."""
"""Test reference_creators_citation().
When a reference with an ID and a date has one creator.
When a reference with an ID and a date has two creators.
When a reference with an ID and a date has three creators.
When a reference with an ID and a date has one creator described with 'name'.
"""
reference = init(zw0, "DOI", DOI)
reference["data"]["date"] = DATE
reference["data"]["creators"] = value
assert zw0.reference_creators_citation(DOI) == expected

def test_reference_creators_citation_year_without(self, zw0):
@mark.parametrize("value, expected", [
param(CREATORS[:3], "AuthorLastA et al. ()", id="three_authors"),
param(CREATORS[-1:], "", id="no_last_name"),
])
def test_reference_creators_citation_year_without(self, zw0, value, expected):
"""When a reference has no year (because no date)."""
reference = init(zw0, "DOI", DOI)
reference["data"]["date"] = ""
reference["data"]["creators"] = CREATORS[:3]
assert zw0.reference_creators_citation(DOI) == "AuthorLastA et al. ()"
reference["data"]["creators"] = value
assert zw0.reference_creators_citation(DOI) == expected

0 comments on commit edbb35b

Please sign in to comment.