Source

onpython3yet / onpython3yet / packages / tests / test_models.py

Full commit

import unittest

from packages.models import Package, PackageRequirement, MostRequiredPackages


class TestPackages(unittest.TestCase):

    def setUp(self):

        self.foo = Package(
            name="foo",
            current_version="1.0"
        )
        self.foo.save()

        self.bar = Package(
            name="bar",
            current_version="1.0"
        )
        self.bar.save()

        self.baz = Package(
            name="baz",
            current_version="1.0"
        )
        self.baz.save()

        # foo depends on bar:
        r = PackageRequirement(
            package=self.foo,
            required_package=self.bar
        )
        r.save()

        # foo depends on baz:
        r = PackageRequirement(
            package=self.foo,
            required_package=self.baz
        )
        r.save()

        self.fred = Package(
            name="fred",
            current_version="0.1"
        )
        self.fred.save()

        # fred also depends on baz:
        r = PackageRequirement(
            package=self.fred,
            required_package=self.baz
        )
        r.save()

    def tearDown(self):
        for pr in PackageRequirement.objects.all():
            pr.delete()
        for p in Package.objects.all():
            p.delete()

    def test_requirements(self):

        # check requirements:
        self.assertEqual(sorted([r.required_package.name for r in
                                 self.foo.requirements.all()]),
                         ['bar', 'baz'])
        self.assertEqual(sorted([r.required_package.name for r in
                                 self.fred.requirements.all()]),
                         ['baz'])

        # check who depends on this:
        self.assertEqual(sorted([r.package.name for r in
                                 self.baz.dependents.all()]),
                         ['foo', 'fred'])

    def test_requirement_counts(self):

        pkg_by_count = [o for o in MostRequiredPackages.objects.all()]

        self.assertEqual(pkg_by_count[0].name, 'baz')
        self.assertEqual(pkg_by_count[0].required_count, 2)

        self.assertEqual(pkg_by_count[1].name, 'bar')
        self.assertEqual(pkg_by_count[1].required_count, 1)

        # requirement count should only include non-python3 modules:
        self.baz.supports_python_3 = True
        self.baz.save()

        # now baz should not show up anymore:
        pkg_by_count = [o.name for o in MostRequiredPackages.objects.all()]
        self.assertEqual(pkg_by_count, [u'bar', u'foo', u'fred'])