# WARNING: Do not edit by hand, this file was generated by Crank:
#
#   https://github.com/gocardless/crank
#

import json

import pytest
import requests
import responses

from gocardless_pro.errors import MalformedResponseError
from gocardless_pro import resources
from gocardless_pro import list_response

from .. import helpers
  

@responses.activate
def test_negative_balance_limits_list():
    fixture = helpers.load_fixture('negative_balance_limits')['list']
    helpers.stub_response(fixture)
    response = helpers.client.negative_balance_limits.list(*fixture['url_params'])
    body = fixture['body']['negative_balance_limits']

    assert isinstance(response, list_response.ListResponse)
    assert isinstance(response.records[0], resources.NegativeBalanceLimit)

    assert response.before == fixture['body']['meta']['cursors']['before']
    assert response.after == fixture['body']['meta']['cursors']['after']
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is None
    assert [r.balance_limit for r in response.records] == [b.get('balance_limit') for b in body]
    assert [r.created_at for r in response.records] == [b.get('created_at') for b in body]
    assert [r.currency for r in response.records] == [b.get('currency') for b in body]
    assert [r.id for r in response.records] == [b.get('id') for b in body]

@responses.activate
def test_timeout_negative_balance_limits_list_retries():
    fixture = helpers.load_fixture('negative_balance_limits')['list']
    with helpers.stub_timeout_then_response(fixture) as rsps:
      response = helpers.client.negative_balance_limits.list(*fixture['url_params'])
      assert len(rsps.calls) == 2
      assert rsps.calls[0].request.headers.get('Idempotency-Key') == rsps.calls[1].request.headers.get('Idempotency-Key')
    body = fixture['body']['negative_balance_limits']

    assert isinstance(response, list_response.ListResponse)
    assert isinstance(response.records[0], resources.NegativeBalanceLimit)

    assert response.before == fixture['body']['meta']['cursors']['before']
    assert response.after == fixture['body']['meta']['cursors']['after']

def test_502_negative_balance_limits_list_retries():
    fixture = helpers.load_fixture('negative_balance_limits')['list']
    with helpers.stub_502_then_response(fixture) as rsps:
      response = helpers.client.negative_balance_limits.list(*fixture['url_params'])
      assert len(rsps.calls) == 2
      assert rsps.calls[0].request.headers.get('Idempotency-Key') == rsps.calls[1].request.headers.get('Idempotency-Key')
    body = fixture['body']['negative_balance_limits']

    assert isinstance(response, list_response.ListResponse)
    assert isinstance(response.records[0], resources.NegativeBalanceLimit)

    assert response.before == fixture['body']['meta']['cursors']['before']
    assert response.after == fixture['body']['meta']['cursors']['after']

@responses.activate
def test_negative_balance_limits_all():
    fixture = helpers.load_fixture('negative_balance_limits')['list']

    def callback(request):
        if 'after=123' in request.url:
          fixture['body']['meta']['cursors']['after'] = None
        else:
          fixture['body']['meta']['cursors']['after'] = '123'
        return [200, {}, json.dumps(fixture['body'])]

    url = 'http://example.com' + fixture['path_template']
    responses.add_callback(fixture['method'], url, callback)

    all_records = list(helpers.client.negative_balance_limits.all())
    assert len(all_records) == len(fixture['body']['negative_balance_limits']) * 2
    for record in all_records:
      assert isinstance(record, resources.NegativeBalanceLimit)
    
  

@responses.activate
def test_negative_balance_limits_create():
    fixture = helpers.load_fixture('negative_balance_limits')['create']
    helpers.stub_response(fixture)
    response = helpers.client.negative_balance_limits.create(*fixture['url_params'])
    body = fixture['body']['negative_balance_limits']

    assert isinstance(response, resources.NegativeBalanceLimit)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.balance_limit == body.get('balance_limit')
    assert response.created_at == body.get('created_at')
    assert response.currency == body.get('currency')
    assert response.id == body.get('id')
    assert response.links.creator_user == body.get('links')['creator_user']
    assert response.links.creditor == body.get('links')['creditor']

@responses.activate
def test_timeout_negative_balance_limits_create_retries():
    fixture = helpers.load_fixture('negative_balance_limits')['create']
    with helpers.stub_timeout_then_response(fixture) as rsps:
      response = helpers.client.negative_balance_limits.create(*fixture['url_params'])
      assert len(rsps.calls) == 2
      assert rsps.calls[0].request.headers.get('Idempotency-Key') == rsps.calls[1].request.headers.get('Idempotency-Key')
    body = fixture['body']['negative_balance_limits']

    assert isinstance(response, resources.NegativeBalanceLimit)

def test_502_negative_balance_limits_create_retries():
    fixture = helpers.load_fixture('negative_balance_limits')['create']
    with helpers.stub_502_then_response(fixture) as rsps:
      response = helpers.client.negative_balance_limits.create(*fixture['url_params'])
      assert len(rsps.calls) == 2
      assert rsps.calls[0].request.headers.get('Idempotency-Key') == rsps.calls[1].request.headers.get('Idempotency-Key')
    body = fixture['body']['negative_balance_limits']

    assert isinstance(response, resources.NegativeBalanceLimit)
  
