import vcr
import pytest
import yaml
from habanero import Crossref

cr = Crossref()

# see https://github.com/sckott/habanero/issues/91

@vcr.use_cassette("test/vcr_cassettes/limit_of_zero_with_id.yaml")
def test_limit_of_zero_with_id():
    "param: limit - zero limit works"
    res = cr.members(ids=2984, works=True, facet="issn:*", limit=0)
    assert 0 == len(res['message']['items'])

@vcr.use_cassette("test/vcr_cassettes/offset_of_zero_with_id.yaml")
def test_offset_of_zero_with_id():
    "param: offset - zero offset works"
    res = cr.members(ids=2984, works=True, limit=1, offset=0)
    assert 0 == res['message']['query']['start-index']
    with open("test/vcr_cassettes/offset_of_zero_with_id.yaml") as f:
        x = yaml.safe_load(f)
    uri = x['interactions'][0]['request']['uri']
    assert "offset" in uri

@vcr.use_cassette("test/vcr_cassettes/limit_of_zero_without_id.yaml")
def test_limit_of_zero_without_id():
    "param: limit - zero limit works"
    res = cr.members(limit=0)
    assert 0 == len(res['message']['items'])

@vcr.use_cassette("test/vcr_cassettes/offset_of_zero_without_id.yaml")
def test_offset_of_zero_without_id():
    "param: offset - zero offset works"
    res = cr.members(limit=1, offset=0)
    assert 0 == res['message']['query']['start-index']
    with open("test/vcr_cassettes/offset_of_zero_without_id.yaml") as f:
        x = yaml.safe_load(f)
    uri = x['interactions'][0]['request']['uri']
    assert "offset" in uri

