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

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

    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.action for r in response.records] == [b.get('action') for b in body]
    assert [r.created_at for r in response.records] == [b.get('created_at') for b in body]
    assert [r.customer_notifications for r in response.records] == [b.get('customer_notifications') for b in body]
    assert [r.id for r in response.records] == [b.get('id') for b in body]
    assert [r.metadata for r in response.records] == [b.get('metadata') for b in body]
    assert [r.resource_metadata for r in response.records] == [b.get('resource_metadata') for b in body]
    assert [r.resource_type for r in response.records] == [b.get('resource_type') for b in body]

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

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

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

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

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

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

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

@responses.activate
def test_events_get():
    fixture = helpers.load_fixture('events')['get']
    helpers.stub_response(fixture)
    response = helpers.client.events.get(*fixture['url_params'])
    body = fixture['body']['events']

    assert isinstance(response, resources.Event)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is None
    assert response.action == body.get('action')
    assert response.created_at == body.get('created_at')
    assert response.customer_notifications == body.get('customer_notifications')
    assert response.id == body.get('id')
    assert response.metadata == body.get('metadata')
    assert response.resource_metadata == body.get('resource_metadata')
    assert response.resource_type == body.get('resource_type')
    assert response.details.bank_account_id == body.get('details')['bank_account_id']
    assert response.details.cause == body.get('details')['cause']
    assert response.details.currency == body.get('details')['currency']
    assert response.details.description == body.get('details')['description']
    assert response.details.not_retried_reason == body.get('details')['not_retried_reason']
    assert response.details.origin == body.get('details')['origin']
    assert response.details._property == body.get('details')['property']
    assert response.details.reason_code == body.get('details')['reason_code']
    assert response.details.scheme == body.get('details')['scheme']
    assert response.details.will_attempt_retry == body.get('details')['will_attempt_retry']
    assert response.links.bank_authorisation == body.get('links')['bank_authorisation']
    assert response.links.billing_request == body.get('links')['billing_request']
    assert response.links.billing_request_flow == body.get('links')['billing_request_flow']
    assert response.links.creditor == body.get('links')['creditor']
    assert response.links.customer == body.get('links')['customer']
    assert response.links.customer_bank_account == body.get('links')['customer_bank_account']
    assert response.links.instalment_schedule == body.get('links')['instalment_schedule']
    assert response.links.mandate == body.get('links')['mandate']
    assert response.links.mandate_request_mandate == body.get('links')['mandate_request_mandate']
    assert response.links.new_customer_bank_account == body.get('links')['new_customer_bank_account']
    assert response.links.new_mandate == body.get('links')['new_mandate']
    assert response.links.organisation == body.get('links')['organisation']
    assert response.links.parent_event == body.get('links')['parent_event']
    assert response.links.payer_authorisation == body.get('links')['payer_authorisation']
    assert response.links.payment == body.get('links')['payment']
    assert response.links.payment_request_payment == body.get('links')['payment_request_payment']
    assert response.links.payout == body.get('links')['payout']
    assert response.links.previous_customer_bank_account == body.get('links')['previous_customer_bank_account']
    assert response.links.refund == body.get('links')['refund']
    assert response.links.scheme_identifier == body.get('links')['scheme_identifier']
    assert response.links.subscription == body.get('links')['subscription']

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

    assert isinstance(response, resources.Event)

def test_502_events_get_retries():
    fixture = helpers.load_fixture('events')['get']
    with helpers.stub_502_then_response(fixture) as rsps:
      response = helpers.client.events.get(*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']['events']

    assert isinstance(response, resources.Event)
  
