File size: 1,763 Bytes
2601533
 
 
fc43558
2601533
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from enum import Enum
import pycrdt
import pytest
from lynxkite_app.crdt import crdt_update


@pytest.fixture
def empty_dict_workspace():
    ydoc = pycrdt.Doc()
    ydoc["workspace"] = ws = pycrdt.Map()
    yield ws


@pytest.fixture
def empty_list_workspace():
    ydoc = pycrdt.Doc()
    ydoc["workspace"] = ws = pycrdt.Array()
    yield ws


class MyEnum(Enum):
    VALUE = 1


@pytest.mark.parametrize(
    "python_obj,expected",
    [
        (
            {
                "key1": "value1",
                "key2": {
                    "nested_key1": "nested_value1",
                    "nested_key2": ["nested_value2"],
                    "nested_key3": MyEnum.VALUE,
                },
            },
            {
                "key1": "value1",
                "key2": {
                    "nested_key1": "nested_value1",
                    "nested_key2": ["nested_value2"],
                    "nested_key3": "1",
                },
            },
        )
    ],
)
def test_crdt_update_with_dict(empty_dict_workspace, python_obj, expected):
    crdt_update(empty_dict_workspace, python_obj)
    assert empty_dict_workspace.to_py() == expected


@pytest.mark.parametrize(
    "python_obj,expected",
    [
        (
            [
                "value1",
                {"nested_key1": "nested_value1", "nested_key2": ["nested_value2"]},
                MyEnum.VALUE,
            ],
            [
                "value1",
                {"nested_key1": "nested_value1", "nested_key2": ["nested_value2"]},
                "1",
            ],
        ),
    ],
)
def test_crdt_update_with_list(empty_list_workspace, python_obj, expected):
    crdt_update(empty_list_workspace, python_obj)
    assert empty_list_workspace.to_py() == expected