class UserFactoryTestCase(unittest.TestCase):
    def setUp( self ):
        self._db = InMemoryDatabase()
        self.assertNumOfUsersInDbEquals( 0 )

    def tearDown( self ):
        self._db.close()

    def assertNumOfUsersInDbEquals( self, num ):
        query = 'SELECT COUNT( DISTINCT username ) FROM permission'
        cursor = self._db.cursor()
        cursor.execute( query )
        result = cursor.fetchone()
        self.assertEquals( num, int( result[ 0 ]))

    def insertUsers( self, usernames ):
        query = 'INSERT INTO permission ( username ) VALUES ( %s )'
        cursor = self._db.cursor()
        arguments = [ ( name, ) for name in usernames ]
        cursor.executemany( query, arguments )
        cursor.close()
        self.assertNumOfUsersInDbEquals( len( usernames ))

    def testGetAll( self ):
        usernames = [ 'user-1', 'user-2' ]
        self.insertUsers( usernames )
        userFactory  = UserFactory( self._db )
        self.assertEquals( usernames, userFactory.getAllIDs() )
class MilestoneFactoryTestCase(unittest.TestCase):
    def setUp( self ):
        self._db = InMemoryDatabase()
        self.assertNumOfMilestonesInDbEquals( 0 )

    def tearDown( self ):
        self._db.close()
        MilestoneFactory._cache = {}

    def assertNumOfMilestonesInDbEquals( self, num ):
        query = 'SELECT COUNT(*) FROM milestone'
        cursor = self._db.cursor()
        cursor.execute( query )
        result = cursor.fetchone()
        self.assertEquals( num, int( result[ 0 ]))

    def insertMilestones( self, names ):
        query = 'INSERT INTO milestone ( name ) VALUES ( %s )'
        cursor = self._db.cursor()
        arguments = [ ( name, ) for name in names ]
        cursor.executemany( query, arguments )
        cursor.close()
        self.assertNumOfMilestonesInDbEquals( len( names ))

    def testGetAll( self ):
        self.insertMilestones([ 'random-1', 'random-2' ])
        m = MilestoneFactory( self._db, None, None )
        milestones = m.getAllIDs()
        self.assertEquals( 2, len ( milestones ))
        self.assertEquals( 'random-1', milestones[ 0 ] )
        self.assertEquals( 'random-2', milestones[ 1 ] )
Пример #3
0
    def test(self):
        """Testing WikiFormatter"""

        # Environment stub
        from trac.core import ComponentManager
        from trac.config import Configuration
        from trac.log import logger_factory
        from trac.test import InMemoryDatabase
        from trac.web.href import Href

        db = InMemoryDatabase()

        class DummyEnvironment(ComponentManager):
            def __init__(self):
                ComponentManager.__init__(self)
                self.log = logger_factory('null')
                self.config = Configuration(None)
                self.href = Href('/')
                self.abs_href = Href('http://www.example.com/')
                self._wiki_pages = {}
                self.path = ''

            def component_activated(self, component):
                component.env = self
                component.config = self.config
                component.log = self.log

            def get_db_cnx(self):
                return db

        # Load all the components that provide IWikiSyntaxProvider
        # implementations that are tested. Ideally those should be tested
        # in separate unit tests.
        import trac.versioncontrol.web_ui.browser
        import trac.versioncontrol.web_ui.changeset
        import trac.ticket.query
        import trac.ticket.report
        import trac.ticket.roadmap
        import trac.Search

        env = DummyEnvironment()

        out = StringIO.StringIO()
        formatter = self.formatter(env)
        formatter.format(self.input, out)
        v = out.getvalue().replace('\r', '')
        try:
            self.assertEquals(self.correct, v)
        except AssertionError, e:
            raise AssertionError('%s\n\n%s:%s: for the input '
                                 '(formatter flavor was "%s")' \
                                 % (str(e), self.file, self.line,
                                    formatter.flavor))
