mirror of
				https://github.com/ilri/dspace-statistics-api.git
				synced 2025-11-04 14:39:09 +01:00 
			
		
		
		
	Compare commits
	
		
			2 Commits
		
	
	
		
			ab82e90773
			...
			3e271c7852
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						3e271c7852
	
				 | 
					
					
						|||
| 
						
						
							
						
						d7ba14c590
	
				 | 
					
					
						
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										373
									
								
								tests/test_api_collections.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										373
									
								
								tests/test_api_collections.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,373 @@
 | 
				
			|||||||
 | 
					from falcon import testing
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					import pytest
 | 
				
			||||||
 | 
					from unittest.mock import patch
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from dspace_statistics_api.app import api
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.fixture
 | 
				
			||||||
 | 
					def client():
 | 
				
			||||||
 | 
					    return testing.TestClient(api)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_get_collection(client):
 | 
				
			||||||
 | 
					    """Test requesting a single collection."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_get("/collection/8ea4b611-1f59-4d4e-b78d-a9921a72cfe7")
 | 
				
			||||||
 | 
					    response_doc = json.loads(response.text)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert isinstance(response_doc["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response_doc["id"], str)
 | 
				
			||||||
 | 
					    assert isinstance(response_doc["views"], int)
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_get_missing_collection(client):
 | 
				
			||||||
 | 
					    """Test requesting a single non-existing collection."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_get("/collection/508abe0a-689f-402e-885d-2f6b02e7a39c")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 404
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_get_collections(client):
 | 
				
			||||||
 | 
					    """Test requesting 100 collections."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_get("/collections", query_string="limit=100")
 | 
				
			||||||
 | 
					    response_doc = json.loads(response.text)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert isinstance(response_doc["currentPage"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response_doc["totalPages"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response_doc["statistics"], list)
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_get_collections_invalid_limit(client):
 | 
				
			||||||
 | 
					    """Test requesting 100 collections with an invalid limit parameter."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_get("/collections", query_string="limit=101")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 400
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_get_collections_invalid_page(client):
 | 
				
			||||||
 | 
					    """Test requesting 100 collections with an invalid page parameter."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_get("/collections", query_string="page=-1")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 400
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.mark.xfail
 | 
				
			||||||
 | 
					def test_post_collections_valid_dateFrom(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /collections with a valid dateFrom parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "dateFrom": "2020-01-01T00:00:00Z",
 | 
				
			||||||
 | 
					        "collections": [
 | 
				
			||||||
 | 
					            "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
 | 
				
			||||||
 | 
					            "260548c8-fda4-4dc8-a979-03495753cdd5",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/collections", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 100
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 2
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_collections_valid_dateFrom_mocked(client):
 | 
				
			||||||
 | 
					    """Mock test POSTing a request to /collections with a valid dateFrom parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "dateFrom": "2020-01-01T00:00:00Z",
 | 
				
			||||||
 | 
					        "collections": [
 | 
				
			||||||
 | 
					            "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
 | 
				
			||||||
 | 
					            "260548c8-fda4-4dc8-a979-03495753cdd5",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get_views_return_value = {
 | 
				
			||||||
 | 
					        "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 21,
 | 
				
			||||||
 | 
					        "260548c8-fda4-4dc8-a979-03495753cdd5": 0,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    get_downloads_return_value = {
 | 
				
			||||||
 | 
					        "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 575,
 | 
				
			||||||
 | 
					        "260548c8-fda4-4dc8-a979-03495753cdd5": 899,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with patch(
 | 
				
			||||||
 | 
					        "dspace_statistics_api.app.get_views", return_value=get_views_return_value
 | 
				
			||||||
 | 
					    ):
 | 
				
			||||||
 | 
					        with patch(
 | 
				
			||||||
 | 
					            "dspace_statistics_api.app.get_downloads",
 | 
				
			||||||
 | 
					            return_value=get_downloads_return_value,
 | 
				
			||||||
 | 
					        ):
 | 
				
			||||||
 | 
					            response = client.simulate_post("/collections", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 100
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 2
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_collections_invalid_dateFrom(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /collections with an invalid dateFrom parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "dateFrom": "2020-01-01T00:00:00",
 | 
				
			||||||
 | 
					        "collections": [
 | 
				
			||||||
 | 
					            "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
 | 
				
			||||||
 | 
					            "260548c8-fda4-4dc8-a979-03495753cdd5",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/collections", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 400
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.mark.xfail
 | 
				
			||||||
 | 
					def test_post_collections_valid_dateTo(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /collections with a valid dateTo parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "dateTo": "2020-01-01T00:00:00Z",
 | 
				
			||||||
 | 
					        "collections": [
 | 
				
			||||||
 | 
					            "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
 | 
				
			||||||
 | 
					            "260548c8-fda4-4dc8-a979-03495753cdd5",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/collections", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 100
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 2
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_collections_valid_dateTo_mocked(client):
 | 
				
			||||||
 | 
					    """Mock test POSTing a request to /collections with a valid dateTo parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "dateTo": "2020-01-01T00:00:00Z",
 | 
				
			||||||
 | 
					        "collections": [
 | 
				
			||||||
 | 
					            "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
 | 
				
			||||||
 | 
					            "260548c8-fda4-4dc8-a979-03495753cdd5",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get_views_return_value = {
 | 
				
			||||||
 | 
					        "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 21,
 | 
				
			||||||
 | 
					        "260548c8-fda4-4dc8-a979-03495753cdd5": 0,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    get_downloads_return_value = {
 | 
				
			||||||
 | 
					        "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 575,
 | 
				
			||||||
 | 
					        "260548c8-fda4-4dc8-a979-03495753cdd5": 899,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with patch(
 | 
				
			||||||
 | 
					        "dspace_statistics_api.app.get_views", return_value=get_views_return_value
 | 
				
			||||||
 | 
					    ):
 | 
				
			||||||
 | 
					        with patch(
 | 
				
			||||||
 | 
					            "dspace_statistics_api.app.get_downloads",
 | 
				
			||||||
 | 
					            return_value=get_downloads_return_value,
 | 
				
			||||||
 | 
					        ):
 | 
				
			||||||
 | 
					            response = client.simulate_post("/collections", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 100
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 2
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_collections_invalid_dateTo(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /collections with an invalid dateTo parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "dateFrom": "2020-01-01T00:00:00",
 | 
				
			||||||
 | 
					        "collections": [
 | 
				
			||||||
 | 
					            "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
 | 
				
			||||||
 | 
					            "260548c8-fda4-4dc8-a979-03495753cdd5",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/collections", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 400
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.mark.xfail
 | 
				
			||||||
 | 
					def test_post_collections_valid_limit(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /collections with a valid limit parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "limit": 1,
 | 
				
			||||||
 | 
					        "collections": [
 | 
				
			||||||
 | 
					            "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
 | 
				
			||||||
 | 
					            "260548c8-fda4-4dc8-a979-03495753cdd5",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/collections", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 1
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 1
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_collections_valid_limit_mocked(client):
 | 
				
			||||||
 | 
					    """Mock test POSTing a request to /collections with a valid limit parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "limit": 1,
 | 
				
			||||||
 | 
					        "collections": [
 | 
				
			||||||
 | 
					            "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
 | 
				
			||||||
 | 
					            "260548c8-fda4-4dc8-a979-03495753cdd5",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get_views_return_value = {"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 21}
 | 
				
			||||||
 | 
					    get_downloads_return_value = {"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 575}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with patch(
 | 
				
			||||||
 | 
					        "dspace_statistics_api.app.get_views", return_value=get_views_return_value
 | 
				
			||||||
 | 
					    ):
 | 
				
			||||||
 | 
					        with patch(
 | 
				
			||||||
 | 
					            "dspace_statistics_api.app.get_downloads",
 | 
				
			||||||
 | 
					            return_value=get_downloads_return_value,
 | 
				
			||||||
 | 
					        ):
 | 
				
			||||||
 | 
					            response = client.simulate_post("/collections", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 1
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 1
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_collections_invalid_limit(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /collections with an invalid limit parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "limit": -1,
 | 
				
			||||||
 | 
					        "collections": [
 | 
				
			||||||
 | 
					            "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
 | 
				
			||||||
 | 
					            "260548c8-fda4-4dc8-a979-03495753cdd5",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/collections", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 400
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.mark.xfail
 | 
				
			||||||
 | 
					def test_post_collections_valid_page(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /collections with a valid page parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "page": 0,
 | 
				
			||||||
 | 
					        "collections": [
 | 
				
			||||||
 | 
					            "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
 | 
				
			||||||
 | 
					            "260548c8-fda4-4dc8-a979-03495753cdd5",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/collections", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 100
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert response.json["totalPages"] == 0
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 2
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_collections_valid_page_mocked(client):
 | 
				
			||||||
 | 
					    """Mock test POSTing a request to /collections with a valid page parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "page": 0,
 | 
				
			||||||
 | 
					        "collections": [
 | 
				
			||||||
 | 
					            "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
 | 
				
			||||||
 | 
					            "260548c8-fda4-4dc8-a979-03495753cdd5",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get_views_return_value = {
 | 
				
			||||||
 | 
					        "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 21,
 | 
				
			||||||
 | 
					        "260548c8-fda4-4dc8-a979-03495753cdd5": 0,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    get_downloads_return_value = {
 | 
				
			||||||
 | 
					        "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 575,
 | 
				
			||||||
 | 
					        "260548c8-fda4-4dc8-a979-03495753cdd5": 899,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with patch(
 | 
				
			||||||
 | 
					        "dspace_statistics_api.app.get_views", return_value=get_views_return_value
 | 
				
			||||||
 | 
					    ):
 | 
				
			||||||
 | 
					        with patch(
 | 
				
			||||||
 | 
					            "dspace_statistics_api.app.get_downloads",
 | 
				
			||||||
 | 
					            return_value=get_downloads_return_value,
 | 
				
			||||||
 | 
					        ):
 | 
				
			||||||
 | 
					            response = client.simulate_post("/collections", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 100
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 2
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_collections_invalid_page(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /collections with an invalid page parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "page": -1,
 | 
				
			||||||
 | 
					        "collections": [
 | 
				
			||||||
 | 
					            "8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
 | 
				
			||||||
 | 
					            "260548c8-fda4-4dc8-a979-03495753cdd5",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/collections", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 400
 | 
				
			||||||
							
								
								
									
										373
									
								
								tests/test_api_communities.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										373
									
								
								tests/test_api_communities.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,373 @@
 | 
				
			|||||||
 | 
					from falcon import testing
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					import pytest
 | 
				
			||||||
 | 
					from unittest.mock import patch
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from dspace_statistics_api.app import api
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.fixture
 | 
				
			||||||
 | 
					def client():
 | 
				
			||||||
 | 
					    return testing.TestClient(api)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_get_community(client):
 | 
				
			||||||
 | 
					    """Test requesting a single community."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_get("/community/bde7139c-d321-46bb-aef6-ae70799e5edb")
 | 
				
			||||||
 | 
					    response_doc = json.loads(response.text)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert isinstance(response_doc["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response_doc["id"], str)
 | 
				
			||||||
 | 
					    assert isinstance(response_doc["views"], int)
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_get_missing_community(client):
 | 
				
			||||||
 | 
					    """Test requesting a single non-existing community."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_get("/item/dec6bfc6-efeb-4f74-8436-79fa80bb5c21")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 404
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_get_communities(client):
 | 
				
			||||||
 | 
					    """Test requesting 100 communities."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_get("/communities", query_string="limit=100")
 | 
				
			||||||
 | 
					    response_doc = json.loads(response.text)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert isinstance(response_doc["currentPage"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response_doc["totalPages"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response_doc["statistics"], list)
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_get_communities_invalid_limit(client):
 | 
				
			||||||
 | 
					    """Test requesting 100 communities with an invalid limit parameter."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_get("/communities", query_string="limit=101")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 400
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_get_communities_invalid_page(client):
 | 
				
			||||||
 | 
					    """Test requesting 100 communities with an invalid page parameter."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_get("/communities", query_string="page=-1")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 400
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.mark.xfail
 | 
				
			||||||
 | 
					def test_post_communities_valid_dateFrom(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /communities with a valid dateFrom parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "dateFrom": "2020-01-01T00:00:00Z",
 | 
				
			||||||
 | 
					        "communities": [
 | 
				
			||||||
 | 
					            "bde7139c-d321-46bb-aef6-ae70799e5edb",
 | 
				
			||||||
 | 
					            "2a920a61-b08a-4642-8e5d-2639c6702b1f",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/communities", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 100
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 2
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_communities_valid_dateFrom_mocked(client):
 | 
				
			||||||
 | 
					    """Mock test POSTing a request to /communities with a valid dateFrom parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "dateFrom": "2020-01-01T00:00:00Z",
 | 
				
			||||||
 | 
					        "communities": [
 | 
				
			||||||
 | 
					            "bde7139c-d321-46bb-aef6-ae70799e5edb",
 | 
				
			||||||
 | 
					            "2a920a61-b08a-4642-8e5d-2639c6702b1f",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get_views_return_value = {
 | 
				
			||||||
 | 
					        "bde7139c-d321-46bb-aef6-ae70799e5edb": 309,
 | 
				
			||||||
 | 
					        "2a920a61-b08a-4642-8e5d-2639c6702b1f": 0,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    get_downloads_return_value = {
 | 
				
			||||||
 | 
					        "bde7139c-d321-46bb-aef6-ae70799e5edb": 400,
 | 
				
			||||||
 | 
					        "2a920a61-b08a-4642-8e5d-2639c6702b1f": 290,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with patch(
 | 
				
			||||||
 | 
					        "dspace_statistics_api.app.get_views", return_value=get_views_return_value
 | 
				
			||||||
 | 
					    ):
 | 
				
			||||||
 | 
					        with patch(
 | 
				
			||||||
 | 
					            "dspace_statistics_api.app.get_downloads",
 | 
				
			||||||
 | 
					            return_value=get_downloads_return_value,
 | 
				
			||||||
 | 
					        ):
 | 
				
			||||||
 | 
					            response = client.simulate_post("/communities", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 100
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 2
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_communities_invalid_dateFrom(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /communities with an invalid dateFrom parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "dateFrom": "2020-01-01T00:00:00",
 | 
				
			||||||
 | 
					        "communities": [
 | 
				
			||||||
 | 
					            "bde7139c-d321-46bb-aef6-ae70799e5edb",
 | 
				
			||||||
 | 
					            "2a920a61-b08a-4642-8e5d-2639c6702b1f",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/communities", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 400
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.mark.xfail
 | 
				
			||||||
 | 
					def test_post_communities_valid_dateTo(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /communities with a valid dateTo parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "dateTo": "2020-01-01T00:00:00Z",
 | 
				
			||||||
 | 
					        "communities": [
 | 
				
			||||||
 | 
					            "bde7139c-d321-46bb-aef6-ae70799e5edb",
 | 
				
			||||||
 | 
					            "2a920a61-b08a-4642-8e5d-2639c6702b1f",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/communities", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 100
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 2
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_communities_valid_dateTo_mocked(client):
 | 
				
			||||||
 | 
					    """Mock test POSTing a request to /communities with a valid dateTo parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "dateTo": "2020-01-01T00:00:00Z",
 | 
				
			||||||
 | 
					        "communities": [
 | 
				
			||||||
 | 
					            "bde7139c-d321-46bb-aef6-ae70799e5edb",
 | 
				
			||||||
 | 
					            "2a920a61-b08a-4642-8e5d-2639c6702b1f",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get_views_return_value = {
 | 
				
			||||||
 | 
					        "bde7139c-d321-46bb-aef6-ae70799e5edb": 21,
 | 
				
			||||||
 | 
					        "2a920a61-b08a-4642-8e5d-2639c6702b1f": 0,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    get_downloads_return_value = {
 | 
				
			||||||
 | 
					        "bde7139c-d321-46bb-aef6-ae70799e5edb": 575,
 | 
				
			||||||
 | 
					        "2a920a61-b08a-4642-8e5d-2639c6702b1f": 899,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with patch(
 | 
				
			||||||
 | 
					        "dspace_statistics_api.app.get_views", return_value=get_views_return_value
 | 
				
			||||||
 | 
					    ):
 | 
				
			||||||
 | 
					        with patch(
 | 
				
			||||||
 | 
					            "dspace_statistics_api.app.get_downloads",
 | 
				
			||||||
 | 
					            return_value=get_downloads_return_value,
 | 
				
			||||||
 | 
					        ):
 | 
				
			||||||
 | 
					            response = client.simulate_post("/communities", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 100
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 2
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_communities_invalid_dateTo(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /communities with an invalid dateTo parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "dateFrom": "2020-01-01T00:00:00",
 | 
				
			||||||
 | 
					        "communities": [
 | 
				
			||||||
 | 
					            "bde7139c-d321-46bb-aef6-ae70799e5edb",
 | 
				
			||||||
 | 
					            "2a920a61-b08a-4642-8e5d-2639c6702b1f",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/communities", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 400
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.mark.xfail
 | 
				
			||||||
 | 
					def test_post_communities_valid_limit(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /communities with a valid limit parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "limit": 1,
 | 
				
			||||||
 | 
					        "communities": [
 | 
				
			||||||
 | 
					            "bde7139c-d321-46bb-aef6-ae70799e5edb",
 | 
				
			||||||
 | 
					            "2a920a61-b08a-4642-8e5d-2639c6702b1f",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/communities", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 1
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 1
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_communities_valid_limit_mocked(client):
 | 
				
			||||||
 | 
					    """Mock test POSTing a request to /communities with a valid limit parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "limit": 1,
 | 
				
			||||||
 | 
					        "communities": [
 | 
				
			||||||
 | 
					            "bde7139c-d321-46bb-aef6-ae70799e5edb",
 | 
				
			||||||
 | 
					            "2a920a61-b08a-4642-8e5d-2639c6702b1f",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get_views_return_value = {"bde7139c-d321-46bb-aef6-ae70799e5edb": 200}
 | 
				
			||||||
 | 
					    get_downloads_return_value = {"bde7139c-d321-46bb-aef6-ae70799e5edb": 309}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with patch(
 | 
				
			||||||
 | 
					        "dspace_statistics_api.app.get_views", return_value=get_views_return_value
 | 
				
			||||||
 | 
					    ):
 | 
				
			||||||
 | 
					        with patch(
 | 
				
			||||||
 | 
					            "dspace_statistics_api.app.get_downloads",
 | 
				
			||||||
 | 
					            return_value=get_downloads_return_value,
 | 
				
			||||||
 | 
					        ):
 | 
				
			||||||
 | 
					            response = client.simulate_post("/communities", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 1
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 1
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_communities_invalid_limit(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /communities with an invalid limit parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "limit": -1,
 | 
				
			||||||
 | 
					        "communities": [
 | 
				
			||||||
 | 
					            "bde7139c-d321-46bb-aef6-ae70799e5edb",
 | 
				
			||||||
 | 
					            "2a920a61-b08a-4642-8e5d-2639c6702b1f",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/communities", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 400
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.mark.xfail
 | 
				
			||||||
 | 
					def test_post_communities_valid_page(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /communities with a valid page parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "page": 0,
 | 
				
			||||||
 | 
					        "communities": [
 | 
				
			||||||
 | 
					            "bde7139c-d321-46bb-aef6-ae70799e5edb",
 | 
				
			||||||
 | 
					            "2a920a61-b08a-4642-8e5d-2639c6702b1f",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/communities", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 100
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert response.json["totalPages"] == 0
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 2
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_communities_valid_page_mocked(client):
 | 
				
			||||||
 | 
					    """Mock test POSTing a request to communities with a valid page parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "page": 0,
 | 
				
			||||||
 | 
					        "communities": [
 | 
				
			||||||
 | 
					            "bde7139c-d321-46bb-aef6-ae70799e5edb",
 | 
				
			||||||
 | 
					            "2a920a61-b08a-4642-8e5d-2639c6702b1f",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get_views_return_value = {
 | 
				
			||||||
 | 
					        "bde7139c-d321-46bb-aef6-ae70799e5edb": 21,
 | 
				
			||||||
 | 
					        "2a920a61-b08a-4642-8e5d-2639c6702b1f": 0,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    get_downloads_return_value = {
 | 
				
			||||||
 | 
					        "bde7139c-d321-46bb-aef6-ae70799e5edb": 575,
 | 
				
			||||||
 | 
					        "2a920a61-b08a-4642-8e5d-2639c6702b1f": 899,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with patch(
 | 
				
			||||||
 | 
					        "dspace_statistics_api.app.get_views", return_value=get_views_return_value
 | 
				
			||||||
 | 
					    ):
 | 
				
			||||||
 | 
					        with patch(
 | 
				
			||||||
 | 
					            "dspace_statistics_api.app.get_downloads",
 | 
				
			||||||
 | 
					            return_value=get_downloads_return_value,
 | 
				
			||||||
 | 
					        ):
 | 
				
			||||||
 | 
					            response = client.simulate_post("/communities", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
 | 
					    assert response.json["limit"] == 100
 | 
				
			||||||
 | 
					    assert response.json["currentPage"] == 0
 | 
				
			||||||
 | 
					    assert isinstance(response.json["totalPages"], int)
 | 
				
			||||||
 | 
					    assert len(response.json["statistics"]) == 2
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][0]["downloads"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["views"], int)
 | 
				
			||||||
 | 
					    assert isinstance(response.json["statistics"][1]["downloads"], int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_post_communities_invalid_page(client):
 | 
				
			||||||
 | 
					    """Test POSTing a request to /communities with an invalid page parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    request_body = {
 | 
				
			||||||
 | 
					        "page": -1,
 | 
				
			||||||
 | 
					        "communities": [
 | 
				
			||||||
 | 
					            "bde7139c-d321-46bb-aef6-ae70799e5edb",
 | 
				
			||||||
 | 
					            "2a920a61-b08a-4642-8e5d-2639c6702b1f",
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_post("/communities", json=request_body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert response.status_code == 400
 | 
				
			||||||
							
								
								
									
										18
									
								
								tests/test_api_docs.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								tests/test_api_docs.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					from falcon import testing
 | 
				
			||||||
 | 
					import pytest
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from dspace_statistics_api.app import api
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.fixture
 | 
				
			||||||
 | 
					def client():
 | 
				
			||||||
 | 
					    return testing.TestClient(api)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_get_docs(client):
 | 
				
			||||||
 | 
					    """Test requesting the documentation at the root."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    response = client.simulate_get("/")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert isinstance(response.content, bytes)
 | 
				
			||||||
 | 
					    assert response.status_code == 200
 | 
				
			||||||
@@ -11,15 +11,6 @@ def client():
 | 
				
			|||||||
    return testing.TestClient(api)
 | 
					    return testing.TestClient(api)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_get_docs(client):
 | 
					 | 
				
			||||||
    """Test requesting the documentation at the root."""
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    response = client.simulate_get("/")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    assert isinstance(response.content, bytes)
 | 
					 | 
				
			||||||
    assert response.status_code == 200
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_get_item(client):
 | 
					def test_get_item(client):
 | 
				
			||||||
    """Test requesting a single item."""
 | 
					    """Test requesting a single item."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -70,7 +61,7 @@ def test_get_items_invalid_page(client):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
@pytest.mark.xfail
 | 
					@pytest.mark.xfail
 | 
				
			||||||
def test_post_items_valid_dateFrom(client):
 | 
					def test_post_items_valid_dateFrom(client):
 | 
				
			||||||
    """Test POSTing a request with a valid dateFrom parameter in the request body."""
 | 
					    """Test POSTing a request to /items with a valid dateFrom parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request_body = {
 | 
					    request_body = {
 | 
				
			||||||
        "dateFrom": "2020-01-01T00:00:00Z",
 | 
					        "dateFrom": "2020-01-01T00:00:00Z",
 | 
				
			||||||
@@ -94,7 +85,7 @@ def test_post_items_valid_dateFrom(client):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_post_items_valid_dateFrom_mocked(client):
 | 
					def test_post_items_valid_dateFrom_mocked(client):
 | 
				
			||||||
    """Mock test POSTing a request with a valid dateFrom parameter in the request body."""
 | 
					    """Mock test POSTing a request to /items with a valid dateFrom parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request_body = {
 | 
					    request_body = {
 | 
				
			||||||
        "dateFrom": "2020-01-01T00:00:00Z",
 | 
					        "dateFrom": "2020-01-01T00:00:00Z",
 | 
				
			||||||
@@ -134,7 +125,7 @@ def test_post_items_valid_dateFrom_mocked(client):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_post_items_invalid_dateFrom(client):
 | 
					def test_post_items_invalid_dateFrom(client):
 | 
				
			||||||
    """Test POSTing a request with an invalid dateFrom parameter in the request body."""
 | 
					    """Test POSTing a request to /items with an invalid dateFrom parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request_body = {
 | 
					    request_body = {
 | 
				
			||||||
        "dateFrom": "2020-01-01T00:00:00",
 | 
					        "dateFrom": "2020-01-01T00:00:00",
 | 
				
			||||||
@@ -151,7 +142,7 @@ def test_post_items_invalid_dateFrom(client):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
@pytest.mark.xfail
 | 
					@pytest.mark.xfail
 | 
				
			||||||
def test_post_items_valid_dateTo(client):
 | 
					def test_post_items_valid_dateTo(client):
 | 
				
			||||||
    """Test POSTing a request with a valid dateTo parameter in the request body."""
 | 
					    """Test POSTing a request to /items with a valid dateTo parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request_body = {
 | 
					    request_body = {
 | 
				
			||||||
        "dateTo": "2020-01-01T00:00:00Z",
 | 
					        "dateTo": "2020-01-01T00:00:00Z",
 | 
				
			||||||
@@ -175,7 +166,7 @@ def test_post_items_valid_dateTo(client):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_post_items_valid_dateTo_mocked(client):
 | 
					def test_post_items_valid_dateTo_mocked(client):
 | 
				
			||||||
    """Mock test POSTing a request with a valid dateTo parameter in the request body."""
 | 
					    """Mock test POSTing a request to /items with a valid dateTo parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request_body = {
 | 
					    request_body = {
 | 
				
			||||||
        "dateTo": "2020-01-01T00:00:00Z",
 | 
					        "dateTo": "2020-01-01T00:00:00Z",
 | 
				
			||||||
@@ -215,7 +206,7 @@ def test_post_items_valid_dateTo_mocked(client):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_post_items_invalid_dateTo(client):
 | 
					def test_post_items_invalid_dateTo(client):
 | 
				
			||||||
    """Test POSTing a request with an invalid dateTo parameter in the request body."""
 | 
					    """Test POSTing a request to /items with an invalid dateTo parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request_body = {
 | 
					    request_body = {
 | 
				
			||||||
        "dateFrom": "2020-01-01T00:00:00",
 | 
					        "dateFrom": "2020-01-01T00:00:00",
 | 
				
			||||||
@@ -232,7 +223,7 @@ def test_post_items_invalid_dateTo(client):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
@pytest.mark.xfail
 | 
					@pytest.mark.xfail
 | 
				
			||||||
def test_post_items_valid_limit(client):
 | 
					def test_post_items_valid_limit(client):
 | 
				
			||||||
    """Test POSTing a request with a valid limit parameter in the request body."""
 | 
					    """Test POSTing a request to /items with a valid limit parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request_body = {
 | 
					    request_body = {
 | 
				
			||||||
        "limit": 1,
 | 
					        "limit": 1,
 | 
				
			||||||
@@ -254,7 +245,7 @@ def test_post_items_valid_limit(client):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_post_items_valid_limit_mocked(client):
 | 
					def test_post_items_valid_limit_mocked(client):
 | 
				
			||||||
    """Mock test POSTing a request with a valid limit parameter in the request body."""
 | 
					    """Mock test POSTing a request to /items with a valid limit parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request_body = {
 | 
					    request_body = {
 | 
				
			||||||
        "limit": 1,
 | 
					        "limit": 1,
 | 
				
			||||||
@@ -286,7 +277,7 @@ def test_post_items_valid_limit_mocked(client):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_post_items_invalid_limit(client):
 | 
					def test_post_items_invalid_limit(client):
 | 
				
			||||||
    """Test POSTing a request with an invalid limit parameter in the request body."""
 | 
					    """Test POSTing a request to /items with an invalid limit parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request_body = {
 | 
					    request_body = {
 | 
				
			||||||
        "limit": -1,
 | 
					        "limit": -1,
 | 
				
			||||||
@@ -303,7 +294,7 @@ def test_post_items_invalid_limit(client):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
@pytest.mark.xfail
 | 
					@pytest.mark.xfail
 | 
				
			||||||
def test_post_items_valid_page(client):
 | 
					def test_post_items_valid_page(client):
 | 
				
			||||||
    """Test POSTing a request with a valid page parameter in the request body."""
 | 
					    """Test POSTing a request to /items with a valid page parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request_body = {
 | 
					    request_body = {
 | 
				
			||||||
        "page": 0,
 | 
					        "page": 0,
 | 
				
			||||||
@@ -327,7 +318,7 @@ def test_post_items_valid_page(client):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_post_items_valid_page_mocked(client):
 | 
					def test_post_items_valid_page_mocked(client):
 | 
				
			||||||
    """Mock test POSTing a request with a valid page parameter in the request body."""
 | 
					    """Mock test POSTing a request to /items with a valid page parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request_body = {
 | 
					    request_body = {
 | 
				
			||||||
        "page": 0,
 | 
					        "page": 0,
 | 
				
			||||||
@@ -367,7 +358,7 @@ def test_post_items_valid_page_mocked(client):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_post_items_invalid_page(client):
 | 
					def test_post_items_invalid_page(client):
 | 
				
			||||||
    """Test POSTing a request with an invalid page parameter in the request body."""
 | 
					    """Test POSTing a request to /items with an invalid page parameter in the request body."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request_body = {
 | 
					    request_body = {
 | 
				
			||||||
        "page": -1,
 | 
					        "page": -1,
 | 
				
			||||||
		Reference in New Issue
	
	Block a user