Skip to content

Commit f6639f2

Browse files
committed
Added tests for data review tool
1 parent 3c292b1 commit f6639f2

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Author: Shaun Hutchinson,
2+
# Date: 2023-06-22
3+
from dash._callback_context import context_value
4+
from dash._utils import AttributeDict
5+
from dash import html, dash_table
6+
import dash_mantine_components as dmc
7+
from dash.testing.browser import Browser
8+
import pytest
9+
import sys
10+
import os
11+
12+
# ensure that the parent directory is on the path for relative imports
13+
sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
14+
15+
from src.data_review_tool.app import *
16+
from src.data_review_tool.pages.article_review import *
17+
18+
19+
def test_collapse():
20+
"""Test that the collapse function returns None when the no accordion is clicked."""
21+
assert collapse(True) == None
22+
23+
24+
def test_cell_clicked():
25+
"Test that the cell_clicked function returns the correct value when a cell is clicked."
26+
assert cell_clicked(1) == "/"
27+
assert cell_clicked(0) == dash.no_update
28+
29+
30+
def test_directory_structure():
31+
"Test that the directory structure is as expected."
32+
dir = "data/data-review-tool"
33+
expected = ["processed", "raw"]
34+
assert sorted(os.listdir(dir)) == expected
35+
36+
37+
def test_find_start_end_char():
38+
"Test that the find_start_end_char function returns the correct values."
39+
text = "This is a test"
40+
entity = "test"
41+
assert find_start_end_char(text, entity) == (10, 14)
42+
43+
44+
def test_update_button():
45+
"Test that the update_button function returns correct children for Delete or Restore."
46+
assert update_button(True)[0].children == "Delete Entity"
47+
assert update_button(False)[0].children == "Restore Entity"
48+
49+
50+
def test_chips_values():
51+
"Test that the chips_values function returns the correct values."
52+
site = "test"
53+
taxa, region, geog, alti, age, email = None, None, None, None, None, None
54+
accordian = "SITE"
55+
data = {"entities": {"SITE": {"test": {"corrected_name": "test"}}}}
56+
57+
assert chips_values(
58+
site, taxa, region, geog, alti, age, email, accordian, data
59+
) == ("test", False, False, "test")
60+
61+
62+
def test_enable_correct_button():
63+
"Test that the enable_correct_button function returns the correct values."
64+
assert enable_correct_button("Pinus") == False
65+
66+
67+
def test_update_chips():
68+
"Test that the update_chips function returns the correct values."
69+
deleted = False
70+
data = {"entities": {"SITE": {"test": {"corrected_name": "test"}}}}
71+
72+
assert update_chips(deleted, data)[0] == "test"
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Author: Shaun Hutchinson,
2+
# Date: 2023-06-22
3+
from dash._utils import AttributeDict
4+
from dash import html, dash_table
5+
import dash_mantine_components as dmc
6+
import pandas as pd
7+
import pytest
8+
from dash.testing.application_runners import import_app
9+
from dash.testing.composite import DashComposite
10+
import sys
11+
import os
12+
13+
sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
14+
15+
# Import the names of callback functions you want to test
16+
from src.data_review_tool.app import *
17+
from src.data_review_tool.pages.home import *
18+
from src.data_review_tool.pages.config import *
19+
20+
21+
def test_directory_structure():
22+
"test that the data directory structure is correct"
23+
dir = "data/data-review-tool"
24+
expected = ["processed", "raw"]
25+
26+
assert sorted(os.listdir(dir)) == expected
27+
28+
29+
def test_current_article_clicked():
30+
"test that clicking 'Review' button redirects to the correct article"
31+
active_cell_current = AttributeDict({"row": 0, "column_id": "Review"})
32+
current_data = [{"gddid": "1234", "Review": "Review"}]
33+
active_cell_completed = None
34+
completed_data = None
35+
active_cell_nonrelevant = None
36+
nonrelevant_data = None
37+
38+
# Create the expected output
39+
expected = "/article/1234"
40+
assert (
41+
current_article_clicked(
42+
active_cell_current,
43+
current_data,
44+
active_cell_completed,
45+
completed_data,
46+
active_cell_nonrelevant,
47+
nonrelevant_data,
48+
)
49+
== expected
50+
)
51+
52+
53+
def test_get_article_tab():
54+
"""Test that the tab is created correctly"""
55+
tab_header = "Current"
56+
data = pd.DataFrame({"gddid": ["1234", "5678"]})
57+
58+
# Create the expected output
59+
expected = dmc.Tab(
60+
children=dmc.Text(tab_header, style=tab_header_style),
61+
value=tab_header,
62+
rightSection=dmc.Badge(
63+
f"{data.shape[0]}",
64+
p=0,
65+
variant="filled",
66+
style=badge_style,
67+
sx={"width": 20, "height": 20, "pointerEvents": "none"},
68+
),
69+
)
70+
71+
assert (
72+
get_article_tab(tab_header, data).rightSection.children
73+
== expected.rightSection.children
74+
)
75+
assert get_article_tab(tab_header, data).value == expected.value
76+
77+
78+
def test_get_article_table():
79+
"""Test that the table is created correctly"""
80+
table_id = "current-table"
81+
location_id = "current-table-location"
82+
tab_header = "Current Articles"
83+
data = pd.DataFrame({"gddid": ["1234", "5678"]})
84+
85+
expected = dmc.TabsPanel(
86+
html.Div(
87+
[
88+
dash_table.DataTable(
89+
id=table_id,
90+
filter_action="native",
91+
sort_action="native",
92+
page_action="native",
93+
page_size=10,
94+
style_data=table_data_style,
95+
filter_options={"placeholder_text": ""},
96+
columns=[{"name": i, "id": i} for i in data.columns],
97+
data=data.to_dict("records"),
98+
style_data_conditional=table_conditional_style,
99+
style_table={
100+
"overflowX": "auto",
101+
"padding-top": "20px",
102+
},
103+
style_cell=table_cell_style,
104+
style_header=table_header_style,
105+
),
106+
dcc.Location(id=location_id, refresh=True),
107+
],
108+
style=tab_body_style,
109+
),
110+
value=tab_header,
111+
)
112+
assert (
113+
get_article_table(table_id, location_id, tab_header, data).value
114+
== expected.value
115+
)

0 commit comments

Comments
 (0)