|
| 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