import json

import pytest

from idempotency_key.encoders import BasicKeyEncoder
from idempotency_key.exceptions import MissingIdempotencyKeyError


def test_basic_encoding():
    class Request:
        path_info = "/myURL/path/"
        method = "POST"
        body = json.dumps({"key": "value"}).encode("UTF-8")

    request = Request()
    obj = BasicKeyEncoder()
    enc_key = obj.encode_key(request, "MyKey")
    assert enc_key == "<KEY>"


def test_basic_encoder_null_key():
    class Request:
        path_info = "/myURL/path/"
        method = "POST"
        body = json.dumps({"key": "value"}).encode("UTF-8")

    request = Request()
    obj = BasicKeyEncoder()
    with pytest.raises(MissingIdempotencyKeyError) as e_info:
        obj.encode_key(request, None)
    assert e_info.value.args[0] == "Idempotency key cannot be None."

import json

import pytest

from idempotency_key.encoders import BasicKeyEncoder
from idempotency_key.exceptions import MissingIdempotencyKeyError


def test_basic_encoding():
    class Request:
        path_info = "/myURL/path/"
        method = "POST"
        body = json.dumps({"key": "value"}).encode("UTF-8")

    request = Request()
    obj = BasicKeyEncoder()
    enc_key = obj.encode_key(request, "MyKey")
    assert enc_key == "<KEY>"


def test_basic_encoder_null_key():
    class Request:
        path_info = "/myURL/path/"
        method = "POST"
        body = json.dumps({"key": "value"}).encode("UTF-8")

    request = Request()
    obj = BasicKeyEncoder()
    with pytest.raises(MissingIdempotencyKeyError) as e_info:
        obj.encode_key(request, None)
    assert e_info.value.args[0] == "Idempotency key cannot be None."
