tests.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright 2014 The Distro Tracker Developers
  2. # See the COPYRIGHT file at the top-level directory of this distribution and
  3. # at http://deb.li/DTAuthors
  4. #
  5. # This file is part of Distro Tracker. It is subject to the license terms
  6. # in the LICENSE file found in the top-level directory of this
  7. # distribution and at http://deb.li/DTLicense. No part of Distro Tracker,
  8. # including this file, may be copied, modified, propagated, or distributed
  9. # except according to the terms contained in the LICENSE file.
  10. """
  11. Tests for the :mod:`distro_tracker.derivative` app.
  12. """
  13. from distro_tracker.test import TestCase
  14. from distro_tracker.core.models import Repository
  15. from distro_tracker.core.models import RepositoryRelation
  16. from distro_tracker.core.models import SourcePackageName
  17. from distro_tracker.derivative.utils import compare_repositories
  18. from distro_tracker.derivative.utils import split_version
  19. from distro_tracker.derivative.utils import categorize_version_comparison
  20. class GenerateComparatingListTest(TestCase):
  21. def add_package(self, name, v1, v2):
  22. """Create package with v1 in derivative and v2 in parent."""
  23. pkgname = SourcePackageName.objects.create(name=name)
  24. for version, repo in ((v1, self.derivative_repo),
  25. (v2, self.target_repo)):
  26. if not version:
  27. continue
  28. srcpkg = pkgname.source_package_versions.create(version=version)
  29. srcpkg.repository_entries.create(repository=repo)
  30. def setUp(self):
  31. self.derivative_repo = Repository.objects.create(
  32. name='derivative_repo', shorthand='derivative_repo',
  33. codename='derivative_codename', suite='derivative_suite')
  34. self.target_repo = Repository.objects.create(
  35. name='target_repo', shorthand='target_repo',
  36. codename='initial_codename', suite='initial_suite')
  37. self.relation = RepositoryRelation.objects.create(
  38. repository=self.derivative_repo,
  39. target_repository=self.target_repo,
  40. name='derivative')
  41. self.add_package('pkg1', '1.0.0', '2.0.0')
  42. self.add_package('pkg2', '1.0.0', '1.0.1')
  43. self.add_package('foopkg', '1.0.0', '1.0.1')
  44. def test_compare_repositories_same_categories(self):
  45. # Test that the list is correctly sorted by package name as all packages
  46. # are in the same category
  47. pkglist = compare_repositories(self.derivative_repo, self.target_repo)
  48. self.assertEqual(pkglist[0]['name'], 'foopkg')
  49. self.assertEqual(pkglist[1]['name'], 'pkg1')
  50. self.assertEqual(pkglist[0]['category'], pkglist[2]['category'])
  51. self.assertEqual(pkglist[0]['category'], 'older_version')
  52. def test_compare_repositories_different_categories(self):
  53. # Test that the list is sorted by category then by name
  54. self.add_package('foopkg2', None, '1.0.1')
  55. self.add_package('firstpkg', None, '1.0')
  56. pkglist = compare_repositories(self.derivative_repo, self.target_repo)
  57. self.assertEqual(pkglist[0]['name'], 'foopkg')
  58. self.assertEqual(pkglist[1]['name'], 'pkg1')
  59. self.assertEqual(pkglist[0]['category'], pkglist[2]['category'])
  60. self.assertEqual(pkglist[0]['category'], 'older_version')
  61. self.assertEqual(pkglist[3]['category'], 'missing_pkg')
  62. self.assertEqual(pkglist[4]['name'], 'foopkg2')
  63. class CategorizeVersionComparisonTest(TestCase):
  64. def test_version_equal(self):
  65. a = '2.1.0-1'
  66. b = '2.1.0-1'
  67. self.assertEqual('equal', categorize_version_comparison(a, b))
  68. def test_first_version_is_none(self):
  69. a = None
  70. b = '2.1.0-1'
  71. self.assertEqual('missing_pkg', categorize_version_comparison(a, b))
  72. def test_second_version_is_missing(self):
  73. a = '2.1.0-1'
  74. b = None
  75. self.assertEqual('new_pkg', categorize_version_comparison(a, b))
  76. def test_epoch_is_older(self):
  77. a = '0:2.1.0.0'
  78. b = '1:1.0.1-4'
  79. self.assertEqual('older_version', categorize_version_comparison(a, b))
  80. def test_older_version(self):
  81. a = '1:2.1.3-4'
  82. b = '1:2.1.6'
  83. self.assertEqual('older_version', categorize_version_comparison(a, b))
  84. def test_newer_version(self):
  85. a = '1:2.1.6'
  86. b = '1:2.1.3-4'
  87. self.assertEqual('newer_version', categorize_version_comparison(a, b))
  88. def test_older_revision(self):
  89. a = '1:2.1.3-4'
  90. b = '1:2.1.3-6'
  91. self.assertEqual('older_revision', categorize_version_comparison(a, b))
  92. def test_newer_revision(self):
  93. a = '2.1.3-8'
  94. b = '2.1.3-6'
  95. self.assertEqual('newer_revision', categorize_version_comparison(a, b))
  96. class SplitVersionTest(TestCase):
  97. def test_with_epoch_and_revision(self):
  98. version = '4:9.3.4-1-12'
  99. epoch, upstream, revision = split_version(version)
  100. self.assertEqual(epoch, '4')
  101. self.assertEqual(upstream, '9.3.4-1')
  102. self.assertEqual(revision, '12')
  103. def test_without_epoch_but_revision(self):
  104. version = '9.3.4-12'
  105. epoch, upstream, revision = split_version(version)
  106. self.assertEqual(epoch, '~')
  107. self.assertEqual(upstream, '9.3.4')
  108. self.assertEqual(revision, '12')
  109. def test_without_epoch_and_revision(self):
  110. version = '9.3.4'
  111. epoch, upstream, revision = split_version(version)
  112. self.assertEqual(epoch, '~')
  113. self.assertEqual(upstream, '9.3.4')
  114. self.assertEqual(revision, '~')