Beispiel #1
0
def setup_table():
    """ Creates a fake_provider table to test with. """
    db_conn = make_WHConnection()
    Table('fake_provider', db_conn.metadata,
          Column('id', Integer, primary_key=True),
          Column('ref', String, unique=True), Column('name', String))
    with db_conn.engine.begin() as conn:
        db_conn.metadata.create_all(db_conn.engine)
Beispiel #2
0
def test_context_manager_commit():
    db_conn = make_WHConnection()
    table = db_conn.get_table('fake_provider')

    with db_conn.trans() as wht:
        rows_from_before = len(wht.get_data(table.select()))
        wht.insert_single_entry({'ref': 'tr', 'name': 'Tru'}, table)
    with db_conn.trans() as wht:
        rows_after_commit = len(wht.get_data(table.select()))
    assert_equal(rows_from_before + 1, rows_after_commit)
Beispiel #3
0
def test_context_manager_rollback():
    db_conn = make_WHConnection()
    table = db_conn.get_table('fake_provider')
    try:
        with db_conn.trans() as wht:
            rows_from_before = len(wht.get_data(table.select()))
            wht.insert_single_entry({'ref': 'fk', 'name': 'Fake'}, table)
            raise ValueError()
    except ValueError:
        with db_conn.trans() as wht:
            rows_after_rollback = len(wht.get_data(table.select()))
            assert_equal(rows_from_before, rows_after_rollback)
Beispiel #4
0
def test_insert_duplicate_manage():
    db_conn = make_WHConnection()
    table = db_conn.get_table('fake_provider')
    entries_1 = mk_test_entries(10, 'a', 'ABC')
    entries_2 = mk_test_entries(5, 'a', 'ABC')
    entries_3 = mk_test_entries(10, 'b', 'BCD')
    entries = entries_1 + entries_2 + entries_3
    db_conn.chunk_insert_manage_duplicates(entries, table, chunk_size=10)
    with db_conn.trans() as wht:
        data = wht.get_data('select ref, name from fake_provider')
        assert_list_equal(data, [(e['ref'], e['name'])
                                 for e in entries_1 + entries_3])
Beispiel #5
0
def test_cm_bulk_insert():
    db_conn = make_WHConnection()
    table = db_conn.get_table('fake_provider')
    test_entries = mk_test_entries(1000)

    with db_conn.trans() as trans:
        trans.bulk_insert(test_entries, table)
    with db_conn.trans() as trans:
        count = trans.get_data('SELECT count(*) FROM fake_provider')[0][0]
        assert_equal(len(test_entries), count)
    with assert_raises(RowDuplicateError):
        with db_conn.trans() as trans:
            trans.bulk_insert(test_entries, table)
    with db_conn.trans() as trans:
        count = trans.get_data('SELECT count(*) FROM fake_provider')[0][0]
        assert_equal(len(test_entries), count)
Beispiel #6
0
def test_cm_insert_single_entry():
    db_conn = make_WHConnection()
    table = db_conn.get_table('fake_provider')
    entry = {'ref': 'test_cm', 'name': 'Test CM'}
    with db_conn.trans() as wht:
        pk = wht.insert_single_entry(entry, table)
    with db_conn.trans() as wht:
        rows = wht.get_data(text('SELECT * FROM fake_provider WHERE id = :pk'),
                            pk=pk)
        assert_equal(1, len(rows))
        assert_equal(entry['ref'], rows[0]['ref'])
        assert_equal(entry['name'], rows[0]['name'])
    try:
        with db_conn.trans() as wht:
            wht.insert_single_entry(entry, table)
            assert False
    except RowDuplicateError:
        assert True
