File: test_super_collect.py

package info (click to toggle)
python-super-collections 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208 kB
  • sloc: python: 1,065; sh: 34; makefile: 3
file content (120 lines) | stat: -rw-r--r-- 3,324 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os
from collections import deque, UserList
from collections.abc import Sequence
import json

from rich import print
import pytest


from super_collections import SuperList, SuperDict, SuperCollection, super_collect


CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))

LIST_TYPES = ['CustomListLike']  # Example duck-typed name



class CustomListLike:
    "A custom list, vanilla"
    def __init__(self):
        self._data = [1, 2, 3]

    def __getitem__(self, index):
        return self._data[index]

    def __len__(self):
        return len(self._data)
Sequence.register(CustomListLike)


class CustomListLike2:
    "A custom list-like object, of Sequence type"
    def __init__(self):
        pass

    def __getitem__(self, index):
        if index >= len(self):
            raise IndexError("Too high")
        value = index * 2
        print("Index, Value:", index, value)
        return value

    def __len__(self):
        return 5
Sequence.register(CustomListLike2)

def assert_type(obj, class_):
    "Syntactic sugar for super_collection test"
    print(f"Testing '{type(obj).__name__}' as {class_}")
    coll_obj = super_collect(obj)
    assert coll_obj is not None, f"Object {type(obj)} translates to None?"
    assert isinstance(coll_obj, class_)
    assert isinstance(coll_obj, SuperCollection)

# ---------------------
# Tests
# ---------------------
def test_super_collect_list():
    obj = super_collect([1, 2])
    print("Obj:", type(obj))
    print("Obj:", type(obj).__name__)
    assert isinstance(obj, SuperList)

def test_super_collect_tuple():
    assert_type((1, 2), SuperList)

def test_super_collect_deque():
    assert_type(deque([1, 2]), SuperList)

def test_super_collect_userlist():
    assert_type(UserList([1, 2]), SuperList)

def test_super_collect_duck_type():
    assert_type(CustomListLike(), SuperList)
    # Registered as sequence:
    assert_type(CustomListLike2(), SuperList)

def test_super_collect_set():
    assert_type({1, 2}, SuperList)

def test_super_collect_dict():
    obj = {'a': 1}
    assert_type(obj, SuperDict)


@pytest.mark.parametrize("bad_type", ["string", b"bytes", bytearray(b"abc")])
def test_super_collect_invalid_types(bad_type):
    with pytest.raises(TypeError):
        print(f"Checking that {bad_type} <{type(bad_type).__name__}> is NOT accepted.")
        super_collect(bad_type)


def test_super_collection_class():
    obj1 = SuperList([1, 3, 5])
    assert isinstance(obj1, SuperCollection)

    obj2 = SuperDict({'a': 5, 'b':7})
    assert isinstance(obj2, SuperCollection)

    obj = SuperCollection.collect([obj1, obj2])
    print("My SuperCollection object:", obj)
    assert isinstance(obj, SuperCollection)


def test_read_json():
    "Test collecting a JSON file into a SuperCollection"
    # This file is actually a dictionary:
    FILENAME = os.path.join(CURRENT_DIR, 'solar_system.json')
    # Open the file and load its contents
    with open(FILENAME, 'r', encoding='utf-8') as f:
        data = json.load(f)
    # Now `data` is a Python structure — usually a dict or list
    content = SuperCollection.collect(data)
    print(str(content)[:200])
    print("(...)")
    # Make sure it is reflexive
    content2 = SuperCollection.collect(content)
    assert content == content2
    assert dict(content) == dict(content2)