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

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

@responses.activate
def test_mandate_imports_create_new_idempotency_key_for_each_call():
    fixture = helpers.load_fixture('mandate_imports')['create']
    helpers.stub_response(fixture)
    helpers.client.mandate_imports.create(*fixture['url_params'])
    helpers.client.mandate_imports.create(*fixture['url_params'])
    assert responses.calls[0].request.headers.get('Idempotency-Key') != responses.calls[1].request.headers.get('Idempotency-Key')

def test_timeout_mandate_imports_create_idempotency_conflict():
    create_fixture = helpers.load_fixture('mandate_imports')['create']
    get_fixture = helpers.load_fixture('mandate_imports')['get']
    with helpers.stub_timeout_then_idempotency_conflict(create_fixture, get_fixture) as rsps:
      response = helpers.client.mandate_imports.create(*create_fixture['url_params'])
      assert len(rsps.calls) == 2

    assert isinstance(response, resources.MandateImport)

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

    assert isinstance(response, resources.MandateImport)

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

    assert isinstance(response, resources.MandateImport)
  

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

    assert isinstance(response, resources.MandateImport)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is None
    assert response.created_at == body.get('created_at')
    assert response.id == body.get('id')
    assert response.scheme == body.get('scheme')
    assert response.status == body.get('status')
    assert response.links.creditor == body.get('links')['creditor']

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

    assert isinstance(response, resources.MandateImport)

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

    assert isinstance(response, resources.MandateImport)
  

@responses.activate
def test_mandate_imports_submit():
    fixture = helpers.load_fixture('mandate_imports')['submit']
    helpers.stub_response(fixture)
    response = helpers.client.mandate_imports.submit(*fixture['url_params'])
    body = fixture['body']['mandate_imports']

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

def test_timeout_mandate_imports_submit_doesnt_retry():
    fixture = helpers.load_fixture('mandate_imports')['submit']
    with helpers.stub_timeout(fixture) as rsps:
      with pytest.raises(requests.ConnectTimeout):
        response = helpers.client.mandate_imports.submit(*fixture['url_params'])
      assert len(rsps.calls) == 1

def test_502_mandate_imports_submit_doesnt_retry():
    fixture = helpers.load_fixture('mandate_imports')['submit']
    with helpers.stub_502(fixture) as rsps:
      with pytest.raises(MalformedResponseError):
        response = helpers.client.mandate_imports.submit(*fixture['url_params'])
      assert len(rsps.calls) == 1
  

@responses.activate
def test_mandate_imports_cancel():
    fixture = helpers.load_fixture('mandate_imports')['cancel']
    helpers.stub_response(fixture)
    response = helpers.client.mandate_imports.cancel(*fixture['url_params'])
    body = fixture['body']['mandate_imports']

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

def test_timeout_mandate_imports_cancel_doesnt_retry():
    fixture = helpers.load_fixture('mandate_imports')['cancel']
    with helpers.stub_timeout(fixture) as rsps:
      with pytest.raises(requests.ConnectTimeout):
        response = helpers.client.mandate_imports.cancel(*fixture['url_params'])
      assert len(rsps.calls) == 1

def test_502_mandate_imports_cancel_doesnt_retry():
    fixture = helpers.load_fixture('mandate_imports')['cancel']
    with helpers.stub_502(fixture) as rsps:
      with pytest.raises(MalformedResponseError):
        response = helpers.client.mandate_imports.cancel(*fixture['url_params'])
      assert len(rsps.calls) == 1
  