class ComponentFactoryTest(unittest.TestCase):
    def setUp( self ):
        self._db = InMemoryDatabase()
        self.assertNumOfComponentsInDbEquals( 0 )

    def tearDown( self ):
        self._db.close()
        ComponentFactory._cache = {}

    def assertNumOfComponentsInDbEquals( self, num ):
        query = 'SELECT COUNT(*) FROM component'
        cursor = self._db.cursor()
        cursor.execute( query )
        result = cursor.fetchone()
        self.assertEquals( num, int( result[ 0 ]))

    def testGetAll( self ):
        componentIDs = [ 'component-a', 'component-b' ]
        self.insertComponentsIntoDb( componentIDs )
        componentFactory = ComponentFactory( self._db )
        all = componentFactory.getAllIDs()
        self.assertEquals( componentIDs, all )

    def insertComponentsIntoDb( self, ids ):
        query = 'INSERT INTO component ( name ) VALUES ( %s )'
        cursor = self._db.cursor()
        arguments = [ ( id, ) for id in ids ]
        cursor.executemany( query, arguments )
        cursor.close()
        self.assertNumOfComponentsInDbEquals( len( ids ))


    def testSameObjectOnSameName( self ):
        componentFactory = ComponentFactory( None )
        o1 = componentFactory.getByName( 'test' )
        o2 = componentFactory.getByName( 'test' )
        o3 = componentFactory.getByName( 'test2' )
        self.assertTrue( o1 is o2 )
        self.assertFalse( o1 is o3 )
