Пример #1
0
    def test(self):
        uf = UnionFind()
        uf.union('alpha', 'bravo', 'charlie', 'delta')
        uf.consolidate(mysql_db, mysql_table, role='player', type='individual')
        for el in self._select_star():
            self.assertSetEqual(
                set(['role', 'type', '_id', 'parent', 'weight']),
                set(el.keys()))
            self.assertEqual(el['role'], 'player')
            self.assertEqual(el['type'], 'individual')

        uf = UnionFind(mysql_db,
                       mysql_table,
                       'mysql',
                       role='player',
                       type='organization')
        uf.union('adams', 'boston', 'chicago')
        for el in self._select_star():
            if el['type'] == 'organization':
                self.assertIn(el['_id'], ['adams', 'boston', 'chicago'])
            elif el['type'] == 'individual':
                self.assertIn(el['_id'],
                              ['alpha', 'bravo', 'charlie', 'delta'])
            else:
                raise self.failureException
Пример #2
0
class MySQLConsolidateUnionFindTestCase(UnionFindTestCase):
    def setUp(self):
        with mysql_db:
            cur = mysql_db.cursor(MySQLdb.cursors.DictCursor)
            cur.execute('DROP TABLE IF EXISTS %s' % mysql_table)
        self.uf = UnionFind()

    def test_consolidate_mysql(self):
        self.test_deunion()
        self.uf.consolidate(mysql_db, mysql_table)
        # instantiate a new unionfind instance that uses db stuff
        uf2 = UnionFind(mysql_db, mysql_table, 'mysql')

        cur = mysql_db.cursor(MySQLdb.cursors.DictCursor)
        cur.execute('SELECT * FROM %s' % mysql_table)
        for el in cur.fetchall():
            # check if everything has been correctly stored into the db
            assert el['_id'] in self.uf.parents
            assert el['parent'] == self.uf.parents[el['_id']]['parent']
            assert el['weight'] == self.uf.parents[el['_id']]['weight']

            # check if the new unionfind structure, initialized from
            # the db contents, actually contains the same elements
            assert el['_id'] in uf2.parents
            # does this guy have the same root?
            assert self.uf[el['_id']] == uf2[el['_id']]
            # and the same weight?
            assert self.uf.parents[el['_id']]['weight'] == uf2.parents[el['_id']]['weight']


    def test_consolidate_mysql_extra_fields(self):
        self.uf.deunion()
        self.uf.consolidate(mysql_db, mysql_table, gender='male', country='USA', state='NY')
        # instantiate a new unionfind instance that uses db stuff
        uf2 = UnionFind(mysql_db, mysql_table, 'mysql', gender='male', country='USA', state='NY')

        cur = mysql_db.cursor(MySQLdb.cursors.DictCursor)
        cur.execute('SELECT * FROM %s' % mysql_table)
        for el in cur.fetchall():
            # check if everything has been correctly stored into the db
            assert el['_id'] in self.uf.parents
            assert el['parent'] == self.uf.parents[el['_id']]['parent']
            assert el['weight'] == self.uf.parents[el['_id']]['weight']

            # check if the new unionfind structure, initialized from
            # the db contents, actually contains the same elements
            assert el['_id'] in uf2.parents
            # does this guy have the same root?
            assert self.uf[el['_id']] == uf2[el['_id']]
            # and the same weight?
            assert self.uf.parents[el['_id']]['weight'] == uf2.parents[el['_id']]['weight']
Пример #3
0
    def test(self):
        uf = UnionFind()
        uf.union('alpha', 'bravo', 'charlie', 'delta')
        uf.consolidate(mysql_db, mysql_table, role='player', type='individual')
        for el in self._select_star():
            self.assertSetEqual(set(['role', 'type', '_id', 'parent', 'weight']), set(el.keys()))
            self.assertEqual(el['role'], 'player')
            self.assertEqual(el['type'], 'individual')

        uf = UnionFind(mysql_db, mysql_table, 'mysql',  role='player', type='organization')
        uf.union('adams', 'boston', 'chicago')
        for el in self._select_star():
            if el['type'] == 'organization':
                self.assertIn(el['_id'], ['adams', 'boston', 'chicago'])
            elif el['type'] == 'individual':
                self.assertIn(el['_id'], ['alpha', 'bravo', 'charlie', 'delta'])
            else:
                raise self.failureException
Пример #4
0
class MySQLConsolidateUnionFindTestCase(UnionFindTestCase):
    def setUp(self):
        with mysql_db:
            cur = mysql_db.cursor(MySQLdb.cursors.DictCursor)
            cur.execute('DROP TABLE IF EXISTS %s' % mysql_table)
        self.uf = UnionFind()

    def test_consolidate_mysql(self):
        self.test_deunion()
        self.uf.consolidate(mysql_db, mysql_table)
        # instantiate a new unionfind instance that uses db stuff
        uf2 = UnionFind(mysql_db, mysql_table, 'mysql')

        cur = mysql_db.cursor(MySQLdb.cursors.DictCursor)
        cur.execute('SELECT * FROM %s' % mysql_table)
        for el in cur.fetchall():
            # check if everything has been correctly stored into the db
            assert el['_id'] in self.uf.parents
            assert el['parent'] == self.uf.parents[el['_id']]['parent']
            assert el['weight'] == self.uf.parents[el['_id']]['weight']

            # check if the new unionfind structure, initialized from
            # the db contents, actually contains the same elements
            assert el['_id'] in uf2.parents
            # does this guy have the same root?
            assert self.uf[el['_id']] == uf2[el['_id']]
            # and the same weight?
            assert self.uf.parents[el['_id']]['weight'] == uf2.parents[
                el['_id']]['weight']

    def test_consolidate_mysql_extra_fields(self):
        self.uf.deunion()
        self.uf.consolidate(mysql_db,
                            mysql_table,
                            gender='male',
                            country='USA',
                            state='NY')
        # instantiate a new unionfind instance that uses db stuff
        uf2 = UnionFind(mysql_db,
                        mysql_table,
                        'mysql',
                        gender='male',
                        country='USA',
                        state='NY')

        cur = mysql_db.cursor(MySQLdb.cursors.DictCursor)
        cur.execute('SELECT * FROM %s' % mysql_table)
        for el in cur.fetchall():
            # check if everything has been correctly stored into the db
            assert el['_id'] in self.uf.parents
            assert el['parent'] == self.uf.parents[el['_id']]['parent']
            assert el['weight'] == self.uf.parents[el['_id']]['weight']

            # check if the new unionfind structure, initialized from
            # the db contents, actually contains the same elements
            assert el['_id'] in uf2.parents
            # does this guy have the same root?
            assert self.uf[el['_id']] == uf2[el['_id']]
            # and the same weight?
            assert self.uf.parents[el['_id']]['weight'] == uf2.parents[
                el['_id']]['weight']