From e79fa857a11da7fc7319a0b83f9e167fd8f90ff9 Mon Sep 17 00:00:00 2001 From: Pat Riehecky Date: Mar 21 2019 16:11:57 +0000 Subject: More testing and a new filename bug to chase --- diff --git a/updateinfo/updateinfo_django_app/fixtures/test_package.yaml b/updateinfo/updateinfo_django_app/fixtures/test_package.yaml index f1d78ea..77ce902 100644 --- a/updateinfo/updateinfo_django_app/fixtures/test_package.yaml +++ b/updateinfo/updateinfo_django_app/fixtures/test_package.yaml @@ -5,6 +5,7 @@ _name: fedora-release _version: 29 _release: 7 + _arch: 1 _srpm: fedora-release-29-7.src.rpm - model: updateinfo_django_app.Package pk: 2 @@ -13,6 +14,7 @@ _name: fedora-release _version: 28 _release: 6 + _arch: 1 _srpm: fedora-release-28-6.src.rpm _src_repo_base: '/' _builddate: 2000-01-01 00:00:00 diff --git a/updateinfo/updateinfo_django_app/models/package.py b/updateinfo/updateinfo_django_app/models/package.py index af200c9..3925b62 100644 --- a/updateinfo/updateinfo_django_app/models/package.py +++ b/updateinfo/updateinfo_django_app/models/package.py @@ -27,6 +27,8 @@ from django.utils.encoding import python_2_unicode_compatible from django.core.exceptions import ObjectDoesNotExist from django.db import models, transaction +from django.db.models.signals import pre_save, post_save +from django.dispatch import receiver ############################################################ ############################################################ @@ -129,19 +131,19 @@ class Package(UpdateinfoPackage, models.Model): '''Replace the default django implementation with a somewhat looser check''' return UpdateinfoPackage.__eq__(self, other) - def save(self, *args, **kwargs): - '''Make sure to populate the derrived filename''' - # calculate the filename and store the result - self.filename = self.filename # this is not a mistaken line, it sanatizes + #def save(self, *args, **kwargs): + # '''Make sure to populate the derrived filename''' + # # calculate the filename and store the result + # self.filename = self.filename # this is not a mistaken line, it sanatizes - with transaction.atomic(): - super(Package, self).save(self, *args, **kwargs) # Call the "real" save() method on this model. + # with transaction.atomic(): + # super(Package, self).save(self, *args, **kwargs) # Call the "real" save() method on this model. - ### XXX FIXME - ### Can I do this with signals? - self.sums._package = self # Now that there is a PRIMARY KEY ensure association + # ### XXX FIXME + # ### Can I do this with signals? + # self.sums._package = self # Now that there is a PRIMARY KEY ensure association - self.sums.save() + # self.sums.save() @property def arch(self): @@ -168,6 +170,9 @@ class Package(UpdateinfoPackage, models.Model): except ObjectDoesNotExist: raise ValueError("Unknown Arch '" + value + "'") + if self.pk: + self.save() + ############################################################ @python_2_unicode_compatible class PackageArch(models.Model): @@ -187,3 +192,22 @@ class PackageArch(models.Model): def __str__(self): '''A simple representation of an Arch''' return self._arch + +############################################################ +@receiver(pre_save, sender=Package) +def _save_package_sanitize_filename(sender, **kwargs): + ''' + Make sure the expected filename matches the stored one + this will also sanity check the model for fields + ''' + kwargs['instance'].filename = kwargs['instance'].filename + +############################################################ +@receiver(post_save, sender=Package) +def _save_package_sum_store_relation(sender, **kwargs): + ''' + Once the package object is saved, you can add + the relationship to the sum store, if necessary. + ''' + kwargs['instance'].sums._package = kwargs['instance'] + kwargs['instance'].sums.save() diff --git a/updateinfo/updateinfo_django_app/tests/test_package.py b/updateinfo/updateinfo_django_app/tests/test_package.py index 4c2d7c6..82a9181 100644 --- a/updateinfo/updateinfo_django_app/tests/test_package.py +++ b/updateinfo/updateinfo_django_app/tests/test_package.py @@ -61,10 +61,44 @@ class UpdateinfoCompatPackageModelTests(TestCase, UpdateinfoPackageTests): result = True self.assertTrue(result, msg='Not the django object') + def test_django_fetch(self): + '''Make sure we can fetch a package from the database''' + + testpkg = self.Package.objects.get(_filename='fedora-release-29-7.noarch.rpm') + + result = False + if testpkg.name == 'fedora-release': + if testpkg.version == '29': + if testpkg.release == '7': + if testpkg.arch == 'noarch': + result = True + + self.assertTrue(result, msg="Couldn't fetch fixture object?") + + def test_django_fetch_with_sum(self): + '''Make sure we can fetch a package/sum from the database''' + + testpkg = self.Package.objects.get(_filename='fedora-release-28-6.noarch.rpm') + + result = False + if testpkg.name == 'fedora-release': + if testpkg.version == '28': + if testpkg.release == '6': + if testpkg.arch == 'noarch': + if testpkg.sums['md5'] == 'd41d8cd98f00b204e9800998ecf8427e': + if testpkg.sums['sha'] == 'da39a3ee5e6b4b0d3255bfef95601890afd80709': + if testpkg.sums['sha1'] == 'da39a3ee5e6b4b0d3255bfef95601890afd80709': + if testpkg.sums['sha256'] == 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': + if testpkg.sums['sha384'] == '38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b': + if testpkg.sums['sha512'] == 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e': + result = True + + self.assertTrue(result, msg="Couldn't fetch fixture object with sums?") + def test_django_save(self): '''Make sure we can save a package/sum to the database''' testpkg = self.Package() - testpkg.name = 'asdf' + testpkg.name = 'savetest' testpkg.version = 'jkl' testpkg.release = 'gh' testpkg.arch = 'src' @@ -73,6 +107,112 @@ class UpdateinfoCompatPackageModelTests(TestCase, UpdateinfoPackageTests): with transaction.atomic(): testpkg.save() + fetchobj = self.Package.objects.get(_filename='savetest-jkl-gh.src.rpm') + + def test_django_changes_filename_arch(self): + '''Make sure when we change the arch, it changes filename''' + testpkg = self.Package() + testpkg.name = 'pkgname' + testpkg.version = 'jkl' + testpkg.release = 'gh' + testpkg.arch = 'src' + + with transaction.atomic(): + testpkg.save() + + changeobj = self.Package.objects.get(_filename='pkgname-jkl-gh.src.rpm') + changeobj.arch = 'noarch' + changeobj.save() + + testobj = self.Package.objects.get(_filename='pkgname-jkl-gh.noarch.rpm') + + result = False + if testobj.name == 'asdf': + if testobj.version == 'jkl': + if testobj.release == 'gh': + if testobj.arch == 'noarch': + result = True + + self.assertTrue(result, msg="Couldn't fetch changed compat object?") + + def test_django_changes_filename_name(self): + '''Make sure when we change the name, it changes filename''' + testpkg = self.Package() + testpkg.name = 'pkgname' + testpkg.version = 'jkl' + testpkg.release = 'gh' + testpkg.arch = 'src' + + with transaction.atomic(): + testpkg.save() + + changeobj = self.Package.objects.get(_filename='pkgname-jkl-gh.src.rpm') + changeobj.name = 'newname' + changeobj.save() + + testobj = self.Package.objects.get(_filename='newname-jkl-gh.src.rpm') + + result = False + if testobj.name == 'newname': + if testobj.version == 'jkl': + if testobj.release == 'gh': + if testobj.arch == 'src': + result = True + + self.assertTrue(result, msg="Couldn't fetch changed compat object?") + + def test_django_changes_filename_version(self): + '''Make sure when we change the version, it changes filename''' + testpkg = self.Package() + testpkg.name = 'pkgname' + testpkg.version = 'jkl' + testpkg.release = 'gh' + testpkg.arch = 'src' + + with transaction.atomic(): + testpkg.save() + + changeobj = self.Package.objects.get(_filename='pkgname-jkl-gh.src.rpm') + changeobj.version = 'otherversion' + changeobj.save() + + testobj = self.Package.objects.get(_filename='pkgname-otherversion-gh.src.rpm') + + result = False + if testobj.name == 'pkgname': + if testobj.version == 'otherversion': + if testobj.release == 'gh': + if testobj.arch == 'src': + result = True + + self.assertTrue(result, msg="Couldn't fetch changed compat object?") + + def test_django_changes_filename_release(self): + '''Make sure when we change the release, it changes filename''' + testpkg = self.Package() + testpkg.name = 'pkgname' + testpkg.version = 'jkl' + testpkg.release = 'gh' + testpkg.arch = 'src' + + with transaction.atomic(): + testpkg.save() + + changeobj = self.Package.objects.get(_filename='pkgname-jkl-gh.src.rpm') + changeobj.release = 'newrelease' + changeobj.save() + + testobj = self.Package.objects.get(_filename='pkgname-jkl-newrelease.src.rpm') + + result = False + if testobj.name == 'pkgname': + if testobj.version == 'jkl': + if testobj.release == 'newrelease': + if testobj.arch == 'src': + result = True + + self.assertTrue(result, msg="Couldn't fetch changed compat object?") + def test_django_bad_arch(self): '''Make sure we can save a package with a bad arch''' testpkg = self.Package() @@ -111,6 +251,34 @@ class UpdateinfoCompatPackageModelTests(TestCase, UpdateinfoPackageTests): self.assertTrue(result, msg="Couldn't fetch saved compat object?") + def test_django_save_add_sums(self): + '''Did the relationship between the sum and this package get setup on add sums?''' + testpkg = self.Package() + testpkg.name = 'asdf' + testpkg.version = 'jkl' + testpkg.release = 'GH' + testpkg.arch = 'src' + + with transaction.atomic(): + testpkg.save() + + fetchobj = self.Package.objects.get(_filename='asdf-jkl-GH.src.rpm') + fetchobj.sums['md5'] = 'd41d8cd98f00b204e9800998ecf8427e' + fetchobj.save() + + newobj = self.Package.objects.get(_filename='asdf-jkl-GH.src.rpm') + + result = False + if newobj.name == 'asdf': + if newobj.version == 'jkl': + if newobj.release == 'GH': + if newobj.arch == 'src': + if newobj.sums['md5'] == 'd41d8cd98f00b204e9800998ecf8427e': + result = True + + self.assertTrue(result, msg="Couldn't fetch saved compat object and sums?") + + def test_django_save_has_sums(self): '''Did the relationship between the sum and this package get setup?''' testpkg = self.Package()