| 1 |
from array import array |
| 2 |
from collections import defaultdict, namedtuple |
| 3 |
from decimal import Decimal |
| 4 |
from itertools import count |
| 5 |
|
| 6 |
from graphql.pyutils import FrozenDict, FrozenList, is_collection, is_iterable |
| 7 |
|
| 8 |
|
| 9 |
def describe_is_collection(): |
| 10 |
def should_return_true_for_lists(): |
| 11 |
assert is_collection([]) is True |
| 12 |
assert is_collection([0, 1, 2]) is True |
| 13 |
assert is_collection(["A", "B", "C"]) is True |
| 14 |
|
| 15 |
def should_return_true_for_frozen_lists(): |
| 16 |
assert is_collection(FrozenList()) is True |
| 17 |
assert is_collection(FrozenList([0, 1, 2])) is True |
| 18 |
assert is_collection(FrozenList(["A", "B", "C"])) is True |
| 19 |
|
| 20 |
def should_return_true_for_tuples(): |
| 21 |
assert is_collection(()) is True |
| 22 |
assert is_collection((0, 1, 1)) is True |
| 23 |
assert is_collection(("A", "B", "C")) is True |
| 24 |
|
| 25 |
def should_return_true_for_named_tuples(): |
| 26 |
named = namedtuple("named", "A B C") |
| 27 |
assert is_collection(named(0, 1, 2)) is True |
| 28 |
|
| 29 |
def should_return_true_for_arrays(): |
| 30 |
assert is_collection(array("b")) is True |
| 31 |
assert is_collection(array("b", [0, 1, 2])) is True |
| 32 |
assert is_collection(array("i")) is True |
| 33 |
assert is_collection(array("i", [0, 1, 2])) is True |
| 34 |
|
| 35 |
def should_return_true_for_keys_view(): |
| 36 |
assert is_collection({}.keys()) is True |
| 37 |
assert is_collection({0: "A", 1: "B", 2: "C"}.keys()) is True |
| 38 |
|
| 39 |
def should_return_true_for_values_view(): |
| 40 |
assert is_collection({}.values()) is True |
| 41 |
assert is_collection({0: "A", 1: "B", 2: "C"}.values()) is True |
| 42 |
|
| 43 |
def should_return_true_for_ranges(): |
| 44 |
assert is_collection(range(10)) is True |
| 45 |
|
| 46 |
def should_return_false_for_range_function(): |
| 47 |
assert is_collection(range) is False |
| 48 |
|
| 49 |
def should_return_false_for_generator_function(): |
| 50 |
assert is_collection(count) is False |
| 51 |
|
| 52 |
def should_return_false_for_infinite_generators(): |
| 53 |
assert is_collection(count()) is False |
| 54 |
|
| 55 |
def should_return_false_for_none_object(): |
| 56 |
assert is_collection(None) is False |
| 57 |
|
| 58 |
def should_return_false_for_strings(): |
| 59 |
assert is_collection("ABC") is False |
| 60 |
assert is_collection("0") is False |
| 61 |
assert is_collection("") is False |
| 62 |
|
| 63 |
def should_return_false_for_byte_strings(): |
| 64 |
assert is_collection(b"ABC") is False |
| 65 |
assert is_collection(b"0") is False |
| 66 |
assert is_collection(b"") is False |
| 67 |
|
| 68 |
def should_return_false_for_ints(): |
| 69 |
assert is_collection(1) is False |
| 70 |
assert is_collection(-1) is False |
| 71 |
assert is_collection(0) is False |
| 72 |
|
| 73 |
def should_return_false_for_floats(): |
| 74 |
assert is_collection(1.0) is False |
| 75 |
assert is_collection(-1.0) is False |
| 76 |
assert is_collection(0.0) is False |
| 77 |
assert is_collection(float("NaN")) is False |
| 78 |
assert is_collection(float("Inf")) is False |
| 79 |
assert is_collection(float("-Inf")) is False |
| 80 |
|
| 81 |
def should_return_false_for_decimals(): |
| 82 |
assert is_collection(Decimal("1.0")) is False |
| 83 |
assert is_collection(Decimal("-1.0")) is False |
| 84 |
assert is_collection(Decimal("0.0")) is False |
| 85 |
assert is_collection(Decimal("NaN")) is False |
| 86 |
assert is_collection(Decimal("Inf")) is False |
| 87 |
assert is_collection(Decimal("-Inf")) is False |
| 88 |
|
| 89 |
def should_return_false_for_booleans(): |
| 90 |
assert is_collection(True) is False |
| 91 |
assert is_collection(False) is False |
| 92 |
|
| 93 |
def should_return_true_for_sets(): |
| 94 |
assert is_collection(set()) is True |
| 95 |
assert is_collection({0, 1, 2}) is True |
| 96 |
assert is_collection({"A", "B", "C"}) is True |
| 97 |
|
| 98 |
def should_return_false_for_dicts(): |
| 99 |
assert is_collection({}) is False |
| 100 |
assert is_collection({"__iter__": True}) is False |
| 101 |
assert is_collection({0: "A", 1: "B", 2: "C"}) is False |
| 102 |
|
| 103 |
def should_return_false_for_frozen_dicts(): |
| 104 |
assert is_collection(FrozenDict()) is False |
| 105 |
assert is_collection(FrozenDict({"__iter__": True})) is False |
| 106 |
assert is_collection(FrozenDict({0: "A", 1: "B", 2: "C"})) is False |
| 107 |
|
| 108 |
def should_return_false_for_default_dicts(): |
| 109 |
assert is_collection(defaultdict(list)) is False |
| 110 |
|
| 111 |
def should_return_false_for_simple_objects(): |
| 112 |
assert is_collection(object) is False |
| 113 |
assert is_collection(object()) is False |
| 114 |
|
| 115 |
def should_return_false_for_invalid_iterator_object(): |
| 116 |
class NoIterator: |
| 117 |
__iter__ = None |
| 118 |
|
| 119 |
assert is_collection(NoIterator) is False |
| 120 |
assert is_collection(NoIterator()) is False |
| 121 |
|
| 122 |
|
| 123 |
def describe_is_iterable(): |
| 124 |
def should_return_true_for_lists(): |
| 125 |
assert is_iterable([]) is True |
| 126 |
assert is_iterable([0, 1, 2]) is True |
| 127 |
assert is_iterable(["A", "B", "C"]) is True |
| 128 |
|
| 129 |
def should_return_true_for_frozen_lists(): |
| 130 |
assert is_iterable(FrozenList()) is True |
| 131 |
assert is_iterable(FrozenList([0, 1, 2])) is True |
| 132 |
assert is_iterable(FrozenList(["A", "B", "C"])) is True |
| 133 |
|
| 134 |
def should_return_true_for_tuples(): |
| 135 |
assert is_iterable(()) is True |
| 136 |
assert is_iterable((0, 1, 1)) is True |
| 137 |
assert is_iterable(("A", "B", "C")) is True |
| 138 |
|
| 139 |
def should_return_true_for_named_tuples(): |
| 140 |
named = namedtuple("named", "a b c") |
| 141 |
assert is_iterable(named(0, 1, 2)) is True |
| 142 |
|
| 143 |
def should_return_true_for_arrays(): |
| 144 |
assert is_iterable(array("b")) is True |
| 145 |
assert is_iterable(array("b", [0, 1, 2])) is True |
| 146 |
assert is_iterable(array("i")) is True |
| 147 |
assert is_iterable(array("i", [0, 1, 2])) is True |
| 148 |
|
| 149 |
def should_return_true_for_keys_view(): |
| 150 |
assert is_iterable({}.keys()) is True |
| 151 |
assert is_iterable({0: "A", 1: "B", 2: "C"}.keys()) is True |
| 152 |
|
| 153 |
def should_return_true_for_values_view(): |
| 154 |
assert is_iterable({}.values()) is True |
| 155 |
assert is_iterable({0: "A", 1: "B", 2: "C"}.values()) is True |
| 156 |
|
| 157 |
def should_return_true_for_ranges(): |
| 158 |
assert is_iterable(range(10)) is True |
| 159 |
|
| 160 |
def should_return_false_for_range_function(): |
| 161 |
assert is_iterable(range) is False |
| 162 |
|
| 163 |
def should_return_false_for_generator_function(): |
| 164 |
assert is_iterable(count) is False |
| 165 |
|
| 166 |
def should_return_true_for_infinite_generators(): |
| 167 |
assert is_iterable(count()) is True |
| 168 |
|
| 169 |
def should_return_false_for_none_object(): |
| 170 |
assert is_iterable(None) is False |
| 171 |
|
| 172 |
def should_return_false_for_strings(): |
| 173 |
assert is_iterable("ABC") is False |
| 174 |
assert is_iterable("0") is False |
| 175 |
assert is_iterable("") is False |
| 176 |
|
| 177 |
def should_return_false_for_byte_strings(): |
| 178 |
assert is_iterable(b"ABC") is False |
| 179 |
assert is_iterable(b"0") is False |
| 180 |
assert is_iterable(b"") is False |
| 181 |
|
| 182 |
def should_return_false_for_ints(): |
| 183 |
assert is_iterable(1) is False |
| 184 |
assert is_iterable(-1) is False |
| 185 |
assert is_iterable(0) is False |
| 186 |
|
| 187 |
def should_return_false_for_floats(): |
| 188 |
assert is_iterable(1.0) is False |
| 189 |
assert is_iterable(-1.0) is False |
| 190 |
assert is_iterable(0.0) is False |
| 191 |
assert is_iterable(float("NaN")) is False |
| 192 |
assert is_iterable(float("Inf")) is False |
| 193 |
assert is_iterable(float("-Inf")) is False |
| 194 |
|
| 195 |
def should_return_false_for_decimals(): |
| 196 |
assert is_iterable(Decimal("1.0")) is False |
| 197 |
assert is_iterable(Decimal("-1.0")) is False |
| 198 |
assert is_iterable(Decimal("0.0")) is False |
| 199 |
assert is_iterable(Decimal("NaN")) is False |
| 200 |
assert is_iterable(Decimal("Inf")) is False |
| 201 |
assert is_iterable(Decimal("-Inf")) is False |
| 202 |
|
| 203 |
def should_return_false_for_booleans(): |
| 204 |
assert is_iterable(True) is False |
| 205 |
assert is_iterable(False) is False |
| 206 |
|
| 207 |
def should_return_true_for_sets(): |
| 208 |
assert is_iterable(set()) is True |
| 209 |
assert is_iterable({0, 1, 2}) is True |
| 210 |
assert is_iterable({"A", "B", "C"}) is True |
| 211 |
|
| 212 |
def should_return_false_for_dicts(): |
| 213 |
assert is_iterable({}) is False |
| 214 |
assert is_iterable({"__iter__": True}) is False |
| 215 |
assert is_iterable({0: "A", 1: "B", 2: "C"}) is False |
| 216 |
|
| 217 |
def should_return_false_for_frozen_dicts(): |
| 218 |
assert is_iterable(FrozenDict()) is False |
| 219 |
assert is_iterable(FrozenDict({"__iter__": True})) is False |
| 220 |
assert is_iterable(FrozenDict({0: "A", 1: "B", 2: "C"})) is False |
| 221 |
|
| 222 |
def should_return_false_for_default_dicts(): |
| 223 |
assert is_iterable(defaultdict(list)) is False |
| 224 |
|
| 225 |
def should_return_false_for_simple_objects(): |
| 226 |
assert is_iterable(object) is False |
| 227 |
assert is_iterable(object()) is False |
| 228 |
|
| 229 |
def should_return_false_for_invalid_iterator_object(): |
| 230 |
class NoIterator: |
| 231 |
__iter__ = None |
| 232 |
|
| 233 |
assert is_iterable(NoIterator) is False |
| 234 |
assert is_iterable(NoIterator()) is False |