File size: 1,073 Bytes
b247dc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Cache test."""
import json

import numpy as np

from manifest.caches.serializers import ArraySerializer, NumpyByteSerializer


def test_response_to_key_array() -> None:
    """Test array serializer initialization."""
    serializer = ArraySerializer()
    arr = np.random.rand(4, 4)
    res = {"response": {"choices": [{"array": arr}]}}
    key = serializer.response_to_key(res)
    key_dct = json.loads(key)
    assert isinstance(key_dct["response"]["choices"][0]["array"], str)

    res2 = serializer.key_to_response(key)
    assert np.allclose(arr, res2["response"]["choices"][0]["array"])


def test_response_to_key_numpybytes() -> None:
    """Test array serializer initialization."""
    serializer = NumpyByteSerializer()
    arr = np.random.rand(4, 4)
    res = {"response": {"choices": [{"array": arr}]}}
    key = serializer.response_to_key(res)
    key_dct = json.loads(key)
    assert isinstance(key_dct["response"]["choices"][0]["array"], str)

    res2 = serializer.key_to_response(key)
    assert np.allclose(arr, res2["response"]["choices"][0]["array"])