-
Notifications
You must be signed in to change notification settings - Fork 0
Feature: Get valid values data from an external source instead of a local file #49
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
Draft
Sarrabah
wants to merge
63
commits into
main
Choose a base branch
from
feat/use-external-valid-values-source
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 33 commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
46d68a6
feat: Create a new validator with valid values getted from a url path
Sarrabah 67145f3
test: Test a real case
Sarrabah 1d6c038
refactor: Linting
Sarrabah ac68603
docs: Generate documentation to the testing validator
Sarrabah 1659be0
refactor: Reduce the test file size
Sarrabah 95c805d
refactor: Delete the local csv file
Sarrabah c425167
test: test get valid values function
Sarrabah 0d2d290
refactor: Renaming
Sarrabah f86ae8e
refactor: Linting
Sarrabah f901028
refactor: Add comment
Sarrabah ff7e693
feat: Apply changes to canton
Sarrabah 765288f
fix: Treat csv files
Sarrabah 1e69356
test: Add more tests
Sarrabah ab96f18
refactor: Reorganize data files for testing
Sarrabah 23ab4b9
test: Ignore this one
Sarrabah 8960f68
test: Remove comment
Sarrabah 645ce55
A void commit
Sarrabah 601878d
test: Reorganization
Sarrabah fbffcc7
refactor: Linting
Sarrabah 6f97b69
fix: Skip the csv verification
Sarrabah 38f0410
Reorder imports
Sarrabah b84db05
refactor: Linting
Sarrabah a107581
fix: Add sleeping time
Sarrabah c28df74
fix: Return to the first canton code logic
Sarrabah cdf3d4e
refactor: Remove unused import
Sarrabah d7c467b
test: Use a simple incorrect url
Sarrabah aa50bb4
feat: Readapt canton
Sarrabah 502d01f
fix: Correct the import
Sarrabah 49a6525
refactor: Ignore type
Sarrabah 4c917a4
fix: Add the forgotten import
Sarrabah c530b36
test: Remove the test code
Sarrabah 3547351
refactor: Remove exceptions
Sarrabah 42b443f
refactor: Linting
Sarrabah ef120be
feat: Ignore Canton changes
Sarrabah 997b15d
refactor: Renaming function
Sarrabah 9536e5d
test: Test a local csv file
Sarrabah cb51cc6
refactor: Implement private functions to be more redable
Sarrabah 996f343
refactor: Use meaningful names
Sarrabah e899501
feat: Error handling when the csv file is not well formatted
Sarrabah 45c056a
test: Test a non existing column
Sarrabah e7f5354
test: Test a missing file
Sarrabah 27dc7f1
feat: Use guard clauses and start without error handling
Sarrabah 5f5143a
test: Testing and errro handling when the csv file is not well formatted
Sarrabah c5f374e
test: Test a missing column
Sarrabah 0cb318d
test: Test a non existing file
Sarrabah 8f8ece0
test: Testing file and ftp schema
Sarrabah 3f94dea
refactor: Add more errors handling
Sarrabah 51e1514
docs: Add a docstring
Sarrabah dc3de41
test: WIP to use dependency inversion
Sarrabah d24e7f8
refactor: Remove a comment
Sarrabah 2a8677b
refactor: Renaming and typing variables
Sarrabah c10900e
refactor: Raise exceptions without printing a message
Sarrabah 6bb9399
feat: Implement dependency inversion WIP
Sarrabah bc3ee55
fix: Use Union term
Sarrabah 9c6903d
test: Test a not well formatted csv file
Sarrabah d2c5082
docs: Update docstring
Sarrabah 2cfe6bb
refactor: Renaming
Sarrabah 755b638
refactor: Remove a non important error handling
Sarrabah ae61328
refactor: Refactoring conditions
Sarrabah 7db2ca1
refactor: Using the same type
Sarrabah 2fe5c16
test: Implement the dependency inversion
Sarrabah dc8a951
test: Reorganize tests and renaming
Sarrabah 04abe80
refactor: Move get values function to set_format file
Sarrabah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from frformat import set_format | ||
| from frformat.get_values_script import get_valid_values | ||
| from frformat.versioned_set import VersionedSet | ||
| from frformat.versions import Millesime | ||
|
|
||
| name = "Nom de validateur" | ||
| description = "Vérification si la valeur donnée est bien valide selon des valeurs valides récupérées" | ||
| source = "source1" | ||
|
|
||
| validator_versioned_data = VersionedSet[Millesime]() | ||
|
|
||
| valid_values_2023 = get_valid_values( | ||
| "https://www.insee.fr/fr/statistiques/fichier/8377162/v_commune_2025.csv", "COM" | ||
| ) | ||
| validator_versioned_data.add_version(Millesime.M2023, valid_values_2023) | ||
|
|
||
|
|
||
| Validator = set_format.new( | ||
| "Validator", name, description, source, validator_versioned_data | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import csv | ||
| import io | ||
| import os | ||
| import urllib.parse | ||
| import urllib.request | ||
|
|
||
|
|
||
| def get_valid_values(path: str, column: str) -> frozenset[str]: | ||
|
|
||
| parsed_url = urllib.parse.urlparse(path) | ||
| is_url = parsed_url.scheme in ("http", "https") | ||
|
Sarrabah marked this conversation as resolved.
Outdated
|
||
|
|
||
| valid_values = [] | ||
|
|
||
| if is_url: | ||
| try: | ||
| response = urllib.request.urlopen(path) | ||
| csvfile = io.StringIO(response.read().decode("utf-8")) | ||
| except Exception as e: | ||
| raise ValueError(f"Failed to fetch CSV from URL: {e} .") | ||
|
|
||
| elif os.path.isfile(path): | ||
|
Sarrabah marked this conversation as resolved.
Outdated
|
||
| splitted_path = path.split(".") | ||
| if splitted_path[len(splitted_path) - 1] == "csv": | ||
|
Sarrabah marked this conversation as resolved.
Outdated
|
||
| try: | ||
| csvfile = open(path, newline="", encoding="utf-8") | ||
|
|
||
| except Exception as e: | ||
| raise ValueError(f"Failed to open local CSV file: {e} .") | ||
| else: | ||
| raise ValueError("The given path must be referenced to a csv file.") | ||
| else: | ||
| raise ValueError(f"Invalid path: {path}.It must be a URL or existing csv file.") | ||
|
|
||
| with csvfile: | ||
| reader = csv.DictReader(csvfile) | ||
| for row in reader: | ||
|
Sarrabah marked this conversation as resolved.
Outdated
|
||
| if column in row: | ||
| valid_values.append(row[column]) | ||
| else: | ||
| raise ValueError(f"CSV file is missing the {column} column.") | ||
|
|
||
| return frozenset(valid_values) | ||
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| "Username","Identifier","First name","Last name" | ||
| "booker12","9012","Rachel","Booker" | ||
| "grey07","2070","Laura","Grey" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import pytest | ||
|
|
||
| from frformat import Millesime, Validator | ||
| from frformat.get_values_script import get_valid_values | ||
|
|
||
|
|
||
| def test_validator(): | ||
| isvalid = Validator(Millesime.M2023).is_valid("01001") | ||
| assert isvalid is True | ||
|
|
||
|
|
||
| def test_get_valid_values_with_local_file(): | ||
| valid_values = get_valid_values( | ||
| "src/tests/test_files_data/values.csv", "First name" | ||
| ) | ||
| assert valid_values == frozenset({"Rachel", "Laura"}) | ||
|
|
||
| with pytest.raises( | ||
| ValueError, match="The given path must be referenced to a csv file." | ||
| ): | ||
| valid_values = get_valid_values( | ||
| "src/tests/test_files_data/text_file.odt", "coucou" | ||
| ) | ||
|
|
||
| with pytest.raises(ValueError, match="CSV file is missing the Link column."): | ||
| valid_values = get_valid_values("src/tests/test_files_data/values.csv", "Link") | ||
|
|
||
|
|
||
| # mocking ? | ||
| """ def test_get_valid_values_with_url(): | ||
| valid_values = get_valid_values( | ||
| "https://cdn.wsform.com/wp-content/uploads/2021/04/month.csv", | ||
| "Name", | ||
| ) | ||
| assert valid_values == frozenset( | ||
| { | ||
| "January", | ||
| "Feburary", | ||
| "March", | ||
| "April", | ||
| "May", | ||
| "June", | ||
| "July", | ||
| "August", | ||
| "September", | ||
| "October", | ||
| "November", | ||
| "December", | ||
| } | ||
| ) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| valid_values = get_valid_values( | ||
| "https://coucou.csv/", | ||
| "Name", | ||
| ) | ||
| """ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.