Пример #5
0
class CacheTestCase(unittest.TestCase):

    def setUp(self):
        self.db = InMemoryDatabase()
        self.log = logger_factory('test')
        cursor = self.db.cursor()
        cursor.execute("INSERT INTO system (name, value) VALUES (%s,%s)",
                       ('youngest_rev', ''))

    def test_initial_sync_with_empty_repos(self):
        t = datetime(2001, 1, 1, 1, 1, 1, 0, utc)
        def no_changeset(rev):
            raise NoSuchChangeset(rev)
            
        repos = Mock(Repository, 'test-repos', None, self.log,
                     get_changeset=no_changeset,
                     get_oldest_rev=lambda: 1,
                     get_youngest_rev=lambda: 0,
                     normalize_rev=no_changeset,
                     next_rev=lambda x: None)
        cache = CachedRepository(self.db, repos, None, self.log)
        cache.sync()

        cursor = self.db.cursor()
        cursor.execute("SELECT rev,time,author,message FROM revision")
        self.assertEquals(None, cursor.fetchone())
        cursor.execute("SELECT COUNT(*) FROM node_change")
        self.assertEquals(0, cursor.fetchone()[0])

    def test_initial_sync(self):
        t1 = datetime(2001, 1, 1, 1, 1, 1, 0, utc)
        t2 = datetime(2002, 1, 1, 1, 1, 1, 0, utc)
        changes = [('trunk', Node.DIRECTORY, Changeset.ADD, None, None),
                   ('trunk/README', Node.FILE, Changeset.ADD, None, None)]
        changesets = [Mock(Changeset, 0, '', '', t1,
                           get_changes=lambda: []),
                      Mock(Changeset, 1, 'Import', 'joe', t2,
                           get_changes=lambda: iter(changes))]
        repos = Mock(Repository, 'test-repos', None, self.log,
                     get_changeset=lambda x: changesets[int(x)],
                     get_oldest_rev=lambda: 0,
                     get_youngest_rev=lambda: 1,
                     normalize_rev=lambda x: x,
                     next_rev=lambda x: int(x) == 0 and 1 or None)
        cache = CachedRepository(self.db, repos, None, self.log)
        cache.sync()

        cursor = self.db.cursor()
        cursor.execute("SELECT rev,time,author,message FROM revision")
        self.assertEquals(('0', to_timestamp(t1), '', ''), cursor.fetchone())
        self.assertEquals(('1', to_timestamp(t2), 'joe', 'Import'), cursor.fetchone())
        self.assertEquals(None, cursor.fetchone())
        cursor.execute("SELECT rev,path,node_type,change_type,base_path,"
                       "base_rev FROM node_change")
        self.assertEquals(('1', 'trunk', 'D', 'A', None, None),
                          cursor.fetchone())
        self.assertEquals(('1', 'trunk/README', 'F', 'A', None, None),
                          cursor.fetchone())
        self.assertEquals(None, cursor.fetchone())

    def test_update_sync(self):
        t1 = datetime(2001, 1, 1, 1, 1, 1, 0, utc)
        t2 = datetime(2002, 1, 1, 1, 1, 1, 0, utc)
        t3 = datetime(2003, 1, 1, 1, 1, 1, 0, utc)
        cursor = self.db.cursor()
        cursor.execute("INSERT INTO revision (rev,time,author,message) "
                       "VALUES (0,%s,'','')", (to_timestamp(t1),))
        cursor.execute("INSERT INTO revision (rev,time,author,message) "
                       "VALUES (1,%s,'joe','Import')", (to_timestamp(t2),))
        cursor.executemany("INSERT INTO node_change (rev,path,node_type,"
                           "change_type,base_path,base_rev) "
                           "VALUES ('1',%s,%s,%s,%s,%s)",
                           [('trunk', 'D', 'A', None, None),
                            ('trunk/README', 'F', 'A', None, None)])
        cursor.execute("UPDATE system SET value='1' WHERE name='youngest_rev'")

        changes = [('trunk/README', Node.FILE, Changeset.EDIT, 'trunk/README', 1)]
        changeset = Mock(Changeset, 2, 'Update', 'joe', t3,
                         get_changes=lambda: iter(changes))
        repos = Mock(Repository, 'test-repos', None, self.log,
                     get_changeset=lambda x: changeset,
                     get_youngest_rev=lambda: 2,
                     get_oldest_rev=lambda: 0,
                     normalize_rev=lambda x: x,                    
                     next_rev=lambda x: x and int(x) == 1 and 2 or None)
        cache = CachedRepository(self.db, repos, None, self.log)
        cache.sync()

        cursor = self.db.cursor()
        cursor.execute("SELECT time,author,message FROM revision WHERE rev='2'")
        self.assertEquals((to_timestamp(t3), 'joe', 'Update'), cursor.fetchone())
        self.assertEquals(None, cursor.fetchone())
        cursor.execute("SELECT path,node_type,change_type,base_path,base_rev "
                       "FROM node_change WHERE rev='2'")
        self.assertEquals(('trunk/README', 'F', 'E', 'trunk/README', '1'),
                          cursor.fetchone())
        self.assertEquals(None, cursor.fetchone())

    def test_get_changes(self):
        t1 = datetime(2001, 1, 1, 1, 1, 1, 0, utc)
        t2 = datetime(2002, 1, 1, 1, 1, 1, 0, utc)
        cursor = self.db.cursor()
        cursor.execute("INSERT INTO revision (rev,time,author,message) "
                       "VALUES (0,%s,'','')", (to_timestamp(t1),))
        cursor.execute("INSERT INTO revision (rev,time,author,message) "
                       "VALUES (1,%s,'joe','Import')", (to_timestamp(t2),))
        cursor.executemany("INSERT INTO node_change (rev,path,node_type,"
                           "change_type,base_path,base_rev) "
                           "VALUES ('1',%s,%s,%s,%s,%s)",
                           [('trunk', 'D', 'A', None, None),
                            ('trunk/README', 'F', 'A', None, None)])
        cursor.execute("UPDATE system SET value='1' WHERE name='youngest_rev'")

        repos = Mock(Repository, 'test-repos', None, self.log,
                     get_changeset=lambda x: None,
                     get_youngest_rev=lambda: 1,
                     get_oldest_rev=lambda: 0,
                     next_rev=lambda x: None,
                     normalize_rev=lambda rev: rev)
        cache = CachedRepository(self.db, repos, None, self.log)
        self.assertEqual('1', cache.youngest_rev)
        changeset = cache.get_changeset(1)
        self.assertEqual('joe', changeset.author)
        self.assertEqual('Import', changeset.message)
        self.assertEqual(t2, changeset.date)
        changes = changeset.get_changes()
        self.assertEqual(('trunk', Node.DIRECTORY, Changeset.ADD, None, None),
                         changes.next())
        self.assertEqual(('trunk/README', Node.FILE, Changeset.ADD, None, None),
                         changes.next())
        self.assertRaises(StopIteration, changes.next)
 def setUp( self ):
     self._db = InMemoryDatabase()
     self.assertNumOfComponentsInDbEquals( 0 )
