# 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_verification_details_create():
    fixture = helpers.load_fixture('verification_details')['create']
    helpers.stub_response(fixture)
    response = helpers.client.verification_details.create(*fixture['url_params'])
    body = fixture['body']['verification_details']

    assert isinstance(response, resources.VerificationDetail)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.address_line1 == body.get('address_line1')
    assert response.address_line2 == body.get('address_line2')
    assert response.address_line3 == body.get('address_line3')
    assert response.city == body.get('city')
    assert response.company_number == body.get('company_number')
    assert response.description == body.get('description')
    assert response.directors == body.get('directors')
    assert response.name == body.get('name')
    assert response.postal_code == body.get('postal_code')
    assert response.links.creditor == body.get('links')['creditor']

@responses.activate
def test_timeout_verification_details_create_retries():
    fixture = helpers.load_fixture('verification_details')['create']
    with helpers.stub_timeout_then_response(fixture) as rsps:
      response = helpers.client.verification_details.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']['verification_details']

    assert isinstance(response, resources.VerificationDetail)

def test_502_verification_details_create_retries():
    fixture = helpers.load_fixture('verification_details')['create']
    with helpers.stub_502_then_response(fixture) as rsps:
      response = helpers.client.verification_details.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']['verification_details']

    assert isinstance(response, resources.VerificationDetail)
  

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

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

    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.address_line1 for r in response.records] == [b.get('address_line1') for b in body]
    assert [r.address_line2 for r in response.records] == [b.get('address_line2') for b in body]
    assert [r.address_line3 for r in response.records] == [b.get('address_line3') for b in body]
    assert [r.city for r in response.records] == [b.get('city') for b in body]
    assert [r.company_number for r in response.records] == [b.get('company_number') for b in body]
    assert [r.description for r in response.records] == [b.get('description') for b in body]
    assert [r.directors for r in response.records] == [b.get('directors') for b in body]
    assert [r.name for r in response.records] == [b.get('name') for b in body]
    assert [r.postal_code for r in response.records] == [b.get('postal_code') for b in body]

@responses.activate
def test_timeout_verification_details_list_retries():
    fixture = helpers.load_fixture('verification_details')['list']
    with helpers.stub_timeout_then_response(fixture) as rsps:
      response = helpers.client.verification_details.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']['verification_details']

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

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

def test_502_verification_details_list_retries():
    fixture = helpers.load_fixture('verification_details')['list']
    with helpers.stub_502_then_response(fixture) as rsps:
      response = helpers.client.verification_details.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']['verification_details']

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

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

@responses.activate
def test_verification_details_all():
    fixture = helpers.load_fixture('verification_details')['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.verification_details.all())
    assert len(all_records) == len(fixture['body']['verification_details']) * 2
    for record in all_records:
      assert isinstance(record, resources.VerificationDetail)
    
  
