File: test_db.py

package info (click to toggle)
pyroute2 0.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,700 kB
  • sloc: python: 50,245; makefile: 280; javascript: 183; ansic: 81; sh: 44; awk: 17
file content (44 lines) | stat: -rw-r--r-- 1,093 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import sqlite3

import pytest

from pyroute2 import NDB

try:
    import psycopg2
except ImportError:
    pytest.skip('no psycopg2 module installed', allow_module_level=True)


def test_no_cleanup(spec):
    # start and stop the DB, leaving all the data in the DB file
    NDB(
        db_provider='sqlite3',
        db_spec=spec.db_spec,
        db_cleanup=False,
        log=spec.log_spec,
    ).close()

    # open the DB file
    db = sqlite3.connect(spec.db_spec)
    cursor = db.cursor()
    cursor.execute('SELECT * FROM interfaces')
    interfaces = cursor.fetchall()

    # at least two records: idx 0 and loopback
    assert len(interfaces) > 1
    # all the interfaces must be of the same source, 'localhost'
    assert set([x[0] for x in interfaces]) == set(('localhost',))


def test_postgres_fail(spec):
    try:
        NDB(
            db_provider='postgres',
            db_spec={'dbname': 'some-nonsense-db-name'},
            log=spec.log_spec,
        ).close()
    except psycopg2.OperationalError:
        return

    raise Exception('postgresql exception was expected')