Пример #7
0
class CacheTestCase(unittest.TestCase):

    def setUp(self):
        self.db = InMemoryDatabase()
        self.log = logger_factory('test')

    def test_initial_sync_with_empty_repos(self):
        changeset = Mock(Changeset, 0, '', '', 42000,
                         get_changes=lambda: [])
        repos = Mock(Repository, 'test-repos', None, self.log,
                     get_changeset=lambda x: changeset,
                     get_oldest_rev=lambda: 0,
                     get_youngest_rev=lambda: 0,
                     normalize_rev=lambda x: x,
                     next_rev=lambda x: None)
        cache = CachedRepository(self.db, repos, None, self.log)
        cache.sync()

        cursor = self.db.cursor()
        cursor.execute("SELECT rev,time,author,message FROM revision")
        self.assertEquals(('0', 42000, '', ''), cursor.fetchone())
        cursor.execute("SELECT COUNT(*) FROM node_change")
        self.assertEquals(0, cursor.fetchone()[0])

    def test_initial_sync(self):
        changes = [('trunk', Node.DIRECTORY, Changeset.ADD, None, None),
                   ('trunk/README', Node.FILE, Changeset.ADD, None, None)]
        changesets = [Mock(Changeset, 0, '', '', 41000,
                           get_changes=lambda: []),
                      Mock(Changeset, 1, 'Import', 'joe', 42000,
                           get_changes=lambda: iter(changes))]
        repos = Mock(Repository, 'test-repos', None, self.log,
                     get_changeset=lambda x: changesets[int(x)],
                     get_oldest_rev=lambda: 0,
                     get_youngest_rev=lambda: 1,
                     normalize_rev=lambda x: x,
                     next_rev=lambda x: int(x) == 0 and 1 or None)
        cache = CachedRepository(self.db, repos, None, self.log)
        cache.sync()

        cursor = self.db.cursor()
        cursor.execute("SELECT rev,time,author,message FROM revision")
        self.assertEquals(('0', 41000, '', ''), cursor.fetchone())
        self.assertEquals(('1', 42000, 'joe', 'Import'), cursor.fetchone())
        self.assertEquals(None, cursor.fetchone())
        cursor.execute("SELECT rev,path,node_type,change_type,base_path,"
                       "base_rev FROM node_change")
        self.assertEquals(('1', 'trunk', 'D', 'A', None, None),
                          cursor.fetchone())
        self.assertEquals(('1', 'trunk/README', 'F', 'A', None, None),
                          cursor.fetchone())
        self.assertEquals(None, cursor.fetchone())

    def test_update_sync(self):
        cursor = self.db.cursor()
        cursor.execute("INSERT INTO revision (rev,time,author,message) "
                       "VALUES (0,41000,'','')")
        cursor.execute("INSERT INTO revision (rev,time,author,message) "
                       "VALUES (1,42000,'joe','Import')")
        cursor.executemany("INSERT INTO node_change (rev,path,node_type,"
                           "change_type,base_path,base_rev) "
                           "VALUES ('1',%s,%s,%s,%s,%s)",
                           [('trunk', 'D', 'A', None, None),
                            ('trunk/README', 'F', 'A', None, None)])

        changes = [('trunk/README', Node.FILE, Changeset.EDIT, 'trunk/README', 1)]
        changeset = Mock(Changeset, 2, 'Update', 'joe', 42042,
                         get_changes=lambda: iter(changes))
        repos = Mock(Repository, 'test-repos', None, self.log,
                     get_changeset=lambda x: changeset,
                     get_youngest_rev=lambda: 2,
                     next_rev=lambda x: int(x) == 1 and 2 or None)
        cache = CachedRepository(self.db, repos, None, self.log)
        cache.sync()

        cursor = self.db.cursor()
        cursor.execute("SELECT time,author,message FROM revision WHERE rev='2'")
        self.assertEquals((42042, 'joe', 'Update'), cursor.fetchone())
        self.assertEquals(None, cursor.fetchone())
        cursor.execute("SELECT path,node_type,change_type,base_path,base_rev "
                       "FROM node_change WHERE rev='2'")
        self.assertEquals(('trunk/README', 'F', 'E', 'trunk/README', '1'),
                          cursor.fetchone())
        self.assertEquals(None, cursor.fetchone())

    def test_get_changes(self):
        cursor = self.db.cursor()
        cursor.execute("INSERT INTO revision (rev,time,author,message) "
                       "VALUES (0,41000,'','')")
        cursor.execute("INSERT INTO revision (rev,time,author,message) "
                       "VALUES (1,42000,'joe','Import')")
        cursor.executemany("INSERT INTO node_change (rev,path,node_type,"
                           "change_type,base_path,base_rev) "
                           "VALUES ('1',%s,%s,%s,%s,%s)",
                           [('trunk', 'D', 'A', None, None),
                            ('trunk/README', 'F', 'A', None, None)])

        repos = Mock(Repository, 'test-repos', None, self.log,
                     get_changeset=lambda x: None,
                     get_youngest_rev=lambda: 1,
                     next_rev=lambda x: None, normalize_rev=lambda rev: rev)
        cache = CachedRepository(self.db, repos, None, self.log)
        self.assertEqual('1', cache.youngest_rev)
        changeset = cache.get_changeset(1)
        self.assertEqual('joe', changeset.author)
        self.assertEqual('Import', changeset.message)
        self.assertEqual(42000, changeset.date)
        changes = changeset.get_changes()
        self.assertEqual(('trunk', Node.DIRECTORY, Changeset.ADD, None, None),
                         changes.next())
        self.assertEqual(('trunk/README', Node.FILE, Changeset.ADD, None, None),
                         changes.next())
        self.assertRaises(StopIteration, changes.next)
