class Test(unittest.TestCase):

    def setUp(self):
        self.sources=["localhost", "localhost123"]
        self.package=Package(name="kernel", version="2.6.18", subversion="164-el5", architecture="x86_64")
        self.package.allsources=self.sources
        self.package.addsource("localhost")

    def tearDown(self):
        pass

    def testhash(self):
        #print hash(self.package)
        self.assertEquals(hash(self.package), hash("kernelx86_642.6.18164-el5"))

    def testEquals(self):
        self.assertEquals(self.package, Package(name="kernel", version="2.6.18", subversion="164-el5", architecture="x86_64", allsources=self.sources, sources=["localhost123"]))

    def testNotEquals1(self):
        self.assertNotEquals(self.package, Package(name="kernel-devel", version="2.6.18", subversion="164-el5", architecture="x86_64", allsources=self.sources, sources=["localhost123"]))
    def testNotEquals2(self):
        self.assertNotEquals(self.package, Package(name="kernel", version="2.6.18", subversion="194-el5", architecture="x86_64", allsources=self.sources, sources=["localhost123"]))
    def testNotEquals3(self):
        self.assertNotEquals(self.package, Package(name="kernel", version="2.6.18", subversion="164-el5", architecture="i386", allsources=self.sources, sources=["localhost123"]))

    def testDeepCmp(self):
        self.assertEquals(self.package.cmp(Package(name="kernel", version="2.6.18", subversion="164-el5", architecture="x86_64", allsources=self.sources, sources=["localhost"]), True), 0)

    def testNotDeepCmp1(self):
        package=Package(name="kernel", version="2.6.18", subversion="164-el5", architecture="x86_64", sources=["localhost123"], allsources=self.sources)
        self.assertEquals(self.package.cmp(package, True), -1)
    def testNotDeepCmp2(self):
        self.assertEquals(self.package.cmp(Package(name="kernel", version="2.6.18", subversion="194-el5", architecture="x86_64", allsources=self.sources, sources=["localhost"]), True), -1)
    def testNotDeepCmp3(self):
        self.assertEquals(self.package.cmp(Package(name="kernel", version="2.6.18", subversion="164-el5", architecture="i386", allsources=self.sources, sources=["localhost123"]), True), 1)
        
    def testToString(self):
        self.assertEquals(str(self.package), "kernel-2.6.18-164-el5.x86_64")
    def testRepr(self):
        self.assertEquals(self.package.__repr__(), 'Package(name="kernel", architecture="x86_64", version="2.6.18", subversion="164-el5", sources=[\'localhost\'], allsources=[\'localhost\', \'localhost123\'], hashlevel=4, index=0)')
        
    def testCopy(self):
        package=self.package.copy()
        self.assertEquals(self.package, package)
        package.addsource("localhost1")
        self.assertEquals(self.package, package)
        self.assertNotEquals(self.package.sources, package.sources)
        
    def testAddSource1(self):
        self.package.addsource("localhost123")
        self.assertEquals(self.package.resolvesources(), self.sources, "Sources from package %s != sources %s" %(self.package.resolvesources(), self.sources))
        
    def testAddSource2(self):
        self.package.addsource("localhost1")
        self.assertEquals(self.package.resolvesources(), ["localhost", "localhost1"], "Sources from package %s != sources %s" %(self.package.resolvesources(), self.sources))
class Test(unittest.TestCase):


    def setUp(self):
        self.sources=["localhost", "localhost1"]
        Package.DEFAULT_HASHLEVEL=4
        self.packages=Packages(self.sources)
        self.testpackage=Package("kernel", "2.6.18", "164.el5", "x86_64", ["localhost"], self.sources)
        self.testpackage2=Package("kernel", "2.6.18", "164.el5", "x86_64", ["localhost1"], self.sources)
        self.testpackage3=Package("kernel-devel", "2.6.18", "164.el5", "x86_64", ["localhost"], self.sources)
        self.testpackage4=Package("kernel-devel", "2.6.18", "164.el5", "i686", ["localhost1"], self.sources)
        self.testpackage5=Package("kernel-debuginfo", "2.6.18", "164.el5", "x86_64", ["localhost"], self.sources)
        self.testpackage6=Package("kernel-debuginfo", "2.6.18", "194.el5", "x86_64", ["localhost1"], self.sources)

    def tearDown(self):
        pass

