def positive_test(key: Key, *, value: Value = "v") -> None:
    # Add key to the storage
    sync(redis.set)(key, value)
    assert sync(redis.get)(key) == value

    response, execution_time = timed(client.delete)(f"/items/{key}")
    assert response.status_code == 204
    assert 0.1 > execution_time
Example #2
0
def positive_test(keys: List[Key], *, value: Value = "v") -> None:
    # Add keys to the storage
    sync(redis.multi_set)([(key, value) for key in keys])
    assert len(sync(redis.multi_get)(keys)) == len(keys)

    response, execution_time = timed(client.delete)("/bulk/items", json=keys)
    assert response.status_code == 200
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"deleted_count": len(set(keys))}
    assert 0.1 > execution_time
Example #3
0
def positive_test(key: Key, value: Value) -> None:
    # Add key to the storage
    sync(redis.set)(key, value)
    assert sync(redis.get)(key) == value

    response, execution_time = timed(client.get)(f"/items/{key}")
    assert response.status_code == 201
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"value": value}
    assert 0.1 > execution_time
def positive_test(key: Key, value: Value) -> None:
    response, execution_time = timed(client.put)(f"/items/{key}?value={value}")
    assert response.status_code == 201
    assert response.headers.get("content-type") == "application/json"
    assert response.headers.get("location") == f"/items/{key}"
    assert response.json() == {"stored": f"/items/{key}"}
    assert sync(redis.get)(key) == value
    assert 0.1 > execution_time
Example #5
0
def test_negative() -> None:
    response, execution_time = timed(client.get)("/items")
    assert response.status_code == 404
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"detail": "Not Found"}
    assert 0.1 > execution_time

    assert sync(redis.get)("invalid_key") is None
    response, execution_time = timed(client.get)("/items/invalid_key")
    assert response.status_code == 404
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {"detail": "Item not found"}
    assert 0.1 > execution_time
from typing import Dict

from fastapi.testclient import TestClient

from app.main import Key, Value, app, redis
from tests.utils import get_random_string, sync, timed

client = TestClient(app)
sync(redis.clear)()  # Flush Redis database prior to the test


def positive_test(items: Dict[Key, Value]) -> None:
    response, execution_time = timed(client.put)("/bulk/items", json=items)
    assert response.status_code == 201
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {
        "stored": [f"/items/{key}" for key in items.keys()]
    }
    assert 0.1 > execution_time


def test_positive() -> None:
    positive_test(items={"test_key": "test_value"})
    positive_test(items={"key1": "value1", "key2": "value2", "key3": "value3"})


def test_negative() -> None:
    response, execution_time = timed(client.put)("/bulk/items")
    assert response.status_code == 422
    assert response.headers.get("content-type") == "application/json"
    assert response.json() == {