Skip to content

Commit

Permalink
Fix tests and CI/CD workflows
Browse files Browse the repository at this point in the history
For consistent test results, teardown code was added to the pytest setup
of the flask app.
The CI/CD workflows were updated to use the latest versions of the
actions.
The path of the colorPalette.json file was changed and the file is now
included in the setup file of the backend.
  • Loading branch information
stephanzwicknagl committed Feb 2, 2024
1 parent 034e620 commit c0f05e0
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 62 deletions.
54 changes: 27 additions & 27 deletions .github/workflows/build_and_test.yml
Expand Up @@ -14,15 +14,15 @@ jobs:
runs-on: "ubuntu-latest"
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Cache dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('requirements.txt') }}
Expand All @@ -37,16 +37,16 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: '19'
node-version: '20'

- name: Cache node_modules
id: cache-node-modules
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ./frontend/node_modules
key: node-modules-${{ hashFiles('./frontend/package-lock.json') }}
Expand All @@ -61,27 +61,27 @@ jobs:
needs: [ python, node ]
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Get python packages from cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('requirements.txt') }}
id: cache

- name: Setup node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: '19'
node-version: '20'
- name: Get node from cache
id: cache-node-modules
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ./frontend/node_modules
key: node-modules-${{ hashFiles('./frontend/package-lock.json') }}
Expand All @@ -102,7 +102,7 @@ jobs:
kill -9 $PID
- name: Upload frontend to GitHub
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
retention-days: 7
name: frontend-${{ github.sha }}
Expand All @@ -114,15 +114,15 @@ jobs:
needs: python
steps:
- name: Checking out repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Get python packages from cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('requirements.txt') }}
Expand All @@ -133,7 +133,7 @@ jobs:
run: python -m build --sdist --wheel --outdir dist/;

- name: Upload backend to GitHub
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
retention-days: 7
name: backend-${{ github.sha }}
Expand All @@ -145,19 +145,19 @@ jobs:
needs: build-backend
steps:
- name: Checking out repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Get artifact
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: backend-${{ github.sha }}

- name: Setup Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Get python packages from cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('requirements.txt') }}
Expand All @@ -184,23 +184,23 @@ jobs:
needs: [ build-backend, frontend ]
steps:
- name: Checking out repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Get backend artifact
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: backend-${{ github.sha }}
- name: Get frontend artifact
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: frontend-${{ github.sha }}

- name: Setup Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Get python packages from cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('requirements.txt') }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish_pypi.yml
Expand Up @@ -64,12 +64,12 @@ jobs:
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Cache conda env
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('requirements.txt') }}
Expand Down
3 changes: 3 additions & 0 deletions backend/setup.cfg
Expand Up @@ -29,6 +29,9 @@ install_requires =
[options.packages.find]
where = src

[options.package_data]
viasp = server/colorPalette.json

[options.entry_points]
console_scripts =
viasp_server = viasp.__main__:backend
Expand Down
File renamed without changes.
30 changes: 15 additions & 15 deletions backend/src/viasp/server/startup.py
Expand Up @@ -15,22 +15,23 @@
import shutil
from subprocess import Popen
from time import time
import json

import viasp_dash
from dash import Dash, jupyter_dash
from dash._jupyter import _jupyter_config

from viasp import clingoApiClient
from viasp.shared.defaults import (DEFAULT_BACKEND_HOST, DEFAULT_BACKEND_PORT,
DEFAULT_BACKEND_PROTOCOL, CLINGRAPH_PATH,
GRAPH_PATH, PROGRAM_STORAGE_PATH, STDIN_TMP_STORAGE_PATH,
COLOR_PALETTE)
DEFAULT_BACKEND_PROTOCOL, CLINGRAPH_PATH,
GRAPH_PATH, PROGRAM_STORAGE_PATH,
STDIN_TMP_STORAGE_PATH, COLOR_PALETTE_PATH)



