Example #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
Example #2
0
def test_empty_string_is_unshrunk():
    assert shrink("", lambda x: True) == ""
Example #3
0
def test_strings_shrink_to_a_where_possible():
    assert shrink("bees", lambda x: len(x) >= 2) == "aa"
Example #4
0
def test_unshrinkable_strings_are_not_shrunk():
    assert shrink("bees", lambda x: x == "bees") == "bees"
Example #5
0
def test_dicts_shrink_values():
    assert shrink({"a": 100}, lambda x: x.get("a", 0) > 0) == {"a": 1}
Example #6
0
def test_dicts_shrink_to_the_empty_dict():
    assert shrink({"a": "b"}, lambda x: True) == {}
Example #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])
Example #8
0
def test_lists_shrunk_by_cutting_off_from_both_ends():
    assert shrink(list("bees"), lambda x: "ee" in "".join(x)) == ["e", "e"]
Example #9
0
def test_numbers_do_not_shrink_if_they_cannot():
    assert shrink(100, lambda x: x == 100) == 100
Example #10
0
def test_positive_floats_shrink_to_one():
    assert shrink(100.0, lambda x: x > 0) == 1
Example #11
0
def test_shrink_none_produces_none():
    assert shrink(None, lambda x: True) is None
Example #12
0
def test_negative_floats_shrink_to_minus_one():
    assert shrink(-100.0, lambda x: x < 0) == -1
Example #13
0
def test_numbers_shrink_to_zero():
    assert shrink(100, lambda x: True) == 0
Example #14
0
def test_shrink_true_doesnt_drop_to_false_if_conditions_dont_permit():
    assert shrink(True, lambda x: x) is True
Example #15
0
def test_unhandled_types_are_passed_through():
    def a_function():
        pass

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