Beispiel #7
0
def test_dbc_insert():
    test_entries = mk_test_entries()
    new_test_entries = mk_test_entries(10, 'a', 'ABC')
    new_test_entries.extend(test_entries[:15])
    db_conn = make_WHConnection()
    table = db_conn.get_table('fake_provider')

    db_conn.insert_data_cm(test_entries,
                           table,
                           bulk=True,
                           auto_separate_insert=False)  #this should pass
    with db_conn.trans() as wht:
        count = wht.get_data('SELECT count(*) FROM fake_provider')[0][0]
        assert_equal(len(test_entries), count)

    # Try bulk insert on data partially in db (should fail)
    with assert_raises(RowDuplicateError):
        db_conn.insert_data_cm(new_test_entries,
                               table,
                               bulk=True,
                               auto_separate_insert=False)
    with db_conn.trans() as wht:
        count = wht.get_data('SELECT count(*) FROM fake_provider')[0][0]
        assert_equal(len(test_entries), count)

    # Try row-by-row insert on data partially in db (should insert 10 of 25)
    db_conn.insert_data_cm(new_test_entries, table, bulk=False)
    with db_conn.trans() as wht:
        count = wht.get_data('SELECT count(*) FROM fake_provider')[0][0]
        assert_equal(len(test_entries) + 10, count)

    # Try bulk insert on data partially in DB, and then row-by-row insert on fail.
    new_test_entries.extend(mk_test_entries(5, 'd', 'DEF'))
    db_conn.insert_data_cm(new_test_entries,
                           table,
                           bulk=True,
                           auto_separate_insert=True)
    with db_conn.trans() as wht:
        count = wht.get_data('SELECT count(*) FROM fake_provider')[0][0]
        assert_equal(len(test_entries) + 10 + 5, count)
    def setUp(self):
        #create tables if missing
        self.db_conn = make_WHConnection()
        create_tables(self.db_conn.engine)

        #create a fake provider
        self.provider_ids_to_drop = []
        example_provider = {'ref': 'zyx.pl', 'name': 'Mr. Zyx'}
        provider_table = self.db_conn.get_table('provider')
        try:
            with self.db_conn.trans() as trans:
                p_pk = trans.insert_single_entry(example_provider,
                                                 provider_table)
                self.provider_ids_to_drop.append(p_pk)
        except RowDuplicateError:  # then we wouldn't drop that one
            with self.db_conn.trans() as trans:
                p_pk = trans.get_data(
                    'select id from provider where ref = :ref',
                    ref=example_provider['ref'])[0][0]
        #create a set of fake stations
        self.station_ids_to_drop = []
        station_table = self.db_conn.get_table('station')
        for i in range(1, 51):
            entry = {
                'ref': 'zyx.pl.%d' % i,
                'name': 'Zyx %d station',
                'lon': 0.1 * i,
                'lat': 50 + i,
                'provider_id': p_pk,
                'ref_provider': '%d_ZYX' % i
            }
            try:
                with self.db_conn.trans() as trans:
                    s_pk = trans.insert_single_entry(entry, station_table)
                    self.station_ids_to_drop.append(s_pk)
            except RowDuplicateError:
                pass

        self.finder = StationFinder(self.db_conn)
Beispiel #9
0
def teardown_table():
    """ Drops the fake_provider table created in setup() """
    db_conn = make_WHConnection()
    with db_conn.engine.begin() as conn:
        conn.execute('DROP TABLE fake_provider')
Beispiel #10
0
    def setUp(self):
        self.db_conn = make_WHConnection()
        create_tables(self.db_conn.engine)

        self.points = [
            {
                'lat': 49.220014,
                'lon': 20.011048,
                'i': None,
                'j': None
            },  #swinica
            {
                'lat': 49.219201,
                'lon': 20.016541,
                'i': None,
                'j': None
            },  #zawrat
            {
                'lat': 49.225046,
                'lon': 19.992273,
                'i': None,
                'j': None
            },  #liliowe
            {
                'lat': 49.232760,
                'lon': 19.982821,
                'i': None,
                'j': None
            },  #PKL Kasprowy
            {
                'lat': 49.242626,
                'lon': 20.007191,
                'i': None,
                'j': None
            },  #Murowaniec
            {
                'lon': 0,
                'lat': 0,
                'i': 100,
                'j': 51
            },
            {
                'lon': 0.02,
                'lat': 0,
                'i': 101,
                'j': 51
            },
            {
                'lon': 0,
                'lat': 0.02,
                'i': 100,
                'j': 52
            },
            {
                'lon': 0.02,
                'lat': 0.02,
                'i': 101,
                'j': 52
            }
        ]
        self.stations = [{
            'lat': 49.2324,
            'lon': 19.9819,
            'ref': 'kas',
            'name': 'Kasprowy Wierch Observatory'
        }, {
            'lon': 0.005,
            'lat': 0.005,
            'ref': 'is.left.down',
            'name': 'Left Down'
        }, {
            'lon': 0.015,
            'lat': 0.005,
            'ref': 'is.right.down',
            'name': 'Right Down'
        }, {
            'lon': 0.005,
            'lat': 0.015,
            'ref': 'is.left.up',
            'name': 'Left Up'
        }, {
            'lon': 0.015,
            'lat': 0.015,
            'ref': 'is.right.up',
            'name': 'Right Up'
        }]

        with self.db_conn.trans() as wht:
            self.schedule_id = wht.insert_single_entry(
                {'ref': 'test'}, self.db_conn.get_table('schedule'))
            self.provider_id = wht.insert_single_entry(
                {
                    'ref': 'PF',
                    'name': 'PointFinder'
                }, self.db_conn.get_table('provider'))
            for station in self.stations:
                station.update({'provider_id': self.provider_id})
                wht.insert_single_entry(station,
                                        self.db_conn.get_table('station'))

            for i, point in enumerate(self.points):
                point_id = wht.insert_single_entry(
                    {
                        'lon': point['lon'],
                        'lat': point['lat']
                    }, self.db_conn.get_table('grid_point'))
                wht.insert_single_entry(
                    {
                        'point_id': point_id,
                        'schedule_id': self.schedule_id,
                        'i': point['i'],
                        'j': point['j']
                    }, self.db_conn.get_table('grid'))
                self.points[i].update({'point_id': point_id})