Example #1
0
def test_they_can_be_used_as_a_dict_key():
    lookup = {ReadOnce("a"): "success", ReadOnce("b"): "another"}

    assert lookup[ReadOnce("a")] == "success"
Example #2
0
def test_they_can_be_hashed_if_the_inner_type_can_be():
    a = ReadOnce("the same")
    b = ReadOnce("the same")

    assert hash(a) == hash(b)
Example #3
0
def test_they_cant_be_compared_to_other_types():
    a = ReadOnce("the same")
    b = "the same"
    with pytest.raises(RuntimeError):
        a != b
Example #4
0
def test_they_are_equal_if_the_contents_is():
    a = ReadOnce("the same")
    b = ReadOnce("the same")
    assert a == b
Example #5
0
def test_it_returns_the_value_stored():
    something = ReadOnce("hello - only once")
    assert something.get_contents() == "hello - only once"
Example #6
0
def test_they_are_not_equal_if_the_contents_isnt():
    a = ReadOnce("the same")
    b = ReadOnce("but different")
    assert a != b
Example #7
0
def test_the_repr_it_returns_indicates_the_type():
    something = ReadOnce("Hello!")
    assert repr(something) == "ReadOnce<str>"
Example #8
0
def test_they_cannot_be_pickled():
    contents = ReadOnce("the actual value")
    with pytest.raises(RuntimeError):
        pickle.dumps(contents)
Example #9
0
def test_turning_it_into_a_string_only_works_once():
    something = ReadOnce("the actual value")
    _first_usage = str(something)
    with pytest.raises(ReadTwiceError):
        _second_usage = str(something)
Example #10
0
def test_it_can_be_used_in_f_strings():
    something = ReadOnce("Hello!")
    assert f"you said: {something}" == "you said: Hello!"
Example #11
0
def test_turning_it_into_a_string_turns_the_underlying_value_into_a_string():
    something = ReadOnce("the actual value")
    assert str(something) == "the actual value"
Example #12
0
def test_an_isinstance_equiv_is_provided():
    assert ReadOnce("hello - only once").isinstance(str)
    assert ReadOnce(5).isinstance(object)
Example #13
0
def test_type_information_can_still_be_accessed():
    assert ReadOnce("hello - only once").type == str
    assert ReadOnce(5).type == int
Example #14
0
def test_the_exception_message_does_not_contain_the_value():
    something = ReadOnce("secret")
    _first = something.get_contents()
    with pytest.raises(ReadTwiceError) as error:
        _second = something.get_contents()
    assert "secret" not in str(error.value)
Example #15
0
def test_it_raise_an_exception_the_second_time_its_read():
    something = ReadOnce("hello - only once")
    _first = something.get_contents()
    with pytest.raises(ReadTwiceError):
        _second = something.get_contents()