Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct dpdb main test #26

Merged
merged 10 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:

- name: Install project dependencies
run: |
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pip install .[test]

- name: Lint with flake8
run: |
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def read_files(files, delim: str = "\n") -> str:
'Topic :: Scientific/Engineering :: Visualization',
'Topic :: Multimedia :: Graphics :: Presentation']

tests_require = ['pytest']
tests_require = ['pytest', 'pytest-mock']

setup(name="tdvisu",
version=version,
Expand Down
30 changes: 20 additions & 10 deletions tdvisu/construct_dpdb_visu.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ def db_config(filename: str = 'database.ini',
def query_problem(cur, problem):
cur.execute("SELECT type FROM "
"public.problem WHERE id=%s", (problem,))
return cur.fetchone()
result = cur.fetchone()
return result


def query_num_vars(cur, problem):
cur.execute(
"SELECT num_vertices FROM "
"public.problem WHERE id=%s", (problem,))
return cur.fetchone()[0]
result = cur.fetchone()[0]
return result


def query_sat_clause(cur, problem):
Expand All @@ -121,53 +123,61 @@ def query_sat_clause(cur, problem):
LOGGER.error(
"dpdb.py SHARPSAT needs to be run with '--store-formula'!")
raise
return cur.fetchall()
result = cur.fetchall()
return result


def query_td_bag_grouped(cur, problem):
cur.execute("SELECT bag FROM public.p%d_td_bag GROUP BY bag" % problem)
return cur.fetchall()
result = cur.fetchall()
return result


def query_td_node_status(cur, problem, bag):
cur.execute(
("SELECT start_time,end_time-start_time "
"FROM public.p%d_td_node_status" % problem)
+ " WHERE node=%s", (bag,))
return cur.fetchone()
result = cur.fetchone()
return result


def query_td_bag(cur, problem, bag):
cur.execute(
("SELECT node FROM public.p%d_td_bag" % problem)
+ " WHERE bag=%s", (bag,))
return cur.fetchall()
result = cur.fetchall()
return result


def query_td_node_status_ordered(cur, problem):
cur.execute(
"SELECT node FROM public.p%d_td_node_status ORDER BY start_time" %
problem)
return cur.fetchall()
result = cur.fetchall()
return result


def query_column_name(cur, problem, bag):
cur.execute(
"SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS "
"WHERE TABLE_NAME = 'p%d_td_node_%d'" % (problem, bag))
return cur.fetchall()
result = cur.fetchall()
return result


def query_bag(cur, problem, bag):
cur.execute(
"SELECT * FROM public.p%d_td_node_%d" % (problem, bag))
return cur.fetchall()
result = cur.fetchall()
return result


def query_edgearray(cur, problem):
cur.execute(
"SELECT node,parent FROM public.p%d_td_edge" % problem)
return cur.fetchall()
result = cur.fetchall()
return result


class IDpdbVisuConstruct(metaclass=abc.ABCMeta):
Expand Down
1 change: 0 additions & 1 deletion tdvisu/dbjson9.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"incidenceGraph": {"edges": [{"id": 1, "list": [1, 4, 6]}, {"id": 2, "list": [1, -5]}, {"id": 3, "list": [-1, 7]}, {"id": 4, "list": [2, 3]}, {"id": 5, "list": [2, 5]}, {"id": 6, "list": [2, -6]}, {"id": 7, "list": [3, -8]}, {"id": 8, "list": [4, -8]}, {"id": 9, "list": [-4, 6]}, {"id": 10, "list": [-4, 7]}], "infer_primal": true, "var_name_one": "c_", "var_name_two": "v_"}, "tdTimeline": [[3], [3, [[["v1", "v4"], [0, 0], [1, 1], [0, 1], [1, 0]], "sol bag 3", "", true]], [5, [[["v1", "v4"], [0, 0], [1, 1], [0, 1], [1, 0]], "sol bag 5", "", true]], [4, [[["v1", "v4"], [0, 0], [1, 1], [0, 1], [1, 0]], "sol bag 4", "", true]], [2, [[["v1", "v4"], [0, 0], [1, 1], [0, 1], [1, 0]], "sol bag 2", "", true]], [1, [[["v1", "v4"], [0, 0], [1, 1], [0, 1], [1, 0]], "sol bag 1", "", true]]], "treeDecJson": {"bagpre": "bag %s", "edgearray": [[2, 1], [3, 2], [4, 2], [5, 4]], "labeldict": [{"id": 1, "items": [1, 2, 4, 6], "labels": ["[1, 2, 4, 6]", "dtime=0.0008s"]}, {"id": 2, "items": [1, 2, 4, 6], "labels": ["[1, 2, 4, 6]", "dtime=0.0008s"]}, {"id": 3, "items": [1, 2, 4, 6], "labels": ["[1, 2, 4, 6]", "dtime=0.0008s"]}, {"id": 4, "items": [1, 2, 4, 6], "labels": ["[1, 2, 4, 6]", "dtime=0.0008s"]}, {"id": 5, "items": [1, 2, 4, 6], "labels": ["[1, 2, 4, 6]", "dtime=0.0008s"]}], "num_vars": 8}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"incidenceGraph": {"edges": [{"id": 1, "list": [1, 4, 6]}, {"id": 2, "list": [1, -5]}, {"id": 3, "list": [-1, 7]}, {"id": 4, "list": [2, 3]}, {"id": 5, "list": [2, 5]}, {"id": 6, "list": [2, -6]}, {"id": 7, "list": [3, -8]}, {"id": 8, "list": [4, -8]}, {"id": 9, "list": [-4, 6]}, {"id": 10, "list": [-4, 7]}], "infer_primal": true, "var_name_one": "c_", "var_name_two": "v_"}, "tdTimeline": [[3], [3, [[["v1", "v2"], [1, 1], [1, 0], [0, 1]], "sol bag 3", "", true]], [5, [[["v2", "v8"], [0, 0], [1, 1], [0, 1], [1, 0]], "sol bag 5", "", true]], [4, [[["v2", "v4"], [1, 1], [0, 0], [1, 0], [0, 1]], "sol bag 4", "", true]], [2, [[["v1", "v4"], [0, 0], [1, 1], [0, 1], [1, 0]], "sol bag 2", "", true]], [1, [[["v1", "v4", "v7"], [0, 0, 1], [1, 0, 1], [0, 0, 0], [1, 1, 1], [0, 1, 1]], "sol bag 1", "", true]]], "treeDecJson": {"bagpre": "bag %s", "edgearray": [[2, 1], [3, 2], [4, 2], [5, 4]], "labeldict": [{"id": 1, "items": [1, 4, 7], "labels": ["[1, 4, 7]", "dtime=0.0008s"]}, {"id": 2, "items": [1, 2, 4, 6], "labels": ["[1, 2, 4, 6]", "dtime=0.0009s"]}, {"id": 3, "items": [1, 2, 5], "labels": ["[1, 2, 5]", "dtime=0.0031s"]}, {"id": 4, "items": [2, 4, 8], "labels": ["[2, 4, 8]", "dtime=0.0017s"]}, {"id": 5, "items": [2, 3, 8], "labels": ["[2, 3, 8]", "dtime=0.0016s"]}], "num_vars": 8}}
97 changes: 96 additions & 1 deletion test/test_construct_dpdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@

"""

import argparse
import datetime
import tempfile

from pathlib import Path
import psycopg2 as pg

from tdvisu.construct_dpdb_visu import (read_cfg, db_config, DEFAULT_DBCONFIG,
IDpdbVisuConstruct, DpdbSharpSatVisu,
DpdbSatVisu, DpdbMinVcVisu)
DpdbSatVisu, DpdbMinVcVisu, main)

DIR = Path(__file__).parent
SECTION = 'postgresql'
Expand Down Expand Up @@ -73,3 +79,92 @@ def test_problem_interface():
assert issubclass(DpdbSatVisu, IDpdbVisuConstruct)
assert issubclass(DpdbSharpSatVisu, IDpdbVisuConstruct)
assert issubclass(DpdbMinVcVisu, IDpdbVisuConstruct)


def test_main(mocker):
"""Test behaviour of construct_dpdb_visu.main"""

mock_connect = mocker.patch('tdvisu.construct_dpdb_visu.pg.connect')
ta_status = mock_connect.return_value.__enter__.return_value.get_transaction_status
ta_status.return_value = pg.extensions.TRANSACTION_STATUS_IDLE

query_problem = mocker.patch(
'tdvisu.construct_dpdb_visu.query_problem',
return_value=('Sat',))
query_num_vars = mocker.patch('tdvisu.construct_dpdb_visu.query_num_vars',
return_value=8)
query_td_node_status_ordered= mocker.patch(
'tdvisu.construct_dpdb_visu.query_td_node_status_ordered',
return_value=[(3,), (5,), (4,), (2,), (1,)])
query_sat_clause=mocker.patch(
'tdvisu.construct_dpdb_visu.query_sat_clause',
return_value=[(True, None, None, True, None, True, None, None, None, None),
(True, None, None, None, False,
None, None, None, None, None),
(False, None, None, None, None,
None, True, None, None, None),
(None, True, True, None, None, None, None, None, None, None),
(None, True, None, None, True, None, None, None, None, None),
(None, True, None, None, None,
False, None, None, None, None),
(None, None, True, None, None,
None, None, False, None, None),
(None, None, None, True, None,
None, None, False, None, None),
(None, None, None, False, None,
True, None, None, None, None),
(None, None, None, False, None, None, True, None, None, None)])

query_td_bag_grouped=mocker.patch('tdvisu.construct_dpdb_visu.query_td_bag_grouped',
return_value=[[1, 2, 3, 4, 5]])
query_td_node_status=mocker.patch('tdvisu.construct_dpdb_visu.query_td_node_status', return_value=(
"2020-07-13 02:06:18.053880", datetime.timedelta(microseconds=768)))
query_td_bag=mocker.patch('tdvisu.construct_dpdb_visu.query_td_bag',
return_value=[(1,), (2,), (4,), (6,)])
query_column_name=mocker.patch('tdvisu.construct_dpdb_visu.query_column_name',
return_value=[('v1',), ('v2',), ('v4',), ('v6',)])
query_bag=mocker.patch(
'tdvisu.construct_dpdb_visu.query_bag',
return_value=[(False, None, False, None),
(True, None, True, None),
(False, None, True, None),
(True, None, False, None)])

query_edgearray=mocker.patch('tdvisu.construct_dpdb_visu.query_edgearray',
return_value=[(2, 1), (3, 2), (4, 2), (5, 4)])

parser = argparse.ArgumentParser()
parser.add_argument('problemnumber', type=int,
help="selected problem-id in the postgres-database.")
parser.add_argument('--twfile',
type=argparse.FileType('r', encoding='UTF-8'),
help="tw-file containing the edges of the graph - "
"obtained from dpdb with option --gr-file GR_FILE.")
parser.add_argument('--loglevel', help="set the minimal loglevel for root")
parser.add_argument('--outfile', default='dbjson%d.json',
help="default:'dbjson%%d.json'")
parser.add_argument('--pretty', action='store_true',
help="pretty-print the JSON.")
parser.add_argument('--inter-nodes', action='store_true',
help="calculate and animate the shortest path between "
"successive bags in the order of evaluation.")

with tempfile.TemporaryDirectory() as tmpdirname:
# set cmd-arguments
outfile = str(Path(tmpdirname) / 'test_main.json')
_args = parser.parse_args(['1', '--outfile', outfile])
# one mocked run
main(_args)

# Assertions
mock_connect.assert_called_once()
query_problem.assert_called_once()
query_num_vars.assert_called_once()
query_td_bag_grouped.assert_called_once()
query_sat_clause.assert_called_once()
query_td_node_status_ordered.assert_called_once()
assert query_bag.call_count == 5
assert query_column_name.call_count == 5
assert query_td_bag.call_count == 5
assert query_td_node_status.call_count == 5

2 changes: 1 addition & 1 deletion test/test_svgjoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

WRITE = False # ??? Write Testimages instead of just reading them ???

IMAGE_FOLDER = join('expected_images', 'test_sat_and_join')
IMAGE_FOLDER = join('expected_files', 'test_sat_and_join')
DIR = join(dirname(__file__), IMAGE_FOLDER)
FILE1 = join(dirname(__file__), 'IncidenceGraphStep11.svg')
FILE2 = join(dirname(__file__), 'PrimalGraphStep11.svg')
Expand Down
2 changes: 1 addition & 1 deletion test/test_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from tdvisu.visualization import main

EXPECT_DIR = Path(__file__).parent / 'expected_images'
EXPECT_DIR = Path(__file__).parent / 'expected_files'


def test_sat_and_join(tmpdir):
Expand Down