Пример #8
0
 def setUp(self):
     self.db = InMemoryDatabase()
     self.log = logger_factory('test')
Пример #9
0
 def get_db_cnx(self):
     if not hasattr(self, '_db'):
         self._db = InMemoryDatabase()
     return self._db
Пример #10
0
 def setUp(self):
     self.db = InMemoryDatabase()
     self.log = logger_factory('test')
     cursor = self.db.cursor()
     cursor.execute("INSERT INTO system (name, value) VALUES (%s,%s)",
                    ('youngest_rev', ''))
Пример #11
0
 def setUp(self):
     self.db = InMemoryDatabase()
     self.log = logger_factory('test')
     cursor = self.db.cursor()
     cursor.execute("INSERT INTO system (name, value) VALUES (%s,%s)",
                    ('youngest_rev', ''))
 def setUp( self ):
     self._db = InMemoryDatabase()
     self.assertEmptyPermissionsTable()
 def setUp( self ):
     self._db = InMemoryDatabase()
     self._cursor = self._db.cursor()
     self.assertFalse( self.milestoneRelationshipTableExists() )
class MilestoneRelationshipResolverTestCase(unittest.TestCase):
    def setUp( self ):
        self._db = InMemoryDatabase()
        self._cursor = self._db.cursor()
        self.assertFalse( self.milestoneRelationshipTableExists() )

    def tearDown( self ):
        self._cursor.close()
        self._db.close()

    def milestoneRelationshipTableExists( self ):
        query = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=%s"
        try:
            cursor = self._db.cursor()
            cursor.execute( query, [ 'milestoneRelationship' ])
            result = cursor.fetchone()
            return int( result[ 0 ]) == 1
        finally:
            try: cursor.close()
            except Exception,e: pass

    def testTableAutomaticallyCreated( self ):
        resolver = MilestoneRelationshipResolver( 'test-milestone', self._db )
        self.assertTrue( self.milestoneRelationshipTableExists() )

    def testGetAllAssignedComponentes( self ):
        resolver = MilestoneRelationshipResolver( 'test-milestone', self._db )
        self.assertEquals( [], resolver.getAllAssignedComponentIDs() )

    def testGetAllAssignedProjects( self ):
        resolver = MilestoneRelationshipResolver( 'test-milestone', self._db )
        self.assertEquals( [], resolver.getAllAssignedProjectIDs() )

    def testAssignComponent( self ):
        resolver1 = MilestoneRelationshipResolver( 'test-milestone', self._db ) 
        resolver2 = MilestoneRelationshipResolver( 'test-milestone-2', self._db ) 
        resolver1.assignToComponentID( 'test-component-name' )
        resolver2.assignToComponentID( 'another-component-name' )

        components1 = resolver1.getAllAssignedComponentIDs()
        components2 = resolver2.getAllAssignedComponentIDs()
        self.assertEquals([ 'test-component-name' ], components1 )
        self.assertEquals([ 'another-component-name' ], components2 )

    def testAssignProject( self ):
        resolver1 = MilestoneRelationshipResolver( 'test-milestone', self._db )
        resolver2 = MilestoneRelationshipResolver( 'test-milestone-2', self._db )
        resolver1.assignToProjectID( 'test-project-name' )
        resolver2.assignToProjectID( 'another-project-name' )

        projects1 = resolver1.getAllAssignedProjectIDs()
        projects2 = resolver2.getAllAssignedProjectIDs()
        self.assertEquals([ 'test-project-name' ], projects1 )
        self.assertEquals([ 'another-project-name' ], projects2 )

    def testAssignComponentsChangedDatabase( self ):
        resolver = MilestoneRelationshipResolver( 'test-milestone', self._db )
        resolver.assignToComponentID( 'component-a' )
        self.assertDatabaseContainsSingleRow( 
                ( 'test-milestone', 'component', 'component-a' ))

    def testAssignProjectChangesDatabase( self ):
        resolver = MilestoneRelationshipResolver( 'test-milestone', self._db )
        resolver.assignToProjectID( 'project-a' )
        self.assertDatabaseContainsSingleRow( 
                ( 'test-milestone', 'project', 'project-a' ))

    def testUnassignCompoent( self ):
        resolver = MilestoneRelationshipResolver( 'test-milestone', self._db )
        resolver.assignToComponentID( 'test-component' )
        components = resolver.getAllAssignedComponentIDs()
        self.assertEquals([ 'test-component' ], components )
        
        resolver.unassignFromComponentID( 'test-component' )
        components = resolver.getAllAssignedComponentIDs()
        self.assertEquals( [], components )
        self.assertDatabaseContainsNoRow()

    def testUnassignProject( self ):
        resolver = MilestoneRelationshipResolver( 'test-milestone', self._db )
        resolver.assignToProjectID( 'test-project' )
        projects = resolver.getAllAssignedProjectIDs()
        self.assertEquals([ 'test-project' ], projects )
        
        resolver.unassignFromProjectID( 'test-project' )
        projects = resolver.getAllAssignedProjectIDs()
        self.assertEquals( [], projects )
        self.assertDatabaseContainsNoRow()

    def assertDatabaseContainsSingleRow( self, row ):
        query = 'SELECT milestoneId, relationshipType, id FROM milestoneRelationship';
        self._cursor.execute( query )
        result = self._cursor.fetchall()
        self.assertEquals( 1, len( result ))
        self.assertEquals( row, result[ 0 ])

    def assertDatabaseContainsNoRow( self ):
        query = 'SELECT * FROM milestoneRelationship'
        self._cursor.execute( query )
        result = self._cursor.fetchall()
        self.assertEquals( 0, len( result ), result )
