diff --git a/src/aki_prj23_transparenzregister/ui/assets/typography.css b/src/aki_prj23_transparenzregister/ui/assets/typography.css index 699a279..c8129d3 100644 --- a/src/aki_prj23_transparenzregister/ui/assets/typography.css +++ b/src/aki_prj23_transparenzregister/ui/assets/typography.css @@ -1,3 +1,7 @@ body { margin: 0; } + +.normal_h3 { + margin: 0; +} diff --git a/src/aki_prj23_transparenzregister/ui/pages/about.py b/src/aki_prj23_transparenzregister/ui/pages/about.py index 3a364bc..144e536 100644 --- a/src/aki_prj23_transparenzregister/ui/pages/about.py +++ b/src/aki_prj23_transparenzregister/ui/pages/about.py @@ -19,7 +19,7 @@ __version__: Final[str] = _DISTRIBUTION_METADATA["Version"] @lru_cache -def layout() -> list[html]: +def layout() -> html.Div: """Defines the layout of the about page. Returns: @@ -30,22 +30,25 @@ def layout() -> list[html]: git_revision = git_revision.replace(_, "") git_revision = git_revision.strip() - return [ - header_elements.create_selection_header("About"), - html.H3("About Us"), - html.H3("Description"), - html.P(_DISTRIBUTION_METADATA["Summary"]), - html.H3("Key Features"), - html.Ul( - [ - html.Li("Some feature."), - ] - ), - html.H3("Version"), - html.Ul( - [ - html.Li(f"Software Version: {__version__}"), - html.Li(f"Build from GiT Revision: {git_revision}"), - ] - ), - ] + + return html.Div( + children=[ + header_elements.create_selection_header("About"), + html.H3("About Us", className="normal_h3"), + html.H3("Description"), + html.P(_DISTRIBUTION_METADATA["Summary"]), + html.H3("Key Features", className="normal_h3"), + html.Ul( + [ + html.Li("Some feature."), + ] + ), + html.H3("Version", className="normal_h3"), + html.Ul( + [ + html.Li(f"Software Version: {__version__}"), + html.Li(f"Build from GiT Revision: {git_revision}"), + ] + ), + ] + ) diff --git a/src/aki_prj23_transparenzregister/ui/pages/search_queue.py b/src/aki_prj23_transparenzregister/ui/pages/search_queue.py new file mode 100644 index 0000000..0dddd94 --- /dev/null +++ b/src/aki_prj23_transparenzregister/ui/pages/search_queue.py @@ -0,0 +1,66 @@ +"""A page giving some details about this application including versioning.""" +from functools import lru_cache + +import dash +from dash import Input, Output, callback, dcc, html +from dash._callback import NoUpdate + +from aki_prj23_transparenzregister.ui import header_elements +from aki_prj23_transparenzregister.ui.session_handler import SessionHandler +from aki_prj23_transparenzregister.utils.sql import entities + +dash.register_page(__name__, path_template="/search/", title="Add data mining Job") + + +@lru_cache +def layout() -> html.Div: + """Defines the layout of the about page. + + Returns: + A list of html element making up this page. + """ + return html.Div( + children=[ + header_elements.create_selection_header("Search more companies"), + html.H3( + "Please enter a Company to add to the search queue.", + className="normal_h3", + ), + html.P( + "If a term is entered to the search queue data for that company will be searched for on the next run." + ), + html.P( + "If the company was already searched for this is being reset and the priority is increased." + ), + html.P( + "Please note that it can take a day until your queried data is mined and processed." + ), + dcc.Input(id="input-text", type="text", placeholder="", debounce=True), + html.P("", id="response"), + ] + ) + + +@callback( + Output("response", "children"), + Input("input-text", "value"), +) +def add_company_to_search_que(input_text: str | None) -> NoUpdate | list[str | html.B]: + """Adds a company to the search que.""" + if not input_text: + return dash.no_update + db = SessionHandler.session + if not db: + raise ValueError("No session created.") + db_result = ( + db.query(entities.MissingCompany) + .filter(entities.MissingCompany.name == input_text) + .first() + ) + if db_result: + db_result.number_of_links += 1000 + db_result.searched_for = False + else: + db.add(entities.MissingCompany(name=input_text, number_of_links=1000)) + db.commit() + return ["Added: ", html.B(input_text), " to the search que."] diff --git a/tests/ui/about_test.py b/tests/ui/about_test.py index beaa8bb..451fbe2 100644 --- a/tests/ui/about_test.py +++ b/tests/ui/about_test.py @@ -1,5 +1,5 @@ """Tests if the about page can be created.""" -from dash.development.base_component import Component +from dash import html from aki_prj23_transparenzregister.ui import app from aki_prj23_transparenzregister.ui.pages import about @@ -10,6 +10,4 @@ _ = app def test_layout() -> None: """Checks if the about page can be created.""" result = about.layout() - assert isinstance(result, list) - for e in result: - assert isinstance(e, Component) + assert isinstance(result, html.Div) diff --git a/tests/ui/search_queue_test.py b/tests/ui/search_queue_test.py new file mode 100644 index 0000000..96dd04b --- /dev/null +++ b/tests/ui/search_queue_test.py @@ -0,0 +1,64 @@ +"""Tests if the search que page can be created.""" +from collections.abc import Generator + +import dash +import pytest +from dash import html +from sqlalchemy.orm import Session + +from aki_prj23_transparenzregister.ui import app +from aki_prj23_transparenzregister.ui.pages import search_queue +from aki_prj23_transparenzregister.ui.session_handler import SessionHandler +from aki_prj23_transparenzregister.utils.sql import entities + +_ = app + + +def test_layout() -> None: + """Checks if the about page can be created.""" + result = search_queue.layout() + assert isinstance(result, html.Div) + + +@pytest.mark.parametrize("input_text", ["", None]) +def test_empty(input_text: str | None) -> None: + """Tests if nothing happens on an empty string.""" + assert search_queue.add_company_to_search_que(input_text) == dash.no_update + + +@pytest.mark.parametrize("input_text", ["test"]) +def test_missing_db(input_text: str | None) -> None: + """Tests what happens if no db is defined.""" + with pytest.raises(ValueError, match="No session created."): + search_queue.add_company_to_search_que(input_text) + + +@pytest.fixture() +def _set_session(full_db: Session) -> Generator[None, None, None]: + """Sets a session for the dash application to be used.""" + SessionHandler.session = full_db + yield + SessionHandler.session = None + + +@pytest.mark.usefixtures("_set_session") +@pytest.mark.parametrize("name", ["added-company", "second", "third"]) +def test_add_company_to_queue(name: str) -> None: + """Adds a company to the search que via the webgui.""" + db = SessionHandler.session + assert db + assert not ( + db.query(entities.MissingCompany) + .filter(entities.MissingCompany.name == name) + .first() + ) + for n_lines in [1000, 2000, 3000]: + search_queue.add_company_to_search_que(name) + result = ( + db.query(entities.MissingCompany) + .filter(entities.MissingCompany.name == name) + .first() + ) + assert result + assert result.number_of_links == n_lines # noqa: PLR2004 + del result