-
-
Notifications
You must be signed in to change notification settings - Fork 28
feat(charts): add TextAlign enum and improve annotation validation #488
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
942dc2e
feat(charts): add TextAlign enum and improve annotation validation
palewire 046aa19
test: improve circle style validation error message assertions
palewire 546a8e1
fix: improve error message clarity in ConnectorLine circle_style vali…
palewire 1c98e4c
fix(charts): correct circle_style validator enum handling
palewire ac9aded
docs: add TextAlign enum documentation to API reference
palewire 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
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,50 @@ | ||
| """Text alignment enums for annotations.""" | ||
|
|
||
| from enum import Enum | ||
|
|
||
|
|
||
| class TextAlign(str, Enum): | ||
| """Text alignment positions for annotations. | ||
|
|
||
| Represents a 3x3 grid of alignment positions combining vertical and horizontal alignment. | ||
|
|
||
| Examples: | ||
| >>> from datawrapper.charts import TextAnnotation, TextAlign | ||
| >>> anno = TextAnnotation( | ||
| ... text="Top left corner", x=10, y=20, align=TextAlign.TOP_LEFT | ||
| ... ) | ||
| >>> anno.align | ||
| <TextAlign.TOP_LEFT: 'tl'> | ||
|
|
||
| >>> # Using raw string (backwards compatible) | ||
| >>> anno = TextAnnotation(text="Center", x=50, y=50, align="mc") | ||
| >>> anno.align | ||
| 'mc' | ||
| """ | ||
|
|
||
| #: Top-left alignment | ||
| TOP_LEFT = "tl" | ||
|
|
||
| #: Top-center alignment | ||
| TOP_CENTER = "tc" | ||
|
|
||
| #: Top-right alignment | ||
| TOP_RIGHT = "tr" | ||
|
|
||
| #: Middle-left alignment | ||
| MIDDLE_LEFT = "ml" | ||
|
|
||
| #: Middle-center alignment | ||
| MIDDLE_CENTER = "mc" | ||
|
|
||
| #: Middle-right alignment | ||
| MIDDLE_RIGHT = "mr" | ||
|
|
||
| #: Bottom-left alignment | ||
| BOTTOM_LEFT = "bl" | ||
|
|
||
| #: Bottom-center alignment | ||
| BOTTOM_CENTER = "bc" | ||
|
|
||
| #: Bottom-right alignment | ||
| BOTTOM_RIGHT = "br" |
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,94 @@ | ||
| """Unit tests for AreaFill opacity field validator.""" | ||
|
|
||
| import pytest | ||
| from pydantic import ValidationError | ||
|
|
||
| from datawrapper.charts.annos import AreaFill | ||
|
|
||
|
|
||
| def test_opacity_valid_default(): | ||
| """Test that the default opacity value (0.3) is valid.""" | ||
| fill = AreaFill(**{"from": "baseline", "to": "new"}) | ||
| assert fill.opacity == 0.3 | ||
|
|
||
|
|
||
| def test_opacity_valid_zero(): | ||
| """Test that opacity=0.0 is valid (minimum boundary).""" | ||
| fill = AreaFill(**{"from": "baseline", "to": "new", "opacity": 0.0}) | ||
| assert fill.opacity == 0.0 | ||
|
|
||
|
|
||
| def test_opacity_valid_one(): | ||
| """Test that opacity=1.0 is valid (maximum boundary).""" | ||
| fill = AreaFill(**{"from": "baseline", "to": "new", "opacity": 1.0}) | ||
| assert fill.opacity == 1.0 | ||
|
|
||
|
|
||
| def test_opacity_valid_middle(): | ||
| """Test that a middle value like 0.5 is valid.""" | ||
| fill = AreaFill(**{"from": "baseline", "to": "new", "opacity": 0.5}) | ||
| assert fill.opacity == 0.5 | ||
|
|
||
|
|
||
| def test_opacity_valid_decimal(): | ||
| """Test that decimal values are valid.""" | ||
| fill = AreaFill(**{"from": "baseline", "to": "new", "opacity": 0.75}) | ||
| assert fill.opacity == 0.75 | ||
|
|
||
|
|
||
| def test_opacity_invalid_negative(): | ||
| """Test that negative opacity values raise ValidationError.""" | ||
| with pytest.raises(ValidationError) as exc_info: | ||
| AreaFill(**{"from": "baseline", "to": "new", "opacity": -0.1}) | ||
|
|
||
| error = exc_info.value.errors()[0] | ||
| assert "Invalid opacity: -0.1" in str(error.get("ctx", {}).get("error", "")) | ||
| assert "Must be between 0.0 and 1.0" in str(error.get("ctx", {}).get("error", "")) | ||
|
|
||
|
|
||
| def test_opacity_invalid_over_one(): | ||
| """Test that opacity values over 1.0 raise ValidationError.""" | ||
| with pytest.raises(ValidationError) as exc_info: | ||
| AreaFill(**{"from": "baseline", "to": "new", "opacity": 1.1}) | ||
|
|
||
| error = exc_info.value.errors()[0] | ||
| assert "Invalid opacity: 1.1" in str(error.get("ctx", {}).get("error", "")) | ||
| assert "Must be between 0.0 and 1.0" in str(error.get("ctx", {}).get("error", "")) | ||
|
|
||
|
|
||
| def test_opacity_invalid_large_value(): | ||
| """Test that very large opacity values raise ValidationError.""" | ||
| with pytest.raises(ValidationError) as exc_info: | ||
| AreaFill(**{"from": "baseline", "to": "new", "opacity": 2.0}) | ||
|
|
||
| error = exc_info.value.errors()[0] | ||
| assert "Invalid opacity: 2.0" in str(error.get("ctx", {}).get("error", "")) | ||
| assert "Must be between 0.0 and 1.0" in str(error.get("ctx", {}).get("error", "")) | ||
|
|
||
|
|
||
| def test_opacity_invalid_very_negative(): | ||
| """Test that very negative opacity values raise ValidationError.""" | ||
| with pytest.raises(ValidationError) as exc_info: | ||
| AreaFill(**{"from": "baseline", "to": "new", "opacity": -1.0}) | ||
|
|
||
| error = exc_info.value.errors()[0] | ||
| assert "Invalid opacity: -1.0" in str(error.get("ctx", {}).get("error", "")) | ||
| assert "Must be between 0.0 and 1.0" in str(error.get("ctx", {}).get("error", "")) | ||
|
|
||
|
|
||
| def test_opacity_boundary_just_below_zero(): | ||
| """Test that opacity just below 0.0 raises ValidationError.""" | ||
| with pytest.raises(ValidationError) as exc_info: | ||
| AreaFill(**{"from": "baseline", "to": "new", "opacity": -0.01}) | ||
|
|
||
| error = exc_info.value.errors()[0] | ||
| assert "Invalid opacity: -0.01" in str(error.get("ctx", {}).get("error", "")) | ||
|
|
||
|
|
||
| def test_opacity_boundary_just_above_one(): | ||
| """Test that opacity just above 1.0 raises ValidationError.""" | ||
| with pytest.raises(ValidationError) as exc_info: | ||
| AreaFill(**{"from": "baseline", "to": "new", "opacity": 1.01}) | ||
|
|
||
| error = exc_info.value.errors()[0] | ||
| assert "Invalid opacity: 1.01" in str(error.get("ctx", {}).get("error", "")) |
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.