Skip to content

Commit

Permalink
Refactor to consolidate testing based on discussion in #6
Browse files Browse the repository at this point in the history
* Move fixtures to conftest.py
* Merge crossref and wiley mockers
* Add integration test in test_awd.py
  • Loading branch information
ehanson8 committed Aug 4, 2021
1 parent b5883b3 commit d5dae0f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 40 deletions.
5 changes: 2 additions & 3 deletions awd/wiley.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import requests


headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
}


Expand Down
34 changes: 34 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import json

import pytest
import requests_mock


@pytest.fixture()
def web_mock(work_record):
with requests_mock.Mocker() as m:
pdf = b"Test content"
request_headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
}
m.get(
"http://example.com/doi/10.1002/term.3131",
text="Forbidden",
status_code=403,
)
m.get(
"http://example.com/doi/10.1002/term.3131",
content=pdf,
request_headers=request_headers,
)
m.get(
"http://example.com/works/10.1002/term.3131?mailto=dspace-lib@mit.edu",
json=work_record,
)
yield m


@pytest.fixture()
def work_record():
return json.loads(open("fixtures/crossref.json", "r").read())
32 changes: 32 additions & 0 deletions tests/test_awd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from awd import crossref, wiley


def test_awd(web_mock):
dois = crossref.get_dois_from_spreadsheet("fixtures/test.xlsx")
for doi in dois:
work = crossref.get_crossref_work_from_doi("http://example.com/works/", doi)
value_dict = crossref.get_metadata_dict_from_crossref_work(work)
pdf = wiley.get_wiley_pdf("http://example.com/doi/", doi)
# value_dict transformed into DSpace metadata
# DSpace metadata and pdf sent to DSpace submission service
assert pdf == b"Test content"
assert value_dict["publisher"] == "Wiley"
assert value_dict["author"] == [
"Eivazzadeh‐Keihan, Reza",
"Bahojb Noruzi, Ehsan",
"Khanmohammadi Chenab, Karim",
"Jafari, Amir",
"Radinekiyan, Fateme",
"Hashemi, Seyed Masoud",
"Ahmadpour, Farnoush",
"Behboudi, Ali",
"Mosafer, Jafar",
"Mokhtarzadeh, Ahad",
"Maleki, Ali",
"Hamblin, Michael R.",
]
assert value_dict["URL"] == "http://dx.doi.org/10.1002/term.3131"
assert value_dict["container-title"] == [
"Journal of Tissue Engineering and Regenerative Medicine"
]
assert value_dict["issued"] == "2020-09-30"
24 changes: 2 additions & 22 deletions tests/test_crossref.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
import json

import pytest
import requests_mock

from awd import crossref


@pytest.fixture()
def crossref_mock(work_record):
with requests_mock.Mocker() as m:
m.get(
"http://example.com/works/10.1002/term.3131?mailto=dspace-lib@mit.edu",
json=work_record,
)
yield m


@pytest.fixture()
def work_record():
return json.loads(open("fixtures/crossref.json", "r").read())


def test_get_dois_from_spreadsheet():
dois = crossref.get_dois_from_spreadsheet("fixtures/test.xlsx")
for doi in dois:
assert doi == "10.1002/term.3131"


def test_get_crossref_work_from_dois(crossref_mock):
def test_get_crossref_work_from_dois(web_mock):
doi = "10.1002/term.3131"
work = crossref.get_crossref_work_from_doi("http://example.com/works/", doi)
assert work["message"]["title"] == [
"Metal‐based nanoparticles for bone tissue engineering"
]


def test_get_metadata_dict_from_crossref_work(crossref_mock, work_record):
def test_get_metadata_dict_from_crossref_work(web_mock, work_record):
value_dict = crossref.get_metadata_dict_from_crossref_work(work_record)
assert (
value_dict["title"] == "Metal‐based nanoparticles for bone tissue engineering"
Expand Down
16 changes: 1 addition & 15 deletions tests/test_wiley.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
import pytest
import requests_mock

from awd import wiley


@pytest.fixture()
def wiley_mock():
with requests_mock.Mocker() as m:
pdf = b"Test content"
m.get(
"http://example.com/doi/10.1002/term.3131",
content=pdf,
)
yield m


def test_get_crossref_work_from_dois(wiley_mock):
def test_get_crossref_work_from_dois(web_mock):
doi = "10.1002/term.3131"
pdf = wiley.get_wiley_pdf("http://example.com/doi/", doi)
assert pdf == b"Test content"

0 comments on commit d5dae0f

Please sign in to comment.