def run(host=DEFAULT_BACKEND_HOST, port=DEFAULT_BACKEND_PORT):
""" create the dash app, set layout and start the backend on host:port """

# if running in binder, get proxy information
# and set the backend URL, which will be used
# by the frontend
Expand All @@ -40,17 +41,16 @@ def run(host=DEFAULT_BACKEND_HOST, port=DEFAULT_BACKEND_PORT):
_default_server_url = _jupyter_config['server_url']

_default_requests_pathname_prefix = (
_jupyter_config['base_subpath'].rstrip('/') + '/proxy/' + str(port)
)
_jupyter_config['base_subpath'].rstrip('/') + '/proxy/' +
str(port))

backend_url = _default_server_url+_default_requests_pathname_prefix
backend_url = _default_server_url + _default_requests_pathname_prefix
elif 'google.colab' in sys.modules:
from google.colab.output import eval_js # type: ignore
backend_url=eval_js(f"google.colab.kernel.proxyPort({port})")
from google.colab.output import eval_js # type: ignore
backend_url = eval_js(f"google.colab.kernel.proxyPort({port})")
else:
backend_url = f"{DEFAULT_BACKEND_PROTOCOL}://{host}:{port}"


command = ["viasp_server", "--host", host, "--port", str(port)]

# if 'ipykernel_launcher.py' in sys.argv[0]:
Expand All @@ -60,12 +60,12 @@ def run(host=DEFAULT_BACKEND_HOST, port=DEFAULT_BACKEND_PORT):
log = open('viasp.log', 'w', encoding="utf-8")
viasp_backend = Popen(command, stdout=log, stderr=log)

color_palette = json.load(
open(COLOR_PALETTE_PATH, "r"))
app = Dash(__name__)
app.layout = viasp_dash.ViaspDash(
id="myID",
backendURL=backend_url,
colorPalette=COLOR_PALETTE
)
app.layout = viasp_dash.ViaspDash(id="myID",
backendURL=backend_url,
colorPalette=color_palette)
app.title = "viASP"

# make sure the backend is up, before continuing with other modules
Expand Down
6 changes: 3 additions & 3 deletions backend/src/viasp/shared/defaults.py
@@ -1,6 +1,5 @@
import pathlib
import os
import json

DEFAULT_BACKEND_PROTOCOL = "http"
DEFAULT_BACKEND_HOST = "localhost"
Expand All @@ -9,8 +8,9 @@
DEFAULT_BACKEND_URL = f"{DEFAULT_BACKEND_PROTOCOL}://{DEFAULT_BACKEND_HOST}:{DEFAULT_BACKEND_PORT}"
SHARED_PATH = pathlib.Path(__file__).parent.resolve()
GRAPH_PATH = SHARED_PATH / "viasp_graph_storage.db"
STATIC_PATH = pathlib.Path(__file__).parent.parent.resolve() / "server/static/"
SERVER_PATH = pathlib.Path(__file__).parent.parent.resolve() / "server/"
STATIC_PATH = os.path.join(SERVER_PATH, "static")
CLINGRAPH_PATH = os.path.join(STATIC_PATH, "clingraph")
PROGRAM_STORAGE_PATH = SHARED_PATH / "prg.lp"
STDIN_TMP_STORAGE_PATH = SHARED_PATH / "viasp_stdin_tmp.lp"
COLOR_PALETTE = json.load(open(os.path.join(STATIC_PATH, "colorPalette.json"), "r"))
COLOR_PALETTE_PATH = SERVER_PATH / "colorPalette.json"
29 changes: 23 additions & 6 deletions backend/test/conftest.py
Expand Up @@ -146,23 +146,40 @@ def client_with_a_single_node_graph(get_sort_program_and_get_all_graphs, a_1) ->


