From 1fd6a618b60d7168fd8f37585d5d39d22d775afd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 28 Mar 2024 07:11:39 +0100 Subject: Adding upstream version 0.13.0. Signed-off-by: Daniel Baumann --- tests/units/tools/test_utils.py | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/units/tools/test_utils.py (limited to 'tests/units/tools/test_utils.py') diff --git a/tests/units/tools/test_utils.py b/tests/units/tools/test_utils.py new file mode 100644 index 0000000..448324f --- /dev/null +++ b/tests/units/tools/test_utils.py @@ -0,0 +1,57 @@ +# Copyright (c) 2023-2024 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the LICENSE file. + +"""Tests for `anta.tools.utils`.""" +from __future__ import annotations + +from contextlib import nullcontext as does_not_raise +from typing import Any + +import pytest + +from anta.tools.utils import get_failed_logs + +EXPECTED_OUTPUTS = [ + {"id": 1, "name": "Alice", "age": 30, "email": "alice@example.com"}, + {"id": 2, "name": "Bob", "age": 35, "email": "bob@example.com"}, + {"id": 3, "name": "Charlie", "age": 40, "email": "charlie@example.com"}, + {"id": 4, "name": "Jon", "age": 25, "email": "Jon@example.com"}, +] + +ACTUAL_OUTPUTS = [ + {"id": 1, "name": "Alice", "age": 30, "email": "alice@example.com"}, + {"id": 2, "name": "Bob", "age": 35, "email": "bob@example.com"}, + {"id": 3, "name": "Charlie", "age": 40, "email": "charlie@example.com"}, + {"id": 4, "name": "Rob", "age": 25, "email": "Jon@example.com"}, +] + + +@pytest.mark.parametrize( + "expected_output, actual_output, expected_result, expected_raise", + [ + pytest.param(EXPECTED_OUTPUTS[0], ACTUAL_OUTPUTS[0], "", does_not_raise(), id="no difference"), + pytest.param( + EXPECTED_OUTPUTS[0], + ACTUAL_OUTPUTS[1], + "\nExpected `1` as the id, but found `2` instead.\nExpected `Alice` as the name, but found `Bob` instead.\n" + "Expected `30` as the age, but found `35` instead.\nExpected `alice@example.com` as the email, but found `bob@example.com` instead.", + does_not_raise(), + id="different data", + ), + pytest.param( + EXPECTED_OUTPUTS[0], + {}, + "\nExpected `1` as the id, but it was not found in the actual output.\nExpected `Alice` as the name, but it was not found in the actual output.\n" + "Expected `30` as the age, but it was not found in the actual output.\nExpected `alice@example.com` as the email, but it was not found in " + "the actual output.", + does_not_raise(), + id="empty actual output", + ), + pytest.param(EXPECTED_OUTPUTS[3], ACTUAL_OUTPUTS[3], "\nExpected `Jon` as the name, but found `Rob` instead.", does_not_raise(), id="different name"), + ], +) +def test_get_failed_logs(expected_output: dict[Any, Any], actual_output: dict[Any, Any], expected_result: str, expected_raise: Any) -> None: + """Test get_failed_logs.""" + with expected_raise: + assert get_failed_logs(expected_output, actual_output) == expected_result -- cgit v1.2.3