Esempio n. 1
0
    def _try_simplifying_key(self, storage, key, predicate):
        storage.begin()
        old_value = storage.get(key)
        storage.commit({}, (key, ))

        def test_validity(new_json):
            dumped_json = json.dumps(new_json)
            storage.begin()
            storage.commit({key: dumped_json}, ())

            return predicate()

        parsed_old_value = json.loads(old_value)

        shrunk_value = shrink(parsed_old_value, test_validity)

        storage.begin()
        storage.commit({key: json.dumps(shrunk_value)}, ())

        if shrunk_value != parsed_old_value:
            print("Shrunk key: {key}".format(key=key))
            return True
        else:
            return False
Esempio n. 2
0
def test_empty_string_is_unshrunk():
    assert shrink("", lambda x: True) == ""
Esempio n. 3
0
def test_strings_shrink_to_a_where_possible():
    assert shrink("bees", lambda x: len(x) >= 2) == "aa"
Esempio n. 4
0
def test_unshrinkable_strings_are_not_shrunk():
    assert shrink("bees", lambda x: x == "bees") == "bees"
Esempio n. 5
0
def test_dicts_shrink_values():
    assert shrink({"a": 100}, lambda x: x.get("a", 0) > 0) == {"a": 1}
Esempio n. 6
0
def test_dicts_shrink_to_the_empty_dict():
    assert shrink({"a": "b"}, lambda x: True) == {}
Esempio n. 7
0
def test_lists_shrink_down_sub_elements():
    assert (shrink([3, 3, 3],
                   lambda x: len(x) == 3 and all(y > 0
                                                 for y in x)) == [1, 1, 1])
Esempio n. 8
0
def test_lists_shrunk_by_cutting_off_from_both_ends():
    assert shrink(list("bees"), lambda x: "ee" in "".join(x)) == ["e", "e"]
Esempio n. 9
0
def test_numbers_do_not_shrink_if_they_cannot():
    assert shrink(100, lambda x: x == 100) == 100
Esempio n. 10
0
def test_positive_floats_shrink_to_one():
    assert shrink(100.0, lambda x: x > 0) == 1
Esempio n. 11
0
def test_shrink_none_produces_none():
    assert shrink(None, lambda x: True) is None
Esempio n. 12
0
def test_negative_floats_shrink_to_minus_one():
    assert shrink(-100.0, lambda x: x < 0) == -1
Esempio n. 13
0
def test_numbers_shrink_to_zero():
    assert shrink(100, lambda x: True) == 0
Esempio n. 14
0
def test_shrink_true_doesnt_drop_to_false_if_conditions_dont_permit():
    assert shrink(True, lambda x: x) is True
Esempio n. 15
0
def test_unhandled_types_are_passed_through():
    def a_function():
        pass

    assert shrink(a_function, lambda x: True) is a_function
Esempio n. 16
0
def test_shrink_false_produces_false():
    assert shrink(False, lambda x: True) is False
Esempio n. 17
0
def test_empty_list_is_unshrunk():
    assert shrink([], lambda x: True) == []
Esempio n. 18
0
def test_strings_shrink_to_empty():
    assert shrink("bees", lambda x: True) == ""
Esempio n. 19
0
def test_lists_shrink_to_the_empty_list():
    assert shrink([1, 2, 3], lambda x: True) == []
Esempio n. 20
0
def test_strings_shrink_by_cutting_off_end():
    assert shrink("bees", lambda x: x.startswith("be")) == "be"
Esempio n. 21
0
def test_empty_dict_is_unshrunk():
    assert shrink({}, lambda x: True) == {}
Esempio n. 22
0
def test_strings_shrink_by_cutting_off_start():
    assert shrink("bees", lambda x: x.endswith("s")) == "s"
Esempio n. 23
0
def test_dicts_are_shrunk_by_dropping_keys():
    assert shrink({"a": "", "c": ""}, lambda x: "a" in x) == {"a": ""}
Esempio n. 24
0
def test_strings_shrink_by_cutting_off_from_both_ends():
    assert shrink("bees", lambda x: "ee" in x) == "ee"