aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/functional/test_objformats.py
blob: 308cac67085f38d723d1b464ac15d812339b708d (plain) (blame)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
from pathlib import Path

import pytest
from jsondiff import diff
from sec_certs.sample.cc import CCCertificate

from sec_certs_page.common.objformats import ObjFormat, WorkingFormat, freeze, unfreeze


@pytest.fixture(scope="module")
def cert1():
    test_path = Path(__file__).parent / "data" / "cert1.json"
    with test_path.open() as f:
        return test_path, json.load(f)


@pytest.fixture(scope="module")
def cert2():
    test_path = Path(__file__).parent / "data" / "cert2.json"
    with test_path.open() as f:
        return test_path, json.load(f)


def test_load_cert(cert1):
    test_path, cert_data = cert1
    cert = CCCertificate.from_json(test_path)
    storage_format = ObjFormat(cert).to_raw_format().to_working_format().to_storage_format()
    obj_format = storage_format.to_working_format().to_raw_format().to_obj_format()
    assert cert == obj_format.get()

    # Ditch this, it is non-deterministic and making it deterministic would be quite hard.
    # json_mapping = storage_format.to_json_mapping()
    # assert json_mapping == cert_data


@pytest.mark.xfail(reason="Storage format is lossy for non-str dict keys.")
def test_diff(cert1, cert2):
    d = diff(cert1[1], cert2[1], syntax="explicit")
    working = WorkingFormat(d)
    working.to_raw_format().get()
    working_back = working.to_storage_format().to_working_format().get()
    assert working.get() == unfreeze(working_back)


def test_freeze_unfreeze(cert1, cert2):
    d = diff(cert1[1], cert2[1], syntax="explicit")

    df = freeze(d)
    duf = unfreeze(df)

    assert d == duf