Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 24, 2023
1 parent 6a00252 commit 6e28660
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 43 deletions.
29 changes: 17 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import multiprocessing as mp
import time

import ase.build
import ase.collections
import pytest
from zndraw.app import create_app, socketio
from zndraw.utils import get_port
import multiprocessing as mp
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time

from zndraw import ZnDraw
from zndraw.app import create_app, socketio
from zndraw.utils import get_port


@pytest.fixture
Expand All @@ -21,24 +22,29 @@ def water() -> ase.Atoms:
def ase_s22() -> list[ase.Atoms]:
return list(ase.collections.s22)


@pytest.fixture()
def setup(request):
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument("--headless")
options.add_argument("--disable-3d-apis")
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
driver = webdriver.Chrome(
service=ChromeService(ChromeDriverManager().install()), options=options
)
request.cls.driver = driver
yield request.cls.driver
request.cls.driver.close()


@pytest.fixture()
def server():

port = get_port()

def run_server():
app = create_app(None, False, True)
socketio.run(app, port=port, debug=False, host="0.0.0.0") # NEVER EVER USE DEBUG=TRUE HERE!!!
socketio.run(
app, port=port, debug=False, host="0.0.0.0"
) # NEVER EVER USE DEBUG=TRUE HERE!!!

server_proc = mp.Process(
target=run_server,
Expand All @@ -50,11 +56,10 @@ def run_server():
server_proc.terminate()
server_proc.join()


@pytest.fixture()
def vis(server) -> ZnDraw:
vis = ZnDraw(url=server)
# vis = ZnDraw(url="https://zndraw.icp.uni-stuttgart.de/", token="18f6d530e74246a9b96cca91f7fc55bc")
yield vis
# vis.close()


25 changes: 11 additions & 14 deletions tests/test_server.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from typing import Any
import pytest
import multiprocessing as mp
import time

import pytest
from ase.build import molecule
from selenium.webdriver.support.wait import WebDriverWait

from zndraw import ZnDraw
from zndraw.zndraw import ZnDrawDefault
import multiprocessing as mp


# @pytest.mark.chrome
@pytest.mark.usefixtures("setup")
class TestZnDraw:

def test_title(self, server):
self.driver.get(server)
assert self.driver.title == "ZnDraw"

def test_vis(self, server):
self.driver.get(server)
proc = mp.Process(target=ZnDrawDefault,
proc = mp.Process(
target=ZnDrawDefault,
kwargs={"url": server, "token": "default"},
)
proc.start()
Expand All @@ -28,14 +29,13 @@ def test_vis(self, server):
assert vis.socket.connected
for idx in range(10):
vis[idx] = molecule("H2O")

assert len(vis) == 10

finally:
proc.terminate()
proc.join()


# vis[0] = molecule("H2O")
# assert vis.socket.connected
# assert vis[0] == molecule("H2O")
Expand Down Expand Up @@ -67,14 +67,11 @@ def test_vis(self, server):
# return True
# except:
# return False
# # return True

# wait.until(WaitUntil())
# # return True


# wait.until(WaitUntil())

# assert vis.socket.connected
# vis[0] = molecule("H2O")
# assert vis.socket.connected
# assert vis[0] == molecule("H2O")

6 changes: 4 additions & 2 deletions zndraw/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import uuid

from flask import Flask
from flask_socketio import SocketIO
import uuid

socketio = SocketIO()


def create_app(use_token, upgrade_insecure_requests, compute_bonds) -> Flask:
"""Create the Flask app."""

Expand All @@ -18,8 +20,8 @@ def create_app(use_token, upgrade_insecure_requests, compute_bonds) -> Flask:
app.config["upgrade_insecure_requests"] = upgrade_insecure_requests
app.config["compute_bonds"] = compute_bonds


from .server import main as main_blueprint

app.register_blueprint(main_blueprint)

socketio.init_app(app, cors_allowed_origins="*")
Expand Down
4 changes: 1 addition & 3 deletions zndraw/server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

# example https://github.com/miguelgrinberg/Flask-SocketIO-Chat

from flask import Blueprint

main = Blueprint('main', __name__)
main = Blueprint("main", __name__)

from . import routes, events
13 changes: 6 additions & 7 deletions zndraw/server/events.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from ..app import socketio as io
from flask import request, session
from flask import current_app as app
from flask_socketio import join_room, emit, call
import contextlib
import logging

from flask import current_app as app
from flask import request, session
from flask_socketio import call, emit, join_room

from ..app import socketio as io

log = logging.getLogger(__name__)


Expand Down Expand Up @@ -56,8 +58,6 @@ def disconnect():
)




@io.on("join")
def join(token):
# only used by pyclients that only connect via socket (no HTML)
Expand All @@ -68,7 +68,6 @@ def join(token):
app.config["DEFAULT_PYCLIENT"] = request.sid



@io.on("scene:schema")
def scene_schema():
import enum
Expand Down
12 changes: 7 additions & 5 deletions zndraw/server/routes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

from flask import render_template, session, redirect
from flask import current_app
import logging
import uuid

from flask import current_app, redirect, render_template, session

from . import main
import logging

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -32,11 +32,13 @@ def token(token):
session["token"] = token
return redirect("/")


@main.route("/exit")
def exit_route():
"""Exit the session."""
log.critical("Server shutting down...")

from ..app import socketio

socketio.stop()
return "Server shutting down..."
return "Server shutting down..."

0 comments on commit 6e28660

Please sign in to comment.