Skip to content

Commit

Permalink
write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZihengSun committed May 21, 2023
1 parent 71ce280 commit 8211ed7
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pygeoweaver/__pycache__/__init__.cpython-39.pyc
pygeoweaver/__pycache__/__main__.cpython-39.pyc
pygeoweaver/__pycache__/server.cpython-39.pyc
pygeoweaver/__pycache__/utils.cpython-39.pyc
.coverage
5 changes: 4 additions & 1 deletion pygeoweaver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
import webbrowser
from pygeoweaver.constants import GEOWEAVER_DEFAULT_ENDPOINT_URL
from pygeoweaver.utils import checkIPython, checkOS, download_geoweaver_jar, get_root_dir
from pygeoweaver.utils import checkIPython, checkOS, download_geoweaver_jar, get_logger, get_root_dir

"""
This module provides function to start and stop Geoweaver server.
Expand All @@ -11,6 +11,7 @@
"""

logger = get_logger(__name__)

def start(force=False):
download_geoweaver_jar(overwrite=force)
Expand All @@ -31,9 +32,11 @@ def stop():
def show(geoweaver_url = GEOWEAVER_DEFAULT_ENDPOINT_URL):
download_geoweaver_jar() # check if geoweaver is initialized
if checkIPython():
logger.info("enter ipython block")
from IPython.display import IFrame
return IFrame(src=geoweaver_url, width='100%', height='500px')
else:
logger.info("enter self opening block")
webbrowser.open(geoweaver_url)


19 changes: 17 additions & 2 deletions pygeoweaver/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import subprocess
import requests
Expand Down Expand Up @@ -54,7 +55,10 @@ def checkOS():


def checkIPython():
return get_ipython().__class__.__name__ == "ZMQInteractiveShell"
try:
return get_ipython().__class__.__name__ == "ZMQInteractiveShell"
except:
return False


def is_java_installed():
Expand Down Expand Up @@ -90,4 +94,15 @@ def checkJava():
else:
print("Java is not installed. Installing...")
install_java()
print("Java installation complete.")
print("Java installation complete.")


def get_logger(class_name):
logger = logging.getLogger(class_name)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
return logger

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools>=61.0", "wheel"]
requires = ["setuptools>=61.0", "wheel", "pytest-cov"]
build-backend = "setuptools.build_meta"

[project]
Expand Down Expand Up @@ -34,3 +34,6 @@ setuptools = ">=61.0"
requests = "2.28.2"

[tool.poetry.scripts]

[tool.pytest.ini_options]
addopts = "-p no:warnings --cov=pygeoweaver --cov-report=html"
37 changes: 37 additions & 0 deletions test/test_detail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


from io import StringIO
import sys
import unittest
from pygeoweaver.sc_detail import detail_host, detail_process, detail_workflow
from pygeoweaver.utils import get_logger


logger = get_logger(__name__)



class TestDetail(unittest.TestCase):


def test_detail_process(self, capsys):
output_capture = StringIO()
detail_process("not_existing_id")
captured = capsys.readouterr()
output = output_capture.getvalue()
self.assertIn("No host found with id: not_existing_id", output)

@unittest.skip("")
def test_detail_workflow(self):
stdout_capture = StringIO()
detail_workflow("not_existing_id")
output = stdout_capture.getvalue()
self.assertIn("No process found with id: not_existing_id", output)

@unittest.skip("")
def test_detail_host(self):
stdout_capture = StringIO()
detail_host("not_existing_id")
output = stdout_capture.getvalue()
self.assertIn("No workflow found with id: not_existing_id", output)

29 changes: 28 additions & 1 deletion test/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,52 @@
The main function of pygeoweaver
To run in CLI mode.
"""
import logging
from unittest.mock import patch
import requests
from pygeoweaver import start, stop

import unittest

from pygeoweaver.constants import GEOWEAVER_DEFAULT_ENDPOINT_URL
from pygeoweaver.server import show
from pygeoweaver.utils import get_logger


logger = get_logger(__name__)



class TestServer(unittest.TestCase):

def test_server(self):
def test_server_start_stop(self):
start()
response = requests.get(GEOWEAVER_DEFAULT_ENDPOINT_URL)
self.assertEqual(response.status_code, 200, f"Failed to access URL: {GEOWEAVER_DEFAULT_ENDPOINT_URL}")
stop()
with self.assertRaises(requests.exceptions.ConnectionError):
response = requests.get(GEOWEAVER_DEFAULT_ENDPOINT_URL)

stop() # stop again should have no issue


def test_windows(self):
with patch('pygeoweaver.server.checkOS') as mock_checkos:
mock_checkos.return_value = 3
with self.assertRaises(RuntimeError):
start()
with self.assertRaises(RuntimeError):
stop()

def test_show_gui(self):
with patch('pygeoweaver.webbrowser.open') as mock_browser_open:
show()
mock_browser_open.assert_called_once()

with patch('pygeoweaver.server.checkIPython') as mock_checkipython:
mock_checkipython.return_value = True
show()
mock_browser_open.assert_called_once()

if __name__ == "__main__":
unittest.main()

0 comments on commit 8211ed7

Please sign in to comment.