|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 | """Tests for ohsome API response"""
|
4 | 4 | import warnings
|
| 5 | +from datetime import datetime |
5 | 6 |
|
6 | 7 | import geopandas as gpd
|
7 | 8 | import pandas as pd
|
8 | 9 | import pytest
|
| 10 | +from geopandas.testing import assert_geodataframe_equal |
| 11 | +from shapely import Point |
9 | 12 |
|
10 | 13 |
|
11 | 14 | @pytest.mark.vcr
|
@@ -463,3 +466,80 @@ def test_all_columns_with_timestamps_to_be_without_timezone(base_client):
|
463 | 466 | assert at_timestamp.tz is None
|
464 | 467 | assert timestamp.tz is None
|
465 | 468 | assert at_snapshotTimestamp.tz is None
|
| 469 | + |
| 470 | + |
| 471 | +def test_explode_tags(dummy_ohsome_response): |
| 472 | + """Test if the explode_tags parameter explodes tags.""" |
| 473 | + expected_df = gpd.GeoDataFrame( |
| 474 | + data={ |
| 475 | + "highway": ["primary"], |
| 476 | + "@other_tags": [{"width": "10"}], |
| 477 | + "@snapshotTimestamp": [datetime(2024, 1, 1)], |
| 478 | + "@osmId": ["node/1234"], |
| 479 | + }, |
| 480 | + geometry=[Point(0, 0)], |
| 481 | + crs="EPSG:4326", |
| 482 | + ) |
| 483 | + |
| 484 | + computed_df = dummy_ohsome_response.as_dataframe( |
| 485 | + explode_tags=("highway",), multi_index=False |
| 486 | + ) |
| 487 | + |
| 488 | + assert_geodataframe_equal(computed_df, expected_df, check_like=True) |
| 489 | + |
| 490 | + |
| 491 | +def test_explode_tags_none(dummy_ohsome_response): |
| 492 | + """Test if the explode_tags parameter can be set to explode all (to get previous behaviour).""" |
| 493 | + expected_df = gpd.GeoDataFrame( |
| 494 | + data={ |
| 495 | + "highway": ["primary"], |
| 496 | + "width": ["10"], |
| 497 | + "@snapshotTimestamp": [datetime(2024, 1, 1)], |
| 498 | + "@osmId": ["node/1234"], |
| 499 | + }, |
| 500 | + geometry=[Point(0, 0)], |
| 501 | + crs="EPSG:4326", |
| 502 | + ) |
| 503 | + |
| 504 | + computed_df = dummy_ohsome_response.as_dataframe( |
| 505 | + explode_tags=None, multi_index=False |
| 506 | + ) |
| 507 | + |
| 508 | + assert_geodataframe_equal(computed_df, expected_df, check_like=True) |
| 509 | + |
| 510 | + |
| 511 | +def test_explode_tags_empy(dummy_ohsome_response): |
| 512 | + """Test if explode_tags parameter can be disabled.""" |
| 513 | + expected_df = gpd.GeoDataFrame( |
| 514 | + data={ |
| 515 | + "@other_tags": [{"width": "10", "highway": "primary"}], |
| 516 | + "@snapshotTimestamp": [datetime(2024, 1, 1)], |
| 517 | + "@osmId": ["node/1234"], |
| 518 | + }, |
| 519 | + geometry=[Point(0, 0)], |
| 520 | + crs="EPSG:4326", |
| 521 | + ) |
| 522 | + |
| 523 | + computed_df = dummy_ohsome_response.as_dataframe(explode_tags=(), multi_index=False) |
| 524 | + |
| 525 | + assert_geodataframe_equal(computed_df, expected_df, check_like=True) |
| 526 | + |
| 527 | + |
| 528 | +def test_explode_tags_missing_in_response(dummy_ohsome_response): |
| 529 | + """Test if the explode_tags keys are always present in the result, even if they are not part of the response.""" |
| 530 | + expected_df = gpd.GeoDataFrame( |
| 531 | + data={ |
| 532 | + "this_key_does_not_exist": [None], |
| 533 | + "@other_tags": [{"width": "10", "highway": "primary"}], |
| 534 | + "@snapshotTimestamp": [datetime(2024, 1, 1)], |
| 535 | + "@osmId": ["node/1234"], |
| 536 | + }, |
| 537 | + geometry=[Point(0, 0)], |
| 538 | + crs="EPSG:4326", |
| 539 | + ) |
| 540 | + |
| 541 | + computed_df = dummy_ohsome_response.as_dataframe( |
| 542 | + explode_tags=("this_key_does_not_exist",), multi_index=False |
| 543 | + ) |
| 544 | + |
| 545 | + assert_geodataframe_equal(computed_df, expected_df, check_like=True) |
0 commit comments