Skip to content

Commit

Permalink
Parameterized website tests (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogenesAnalytics committed Mar 24, 2024
1 parent d280305 commit e2848d3
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 162 deletions.
92 changes: 71 additions & 21 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def base_custom_config() -> Dict[str, Any]:
"subject": "Testing",
"title": "Testing",
"form_backend_url": f"http://localhost:{port}{submit}",
"ignore_file_upload": False,
"email": "[email protected]",
"questions": [],
}
Expand Down Expand Up @@ -74,6 +75,15 @@ def create_temp_websrc_dir(src: Path, dst: Path, src_files: Tuple[str, ...]) ->
return sub_dir


def get_project_directory() -> Path:
"""Get project directory path object."""
# Get the path of the current file (test_file.py)
current_file_path = Path(os.path.abspath(__file__))

# get grand parent dir
return current_file_path.parents[1]


def load_config_file(directory: Path) -> Dict[str, Any]:
"""Load the JSON config file at directory."""
# open the config file in the project dir
Expand All @@ -89,7 +99,7 @@ def write_config_file(config: Dict[str, Any], src_path: Path) -> None:
json.dump(config, json_file, indent=2)


def prepare_default_config(config: Dict[str, Any], src_path: Path) -> None:
def prepare_default_config(config: Dict[str, Any]) -> Dict[str, Any]:
"""Update the default config copy with values approprate for testing."""
# get port and submit route
port, submit = get_server_info()
Expand All @@ -113,8 +123,8 @@ def prepare_default_config(config: Dict[str, Any], src_path: Path) -> None:
# add custom section with accept attr
question["custom"] = {"accept": "*"}

# write out updated file
write_config_file(config, src_path)
# get updated config data
return config


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -233,14 +243,10 @@ def sb_test_url() -> str:
return "https://seleniumbase.io/realworld/login"


@pytest.fixture(scope="session")
def project_directory() -> Path:
@pytest.fixture(scope="function")
def project_dir() -> Path:
"""Get the path of the project directory."""
# Get the path of the current file (test_file.py)
current_file_path = Path(os.path.abspath(__file__))

# get grand parent dir
return current_file_path.parents[1]
return get_project_directory()


@pytest.fixture(scope="session")
Expand All @@ -252,11 +258,23 @@ def website_files() -> Tuple[str, ...]:

@pytest.fixture(scope="session")
def session_websrc_tmp_dir(
project_directory: Path, session_tmp_dir: Path, website_files: Tuple[str, ...]
session_tmp_dir: Path, website_files: Tuple[str, ...]
) -> Generator[Path, None, None]:
"""Create a per-session copy of the website source code for editing."""
# project dir
project_dir = get_project_directory()

# create a temporary directory
temp_dir = create_temp_websrc_dir(project_directory, session_tmp_dir, website_files)
temp_dir = create_temp_websrc_dir(project_dir, session_tmp_dir, website_files)

# get the default config
default_config = load_config_file(temp_dir)

# now update config.json with new backend url
updated_config = prepare_default_config(default_config)

# write to websrc temp copy
write_config_file(updated_config, temp_dir)

# provide the temporary directory path to the test function
yield temp_dir
Expand All @@ -265,20 +283,21 @@ def session_websrc_tmp_dir(
shutil.rmtree(temp_dir)


@pytest.fixture(scope="session")
def default_site_config(project_directory: Path) -> Dict[str, Any]:
@pytest.fixture(scope="function")
def default_user_config(project_dir: Path) -> Dict[str, Any]:
"""Load the default config.json file."""
return load_config_file(project_directory)
return load_config_file(project_dir)


@pytest.fixture(scope="function")
def updated_user_config(default_user_config: Dict[str, Any]) -> Dict[str, Any]:
"""Updates the user config file with appropriate testing attributes."""
return prepare_default_config(default_user_config)


@pytest.fixture(scope="session")
def session_web_app(
default_site_config: Dict[str, Any], session_websrc_tmp_dir: Path
) -> Flask:
def session_web_app(session_websrc_tmp_dir: Path) -> Flask:
"""Create a session-scoped Flask app for testing with the website source."""
# now update config.json with new backend url
prepare_default_config(default_site_config, session_websrc_tmp_dir)

# create app
return build_flask_app(session_websrc_tmp_dir)

Expand Down Expand Up @@ -318,6 +337,12 @@ def all_inputs_config(dummy_form_inputs: Dict[str, Any]) -> Dict[str, Any]:
if input_type == "selectbox":
# setup options
options = [
{
"label": "--Select option--",
"value": "",
"selected": True,
"disabled": True,
},
{"label": "Option1", "value": "Opt1"},
{"label": "Option2", "value": "Opt2"},
{"label": "Option3", "value": "Opt3"},
Expand All @@ -337,6 +362,31 @@ def all_inputs_config(dummy_form_inputs: Dict[str, Any]) -> Dict[str, Any]:
return config


@pytest.fixture(
scope="function",
params=[
pytest.param("user"),
pytest.param("all_inputs"),
],
)
def all_default_configs(
request, updated_user_config: Dict[str, Any], all_inputs_config: Dict[str, Any]
) -> Dict[str, Any]:
"""Parameterized default configs fixture."""
# get current markers and config type
config_type = request.param

# match config to use
match config_type:
case "user":
config = updated_user_config
case "all_inputs":
config = all_inputs_config

# get appropriate config
return config


@pytest.fixture(scope="function")
def multiple_select_options_config() -> Dict[str, Any]:
"""Custom config file fixture for testing multiple select options."""
Expand Down
17 changes: 13 additions & 4 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def check_files_subset(source_dir: Path, webfiles: Tuple[str, ...]) -> bool:

@pytest.mark.fixture
def test_websrc_in_project_dir(
project_directory: Path, website_files: Tuple[str, ...]
project_dir: Path, website_files: Tuple[str, ...]
) -> None:
"""Simply confirm that the website files are in the project dir path."""
assert check_files_subset(project_directory, website_files)
assert check_files_subset(project_dir, website_files)


@pytest.mark.fixture
Expand All @@ -43,11 +43,11 @@ def test_websrc_in_temp_dir(

@pytest.mark.fixture
def test_config_keys_in_form_inputs(
default_site_config: Dict[str, Any], dummy_form_inputs: Dict[str, Any]
default_user_config: Dict[str, Any], dummy_form_inputs: Dict[str, Any]
) -> None:
"""Check that keys from config.json are present in form input testing fixture."""
# get types from questions section of config.json
question_types = [q["type"] for q in default_site_config["questions"]]
question_types = [q["type"] for q in default_user_config["questions"]]

# check config question types missing form inputs (if any)
missing_keys = set(question_types) - set(dummy_form_inputs)
Expand Down Expand Up @@ -295,6 +295,15 @@ def test_all_inputs_config_schema(all_inputs_config: Dict[str, Any]) -> None:
assert check_config_schema(all_inputs_config), "Error in all inputs config fixture."


@pytest.mark.fixture
def test_all_default_configs_upload_files(all_default_configs: Dict[str, Any]) -> None:
"""Check that all the default configs upload files."""
# check config for file upload attr
assert not all_default_configs[
"ignore_file_upload"
], "Default site configs should not ignore file uploads."


@pytest.mark.fixture
def test_multi_options_config_schema(
multiple_select_options_config: Dict[str, Any]
Expand Down
Loading

0 comments on commit e2848d3

Please sign in to comment.