Пример #15
0
 def setUp( self ):
     self._db = InMemoryDatabase()
     self.assertNumOfUsersInDbEquals( 0 )
class TicketFactoryTest(unittest.TestCase):
    def setUp( self ):
        self._db = InMemoryDatabase()
        self.assertNumOfTicketsInDb( 0 )

    def tearDown( self ):
        self._db.close()
        TicketFactory._cache = {}

    def assertNumOfTicketsInDb( self, num ):
        query = 'SELECT COUNT(*) FROM ticket'
        cursor = self._db.cursor()
        cursor.execute( query )
        result = cursor.fetchone()
        self.assertEquals( int( result[ 0 ]), num )

    def testGetByNameDoesntRaiseException( self ):
        factory = TicketFactory( self._db, None, None )
        factory.getById( 123 )
    
    def testTicketAndProjectCorrectly( self ):
        componentFactory = MockFactory()
        projectFactory = MockFactory()
        self._insertTicketInDb( 1, 'test-1', 'random-component-1', 
                'random-project-1' )
        self._insertTicketInDb( 2, 'test-2', 'random-component-2', 
                'random-project-2' )
        factory = TicketFactory( self._db, componentFactory, projectFactory )


        def assertTicketProperties( ticket, index ):
            self.assertEqual( index, ticket.id() )
            self.assertEqual( 'test-%s' % index, ticket.title() )
            self.assertEqual([ 'random-component-%s' % index ], 
                    componentFactory.requestedNames )
            self.assertEqual([ 'random-project-%s' % index ], 
                    projectFactory.requestedNames )

            componentFactory.requestedNames = []
            projectFactory.requestedNames = []


        ticket = factory.getById( 1 )
        assertTicketProperties( ticket, 1 )

        ticket = factory.getById( 2 )
        assertTicketProperties( ticket, 2 )


    def testCanBeAccessed( self ):
        self._insertTicketInDb( 1, 'test', 'random-component', 'random-project' )

        accessibleFactory = MockFactory( True )
        unaccessibleFactory = MockFactory( False )

        factory = TicketFactory( self._db, unaccessibleFactory, unaccessibleFactory )
        self.assertFalse( factory.getById( 1 ).canBeAccessedBy( None ))
        
        TicketFactory._cache = {}
        factory = TicketFactory( self._db, accessibleFactory, unaccessibleFactory )
        self.assertTrue( factory.getById( 1 ).canBeAccessedBy( None ))

        TicketFactory._cache = {}
        factory = TicketFactory( self._db, unaccessibleFactory, accessibleFactory )
        self.assertTrue( factory.getById( 1 ).canBeAccessedBy( None ))

        TicketFactory._cache = {}
        factory = TicketFactory( self._db, accessibleFactory, accessibleFactory )
        self.assertTrue( factory.getById( 1 ).canBeAccessedBy( None ))

    def _insertTicketInDb( self, id, title, componentName, projectName ):
        query= 'INSERT INTO ticket ( id, summary, component ) VALUES ( %s, %s, %s )'
        cursor = self._db.cursor()
        cursor.execute( query, [ id, title, componentName ])

        query = "INSERT INTO ticket_custom ( ticket, name, value ) VALUES ( %s, " + \
                "'project', %s )"
        cursor.execute( query, [ id, projectName ])
        cursor.close()
 def setUp( self ):
     self._db = InMemoryDatabase()
     self.assertNumOfTicketsInDb( 0 )
 def setUp( self ):
     self._db = InMemoryDatabase()
     self.assertNumOfMilestonesInDbEquals( 0 )
