|
| 1 | +import json |
1 | 2 | from io import BytesIO |
2 | 3 |
|
3 | 4 | import cf_xarray # noqa: F401 |
@@ -615,6 +616,45 @@ def test_cf_multiple_position_csv(cf_client): |
615 | 616 | assert key in csv_data[0], f"column {key} should be in the header" |
616 | 617 |
|
617 | 618 |
|
| 619 | +def test_cf_post_position_csv_body(cf_client): |
| 620 | + response = cf_client.post( |
| 621 | + "/datasets/air/edr/position", |
| 622 | + content="lon,lat\n202,43\n205,45\n", |
| 623 | + headers={"content-type": "text/csv"}, |
| 624 | + ) |
| 625 | + assert response.status_code == 200, response.text |
| 626 | + axes = response.json()["domain"]["axes"] |
| 627 | + assert axes["x"] == {"values": [202.5, 205.0]} |
| 628 | + assert axes["y"] == {"values": [42.5, 45.0]} |
| 629 | + |
| 630 | + |
| 631 | +def test_cf_post_position_geojson_body(cf_client): |
| 632 | + body = { |
| 633 | + "type": "FeatureCollection", |
| 634 | + "features": [ |
| 635 | + { |
| 636 | + "type": "Feature", |
| 637 | + "geometry": {"type": "Point", "coordinates": [202, 43]}, |
| 638 | + "properties": {}, |
| 639 | + }, |
| 640 | + { |
| 641 | + "type": "Feature", |
| 642 | + "geometry": {"type": "Point", "coordinates": [205, 45]}, |
| 643 | + "properties": {}, |
| 644 | + }, |
| 645 | + ], |
| 646 | + } |
| 647 | + response = cf_client.post( |
| 648 | + "/datasets/air/edr/position", |
| 649 | + json=body, |
| 650 | + headers={"content-type": "application/geo+json"}, |
| 651 | + ) |
| 652 | + assert response.status_code == 200, response.text |
| 653 | + axes = response.json()["domain"]["axes"] |
| 654 | + assert axes["x"] == {"values": [202.5, 205.0]} |
| 655 | + assert axes["y"] == {"values": [42.5, 45.0]} |
| 656 | + |
| 657 | + |
618 | 658 | def test_cf_area_query(cf_client, cf_air_dataset): |
619 | 659 | coords = "POLYGON((201 41, 201 49, 209 49, 209 41, 201 41))" |
620 | 660 | response = cf_client.get(f"/datasets/air/edr/area?coords={coords}&f=cf_covjson") |
@@ -716,6 +756,56 @@ def test_cf_area_geojson_query(cf_client, cf_air_dataset): |
716 | 756 | assert len(features) == 36, "There should be 36 data points" |
717 | 757 |
|
718 | 758 |
|
| 759 | +@pytest.mark.parametrize( |
| 760 | + "content_type,body,expected_status", |
| 761 | + [ |
| 762 | + ( |
| 763 | + "application/geo+json", |
| 764 | + json.dumps( |
| 765 | + { |
| 766 | + "type": "Feature", |
| 767 | + "geometry": { |
| 768 | + "type": "Polygon", |
| 769 | + "coordinates": [ |
| 770 | + [[201, 41], [201, 49], [209, 49], [209, 41], [201, 41]], |
| 771 | + ], |
| 772 | + }, |
| 773 | + "properties": {}, |
| 774 | + }, |
| 775 | + ), |
| 776 | + 200, |
| 777 | + ), |
| 778 | + ( |
| 779 | + "application/wkt", |
| 780 | + "POLYGON((201 41, 201 49, 209 49, 209 41, 201 41))", |
| 781 | + 200, |
| 782 | + ), |
| 783 | + ("application/geo+json", "", 422), |
| 784 | + ( |
| 785 | + "application/geo+json", |
| 786 | + json.dumps({"type": "Point", "coordinates": [202, 43]}), |
| 787 | + 422, |
| 788 | + ), |
| 789 | + ], |
| 790 | + ids=["geojson_feature", "wkt", "empty_body", "point_rejected"], |
| 791 | +) |
| 792 | +def test_cf_post_area_body(cf_client, content_type, body, expected_status): |
| 793 | + response = cf_client.post( |
| 794 | + "/datasets/air/edr/area", |
| 795 | + content=body, |
| 796 | + headers={"content-type": content_type}, |
| 797 | + ) |
| 798 | + assert response.status_code == expected_status, response.text |
| 799 | + if expected_status == 200: |
| 800 | + axes = response.json()["domain"]["axes"] |
| 801 | + assert axes["x"] == { |
| 802 | + "values": [202.5, 205.0, 207.5, 202.5, 205.0, 207.5, 202.5, 205.0, 207.5], |
| 803 | + } |
| 804 | + assert axes["y"] == { |
| 805 | + "values": [47.5, 47.5, 47.5, 45.0, 45.0, 45.0, 42.5, 42.5, 42.5], |
| 806 | + } |
| 807 | + |
| 808 | + |
719 | 809 | def test_cf_area_nc_query(cf_client, cf_air_dataset): |
720 | 810 | coords = "POLYGON((201 41, 201 49, 209 49, 209 41, 201 41))" |
721 | 811 | response = cf_client.get(f"/datasets/air/edr/area?coords={coords}&f=nc") |
|
0 commit comments