Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/0.7' into 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
hartym committed Nov 9, 2024
2 parents b44bbd0 + b360ce5 commit f022dd2
Show file tree
Hide file tree
Showing 8 changed files with 1,017 additions and 75 deletions.
2 changes: 1 addition & 1 deletion docs/_static/css/harp.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
}

.sidebar-logo-container {
margin: 1em auto 0.2em auto;
margin: 1em auto 0.2em;
width: 80%;
}

Expand Down
1 change: 1 addition & 0 deletions docs/changelogs/unreleased.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Fixed
:::::

* UI: Fixed the topology UI where changing the state of a remote would cause the page to crash (#578, @ArthurD1).
* UI: Correctly display header values containing a semicolon character (#577, @ArthurD1)
11 changes: 9 additions & 2 deletions harp/utils/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,12 @@ def popitem(self):
raise NotImplementedError()


def all_combinations(iterable):
return set(chain(*(combinations(iterable, n + 1) for n in range(len(iterable)))))
def all_combinations(iterable, *, maxlen=None):
return set(
chain(
*(
combinations(iterable, n + 1)
for n in range(min(maxlen, len(iterable)) if maxlen and (maxlen > 0) else len(iterable))
)
)
)
128 changes: 64 additions & 64 deletions harp/utils/testing/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

import pytest

from harp.http.utils import HTTP_METHODS
from harp.typing import Maybe, NotSet


def parametrize_with_http_status_codes(include=None):
statuses = list(map(lambda x: x.value, http.HTTPStatus))
statuses = [status.value for status in http.HTTPStatus]
if include is not None:
statuses = list(filter(lambda x: x // 100 in include, statuses))
statuses = [status for status in statuses if status // 100 in include]
return pytest.mark.parametrize("status_code", statuses)


def _filter_methods_by_attribute(attribute, value):
return {name for name, method in HTTP_METHODS.items() if getattr(method, attribute) is value}


def _update_methods_set(methods, include, attribute, value):
if include is True:
methods |= _filter_methods_by_attribute(attribute, value)
elif include is False:
methods -= _filter_methods_by_attribute(attribute, value)


def parametrize_with_http_methods(
*,
include_safe=NotSet,
Expand All @@ -28,75 +40,63 @@ def parametrize_with_http_methods(
include_not_having_response_body=NotSet,
exclude=(),
):
from harp.http.utils import HTTP_METHODS

if {
include_safe,
include_unsafe,
include_idempotent,
include_non_idempotent,
include_standard,
include_non_standard,
include_having_request_body,
include_maybe_having_request_body,
include_not_having_request_body,
include_having_response_body,
include_maybe_having_response_body,
include_not_having_response_body,
} == {NotSet}:
"""
Parametrize a test with HTTP methods based on various inclusion and exclusion criteria.
Parameters:
- include_safe (bool or NotSet): Include safe methods (e.g., GET, HEAD).
- include_unsafe (bool or NotSet): Include unsafe methods (e.g., POST, PUT).
- include_idempotent (bool or NotSet): Include idempotent methods (e.g., GET, PUT).
- include_non_idempotent (bool or NotSet): Include non-idempotent methods (e.g., POST).
- include_standard (bool or NotSet): Include standard HTTP methods.
- include_non_standard (bool or NotSet): Include non-standard HTTP methods.
- include_having_request_body (bool or NotSet): Include methods that have a request body.
- include_maybe_having_request_body (bool or NotSet): Include methods that may have a request body.
- include_not_having_request_body (bool or NotSet): Include methods that do not have a request body.
- include_having_response_body (bool or NotSet): Include methods that have a response body.
- include_maybe_having_response_body (bool or NotSet): Include methods that may have a response body.
- include_not_having_response_body (bool or NotSet): Include methods that do not have a response body.
- exclude (tuple): Methods to exclude from the parametrization.
Returns:
- pytest.mark.parametrize: A pytest parametrize marker with the selected HTTP methods.
"""
if all(
param is NotSet
for param in [
include_safe,
include_unsafe,
include_idempotent,
include_non_idempotent,
include_standard,
include_non_standard,
include_having_request_body,
include_maybe_having_request_body,
include_not_having_request_body,
include_having_response_body,
include_maybe_having_response_body,
include_not_having_response_body,
]
):
include_standard = True

methods = set()
if include_safe is True:
methods |= {name for name, method in HTTP_METHODS.items() if method.safe is True}
if include_unsafe is True:
methods |= {name for name, method in HTTP_METHODS.items() if method.safe is False}
if include_idempotent is True:
methods |= {name for name, method in HTTP_METHODS.items() if method.idempotent is True}
if include_non_idempotent is True:
methods |= {name for name, method in HTTP_METHODS.items() if method.idempotent is False}
_update_methods_set(methods, include_safe, "safe", True)
_update_methods_set(methods, include_unsafe, "safe", False)
_update_methods_set(methods, include_idempotent, "idempotent", True)
_update_methods_set(methods, include_non_idempotent, "idempotent", False)
if include_standard is True:
methods |= set(HTTP_METHODS.keys())
if include_non_standard is True:
methods |= {"BREW", "REMIX"}
if include_having_request_body is True:
methods |= {name for name, method in HTTP_METHODS.items() if method.request_body is True}
if include_not_having_request_body is True:
methods |= {name for name, method in HTTP_METHODS.items() if method.request_body is False}
if include_maybe_having_request_body is True:
methods |= {name for name, method in HTTP_METHODS.items() if method.request_body is Maybe}
if include_having_response_body is True:
methods |= {name for name, method in HTTP_METHODS.items() if method.response_body is True}
if include_not_having_response_body is True:
methods |= {name for name, method in HTTP_METHODS.items() if method.response_body is False}
if include_maybe_having_response_body is True:
methods |= {name for name, method in HTTP_METHODS.items() if method.response_body is Maybe}

if include_safe is False:
methods -= {name for name, method in HTTP_METHODS.items() if method.safe is True}
if include_unsafe is False:
methods -= {name for name, method in HTTP_METHODS.items() if method.safe is False}
if include_idempotent is False:
methods -= {name for name, method in HTTP_METHODS.items() if method.idempotent is True}
if include_non_idempotent is False:
methods -= {name for name, method in HTTP_METHODS.items() if method.idempotent is False}
if include_standard is False:
methods -= set(HTTP_METHODS.keys())
if include_non_standard is False:
methods -= {"BREW", "REMIX"}
if include_having_request_body is False:
methods -= {name for name, method in HTTP_METHODS.items() if method.request_body is True}
if include_not_having_request_body is False:
methods -= {name for name, method in HTTP_METHODS.items() if method.request_body is False}
if include_maybe_having_request_body is False:
methods -= {name for name, method in HTTP_METHODS.items() if method.request_body is Maybe}
if include_having_response_body is False:
methods -= {name for name, method in HTTP_METHODS.items() if method.response_body is True}
if include_not_having_response_body is False:
methods -= {name for name, method in HTTP_METHODS.items() if method.response_body is False}
if include_maybe_having_response_body is False:
methods -= {name for name, method in HTTP_METHODS.items() if method.response_body is Maybe}
_update_methods_set(methods, include_having_request_body, "request_body", True)
_update_methods_set(methods, include_not_having_request_body, "request_body", False)
_update_methods_set(methods, include_maybe_having_request_body, "request_body", Maybe)
_update_methods_set(methods, include_having_response_body, "response_body", True)
_update_methods_set(methods, include_not_having_response_body, "response_body", False)
_update_methods_set(methods, include_maybe_having_response_body, "response_body", Maybe)

methods -= set(exclude)

return pytest.mark.parametrize("method", list(sorted(methods)))
return pytest.mark.parametrize("method", sorted(methods))
Loading

0 comments on commit f022dd2

Please sign in to comment.