# (C) Datadog, Inc. 2022-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os

import mock
import pytest

from datadog_checks.arangodb import ArangodbCheck
from datadog_checks.dev.http import MockResponse
from datadog_checks.dev.utils import get_metadata_metrics

from .common import METRICS


@pytest.mark.integration
def test_invalid_endpoint(aggregator, instance_invalid_endpoint, dd_run_check):
    check = ArangodbCheck('arangodb', {}, [instance_invalid_endpoint])
    with pytest.raises(Exception):
        dd_run_check(check)
    aggregator.assert_service_check('arangodb.openmetrics.health', ArangodbCheck.CRITICAL, count=1)


@pytest.mark.integration
@pytest.mark.parametrize(
    'tag_condition, base_tags',
    [
        pytest.param(
            'valid_id_mode',
            ['endpoint:http://localhost:8529/_admin/metrics/v2', 'server_mode:default', 'server_id:1'],
            id="valid id and valid mode",
        ),
        pytest.param(
            'invalid_mode_valid_id',
            ['endpoint:http://localhost:8529/_admin/metrics/v2', 'server_id:1'],
            id="invalid mode but valid id",
        ),
        pytest.param(
            'valid_mode_invalid_id',
            ['endpoint:http://localhost:8529/_admin/metrics/v2', 'server_mode:default'],
            id="valid mode but invalid id",
        ),
        pytest.param(
            'invalid_mode_invalid_id',
            ['endpoint:http://localhost:8529/_admin/metrics/v2'],
            id="invalid mode and invalid id",
        ),
    ],
)
def test_check(instance, dd_run_check, aggregator, tag_condition, base_tags):
    check = ArangodbCheck('arangodb', {}, [instance])

    def mock_requests_get(url, *args, **kwargs):
        fixture = url.rsplit('/', 1)[-1]
        return MockResponse(file_path=os.path.join(os.path.dirname(__file__), 'fixtures', tag_condition, fixture))

    with mock.patch('requests.get', side_effect=mock_requests_get, autospec=True):
        dd_run_check(check)

    aggregator.assert_service_check(
        'arangodb.openmetrics.health',
        ArangodbCheck.OK,
        count=1,
        tags=['endpoint:http://localhost:8529/_admin/metrics/v2'],
    )
    aggregator.assert_metrics_using_metadata(get_metadata_metrics())
    for metric in METRICS:
        aggregator.assert_metric(metric)
        for tag in base_tags:
            aggregator.assert_metric_has_tag(metric, tag)

    aggregator.assert_all_metrics_covered()

@@ -108,16 +108,31 @@
 ICENSE)%0A
+import logging%0A
 import o
@@ -159,16 +159,47 @@
 t pytest
+%0Afrom requests import HTTPError
 %0A%0Afrom d
@@ -2522,8 +2522,1529 @@
 vered()%0A
+%0A%0A@pytest.mark.parametrize(%0A    'side_effect, log_message',%0A    %5B%0A        pytest.param(%0A            HTTPError, %22Unable to get server foo, skipping %60server_foo%60 tag.%22, id=%22HTTPError getting server tag%22%0A        ),%0A        pytest.param(%0A            Exception,%0A            %22Unable to query %60http://localhost:8529/test_endpoint/foo%60 to collect %60server_foo%60 tag, received error:%22,%0A            id=%22Exception getting server tag%22,%0A        ),%0A    %5D,%0A)%0Adef test_get_server_tag(instance, caplog, side_effect, log_message):%0A    caplog.clear()%0A    check = ArangodbCheck('arangodb', %7B%7D, %5Binstance%5D)%0A    with mock.patch(%22datadog_checks.base.utils.http.RequestsWrapper.get%22, side_effect=side_effect):%0A        caplog.set_level(logging.DEBUG)%0A        check.get_server_tag('foo', '/test_endpoint/foo')%0A%0A    assert log_message in caplog.text%0A%0A%0A@pytest.mark.parametrize(%0A    'server_tags, args',%0A    %5B%0A        pytest.param(%5BNone, None%5D, %5B%5D, id=%22No server tags returned%22),%0A        pytest.param(%0A            %5B'server_mode:foo', 'server_id:bar'%5D, %5B'server_mode:foo', 'server_id:bar'%5D, id=%22Server tags returned%22%0A        ),%0A    %5D,%0A)%0Adef test_refresh_scrapers(instance, server_tags, args):%0A    check = ArangodbCheck('arangodb', %7B%7D, %5Binstance%5D)%0A    with mock.patch(%22datadog_checks.arangodb.check.ArangodbCheck.get_server_tag%22) as mock_get_server_tag:%0A        mock_get_server_tag.side_effect = server_tags%0A        check.set_dynamic_tags = mock.MagicMock()%0A        check.refresh_scrapers()%0A        check.set_dynamic_tags.assert_called_once_with(*args)%0A

add unit tests (#12082)