def test_root_top_level(): key = key_module.Key("This", "key") assert key.root() is key
def test_constructor_with_parent_bad_type(self): parent = unittest.mock.sentinel.parent with pytest.raises(exceptions.BadValueError): key_module.Key("Zip", 10, parent=parent)
def test_invalid_argument_combination(): with pytest.raises(TypeError): key_module.Key(flat=["a", "b"], urlsafe=b"foo")
def test_to_old_key(): key = key_module.Key("a", "b") with pytest.raises(NotImplementedError): key.to_old_key()
def test_constructor_with_flat_and_pairs(): with pytest.raises(TypeError): key_module.Key(pairs=[("Kind", 1)], flat=["Kind", 1])
def test_urlsafe(): key = key_module.Key("d", None, app="f") assert key.urlsafe() == b"agFmcgULEgFkDA"
def test_to_legacy_urlsafe_name(): key = key_module.Key("d", "x", app="f") assert ( key.to_legacy_urlsafe(location_prefix="s~") == b"agNzfmZyCAsSAWQiAXgM" )
def test_pairs(): key = key_module.Key("a", "b") assert key.pairs() == (("a", "b"),)
def test_pairs_partial_key(): key = key_module.Key("This", "key", "that", None) assert key.pairs() == (("This", "key"), ("that", None))
def test_string_id(): pairs = (("x", "x"), (11, None), (None, None)) for id_or_name, expected in pairs: key = key_module.Key("Kind", id_or_name) assert key.string_id() == expected
def test_integer_id(): pairs = (("x", None), (11, 11), (None, None)) for id_or_name, expected in pairs: key = key_module.Key("Kind", id_or_name) assert key.integer_id() == expected
def test_id(): for id_or_name in ("x", 11, None): key = key_module.Key("Kind", id_or_name) assert key.id() == id_or_name
def test_app(): app = "s~example" key = key_module.Key("X", 100, app=app) assert key.app() != app assert key.app() == app[2:]
def test_namespace(): namespace = "my-space" key = key_module.Key("abc", 1, namespace=namespace) assert key.namespace() == namespace
def test_reference_bad_integer_id(): for id_ in (-10, 0, 2 ** 64): key = key_module.Key("kind", id_, app="app") with pytest.raises(ValueError): key.reference()
def test_flat(): key = key_module.Key("This", "key") assert key.flat() == ("This", "key")
def test_serialized(): key = key_module.Key("a", 108, app="c") assert key.serialized() == b"j\x01cr\x07\x0b\x12\x01a\x18l\x0c"
def test_flat_partial_key(): key = key_module.Key("Kind", None) assert key.flat() == ("Kind", None)
def test_to_legacy_urlsafe(): key = key_module.Key("d", 123, app="f") assert ( key.to_legacy_urlsafe(location_prefix="s~") == b"agNzfmZyBwsSAWQYeww" )
def test_kind(): key = key_module.Key("This", "key") assert key.kind() == "This" key = key_module.Key("a", "b", "c", "d") assert key.kind() == "c"
def test_constructor_empty_path(): with pytest.raises(TypeError): key_module.Key(pairs=())
def test_reference(): key = key_module.Key("This", "key", app="fire") assert key.reference() == make_reference( path=({"type": "This", "name": "key"},), app="fire", namespace=None )
def test_constructor_invalid_id_type(): with pytest.raises(TypeError): key_module.Key("Kind", object()) with pytest.raises(exceptions.BadArgumentError): key_module.Key("Kind", None, "Also", 10)
def test_reference_cached(): key = key_module.Key("This", "key") key._reference = mock.sentinel.reference assert key.reference() is mock.sentinel.reference
def test_constructor_with_project_and_app(): with pytest.raises(TypeError): key_module.Key("Kind", 10, project="foo", app="bar")
def test_reference_bad_kind(): too_long = "a" * (key_module._MAX_KEYPART_BYTES + 1) for kind in ("", too_long): key = key_module.Key(kind, "key", app="app") with pytest.raises(ValueError): key.reference()
def test_constructor_insufficient_args(): with pytest.raises(TypeError): key_module.Key(app="foo")
def test_reference_bad_string_id(): too_long = "a" * (key_module._MAX_KEYPART_BYTES + 1) for id_ in ("", too_long): key = key_module.Key("kind", id_, app="app") with pytest.raises(ValueError): key.reference()
def test___repr__defaults(): key = key_module.Key("a", "b") assert repr(key) == "Key('a', 'b')" assert str(key) == "Key('a', 'b')"
def test_root(): key = key_module.Key("a", "b", "c", "d") root = key.root() assert root._key == key._key.parent assert root._reference is None