commit fa1c0a4c6a101cdd62e93ee395ce3767fd1d6e53 Author: juga0 juga@riseup.net Date: Sun Mar 22 08:42:49 2020 +0000
fix: tests: Add tests loading results
in ResultDump and incrementing relay's monitoring KeyValues. --- tests/unit/conftest.py | 12 ++----- tests/unit/lib/test_resultdump.py | 66 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 11 deletions(-)
diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index a74d03e..49db357 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -296,14 +296,6 @@ def end_event():
@pytest.fixture(scope='function') -def rd(args, conf, end_event): +def rd(args, conf_results): from sbws.lib.resultdump import ResultDump - # in Travis the next line gives the error: - # TypeError: __init__() takes 3 positional arguments but 4 were given - # No idea why. - # Returning None to disable the test in case ResultDump can not be - # initialized. - try: - return ResultDump(args, conf, end_event) - except TypeError: - return None + return ResultDump(args, conf_results) diff --git a/tests/unit/lib/test_resultdump.py b/tests/unit/lib/test_resultdump.py index c956dea..1180bdb 100644 --- a/tests/unit/lib/test_resultdump.py +++ b/tests/unit/lib/test_resultdump.py @@ -1,6 +1,16 @@ # -*- coding: utf-8 -*- """Unit tests for resultdump.""" -from sbws.lib.resultdump import trim_results_ip_changed + +import datetime + +from sbws.lib.relaylist import Relay +from sbws.lib.resultdump import ( + ResultError, + ResultErrorStream, + ResultSuccess, + trim_results_ip_changed, + load_result_file +)
def test_trim_results_ip_changed_defaults(resultdict_ip_not_changed): @@ -31,3 +41,57 @@ def test_trim_results_ip_changed_on_changed_ipv6(caplog, assert record.levelname == 'WARNING' assert 'Reseting bandwidth results when IPv6 changes, ' \ 'is not yet implemented.\n' in caplog.text + + +def test_resultdump( + rd, args, conf_results, controller, router_status, server_descriptor +): + from sbws import settings + relay = Relay( + router_status.fingerprint, + controller, + ns=router_status, + desc=server_descriptor, + ) + relay.increment_relay_recent_priority_list() + relay.increment_relay_recent_measurement_attempt() + r = ResultSuccess( + [], 2000, relay, ["A", "B"], "http://localhost/bw", "scanner_nick", + ) + # Storing the result with `rd.queue.put` will not store the result to disk + # because the thread is not spawned with pytest. + rd.store_result(r) + results = rd.results_for_relay(relay) + # It has stored the result + assert 1 == len(results) + # The result has the correct attribute + assert 1 == len(results[0].relay_recent_priority_list) + # Store a second result for the sme relay + r = ResultError( + relay, ["A", "B"], "http://localhost/bw", "scanner_nick", + ) + rd.store_result(r) + assert 2 == len(results) + assert 1 == len(results[1].relay_recent_priority_list) + settings.set_end_event() + + +def test_load(datadir): + results = load_result_file(str(datadir.join("results.txt"))) + results = [v for values in results.values() for v in values] + r1 = results[1] + assert isinstance(r1, ResultSuccess) + assert isinstance( + r1.relay_recent_measurement_attempt[0], datetime.datetime + ) + assert 2 == len(r1.relay_recent_measurement_attempt) + assert 3 == len(r1.relay_recent_priority_list) + assert 3 == len(r1.relay_in_recent_consensus) + r2 = results[2] + assert isinstance(r2, ResultErrorStream) + assert isinstance( + r2.relay_recent_measurement_attempt[0], datetime.datetime + ) + assert 2 == len(r2.relay_recent_measurement_attempt) + assert 3 == len(r2.relay_recent_priority_list) + assert 3 == len(r2.relay_in_recent_consensus)