Commit b410e4b5 authored by Raphaël Hertzog's avatar Raphaël Hertzog

test: new add_package_data() helper method

parent 482df1a5
......@@ -32,6 +32,8 @@ from distro_tracker.core.models import (
Architecture,
BinaryPackageName,
ContributorName,
PackageData,
PackageName,
Repository,
SourcePackage,
SourcePackageName,
......@@ -161,6 +163,7 @@ class DatabaseMixin(object):
- binary_packages (list of package names)
- repository (shorthand of a repository)
- repositories (list of repositories' shorthand)
- data (dict used to generate associated PackageData)
If the shorthand of the requested repository is 'default', then
its default field will be set to True.
......@@ -209,6 +212,9 @@ class DatabaseMixin(object):
for repo_shorthand in kwargs.get('repositories', []):
self.add_to_repository(srcpkg, repo_shorthand)
if 'data' in kwargs:
self.add_package_data(srcpkg.source_package_name, **kwargs['data'])
srcpkg.save()
return srcpkg
......@@ -253,6 +259,22 @@ class DatabaseMixin(object):
return srcpkg.repository_entries.filter(
repository__shorthand=shorthand).delete()[0]
def add_package_data(self, pkgname, **kwargs):
"""
Creates PackageData objects associated to the package indicated
in pkgname. Each named parameter results in PackageData instance
with the `key` being the name of the parameter and the `value`
being the value of the named parameter.
:param pkgname: the name of the package to which we want to associate
data
:type pkgname: `str` or :class:`~distro_tracker.core.models.PackageName`
"""
if not isinstance(pkgname, PackageName):
pkgname, _ = PackageName.objects.get_or_create(name=str(pkgname))
for key, value in kwargs.items():
PackageData.objects.create(package=pkgname, key=key, value=value)
class SimpleTestCase(TempDirsMixin, TestCaseHelpersMixin,
django.test.SimpleTestCase):
......
......@@ -17,7 +17,12 @@ import os.path
from django.conf import settings
from distro_tracker.core.models import PackageName, Repository, SourcePackage
from distro_tracker.core.models import (
PackageData,
PackageName,
Repository,
SourcePackage
)
from distro_tracker.test import (
SimpleTestCase,
TempDirsMixin,
......@@ -195,6 +200,21 @@ class DatabaseMixinTests(object):
repository = Repository.objects.get(shorthand='foobar')
self.assertFalse(repository.default)
def test_create_source_package_with_data(self):
data = {
'key1': {'sample': 'data'},
'key2': ['sample', 'data']
}
srcpkg = self.create_source_package(data=data)
pkgdata1 = PackageData.objects.get(package__name=srcpkg.name,
key='key1')
self.assertEqual(pkgdata1.value, data['key1'])
pkgdata2 = PackageData.objects.get(package__name=srcpkg.name,
key='key2')
self.assertEqual(pkgdata2.value, data['key2'])
def test_add_to_repository_creates_repository(self):
srcpkg = self.create_source_package()
......@@ -228,6 +248,26 @@ class DatabaseMixinTests(object):
self.assertEqual(result, 0)
def test_add_package_data_with_name(self):
data1 = {'sample': 'data'}
data2 = ['sample', 'data']
self.add_package_data('dpkg', key1=data1, key2=data2)
pkgdata1 = PackageData.objects.get(package__name='dpkg', key='key1')
self.assertEqual(pkgdata1.value, data1)
pkgdata2 = PackageData.objects.get(package__name='dpkg', key='key2')
self.assertEqual(pkgdata2.value, data2)
def test_add_package_data_with_PackageName(self):
pkgname, _ = PackageName.objects.get_or_create(name='sample')
data = ['foo']
self.add_package_data(pkgname, key1=data)
pkgdata = PackageData.objects.get(package=pkgname, key='key1')
self.assertEqual(pkgdata.value, data)
class TempDirsOnSimpleTestCase(TempDirsTests, TestCaseHelpersTests,
SimpleTestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment