From 720aa9a58511da3878e16c5db21ed8e6faf6157f Mon Sep 17 00:00:00 2001 From: Christoph Souris Date: Thu, 1 Aug 2024 16:15:00 +0200 Subject: [PATCH] Improved bulk operation tests. --- tests/model/bulk_operations.json | 2 +- tests/model/test_bulk_operation.py | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/tests/model/bulk_operations.json b/tests/model/bulk_operations.json index f5ba31f..861a966 100644 --- a/tests/model/bulk_operations.json +++ b/tests/model/bulk_operations.json @@ -29,7 +29,7 @@ "creationRamp": 5, "generalStatus": "EXECUTING_WITH_ERROR", "failedParentId": "1", - "id": 1, + "id": 2, "note": "Some description", "operationPrototype": { "c8y_Firmware": { diff --git a/tests/model/test_bulk_operation.py b/tests/model/test_bulk_operation.py index ae2a386..93f0b79 100644 --- a/tests/model/test_bulk_operation.py +++ b/tests/model/test_bulk_operation.py @@ -7,12 +7,15 @@ import json import os from datetime import datetime, timezone +from unittest.mock import Mock + from dateutil import parser from typing import List import pytest -from c8y_api.model import BulkOperation +from c8y_api import CumulocityApi +from c8y_api.model import BulkOperation, BulkOperations def fix_sample_jsons() -> List[dict]: @@ -22,12 +25,21 @@ def fix_sample_jsons() -> List[dict]: root = json.load(f) return root['bulkOperations'] + def test_parsing_collection(): + """Verify that parsing a collection works as expected. + The bulk operations JSON is non-standard as the name differs from the REST endpoint.""" path = os.path.dirname(__file__) + '/bulk_operations.json' with open(path, encoding='utf-8', mode='rt') as f: - root = json.load(f) - - + document = json.load(f) + page1 = document + page2 = {"bulkOperations": []} + + mock_c8y = CumulocityApi("base_url", "tenant_id", "user", "password") + mock_c8y.get = Mock(side_effect=[page1, page2]) + operations = BulkOperations(mock_c8y).get_all() + assert len(operations) == len(document['bulkOperations']) + assert operations[0].id == document['bulkOperations'][0]['id'] @pytest.mark.parametrize('sample_json', fix_sample_jsons())