Permalink
Browse files

migrate test/transactiontests.py to tests/.

  • Loading branch information...
Ales Kozumplik
Ales Kozumplik committed Apr 20, 2012
1 parent 8de0dae commit 1a3f50ba67ecbb211f291bfbd79f27e36853885e
Showing with 115 additions and 185 deletions.
  1. +0 −165 test/transactiontests.py
  2. +40 −20 tests/base.py
  3. +75 −0 tests/test_transaction.py
View
@@ -1,165 +0,0 @@
-from yum.constants import *
-import unittest
-import settestpath
-from testbase import *
-
-from yum.transactioninfo import TransactionData
-
-class TransactionDataTests(unittest.TestCase):
- ''' Test cases for yum.transactioninfo.TransactionData'''
-
- def setUp(self):
- self.tsInfo = TransactionData()
- self.rpmdb = packageSack.PackageSack()
- self.pkgSack = packageSack.PackageSack()
- self.tsInfo.setDatabases(self.rpmdb, self.pkgSack)
- self.foo1 = FakePackage('foo', '1', '0', '0', 'noarch')
- self.foo2 = FakePackage('foo', '2', '0', '0', 'i386')
- self.bar1 = FakePackage('bar', '1', '0', '0', 'i386')
- self.bar2 = FakePackage('bar', '2', '0', '0', 'noarch')
- self.foogui1 = FakePackage('foogui', '1', '0', '0', 'x86_64')
- self.foogui2 = FakePackage('foogui', '2', '0', '0', 'noarch')
-
-
- def testLenght(self):
- ''' test __len__ method '''
- self.tsInfo.addInstall(self.foo1)
- self.tsInfo.addUpdate(self.foogui2,self.foogui1)
- self.dumpTsInfo()
- self.assertEqual(len(self.tsInfo),3)
-
- def testAddTheSameTwice(self):
- ''' test add the same twice '''
- txmbr1 = self.tsInfo.addInstall(self.foo1)
- txmbr2 = self.tsInfo.addInstall(self.foo2)
- txmbr3 = self.tsInfo.addInstall(self.foo1)
- self.dumpTsInfo()
- self.assertEqual(len(self.tsInfo),2) # only 2 members
- # self.assertEquals(txmbr3, txmbr1) # 1 & 3 should be equal
-
- def testExists(self):
- ''' test exists method '''
- self.tsInfo.addInstall(self.foo1)
- self.tsInfo.addInstall(self.bar1)
- self.dumpTsInfo()
- self.assertEqual(self.tsInfo.exists(self.foo1.pkgtup),1)
- self.assertEqual(self.tsInfo.exists(self.bar1.pkgtup),1)
- self.assertEqual(self.tsInfo.exists(self.foogui1.pkgtup),0)
-
- def testRemove(self):
- ''' test remove from transaction '''
- txmbr = self.tsInfo.addInstall(self.foo1)
- self.tsInfo.addInstall(self.bar2)
- self.tsInfo.remove(self.bar2.pkgtup)
- self.dumpTsInfo()
- self.assertResult([txmbr])
-
- def testAddToTransaction(self):
- ''' test adding basic adding to Transaction '''
- txmbr1 = self.tsInfo.addInstall(self.foo1)
- txmbr2 = self.tsInfo.addUpdate(self.foogui2,self.foogui1)
- txmbr3 = self.tsInfo.getMembers(self.foogui1.pkgtup)[0]
- self.dumpTsInfo()
- self.assertResult([txmbr1,txmbr2,txmbr3])
-
- def testGetFromTransaction(self):
- ''' test getting from Transaction '''
- self.tsInfo.addInstall(self.foo2)
- self.tsInfo.addObsoleting(self.bar2,self.bar1)
- self.tsInfo.addUpdate(self.foogui2,self.foogui1)
- self.tsInfo.addErase(self.foo1)
- self.dumpTsInfo()
- # get install member foo-2.0 - u
- txmbr = self.tsInfo.getMembers(self.foo2.pkgtup)[0]
- self.assertEqual(txmbr.po, self.foo2)
- self.assertEqual(txmbr.current_state, TS_AVAILABLE)
- self.assertEqual(txmbr.output_state, TS_INSTALL)
- self.assertEqual(txmbr.po.state, TS_INSTALL)
- self.assertEqual(txmbr.ts_state, 'u')
- # get erase member foo-1.0 - e
- txmbr = self.tsInfo.getMembers(self.foo1.pkgtup)[0]
- self.assertEqual(txmbr.po, self.foo1)
- self.assertEqual(txmbr.current_state, TS_INSTALL)
- self.assertEqual(txmbr.output_state, TS_ERASE)
- self.assertEqual(txmbr.po.state, TS_INSTALL)
- self.assertEqual(txmbr.ts_state, 'e')
- # get Obsoleting
- txmbr = self.tsInfo.getMembers(self.bar2.pkgtup)[0]
- self.assertEqual(txmbr.po, self.bar2)
- self.assertEqual(txmbr.current_state, TS_AVAILABLE)
- self.assertEqual(txmbr.output_state, TS_OBSOLETING)
- self.assertEqual(txmbr.po.state, TS_OBSOLETING)
- self.assertEqual(txmbr.ts_state, 'u')
- self.assertEqual(txmbr.relatedto, [(self.bar1, 'obsoletes')])
- self.assertEqual(txmbr.obsoletes, [self.bar1])
- # get update member
- txmbr = self.tsInfo.getMembers(self.foogui2.pkgtup)[0]
- self.assertEqual(txmbr.po, self.foogui2)
- self.assertEqual(txmbr.current_state, TS_AVAILABLE)
- self.assertEqual(txmbr.output_state, TS_UPDATE)
- self.assertEqual(txmbr.po.state, TS_UPDATE)
- self.assertEqual(txmbr.ts_state, 'u')
- self.assertEqual(txmbr.relatedto, [(self.foogui1, 'updates')])
- self.assertEqual(txmbr.updates, [self.foogui1])
-
-
- def testAddUpdatesAndObsoletes(self):
- ''' test addUpdated,addObsoleted'''
- txmbr1 = self.tsInfo.addUpdated(self.foo1,self.foo2)
- txmbr2 = self.tsInfo.addObsoleted(self.bar1,self.bar2)
- self.dumpTsInfo()
- self.assertResult([txmbr1,txmbr2])
- txmbr = self.tsInfo.getMembersWithState(output_states=[TS_UPDATED])[0]
- self.assertEqual(txmbr.po, self.foo1)
- txmbr = self.tsInfo.getMembersWithState(output_states=[TS_OBSOLETED])[0]
- self.assertEqual(txmbr.po, self.bar1)
-
-
- def testMatchNaevr(self):
- ''' test MatchNaevr '''
- self.tsInfo.addInstall(self.foo1)
- self.tsInfo.addObsoleting(self.bar2,self.bar1)
- self.tsInfo.addUpdate(self.foogui2,self.foogui1)
- self.dumpTsInfo()
- res = self.tsInfo.matchNaevr( name='foogui')
- self.assertEqual(len(res),2) # foogui-1.0, foogui-2.0
- res = self.tsInfo.matchNaevr( arch='noarch')
- self.assertEqual(len(res),3) # foo-1.0, bar-2.0, foogui-2.0
- res = self.tsInfo.matchNaevr( epoch='0',ver='1', rel='0')
- self.assertEqual(len(res),2) # foo-1.0, foogui-1.0
-
- def testgetMembersWithState(self):
- ''' test getMembersWithState'''
- self.tsInfo.addInstall(self.foo1)
- self.tsInfo.addObsoleting(self.bar2,self.bar1)
- self.tsInfo.addUpdate(self.foogui2,self.foogui1)
- self.dumpTsInfo()
- res = self.tsInfo.getMembersWithState(output_states=[TS_INSTALL,TS_UPDATE])
- self.assertEqual(len(res),2) # foo-1.0, bar-2.0
- res = self.tsInfo.getMembersWithState(output_states=[TS_UPDATED])
- self.assertEqual(len(res),1) # bar-1.0
-
- def assertResult(self, txmbrs):
- """Check if self.tsInfo contains the given txmbr.
- """
- errors = ["Problems with members in txInfo \n\n"]
- txmbrs = set(txmbrs)
- found = set(self.tsInfo.getMembers())
-
- # Look for needed members
- for txmbr in txmbrs:
- if not self.tsInfo.exists(txmbr.po.pkgtup):
- errors.append(" %s was not found in tsInfo!\n" % txmbr)
-
- for txmbr in found - txmbrs:
- errors.append("%s should not be in tsInfo\n" % txmbr)
-
- if len(errors) > 1:
- errors.append("\nTest case was:\n\n")
- errors.extend(inspect.getsource(inspect.stack()[1][0].f_code))
- errors.append("\n")
- self.fail("".join(errors))
-
- def dumpTsInfo(self):
- for txmbr in self.tsInfo:
- print txmbr
View
@@ -11,13 +11,31 @@
SYSTEM_NSOLVABLES = TOTAL_RPMDB_COUNT + 2
TOTAL_NSOLVABLES = 6
+# testing infrastructure
+
def repo(reponame):
return os.path.join(repo_dir(), reponame)
def repo_dir():
this_dir=os.path.dirname(__file__)
return os.path.join(this_dir, "repos")
+# mock objects
+
+def create_mock_package(name, major_version):
+ pkg = mock.Mock(spec_set=['pkgtup', 'name', 'reponame', 'repoid',
+ 'arch', 'evr', 'state'])
+ pkg.name = name
+ pkg.reponame = pkg.repoid = 'main'
+ pkg.arch = 'noarch'
+ pkg.evr = '%d-1' % major_version
+ pkg.pkgtup = (pkg.name, pkg.arch, 0, str(major_version) , '1')
+ return pkg
+
+def mock_packages():
+ return [create_mock_package("within%s" % chr(i), 2)
+ for i in range(ord('A'), ord('I'))]
+
def mock_yum_base(*extra_repos):
yumbase = MockYumBase()
yumbase.conf = FakeConf()
@@ -39,26 +57,6 @@ def _init_hawkey_sack(self):
return self._sack
-class ResultTestCase(unittest.TestCase):
-
- # originally from testbase.py
- def assertResult(self, yumbase, pkgs):
- """ Check if "system" contains the given pkgs. pkgs must be present. Any
- other pkgs result in an error. Pkgs are present if they are in the
- rpmdb and are not REMOVEd or they are INSTALLed.
- """
- pkgs = set(pkgs)
- installed = set(dnf.queries.installed_by_name(yumbase.sack, None))
-
- yumbase.buildTransaction()
- for txmbr in yumbase.tsInfo.getMembersWithState(
- output_states=dnf.yum.constants.TS_REMOVE_STATES):
- installed.remove(txmbr.po)
- for txmbr in yumbase.tsInfo.getMembersWithState(
- output_states=dnf.yum.constants.TS_INSTALL_STATES):
- installed.add(txmbr.po)
- self.assertEqual(pkgs, installed)
-
# mock object taken from testbase.py in yum/test:
class FakeConf(object):
def __init__(self):
@@ -82,3 +80,25 @@ def __init__(self):
self.protected_multilib = False
self.clean_requirements_on_remove = True
self.upgrade_requirements_on_install = False
+
+# specialized test cases
+
+class ResultTestCase(unittest.TestCase):
+
+ # originally from testbase.py
+ def assertResult(self, yumbase, pkgs):
+ """ Check if "system" contains the given pkgs. pkgs must be present. Any
+ other pkgs result in an error. Pkgs are present if they are in the
+ rpmdb and are not REMOVEd or they are INSTALLed.
+ """
+ pkgs = set(pkgs)
+ installed = set(dnf.queries.installed_by_name(yumbase.sack, None))
+
+ yumbase.buildTransaction()
+ for txmbr in yumbase.tsInfo.getMembersWithState(
+ output_states=dnf.yum.constants.TS_REMOVE_STATES):
+ installed.remove(txmbr.po)
+ for txmbr in yumbase.tsInfo.getMembersWithState(
+ output_states=dnf.yum.constants.TS_INSTALL_STATES):
+ installed.add(txmbr.po)
+ self.assertEqual(pkgs, installed)
View
@@ -0,0 +1,75 @@
+import base
+import unittest
+from yum.constants import *
+
+from yum.transactioninfo import TransactionData
+
+class TransactionDataTests(unittest.TestCase):
+ ''' Test cases for yum.transactioninfo.TransactionData'''
+
+ def setUp(self):
+ self.tsInfo = TransactionData()
+ self.pkgs = base.mock_packages()
+
+ def testLenght(self):
+ ''' test __len__ method '''
+ self.tsInfo.addInstall(self.pkgs[0])
+ self.tsInfo.addUpdate(self.pkgs[2], self.pkgs[1])
+ self.assertEqual(len(self.tsInfo),3)
+
+ def testAddTheSameTwice(self):
+ ''' test add the same twice '''
+ first = self.tsInfo.addInstall(self.pkgs[0])
+ self.tsInfo.addInstall(self.pkgs[1])
+ last = self.tsInfo.addInstall(self.pkgs[0])
+ self.assertEqual(len(self.tsInfo),2) # only 2 members
+ self.assertEqual(first, last)
+
+ def testExists(self):
+ ''' test exists method '''
+ self.tsInfo.addInstall(self.pkgs[0])
+ self.tsInfo.addInstall(self.pkgs[1])
+ assert(self.tsInfo.exists(self.pkgs[0].pkgtup))
+ assert(self.tsInfo.exists(self.pkgs[1].pkgtup))
+ self.assertFalse(self.tsInfo.exists(self.pkgs[2].pkgtup))
+
+ def testRemove(self):
+ ''' test remove from transaction '''
+ pkg = self.pkgs[0]
+ txmbr = self.tsInfo.addInstall(pkg)
+ self.tsInfo.remove(pkg.pkgtup)
+ self.assertEqual(len(self.tsInfo), 0)
+
+ def testGetFromTransaction(self):
+ ''' test getting from Transaction '''
+ self.tsInfo.addInstall(self.pkgs[0])
+ txmbr = self.tsInfo.getMembers(self.pkgs[0].pkgtup)[0]
+ self.assertEqual(txmbr.output_state, TS_INSTALL)
+
+ def testAddUpdatesAndObsoletes(self):
+ ''' test addUpdated,addObsoleted'''
+ txmbr1 = self.tsInfo.addUpdated(self.pkgs[1], self.pkgs[2])
+ txmbr2 = self.tsInfo.addObsoleted(self.pkgs[3], self.pkgs[4])
+ self.assertEqual(len(self.tsInfo), 2)
+ txmbr = self.tsInfo.getMembersWithState(output_states=[TS_UPDATED])[0]
+ self.assertEqual(txmbr.po, self.pkgs[1])
+ txmbr = self.tsInfo.getMembersWithState(output_states=[TS_OBSOLETED])[0]
+ self.assertEqual(txmbr.po, self.pkgs[3])
+
+ def testMatchNaevr(self):
+ ''' test MatchNaevr '''
+ self.tsInfo.addInstall(self.pkgs[0])
+ self.tsInfo.addUpdated(self.pkgs[2],self.pkgs[1])
+ res = self.tsInfo.matchNaevr(name='withinC')
+ self.assertEqual(len(res),1)
+ res = self.tsInfo.matchNaevr(arch='noarch')
+ self.assertEqual(len(res),2)
+
+ def testgetMembersWithState(self):
+ ''' test getMembersWithState'''
+ self.tsInfo.addInstall(self.pkgs[0])
+ self.tsInfo.addUpdate(self.pkgs[2],self.pkgs[1])
+ res = self.tsInfo.getMembersWithState(output_states=[TS_INSTALL,TS_UPDATE])
+ self.assertEqual(len(res),2)
+ res = self.tsInfo.getMembersWithState(output_states=[TS_UPDATED])
+ self.assertEqual(len(res),1)

0 comments on commit 1a3f50b

Please sign in to comment.