#    def testaddPackages(self):
#        self.packages.add(self.testpackage)
#        self.packages.add(self.testpackage2)
#        self.packages.add(self.testpackage3)
#        self.packages.add(self.testpackage4)
#        self.packages.add(self.testpackage5)
#        self.packages.add(self.testpackage6)
#        print self.testpackage.__repr__()
#        print self.testpackage2.__repr__()
#        print self.testpackage3.__repr__()
#        print self.testpackage4.__repr__()
#        print self.testpackage5.__repr__()
#        print self.testpackage6.__repr__()
#        keys=self.packages.packages.keys()
#        keys.sort()
#        retkeys=[hash("kernel-develi686"), hash("kernelx86_642.6.18164.el5"), hash("kernel-develx86_64"), hash("kernel-debuginfox86_642.6.18164.el5"), hash("kernel-debuginfox86_642.6.18194.el5")]
#        retkeys.sort()
#        print keys
#        print retkeys
#        print self.packages
#        self.assertEquals(keys, retkeys)

    def testaddPackage1(self):
        self.packages.add(self.testpackage)
        self.assertTrue(self.testpackage in self.packages, "Could not add package %s to packages %s" %(self.testpackage, self.packages))
        
    def testaddPackage2(self):
        self.packages.add(self.testpackage2)
        self.assertTrue(self.testpackage2 in self.packages, "Could not add package %s to packages %s" %(self.testpackage2, self.packages))
        
    def testContains1(self):
        self.packages.add(self.testpackage)
        self.failUnless(self.testpackage in self.packages, "%s in %s" %(self.testpackage.__repr__(), self.packages))
        self.failUnless(not self.testpackage2 in self.packages, "%s not in %s" %(self.testpackage2.__repr__(), self.packages))
        self.failUnless(not self.testpackage3 in self.packages, "%s not in %s" %(self.testpackage3.__repr__(), self.packages))

    def testContains2(self):
        self.packages.add(self.testpackage)
        self.packages.add(self.testpackage2)
        self.failUnless(self.testpackage in self.packages, "%s in %s" %(self.testpackage.__repr__(), self.packages))
        self.failUnless(self.packages[self.testpackage2] in self.packages, "%s in %s" %(self.testpackage2.__repr__(), self.packages))
        self.failUnless(not self.testpackage3 in self.packages, "%s not in %s" %(self.testpackage3.__repr__(), self.packages))

    def testContains3(self):
        self.packages.add(self.testpackage)
        self.packages.add(self.testpackage3)
        self.failUnless(self.testpackage in self.packages, "%s in %s" %(self.testpackage.__repr__(), self.packages))
        self.failUnless(not self.testpackage2 in self.packages, "%s not in %s" %(self.testpackage2.__repr__(), self.packages))
        self.failUnless(self.testpackage3 in self.packages, "%s in %s" %(self.testpackage3.__repr__(), self.packages))

    def testremove1(self):
        self.packages.add(self.testpackage)
        self.packages.remove(self.testpackage)
        self.assertFalse(self.testpackage in self.packages, "%s should not be part of packages." %self.testpackage.__repr__())
        
    def testremove2(self):
        self.packages.add(self.testpackage.copy())
        self.packages.add(self.testpackage2.copy())
        self.packages.remove(self.testpackage)
        self.assertFalse(self.testpackage in self.packages, "%s should not be part of packages: %s." %(self.testpackage.__repr__(), self.packages))
        self.assertTrue(self.testpackage2 in self.packages, "%s should be part of packages, packages: %s." %(self.testpackage2.__repr__(), self.packages))
        
    def testremove3(self):
        self.packages.add(self.testpackage.copy())
        self.packages.add(self.testpackage2.copy())
        del self.packages[self.testpackage]
        self.assertFalse(self.testpackage in self.packages, "%s should not be part of packages: %s." %(self.testpackage.__repr__(), self.packages))
        self.assertTrue(self.testpackage2 in self.packages, "%s should be part of packages, packages: %s." %(self.testpackage2.__repr__(), self.packages))

    def testiterrange(self):
        self.packages.add(self.testpackage)
        self.packages.add(self.testpackage2)
        self.packages.add(self.testpackage3)
        self.packages.add(self.testpackage4)
        self.packages.add(self.testpackage5)
        self.packages.add(self.testpackage6)
        self.assertEquals(list(self.packages.iterrange(0, 2)), 
                          [Package(name="kernel-debuginfo", architecture="x86_64", version="2.6.18", subversion="194.el5",
                                   sources=['localhost1'], allsources=['localhost', 'localhost1'], hashlevel=4, 
                                   index=4), 
                           Package(name="kernel-devel", architecture="x86_64", version="2.6.18", subversion="164.el5", 
                                   sources=['localhost'], allsources=['localhost', 'localhost1'], hashlevel=4, index=1)])
        self.assertEquals(list(self.packages.iterrange(4, 6)), 
                          [Package(name="kernel-debuginfo", architecture="x86_64", version="2.6.18", subversion="164.el5", 
                                   sources=['localhost'], allsources=['localhost', 'localhost1'], hashlevel=4, index=3)])
        #print list(self.packages.iterrange(2, 4))
        self.assertEquals(list(self.packages.iterrange(2, 4)),
                           [Package(name="kernel", architecture="x86_64", version="2.6.18", subversion="164.el5", 
                                    sources=['localhost', 'localhost1'], allsources=['localhost', 'localhost1'], 
                                    hashlevel=4, index=0), 
                            Package(name="kernel-devel", architecture="i686", version="2.6.18", subversion="164.el5", 
                                    sources=['localhost1'], allsources=['localhost', 'localhost1'], 
                                    hashlevel=4, index=2)])
    def testiterrange2(self):
        self.packages.add(self.testpackage)
        self.packages.add(self.testpackage2)
        self.packages.add(self.testpackage3)
        self.packages.add(self.testpackage4)
        self.packages.add(self.testpackage5)
        self.packages.add(self.testpackage6)
        self.assertEquals(list(self.packages.iterrange(0, 2, self.packages.sort)), 
                          [Package(name="kernel-debuginfo", architecture="x86_64", version="2.6.18", subversion="164.el5", 
                                   sources=['localhost'], allsources=['localhost', 'localhost1'], hashlevel=4, index=3), 
                           Package(name="kernel-debuginfo", architecture="x86_64", version="2.6.18", subversion="194.el5", 
                                   sources=['localhost1'], allsources=['localhost', 'localhost1'], hashlevel=4, index=4)])
        self.assertEquals(list(self.packages.iterrange(4, 6)), 
                          [Package(name="kernel-debuginfo", architecture="x86_64", version="2.6.18", subversion="164.el5", 
                                   sources=['localhost'], allsources=['localhost', 'localhost1'], hashlevel=4, index=3)])
        #print list(self.packages.iterrange(2, 4))
        self.assertEquals(list(self.packages.iterrange(2, 4)),
                           [Package(name="kernel", architecture="x86_64", version="2.6.18", subversion="164.el5", 
                                    sources=['localhost', 'localhost1'], allsources=['localhost', 'localhost1'], 
                                    hashlevel=4, index=0), 
                            Package(name="kernel-devel", architecture="i686", version="2.6.18", subversion="164.el5", 
                                    sources=['localhost1'], allsources=['localhost', 'localhost1'], hashlevel=4, index=2)])
        
    def testsort(self):
        self.packages.add(self.testpackage)
        self.packages.add(self.testpackage2)
        self.packages.add(self.testpackage3)
        self.packages.add(self.testpackage4)
        self.packages.add(self.testpackage5)
        self.packages.add(self.testpackage6)
        print map(lambda pkg: pkg.hashstring(), self.packages.sort())