class PermissionManagerTestCase(unittest.TestCase):
    def setUp( self ):
        self._db = InMemoryDatabase()
        self.assertEmptyPermissionsTable()

    def tearDown( self ):
        self._db.close()

    def assertEmptyPermissionsTable( self ):
        try:
            cursor = self._db.cursor()
            cursor.execute( 'SELECT COUNT(*) FROM permission' )
            result = cursor.fetchone()
            self.assertEquals( 0, int( result[ 0 ]))
        finally:
            try: cursor.close()
            except Exception,e: pass

    def testEmptyHasPermission( self ):
        permManager = PermissionManager( 'test', self._db )
        self.assertFalse( permManager.hasPermission( 'random' ))

    def testHasPermissionByGrantedPermission( self ):
        permManager = PermissionManager( 'test', self._db )
        permManager.grantPermission( 'random' )
        permManager.hasPermission( 'random' )
        self.assertTrue( permManager.hasPermission( 'random' ))

    def testHasDeepPermission( self ):
        permManager = PermissionManager( 'group1', self._db )
        permManager.grantPermission( 'group2' )

        permManager = PermissionManager( 'group2', self._db )
        permManager.grantPermission( 'deepPerm' )

        permManager = PermissionManager( 'user', self._db )
        self.assertFalse( permManager.hasPermission( 'group1' ))
        self.assertFalse( permManager.hasPermission( 'deepPerm' ))
        permManager.grantPermission( 'group1' )
        self.assertTrue( permManager.hasPermission( 'group1' ))
        self.assertTrue( permManager.hasPermission( 'deepPerm' ))
        
    def testWrittenToDabatabse( self ):
        permManager = PermissionManager( 'test', self._db )
        permManager.grantPermission( 'perm' )
        try:
            cursor = self._db.cursor()
            cursor.execute( 'SELECT username, action FROM permission' );
            result = cursor.fetchall()
            self.assertEquals( 1, len( result ))
            self.assertEquals( ( 'test', 'perm' ), result[ 0 ])
        finally:
            try: cursor.close()
            except Exception,e: pass


    def testRemovePermission( self ):
        permManager = PermissionManager( 'test', self._db )
        permManager.grantPermission( 'perm1' )
        permManager.grantPermission( 'perm2' )
        permManager.removePermission( 'perm1' )
        self.assertFalse( permManager.hasPermission( 'perm1' ))
        self.assertTrue( permManager.hasPermission( 'perm2' ))
        permManager.removePermission( 'perm2' )
        self.assertFalse( permManager.hasPermission( 'perm2' ))
        self.assertEmptyPermissionsTable()