Skip to content

Commit

Permalink
Integrate lifelist_metadata into ObservationController.life_list()
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Jul 17, 2023
1 parent 7a29622 commit 5bad51d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']

Expand Down
23 changes: 21 additions & 2 deletions pyinaturalist/controllers/observation_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pyinaturalist.v1 import (
create_observation,
delete_observation,
get_life_list_metadata,
get_observation_histogram,
get_observation_identifiers,
get_observation_observers,
Expand Down Expand Up @@ -89,9 +90,27 @@ def identifiers(self, **params) -> UserCounts:
response = self.client.request(get_observation_identifiers, **params)
return UserCounts.from_json(response)

@document_controller_params(get_observation_taxonomy, add_common_args=False)
def life_list(self, user_id: IntOrStr, **params) -> LifeList:
@document_controller_params(get_observation_taxonomy)
def life_list(
self, user_id: Optional[IntOrStr] = None, locale: Optional[str] = None, **params
) -> LifeList:
"""Get taxa from a user's dynamic life list
Args:
user_id: iNaturalist user ID or username
locale: Locale preference for taxon common names
"""
response = self.client.request(get_observation_taxonomy, user_id=user_id, **params)

# Add additional metadata to a life list response; requires a user ID
if user_id:
metadata = self.client.request(
get_life_list_metadata, user_id=user_id, locale=locale, **params
)
meta_by_id = {item['id']: item for item in metadata['results']}
for taxon in response['results']:
taxon.update(meta_by_id.get(taxon['id'], {}))

return LifeList.from_json(response)

@document_controller_params(get_observation_observers)
Expand Down
38 changes: 32 additions & 6 deletions test/controllers/test_observation_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,42 @@ def test_life_list(requests_mock):
status_code=200,
)
client = iNatClient()
results = client.observations.life_list(user_id=545640, taxon_id=52775)
results = client.observations.life_list(taxon_id=52775)

assert isinstance(results, LifeList)
assert len(results) == 31

bombus = results[8]
assert bombus.id == 52775
assert bombus.name == 'Bombus'
assert bombus.count == 4
assert bombus.descendant_obs_count == results.get_count(52775) == 154
t = results[8]
assert t.id == 52775
assert t.name == 'Bombus'
assert t.count == 4
assert t.descendant_obs_count == results.get_count(52775) == 154


def test_life_list__with_user_id(requests_mock):
"""With user_id, the results should include extra metadata"""
requests_mock.get(
f'{API_V1}/observations/taxonomy',
json=j_life_list_1,
status_code=200,
)
requests_mock.get(
f'{API_V1}/taxa/lifelist_metadata',
json=j_life_list_metadata,
status_code=200,
)
client = iNatClient()
results = client.observations.life_list(user_id=545640, taxon_id=574)

assert isinstance(results, LifeList)
assert len(results) == 10

t = results[4]
assert t.id == 573
assert t.name == 'Galliformes'
assert t.preferred_common_name == 'Landfowl'
assert t.count == 0
assert t.descendant_obs_count == results.get_count(573) == 4


def test_popular_fields(requests_mock):
Expand Down

0 comments on commit 5bad51d

Please sign in to comment.