Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate contributions #214

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dp_wizard/app/components/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ def demo_tooltip(is_demo: bool, text: str): # pragma: no cover
def hide_if(condition: bool, el): # pragma: no cover
display = "none" if condition else "block"
return ui.div(el, style=f"display: {display};")


def info_box(content): # pragma: no cover
return ui.div(content, class_="alert alert-info", role="alert")
34 changes: 27 additions & 7 deletions dp_wizard/app/dataset_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from shiny import ui, reactive, render, Inputs, Outputs, Session

from dp_wizard.utils.argparse_helpers import get_cli_info
from dp_wizard.app.components.outputs import output_code_sample, demo_tooltip
from dp_wizard.app.components.outputs import (
output_code_sample,
demo_tooltip,
info_box,
hide_if,
)
from dp_wizard.utils.code_generators import make_privacy_unit_block


Expand All @@ -25,11 +30,14 @@ def dataset_ui():
"How many rows of the CSV can one individual contribute to? "
'This is the "unit of privacy" which will be protected.'
),
ui.input_numeric(
"contributions",
["Contributions", ui.output_ui("contributions_demo_tooltip_ui")],
cli_info.contributions,
min=1,
ui.row(
ui.input_numeric(
"contributions",
["Contributions", ui.output_ui("contributions_demo_tooltip_ui")],
cli_info.contributions,
min=1,
),
ui.output_ui("contributions_validation_ui"),
),
ui.output_ui("python_tooltip_ui"),
output_code_sample("Unit of Privacy", "unit_of_privacy_python"),
Expand Down Expand Up @@ -80,6 +88,18 @@ def contributions_demo_tooltip_ui():
f"can occur at most {contributions()} times in the dataset. ",
)

@reactive.calc
def contributions_valid():
contributions = input.contributions()
return isinstance(contributions, int) and contributions >= 1

@render.ui
def contributions_validation_ui():
return hide_if(
contributions_valid(),
info_box(ui.markdown("Contributions must be 1 or greater.")),
)

@render.ui
def python_tooltip_ui():
return demo_tooltip(
Expand All @@ -95,7 +115,7 @@ def define_analysis_button_ui():
button = ui.input_action_button(
"go_to_analysis", "Define analysis", disabled=not button_enabled()
)
if button_enabled():
if button_enabled() and contributions_valid():
return button
return [
button,
Expand Down
12 changes: 12 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def expect_no_error():
# Now upload:
csv_path = Path(__file__).parent / "fixtures" / "fake.csv"
page.get_by_label("Choose CSV file").set_input_files(csv_path.resolve())

# Check validation of contributions:
# Playwright itself won't let us fill non-numbers in this field.
# "assert define_analysis_button.is_enabled()" has spurious errors.
page.get_by_label("Contributions").fill("0")
expect_visible("Contributions must be 1 or greater")
expect_visible("Choose CSV and Contributions before proceeding")

page.get_by_label("Contributions").fill("42")
expect_not_visible("Contributions must be 1 or greater")
expect_not_visible("Choose CSV and Contributions before proceeding")

expect_no_error()

# -- Define analysis --
Expand Down
Loading