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

Update get cmd for 'all' when retrieving all data-types. #541

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 10 additions & 10 deletions src/scribe_data/cli/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def prompt_user_download_all():
elif data_type:
if prompt_user_download_all():
parse_wd_lexeme_dump(
language=None,
language="all",
wikidata_dump_type=["form"],
data_types=[data_type],
type_output_dir=output_dir,
Expand Down Expand Up @@ -208,35 +208,35 @@ def prompt_user_download_all():
f"Updating data for language(s): {language.title()}; data type(s): {data_type.capitalize()}"
)
existing_files = list(Path(output_dir).glob(f"{language}/{data_type}.json"))
if existing_files:
if existing_files and not overwrite:
Copy link
Collaborator Author

@axif0 axif0 Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If user gives -o for overwrite enable, it still asks user input. Like this in first cmd-

image

The second cmd is the outputted result after fixing.

print(
f"Existing file(s) found for {language.title()} and {data_type.capitalize()} in the {output_dir} directory."
)
for idx, file in enumerate(existing_files, start=1):
print(f"{idx}. {file.name}")

print("\nChoose an option:")
print("1. Overwrite existing data (press 'o')")
print("2. Skip process (press anything else)")
user_choice = input("Enter your choice: ").strip().lower()
user_choice = questionary.confirm(
"Overwrite existing data?", default=False
).ask()

if user_choice == "o":
if user_choice:
print("Overwrite chosen. Removing existing files...")
for file in existing_files:
file.unlink()
if file.exists(): # Check if the file exists before unlinking
file.unlink()
else:
print(f"Skipping update for {language.title()} {data_type}.")
return {"success": False, "skipped": True}

