-
Notifications
You must be signed in to change notification settings - Fork 12
fix(rnd-21448): Fixtures.to_df() returns empty values for nested vessel/corporate_entities fields #571
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
fix(rnd-21448): Fixtures.to_df() returns empty values for nested vessel/corporate_entities fields #571
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a00fa24
fix: populating fixtures data
hansenfan 1928359
fix: aligned with pr conventions
hansenfan 296f93b
fix: fixed docstring
hansenfan 7ce8889
fix: updated documentation
hansenfan f6d5428
fix: deployment formatting
hansenfan 4e814e4
fix: integration tests
hansenfan 09dcfd9
fix: format integration tests with black
hansenfan 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| from unittest import TestCase | ||
|
|
||
| from vortexasdk.api.entity_flattening import ( | ||
| convert_to_flat_dict, | ||
| convert_fixture_to_flat_dict, | ||
| ) | ||
|
|
||
|
|
||
| class TestFixtureFlattening(TestCase): | ||
| """Test that convert_fixture_to_flat_dict groups corporate_entities by layer.""" | ||
|
|
||
| FIXTURE = { | ||
| "id": "abc123", | ||
| "vessel": { | ||
| "id": "v1", | ||
| "name": "ALPINE EAGLE", | ||
| "imo": 9480980, | ||
| "mmsi": 255804460, | ||
| "dwt": 300000, | ||
| "cubic_capacity": 330000, | ||
| "vessel_class": "vlcc", | ||
| "classes": [], | ||
| "corporate_entities": [ | ||
| { | ||
| "id": "ec1", | ||
| "layer": "effective_controller", | ||
| "label": "Frontline Ltd", | ||
| "probability": 1, | ||
| "source": "external", | ||
| }, | ||
| { | ||
| "id": "tc1", | ||
| "layer": "time_charterer", | ||
| "label": "Shell", | ||
| "probability": 0.8, | ||
| "source": "model", | ||
| "start_timestamp": "2023-06-01", | ||
| "end_timestamp": "2024-06-01", | ||
| }, | ||
| ], | ||
| "tags": [], | ||
| "flag": [], | ||
| "scrubber": [], | ||
| }, | ||
| "tonnes": 270000, | ||
| "laycan_from": "2024-01-01T00:00:00+0000", | ||
| "laycan_to": "2024-01-03T00:00:00+0000", | ||
| "fixing_timestamp": "2023-12-28T14:30:00+0000", | ||
| "vtx_fulfilled": True, | ||
| "origin": {"id": "org1", "label": "Arabian Gulf"}, | ||
| "destination": {"id": "dst1", "label": "South Korea"}, | ||
| "product": {"id": "prd1", "label": "Crude"}, | ||
| "charterer": {"id": "c1", "label": "Trafigura"}, | ||
| } | ||
|
|
||
| def test_generic_flattener_uses_numeric_indices(self): | ||
| flat = convert_to_flat_dict(self.FIXTURE, columns="all") | ||
| assert "vessel.corporate_entities.0.label" in flat | ||
| assert ( | ||
| "vessel.corporate_entities.effective_controller.label" not in flat | ||
| ) | ||
|
|
||
| def test_fixture_flattener_groups_by_layer(self): | ||
| flat = convert_fixture_to_flat_dict(self.FIXTURE, columns="all") | ||
| assert ( | ||
| flat["vessel.corporate_entities.effective_controller.label"] | ||
| == "Frontline Ltd" | ||
| ) | ||
| assert ( | ||
| flat["vessel.corporate_entities.time_charterer.label"] == "Shell" | ||
| ) | ||
|
|
||
| def test_fixture_flattener_preserves_flat_fields(self): | ||
| flat = convert_fixture_to_flat_dict(self.FIXTURE, columns="all") | ||
| assert flat["vessel.name"] == "ALPINE EAGLE" | ||
| assert flat["vessel.imo"] == 9480980 | ||
| assert flat["tonnes"] == 270000 | ||
| assert flat["charterer.label"] == "Trafigura" | ||
|
|
||
| def test_fixture_flattener_column_filter(self): | ||
| cols = [ | ||
| "vessel.name", | ||
| "vessel.corporate_entities.effective_controller.label", | ||
| ] | ||
| flat = convert_fixture_to_flat_dict(self.FIXTURE, columns=cols) | ||
| assert set(flat.keys()) == set(cols) |
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,241 @@ | ||
| from datetime import datetime | ||
|
|
||
| from tests.testcases import TestCaseUsingRealAPI | ||
| from vortexasdk.endpoints.fixtures import Fixtures | ||
| from vortexasdk.api.entity_flattening import ( | ||
| convert_to_flat_dict, | ||
| convert_fixture_to_flat_dict, | ||
| ) | ||
|
|
||
|
|
||
| class TestFixturesReal(TestCaseUsingRealAPI): | ||
| """Integration tests for Fixtures endpoint (RND-21448).""" | ||
|
|
||
| def test_rnd_21448_old_flattener_broken_behavior(self): | ||
| """ | ||
| RND-21448: INITIAL BROKEN BEHAVIOR | ||
|
|
||
| Before fix: When users tried to request layer-based columns like | ||
| "vessel.corporate_entities.effective_controller.label" using the old | ||
| convert_to_flat_dict, those columns would be EMPTY/MISSING because | ||
| the old flattener uses numeric indices. | ||
|
|
||
| This reproduces the exact bug from the ticket. | ||
| """ | ||
| result = ( | ||
| Fixtures() | ||
| .search( | ||
| filter_time_min=datetime(2020, 1, 1), | ||
| filter_time_max=datetime(2020, 1, 2), | ||
| ) | ||
| .to_list() | ||
| ) | ||
|
|
||
| assert len(result) > 0 | ||
| first_fixture = result[0].model_dump() | ||
|
|
||
| # Simulate old to_df() behavior: request layer-based columns | ||
| old_flat = convert_to_flat_dict( | ||
| first_fixture, | ||
| columns=[ | ||
| "vessel.name", | ||
| "tonnes", | ||
| "vessel.corporate_entities.effective_controller.label", | ||
| "vessel.corporate_entities.time_charterer.label", | ||
| ], | ||
| ) | ||
|
|
||
| # BROKEN: Requested columns would be missing or None | ||
| assert ( | ||
| "vessel.corporate_entities.effective_controller.label" | ||
| not in old_flat | ||
| or old_flat["vessel.corporate_entities.effective_controller.label"] | ||
| is None | ||
| ), "RND-21448 (BROKEN): Requesting layer-based corporate_entities returns empty/missing" | ||
|
|
||
| assert ( | ||
| "vessel.corporate_entities.time_charterer.label" not in old_flat | ||
| or old_flat["vessel.corporate_entities.time_charterer.label"] | ||
| is None | ||
| ), "RND-21448 (BROKEN): Requesting layer-based corporate_entities returns empty/missing" | ||
|
|
||
| # OLD behavior: has numeric indices instead | ||
| old_flat_all = convert_to_flat_dict(first_fixture, columns="all") | ||
| numeric_keys = [ | ||
| k | ||
| for k in old_flat_all.keys() | ||
| if "corporate_entities.0." in k or "corporate_entities.1." in k | ||
| ] | ||
| assert ( | ||
| len(numeric_keys) > 0 | ||
| ), "RND-21448 (BROKEN): Old flattener produces numeric indices like corporate_entities.0.label" | ||
|
|
||
| def test_rnd_21448_new_fixture_flattener_works(self): | ||
| """ | ||
| RND-21448: After fix, the new fixture flattener correctly groups corporate_entities | ||
| by layer name, making layer-based columns accessible and populated. | ||
|
|
||
| This test verifies the FIX works: convert_fixture_to_flat_dict produces layer-based keys | ||
| with actual values matching what's in to_list(). | ||
| """ | ||
| result = ( | ||
| Fixtures() | ||
| .search( | ||
| filter_time_min=datetime(2020, 1, 1), | ||
| filter_time_max=datetime(2020, 1, 2), | ||
| ) | ||
| .to_list() | ||
| ) | ||
|
|
||
| assert len(result) > 0 | ||
| first_fixture = result[0] | ||
| first_fixture_dict = first_fixture.model_dump() | ||
|
|
||
| new_flat = convert_fixture_to_flat_dict( | ||
| first_fixture_dict, columns="all" | ||
| ) | ||
|
|
||
| layer_based_keys = [ | ||
| k | ||
| for k in new_flat.keys() | ||
| if "corporate_entities.effective_controller." in k | ||
| or "corporate_entities.time_charterer." in k | ||
| or "corporate_entities.charterer." in k | ||
| ] | ||
| assert ( | ||
| len(layer_based_keys) > 0 | ||
| ), "RND-21448 (fixed): new flattener should produce layer-based keys" | ||
|
|
||
| for key in layer_based_keys: | ||
| value = new_flat.get(key) | ||
| if value is not None: | ||
| # At least some layer-based values should be populated | ||
| assert True | ||
| break | ||
| else: | ||
| assert ( | ||
| len(layer_based_keys) > 0 | ||
| ), "RND-21448 (fixed): layer-based keys should be present" | ||
|
|
||
| assert "tonnes" in new_flat | ||
| assert new_flat.get("tonnes") is not None | ||
|
|
||
| def test_rnd_21448_to_df_integration(self): | ||
| """ | ||
| RND-21448: Verify that to_df() with the new fixture flattener returns | ||
| populated nested vessel fields (matching to_list() data). | ||
| """ | ||
|
|
||
| list_result = ( | ||
| Fixtures() | ||
| .search( | ||
| filter_time_field="fixing_timestamp", | ||
| filter_time_min=datetime(2020, 1, 1), | ||
| filter_time_max=datetime(2020, 1, 2), | ||
| ) | ||
| .to_list() | ||
| ) | ||
|
|
||
| assert len(list_result) > 0 | ||
|
|
||
| df = ( | ||
| Fixtures() | ||
| .search( | ||
| filter_time_min=datetime(2020, 1, 1), | ||
| filter_time_max=datetime(2020, 1, 2), | ||
| ) | ||
| .to_df( | ||
| columns=[ | ||
| "id", | ||
| "vessel.id", | ||
| "vessel.name", | ||
| "vessel.imo", | ||
| "tonnes", | ||
| "vessel.corporate_entities.effective_controller.label", | ||
| "vessel.corporate_entities.time_charterer.label", | ||
| "charterer.label", | ||
| ] | ||
| ) | ||
| ) | ||
|
|
||
| assert len(df) > 0 | ||
|
|
||
| assert df["vessel.name"].notna().any() | ||
| assert df["vessel.imo"].notna().any() | ||
| assert df["tonnes"].notna().any() | ||
|
|
||
| has_effective_controller = ( | ||
| df["vessel.corporate_entities.effective_controller.label"] | ||
| .notna() | ||
| .any() | ||
| ) | ||
| has_time_charterer = ( | ||
| df["vessel.corporate_entities.time_charterer.label"].notna().any() | ||
| ) | ||
| assert has_effective_controller or has_time_charterer | ||
|
|
||
| def test_to_df_returns_data(self): | ||
| """Test that to_df() returns fixtures with default columns.""" | ||
| df = ( | ||
| Fixtures() | ||
| .search( | ||
| filter_time_field="fixing_timestamp", | ||
| filter_time_min=datetime(2020, 1, 1), | ||
| filter_time_max=datetime(2020, 1, 2), | ||
| ) | ||
| .to_df() | ||
| ) | ||
|
|
||
| assert len(df) > 0 | ||
| assert "vessel.name" in df.columns | ||
| assert "tonnes" in df.columns | ||
| assert "charterer.label" in df.columns | ||
|
|
||
| def test_to_df_nested_corporate_entities_populated(self): | ||
| """Test that nested vessel.corporate_entities fields are populated (RND-21448).""" | ||
| df = ( | ||
| Fixtures() | ||
| .search( | ||
| filter_time_field="fixing_timestamp", | ||
| filter_time_min=datetime(2020, 1, 1), | ||
| filter_time_max=datetime(2020, 1, 2), | ||
| ) | ||
| .to_df( | ||
| columns=[ | ||
| "id", | ||
| "vessel.name", | ||
| "vessel.imo", | ||
| "vessel.corporate_entities.effective_controller.label", | ||
| "vessel.corporate_entities.time_charterer.label", | ||
| "tonnes", | ||
| ] | ||
| ) | ||
| ) | ||
|
|
||
| assert len(df) > 0 | ||
| assert "vessel.name" in df.columns | ||
| assert ( | ||
| "vessel.corporate_entities.effective_controller.label" | ||
| in df.columns | ||
| ) | ||
| assert ( | ||
| df["vessel.corporate_entities.effective_controller.label"] | ||
| .notna() | ||
| .any() | ||
| ) | ||
|
|
||
| def test_to_df_tonnes_column_populated(self): | ||
| """Test that tonnes column (fixed typo from tones) is populated.""" | ||
| df = ( | ||
| Fixtures() | ||
| .search( | ||
| filter_time_field="fixing_timestamp", | ||
| filter_time_min=datetime(2020, 1, 1), | ||
| filter_time_max=datetime(2020, 1, 2), | ||
| ) | ||
| .to_df(columns=["id", "tonnes"]) | ||
| ) | ||
|
|
||
| assert len(df) > 0 | ||
| assert "tonnes" in df.columns | ||
| assert df["tonnes"].notna().sum() > len(df) * 0.5 |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would appreciate some input on this. I think because changing to tonnes could be a breaking change it is doing this to make sure that people don't have to modify anything but idk if that's a good practice