@pytest.fixture(params=["program_simple", "program_multiple_sorts", "program_recursive"])
def client_with_a_graph(request, get_sort_program_and_get_all_graphs) -> Generator[Tuple[FlaskClient, ProgramAnalyzer, List[Tuple[Mapping, str, str, int]], str], Any, Any]:
def client_with_a_graph(
request, get_sort_program_and_get_all_graphs
) -> Generator[Tuple[FlaskClient, ProgramAnalyzer, List[Tuple[
Mapping, str, str, int]], str], Any, Any]:
app = create_app_with_registered_blueprints(app_bp, api_bp, dag_bp)

program = request.getfixturevalue(request.param)
serializable_graphs, analyzer = get_sort_program_and_get_all_graphs(program)
serializable_graphs, analyzer = get_sort_program_and_get_all_graphs(
program)
with app.test_client() as client:
for serializable_graph, hash, sorted_program, _ in serializable_graphs:
client.post("graph", json={"data": serializable_graph, "hash": hash, "sort": sorted_program})
client.post("graph",
json={
"data": serializable_graph,
"hash": hash,
"sort": sorted_program
})
yield client, analyzer, serializable_graphs, program
_ = client.delete("/control/clingraph")
_ = client.post("/control/models/clear")
_ = client.delete("/graph/clear")

@pytest.fixture
def client_with_a_clingraph(client_with_a_graph, get_clingo_stable_models) -> Generator[Tuple[FlaskClient, ProgramAnalyzer, List[Tuple[Mapping, str, str, int]], str], Any, Any]:
def client_with_a_clingraph(
client_with_a_graph, get_clingo_stable_models
) -> Generator[Tuple[FlaskClient, ProgramAnalyzer, List[Tuple[
Mapping, str, str, int]], str], Any, Any]:
client, analyzer, serializable_graphs, program = client_with_a_graph

_ = client.delete("/control/clingraph")
serialized = get_clingo_stable_models(program)
_ = client.post("/control/models", json=serialized, headers={'Content-Type': 'application/json'})
print(f"Marking models: {serialized}", flush=True)
_ = client.post("/control/models",
json=serialized,
headers={'Content-Type': 'application/json'})

yield client, analyzer, serializable_graphs, program

Expand Down
16 changes: 9 additions & 7 deletions backend/test/test_clingraph.py
Expand Up @@ -98,13 +98,15 @@ def test_clingraph_edges(client_with_a_clingraph):
assert res.status_code == 200
assert res.data == b'ok'

res = client.post(f"/graph/edges",
json={
"shownRecursion": [],
"usingClingraph": "true"
})
assert res.status_code == 200
assert type(res.json) == list
if "{b(X)}" in program:
res = client.post(f"/graph/edges",
json={
"shownRecursion": [],
"usingClingraph": "true"
})
assert res.status_code == 200
assert type(res.json) == list
# program_simple and program_multiple_sorts
assert len(res.json) == 12
else:
assert len(res.json) == 2
2 changes: 1 addition & 1 deletion docs/viasp/colorPalette.rst
Expand Up @@ -2,7 +2,7 @@
Color Palette
=================

The color Palette of viASP's frontend is defined by the file `/server/static/colorPalette.json`.
The color Palette of viASP's frontend is defined by the file `/server/colorPalette.json`.

The default file contains the following JSON object:

Expand Down
4 changes: 4 additions & 0 deletions examples/quickstart.py
Expand Up @@ -19,6 +19,10 @@ def main():
ctl.viasp.mark(m)
print(handle.get())
ctl.viasp.show()
ctl.viasp.clingraph(viz_encoding="viz_hamiltonian.lp",
engine="dot",
graphviz_type="graph")



app = startup.run()
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/demo/App.js
@@ -1,6 +1,6 @@
/* eslint no-magic-numbers: 0 */
import React from 'react';
import colorPaletteData from '../../../backend/src/viasp/server/static/colorPalette.json';
import colorPaletteData from '../../../backend/src/viasp/server/colorPalette.json';

import { ViaspDash } from '../lib';

Expand Down

0 comments on commit c0f05e0

Please sign in to comment.