Unit Tests

Unit testing is done with pytest. To run all the tests, simply cd to the top level directory and type this shell command:

$ pytest

The tests are all in the tests directory:

  • test_optipass.py has functions that test the interface to OptiPass

You can run one set of tests by including the file name in the shell command, e.g.

$ pytest test/test_optipass.py

Test the OP Class

The OP class is the abstract interface to the op-server REST server.

When the main application starts it calls the setup method. That method sends HTTP requests to the server and it saves the returned values so they are accessible to static methods of the OP class.

To test setup we use a library named responses. It allows us to "short-circuit" the HTTP request. Whenever setup tries to send a URL, the responses module catches it and returns a response to use for the test.

Here's an example. To get the list of project names from the server, the setup method has this code:

      req = f'{server}/projects'
      resp = requests.get(req)

In the test, we have this code:

    path_to_projects = f'{server}/projects'
    responses.get(path_to_projects, json = ["demo"])

What that means is "whenever there is an HTTP request to a URL of the form f'{server}/projects' reply with the JSON form of the list ["demo"]. In other words, trick the setup code into thinking it connected to a server that has the demo data.

The body of the test_setup method defines responses for all of the requests that setup will try to send. It then calls setup, and when it returns the test code will check all of the attributes of the OP class to make sure they have the expected values.

test_setup(barriers, targets, layout, mapinfo)

Test the attributes saved by the setup method.

Source code in test/test_optipass.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
@responses.activate
def test_setup(barriers, targets, layout, mapinfo):
    '''
    Test the attributes saved by the setup method.
    '''
    server = 'http://localhost:8000'

    path_to_projects = f'{server}/projects'
    responses.get(path_to_projects, json = ["demo"])

    with open(barriers) as f:
        path_to_barriers = f'{server}/barriers/demo'
        responses.get(path_to_barriers, json = {"project":"demo", "barriers":f.read()})

    with open(targets) as f:
        target_csv = f.read()
    with open(layout) as f:
        target_layout = f.read()
    path_to_targets = f'{server}/targets/demo'
    responses.get(path_to_targets, json = {"project":"demo", "targets":target_csv, "layout": target_layout})

    path_to_colnames = f'{server}/colnames/demo'
    responses.get(path_to_colnames, json = {"name":None, "files":["colnames.csv"]})

    with open(mapinfo) as f:
        path_to_mapinfo = f'{server}/mapinfo/demo'
        responses.get(path_to_mapinfo, json = {"project":"demo", "mapinfo":f.read()})

    OP.setup(server, 'demo', 1)

    assert OP.server_url == 'http://localhost:8000'
    assert OP.project_name == 'demo'
    assert OP.initial_tab == 1

    assert len(OP.target_frame) == 2
    assert list(OP.target_frame.columns) == ['long','short','label','infra']
    assert list(OP.target_frame.index) == ['T1','T2']

    assert OP.target_layout == ['T1 T2']

    assert OP.mapping_name is None
    assert OP.target_columns == ['colnames.csv']

    assert OP.mapinfo['map_type'] == 'StaticMap'
    assert OP.mapinfo['map_file'] == 'Riverlands.png'
    assert OP.mapinfo['map_title'] == 'The Riverlands'

    assert OP.region_names == ['Red Fork','Trident']

    assert len(OP.barrier_frame) == 6
    assert list(OP.barrier_frame.index) == list('ABCDEF')
    assert len(OP.barrier_frame.columns) == 8
    assert round(OP.barrier_frame.cost.sum()) == 590000

    assert sorted(OP.total_cost.keys()) == OP.region_names
    assert round(sum(OP.total_cost.values())) ==  round(OP.barrier_frame.cost.sum())

    with pytest.raises(AttributeError) as err:
        OP.initial_tab = 2
    assert 'no setter' in str(err)

Widget Tests

TBD