Skip to content

Commit

Permalink
fix #75 occurrences count changes
Browse files Browse the repository at this point in the history
- add checker in occurrences.count that values are at most length 1
- add test for this checker
- add docs for occurrences.count saying values length 1 only, point to search for more than 1
- bump version
  • Loading branch information
sckott committed Nov 18, 2020
1 parent 4bfdb7b commit 15b0524
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
12 changes: 11 additions & 1 deletion pygbif/gbifutils.py
Expand Up @@ -137,11 +137,21 @@ def check_data(x, y):


def len2(x):
if x.__class__ is str:
if isinstance(x, int):
return len([x])
elif isinstance(x, str):
return len([x])
else:
return len(x)

def stuff(**kwargs):
return kwargs

def check_param_lens(**kwargs):
tmp = {k: v for k, v in kwargs.items() if v is not None}
for k,v in tmp.items():
if len2(v) > 1:
raise TypeError(k + " must be length 1")

def get_meta(x):
if has_meta(x):
Expand Down
10 changes: 9 additions & 1 deletion pygbif/occurrences/count.py
@@ -1,4 +1,4 @@
from pygbif.gbifutils import gbif_baseurl, bool2str, gbif_GET
from pygbif.gbifutils import gbif_baseurl, bool2str, gbif_GET, check_param_lens


def count(
Expand All @@ -16,6 +16,10 @@ def count(
"""
Returns occurrence counts for a predefined set of dimensions
For all parameters below, only one value allowed per function call.
See :func:`~occurrences.search` for passing more than one value
per parameter.
:param taxonKey: [int] A GBIF occurrence identifier
:param basisOfRecord: [str] A GBIF occurrence identifier
:param country: [str] A GBIF occurrence identifier
Expand All @@ -36,6 +40,10 @@ def count(
occurrences.count(isGeoreferenced = True)
occurrences.count(basisOfRecord = 'OBSERVATION')
"""
check_param_lens(taxonKey=taxonKey,basisOfRecord=basisOfRecord,
country=country,isGeoreferenced=isGeoreferenced,
datasetKey=datasetKey,publishingCountry=publishingCountry,
typeStatus=typeStatus,issue=issue,year=year)
url = gbif_baseurl + "occurrence/count"
isGeoreferenced = bool2str(isGeoreferenced)
out = gbif_GET(
Expand Down
2 changes: 1 addition & 1 deletion pygbif/package_metadata.py
@@ -1,4 +1,4 @@
__version__ = "0.5.0"
__version__ = "0.5.1"
__title__ = "pygbif"
__author__ = "Scott Chamberlain"
__license__ = "MIT"
6 changes: 5 additions & 1 deletion test/test-occurrences-count.py
@@ -1,4 +1,5 @@
"""Tests for occurrences module - count methods"""
from nose.tools import *
import vcr
from pygbif import occurrences

Expand Down Expand Up @@ -35,6 +36,10 @@ def test_count():
res = occurrences.count(taxonKey=3329049)
assert int == res.__class__

@raises(TypeError)
def test_count_param_length():
"occurrences.count_param_length"
occurrences.count(datasetKey=['foo', 'bar'])

@vcr.use_cassette("test/vcr_cassettes/test_count_basisofrecord.yaml")
def test_count_basisofrecord():
Expand Down Expand Up @@ -79,7 +84,6 @@ def test_count_schema():
assert dict == res[0].__class__
assert "dimensions" == list(res[0].keys())[0]


@vcr.use_cassette("test/vcr_cassettes/test_count_publishingcountries.yaml")
def test_count_publishingcountries():
"occurrences.count_publishingcountries - basic test"
Expand Down

0 comments on commit 15b0524

Please sign in to comment.