diff options
author | Tim Harder <radhermit@gmail.com> | 2021-03-04 13:41:41 -0700 |
---|---|---|
committer | Tim Harder <radhermit@gmail.com> | 2021-03-04 13:44:52 -0700 |
commit | 00684d51abf632310b58e5ed56b0ed61d9eeeb3d (patch) | |
tree | 19e965b2eb1ba5bc87ee5227dbb571a243a0ac56 /tests/package | |
parent | coverage: minor config update (diff) | |
download | pkgcore-00684d51abf632310b58e5ed56b0ed61d9eeeb3d.tar.gz pkgcore-00684d51abf632310b58e5ed56b0ed61d9eeeb3d.tar.bz2 pkgcore-00684d51abf632310b58e5ed56b0ed61d9eeeb3d.zip |
tests: move to repo root dir
Diffstat (limited to 'tests/package')
-rw-r--r-- | tests/package/__init__.py | 0 | ||||
-rw-r--r-- | tests/package/test_base.py | 81 | ||||
-rw-r--r-- | tests/package/test_metadata.py | 47 | ||||
-rw-r--r-- | tests/package/test_mutated.py | 62 |
4 files changed, 190 insertions, 0 deletions
diff --git a/tests/package/__init__.py b/tests/package/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/package/__init__.py diff --git a/tests/package/test_base.py b/tests/package/test_base.py new file mode 100644 index 000000000..63dab2f3c --- /dev/null +++ b/tests/package/test_base.py @@ -0,0 +1,81 @@ +from functools import partial + +from snakeoil.test import TestCase + +from pkgcore.package import base +from pkgcore.test import malleable_obj + + +def fake_pkg(cat='dev-util', pkg='bsdiff', ver='1.0', **attrs): + d = {} + d['category'] = cat + d['pkg'] = pkg + d['ver'] = ver + d['key'] = f"{cat}/{pkg}" + d["cpvstr"] = f"{cat}/{pkg}-{ver}" + d['built'] = False + d.update(attrs) + return malleable_obj(**d) + + +class mixin: + + def mk_inst(self): + raise NotImplementedError(self, "mk_inst") + + def test_setattr(self): + self.assertRaises(AttributeError, setattr, + self.mk_inst(), "asdf", 1) + + def test_delattr(self): + self.assertRaises(AttributeError, delattr, + self.mk_inst(), "asdf") + + +class TestBasePkg(mixin, TestCase): + + mk_inst = kls = staticmethod(base.base) + + def test_properties(self): + o = self.kls() + for f in ("versioned_atom", "unversioned_atom"): + self.assertRaises(NotImplementedError, getattr, o, f) + self.assertRaises(AttributeError, o.__setattr__, f, "a") + self.assertRaises(AttributeError, o.__delattr__, f) + + def test_getattr(self): + class Class(base.base): + __slotting_intentionally_disabled__ = True + _get_attr = {str(x): partial((lambda a, s: a), x) + for x in range(10)} + _get_attr["a"] = lambda s:"foo" + __getattr__ = base.dynamic_getattr_dict + + o = Class() + for x in range(10): + self.assertEqual(getattr(o, str(x)), x) + self.assertEqual(o.a, "foo") + self.assertEqual(self.mk_inst().built, False) + + +class TestWrapper(mixin, TestCase): + + kls = base.wrapper + + def mk_inst(self, overrides=None, **kwds): + kls = self.kls + if overrides: + class kls(self.kls): + locals().update(overrides) + __slots__ = () + + pkg = fake_pkg(**kwds) + return kls(pkg) + + def test_built_passthru(self): + # test pass thrus + self.assertEqual(self.mk_inst().built, False) + self.assertEqual(self.mk_inst(built=True).built, True) + # verify that wrapping will override it + self.assertEqual(self.mk_inst(overrides={'built':False}, + built=True).built, False) diff --git a/tests/package/test_metadata.py b/tests/package/test_metadata.py new file mode 100644 index 000000000..a697d4118 --- /dev/null +++ b/tests/package/test_metadata.py @@ -0,0 +1,47 @@ +from snakeoil.test import TestCase + +from pkgcore.package import base, metadata + + +def make_pkg_kls(attrs=(), callbacks={}): + + class simple_pkg(base.base): + _get_attr = callbacks + __slots__ = ("_args", "_kwds", "_data", "_fetch_called",) + tuple(attrs) + + def __init__(self, *args, **kwds): + self._args = args + self._data = kwds.pop("data", {}) + self._kwds = kwds + self._fetch_called = False + + __setattr__ = object.__setattr__ + + class metadata_pkg(metadata.DeriveMetadataKls(simple_pkg)): + + __slots__ = () + def _fetch_metadata(self): + self._fetch_called = True + return self._data + + return metadata_pkg + + +class TestMetadataPackage(TestCase): + + def test_init(self): + class repo: + _parent_repo = "foon" + + kls = make_pkg_kls() + o = kls(repo, "monkeys", dar=1) + self.assertEqual(o._parent, repo) + self.assertEqual(o.repo, "foon") + self.assertEqual(o._args, ("monkeys",)) + self.assertEqual(o._kwds, {"dar":1}) + self.assertEqual(o._fetch_called, False) + + def test_getdata(self): + kls = make_pkg_kls() + o = kls(None, data={'a': 'b'}) + self.assertEqual(o.data, {'a': 'b'}) diff --git a/tests/package/test_mutated.py b/tests/package/test_mutated.py new file mode 100644 index 000000000..ba0c3d1be --- /dev/null +++ b/tests/package/test_mutated.py @@ -0,0 +1,62 @@ +from functools import partial + +from snakeoil.compatibility import cmp +from snakeoil.klass import inject_richcmp_methods_from_cmp +from snakeoil.test import TestCase + +from pkgcore.package.base import base, dynamic_getattr_dict +from pkgcore.package.mutated import MutatedPkg + + +def passthru(val, self): + return val + +class FakePkg(base): + + # XXX why isn't this using existing classes? + __slotting_intentionally_disabled__ = True + + def __init__(self, pkg, ver, data): + base.__init__(self) + self.pkg = pkg + self.ver = ver + self._get_attr = {k: partial(passthru, v) for k, v in data.items()} + + # disable protection. don't want it here + __setattr__ = object.__setattr__ + __getattr__ = dynamic_getattr_dict + + def __cmp__(self, other): + return cmp(self.ver, other.ver) + + inject_richcmp_methods_from_cmp(locals()) + + +class TestMutatedPkg(TestCase): + + def make_fakepkg(self, pkg="dar", ver=1, data=None): + if data is None: + data = {"a":1} + return FakePkg(pkg, ver, data) + + def test_raw_pkg(self): + pkg = self.make_fakepkg() + self.assertIdentical(MutatedPkg(pkg, {})._raw_pkg, pkg) + + def test_cmp(self): + pkg1 = self.make_fakepkg() + pkg2 = self.make_fakepkg(ver=2) + mpkg1 = MutatedPkg(pkg1, {}) + mpkg2 = MutatedPkg(pkg2, {}) + + for lpkg in (pkg1, mpkg1): + self.assertTrue(cmp(lpkg, mpkg2) < 0) + self.assertTrue(cmp(mpkg2, lpkg) > 0) + self.assertEqual(mpkg1, mpkg1) + self.assertEqual(pkg1, mpkg1) + + def test_getattr(self): + pkg = self.make_fakepkg() + self.assertEqual(MutatedPkg(pkg, {}).a, 1) + self.assertEqual(MutatedPkg(pkg, {"a":2}).a, 2) + self.assertRaises(AttributeError, MutatedPkg(pkg, {}).__getattr__, "b") |