This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add capability of querying and clearing record of requests to p…
…artner API (#415) Co-authored-by: Raphael Pierzina <raphael@hackebrot.de>
- Loading branch information
Showing
5 changed files
with
248 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
test-engineering/contract-tests/partner/app/record_keeper.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from collections import Counter | ||
from multiprocessing.managers import SyncManager | ||
from typing import List, Tuple | ||
|
||
from fastapi import Request | ||
|
||
from models import Header, QueryParameter, Record, RecordCount, Records | ||
|
||
|
||
class RecordKeeper: | ||
"""Responsible for Contile request history management""" | ||
|
||
def __init__(self, multi_process_manager: SyncManager) -> None: | ||
"""Create an instance of RecordKeeper.""" | ||
|
||
self._records: List[Record] = multi_process_manager.list() | ||
|
||
def add(self, request: Request) -> None: | ||
"""Create record from Fast API Request and add record to the record keeper.""" | ||
|
||
headers: Tuple[Header, ...] = tuple( | ||
Header(name=name, value=value) for name, value in request.headers.items() | ||
) | ||
|
||
query_parameters: Tuple[QueryParameter, ...] = tuple( | ||
QueryParameter(name=name, value=value) | ||
for name, value in request.query_params.multi_items() | ||
) | ||
|
||
record: Record = Record( | ||
method=request.method, | ||
headers=headers, | ||
path=request.url.path, | ||
query_parameters=query_parameters, | ||
) | ||
|
||
self._records.append(record) | ||
|
||
def clear(self) -> None: | ||
"""Remove all records from the record keeper.""" | ||
|
||
self._records[:] = [] | ||
|
||
def get_all(self) -> Records: | ||
"""Return all records in the record keeper with a counter.""" | ||
|
||
records: List[RecordCount] = [ | ||
RecordCount(count=count, record=record) | ||
for record, count in Counter(self._records).items() | ||
] | ||
|
||
return Records(records=records) |