query_result = query_data(
query_data(
languages=[language_or_sub_language],
data_type=data_types,
output_dir=output_dir,
overwrite=overwrite,
interactive=interactive,
)

if not all_bool and not query_result.get("skipped", False):
if not all_bool:
print(f"Updated data was saved in: {Path(output_dir).resolve()}.")

else:
Expand Down
4 changes: 2 additions & 2 deletions src/scribe_data/wikidata/query_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def query_data(

else:
print(f"Skipping update for {lang.title()} {target_type}.")
return {"success": False, "skipped": True}
# return {"success": False, "skipped": True}

print(f"Querying and formatting {lang.title()} {target_type}")

Expand Down Expand Up @@ -342,7 +342,7 @@ def query_data(
print(
f"Successfully queried and formatted data for {lang.title()} {target_type}."
)
return {"success": True, "skipped": False}
# return {"success": True, "skipped": False}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we are using return inside that loop, the function will exit on the first iteration that reaches any return statement. As a result, tqdm never finishes iterating through its items.
Also in tqdm never ends as 100%. like this-
image

Copy link
Collaborator Author

@axif0 axif0 Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to check #537 for iterating through all the language.



# if __name__ == "__main__":
Expand Down
148 changes: 107 additions & 41 deletions tests/cli/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,46 @@ def test_invalid_arguments(self):

# MARK: All Data

# @patch("scribe_data.cli.get.query_data")
# @patch("scribe_data.cli.get.prompt_user_download_all", return_value=False)
# def test_get_all_data_types_for_language(self, mock_prompt, mock_query_data):
# """
# Test retrieving all data types for a specific language.

# Ensures that `query_data` is called properly when `--all` flag is used with a language.
# """
# get_data(all_bool=True, language="English")
# mock_query_data.assert_called_once_with(
# languages=["English"],
# data_type=None,
# output_dir="scribe_data_json_export",
# overwrite=False,
# )

# @patch("scribe_data.cli.get.query_data")
# @patch("scribe_data.cli.get.prompt_user_download_all", return_value=False)
# def test_get_all_languages_for_data_type(self, mock_prompt, mock_query_data):
# """
# Test retrieving all languages for a specific data type.

# Ensures that `query_data` is called properly when `--all` flag is used with a data type.
# """
# get_data(all_bool=True, data_type="nouns")
# mock_query_data.assert_called_once_with(
# languages=None,
# data_type=["nouns"],
# output_dir="scribe_data_json_export",
# overwrite=False,
# )
@patch("scribe_data.cli.get.query_data")
@patch("scribe_data.cli.get.parse_wd_lexeme_dump")
@patch("scribe_data.cli.get.questionary.confirm")
def test_get_all_data_types_for_language_user_says_yes(
self, mock_questionary_confirm, mock_parse, mock_query_data
):
"""
Test the behavior when the user agrees to query Wikidata directly.

This test checks that `parse_wd_lexeme_dump` is called with the correct parameters
when the user confirms they want to query Wikidata.
"""
mock_questionary_confirm.return_value.ask.return_value = True

get_data(all_bool=True, language="English")

mock_parse.assert_called_once_with(
language="English",
wikidata_dump_type=["form"],
data_types=None, # because data_types = [data_type] if provided else None
type_output_dir="scribe_data_json_export", # default for JSON
)
mock_query_data.assert_not_called()

@patch("scribe_data.cli.get.parse_wd_lexeme_dump")
def test_get_all_languages_and_data_types(self, mock_parse):
"""
Test retrieving all languages for a specific data type.

Ensures that `query_data` is called properly when `--all` flag is used with a data type.
"""
get_data(all_bool=True)

mock_parse.assert_called_once_with(
language="all",
wikidata_dump_type=["form", "translations"],
data_types="all",
type_output_dir="scribe_data_json_export",
wikidata_dump_path=None,
)

# MARK: Language and Data Type

Expand Down Expand Up @@ -211,42 +220,99 @@ def test_get_data_with_overwrite_false(self, mock_query_data):
# MARK : User Chooses to skip

@patch("scribe_data.cli.get.query_data")
@patch("scribe_data.cli.get.Path.glob")
@patch("builtins.input", return_value="s")
def test_user_skips_existing_file(self, mock_input, mock_glob, mock_query_data):
@patch(
"scribe_data.cli.get.Path.glob",
return_value=[Path("./test_output/English/nouns.json")],
)
@patch("scribe_data.cli.get.questionary.confirm")
def test_user_skips_existing_file(
self, mock_questionary_confirm, mock_glob, mock_query_data
):
"""
Test the behavior when the user chooses to skip an existing file.

Ensures that the file is not overwritten and the function returns the correct result.
"""
mock_glob.return_value = [Path("./test_output/English/nouns.json")]
mock_questionary_confirm.return_value.ask.return_value = False
result = get_data(
language="English", data_type="nouns", output_dir="./test_output"
)

# Validate the skip result
self.assertEqual(result, {"success": False, "skipped": True})
mock_query_data.assert_not_called()

# MARK : User Chooses to overwrite

@patch("scribe_data.cli.get.query_data")
@patch("scribe_data.cli.get.Path.glob")
@patch("builtins.input", return_value="o")
@patch("scribe_data.cli.get.Path.unlink")
@patch(
"scribe_data.cli.get.Path.glob",
return_value=[Path("./test_output/English/nouns.json")],
)
@patch("scribe_data.cli.get.questionary.confirm")
def test_user_overwrites_existing_file(
self, mock_unlink, mock_input, mock_glob, mock_query_data
self, mock_questionary_confirm, mock_glob, mock_query_data
):
"""
Test the behavior when the user chooses to overwrite an existing file.

Ensures that the file is overwritten and the function returns the correct result.
"""
mock_glob.return_value = [Path("./test_output/English/nouns.json")]
mock_questionary_confirm.return_value.ask.return_value = True
get_data(language="English", data_type="nouns", output_dir="./test_output")
mock_unlink.assert_called_once_with()

mock_query_data.assert_called_once_with(
languages=["English"],
data_type=["nouns"],
output_dir="./test_output",
overwrite=False,
interactive=False,
)

# MARK: Translations
@patch("scribe_data.cli.get.parse_wd_lexeme_dump")
def test_get_translations_no_language_specified(self, mock_parse):
"""
Test behavior when no language is specified for 'translations'.
Expect language="all".
"""
get_data(data_type="translations")
mock_parse.assert_called_once_with(
language="all",
wikidata_dump_type=["translations"],
type_output_dir="scribe_data_json_export", # default output dir for JSON
wikidata_dump_path=None,
)

@patch("scribe_data.cli.get.parse_wd_lexeme_dump")
def test_get_translations_with_specific_language(self, mock_parse):
"""
Test behavior when a specific language is provided for 'translations'.
Expect parse_wd_lexeme_dump to be called with that language.
"""
get_data(
language="Spanish", data_type="translations", output_dir="./test_output"
)
mock_parse.assert_called_once_with(
language="Spanish",
wikidata_dump_type=["translations"],
type_output_dir="./test_output",
wikidata_dump_path=None,
)

@patch("scribe_data.cli.get.parse_wd_lexeme_dump")
def test_get_translations_with_dump(self, mock_parse):
"""
Test behavior when a Wikidata dump path is specified for 'translations'.
Even with a language, it should call parse_wd_lexeme_dump
passing that dump path.
"""
get_data(
language="German", data_type="translations", wikidata_dump="./wikidump.json"
)
mock_parse.assert_called_once_with(
language="German",
wikidata_dump_type=["translations"],
type_output_dir="scribe_data_json_export", # default for JSON
wikidata_dump_path="./wikidump.json",
)
Loading