Exemple #1
0
    def test_key_len(self):
        ''' Test the length of a `Key`, which should only be 0 in the case of an incomplete key. '''

        k, nk = Key("Sample"), Key("Sample", "sample")

        # a key with no ID should evaluate to 0 via len()
        self.assertEqual(len(k), 0)
        self.assertEqual(len(nk), 1)
Exemple #2
0
    def test_key_nonzero(self):
        ''' Test nonzero functionality in a key. '''

        # sample zero key and nonzero key
        k, nk = Key("Sample"), Key("Sample", "sample")

        # key with no ID should evaluate to falsy
        self.assertTrue(nk)
        self.assertTrue(not k)
Exemple #3
0
  def test_key_flatten(self):

    """ Test flattening a Key into a raw iterable """

    # sample key
    k = Key("Sample", "sample")

    # flatten in & out
    self.assertEqual(Key(raw=k.flatten()), k)
    self.assertEqual(Key.from_raw(k.flatten()), k)
Exemple #4
0
  def test_urlsafe_key_format(self):

    """ Test constructing a key from its encoded form """

    # sample key
    k = Key("Sample", "sample")

    # urlsafe in & out
    self.assertEqual(Key(urlsafe=k.urlsafe()), k)
    self.assertEqual(Key.from_urlsafe(k.urlsafe()), k)
Exemple #5
0
    def test_abstract_key(self):
        ''' Make sure `model.AbstractKey` works abstractly. '''

        # should not be able to instantiate `AbstractKey`
        with self.assertRaises(exceptions.AbstractConstructionFailure):
            AbstractKey()

        self.assertIsInstance(Key(), AbstractKey)
Exemple #6
0
    def test_raw_key_format(self):
        ''' Try constructing a key from a raw iterable. '''

        # sample key
        k = Key("Sample", "sample")

        # tupled raw
        self.assertEqual(Key(raw=k.flatten()), k)
        self.assertEqual(Key.from_raw(k.flatten()), k)

        # joined raw
        joined, struct = k.flatten(True)
        self.assertEqual(Key(raw=joined), k)
        self.assertEqual(Key.from_raw(joined), k)
Exemple #7
0
    def test_construct_key(self):
        ''' Try constructing a key manually. '''

        # test kinded empty key
        k = Key("TestKind")
        self.assertEqual(k.kind, "TestKind")

        # test kinded ID'd key
        k = Key("TestKind", "sample")
        self.assertEqual(k.kind, "TestKind")
        self.assertEqual(k.id, "sample")

        # test parented, kinded ID'd key
        pk = Key("TestParentKind", "parent")
        k = Key("TestKind", "child", parent=pk)
        self.assertEqual(k.kind, "TestKind")
        self.assertEqual(k.id, "child")
        self.assertEqual(k.parent, pk)

        # make sure basic properties are ok
        self.assertEqual(k.__slots__, set())
        self.assertEqual(k.__class__.__name__, "Key")
        self.assertEqual(k.__class__.__slots__, set())
Exemple #8
0
    def test_key_flatten(self):
        ''' Try flattening a Key into a raw iterable. '''

        # sample key
        k = Key("Sample", "sample")

        # flatten in & out
        self.assertEqual(Key(raw=k.flatten()), k)
        self.assertEqual(Key.from_raw(k.flatten()), k)
Exemple #9
0
    def test_urlsafe_key_format(self):
        ''' Try constructing a key from its URL-encoded form. '''

        # sample key
        k = Key("Sample", "sample")

        # urlsafe in & out
        self.assertEqual(Key(urlsafe=k.urlsafe()), k)
        self.assertEqual(Key.from_urlsafe(k.urlsafe()), k)
Exemple #10
0
    def test_key_stringify(self):
        ''' Test the string representation of a `Key` object. '''

        # build and stringify key
        k = Key("SampleKind", "sample_id")
        x = str(k)

        # make sure the kind is somewhere
        self.assertTrue(("kind" in x))
        self.assertTrue(("SampleKind" in x))

        # make sure the ID is somewhere
        self.assertTrue(("id" in x))
        self.assertTrue(("sample_id" in x))

        # make sure the key class is somewhere
        self.assertTrue(('Key' in x))
Exemple #11
0
    def test_key_inheritance(self):
        ''' Make sure there's a proper inheritance structure for `model.Key`. '''

        # test basic key inheritance
        k = Key("TestKind", "sample")

        # test basic inheritance
        self.assertIsInstance(k, Key)
        self.assertIsInstance(k, AbstractKey)
        self.assertIsInstance(k, object)

        # test class inheritance
        self.assertTrue(issubclass(Key, AbstractKey))
        self.assertTrue(AbstractKey in Key.__bases__)
        self.assertIsInstance(Key, AbstractKey.__metaclass__)

        # check `AbstractKey` inheritance
        self.assertTrue(type(k) == Key)
        self.assertTrue(issubclass(Key, object))
Exemple #12
0
  def test_raw_key_format(self):

    """ Test constructing a key from a raw iterable """

    # sample key
    k = Key("Sample", "sample")

    # tupled raw
    self.assertEqual(Key(raw=k.flatten()), k)
    self.assertEqual(Key.from_raw(k.flatten()), k)

    # joined raw
    joined, struct = k.flatten(True)
    self.assertEqual(Key(raw=joined), k)
    self.assertEqual(Key.from_raw(joined), k)