repo_url,sha,test_name,test_file,category,label,status,pr_link,task_types,test_code,known_fix_diff https://github.com/abeelen/nikamap,d39c488c89bf5d90fffe3c05580fc13551522ee5,nikamap/tests/test_nikamap.py::test_nikamap_write,nikamap/tests/test_nikamap.py,NIO,flaky,Opened,https://github.com/abeelen/nikamap/pull/2,classify;root_cause,"from __future__ import absolute_import, division, print_function import pytest import numpy as np import astropy.units as u from astropy.io import fits from astropy.wcs import WCS from astropy.table import Table from astropy.nddata import StdDevUncertainty from astropy.modeling import models from astropy.stats.funcs import gaussian_fwhm_to_sigma from astropy.convolution import MexicanHat2DKernel from photutils.datasets import make_gaussian_sources_image import numpy.testing as npt import matplotlib.pyplot as plt # from nikamap.nikamapNDDataArray, import NikaMap, jk_nikamap # import nikamap as nm # data_path = op.join(nm.__path__[0], 'data') from ..nikamap import NikaMap, NikaBeam, NikaFits, retrieve_primary_keys from ..utils import pos_gridded # from pytest-django #393 def getfixturevalue(request, value): if hasattr(request, ""getfixturevalue""): return request.getfixturevalue(value) return request.getfuncargvalue(value) def test_nikabeam_exceptions(): # TODO: Should probably be assertions at the __init__ stage... fwhm = 18 * u.arcsec with pytest.raises(AttributeError): beam = NikaBeam() with pytest.raises(AttributeError): beam = NikaBeam(fwhm.value) with pytest.raises(TypeError): beam = NikaBeam(fwhm, fwhm) def test_nikabeam_init(): # TODO: What if we init with an array ? fwhm = 18 * u.arcsec pix_scale = u.equivalencies.pixel_scale(2 * u.arcsec / u.pixel) beam = NikaBeam(fwhm, pix_scale) assert beam.fwhm == fwhm assert beam.fwhm_pix == fwhm.to(u.pixel, equivalencies=pix_scale) assert beam.sigma == fwhm * gaussian_fwhm_to_sigma assert beam.sigma_pix == fwhm.to(u.pixel, equivalencies=pix_scale) * gaussian_fwhm_to_sigma assert beam.area == 2 * np.pi * (fwhm * gaussian_fwhm_to_sigma) ** 2 assert beam.area_pix == 2 * np.pi * (fwhm.to(u.pixel, equivalencies=pix_scale) * gaussian_fwhm_to_sigma) ** 2 beam.normalize(""peak"") npt.assert_allclose(beam.area_pix.value, np.sum(beam.array), rtol=1e-4) assert str(beam) == "" None: self.addr_ipv4 = (str(DEFAULT_IPV4_HOSTNAME), DEFAULT_PORT) self.addr_ipv6 = (str(DEFAULT_IPV6_HOSTNAME), DEFAULT_PORT) self.addr_dual = ('httpbin.org', DEFAULT_HTTP_PORT) @mock.patch('socket.socket') def test_new_socket_connection_ipv4(self, mock_socket: mock.Mock) -> None: conn = new_socket_connection(self.addr_ipv4) mock_socket.assert_called_with(socket.AF_INET, socket.SOCK_STREAM, 0) self.assertEqual(conn, mock_socket.return_value) mock_socket.return_value.connect.assert_called_with(self.addr_ipv4) @mock.patch('socket.socket') def test_new_socket_connection_ipv6(self, mock_socket: mock.Mock) -> None: conn = new_socket_connection(self.addr_ipv6) mock_socket.assert_called_with(socket.AF_INET6, socket.SOCK_STREAM, 0) self.assertEqual(conn, mock_socket.return_value) mock_socket.return_value.connect.assert_called_with( (self.addr_ipv6[0], self.addr_ipv6[1], 0, 0)) @mock.patch('socket.create_connection') def test_new_socket_connection_dual(self, mock_socket: mock.Mock) -> None: conn = new_socket_connection(self.addr_dual) mock_socket.assert_called_with(self.addr_dual, timeout=DEFAULT_TIMEOUT) self.assertEqual(conn, mock_socket.return_value) @mock.patch('proxy.common.utils.new_socket_connection') def test_decorator(self, mock_new_socket_connection: mock.Mock) -> None: @socket_connection(self.addr_ipv4) def dummy(conn: socket.socket) -> None: self.assertEqual(conn, mock_new_socket_connection.return_value) dummy() # type: ignore @mock.patch('proxy.common.utils.new_socket_connection') def test_context_manager( self, mock_new_socket_connection: mock.Mock) -> None: with socket_connection(self.addr_ipv4) as conn: self.assertEqual(conn, mock_new_socket_connection.return_value) ", https://github.com/actris-cloudnet/cloudnetpy,4caaec217c8e100fa2a11f76654917e0bd9560a7,tests/unit/test_meta_for_old_files.py::test_fix_old_data_2,tests/unit/test_meta_for_old_files.py,NIO,flaky,Open,https://github.com/actris-cloudnet/cloudnetpy/pull/34,classify;root_cause,"import numpy as np import numpy.ma as ma from cloudnetpy.plotting import legacy_meta from numpy.testing import assert_array_equal data_orig = ma.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) def test_fix_old_data(): data, name = legacy_meta.fix_legacy_data(data_orig, 'specific_humidity') assert_array_equal(data_orig, data) assert_array_equal(name, 'q') def test_fix_old_data_2(): global data_orig data, name = legacy_meta.fix_legacy_data(data_orig, 'detection_status') assert_array_equal(data_orig, data) assert ma.count(data) == 7 # Reset data_orig data_orig = ma.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) ", https://github.com/AdventielFr/ecs-crd-cli,7b296c64086a204d3540da272e3d8dd74721c6c4,tests/test_prepareDeploymentServiceDefinitionStep.py::test_process_application_autoscaling_scalable_target_max_capacity_invalid,tests/test_prepareDeploymentServiceDefinitionStep.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from unittest.mock import MagicMock import logging from ecs_crd.canaryReleaseInfos import CanaryReleaseInfos from ecs_crd.prepareDeploymentServiceDefinitionStep import PrepareDeploymentServiceDefinitionStep from ecs_crd.canaryReleaseInfos import ScaleInfos logger = logging.Logger('mock') infos = CanaryReleaseInfos(action='test') step = PrepareDeploymentServiceDefinitionStep(infos, logger) def test_process_step_scaling_policy_configuration_adjustment_type_invalid(): with pytest.raises(ValueError): source = {} source['adjustment_type']='a' target = {} step._process_step_scaling_policy_configuration_adjustment_type(source, target) def test_process_step_scaling_policy_configuration_adjustment_type_valid(): source = {} source['adjustment_type']='ChangeInCapacity' target = {} step._process_step_scaling_policy_configuration_adjustment_type(source, target) assert target['AdjustmentType'] == source['adjustment_type'] source['adjustment_type']='PercentChangeInCapacity' target = {} step._process_step_scaling_policy_configuration_adjustment_type(source, target) assert target['AdjustmentType'] == source['adjustment_type'] source['adjustment_type']='ExactCapacity' target = {} step._process_step_scaling_policy_configuration_adjustment_type(source, target) assert target['AdjustmentType'] == source['adjustment_type'] def test_process_step_scaling_policy_configuration_cooldown_invalid(): with pytest.raises(ValueError): source = {} source['cooldown']='a' target = {} step._process_step_scaling_policy_configuration_cooldown(source, target) def test_process_step_scaling_policy_configuration_cooldown_valid(): source = {} source['cooldown']= 60 target = {} step._process_step_scaling_policy_configuration_cooldown(source, target) assert target['Cooldown'] == source['cooldown'] def test_process_step_scaling_policy_configuration_metric_aggregation_type_invalid(): with pytest.raises(ValueError): source = {} source['metric_aggregation_type']='a' target = {} step._process_step_scaling_policy_configuration_metric_aggregation_type(source, target) def test_process_step_scaling_policy_configuration_metric_aggregation_type_valid(): source = {} source['metric_aggregation_type']='Average' target = {} step._process_step_scaling_policy_configuration_metric_aggregation_type(source, target) assert target['MetricAggregationType'] == source['metric_aggregation_type'] source['metric_aggregation_type']='Minimum' target = {} step._process_step_scaling_policy_configuration_metric_aggregation_type(source, target) assert target['MetricAggregationType'] == source['metric_aggregation_type'] source['metric_aggregation_type']='Maximum' target = {} step._process_step_scaling_policy_configuration_metric_aggregation_type(source, target) assert target['MetricAggregationType'] == source['metric_aggregation_type'] def test_process_step_scaling_policy_configuration_step_adjustments_metric_interval_lower_bound_invalid(): with pytest.raises(ValueError): source = {} source['metric_interval_lower_bound']='a' target = {} step._process_step_scaling_policy_configuration_step_adjustments_metric_interval_lower_bound(source, target) def test_process_step_scaling_policy_configuration_step_adjustments_metric_interval_lower_bound_valid(): source = {} source['metric_interval_lower_bound']= 0 target = {} step._process_step_scaling_policy_configuration_step_adjustments_metric_interval_lower_bound(source, target) assert target['MetricIntervalLowerBound'] == source['metric_interval_lower_bound'] def test_process_process_step_scaling_policy_configuration_step_adjustments_metric_interval_upper_bound_invalid(): with pytest.raises(ValueError): source = {} source['metric_interval_upper_bound']='a' target = {} step._process_step_scaling_policy_configuration_step_adjustments_metric_interval_upper_bound(source, target) def test_process_process_step_scaling_policy_configuration_step_adjustments_metric_interval_upper_bound_valid(): source = {} source['metric_interval_upper_bound']= 0 target = {} step._process_step_scaling_policy_configuration_step_adjustments_metric_interval_upper_bound(source, target) assert target['MetricIntervalUpperBound'] == source['metric_interval_upper_bound'] def test_process_process_step_scaling_policy_configuration_step_adjustments_scaling_adjustment_invalid(): with pytest.raises(ValueError): source = {} source['scaling_adjustment']='a' target = {} step._process_step_scaling_policy_configuration_step_adjustments_scaling_adjustment(source, target) def test_process_process_step_scaling_policy_configuration_step_adjustments_scaling_adjustment_valid(): source = {} source['scaling_adjustment']= 0 target = {} step._process_step_scaling_policy_configuration_step_adjustments_scaling_adjustment(source, target) assert target['ScalingAdjustment'] == source['scaling_adjustment'] def test_process_cloudwatch_alarm_metric_name_invalid(): with pytest.raises(ValueError): source = {} source['metric_name']='a' target = {} step._process_cloudwatch_alarm_metric_name(source, target) def test_process_cloudwatch_alarm_metric_name_empty(): with pytest.raises(ValueError): source = {} target = {} step._process_cloudwatch_alarm_metric_name(source, target) def test_process_cloudwatch_alarm_metric_name_valid(): source = {} source['metric_name']='CPUUtilization' target = {} step._process_cloudwatch_alarm_metric_name(source, target) assert target['MetricName'] == source['metric_name'] source['metric_name']='MemoryUtilization' target = {} step._process_cloudwatch_alarm_metric_name(source, target) assert target['MetricName'] == source['metric_name'] def test_process_cloudwatch_alarm_alarm_description_empty(): source = {} target = {} target['MetricName'] = 'CPUUtilization' step._process_cloudwatch_alarm_alarm_description(source, target) assert target['AlarmDescription'] == f'Containers {target[""MetricName""]} High' def test_process_cloudwatch_alarm_alarm_description_not_empty(): source = {} target = {} target['MetricName'] = 'CPUUtilization' source['alarm_description']='test' step._process_cloudwatch_alarm_alarm_description(source, target) assert target['AlarmDescription'] == source['alarm_description'] def test_process_cloudwatch_alarm_namespace(): source = {} expected= {} source['namespace'] = 'test' step._process_cloudwatch_alarm_namespace(source, expected) assert expected['Namespace'] == source['namespace'] def test_process_cloudwatch_alarm_statistic_invalid(): with pytest.raises(ValueError): source = {} source['statistic']='a' target = {} step._process_cloudwatch_alarm_statistic(source, target) def test_process_cloudwatch_alarm_statistic_valid(): source = {} target = {} step._process_cloudwatch_alarm_statistic(source, target) assert target['Statistic'] == 'Average' source['statistic']='Average' step._process_cloudwatch_alarm_statistic(source, target) assert target['Statistic'] == source['statistic'] source['statistic']='Minimum' target = {} step._process_cloudwatch_alarm_statistic(source, target) assert target['Statistic'] == source['statistic'] source['statistic']='Maximum' target = {} step._process_cloudwatch_alarm_statistic(source, target) assert target['Statistic'] == source['statistic'] source['statistic']='SampleCount' target = {} step._process_cloudwatch_alarm_statistic(source, target) assert target['Statistic'] == source['statistic'] source['statistic']='Sum' target = {} step._process_cloudwatch_alarm_statistic(source, target) assert target['Statistic'] == source['statistic'] def test_process_cloudwatch_alarm_period_invalid(): with pytest.raises(ValueError): source = {} source['period']='a' target = {} step._process_cloudwatch_alarm_period(source, target) def test_process_cloudwatch_alarm_period_valid(): source = {} source['period']= 60 target = {} step._process_cloudwatch_alarm_period(source, target) assert target['Period'] == source['period'] def test_process_cloudwatch_alarm_evaluation_periods_invalid(): with pytest.raises(ValueError): source = {} source['evaluation_periods']='a' target = {} step._process_cloudwatch_alarm_evaluation_periods(source, target) def test_process_cloudwatch_alarm_evaluation_periods_valid(): source = {} source['evaluation_periods']= 2 target = {} step._process_cloudwatch_alarm_evaluation_periods(source, target) assert target['EvaluationPeriods'] == source['evaluation_periods'] def test_process_cloudwatch_alarm_evaluation_threshold_invalid(): with pytest.raises(ValueError): source = {} source['threshold']='a' target = {} step._process_cloudwatch_alarm_threshold(source, target) def test_process_cloudwatch_alarm_evaluation_threshold_required(): with pytest.raises(ValueError): source = {} target = {} step._process_cloudwatch_alarm_threshold(source, target) def test_process_cloudwatch_alarm_evaluation_periods_valid(): source = {} source['threshold']= 50 target = {} step._process_cloudwatch_alarm_threshold(source, target) assert target['Threshold'] == source['threshold'] def test_process_cloudwatch_alarm_comparison_operator_invalid(): with pytest.raises(ValueError): source = {} source['comparison_operator']='a' ", https://github.com/agile4you/bottle-neck,ebc670a4b178255473d68e9b4122ba04e38f4810,test/test_handlers.py::test_handler_register_class_pass,test/test_handlers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """"""Unit Tests for `bottle_neck.handlers` module. """""" __author__ = 'Papavassiliou Vassilis' import pytest import bottle_neck.cbv as handler @pytest.fixture(scope=""module"") def mock_handler(): """"""Pytest fixture for `bottle_neck.handlers.BaseHandler` subclass. """""" class MockHandler(handler.BaseHandler): """"""Web handler fixture class. """""" cors_enabled = True base_endpoint = '/mock' def get(self, mock_id): return mock_id @handler.route_method('GET', extra_part=True) @handler.plugin_method('mock_plugin') def version(self): return 0, 0, 1 return MockHandler @pytest.fixture(scope=""module"") def mock_plugin(): """"""Pytest fixture for `bottle_neck.handlers.BasePlugin` subclass. """""" class MockPlugin(handler.BaseHandlerPlugin): def apply(self, *args, **kwargs): try: kwargs['pk'] = int(kwargs['pk']) except ValueError: kwargs['pk'] = -1 return self._wrapped(*args, **kwargs) return MockPlugin def test_handler_route_method_function_pass(): """"""Test `bottle_neck.handlers.route_method` function pass. """""" class TestCls(object): @handler.route_method('POST', extra_part=True) def fn(self): pass assert TestCls.fn.http_method == 'POST' \ and TestCls.fn.url_extra_part is 'fn' def test_handler_route_method_function_fail(): """"""Test `bottle_neck.handlers.route_method` function fail. """""" with pytest.raises(handler.HandlerHTTPMethodError): class TestCls(object): @handler.route_method('PORT', extra_part=True) def fn(self): pass assert TestCls def test_handler_plugin_method_function_pass(): """"""Test `bottle_neck.handlers.route_method` function pass. """""" class TestCls(object): @handler.plugin_method('log', 'auth') def fn(self): pass assert TestCls.fn.log is TestCls.fn.auth is True def test_handler_singleton(mock_handler): """"""Test `bottle_neck.handlers.BaseHandler` subclass for singleton pattern. """""" assert mock_handler() == mock_handler() def test_handler_cls_attrs(mock_handler): """"""Test `bottle_neck.handlers.BaseHandler` subclass for init class attrs. """""" ch_attrs = ('plugins', 'global_plugins', 'base_endpoint', 'cors_enabled') assert set(ch_attrs).issubset(set(dir(mock_handler))) def test_handler_cls_add_plugin_function_based(mock_handler): """"""Test `bottle_neck.handlers.BaseHandler` explicit adding a plugin. """""" def kwargs_plugin(handler): """"""Testing function-based plugin. """""" def _decorator(*args, **kwargs): # pragma: no cover return handler(*args, **kwargs) return _decorator mock_handler.add_plugin( global_scope=False, plugin_callables=[kwargs_plugin] ) assert 'kwargs_plugin' in mock_handler.plugins def test_handler_add_plugin_class_based(mock_handler, mock_plugin): """"""Test `bottle_neck.handlers.BaseHandler.add_plugin` method. """""" mock_handler.add_plugin( global_scope=True, plugin_callables=[mock_plugin] ) assert 'mock_plugin' in mock_handler.global_plugins assert mock_plugin in mock_handler.global_plugins.values() def test_handler_register_class_fail(mock_app): """"""Test `bottle_neck.handlers.BaseHandler.register_app` method error handling. """""" with pytest.raises(handler.HandlerError): handler.BaseHandler.register_app(mock_app) def test_handler_register_class_pass(mock_app, mock_handler): """"""Test `bottle_neck.handlers.BaseHandler.register_app` method. """""" mock_handler.register_app(mock_app) assert all(['/mock' in i.rule for i in mock_app.routes]) ", https://github.com/agile4you/bottle-neck,ebc670a4b178255473d68e9b4122ba04e38f4810,test/test_routing.py::test_router_mount_pass,test/test_routing.py,OD,flaky,Opened,https://github.com/agile4you/bottle-neck/pull/4,classify;root_cause,"# -*- coding: utf-8 -*- """"""Unit Tests for `bottle_neck.routing` module. """""" import pytest import bottle_neck.routing as routing import bottle_neck.cbv as handler @pytest.fixture(scope='module') def mock_router(): """"""pytest fixture for `bottle_neck.routing.Router` class """""" return routing.Router() @pytest.fixture(scope=""module"") def mock_handler(): """"""Pytest fixture for `bottle_neck.handlers.BaseHandler` subclass. """""" class MockHandler(handler.BaseHandler): """"""Web handler fixture class. """""" cors_enabled = True base_endpoint = '/mock' def get(self, mock_id): return mock_id return MockHandler def test_router_register_handler_fn_pass(mock_router): """"""Test `bottle_neck.routing.Router.register_handler` for function-based handler pass. """""" def fn(): pass mock_router.register_handler(fn, entrypoint='/', methods=('GET', )) assert len(mock_router) == 1 def test_router_register_handler_fail(mock_router): """"""Test `bottle_neck.routing.Router.register_handler` error handling. """""" with pytest.raises(routing.RouteError): mock_router.register_handler('handler', entrypoint='/') def test_router_register_handler_cbv_pass(mock_router, mock_handler): """"""Test `bottle_neck.routing.Router.register_handler` for class-based handler pass. """""" mock_router.register_handler(mock_handler, entrypoint='/api') assert len(mock_router) == 2 def test_router_mount_pass(mock_router, mock_app): """"""Test `bottle_neck.routing.Router.mount` method. """""" init_mounts = len(mock_app.routes) mock_router.mount(mock_app) assert len(mock_app.routes) > init_mounts ", https://github.com/agile4you/bottle-neck,ebc670a4b178255473d68e9b4122ba04e38f4810,test/test_routing.py::test_router_register_handler_cbv_pass,test/test_routing.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """"""Unit Tests for `bottle_neck.routing` module. """""" import pytest import bottle_neck.routing as routing import bottle_neck.cbv as handler @pytest.fixture(scope='module') def mock_router(): """"""pytest fixture for `bottle_neck.routing.Router` class """""" return routing.Router() @pytest.fixture(scope=""module"") def mock_handler(): """"""Pytest fixture for `bottle_neck.handlers.BaseHandler` subclass. """""" class MockHandler(handler.BaseHandler): """"""Web handler fixture class. """""" cors_enabled = True base_endpoint = '/mock' def get(self, mock_id): return mock_id return MockHandler def test_router_register_handler_fn_pass(mock_router): """"""Test `bottle_neck.routing.Router.register_handler` for function-based handler pass. """""" def fn(): pass mock_router.register_handler(fn, entrypoint='/', methods=('GET', )) assert len(mock_router) == 1 def test_router_register_handler_fail(mock_router): """"""Test `bottle_neck.routing.Router.register_handler` error handling. """""" with pytest.raises(routing.RouteError): mock_router.register_handler('handler', entrypoint='/') def test_router_register_handler_cbv_pass(mock_router, mock_handler): """"""Test `bottle_neck.routing.Router.register_handler` for class-based handler pass. """""" mock_router.register_handler(mock_handler, entrypoint='/api') assert len(mock_router) == 2 def test_router_mount_pass(mock_router, mock_app): """"""Test `bottle_neck.routing.Router.mount` method. """""" init_mounts = len(mock_app.routes) mock_router.mount(mock_app) assert len(mock_app.routes) > init_mounts ", https://github.com/agile4you/bottle-neck,ebc670a4b178255473d68e9b4122ba04e38f4810,test/test_routing.py::test_router_register_handler_fn_pass,test/test_routing.py,NIO,flaky,Opened,https://github.com/agile4you/bottle-neck/pull/3,classify;root_cause,"# -*- coding: utf-8 -*- """"""Unit Tests for `bottle_neck.routing` module. """""" import pytest import bottle_neck.routing as routing import bottle_neck.cbv as handler @pytest.fixture(scope='module') def mock_router(): """"""pytest fixture for `bottle_neck.routing.Router` class """""" return routing.Router() @pytest.fixture(scope=""module"") def mock_handler(): """"""Pytest fixture for `bottle_neck.handlers.BaseHandler` subclass. """""" class MockHandler(handler.BaseHandler): """"""Web handler fixture class. """""" cors_enabled = True base_endpoint = '/mock' def get(self, mock_id): return mock_id return MockHandler def test_router_register_handler_fn_pass(mock_router): """"""Test `bottle_neck.routing.Router.register_handler` for function-based handler pass. """""" def fn(): pass mock_router.register_handler(fn, entrypoint='/', methods=('GET', )) assert len(mock_router) == 1 def test_router_register_handler_fail(mock_router): """"""Test `bottle_neck.routing.Router.register_handler` error handling. """""" with pytest.raises(routing.RouteError): mock_router.register_handler('handler', entrypoint='/') def test_router_register_handler_cbv_pass(mock_router, mock_handler): """"""Test `bottle_neck.routing.Router.register_handler` for class-based handler pass. """""" mock_router.register_handler(mock_handler, entrypoint='/api') assert len(mock_router) == 2 def test_router_mount_pass(mock_router, mock_app): """"""Test `bottle_neck.routing.Router.mount` method. """""" init_mounts = len(mock_app.routes) mock_router.mount(mock_app) assert len(mock_app.routes) > init_mounts ", https://github.com/AGTGreg/runium,ba89015859976d3426d25a53af5fa4d8827c7483,tests/test_runium.py::TestStartIn::test_processing,tests/test_runium.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import pytest import time from runium.core import Runium @pytest.fixture(scope=""module"") def rnt(): return Runium() @pytest.fixture(scope=""module"") def rnp(): return Runium(mode='multiprocessing') class TestTimes(): def test_threading(self, rnt): rt = rnt.new_task(runnium_param).run(times=3).result() assert rt['iterations'] == 3 def test_processing(self, rnp): rp = rnp.new_task(runnium_param).run(times=3).result() assert rp['iterations'] == 3 class TestLoopDrift(): def test_threading(selft, rnt): prev_time = time.time() rnt.new_task(simple_task).run(every=0.1, times=10).result() time_elapsed = time.time() - prev_time is_ok = (time_elapsed < 1) and (time_elapsed > 0.88) assert is_ok is True def test_processing(selft, rnp): prev_time = time.time() rnp.new_task(simple_task).run(every=0.1, times=10).result() time_elapsed = time.time() - prev_time is_ok = (time_elapsed < 1) and (time_elapsed > 0.88) assert is_ok is True class TestStartIn(): def test_threading(self, rnt): prev_time = time.time() rnt.new_task(simple_task).run(start_in=0.1).result() time_elapsed = time.time() - prev_time is_ok = (time_elapsed < 0.11) and (time_elapsed > 0.09) assert is_ok is True def test_processing(self, rnp): prev_time = time.time() rnp.new_task(simple_task).run(start_in=0.1).result() time_elapsed = time.time() - prev_time is_ok = (time_elapsed < 0.11) and (time_elapsed > 0.09) assert is_ok is True class TestTaskSkipping(): def test_threading(self, rnt): prev_time = time.time() rnt.new_task(sleepy_task).run(every=0.1, times=2).result() time_elapsed = time.time() - prev_time is_ok = (time_elapsed < 0.51) and (time_elapsed > 0.2) assert is_ok is True def test_processing(self, rnp): prev_time = time.time() rnp.new_task(sleepy_task).run(every=0.1, times=2).result() time_elapsed = time.time() - prev_time is_ok = (time_elapsed < 0.51) and (time_elapsed > 0.2) assert is_ok is True class TestKwargs(): def test_threading(self, rnt): r = rnt.new_task( task_with_kwargs, kwargs={'msg': 'Spam, Spam, Spam, egg and Spam'} ).run().result() assert r == 'Spam, Spam, Spam, egg and Spam' def test_processing(self, rnp): r = rnp.new_task( task_with_kwargs, kwargs={'msg': 'Spam, Spam, Spam, egg and Spam'} ).run().result() assert r == 'Spam, Spam, Spam, egg and Spam' class TestReturnException(): def test_threading(self, rnt): r = rnt.new_task(task_with_exception).run().result() assert type(r).__name__ == 'ValueError' def test_processing(self, rnp): r = rnp.new_task(task_with_exception).run().result() assert type(r).__name__ == 'ValueError' class TestKwargsBleeding(): def test_threading(self, rnt): assert rnt.new_task(simple_task).run().result() is True def test_processing(self, rnp): assert rnp.new_task(simple_task).run().result() is True class TestCallbacks(object): def test_on_finished_success_threading(self, rnt): assert rnt.new_task(task_callback_success).on_finished( se_callback, updates_result=True ).run().result() == 'Success' def test_on_finished_success_processing(self, rnp): assert rnp.new_task(task_callback_success).on_finished( se_callback, updates_result=True ).run().result() == 'Success' def test_on_finished_error_threading(self, rnt): assert rnt.new_task(task_callback_error).on_finished( se_callback, updates_result=True ).run().result() == 'Error' def test_on_finished_error_processing(self, rnp): assert rnp.new_task(task_callback_error).on_finished( se_callback, updates_result=True ).run().result() == 'Error' def test_on_success_threading(self, rnt): assert rnt.new_task(task_callback_success).on_success( success_callback, updates_result=True ).run().result() == 'Success' def test_on_success_processing(self, rnp): assert rnp.new_task(task_callback_success).on_success( success_callback, updates_result=True ).run().result() == 'Success' def test_on_error_threading(self, rnt): assert rnt.new_task(task_callback_error).on_error( error_callback, updates_result=True ).run().result() == 'Error' def test_on_error_processing(self, rnp): assert rnp.new_task(task_callback_error).on_error( error_callback, updates_result=True ).run().result() == 'Error' def test_on_iter_success_threading(self, rnt): assert rnt.new_task(task_callback_success).on_iter( se_callback, updates_result=True ).run(times=3).result() == 'Success' def test_on_iter_success_processing(self, rnp): assert rnp.new_task(task_callback_success).on_iter( se_callback, updates_result=True ).run(times=3).result() == 'Success' def test_on_iter_error_threading(self, rnt): assert rnt.new_task(task_callback_error).on_iter( se_callback, updates_result=True ).run(times=3).result() == 'Error' def test_on_iter_error_processing(self, rnp): assert rnp.new_task(task_callback_error).on_iter( se_callback, updates_result=True ).run(times=3).result() == 'Error' class TestTasksList(object): def test_count_threading(self, rnt): r1 = rnt.new_task(simple_task) tasks_prev_count = len(rnt.pending_tasks()) r1.run().result() assert tasks_prev_count == 1 def test_count_finished_threading(self, rnt): rnt.new_task(simple_task).run().result() assert len(rnt.pending_tasks()) == 0 def test_count_processing(self, rnp): r1 = rnp.new_task(simple_task) tasks_prev_count = len(rnp.pending_tasks()) r1.run().result() assert tasks_prev_count == 1 def test_count_finished_processing(self, rnp): rnp.new_task(simple_task).run().result() assert len(rnp.pending_tasks()) == 0 # DUMMY TASKS ================================================================= def simple_task(): return True def task_with_kwargs(msg=None, **kwargs): return msg def runnium_param(runium): return runium def task_with_exception(runium): raise ValueError('ni') return runium def sleepy_task(): time.sleep(0.2) return True def task_callback_success(): return 'Callback not fired.' def task_callback_error(): raise Exception('Callback not fired.') def success_callback(success): if success: return 'Success' return None def error_callback(error): if error: return 'Error' return None def se_callback(success, error): if success: return 'Success' elif error: return 'Error' return None ", https://github.com/AGTGreg/runium,ba89015859976d3426d25a53af5fa4d8827c7483,tests/test_runium.py::TestTaskSkipping::test_processing,tests/test_runium.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import pytest import time from runium.core import Runium @pytest.fixture(scope=""module"") def rnt(): return Runium() @pytest.fixture(scope=""module"") def rnp(): return Runium(mode='multiprocessing') class TestTimes(): def test_threading(self, rnt): rt = rnt.new_task(runnium_param).run(times=3).result() assert rt['iterations'] == 3 def test_processing(self, rnp): rp = rnp.new_task(runnium_param).run(times=3).result() assert rp['iterations'] == 3 class TestLoopDrift(): def test_threading(selft, rnt): prev_time = time.time() rnt.new_task(simple_task).run(every=0.1, times=10).result() time_elapsed = time.time() - prev_time is_ok = (time_elapsed < 1) and (time_elapsed > 0.88) assert is_ok is True def test_processing(selft, rnp): prev_time = time.time() rnp.new_task(simple_task).run(every=0.1, times=10).result() time_elapsed = time.time() - prev_time is_ok = (time_elapsed < 1) and (time_elapsed > 0.88) assert is_ok is True class TestStartIn(): def test_threading(self, rnt): prev_time = time.time() rnt.new_task(simple_task).run(start_in=0.1).result() time_elapsed = time.time() - prev_time is_ok = (time_elapsed < 0.11) and (time_elapsed > 0.09) assert is_ok is True def test_processing(self, rnp): prev_time = time.time() rnp.new_task(simple_task).run(start_in=0.1).result() time_elapsed = time.time() - prev_time is_ok = (time_elapsed < 0.11) and (time_elapsed > 0.09) assert is_ok is True class TestTaskSkipping(): def test_threading(self, rnt): prev_time = time.time() rnt.new_task(sleepy_task).run(every=0.1, times=2).result() time_elapsed = time.time() - prev_time is_ok = (time_elapsed < 0.51) and (time_elapsed > 0.2) assert is_ok is True def test_processing(self, rnp): prev_time = time.time() rnp.new_task(sleepy_task).run(every=0.1, times=2).result() time_elapsed = time.time() - prev_time is_ok = (time_elapsed < 0.51) and (time_elapsed > 0.2) assert is_ok is True class TestKwargs(): def test_threading(self, rnt): r = rnt.new_task( task_with_kwargs, kwargs={'msg': 'Spam, Spam, Spam, egg and Spam'} ).run().result() assert r == 'Spam, Spam, Spam, egg and Spam' def test_processing(self, rnp): r = rnp.new_task( task_with_kwargs, kwargs={'msg': 'Spam, Spam, Spam, egg and Spam'} ).run().result() assert r == 'Spam, Spam, Spam, egg and Spam' class TestReturnException(): def test_threading(self, rnt): r = rnt.new_task(task_with_exception).run().result() assert type(r).__name__ == 'ValueError' def test_processing(self, rnp): r = rnp.new_task(task_with_exception).run().result() assert type(r).__name__ == 'ValueError' class TestKwargsBleeding(): def test_threading(self, rnt): assert rnt.new_task(simple_task).run().result() is True def test_processing(self, rnp): assert rnp.new_task(simple_task).run().result() is True class TestCallbacks(object): def test_on_finished_success_threading(self, rnt): assert rnt.new_task(task_callback_success).on_finished( se_callback, updates_result=True ).run().result() == 'Success' def test_on_finished_success_processing(self, rnp): assert rnp.new_task(task_callback_success).on_finished( se_callback, updates_result=True ).run().result() == 'Success' def test_on_finished_error_threading(self, rnt): assert rnt.new_task(task_callback_error).on_finished( se_callback, updates_result=True ).run().result() == 'Error' def test_on_finished_error_processing(self, rnp): assert rnp.new_task(task_callback_error).on_finished( se_callback, updates_result=True ).run().result() == 'Error' def test_on_success_threading(self, rnt): assert rnt.new_task(task_callback_success).on_success( success_callback, updates_result=True ).run().result() == 'Success' def test_on_success_processing(self, rnp): assert rnp.new_task(task_callback_success).on_success( success_callback, updates_result=True ).run().result() == 'Success' def test_on_error_threading(self, rnt): assert rnt.new_task(task_callback_error).on_error( error_callback, updates_result=True ).run().result() == 'Error' def test_on_error_processing(self, rnp): assert rnp.new_task(task_callback_error).on_error( error_callback, updates_result=True ).run().result() == 'Error' def test_on_iter_success_threading(self, rnt): assert rnt.new_task(task_callback_success).on_iter( se_callback, updates_result=True ).run(times=3).result() == 'Success' def test_on_iter_success_processing(self, rnp): assert rnp.new_task(task_callback_success).on_iter( se_callback, updates_result=True ).run(times=3).result() == 'Success' def test_on_iter_error_threading(self, rnt): assert rnt.new_task(task_callback_error).on_iter( se_callback, updates_result=True ).run(times=3).result() == 'Error' def test_on_iter_error_processing(self, rnp): assert rnp.new_task(task_callback_error).on_iter( se_callback, updates_result=True ).run(times=3).result() == 'Error' class TestTasksList(object): def test_count_threading(self, rnt): r1 = rnt.new_task(simple_task) tasks_prev_count = len(rnt.pending_tasks()) r1.run().result() assert tasks_prev_count == 1 def test_count_finished_threading(self, rnt): rnt.new_task(simple_task).run().result() assert len(rnt.pending_tasks()) == 0 def test_count_processing(self, rnp): r1 = rnp.new_task(simple_task) tasks_prev_count = len(rnp.pending_tasks()) r1.run().result() assert tasks_prev_count == 1 def test_count_finished_processing(self, rnp): rnp.new_task(simple_task).run().result() assert len(rnp.pending_tasks()) == 0 # DUMMY TASKS ================================================================= def simple_task(): return True def task_with_kwargs(msg=None, **kwargs): return msg def runnium_param(runium): return runium def task_with_exception(runium): raise ValueError('ni') return runium def sleepy_task(): time.sleep(0.2) return True def task_callback_success(): return 'Callback not fired.' def task_callback_error(): raise Exception('Callback not fired.') def success_callback(success): if success: return 'Success' return None def error_callback(error): if error: return 'Error' return None def se_callback(success, error): if success: return 'Success' elif error: return 'Error' return None ", https://github.com/AguaClara/aguaclara,9ee3d1d007bc984b73b19520d48954b6d81feecc,tests/core/test_cache.py::test_ac_cache,tests/core/test_cache.py,NIO,flaky,nan,nan,classify;root_cause,"from aguaclara.core.cache import ac_cache, HashableObject class ComputedObject(HashableObject): def __init__(self): self.a = 2 self.b = 3 self.c = 4 @property @ac_cache def product(self): increment_n_calls() return self.a * self.b * self.c @property @ac_cache def sum(self): increment_n_calls() return self.a + self.b + self.c @ac_cache def sum_with_arg(self, my_arg): increment_n_calls() return self.sum + my_arg @ac_cache def sum_with_kwarg(self, my_arg=10): increment_n_calls() return self.sum + my_arg # Keep track of the total number of calls side_effect_n_calls = 0 def increment_n_calls(): global side_effect_n_calls side_effect_n_calls = side_effect_n_calls +1 def test_ac_cache(): my_computed_object = ComputedObject() assert 24 == my_computed_object.product assert 1 == side_effect_n_calls assert 9 == my_computed_object.sum assert 2 == side_effect_n_calls assert 9 == my_computed_object.sum assert 2 == side_effect_n_calls my_computed_object.a=3 assert 36 == my_computed_object.product assert 3 == side_effect_n_calls assert 10 == my_computed_object.sum assert 4 == side_effect_n_calls assert 15 == my_computed_object.sum_with_arg(5) assert 5 == side_effect_n_calls assert 20 == my_computed_object.sum_with_kwarg() assert 6 == side_effect_n_calls assert 20 == my_computed_object.sum_with_kwarg() assert 6 == side_effect_n_calls assert 25 == my_computed_object.sum_with_kwarg(my_arg=15) assert 7 == side_effect_n_calls ", https://github.com/airbrake/pybrake,1f991a6a9812d57cf84f24b348a0044b419733bf,pybrake/test_celery_integration.py::test_celery_integration,pybrake/test_celery_integration.py,NIO,flaky,Open,https://github.com/airbrake/pybrake/pull/165,classify;root_cause,"import json import threading import time from http.server import BaseHTTPRequestHandler, HTTPServer from .test_celery import raise_error notice = None class Handler(BaseHTTPRequestHandler): def do_POST(self): content_length = int(self.headers[""Content-Length""]) post_data = self.rfile.read(content_length).decode(""utf-8"") global notice # pylint: disable=global-statement notice = json.loads(post_data) self.send_response(200) self.wfile.write('{""id"":""1""}'.encode(""utf-8"")) def test_celery_integration(): server_address = ("""", 8080) server = HTTPServer(server_address, Handler) httpd_thread = threading.Thread(target=server.serve_forever) httpd_thread.setDaemon(True) httpd_thread.start() raise_error.apply() for _ in range(10): if notice is None: time.sleep(1) else: break errors = notice[""errors""] assert len(errors) == 1 error = errors[0] assert error[""type""] == ""ValueError"" assert error[""message""] == ""Test"" backtrace = error[""backtrace""] assert len(backtrace) == 2 frame = backtrace[0] assert frame[""file""] == ""/PROJECT_ROOT/pybrake/test_celery.py"" assert frame[""function""] == ""raise_error"" assert frame[""line""] == 16 server.socket.close() server.shutdown() ", https://github.com/airbrake/pybrake,9bf82941d8bf521055b258cea91596a11e4eb81f,pybrake/test_celery_integration.py::test_celery_integration,pybrake/test_celery_integration.py,NIO,flaky,Accepted,https://github.com/airbrake/pybrake/pull/163,classify;root_cause;fix_proposal,"import json import threading import time from http.server import BaseHTTPRequestHandler, HTTPServer from .test_celery import raise_error notice = None class Handler(BaseHTTPRequestHandler): def do_POST(self): content_length = int(self.headers[""Content-Length""]) post_data = self.rfile.read(content_length).decode(""utf-8"") global notice # pylint: disable=global-statement notice = json.loads(post_data) self.send_response(200) self.wfile.write('{""id"":""1""}'.encode(""utf-8"")) def test_celery_integration(): server_address = ("""", 8080) server = HTTPServer(server_address, Handler) httpd_thread = threading.Thread(target=server.serve_forever) httpd_thread.setDaemon(True) httpd_thread.start() raise_error.apply() for _ in range(10): if notice is None: time.sleep(1) else: break errors = notice[""errors""] assert len(errors) == 1 error = errors[0] assert error[""type""] == ""ValueError"" assert error[""message""] == ""Test"" backtrace = error[""backtrace""] assert len(backtrace) == 2 frame = backtrace[0] assert frame[""file""] == ""/PROJECT_ROOT/pybrake/test_celery.py"" assert frame[""function""] == ""raise_error"" assert frame[""line""] == 16 ", https://github.com/aitgon/wopmars,33680f94163d82ce43557cf7f87a5f9ead496fdc,wopmars/tests/test_wopmars.py::TestWopmars::test_dry_run,wopmars/tests/test_wopmars.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pathlib import shutil from unittest import TestCase import os import subprocess import unittest from wopmars.utils.OptionManager import OptionManager from wopmars import WopMars from wopmars.Base import Base from wopmars.SQLManager import SQLManager from wopmars.models.Execution import Execution from wopmars.models.TableModificationTime import TableModificationTime from wopmars.utils.PathManager import PathManager from wopmars.utils.various import get_current_time class TestWopmars(TestCase): def setUp(self): self.test_path = PathManager.get_test_path() # Get tests path OptionManager.initial_test_setup() # Set tests arguments self.__db_url = OptionManager.instance()[""--database""] self.__example_def_file1 = os.path.join(self.test_path, ""resource/wopfile/example_def_file1.yml"") self.__example_def_file1_only_database = \ os.path.join(self.test_path, ""resource/wopfile/example_def_file1_only_database.yml"") self.__example_def_file2_only_files = os.path.join(self.test_path, ""resource/wopfile/example_def_file2_only_files.yml"") self.__example_def_file4 = os.path.join(self.test_path, ""resource/wopfile/example_def_file4.yml"") self.__example_def_file5_never_ready = os.path.join(self.test_path, ""resource/wopfile/example_def_file5_never_ready.yml"") self.__example_def_file_input_not_ready = os.path.join(self.test_path, ""resource/wopfile/example_def_file_input_not_ready.yml"") def tearDown(self): SQLManager.instance().get_session().close() SQLManager.instance().drop_all() shutil.rmtree(os.path.join(self.test_path, ""outdir""), ignore_errors=True) OptionManager._drop() SQLManager._drop() def test_run(self): cmd_line = [""python"", ""-D"", self.__db_url, ""-w"", self.__example_def_file1, ""-v"", ""-d"", self.test_path] with self.assertRaises(SystemExit) as se: WopMars().run(cmd_line) self.assertTrue(os.path.exists(os.path.join(self.test_path, 'outdir/output_file1.txt'))) self.assertTrue(os.path.exists(os.path.join(self.test_path, 'outdir/output_file2.txt'))) self.assertTrue(os.path.exists(os.path.join(self.test_path, 'outdir/output_file7.txt'))) # self.assertEqual(se.exception.code, 0) def test_run_touch(self): """"""This is a tests for the touch function"""""" ################################################################################################################ # # Normal run until rule 2 # ################################################################################################################ cmd_line = [""python"", ""-D"", self.__db_url, ""-w"", self.__example_def_file1, ""-v"", ""-d"", self.test_path, ""-U"", ""rule2""] with self.assertRaises(SystemExit) as se: WopMars().run(cmd_line) # self.assertTrue(os.path.exists(os.path.join(self.test_path, 'outdir/output_file1.txt'))) # self.assertEqual(se.exception.code, 0) output_file1_mtime = os.path.getmtime(os.path.join(self.test_path, 'outdir/output_file1.txt')) foobase_mtime = SQLManager.instance().get_session().query(TableModificationTime)\ .filter_by(table_name='FooBase').first().mtime_epoch_millis ################################################################################################################ # # Dry-run # ################################################################################################################ cmd_line = [""python"", ""-D"", self.__db_url, ""-w"", self.__example_def_file1, ""-v"", ""-d"", self.test_path, ""-U"", ""rule2 --dry-run""] with self.assertRaises(SystemExit) as se: WopMars().run(cmd_line) # self.assertTrue(os.path.exists(os.path.join(self.test_path, 'outdir/output_file1.txt'))) # self.assertEqual(se.exception.code, 0) output_file1_mtime_after_dry = os.path.getmtime(os.path.join(self.test_path, 'outdir/output_file1.txt')) foobase_mtime_after_dry = SQLManager.instance().get_session().query(TableModificationTime)\ .filter_by(table_name='FooBase').first().mtime_epoch_millis ################################################################################################################ # # Touch output files # ################################################################################################################ cmd_line = [""python"", ""-D"", self.__db_url, ""-w"", self.__example_def_file1, ""-v"", ""-d"", self.test_path, ""-U"", ""rule2"", ""--touch""] with self.assertRaises(SystemExit) as se: WopMars().run(cmd_line) output_file1_mtime_after_touch = os.path.getmtime(os.path.join(self.test_path, 'outdir/output_file1.txt')) foobase_mtime_after_touch = SQLManager.instance().get_session().query(TableModificationTime)\ .filter_by(table_name='FooBase').first().mtime_epoch_millis ################################################################################################################ # # Assert that modification times have been updated # ################################################################################################################ self.assertEqual(output_file1_mtime, output_file1_mtime_after_dry) self.assertEqual(foobase_mtime, foobase_mtime_after_dry) self.assertLess(output_file1_mtime, output_file1_mtime_after_touch) self.assertLess(foobase_mtime, foobase_mtime_after_touch) def test_db_forceall(self): """"""Tests the forceall option in the database. Will carry out different runs in either default or forceall mode that will either increase or keep same the number of rows of the FooBase table"""""" pathlib.Path(os.path.join(self.test_path, ""outdir/output_file1.txt"")).touch() ################################################################################################################ # # Run 1: normal mode. Results in 1000 rows # ################################################################################################################ cmd_line = [""python"", ""-D"", self.__db_url, ""-w"", self.__example_def_file1_only_database, ""-v"", ""-d"", self.test_path] with self.assertRaises(SystemExit) as se: WopMars().run(cmd_line) foobase_model = Base.metadata.tables['FooBase'] row_count = SQLManager.instance().get_session().query(foobase_model).count() self.assertTrue(row_count == 1000) self.assertEqual(se.exception.code, 0) ################################################################################################################ # # Run 2: normal mode. Still 1000 rows, because it does not execute # ################################################################################################################ cmd_line = [""python"", ""-D"", self.__db_url, ""-w"", self.__example_def_file1_only_database, ""-v"", ""-d"", self.test_path] with self.assertRaises(SystemExit) as se: WopMars().run(cmd_line) row_count = SQLManager.instance().get_session().query(foobase_model).count() self.assertTrue(row_count == 1000) self.assertEqual(se.exception.code, 0) ################################################################################################################ # # Run 3: forceall mode. Now 2000 rows, because it is forced to execute # ################################################################################################################ cmd_line = [""python"", ""-D"", self.__db_url, ""-w"", self.__example_def_file1_only_database, ""-v"", ""-d"", self.test_path, ""--forceall""] with self.assertRaises(SystemExit) as se: WopMars().run(cmd_line) row_count = SQLManager.instance().get_session().query(foobase_model).count() self.assertTrue(row_count == 2000) self.assertEqual(se.exception.code, 0) ################################################################################################################ # # Run 4: normal mode. Again 2000 rows, because it does not execute # ################################################################################################################ cmd_line = [""python"", ""-D"", self.__db_url, ""-w"", self.__example_def_file1_only_database, ""-v"", ""-d"", self.test_path] with self.assertRaises(SystemExit) as se: WopMars().run(cmd_line) row_count = SQLManager.instance().get_session().query(foobase_model).count() self.assertTrue(row_count == 2000) self.assertEqual(se.exception.code, 0) def test_forceall_dryrun(self): """"""This tests carries out a normal a forceall and a dryrun and stores the modification times. Then it will tests that the modification time of the two first outputs are different and the second and third equal"""""" cmd_line = [""python"", ""-D"", self.__db_url, ""-w"", self.__example_def_file1, ""-v"", ""-d"", self.test_path] with self.assertRaises(SystemExit) as se: WopMars().run(cmd_line) # Test that modification date of first run is different from second forced run mtime1 = os.path.getmtime(os.path.join(self.test_path, 'outdir/output_file7.txt')) # Run forceall cmd_line_force = [""python"", ""-D"", self.__db_url, ""-w"", self.__example_def_file1, ""-v"", ""-d"", self.test_path, ""--forceall""] with self.assertRaises(SystemExit) as se: WopMars().run(cmd_line_force) mtime2 = os.path", https://github.com/Alexei-Kornienko/schematics_to_swagger,d9c1c9e43a25c5a4ff60701b3bf22fc039c815c2,tests/test_model.py::test_compound_type,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from schematics import types import schematics_to_swagger from tests import models WEATHER_REPORT_DEFINITION = { 'title': 'WeatherReport', 'type': 'object', 'description': 'Some sample class for Weather report', 'properties': { 'city': { 'type': 'string', 'maxLength': 50, 'readOnly': True }, 'temperature': { 'type': 'number', 'format': 'double' }, 'author': { 'type': 'string', 'format': 'email', }, 'some_url': { 'type': 'string', 'format': 'uri', }, 'taken_at': { 'type': 'string', 'format': 'date-time' } }, 'required': ['temperature'] } WEATHER_STATS_DEF = { 'title': 'WeatherStats', 'type': 'object', 'description': None, 'properties': { 'last_report': {'$ref': '#/definitions/WeatherReport'}, 'prev_reports': { 'type': 'array', 'items': { '$ref': '#/definitions/WeatherReport' } }, 'date_list': { 'type': 'array', 'items': { 'type': 'string', 'format': 'date-time' } } }, } WEATHER_STATS_DEF_V3 = { 'title': 'WeatherStats', 'type': 'object', 'description': None, 'properties': { 'last_report': { '$ref': '#/components/schemas/WeatherReport' }, 'prev_reports': { 'type': 'array', 'items': { '$ref': '#/components/schemas/WeatherReport' }, }, 'date_list': { 'type': 'array', 'items': { 'type': 'string', 'format': 'date-time' } } }, } WEATHER_PRIVATE_DATA = { 'title': 'WeatherPrivateData', 'type': 'object', 'description': 'Some sample model with private field', 'properties': { 'city': { 'type': 'string', 'maxLength': 50, 'readOnly': True }, 'temperature': { 'type': 'number', 'format': 'double', 'minimum': -50, 'maximum': 50 } }, 'required': ['temperature'] } def test_model_to_definition(): expected = WEATHER_REPORT_DEFINITION definition = schematics_to_swagger.model_to_definition(models.WeatherReport) assert expected == definition def test_read_models_from_module(): expected = { 'WeatherReport': WEATHER_REPORT_DEFINITION, 'WeatherStats': WEATHER_STATS_DEF, 'WeatherPrivateData': WEATHER_PRIVATE_DATA } data = schematics_to_swagger.read_models_from_module(models) assert expected == data def test_compound_type(): expected = WEATHER_STATS_DEF data = schematics_to_swagger.model_to_definition(models.WeatherStats) assert expected == data def test_private_fields(): expected = WEATHER_PRIVATE_DATA definition = schematics_to_swagger.model_to_definition(models.WeatherPrivateData) assert expected == definition def test_read_models_from_module_v3(): expected = { 'WeatherReport': WEATHER_REPORT_DEFINITION, 'WeatherStats': WEATHER_STATS_DEF_V3, 'WeatherPrivateData': WEATHER_PRIVATE_DATA } data = schematics_to_swagger.read_models_from_module(models, version=3) assert expected == data def test_model_type_with_metadata_v3(): expected = {'allOf': [{'$ref': '#/components/schemas/WeatherReport'}], 'readOnly': True} model = type( 'NewWeatherStats', (models.WeatherStats,), {'last_report': types.ModelType(models.WeatherReport, metadata={'readOnly': True})}) schematics_to_swagger.version_dependencies(3) definition = schematics_to_swagger.model_to_definition(model) assert expected == definition['properties']['last_report'] def test_model_type_required_with_metadata_v3(): expected = {'$ref': '#/components/schemas/WeatherReport', 'readOnly': True} model = type( 'NewWeatherStats', (models.WeatherStats,), {'last_report': types.ModelType(models.WeatherReport, required=True, metadata={'readOnly': True})}) schematics_to_swagger.version_dependencies(3) definition = schematics_to_swagger.model_to_definition(model) assert expected == definition['properties']['last_report'] assert ['last_report'] == definition['required'] ", https://github.com/Alexei-Kornienko/schematics_to_swagger,d9c1c9e43a25c5a4ff60701b3bf22fc039c815c2,tests/test_model.py::test_read_models_from_module,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from schematics import types import schematics_to_swagger from tests import models WEATHER_REPORT_DEFINITION = { 'title': 'WeatherReport', 'type': 'object', 'description': 'Some sample class for Weather report', 'properties': { 'city': { 'type': 'string', 'maxLength': 50, 'readOnly': True }, 'temperature': { 'type': 'number', 'format': 'double' }, 'author': { 'type': 'string', 'format': 'email', }, 'some_url': { 'type': 'string', 'format': 'uri', }, 'taken_at': { 'type': 'string', 'format': 'date-time' } }, 'required': ['temperature'] } WEATHER_STATS_DEF = { 'title': 'WeatherStats', 'type': 'object', 'description': None, 'properties': { 'last_report': {'$ref': '#/definitions/WeatherReport'}, 'prev_reports': { 'type': 'array', 'items': { '$ref': '#/definitions/WeatherReport' } }, 'date_list': { 'type': 'array', 'items': { 'type': 'string', 'format': 'date-time' } } }, } WEATHER_STATS_DEF_V3 = { 'title': 'WeatherStats', 'type': 'object', 'description': None, 'properties': { 'last_report': { '$ref': '#/components/schemas/WeatherReport' }, 'prev_reports': { 'type': 'array', 'items': { '$ref': '#/components/schemas/WeatherReport' }, }, 'date_list': { 'type': 'array', 'items': { 'type': 'string', 'format': 'date-time' } } }, } WEATHER_PRIVATE_DATA = { 'title': 'WeatherPrivateData', 'type': 'object', 'description': 'Some sample model with private field', 'properties': { 'city': { 'type': 'string', 'maxLength': 50, 'readOnly': True }, 'temperature': { 'type': 'number', 'format': 'double', 'minimum': -50, 'maximum': 50 } }, 'required': ['temperature'] } def test_model_to_definition(): expected = WEATHER_REPORT_DEFINITION definition = schematics_to_swagger.model_to_definition(models.WeatherReport) assert expected == definition def test_read_models_from_module(): expected = { 'WeatherReport': WEATHER_REPORT_DEFINITION, 'WeatherStats': WEATHER_STATS_DEF, 'WeatherPrivateData': WEATHER_PRIVATE_DATA } data = schematics_to_swagger.read_models_from_module(models) assert expected == data def test_compound_type(): expected = WEATHER_STATS_DEF data = schematics_to_swagger.model_to_definition(models.WeatherStats) assert expected == data def test_private_fields(): expected = WEATHER_PRIVATE_DATA definition = schematics_to_swagger.model_to_definition(models.WeatherPrivateData) assert expected == definition def test_read_models_from_module_v3(): expected = { 'WeatherReport': WEATHER_REPORT_DEFINITION, 'WeatherStats': WEATHER_STATS_DEF_V3, 'WeatherPrivateData': WEATHER_PRIVATE_DATA } data = schematics_to_swagger.read_models_from_module(models, version=3) assert expected == data def test_model_type_with_metadata_v3(): expected = {'allOf': [{'$ref': '#/components/schemas/WeatherReport'}], 'readOnly': True} model = type( 'NewWeatherStats', (models.WeatherStats,), {'last_report': types.ModelType(models.WeatherReport, metadata={'readOnly': True})}) schematics_to_swagger.version_dependencies(3) definition = schematics_to_swagger.model_to_definition(model) assert expected == definition['properties']['last_report'] def test_model_type_required_with_metadata_v3(): expected = {'$ref': '#/components/schemas/WeatherReport', 'readOnly': True} model = type( 'NewWeatherStats', (models.WeatherStats,), {'last_report': types.ModelType(models.WeatherReport, required=True, metadata={'readOnly': True})}) schematics_to_swagger.version_dependencies(3) definition = schematics_to_swagger.model_to_definition(model) assert expected == definition['properties']['last_report'] assert ['last_report'] == definition['required'] ", https://github.com/alexrudy/supertunnel,4c64e2fedda3382b809a2c3d01c711ec4961f933,supertunnel/ssh/continuous_test.py::test_backoff,supertunnel/ssh/continuous_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import collections import logging import time import itertools import io import subprocess import selectors from typing import Any from typing import Deque, Type, List, Tuple import pytest from .config import SSHConfiguration from .continuous import Action from .continuous import ContinuousSSH @pytest.fixture def config(): cfg = SSHConfiguration() return cfg @pytest.fixture def popen(monkeypatch): monkeypatch.setattr(subprocess, ""Popen"", MockPopen) return MockPopen @pytest.fixture def proc(popen, messenger, config, stream): proc = ContinuousSSH(config, stream) proc._messenger = messenger return proc def test_continuous_repr(proc): assert repr(proc).startswith(""ContinuousSSH"") @pytest.mark.parametrize( ""logline,expected_action"", [ (""Entering interactive session"", Action.CONNECTED), (""debug1: Reading configuration"", Action.CONTINUE), (""Host example.com not responding"", Action.DISCONNECTED), ], ) def test_ssh_log_line(proc: ContinuousSSH, logline: str, expected_action: Action, caplog: Any) -> None: logger = logging.getLogger(__name__).getChild(""ssh"") assert proc._handle_ssh_line(logline, logger) == expected_action for record in caplog.records: assert ""debug1"" not in record.message def test_backoff(monkeypatch: Any, proc: ContinuousSSH, caplog: Any) -> None: sleep_times = [] def mock_sleep(duration): sleep_times.append(duration) times: Deque[float] = collections.deque([]) def mock_monotonic(): now = times.popleft() print(f""Time: {now}"") return now monkeypatch.setattr(time, ""sleep"", mock_sleep) monkeypatch.setattr(time, ""monotonic"", mock_monotonic) # Test backoff when process appeared to take 0 time. times.extend([0.1, 0.1]) with proc._backoff(): pass last_wait = sleep_times.pop() assert last_wait == pytest.approx(0.1) assert proc._backoff_time == pytest.approx(0.2) assert not times times.extend([0.0, 0.2]) with proc._backoff(): pass assert not sleep_times assert proc._backoff_time == pytest.approx(0.1) class MockPopenException(Exception): pass class MockPopen: _pid = 0 _success = False _raise = False _stdout = b"""" _stderr = b"""" def __init__(self, cmd, **settings): self._cmd = cmd self._settings = settings self.returncode = None self.pid = self._pid = self._pid + 1 self.stdout = io.BytesIO(self._stdout) self.stderr = io.BytesIO(self._stderr) self._polls = 5 def _finish(self): if self._raise: self._raise = False raise MockPopenException(""Hmm.."") if self._success: self.returncode = 0 else: self.returncode = 1 return self.returncode def poll(self): self._polls -= 1 if not self._polls: self._finish() return self.returncode def wait(self): return self._finish() def kill(self): return self._finish() def terminate(self): return self._finish() class MockSelector: events: List[int] = [] def __init__(self): self.keys = [] def close(self): pass def register(self, fileobj, event): self.keys.append(selectors.SelectorKey(fileobj=fileobj, fd=0, events=[event], data=None)) def select(self, *, timeout=None): if not self.events: return [] event = self.events.pop() return [(key, event) for (key, event) in itertools.product(self.keys, [event])] @pytest.fixture def selector(monkeypatch): monkeypatch.setattr(selectors, ""DefaultSelector"", MockSelector) return MockSelector def get_transitions(proc): prev = None for status, _ in proc._messenger._messages: if prev == status: continue yield status prev = status def test_run_continuous(selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen]) -> None: selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: Your server is not responding\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_hang( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._success = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_raise( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._raise = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" with pytest.raises(MockPopenException): proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run(monkeypatch, proc): def mock_run_once(*args): raise KeyboardInterrupt() monkeypatch.setattr(ContinuousSSH, ""_run_once"", mock_run_once) proc.run() assert list(get_transitions(proc)) == [""disconnected""] ", https://github.com/alexrudy/supertunnel,4c64e2fedda3382b809a2c3d01c711ec4961f933,supertunnel/ssh/continuous_test.py::test_continuous_repr,supertunnel/ssh/continuous_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import collections import logging import time import itertools import io import subprocess import selectors from typing import Any from typing import Deque, Type, List, Tuple import pytest from .config import SSHConfiguration from .continuous import Action from .continuous import ContinuousSSH @pytest.fixture def config(): cfg = SSHConfiguration() return cfg @pytest.fixture def popen(monkeypatch): monkeypatch.setattr(subprocess, ""Popen"", MockPopen) return MockPopen @pytest.fixture def proc(popen, messenger, config, stream): proc = ContinuousSSH(config, stream) proc._messenger = messenger return proc def test_continuous_repr(proc): assert repr(proc).startswith(""ContinuousSSH"") @pytest.mark.parametrize( ""logline,expected_action"", [ (""Entering interactive session"", Action.CONNECTED), (""debug1: Reading configuration"", Action.CONTINUE), (""Host example.com not responding"", Action.DISCONNECTED), ], ) def test_ssh_log_line(proc: ContinuousSSH, logline: str, expected_action: Action, caplog: Any) -> None: logger = logging.getLogger(__name__).getChild(""ssh"") assert proc._handle_ssh_line(logline, logger) == expected_action for record in caplog.records: assert ""debug1"" not in record.message def test_backoff(monkeypatch: Any, proc: ContinuousSSH, caplog: Any) -> None: sleep_times = [] def mock_sleep(duration): sleep_times.append(duration) times: Deque[float] = collections.deque([]) def mock_monotonic(): now = times.popleft() print(f""Time: {now}"") return now monkeypatch.setattr(time, ""sleep"", mock_sleep) monkeypatch.setattr(time, ""monotonic"", mock_monotonic) # Test backoff when process appeared to take 0 time. times.extend([0.1, 0.1]) with proc._backoff(): pass last_wait = sleep_times.pop() assert last_wait == pytest.approx(0.1) assert proc._backoff_time == pytest.approx(0.2) assert not times times.extend([0.0, 0.2]) with proc._backoff(): pass assert not sleep_times assert proc._backoff_time == pytest.approx(0.1) class MockPopenException(Exception): pass class MockPopen: _pid = 0 _success = False _raise = False _stdout = b"""" _stderr = b"""" def __init__(self, cmd, **settings): self._cmd = cmd self._settings = settings self.returncode = None self.pid = self._pid = self._pid + 1 self.stdout = io.BytesIO(self._stdout) self.stderr = io.BytesIO(self._stderr) self._polls = 5 def _finish(self): if self._raise: self._raise = False raise MockPopenException(""Hmm.."") if self._success: self.returncode = 0 else: self.returncode = 1 return self.returncode def poll(self): self._polls -= 1 if not self._polls: self._finish() return self.returncode def wait(self): return self._finish() def kill(self): return self._finish() def terminate(self): return self._finish() class MockSelector: events: List[int] = [] def __init__(self): self.keys = [] def close(self): pass def register(self, fileobj, event): self.keys.append(selectors.SelectorKey(fileobj=fileobj, fd=0, events=[event], data=None)) def select(self, *, timeout=None): if not self.events: return [] event = self.events.pop() return [(key, event) for (key, event) in itertools.product(self.keys, [event])] @pytest.fixture def selector(monkeypatch): monkeypatch.setattr(selectors, ""DefaultSelector"", MockSelector) return MockSelector def get_transitions(proc): prev = None for status, _ in proc._messenger._messages: if prev == status: continue yield status prev = status def test_run_continuous(selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen]) -> None: selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: Your server is not responding\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_hang( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._success = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_raise( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._raise = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" with pytest.raises(MockPopenException): proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run(monkeypatch, proc): def mock_run_once(*args): raise KeyboardInterrupt() monkeypatch.setattr(ContinuousSSH, ""_run_once"", mock_run_once) proc.run() assert list(get_transitions(proc)) == [""disconnected""] ", https://github.com/alexrudy/supertunnel,4c64e2fedda3382b809a2c3d01c711ec4961f933,supertunnel/ssh/continuous_test.py::test_run,supertunnel/ssh/continuous_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import collections import logging import time import itertools import io import subprocess import selectors from typing import Any from typing import Deque, Type, List, Tuple import pytest from .config import SSHConfiguration from .continuous import Action from .continuous import ContinuousSSH @pytest.fixture def config(): cfg = SSHConfiguration() return cfg @pytest.fixture def popen(monkeypatch): monkeypatch.setattr(subprocess, ""Popen"", MockPopen) return MockPopen @pytest.fixture def proc(popen, messenger, config, stream): proc = ContinuousSSH(config, stream) proc._messenger = messenger return proc def test_continuous_repr(proc): assert repr(proc).startswith(""ContinuousSSH"") @pytest.mark.parametrize( ""logline,expected_action"", [ (""Entering interactive session"", Action.CONNECTED), (""debug1: Reading configuration"", Action.CONTINUE), (""Host example.com not responding"", Action.DISCONNECTED), ], ) def test_ssh_log_line(proc: ContinuousSSH, logline: str, expected_action: Action, caplog: Any) -> None: logger = logging.getLogger(__name__).getChild(""ssh"") assert proc._handle_ssh_line(logline, logger) == expected_action for record in caplog.records: assert ""debug1"" not in record.message def test_backoff(monkeypatch: Any, proc: ContinuousSSH, caplog: Any) -> None: sleep_times = [] def mock_sleep(duration): sleep_times.append(duration) times: Deque[float] = collections.deque([]) def mock_monotonic(): now = times.popleft() print(f""Time: {now}"") return now monkeypatch.setattr(time, ""sleep"", mock_sleep) monkeypatch.setattr(time, ""monotonic"", mock_monotonic) # Test backoff when process appeared to take 0 time. times.extend([0.1, 0.1]) with proc._backoff(): pass last_wait = sleep_times.pop() assert last_wait == pytest.approx(0.1) assert proc._backoff_time == pytest.approx(0.2) assert not times times.extend([0.0, 0.2]) with proc._backoff(): pass assert not sleep_times assert proc._backoff_time == pytest.approx(0.1) class MockPopenException(Exception): pass class MockPopen: _pid = 0 _success = False _raise = False _stdout = b"""" _stderr = b"""" def __init__(self, cmd, **settings): self._cmd = cmd self._settings = settings self.returncode = None self.pid = self._pid = self._pid + 1 self.stdout = io.BytesIO(self._stdout) self.stderr = io.BytesIO(self._stderr) self._polls = 5 def _finish(self): if self._raise: self._raise = False raise MockPopenException(""Hmm.."") if self._success: self.returncode = 0 else: self.returncode = 1 return self.returncode def poll(self): self._polls -= 1 if not self._polls: self._finish() return self.returncode def wait(self): return self._finish() def kill(self): return self._finish() def terminate(self): return self._finish() class MockSelector: events: List[int] = [] def __init__(self): self.keys = [] def close(self): pass def register(self, fileobj, event): self.keys.append(selectors.SelectorKey(fileobj=fileobj, fd=0, events=[event], data=None)) def select(self, *, timeout=None): if not self.events: return [] event = self.events.pop() return [(key, event) for (key, event) in itertools.product(self.keys, [event])] @pytest.fixture def selector(monkeypatch): monkeypatch.setattr(selectors, ""DefaultSelector"", MockSelector) return MockSelector def get_transitions(proc): prev = None for status, _ in proc._messenger._messages: if prev == status: continue yield status prev = status def test_run_continuous(selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen]) -> None: selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: Your server is not responding\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_hang( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._success = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_raise( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._raise = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" with pytest.raises(MockPopenException): proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run(monkeypatch, proc): def mock_run_once(*args): raise KeyboardInterrupt() monkeypatch.setattr(ContinuousSSH, ""_run_once"", mock_run_once) proc.run() assert list(get_transitions(proc)) == [""disconnected""] ", https://github.com/alexrudy/supertunnel,4c64e2fedda3382b809a2c3d01c711ec4961f933,supertunnel/ssh/continuous_test.py::test_run_continuous,supertunnel/ssh/continuous_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import collections import logging import time import itertools import io import subprocess import selectors from typing import Any from typing import Deque, Type, List, Tuple import pytest from .config import SSHConfiguration from .continuous import Action from .continuous import ContinuousSSH @pytest.fixture def config(): cfg = SSHConfiguration() return cfg @pytest.fixture def popen(monkeypatch): monkeypatch.setattr(subprocess, ""Popen"", MockPopen) return MockPopen @pytest.fixture def proc(popen, messenger, config, stream): proc = ContinuousSSH(config, stream) proc._messenger = messenger return proc def test_continuous_repr(proc): assert repr(proc).startswith(""ContinuousSSH"") @pytest.mark.parametrize( ""logline,expected_action"", [ (""Entering interactive session"", Action.CONNECTED), (""debug1: Reading configuration"", Action.CONTINUE), (""Host example.com not responding"", Action.DISCONNECTED), ], ) def test_ssh_log_line(proc: ContinuousSSH, logline: str, expected_action: Action, caplog: Any) -> None: logger = logging.getLogger(__name__).getChild(""ssh"") assert proc._handle_ssh_line(logline, logger) == expected_action for record in caplog.records: assert ""debug1"" not in record.message def test_backoff(monkeypatch: Any, proc: ContinuousSSH, caplog: Any) -> None: sleep_times = [] def mock_sleep(duration): sleep_times.append(duration) times: Deque[float] = collections.deque([]) def mock_monotonic(): now = times.popleft() print(f""Time: {now}"") return now monkeypatch.setattr(time, ""sleep"", mock_sleep) monkeypatch.setattr(time, ""monotonic"", mock_monotonic) # Test backoff when process appeared to take 0 time. times.extend([0.1, 0.1]) with proc._backoff(): pass last_wait = sleep_times.pop() assert last_wait == pytest.approx(0.1) assert proc._backoff_time == pytest.approx(0.2) assert not times times.extend([0.0, 0.2]) with proc._backoff(): pass assert not sleep_times assert proc._backoff_time == pytest.approx(0.1) class MockPopenException(Exception): pass class MockPopen: _pid = 0 _success = False _raise = False _stdout = b"""" _stderr = b"""" def __init__(self, cmd, **settings): self._cmd = cmd self._settings = settings self.returncode = None self.pid = self._pid = self._pid + 1 self.stdout = io.BytesIO(self._stdout) self.stderr = io.BytesIO(self._stderr) self._polls = 5 def _finish(self): if self._raise: self._raise = False raise MockPopenException(""Hmm.."") if self._success: self.returncode = 0 else: self.returncode = 1 return self.returncode def poll(self): self._polls -= 1 if not self._polls: self._finish() return self.returncode def wait(self): return self._finish() def kill(self): return self._finish() def terminate(self): return self._finish() class MockSelector: events: List[int] = [] def __init__(self): self.keys = [] def close(self): pass def register(self, fileobj, event): self.keys.append(selectors.SelectorKey(fileobj=fileobj, fd=0, events=[event], data=None)) def select(self, *, timeout=None): if not self.events: return [] event = self.events.pop() return [(key, event) for (key, event) in itertools.product(self.keys, [event])] @pytest.fixture def selector(monkeypatch): monkeypatch.setattr(selectors, ""DefaultSelector"", MockSelector) return MockSelector def get_transitions(proc): prev = None for status, _ in proc._messenger._messages: if prev == status: continue yield status prev = status def test_run_continuous(selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen]) -> None: selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: Your server is not responding\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_hang( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._success = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_raise( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._raise = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" with pytest.raises(MockPopenException): proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run(monkeypatch, proc): def mock_run_once(*args): raise KeyboardInterrupt() monkeypatch.setattr(ContinuousSSH, ""_run_once"", mock_run_once) proc.run() assert list(get_transitions(proc)) == [""disconnected""] ", https://github.com/alexrudy/supertunnel,4c64e2fedda3382b809a2c3d01c711ec4961f933,supertunnel/ssh/continuous_test.py::test_run_continuous_hang,supertunnel/ssh/continuous_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import collections import logging import time import itertools import io import subprocess import selectors from typing import Any from typing import Deque, Type, List, Tuple import pytest from .config import SSHConfiguration from .continuous import Action from .continuous import ContinuousSSH @pytest.fixture def config(): cfg = SSHConfiguration() return cfg @pytest.fixture def popen(monkeypatch): monkeypatch.setattr(subprocess, ""Popen"", MockPopen) return MockPopen @pytest.fixture def proc(popen, messenger, config, stream): proc = ContinuousSSH(config, stream) proc._messenger = messenger return proc def test_continuous_repr(proc): assert repr(proc).startswith(""ContinuousSSH"") @pytest.mark.parametrize( ""logline,expected_action"", [ (""Entering interactive session"", Action.CONNECTED), (""debug1: Reading configuration"", Action.CONTINUE), (""Host example.com not responding"", Action.DISCONNECTED), ], ) def test_ssh_log_line(proc: ContinuousSSH, logline: str, expected_action: Action, caplog: Any) -> None: logger = logging.getLogger(__name__).getChild(""ssh"") assert proc._handle_ssh_line(logline, logger) == expected_action for record in caplog.records: assert ""debug1"" not in record.message def test_backoff(monkeypatch: Any, proc: ContinuousSSH, caplog: Any) -> None: sleep_times = [] def mock_sleep(duration): sleep_times.append(duration) times: Deque[float] = collections.deque([]) def mock_monotonic(): now = times.popleft() print(f""Time: {now}"") return now monkeypatch.setattr(time, ""sleep"", mock_sleep) monkeypatch.setattr(time, ""monotonic"", mock_monotonic) # Test backoff when process appeared to take 0 time. times.extend([0.1, 0.1]) with proc._backoff(): pass last_wait = sleep_times.pop() assert last_wait == pytest.approx(0.1) assert proc._backoff_time == pytest.approx(0.2) assert not times times.extend([0.0, 0.2]) with proc._backoff(): pass assert not sleep_times assert proc._backoff_time == pytest.approx(0.1) class MockPopenException(Exception): pass class MockPopen: _pid = 0 _success = False _raise = False _stdout = b"""" _stderr = b"""" def __init__(self, cmd, **settings): self._cmd = cmd self._settings = settings self.returncode = None self.pid = self._pid = self._pid + 1 self.stdout = io.BytesIO(self._stdout) self.stderr = io.BytesIO(self._stderr) self._polls = 5 def _finish(self): if self._raise: self._raise = False raise MockPopenException(""Hmm.."") if self._success: self.returncode = 0 else: self.returncode = 1 return self.returncode def poll(self): self._polls -= 1 if not self._polls: self._finish() return self.returncode def wait(self): return self._finish() def kill(self): return self._finish() def terminate(self): return self._finish() class MockSelector: events: List[int] = [] def __init__(self): self.keys = [] def close(self): pass def register(self, fileobj, event): self.keys.append(selectors.SelectorKey(fileobj=fileobj, fd=0, events=[event], data=None)) def select(self, *, timeout=None): if not self.events: return [] event = self.events.pop() return [(key, event) for (key, event) in itertools.product(self.keys, [event])] @pytest.fixture def selector(monkeypatch): monkeypatch.setattr(selectors, ""DefaultSelector"", MockSelector) return MockSelector def get_transitions(proc): prev = None for status, _ in proc._messenger._messages: if prev == status: continue yield status prev = status def test_run_continuous(selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen]) -> None: selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: Your server is not responding\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_hang( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._success = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_raise( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._raise = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" with pytest.raises(MockPopenException): proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run(monkeypatch, proc): def mock_run_once(*args): raise KeyboardInterrupt() monkeypatch.setattr(ContinuousSSH, ""_run_once"", mock_run_once) proc.run() assert list(get_transitions(proc)) == [""disconnected""] ", https://github.com/alexrudy/supertunnel,4c64e2fedda3382b809a2c3d01c711ec4961f933,supertunnel/ssh/continuous_test.py::test_run_continuous_raise,supertunnel/ssh/continuous_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import collections import logging import time import itertools import io import subprocess import selectors from typing import Any from typing import Deque, Type, List, Tuple import pytest from .config import SSHConfiguration from .continuous import Action from .continuous import ContinuousSSH @pytest.fixture def config(): cfg = SSHConfiguration() return cfg @pytest.fixture def popen(monkeypatch): monkeypatch.setattr(subprocess, ""Popen"", MockPopen) return MockPopen @pytest.fixture def proc(popen, messenger, config, stream): proc = ContinuousSSH(config, stream) proc._messenger = messenger return proc def test_continuous_repr(proc): assert repr(proc).startswith(""ContinuousSSH"") @pytest.mark.parametrize( ""logline,expected_action"", [ (""Entering interactive session"", Action.CONNECTED), (""debug1: Reading configuration"", Action.CONTINUE), (""Host example.com not responding"", Action.DISCONNECTED), ], ) def test_ssh_log_line(proc: ContinuousSSH, logline: str, expected_action: Action, caplog: Any) -> None: logger = logging.getLogger(__name__).getChild(""ssh"") assert proc._handle_ssh_line(logline, logger) == expected_action for record in caplog.records: assert ""debug1"" not in record.message def test_backoff(monkeypatch: Any, proc: ContinuousSSH, caplog: Any) -> None: sleep_times = [] def mock_sleep(duration): sleep_times.append(duration) times: Deque[float] = collections.deque([]) def mock_monotonic(): now = times.popleft() print(f""Time: {now}"") return now monkeypatch.setattr(time, ""sleep"", mock_sleep) monkeypatch.setattr(time, ""monotonic"", mock_monotonic) # Test backoff when process appeared to take 0 time. times.extend([0.1, 0.1]) with proc._backoff(): pass last_wait = sleep_times.pop() assert last_wait == pytest.approx(0.1) assert proc._backoff_time == pytest.approx(0.2) assert not times times.extend([0.0, 0.2]) with proc._backoff(): pass assert not sleep_times assert proc._backoff_time == pytest.approx(0.1) class MockPopenException(Exception): pass class MockPopen: _pid = 0 _success = False _raise = False _stdout = b"""" _stderr = b"""" def __init__(self, cmd, **settings): self._cmd = cmd self._settings = settings self.returncode = None self.pid = self._pid = self._pid + 1 self.stdout = io.BytesIO(self._stdout) self.stderr = io.BytesIO(self._stderr) self._polls = 5 def _finish(self): if self._raise: self._raise = False raise MockPopenException(""Hmm.."") if self._success: self.returncode = 0 else: self.returncode = 1 return self.returncode def poll(self): self._polls -= 1 if not self._polls: self._finish() return self.returncode def wait(self): return self._finish() def kill(self): return self._finish() def terminate(self): return self._finish() class MockSelector: events: List[int] = [] def __init__(self): self.keys = [] def close(self): pass def register(self, fileobj, event): self.keys.append(selectors.SelectorKey(fileobj=fileobj, fd=0, events=[event], data=None)) def select(self, *, timeout=None): if not self.events: return [] event = self.events.pop() return [(key, event) for (key, event) in itertools.product(self.keys, [event])] @pytest.fixture def selector(monkeypatch): monkeypatch.setattr(selectors, ""DefaultSelector"", MockSelector) return MockSelector def get_transitions(proc): prev = None for status, _ in proc._messenger._messages: if prev == status: continue yield status prev = status def test_run_continuous(selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen]) -> None: selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: Your server is not responding\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_hang( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._success = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_raise( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._raise = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" with pytest.raises(MockPopenException): proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run(monkeypatch, proc): def mock_run_once(*args): raise KeyboardInterrupt() monkeypatch.setattr(ContinuousSSH, ""_run_once"", mock_run_once) proc.run() assert list(get_transitions(proc)) == [""disconnected""] ", https://github.com/alexrudy/supertunnel,4c64e2fedda3382b809a2c3d01c711ec4961f933,supertunnel/ssh/continuous_test.py::test_ssh_log_line[debug1: Reading configuration-Action.CONTINUE],supertunnel/ssh/continuous_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import collections import logging import time import itertools import io import subprocess import selectors from typing import Any from typing import Deque, Type, List, Tuple import pytest from .config import SSHConfiguration from .continuous import Action from .continuous import ContinuousSSH @pytest.fixture def config(): cfg = SSHConfiguration() return cfg @pytest.fixture def popen(monkeypatch): monkeypatch.setattr(subprocess, ""Popen"", MockPopen) return MockPopen @pytest.fixture def proc(popen, messenger, config, stream): proc = ContinuousSSH(config, stream) proc._messenger = messenger return proc def test_continuous_repr(proc): assert repr(proc).startswith(""ContinuousSSH"") @pytest.mark.parametrize( ""logline,expected_action"", [ (""Entering interactive session"", Action.CONNECTED), (""debug1: Reading configuration"", Action.CONTINUE), (""Host example.com not responding"", Action.DISCONNECTED), ], ) def test_ssh_log_line(proc: ContinuousSSH, logline: str, expected_action: Action, caplog: Any) -> None: logger = logging.getLogger(__name__).getChild(""ssh"") assert proc._handle_ssh_line(logline, logger) == expected_action for record in caplog.records: assert ""debug1"" not in record.message def test_backoff(monkeypatch: Any, proc: ContinuousSSH, caplog: Any) -> None: sleep_times = [] def mock_sleep(duration): sleep_times.append(duration) times: Deque[float] = collections.deque([]) def mock_monotonic(): now = times.popleft() print(f""Time: {now}"") return now monkeypatch.setattr(time, ""sleep"", mock_sleep) monkeypatch.setattr(time, ""monotonic"", mock_monotonic) # Test backoff when process appeared to take 0 time. times.extend([0.1, 0.1]) with proc._backoff(): pass last_wait = sleep_times.pop() assert last_wait == pytest.approx(0.1) assert proc._backoff_time == pytest.approx(0.2) assert not times times.extend([0.0, 0.2]) with proc._backoff(): pass assert not sleep_times assert proc._backoff_time == pytest.approx(0.1) class MockPopenException(Exception): pass class MockPopen: _pid = 0 _success = False _raise = False _stdout = b"""" _stderr = b"""" def __init__(self, cmd, **settings): self._cmd = cmd self._settings = settings self.returncode = None self.pid = self._pid = self._pid + 1 self.stdout = io.BytesIO(self._stdout) self.stderr = io.BytesIO(self._stderr) self._polls = 5 def _finish(self): if self._raise: self._raise = False raise MockPopenException(""Hmm.."") if self._success: self.returncode = 0 else: self.returncode = 1 return self.returncode def poll(self): self._polls -= 1 if not self._polls: self._finish() return self.returncode def wait(self): return self._finish() def kill(self): return self._finish() def terminate(self): return self._finish() class MockSelector: events: List[int] = [] def __init__(self): self.keys = [] def close(self): pass def register(self, fileobj, event): self.keys.append(selectors.SelectorKey(fileobj=fileobj, fd=0, events=[event], data=None)) def select(self, *, timeout=None): if not self.events: return [] event = self.events.pop() return [(key, event) for (key, event) in itertools.product(self.keys, [event])] @pytest.fixture def selector(monkeypatch): monkeypatch.setattr(selectors, ""DefaultSelector"", MockSelector) return MockSelector def get_transitions(proc): prev = None for status, _ in proc._messenger._messages: if prev == status: continue yield status prev = status def test_run_continuous(selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen]) -> None: selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: Your server is not responding\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_hang( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._success = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_raise( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._raise = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" with pytest.raises(MockPopenException): proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run(monkeypatch, proc): def mock_run_once(*args): raise KeyboardInterrupt() monkeypatch.setattr(ContinuousSSH, ""_run_once"", mock_run_once) proc.run() assert list(get_transitions(proc)) == [""disconnected""] ", https://github.com/alexrudy/supertunnel,4c64e2fedda3382b809a2c3d01c711ec4961f933,supertunnel/ssh/continuous_test.py::test_ssh_log_line[Entering interactive session-Action.CONNECTED],supertunnel/ssh/continuous_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import collections import logging import time import itertools import io import subprocess import selectors from typing import Any from typing import Deque, Type, List, Tuple import pytest from .config import SSHConfiguration from .continuous import Action from .continuous import ContinuousSSH @pytest.fixture def config(): cfg = SSHConfiguration() return cfg @pytest.fixture def popen(monkeypatch): monkeypatch.setattr(subprocess, ""Popen"", MockPopen) return MockPopen @pytest.fixture def proc(popen, messenger, config, stream): proc = ContinuousSSH(config, stream) proc._messenger = messenger return proc def test_continuous_repr(proc): assert repr(proc).startswith(""ContinuousSSH"") @pytest.mark.parametrize( ""logline,expected_action"", [ (""Entering interactive session"", Action.CONNECTED), (""debug1: Reading configuration"", Action.CONTINUE), (""Host example.com not responding"", Action.DISCONNECTED), ], ) def test_ssh_log_line(proc: ContinuousSSH, logline: str, expected_action: Action, caplog: Any) -> None: logger = logging.getLogger(__name__).getChild(""ssh"") assert proc._handle_ssh_line(logline, logger) == expected_action for record in caplog.records: assert ""debug1"" not in record.message def test_backoff(monkeypatch: Any, proc: ContinuousSSH, caplog: Any) -> None: sleep_times = [] def mock_sleep(duration): sleep_times.append(duration) times: Deque[float] = collections.deque([]) def mock_monotonic(): now = times.popleft() print(f""Time: {now}"") return now monkeypatch.setattr(time, ""sleep"", mock_sleep) monkeypatch.setattr(time, ""monotonic"", mock_monotonic) # Test backoff when process appeared to take 0 time. times.extend([0.1, 0.1]) with proc._backoff(): pass last_wait = sleep_times.pop() assert last_wait == pytest.approx(0.1) assert proc._backoff_time == pytest.approx(0.2) assert not times times.extend([0.0, 0.2]) with proc._backoff(): pass assert not sleep_times assert proc._backoff_time == pytest.approx(0.1) class MockPopenException(Exception): pass class MockPopen: _pid = 0 _success = False _raise = False _stdout = b"""" _stderr = b"""" def __init__(self, cmd, **settings): self._cmd = cmd self._settings = settings self.returncode = None self.pid = self._pid = self._pid + 1 self.stdout = io.BytesIO(self._stdout) self.stderr = io.BytesIO(self._stderr) self._polls = 5 def _finish(self): if self._raise: self._raise = False raise MockPopenException(""Hmm.."") if self._success: self.returncode = 0 else: self.returncode = 1 return self.returncode def poll(self): self._polls -= 1 if not self._polls: self._finish() return self.returncode def wait(self): return self._finish() def kill(self): return self._finish() def terminate(self): return self._finish() class MockSelector: events: List[int] = [] def __init__(self): self.keys = [] def close(self): pass def register(self, fileobj, event): self.keys.append(selectors.SelectorKey(fileobj=fileobj, fd=0, events=[event], data=None)) def select(self, *, timeout=None): if not self.events: return [] event = self.events.pop() return [(key, event) for (key, event) in itertools.product(self.keys, [event])] @pytest.fixture def selector(monkeypatch): monkeypatch.setattr(selectors, ""DefaultSelector"", MockSelector) return MockSelector def get_transitions(proc): prev = None for status, _ in proc._messenger._messages: if prev == status: continue yield status prev = status def test_run_continuous(selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen]) -> None: selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: Your server is not responding\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_hang( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._success = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_raise( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._raise = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" with pytest.raises(MockPopenException): proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run(monkeypatch, proc): def mock_run_once(*args): raise KeyboardInterrupt() monkeypatch.setattr(ContinuousSSH, ""_run_once"", mock_run_once) proc.run() assert list(get_transitions(proc)) == [""disconnected""] ", https://github.com/alexrudy/supertunnel,4c64e2fedda3382b809a2c3d01c711ec4961f933,supertunnel/ssh/continuous_test.py::test_ssh_log_line[Host example.com not responding-Action.DISCONNECTED],supertunnel/ssh/continuous_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import collections import logging import time import itertools import io import subprocess import selectors from typing import Any from typing import Deque, Type, List, Tuple import pytest from .config import SSHConfiguration from .continuous import Action from .continuous import ContinuousSSH @pytest.fixture def config(): cfg = SSHConfiguration() return cfg @pytest.fixture def popen(monkeypatch): monkeypatch.setattr(subprocess, ""Popen"", MockPopen) return MockPopen @pytest.fixture def proc(popen, messenger, config, stream): proc = ContinuousSSH(config, stream) proc._messenger = messenger return proc def test_continuous_repr(proc): assert repr(proc).startswith(""ContinuousSSH"") @pytest.mark.parametrize( ""logline,expected_action"", [ (""Entering interactive session"", Action.CONNECTED), (""debug1: Reading configuration"", Action.CONTINUE), (""Host example.com not responding"", Action.DISCONNECTED), ], ) def test_ssh_log_line(proc: ContinuousSSH, logline: str, expected_action: Action, caplog: Any) -> None: logger = logging.getLogger(__name__).getChild(""ssh"") assert proc._handle_ssh_line(logline, logger) == expected_action for record in caplog.records: assert ""debug1"" not in record.message def test_backoff(monkeypatch: Any, proc: ContinuousSSH, caplog: Any) -> None: sleep_times = [] def mock_sleep(duration): sleep_times.append(duration) times: Deque[float] = collections.deque([]) def mock_monotonic(): now = times.popleft() print(f""Time: {now}"") return now monkeypatch.setattr(time, ""sleep"", mock_sleep) monkeypatch.setattr(time, ""monotonic"", mock_monotonic) # Test backoff when process appeared to take 0 time. times.extend([0.1, 0.1]) with proc._backoff(): pass last_wait = sleep_times.pop() assert last_wait == pytest.approx(0.1) assert proc._backoff_time == pytest.approx(0.2) assert not times times.extend([0.0, 0.2]) with proc._backoff(): pass assert not sleep_times assert proc._backoff_time == pytest.approx(0.1) class MockPopenException(Exception): pass class MockPopen: _pid = 0 _success = False _raise = False _stdout = b"""" _stderr = b"""" def __init__(self, cmd, **settings): self._cmd = cmd self._settings = settings self.returncode = None self.pid = self._pid = self._pid + 1 self.stdout = io.BytesIO(self._stdout) self.stderr = io.BytesIO(self._stderr) self._polls = 5 def _finish(self): if self._raise: self._raise = False raise MockPopenException(""Hmm.."") if self._success: self.returncode = 0 else: self.returncode = 1 return self.returncode def poll(self): self._polls -= 1 if not self._polls: self._finish() return self.returncode def wait(self): return self._finish() def kill(self): return self._finish() def terminate(self): return self._finish() class MockSelector: events: List[int] = [] def __init__(self): self.keys = [] def close(self): pass def register(self, fileobj, event): self.keys.append(selectors.SelectorKey(fileobj=fileobj, fd=0, events=[event], data=None)) def select(self, *, timeout=None): if not self.events: return [] event = self.events.pop() return [(key, event) for (key, event) in itertools.product(self.keys, [event])] @pytest.fixture def selector(monkeypatch): monkeypatch.setattr(selectors, ""DefaultSelector"", MockSelector) return MockSelector def get_transitions(proc): prev = None for status, _ in proc._messenger._messages: if prev == status: continue yield status prev = status def test_run_continuous(selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen]) -> None: selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: Your server is not responding\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_hang( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._success = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run_continuous_raise( selector: Type[MockSelector], proc: ContinuousSSH, caplog: Any, popen: Type[MockPopen] ) -> None: popen._raise = True selector.events = [selectors.EVENT_READ] * 3 popen._stdout = b""Entering interactive session\ndebug1: some other message\ndone"" with pytest.raises(MockPopenException): proc._run_once() assert list(get_transitions(proc)) == [""connecting"", ""connected"", ""disconnected""] def test_run(monkeypatch, proc): def mock_run_once(*args): raise KeyboardInterrupt() monkeypatch.setattr(ContinuousSSH, ""_run_once"", mock_run_once) proc.run() assert list(get_transitions(proc)) == [""disconnected""] ", https://github.com/aluttik/kitsune,43824cccf46f433a71b30a7febc0e3500b831067,tests/test_kitsune.py::test_kitsune_printer_plain,tests/test_kitsune.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- import io import re import time from kitsune import KitsunePrinter # longer delay = more reliable tests DELAY_SECONDS = 0.5 def test_kitsune_printer_plain(tmpdir): a = tmpdir.join(""a.log"") b = tmpdir.join(""bb.log"") # 'touch' the files a.ensure(file=True) b.ensure(file=True) buf = io.StringIO() printer = KitsunePrinter([a.strpath, b.strpath], color=False, stream=buf) printer.start() time.sleep(DELAY_SECONDS) a.write(""foo\n"", mode=""a+"") time.sleep(DELAY_SECONDS) b.write(""bar\n"", mode=""a+"") time.sleep(DELAY_SECONDS) a.write(""baz\n"", mode=""a+"") time.sleep(DELAY_SECONDS) output = buf.getvalue().splitlines() assert len(output) == 3 assert output[0] == ""a.log | foo"" assert output[1] == ""bb.log | bar"" assert output[2] == ""a.log | baz"" printer.stop() a.write(""printer already stopped\n"", mode=""a+"") time.sleep(DELAY_SECONDS) output = buf.getvalue().splitlines() assert len(output) == 3 assert output[0] == ""a.log | foo"" assert output[1] == ""bb.log | bar"" assert output[2] == ""a.log | baz"" def test_kitsune_printer_rainbow(tmpdir): a = tmpdir.join(""a.log"") b = tmpdir.join(""bb.log"") # 'touch' the files a.ensure(file=True) b.ensure(file=True) buf = io.StringIO() printer = KitsunePrinter([a.strpath, b.strpath], color=True, stream=buf) printer.start() time.sleep(DELAY_SECONDS) a.write(""foo\n"", mode=""a+"") time.sleep(DELAY_SECONDS) b.write(""bar\n"", mode=""a+"") time.sleep(DELAY_SECONDS) a.write(""baz\n"", mode=""a+"") time.sleep(DELAY_SECONDS) colored_line_regex = re.compile(r""(\x1b\[3[0-7](?:;1)?m)(\w+\..+ +\|\x1b\[0m .*)\n"") value = buf.getvalue() matches = colored_line_regex.findall(value) assert len(matches) == 3 output = tuple(zip(*matches))[1] assert output[0] == ""a.log |\033[0m foo"" assert output[1] == ""bb.log |\033[0m bar"" assert output[2] == ""a.log |\033[0m baz"" printer.stop() a.write(""printer already stopped\n"", mode=""a+"") time.sleep(DELAY_SECONDS) value = buf.getvalue() matches = colored_line_regex.findall(value) assert len(matches) == 3 output = tuple(zip(*matches))[1] assert output[0] == ""a.log |\033[0m foo"" assert output[1] == ""bb.log |\033[0m bar"" assert output[2] == ""a.log |\033[0m baz"" ", https://github.com/apache/beam,faae168fa34e97475df70b707f4df91c4946c6ca,sdks/python/apache_beam/runners/portability/portable_runner_test.py::PortableRunnerTest::test_assert_that,sdks/python/apache_beam/runners/portability/portable_runner_test.py,OD,flaky,Accepted,https://github.com/apache/beam/pull/36485,classify;root_cause,"# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the ""License""); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # pytype: skip-file import inspect import logging import socket import subprocess import sys import tempfile import time import unittest from unittest.mock import MagicMock import grpc import apache_beam as beam from apache_beam.options.pipeline_options import DebugOptions from apache_beam.options.pipeline_options import DirectOptions from apache_beam.options.pipeline_options import PipelineOptions from apache_beam.options.pipeline_options import PortableOptions from apache_beam.portability import python_urns from apache_beam.portability.api import beam_job_api_pb2 from apache_beam.portability.api import beam_job_api_pb2_grpc from apache_beam.runners.portability import portable_runner from apache_beam.runners.portability.fn_api_runner import fn_runner_test from apache_beam.runners.portability.local_job_service import LocalJobServicer from apache_beam.runners.portability.portable_runner import PortableRunner from apache_beam.runners.worker import worker_pool_main from apache_beam.runners.worker.channel_factory import GRPCChannelFactory from apache_beam.testing.util import assert_that from apache_beam.testing.util import equal_to from apache_beam.transforms import environments from apache_beam.transforms import userstate _LOGGER = logging.getLogger(__name__) class PortableRunnerTest(fn_runner_test.FnApiRunnerTest): # Controls job service interaction, not sdk harness interaction. _use_subprocesses = False @classmethod def _pick_unused_port(cls): return cls._pick_unused_ports(num_ports=1)[0] @staticmethod def _pick_unused_ports(num_ports): """"""Not perfect, but we have to provide a port to the subprocess."""""" # TODO(robertwb): Consider letting the subprocess communicate a choice of # port back. sockets = [] ports = [] for _ in range(0, num_ports): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sockets.append(s) s.bind(('localhost', 0)) _, port = s.getsockname() ports.append(port) try: return ports finally: for s in sockets: s.close() @classmethod def _start_local_runner_subprocess_job_service(cls): cls._maybe_kill_subprocess() # TODO(robertwb): Consider letting the subprocess pick one and # communicate it back... # pylint: disable=unbalanced-tuple-unpacking job_port, expansion_port = cls._pick_unused_ports(num_ports=2) _LOGGER.info('Starting server on port %d.', job_port) cls._subprocess = subprocess.Popen( cls._subprocess_command(job_port, expansion_port)) address = 'localhost:%d' % job_port job_service = beam_job_api_pb2_grpc.JobServiceStub( GRPCChannelFactory.insecure_channel(address)) _LOGGER.info('Waiting for server to be ready...') start = time.time() # Long timeout based previously flaky test. See issue #22115. timeout = 300 while True: time.sleep(0.1) if cls._subprocess.poll() is not None: raise RuntimeError( 'Subprocess terminated unexpectedly with exit code %d.' % cls._subprocess.returncode) elif time.time() - start > timeout: raise RuntimeError( 'Pipeline timed out waiting for job service subprocess.') else: try: job_service.GetState( beam_job_api_pb2.GetJobStateRequest(job_id='[fake]')) break except grpc.RpcError as exn: if exn.code() != grpc.StatusCode.UNAVAILABLE: # We were able to contact the service for our fake state request. break _LOGGER.info('Server ready.') return address @classmethod def _get_job_endpoint(cls): if '_job_endpoint' not in cls.__dict__: cls._job_endpoint = cls._create_job_endpoint() return cls._job_endpoint @classmethod def _create_job_endpoint(cls): if cls._use_subprocesses: return cls._start_local_runner_subprocess_job_service() else: cls._servicer = LocalJobServicer() return 'localhost:%d' % cls._servicer.start_grpc_server() @classmethod def get_runner(cls): return portable_runner.PortableRunner() @classmethod def tearDownClass(cls): cls._maybe_kill_subprocess() @classmethod def _maybe_kill_subprocess(cls): if hasattr(cls, '_subprocess') and cls._subprocess.poll() is None: cls._subprocess.kill() time.sleep(0.1) def create_options(self): def get_pipeline_name(): for _, _, _, method_name, _, _ in inspect.stack(): if method_name.find('test') != -1: return method_name return 'unknown_test' # Set the job name for better debugging. options = PipelineOptions.from_dictionary( {'job_name': get_pipeline_name() + '_' + str(time.time())}) options.view_as(PortableOptions).job_endpoint = self._get_job_endpoint() # Override the default environment type for testing. options.view_as(PortableOptions).environment_type = ( python_urns.EMBEDDED_PYTHON) # Enable caching (disabled by default) options.view_as(DebugOptions).add_experiment('state_cache_size=100') # Enable time-based data buffer (disabled by default) options.view_as(DebugOptions).add_experiment( 'data_buffer_time_limit_ms=1000') return options def create_pipeline(self, is_drain=False): return beam.Pipeline(self.get_runner(), self.create_options()) def test_pardo_state_with_custom_key_coder(self): """"""Tests that state requests work correctly when the key coder is an SDK-specific coder, i.e. non standard coder. This is additionally enforced by Java's ProcessBundleDescriptorsTest and by Flink's ExecutableStageDoFnOperator which detects invalid encoding by checking for the correct key group of the encoded key."""""" index_state_spec = userstate.CombiningValueStateSpec('index', sum) # Test params # Ensure decent amount of elements to serve all partitions n = 200 duplicates = 1 split = n // (duplicates + 1) inputs = [(i % split, str(i % split)) for i in range(0, n)] # Use a DoFn which has to use FastPrimitivesCoder because the type cannot # be inferred class Input(beam.DoFn): def process(self, impulse): for i in inputs: yield i class AddIndex(beam.DoFn): def process(self, kv, index=beam.DoFn.StateParam(index_state_spec)): k, v = kv index.add(1) yield k, v, index.read() expected = [(i % split, str(i % split), i // split + 1) for i in range(0, n)] with self.create_pipeline() as p: assert_that( p | beam.Impulse() | beam.ParDo(Input()) | beam.ParDo(AddIndex()), equal_to(expected)) # Inherits all other tests from fn_api_runner_test.FnApiRunnerTest def test_sdf_default_truncate_when_bounded(self): raise unittest.SkipTest(""Portable runners don't support drain yet."") def test_sdf_default_truncate_when_unbounded(self): raise unittest.SkipTest(""Portable runners don't support drain yet."") def test_sdf_with_truncate(self): raise unittest.SkipTest(""Portable runners don't support drain yet."") def test_draining_sdf_with_sdf_initiated_checkpointing(self): raise unittest.SkipTest(""Portable runners don't support drain yet."") def test_pardo_et_timer_with_no_firing(self): if type(self).__name__ in { 'PortableRunnerTest', 'PortableRunnerTestWithSubprocesses', 'PortableRunnerTestWithSubprocessesAndMultiWorkers', 'PortableRunnerTestWithExternalEnv', 'PortableRunnerTestWithLocalDocker', 'PortableRunnerOptimizedWithoutFusion', 'SamzaRunnerTest', 'SparkRunnerTest' }: raise unittest.SkipTest(""https://github.com/apache/beam/issues/35168"") super().test_pardo_et_timer_with_no_firing() def test_pardo_et_timer_with_no_reset(self): if type(self).__name__ in { 'PortableRunnerTest', 'PortableRunnerTestWithSubprocesses', 'PortableRunnerTestWithSubprocessesAndMultiWorkers', 'PortableRunnerTestWithExternalEnv', 'PortableRunnerTestWithLocalDocker', 'PortableRunnerOptimizedWithoutFusion', 'SamzaRunnerTest', 'SparkRunnerTest' }: raise unittest.SkipTest(""https://github.com/apache/beam/issues/35168"") super().test_pardo_et_timer_with_no_reset() @unittest.skip(""https://github.com/apache/beam/issues/19422"") class PortableRunnerOptimized(PortableRunnerTest): def create_options(self): options = super().create_options() options.view_as(DebugOptions).add_experiment('pre_optimize=all') options.view_as(DebugOptions).add_experiment('state_cache_size=100') options.view_as(DebugOptions).add_experiment( 'data_buffer_time_limit_ms=1000') return options # TODO(https://github.com/apache/beam/issues/19422): Delete this test after # PortableRunner supports beam:runner:executable_stage:v1. class PortableRunnerOptimizedWithoutFusion(PortableRunnerTest): def create_options(self): options = super().create_options() options.view_as(DebugOptions).add_experiment( 'pre_optimize=all_except_fusion') options.view_as(DebugOptions).add_experiment('st", https://github.com/attzonko/mmpy_bot,4faa19a9e04c75af94c45c24dbd0c9d9ecb867d4,tests/unit_tests/test_scheduler.py::test_add_onetime_job_without_trigger_time,tests/unit_tests/test_scheduler.py,OD-Vic,flaky,Deleted,nan,classify;root_cause,"import pytest import time from datetime import datetime, timedelta from mmpy_bot.scheduler import schedule def foo(msg): print(msg) def foo2(dict_acc): dict_acc['acc'] += 1 @pytest.fixture(scope=""function"") def my_schedule(): return schedule def test_add_onetime_job_without_trigger_time(my_schedule): my_schedule.once().do(foo, msg='hello') if len(my_schedule.jobs) != 1: raise AssertionError(""Job is not added to schedule."") time.sleep(1) # wait for worker threads to start my_schedule.run_pending() if len(my_schedule.jobs) != 0: raise AssertionError(""Job is not executed by schedule."") def test_add_onetime_job_with_trigger_time(my_schedule): run_time = datetime.now() + timedelta(seconds=2) my_schedule.once(run_time).do(foo, msg='hello') if len(my_schedule.jobs) != 1: raise AssertionError(""Job is not added to schedule."") time.sleep(3) # wait for worker threads to start my_schedule.run_pending() if len(my_schedule.jobs) != 0: raise AssertionError(""Job is not executed by schedule."") def test_add_periodic_job(my_schedule): dict_acc = {'acc': 0} my_schedule.every(1).seconds.do(foo2, dict_acc=dict_acc) time.sleep(2) # wait for worker threads to start my_schedule.run_pending() time.sleep(2) # wait for worker threads to start my_schedule.run_pending() if dict_acc['acc'] != 2: raise AssertionError(""Incorrect periodic job execution."") ", https://github.com/attzonko/mmpy_bot,4faa19a9e04c75af94c45c24dbd0c9d9ecb867d4,tests/unit_tests/test_scheduler.py::test_add_onetime_job_with_trigger_time,tests/unit_tests/test_scheduler.py,OD-Vic,flaky,Deleted,nan,classify;root_cause,"import pytest import time from datetime import datetime, timedelta from mmpy_bot.scheduler import schedule def foo(msg): print(msg) def foo2(dict_acc): dict_acc['acc'] += 1 @pytest.fixture(scope=""function"") def my_schedule(): return schedule def test_add_onetime_job_without_trigger_time(my_schedule): my_schedule.once().do(foo, msg='hello') if len(my_schedule.jobs) != 1: raise AssertionError(""Job is not added to schedule."") time.sleep(1) # wait for worker threads to start my_schedule.run_pending() if len(my_schedule.jobs) != 0: raise AssertionError(""Job is not executed by schedule."") def test_add_onetime_job_with_trigger_time(my_schedule): run_time = datetime.now() + timedelta(seconds=2) my_schedule.once(run_time).do(foo, msg='hello') if len(my_schedule.jobs) != 1: raise AssertionError(""Job is not added to schedule."") time.sleep(3) # wait for worker threads to start my_schedule.run_pending() if len(my_schedule.jobs) != 0: raise AssertionError(""Job is not executed by schedule."") def test_add_periodic_job(my_schedule): dict_acc = {'acc': 0} my_schedule.every(1).seconds.do(foo2, dict_acc=dict_acc) time.sleep(2) # wait for worker threads to start my_schedule.run_pending() time.sleep(2) # wait for worker threads to start my_schedule.run_pending() if dict_acc['acc'] != 2: raise AssertionError(""Incorrect periodic job execution."") ", https://github.com/AuraiProject/freesia,16834c17725edac74eb50fc5ce2ff1db6f3f4840,tests/test_route.py::RouteTestCase::test_url_match,tests/test_route.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import unittest from collections import namedtuple from freesia.route import Route TestRule = namedtuple(""TestRule"", [""rule"", ""regex"", ""matching"", ""not_matching""]) test_rules = (TestRule(*u) for u in [ (""/test"", ""/test"", ""/test"", ""/testtest""), (""/test/"", ""/test/(?P[^/]+)"", ""/test/mike"", ""/test/mike/mike""), (""/test/"", ""/test/(?P-?\d+)"", ""/test/1"", ""/test1.o""), (""/test/"", ""/test/(?P-?[\d.]+)"", ""/test/1.0"", ""/test/1"") ]) async def temp(): pass class RouteTestCase(unittest.TestCase): def setUp(self): self.route_bak = Route globals()[""Route""] = type(""Route"", Route.__bases__, dict(Route.__dict__)) def tearDown(self): globals()[""Route""] = self.route_bak def test_route_backup(self): self.assertNotEqual(id(self.route_bak), id(Route)) def test_expect_regex_rules(self, rules=None): if rules is None: rules = test_rules for r in rules: route = Route(r.rule, [""GET""], temp, { ""checking_param"": False }) with self.subTest(expect=r.regex, actual=route.regex_pattern): self.assertEqual(r.regex, route.regex_pattern) def test_custom_filter(self): custom_f = ( r"".*"", lambda _: ""to_url"", lambda _: ""to_origin"", ) Route.set_filter(""custom"", custom_f) rules = [ TestRule(""/test/"", ""/test/(?P.*)"", """", """"), ] self.test_expect_regex_rules(rules) def test_url_match(self): for r in test_rules: route = Route(r.rule, [""GET""], temp, { ""checking_param"": False }) with self.subTest(matching=r.matching): t, _ = route.match(r.matching, ""GET"") self.assertIsNotNone(t) with self.subTest(not_matching=r.not_matching): t, _ = route.match(r.not_matching, ""GET"") self.assertIsNone(t) def test_build_url(self): route = Route(""/test/"", [""GET""], temp, { ""checking_param"": False }) self.assertEqual(""/test/1"", route.build_url([1])) with self.assertRaises(ValueError): route.build_url([""error""]) ", https://github.com/avature/confight,be2d162c99cc3c709289913de137f8d8bfbd35d5,test_confight.py::TestLoad::test_it_should_load_and_merge_lists_of_paths,test_confight.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# -*- coding: utf-8 -*- import os try: import subprocess32 as subprocess except ImportError: import subprocess try: import mock except ImportError: from unittest import mock import pytest from hamcrest import (assert_that, has_entry, has_key, has_entries, is_, empty, only_contains, contains, contains_string) from confight import (parse, merge, find, load, load_paths, load_app, load_user_app, FORMATS) @pytest.fixture def examples(tmpdir): return Repository(tmpdir) FILES = [ 'basic_file.toml', 'basic_file.ini', 'basic_file.json', 'basic_file.cfg', 'basic_file.js' ] if ""yaml"" in FORMATS: FILES.extend([""basic_file.yaml"", ""basic_file.yml""]) INVALID_FILES = [ 'invalid.toml', 'invalid.ini', 'invalid.json', 'invalid.cfg', 'invalid.js' ] INVALID_EXTENSIONS = ['bad_ext.ext', 'bad_ext.j'] SORTED_FILES = ['00_base.toml', '01_first.json', 'AA_second.ini'] class TestParse(object): @pytest.mark.parametrize(""name"", FILES) def test_it_should_detect_format_from_extension(self, name, examples): config = parse(examples.get(name)) assert_that(config, has_entry('section', has_key('string'))) @pytest.mark.parametrize(""name"", FILES) def test_it_loads_strings_as_unicode(self, name, examples): config = parse(examples.get(name)) assert_that(config, has_entry( 'section', has_entry('unicode', is_(u'💩')) )) @pytest.mark.parametrize(""name, format"", [ ('basic_file_toml', 'toml'), ('basic_file_ini', 'ini'), ('basic_file_json', 'json'), ]) def test_it_should_load_for_given_format(self, name, format, examples): config = parse(examples.get(name), format) assert_that(config, has_entry('section', has_key('string'))) def test_it_should_fail_with_missing_files(self): with pytest.raises(Exception): parse('/path/to/nowhere.json') @pytest.mark.parametrize(""name"", INVALID_FILES) def test_it_should_fail_with_invalid_files(self, name, examples): with pytest.raises(Exception): parse(examples.get(name)) @pytest.mark.parametrize(""name"", INVALID_EXTENSIONS) def test_it_should_fail_with_invalid_extensions(self, name, examples): with pytest.raises(Exception): parse(examples.get(name)) class TestMerge(object): def test_it_should_give_priority_to_last_value(self): configs = [ {'key': 1}, {'key': 2}, {'key': 3}, ] result = merge(configs) assert_that(result, has_entry('key', 3)) def test_it_should_add_all_values(self): configs = [ {'section1': {'key1': 1}}, {'section2': {'key2': 2}}, {'section3': {'key3': 3}}, ] result = merge(configs) assert_that(result, has_entries({ 'section1': has_entry('key1', 1), 'section2': has_entry('key2', 2), 'section3': has_entry('key3', 3), })) def test_it_should_merge_dicts_recursively(self): configs = [ {'section': {'lv1': {'lv2': {'lv3': 1}}}}, {'section': {'lv1': {'lv2': {'lv3': 2}}}}, {'section': {'lv1': {'lv2': {'lv3': 3}}}}, ] result = merge(configs) assert_that(result, has_entry( 'section', has_entry( 'lv1', has_entry( 'lv2', has_entry( 'lv3', 3))) )) def test_it_should_ignore_scalar_values_given_as_configs(self): configs = [ {'section': {'key': 1}}, {'section': None}, {'section': {'key': 2}}, {'section': []}, {'section': {'key': 3}}, ] result = merge(configs) assert_that(result, has_entry('section', has_entry('key', 3))) class TestFind(object): def test_it_should_load_files_in_order(self, examples): examples.clear() expected_files = sorted(examples.get_many(SORTED_FILES)) found = find(str(examples.tmpdir)) assert_that(found, is_(expected_files)) def test_it_should_load_full_paths(self, examples): examples.clear() examples.get_many(SORTED_FILES) found = find(str(examples.tmpdir)) assert_that( all(os.path.isabs(path) for path in found), is_(True) ) def test_it_should_normalize_relative_paths(self): path = os.path.join('.', os.path.basename(__file__)) found = find(path) assert_that(found, contains(__file__)) def test_it_should_load_existing_files(self, examples): path = examples.get(FILES[0]) found = find(path) assert_that(found, contains(path)) def test_it_should_expand_user_variables(self, tmpdir, examples): path = examples.get(FILES[0]) home = os.path.expanduser('~') relpath = os.path.join('~', os.path.relpath(path, start=home)) found = find(relpath) assert_that(found, contains(path)) def test_it_should_return_nothing_for_missing_directories(self): assert_that(find('/path/to/nowhere'), is_(empty())) def test_it_should_ignore_invalid_files(self): found = find(None) assert_that(found, is_(empty())) def test_it_should_ignore_unreadable_files(self, examples): unreadable_path = examples.load(FILES[0]) os.chmod(unreadable_path, 0o222) found = find(unreadable_path) assert_that(found, is_(empty())) def test_it_should_ignore_unexplorable_dirs(self, tmpdir): unexplorable_dir = str(tmpdir) os.chmod(unexplorable_dir, 0o444) found = find(unexplorable_dir) assert_that(found, is_(empty())) @mock.patch('confight.logger') def test_it_should_warn_about_executable_config_files(self, logger, examples): executable_file = examples.load(FILES[0]) os.chmod(executable_file, 0o777) found = find(executable_file) assert_that(found, contains(executable_file)) logger.warning.assert_called() class TestLoad(object): def test_it_should_load_and_merge_lists_of_paths(self, examples): paths = sorted(examples.get_many(SORTED_FILES)) config = load(paths) assert_that(config, has_entry('section', has_entry('key', 'second'))) def test_it_should_load_paths_for_given_format(self, examples): paths = examples.get_many(['00_base.toml', 'basic_file_toml']) config = load(paths, format='toml') assert_that(config, has_entry('section', has_entry('key', 'basic'))) def test_it_should_use_given_parser(self): paths = ['/path/to/1', '/path/to/2'] def myparse(path, format=None): return {'path': path, 'format': format} config = load(paths, format='toml', parser=myparse) assert_that(config, has_entries({ 'path': paths[-1], 'format': 'toml' })) def test_it_should_use_given_merger(self, examples): paths = examples.get_many(SORTED_FILES) def mymerge(configs): return configs config = load(paths, merger=mymerge) assert_that(config, only_contains(has_key('section'))) class TestLoadPaths(object): def test_it_should_load_from_file_and_directory(self, examples): examples.clear() paths = sorted(examples.get_many(SORTED_FILES)) config = load_paths([paths[0], str(examples.tmpdir)]) assert_that(config, has_entry('section', has_entry('key', 'second'))) def test_it_should_filter_extensions(self, examples): examples.clear() examples.get_many(FILES) expected_contents = parse(examples.get(FILES[0])) # toml file config = load_paths( [str(examples.tmpdir)], extension='toml', force_extension=True ) # Only reads the toml file assert_that(config, is_(expected_contents)) def test_merges_must_retain_order(self, examples): examples.clear() paths = examples.get_many(FILES) config = load(paths) good_data = [ 'string', 'integer', 'float', 'boolean', 'list', 'key', 'unicode', 'subsection', 'null' ] assert_that(config[""section""].keys(), contains(*good_data)) class LoadAppBehaviour(object): def loaded_paths(self, config): return sorted(config, key=lambda k: config[k]) def call_config_loader(self, loader, *args, **kwargs): """"""Simulate config loading Result is a dictionary with all loaded d[key, int] with every loaded path the order in which they were loaded. """""" def myparser(path, format=None, _data={'n': 0}): _data['n'] += 1 return {path: _data['n']} def myfinder(path): return [path] kwargs.setdefault('parser', myparser) kwargs.setdefault('finder', myfinder) return loader(*args, **kwargs) class TestLoadApp(LoadAppBehaviour): def test_it_should_load_from_default_path(self): config = self.load_app('myapp') assert_that(self.loaded_paths(config), contains( '/etc/myapp/config.toml', '/etc/myapp/conf.d', )) def test_it_should_be_able_to_ignore_file(self): config = self.load_app('myapp', file_path=None) assert_that(self.loaded_paths(config), contains( '/etc/myapp/conf.d', )) def test_it_should_be_able_to_ignore_directory(self): config = self.load_app('myapp', dir_path=None) assert_that(self.loaded_paths(config), contains( '/etc/myapp/config.toml', )) def test_it_should_load_extra_paths(self): config = self.load_app('myapp', paths=['/extra/path']) assert_that(self.loaded_paths(config), contains( '/etc/myapp/config.toml', '/etc/myapp/conf.d', '", https://github.com/bazaar-projects/docopt-ng,bbed40a2335686d2e14ac0e6c3188374dc4784da,tests/test_docopt_ng.py::test_docopt_ng_more_magic_global_arguments_and_dot_access,tests/test_docopt_ng.py,NIO,flaky,Opened,https://github.com/jazzband/docopt-ng/pull/20,classify;root_cause,"import pytest import docopt from docopt import DocoptExit, DocoptLanguageError, Option, Argument, Command, OptionsShortcut, Required, NotRequired, parse_argv, Tokens from pytest import raises from docopt import magic from docopt import magic_docopt from docopt import docopt as user_provided_alias_containing_magic def test_docopt_ng_more_magic_spellcheck_and_expansion(): o = [Option(""-h""), Option(""-v"", ""--verbose""), Option(None, ""--file"", 1)] TS = lambda s: Tokens(s, error=DocoptExit) assert parse_argv(TS(""""), options=o) == [] assert parse_argv(TS(""-h""), options=o) == [Option(""-h"", None, 0, True)] assert parse_argv(TS(""-V""), options=o, more_magic=True) == [Option(""-v"", ""--verbose"", 0, True)] assert parse_argv(TS(""-h --File f.txt""), options=o, more_magic=True) == [Option(""-h"", None, 0, True), Option(None, ""--file"", 1, ""f.txt"")] assert parse_argv(TS(""-h --fiLe f.txt arg""), options=o, more_magic=True) == [ Option(""-h"", None, 0, True), Option(None, ""--file"", 1, ""f.txt""), Argument(None, ""arg""), ] assert parse_argv(TS(""-h -f f.txt arg arg2""), options=o, more_magic=True) == [ Option(""-h"", None, 0, True), Option(None, ""--file"", 1, ""f.txt""), Argument(None, ""arg""), Argument(None, ""arg2""), ] def test_docopt_ng_as_magic_docopt_more_magic_global_arguments_and_dot_access(): doc = """"""Usage: prog [-vqr] [FILE] prog INPUT OUTPUT prog --help Options: -v print status messages -q report only file names -r show all occurrences of the same error --help """""" global arguments magic_docopt(doc, ""-v file.py"") assert arguments == {""-v"": True, ""-q"": False, ""-r"": False, ""--help"": False, ""FILE"": ""file.py"", ""INPUT"": None, ""OUTPUT"": None} assert arguments.v == True assert arguments.FILE == ""file.py"" arguments = None magic(doc, ""-v file.py"") assert arguments == {""-v"": True, ""-q"": False, ""-r"": False, ""--help"": False, ""FILE"": ""file.py"", ""INPUT"": None, ""OUTPUT"": None} assert arguments.v == True assert arguments.FILE == ""file.py"" arguments = None user_provided_alias_containing_magic(doc, ""-v file.py"") assert arguments == {""-v"": True, ""-q"": False, ""-r"": False, ""--help"": False, ""FILE"": ""file.py"", ""INPUT"": None, ""OUTPUT"": None} def test_docopt_ng_more_magic_no_make_global_arguments_if_assigned(): doc = """"""Usage: prog [-vqr] [FILE] prog INPUT OUTPUT prog --help Options: -v print status messages -q report only file names -r show all occurrences of the same error --help """""" global arguments global arguments arguments = None opts = magic_docopt(doc, argv=""-v file.py"") assert arguments == None assert opts == {""-v"": True, ""-q"": False, ""-r"": False, ""--help"": False, ""FILE"": ""file.py"", ""INPUT"": None, ""OUTPUT"": None} assert opts.v == True assert opts.FILE == ""file.py"" def test_docopt_ng_more_magic_global_arguments_and_dot_access(): doc = """"""Usage: prog [-vqr] [FILE] prog INPUT OUTPUT prog --help Options: -v print status messages -q report only file names -r show all occurrences of the same error --help """""" global arguments docopt.docopt(doc, ""-v file.py"", more_magic=True) assert arguments == {""-v"": True, ""-q"": False, ""-r"": False, ""--help"": False, ""FILE"": ""file.py"", ""INPUT"": None, ""OUTPUT"": None} assert arguments.v == True assert arguments.FILE == ""file.py"" arguments = None docopt.docopt(doc.replace(""FILE"", """"), ""-v"", more_magic=True) str_arguments = str(arguments) assert arguments == {""-v"": True, ""-q"": False, ""-r"": False, ""--help"": False, """": None, ""INPUT"": None, ""OUTPUT"": None} assert arguments.FILE == None with raises(DocoptExit): docopt.docopt(doc, ""-v input.py output.py"") with raises(DocoptExit): docopt.docopt(doc, ""--fake"") def test_docopt_ng__doc__if_no_doc(): import sys __doc__, sys.argv = ""usage: prog --long="", [None, ""--long=""] assert docopt.docopt() == {""--long"": """"} __doc__, sys.argv = ""usage:\n\tprog -l \noptions:\n\t-l \n"", [None, ""-l"", """"] assert docopt.docopt() == {""-l"": """"} __doc__, sys.argv = None, [None, ""-l"", """"] with raises(DocoptLanguageError): docopt.docopt() def test_docopt_ng_negative_float(): args = docopt.docopt(""usage: prog --negative_pi=NEGPI NEGTAU"", ""--negative_pi -3.14 -6.28"") assert args == {""--negative_pi"": ""-3.14"", ""NEGTAU"": ""-6.28""} def test_docopt_ng_doubledash_version(): with pytest.raises(SystemExit) as pytest_wrapped_e: args = docopt.docopt(""usage: prog"", version=1, argv=""prog --version"") assert pytest_wrapped_e.type == SystemExit def test_docopt_ng__doc__if_no_doc_indirection(): import sys __doc__, sys.argv = ""usage: prog --long="", [None, ""--long=""] def test_indirect(): return docopt.docopt() assert test_indirect() == {""--long"": """"} def test_even_more_indirect(): return test_indirect() assert test_even_more_indirect() == {""--long"": """"} ", https://github.com/benjaminp/six,c0be8815d13df45b6ae471c4c436cce8c192245d,test_six.py::test_lazy,test_six.py,NIO,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2010-2020 Benjamin Peterson # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the ""Software""), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import operator import sys import types import unittest import abc import pytest import six def test_add_doc(): def f(): """"""Icky doc"""""" pass six._add_doc(f, """"""New doc"""""") assert f.__doc__ == ""New doc"" def test_import_module(): from logging import handlers m = six._import_module(""logging.handlers"") assert m is handlers def test_integer_types(): assert isinstance(1, six.integer_types) assert isinstance(-1, six.integer_types) assert isinstance(six.MAXSIZE + 23, six.integer_types) assert not isinstance(.1, six.integer_types) def test_string_types(): assert isinstance(""hi"", six.string_types) assert isinstance(six.u(""hi""), six.string_types) assert issubclass(six.text_type, six.string_types) def test_class_types(): class X: pass class Y(object): pass assert isinstance(X, six.class_types) assert isinstance(Y, six.class_types) assert not isinstance(X(), six.class_types) def test_text_type(): assert type(six.u(""hi"")) is six.text_type def test_binary_type(): assert type(six.b(""hi"")) is six.binary_type def test_MAXSIZE(): try: # This shouldn't raise an overflow error. six.MAXSIZE.__index__() except AttributeError: # Before Python 2.6. pass pytest.raises( (ValueError, OverflowError), operator.mul, [None], six.MAXSIZE + 1) def test_lazy(): if six.PY3: html_name = ""html.parser"" else: html_name = ""HTMLParser"" assert html_name not in sys.modules mod = six.moves.html_parser assert sys.modules[html_name] is mod assert ""htmlparser"" not in six._MovedItems.__dict__ try: import _tkinter except ImportError: have_tkinter = False else: have_tkinter = True have_gdbm = True try: import gdbm except ImportError: try: import dbm.gnu except ImportError: have_gdbm = False @pytest.mark.parametrize(""item_name"", [item.name for item in six._moved_attributes]) def test_move_items(item_name): """"""Ensure that everything loads correctly."""""" try: item = getattr(six.moves, item_name) if isinstance(item, types.ModuleType): __import__(""six.moves."" + item_name) except ImportError: if item_name == ""winreg"" and not sys.platform.startswith(""win""): pytest.skip(""Windows only module"") if item_name.startswith(""tkinter""): if not have_tkinter: pytest.skip(""requires tkinter"") if item_name.startswith(""dbm_gnu"") and not have_gdbm: pytest.skip(""requires gdbm"") raise assert item_name in dir(six.moves) @pytest.mark.parametrize(""item_name"", [item.name for item in six._urllib_parse_moved_attributes]) def test_move_items_urllib_parse(item_name): """"""Ensure that everything loads correctly."""""" assert item_name in dir(six.moves.urllib.parse) getattr(six.moves.urllib.parse, item_name) @pytest.mark.parametrize(""item_name"", [item.name for item in six._urllib_error_moved_attributes]) def test_move_items_urllib_error(item_name): """"""Ensure that everything loads correctly."""""" assert item_name in dir(six.moves.urllib.error) getattr(six.moves.urllib.error, item_name) @pytest.mark.parametrize(""item_name"", [item.name for item in six._urllib_request_moved_attributes]) def test_move_items_urllib_request(item_name): """"""Ensure that everything loads correctly."""""" assert item_name in dir(six.moves.urllib.request) getattr(six.moves.urllib.request, item_name) @pytest.mark.parametrize(""item_name"", [item.name for item in six._urllib_response_moved_attributes]) def test_move_items_urllib_response(item_name): """"""Ensure that everything loads correctly."""""" assert item_name in dir(six.moves.urllib.response) getattr(six.moves.urllib.response, item_name) @pytest.mark.parametrize(""item_name"", [item.name for item in six._urllib_robotparser_moved_attributes]) def test_move_items_urllib_robotparser(item_name): """"""Ensure that everything loads correctly."""""" assert item_name in dir(six.moves.urllib.robotparser) getattr(six.moves.urllib.robotparser, item_name) def test_import_moves_error_1(): from six.moves.urllib.parse import urljoin from six import moves # In 1.4.1: AttributeError: 'Module_six_moves_urllib_parse' object has no attribute 'urljoin' assert moves.urllib.parse.urljoin def test_import_moves_error_2(): from six import moves assert moves.urllib.parse.urljoin # In 1.4.1: ImportError: cannot import name urljoin from six.moves.urllib.parse import urljoin def test_import_moves_error_3(): from six.moves.urllib.parse import urljoin # In 1.4.1: ImportError: cannot import name urljoin from six.moves.urllib_parse import urljoin def test_from_imports(): from six.moves.queue import Queue assert isinstance(Queue, six.class_types) from six.moves.configparser import ConfigParser assert isinstance(ConfigParser, six.class_types) def test_filter(): from six.moves import filter f = filter(lambda x: x % 2, range(10)) assert six.advance_iterator(f) == 1 def test_filter_false(): from six.moves import filterfalse f = filterfalse(lambda x: x % 3, range(10)) assert six.advance_iterator(f) == 0 assert six.advance_iterator(f) == 3 assert six.advance_iterator(f) == 6 def test_map(): from six.moves import map assert six.advance_iterator(map(lambda x: x + 1, range(2))) == 1 def test_getoutput(): from six.moves import getoutput output = getoutput('echo ""foo""') assert output == 'foo' def test_zip(): from six.moves import zip assert six.advance_iterator(zip(range(2), range(2))) == (0, 0) def test_zip_longest(): from six.moves import zip_longest it = zip_longest(range(2), range(1)) assert six.advance_iterator(it) == (0, 0) assert six.advance_iterator(it) == (1, None) class TestCustomizedMoves: def teardown_method(self, meth): try: del six._MovedItems.spam except AttributeError: pass try: del six.moves.__dict__[""spam""] except KeyError: pass def test_moved_attribute(self): attr = six.MovedAttribute(""spam"", ""foo"", ""bar"") if six.PY3: assert attr.mod == ""bar"" else: assert attr.mod == ""foo"" assert attr.attr == ""spam"" attr = six.MovedAttribute(""spam"", ""foo"", ""bar"", ""lemma"") assert attr.attr == ""lemma"" attr = six.MovedAttribute(""spam"", ""foo"", ""bar"", ""lemma"", ""theorm"") if six.PY3: assert attr.attr == ""theorm"" else: assert attr.attr == ""lemma"" def test_moved_module(self): attr = six.MovedModule(""spam"", ""foo"") if six.PY3: assert attr.mod == ""spam"" else: assert attr.mod == ""foo"" attr = six.MovedModule(""spam"", ""foo"", ""bar"") if six.PY3: assert attr.mod == ""bar"" else: assert attr.mod == ""foo"" def test_custom_move_module(self): attr = six.MovedModule(""spam"", ""six"", ""six"") six.add_move(attr) six.remove_move(""spam"") assert not hasattr(six.moves, ""spam"") attr = six.MovedModule(""spam"", ""six"", ""six"") six.add_move(attr) from six.moves import spam assert spam is six six.remove_move(""spam"") assert not hasattr(six.moves, ""spam"") def test_custom_move_attribute(self): attr = six.MovedAttribute(""spam"", ""six"", ""six"", ""u"", ""u"") six.add_move(attr) six.remove_move(""spam"") assert not hasattr(six.moves, ""spam"") attr = six.MovedAttribute(""spam"", ""six"", ""six"", ""u"", ""u"") six.add_move(attr) from six.moves import spam assert spam is six.u six.remove_move(""spam"") assert not hasattr(six.moves, ""spam"") def test_empty_remove(self): pytest.raises(AttributeError, six.remove_move, ""eggs"") def test_get_unbound_function(): class X(object): def m(self): pass assert six.get_unbound_function(X.m) is X.__dict__[""m""] def test_get_method_self(): class X(object): def m(self): pass x = X() assert six.get_method_self(x.m) is x pytest.raises(AttributeError, six.get_method_self, 42) def test_get_method_function(): class X(object): def m(self): pass x = X() assert six.get_method_function(x.m) is X.__dict__[""m""] pytest.raises(AttributeError, six.get_method_function, hasattr) def test_get_function_closure(): def f(): x = 42 d", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testGetAvailableAnimations,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testGetAvailableHandlers,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testGetAvailableMotorRegisters,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testGetAvailableSounds,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testGetMotorRegister,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testGetMotorRegisterRange,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testMoveMotorInDirection,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testMoveMotorInTime,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testMoveMotorToPosition,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testPauseAnimation,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testPauseAudio,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testPlayAnimation,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testPlayAudio,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testResumeAnimation,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testResumeAudio,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testSetMotorRegister,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testStopAnimation,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_http_test.py::TestHttpClientApiMethods::testStopAudio,butter/mas/tests/clients/client_http_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import HttpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestHttpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = HttpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testGetAvailableAnimations,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testGetAvailableHandlers,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testGetAvailableMotorRegisters,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testGetAvailableSounds,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testGetMotorRegister,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testGetMotorRegisterRange,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testMoveMotorInDirection,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testMoveMotorInTime,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testMoveMotorToPosition,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testPauseAnimation,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testPauseAudio,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testPlayAnimation,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testPlayAudio,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testResumeAnimation,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testResumeAudio,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testSetMotorRegister,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testStopAnimation,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_tcp_test.py::TestTcpClientApiMethods::testStopAudio,butter/mas/tests/clients/client_tcp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import TcpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestTcpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = TcpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testGetAvailableAnimations,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testGetAvailableHandlers,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testGetAvailableMotorRegisters,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testGetAvailableSounds,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testGetMotorRegister,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testMoveMotorInDirection,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testMoveMotorInTime,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testMoveMotorToPosition,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testPauseAnimation,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testPauseAudio,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testPlayAnimation,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testPlayAudio,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testResumeAnimation,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testResumeAudio,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testSetMotorRegister,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testStopAnimation,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bennymeg/Butter.MAS.PythonAPI,f86ebe75df3826f62a268645cdbe4400b43fab07,butter/mas/tests/clients/client_udp_test.py::TestUdpClientApiMethods::testStopAudio,butter/mas/tests/clients/client_udp_test.py,NOD,flaky,nan,nan,classify;root_cause,"from butter.mas.api import UdpClient from butter.mas.tests.clients.client_test import TestClientApiMethods class TestUdpClientApiMethods(TestClientApiMethods): @classmethod def setUpClass(self): self.client = UdpClient('localhost') ", https://github.com/bmweiner/skillful,8646f54faf62cb63f165f7699b8ace5b4a08233c,skillful/tests/test_controller.py::TestSkill::test_process,skillful/tests/test_controller.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"""""""skillful controller module tests"""""" # pylint: disable=protected-access # pylint: disable=too-few-public-methods # pylint: disable=unused-argument # pylint: disable=unused-variable import unittest from skillful import controller from skillful import interface from skillful import validate from skillful.tests import data class TestSkill(unittest.TestCase): """"""Test Skill class."""""" @classmethod def setUpClass(cls): cls.skill = controller.Skill() cls.skill_app_id = controller.Skill('12345') def test___init__(self): """"""Test __init__ method."""""" self.assertIsInstance(self.skill.valid, validate.Valid) self.assertIsInstance(self.skill.request, interface.RequestBody) self.assertIsInstance(self.skill.response, interface.ResponseBody) self.assertIsInstance(self.skill.logic, dict) self.assertTrue(callable(self.skill.launch)) self.assertTrue(callable(self.skill.intent)) self.assertTrue(callable(self.skill.session_ended)) def test_register(self): """"""Test register method."""""" @self.skill.register('test_logic') def sample_func(): """"""Decorated function."""""" return True self.assertTrue(self.skill.logic['test_logic']()) def test_register_context_error(self): """"""Test register method for context error."""""" @self.skill.register('test_logic') def sample_func(): """"""Decorated function."""""" pass self.skill.logic['test_logic']() self.assertRaises(RuntimeError, sample_func) def test_pass_attributes(self): """"""Test pass_attributes method."""""" self.skill.request.session.attributes = {'name': 'skill', 'age': 10} self.skill.pass_session_attributes() actual = self.skill.response.sessionAttributes self.assertEqual(actual['name'], 'skill') self.assertEqual(actual['age'], 10) def test_terminate(self): """"""Test terminate method."""""" self.skill.terminate() self.assertTrue(self.skill.response.response.shouldEndSession) def test_dispatch_launch(self): """"""Test dispath method for launch."""""" @self.skill.launch def sample_func(): """"""Decorated function."""""" self.skill.response.sessionAttributes['run'] = True self.skill.request.request.type = 'LaunchRequest' self.skill.dispatch() self.assertTrue(self.skill.response.sessionAttributes['run']) def test_dispatch_intent(self): """"""Test dispath method for intent."""""" @self.skill.intent('test_intent') def sample_func(): """"""Decorated function."""""" self.skill.response.sessionAttributes['run'] = True self.skill.request.request.type = 'IntentRequest' self.skill.request.request.intent = interface.Intent() self.skill.request.request.intent.name = 'test_intent' self.skill.dispatch() self.assertTrue(self.skill.response.sessionAttributes['run']) def test_dispatch_session_end(self): """"""Test dispath method for session_end."""""" @self.skill.session_ended def sample_func(): """"""Decorated function."""""" self.skill.response.sessionAttributes['run'] = True self.skill.request.request.type = 'SessionEndedRequest' self.skill.dispatch() self.assertTrue(self.skill.response.sessionAttributes['run']) def test_dispatch_missing(self): """"""Test dispath method for missing."""""" self.skill.logic = {} self.assertRaises(KeyError, self.skill.dispatch) def test_process(self): """"""Test process method."""""" self.skill.logic = {} @self.skill.launch def sample_func(): """"""Decorated function."""""" self.skill.response.set_speech_text('Welcome to skillful.') self.skill.response.set_reprompt_ssml('Hello.') actual = self.skill.process(data.SAMPLE_LAUNCH_REQUEST) self.assertRegexpMatches(actual, '""version"": ""1.0""') self.assertRegexpMatches(actual, '""text"": ""Welcome to skillful.""') self.assertRegexpMatches(actual, '""shouldEndSession"": false') self.assertRegexpMatches(actual, '""ssml"": ""Hello.""') def test_process_invalid1(self): """"""Test process method for invalid application id."""""" self.skill.logic = {} self.skill.valid.app_id = '12345' @self.skill.launch def sample_func(): """"""Decorated function."""""" pass self.skill.logic['LaunchRequest']() self.assertFalse(self.skill.process(data.SAMPLE_LAUNCH_REQUEST)) def test_process_end(self): """"""Test process method for invalid."""""" self.skill.logic = {} @self.skill.session_ended def sample_func(): """"""Decorated function."""""" pass actual = self.skill.process(data.SAMPLE_SESSION_ENDED_REQUEST) expected = '""response"": {""shouldEndSession"": true}' self.assertRegexpMatches(actual, expected) ", https://github.com/bmweiner/skillful,8646f54faf62cb63f165f7699b8ace5b4a08233c,skillful/tests/test_controller.py::TestSkill::test_process_end,skillful/tests/test_controller.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"""""""skillful controller module tests"""""" # pylint: disable=protected-access # pylint: disable=too-few-public-methods # pylint: disable=unused-argument # pylint: disable=unused-variable import unittest from skillful import controller from skillful import interface from skillful import validate from skillful.tests import data class TestSkill(unittest.TestCase): """"""Test Skill class."""""" @classmethod def setUpClass(cls): cls.skill = controller.Skill() cls.skill_app_id = controller.Skill('12345') def test___init__(self): """"""Test __init__ method."""""" self.assertIsInstance(self.skill.valid, validate.Valid) self.assertIsInstance(self.skill.request, interface.RequestBody) self.assertIsInstance(self.skill.response, interface.ResponseBody) self.assertIsInstance(self.skill.logic, dict) self.assertTrue(callable(self.skill.launch)) self.assertTrue(callable(self.skill.intent)) self.assertTrue(callable(self.skill.session_ended)) def test_register(self): """"""Test register method."""""" @self.skill.register('test_logic') def sample_func(): """"""Decorated function."""""" return True self.assertTrue(self.skill.logic['test_logic']()) def test_register_context_error(self): """"""Test register method for context error."""""" @self.skill.register('test_logic') def sample_func(): """"""Decorated function."""""" pass self.skill.logic['test_logic']() self.assertRaises(RuntimeError, sample_func) def test_pass_attributes(self): """"""Test pass_attributes method."""""" self.skill.request.session.attributes = {'name': 'skill', 'age': 10} self.skill.pass_session_attributes() actual = self.skill.response.sessionAttributes self.assertEqual(actual['name'], 'skill') self.assertEqual(actual['age'], 10) def test_terminate(self): """"""Test terminate method."""""" self.skill.terminate() self.assertTrue(self.skill.response.response.shouldEndSession) def test_dispatch_launch(self): """"""Test dispath method for launch."""""" @self.skill.launch def sample_func(): """"""Decorated function."""""" self.skill.response.sessionAttributes['run'] = True self.skill.request.request.type = 'LaunchRequest' self.skill.dispatch() self.assertTrue(self.skill.response.sessionAttributes['run']) def test_dispatch_intent(self): """"""Test dispath method for intent."""""" @self.skill.intent('test_intent') def sample_func(): """"""Decorated function."""""" self.skill.response.sessionAttributes['run'] = True self.skill.request.request.type = 'IntentRequest' self.skill.request.request.intent = interface.Intent() self.skill.request.request.intent.name = 'test_intent' self.skill.dispatch() self.assertTrue(self.skill.response.sessionAttributes['run']) def test_dispatch_session_end(self): """"""Test dispath method for session_end."""""" @self.skill.session_ended def sample_func(): """"""Decorated function."""""" self.skill.response.sessionAttributes['run'] = True self.skill.request.request.type = 'SessionEndedRequest' self.skill.dispatch() self.assertTrue(self.skill.response.sessionAttributes['run']) def test_dispatch_missing(self): """"""Test dispath method for missing."""""" self.skill.logic = {} self.assertRaises(KeyError, self.skill.dispatch) def test_process(self): """"""Test process method."""""" self.skill.logic = {} @self.skill.launch def sample_func(): """"""Decorated function."""""" self.skill.response.set_speech_text('Welcome to skillful.') self.skill.response.set_reprompt_ssml('Hello.') actual = self.skill.process(data.SAMPLE_LAUNCH_REQUEST) self.assertRegexpMatches(actual, '""version"": ""1.0""') self.assertRegexpMatches(actual, '""text"": ""Welcome to skillful.""') self.assertRegexpMatches(actual, '""shouldEndSession"": false') self.assertRegexpMatches(actual, '""ssml"": ""Hello.""') def test_process_invalid1(self): """"""Test process method for invalid application id."""""" self.skill.logic = {} self.skill.valid.app_id = '12345' @self.skill.launch def sample_func(): """"""Decorated function."""""" pass self.skill.logic['LaunchRequest']() self.assertFalse(self.skill.process(data.SAMPLE_LAUNCH_REQUEST)) def test_process_end(self): """"""Test process method for invalid."""""" self.skill.logic = {} @self.skill.session_ended def sample_func(): """"""Decorated function."""""" pass actual = self.skill.process(data.SAMPLE_SESSION_ENDED_REQUEST) expected = '""response"": {""shouldEndSession"": true}' self.assertRegexpMatches(actual, expected) ", https://github.com/bpepple/metron-tagger,2be176293218fa101dd84c95c72b8ae646b2731a,tests/test_taggerlib_filesorter.py::test_sort_comic,tests/test_taggerlib_filesorter.py,NIO,flaky,Accepted,https://github.com/bpepple/metron-tagger/pull/46,classify;root_cause;fix_proposal,"from darkseid.comicarchive import ComicArchive from metrontagger.taggerlib.filesorter import FileSorter def test_sort_comic(fake_comic, fake_metadata, tmp_path): test_dir = tmp_path / ""sort1"" test_dir.mkdir() result_dir = ( test_dir / fake_metadata.publisher / fake_metadata.series / f""v{fake_metadata.volume}"" ) # Write metadata to fake file comic = ComicArchive(fake_comic) comic.write_metadata(fake_metadata) file_sorter = FileSorter(test_dir) res = file_sorter.sort_comics(fake_comic) assert result_dir.is_dir() assert res is True def test_sort_files_without_metadata(fake_comic, tmp_path): test_dir = tmp_path / ""sort2"" test_dir.mkdir() # If we add more tests we should probably create another tmpfile # since we are removing the metadata from the tmpfile comic = ComicArchive(fake_comic) comic.remove_metadata() file_sorter = FileSorter(test_dir) res = file_sorter.sort_comics(fake_comic) assert res is False ", https://github.com/Burnysc2/python-sc2,78d2ebe5c87aa9abc1e8a505f095ccd5e9dec358,test/test_pickled_data.py::test_bot_ai,test/test_pickled_data.py,NIO,flaky,Accepted,https://github.com/BurnySc2/python-sc2/pull/109,classify;root_cause;fix_proposal,"import sys, os sys.path.append(os.path.join(os.path.dirname(__file__), "".."")) from sc2.game_data import GameData from sc2.game_info import GameInfo from sc2.game_info import Ramp from sc2.game_state import GameState from sc2.bot_ai import BotAI from sc2.units import Units from sc2.unit import Unit from sc2.position import Point2, Point3, Size, Rect from sc2.game_data import Cost from sc2.ids.unit_typeid import UnitTypeId from sc2.ids.upgrade_id import UpgradeId from sc2.ids.ability_id import AbilityId from sc2.ids.buff_id import BuffId from sc2.ids.effect_id import EffectId from sc2.data import Race import pickle, pytest, random, math, lzma from hypothesis import given, event, settings, strategies as st from typing import Iterable import time """""" You can execute this test running the following command from the root python-sc2 folder: pipenv run pytest test/test_pickled_data.py This test/script uses the pickle files located in ""python-sc2/test/pickle_data"" generated by ""generate_pickle_files_bot.py"" file, which is a bot that starts a game on each of the maps defined in the main function. It will load the pickle files, recreate the bot object from scratch and tests most of the bot properties and functions. All functions that require some kind of query or interaction with the API directly will have to be tested in the ""autotest_bot.py"" in a live game. """""" from sc2.constants import TARGET_GROUND, TARGET_AIR, TARGET_BOTH, IS_LIGHT, IS_ARMORED, IS_MECHANICAL, IS_BIOLOGICAL from sc2.data import TargetType from sc2.data import Attribute def get_map_specific_bots() -> Iterable[BotAI]: folder = os.path.dirname(__file__) subfolder_name = ""pickle_data"" pickle_folder_path = os.path.join(folder, subfolder_name) files = os.listdir(pickle_folder_path) for file in (f for f in files if f.endswith("".xz"")): with lzma.open(os.path.join(folder, subfolder_name, file), ""rb"") as f: raw_game_data, raw_game_info, raw_observation = pickle.load(f) # Build fresh bot object, and load the pickle'd data into the bot object bot = BotAI() game_data = GameData(raw_game_data.data) game_info = GameInfo(raw_game_info.game_info) game_state = GameState(raw_observation) bot._initialize_variables() bot._prepare_start(client=None, player_id=1, game_info=game_info, game_data=game_data) bot._prepare_step(state=game_state, proto_game_info=raw_game_info) yield bot # Global bot object that is used in TestClass.test_position_* bot_object_generator = get_map_specific_bots() random_bot_object: BotAI = next(bot_object_generator) # Always pick acropolis to get same test results # while random_bot_object.game_info.map_name != ""Acropolis LE"": # random_bot_object = next(bot_object_generator) def test_bot_ai(): bot: BotAI = random_bot_object # Test initial bot attributes at game start # Properties from _prepare_start assert 1 <= bot.player_id <= 2 assert isinstance(bot.race, Race) assert isinstance(bot.enemy_race, Race) # Properties from _prepare_step assert bot.units.amount == bot.workers.amount assert bot.structures.amount == bot.townhalls.amount assert bot.workers.amount == 12 assert bot.townhalls.amount == 1 assert bot.gas_buildings.amount == 0 assert bot.minerals == 50 assert bot.vespene == 0 assert bot.supply_army == 0 assert bot.supply_workers == 12 assert bot.supply_cap == 15 assert bot.supply_used == 12 assert bot.supply_left == 3 assert bot.idle_worker_count == 0 assert bot.army_count == 0 # Test properties updated by ""_prepare_units"" function assert not bot.blips assert bot.units assert bot.structures assert not bot.enemy_units assert not bot.enemy_structures assert bot.mineral_field assert bot.vespene_geyser assert bot.resources assert len(bot.destructables) >= 0 assert isinstance(bot.destructables, (list, set, dict)) assert len(bot.watchtowers) >= 0 assert bot.all_units assert bot.workers assert bot.townhalls assert not bot.gas_buildings # Test bot_ai functions assert bot.time == 0 assert bot.time_formatted in {""0:00"", ""00:00""} assert bot.start_location is None # Is populated by main.py bot._game_info.player_start_location = bot.townhalls.random.position assert bot.townhalls.random.position not in bot.enemy_start_locations assert bot.enemy_units == Units([], bot) assert bot.enemy_structures == Units([], bot) bot._game_info.map_ramps, bot._game_info.vision_blockers = bot._game_info._find_ramps_and_vision_blockers() assert bot.main_base_ramp # Test if any ramp was found # The following functions need to be tested by autotest_bot.py because they use API query which isn't available here as this file only uses the pickle files and is not able to interact with the API as SC2 is not running while this test runs assert bot.can_feed(UnitTypeId.MARINE) assert bot.can_feed(UnitTypeId.SIEGETANK) assert not bot.can_feed(UnitTypeId.THOR) assert not bot.can_feed(UnitTypeId.BATTLECRUISER) assert not bot.can_feed(UnitTypeId.IMMORTAL) assert bot.can_afford(UnitTypeId.ZERGLING) assert bot.can_afford(UnitTypeId.MARINE) assert bot.can_afford(UnitTypeId.SCV) assert bot.can_afford(UnitTypeId.DRONE) assert bot.can_afford(UnitTypeId.PROBE) assert bot.can_afford(AbilityId.COMMANDCENTERTRAIN_SCV) assert bot.can_afford(UnitTypeId.MARINE) assert not bot.can_afford(UnitTypeId.SIEGETANK) assert not bot.can_afford(UnitTypeId.BATTLECRUISER) assert not bot.can_afford(UnitTypeId.MARAUDER) assert not bot.can_afford(UpgradeId.WARPGATERESEARCH) assert not bot.can_afford(AbilityId.RESEARCH_WARPGATE) # Store old values for minerals, vespene old_values = bot.minerals, bot.vespene, bot.supply_cap, bot.supply_left, bot.supply_used bot.vespene = 50 assert bot.can_afford(UpgradeId.WARPGATERESEARCH) assert bot.can_afford(AbilityId.RESEARCH_WARPGATE) bot.minerals = 150 bot.supply_cap = 15 bot.supply_left = -1 bot.supply_used = 16 # Confirm that units that don't cost supply can be built while at negative supply using can_afford function assert bot.can_afford(UnitTypeId.GATEWAY) assert bot.can_afford(UnitTypeId.PYLON) assert bot.can_afford(UnitTypeId.OVERLORD) assert bot.can_afford(UnitTypeId.BANELING) assert not bot.can_afford(UnitTypeId.ZERGLING) assert not bot.can_afford(UnitTypeId.MARINE) bot.minerals, bot.vespene, bot.supply_cap, bot.supply_left, bot.supply_used = old_values worker = bot.workers.random assert bot.select_build_worker(worker.position) == worker for w in bot.workers: if w == worker: continue assert bot.select_build_worker(w.position) != worker assert bot.already_pending_upgrade(UpgradeId.STIMPACK) == 0 assert bot.already_pending(UpgradeId.STIMPACK) == 0 assert bot.already_pending(UnitTypeId.SCV) == 0 assert 0 < bot.get_terrain_height(worker) assert bot.in_placement_grid(worker) assert bot.in_pathing_grid(worker) # The pickle data was created by a terran bot, so there is no creep under any worker assert not bot.has_creep(worker) # Why did this stop working, not visible on first frame? assert bot.is_visible(worker), f""Visibility value at worker is {bot.state.visibility[worker.position.rounded]}"" # Check price for morphing units and upgrades cost_100 = [ AbilityId.ARMORYRESEARCH_TERRANSHIPWEAPONSLEVEL1, UpgradeId.TERRANSHIPWEAPONSLEVEL1, AbilityId.ARMORYRESEARCH_TERRANVEHICLEPLATINGLEVEL1, UpgradeId.TERRANVEHICLEARMORSLEVEL1, AbilityId.ARMORYRESEARCH_TERRANVEHICLEWEAPONSLEVEL1, UpgradeId.TERRANVEHICLEWEAPONSLEVEL1, AbilityId.ENGINEERINGBAYRESEARCH_TERRANINFANTRYARMORLEVEL1, UpgradeId.TERRANINFANTRYARMORSLEVEL1, AbilityId.ENGINEERINGBAYRESEARCH_TERRANINFANTRYWEAPONSLEVEL1, UpgradeId.TERRANINFANTRYWEAPONSLEVEL1, AbilityId.RESEARCH_ZERGMELEEWEAPONSLEVEL1, UpgradeId.ZERGMELEEWEAPONSLEVEL1, AbilityId.RESEARCH_ZERGMISSILEWEAPONSLEVEL1, UpgradeId.ZERGMISSILEWEAPONSLEVEL1, AbilityId.FORGERESEARCH_PROTOSSGROUNDARMORLEVEL1, UpgradeId.PROTOSSGROUNDARMORSLEVEL1, AbilityId.FORGERESEARCH_PROTOSSGROUNDWEAPONSLEVEL1, UpgradeId.PROTOSSGROUNDWEAPONSLEVEL1, AbilityId.ENGINEERINGBAYRESEARCH_TERRANINFANTRYWEAPONSLEVEL1, UpgradeId.TERRANINFANTRYWEAPONSLEVEL1, AbilityId.ENGINEERINGBAYRESEARCH_TERRANINFANTRYWEAPONSLEVEL1, UpgradeId.TERRANINFANTRYWEAPONSLEVEL1, AbilityId.RESEARCH_ZERGFLYERATTACKLEVEL1, UpgradeId.ZERGFLYERWEAPONSLEVEL1, AbilityId.CYBERNETICSCORERESEARCH_PROTOSSAIRWEAPONSLEVEL1, UpgradeId.PROTOSSAIRWEAPONSLEVEL1, ] cost_175 = [ AbilityId.ARMORYRESEARCH_TERRANSHIPWEAPONSLEVEL2, UpgradeId.TERRANSHIPWEAPONSLEVEL2, AbilityId.ARMORYRESEARCH_TERRANVEHICLEPLATINGLEVEL2, UpgradeId.TERRANVEHICLEARMORSLEVEL2, AbilityId.ARMORYRESEARCH_TERRANVEHICLEWEAPONSLEVEL2, UpgradeId.TERRANVEHICLEWEAPONSLEVEL2, AbilityId.ENGINEERINGBAYRESEARCH_TERRANINFANTRYARMORLEVEL2, UpgradeId.TERRANINFANTRYARMORSLEVEL2, AbilityId.ENGINEERINGBAYRESEARCH_TERRANINFANTRYWEAPONSLEVEL2, UpgradeId.TERRANINFANTRYWEAPONSLEVEL2, AbilityId.ENGINEERINGBAYRESEARCH_TERRANINFANTRYWEAPONSLEVEL2, UpgradeId.TERRANINFANTRYWEAPONSLEVEL2, AbilityId.ENGINEERINGBAYRESEARCH_TERRANINFANTRYWEAPONSLEVEL2, UpgradeId.TERRANINFANTRYWEAPONSLEVEL2, AbilityId.RESEARCH_ZERGFLYERATTACKLEVEL2, UpgradeId.ZERGFLYERWEAPONSLEVEL2, AbilityId.CYBERNETICSCORERESEARCH_PROTOSSAIRWEAPONSLEVEL2, UpgradeId.PROTOSSAIRWEAPONSLEVEL2, ] cost_200 = [ AbilityId.RES", https://github.com/bwohlberg/sporco,efd9237ec0d254e2ff630ad0ac17eb8dc566c586,tests/test_plot.py::TestSet01::test_09,tests/test_plot.py,OD-Vic,flaky,nan,nan,classify;root_cause,"from __future__ import division from builtins import object import pytest import matplotlib matplotlib.use('Agg') import numpy as np from sporco import plot from sporco import util # Monkey patch in_ipython and in_notebook functions to allow testing of # functions that depend on these tests def in_ipython(): return True util.in_ipython = in_ipython def in_notebook(): return True util.in_notebook = in_notebook # Dummy get_ipython function to allow testing of code segments that # are only intended to be run within ipython or a notebook def get_ipython(): class ipython_dummy(object): def run_line_magic(*args): pass return ipython_dummy() plot.get_ipython = get_ipython @pytest.mark.filterwarnings('ignore:matplotlib is currently using') class TestSet01(object): def setup_method(self, method): self.x = np.linspace(-1, 1, 20)[np.newaxis, :] self.y = np.linspace(-1, 1, 20)[:, np.newaxis] self.z = np.sqrt(self.x**2 + self.y**2) def test_01(self): x = np.linspace(-1, 1, 20) y = x**2 plot.plot(y, title='Plot Test', xlbl='x', ylbl='y', lgnd=('Legend')) plot.close() def test_02(self): x = np.linspace(-1, 1, 20) y = x**2 fig = plot.figure() plot.plot(y, x=x, title='Plot Test', xlbl='x', ylbl='y', fig=fig) plot.close() def test_03(self): fig, ax = plot.subplots(nrows=1, ncols=1) plot.surf(self.z, title='Surf Test', xlbl='x', ylbl='y', zlbl='z', elev=0.0, fig=fig, ax=ax) plot.close() def test_04(self): fig = plot.figure() plot.surf(self.z, x=self.x, y=self.y, title='Surf Test', xlbl='x', ylbl='y', zlbl='z', cntr=5, fig=fig) plot.close() def test_05(self): plot.contour(self.z, x=self.x, y=self.y, title='Contour Test', xlbl='x', ylbl='y') plot.close() def test_06(self): fig = plot.figure() plot.contour(self.z, title='Contour Test', xlbl='x', ylbl='y', fig=fig) plot.close() def test_07(self): plot.imview(self.z.astype(np.float16), title='Imview Test', cbar=True) plot.close() def test_08(self): fig = plot.figure() plot.imview(self.z, title='Imview Test', fltscl=True, fig=fig) plot.close() def test_09(self): fg, ax = plot.imview(self.z, title='Imview Test', fltscl=True, cbar=None) ax.format_coord(0, 0) plot.close(fg) def test_10(self): fig = plot.figure() plot.imview((100.0*self.z).astype(np.int16), title='Imview Test', fltscl=True, fig=fig) plot.close() def test_11(self): fig = plot.figure() plot.imview((100.0*self.z).astype(np.uint16), title='Imview Test', fltscl=True, fig=fig) plot.close() def test_12(self): z3 = np.dstack((self.z, 2*self.z, 3*self.z)) fig = plot.figure() plot.imview(z3, title='Imview Test', fig=fig) plot.close() def test_13(self): plot.set_ipython_plot_backend() def test_14(self): plot.set_notebook_plot_backend() def test_15(self): plot.config_notebook_plotting() assert plot.plot.__name__ == 'plot_wrap' ", https://github.com/cahoy/NestedDictionary,881f0ea8af36a60fcd1b9d7a84b1aec4cd7072b2,easy_dict/tests/test_01_default.py::test_del,easy_dict/tests/test_01_default.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from pytest import fixture, raises import easy_dict as nd @fixture(scope='module') def x(): return nd.NestedDict() @fixture(scope='module') def c(): val = nd.NestedDict() val[123] = 'abc' val[456] = 'def' val[789] = 'ghi' return val def test_quick_assign(x): x[123] = 'abc' x['def'] = 456 assert x[123] == 'abc' assert x['def'] == 456 def test_update(x): x.update({'foo': 'bar'}) x.update(baz='qux') assert x['foo'] == 'bar' assert x['baz'] == 'qux' def test_setdefault(x): # with raises(NotImplementedError): x.setdefault(key='y', default='z') x.setdefault(key='y', default='w') assert x['y'] == 'z' def test_length(c): assert len(c) == 3 def test_del(x): del x[123] # assert x == {'foo': 'bar', 'baz': 'qux', 'def': 456} with raises(KeyError): assert x[123] def test_iter_key(c): s = set() for key in c: s.add(key) assert s == {123, 456, 789} def test_iter_item(c): s = set() for key, val in c.items(): s.add((key, val)) assert s == {(123, 'abc'), (456, 'def'), (789, 'ghi')} def test_set_deep_keys_with_non_bracket(x): x.clear() x['a'] = None with raises(Exception): x['a']['b'] = None # internally, executing __getitem__ then attempt to perform __setitem__", https://github.com/casamagalhaes/panamah-sdk-python,746f3fb7ebcf01810917bf9afa8e7ff5a4efad21,tests/test_processor.py::TestStream::test_expiration_by_max_age,tests/test_processor.py,NOD,flaky,RepoArchived,nan,classify;root_cause,"import os import shutil import json from time import sleep from unittest import main, mock, TestCase from panamah_sdk.batch import Batch from panamah_sdk.operation import Update from panamah_sdk.processor import BatchProcessor, BATCH_MAX_LENGTH, BATCH_MAX_SIZE, BATCH_MAX_AGE, ROOT_PATH, ACCUMULATED_PATH, SENT_PATH from panamah_sdk.models.base import Model, StringField from panamah_sdk.models.definitions import PanamahHolding, PanamahAcesso, PanamahSecao, PanamahLoja, PanamahProduto from .server import start as start_test_server, stop as stop_test_server, set_next_response, clear_next_response, get_last_request class Response(): def __init__(self, status_code, json_response): self.status_code = status_code self.json_response = json_response def json(self): return self.json_response def test_expiration(self, by_max_length=False, by_max_size=False, by_max_age=False): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=1 if by_max_length else 999999, batch_max_size=80 if by_max_size else 99999 * 1024, batch_max_age=.5 if by_max_age else 9999999999) with mock.patch.object(b, 'send_accumulated_batches') as send_accumulated_batches_method: self.assertFalse(b.current_batch_expired()) b.save(secao) if (by_max_age): sleep(.6) self.assertTrue(b.current_batch_expired()) self.assertFalse(b.accumulated_batch_exists()) batch_name = b.current_batch.get_filename_by_created_date() b.process() self.assertTrue(b.accumulated_batch_exists()) self.assertTrue(os.path.exists('%s/%s' % (ACCUMULATED_PATH, batch_name))) self.assertEqual(len(b.get_accumulated_batches()), 1) self.assertEqual(b.current_batch.length, 0) b.save(holding) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 1) self.assertEqual(len(b.get_accumulated_batches()), 2) b.save(acesso) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 2) self.assertEqual(len(b.get_accumulated_batches()), 3) class TestStream(TestCase): def setUp(self): if os.path.exists(ROOT_PATH): shutil.rmtree(ROOT_PATH) def test_initialization_and_accumulation(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345') self.assertTrue(os.path.exists(ROOT_PATH)) self.assertTrue(os.path.exists(ACCUMULATED_PATH)) self.assertTrue(os.path.exists(SENT_PATH)) b.save(holding) self.assertEqual(b.current_batch.length, 1) self.assertEqual(b.current_batch.size, 107) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 0.5, delta=0.2) b.save(acesso) self.assertEqual(b.current_batch.length, 2) self.assertEqual(b.current_batch.size, 221) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1, delta=0.2) b.save(secao) self.assertEqual(b.current_batch.length, 3) self.assertEqual(b.current_batch.size, 344) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1.5, delta=0.2) b.delete(holding) self.assertEqual(b.current_batch.length, 4) self.assertEqual(b.current_batch.size, 429) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2, delta=0.2) b.delete(secao) self.assertEqual(b.current_batch.length, 5) self.assertEqual(b.current_batch.size, 512) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2.5, delta=0.2) b.delete(acesso) self.assertEqual(b.current_batch.length, 6) self.assertEqual(b.current_batch.size, 596) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 3, delta=0.2) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}]' ) b.save(holding) self.assertEqual(b.current_batch.length, 6) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}, {""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}]' ) def test_expiration_by_max_length(self): test_expiration(self, by_max_length=True) def test_expiration_by_max_size(self): test_expiration(self, by_max_size=True) def test_expiration_by_max_age(self): test_expiration(self, by_max_age=True) def test_sending_batch(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=6) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.process() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_flushing(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=9999) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.flush() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_requesting_pending_resources(self): b = BatchProcessor('auth', 'secret', '12345') with mock.patch.object(b.client, 'get') as get_method: get_method.return_value = Response(200, { ""00934509022"": { ""HOLDING"": [ ""07128945000132"" ], ""LOJA"": [ ""111"" ], ""PRODUTO"": [ ""1"" ], ""SECAO"": [ ""xxxx"" ] }, ""02541926375"": { ""LOJA"": [ ", https://github.com/casamagalhaes/panamah-sdk-python,746f3fb7ebcf01810917bf9afa8e7ff5a4efad21,tests/test_processor.py::TestStream::test_expiration_by_max_length,tests/test_processor.py,NOD,flaky,RepoArchived,nan,classify;root_cause,"import os import shutil import json from time import sleep from unittest import main, mock, TestCase from panamah_sdk.batch import Batch from panamah_sdk.operation import Update from panamah_sdk.processor import BatchProcessor, BATCH_MAX_LENGTH, BATCH_MAX_SIZE, BATCH_MAX_AGE, ROOT_PATH, ACCUMULATED_PATH, SENT_PATH from panamah_sdk.models.base import Model, StringField from panamah_sdk.models.definitions import PanamahHolding, PanamahAcesso, PanamahSecao, PanamahLoja, PanamahProduto from .server import start as start_test_server, stop as stop_test_server, set_next_response, clear_next_response, get_last_request class Response(): def __init__(self, status_code, json_response): self.status_code = status_code self.json_response = json_response def json(self): return self.json_response def test_expiration(self, by_max_length=False, by_max_size=False, by_max_age=False): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=1 if by_max_length else 999999, batch_max_size=80 if by_max_size else 99999 * 1024, batch_max_age=.5 if by_max_age else 9999999999) with mock.patch.object(b, 'send_accumulated_batches') as send_accumulated_batches_method: self.assertFalse(b.current_batch_expired()) b.save(secao) if (by_max_age): sleep(.6) self.assertTrue(b.current_batch_expired()) self.assertFalse(b.accumulated_batch_exists()) batch_name = b.current_batch.get_filename_by_created_date() b.process() self.assertTrue(b.accumulated_batch_exists()) self.assertTrue(os.path.exists('%s/%s' % (ACCUMULATED_PATH, batch_name))) self.assertEqual(len(b.get_accumulated_batches()), 1) self.assertEqual(b.current_batch.length, 0) b.save(holding) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 1) self.assertEqual(len(b.get_accumulated_batches()), 2) b.save(acesso) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 2) self.assertEqual(len(b.get_accumulated_batches()), 3) class TestStream(TestCase): def setUp(self): if os.path.exists(ROOT_PATH): shutil.rmtree(ROOT_PATH) def test_initialization_and_accumulation(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345') self.assertTrue(os.path.exists(ROOT_PATH)) self.assertTrue(os.path.exists(ACCUMULATED_PATH)) self.assertTrue(os.path.exists(SENT_PATH)) b.save(holding) self.assertEqual(b.current_batch.length, 1) self.assertEqual(b.current_batch.size, 107) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 0.5, delta=0.2) b.save(acesso) self.assertEqual(b.current_batch.length, 2) self.assertEqual(b.current_batch.size, 221) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1, delta=0.2) b.save(secao) self.assertEqual(b.current_batch.length, 3) self.assertEqual(b.current_batch.size, 344) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1.5, delta=0.2) b.delete(holding) self.assertEqual(b.current_batch.length, 4) self.assertEqual(b.current_batch.size, 429) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2, delta=0.2) b.delete(secao) self.assertEqual(b.current_batch.length, 5) self.assertEqual(b.current_batch.size, 512) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2.5, delta=0.2) b.delete(acesso) self.assertEqual(b.current_batch.length, 6) self.assertEqual(b.current_batch.size, 596) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 3, delta=0.2) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}]' ) b.save(holding) self.assertEqual(b.current_batch.length, 6) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}, {""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}]' ) def test_expiration_by_max_length(self): test_expiration(self, by_max_length=True) def test_expiration_by_max_size(self): test_expiration(self, by_max_size=True) def test_expiration_by_max_age(self): test_expiration(self, by_max_age=True) def test_sending_batch(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=6) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.process() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_flushing(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=9999) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.flush() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_requesting_pending_resources(self): b = BatchProcessor('auth', 'secret', '12345') with mock.patch.object(b.client, 'get') as get_method: get_method.return_value = Response(200, { ""00934509022"": { ""HOLDING"": [ ""07128945000132"" ], ""LOJA"": [ ""111"" ], ""PRODUTO"": [ ""1"" ], ""SECAO"": [ ""xxxx"" ] }, ""02541926375"": { ""LOJA"": [ ", https://github.com/casamagalhaes/panamah-sdk-python,746f3fb7ebcf01810917bf9afa8e7ff5a4efad21,tests/test_processor.py::TestStream::test_expiration_by_max_size,tests/test_processor.py,NOD,flaky,RepoArchived,nan,classify;root_cause,"import os import shutil import json from time import sleep from unittest import main, mock, TestCase from panamah_sdk.batch import Batch from panamah_sdk.operation import Update from panamah_sdk.processor import BatchProcessor, BATCH_MAX_LENGTH, BATCH_MAX_SIZE, BATCH_MAX_AGE, ROOT_PATH, ACCUMULATED_PATH, SENT_PATH from panamah_sdk.models.base import Model, StringField from panamah_sdk.models.definitions import PanamahHolding, PanamahAcesso, PanamahSecao, PanamahLoja, PanamahProduto from .server import start as start_test_server, stop as stop_test_server, set_next_response, clear_next_response, get_last_request class Response(): def __init__(self, status_code, json_response): self.status_code = status_code self.json_response = json_response def json(self): return self.json_response def test_expiration(self, by_max_length=False, by_max_size=False, by_max_age=False): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=1 if by_max_length else 999999, batch_max_size=80 if by_max_size else 99999 * 1024, batch_max_age=.5 if by_max_age else 9999999999) with mock.patch.object(b, 'send_accumulated_batches') as send_accumulated_batches_method: self.assertFalse(b.current_batch_expired()) b.save(secao) if (by_max_age): sleep(.6) self.assertTrue(b.current_batch_expired()) self.assertFalse(b.accumulated_batch_exists()) batch_name = b.current_batch.get_filename_by_created_date() b.process() self.assertTrue(b.accumulated_batch_exists()) self.assertTrue(os.path.exists('%s/%s' % (ACCUMULATED_PATH, batch_name))) self.assertEqual(len(b.get_accumulated_batches()), 1) self.assertEqual(b.current_batch.length, 0) b.save(holding) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 1) self.assertEqual(len(b.get_accumulated_batches()), 2) b.save(acesso) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 2) self.assertEqual(len(b.get_accumulated_batches()), 3) class TestStream(TestCase): def setUp(self): if os.path.exists(ROOT_PATH): shutil.rmtree(ROOT_PATH) def test_initialization_and_accumulation(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345') self.assertTrue(os.path.exists(ROOT_PATH)) self.assertTrue(os.path.exists(ACCUMULATED_PATH)) self.assertTrue(os.path.exists(SENT_PATH)) b.save(holding) self.assertEqual(b.current_batch.length, 1) self.assertEqual(b.current_batch.size, 107) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 0.5, delta=0.2) b.save(acesso) self.assertEqual(b.current_batch.length, 2) self.assertEqual(b.current_batch.size, 221) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1, delta=0.2) b.save(secao) self.assertEqual(b.current_batch.length, 3) self.assertEqual(b.current_batch.size, 344) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1.5, delta=0.2) b.delete(holding) self.assertEqual(b.current_batch.length, 4) self.assertEqual(b.current_batch.size, 429) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2, delta=0.2) b.delete(secao) self.assertEqual(b.current_batch.length, 5) self.assertEqual(b.current_batch.size, 512) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2.5, delta=0.2) b.delete(acesso) self.assertEqual(b.current_batch.length, 6) self.assertEqual(b.current_batch.size, 596) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 3, delta=0.2) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}]' ) b.save(holding) self.assertEqual(b.current_batch.length, 6) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}, {""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}]' ) def test_expiration_by_max_length(self): test_expiration(self, by_max_length=True) def test_expiration_by_max_size(self): test_expiration(self, by_max_size=True) def test_expiration_by_max_age(self): test_expiration(self, by_max_age=True) def test_sending_batch(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=6) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.process() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_flushing(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=9999) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.flush() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_requesting_pending_resources(self): b = BatchProcessor('auth', 'secret', '12345') with mock.patch.object(b.client, 'get') as get_method: get_method.return_value = Response(200, { ""00934509022"": { ""HOLDING"": [ ""07128945000132"" ], ""LOJA"": [ ""111"" ], ""PRODUTO"": [ ""1"" ], ""SECAO"": [ ""xxxx"" ] }, ""02541926375"": { ""LOJA"": [ ", https://github.com/casamagalhaes/panamah-sdk-python,746f3fb7ebcf01810917bf9afa8e7ff5a4efad21,tests/test_processor.py::TestStream::test_get_pending_resources,tests/test_processor.py,NOD,flaky,RepoArchived,nan,classify;root_cause,"import os import shutil import json from time import sleep from unittest import main, mock, TestCase from panamah_sdk.batch import Batch from panamah_sdk.operation import Update from panamah_sdk.processor import BatchProcessor, BATCH_MAX_LENGTH, BATCH_MAX_SIZE, BATCH_MAX_AGE, ROOT_PATH, ACCUMULATED_PATH, SENT_PATH from panamah_sdk.models.base import Model, StringField from panamah_sdk.models.definitions import PanamahHolding, PanamahAcesso, PanamahSecao, PanamahLoja, PanamahProduto from .server import start as start_test_server, stop as stop_test_server, set_next_response, clear_next_response, get_last_request class Response(): def __init__(self, status_code, json_response): self.status_code = status_code self.json_response = json_response def json(self): return self.json_response def test_expiration(self, by_max_length=False, by_max_size=False, by_max_age=False): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=1 if by_max_length else 999999, batch_max_size=80 if by_max_size else 99999 * 1024, batch_max_age=.5 if by_max_age else 9999999999) with mock.patch.object(b, 'send_accumulated_batches') as send_accumulated_batches_method: self.assertFalse(b.current_batch_expired()) b.save(secao) if (by_max_age): sleep(.6) self.assertTrue(b.current_batch_expired()) self.assertFalse(b.accumulated_batch_exists()) batch_name = b.current_batch.get_filename_by_created_date() b.process() self.assertTrue(b.accumulated_batch_exists()) self.assertTrue(os.path.exists('%s/%s' % (ACCUMULATED_PATH, batch_name))) self.assertEqual(len(b.get_accumulated_batches()), 1) self.assertEqual(b.current_batch.length, 0) b.save(holding) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 1) self.assertEqual(len(b.get_accumulated_batches()), 2) b.save(acesso) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 2) self.assertEqual(len(b.get_accumulated_batches()), 3) class TestStream(TestCase): def setUp(self): if os.path.exists(ROOT_PATH): shutil.rmtree(ROOT_PATH) def test_initialization_and_accumulation(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345') self.assertTrue(os.path.exists(ROOT_PATH)) self.assertTrue(os.path.exists(ACCUMULATED_PATH)) self.assertTrue(os.path.exists(SENT_PATH)) b.save(holding) self.assertEqual(b.current_batch.length, 1) self.assertEqual(b.current_batch.size, 107) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 0.5, delta=0.2) b.save(acesso) self.assertEqual(b.current_batch.length, 2) self.assertEqual(b.current_batch.size, 221) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1, delta=0.2) b.save(secao) self.assertEqual(b.current_batch.length, 3) self.assertEqual(b.current_batch.size, 344) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1.5, delta=0.2) b.delete(holding) self.assertEqual(b.current_batch.length, 4) self.assertEqual(b.current_batch.size, 429) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2, delta=0.2) b.delete(secao) self.assertEqual(b.current_batch.length, 5) self.assertEqual(b.current_batch.size, 512) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2.5, delta=0.2) b.delete(acesso) self.assertEqual(b.current_batch.length, 6) self.assertEqual(b.current_batch.size, 596) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 3, delta=0.2) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}]' ) b.save(holding) self.assertEqual(b.current_batch.length, 6) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}, {""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}]' ) def test_expiration_by_max_length(self): test_expiration(self, by_max_length=True) def test_expiration_by_max_size(self): test_expiration(self, by_max_size=True) def test_expiration_by_max_age(self): test_expiration(self, by_max_age=True) def test_sending_batch(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=6) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.process() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_flushing(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=9999) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.flush() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_requesting_pending_resources(self): b = BatchProcessor('auth', 'secret', '12345') with mock.patch.object(b.client, 'get') as get_method: get_method.return_value = Response(200, { ""00934509022"": { ""HOLDING"": [ ""07128945000132"" ], ""LOJA"": [ ""111"" ], ""PRODUTO"": [ ""1"" ], ""SECAO"": [ ""xxxx"" ] }, ""02541926375"": { ""LOJA"": [ ", https://github.com/casamagalhaes/panamah-sdk-python,746f3fb7ebcf01810917bf9afa8e7ff5a4efad21,tests/test_processor.py::TestStream::test_initialization_and_accumulation,tests/test_processor.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import os import shutil import json from time import sleep from unittest import main, mock, TestCase from panamah_sdk.batch import Batch from panamah_sdk.operation import Update from panamah_sdk.processor import BatchProcessor, BATCH_MAX_LENGTH, BATCH_MAX_SIZE, BATCH_MAX_AGE, ROOT_PATH, ACCUMULATED_PATH, SENT_PATH from panamah_sdk.models.base import Model, StringField from panamah_sdk.models.definitions import PanamahHolding, PanamahAcesso, PanamahSecao, PanamahLoja, PanamahProduto from .server import start as start_test_server, stop as stop_test_server, set_next_response, clear_next_response, get_last_request class Response(): def __init__(self, status_code, json_response): self.status_code = status_code self.json_response = json_response def json(self): return self.json_response def test_expiration(self, by_max_length=False, by_max_size=False, by_max_age=False): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=1 if by_max_length else 999999, batch_max_size=80 if by_max_size else 99999 * 1024, batch_max_age=.5 if by_max_age else 9999999999) with mock.patch.object(b, 'send_accumulated_batches') as send_accumulated_batches_method: self.assertFalse(b.current_batch_expired()) b.save(secao) if (by_max_age): sleep(.6) self.assertTrue(b.current_batch_expired()) self.assertFalse(b.accumulated_batch_exists()) batch_name = b.current_batch.get_filename_by_created_date() b.process() self.assertTrue(b.accumulated_batch_exists()) self.assertTrue(os.path.exists('%s/%s' % (ACCUMULATED_PATH, batch_name))) self.assertEqual(len(b.get_accumulated_batches()), 1) self.assertEqual(b.current_batch.length, 0) b.save(holding) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 1) self.assertEqual(len(b.get_accumulated_batches()), 2) b.save(acesso) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 2) self.assertEqual(len(b.get_accumulated_batches()), 3) class TestStream(TestCase): def setUp(self): if os.path.exists(ROOT_PATH): shutil.rmtree(ROOT_PATH) def test_initialization_and_accumulation(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345') self.assertTrue(os.path.exists(ROOT_PATH)) self.assertTrue(os.path.exists(ACCUMULATED_PATH)) self.assertTrue(os.path.exists(SENT_PATH)) b.save(holding) self.assertEqual(b.current_batch.length, 1) self.assertEqual(b.current_batch.size, 107) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 0.5, delta=0.2) b.save(acesso) self.assertEqual(b.current_batch.length, 2) self.assertEqual(b.current_batch.size, 221) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1, delta=0.2) b.save(secao) self.assertEqual(b.current_batch.length, 3) self.assertEqual(b.current_batch.size, 344) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1.5, delta=0.2) b.delete(holding) self.assertEqual(b.current_batch.length, 4) self.assertEqual(b.current_batch.size, 429) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2, delta=0.2) b.delete(secao) self.assertEqual(b.current_batch.length, 5) self.assertEqual(b.current_batch.size, 512) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2.5, delta=0.2) b.delete(acesso) self.assertEqual(b.current_batch.length, 6) self.assertEqual(b.current_batch.size, 596) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 3, delta=0.2) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}]' ) b.save(holding) self.assertEqual(b.current_batch.length, 6) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}, {""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}]' ) def test_expiration_by_max_length(self): test_expiration(self, by_max_length=True) def test_expiration_by_max_size(self): test_expiration(self, by_max_size=True) def test_expiration_by_max_age(self): test_expiration(self, by_max_age=True) def test_sending_batch(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=6) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.process() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_flushing(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=9999) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.flush() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_requesting_pending_resources(self): b = BatchProcessor('auth', 'secret', '12345') with mock.patch.object(b.client, 'get') as get_method: get_method.return_value = Response(200, { ""00934509022"": { ""HOLDING"": [ ""07128945000132"" ], ""LOJA"": [ ""111"" ], ""PRODUTO"": [ ""1"" ], ""SECAO"": [ ""xxxx"" ] }, ""02541926375"": { ""LOJA"": [ ", https://github.com/casamagalhaes/panamah-sdk-python,746f3fb7ebcf01810917bf9afa8e7ff5a4efad21,tests/test_processor.py::TestStream::test_recover_failures,tests/test_processor.py,NOD,flaky,RepoArchived,nan,classify;root_cause,"import os import shutil import json from time import sleep from unittest import main, mock, TestCase from panamah_sdk.batch import Batch from panamah_sdk.operation import Update from panamah_sdk.processor import BatchProcessor, BATCH_MAX_LENGTH, BATCH_MAX_SIZE, BATCH_MAX_AGE, ROOT_PATH, ACCUMULATED_PATH, SENT_PATH from panamah_sdk.models.base import Model, StringField from panamah_sdk.models.definitions import PanamahHolding, PanamahAcesso, PanamahSecao, PanamahLoja, PanamahProduto from .server import start as start_test_server, stop as stop_test_server, set_next_response, clear_next_response, get_last_request class Response(): def __init__(self, status_code, json_response): self.status_code = status_code self.json_response = json_response def json(self): return self.json_response def test_expiration(self, by_max_length=False, by_max_size=False, by_max_age=False): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=1 if by_max_length else 999999, batch_max_size=80 if by_max_size else 99999 * 1024, batch_max_age=.5 if by_max_age else 9999999999) with mock.patch.object(b, 'send_accumulated_batches') as send_accumulated_batches_method: self.assertFalse(b.current_batch_expired()) b.save(secao) if (by_max_age): sleep(.6) self.assertTrue(b.current_batch_expired()) self.assertFalse(b.accumulated_batch_exists()) batch_name = b.current_batch.get_filename_by_created_date() b.process() self.assertTrue(b.accumulated_batch_exists()) self.assertTrue(os.path.exists('%s/%s' % (ACCUMULATED_PATH, batch_name))) self.assertEqual(len(b.get_accumulated_batches()), 1) self.assertEqual(b.current_batch.length, 0) b.save(holding) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 1) self.assertEqual(len(b.get_accumulated_batches()), 2) b.save(acesso) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 2) self.assertEqual(len(b.get_accumulated_batches()), 3) class TestStream(TestCase): def setUp(self): if os.path.exists(ROOT_PATH): shutil.rmtree(ROOT_PATH) def test_initialization_and_accumulation(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345') self.assertTrue(os.path.exists(ROOT_PATH)) self.assertTrue(os.path.exists(ACCUMULATED_PATH)) self.assertTrue(os.path.exists(SENT_PATH)) b.save(holding) self.assertEqual(b.current_batch.length, 1) self.assertEqual(b.current_batch.size, 107) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 0.5, delta=0.2) b.save(acesso) self.assertEqual(b.current_batch.length, 2) self.assertEqual(b.current_batch.size, 221) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1, delta=0.2) b.save(secao) self.assertEqual(b.current_batch.length, 3) self.assertEqual(b.current_batch.size, 344) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1.5, delta=0.2) b.delete(holding) self.assertEqual(b.current_batch.length, 4) self.assertEqual(b.current_batch.size, 429) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2, delta=0.2) b.delete(secao) self.assertEqual(b.current_batch.length, 5) self.assertEqual(b.current_batch.size, 512) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2.5, delta=0.2) b.delete(acesso) self.assertEqual(b.current_batch.length, 6) self.assertEqual(b.current_batch.size, 596) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 3, delta=0.2) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}]' ) b.save(holding) self.assertEqual(b.current_batch.length, 6) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}, {""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}]' ) def test_expiration_by_max_length(self): test_expiration(self, by_max_length=True) def test_expiration_by_max_size(self): test_expiration(self, by_max_size=True) def test_expiration_by_max_age(self): test_expiration(self, by_max_age=True) def test_sending_batch(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=6) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.process() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_flushing(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=9999) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.flush() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_requesting_pending_resources(self): b = BatchProcessor('auth', 'secret', '12345') with mock.patch.object(b.client, 'get') as get_method: get_method.return_value = Response(200, { ""00934509022"": { ""HOLDING"": [ ""07128945000132"" ], ""LOJA"": [ ""111"" ], ""PRODUTO"": [ ""1"" ], ""SECAO"": [ ""xxxx"" ] }, ""02541926375"": { ""LOJA"": [ ", https://github.com/casamagalhaes/panamah-sdk-python,746f3fb7ebcf01810917bf9afa8e7ff5a4efad21,tests/test_processor.py::TestStream::test_requesting_pending_resources,tests/test_processor.py,NOD,flaky,RepoArchived,nan,classify;root_cause,"import os import shutil import json from time import sleep from unittest import main, mock, TestCase from panamah_sdk.batch import Batch from panamah_sdk.operation import Update from panamah_sdk.processor import BatchProcessor, BATCH_MAX_LENGTH, BATCH_MAX_SIZE, BATCH_MAX_AGE, ROOT_PATH, ACCUMULATED_PATH, SENT_PATH from panamah_sdk.models.base import Model, StringField from panamah_sdk.models.definitions import PanamahHolding, PanamahAcesso, PanamahSecao, PanamahLoja, PanamahProduto from .server import start as start_test_server, stop as stop_test_server, set_next_response, clear_next_response, get_last_request class Response(): def __init__(self, status_code, json_response): self.status_code = status_code self.json_response = json_response def json(self): return self.json_response def test_expiration(self, by_max_length=False, by_max_size=False, by_max_age=False): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=1 if by_max_length else 999999, batch_max_size=80 if by_max_size else 99999 * 1024, batch_max_age=.5 if by_max_age else 9999999999) with mock.patch.object(b, 'send_accumulated_batches') as send_accumulated_batches_method: self.assertFalse(b.current_batch_expired()) b.save(secao) if (by_max_age): sleep(.6) self.assertTrue(b.current_batch_expired()) self.assertFalse(b.accumulated_batch_exists()) batch_name = b.current_batch.get_filename_by_created_date() b.process() self.assertTrue(b.accumulated_batch_exists()) self.assertTrue(os.path.exists('%s/%s' % (ACCUMULATED_PATH, batch_name))) self.assertEqual(len(b.get_accumulated_batches()), 1) self.assertEqual(b.current_batch.length, 0) b.save(holding) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 1) self.assertEqual(len(b.get_accumulated_batches()), 2) b.save(acesso) if (by_max_age): sleep(.6) b.process() self.assertEqual(send_accumulated_batches_method.call_count, 2) self.assertEqual(len(b.get_accumulated_batches()), 3) class TestStream(TestCase): def setUp(self): if os.path.exists(ROOT_PATH): shutil.rmtree(ROOT_PATH) def test_initialization_and_accumulation(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345') self.assertTrue(os.path.exists(ROOT_PATH)) self.assertTrue(os.path.exists(ACCUMULATED_PATH)) self.assertTrue(os.path.exists(SENT_PATH)) b.save(holding) self.assertEqual(b.current_batch.length, 1) self.assertEqual(b.current_batch.size, 107) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 0.5, delta=0.2) b.save(acesso) self.assertEqual(b.current_batch.length, 2) self.assertEqual(b.current_batch.size, 221) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1, delta=0.2) b.save(secao) self.assertEqual(b.current_batch.length, 3) self.assertEqual(b.current_batch.size, 344) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 1.5, delta=0.2) b.delete(holding) self.assertEqual(b.current_batch.length, 4) self.assertEqual(b.current_batch.size, 429) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2, delta=0.2) b.delete(secao) self.assertEqual(b.current_batch.length, 5) self.assertEqual(b.current_batch.size, 512) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 2.5, delta=0.2) b.delete(acesso) self.assertEqual(b.current_batch.length, 6) self.assertEqual(b.current_batch.size, 596) sleep(.5) self.assertAlmostEqual(b.current_batch.age, 3, delta=0.2) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}]' ) b.save(holding) self.assertEqual(b.current_batch.length, 6) self.assertEqual( b.current_batch.json(), '[{""data"": {""id"": ""4321"", ""funcionarioIds"": [""1"", ""2""]}, ""tipo"": ""ACESSO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""data"": {""id"": ""5555"", ""codigo"": ""6666"", ""descricao"": ""teste""}, ""tipo"": ""SECAO"", ""op"": ""update"", ""assinanteId"": ""12345""}, {""tipo"": ""HOLDING"", ""op"": ""delete"", ""data"": {""id"": ""1234""}, ""assinanteId"": ""12345""}, {""tipo"": ""SECAO"", ""op"": ""delete"", ""data"": {""id"": ""5555""}, ""assinanteId"": ""12345""}, {""tipo"": ""ACESSO"", ""op"": ""delete"", ""data"": {""id"": ""4321""}, ""assinanteId"": ""12345""}, {""data"": {""id"": ""1234"", ""descricao"": ""teste""}, ""tipo"": ""HOLDING"", ""op"": ""update"", ""assinanteId"": ""12345""}]' ) def test_expiration_by_max_length(self): test_expiration(self, by_max_length=True) def test_expiration_by_max_size(self): test_expiration(self, by_max_size=True) def test_expiration_by_max_age(self): test_expiration(self, by_max_age=True) def test_sending_batch(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=6) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.process() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_flushing(self): holding = PanamahHolding(id='1234', descricao='teste') acesso = PanamahAcesso(id='4321', funcionario_ids=['1', '2']) secao = PanamahSecao(id='5555', codigo='6666', descricao='teste') b = BatchProcessor('auth', 'secret', '12345', batch_max_length=9999) b.save(holding) b.save(acesso) b.save(secao) b.delete(holding) b.delete(secao) b.delete(acesso) start_test_server() try: b.process() b.flush() request = get_last_request() expected_payload = [{'data': {'id': '1234', 'descricao': 'teste'}, 'tipo': 'HOLDING', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '4321', 'funcionarioIds': ['1', '2']}, 'tipo': 'ACESSO', 'op': 'update', 'assinanteId': '12345'}, {'data': {'id': '5555', 'codigo': '6666', 'descricao': 'teste'}, 'tipo': 'SECAO', 'op': 'update', 'assinanteId': '12345'}, {'tipo': 'HOLDING', 'op': 'delete', 'data': {'id': '1234'}, 'assinanteId': '12345'}, {'tipo': 'SECAO', 'op': 'delete', 'data': {'id': '5555'}, 'assinanteId': '12345'}, {'tipo': 'ACESSO', 'op': 'delete', 'data': {'id': '4321'}, 'assinanteId': '12345'}] self.assertListEqual(request['payload'], expected_payload) finally: stop_test_server() def test_requesting_pending_resources(self): b = BatchProcessor('auth', 'secret', '12345') with mock.patch.object(b.client, 'get') as get_method: get_method.return_value = Response(200, { ""00934509022"": { ""HOLDING"": [ ""07128945000132"" ], ""LOJA"": [ ""111"" ], ""PRODUTO"": [ ""1"" ], ""SECAO"": [ ""xxxx"" ] }, ""02541926375"": { ""LOJA"": [ ", https://github.com/casamagalhaes/panamah-sdk-python,746f3fb7ebcf01810917bf9afa8e7ff5a4efad21,tests/test_stream.py::TestStream::test_events,tests/test_stream.py,NOD,flaky,RepoArchived,nan,classify;root_cause,"from unittest import main, mock, TestCase from panamah_sdk.stream import PanamahStream from panamah_sdk.models.base import Model, StringField from panamah_sdk.models.definitions import PanamahAcesso from .server import start as start_test_server, stop as stop_test_server, set_next_response, clear_next_response class TestStream(TestCase): def test_initialization_and_operations(self): class ChildModel(Model): name = 'PRODUTO' schema = { 'id': StringField(required=True) } # should not accept non-model objects on save stream = PanamahStream('auth', 'secret', '123') try: stream.save(None) except Exception as e: self.assertEqual( str(e), 'model deve ser um modelo valido do Panamah') # should not accept non-model objects on delete stream = PanamahStream('auth', 'secret', '123') try: stream.delete(None) except Exception as e: self.assertEqual( str(e), 'model deve ser um modelo valido do Panamah') # should validate models on save try: model = ChildModel() stream.save(model) except Exception as e: self.assertEqual( str(e), 'ChildModel.id -> propriedade obrigatoria') # should validate models on delete try: model = ChildModel() stream.delete(model) except Exception as e: self.assertEqual( str(e), 'ChildModel.id -> propriedade obrigatoria') def test_events(self): global before_save_called global before_delete_called class ChildModel(Model): name = 'PRODUTO' schema = { 'id': StringField(required=True) } stream = PanamahStream('auth', 'secret', '123') before_save_called = False before_delete_called = False class TestEvents: def before_save(self, model, prevent_save): global before_save_called before_save_called = True prevent_save() def before_delete(self, model, prevent_delete): global before_delete_called before_delete_called = True prevent_delete() test_events = TestEvents() stream.on('before_save', test_events.before_save) stream.on('before_delete', test_events.before_delete) with mock.patch.object(stream.processor, 'save') as processor_save_method: processor_save_method.return_value = True model = ChildModel(id='1') stream.save(model) self.assertTrue(before_save_called) stream.delete(model) self.assertTrue(before_delete_called) before_save_called = False before_delete_called = False stream.off('before_save', test_events.before_save) stream.off('before_delete', test_events.before_delete) stream.save(model) stream.delete(model) self.assertFalse(before_save_called) self.assertFalse(before_delete_called) if __name__ == '__main__': main() ", https://github.com/cbornet/python-httpproblem,0b08139277e9cba02339eeb0996c0910d047274a,tests/test_problem.py::test_exception_to_http_response,tests/test_problem.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from pytest import mark from httpproblem import problem, problem_http_response, Problem, activate_traceback, deactivate_traceback,\ set_serialize_function @mark.parametrize(""status, title, detail, type, instance, kwargs, expected"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } ), ( None, None, None, None, None, {}, {} ), ( 404, None, None, None, None, {}, { 'status': 404, 'title': 'Not Found' } ), ( 404, None, None, 'about:blank', None, {}, { 'status': 404, 'title': 'Not Found', 'type': 'about:blank' } ), ( 1000, None, None, None, None, {}, { 'status': 1000 } ), ]) def test_problem(status, title, detail, type, instance, kwargs, expected): assert problem(status, title, detail, type, instance, **kwargs) == expected @mark.parametrize(""status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'test_header_key': 'test_header_value'}, {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' }, { 'Content-Type': 'application/problem+json', 'test_header_key': 'test_header_value' } ), ( None, None, None, None, None, None, {}, {}, { 'Content-Type': 'application/problem+json' } ), ( None, None, None, None, None, {'Content-Type': 'text/plain'}, {}, {}, { 'Content-Type': 'text/plain' } ), ( None, None, None, None, None, {'content-type': 'text/plain'}, {}, {}, { 'content-type': 'text/plain' } ) ]) def test_problem_http_response(status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers): response = problem_http_response(status, title, detail, type, instance, headers, **kwargs) assert response['statusCode'] == status assert response['headers'] == expected_headers assert json.loads(response['body']) == expected_body def test_exception_to_dict(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: exception_as_dict = e.to_dict() assert exception_as_dict == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_dict_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict() assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_dict_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict(with_traceback=True) assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_http_response(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: response = e.to_http_response() assert response['statusCode'] == 1000 assert response['headers'] == {'Content-Type': 'application/problem+json'} assert json.loads(response['body']) == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_http_response_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response() body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_exception_to_http_response_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response(with_traceback=True) body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_set_serialize_function(): set_serialize_function(lambda data: 'dummy') assert problem_http_response() == { 'statusCode': None, 'body': 'dummy', 'headers': {'Content-Type': 'application/problem+json'}, } def test_str(): assert str(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" def test_repr(): assert repr(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" ", https://github.com/cbornet/python-httpproblem,0b08139277e9cba02339eeb0996c0910d047274a,tests/test_problem.py::test_exception_to_http_response_with_global_traceback,tests/test_problem.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from pytest import mark from httpproblem import problem, problem_http_response, Problem, activate_traceback, deactivate_traceback,\ set_serialize_function @mark.parametrize(""status, title, detail, type, instance, kwargs, expected"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } ), ( None, None, None, None, None, {}, {} ), ( 404, None, None, None, None, {}, { 'status': 404, 'title': 'Not Found' } ), ( 404, None, None, 'about:blank', None, {}, { 'status': 404, 'title': 'Not Found', 'type': 'about:blank' } ), ( 1000, None, None, None, None, {}, { 'status': 1000 } ), ]) def test_problem(status, title, detail, type, instance, kwargs, expected): assert problem(status, title, detail, type, instance, **kwargs) == expected @mark.parametrize(""status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'test_header_key': 'test_header_value'}, {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' }, { 'Content-Type': 'application/problem+json', 'test_header_key': 'test_header_value' } ), ( None, None, None, None, None, None, {}, {}, { 'Content-Type': 'application/problem+json' } ), ( None, None, None, None, None, {'Content-Type': 'text/plain'}, {}, {}, { 'Content-Type': 'text/plain' } ), ( None, None, None, None, None, {'content-type': 'text/plain'}, {}, {}, { 'content-type': 'text/plain' } ) ]) def test_problem_http_response(status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers): response = problem_http_response(status, title, detail, type, instance, headers, **kwargs) assert response['statusCode'] == status assert response['headers'] == expected_headers assert json.loads(response['body']) == expected_body def test_exception_to_dict(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: exception_as_dict = e.to_dict() assert exception_as_dict == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_dict_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict() assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_dict_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict(with_traceback=True) assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_http_response(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: response = e.to_http_response() assert response['statusCode'] == 1000 assert response['headers'] == {'Content-Type': 'application/problem+json'} assert json.loads(response['body']) == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_http_response_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response() body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_exception_to_http_response_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response(with_traceback=True) body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_set_serialize_function(): set_serialize_function(lambda data: 'dummy') assert problem_http_response() == { 'statusCode': None, 'body': 'dummy', 'headers': {'Content-Type': 'application/problem+json'}, } def test_str(): assert str(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" def test_repr(): assert repr(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" ", https://github.com/cbornet/python-httpproblem,0b08139277e9cba02339eeb0996c0910d047274a,tests/test_problem.py::test_exception_to_http_response_with_traceback_param,tests/test_problem.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from pytest import mark from httpproblem import problem, problem_http_response, Problem, activate_traceback, deactivate_traceback,\ set_serialize_function @mark.parametrize(""status, title, detail, type, instance, kwargs, expected"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } ), ( None, None, None, None, None, {}, {} ), ( 404, None, None, None, None, {}, { 'status': 404, 'title': 'Not Found' } ), ( 404, None, None, 'about:blank', None, {}, { 'status': 404, 'title': 'Not Found', 'type': 'about:blank' } ), ( 1000, None, None, None, None, {}, { 'status': 1000 } ), ]) def test_problem(status, title, detail, type, instance, kwargs, expected): assert problem(status, title, detail, type, instance, **kwargs) == expected @mark.parametrize(""status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'test_header_key': 'test_header_value'}, {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' }, { 'Content-Type': 'application/problem+json', 'test_header_key': 'test_header_value' } ), ( None, None, None, None, None, None, {}, {}, { 'Content-Type': 'application/problem+json' } ), ( None, None, None, None, None, {'Content-Type': 'text/plain'}, {}, {}, { 'Content-Type': 'text/plain' } ), ( None, None, None, None, None, {'content-type': 'text/plain'}, {}, {}, { 'content-type': 'text/plain' } ) ]) def test_problem_http_response(status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers): response = problem_http_response(status, title, detail, type, instance, headers, **kwargs) assert response['statusCode'] == status assert response['headers'] == expected_headers assert json.loads(response['body']) == expected_body def test_exception_to_dict(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: exception_as_dict = e.to_dict() assert exception_as_dict == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_dict_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict() assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_dict_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict(with_traceback=True) assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_http_response(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: response = e.to_http_response() assert response['statusCode'] == 1000 assert response['headers'] == {'Content-Type': 'application/problem+json'} assert json.loads(response['body']) == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_http_response_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response() body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_exception_to_http_response_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response(with_traceback=True) body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_set_serialize_function(): set_serialize_function(lambda data: 'dummy') assert problem_http_response() == { 'statusCode': None, 'body': 'dummy', 'headers': {'Content-Type': 'application/problem+json'}, } def test_str(): assert str(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" def test_repr(): assert repr(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" ", https://github.com/cbornet/python-httpproblem,0b08139277e9cba02339eeb0996c0910d047274a,tests/test_problem.py::test_problem_http_response[1000-test_title-test_detail-test_type-test_instance-headers0-kwargs0-expected_body0-expected_headers0],tests/test_problem.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from pytest import mark from httpproblem import problem, problem_http_response, Problem, activate_traceback, deactivate_traceback,\ set_serialize_function @mark.parametrize(""status, title, detail, type, instance, kwargs, expected"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } ), ( None, None, None, None, None, {}, {} ), ( 404, None, None, None, None, {}, { 'status': 404, 'title': 'Not Found' } ), ( 404, None, None, 'about:blank', None, {}, { 'status': 404, 'title': 'Not Found', 'type': 'about:blank' } ), ( 1000, None, None, None, None, {}, { 'status': 1000 } ), ]) def test_problem(status, title, detail, type, instance, kwargs, expected): assert problem(status, title, detail, type, instance, **kwargs) == expected @mark.parametrize(""status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'test_header_key': 'test_header_value'}, {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' }, { 'Content-Type': 'application/problem+json', 'test_header_key': 'test_header_value' } ), ( None, None, None, None, None, None, {}, {}, { 'Content-Type': 'application/problem+json' } ), ( None, None, None, None, None, {'Content-Type': 'text/plain'}, {}, {}, { 'Content-Type': 'text/plain' } ), ( None, None, None, None, None, {'content-type': 'text/plain'}, {}, {}, { 'content-type': 'text/plain' } ) ]) def test_problem_http_response(status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers): response = problem_http_response(status, title, detail, type, instance, headers, **kwargs) assert response['statusCode'] == status assert response['headers'] == expected_headers assert json.loads(response['body']) == expected_body def test_exception_to_dict(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: exception_as_dict = e.to_dict() assert exception_as_dict == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_dict_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict() assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_dict_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict(with_traceback=True) assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_http_response(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: response = e.to_http_response() assert response['statusCode'] == 1000 assert response['headers'] == {'Content-Type': 'application/problem+json'} assert json.loads(response['body']) == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_http_response_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response() body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_exception_to_http_response_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response(with_traceback=True) body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_set_serialize_function(): set_serialize_function(lambda data: 'dummy') assert problem_http_response() == { 'statusCode': None, 'body': 'dummy', 'headers': {'Content-Type': 'application/problem+json'}, } def test_str(): assert str(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" def test_repr(): assert repr(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" ", https://github.com/cbornet/python-httpproblem,0b08139277e9cba02339eeb0996c0910d047274a,tests/test_problem.py::test_problem_http_response[None-None-None-None-None-headers2-kwargs2-expected_body2-expected_headers2],tests/test_problem.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from pytest import mark from httpproblem import problem, problem_http_response, Problem, activate_traceback, deactivate_traceback,\ set_serialize_function @mark.parametrize(""status, title, detail, type, instance, kwargs, expected"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } ), ( None, None, None, None, None, {}, {} ), ( 404, None, None, None, None, {}, { 'status': 404, 'title': 'Not Found' } ), ( 404, None, None, 'about:blank', None, {}, { 'status': 404, 'title': 'Not Found', 'type': 'about:blank' } ), ( 1000, None, None, None, None, {}, { 'status': 1000 } ), ]) def test_problem(status, title, detail, type, instance, kwargs, expected): assert problem(status, title, detail, type, instance, **kwargs) == expected @mark.parametrize(""status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'test_header_key': 'test_header_value'}, {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' }, { 'Content-Type': 'application/problem+json', 'test_header_key': 'test_header_value' } ), ( None, None, None, None, None, None, {}, {}, { 'Content-Type': 'application/problem+json' } ), ( None, None, None, None, None, {'Content-Type': 'text/plain'}, {}, {}, { 'Content-Type': 'text/plain' } ), ( None, None, None, None, None, {'content-type': 'text/plain'}, {}, {}, { 'content-type': 'text/plain' } ) ]) def test_problem_http_response(status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers): response = problem_http_response(status, title, detail, type, instance, headers, **kwargs) assert response['statusCode'] == status assert response['headers'] == expected_headers assert json.loads(response['body']) == expected_body def test_exception_to_dict(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: exception_as_dict = e.to_dict() assert exception_as_dict == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_dict_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict() assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_dict_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict(with_traceback=True) assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_http_response(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: response = e.to_http_response() assert response['statusCode'] == 1000 assert response['headers'] == {'Content-Type': 'application/problem+json'} assert json.loads(response['body']) == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_http_response_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response() body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_exception_to_http_response_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response(with_traceback=True) body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_set_serialize_function(): set_serialize_function(lambda data: 'dummy') assert problem_http_response() == { 'statusCode': None, 'body': 'dummy', 'headers': {'Content-Type': 'application/problem+json'}, } def test_str(): assert str(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" def test_repr(): assert repr(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" ", https://github.com/cbornet/python-httpproblem,0b08139277e9cba02339eeb0996c0910d047274a,tests/test_problem.py::test_problem_http_response[None-None-None-None-None-headers3-kwargs3-expected_body3-expected_headers3],tests/test_problem.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from pytest import mark from httpproblem import problem, problem_http_response, Problem, activate_traceback, deactivate_traceback,\ set_serialize_function @mark.parametrize(""status, title, detail, type, instance, kwargs, expected"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } ), ( None, None, None, None, None, {}, {} ), ( 404, None, None, None, None, {}, { 'status': 404, 'title': 'Not Found' } ), ( 404, None, None, 'about:blank', None, {}, { 'status': 404, 'title': 'Not Found', 'type': 'about:blank' } ), ( 1000, None, None, None, None, {}, { 'status': 1000 } ), ]) def test_problem(status, title, detail, type, instance, kwargs, expected): assert problem(status, title, detail, type, instance, **kwargs) == expected @mark.parametrize(""status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'test_header_key': 'test_header_value'}, {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' }, { 'Content-Type': 'application/problem+json', 'test_header_key': 'test_header_value' } ), ( None, None, None, None, None, None, {}, {}, { 'Content-Type': 'application/problem+json' } ), ( None, None, None, None, None, {'Content-Type': 'text/plain'}, {}, {}, { 'Content-Type': 'text/plain' } ), ( None, None, None, None, None, {'content-type': 'text/plain'}, {}, {}, { 'content-type': 'text/plain' } ) ]) def test_problem_http_response(status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers): response = problem_http_response(status, title, detail, type, instance, headers, **kwargs) assert response['statusCode'] == status assert response['headers'] == expected_headers assert json.loads(response['body']) == expected_body def test_exception_to_dict(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: exception_as_dict = e.to_dict() assert exception_as_dict == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_dict_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict() assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_dict_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict(with_traceback=True) assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_http_response(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: response = e.to_http_response() assert response['statusCode'] == 1000 assert response['headers'] == {'Content-Type': 'application/problem+json'} assert json.loads(response['body']) == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_http_response_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response() body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_exception_to_http_response_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response(with_traceback=True) body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_set_serialize_function(): set_serialize_function(lambda data: 'dummy') assert problem_http_response() == { 'statusCode': None, 'body': 'dummy', 'headers': {'Content-Type': 'application/problem+json'}, } def test_str(): assert str(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" def test_repr(): assert repr(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" ", https://github.com/cbornet/python-httpproblem,0b08139277e9cba02339eeb0996c0910d047274a,tests/test_problem.py::test_problem_http_response[None-None-None-None-None-None-kwargs1-expected_body1-expected_headers1],tests/test_problem.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from pytest import mark from httpproblem import problem, problem_http_response, Problem, activate_traceback, deactivate_traceback,\ set_serialize_function @mark.parametrize(""status, title, detail, type, instance, kwargs, expected"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } ), ( None, None, None, None, None, {}, {} ), ( 404, None, None, None, None, {}, { 'status': 404, 'title': 'Not Found' } ), ( 404, None, None, 'about:blank', None, {}, { 'status': 404, 'title': 'Not Found', 'type': 'about:blank' } ), ( 1000, None, None, None, None, {}, { 'status': 1000 } ), ]) def test_problem(status, title, detail, type, instance, kwargs, expected): assert problem(status, title, detail, type, instance, **kwargs) == expected @mark.parametrize(""status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers"", [ ( 1000, 'test_title', 'test_detail', 'test_type', 'test_instance', {'test_header_key': 'test_header_value'}, {'custom': 'test_custom'}, { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' }, { 'Content-Type': 'application/problem+json', 'test_header_key': 'test_header_value' } ), ( None, None, None, None, None, None, {}, {}, { 'Content-Type': 'application/problem+json' } ), ( None, None, None, None, None, {'Content-Type': 'text/plain'}, {}, {}, { 'Content-Type': 'text/plain' } ), ( None, None, None, None, None, {'content-type': 'text/plain'}, {}, {}, { 'content-type': 'text/plain' } ) ]) def test_problem_http_response(status, title, detail, type, instance, headers, kwargs, expected_body, expected_headers): response = problem_http_response(status, title, detail, type, instance, headers, **kwargs) assert response['statusCode'] == status assert response['headers'] == expected_headers assert json.loads(response['body']) == expected_body def test_exception_to_dict(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: exception_as_dict = e.to_dict() assert exception_as_dict == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_dict_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict() assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_dict_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: exception_as_dict = e.to_dict(with_traceback=True) assert ""Traceback (most recent call last):"" in exception_as_dict['traceback'] del exception_as_dict['traceback'] assert exception_as_dict == {} def test_exception_to_http_response(): deactivate_traceback() try: raise Problem(1000, 'test_title', 'test_detail', 'test_type', 'test_instance', custom='test_custom') except Problem as e: response = e.to_http_response() assert response['statusCode'] == 1000 assert response['headers'] == {'Content-Type': 'application/problem+json'} assert json.loads(response['body']) == { 'status': 1000, 'title': 'test_title', 'detail': 'test_detail', 'type': 'test_type', 'instance': 'test_instance', 'custom': 'test_custom' } def test_exception_to_http_response_with_global_traceback(): activate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response() body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_exception_to_http_response_with_traceback_param(): deactivate_traceback() try: raise Problem() except Problem as e: response = e.to_http_response(with_traceback=True) body = json.loads(response['body']) assert ""Traceback (most recent call last):"" in body['traceback'] del body['traceback'] assert body == {} def test_set_serialize_function(): set_serialize_function(lambda data: 'dummy') assert problem_http_response() == { 'statusCode': None, 'body': 'dummy', 'headers': {'Content-Type': 'application/problem+json'}, } def test_str(): assert str(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" def test_repr(): assert repr(Problem(400)) == ""{'status': 400, 'title': 'Bad Request'}"" ", https://github.com/cbsinteractive/elemental,ca570c9f5fc43e20a4db23fc0137380f87be63f0,elemental/client_test.py::test_send_request_should_call_request_as_expected,elemental/client_test.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import json import os import mock import pytest import requests from .client import (ElementalException, ElementalLive, InvalidRequest, InvalidResponse) USER = ""FAKE"" API_KEY = ""FAKE"" ELEMENTAL_ADDRESS = ""FAKE_ADDRESS.com"" HEADERS = {'Accept': 'application/xml', 'Content-Type': 'application/xml'} REQUEST_BODY = ""FAKE"" def file_fixture(file_name): with open(os.path.join(""elemental/test_templates"", file_name)) as f: return f.read() def mock_response(status=200, content=None, text=None, json_data=None, raise_for_status=None): mock_resp = mock.Mock() mock_resp.raise_for_status = mock.Mock() # mock raise_for_status call w/optional error if raise_for_status: mock_resp.raise_for_status.side_effect = raise_for_status # set status code and content mock_resp.status_code = status mock_resp.content = content mock_resp.text = text # add json data if provided if json_data: mock_resp.json = mock.Mock(return_value=json_data) return mock_resp def test_ElementalLive_should_receive_server_ip(): e = ElementalLive(ELEMENTAL_ADDRESS) assert e.server_ip == ELEMENTAL_ADDRESS def test_generate_header_with_authentication_should_contain_user(): client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) headers = client.generate_headers(f'{ELEMENTAL_ADDRESS}/live_events') assert headers['X-Auth-User'] == USER assert headers['Accept'] == 'application/xml' assert headers['Content-Type'] == 'application/xml' def test_genterate_header_without_authentication_should_not_contain_user(): client = ElementalLive(ELEMENTAL_ADDRESS) headers = client.generate_headers() assert 'X-Auth-User' not in headers assert headers['Accept'] == 'application/xml' assert headers['Content-Type'] == 'application/xml' @mock.patch('requests.request') def test_send_request_should_call_request_as_expected(mock_request): mock_request.return_value = mock_response(status=200) client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) client.send_request( 'POST', f'{ELEMENTAL_ADDRESS}/live_events', HEADERS, REQUEST_BODY) request_to_elemental = mock_request.call_args_list[0][1] assert request_to_elemental['url'] == f'{ELEMENTAL_ADDRESS}/live_events' assert request_to_elemental['method'] == 'POST' assert request_to_elemental['headers']['Accept'] == 'application/xml' assert request_to_elemental['headers']['Content-Type'] == 'application/xml' @mock.patch('requests.request') def test_send_request_should_return_response_on_correct_status_code( mock_request): response_from_elemental_api = file_fixture('success_response_for_' 'create.xml') mock_request.return_value = mock_response( status=201, text=response_from_elemental_api) client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) response = client.send_request( 'POST', f'{ELEMENTAL_ADDRESS}/live_events', HEADERS, REQUEST_BODY) assert response.text == response_from_elemental_api assert response.status_code == 201 @mock.patch('requests.request') def test_send_request_should_raise_InvalidRequest_on_RequestException( mock_request): mock_request.side_effect = requests.exceptions.RequestException() client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) with pytest.raises(InvalidRequest): client.send_request( 'POST', f'{ELEMENTAL_ADDRESS}/live_events', HEADERS, REQUEST_BODY) @mock.patch('requests.request') def test_send_request_should_raise_InvalidResponse_on_invalid_status_code( mock_request): response_from_elemental_api = file_fixture('fail_to_create_response.xml') mock_request.return_value = mock_response( status=404, text=response_from_elemental_api) client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) with pytest.raises(InvalidResponse) as exc_info: client.send_request( 'POST', f'{ELEMENTAL_ADDRESS}/live_events', HEADERS, REQUEST_BODY) assert str(exc_info.value).endswith( f""Response: 404\n{response_from_elemental_api}"") def test_create_event_should_call_send_request_as_expect_and_return_event_id(): client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) client.generate_headers = mock.Mock() client.generate_headers.return_value = HEADERS client.send_request = mock.Mock() response_from_elemental_api = file_fixture('success_response_for_' 'create.xml') client.send_request.return_value = mock_response( status=201, content=response_from_elemental_api) event_id = client.create_event({'username': os.getenv('ACCESS_KEY'), 'password': os.getenv('SECRET_KEY'), 'mediastore_container_master': 'https://hu5n3jjiyi2jev.data.media' 'store.us-east-1.amazonaws.com/master', 'mediastore_container_backup': 'https://hu5n3jjiyi2jev.data.medias' 'tore.us-east-1.amazonaws.com/backup', 'input_device': {'id': '1', 'name': None, 'device_name': 'HD-SDI 1', 'device_number': '0', 'device_type': 'AJA', 'description': 'AJA Capture Card', 'channel': '1', 'channel_type': 'HD-SDI', 'quad': 'false', 'availability': False}}) response_from_elemental_api = client.send_request.call_args_list[0][1] assert response_from_elemental_api['http_method'] == 'POST' assert response_from_elemental_api['url'] == \ f'{ELEMENTAL_ADDRESS}/live_events' assert response_from_elemental_api['headers'] == HEADERS assert event_id == {'id': '80'} def test_delete_event_should_call_send_request_as_expect(): client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) client.generate_headers = mock.Mock() client.generate_headers.return_value = HEADERS client.send_request = mock.Mock() client.send_request.return_value = mock_response(status=200) event_id = 999 client.delete_event(event_id) client.send_request.assert_called_once_with( http_method='DELETE', url=f'{ELEMENTAL_ADDRESS}/live_events/{event_id}', headers=HEADERS) def test_start_event_should_call_send_request_as_expect(): client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) client.generate_headers = mock.Mock() client.generate_headers.return_value = HEADERS client.send_request = mock.Mock() client.send_request.return_value = mock_response(status=200) event_id = 999 client.start_event(event_id) client.send_request.assert_called_once_with( http_method='POST', url=f'{ELEMENTAL_ADDRESS}/live_events/{event_id}/start', headers=HEADERS, body="""") def test_stop_event_should_call_send_request_as_expect(): client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) client.generate_headers = mock.Mock() client.generate_headers.return_value = HEADERS client.send_request = mock.Mock() client.send_request.return_value = mock_response(status=200) event_id = 999 client.stop_event(event_id) client.send_request.assert_called_once_with( http_method='POST', url=f'{ELEMENTAL_ADDRESS}/live_events/{event_id}/stop', headers=HEADERS, body="""") def send_request_side_effect(**kwargs): if kwargs['url'] == f'{ELEMENTAL_ADDRESS}/live_events': return mock_response(status=200, text=file_fixture('sample_event_list.xml')) else: return mock_response(status=200, text=file_fixture('sample_device_list.xml')) def test_find_devices_in_use_will_call_send_request_as_expect(): client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) client.generate_headers = mock.Mock() client.generate_headers.return_value = HEADERS client.send_request = mock.Mock() client.send_request.return_value = \ mock_response(status=200, text=file_fixture('sample_event_list.xml')) client.find_devices_in_use() client.send_request.assert_called_with(http_method=""GET"", url=f'{ELEMENTAL_ADDRESS}' f'/live_events?' f'filter=active', headers=HEADERS) def test_find_devices_in_use_will_return_in_used_devices(): client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) client.generate_headers = mock.Mock() client.generate_headers.return_value = HEADERS client.send_request = mock.Mock() client.send_request.return_value = \ mock_response(status=200, text=file_fixture('sample_event_list.xml')) devices = client.find_devices_in_use() assert devices == {'HD-SDI 1'} def test_get_input_devices_will_call_send_request_as_expect(): client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) client.generate_headers = mock.Mock() client.generate_headers.return_value = HEADERS client.send_request = mock.Mock() client.find_devices_in_use = mock.Mock() client.find_devices_in_use.return_value = (""HD-SDI 1"",) ", https://github.com/cesar-rodriguez/terrascan,fd4ca9d23e694185fd5cb9ddf357b52b15adc6ca,tests/test_terrascan.py::test_success,tests/test_terrascan.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Tests for `terrascan` package."""""" import pytest from terrascan import terrascan def test_with_empty_args(): """""" User passes no args, should fail with SystemExit """""" with pytest.raises(SystemExit): terrascan.main(['-h']) def test_success(): """""" Test successful terraform templates """""" with pytest.raises(SystemExit) as pytest_wrapped_e: terrascan.main([ '-l', 'tests/infrastructure/success', ]) assert pytest_wrapped_e.value.code == 0 def test_fail(): """""" Test successful terraform templates """""" with pytest.raises(SystemExit) as pytest_wrapped_e: terrascan.main([ '-l', 'tests/infrastructure/fail', ]) assert pytest_wrapped_e.value.code == 4", https://github.com/chakki-works/chazutsu,52b2f8acaac1b6c7eab97dded1a1757d3adcefa7,tests/test_movie_review.py::test_download,tests/test_movie_review.py,NOD,flaky,Open,https://github.com/chakki-works/chazutsu/pull/13,classify;root_cause,"import os import sys import shutil import unittest sys.path.append(os.path.join(os.path.dirname(__file__), ""../"")) import chazutsu.datasets DATA_ROOT = os.path.join(os.path.dirname(__file__), ""data/mr"") if not os.path.exists(DATA_ROOT): os.mkdir(DATA_ROOT) class TestMovieReview(unittest.TestCase): @classmethod def tearDownClass(cls): if os.path.exists(DATA_ROOT): shutil.rmtree(DATA_ROOT) def test_prepare_polarity(self): d = chazutsu.datasets.MovieReview.polarity() dataset_root, extracted = d.save_and_extract(DATA_ROOT) path = d.prepare(dataset_root, extracted) pos = 0 neg = 0 try: with open(path, encoding=""utf-8"") as f: for ln in f: els = ln.strip().split(""\t"") if len(els) != 2: raise Exception(""data file is not constructed by label and text."") if els[0] == ""1"": pos += 1 else: neg += 1 except Exception as ex: d.clear_trush() self.fail(ex) count = d.get_line_count(path) d.clear_trush() os.remove(path) # pos=1000, neg=1000 self.assertEqual(count, 2000) self.assertEqual(pos, 1000) self.assertEqual(neg, 1000) def test_extract_polarity_v1(self): d = chazutsu.datasets.MovieReview.polarity_v1() dataset_root, extracted = d.save_and_extract(DATA_ROOT) path = d.prepare(dataset_root, extracted) pos = 0 neg = 0 try: with open(path, encoding=""utf-8"") as f: for ln in f: els = ln.strip().split(""\t"") if len(els) != 2: raise Exception(""data file is not constructed by label and text."") if els[0] == ""1"": pos += 1 else: neg += 1 except Exception as ex: d.clear_trush() self.fail(ex) count = d.get_line_count(path) d.clear_trush() # pos=1000, neg=1000 self.assertEqual(count, 5331 + 5331) self.assertEqual(pos, 5331) self.assertEqual(neg, 5331) def test_extract_rating(self): d = chazutsu.datasets.MovieReview.rating() dataset_root, extracted = d.save_and_extract(DATA_ROOT) path = d.prepare(dataset_root, extracted) try: with open(path, encoding=""utf-8"") as f: for ln in f: els = ln.strip().split(""\t"") if len(els) != 2: raise Exception(""data file is not constructed by label and text."") except Exception as ex: d.clear_trush() self.fail(ex) count = d.get_line_count(path) d.clear_trush() self.assertTrue(count > 0) def test_extract_subjectivity(self): d = chazutsu.datasets.MovieReview.subjectivity() dataset_root, extracted = d.save_and_extract(DATA_ROOT) path = d.prepare(dataset_root, extracted) sub = 0 obj = 0 try: with open(path, encoding=""utf-8"") as f: for ln in f: els = ln.strip().split(""\t"") if len(els) != 2: raise Exception(""data file is not constructed by label and text."") if els[0] == ""1"": sub += 1 else: obj += 1 except Exception as ex: d.clear_trush() self.fail(ex) count = d.get_line_count(path) d.clear_trush() # sub=5000, obj=5000 self.assertEqual(count, 5000*2) self.assertEqual(sub, 5000) self.assertEqual(obj, 5000) def test_download(self): r = chazutsu.datasets.MovieReview.subjectivity().download(DATA_ROOT) target, data = r.test_data(split_target=True) self.assertEqual(target.shape[0], data.shape[0]) r.make_vocab(vocab_size=1000) X, y = r.column(""review"").as_word_seq(fixed_len=20).to_batch(""train"", with_target=True) self.assertEqual(y.shape, (len(y), 1)) self.assertEqual(X.shape, (len(y), 20, len(r.vocab))) backed = r.column(""review"").back(X) print(backed[:3]) shutil.rmtree(r.root) if __name__ == ""__main__"": unittest.main() ", https://github.com/chaosmail/python-fs,19597eeff0c5fdfaef59bb5b9c15ce9f58476bfc,fs/tests/test_addpath.py::test_addpath,fs/tests/test_addpath.py,NIO,flaky,Open,https://github.com/chaosmail/python-fs/pull/12,classify;root_cause,"import fs import pytest import sys from .setup import * def test_addpath(): TEST_MODULE = 'test_foo_bar.py' fs.touch(fs.join(TEST_DIR, TEST_MODULE)) with pytest.raises(ImportError): import test_foo_bar fs.addpath(TEST_DIR) import test_foo_bar sys.path.remove(TEST_DIR) del sys.modules['test_foo_bar'] def test_addpath_not_existing_path(): WRONG_TEST_DIR = 'foobartest' with pytest.raises(ValueError): fs.addpath(WRONG_TEST_DIR) ", https://github.com/chaosmail/python-fs,2567922ced9387e327e65f3244caff3b7af35684,fs/tests/test_addpath.py::test_addpath,fs/tests/test_addpath.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"import fs import pytest from .setup import * def test_addpath(): TEST_MODULE = 'test_foo_bar.py' fs.touch(fs.join(TEST_DIR, TEST_MODULE)) with pytest.raises(ImportError): import test_foo_bar fs.addpath(TEST_DIR) import test_foo_bar def test_addpath_not_existing_path(): WRONG_TEST_DIR = 'foobartest' with pytest.raises(ValueError): fs.addpath(WRONG_TEST_DIR)", https://github.com/chaosmail/python-fs,2567922ced9387e327e65f3244caff3b7af35684,fs/tests/test_mkdir.py::test_mkdir,fs/tests/test_mkdir.py,NIO,flaky,Accepted,https://github.com/chaosmail/python-fs/pull/9,classify;root_cause;fix_proposal,"import os.path import fs import unittest from .setup import * def test_mkdir(): dir_name = ""foo"" path = os.path.join(TEST_DIR, dir_name) if (os.path.exists(path)): raise ValueError(""Directory %s already exists!"" % path) fs.mkdir(path) assert os.path.exists(path) is True def test_mkdir_recursive(): dir_name = ""foo/bar/baz"" path = os.path.join(TEST_DIR, dir_name) if (os.path.exists(path)): raise ValueError(""Directory %s already exists!"" % path) fs.mkdir(path) assert os.path.exists(path) is True def test_mkdir_recursive_fail(): dir_name = ""foo/bar/bal"" path = os.path.join(TEST_DIR, dir_name) if (os.path.exists(path)): raise ValueError(""Directory %s already exists!"" % path) try: fs.mkdir(path, recursive=False) except FileNotFoundError: pass", https://github.com/chaosmail/python-fs,2567922ced9387e327e65f3244caff3b7af35684,fs/tests/test_mkdir.py::test_mkdir_recursive,fs/tests/test_mkdir.py,NIO,flaky,Accepted,https://github.com/chaosmail/python-fs/pull/9,classify;root_cause;fix_proposal,"import os.path import fs import unittest from .setup import * def test_mkdir(): dir_name = ""foo"" path = os.path.join(TEST_DIR, dir_name) if (os.path.exists(path)): raise ValueError(""Directory %s already exists!"" % path) fs.mkdir(path) assert os.path.exists(path) is True def test_mkdir_recursive(): dir_name = ""foo/bar/baz"" path = os.path.join(TEST_DIR, dir_name) if (os.path.exists(path)): raise ValueError(""Directory %s already exists!"" % path) fs.mkdir(path) assert os.path.exists(path) is True def test_mkdir_recursive_fail(): dir_name = ""foo/bar/bal"" path = os.path.join(TEST_DIR, dir_name) if (os.path.exists(path)): raise ValueError(""Directory %s already exists!"" % path) try: fs.mkdir(path, recursive=False) except FileNotFoundError: pass", https://github.com/chaosmail/python-fs,2567922ced9387e327e65f3244caff3b7af35684,fs/tests/test_rename.py::test_rename_directory,fs/tests/test_rename.py,NIO,flaky,Accepted,https://github.com/chaosmail/python-fs/pull/9,classify;root_cause;fix_proposal,"import os.path import fs from .setup import * def test_rename_file(): rename_to = ""test_rename.txt"" rename_file = os.path.join(TEST_DIR, rename_to) if (os.path.exists(rename_file)): raise ValueError(""File %s already exists!"" % rename_file) fs.rename(TEST_FILE, rename_file) assert os.path.exists(rename_file) is True def test_rename_directory(): rename_to = ""test_rename"" rename_dir = os.path.join(TEST_DIR, rename_to) if (os.path.exists(rename_dir)): raise ValueError(""Directory %s already exists!"" % rename_dir) fs.rename(TEST_DIR_2, rename_dir) assert os.path.exists(rename_dir) is True", https://github.com/chaosmail/python-fs,2567922ced9387e327e65f3244caff3b7af35684,fs/tests/test_rename.py::test_rename_file,fs/tests/test_rename.py,NIO,flaky,Accepted,https://github.com/chaosmail/python-fs/pull/9,classify;root_cause;fix_proposal,"import os.path import fs from .setup import * def test_rename_file(): rename_to = ""test_rename.txt"" rename_file = os.path.join(TEST_DIR, rename_to) if (os.path.exists(rename_file)): raise ValueError(""File %s already exists!"" % rename_file) fs.rename(TEST_FILE, rename_file) assert os.path.exists(rename_file) is True def test_rename_directory(): rename_to = ""test_rename"" rename_dir = os.path.join(TEST_DIR, rename_to) if (os.path.exists(rename_dir)): raise ValueError(""Directory %s already exists!"" % rename_dir) fs.rename(TEST_DIR_2, rename_dir) assert os.path.exists(rename_dir) is True", https://github.com/chaosmail/python-fs,2567922ced9387e327e65f3244caff3b7af35684,fs/tests/test_touch.py::test_touch_on_new_file,fs/tests/test_touch.py,NIO,flaky,Accepted,https://github.com/chaosmail/python-fs/pull/9,classify;root_cause;fix_proposal,"import os import os.path import time import pytest import fs from .setup import * def test_touch_on_existing_file(): before_time = time.ctime(os.path.getmtime(TEST_FILE)) fs.touch(TEST_FILE) after_time = time.ctime(os.path.getmtime(TEST_FILE)) assert after_time >= before_time def test_touch_on_new_file(): new_file = os.path.join(TEST_DIR, ""new_file.txt"") if (os.path.exists(new_file)): raise ValueError(""File new_file.txt already exists!"") fs.touch(new_file) assert os.path.exists(new_file) is True @pytest.mark.skipif(os.name == ""nt"", reason=""does not work on windows"") def test_touch_on_directory(): before_time = time.ctime(os.path.getmtime(TEST_DIR)) fs.touch(TEST_DIR) after_time = time.ctime(os.path.getmtime(TEST_DIR)) assert after_time >= before_time ", https://github.com/chie8842/expstock,28b7ece83fa601efd2b6c4b8b7bdd1a3b9ebc1df,expstock/test_expstock.py::test_append_param,expstock/test_expstock.py,NIO,flaky,Opened,https://github.com/chie8842/expstock/pull/18,classify;root_cause,"from __future__ import absolute_import from __future__ import division from __future__ import print_function from datetime import datetime import mock import os import platform import pytest from freezegun import freeze_time from expstock.expstock import ExpStock @freeze_time('2018-04-01') def test_variables(init_ExpStock): e = init_ExpStock.__next__() assert e.stock_root_dir == 'experiments' assert e.params == [] assert e.memo == '' assert e.git_check == True print(datetime.now()) print(e.log_dirname) assert e.log_dirname == os.path.abspath('experiments/20180401_000000_Untitled') def test_variables_with_args(init_ExpStock_with_args): e = init_ExpStock_with_args.__next__() #patched_time = datetime.now() #mocker.patch.object(e._set_dirname, autospec='experiments/20180401') assert e.stock_root_dir == 'experiments' assert e.params == [('a', 'This is a test param'), ('b', 12345), ('c', {'Name': 'Chie Hayashida'}) ] assert e.memo == 'This is a test memo' assert e.git_check == False print(datetime.now()) print(e.log_dirname) assert e.log_dirname == os.path.abspath('test_logs/test') def test__mk_logdir(init_ExpStock_with_args): e = init_ExpStock_with_args.__next__() print(e.log_dirname) e._mk_logdir() assert os.path.isdir(e.log_dirname) def test__get_machine_info(init_ExpStock): e = init_ExpStock.__next__() machine_info = e._get_machine_info() assert machine_info[0] == ('system', platform.system()) assert machine_info[1] == ('node', platform.node()) assert machine_info[2] == ('release', platform.release()) assert machine_info[3] == ('version', platform.version()) assert machine_info[4] == ('machine', platform.machine()) assert machine_info[5] == ('processor', platform.processor()) def test__get_git_info(init_ExpStock): import subprocess e = init_ExpStock.__next__() git_head, git_diff = e._get_git_info() expect_git_head = subprocess.check_output(['git', 'log', '-n', '1', '--format=%H']) expect_git_diff = subprocess.check_output(['git', 'diff']) print(git_head) print('test') assert git_head == expect_git_head assert git_diff == expect_git_diff def test_append_param(init_ExpStock): e = init_ExpStock.__next__() test_param1 = 12345 test_param2 = 'test_param' e.append_param(test_param1=test_param1, test_param2=test_param2) e.params.sort() assert e.params == [('test_param1', 12345), ('test_param2', 'test_param')] print(e.params.sort()) def test_set_memo(init_ExpStock): e = init_ExpStock.__next__() test_memo = 'This is a test experiment.' e.set_memo(test_memo) print(e.memo) assert e.memo == test_memo def test__write_log(init_ExpStock_with_args): e = init_ExpStock_with_args.__next__() filename = 'git_head.txt' filepath = os.path.join(e.log_dirname, filename) e._write_log(filename, b'testtesttest', 'wb') assert os.path.isfile(filepath) with open(filepath) as f: data = f.read() assert data == 'testtesttest' def test__write_logs(init_ExpStock_with_args): e = init_ExpStock_with_args.__next__() filename = 'params.txt' filepath = os.path.join(e.log_dirname, filename) e._write_logs(filename, e.params) assert os.path.isfile(filepath) with open(filepath) as f: data = f.read() print(data) assert data == '''a = This is a test param b = 12345 c = {'Name': 'Chie Hayashida'} ''' def test__create_report(init_ExpStock_with_args): e = init_ExpStock_with_args.__next__() e._create_report() filename = 'report.txt' filepath = os.path.join(e.log_dirname, filename) assert os.path.isfile(filepath) ", https://github.com/chinapandaman/PyPDFForm,fcc1b297005a39900c1f928f602cbc5f38a1d60e,tests/unit/test_font.py::test_register_font_and_is_registered,tests/unit/test_font.py,NIO,flaky,Accepted,https://github.com/chinapandaman/PyPDFForm/pull/254,classify;root_cause;fix_proposal,"# -*- coding: utf-8 -*- import os import pytest from PyPDFForm.core.font import Font as FontCore @pytest.fixture def font_samples(): return os.path.join(os.path.dirname(__file__), "".."", "".."", ""font_samples"") def test_register_bad_font(): assert not FontCore().register_font(""foo"", b""bar"") def test_register_font_and_is_registered(font_samples): assert not FontCore().is_registered(""LiberationSerif"") with open(os.path.join(font_samples, ""LiberationSerif-Regular.ttf""), ""rb+"") as f: FontCore().register_font(""LiberationSerif"", f.read()) assert FontCore().is_registered(""LiberationSerif"") ", https://github.com/chinapnr/fishbase,1615ccff9106bf42fc6ae862cd13353c12c1b76d,test/test_logger.py::TestFishLogging::test_format3,test/test_logger.py,OD-Brit,flaky,MovedOrRenamed,nan,classify;root_cause,"# coding=utf-8 # fish_logging.py 单元测试 # 2018.7.17 create by Hu Jun import os import time import shutil import pytest from fishbase.fish_logger import set_log_file, logger as log # 定义当前路径 current_path = os.path.dirname(os.path.abspath(__file__)) class TestFishLogging(object): log_filename = '' suffix = ""%Y-%m-%d"" suffix_time = '' log_path = '' def setup_class(self): # 定义配置文件名 self.log_path = os.path.join(current_path, 'log_path') if not os.path.exists(self.log_path): os.mkdir(self.log_path) self.log_filename = os.path.join(self.log_path, 'unittest.log') current_time_tuple = time.localtime() self.suffix_time = time.strftime(self.suffix, current_time_tuple) def teardown_class(self): try: # 关闭日志文件句柄 for h in log.handlers: h.close() shutil.rmtree(self.log_path) except Exception as _: pass def test_format1(self): set_log_file(self.log_filename) log.info('test_format1') assert 'unittest.log.{}'.format(self.suffix_time) in os.listdir(self.log_path) def test_format2(self): set_log_file(self.log_filename, file_name_format='%project_name-%date-%log') log.info('test_format2') assert 'unittest.{}.log'.format(self.suffix_time) in os.listdir(self.log_path) def test_format3(self): set_log_file(self.log_filename, file_name_format='%date-%project_name-%log') log.info('test_format3') assert 'unittest.log.{}'.format(self.suffix_time) in os.listdir(self.log_path) def test_format4(self): with pytest.raises(ValueError): set_log_file(self.log_filename, file_name_format='%date-%project_name-%log1') ", https://github.com/chinapnr/fishbase,ddd1cced20caff3518f13073381d31461caa39a5,test/test_logger.py::TestFishLogging::test_format_03,test/test_logger.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"# coding=utf-8 # fish_logging.py 单元测试 # 2018.7.17 create by Hu Jun import os import time import shutil import pytest from fishbase.fish_logger import set_log_file, logger as log # 定义当前路径 current_path = os.path.dirname(os.path.abspath(__file__)) class TestFishLogging(object): log_filename = '' suffix = ""%Y-%m-%d"" suffix_time = '' log_path = '' def setup_class(self): # 定义配置文件名 self.log_path = os.path.join(current_path, 'log_path') if not os.path.exists(self.log_path): os.mkdir(self.log_path) self.log_filename = os.path.join(self.log_path, 'unittest.log') current_time_tuple = time.localtime() self.suffix_time = time.strftime(self.suffix, current_time_tuple) def teardown_class(self): try: # 关闭日志文件句柄 for h in log.handlers: h.close() shutil.rmtree(self.log_path) except Exception as _: pass def test_format_01(self): set_log_file(self.log_filename) log.info('test_format1') assert 'unittest.log.{}'.format(self.suffix_time) in os.listdir(self.log_path) def test_format_02(self): set_log_file(self.log_filename, file_name_format='%project_name-%date-%log') log.info('test_format2') assert 'unittest.{}.log'.format(self.suffix_time) in os.listdir(self.log_path) def test_format_03(self): set_log_file(self.log_filename, file_name_format='%date-%project_name-%log') log.info('test_format3') assert 'unittest.log.{}'.format(self.suffix_time) in os.listdir(self.log_path) def test_format_04(self): with pytest.raises(ValueError): set_log_file(self.log_filename, file_name_format='%date-%project_name-%log1') def test_without_dir(self): for h in log.handlers: h.close() shutil.rmtree(self.log_path) set_log_file(self.log_filename) assert os.path.exists(self.log_path) ", https://github.com/CitrineInformatics/citrine-python,cf2ec34cad3f1ff44478cad44e4918d7925cba25,tests/_util/test_functions.py::test_shadow_classes_in_module,tests/_util/test_functions.py,NIO,flaky,Accepted,https://github.com/CitrineInformatics/citrine-python/pull/690,classify;root_cause;fix_proposal,"import uuid import pytest import warnings from mock import patch, call, mock_open from gemd.entity.bounds.real_bounds import RealBounds from gemd.entity.link_by_uid import LinkByUID from citrine._utils.functions import get_object_id, validate_type, object_to_link_by_uid, \ rewrite_s3_links_locally, write_file_locally, shadow_classes_in_module, migrate_deprecated_argument, \ format_escaped_url from gemd.entity.attribute.property import Property from citrine.resources.condition_template import ConditionTemplate def test_get_object_id_from_base_attribute(): with pytest.raises(ValueError): get_object_id(Property('some property')) def test_get_object_id_from_data_concepts(): uid = str(uuid.uuid4()) template = ConditionTemplate( name='test', bounds=RealBounds(0.0, 1.0, ''), uids={'id': uid} ) assert uid == get_object_id(template) def test_get_object_id_from_data_concepts_id_is_none(): template = ConditionTemplate( name='test', bounds=RealBounds(0.0, 1.0, '') ) with pytest.raises(ValueError): template.uids = {'id': None} def test_get_object_id_link_by_uid_bad_scope(): with pytest.raises(ValueError): get_object_id(LinkByUID('bad_scope', '123')) def test_get_object_id_wrong_type(): with pytest.raises(TypeError): get_object_id('no id here') def test_validate_type_wrong_type(): with pytest.raises(Exception): validate_type({'type': 'int'}, 'foo') def test_validate_type_set_type(): assert {'type': 'int'} == validate_type({}, 'int') def test_object_to_link_by_uid_missing_uids(): assert {'foo': 'bar'} == object_to_link_by_uid({'foo': 'bar'}) def test_rewrite_s3_links_locally(): assert ""http://localhost:9572"" == rewrite_s3_links_locally(""http://localstack:4572"") @patch(""os.path.isdir"") @patch(""os.path.join"") @patch(""os.makedirs"") def test_write_file_locally(mock_makedirs, mock_join, mock_isdir): mock_isdir.return_value = False mock_join.return_value = ""/User/is/fake/myfile.pdf"" with patch(""builtins.open"", mock_open()) as m: write_file_locally(b""something"", ""/User/is/fake/myfile.pdf"") assert m.call_args_list == [call('/User/is/fake/myfile.pdf', 'wb')] handle = m() handle.write.assert_called_once_with(b'something') mock_makedirs.assert_called_once_with(""/User/is/fake"") def test_write_file_locally_fails_with_no_filename(): with pytest.raises(ValueError): write_file_locally(b""anything"", ""/user/is/"") def test_shadow_classes_in_module(): # Given from tests._util import source_mod, target_mod assert getattr(target_mod, 'ExampleClass', None) == None # When shadow_classes_in_module(source_mod, target_mod) # Then (ensure the class is copied over) copied_class = getattr(target_mod, 'ExampleClass', None) # Do this vs a direct ref so IJ doesn't warn us assert copied_class == source_mod.ExampleClass # Python also considers the classes equivalent assert issubclass(copied_class, source_mod.ExampleClass) assert issubclass(source_mod.ExampleClass, copied_class) # Reset target_mod status for attr in dir(target_mod): delattr(target_mod, attr) def test_migrate_deprecated_argument(): with pytest.raises(ValueError): # ValueError if neither argument is specified migrate_deprecated_argument(None, ""new name"", None, ""old name"") with pytest.raises(ValueError): # ValueError if both arguments are specified migrate_deprecated_argument(""something"", ""new name"", ""something else"", ""old name"") # Return the value if the new argument is specified assert migrate_deprecated_argument(14, ""new name"", None, ""old name"") == 14 with warnings.catch_warnings(record=True) as caught: # If the old argument is specified, return the value and throw a deprecation warning assert migrate_deprecated_argument(None, ""new name"", 15, ""old name"") == 15 assert issubclass(caught[0].category, DeprecationWarning) msg = str(caught[0].message) assert ""old name"" in msg and ""new name"" in msg def test_format_escaped_url(): url = format_escaped_url('http://base.com/{}/{}/{word1}/{word2}', 1, '&', word1='fine', word2='+/?#') assert 'http://base.com/' in url assert 'fine' in url assert '1' in url for c in '&' + '+?#': assert c not in url assert 6 == sum(c == '/' for c in url) ", https://github.com/clapeyre/h5nav,1c866a5085f1605a51064f5642e91d3682f8f572,tests/test_cli.py::test_cat,tests/test_cli.py,ID,flaky,Opened,https://github.com/clapeyre/h5nav/pull/7,classify;root_cause,"import os import pytest import numpy as np from .context import cli, setup_module, teardown_module, interp # `get_whitespace_name` command def test_get_whitespace_name(interp): name = interp.get_whitespace_name(""Group1"") assert name == ""Group1"" name = interp.get_whitespace_name("" Group2"") assert name == "" Group2"" def test_get_whitespace_fail(interp): with pytest.raises(cli.UnknownLabelError): interp.get_whitespace_name('zzz') # `help` functions def test_helps_work(capsys, interp): for cmd in [""help_"" + cmd[3:] for cmd in dir(interp) if cmd[:3] == ""do_""]: if cmd == ""help_help"": continue assert hasattr(interp, cmd) # `ls` command def test_ls_simple(capsys, interp): interp.do_ls('') out, err = capsys.readouterr() assert out == "" Group2/ Group1/\n"" def test_ls_star(capsys, interp): interp.do_ls('*') out, err = capsys.readouterr() assert out == """"""\ Group2/ field1 Group1/ Subgroup1/ field1 ./ \n"""""" def test_ls_unknown_label(capsys, interp): interp.do_ls('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_ls('zzz') # `cd` command def test_cd_group(interp): assert interp.position == '/' interp.do_cd(' Group2') assert interp.position == '/ Group2/' def test_cd_dataset(capsys, interp): interp.do_cd('Group1') interp.do_cd('field1') out, err = capsys.readouterr() assert out == ""*** can only cd into groups\n"" def test_cd_dotdot(interp): interp.do_cd(' Group2') interp.do_cd('..') assert interp.position == '/' def test_cd_root(interp): interp.do_cd('Group1') interp.do_cd('Subgroup1') interp.do_cd('') assert interp.position == '/' def test_cd_2args(capsys, interp): interp.do_cd('Group1 Group2') out, err = capsys.readouterr() assert out == ""*** invalid number of arguments\n"" def test_cd_unknown_label(capsys, interp): interp.do_cd('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_cd('zzz') # `cat` command def test_cat(capsys, interp): interp.do_cd(""Group1"") interp.do_cat(""field1"") out, err = capsys.readouterr() assert out == ""information\n"" def test_cat_star(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_cat(""*"") out, err = capsys.readouterr() assert out == """"""\ field2 : [ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198] field1 : [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99] """""" # `stats` command def test_stats(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_stats(""field1"") out, err = capsys.readouterr() assert out == """"""\ Type mean +/- std*2 [ min, max ] (Shape) -------------------------------------------------------------------------- int64 4.9500e+01 +/- 5.7732e+01 [ 0.0000e+00, 9.9000e+01] (100,) """""" # `pdf` command def test_pdf(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_pdf(""field1"") out, err = capsys.readouterr() assert out == """"""\ Min Max | Pdf (10 buckets) ---------------------------------------------- 0.0000e+00 9.9000e+01 | [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] """""" # `dump` command def test_dump(interp): fname = ""field1.npy"" interp.do_cd("" Group2"") interp.do_dump(""field1"") data = np.load(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `txt_dump` command def test_txt_dump(interp): fname = ""field1.txt"" interp.do_cd("" Group2"") interp.do_txt_dump(""field1"") data = np.loadtxt(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `rm` command def test_rm_dataset(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_rm(""field1"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == "" field2"" # `rm` command def test_rm_group(capsys, interp): interp.do_rm(""Group2"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == ""Group1/"" # `exit` command def test_exit(capsys, interp): with pytest.raises(SystemExit): interp.do_exit('') out, err = capsys.readouterr() assert out == ""Bye!\n"" ", https://github.com/clapeyre/h5nav,595040c2f9b65196ceaf18d1c2c58d848466a22c,tests/test_cli.py::test_cat_star,tests/test_cli.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest import numpy as np from .context import cli, setup_module, teardown_module, interp # `get_whitespace_name` command def test_get_whitespace_name(interp): name = interp.get_whitespace_name(""Group1"") assert name == ""Group1"" name = interp.get_whitespace_name("" Group2"") assert name == "" Group2"" def test_get_whitespace_fail(interp): with pytest.raises(cli.UnknownLabelError): interp.get_whitespace_name('zzz') # `help` functions def test_helps_work(capsys, interp): for cmd in [""help_"" + cmd[3:] for cmd in dir(interp) if cmd[:3] == ""do_""]: if cmd == ""help_help"": continue assert hasattr(interp, cmd) # `ls` command def test_ls_simple(capsys, interp): interp.do_ls('') out, err = capsys.readouterr() assert out == "" Group2/ Group1/\n"" def test_ls_star(capsys, interp): interp.do_ls('*') out, err = capsys.readouterr() assert out == """"""\ Group2/ field1 Group1/ Subgroup1/ field1 ./ \n"""""" def test_ls_unknown_label(capsys, interp): interp.do_ls('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_ls('zzz') # `cd` command def test_cd_group(interp): assert interp.position == '/' interp.do_cd(' Group2') assert interp.position == '/ Group2/' def test_cd_dataset(capsys, interp): interp.do_cd('Group1') interp.do_cd('field1') out, err = capsys.readouterr() assert out == ""*** can only cd into groups\n"" def test_cd_dotdot(interp): interp.do_cd(' Group2') interp.do_cd('..') assert interp.position == '/' def test_cd_root(interp): interp.do_cd('Group1') interp.do_cd('Subgroup1') interp.do_cd('') assert interp.position == '/' def test_cd_2args(capsys, interp): interp.do_cd('Group1 Group2') out, err = capsys.readouterr() assert out == ""*** invalid number of arguments\n"" def test_cd_unknown_label(capsys, interp): interp.do_cd('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_cd('zzz') # `cat` command def test_cat(capsys, interp): interp.do_cd(""Group1"") interp.do_cat(""field1"") out, err = capsys.readouterr() assert out == ""information\n"" def test_cat_star(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_cat(""*"") out, err = capsys.readouterr() assert out == """"""\ field2 : [ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198] field1 : [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99] """""" # `stats` command def test_stats(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_stats(""field1"") out, err = capsys.readouterr() assert out == """"""\ Type mean +/- std*2 [ min, max ] (Shape) -------------------------------------------------------------------------- int64 4.9500e+01 +/- 5.7732e+01 [ 0.0000e+00, 9.9000e+01] (100,) """""" # `pdf` command def test_pdf(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_pdf(""field1"") out, err = capsys.readouterr() assert out == """"""\ Min Max | Pdf (10 buckets) ---------------------------------------------- 0.0000e+00 9.9000e+01 | [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] """""" # `dump` command def test_dump(interp): fname = ""field1.npy"" interp.do_cd("" Group2"") interp.do_dump(""field1"") data = np.load(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `txt_dump` command def test_txt_dump(interp): fname = ""field1.txt"" interp.do_cd("" Group2"") interp.do_txt_dump(""field1"") data = np.loadtxt(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `rm` command def test_rm_dataset(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_rm(""field1"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == "" field2"" # `rm` command def test_rm_group(capsys, interp): interp.do_rm(""Group2"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == ""Group1/"" # `exit` command def test_exit(capsys, interp): with pytest.raises(SystemExit): interp.do_exit('') out, err = capsys.readouterr() assert out == ""Bye!\n"" ", https://github.com/clapeyre/h5nav,595040c2f9b65196ceaf18d1c2c58d848466a22c,tests/test_cli.py::test_cd_group,tests/test_cli.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest import numpy as np from .context import cli, setup_module, teardown_module, interp # `get_whitespace_name` command def test_get_whitespace_name(interp): name = interp.get_whitespace_name(""Group1"") assert name == ""Group1"" name = interp.get_whitespace_name("" Group2"") assert name == "" Group2"" def test_get_whitespace_fail(interp): with pytest.raises(cli.UnknownLabelError): interp.get_whitespace_name('zzz') # `help` functions def test_helps_work(capsys, interp): for cmd in [""help_"" + cmd[3:] for cmd in dir(interp) if cmd[:3] == ""do_""]: if cmd == ""help_help"": continue assert hasattr(interp, cmd) # `ls` command def test_ls_simple(capsys, interp): interp.do_ls('') out, err = capsys.readouterr() assert out == "" Group2/ Group1/\n"" def test_ls_star(capsys, interp): interp.do_ls('*') out, err = capsys.readouterr() assert out == """"""\ Group2/ field1 Group1/ Subgroup1/ field1 ./ \n"""""" def test_ls_unknown_label(capsys, interp): interp.do_ls('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_ls('zzz') # `cd` command def test_cd_group(interp): assert interp.position == '/' interp.do_cd(' Group2') assert interp.position == '/ Group2/' def test_cd_dataset(capsys, interp): interp.do_cd('Group1') interp.do_cd('field1') out, err = capsys.readouterr() assert out == ""*** can only cd into groups\n"" def test_cd_dotdot(interp): interp.do_cd(' Group2') interp.do_cd('..') assert interp.position == '/' def test_cd_root(interp): interp.do_cd('Group1') interp.do_cd('Subgroup1') interp.do_cd('') assert interp.position == '/' def test_cd_2args(capsys, interp): interp.do_cd('Group1 Group2') out, err = capsys.readouterr() assert out == ""*** invalid number of arguments\n"" def test_cd_unknown_label(capsys, interp): interp.do_cd('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_cd('zzz') # `cat` command def test_cat(capsys, interp): interp.do_cd(""Group1"") interp.do_cat(""field1"") out, err = capsys.readouterr() assert out == ""information\n"" def test_cat_star(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_cat(""*"") out, err = capsys.readouterr() assert out == """"""\ field2 : [ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198] field1 : [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99] """""" # `stats` command def test_stats(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_stats(""field1"") out, err = capsys.readouterr() assert out == """"""\ Type mean +/- std*2 [ min, max ] (Shape) -------------------------------------------------------------------------- int64 4.9500e+01 +/- 5.7732e+01 [ 0.0000e+00, 9.9000e+01] (100,) """""" # `pdf` command def test_pdf(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_pdf(""field1"") out, err = capsys.readouterr() assert out == """"""\ Min Max | Pdf (10 buckets) ---------------------------------------------- 0.0000e+00 9.9000e+01 | [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] """""" # `dump` command def test_dump(interp): fname = ""field1.npy"" interp.do_cd("" Group2"") interp.do_dump(""field1"") data = np.load(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `txt_dump` command def test_txt_dump(interp): fname = ""field1.txt"" interp.do_cd("" Group2"") interp.do_txt_dump(""field1"") data = np.loadtxt(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `rm` command def test_rm_dataset(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_rm(""field1"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == "" field2"" # `rm` command def test_rm_group(capsys, interp): interp.do_rm(""Group2"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == ""Group1/"" # `exit` command def test_exit(capsys, interp): with pytest.raises(SystemExit): interp.do_exit('') out, err = capsys.readouterr() assert out == ""Bye!\n"" ", https://github.com/clapeyre/h5nav,595040c2f9b65196ceaf18d1c2c58d848466a22c,tests/test_cli.py::test_dump,tests/test_cli.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest import numpy as np from .context import cli, setup_module, teardown_module, interp # `get_whitespace_name` command def test_get_whitespace_name(interp): name = interp.get_whitespace_name(""Group1"") assert name == ""Group1"" name = interp.get_whitespace_name("" Group2"") assert name == "" Group2"" def test_get_whitespace_fail(interp): with pytest.raises(cli.UnknownLabelError): interp.get_whitespace_name('zzz') # `help` functions def test_helps_work(capsys, interp): for cmd in [""help_"" + cmd[3:] for cmd in dir(interp) if cmd[:3] == ""do_""]: if cmd == ""help_help"": continue assert hasattr(interp, cmd) # `ls` command def test_ls_simple(capsys, interp): interp.do_ls('') out, err = capsys.readouterr() assert out == "" Group2/ Group1/\n"" def test_ls_star(capsys, interp): interp.do_ls('*') out, err = capsys.readouterr() assert out == """"""\ Group2/ field1 Group1/ Subgroup1/ field1 ./ \n"""""" def test_ls_unknown_label(capsys, interp): interp.do_ls('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_ls('zzz') # `cd` command def test_cd_group(interp): assert interp.position == '/' interp.do_cd(' Group2') assert interp.position == '/ Group2/' def test_cd_dataset(capsys, interp): interp.do_cd('Group1') interp.do_cd('field1') out, err = capsys.readouterr() assert out == ""*** can only cd into groups\n"" def test_cd_dotdot(interp): interp.do_cd(' Group2') interp.do_cd('..') assert interp.position == '/' def test_cd_root(interp): interp.do_cd('Group1') interp.do_cd('Subgroup1') interp.do_cd('') assert interp.position == '/' def test_cd_2args(capsys, interp): interp.do_cd('Group1 Group2') out, err = capsys.readouterr() assert out == ""*** invalid number of arguments\n"" def test_cd_unknown_label(capsys, interp): interp.do_cd('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_cd('zzz') # `cat` command def test_cat(capsys, interp): interp.do_cd(""Group1"") interp.do_cat(""field1"") out, err = capsys.readouterr() assert out == ""information\n"" def test_cat_star(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_cat(""*"") out, err = capsys.readouterr() assert out == """"""\ field2 : [ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198] field1 : [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99] """""" # `stats` command def test_stats(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_stats(""field1"") out, err = capsys.readouterr() assert out == """"""\ Type mean +/- std*2 [ min, max ] (Shape) -------------------------------------------------------------------------- int64 4.9500e+01 +/- 5.7732e+01 [ 0.0000e+00, 9.9000e+01] (100,) """""" # `pdf` command def test_pdf(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_pdf(""field1"") out, err = capsys.readouterr() assert out == """"""\ Min Max | Pdf (10 buckets) ---------------------------------------------- 0.0000e+00 9.9000e+01 | [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] """""" # `dump` command def test_dump(interp): fname = ""field1.npy"" interp.do_cd("" Group2"") interp.do_dump(""field1"") data = np.load(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `txt_dump` command def test_txt_dump(interp): fname = ""field1.txt"" interp.do_cd("" Group2"") interp.do_txt_dump(""field1"") data = np.loadtxt(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `rm` command def test_rm_dataset(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_rm(""field1"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == "" field2"" # `rm` command def test_rm_group(capsys, interp): interp.do_rm(""Group2"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == ""Group1/"" # `exit` command def test_exit(capsys, interp): with pytest.raises(SystemExit): interp.do_exit('') out, err = capsys.readouterr() assert out == ""Bye!\n"" ", https://github.com/clapeyre/h5nav,595040c2f9b65196ceaf18d1c2c58d848466a22c,tests/test_cli.py::test_get_whitespace_name,tests/test_cli.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest import numpy as np from .context import cli, setup_module, teardown_module, interp # `get_whitespace_name` command def test_get_whitespace_name(interp): name = interp.get_whitespace_name(""Group1"") assert name == ""Group1"" name = interp.get_whitespace_name("" Group2"") assert name == "" Group2"" def test_get_whitespace_fail(interp): with pytest.raises(cli.UnknownLabelError): interp.get_whitespace_name('zzz') # `help` functions def test_helps_work(capsys, interp): for cmd in [""help_"" + cmd[3:] for cmd in dir(interp) if cmd[:3] == ""do_""]: if cmd == ""help_help"": continue assert hasattr(interp, cmd) # `ls` command def test_ls_simple(capsys, interp): interp.do_ls('') out, err = capsys.readouterr() assert out == "" Group2/ Group1/\n"" def test_ls_star(capsys, interp): interp.do_ls('*') out, err = capsys.readouterr() assert out == """"""\ Group2/ field1 Group1/ Subgroup1/ field1 ./ \n"""""" def test_ls_unknown_label(capsys, interp): interp.do_ls('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_ls('zzz') # `cd` command def test_cd_group(interp): assert interp.position == '/' interp.do_cd(' Group2') assert interp.position == '/ Group2/' def test_cd_dataset(capsys, interp): interp.do_cd('Group1') interp.do_cd('field1') out, err = capsys.readouterr() assert out == ""*** can only cd into groups\n"" def test_cd_dotdot(interp): interp.do_cd(' Group2') interp.do_cd('..') assert interp.position == '/' def test_cd_root(interp): interp.do_cd('Group1') interp.do_cd('Subgroup1') interp.do_cd('') assert interp.position == '/' def test_cd_2args(capsys, interp): interp.do_cd('Group1 Group2') out, err = capsys.readouterr() assert out == ""*** invalid number of arguments\n"" def test_cd_unknown_label(capsys, interp): interp.do_cd('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_cd('zzz') # `cat` command def test_cat(capsys, interp): interp.do_cd(""Group1"") interp.do_cat(""field1"") out, err = capsys.readouterr() assert out == ""information\n"" def test_cat_star(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_cat(""*"") out, err = capsys.readouterr() assert out == """"""\ field2 : [ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198] field1 : [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99] """""" # `stats` command def test_stats(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_stats(""field1"") out, err = capsys.readouterr() assert out == """"""\ Type mean +/- std*2 [ min, max ] (Shape) -------------------------------------------------------------------------- int64 4.9500e+01 +/- 5.7732e+01 [ 0.0000e+00, 9.9000e+01] (100,) """""" # `pdf` command def test_pdf(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_pdf(""field1"") out, err = capsys.readouterr() assert out == """"""\ Min Max | Pdf (10 buckets) ---------------------------------------------- 0.0000e+00 9.9000e+01 | [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] """""" # `dump` command def test_dump(interp): fname = ""field1.npy"" interp.do_cd("" Group2"") interp.do_dump(""field1"") data = np.load(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `txt_dump` command def test_txt_dump(interp): fname = ""field1.txt"" interp.do_cd("" Group2"") interp.do_txt_dump(""field1"") data = np.loadtxt(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `rm` command def test_rm_dataset(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_rm(""field1"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == "" field2"" # `rm` command def test_rm_group(capsys, interp): interp.do_rm(""Group2"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == ""Group1/"" # `exit` command def test_exit(capsys, interp): with pytest.raises(SystemExit): interp.do_exit('') out, err = capsys.readouterr() assert out == ""Bye!\n"" ", https://github.com/clapeyre/h5nav,595040c2f9b65196ceaf18d1c2c58d848466a22c,tests/test_cli.py::test_ls_simple,tests/test_cli.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest import numpy as np from .context import cli, setup_module, teardown_module, interp # `get_whitespace_name` command def test_get_whitespace_name(interp): name = interp.get_whitespace_name(""Group1"") assert name == ""Group1"" name = interp.get_whitespace_name("" Group2"") assert name == "" Group2"" def test_get_whitespace_fail(interp): with pytest.raises(cli.UnknownLabelError): interp.get_whitespace_name('zzz') # `help` functions def test_helps_work(capsys, interp): for cmd in [""help_"" + cmd[3:] for cmd in dir(interp) if cmd[:3] == ""do_""]: if cmd == ""help_help"": continue assert hasattr(interp, cmd) # `ls` command def test_ls_simple(capsys, interp): interp.do_ls('') out, err = capsys.readouterr() assert out == "" Group2/ Group1/\n"" def test_ls_star(capsys, interp): interp.do_ls('*') out, err = capsys.readouterr() assert out == """"""\ Group2/ field1 Group1/ Subgroup1/ field1 ./ \n"""""" def test_ls_unknown_label(capsys, interp): interp.do_ls('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_ls('zzz') # `cd` command def test_cd_group(interp): assert interp.position == '/' interp.do_cd(' Group2') assert interp.position == '/ Group2/' def test_cd_dataset(capsys, interp): interp.do_cd('Group1') interp.do_cd('field1') out, err = capsys.readouterr() assert out == ""*** can only cd into groups\n"" def test_cd_dotdot(interp): interp.do_cd(' Group2') interp.do_cd('..') assert interp.position == '/' def test_cd_root(interp): interp.do_cd('Group1') interp.do_cd('Subgroup1') interp.do_cd('') assert interp.position == '/' def test_cd_2args(capsys, interp): interp.do_cd('Group1 Group2') out, err = capsys.readouterr() assert out == ""*** invalid number of arguments\n"" def test_cd_unknown_label(capsys, interp): interp.do_cd('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_cd('zzz') # `cat` command def test_cat(capsys, interp): interp.do_cd(""Group1"") interp.do_cat(""field1"") out, err = capsys.readouterr() assert out == ""information\n"" def test_cat_star(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_cat(""*"") out, err = capsys.readouterr() assert out == """"""\ field2 : [ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198] field1 : [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99] """""" # `stats` command def test_stats(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_stats(""field1"") out, err = capsys.readouterr() assert out == """"""\ Type mean +/- std*2 [ min, max ] (Shape) -------------------------------------------------------------------------- int64 4.9500e+01 +/- 5.7732e+01 [ 0.0000e+00, 9.9000e+01] (100,) """""" # `pdf` command def test_pdf(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_pdf(""field1"") out, err = capsys.readouterr() assert out == """"""\ Min Max | Pdf (10 buckets) ---------------------------------------------- 0.0000e+00 9.9000e+01 | [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] """""" # `dump` command def test_dump(interp): fname = ""field1.npy"" interp.do_cd("" Group2"") interp.do_dump(""field1"") data = np.load(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `txt_dump` command def test_txt_dump(interp): fname = ""field1.txt"" interp.do_cd("" Group2"") interp.do_txt_dump(""field1"") data = np.loadtxt(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `rm` command def test_rm_dataset(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_rm(""field1"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == "" field2"" # `rm` command def test_rm_group(capsys, interp): interp.do_rm(""Group2"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == ""Group1/"" # `exit` command def test_exit(capsys, interp): with pytest.raises(SystemExit): interp.do_exit('') out, err = capsys.readouterr() assert out == ""Bye!\n"" ", https://github.com/clapeyre/h5nav,595040c2f9b65196ceaf18d1c2c58d848466a22c,tests/test_cli.py::test_ls_star,tests/test_cli.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest import numpy as np from .context import cli, setup_module, teardown_module, interp # `get_whitespace_name` command def test_get_whitespace_name(interp): name = interp.get_whitespace_name(""Group1"") assert name == ""Group1"" name = interp.get_whitespace_name("" Group2"") assert name == "" Group2"" def test_get_whitespace_fail(interp): with pytest.raises(cli.UnknownLabelError): interp.get_whitespace_name('zzz') # `help` functions def test_helps_work(capsys, interp): for cmd in [""help_"" + cmd[3:] for cmd in dir(interp) if cmd[:3] == ""do_""]: if cmd == ""help_help"": continue assert hasattr(interp, cmd) # `ls` command def test_ls_simple(capsys, interp): interp.do_ls('') out, err = capsys.readouterr() assert out == "" Group2/ Group1/\n"" def test_ls_star(capsys, interp): interp.do_ls('*') out, err = capsys.readouterr() assert out == """"""\ Group2/ field1 Group1/ Subgroup1/ field1 ./ \n"""""" def test_ls_unknown_label(capsys, interp): interp.do_ls('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_ls('zzz') # `cd` command def test_cd_group(interp): assert interp.position == '/' interp.do_cd(' Group2') assert interp.position == '/ Group2/' def test_cd_dataset(capsys, interp): interp.do_cd('Group1') interp.do_cd('field1') out, err = capsys.readouterr() assert out == ""*** can only cd into groups\n"" def test_cd_dotdot(interp): interp.do_cd(' Group2') interp.do_cd('..') assert interp.position == '/' def test_cd_root(interp): interp.do_cd('Group1') interp.do_cd('Subgroup1') interp.do_cd('') assert interp.position == '/' def test_cd_2args(capsys, interp): interp.do_cd('Group1 Group2') out, err = capsys.readouterr() assert out == ""*** invalid number of arguments\n"" def test_cd_unknown_label(capsys, interp): interp.do_cd('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_cd('zzz') # `cat` command def test_cat(capsys, interp): interp.do_cd(""Group1"") interp.do_cat(""field1"") out, err = capsys.readouterr() assert out == ""information\n"" def test_cat_star(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_cat(""*"") out, err = capsys.readouterr() assert out == """"""\ field2 : [ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198] field1 : [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99] """""" # `stats` command def test_stats(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_stats(""field1"") out, err = capsys.readouterr() assert out == """"""\ Type mean +/- std*2 [ min, max ] (Shape) -------------------------------------------------------------------------- int64 4.9500e+01 +/- 5.7732e+01 [ 0.0000e+00, 9.9000e+01] (100,) """""" # `pdf` command def test_pdf(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_pdf(""field1"") out, err = capsys.readouterr() assert out == """"""\ Min Max | Pdf (10 buckets) ---------------------------------------------- 0.0000e+00 9.9000e+01 | [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] """""" # `dump` command def test_dump(interp): fname = ""field1.npy"" interp.do_cd("" Group2"") interp.do_dump(""field1"") data = np.load(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `txt_dump` command def test_txt_dump(interp): fname = ""field1.txt"" interp.do_cd("" Group2"") interp.do_txt_dump(""field1"") data = np.loadtxt(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `rm` command def test_rm_dataset(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_rm(""field1"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == "" field2"" # `rm` command def test_rm_group(capsys, interp): interp.do_rm(""Group2"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == ""Group1/"" # `exit` command def test_exit(capsys, interp): with pytest.raises(SystemExit): interp.do_exit('') out, err = capsys.readouterr() assert out == ""Bye!\n"" ", https://github.com/clapeyre/h5nav,595040c2f9b65196ceaf18d1c2c58d848466a22c,tests/test_cli.py::test_pdf,tests/test_cli.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest import numpy as np from .context import cli, setup_module, teardown_module, interp # `get_whitespace_name` command def test_get_whitespace_name(interp): name = interp.get_whitespace_name(""Group1"") assert name == ""Group1"" name = interp.get_whitespace_name("" Group2"") assert name == "" Group2"" def test_get_whitespace_fail(interp): with pytest.raises(cli.UnknownLabelError): interp.get_whitespace_name('zzz') # `help` functions def test_helps_work(capsys, interp): for cmd in [""help_"" + cmd[3:] for cmd in dir(interp) if cmd[:3] == ""do_""]: if cmd == ""help_help"": continue assert hasattr(interp, cmd) # `ls` command def test_ls_simple(capsys, interp): interp.do_ls('') out, err = capsys.readouterr() assert out == "" Group2/ Group1/\n"" def test_ls_star(capsys, interp): interp.do_ls('*') out, err = capsys.readouterr() assert out == """"""\ Group2/ field1 Group1/ Subgroup1/ field1 ./ \n"""""" def test_ls_unknown_label(capsys, interp): interp.do_ls('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_ls('zzz') # `cd` command def test_cd_group(interp): assert interp.position == '/' interp.do_cd(' Group2') assert interp.position == '/ Group2/' def test_cd_dataset(capsys, interp): interp.do_cd('Group1') interp.do_cd('field1') out, err = capsys.readouterr() assert out == ""*** can only cd into groups\n"" def test_cd_dotdot(interp): interp.do_cd(' Group2') interp.do_cd('..') assert interp.position == '/' def test_cd_root(interp): interp.do_cd('Group1') interp.do_cd('Subgroup1') interp.do_cd('') assert interp.position == '/' def test_cd_2args(capsys, interp): interp.do_cd('Group1 Group2') out, err = capsys.readouterr() assert out == ""*** invalid number of arguments\n"" def test_cd_unknown_label(capsys, interp): interp.do_cd('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_cd('zzz') # `cat` command def test_cat(capsys, interp): interp.do_cd(""Group1"") interp.do_cat(""field1"") out, err = capsys.readouterr() assert out == ""information\n"" def test_cat_star(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_cat(""*"") out, err = capsys.readouterr() assert out == """"""\ field2 : [ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198] field1 : [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99] """""" # `stats` command def test_stats(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_stats(""field1"") out, err = capsys.readouterr() assert out == """"""\ Type mean +/- std*2 [ min, max ] (Shape) -------------------------------------------------------------------------- int64 4.9500e+01 +/- 5.7732e+01 [ 0.0000e+00, 9.9000e+01] (100,) """""" # `pdf` command def test_pdf(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_pdf(""field1"") out, err = capsys.readouterr() assert out == """"""\ Min Max | Pdf (10 buckets) ---------------------------------------------- 0.0000e+00 9.9000e+01 | [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] """""" # `dump` command def test_dump(interp): fname = ""field1.npy"" interp.do_cd("" Group2"") interp.do_dump(""field1"") data = np.load(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `txt_dump` command def test_txt_dump(interp): fname = ""field1.txt"" interp.do_cd("" Group2"") interp.do_txt_dump(""field1"") data = np.loadtxt(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `rm` command def test_rm_dataset(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_rm(""field1"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == "" field2"" # `rm` command def test_rm_group(capsys, interp): interp.do_rm(""Group2"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == ""Group1/"" # `exit` command def test_exit(capsys, interp): with pytest.raises(SystemExit): interp.do_exit('') out, err = capsys.readouterr() assert out == ""Bye!\n"" ", https://github.com/clapeyre/h5nav,595040c2f9b65196ceaf18d1c2c58d848466a22c,tests/test_cli.py::test_stats,tests/test_cli.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest import numpy as np from .context import cli, setup_module, teardown_module, interp # `get_whitespace_name` command def test_get_whitespace_name(interp): name = interp.get_whitespace_name(""Group1"") assert name == ""Group1"" name = interp.get_whitespace_name("" Group2"") assert name == "" Group2"" def test_get_whitespace_fail(interp): with pytest.raises(cli.UnknownLabelError): interp.get_whitespace_name('zzz') # `help` functions def test_helps_work(capsys, interp): for cmd in [""help_"" + cmd[3:] for cmd in dir(interp) if cmd[:3] == ""do_""]: if cmd == ""help_help"": continue assert hasattr(interp, cmd) # `ls` command def test_ls_simple(capsys, interp): interp.do_ls('') out, err = capsys.readouterr() assert out == "" Group2/ Group1/\n"" def test_ls_star(capsys, interp): interp.do_ls('*') out, err = capsys.readouterr() assert out == """"""\ Group2/ field1 Group1/ Subgroup1/ field1 ./ \n"""""" def test_ls_unknown_label(capsys, interp): interp.do_ls('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_ls('zzz') # `cd` command def test_cd_group(interp): assert interp.position == '/' interp.do_cd(' Group2') assert interp.position == '/ Group2/' def test_cd_dataset(capsys, interp): interp.do_cd('Group1') interp.do_cd('field1') out, err = capsys.readouterr() assert out == ""*** can only cd into groups\n"" def test_cd_dotdot(interp): interp.do_cd(' Group2') interp.do_cd('..') assert interp.position == '/' def test_cd_root(interp): interp.do_cd('Group1') interp.do_cd('Subgroup1') interp.do_cd('') assert interp.position == '/' def test_cd_2args(capsys, interp): interp.do_cd('Group1 Group2') out, err = capsys.readouterr() assert out == ""*** invalid number of arguments\n"" def test_cd_unknown_label(capsys, interp): interp.do_cd('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_cd('zzz') # `cat` command def test_cat(capsys, interp): interp.do_cd(""Group1"") interp.do_cat(""field1"") out, err = capsys.readouterr() assert out == ""information\n"" def test_cat_star(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_cat(""*"") out, err = capsys.readouterr() assert out == """"""\ field2 : [ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198] field1 : [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99] """""" # `stats` command def test_stats(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_stats(""field1"") out, err = capsys.readouterr() assert out == """"""\ Type mean +/- std*2 [ min, max ] (Shape) -------------------------------------------------------------------------- int64 4.9500e+01 +/- 5.7732e+01 [ 0.0000e+00, 9.9000e+01] (100,) """""" # `pdf` command def test_pdf(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_pdf(""field1"") out, err = capsys.readouterr() assert out == """"""\ Min Max | Pdf (10 buckets) ---------------------------------------------- 0.0000e+00 9.9000e+01 | [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] """""" # `dump` command def test_dump(interp): fname = ""field1.npy"" interp.do_cd("" Group2"") interp.do_dump(""field1"") data = np.load(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `txt_dump` command def test_txt_dump(interp): fname = ""field1.txt"" interp.do_cd("" Group2"") interp.do_txt_dump(""field1"") data = np.loadtxt(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `rm` command def test_rm_dataset(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_rm(""field1"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == "" field2"" # `rm` command def test_rm_group(capsys, interp): interp.do_rm(""Group2"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == ""Group1/"" # `exit` command def test_exit(capsys, interp): with pytest.raises(SystemExit): interp.do_exit('') out, err = capsys.readouterr() assert out == ""Bye!\n"" ", https://github.com/clapeyre/h5nav,595040c2f9b65196ceaf18d1c2c58d848466a22c,tests/test_cli.py::test_txt_dump,tests/test_cli.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest import numpy as np from .context import cli, setup_module, teardown_module, interp # `get_whitespace_name` command def test_get_whitespace_name(interp): name = interp.get_whitespace_name(""Group1"") assert name == ""Group1"" name = interp.get_whitespace_name("" Group2"") assert name == "" Group2"" def test_get_whitespace_fail(interp): with pytest.raises(cli.UnknownLabelError): interp.get_whitespace_name('zzz') # `help` functions def test_helps_work(capsys, interp): for cmd in [""help_"" + cmd[3:] for cmd in dir(interp) if cmd[:3] == ""do_""]: if cmd == ""help_help"": continue assert hasattr(interp, cmd) # `ls` command def test_ls_simple(capsys, interp): interp.do_ls('') out, err = capsys.readouterr() assert out == "" Group2/ Group1/\n"" def test_ls_star(capsys, interp): interp.do_ls('*') out, err = capsys.readouterr() assert out == """"""\ Group2/ field1 Group1/ Subgroup1/ field1 ./ \n"""""" def test_ls_unknown_label(capsys, interp): interp.do_ls('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_ls('zzz') # `cd` command def test_cd_group(interp): assert interp.position == '/' interp.do_cd(' Group2') assert interp.position == '/ Group2/' def test_cd_dataset(capsys, interp): interp.do_cd('Group1') interp.do_cd('field1') out, err = capsys.readouterr() assert out == ""*** can only cd into groups\n"" def test_cd_dotdot(interp): interp.do_cd(' Group2') interp.do_cd('..') assert interp.position == '/' def test_cd_root(interp): interp.do_cd('Group1') interp.do_cd('Subgroup1') interp.do_cd('') assert interp.position == '/' def test_cd_2args(capsys, interp): interp.do_cd('Group1 Group2') out, err = capsys.readouterr() assert out == ""*** invalid number of arguments\n"" def test_cd_unknown_label(capsys, interp): interp.do_cd('zzz') out, err = capsys.readouterr() assert out == ""*** unknown label\n"" # with pytest.raises(cli.UnknownLabelError): # interp.do_cd('zzz') # `cat` command def test_cat(capsys, interp): interp.do_cd(""Group1"") interp.do_cat(""field1"") out, err = capsys.readouterr() assert out == ""information\n"" def test_cat_star(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_cat(""*"") out, err = capsys.readouterr() assert out == """"""\ field2 : [ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 194 196 198] field1 : [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99] """""" # `stats` command def test_stats(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_stats(""field1"") out, err = capsys.readouterr() assert out == """"""\ Type mean +/- std*2 [ min, max ] (Shape) -------------------------------------------------------------------------- int64 4.9500e+01 +/- 5.7732e+01 [ 0.0000e+00, 9.9000e+01] (100,) """""" # `pdf` command def test_pdf(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_pdf(""field1"") out, err = capsys.readouterr() assert out == """"""\ Min Max | Pdf (10 buckets) ---------------------------------------------- 0.0000e+00 9.9000e+01 | [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] """""" # `dump` command def test_dump(interp): fname = ""field1.npy"" interp.do_cd("" Group2"") interp.do_dump(""field1"") data = np.load(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `txt_dump` command def test_txt_dump(interp): fname = ""field1.txt"" interp.do_cd("" Group2"") interp.do_txt_dump(""field1"") data = np.loadtxt(fname) assert np.allclose(data, np.zeros(10)) os.remove(fname) # `rm` command def test_rm_dataset(capsys, interp): interp.do_cd(""Group1"") interp.do_cd(""Subgroup1"") interp.do_rm(""field1"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == "" field2"" # `rm` command def test_rm_group(capsys, interp): interp.do_rm(""Group2"") interp.do_ls('') out, err = capsys.readouterr() assert out.split(""\n"")[1] == ""Group1/"" # `exit` command def test_exit(capsys, interp): with pytest.raises(SystemExit): interp.do_exit('') out, err = capsys.readouterr() assert out == ""Bye!\n"" ", https://github.com/Cognexa/plcx,2756c0ba78c4c9e572d95ba002708957bc55d4fa,plcx/tests/comm/test_client.py::test_clientx[123],plcx/tests/comm/test_client.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import asyncio import pytest from plcx.comm.client import ClientX, clientx @pytest.mark.parametrize(""msg"", [""1"", ""123"", ""hola""]) def test_clientx(tcp_server, msg): """""" Test tcp clientx coroutine. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server loop = asyncio.get_event_loop() assert loop.run_until_complete(clientx(host, port, msg.encode(), 32, time_out=5)).decode() == f""received:'{msg}'"" assert ( loop.run_until_complete(clientx(host, port, b""control message"", 32, time_out=5)) == b""received:'control message'"" ) @pytest.mark.parametrize(""msg"", [""111"", ""321"", ""hi""]) def test_clientx_context(tcp_server, msg): """""" Test tcp clientx as context for connection to server. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server client = ClientX(host=host, port=port) assert client.send(msg.encode(), 32, time_out=5).decode() == f""received:'{msg}'"" assert client.send(b""control message"", 32, time_out=5) == b""received:'control message'"" def test_clientx_error(tcp_server): """""" Test raising error. :param tcp_server: tuple with host and port of testing server """""" host, port = tcp_server loop = asyncio.get_event_loop() # testing connection to port 0 with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 0, b"""", 1, max_try=1)) # testing connection to not exist port with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 65432, b"""", 1, max_try=1)) # testing connection to not exist address with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(""hola"", port, b"""", 1, max_try=1)) # testing time out with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, port, b""123"", 16, time_out=0.005, max_try=1)) ", https://github.com/Cognexa/plcx,2756c0ba78c4c9e572d95ba002708957bc55d4fa,plcx/tests/comm/test_client.py::test_clientx[1],plcx/tests/comm/test_client.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import asyncio import pytest from plcx.comm.client import ClientX, clientx @pytest.mark.parametrize(""msg"", [""1"", ""123"", ""hola""]) def test_clientx(tcp_server, msg): """""" Test tcp clientx coroutine. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server loop = asyncio.get_event_loop() assert loop.run_until_complete(clientx(host, port, msg.encode(), 32, time_out=5)).decode() == f""received:'{msg}'"" assert ( loop.run_until_complete(clientx(host, port, b""control message"", 32, time_out=5)) == b""received:'control message'"" ) @pytest.mark.parametrize(""msg"", [""111"", ""321"", ""hi""]) def test_clientx_context(tcp_server, msg): """""" Test tcp clientx as context for connection to server. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server client = ClientX(host=host, port=port) assert client.send(msg.encode(), 32, time_out=5).decode() == f""received:'{msg}'"" assert client.send(b""control message"", 32, time_out=5) == b""received:'control message'"" def test_clientx_error(tcp_server): """""" Test raising error. :param tcp_server: tuple with host and port of testing server """""" host, port = tcp_server loop = asyncio.get_event_loop() # testing connection to port 0 with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 0, b"""", 1, max_try=1)) # testing connection to not exist port with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 65432, b"""", 1, max_try=1)) # testing connection to not exist address with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(""hola"", port, b"""", 1, max_try=1)) # testing time out with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, port, b""123"", 16, time_out=0.005, max_try=1)) ", https://github.com/Cognexa/plcx,2756c0ba78c4c9e572d95ba002708957bc55d4fa,plcx/tests/comm/test_client.py::test_clientx[hola],plcx/tests/comm/test_client.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import asyncio import pytest from plcx.comm.client import ClientX, clientx @pytest.mark.parametrize(""msg"", [""1"", ""123"", ""hola""]) def test_clientx(tcp_server, msg): """""" Test tcp clientx coroutine. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server loop = asyncio.get_event_loop() assert loop.run_until_complete(clientx(host, port, msg.encode(), 32, time_out=5)).decode() == f""received:'{msg}'"" assert ( loop.run_until_complete(clientx(host, port, b""control message"", 32, time_out=5)) == b""received:'control message'"" ) @pytest.mark.parametrize(""msg"", [""111"", ""321"", ""hi""]) def test_clientx_context(tcp_server, msg): """""" Test tcp clientx as context for connection to server. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server client = ClientX(host=host, port=port) assert client.send(msg.encode(), 32, time_out=5).decode() == f""received:'{msg}'"" assert client.send(b""control message"", 32, time_out=5) == b""received:'control message'"" def test_clientx_error(tcp_server): """""" Test raising error. :param tcp_server: tuple with host and port of testing server """""" host, port = tcp_server loop = asyncio.get_event_loop() # testing connection to port 0 with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 0, b"""", 1, max_try=1)) # testing connection to not exist port with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 65432, b"""", 1, max_try=1)) # testing connection to not exist address with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(""hola"", port, b"""", 1, max_try=1)) # testing time out with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, port, b""123"", 16, time_out=0.005, max_try=1)) ", https://github.com/Cognexa/plcx,2756c0ba78c4c9e572d95ba002708957bc55d4fa,plcx/tests/comm/test_client.py::test_clientx_context[111],plcx/tests/comm/test_client.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import asyncio import pytest from plcx.comm.client import ClientX, clientx @pytest.mark.parametrize(""msg"", [""1"", ""123"", ""hola""]) def test_clientx(tcp_server, msg): """""" Test tcp clientx coroutine. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server loop = asyncio.get_event_loop() assert loop.run_until_complete(clientx(host, port, msg.encode(), 32, time_out=5)).decode() == f""received:'{msg}'"" assert ( loop.run_until_complete(clientx(host, port, b""control message"", 32, time_out=5)) == b""received:'control message'"" ) @pytest.mark.parametrize(""msg"", [""111"", ""321"", ""hi""]) def test_clientx_context(tcp_server, msg): """""" Test tcp clientx as context for connection to server. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server client = ClientX(host=host, port=port) assert client.send(msg.encode(), 32, time_out=5).decode() == f""received:'{msg}'"" assert client.send(b""control message"", 32, time_out=5) == b""received:'control message'"" def test_clientx_error(tcp_server): """""" Test raising error. :param tcp_server: tuple with host and port of testing server """""" host, port = tcp_server loop = asyncio.get_event_loop() # testing connection to port 0 with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 0, b"""", 1, max_try=1)) # testing connection to not exist port with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 65432, b"""", 1, max_try=1)) # testing connection to not exist address with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(""hola"", port, b"""", 1, max_try=1)) # testing time out with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, port, b""123"", 16, time_out=0.005, max_try=1)) ", https://github.com/Cognexa/plcx,2756c0ba78c4c9e572d95ba002708957bc55d4fa,plcx/tests/comm/test_client.py::test_clientx_context[321],plcx/tests/comm/test_client.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import asyncio import pytest from plcx.comm.client import ClientX, clientx @pytest.mark.parametrize(""msg"", [""1"", ""123"", ""hola""]) def test_clientx(tcp_server, msg): """""" Test tcp clientx coroutine. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server loop = asyncio.get_event_loop() assert loop.run_until_complete(clientx(host, port, msg.encode(), 32, time_out=5)).decode() == f""received:'{msg}'"" assert ( loop.run_until_complete(clientx(host, port, b""control message"", 32, time_out=5)) == b""received:'control message'"" ) @pytest.mark.parametrize(""msg"", [""111"", ""321"", ""hi""]) def test_clientx_context(tcp_server, msg): """""" Test tcp clientx as context for connection to server. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server client = ClientX(host=host, port=port) assert client.send(msg.encode(), 32, time_out=5).decode() == f""received:'{msg}'"" assert client.send(b""control message"", 32, time_out=5) == b""received:'control message'"" def test_clientx_error(tcp_server): """""" Test raising error. :param tcp_server: tuple with host and port of testing server """""" host, port = tcp_server loop = asyncio.get_event_loop() # testing connection to port 0 with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 0, b"""", 1, max_try=1)) # testing connection to not exist port with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 65432, b"""", 1, max_try=1)) # testing connection to not exist address with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(""hola"", port, b"""", 1, max_try=1)) # testing time out with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, port, b""123"", 16, time_out=0.005, max_try=1)) ", https://github.com/Cognexa/plcx,2756c0ba78c4c9e572d95ba002708957bc55d4fa,plcx/tests/comm/test_client.py::test_clientx_context[hi],plcx/tests/comm/test_client.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import asyncio import pytest from plcx.comm.client import ClientX, clientx @pytest.mark.parametrize(""msg"", [""1"", ""123"", ""hola""]) def test_clientx(tcp_server, msg): """""" Test tcp clientx coroutine. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server loop = asyncio.get_event_loop() assert loop.run_until_complete(clientx(host, port, msg.encode(), 32, time_out=5)).decode() == f""received:'{msg}'"" assert ( loop.run_until_complete(clientx(host, port, b""control message"", 32, time_out=5)) == b""received:'control message'"" ) @pytest.mark.parametrize(""msg"", [""111"", ""321"", ""hi""]) def test_clientx_context(tcp_server, msg): """""" Test tcp clientx as context for connection to server. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server client = ClientX(host=host, port=port) assert client.send(msg.encode(), 32, time_out=5).decode() == f""received:'{msg}'"" assert client.send(b""control message"", 32, time_out=5) == b""received:'control message'"" def test_clientx_error(tcp_server): """""" Test raising error. :param tcp_server: tuple with host and port of testing server """""" host, port = tcp_server loop = asyncio.get_event_loop() # testing connection to port 0 with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 0, b"""", 1, max_try=1)) # testing connection to not exist port with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 65432, b"""", 1, max_try=1)) # testing connection to not exist address with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(""hola"", port, b"""", 1, max_try=1)) # testing time out with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, port, b""123"", 16, time_out=0.005, max_try=1)) ", https://github.com/Cognexa/plcx,2756c0ba78c4c9e572d95ba002708957bc55d4fa,plcx/tests/comm/test_client.py::test_clientx_error,plcx/tests/comm/test_client.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import asyncio import pytest from plcx.comm.client import ClientX, clientx @pytest.mark.parametrize(""msg"", [""1"", ""123"", ""hola""]) def test_clientx(tcp_server, msg): """""" Test tcp clientx coroutine. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server loop = asyncio.get_event_loop() assert loop.run_until_complete(clientx(host, port, msg.encode(), 32, time_out=5)).decode() == f""received:'{msg}'"" assert ( loop.run_until_complete(clientx(host, port, b""control message"", 32, time_out=5)) == b""received:'control message'"" ) @pytest.mark.parametrize(""msg"", [""111"", ""321"", ""hi""]) def test_clientx_context(tcp_server, msg): """""" Test tcp clientx as context for connection to server. :param tcp_server: tuple with host and port of testing server :param msg: message to send """""" host, port = tcp_server client = ClientX(host=host, port=port) assert client.send(msg.encode(), 32, time_out=5).decode() == f""received:'{msg}'"" assert client.send(b""control message"", 32, time_out=5) == b""received:'control message'"" def test_clientx_error(tcp_server): """""" Test raising error. :param tcp_server: tuple with host and port of testing server """""" host, port = tcp_server loop = asyncio.get_event_loop() # testing connection to port 0 with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 0, b"""", 1, max_try=1)) # testing connection to not exist port with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, 65432, b"""", 1, max_try=1)) # testing connection to not exist address with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(""hola"", port, b"""", 1, max_try=1)) # testing time out with pytest.raises((OSError, asyncio.TimeoutError)): loop.run_until_complete(clientx(host, port, b""123"", 16, time_out=0.005, max_try=1)) ", https://github.com/coinbase/coinbase-commerce-python,d306fc562309edb909c8ace501c63327a7635975,tests/api_resources/base/test_create_api_resource.py::TestCreateAPIResource::test_create,tests/api_resources/base/test_create_api_resource.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from coinbase_commerce.api_resources import Checkout from coinbase_commerce.api_resources.base import CreateAPIResource from tests.base_test_case import BaseTestCase class TestCreateResource(CreateAPIResource): RESOURCE_PATH = ""create_tests"" class TestCreateAPIResource(BaseTestCase): def test_create(self): self.stub_request('post', TestCreateResource.RESOURCE_PATH, {'resource': 'checkout', 'foo': 'bar', }) TestCreateResource._api_client = self.client api_obj = TestCreateResource.create() self.assert_requested('post', TestCreateResource.RESOURCE_PATH, data={}) self.assertEqual('bar', api_obj.foo) self.assertIsInstance(api_obj, Checkout) ", https://github.com/coinbase/coinbase-commerce-python,d306fc562309edb909c8ace501c63327a7635975,tests/api_resources/base/test_list_api_resource.py::TestListAPIResource::test_list,tests/api_resources/base/test_list_api_resource.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from coinbase_commerce.api_resources import Charge from coinbase_commerce.api_resources.base import ListAPIResource from tests.base_test_case import BaseTestCase class TestDeleteResource(ListAPIResource): RESOURCE_PATH = ""list_tests"" class TestListAPIResource(BaseTestCase): def test_list(self): self.stub_request('get', TestDeleteResource.RESOURCE_PATH, {'data': [{'resource': 'charge', 'name': 'foo', }, {'resource': 'charge', 'name': 'bar', }], }, ) TestDeleteResource._api_client = self.client res = TestDeleteResource.list() self.assert_requested('get', TestDeleteResource.RESOURCE_PATH, params={}) self.assertEqual(2, len(res.data)) self.assertTrue(all(isinstance(obj, Charge) for obj in res.data)) self.assertEqual('foo', res.data[0].name) self.assertEqual('bar', res.data[1].name) ", https://github.com/coinbase/coinbase-commerce-python,d306fc562309edb909c8ace501c63327a7635975,tests/api_resources/test_charge.py::TestCharge::test_list_iter_mapping,tests/api_resources/test_charge.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from coinbase_commerce.api_resources.charge import Charge from tests.base_test_case import BaseTestCase class TestCharge(BaseTestCase): def test_list(self): # arrange self.stub_request('get', 'charges', {'data': ['boo', 'foo']}) # act charge_list = self.client.charge.list() # assert self.assert_requested('get', 'charges', params={}) self.assertIsInstance(charge_list, Charge) self.assertIsInstance(charge_list.data, list) def test_list_iter(self): # arrange self.stub_request('get', 'charges', {'data': list(range(20))}) # act for charge in self.client.charge.list_paging_iter(): # assert self.assertTrue(charge in range(20)) def test_list_iter_mapping(self): # arrange self.stub_request('get', 'charges', {'data': [{'resource': 'charge'} for _ in range(20)]}) # act for charge in self.client.charge.list_paging_iter(): # assert self.assertIsInstance(charge, Charge) def test_retrieve(self): # arrange self.stub_request('get', 'charges', {'id': 'foo', 'code': 'bar'}) # act resource = self.client.charge.retrieve('foo') # assert self.assert_requested('get', 'charges', 'foo', params={}) self.assertIsInstance(resource, Charge) self.assertEqual(resource.id, 'foo') self.assertEqual(resource.code, 'bar') def test_create(self): # arrange charge_data = {'id': 'foo'} self.stub_request('post', 'charges', charge_data) # act resource = self.client.charge.create(**charge_data) # assert self.assert_requested('post', 'charges', data=charge_data) self.assertIsInstance(resource, Charge) self.assertEqual(resource.id, 'foo') def test_cancel(self): # arrange self.stub_request('post', 'charges', {'id': 'foo', 'code': 'bar'}) # act resource = self.client.charge.cancel('foo') # assert self.assert_requested('post', 'charges', 'foo', 'cancel', data={}) self.assertIsInstance(resource, Charge) self.assertEqual(resource.id, 'foo') def test_resolve(self): # arrange self.stub_request('post', 'charges', {'id': 'foo', 'code': 'bar'}) # act resource = self.client.charge.resolve('foo') # assert self.assert_requested('post', 'charges', 'foo', 'resolve', data={}) self.assertIsInstance(resource, Charge) self.assertEqual(resource.id, 'foo') ", https://github.com/coinbase/coinbase-commerce-python,d306fc562309edb909c8ace501c63327a7635975,tests/api_resources/test_checkout.py::TestCheckout::test_list_iter_mapping,tests/api_resources/test_checkout.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from coinbase_commerce.api_resources.checkout import Checkout from tests.base_test_case import BaseTestCase class TestCheckout(BaseTestCase): def test_list(self): # arrange self.stub_request('get', 'checkouts', {'data': ['foo', 'bar']}) # act checkout = self.client.checkout.list() # assert self.assert_requested('get', 'checkouts', params={}) self.assertIsInstance(checkout, Checkout) self.assertIsInstance(checkout.data, list) def test_list_iter(self): # arrange self.stub_request('get', 'checkouts', {'data': list(range(25))}) # act for checkout in self.client.checkout.list_paging_iter(): # assert self.assertTrue(checkout in range(25)) def test_list_iter_mapping(self): # arrange self.stub_request('get', 'checkouts', {'data': [{'resource': 'checkout'} for _ in range(25)]}) # act for checkout in self.client.checkout.list_paging_iter(): # assert self.assertIsInstance(checkout, Checkout) def test_retrieve(self): # arrange checkout_data = {'id': 'bar'} self.stub_request('get', 'checkouts', checkout_data) # act checkout = self.client.checkout.retrieve(checkout_data.get('id')) # assert self.assert_requested('get', 'checkouts', checkout_data.get('id'), params={}) self.assertIsInstance(checkout, Checkout) self.assertEqual(checkout.id, checkout_data.get('id')) def test_create(self): # arrange checkout_data = {'id': 'bar'} self.stub_request('post', 'checkouts', checkout_data) # act checkout = self.client.checkout.create(**checkout_data) # assert self.assert_requested('post', 'checkouts', data=checkout_data) self.assertIsInstance(checkout, Checkout) self.assertEqual(checkout.id, checkout_data.get('id')) def test_update_save(self): # arrange checkout_data = {'id': 'bar', 'name': 'foo'} self.stub_request('post', 'checkouts', checkout_data) checkout = self.client.checkout.create(**checkout_data) self.stub_request('put', 'checkouts', {'data': {'id': 'bar', 'name': 'new foo'}}) # act checkout.name = 'new foo' checkout.save() # assert self.assert_requested('put', 'checkouts', 'bar', data={'name': 'new foo'}) self.assertEqual(checkout.name, 'new foo') def test_update_modify(self): # arrange self.stub_request('put', 'checkouts', {'id': 'bar', 'name': 'new foo bar'}) # act checkout = self.client.checkout.modify('bar', name='new foo bar') # assert self.assertIsInstance(checkout, Checkout) self.assert_requested('put', 'checkouts', 'bar', data={'name': 'new foo bar'}) self.assertEqual(checkout.name, 'new foo bar') def test_delete(self): # arrange checkout_data = {'id': 'bar'} self.stub_request('post', 'checkouts', checkout_data) checkout = self.client.checkout.create(**checkout_data) checkout_id = checkout.id self.stub_request('delete', 'checkouts', {}) # act checkout.delete() # assert self.assert_requested('delete', 'checkouts', checkout_id, data={}) self.assertFalse(checkout) ", https://github.com/coinbase/coinbase-commerce-python,d306fc562309edb909c8ace501c63327a7635975,tests/api_resources/test_event.py::TestEvent::test_list_iter_mapping,tests/api_resources/test_event.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from coinbase_commerce.api_resources.event import Event from tests.base_test_case import BaseTestCase class TestEvent(BaseTestCase): def test_list(self): # arrange self.stub_request('get', 'events', {'data': ['foo', 'bar']}) # act event_list = self.client.event.list() # assert self.assert_requested('get', 'events', params={}) self.assertIsInstance(event_list, Event) self.assertIsInstance(event_list.data, list) def test_list_iter(self): # arrange self.stub_request('get', 'events', {'data': list(range(20))}) # act for event in self.client.event.list_paging_iter(): # assert self.assertTrue(event in range(20)) def test_list_iter_mapping(self): # arrange self.stub_request('get', 'events', {'data': [{'resource': 'event'} for _ in range(20)]}) # act for event in self.client.event.list_paging_iter(): # assert self.assertIsInstance(event, Event) def test_retrieve(self): data = {'item_id': 'foo', 'data': []} # arrange self.stub_request('get', 'events', data) # act event = self.client.event.retrieve('foo') # assert self.assert_requested('get', 'events', 'foo', params={}) self.assertIsInstance(event, Event) ", https://github.com/coinbase/coinbase-commerce-python,d306fc562309edb909c8ace501c63327a7635975,tests/test_api_client.py::TestApiClient::test_authentication_error,tests/test_api_client.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from mock import PropertyMock, mock from requests import RequestException from requests.sessions import Session from coinbase_commerce.api_resources import Charge, Checkout, Event from coinbase_commerce.client import Client from coinbase_commerce.error import ( APIError, AuthenticationError, InternalServerError, InvalidRequestError, ParamRequiredError, RateLimitExceededError, ResourceNotFoundError, ServiceUnavailableError, ValidationError, ) from coinbase_commerce.response import CoinbaseResponse from tests.base_test_case import BaseTestCase class TestApiClient(BaseTestCase): def test_init(self): api_key, base_url, api_version = TestApiClient.mock_client_params() client = Client(api_key, base_url, api_version) self.assertEqual(client.API_VERSION, api_version) self.assertEqual(client.BASE_API_URI, base_url) self.assertIsInstance(client.session, Session) @mock.patch('requests.session') def test_api_session(self, mocked_session): api_key, base_url, api_version = TestApiClient.mock_client_params() with Client(api_key, base_url, api_version): pass self.assertEqual(mocked_session.call_count, 1) self.assertEqual(mocked_session.return_value.close.call_count, 1) client = Client(api_key, base_url, api_version) self.assertEqual(mocked_session.call_count, 2) client.close() self.assertEqual(mocked_session.return_value.close.call_count, 2) def test_checkout_relation(self): client = TestApiClient.mock_client() checkout = client.checkout checkout2 = client.checkout self.assertTrue(hasattr(client, 'checkout')) self.assertIs(checkout, Checkout) self.assertIs(checkout, checkout2) def test_charge_relation(self): client = TestApiClient.mock_client() charge = client.charge charge2 = client.charge self.assertTrue(hasattr(client, 'charge')) self.assertIs(charge, Charge) self.assertIs(charge, charge2) def test_event_relation(self): client = TestApiClient.mock_client() event = client.event event2 = client.event self.assertTrue(hasattr(client, 'event')) self.assertIs(event, Event) self.assertIs(event, event2) def test_response_class(self): client = TestApiClient.mock_client() self.stub_request('get', 'foo', {}) resp = client._request('get', 'foo') self.assertTrue(isinstance(resp, CoinbaseResponse)) def test_handle_exception(self): client = TestApiClient.mock_client() with self.assertRaises(APIError) as e: client._handle_request_error(Exception) self.assertIn(""probably a configuration issue locally"", e.exception.args[0]) def test_handle_request_exception(self): client = TestApiClient.mock_client() with self.assertRaises(APIError) as e: client._handle_request_error(RequestException()) self.assertIn('Network error: RequestException', e.exception.args[0]) @mock.patch('requests.session', side_effect=mock.MagicMock) def test_invalid_request_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.status_code = PropertyMock(return_value=400) client = TestApiClient.mock_client() with self.assertRaises(InvalidRequestError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_authentication_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.status_code = PropertyMock(return_value=401) client = TestApiClient.mock_client() with self.assertRaises(AuthenticationError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_resource_not_found_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.status_code = PropertyMock(return_value=404) client = TestApiClient.mock_client() with self.assertRaises(ResourceNotFoundError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_rate_limit_exceeded_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock( return_value='{""error"": {}}' ) mock.MagicMock.status_code = PropertyMock(return_value=429) client = TestApiClient.mock_client() with self.assertRaises(RateLimitExceededError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_internal_server_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock( return_value='{""error"": {}}' ) mock.MagicMock.status_code = PropertyMock(return_value=500) client = TestApiClient.mock_client() with self.assertRaises(InternalServerError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_service_unavailable_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock(return_value='{}') mock.MagicMock.status_code = PropertyMock(return_value=503) client = TestApiClient.mock_client() with self.assertRaises(ServiceUnavailableError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_param_required_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock( return_value='{""error"": {""type"": ""param_required""}}' ) client = TestApiClient.mock_client() with self.assertRaises(ParamRequiredError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_validation_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock( return_value='{""error"": {""type"": ""validation_error""}}' ) client = TestApiClient.mock_client() with self.assertRaises(ValidationError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_valid_response_proceed(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=True) mock.MagicMock.content = '{""foo"":""baz""}' mock.MagicMock.body = 'bar' mock.MagicMock.status_code = 200 mock.MagicMock.headers = {} client = TestApiClient.mock_client() resp = client._proceed_response(mock.MagicMock) self.assertIsInstance(resp, CoinbaseResponse) @staticmethod def mock_client(): api_key, base_url, api_version = TestApiClient.mock_client_params() return Client(api_key, base_url, api_version) @staticmethod def mock_client_params(): api_key = 'testapikey' base_url = 'https://base-url.com' api_version = '2018-03-22' return api_key, base_url, api_version ", https://github.com/coinbase/coinbase-commerce-python,d306fc562309edb909c8ace501c63327a7635975,tests/test_api_client.py::TestApiClient::test_invalid_request_error,tests/test_api_client.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from mock import PropertyMock, mock from requests import RequestException from requests.sessions import Session from coinbase_commerce.api_resources import Charge, Checkout, Event from coinbase_commerce.client import Client from coinbase_commerce.error import ( APIError, AuthenticationError, InternalServerError, InvalidRequestError, ParamRequiredError, RateLimitExceededError, ResourceNotFoundError, ServiceUnavailableError, ValidationError, ) from coinbase_commerce.response import CoinbaseResponse from tests.base_test_case import BaseTestCase class TestApiClient(BaseTestCase): def test_init(self): api_key, base_url, api_version = TestApiClient.mock_client_params() client = Client(api_key, base_url, api_version) self.assertEqual(client.API_VERSION, api_version) self.assertEqual(client.BASE_API_URI, base_url) self.assertIsInstance(client.session, Session) @mock.patch('requests.session') def test_api_session(self, mocked_session): api_key, base_url, api_version = TestApiClient.mock_client_params() with Client(api_key, base_url, api_version): pass self.assertEqual(mocked_session.call_count, 1) self.assertEqual(mocked_session.return_value.close.call_count, 1) client = Client(api_key, base_url, api_version) self.assertEqual(mocked_session.call_count, 2) client.close() self.assertEqual(mocked_session.return_value.close.call_count, 2) def test_checkout_relation(self): client = TestApiClient.mock_client() checkout = client.checkout checkout2 = client.checkout self.assertTrue(hasattr(client, 'checkout')) self.assertIs(checkout, Checkout) self.assertIs(checkout, checkout2) def test_charge_relation(self): client = TestApiClient.mock_client() charge = client.charge charge2 = client.charge self.assertTrue(hasattr(client, 'charge')) self.assertIs(charge, Charge) self.assertIs(charge, charge2) def test_event_relation(self): client = TestApiClient.mock_client() event = client.event event2 = client.event self.assertTrue(hasattr(client, 'event')) self.assertIs(event, Event) self.assertIs(event, event2) def test_response_class(self): client = TestApiClient.mock_client() self.stub_request('get', 'foo', {}) resp = client._request('get', 'foo') self.assertTrue(isinstance(resp, CoinbaseResponse)) def test_handle_exception(self): client = TestApiClient.mock_client() with self.assertRaises(APIError) as e: client._handle_request_error(Exception) self.assertIn(""probably a configuration issue locally"", e.exception.args[0]) def test_handle_request_exception(self): client = TestApiClient.mock_client() with self.assertRaises(APIError) as e: client._handle_request_error(RequestException()) self.assertIn('Network error: RequestException', e.exception.args[0]) @mock.patch('requests.session', side_effect=mock.MagicMock) def test_invalid_request_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.status_code = PropertyMock(return_value=400) client = TestApiClient.mock_client() with self.assertRaises(InvalidRequestError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_authentication_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.status_code = PropertyMock(return_value=401) client = TestApiClient.mock_client() with self.assertRaises(AuthenticationError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_resource_not_found_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.status_code = PropertyMock(return_value=404) client = TestApiClient.mock_client() with self.assertRaises(ResourceNotFoundError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_rate_limit_exceeded_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock( return_value='{""error"": {}}' ) mock.MagicMock.status_code = PropertyMock(return_value=429) client = TestApiClient.mock_client() with self.assertRaises(RateLimitExceededError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_internal_server_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock( return_value='{""error"": {}}' ) mock.MagicMock.status_code = PropertyMock(return_value=500) client = TestApiClient.mock_client() with self.assertRaises(InternalServerError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_service_unavailable_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock(return_value='{}') mock.MagicMock.status_code = PropertyMock(return_value=503) client = TestApiClient.mock_client() with self.assertRaises(ServiceUnavailableError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_param_required_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock( return_value='{""error"": {""type"": ""param_required""}}' ) client = TestApiClient.mock_client() with self.assertRaises(ParamRequiredError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_validation_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock( return_value='{""error"": {""type"": ""validation_error""}}' ) client = TestApiClient.mock_client() with self.assertRaises(ValidationError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_valid_response_proceed(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=True) mock.MagicMock.content = '{""foo"":""baz""}' mock.MagicMock.body = 'bar' mock.MagicMock.status_code = 200 mock.MagicMock.headers = {} client = TestApiClient.mock_client() resp = client._proceed_response(mock.MagicMock) self.assertIsInstance(resp, CoinbaseResponse) @staticmethod def mock_client(): api_key, base_url, api_version = TestApiClient.mock_client_params() return Client(api_key, base_url, api_version) @staticmethod def mock_client_params(): api_key = 'testapikey' base_url = 'https://base-url.com' api_version = '2018-03-22' return api_key, base_url, api_version ", https://github.com/coinbase/coinbase-commerce-python,d306fc562309edb909c8ace501c63327a7635975,tests/test_api_client.py::TestApiClient::test_resource_not_found_error,tests/test_api_client.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from mock import PropertyMock, mock from requests import RequestException from requests.sessions import Session from coinbase_commerce.api_resources import Charge, Checkout, Event from coinbase_commerce.client import Client from coinbase_commerce.error import ( APIError, AuthenticationError, InternalServerError, InvalidRequestError, ParamRequiredError, RateLimitExceededError, ResourceNotFoundError, ServiceUnavailableError, ValidationError, ) from coinbase_commerce.response import CoinbaseResponse from tests.base_test_case import BaseTestCase class TestApiClient(BaseTestCase): def test_init(self): api_key, base_url, api_version = TestApiClient.mock_client_params() client = Client(api_key, base_url, api_version) self.assertEqual(client.API_VERSION, api_version) self.assertEqual(client.BASE_API_URI, base_url) self.assertIsInstance(client.session, Session) @mock.patch('requests.session') def test_api_session(self, mocked_session): api_key, base_url, api_version = TestApiClient.mock_client_params() with Client(api_key, base_url, api_version): pass self.assertEqual(mocked_session.call_count, 1) self.assertEqual(mocked_session.return_value.close.call_count, 1) client = Client(api_key, base_url, api_version) self.assertEqual(mocked_session.call_count, 2) client.close() self.assertEqual(mocked_session.return_value.close.call_count, 2) def test_checkout_relation(self): client = TestApiClient.mock_client() checkout = client.checkout checkout2 = client.checkout self.assertTrue(hasattr(client, 'checkout')) self.assertIs(checkout, Checkout) self.assertIs(checkout, checkout2) def test_charge_relation(self): client = TestApiClient.mock_client() charge = client.charge charge2 = client.charge self.assertTrue(hasattr(client, 'charge')) self.assertIs(charge, Charge) self.assertIs(charge, charge2) def test_event_relation(self): client = TestApiClient.mock_client() event = client.event event2 = client.event self.assertTrue(hasattr(client, 'event')) self.assertIs(event, Event) self.assertIs(event, event2) def test_response_class(self): client = TestApiClient.mock_client() self.stub_request('get', 'foo', {}) resp = client._request('get', 'foo') self.assertTrue(isinstance(resp, CoinbaseResponse)) def test_handle_exception(self): client = TestApiClient.mock_client() with self.assertRaises(APIError) as e: client._handle_request_error(Exception) self.assertIn(""probably a configuration issue locally"", e.exception.args[0]) def test_handle_request_exception(self): client = TestApiClient.mock_client() with self.assertRaises(APIError) as e: client._handle_request_error(RequestException()) self.assertIn('Network error: RequestException', e.exception.args[0]) @mock.patch('requests.session', side_effect=mock.MagicMock) def test_invalid_request_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.status_code = PropertyMock(return_value=400) client = TestApiClient.mock_client() with self.assertRaises(InvalidRequestError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_authentication_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.status_code = PropertyMock(return_value=401) client = TestApiClient.mock_client() with self.assertRaises(AuthenticationError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_resource_not_found_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.status_code = PropertyMock(return_value=404) client = TestApiClient.mock_client() with self.assertRaises(ResourceNotFoundError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_rate_limit_exceeded_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock( return_value='{""error"": {}}' ) mock.MagicMock.status_code = PropertyMock(return_value=429) client = TestApiClient.mock_client() with self.assertRaises(RateLimitExceededError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_internal_server_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock( return_value='{""error"": {}}' ) mock.MagicMock.status_code = PropertyMock(return_value=500) client = TestApiClient.mock_client() with self.assertRaises(InternalServerError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_service_unavailable_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock(return_value='{}') mock.MagicMock.status_code = PropertyMock(return_value=503) client = TestApiClient.mock_client() with self.assertRaises(ServiceUnavailableError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_param_required_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock( return_value='{""error"": {""type"": ""param_required""}}' ) client = TestApiClient.mock_client() with self.assertRaises(ParamRequiredError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_validation_error(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=False) mock.MagicMock.content = PropertyMock( return_value='{""error"": {""type"": ""validation_error""}}' ) client = TestApiClient.mock_client() with self.assertRaises(ValidationError): client._request('get', 'foo') @mock.patch('requests.session', side_effect=mock.MagicMock) def test_valid_response_proceed(self, session_mock): mock.MagicMock.ok = PropertyMock(return_value=True) mock.MagicMock.content = '{""foo"":""baz""}' mock.MagicMock.body = 'bar' mock.MagicMock.status_code = 200 mock.MagicMock.headers = {} client = TestApiClient.mock_client() resp = client._proceed_response(mock.MagicMock) self.assertIsInstance(resp, CoinbaseResponse) @staticmethod def mock_client(): api_key, base_url, api_version = TestApiClient.mock_client_params() return Client(api_key, base_url, api_version) @staticmethod def mock_client_params(): api_key = 'testapikey' base_url = 'https://base-url.com' api_version = '2018-03-22' return api_key, base_url, api_version ", https://github.com/ConSou/devtracker,ea892d6d48aa5d4627b429469b59ae3f0ce7f10f,devtracker/test_devtracker.py::test_add_start,devtracker/test_devtracker.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import csv import time from datetime import datetime from datetime import timedelta from pathlib import Path from .dir_and_path_helpers import get_working_dir, mk_file_path, check_file from .time_and_date_helpers import get_date, get_sec, get_time, total_time from .time import current_status, start_time, end_time, make_project_csv from .report import today_report, full_report # dir_and_path_helpers tests # ----------------------------------------------------------------------------------- home = str(Path.home()) path = home + ""/devtrack_projects"" if os.path.exists(path): pass else: os.mkdir(path) def test_install_project_folder(): path = home + ""/devtrack_projects"" assert os.path.exists(path) == True def test_get_working_dir(): assert get_working_dir() == 'devtracker' def test_mk_file_path(): assert mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) def test_check_file(): test_path = mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) assert check_file(test_path) == True # time_and_date_helpers tests # ----------------------------------------------------------------------------------- def test_get_date(): assert get_date() == datetime.today().strftime(""%Y-%m-%d"") def test_get_time(): assert get_time() == datetime.today().strftime(""%H:%M:%S"") def test_get_sec(): assert get_sec('00:01:00') == 60 def test_total_time(): now = datetime.now() later = datetime.now() + timedelta(minutes=15) assert total_time(now.strftime(""%H:%M:%S""), later.strftime(""%H:%M:%S"")) == timedelta(minutes=15) # main method tests # ----------------------------------------------------------------------------------- path = home + ""/devtrack_projects/"" + ""test.csv"" make_project_csv('test') new_start_date = datetime.today().strftime(""%Y-%m-%d"") new_start_time = datetime.today().strftime(""%H:%M:%S"") def test_make_project_csv(): assert os.path.isfile(path) == True def test_make_project_csv_initalize(): initalize_list = [""start_date"", ""start_time"", ""end_date"", ""end_time"", ""total""] with open(path, 'r') as myfile: reader = csv.reader(myfile) headers = next(reader) assert initalize_list == headers def test_start_time(): with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) start_info = next(reader) assert start_info == [new_start_date, new_start_time] def test_end_time_total(): time.sleep(5) end_time(""test"") new_end_date = str(datetime.today().strftime(""%Y-%m-%d"")) new_end_time = str(datetime.today().strftime(""%H:%M:%S"")) FMT = ""%H:%M:%S"" new_total = str(datetime.strptime(new_end_time, FMT) - datetime.strptime(new_start_time, FMT)) with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) full_entry = next(reader) assert full_entry == [new_start_date, new_start_time, new_end_date, new_end_time, new_total] def test_current_status(capsys): current_status(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Status report for 'test': You have not started work on this project yet.\n"" # report test # ----------------------------------------------------------------------------------- def test_today_report_success(capsys): today_report(""test"") captured = capsys.readouterr() res_one = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:05 today. Way to go!\n"" res_two = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:06 today. Way to go!\n"" assert captured.out == res_one or res_two def test_add_start(): start_time(""test"") def test_today_report_err_one(capsys): today_report(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Generating daily report for test...\n[-] You are in the middle of tracking, end this session with `devtracker stop` before generation a report.\n"" def test_no_dir_error(capsys): today_report(""no_dir"") captured = capsys.readouterr() assert captured.out == ""[-] 'devtracker report' was entered. You must run 'devtracker start' to create project tracking file.\n"" def test_full_report(capsys): next_line = ['1999-01-01', '00:00:00', '1999-01-01', '00:00:00', '00:00:00'] following_line = ['1999-01-02', '00:00:01', '1999-01-02', '00:00:02', '00:00:01'] with open(path, 'r', newline='') as myFile: reader = list(csv.reader(myFile)) reader.pop() reader.pop() with open(path, 'w', newline='') as myfile: wr = csv.writer(myfile, delimiter=',', quotechar='""', quoting=csv.QUOTE_ALL) for i in reader: wr.writerow(i) wr.writerow(next_line) wr.writerow(following_line) full_report(""test"") captured = capsys.readouterr() assert captured.out == """"""[+] Generating Full Report ------------------------------------------------------------------------- start_date | start_time | end_date | end_time | total | ------------------------------------------------------------------------- January 1, 1999 | 12:00 AM | January 1, 1999 | 12:00 AM | 00:00:00 | ------------------------------------------------------------------------- January 2, 1999 | 12:00 AM | January 2, 1999 | 12:00 AM | 00:00:01 | [+] You have worked on test for a total of 0:00:01. Way to go! """""" def test_remove(): os.remove(path) ", https://github.com/ConSou/devtracker,ea892d6d48aa5d4627b429469b59ae3f0ce7f10f,devtracker/test_devtracker.py::test_current_status,devtracker/test_devtracker.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import os import csv import time from datetime import datetime from datetime import timedelta from pathlib import Path from .dir_and_path_helpers import get_working_dir, mk_file_path, check_file from .time_and_date_helpers import get_date, get_sec, get_time, total_time from .time import current_status, start_time, end_time, make_project_csv from .report import today_report, full_report # dir_and_path_helpers tests # ----------------------------------------------------------------------------------- home = str(Path.home()) path = home + ""/devtrack_projects"" if os.path.exists(path): pass else: os.mkdir(path) def test_install_project_folder(): path = home + ""/devtrack_projects"" assert os.path.exists(path) == True def test_get_working_dir(): assert get_working_dir() == 'devtracker' def test_mk_file_path(): assert mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) def test_check_file(): test_path = mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) assert check_file(test_path) == True # time_and_date_helpers tests # ----------------------------------------------------------------------------------- def test_get_date(): assert get_date() == datetime.today().strftime(""%Y-%m-%d"") def test_get_time(): assert get_time() == datetime.today().strftime(""%H:%M:%S"") def test_get_sec(): assert get_sec('00:01:00') == 60 def test_total_time(): now = datetime.now() later = datetime.now() + timedelta(minutes=15) assert total_time(now.strftime(""%H:%M:%S""), later.strftime(""%H:%M:%S"")) == timedelta(minutes=15) # main method tests # ----------------------------------------------------------------------------------- path = home + ""/devtrack_projects/"" + ""test.csv"" make_project_csv('test') new_start_date = datetime.today().strftime(""%Y-%m-%d"") new_start_time = datetime.today().strftime(""%H:%M:%S"") def test_make_project_csv(): assert os.path.isfile(path) == True def test_make_project_csv_initalize(): initalize_list = [""start_date"", ""start_time"", ""end_date"", ""end_time"", ""total""] with open(path, 'r') as myfile: reader = csv.reader(myfile) headers = next(reader) assert initalize_list == headers def test_start_time(): with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) start_info = next(reader) assert start_info == [new_start_date, new_start_time] def test_end_time_total(): time.sleep(5) end_time(""test"") new_end_date = str(datetime.today().strftime(""%Y-%m-%d"")) new_end_time = str(datetime.today().strftime(""%H:%M:%S"")) FMT = ""%H:%M:%S"" new_total = str(datetime.strptime(new_end_time, FMT) - datetime.strptime(new_start_time, FMT)) with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) full_entry = next(reader) assert full_entry == [new_start_date, new_start_time, new_end_date, new_end_time, new_total] def test_current_status(capsys): current_status(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Status report for 'test': You have not started work on this project yet.\n"" # report test # ----------------------------------------------------------------------------------- def test_today_report_success(capsys): today_report(""test"") captured = capsys.readouterr() res_one = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:05 today. Way to go!\n"" res_two = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:06 today. Way to go!\n"" assert captured.out == res_one or res_two def test_add_start(): start_time(""test"") def test_today_report_err_one(capsys): today_report(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Generating daily report for test...\n[-] You are in the middle of tracking, end this session with `devtracker stop` before generation a report.\n"" def test_no_dir_error(capsys): today_report(""no_dir"") captured = capsys.readouterr() assert captured.out == ""[-] 'devtracker report' was entered. You must run 'devtracker start' to create project tracking file.\n"" def test_full_report(capsys): next_line = ['1999-01-01', '00:00:00', '1999-01-01', '00:00:00', '00:00:00'] following_line = ['1999-01-02', '00:00:01', '1999-01-02', '00:00:02', '00:00:01'] with open(path, 'r', newline='') as myFile: reader = list(csv.reader(myFile)) reader.pop() reader.pop() with open(path, 'w', newline='') as myfile: wr = csv.writer(myfile, delimiter=',', quotechar='""', quoting=csv.QUOTE_ALL) for i in reader: wr.writerow(i) wr.writerow(next_line) wr.writerow(following_line) full_report(""test"") captured = capsys.readouterr() assert captured.out == """"""[+] Generating Full Report ------------------------------------------------------------------------- start_date | start_time | end_date | end_time | total | ------------------------------------------------------------------------- January 1, 1999 | 12:00 AM | January 1, 1999 | 12:00 AM | 00:00:00 | ------------------------------------------------------------------------- January 2, 1999 | 12:00 AM | January 2, 1999 | 12:00 AM | 00:00:01 | [+] You have worked on test for a total of 0:00:01. Way to go! """""" def test_remove(): os.remove(path) ", https://github.com/ConSou/devtracker,ea892d6d48aa5d4627b429469b59ae3f0ce7f10f,devtracker/test_devtracker.py::test_end_time_total,devtracker/test_devtracker.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"import os import csv import time from datetime import datetime from datetime import timedelta from pathlib import Path from .dir_and_path_helpers import get_working_dir, mk_file_path, check_file from .time_and_date_helpers import get_date, get_sec, get_time, total_time from .time import current_status, start_time, end_time, make_project_csv from .report import today_report, full_report # dir_and_path_helpers tests # ----------------------------------------------------------------------------------- home = str(Path.home()) path = home + ""/devtrack_projects"" if os.path.exists(path): pass else: os.mkdir(path) def test_install_project_folder(): path = home + ""/devtrack_projects"" assert os.path.exists(path) == True def test_get_working_dir(): assert get_working_dir() == 'devtracker' def test_mk_file_path(): assert mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) def test_check_file(): test_path = mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) assert check_file(test_path) == True # time_and_date_helpers tests # ----------------------------------------------------------------------------------- def test_get_date(): assert get_date() == datetime.today().strftime(""%Y-%m-%d"") def test_get_time(): assert get_time() == datetime.today().strftime(""%H:%M:%S"") def test_get_sec(): assert get_sec('00:01:00') == 60 def test_total_time(): now = datetime.now() later = datetime.now() + timedelta(minutes=15) assert total_time(now.strftime(""%H:%M:%S""), later.strftime(""%H:%M:%S"")) == timedelta(minutes=15) # main method tests # ----------------------------------------------------------------------------------- path = home + ""/devtrack_projects/"" + ""test.csv"" make_project_csv('test') new_start_date = datetime.today().strftime(""%Y-%m-%d"") new_start_time = datetime.today().strftime(""%H:%M:%S"") def test_make_project_csv(): assert os.path.isfile(path) == True def test_make_project_csv_initalize(): initalize_list = [""start_date"", ""start_time"", ""end_date"", ""end_time"", ""total""] with open(path, 'r') as myfile: reader = csv.reader(myfile) headers = next(reader) assert initalize_list == headers def test_start_time(): with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) start_info = next(reader) assert start_info == [new_start_date, new_start_time] def test_end_time_total(): time.sleep(5) end_time(""test"") new_end_date = str(datetime.today().strftime(""%Y-%m-%d"")) new_end_time = str(datetime.today().strftime(""%H:%M:%S"")) FMT = ""%H:%M:%S"" new_total = str(datetime.strptime(new_end_time, FMT) - datetime.strptime(new_start_time, FMT)) with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) full_entry = next(reader) assert full_entry == [new_start_date, new_start_time, new_end_date, new_end_time, new_total] def test_current_status(capsys): current_status(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Status report for 'test': You have not started work on this project yet.\n"" # report test # ----------------------------------------------------------------------------------- def test_today_report_success(capsys): today_report(""test"") captured = capsys.readouterr() res_one = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:05 today. Way to go!\n"" res_two = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:06 today. Way to go!\n"" assert captured.out == res_one or res_two def test_add_start(): start_time(""test"") def test_today_report_err_one(capsys): today_report(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Generating daily report for test...\n[-] You are in the middle of tracking, end this session with `devtracker stop` before generation a report.\n"" def test_no_dir_error(capsys): today_report(""no_dir"") captured = capsys.readouterr() assert captured.out == ""[-] 'devtracker report' was entered. You must run 'devtracker start' to create project tracking file.\n"" def test_full_report(capsys): next_line = ['1999-01-01', '00:00:00', '1999-01-01', '00:00:00', '00:00:00'] following_line = ['1999-01-02', '00:00:01', '1999-01-02', '00:00:02', '00:00:01'] with open(path, 'r', newline='') as myFile: reader = list(csv.reader(myFile)) reader.pop() reader.pop() with open(path, 'w', newline='') as myfile: wr = csv.writer(myfile, delimiter=',', quotechar='""', quoting=csv.QUOTE_ALL) for i in reader: wr.writerow(i) wr.writerow(next_line) wr.writerow(following_line) full_report(""test"") captured = capsys.readouterr() assert captured.out == """"""[+] Generating Full Report ------------------------------------------------------------------------- start_date | start_time | end_date | end_time | total | ------------------------------------------------------------------------- January 1, 1999 | 12:00 AM | January 1, 1999 | 12:00 AM | 00:00:00 | ------------------------------------------------------------------------- January 2, 1999 | 12:00 AM | January 2, 1999 | 12:00 AM | 00:00:01 | [+] You have worked on test for a total of 0:00:01. Way to go! """""" def test_remove(): os.remove(path) ", https://github.com/ConSou/devtracker,ea892d6d48aa5d4627b429469b59ae3f0ce7f10f,devtracker/test_devtracker.py::test_full_report,devtracker/test_devtracker.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import os import csv import time from datetime import datetime from datetime import timedelta from pathlib import Path from .dir_and_path_helpers import get_working_dir, mk_file_path, check_file from .time_and_date_helpers import get_date, get_sec, get_time, total_time from .time import current_status, start_time, end_time, make_project_csv from .report import today_report, full_report # dir_and_path_helpers tests # ----------------------------------------------------------------------------------- home = str(Path.home()) path = home + ""/devtrack_projects"" if os.path.exists(path): pass else: os.mkdir(path) def test_install_project_folder(): path = home + ""/devtrack_projects"" assert os.path.exists(path) == True def test_get_working_dir(): assert get_working_dir() == 'devtracker' def test_mk_file_path(): assert mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) def test_check_file(): test_path = mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) assert check_file(test_path) == True # time_and_date_helpers tests # ----------------------------------------------------------------------------------- def test_get_date(): assert get_date() == datetime.today().strftime(""%Y-%m-%d"") def test_get_time(): assert get_time() == datetime.today().strftime(""%H:%M:%S"") def test_get_sec(): assert get_sec('00:01:00') == 60 def test_total_time(): now = datetime.now() later = datetime.now() + timedelta(minutes=15) assert total_time(now.strftime(""%H:%M:%S""), later.strftime(""%H:%M:%S"")) == timedelta(minutes=15) # main method tests # ----------------------------------------------------------------------------------- path = home + ""/devtrack_projects/"" + ""test.csv"" make_project_csv('test') new_start_date = datetime.today().strftime(""%Y-%m-%d"") new_start_time = datetime.today().strftime(""%H:%M:%S"") def test_make_project_csv(): assert os.path.isfile(path) == True def test_make_project_csv_initalize(): initalize_list = [""start_date"", ""start_time"", ""end_date"", ""end_time"", ""total""] with open(path, 'r') as myfile: reader = csv.reader(myfile) headers = next(reader) assert initalize_list == headers def test_start_time(): with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) start_info = next(reader) assert start_info == [new_start_date, new_start_time] def test_end_time_total(): time.sleep(5) end_time(""test"") new_end_date = str(datetime.today().strftime(""%Y-%m-%d"")) new_end_time = str(datetime.today().strftime(""%H:%M:%S"")) FMT = ""%H:%M:%S"" new_total = str(datetime.strptime(new_end_time, FMT) - datetime.strptime(new_start_time, FMT)) with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) full_entry = next(reader) assert full_entry == [new_start_date, new_start_time, new_end_date, new_end_time, new_total] def test_current_status(capsys): current_status(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Status report for 'test': You have not started work on this project yet.\n"" # report test # ----------------------------------------------------------------------------------- def test_today_report_success(capsys): today_report(""test"") captured = capsys.readouterr() res_one = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:05 today. Way to go!\n"" res_two = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:06 today. Way to go!\n"" assert captured.out == res_one or res_two def test_add_start(): start_time(""test"") def test_today_report_err_one(capsys): today_report(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Generating daily report for test...\n[-] You are in the middle of tracking, end this session with `devtracker stop` before generation a report.\n"" def test_no_dir_error(capsys): today_report(""no_dir"") captured = capsys.readouterr() assert captured.out == ""[-] 'devtracker report' was entered. You must run 'devtracker start' to create project tracking file.\n"" def test_full_report(capsys): next_line = ['1999-01-01', '00:00:00', '1999-01-01', '00:00:00', '00:00:00'] following_line = ['1999-01-02', '00:00:01', '1999-01-02', '00:00:02', '00:00:01'] with open(path, 'r', newline='') as myFile: reader = list(csv.reader(myFile)) reader.pop() reader.pop() with open(path, 'w', newline='') as myfile: wr = csv.writer(myfile, delimiter=',', quotechar='""', quoting=csv.QUOTE_ALL) for i in reader: wr.writerow(i) wr.writerow(next_line) wr.writerow(following_line) full_report(""test"") captured = capsys.readouterr() assert captured.out == """"""[+] Generating Full Report ------------------------------------------------------------------------- start_date | start_time | end_date | end_time | total | ------------------------------------------------------------------------- January 1, 1999 | 12:00 AM | January 1, 1999 | 12:00 AM | 00:00:00 | ------------------------------------------------------------------------- January 2, 1999 | 12:00 AM | January 2, 1999 | 12:00 AM | 00:00:01 | [+] You have worked on test for a total of 0:00:01. Way to go! """""" def test_remove(): os.remove(path) ", https://github.com/ConSou/devtracker,ea892d6d48aa5d4627b429469b59ae3f0ce7f10f,devtracker/test_devtracker.py::test_make_project_csv,devtracker/test_devtracker.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import csv import time from datetime import datetime from datetime import timedelta from pathlib import Path from .dir_and_path_helpers import get_working_dir, mk_file_path, check_file from .time_and_date_helpers import get_date, get_sec, get_time, total_time from .time import current_status, start_time, end_time, make_project_csv from .report import today_report, full_report # dir_and_path_helpers tests # ----------------------------------------------------------------------------------- home = str(Path.home()) path = home + ""/devtrack_projects"" if os.path.exists(path): pass else: os.mkdir(path) def test_install_project_folder(): path = home + ""/devtrack_projects"" assert os.path.exists(path) == True def test_get_working_dir(): assert get_working_dir() == 'devtracker' def test_mk_file_path(): assert mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) def test_check_file(): test_path = mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) assert check_file(test_path) == True # time_and_date_helpers tests # ----------------------------------------------------------------------------------- def test_get_date(): assert get_date() == datetime.today().strftime(""%Y-%m-%d"") def test_get_time(): assert get_time() == datetime.today().strftime(""%H:%M:%S"") def test_get_sec(): assert get_sec('00:01:00') == 60 def test_total_time(): now = datetime.now() later = datetime.now() + timedelta(minutes=15) assert total_time(now.strftime(""%H:%M:%S""), later.strftime(""%H:%M:%S"")) == timedelta(minutes=15) # main method tests # ----------------------------------------------------------------------------------- path = home + ""/devtrack_projects/"" + ""test.csv"" make_project_csv('test') new_start_date = datetime.today().strftime(""%Y-%m-%d"") new_start_time = datetime.today().strftime(""%H:%M:%S"") def test_make_project_csv(): assert os.path.isfile(path) == True def test_make_project_csv_initalize(): initalize_list = [""start_date"", ""start_time"", ""end_date"", ""end_time"", ""total""] with open(path, 'r') as myfile: reader = csv.reader(myfile) headers = next(reader) assert initalize_list == headers def test_start_time(): with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) start_info = next(reader) assert start_info == [new_start_date, new_start_time] def test_end_time_total(): time.sleep(5) end_time(""test"") new_end_date = str(datetime.today().strftime(""%Y-%m-%d"")) new_end_time = str(datetime.today().strftime(""%H:%M:%S"")) FMT = ""%H:%M:%S"" new_total = str(datetime.strptime(new_end_time, FMT) - datetime.strptime(new_start_time, FMT)) with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) full_entry = next(reader) assert full_entry == [new_start_date, new_start_time, new_end_date, new_end_time, new_total] def test_current_status(capsys): current_status(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Status report for 'test': You have not started work on this project yet.\n"" # report test # ----------------------------------------------------------------------------------- def test_today_report_success(capsys): today_report(""test"") captured = capsys.readouterr() res_one = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:05 today. Way to go!\n"" res_two = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:06 today. Way to go!\n"" assert captured.out == res_one or res_two def test_add_start(): start_time(""test"") def test_today_report_err_one(capsys): today_report(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Generating daily report for test...\n[-] You are in the middle of tracking, end this session with `devtracker stop` before generation a report.\n"" def test_no_dir_error(capsys): today_report(""no_dir"") captured = capsys.readouterr() assert captured.out == ""[-] 'devtracker report' was entered. You must run 'devtracker start' to create project tracking file.\n"" def test_full_report(capsys): next_line = ['1999-01-01', '00:00:00', '1999-01-01', '00:00:00', '00:00:00'] following_line = ['1999-01-02', '00:00:01', '1999-01-02', '00:00:02', '00:00:01'] with open(path, 'r', newline='') as myFile: reader = list(csv.reader(myFile)) reader.pop() reader.pop() with open(path, 'w', newline='') as myfile: wr = csv.writer(myfile, delimiter=',', quotechar='""', quoting=csv.QUOTE_ALL) for i in reader: wr.writerow(i) wr.writerow(next_line) wr.writerow(following_line) full_report(""test"") captured = capsys.readouterr() assert captured.out == """"""[+] Generating Full Report ------------------------------------------------------------------------- start_date | start_time | end_date | end_time | total | ------------------------------------------------------------------------- January 1, 1999 | 12:00 AM | January 1, 1999 | 12:00 AM | 00:00:00 | ------------------------------------------------------------------------- January 2, 1999 | 12:00 AM | January 2, 1999 | 12:00 AM | 00:00:01 | [+] You have worked on test for a total of 0:00:01. Way to go! """""" def test_remove(): os.remove(path) ", https://github.com/ConSou/devtracker,ea892d6d48aa5d4627b429469b59ae3f0ce7f10f,devtracker/test_devtracker.py::test_make_project_csv_initalize,devtracker/test_devtracker.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import os import csv import time from datetime import datetime from datetime import timedelta from pathlib import Path from .dir_and_path_helpers import get_working_dir, mk_file_path, check_file from .time_and_date_helpers import get_date, get_sec, get_time, total_time from .time import current_status, start_time, end_time, make_project_csv from .report import today_report, full_report # dir_and_path_helpers tests # ----------------------------------------------------------------------------------- home = str(Path.home()) path = home + ""/devtrack_projects"" if os.path.exists(path): pass else: os.mkdir(path) def test_install_project_folder(): path = home + ""/devtrack_projects"" assert os.path.exists(path) == True def test_get_working_dir(): assert get_working_dir() == 'devtracker' def test_mk_file_path(): assert mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) def test_check_file(): test_path = mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) assert check_file(test_path) == True # time_and_date_helpers tests # ----------------------------------------------------------------------------------- def test_get_date(): assert get_date() == datetime.today().strftime(""%Y-%m-%d"") def test_get_time(): assert get_time() == datetime.today().strftime(""%H:%M:%S"") def test_get_sec(): assert get_sec('00:01:00') == 60 def test_total_time(): now = datetime.now() later = datetime.now() + timedelta(minutes=15) assert total_time(now.strftime(""%H:%M:%S""), later.strftime(""%H:%M:%S"")) == timedelta(minutes=15) # main method tests # ----------------------------------------------------------------------------------- path = home + ""/devtrack_projects/"" + ""test.csv"" make_project_csv('test') new_start_date = datetime.today().strftime(""%Y-%m-%d"") new_start_time = datetime.today().strftime(""%H:%M:%S"") def test_make_project_csv(): assert os.path.isfile(path) == True def test_make_project_csv_initalize(): initalize_list = [""start_date"", ""start_time"", ""end_date"", ""end_time"", ""total""] with open(path, 'r') as myfile: reader = csv.reader(myfile) headers = next(reader) assert initalize_list == headers def test_start_time(): with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) start_info = next(reader) assert start_info == [new_start_date, new_start_time] def test_end_time_total(): time.sleep(5) end_time(""test"") new_end_date = str(datetime.today().strftime(""%Y-%m-%d"")) new_end_time = str(datetime.today().strftime(""%H:%M:%S"")) FMT = ""%H:%M:%S"" new_total = str(datetime.strptime(new_end_time, FMT) - datetime.strptime(new_start_time, FMT)) with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) full_entry = next(reader) assert full_entry == [new_start_date, new_start_time, new_end_date, new_end_time, new_total] def test_current_status(capsys): current_status(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Status report for 'test': You have not started work on this project yet.\n"" # report test # ----------------------------------------------------------------------------------- def test_today_report_success(capsys): today_report(""test"") captured = capsys.readouterr() res_one = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:05 today. Way to go!\n"" res_two = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:06 today. Way to go!\n"" assert captured.out == res_one or res_two def test_add_start(): start_time(""test"") def test_today_report_err_one(capsys): today_report(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Generating daily report for test...\n[-] You are in the middle of tracking, end this session with `devtracker stop` before generation a report.\n"" def test_no_dir_error(capsys): today_report(""no_dir"") captured = capsys.readouterr() assert captured.out == ""[-] 'devtracker report' was entered. You must run 'devtracker start' to create project tracking file.\n"" def test_full_report(capsys): next_line = ['1999-01-01', '00:00:00', '1999-01-01', '00:00:00', '00:00:00'] following_line = ['1999-01-02', '00:00:01', '1999-01-02', '00:00:02', '00:00:01'] with open(path, 'r', newline='') as myFile: reader = list(csv.reader(myFile)) reader.pop() reader.pop() with open(path, 'w', newline='') as myfile: wr = csv.writer(myfile, delimiter=',', quotechar='""', quoting=csv.QUOTE_ALL) for i in reader: wr.writerow(i) wr.writerow(next_line) wr.writerow(following_line) full_report(""test"") captured = capsys.readouterr() assert captured.out == """"""[+] Generating Full Report ------------------------------------------------------------------------- start_date | start_time | end_date | end_time | total | ------------------------------------------------------------------------- January 1, 1999 | 12:00 AM | January 1, 1999 | 12:00 AM | 00:00:00 | ------------------------------------------------------------------------- January 2, 1999 | 12:00 AM | January 2, 1999 | 12:00 AM | 00:00:01 | [+] You have worked on test for a total of 0:00:01. Way to go! """""" def test_remove(): os.remove(path) ", https://github.com/ConSou/devtracker,ea892d6d48aa5d4627b429469b59ae3f0ce7f10f,devtracker/test_devtracker.py::test_remove,devtracker/test_devtracker.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"import os import csv import time from datetime import datetime from datetime import timedelta from pathlib import Path from .dir_and_path_helpers import get_working_dir, mk_file_path, check_file from .time_and_date_helpers import get_date, get_sec, get_time, total_time from .time import current_status, start_time, end_time, make_project_csv from .report import today_report, full_report # dir_and_path_helpers tests # ----------------------------------------------------------------------------------- home = str(Path.home()) path = home + ""/devtrack_projects"" if os.path.exists(path): pass else: os.mkdir(path) def test_install_project_folder(): path = home + ""/devtrack_projects"" assert os.path.exists(path) == True def test_get_working_dir(): assert get_working_dir() == 'devtracker' def test_mk_file_path(): assert mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) def test_check_file(): test_path = mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) assert check_file(test_path) == True # time_and_date_helpers tests # ----------------------------------------------------------------------------------- def test_get_date(): assert get_date() == datetime.today().strftime(""%Y-%m-%d"") def test_get_time(): assert get_time() == datetime.today().strftime(""%H:%M:%S"") def test_get_sec(): assert get_sec('00:01:00') == 60 def test_total_time(): now = datetime.now() later = datetime.now() + timedelta(minutes=15) assert total_time(now.strftime(""%H:%M:%S""), later.strftime(""%H:%M:%S"")) == timedelta(minutes=15) # main method tests # ----------------------------------------------------------------------------------- path = home + ""/devtrack_projects/"" + ""test.csv"" make_project_csv('test') new_start_date = datetime.today().strftime(""%Y-%m-%d"") new_start_time = datetime.today().strftime(""%H:%M:%S"") def test_make_project_csv(): assert os.path.isfile(path) == True def test_make_project_csv_initalize(): initalize_list = [""start_date"", ""start_time"", ""end_date"", ""end_time"", ""total""] with open(path, 'r') as myfile: reader = csv.reader(myfile) headers = next(reader) assert initalize_list == headers def test_start_time(): with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) start_info = next(reader) assert start_info == [new_start_date, new_start_time] def test_end_time_total(): time.sleep(5) end_time(""test"") new_end_date = str(datetime.today().strftime(""%Y-%m-%d"")) new_end_time = str(datetime.today().strftime(""%H:%M:%S"")) FMT = ""%H:%M:%S"" new_total = str(datetime.strptime(new_end_time, FMT) - datetime.strptime(new_start_time, FMT)) with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) full_entry = next(reader) assert full_entry == [new_start_date, new_start_time, new_end_date, new_end_time, new_total] def test_current_status(capsys): current_status(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Status report for 'test': You have not started work on this project yet.\n"" # report test # ----------------------------------------------------------------------------------- def test_today_report_success(capsys): today_report(""test"") captured = capsys.readouterr() res_one = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:05 today. Way to go!\n"" res_two = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:06 today. Way to go!\n"" assert captured.out == res_one or res_two def test_add_start(): start_time(""test"") def test_today_report_err_one(capsys): today_report(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Generating daily report for test...\n[-] You are in the middle of tracking, end this session with `devtracker stop` before generation a report.\n"" def test_no_dir_error(capsys): today_report(""no_dir"") captured = capsys.readouterr() assert captured.out == ""[-] 'devtracker report' was entered. You must run 'devtracker start' to create project tracking file.\n"" def test_full_report(capsys): next_line = ['1999-01-01', '00:00:00', '1999-01-01', '00:00:00', '00:00:00'] following_line = ['1999-01-02', '00:00:01', '1999-01-02', '00:00:02', '00:00:01'] with open(path, 'r', newline='') as myFile: reader = list(csv.reader(myFile)) reader.pop() reader.pop() with open(path, 'w', newline='') as myfile: wr = csv.writer(myfile, delimiter=',', quotechar='""', quoting=csv.QUOTE_ALL) for i in reader: wr.writerow(i) wr.writerow(next_line) wr.writerow(following_line) full_report(""test"") captured = capsys.readouterr() assert captured.out == """"""[+] Generating Full Report ------------------------------------------------------------------------- start_date | start_time | end_date | end_time | total | ------------------------------------------------------------------------- January 1, 1999 | 12:00 AM | January 1, 1999 | 12:00 AM | 00:00:00 | ------------------------------------------------------------------------- January 2, 1999 | 12:00 AM | January 2, 1999 | 12:00 AM | 00:00:01 | [+] You have worked on test for a total of 0:00:01. Way to go! """""" def test_remove(): os.remove(path) ", https://github.com/ConSou/devtracker,ea892d6d48aa5d4627b429469b59ae3f0ce7f10f,devtracker/test_devtracker.py::test_start_time,devtracker/test_devtracker.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import os import csv import time from datetime import datetime from datetime import timedelta from pathlib import Path from .dir_and_path_helpers import get_working_dir, mk_file_path, check_file from .time_and_date_helpers import get_date, get_sec, get_time, total_time from .time import current_status, start_time, end_time, make_project_csv from .report import today_report, full_report # dir_and_path_helpers tests # ----------------------------------------------------------------------------------- home = str(Path.home()) path = home + ""/devtrack_projects"" if os.path.exists(path): pass else: os.mkdir(path) def test_install_project_folder(): path = home + ""/devtrack_projects"" assert os.path.exists(path) == True def test_get_working_dir(): assert get_working_dir() == 'devtracker' def test_mk_file_path(): assert mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) def test_check_file(): test_path = mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) assert check_file(test_path) == True # time_and_date_helpers tests # ----------------------------------------------------------------------------------- def test_get_date(): assert get_date() == datetime.today().strftime(""%Y-%m-%d"") def test_get_time(): assert get_time() == datetime.today().strftime(""%H:%M:%S"") def test_get_sec(): assert get_sec('00:01:00') == 60 def test_total_time(): now = datetime.now() later = datetime.now() + timedelta(minutes=15) assert total_time(now.strftime(""%H:%M:%S""), later.strftime(""%H:%M:%S"")) == timedelta(minutes=15) # main method tests # ----------------------------------------------------------------------------------- path = home + ""/devtrack_projects/"" + ""test.csv"" make_project_csv('test') new_start_date = datetime.today().strftime(""%Y-%m-%d"") new_start_time = datetime.today().strftime(""%H:%M:%S"") def test_make_project_csv(): assert os.path.isfile(path) == True def test_make_project_csv_initalize(): initalize_list = [""start_date"", ""start_time"", ""end_date"", ""end_time"", ""total""] with open(path, 'r') as myfile: reader = csv.reader(myfile) headers = next(reader) assert initalize_list == headers def test_start_time(): with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) start_info = next(reader) assert start_info == [new_start_date, new_start_time] def test_end_time_total(): time.sleep(5) end_time(""test"") new_end_date = str(datetime.today().strftime(""%Y-%m-%d"")) new_end_time = str(datetime.today().strftime(""%H:%M:%S"")) FMT = ""%H:%M:%S"" new_total = str(datetime.strptime(new_end_time, FMT) - datetime.strptime(new_start_time, FMT)) with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) full_entry = next(reader) assert full_entry == [new_start_date, new_start_time, new_end_date, new_end_time, new_total] def test_current_status(capsys): current_status(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Status report for 'test': You have not started work on this project yet.\n"" # report test # ----------------------------------------------------------------------------------- def test_today_report_success(capsys): today_report(""test"") captured = capsys.readouterr() res_one = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:05 today. Way to go!\n"" res_two = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:06 today. Way to go!\n"" assert captured.out == res_one or res_two def test_add_start(): start_time(""test"") def test_today_report_err_one(capsys): today_report(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Generating daily report for test...\n[-] You are in the middle of tracking, end this session with `devtracker stop` before generation a report.\n"" def test_no_dir_error(capsys): today_report(""no_dir"") captured = capsys.readouterr() assert captured.out == ""[-] 'devtracker report' was entered. You must run 'devtracker start' to create project tracking file.\n"" def test_full_report(capsys): next_line = ['1999-01-01', '00:00:00', '1999-01-01', '00:00:00', '00:00:00'] following_line = ['1999-01-02', '00:00:01', '1999-01-02', '00:00:02', '00:00:01'] with open(path, 'r', newline='') as myFile: reader = list(csv.reader(myFile)) reader.pop() reader.pop() with open(path, 'w', newline='') as myfile: wr = csv.writer(myfile, delimiter=',', quotechar='""', quoting=csv.QUOTE_ALL) for i in reader: wr.writerow(i) wr.writerow(next_line) wr.writerow(following_line) full_report(""test"") captured = capsys.readouterr() assert captured.out == """"""[+] Generating Full Report ------------------------------------------------------------------------- start_date | start_time | end_date | end_time | total | ------------------------------------------------------------------------- January 1, 1999 | 12:00 AM | January 1, 1999 | 12:00 AM | 00:00:00 | ------------------------------------------------------------------------- January 2, 1999 | 12:00 AM | January 2, 1999 | 12:00 AM | 00:00:01 | [+] You have worked on test for a total of 0:00:01. Way to go! """""" def test_remove(): os.remove(path) ", https://github.com/ConSou/devtracker,ea892d6d48aa5d4627b429469b59ae3f0ce7f10f,devtracker/test_devtracker.py::test_today_report_err_one,devtracker/test_devtracker.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import csv import time from datetime import datetime from datetime import timedelta from pathlib import Path from .dir_and_path_helpers import get_working_dir, mk_file_path, check_file from .time_and_date_helpers import get_date, get_sec, get_time, total_time from .time import current_status, start_time, end_time, make_project_csv from .report import today_report, full_report # dir_and_path_helpers tests # ----------------------------------------------------------------------------------- home = str(Path.home()) path = home + ""/devtrack_projects"" if os.path.exists(path): pass else: os.mkdir(path) def test_install_project_folder(): path = home + ""/devtrack_projects"" assert os.path.exists(path) == True def test_get_working_dir(): assert get_working_dir() == 'devtracker' def test_mk_file_path(): assert mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) def test_check_file(): test_path = mk_file_path('test') == '{}/devtrack_projects/test.csv'.format(home) assert check_file(test_path) == True # time_and_date_helpers tests # ----------------------------------------------------------------------------------- def test_get_date(): assert get_date() == datetime.today().strftime(""%Y-%m-%d"") def test_get_time(): assert get_time() == datetime.today().strftime(""%H:%M:%S"") def test_get_sec(): assert get_sec('00:01:00') == 60 def test_total_time(): now = datetime.now() later = datetime.now() + timedelta(minutes=15) assert total_time(now.strftime(""%H:%M:%S""), later.strftime(""%H:%M:%S"")) == timedelta(minutes=15) # main method tests # ----------------------------------------------------------------------------------- path = home + ""/devtrack_projects/"" + ""test.csv"" make_project_csv('test') new_start_date = datetime.today().strftime(""%Y-%m-%d"") new_start_time = datetime.today().strftime(""%H:%M:%S"") def test_make_project_csv(): assert os.path.isfile(path) == True def test_make_project_csv_initalize(): initalize_list = [""start_date"", ""start_time"", ""end_date"", ""end_time"", ""total""] with open(path, 'r') as myfile: reader = csv.reader(myfile) headers = next(reader) assert initalize_list == headers def test_start_time(): with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) start_info = next(reader) assert start_info == [new_start_date, new_start_time] def test_end_time_total(): time.sleep(5) end_time(""test"") new_end_date = str(datetime.today().strftime(""%Y-%m-%d"")) new_end_time = str(datetime.today().strftime(""%H:%M:%S"")) FMT = ""%H:%M:%S"" new_total = str(datetime.strptime(new_end_time, FMT) - datetime.strptime(new_start_time, FMT)) with open(path, 'r') as myfile: reader = csv.reader(myfile) next(reader) full_entry = next(reader) assert full_entry == [new_start_date, new_start_time, new_end_date, new_end_time, new_total] def test_current_status(capsys): current_status(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Status report for 'test': You have not started work on this project yet.\n"" # report test # ----------------------------------------------------------------------------------- def test_today_report_success(capsys): today_report(""test"") captured = capsys.readouterr() res_one = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:05 today. Way to go!\n"" res_two = ""[+] Generating daily report for test...\n[+] You have worked on test for a total of 0:00:06 today. Way to go!\n"" assert captured.out == res_one or res_two def test_add_start(): start_time(""test"") def test_today_report_err_one(capsys): today_report(""test"") captured = capsys.readouterr() assert captured.out == ""[+] Generating daily report for test...\n[-] You are in the middle of tracking, end this session with `devtracker stop` before generation a report.\n"" def test_no_dir_error(capsys): today_report(""no_dir"") captured = capsys.readouterr() assert captured.out == ""[-] 'devtracker report' was entered. You must run 'devtracker start' to create project tracking file.\n"" def test_full_report(capsys): next_line = ['1999-01-01', '00:00:00', '1999-01-01', '00:00:00', '00:00:00'] following_line = ['1999-01-02', '00:00:01', '1999-01-02', '00:00:02', '00:00:01'] with open(path, 'r', newline='') as myFile: reader = list(csv.reader(myFile)) reader.pop() reader.pop() with open(path, 'w', newline='') as myfile: wr = csv.writer(myfile, delimiter=',', quotechar='""', quoting=csv.QUOTE_ALL) for i in reader: wr.writerow(i) wr.writerow(next_line) wr.writerow(following_line) full_report(""test"") captured = capsys.readouterr() assert captured.out == """"""[+] Generating Full Report ------------------------------------------------------------------------- start_date | start_time | end_date | end_time | total | ------------------------------------------------------------------------- January 1, 1999 | 12:00 AM | January 1, 1999 | 12:00 AM | 00:00:00 | ------------------------------------------------------------------------- January 2, 1999 | 12:00 AM | January 2, 1999 | 12:00 AM | 00:00:01 | [+] You have worked on test for a total of 0:00:01. Way to go! """""" def test_remove(): os.remove(path) ", https://github.com/Cornices/cornice.ext.swagger,17a63e86751c7d8f1b2b75d49056161c80f4cef2,tests/test_app.py::AppSpecViewTest::test_validate_spec,tests/test_app.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import unittest import webtest from pyramid import testing from cornice import Service from cornice.service import clear_services from cornice.validators import colander_validator from flex.core import validate from .support import GetRequestSchema, PutRequestSchema, response_schemas from cornice_swagger import CorniceSwagger class AppTest(unittest.TestCase): def tearDown(self): clear_services() testing.tearDown() def setUp(self): service = Service('IceCream', '/icecream/{flavour}') @service.get(validators=(colander_validator, ), schema=GetRequestSchema(), response_schemas=response_schemas) def view_get(request): """"""Serve icecream"""""" return request.validated @service.put(validators=(colander_validator, ), schema=PutRequestSchema()) def view_put(request): """"""Add flavour"""""" return request.validated api_service = Service('OpenAPI', '/api') @api_service.get() def api_get(request): swagger = CorniceSwagger([service, api_service]) return swagger.generate('IceCreamAPI', '4.2') self.config = testing.setUp() self.config.include('cornice') self.config.include('cornice_swagger') self.config.add_cornice_service(service) self.config.add_cornice_service(api_service) self.app = webtest.TestApp(self.config.make_wsgi_app()) def test_app_get(self): self.app.get('/icecream/strawberry') def test_app_put(self): body = {'id': 'chocolate', 'timestamp': 123, 'obj': {'my_precious': True}} headers = {'bar': 'foo'} self.app.put_json('/icecream/chocolate', body, headers=headers) def test_validate_spec(self): spec = self.app.get('/api').json validate(spec) class AppSpecViewTest(unittest.TestCase): def tearDown(self): clear_services() testing.tearDown() def setUp(self): service = Service('IceCream', '/icecream/{flavour}') @service.get(validators=(colander_validator, ), schema=GetRequestSchema(), response_schemas=response_schemas) def view_get(request): """"""Serve icecream"""""" return request.validated self.config = testing.setUp() self.config.include('cornice') self.config.include('cornice_swagger') self.config.cornice_enable_openapi_view( title='IceCreamAPI', description=""OpenAPI documentation"", version='4.2' ) self.config.add_cornice_service(service) self.app = webtest.TestApp(self.config.make_wsgi_app()) def test_validate_spec(self): spec = self.app.get('/api-explorer/swagger.json').json validate(spec) class AppUIViewTest(unittest.TestCase): def tearDown(self): clear_services() testing.tearDown() def setUp(self): service = Service('IceCream', '/icecream/{flavour}') @service.get(validators=(colander_validator, ), schema=GetRequestSchema(), response_schemas=response_schemas) def view_get(request): """"""Serve icecream"""""" return request.validated self.config = testing.setUp() self.config.include('cornice') self.config.include('cornice_swagger') self.config.cornice_enable_openapi_view( title='IceCreamAPI', description=""OpenAPI documentation"", version='4.2' ) self.config.cornice_enable_openapi_explorer() self.config.add_cornice_service(service) self.app = webtest.TestApp(self.config.make_wsgi_app()) def test_served_explorer(self): result = self.app.get('/api-explorer') 'swagger-ui-bundle.js' in result.text 'swagger-ui-standalone-preset.js' in result.text 'swagger-ui.css' in result.text 'SwaggerUIBundle' in result.text class AppGoodRoutesTest(unittest.TestCase): def tearDown(self): clear_services() testing.tearDown() def setUp(self): service = Service('Ice Route', pyramid_route='ice_test') @service.get() def view_get(request): """"""Serve icecream"""""" return request.validated self.config = testing.setUp() self.config.add_route('ice_test', '/ice_test/{flavour}') self.config.include('cornice') self.config.include('cornice_swagger') self.config.add_cornice_service(service) self.services = [service] self.app = webtest.TestApp(self.config.make_wsgi_app()) def test_route_explicit_registry(self): swagger = CorniceSwagger( self.services, pyramid_registry=self.config.registry) spec = swagger.generate('IceCreamAPI', '4.2') self.assertIn('/ice_test/{flavour}', spec['paths']) def test_route_fallback_registry(self): swagger = CorniceSwagger(self.services) spec = swagger.generate('IceCreamAPI', '4.2') self.assertIn('/ice_test/{flavour}', spec['paths']) class AppBadRoutesTest(unittest.TestCase): def tearDown(self): clear_services() testing.tearDown() def setUp(self): service = Service('Ice Route', pyramid_route='ice_testXXX') self.config = testing.setUp() self.config.include('cornice') self.config.include('cornice_swagger') self.config.add_cornice_service(service) self.services = [service] self.app = webtest.TestApp(self.config.make_wsgi_app()) def test_route(self): with self.assertRaises(ValueError): swagger = CorniceSwagger(self.services) swagger.generate('IceCreamAPI', '4.2') ", https://github.com/cr0hn/aiotasks,ec485c84db55227c9283319a9c58401e294a06ff,tests/unittesting/tasks/memory/test_delay.py::test_memory_delay_add_task_non_coroutine_as_input,tests/unittesting/tasks/memory/test_delay.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import time import uuid import asyncio import logging import msgpack from aiotasks import build_manager log = logging.getLogger(""aiotasks"") # ------------------------------------------------------------------------- # Testing @task decorator # ------------------------------------------------------------------------- def test_memory_delay_task_decorator_oks(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) globals()[""test_memory_delay_task_decorator_oks_finished""] = False @manager.task() async def task_test_memory_delay_task_decorator_oks(num): globals()[""test_memory_delay_task_decorator_oks_finished""] = True async def run(): manager.run() await task_test_memory_delay_task_decorator_oks.delay(1) await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert globals()[""test_memory_delay_task_decorator_oks_finished""] is True del globals()[""test_memory_delay_task_decorator_oks_finished""] def test_memory_delay_task_decorator_timeout_raises(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) globals()[""test_memory_delay_task_decorator_timeout_raises_finished_tasks""] = False @manager.task() async def task_test_memory_delay_task_decorator_timeout_raises(num): await asyncio.sleep(num, loop=event_loop) globals()[""test_memory_delay_task_decorator_timeout_raises_finished_tasks""] = True async def run(): manager.run() await task_test_memory_delay_task_decorator_timeout_raises.delay(1) await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert globals()[""test_memory_delay_task_decorator_timeout_raises_finished_tasks""] is False del globals()[""test_memory_delay_task_decorator_timeout_raises_finished_tasks""] def test_memory_delay_task_decorator_check_correct_timeout_reached(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) _start = time.time() @manager.task() async def task_test_memory_delay_task_decorator_check_correct_timeout_reached(num): pass async def run(): manager.run() await task_test_memory_delay_task_decorator_check_correct_timeout_reached.delay(4) await manager.wait(timeout=0.5, exit_on_finish=False, wait_timeout=0.2) event_loop.run_until_complete(run()) manager.stop() _stop = time.time() _running_time = _stop - _start assert _running_time > 0.5 def test_memory_delay_task_decorator_invalid_function(event_loop): import logging logger = logging.getLogger(""aiotasks"") class CustomLogger(logging.StreamHandler): def __init__(self): super(CustomLogger, self).__init__() self.content = [] def emit(self, record): self.content.append(record.msg) custom = CustomLogger() logger.addHandler(custom) manager = build_manager(dsn=""memory://"", loop=event_loop) async def run(): # Send an invalid task name task_id = uuid.uuid4().hex await manager._task_queue.put((manager.task_list_name, msgpack.packb(dict(task_id=task_id, function=""non_exist"", args=[], kwargs={}), use_bin_type=True))) manager.run() await manager.wait(timeout=0.2, exit_on_finish=False, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert ""No local task with name 'non_exist'"" in custom.content def test_memory_delay_task_decorator_invalid_task_id_format(event_loop): import random import logging logger = logging.getLogger(""aiotasks"") class CustomLogger(logging.StreamHandler): def __init__(self): super(CustomLogger, self).__init__() self.content = [] def emit(self, record): self.content.append(record.msg) custom = CustomLogger() logger.addHandler(custom) manager = build_manager(dsn=""memory://"", loop=event_loop) task_id = random.randint(10, 1000) @manager.task() async def task_test_memory_delay_task_decorator_invalid_task_id_format(): pass async def run(): manager.run() # Send an invalid task name await manager._task_queue.put((manager.task_list_name, msgpack.packb(dict(task_id=task_id, function=""task_test_memory_delay_task_decorator_invalid_task_id_format"", args=[], kwargs={}), use_bin_type=True))) await manager.wait(timeout=0.5, exit_on_finish=False, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert ""Task ID '{}' has not valid UUID4 format"".format(task_id) in custom.content def test_memory_delay_task_decorator_custom_task_name(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) @manager.task(name=""custom_test_memory_delay_task_decorator_custom_name"") async def task_test_memory_delay_task_decorator_custom_name(): pass async def run(): manager.run() await manager.wait(timeout=0.5, exit_on_finish=False, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert ""custom_test_memory_delay_task_decorator_custom_name"" in manager.task_available_tasks.keys() # ------------------------------------------------------------------------- # Testing add_task # ------------------------------------------------------------------------- def test_memory_delay_add_task_oks(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) globals()[""test_task_test_memory_delay_add_task_oks_finished""] = False async def task_test_memory_delay_add_task_oks(num): globals()[""test_task_test_memory_delay_add_task_oks_finished""] = True async def run(): manager.run() # Add task without decorator manager.add_task(task_test_memory_delay_add_task_oks) await task_test_memory_delay_add_task_oks.delay(1) await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert globals()[""test_task_test_memory_delay_add_task_oks_finished""] is True del globals()[""test_task_test_memory_delay_add_task_oks_finished""] def test_memory_delay_add_task_custom_task_name(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) async def task_test_memory_delay_add_task_custom_task_name(): pass async def run(): manager.run() # Add task without decorator manager.add_task(task_test_memory_delay_add_task_custom_task_name, name=""custom_task_test_memory_delay_task_decorator_oks"") await task_test_memory_delay_add_task_custom_task_name.delay() await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert ""custom_task_test_memory_delay_task_decorator_oks"" in manager.task_available_tasks.keys() def test_memory_delay_add_task_non_coroutine_as_input(event_loop): import logging logger = logging.getLogger(""aiotasks"") class CustomLogger(logging.StreamHandler): def __init__(self): super(CustomLogger, self).__init__() self.content = [] def emit(self, record): self.content.append(record.msg) custom = CustomLogger() logger.addHandler(custom) manager = build_manager(dsn=""memory://"", loop=event_loop) def task_test_memory_delay_add_task_non_coroutine_as_input(): pass async def run(): manager.run() # Add task without decorator manager.add_task(task_test_memory_delay_add_task_non_coroutine_as_input, name=""custom_task_test_memory_delay_task_decorator_oks"") event_loop.run_until_complete(run()) manager.stop() assert ""Function 'task_test_memory_delay_add_task_non_coroutine_as_input' is not a coroutine and can't be added as a task"" in custom.content # assert ""custom_task_test_memory_delay_task_decorator_oks"" in manager._tasks.keys() ", https://github.com/cr0hn/aiotasks,ec485c84db55227c9283319a9c58401e294a06ff,tests/unittesting/tasks/memory/test_delay.py::test_memory_delay_task_decorator_invalid_function,tests/unittesting/tasks/memory/test_delay.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import time import uuid import asyncio import logging import msgpack from aiotasks import build_manager log = logging.getLogger(""aiotasks"") # ------------------------------------------------------------------------- # Testing @task decorator # ------------------------------------------------------------------------- def test_memory_delay_task_decorator_oks(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) globals()[""test_memory_delay_task_decorator_oks_finished""] = False @manager.task() async def task_test_memory_delay_task_decorator_oks(num): globals()[""test_memory_delay_task_decorator_oks_finished""] = True async def run(): manager.run() await task_test_memory_delay_task_decorator_oks.delay(1) await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert globals()[""test_memory_delay_task_decorator_oks_finished""] is True del globals()[""test_memory_delay_task_decorator_oks_finished""] def test_memory_delay_task_decorator_timeout_raises(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) globals()[""test_memory_delay_task_decorator_timeout_raises_finished_tasks""] = False @manager.task() async def task_test_memory_delay_task_decorator_timeout_raises(num): await asyncio.sleep(num, loop=event_loop) globals()[""test_memory_delay_task_decorator_timeout_raises_finished_tasks""] = True async def run(): manager.run() await task_test_memory_delay_task_decorator_timeout_raises.delay(1) await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert globals()[""test_memory_delay_task_decorator_timeout_raises_finished_tasks""] is False del globals()[""test_memory_delay_task_decorator_timeout_raises_finished_tasks""] def test_memory_delay_task_decorator_check_correct_timeout_reached(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) _start = time.time() @manager.task() async def task_test_memory_delay_task_decorator_check_correct_timeout_reached(num): pass async def run(): manager.run() await task_test_memory_delay_task_decorator_check_correct_timeout_reached.delay(4) await manager.wait(timeout=0.5, exit_on_finish=False, wait_timeout=0.2) event_loop.run_until_complete(run()) manager.stop() _stop = time.time() _running_time = _stop - _start assert _running_time > 0.5 def test_memory_delay_task_decorator_invalid_function(event_loop): import logging logger = logging.getLogger(""aiotasks"") class CustomLogger(logging.StreamHandler): def __init__(self): super(CustomLogger, self).__init__() self.content = [] def emit(self, record): self.content.append(record.msg) custom = CustomLogger() logger.addHandler(custom) manager = build_manager(dsn=""memory://"", loop=event_loop) async def run(): # Send an invalid task name task_id = uuid.uuid4().hex await manager._task_queue.put((manager.task_list_name, msgpack.packb(dict(task_id=task_id, function=""non_exist"", args=[], kwargs={}), use_bin_type=True))) manager.run() await manager.wait(timeout=0.2, exit_on_finish=False, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert ""No local task with name 'non_exist'"" in custom.content def test_memory_delay_task_decorator_invalid_task_id_format(event_loop): import random import logging logger = logging.getLogger(""aiotasks"") class CustomLogger(logging.StreamHandler): def __init__(self): super(CustomLogger, self).__init__() self.content = [] def emit(self, record): self.content.append(record.msg) custom = CustomLogger() logger.addHandler(custom) manager = build_manager(dsn=""memory://"", loop=event_loop) task_id = random.randint(10, 1000) @manager.task() async def task_test_memory_delay_task_decorator_invalid_task_id_format(): pass async def run(): manager.run() # Send an invalid task name await manager._task_queue.put((manager.task_list_name, msgpack.packb(dict(task_id=task_id, function=""task_test_memory_delay_task_decorator_invalid_task_id_format"", args=[], kwargs={}), use_bin_type=True))) await manager.wait(timeout=0.5, exit_on_finish=False, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert ""Task ID '{}' has not valid UUID4 format"".format(task_id) in custom.content def test_memory_delay_task_decorator_custom_task_name(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) @manager.task(name=""custom_test_memory_delay_task_decorator_custom_name"") async def task_test_memory_delay_task_decorator_custom_name(): pass async def run(): manager.run() await manager.wait(timeout=0.5, exit_on_finish=False, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert ""custom_test_memory_delay_task_decorator_custom_name"" in manager.task_available_tasks.keys() # ------------------------------------------------------------------------- # Testing add_task # ------------------------------------------------------------------------- def test_memory_delay_add_task_oks(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) globals()[""test_task_test_memory_delay_add_task_oks_finished""] = False async def task_test_memory_delay_add_task_oks(num): globals()[""test_task_test_memory_delay_add_task_oks_finished""] = True async def run(): manager.run() # Add task without decorator manager.add_task(task_test_memory_delay_add_task_oks) await task_test_memory_delay_add_task_oks.delay(1) await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert globals()[""test_task_test_memory_delay_add_task_oks_finished""] is True del globals()[""test_task_test_memory_delay_add_task_oks_finished""] def test_memory_delay_add_task_custom_task_name(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) async def task_test_memory_delay_add_task_custom_task_name(): pass async def run(): manager.run() # Add task without decorator manager.add_task(task_test_memory_delay_add_task_custom_task_name, name=""custom_task_test_memory_delay_task_decorator_oks"") await task_test_memory_delay_add_task_custom_task_name.delay() await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert ""custom_task_test_memory_delay_task_decorator_oks"" in manager.task_available_tasks.keys() def test_memory_delay_add_task_non_coroutine_as_input(event_loop): import logging logger = logging.getLogger(""aiotasks"") class CustomLogger(logging.StreamHandler): def __init__(self): super(CustomLogger, self).__init__() self.content = [] def emit(self, record): self.content.append(record.msg) custom = CustomLogger() logger.addHandler(custom) manager = build_manager(dsn=""memory://"", loop=event_loop) def task_test_memory_delay_add_task_non_coroutine_as_input(): pass async def run(): manager.run() # Add task without decorator manager.add_task(task_test_memory_delay_add_task_non_coroutine_as_input, name=""custom_task_test_memory_delay_task_decorator_oks"") event_loop.run_until_complete(run()) manager.stop() assert ""Function 'task_test_memory_delay_add_task_non_coroutine_as_input' is not a coroutine and can't be added as a task"" in custom.content # assert ""custom_task_test_memory_delay_task_decorator_oks"" in manager._tasks.keys() ", https://github.com/cr0hn/aiotasks,ec485c84db55227c9283319a9c58401e294a06ff,tests/unittesting/tasks/memory/test_delay.py::test_memory_delay_task_decorator_invalid_task_id_format,tests/unittesting/tasks/memory/test_delay.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import time import uuid import asyncio import logging import msgpack from aiotasks import build_manager log = logging.getLogger(""aiotasks"") # ------------------------------------------------------------------------- # Testing @task decorator # ------------------------------------------------------------------------- def test_memory_delay_task_decorator_oks(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) globals()[""test_memory_delay_task_decorator_oks_finished""] = False @manager.task() async def task_test_memory_delay_task_decorator_oks(num): globals()[""test_memory_delay_task_decorator_oks_finished""] = True async def run(): manager.run() await task_test_memory_delay_task_decorator_oks.delay(1) await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert globals()[""test_memory_delay_task_decorator_oks_finished""] is True del globals()[""test_memory_delay_task_decorator_oks_finished""] def test_memory_delay_task_decorator_timeout_raises(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) globals()[""test_memory_delay_task_decorator_timeout_raises_finished_tasks""] = False @manager.task() async def task_test_memory_delay_task_decorator_timeout_raises(num): await asyncio.sleep(num, loop=event_loop) globals()[""test_memory_delay_task_decorator_timeout_raises_finished_tasks""] = True async def run(): manager.run() await task_test_memory_delay_task_decorator_timeout_raises.delay(1) await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert globals()[""test_memory_delay_task_decorator_timeout_raises_finished_tasks""] is False del globals()[""test_memory_delay_task_decorator_timeout_raises_finished_tasks""] def test_memory_delay_task_decorator_check_correct_timeout_reached(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) _start = time.time() @manager.task() async def task_test_memory_delay_task_decorator_check_correct_timeout_reached(num): pass async def run(): manager.run() await task_test_memory_delay_task_decorator_check_correct_timeout_reached.delay(4) await manager.wait(timeout=0.5, exit_on_finish=False, wait_timeout=0.2) event_loop.run_until_complete(run()) manager.stop() _stop = time.time() _running_time = _stop - _start assert _running_time > 0.5 def test_memory_delay_task_decorator_invalid_function(event_loop): import logging logger = logging.getLogger(""aiotasks"") class CustomLogger(logging.StreamHandler): def __init__(self): super(CustomLogger, self).__init__() self.content = [] def emit(self, record): self.content.append(record.msg) custom = CustomLogger() logger.addHandler(custom) manager = build_manager(dsn=""memory://"", loop=event_loop) async def run(): # Send an invalid task name task_id = uuid.uuid4().hex await manager._task_queue.put((manager.task_list_name, msgpack.packb(dict(task_id=task_id, function=""non_exist"", args=[], kwargs={}), use_bin_type=True))) manager.run() await manager.wait(timeout=0.2, exit_on_finish=False, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert ""No local task with name 'non_exist'"" in custom.content def test_memory_delay_task_decorator_invalid_task_id_format(event_loop): import random import logging logger = logging.getLogger(""aiotasks"") class CustomLogger(logging.StreamHandler): def __init__(self): super(CustomLogger, self).__init__() self.content = [] def emit(self, record): self.content.append(record.msg) custom = CustomLogger() logger.addHandler(custom) manager = build_manager(dsn=""memory://"", loop=event_loop) task_id = random.randint(10, 1000) @manager.task() async def task_test_memory_delay_task_decorator_invalid_task_id_format(): pass async def run(): manager.run() # Send an invalid task name await manager._task_queue.put((manager.task_list_name, msgpack.packb(dict(task_id=task_id, function=""task_test_memory_delay_task_decorator_invalid_task_id_format"", args=[], kwargs={}), use_bin_type=True))) await manager.wait(timeout=0.5, exit_on_finish=False, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert ""Task ID '{}' has not valid UUID4 format"".format(task_id) in custom.content def test_memory_delay_task_decorator_custom_task_name(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) @manager.task(name=""custom_test_memory_delay_task_decorator_custom_name"") async def task_test_memory_delay_task_decorator_custom_name(): pass async def run(): manager.run() await manager.wait(timeout=0.5, exit_on_finish=False, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert ""custom_test_memory_delay_task_decorator_custom_name"" in manager.task_available_tasks.keys() # ------------------------------------------------------------------------- # Testing add_task # ------------------------------------------------------------------------- def test_memory_delay_add_task_oks(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) globals()[""test_task_test_memory_delay_add_task_oks_finished""] = False async def task_test_memory_delay_add_task_oks(num): globals()[""test_task_test_memory_delay_add_task_oks_finished""] = True async def run(): manager.run() # Add task without decorator manager.add_task(task_test_memory_delay_add_task_oks) await task_test_memory_delay_add_task_oks.delay(1) await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert globals()[""test_task_test_memory_delay_add_task_oks_finished""] is True del globals()[""test_task_test_memory_delay_add_task_oks_finished""] def test_memory_delay_add_task_custom_task_name(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) async def task_test_memory_delay_add_task_custom_task_name(): pass async def run(): manager.run() # Add task without decorator manager.add_task(task_test_memory_delay_add_task_custom_task_name, name=""custom_task_test_memory_delay_task_decorator_oks"") await task_test_memory_delay_add_task_custom_task_name.delay() await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert ""custom_task_test_memory_delay_task_decorator_oks"" in manager.task_available_tasks.keys() def test_memory_delay_add_task_non_coroutine_as_input(event_loop): import logging logger = logging.getLogger(""aiotasks"") class CustomLogger(logging.StreamHandler): def __init__(self): super(CustomLogger, self).__init__() self.content = [] def emit(self, record): self.content.append(record.msg) custom = CustomLogger() logger.addHandler(custom) manager = build_manager(dsn=""memory://"", loop=event_loop) def task_test_memory_delay_add_task_non_coroutine_as_input(): pass async def run(): manager.run() # Add task without decorator manager.add_task(task_test_memory_delay_add_task_non_coroutine_as_input, name=""custom_task_test_memory_delay_task_decorator_oks"") event_loop.run_until_complete(run()) manager.stop() assert ""Function 'task_test_memory_delay_add_task_non_coroutine_as_input' is not a coroutine and can't be added as a task"" in custom.content # assert ""custom_task_test_memory_delay_task_decorator_oks"" in manager._tasks.keys() ", https://github.com/cr0hn/aiotasks,ec485c84db55227c9283319a9c58401e294a06ff,tests/unittesting/tasks/memory/test_subscribers.py::test_memory_subscribers_empty_topics,tests/unittesting/tasks/memory/test_subscribers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import time import uuid import asyncio import msgpack from aiotasks import build_manager def test_memory_subscribers_oks(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) globals()[""test_memory_subscribers_oks_finished""] = False @manager.subscribe(""hello"") async def task_test_memory_subscribers_oks(topic, data): if topic == ""hello"" and data == ""world"": globals()[""test_memory_subscribers_oks_finished""] = True async def run(): manager.run() await manager.publish(""hello"", ""world"") await manager.wait(timeout=0.2, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert globals()[""test_memory_subscribers_oks_finished""] is True del globals()[""test_memory_subscribers_oks_finished""] def test_memory_subscribers_no_topics(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) async def run(): manager.run() await manager.publish(""hello"", ""world"") await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.2) event_loop.run_until_complete(run()) manager.stop() assert len(manager.topics_subscribers) == 0 def test_memory_subscribers_empty_topics(event_loop): import logging logger = logging.getLogger(""aiotasks"") class CustomLogger(logging.StreamHandler): def __init__(self): super(CustomLogger, self).__init__() self.content = [] def emit(self, record): self.content.append(record.msg) custom = CustomLogger() logger.addHandler(custom) manager = build_manager(dsn=""memory://"", loop=event_loop) @manager.subscribe() async def task_test_memory_subscribers_oks(topic, data): if topic == ""hello"" and data == ""world"": globals()[""test_memory_subscribers_oks_finished""] = True async def run(): manager.run() await manager.publish(""hello"", ""world"") await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.2) event_loop.run_until_complete(run()) manager.stop() assert len(manager.topics_subscribers) == 0 assert ""Empty topic fount in function 'task_test_memory_subscribers_oks'. Skipping it."" in custom.content def test_memory_subscribers_duplicated_topics(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) @manager.subscribe(""hello"") async def task_test_memory_subscribers_oks(topic, data): pass @manager.subscribe(""hello"") async def task_test_memory_subscribers_oks_2(topic, data): pass async def run(): manager.run() await manager.publish(""hello"", ""world"") await manager.wait(timeout=0.2, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert len(manager.topics_subscribers) == 1 def test_memory_subscribers_timeout_raises(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop) globals()[""test_memory_subscribers_timeout_raises_finished_tasks""] = False @manager.subscribe(""hello"") async def task_test_memory_subscribers_oks(topic, data): if topic == ""hello"": await asyncio.sleep(data, loop=event_loop) globals()[""test_memory_subscribers_timeout_raises_finished_tasks""] = True async def run(): manager.run() await manager.publish(""hello"", 5) await manager.wait(timeout=0.5, exit_on_finish=True, wait_timeout=0.1) event_loop.run_until_complete(run()) manager.stop() assert globals()[""test_memory_subscribers_timeout_raises_finished_tasks""] is False del globals()[""test_memory_subscribers_timeout_raises_finished_tasks""] ", https://github.com/cr0hn/aiotasks,ec485c84db55227c9283319a9c58401e294a06ff,tests/unittesting/tasks/test_base_async.py::test_build_manager_invalid_prefix,tests/unittesting/tasks/test_base_async.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from aiotasks import build_manager def test_build_manager_invalid_prefix(event_loop): import logging logger = logging.getLogger(""aiotasks"") class CustomLogger(logging.StreamHandler): def __init__(self): super(CustomLogger, self).__init__() self.content = [] def emit(self, record): self.content.append(record.msg) custom = CustomLogger() logger.addHandler(custom) manager = build_manager(dsn=""memory://"", loop=event_loop, prefix=None) assert manager.task_prefix == ""aiotasks"" assert ""Empty task_prefix. Using 'aiotasks' as task_prefix"" in custom.content def test_build_manager_invalid_prefix_type(event_loop): manager = build_manager(dsn=""memory://"", loop=event_loop, prefix=11) assert manager.task_prefix == ""11"" ", https://github.com/crazyscientist/osc-tiny,88c33c2c7f73ea26067e1bb4190bab5dc298dd85,osctiny/tests/test_issues.py::TestIssue::test_get,osctiny/tests/test_issues.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# -*- coding: utf8 -*- from __future__ import unicode_literals import re from sys import version_info from lxml.objectify import ObjectifiedElement from requests.exceptions import HTTPError import responses from .base import OscTest, CallbackFactory class TestIssue(OscTest): def setUp(self): super(TestIssue, self).setUp() def callback(headers, params, request): status, body = 200, """""" boost trac Boost Trac https://svn.boost.org/trac/boost/ https://svn.boost.org/trac/boost/ticket/@@@ boost#(\d+) false bco bugzilla Clutter Project Bugzilla http://bugzilla.clutter-project.org/ http://bugzilla.clutter-project.org/show_bug.cgi?id=@@@ bco#(\d+) false bnc bugzilla SUSE Bugzilla https://apibugzilla.novell.com/ https://bugzilla.suse.com/show_bug.cgi?id=@@@ (?:bnc|BNC|bsc|BSC|boo|BOO)\s*[#:]\s*(\d+) true """""" return status, headers, body self.mock_request( method=responses.GET, url=re.compile(self.osc.url + '/issue_trackers/?$'), callback=CallbackFactory(callback) ) @responses.activate def test_get_trackers(self): response = self.osc.issues.get_trackers() self.assertTrue(isinstance(response, ObjectifiedElement)) self.assertEqual(response.countchildren(), 3) self.assertEqual( {x.text for x in response.xpath(""issue-tracker/name"")}, {'boost', 'bco', 'bnc'} ) @responses.activate def test_get_tracker(self): def callback(headers, params, request): if re.search(""bnc/?$"", request.url): status, body = 200, """""" bnc bugzilla SUSE Bugzilla https://apibugzilla.novell.com/ https://bugzilla.suse.com/show_bug.cgi?id=@@@ (?:bnc|BNC|bsc|BSC|boo|BOO)\s*[#:]\s*(\d+) true """""" else: status, body = 404, """""" Unable to find issue tracker 'bnc2' """""" return status, headers, body self.mock_request( method=responses.GET, url=re.compile(self.osc.url + r'/issue_trackers/[^/]+/?$'), callback=CallbackFactory(callback) ) with self.subTest(""Valid tracker""): response = self.osc.issues.get_tracker(""bnc"") self.assertEqual(response.name.text, ""bnc"") with self.subTest(""Invalid tracker""): self.assertRaises(HTTPError, self.osc.issues.get_tracker, ""nemo"") @responses.activate def test_get(self): def callback(headers, params, request): if params.get(""force_update"", [""0""]) == [""1""]: status, body = 200, u"""""" 2020-01-04 14:12:00 UTC 1160086 bnc https://bugzilla.suse.com/show_bug.cgi?id=1160086 OPEN føø bar nemo nemo@suse.com Caþtæn Nemo """""" else: status, body = 200, """""" 2020-01-04 14:12:00 UTC 1160086 bnc https://bugzilla.suse.com/show_bug.cgi?id=1160086 """""" return status, headers, body self.mock_request( method=responses.GET, url=re.compile(self.osc.url + r'/issue_trackers/bnc/issues/1160086/?.*'), callback=CallbackFactory(callback) ) with self.subTest(""Manual force update""): response = self.osc.issues.get(""bnc"", 1160086, True) self.assertTrue(hasattr(response, ""summary"")) self.assertEqual(len(responses.calls), 2) with self.subTest(""Manual force update""): response = self.osc.issues.get(""bnc"", 1160086, False) self.assertTrue(hasattr(response, ""summary"")) # to whom it may concern: `responses.calls` does not get reset # between sub-tests self.assertEqual(len(responses.calls), 6 if version_info.major < 3 else 4) ", https://github.com/crumpstrr33/Utter-More,418cf5f5ef337700bf0897e746eff8bb275e6da6,test/test_utter_more.py::test_ibu_aut,test/test_utter_more.py,NIO,flaky,Opened,https://github.com/crumpstrr33/Utter-More/pull/1,classify;root_cause,"import filecmp from inspect import getsourcefile import os.path as path, sys cur_dir = path.dirname(path.abspath(getsourcefile(lambda: 0))) sys.path.insert(0, path.join(cur_dir[:cur_dir.rfind(path.sep)], 'utter_more')) import pytest from utter_more import UtterMore sys.path.pop(0) DOUBLE_CURLY = '{{beginning}}{{middle}}{{end}}' DC_ANS = ['{beginning}{middle}{end}', '{beginning}{middle}', '{beginning}{end}', '{beginning}', '{middle}{end}', '{middle}', '{end}', ''] SINGLE_CURLY = '{beginning}{middle}{end}' SC_ANS = ['{beginning}{middle}{end}'] OR_CURLY = '(be|gin|ning)(mid|dle)(en|d)' OC_ANS = ['bemiden', 'bemidd', 'bedleen', 'bedled', 'ginmiden', 'ginmidd', 'gindleen', 'gindled', 'ningmiden', 'ningmidd', 'ningdleen', 'ningdled'] COND_OR_CURLY = '(be^1|gin^2|ning)(mid*1|dle*2)(en*1|d*2)' COC_ANS = ['bemiden', 'bemidd', 'bedleen', 'ginmidd', 'gindleen', 'gindled', 'ningmiden', 'ningmidd', 'ningdleen', 'ningdled'] COND_AND_CURLY = '(be*1|gin*2|ning)(mid^1|dle^2)(en^1|d^2)' CAC_ANS = ['bemiden', 'gindled'] @pytest.fixture() def local_um(): # A fresh and empty UtterMore every time um = UtterMore() return um @pytest.fixture(scope='module') def global_um(): # Retains the utterances added to it um = UtterMore() return um @pytest.mark.parametrize('template, utterances', [ (DOUBLE_CURLY, DC_ANS), (SINGLE_CURLY, SC_ANS), (OR_CURLY, OC_ANS), (COND_OR_CURLY, COC_ANS), (COND_AND_CURLY, CAC_ANS) ]) def test_build_utterances(local_um, template, utterances): """""" Test edge case utterance templates """""" assert local_um.build_utterances(template) == utterances def test_ibu_aut(global_um): """""" Test methods following UtterMore methods: - iter_build_utterances - add_utterance_template """""" global_um.add_utterance_template(DOUBLE_CURLY) global_um.add_utterance_template(SINGLE_CURLY) global_um.add_utterance_template(OR_CURLY) global_um.iter_build_utterances() assert global_um.utterances == [DC_ANS, SC_ANS, OC_ANS] @pytest.mark.parametrize('fname, saved_as, written_as', [ ('alexa_test', 'csv', None), ('csv_test', 'csv', None), ('txt_test', 'txt', None), ('file_override_test', 'txt', 'csv') ]) def test_saving_utterances(global_um, tmpdir, fname, saved_as, written_as): """""" Test the saving methods of UtterMore """""" written_as = written_as or saved_as test_dir = path.join(path.dirname(path.realpath(__file__)), 'test_files') if fname == 'alexa_test': global_um.save_for_alexa(tmpdir, fname) else: global_um.save_utterances(tmpdir, fname, saved_as, written_as=written_as) file_name = fname + '.' + saved_as assert filecmp.cmp(path.join(test_dir, file_name), tmpdir.join(file_name)) ", https://github.com/crumpstrr33/Utter-More,418cf5f5ef337700bf0897e746eff8bb275e6da6,test/test_utter_more.py::test_saving_utterances[alexa_test-csv-None],test/test_utter_more.py,OD-Brit,flaky,Opened,https://github.com/crumpstrr33/Utter-More/pull/2,classify;root_cause,"import filecmp from inspect import getsourcefile import os.path as path, sys cur_dir = path.dirname(path.abspath(getsourcefile(lambda: 0))) sys.path.insert(0, path.join(cur_dir[:cur_dir.rfind(path.sep)], 'utter_more')) import pytest from utter_more import UtterMore sys.path.pop(0) DOUBLE_CURLY = '{{beginning}}{{middle}}{{end}}' DC_ANS = ['{beginning}{middle}{end}', '{beginning}{middle}', '{beginning}{end}', '{beginning}', '{middle}{end}', '{middle}', '{end}', ''] SINGLE_CURLY = '{beginning}{middle}{end}' SC_ANS = ['{beginning}{middle}{end}'] OR_CURLY = '(be|gin|ning)(mid|dle)(en|d)' OC_ANS = ['bemiden', 'bemidd', 'bedleen', 'bedled', 'ginmiden', 'ginmidd', 'gindleen', 'gindled', 'ningmiden', 'ningmidd', 'ningdleen', 'ningdled'] COND_OR_CURLY = '(be^1|gin^2|ning)(mid*1|dle*2)(en*1|d*2)' COC_ANS = ['bemiden', 'bemidd', 'bedleen', 'ginmidd', 'gindleen', 'gindled', 'ningmiden', 'ningmidd', 'ningdleen', 'ningdled'] COND_AND_CURLY = '(be*1|gin*2|ning)(mid^1|dle^2)(en^1|d^2)' CAC_ANS = ['bemiden', 'gindled'] @pytest.fixture() def local_um(): # A fresh and empty UtterMore every time um = UtterMore() return um @pytest.fixture(scope='module') def global_um(): # Retains the utterances added to it um = UtterMore() return um @pytest.mark.parametrize('template, utterances', [ (DOUBLE_CURLY, DC_ANS), (SINGLE_CURLY, SC_ANS), (OR_CURLY, OC_ANS), (COND_OR_CURLY, COC_ANS), (COND_AND_CURLY, CAC_ANS) ]) def test_build_utterances(local_um, template, utterances): """""" Test edge case utterance templates """""" assert local_um.build_utterances(template) == utterances def test_ibu_aut(global_um): """""" Test methods following UtterMore methods: - iter_build_utterances - add_utterance_template """""" global_um.add_utterance_template(DOUBLE_CURLY) global_um.add_utterance_template(SINGLE_CURLY) global_um.add_utterance_template(OR_CURLY) global_um.iter_build_utterances() assert global_um.utterances == [DC_ANS, SC_ANS, OC_ANS] @pytest.mark.parametrize('fname, saved_as, written_as', [ ('alexa_test', 'csv', None), ('csv_test', 'csv', None), ('txt_test', 'txt', None), ('file_override_test', 'txt', 'csv') ]) def test_saving_utterances(global_um, tmpdir, fname, saved_as, written_as): """""" Test the saving methods of UtterMore """""" written_as = written_as or saved_as test_dir = path.join(path.dirname(path.realpath(__file__)), 'test_files') if fname == 'alexa_test': global_um.save_for_alexa(tmpdir, fname) else: global_um.save_utterances(tmpdir, fname, saved_as, written_as=written_as) file_name = fname + '.' + saved_as assert filecmp.cmp(path.join(test_dir, file_name), tmpdir.join(file_name)) ", https://github.com/crumpstrr33/Utter-More,418cf5f5ef337700bf0897e746eff8bb275e6da6,test/test_utter_more.py::test_saving_utterances[csv_test-csv-None],test/test_utter_more.py,OD-Brit,flaky,Opened,https://github.com/crumpstrr33/Utter-More/pull/2,classify;root_cause,"import filecmp from inspect import getsourcefile import os.path as path, sys cur_dir = path.dirname(path.abspath(getsourcefile(lambda: 0))) sys.path.insert(0, path.join(cur_dir[:cur_dir.rfind(path.sep)], 'utter_more')) import pytest from utter_more import UtterMore sys.path.pop(0) DOUBLE_CURLY = '{{beginning}}{{middle}}{{end}}' DC_ANS = ['{beginning}{middle}{end}', '{beginning}{middle}', '{beginning}{end}', '{beginning}', '{middle}{end}', '{middle}', '{end}', ''] SINGLE_CURLY = '{beginning}{middle}{end}' SC_ANS = ['{beginning}{middle}{end}'] OR_CURLY = '(be|gin|ning)(mid|dle)(en|d)' OC_ANS = ['bemiden', 'bemidd', 'bedleen', 'bedled', 'ginmiden', 'ginmidd', 'gindleen', 'gindled', 'ningmiden', 'ningmidd', 'ningdleen', 'ningdled'] COND_OR_CURLY = '(be^1|gin^2|ning)(mid*1|dle*2)(en*1|d*2)' COC_ANS = ['bemiden', 'bemidd', 'bedleen', 'ginmidd', 'gindleen', 'gindled', 'ningmiden', 'ningmidd', 'ningdleen', 'ningdled'] COND_AND_CURLY = '(be*1|gin*2|ning)(mid^1|dle^2)(en^1|d^2)' CAC_ANS = ['bemiden', 'gindled'] @pytest.fixture() def local_um(): # A fresh and empty UtterMore every time um = UtterMore() return um @pytest.fixture(scope='module') def global_um(): # Retains the utterances added to it um = UtterMore() return um @pytest.mark.parametrize('template, utterances', [ (DOUBLE_CURLY, DC_ANS), (SINGLE_CURLY, SC_ANS), (OR_CURLY, OC_ANS), (COND_OR_CURLY, COC_ANS), (COND_AND_CURLY, CAC_ANS) ]) def test_build_utterances(local_um, template, utterances): """""" Test edge case utterance templates """""" assert local_um.build_utterances(template) == utterances def test_ibu_aut(global_um): """""" Test methods following UtterMore methods: - iter_build_utterances - add_utterance_template """""" global_um.add_utterance_template(DOUBLE_CURLY) global_um.add_utterance_template(SINGLE_CURLY) global_um.add_utterance_template(OR_CURLY) global_um.iter_build_utterances() assert global_um.utterances == [DC_ANS, SC_ANS, OC_ANS] @pytest.mark.parametrize('fname, saved_as, written_as', [ ('alexa_test', 'csv', None), ('csv_test', 'csv', None), ('txt_test', 'txt', None), ('file_override_test', 'txt', 'csv') ]) def test_saving_utterances(global_um, tmpdir, fname, saved_as, written_as): """""" Test the saving methods of UtterMore """""" written_as = written_as or saved_as test_dir = path.join(path.dirname(path.realpath(__file__)), 'test_files') if fname == 'alexa_test': global_um.save_for_alexa(tmpdir, fname) else: global_um.save_utterances(tmpdir, fname, saved_as, written_as=written_as) file_name = fname + '.' + saved_as assert filecmp.cmp(path.join(test_dir, file_name), tmpdir.join(file_name)) ", https://github.com/crumpstrr33/Utter-More,418cf5f5ef337700bf0897e746eff8bb275e6da6,test/test_utter_more.py::test_saving_utterances[file_override_test-txt-csv],test/test_utter_more.py,OD-Brit,flaky,Opened,https://github.com/crumpstrr33/Utter-More/pull/2,classify;root_cause,"import filecmp from inspect import getsourcefile import os.path as path, sys cur_dir = path.dirname(path.abspath(getsourcefile(lambda: 0))) sys.path.insert(0, path.join(cur_dir[:cur_dir.rfind(path.sep)], 'utter_more')) import pytest from utter_more import UtterMore sys.path.pop(0) DOUBLE_CURLY = '{{beginning}}{{middle}}{{end}}' DC_ANS = ['{beginning}{middle}{end}', '{beginning}{middle}', '{beginning}{end}', '{beginning}', '{middle}{end}', '{middle}', '{end}', ''] SINGLE_CURLY = '{beginning}{middle}{end}' SC_ANS = ['{beginning}{middle}{end}'] OR_CURLY = '(be|gin|ning)(mid|dle)(en|d)' OC_ANS = ['bemiden', 'bemidd', 'bedleen', 'bedled', 'ginmiden', 'ginmidd', 'gindleen', 'gindled', 'ningmiden', 'ningmidd', 'ningdleen', 'ningdled'] COND_OR_CURLY = '(be^1|gin^2|ning)(mid*1|dle*2)(en*1|d*2)' COC_ANS = ['bemiden', 'bemidd', 'bedleen', 'ginmidd', 'gindleen', 'gindled', 'ningmiden', 'ningmidd', 'ningdleen', 'ningdled'] COND_AND_CURLY = '(be*1|gin*2|ning)(mid^1|dle^2)(en^1|d^2)' CAC_ANS = ['bemiden', 'gindled'] @pytest.fixture() def local_um(): # A fresh and empty UtterMore every time um = UtterMore() return um @pytest.fixture(scope='module') def global_um(): # Retains the utterances added to it um = UtterMore() return um @pytest.mark.parametrize('template, utterances', [ (DOUBLE_CURLY, DC_ANS), (SINGLE_CURLY, SC_ANS), (OR_CURLY, OC_ANS), (COND_OR_CURLY, COC_ANS), (COND_AND_CURLY, CAC_ANS) ]) def test_build_utterances(local_um, template, utterances): """""" Test edge case utterance templates """""" assert local_um.build_utterances(template) == utterances def test_ibu_aut(global_um): """""" Test methods following UtterMore methods: - iter_build_utterances - add_utterance_template """""" global_um.add_utterance_template(DOUBLE_CURLY) global_um.add_utterance_template(SINGLE_CURLY) global_um.add_utterance_template(OR_CURLY) global_um.iter_build_utterances() assert global_um.utterances == [DC_ANS, SC_ANS, OC_ANS] @pytest.mark.parametrize('fname, saved_as, written_as', [ ('alexa_test', 'csv', None), ('csv_test', 'csv', None), ('txt_test', 'txt', None), ('file_override_test', 'txt', 'csv') ]) def test_saving_utterances(global_um, tmpdir, fname, saved_as, written_as): """""" Test the saving methods of UtterMore """""" written_as = written_as or saved_as test_dir = path.join(path.dirname(path.realpath(__file__)), 'test_files') if fname == 'alexa_test': global_um.save_for_alexa(tmpdir, fname) else: global_um.save_utterances(tmpdir, fname, saved_as, written_as=written_as) file_name = fname + '.' + saved_as assert filecmp.cmp(path.join(test_dir, file_name), tmpdir.join(file_name)) ", https://github.com/crumpstrr33/Utter-More,418cf5f5ef337700bf0897e746eff8bb275e6da6,test/test_utter_more.py::test_saving_utterances[txt_test-txt-None],test/test_utter_more.py,OD-Brit,flaky,Opened,https://github.com/crumpstrr33/Utter-More/pull/2,classify;root_cause,"import filecmp from inspect import getsourcefile import os.path as path, sys cur_dir = path.dirname(path.abspath(getsourcefile(lambda: 0))) sys.path.insert(0, path.join(cur_dir[:cur_dir.rfind(path.sep)], 'utter_more')) import pytest from utter_more import UtterMore sys.path.pop(0) DOUBLE_CURLY = '{{beginning}}{{middle}}{{end}}' DC_ANS = ['{beginning}{middle}{end}', '{beginning}{middle}', '{beginning}{end}', '{beginning}', '{middle}{end}', '{middle}', '{end}', ''] SINGLE_CURLY = '{beginning}{middle}{end}' SC_ANS = ['{beginning}{middle}{end}'] OR_CURLY = '(be|gin|ning)(mid|dle)(en|d)' OC_ANS = ['bemiden', 'bemidd', 'bedleen', 'bedled', 'ginmiden', 'ginmidd', 'gindleen', 'gindled', 'ningmiden', 'ningmidd', 'ningdleen', 'ningdled'] COND_OR_CURLY = '(be^1|gin^2|ning)(mid*1|dle*2)(en*1|d*2)' COC_ANS = ['bemiden', 'bemidd', 'bedleen', 'ginmidd', 'gindleen', 'gindled', 'ningmiden', 'ningmidd', 'ningdleen', 'ningdled'] COND_AND_CURLY = '(be*1|gin*2|ning)(mid^1|dle^2)(en^1|d^2)' CAC_ANS = ['bemiden', 'gindled'] @pytest.fixture() def local_um(): # A fresh and empty UtterMore every time um = UtterMore() return um @pytest.fixture(scope='module') def global_um(): # Retains the utterances added to it um = UtterMore() return um @pytest.mark.parametrize('template, utterances', [ (DOUBLE_CURLY, DC_ANS), (SINGLE_CURLY, SC_ANS), (OR_CURLY, OC_ANS), (COND_OR_CURLY, COC_ANS), (COND_AND_CURLY, CAC_ANS) ]) def test_build_utterances(local_um, template, utterances): """""" Test edge case utterance templates """""" assert local_um.build_utterances(template) == utterances def test_ibu_aut(global_um): """""" Test methods following UtterMore methods: - iter_build_utterances - add_utterance_template """""" global_um.add_utterance_template(DOUBLE_CURLY) global_um.add_utterance_template(SINGLE_CURLY) global_um.add_utterance_template(OR_CURLY) global_um.iter_build_utterances() assert global_um.utterances == [DC_ANS, SC_ANS, OC_ANS] @pytest.mark.parametrize('fname, saved_as, written_as', [ ('alexa_test', 'csv', None), ('csv_test', 'csv', None), ('txt_test', 'txt', None), ('file_override_test', 'txt', 'csv') ]) def test_saving_utterances(global_um, tmpdir, fname, saved_as, written_as): """""" Test the saving methods of UtterMore """""" written_as = written_as or saved_as test_dir = path.join(path.dirname(path.realpath(__file__)), 'test_files') if fname == 'alexa_test': global_um.save_for_alexa(tmpdir, fname) else: global_um.save_utterances(tmpdir, fname, saved_as, written_as=written_as) file_name = fname + '.' + saved_as assert filecmp.cmp(path.join(test_dir, file_name), tmpdir.join(file_name)) ", https://github.com/CyberZHG/keras-layer-normalization,077dc72b6bae1a0551c3b879557478b587be340d,tests/test_layer_normalization.py,tests/test_layer_normalization.py,ID,flaky,Accepted,https://github.com/CyberZHG/keras-layer-normalization/pull/4,classify;root_cause;fix_proposal,"import os import tempfile import unittest import numpy as np import tensorflow as tf from keras_multi_head import MultiHeadAttention from keras_layer_normalization.backend import keras from keras_layer_normalization import LayerNormalization class TestLayerNormalization(unittest.TestCase): def test_sample(self): input_layer = keras.layers.Input( shape=(2, 3), name='Input', ) norm_layer = LayerNormalization( name='Layer-Normalization', )(input_layer) model = keras.models.Model( inputs=input_layer, outputs=norm_layer, ) model.compile( optimizer='adam', loss='mse', metrics={}, ) model.summary() inputs = np.array([[ [0.2, 0.1, 0.3], [0.5, 0.1, 0.1], ]]) predict = model.predict(inputs) expected = np.asarray([[ [0.0, -1.22474487, 1.22474487], [1.41421356, -0.707106781, -0.707106781], ]]) self.assertTrue(np.allclose(expected, predict), predict) input_layer = keras.layers.Input( shape=(10, 256), name='Input', ) norm_layer = LayerNormalization( name='Layer-Normalization', beta_initializer='ones', )(input_layer) model = keras.models.Model( inputs=input_layer, outputs=norm_layer, ) model.compile( optimizer='adam', loss='mse', metrics={}, ) model.summary() inputs = np.zeros((2, 10, 256)) predict = model.predict(inputs) expected = np.ones((2, 10, 256)) self.assertTrue(np.allclose(expected, predict)) def test_fit(self): def _leaky_relu(x): return keras.activations.relu(x, alpha=0.01) input_layer = keras.layers.Input( shape=(2, 3), name='Input', ) norm_layer = LayerNormalization( name='Layer-Normalization-1', trainable=False, )(input_layer) att_layer = MultiHeadAttention( head_num=3, activation=_leaky_relu, name='Multi-Head-Attentions' )(norm_layer) dense_layer = keras.layers.Dense(units=3, name='Dense-1')(att_layer) norm_layer = LayerNormalization( name='Layer-Normalization-2', trainable=False, )(dense_layer) dense_layer = keras.layers.Dense(units=3, name='Dense-2')(norm_layer) model = keras.models.Model( inputs=input_layer, outputs=dense_layer, ) model.compile( optimizer=tf.keras.optimizers.Adam(lr=1e-3), loss='mse', metrics={}, ) model.summary() def _generator(batch_size=32): while True: batch_inputs = np.random.random((batch_size, 2, 3)) batch_outputs = np.asarray([[[0.0, -0.1, 0.2]] * 2] * batch_size) yield batch_inputs, batch_outputs model.fit_generator( generator=_generator(), steps_per_epoch=1000, epochs=10, validation_data=_generator(), validation_steps=100, callbacks=[ keras.callbacks.EarlyStopping(monitor='val_loss', patience=5) ], ) model_path = os.path.join(tempfile.gettempdir(), 'test_layer_normalization_%f.h5' % np.random.random()) model.save(model_path) model = keras.models.load_model(model_path, custom_objects={ '_leaky_relu': _leaky_relu, 'MultiHeadAttention': MultiHeadAttention, 'LayerNormalization': LayerNormalization, }) for inputs, _ in _generator(batch_size=3): predicts = model.predict(inputs) expect = np.round(np.asarray([[[0.0, -0.1, 0.2]] * 2] * 3), decimals=1) actual = np.round(predicts, decimals=1) self.assertTrue(np.allclose(expect, actual), (expect, actual)) break def test_fit_zeros(self): def _leaky_relu(x): return keras.activations.relu(x, alpha=0.01) input_layer = keras.layers.Input( shape=(2, 3), name='Input', ) norm_layer = LayerNormalization( name='Layer-Normalization-1', trainable=False, )(input_layer) att_layer = MultiHeadAttention( head_num=3, activation=_leaky_relu, name='Multi-Head-Attentions' )(norm_layer) dense_layer = keras.layers.Dense(units=3, name='Dense-1')(att_layer) norm_layer = LayerNormalization( name='Layer-Normalization-2', trainable=False, )(dense_layer) dense_layer = keras.layers.Dense(units=3, name='Dense-2')(norm_layer) model = keras.models.Model( inputs=input_layer, outputs=dense_layer, ) model.compile( optimizer=tf.keras.optimizers.Adam(lr=1e-3), loss='mse', metrics={}, ) model.summary() def _generator_zeros(batch_size=32): while True: batch_inputs = np.zeros((batch_size, 2, 3)) batch_outputs = np.asarray([[[0.0, -0.1, 0.2]] * 2] * batch_size) yield batch_inputs, batch_outputs model.fit_generator( generator=_generator_zeros(), steps_per_epoch=1000, epochs=10, validation_data=_generator_zeros(), validation_steps=100, callbacks=[ keras.callbacks.EarlyStopping(monitor='val_loss', patience=5) ], ) for inputs, _ in _generator_zeros(batch_size=3): predicts = model.predict(inputs) expect = np.round(np.asarray([[[0.0, -0.1, 0.2]] * 2] * 3), decimals=1) actual = np.round(predicts, decimals=1) self.assertTrue(np.allclose(expect, actual), (expect, actual)) break def test_save_load_json(self): model = keras.models.Sequential() model.add(LayerNormalization(input_shape=(2, 3))) model.compile(optimizer='adam', loss='mse') encoded = model.to_json() model = keras.models.model_from_json(encoded, custom_objects={'LayerNormalization': LayerNormalization}) model.summary() ", https://github.com/CyberZHG/keras-transformer,52a3fa27d5598eca23be9031fc67a011d42cbefb,tests/test_get_model.py,tests/test_get_model.py,ID,flaky,Accepted,https://github.com/CyberZHG/keras-transformer/pull/42,classify;root_cause;fix_proposal,"import os import tempfile import unittest import numpy as np import tensorflow as tf from keras_transformer.backend import keras from keras_transformer import get_custom_objects, get_model class TestGetModel(unittest.TestCase): def test_get_same(self): model = get_model( token_num=13, embed_dim=30, encoder_num=3, decoder_num=2, head_num=3, hidden_dim=120, attention_activation=None, feed_forward_activation='relu', dropout_rate=0.05, use_same_embed=True, embed_weights=np.random.random((13, 30)), trainable=False, ) model.compile( optimizer=tf.keras.optimizers.Adam(), loss=keras.losses.categorical_crossentropy, metrics={}, ) model_path = os.path.join(tempfile.gettempdir(), 'test_transformer_%f.h5' % np.random.random()) model.save(model_path) model = keras.models.load_model(model_path, custom_objects=get_custom_objects()) model.summary() try: keras.utils.plot_model(model, 'transformer_same.png') except Exception as e: print(e) self.assertIsNotNone(model) def test_get_diff(self): model = get_model( token_num=[13, 14], embed_dim=30, encoder_num=3, decoder_num=2, head_num=3, hidden_dim=120, attention_activation=None, feed_forward_activation='relu', dropout_rate=0.05, use_same_embed=False, ) model.compile( optimizer=tf.keras.optimizers.Adam(), loss=keras.losses.categorical_crossentropy, metrics={}, ) model_path = os.path.join(tempfile.gettempdir(), 'test_transformer_%f.h5' % np.random.random()) model.save(model_path) model = keras.models.load_model(model_path, custom_objects=get_custom_objects()) model.summary() try: keras.utils.plot_model(model, 'transformer_diff.png') except Exception as e: print(e) self.assertIsNotNone(model) ", https://github.com/d4nuu8/krllint,2f9376cdae14c201364d9c31b4c19a8ff2f708d2,tests/test_extraneous_whitespace.py::ExtraneousWhiteSpaceTestCase::test_rule_without_fix,tests/test_extraneous_whitespace.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- from unittest import TestCase from importlib import reload from krllint import config from krllint.reporter import Category, MemoryReporter from krllint.linter import _create_arg_parser, Linter class ExtraneousWhiteSpaceTestCase(TestCase): TEST_INPUT = [""foo bar\n""] FIXED_INPUT = [""foo bar\n""] def test_rule_without_fix(self): cli_args = _create_arg_parser().parse_args([""test_rule_without_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, reporter = linter.lint_lines(""test_rule_without_fix"", self.TEST_INPUT) self.assertEqual(reporter.found_issues[Category.CONVENTION], 1) self.assertEqual(reporter.found_issues[Category.REFACTOR], 0) self.assertEqual(reporter.found_issues[Category.WARNING], 0) self.assertEqual(reporter.found_issues[Category.ERROR], 0) self.assertEqual(reporter.found_issues[Category.FATAL], 0) self.assertEqual(lines, self.TEST_INPUT) self.assertEqual(reporter.messages[0].line_number, 0) self.assertEqual(reporter.messages[0].column, 3) self.assertEqual(reporter.messages[0].message, ""superfluous whitespace"") self.assertEqual(reporter.messages[0].code, ""superfluous-whitespace"") def test_rule_with_fix(self): cli_args = _create_arg_parser().parse_args([""--fix"", ""test_rule_with_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, _ = linter.lint_lines(""test_rule_with_fix"", self.TEST_INPUT) self.assertEqual(lines, self.FIXED_INPUT) ", https://github.com/d4nuu8/krllint,2f9376cdae14c201364d9c31b4c19a8ff2f708d2,tests/test_extraneous_whitespace.py::ExtraneousWhiteSpaceTestCase::test_rule_with_fix,tests/test_extraneous_whitespace.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- from unittest import TestCase from importlib import reload from krllint import config from krllint.reporter import Category, MemoryReporter from krllint.linter import _create_arg_parser, Linter class ExtraneousWhiteSpaceTestCase(TestCase): TEST_INPUT = [""foo bar\n""] FIXED_INPUT = [""foo bar\n""] def test_rule_without_fix(self): cli_args = _create_arg_parser().parse_args([""test_rule_without_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, reporter = linter.lint_lines(""test_rule_without_fix"", self.TEST_INPUT) self.assertEqual(reporter.found_issues[Category.CONVENTION], 1) self.assertEqual(reporter.found_issues[Category.REFACTOR], 0) self.assertEqual(reporter.found_issues[Category.WARNING], 0) self.assertEqual(reporter.found_issues[Category.ERROR], 0) self.assertEqual(reporter.found_issues[Category.FATAL], 0) self.assertEqual(lines, self.TEST_INPUT) self.assertEqual(reporter.messages[0].line_number, 0) self.assertEqual(reporter.messages[0].column, 3) self.assertEqual(reporter.messages[0].message, ""superfluous whitespace"") self.assertEqual(reporter.messages[0].code, ""superfluous-whitespace"") def test_rule_with_fix(self): cli_args = _create_arg_parser().parse_args([""--fix"", ""test_rule_with_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, _ = linter.lint_lines(""test_rule_with_fix"", self.TEST_INPUT) self.assertEqual(lines, self.FIXED_INPUT) ", https://github.com/d4nuu8/krllint,2f9376cdae14c201364d9c31b4c19a8ff2f708d2,tests/test_indentation_checker.py::IndentationCheckerTestCase::test_rule_with_fix,tests/test_indentation_checker.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- from unittest import TestCase from importlib import reload from krllint import config from krllint.reporter import Category, MemoryReporter from krllint.linter import _create_arg_parser, Linter class IndentationCheckerTestCase(TestCase): TEST_INPUT = [ ""IF foo THEN\n"", "" bar\n"", ""ENDIF\n"", ""\n"", "" ;FOLD PTP;%{PE}\n"", "" ;ENDFOLD\n"" ] FIXED_INPUT = [ ""IF foo THEN\n"", "" bar\n"", ""ENDIF\n"", ""\n"", "";FOLD PTP;%{PE}\n"", "";ENDFOLD\n"" ] def test_rule_without_fix(self): cli_args = _create_arg_parser().parse_args([""test_rule_without_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, reporter = linter.lint_lines(""test_rule_without_fix"", self.TEST_INPUT) self.assertEqual(reporter.found_issues[Category.CONVENTION], 0) self.assertEqual(reporter.found_issues[Category.REFACTOR], 0) self.assertEqual(reporter.found_issues[Category.WARNING], 3) self.assertEqual(reporter.found_issues[Category.ERROR], 0) self.assertEqual(reporter.found_issues[Category.FATAL], 0) self.assertEqual(lines, self.TEST_INPUT) self.assertEqual(reporter.messages[0].line_number, 1) self.assertEqual(reporter.messages[0].column, 6) self.assertEqual(reporter.messages[0].message, ""wrong indentation (found 6 spaces, exptected 3)"") self.assertEqual(reporter.messages[0].code, ""bad-indentation"") self.assertEqual(reporter.messages[1].line_number, 4) self.assertEqual(reporter.messages[1].column, 3) self.assertEqual(reporter.messages[1].message, ""wrong indentation (found 3 spaces, exptected 0)"") self.assertEqual(reporter.messages[1].code, ""bad-indented-inline-form"") self.assertEqual(reporter.messages[2].line_number, 5) self.assertEqual(reporter.messages[2].column, 3) self.assertEqual(reporter.messages[2].message, ""wrong indentation (found 3 spaces, exptected 0)"") self.assertEqual(reporter.messages[2].code, ""bad-indented-inline-form"") def test_rule_with_fix(self): cli_args = _create_arg_parser().parse_args([""--fix"", ""test_rule_with_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, _ = linter.lint_lines(""test_rule_with_fix"", self.TEST_INPUT) self.assertEqual(lines, self.FIXED_INPUT) ", https://github.com/d4nuu8/krllint,2f9376cdae14c201364d9c31b4c19a8ff2f708d2,tests/test_lower_or_mixed_case_built_in_type.py::LowerOrMixedCaseBuiltInTestCase::test_rule_without_fix,tests/test_lower_or_mixed_case_built_in_type.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- from unittest import TestCase from importlib import reload from krllint import config from krllint.reporter import Category, MemoryReporter from krllint.linter import _create_arg_parser, Linter class LowerOrMixedCaseBuiltInTestCase(TestCase): TEST_INPUT = [""Int\n""] FIXED_INPUT = [""INT\n""] def test_rule_without_fix(self): cli_args = _create_arg_parser().parse_args([""test_rule_without_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, reporter = linter.lint_lines(""test_rule_without_fix"", self.TEST_INPUT) self.assertEqual(reporter.found_issues[Category.CONVENTION], 0) self.assertEqual(reporter.found_issues[Category.REFACTOR], 0) self.assertEqual(reporter.found_issues[Category.WARNING], 1) self.assertEqual(reporter.found_issues[Category.ERROR], 0) self.assertEqual(reporter.found_issues[Category.FATAL], 0) self.assertEqual(lines, self.TEST_INPUT) self.assertEqual(reporter.messages[0].line_number, 0) self.assertEqual(reporter.messages[0].column, 0) self.assertEqual(reporter.messages[0].message, ""lower or mixed case built-in type"") self.assertEqual(reporter.messages[0].code, ""wrong-case-type"") def test_rule_with_fix(self): cli_args = _create_arg_parser().parse_args([""--fix"", ""test_rule_with_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, _ = linter.lint_lines(""test_rule_with_fix"", self.TEST_INPUT) self.assertEqual(lines, self.FIXED_INPUT) ", https://github.com/d4nuu8/krllint,2f9376cdae14c201364d9c31b4c19a8ff2f708d2,tests/test_lower_or_mixed_case_built_in_type.py::LowerOrMixedCaseBuiltInTestCase::test_rule_with_fix,tests/test_lower_or_mixed_case_built_in_type.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- from unittest import TestCase from importlib import reload from krllint import config from krllint.reporter import Category, MemoryReporter from krllint.linter import _create_arg_parser, Linter class LowerOrMixedCaseBuiltInTestCase(TestCase): TEST_INPUT = [""Int\n""] FIXED_INPUT = [""INT\n""] def test_rule_without_fix(self): cli_args = _create_arg_parser().parse_args([""test_rule_without_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, reporter = linter.lint_lines(""test_rule_without_fix"", self.TEST_INPUT) self.assertEqual(reporter.found_issues[Category.CONVENTION], 0) self.assertEqual(reporter.found_issues[Category.REFACTOR], 0) self.assertEqual(reporter.found_issues[Category.WARNING], 1) self.assertEqual(reporter.found_issues[Category.ERROR], 0) self.assertEqual(reporter.found_issues[Category.FATAL], 0) self.assertEqual(lines, self.TEST_INPUT) self.assertEqual(reporter.messages[0].line_number, 0) self.assertEqual(reporter.messages[0].column, 0) self.assertEqual(reporter.messages[0].message, ""lower or mixed case built-in type"") self.assertEqual(reporter.messages[0].code, ""wrong-case-type"") def test_rule_with_fix(self): cli_args = _create_arg_parser().parse_args([""--fix"", ""test_rule_with_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, _ = linter.lint_lines(""test_rule_with_fix"", self.TEST_INPUT) self.assertEqual(lines, self.FIXED_INPUT) ", https://github.com/d4nuu8/krllint,2f9376cdae14c201364d9c31b4c19a8ff2f708d2,tests/test_open_task.py::OpenTaskTestCase::test_rule_without_fix,tests/test_open_task.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- from unittest import TestCase from importlib import reload from krllint import config from krllint.reporter import Category, MemoryReporter from krllint.linter import _create_arg_parser, Linter class OpenTaskTestCase(TestCase): TEST_INPUT = ["";TODO This is a open task\n""] def test_rule_without_fix(self): cli_args = _create_arg_parser().parse_args([""test_rule_without_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, reporter = linter.lint_lines(""test_rule_without_fix"", self.TEST_INPUT) self.assertEqual(reporter.found_issues[Category.CONVENTION], 0) self.assertEqual(reporter.found_issues[Category.REFACTOR], 0) self.assertEqual(reporter.found_issues[Category.WARNING], 1) self.assertEqual(reporter.found_issues[Category.ERROR], 0) self.assertEqual(reporter.found_issues[Category.FATAL], 0) self.assertEqual(lines, self.TEST_INPUT) self.assertEqual(reporter.messages[0].line_number, 0) self.assertEqual(reporter.messages[0].column, 0) self.assertEqual(reporter.messages[0].message, ""complete open task (This is a open task)"") self.assertEqual(reporter.messages[0].code, ""open-task"") def test_rule_with_fix(self): cli_args = _create_arg_parser().parse_args([""--fix"", ""test_rule_with_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, _ = linter.lint_lines(""test_rule_with_fix"", self.TEST_INPUT) self.assertEqual(lines, self.TEST_INPUT)", https://github.com/d4nuu8/krllint,2f9376cdae14c201364d9c31b4c19a8ff2f708d2,tests/test_open_task.py::OpenTaskTestCase::test_rule_with_fix,tests/test_open_task.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- from unittest import TestCase from importlib import reload from krllint import config from krllint.reporter import Category, MemoryReporter from krllint.linter import _create_arg_parser, Linter class OpenTaskTestCase(TestCase): TEST_INPUT = ["";TODO This is a open task\n""] def test_rule_without_fix(self): cli_args = _create_arg_parser().parse_args([""test_rule_without_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, reporter = linter.lint_lines(""test_rule_without_fix"", self.TEST_INPUT) self.assertEqual(reporter.found_issues[Category.CONVENTION], 0) self.assertEqual(reporter.found_issues[Category.REFACTOR], 0) self.assertEqual(reporter.found_issues[Category.WARNING], 1) self.assertEqual(reporter.found_issues[Category.ERROR], 0) self.assertEqual(reporter.found_issues[Category.FATAL], 0) self.assertEqual(lines, self.TEST_INPUT) self.assertEqual(reporter.messages[0].line_number, 0) self.assertEqual(reporter.messages[0].column, 0) self.assertEqual(reporter.messages[0].message, ""complete open task (This is a open task)"") self.assertEqual(reporter.messages[0].code, ""open-task"") def test_rule_with_fix(self): cli_args = _create_arg_parser().parse_args([""--fix"", ""test_rule_with_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, _ = linter.lint_lines(""test_rule_with_fix"", self.TEST_INPUT) self.assertEqual(lines, self.TEST_INPUT)", https://github.com/d4nuu8/krllint,2f9376cdae14c201364d9c31b4c19a8ff2f708d2,tests/test_trailing_white_space_rule.py::TrailingWhiteSpaceTestCase::test_rule_without_fix,tests/test_trailing_white_space_rule.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- from unittest import TestCase from importlib import reload from krllint import config from krllint.reporter import Category, MemoryReporter from krllint.linter import _create_arg_parser, Linter class TrailingWhiteSpaceTestCase(TestCase): TEST_INPUT = [""INT someVariable \n""] FIXED_INPUT = [""INT someVariable\n""] def test_rule_without_fix(self): cli_args = _create_arg_parser().parse_args([""test_rule_without_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, reporter = linter.lint_lines(""test_rule_without_fix"", self.TEST_INPUT) self.assertEqual(reporter.found_issues[Category.CONVENTION], 1) self.assertEqual(reporter.found_issues[Category.REFACTOR], 0) self.assertEqual(reporter.found_issues[Category.WARNING], 0) self.assertEqual(reporter.found_issues[Category.ERROR], 0) self.assertEqual(reporter.found_issues[Category.FATAL], 0) self.assertEqual(lines, self.TEST_INPUT) self.assertEqual(reporter.messages[0].line_number, 0) self.assertEqual(reporter.messages[0].column, 16) self.assertEqual(reporter.messages[0].message, ""trailing whitespace"") self.assertEqual(reporter.messages[0].code, ""trailing-whitespace"") def test_rule_with_fix(self): cli_args = _create_arg_parser().parse_args([""--fix"", ""test_rule_with_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, _ = linter.lint_lines(""test_rule_with_fix"", self.TEST_INPUT) self.assertEqual(lines, self.FIXED_INPUT) ", https://github.com/d4nuu8/krllint,2f9376cdae14c201364d9c31b4c19a8ff2f708d2,tests/test_trailing_white_space_rule.py::TrailingWhiteSpaceTestCase::test_rule_with_fix,tests/test_trailing_white_space_rule.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- from unittest import TestCase from importlib import reload from krllint import config from krllint.reporter import Category, MemoryReporter from krllint.linter import _create_arg_parser, Linter class TrailingWhiteSpaceTestCase(TestCase): TEST_INPUT = [""INT someVariable \n""] FIXED_INPUT = [""INT someVariable\n""] def test_rule_without_fix(self): cli_args = _create_arg_parser().parse_args([""test_rule_without_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, reporter = linter.lint_lines(""test_rule_without_fix"", self.TEST_INPUT) self.assertEqual(reporter.found_issues[Category.CONVENTION], 1) self.assertEqual(reporter.found_issues[Category.REFACTOR], 0) self.assertEqual(reporter.found_issues[Category.WARNING], 0) self.assertEqual(reporter.found_issues[Category.ERROR], 0) self.assertEqual(reporter.found_issues[Category.FATAL], 0) self.assertEqual(lines, self.TEST_INPUT) self.assertEqual(reporter.messages[0].line_number, 0) self.assertEqual(reporter.messages[0].column, 16) self.assertEqual(reporter.messages[0].message, ""trailing whitespace"") self.assertEqual(reporter.messages[0].code, ""trailing-whitespace"") def test_rule_with_fix(self): cli_args = _create_arg_parser().parse_args([""--fix"", ""test_rule_with_fix""]) reload(config) config.REPORTER = MemoryReporter linter = Linter(cli_args, config) lines, _ = linter.lint_lines(""test_rule_with_fix"", self.TEST_INPUT) self.assertEqual(lines, self.FIXED_INPUT) ", https://github.com/daknuett/ljson,1d3dc13001d9d2f61bcfbf9d5c5b16d44fb8076e,test/test_ljson_disk.py::test_contains,test/test_ljson_disk.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import ljson.base.mem import ljson.base.disk import ljson.base.generic import copy, os, pytest from .data import data, header_descriptor, item_meg def test_read(): from io import StringIO, BytesIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) assert list(table_in) == data import pytest with pytest.raises(KeyError): assert table_in[{""foo"": ""bar""}] assert table_in[{""lname"": ""griffin""}][""name""] == [d[""name""] for d in data] fio.seek(0) fio2 = BytesIO(fio.read().encode(""utf-8"")) table_in = ljson.base.disk.Table.from_file(fio2) assert list(table_in) == data def test_edit(tmpdir): import copy data_ = copy.deepcopy(data) filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data_) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table[{""lname"": ""griffin""}][""lname""] = ""Griffin"" data_ = [] for row in data: row_ = copy.copy(row) row_[""lname""] = ""Griffin"" data_.append(row_) assert list(table) == data_ table.additem(item_meg) assert list(table) == data_ + [item_meg] fio = table.file fio.seek(0) table_in = ljson.base.mem.Table.from_file(fio) table._first_next_call = True assert list(table) == list(table_in) assert table[{""lname"": ""griffin""}].getone(""name"") == ""meg"" assert table[{""lname"": ""griffin""}].getone() == item_meg fio.seek(0) print(fio.readline()) # get rid of the header content = fio.read() print(content) fio.seek(0) fio.truncate(0) fio.write(content) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) assert list(table) == list(table_in) import pytest with pytest.raises(RuntimeError): for item in table: table[{""name"": item[""name""]}][""name""] = item[""name""].upper() for d in data_: d[""name""] = d[""name""].upper() assert list(table) == data_ table.close() def test_edit2(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table[{""name"": ""peter""}][""name""] += ""Pan"" print(list(table)) assert table[{""name"": ""peterPan""}].getone(""name"") == ""peterPan"" def test_contexts(): from io import StringIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) with ljson.base.disk.Table.from_file(fio) as table: assert list(table) == data def test_zipping(): from io import StringIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) for row_data, row_mem, row_disk in zip(data, table, table_in): assert row_data == row_mem assert row_data == row_disk def test_selection_iter(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) for row in table_in[{""lname"": ""griffin""}]: assert row[""lname""] == ""griffin"" table_in[{""name"": ""chris""}][""lname""] = ""peters"" for row in table_in[{""lname"": ""griffin""}]: assert row[""lname""] == ""griffin"" for row in table_in[{""lname"": ""peters""}]: assert row[""name""] == ""chris"" def test_save(tmpdir): from io import StringIO filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) assert list(table_in) == data f = StringIO() table_in.save(f) f.seek(0) table = ljson.base.disk.Table.from_file(f) assert list(table) == data def test_open(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) f = open(filename, ""w+"") table.save(f) f.close() with ljson.base.mem.Table.open(filename) as table: assert list(table) == data with ljson.base.disk.Table.open(filename) as table: assert list(table) == data import pytest with pytest.raises(IOError): ljson.base.disk.Table.open(filename + ""**~~"") def test_unique_check(): import copy from io import StringIO header_descriptor_ = copy.copy(header_descriptor) header_descriptor_[""name""][""modifiers""] = [""unique""] header = ljson.base.generic.Header(header_descriptor_) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table.additem(item_meg) import pytest with pytest.raises(ValueError): table.additem({""age"": 12, ""name"": ""chris"", ""lname"": ""griffin""}) def test_contains(): header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) from io import StringIO fio = StringIO() table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) assert {""lname"": ""griffin""} in table assert not {""lname"": ""griffindor""} in table def test_delete(tmpdir): import copy header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, copy.copy(data)) import os filename = os.path.join(str(tmpdir), ""table.ljson"") fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) data_ = copy.copy(data) del(data_[0]) del(table[{""name"": ""peter""}]) assert list(table) == data_ table.additem(item_meg) del(table[{""name"": ""meg""}]) assert list(table) == data_ @pytest.mark.slow def test_speedup_delete(tmpdir, benchmark): benchmark(test_delete, tmpdir) @pytest.mark.slow def test_speedup_edit(tmpdir, benchmark): benchmark(test_edit, tmpdir) @pytest.mark.slow def test_speedup_contains(tmpdir, benchmark): benchmark(test_contains) @pytest.mark.slow def test_speedup_selection_iter(tmpdir, benchmark): benchmark(test_selection_iter, tmpdir) ", https://github.com/daknuett/ljson,1d3dc13001d9d2f61bcfbf9d5c5b16d44fb8076e,test/test_ljson_disk.py::test_delete,test/test_ljson_disk.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import ljson.base.mem import ljson.base.disk import ljson.base.generic import copy, os, pytest from .data import data, header_descriptor, item_meg def test_read(): from io import StringIO, BytesIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) assert list(table_in) == data import pytest with pytest.raises(KeyError): assert table_in[{""foo"": ""bar""}] assert table_in[{""lname"": ""griffin""}][""name""] == [d[""name""] for d in data] fio.seek(0) fio2 = BytesIO(fio.read().encode(""utf-8"")) table_in = ljson.base.disk.Table.from_file(fio2) assert list(table_in) == data def test_edit(tmpdir): import copy data_ = copy.deepcopy(data) filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data_) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table[{""lname"": ""griffin""}][""lname""] = ""Griffin"" data_ = [] for row in data: row_ = copy.copy(row) row_[""lname""] = ""Griffin"" data_.append(row_) assert list(table) == data_ table.additem(item_meg) assert list(table) == data_ + [item_meg] fio = table.file fio.seek(0) table_in = ljson.base.mem.Table.from_file(fio) table._first_next_call = True assert list(table) == list(table_in) assert table[{""lname"": ""griffin""}].getone(""name"") == ""meg"" assert table[{""lname"": ""griffin""}].getone() == item_meg fio.seek(0) print(fio.readline()) # get rid of the header content = fio.read() print(content) fio.seek(0) fio.truncate(0) fio.write(content) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) assert list(table) == list(table_in) import pytest with pytest.raises(RuntimeError): for item in table: table[{""name"": item[""name""]}][""name""] = item[""name""].upper() for d in data_: d[""name""] = d[""name""].upper() assert list(table) == data_ table.close() def test_edit2(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table[{""name"": ""peter""}][""name""] += ""Pan"" print(list(table)) assert table[{""name"": ""peterPan""}].getone(""name"") == ""peterPan"" def test_contexts(): from io import StringIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) with ljson.base.disk.Table.from_file(fio) as table: assert list(table) == data def test_zipping(): from io import StringIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) for row_data, row_mem, row_disk in zip(data, table, table_in): assert row_data == row_mem assert row_data == row_disk def test_selection_iter(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) for row in table_in[{""lname"": ""griffin""}]: assert row[""lname""] == ""griffin"" table_in[{""name"": ""chris""}][""lname""] = ""peters"" for row in table_in[{""lname"": ""griffin""}]: assert row[""lname""] == ""griffin"" for row in table_in[{""lname"": ""peters""}]: assert row[""name""] == ""chris"" def test_save(tmpdir): from io import StringIO filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) assert list(table_in) == data f = StringIO() table_in.save(f) f.seek(0) table = ljson.base.disk.Table.from_file(f) assert list(table) == data def test_open(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) f = open(filename, ""w+"") table.save(f) f.close() with ljson.base.mem.Table.open(filename) as table: assert list(table) == data with ljson.base.disk.Table.open(filename) as table: assert list(table) == data import pytest with pytest.raises(IOError): ljson.base.disk.Table.open(filename + ""**~~"") def test_unique_check(): import copy from io import StringIO header_descriptor_ = copy.copy(header_descriptor) header_descriptor_[""name""][""modifiers""] = [""unique""] header = ljson.base.generic.Header(header_descriptor_) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table.additem(item_meg) import pytest with pytest.raises(ValueError): table.additem({""age"": 12, ""name"": ""chris"", ""lname"": ""griffin""}) def test_contains(): header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) from io import StringIO fio = StringIO() table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) assert {""lname"": ""griffin""} in table assert not {""lname"": ""griffindor""} in table def test_delete(tmpdir): import copy header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, copy.copy(data)) import os filename = os.path.join(str(tmpdir), ""table.ljson"") fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) data_ = copy.copy(data) del(data_[0]) del(table[{""name"": ""peter""}]) assert list(table) == data_ table.additem(item_meg) del(table[{""name"": ""meg""}]) assert list(table) == data_ @pytest.mark.slow def test_speedup_delete(tmpdir, benchmark): benchmark(test_delete, tmpdir) @pytest.mark.slow def test_speedup_edit(tmpdir, benchmark): benchmark(test_edit, tmpdir) @pytest.mark.slow def test_speedup_contains(tmpdir, benchmark): benchmark(test_contains) @pytest.mark.slow def test_speedup_selection_iter(tmpdir, benchmark): benchmark(test_selection_iter, tmpdir) ", https://github.com/daknuett/ljson,1d3dc13001d9d2f61bcfbf9d5c5b16d44fb8076e,test/test_ljson_disk.py::test_edit,test/test_ljson_disk.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import ljson.base.mem import ljson.base.disk import ljson.base.generic import copy, os, pytest from .data import data, header_descriptor, item_meg def test_read(): from io import StringIO, BytesIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) assert list(table_in) == data import pytest with pytest.raises(KeyError): assert table_in[{""foo"": ""bar""}] assert table_in[{""lname"": ""griffin""}][""name""] == [d[""name""] for d in data] fio.seek(0) fio2 = BytesIO(fio.read().encode(""utf-8"")) table_in = ljson.base.disk.Table.from_file(fio2) assert list(table_in) == data def test_edit(tmpdir): import copy data_ = copy.deepcopy(data) filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data_) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table[{""lname"": ""griffin""}][""lname""] = ""Griffin"" data_ = [] for row in data: row_ = copy.copy(row) row_[""lname""] = ""Griffin"" data_.append(row_) assert list(table) == data_ table.additem(item_meg) assert list(table) == data_ + [item_meg] fio = table.file fio.seek(0) table_in = ljson.base.mem.Table.from_file(fio) table._first_next_call = True assert list(table) == list(table_in) assert table[{""lname"": ""griffin""}].getone(""name"") == ""meg"" assert table[{""lname"": ""griffin""}].getone() == item_meg fio.seek(0) print(fio.readline()) # get rid of the header content = fio.read() print(content) fio.seek(0) fio.truncate(0) fio.write(content) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) assert list(table) == list(table_in) import pytest with pytest.raises(RuntimeError): for item in table: table[{""name"": item[""name""]}][""name""] = item[""name""].upper() for d in data_: d[""name""] = d[""name""].upper() assert list(table) == data_ table.close() def test_edit2(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table[{""name"": ""peter""}][""name""] += ""Pan"" print(list(table)) assert table[{""name"": ""peterPan""}].getone(""name"") == ""peterPan"" def test_contexts(): from io import StringIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) with ljson.base.disk.Table.from_file(fio) as table: assert list(table) == data def test_zipping(): from io import StringIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) for row_data, row_mem, row_disk in zip(data, table, table_in): assert row_data == row_mem assert row_data == row_disk def test_selection_iter(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) for row in table_in[{""lname"": ""griffin""}]: assert row[""lname""] == ""griffin"" table_in[{""name"": ""chris""}][""lname""] = ""peters"" for row in table_in[{""lname"": ""griffin""}]: assert row[""lname""] == ""griffin"" for row in table_in[{""lname"": ""peters""}]: assert row[""name""] == ""chris"" def test_save(tmpdir): from io import StringIO filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) assert list(table_in) == data f = StringIO() table_in.save(f) f.seek(0) table = ljson.base.disk.Table.from_file(f) assert list(table) == data def test_open(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) f = open(filename, ""w+"") table.save(f) f.close() with ljson.base.mem.Table.open(filename) as table: assert list(table) == data with ljson.base.disk.Table.open(filename) as table: assert list(table) == data import pytest with pytest.raises(IOError): ljson.base.disk.Table.open(filename + ""**~~"") def test_unique_check(): import copy from io import StringIO header_descriptor_ = copy.copy(header_descriptor) header_descriptor_[""name""][""modifiers""] = [""unique""] header = ljson.base.generic.Header(header_descriptor_) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table.additem(item_meg) import pytest with pytest.raises(ValueError): table.additem({""age"": 12, ""name"": ""chris"", ""lname"": ""griffin""}) def test_contains(): header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) from io import StringIO fio = StringIO() table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) assert {""lname"": ""griffin""} in table assert not {""lname"": ""griffindor""} in table def test_delete(tmpdir): import copy header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, copy.copy(data)) import os filename = os.path.join(str(tmpdir), ""table.ljson"") fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) data_ = copy.copy(data) del(data_[0]) del(table[{""name"": ""peter""}]) assert list(table) == data_ table.additem(item_meg) del(table[{""name"": ""meg""}]) assert list(table) == data_ @pytest.mark.slow def test_speedup_delete(tmpdir, benchmark): benchmark(test_delete, tmpdir) @pytest.mark.slow def test_speedup_edit(tmpdir, benchmark): benchmark(test_edit, tmpdir) @pytest.mark.slow def test_speedup_contains(tmpdir, benchmark): benchmark(test_contains) @pytest.mark.slow def test_speedup_selection_iter(tmpdir, benchmark): benchmark(test_selection_iter, tmpdir) ", https://github.com/daknuett/ljson,1d3dc13001d9d2f61bcfbf9d5c5b16d44fb8076e,test/test_ljson_disk.py::test_read,test/test_ljson_disk.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import ljson.base.mem import ljson.base.disk import ljson.base.generic import copy, os, pytest from .data import data, header_descriptor, item_meg def test_read(): from io import StringIO, BytesIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) assert list(table_in) == data import pytest with pytest.raises(KeyError): assert table_in[{""foo"": ""bar""}] assert table_in[{""lname"": ""griffin""}][""name""] == [d[""name""] for d in data] fio.seek(0) fio2 = BytesIO(fio.read().encode(""utf-8"")) table_in = ljson.base.disk.Table.from_file(fio2) assert list(table_in) == data def test_edit(tmpdir): import copy data_ = copy.deepcopy(data) filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data_) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table[{""lname"": ""griffin""}][""lname""] = ""Griffin"" data_ = [] for row in data: row_ = copy.copy(row) row_[""lname""] = ""Griffin"" data_.append(row_) assert list(table) == data_ table.additem(item_meg) assert list(table) == data_ + [item_meg] fio = table.file fio.seek(0) table_in = ljson.base.mem.Table.from_file(fio) table._first_next_call = True assert list(table) == list(table_in) assert table[{""lname"": ""griffin""}].getone(""name"") == ""meg"" assert table[{""lname"": ""griffin""}].getone() == item_meg fio.seek(0) print(fio.readline()) # get rid of the header content = fio.read() print(content) fio.seek(0) fio.truncate(0) fio.write(content) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) assert list(table) == list(table_in) import pytest with pytest.raises(RuntimeError): for item in table: table[{""name"": item[""name""]}][""name""] = item[""name""].upper() for d in data_: d[""name""] = d[""name""].upper() assert list(table) == data_ table.close() def test_edit2(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table[{""name"": ""peter""}][""name""] += ""Pan"" print(list(table)) assert table[{""name"": ""peterPan""}].getone(""name"") == ""peterPan"" def test_contexts(): from io import StringIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) with ljson.base.disk.Table.from_file(fio) as table: assert list(table) == data def test_zipping(): from io import StringIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) for row_data, row_mem, row_disk in zip(data, table, table_in): assert row_data == row_mem assert row_data == row_disk def test_selection_iter(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) for row in table_in[{""lname"": ""griffin""}]: assert row[""lname""] == ""griffin"" table_in[{""name"": ""chris""}][""lname""] = ""peters"" for row in table_in[{""lname"": ""griffin""}]: assert row[""lname""] == ""griffin"" for row in table_in[{""lname"": ""peters""}]: assert row[""name""] == ""chris"" def test_save(tmpdir): from io import StringIO filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) assert list(table_in) == data f = StringIO() table_in.save(f) f.seek(0) table = ljson.base.disk.Table.from_file(f) assert list(table) == data def test_open(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) f = open(filename, ""w+"") table.save(f) f.close() with ljson.base.mem.Table.open(filename) as table: assert list(table) == data with ljson.base.disk.Table.open(filename) as table: assert list(table) == data import pytest with pytest.raises(IOError): ljson.base.disk.Table.open(filename + ""**~~"") def test_unique_check(): import copy from io import StringIO header_descriptor_ = copy.copy(header_descriptor) header_descriptor_[""name""][""modifiers""] = [""unique""] header = ljson.base.generic.Header(header_descriptor_) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table.additem(item_meg) import pytest with pytest.raises(ValueError): table.additem({""age"": 12, ""name"": ""chris"", ""lname"": ""griffin""}) def test_contains(): header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) from io import StringIO fio = StringIO() table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) assert {""lname"": ""griffin""} in table assert not {""lname"": ""griffindor""} in table def test_delete(tmpdir): import copy header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, copy.copy(data)) import os filename = os.path.join(str(tmpdir), ""table.ljson"") fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) data_ = copy.copy(data) del(data_[0]) del(table[{""name"": ""peter""}]) assert list(table) == data_ table.additem(item_meg) del(table[{""name"": ""meg""}]) assert list(table) == data_ @pytest.mark.slow def test_speedup_delete(tmpdir, benchmark): benchmark(test_delete, tmpdir) @pytest.mark.slow def test_speedup_edit(tmpdir, benchmark): benchmark(test_edit, tmpdir) @pytest.mark.slow def test_speedup_contains(tmpdir, benchmark): benchmark(test_contains) @pytest.mark.slow def test_speedup_selection_iter(tmpdir, benchmark): benchmark(test_selection_iter, tmpdir) ", https://github.com/daknuett/ljson,1d3dc13001d9d2f61bcfbf9d5c5b16d44fb8076e,test/test_ljson_disk.py::test_unique_check,test/test_ljson_disk.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import ljson.base.mem import ljson.base.disk import ljson.base.generic import copy, os, pytest from .data import data, header_descriptor, item_meg def test_read(): from io import StringIO, BytesIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) assert list(table_in) == data import pytest with pytest.raises(KeyError): assert table_in[{""foo"": ""bar""}] assert table_in[{""lname"": ""griffin""}][""name""] == [d[""name""] for d in data] fio.seek(0) fio2 = BytesIO(fio.read().encode(""utf-8"")) table_in = ljson.base.disk.Table.from_file(fio2) assert list(table_in) == data def test_edit(tmpdir): import copy data_ = copy.deepcopy(data) filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data_) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table[{""lname"": ""griffin""}][""lname""] = ""Griffin"" data_ = [] for row in data: row_ = copy.copy(row) row_[""lname""] = ""Griffin"" data_.append(row_) assert list(table) == data_ table.additem(item_meg) assert list(table) == data_ + [item_meg] fio = table.file fio.seek(0) table_in = ljson.base.mem.Table.from_file(fio) table._first_next_call = True assert list(table) == list(table_in) assert table[{""lname"": ""griffin""}].getone(""name"") == ""meg"" assert table[{""lname"": ""griffin""}].getone() == item_meg fio.seek(0) print(fio.readline()) # get rid of the header content = fio.read() print(content) fio.seek(0) fio.truncate(0) fio.write(content) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) assert list(table) == list(table_in) import pytest with pytest.raises(RuntimeError): for item in table: table[{""name"": item[""name""]}][""name""] = item[""name""].upper() for d in data_: d[""name""] = d[""name""].upper() assert list(table) == data_ table.close() def test_edit2(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table[{""name"": ""peter""}][""name""] += ""Pan"" print(list(table)) assert table[{""name"": ""peterPan""}].getone(""name"") == ""peterPan"" def test_contexts(): from io import StringIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) with ljson.base.disk.Table.from_file(fio) as table: assert list(table) == data def test_zipping(): from io import StringIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) for row_data, row_mem, row_disk in zip(data, table, table_in): assert row_data == row_mem assert row_data == row_disk def test_selection_iter(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) for row in table_in[{""lname"": ""griffin""}]: assert row[""lname""] == ""griffin"" table_in[{""name"": ""chris""}][""lname""] = ""peters"" for row in table_in[{""lname"": ""griffin""}]: assert row[""lname""] == ""griffin"" for row in table_in[{""lname"": ""peters""}]: assert row[""name""] == ""chris"" def test_save(tmpdir): from io import StringIO filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table_in = ljson.base.disk.Table.from_file(fio) assert list(table_in) == data f = StringIO() table_in.save(f) f.seek(0) table = ljson.base.disk.Table.from_file(f) assert list(table) == data def test_open(tmpdir): filename = os.path.join(str(tmpdir), ""table.ljson"") header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) f = open(filename, ""w+"") table.save(f) f.close() with ljson.base.mem.Table.open(filename) as table: assert list(table) == data with ljson.base.disk.Table.open(filename) as table: assert list(table) == data import pytest with pytest.raises(IOError): ljson.base.disk.Table.open(filename + ""**~~"") def test_unique_check(): import copy from io import StringIO header_descriptor_ = copy.copy(header_descriptor) header_descriptor_[""name""][""modifiers""] = [""unique""] header = ljson.base.generic.Header(header_descriptor_) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) table.additem(item_meg) import pytest with pytest.raises(ValueError): table.additem({""age"": 12, ""name"": ""chris"", ""lname"": ""griffin""}) def test_contains(): header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) from io import StringIO fio = StringIO() table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) assert {""lname"": ""griffin""} in table assert not {""lname"": ""griffindor""} in table def test_delete(tmpdir): import copy header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, copy.copy(data)) import os filename = os.path.join(str(tmpdir), ""table.ljson"") fio = open(filename, ""w+"") table.save(fio) fio.seek(0) table = ljson.base.disk.Table.from_file(fio) data_ = copy.copy(data) del(data_[0]) del(table[{""name"": ""peter""}]) assert list(table) == data_ table.additem(item_meg) del(table[{""name"": ""meg""}]) assert list(table) == data_ @pytest.mark.slow def test_speedup_delete(tmpdir, benchmark): benchmark(test_delete, tmpdir) @pytest.mark.slow def test_speedup_edit(tmpdir, benchmark): benchmark(test_edit, tmpdir) @pytest.mark.slow def test_speedup_contains(tmpdir, benchmark): benchmark(test_contains) @pytest.mark.slow def test_speedup_selection_iter(tmpdir, benchmark): benchmark(test_selection_iter, tmpdir) ", https://github.com/daknuett/ljson,1d3dc13001d9d2f61bcfbf9d5c5b16d44fb8076e,test/test_ljson_mem.py::test_contains,test/test_ljson_mem.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import ljson.base.mem import ljson.base.generic from .data import data, header_descriptor, item_meg def test_construct(): header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) assert list(table) == data table_postinsert = ljson.base.mem.Table(header, []) for row in data: table_postinsert.additem(row) assert list(table_postinsert) == data assert table[{""name"": ""peter""}].getone()[""age""] == 42 assert list(table[{""name"": ""peter""}]) == data[:1] def test_write_and_read(): from io import StringIO, BytesIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.mem.Table.from_file(fio) assert list(table_in) == data import pytest with pytest.raises(KeyError): assert table_in[{""foo"": ""bar""}] fio.seek(0) fio2 = BytesIO(fio.read().encode(""utf-8"")) table_in = ljson.base.mem.Table.from_file(fio2) assert list(table_in) == data fio3 = BytesIO() table_in.save(fio3) print(fio3.closed) fio3.seek(0, 0) fio2.seek(0, 0) assert list(ljson.base.mem.Table.from_file(fio2)) == list(ljson.base.mem.Table.from_file(fio3)) def test_edit(): import copy data_ = copy.copy(data) header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data_) table[{""lname"": ""griffin""}][""lname""] = ""Griffin"" data_ = [] for row in data: row_ = copy.copy(row) row_[""lname""] = ""Griffin"" data_.append(row_) assert list(table) == data_ table.additem(item_meg) assert list(table) == data_ + [item_meg] def test_unique_check(): import copy header_descriptor_ = copy.copy(header_descriptor) header_descriptor_[""name""][""modifiers""] = [""unique""] header = ljson.base.generic.Header(header_descriptor_) table = ljson.base.mem.Table(header, data) table.additem(item_meg) import pytest with pytest.raises(ValueError): table.additem({""age"": 12, ""name"": ""chris"", ""lname"": ""griffin""}) def test_contains(): header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) assert {""lname"": ""griffin""} in table assert not {""lname"": ""griffindor""} in table def test_delete(): import copy header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, copy.copy(data)) data_ = copy.copy(data) del(data_[0]) del(table[{""name"": ""peter""}]) assert list(table) == data_ ", https://github.com/daknuett/ljson,1d3dc13001d9d2f61bcfbf9d5c5b16d44fb8076e,test/test_ljson_mem.py::test_edit,test/test_ljson_mem.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import ljson.base.mem import ljson.base.generic from .data import data, header_descriptor, item_meg def test_construct(): header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) assert list(table) == data table_postinsert = ljson.base.mem.Table(header, []) for row in data: table_postinsert.additem(row) assert list(table_postinsert) == data assert table[{""name"": ""peter""}].getone()[""age""] == 42 assert list(table[{""name"": ""peter""}]) == data[:1] def test_write_and_read(): from io import StringIO, BytesIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.mem.Table.from_file(fio) assert list(table_in) == data import pytest with pytest.raises(KeyError): assert table_in[{""foo"": ""bar""}] fio.seek(0) fio2 = BytesIO(fio.read().encode(""utf-8"")) table_in = ljson.base.mem.Table.from_file(fio2) assert list(table_in) == data fio3 = BytesIO() table_in.save(fio3) print(fio3.closed) fio3.seek(0, 0) fio2.seek(0, 0) assert list(ljson.base.mem.Table.from_file(fio2)) == list(ljson.base.mem.Table.from_file(fio3)) def test_edit(): import copy data_ = copy.copy(data) header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data_) table[{""lname"": ""griffin""}][""lname""] = ""Griffin"" data_ = [] for row in data: row_ = copy.copy(row) row_[""lname""] = ""Griffin"" data_.append(row_) assert list(table) == data_ table.additem(item_meg) assert list(table) == data_ + [item_meg] def test_unique_check(): import copy header_descriptor_ = copy.copy(header_descriptor) header_descriptor_[""name""][""modifiers""] = [""unique""] header = ljson.base.generic.Header(header_descriptor_) table = ljson.base.mem.Table(header, data) table.additem(item_meg) import pytest with pytest.raises(ValueError): table.additem({""age"": 12, ""name"": ""chris"", ""lname"": ""griffin""}) def test_contains(): header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) assert {""lname"": ""griffin""} in table assert not {""lname"": ""griffindor""} in table def test_delete(): import copy header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, copy.copy(data)) data_ = copy.copy(data) del(data_[0]) del(table[{""name"": ""peter""}]) assert list(table) == data_ ", https://github.com/daknuett/ljson,1d3dc13001d9d2f61bcfbf9d5c5b16d44fb8076e,test/test_ljson_mem.py::test_unique_check,test/test_ljson_mem.py,NIO,flaky,Accepted,https://github.com/daknuett/ljson/pull/4,classify;root_cause;fix_proposal,"import ljson.base.mem import ljson.base.generic from .data import data, header_descriptor, item_meg def test_construct(): header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) assert list(table) == data table_postinsert = ljson.base.mem.Table(header, []) for row in data: table_postinsert.additem(row) assert list(table_postinsert) == data assert table[{""name"": ""peter""}].getone()[""age""] == 42 assert list(table[{""name"": ""peter""}]) == data[:1] def test_write_and_read(): from io import StringIO, BytesIO header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) fio = StringIO() table.save(fio) fio.seek(0) table_in = ljson.base.mem.Table.from_file(fio) assert list(table_in) == data import pytest with pytest.raises(KeyError): assert table_in[{""foo"": ""bar""}] fio.seek(0) fio2 = BytesIO(fio.read().encode(""utf-8"")) table_in = ljson.base.mem.Table.from_file(fio2) assert list(table_in) == data fio3 = BytesIO() table_in.save(fio3) print(fio3.closed) fio3.seek(0, 0) fio2.seek(0, 0) assert list(ljson.base.mem.Table.from_file(fio2)) == list(ljson.base.mem.Table.from_file(fio3)) def test_edit(): import copy data_ = copy.copy(data) header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data_) table[{""lname"": ""griffin""}][""lname""] = ""Griffin"" data_ = [] for row in data: row_ = copy.copy(row) row_[""lname""] = ""Griffin"" data_.append(row_) assert list(table) == data_ table.additem(item_meg) assert list(table) == data_ + [item_meg] def test_unique_check(): import copy header_descriptor_ = copy.copy(header_descriptor) header_descriptor_[""name""][""modifiers""] = [""unique""] header = ljson.base.generic.Header(header_descriptor_) table = ljson.base.mem.Table(header, data) table.additem(item_meg) import pytest with pytest.raises(ValueError): table.additem({""age"": 12, ""name"": ""chris"", ""lname"": ""griffin""}) def test_contains(): header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, data) assert {""lname"": ""griffin""} in table assert not {""lname"": ""griffindor""} in table def test_delete(): import copy header = ljson.base.generic.Header(header_descriptor) table = ljson.base.mem.Table(header, copy.copy(data)) data_ = copy.copy(data) del(data_[0]) del(table[{""name"": ""peter""}]) assert list(table) == data_ ", https://github.com/daliclass/rxpy-backpressure,b1a7bddfb370884135d6f2e7d9a3dd332a90d7cc,tests/test_sized_buffer_backpressure_strategy.py::TestDropBackPressureStrategy::test_on_next_drop_new_message_when_buffer_full,tests/test_sized_buffer_backpressure_strategy.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from time import sleep from unittest import TestCase from unittest.mock import call from rxpy_backpressure.backpressure import BackPressure from rxpy_backpressure.sized_buffer import SizedBufferBackPressureStrategy from rxpy_backpressure.observer import Observer from tests.mocks.mock_observer import MockObserver class TestDropBackPressureStrategy(TestCase): def test_when_on_complete_called_then_defer_to_wrapped_observer(self): mock_observer: MockObserver = MockObserver() buffer: Observer = SizedBufferBackPressureStrategy(mock_observer, cache_size=1) buffer.on_completed() mock_observer.on_completed_mock.assert_called_once() def test_when_on_error_called_then_defer_to_wrapped_observer(self): mock_observer: MockObserver = MockObserver() buffer: Observer = SizedBufferBackPressureStrategy(mock_observer, cache_size=1) error: Exception = Exception(""Some error happened"") buffer.on_error(error) mock_observer.on_error_mock.assert_called_once_with(error) def test_when_on_next_called_and_wrapped_is_not_processing_then_call_wrapped(self): mock_observer: MockObserver = MockObserver() buffer: Observer = SizedBufferBackPressureStrategy(mock_observer, cache_size=1) message: dict = {""id"": 1, ""payload"": ""OK""} buffer.on_next(message) mock_observer.on_next_mock.assert_called_once_with(message) def test_when_on_next_called_cache_subsequent_messages_and_call_afterwards(self): mock_observer: MockObserver = MockObserver(include_sleeps=True) buffer: SizedBufferBackPressureStrategy = BackPressure.SIZED_BUFFER( mock_observer ) messages = [ {""id"": 0, ""payload"": ""OK""}, {""id"": 1, ""payload"": ""OK""}, {""id"": 2, ""payload"": ""OK""}, ] buffer.on_next(messages[0]) buffer.on_next(messages[1]) buffer.on_next(messages[2]) while buffer.is_locked(): sleep(0.25) mock_observer.on_next_mock.assert_has_calls( [ call(messages[0]), call(messages[1]), call(messages[1]), call(messages[2]), call(messages[2]), ] ) def test_when_on_error_called_cache_subsequent_messages_and_call_afterwards(self): mock_observer: MockObserver = MockObserver(include_sleeps=True) buffer: SizedBufferBackPressureStrategy = BackPressure.SIZED_BUFFER( mock_observer ) messages = [ {""id"": 0, ""payload"": ""OK""}, {""id"": 1, ""payload"": ""OK""}, {""id"": 2, ""payload"": ""OK""}, ] buffer.on_error(messages[0]) buffer.on_error(messages[1]) buffer.on_error(messages[2]) while buffer.is_locked(): sleep(0.25) mock_observer.on_error_mock.assert_has_calls( [ call(messages[0]), call(messages[1]), call(messages[1]), call(messages[2]), call(messages[2]), ] ) def test_when_on_next_buffer_following_messages(self): mock_observer: MockObserver = MockObserver(include_sleeps=True) buffer: SizedBufferBackPressureStrategy = BackPressure.SIZED_BUFFER( mock_observer, cache_size=10 ) messages = [ {""id"": 0, ""payload"": ""OK""}, {""id"": 1, ""payload"": ""OK""}, {""id"": 2, ""payload"": ""OK""}, {""id"": 3, ""payload"": ""OK""}, {""id"": 4, ""payload"": ""OK""}, {""id"": 5, ""payload"": ""OK""}, {""id"": 6, ""payload"": ""OK""}, {""id"": 7, ""payload"": ""OK""}, ] buffer.on_next(messages[0]) buffer.on_next(messages[1]) buffer.on_next(messages[2]) buffer.on_next(messages[3]) while buffer.is_locked(): sleep(0.25) buffer.on_next(messages[4]) buffer.on_next(messages[5]) buffer.on_next(messages[6]) buffer.on_next(messages[7]) while buffer.is_locked(): sleep(0.25) mock_observer.on_next_mock.assert_has_calls( [ call(messages[0]), call(messages[1]), call(messages[1]), call(messages[2]), call(messages[2]), call(messages[3]), call(messages[3]), call(messages[4]), call(messages[5]), call(messages[5]), call(messages[6]), call(messages[6]), call(messages[7]), call(messages[7]), ] ) self.assertEqual(4, buffer.counter.get_stats().get(""successful_events"")) self.assertEqual(4, buffer.counter.get_stats().get(""dropped_events"")) def test_on_next_drop_new_message_when_buffer_full(self): mock_observer: MockObserver = MockObserver(include_sleeps=True) buffer: SizedBufferBackPressureStrategy = BackPressure.SIZED_BUFFER( mock_observer, cache_size=2 ) messages = [ {""id"": 0, ""payload"": ""OK""}, {""id"": 1, ""payload"": ""OK""}, {""id"": 2, ""payload"": ""OK""}, {""id"": 3, ""payload"": ""OK""}, {""id"": 4, ""payload"": ""OK""}, {""id"": 5, ""payload"": ""OK""}, {""id"": 6, ""payload"": ""OK""}, {""id"": 7, ""payload"": ""OK""}, ] buffer.on_next(messages[0]) buffer.on_next(messages[1]) buffer.on_next(messages[2]) buffer.on_next(messages[3]) while buffer.is_locked(): sleep(0.25) buffer.on_next(messages[4]) buffer.on_next(messages[5]) buffer.on_next(messages[6]) buffer.on_next(messages[7]) while buffer.is_locked(): sleep(0.25) mock_observer.on_next_mock.assert_has_calls( [ call(messages[0]), call(messages[1]), call(messages[1]), call(messages[4]), call(messages[5]), call(messages[5]), ] ) self.assertEqual(4, buffer.counter.get_stats().get(""successful_events"")) self.assertEqual(4, buffer.counter.get_stats().get(""dropped_events"")) ", https://github.com/DanielSank/observed,d99fb99ff2a470a86efb2763685e8e2c021e799f,observed_test.py::TestBasics::test_callbacks,observed_test.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import itertools import observed from observed import observable_function, observable_method import pytest def get_caller_name(caller): """"""Find the name of a calling (i.e. observed) object. Args: caller: The observed object which is calling an observer. Returns: The name of the caller. If the caller is a function we return that function's .__name__. If the caller is a bound method we return the name of bound object. """""" if hasattr(caller, ""__self__""): # caller is a Foo instance name = caller.__self__.name else: # caller is a function. name = caller.__name__ return name def clear_list(l): """"""Remove all entries from a list in place. Args: l: The list to be cleared. """""" while True: try: l.pop(0) except IndexError: break class Foo(object): """"""A class with some observable methods and some normal methods. Attributes: name - string: A string naming the instance. buf - list: A buffer for collecting a record of called methods. My methods are set up to append a string to buf when they are called. The strings are formatted in one of two ways. If the method being called accepts the observed object as a first argument, the string is: ""%s%s%s""%(self.name, method_name, caller_name) where method_name is the name of the method being called on me and caller_name is the name of the calling Foo instance or the name of the calling function. Note that the name of the calling instance is NOT the same thing as the name of the calling method, which doesn't exist. If the method being called does not accept the caller as a first argument, the string written to buf is: ""%s%s""%(self.name, method_name). """""" def __init__(self, name, buf): """"""Initialize a Foo object. Args: name: A name for this instance. buf: A buffer (list) into which I write data when my methods are called. See the class docstring for details. """""" self.name = name self.buf = buf @observable_method() def bar(self): self.buf.append(""%sbar""%(self.name,)) def baz(self): self.buf.append(""%sbaz""%(self.name,)) @observable_method() def milton(self, caller): caller_name = get_caller_name(caller) self.buf.append(""%smilton%s""%(self.name, caller_name)) def waldo(self, caller): caller_name = get_caller_name(caller) self.buf.append(""%swaldo%s""%(self.name, caller_name)) def observable_methods(self): """"""Get all of this object's observable methods. We don't include milton because our testing procedure isn't smart enough to know to call it with an argument. Returns: A list of the names of my observable methods. """""" return [self.bar] def method_info(self): """"""Get a list of method names and whether they want caller id. Returns: A list of tuples. Each tuple is (str: method name, bool: identify_observed). """""" return [(self.bar, False), (self.baz, False), (self.milton, True), (self.waldo, True)] class Goo(Foo): """"""Same as Foo but using the descriptor strategy for observer persistence. I am entirely similar to Foo except that my observable methods use the descriptor persistence strategy. See the docstring for observable_method for a detailed explanation. """""" def bar(self): self.buf.append(""%sbar""%(self.name,)) bar = observed.get_observable_method(bar, strategy='descriptor') def milton(self, caller): caller_name = get_caller_name(caller) self.buf.append(""%smilton%s""%(self.name, caller_name)) milton = observed.get_observable_method(milton, strategy='descriptor') def get_observables(*objs): """"""Get a list observables from some objects. Args: Any number of objects. Each object must be either a Foo (or subclass) instance or a function. Returns: A list of observable things, either functions or methods bound to the object. Each function passed in as an argument is placed directly into the returned list. For each Foo instance passed in, we get each of that instance's observable methods and place each one in the output list. """""" observables = [] for obj in objs: if isinstance(obj, Foo): observables.extend(obj.observable_methods()) elif isinstance(obj, observed.ObservableFunction): observables.append(obj) else: raise TypeError(""Object of type %s not observable""%(type(obj),)) return observables def get_observer_sets(*objs): """"""Get observers from a set of objects. Returns: A list of tuples. Each tuple is an observer and a boolean corresponding to the value of identify_observed which should be used when registering that observer. """""" observer_sets = [] single_observers = [] for obj in objs: if isinstance(obj, Foo): single_observers.extend(obj.method_info()) else: single_observers.append((obj[0], obj[1])) # for num_observers in range(len(single_observers)): for comb in itertools.combinations(single_observers, 3): observer_sets.append(comb) return observer_sets def get_items(observables, observer_sets): """"""Get all combinations of observer/observed and expected test data. Returns: A list of tuples. Each tuple contains: an observable object a list of (observer, identify_observed) tuples expected buffer data for this combination expected buffer data for calling the obsevable after all observers have been un-registered. """""" def get_buff_data(observable, observer, identify_observed): """"""Get the buffer data an object will write."""""" if hasattr(observer, '__self__'): expected = observer.__self__.name+observer.__name__ else: expected = observer.__name__ if identify_observed: expected = expected + get_caller_name(observable) return expected items = [] for observable, observer_set in itertools.product(observables, observer_sets): # Don't include this combination if it would cause infinite recursion. recursion = False for observer, _ in observer_set: if type(observer) == type(observable) and observer == observable: recursion = True if recursion: continue expected_buf = [] if isinstance(observable, observed.ObservableBoundMethod): final = observable.__self__.name + observable.__name__ elif isinstance(observable, observed.ObservableFunction): final = observable.__name__ for observer, caller_id in observer_set: expected_buf.append(get_buff_data(observable, observer, caller_id)) expected_buf.insert(0, final) expected_buf.sort() items.append((observable, observer_set, expected_buf, [final])) return items class TestBasics: """"""Test that observers are called when the observed object is called."""""" @classmethod def setup_class(self): self.buf = [] @classmethod def teardown_class(self): pass def test_callbacks(self): """""" Test all combinations of types acting as observed and observer. For each combination of observed and observer we check that all observers are called. We also check that after discarding the observers subsequent invocations of the observed object do not call any observers. """""" a = Foo('a', self.buf) b = Foo('b', self.buf) c = Goo('c', self.buf) d = Goo('d', self.buf) @observable_function def f(): self.buf.append('f') @observable_function def g(caller): self.buf.append('g%s'%(get_caller_name(caller),)) # We don't include g in our set of observables because the testing # code isn't smart enough to call it with an argument. observables = get_observables(a, b, c, d, f) observer_sets = get_observer_sets(a, b, c, d, (f, False), (g, True)) items = get_items(observables, observer_sets) for observed, observer_set, expected_buf, final_buf in items: for observer, identify_observed in observer_set: observed.add_observer(observer, identify_observed=identify_observed) observed() self.buf.sort() assert self.buf == expected_buf clear_list(self.buf) for observer, _ in observer_set: observed.discard_observer(observer) observed() assert self.buf == final_buf clear_list(self.buf) def test_discard(self): """"""Test that discard_observer prevents future ivocation."""""" a = Foo('a', self.buf) def f(): self.buf.append('f') a.bar.add_observer(f) result = a.bar.discard_observer(f) assert result == True result = a.bar.discard_observer(f) assert result == False a.bar() assert self.buf == ['abar'] def test_unbound_method(self): """"""Test that calling an unbound method invokes observers."""""" buf = [] f = Foo('f', buf) def func(): buf.append('func') f.bar.add_observer(func) Foo.bar(f) assert buf == ['fbar', 'func'] def test_equality(self): """"""Test equalit", https://github.com/DanielSank/observed,d99fb99ff2a470a86efb2763685e8e2c021e799f,observed_test.py::TestBasics::test_discard,observed_test.py,NIO,flaky,Accepted,https://github.com/DanielSank/observed/pull/24,classify;root_cause;fix_proposal,"import itertools import observed from observed import observable_function, observable_method import pytest def get_caller_name(caller): """"""Find the name of a calling (i.e. observed) object. Args: caller: The observed object which is calling an observer. Returns: The name of the caller. If the caller is a function we return that function's .__name__. If the caller is a bound method we return the name of bound object. """""" if hasattr(caller, ""__self__""): # caller is a Foo instance name = caller.__self__.name else: # caller is a function. name = caller.__name__ return name def clear_list(l): """"""Remove all entries from a list in place. Args: l: The list to be cleared. """""" while True: try: l.pop(0) except IndexError: break class Foo(object): """"""A class with some observable methods and some normal methods. Attributes: name - string: A string naming the instance. buf - list: A buffer for collecting a record of called methods. My methods are set up to append a string to buf when they are called. The strings are formatted in one of two ways. If the method being called accepts the observed object as a first argument, the string is: ""%s%s%s""%(self.name, method_name, caller_name) where method_name is the name of the method being called on me and caller_name is the name of the calling Foo instance or the name of the calling function. Note that the name of the calling instance is NOT the same thing as the name of the calling method, which doesn't exist. If the method being called does not accept the caller as a first argument, the string written to buf is: ""%s%s""%(self.name, method_name). """""" def __init__(self, name, buf): """"""Initialize a Foo object. Args: name: A name for this instance. buf: A buffer (list) into which I write data when my methods are called. See the class docstring for details. """""" self.name = name self.buf = buf @observable_method() def bar(self): self.buf.append(""%sbar""%(self.name,)) def baz(self): self.buf.append(""%sbaz""%(self.name,)) @observable_method() def milton(self, caller): caller_name = get_caller_name(caller) self.buf.append(""%smilton%s""%(self.name, caller_name)) def waldo(self, caller): caller_name = get_caller_name(caller) self.buf.append(""%swaldo%s""%(self.name, caller_name)) def observable_methods(self): """"""Get all of this object's observable methods. We don't include milton because our testing procedure isn't smart enough to know to call it with an argument. Returns: A list of the names of my observable methods. """""" return [self.bar] def method_info(self): """"""Get a list of method names and whether they want caller id. Returns: A list of tuples. Each tuple is (str: method name, bool: identify_observed). """""" return [(self.bar, False), (self.baz, False), (self.milton, True), (self.waldo, True)] class Goo(Foo): """"""Same as Foo but using the descriptor strategy for observer persistence. I am entirely similar to Foo except that my observable methods use the descriptor persistence strategy. See the docstring for observable_method for a detailed explanation. """""" def bar(self): self.buf.append(""%sbar""%(self.name,)) bar = observed.get_observable_method(bar, strategy='descriptor') def milton(self, caller): caller_name = get_caller_name(caller) self.buf.append(""%smilton%s""%(self.name, caller_name)) milton = observed.get_observable_method(milton, strategy='descriptor') def get_observables(*objs): """"""Get a list observables from some objects. Args: Any number of objects. Each object must be either a Foo (or subclass) instance or a function. Returns: A list of observable things, either functions or methods bound to the object. Each function passed in as an argument is placed directly into the returned list. For each Foo instance passed in, we get each of that instance's observable methods and place each one in the output list. """""" observables = [] for obj in objs: if isinstance(obj, Foo): observables.extend(obj.observable_methods()) elif isinstance(obj, observed.ObservableFunction): observables.append(obj) else: raise TypeError(""Object of type %s not observable""%(type(obj),)) return observables def get_observer_sets(*objs): """"""Get observers from a set of objects. Returns: A list of tuples. Each tuple is an observer and a boolean corresponding to the value of identify_observed which should be used when registering that observer. """""" observer_sets = [] single_observers = [] for obj in objs: if isinstance(obj, Foo): single_observers.extend(obj.method_info()) else: single_observers.append((obj[0], obj[1])) # for num_observers in range(len(single_observers)): for comb in itertools.combinations(single_observers, 3): observer_sets.append(comb) return observer_sets def get_items(observables, observer_sets): """"""Get all combinations of observer/observed and expected test data. Returns: A list of tuples. Each tuple contains: an observable object a list of (observer, identify_observed) tuples expected buffer data for this combination expected buffer data for calling the obsevable after all observers have been un-registered. """""" def get_buff_data(observable, observer, identify_observed): """"""Get the buffer data an object will write."""""" if hasattr(observer, '__self__'): expected = observer.__self__.name+observer.__name__ else: expected = observer.__name__ if identify_observed: expected = expected + get_caller_name(observable) return expected items = [] for observable, observer_set in itertools.product(observables, observer_sets): # Don't include this combination if it would cause infinite recursion. recursion = False for observer, _ in observer_set: if type(observer) == type(observable) and observer == observable: recursion = True if recursion: continue expected_buf = [] if isinstance(observable, observed.ObservableBoundMethod): final = observable.__self__.name + observable.__name__ elif isinstance(observable, observed.ObservableFunction): final = observable.__name__ for observer, caller_id in observer_set: expected_buf.append(get_buff_data(observable, observer, caller_id)) expected_buf.insert(0, final) expected_buf.sort() items.append((observable, observer_set, expected_buf, [final])) return items class TestBasics: """"""Test that observers are called when the observed object is called."""""" @classmethod def setup_class(self): self.buf = [] @classmethod def teardown_class(self): pass def test_callbacks(self): """""" Test all combinations of types acting as observed and observer. For each combination of observed and observer we check that all observers are called. We also check that after discarding the observers subsequent invocations of the observed object do not call any observers. """""" a = Foo('a', self.buf) b = Foo('b', self.buf) c = Goo('c', self.buf) d = Goo('d', self.buf) @observable_function def f(): self.buf.append('f') @observable_function def g(caller): self.buf.append('g%s'%(get_caller_name(caller),)) # We don't include g in our set of observables because the testing # code isn't smart enough to call it with an argument. observables = get_observables(a, b, c, d, f) observer_sets = get_observer_sets(a, b, c, d, (f, False), (g, True)) items = get_items(observables, observer_sets) for observed, observer_set, expected_buf, final_buf in items: for observer, identify_observed in observer_set: observed.add_observer(observer, identify_observed=identify_observed) observed() self.buf.sort() assert self.buf == expected_buf clear_list(self.buf) for observer, _ in observer_set: observed.discard_observer(observer) observed() assert self.buf == final_buf clear_list(self.buf) def test_discard(self): """"""Test that discard_observer prevents future ivocation."""""" a = Foo('a', self.buf) def f(): self.buf.append('f') a.bar.add_observer(f) result = a.bar.discard_observer(f) assert result == True result = a.bar.discard_observer(f) assert result == False a.bar() assert self.buf == ['abar'] def test_unbound_method(self): """"""Test that calling an unbound method invokes observers."""""" buf = [] f = Foo('f', buf) def func(): buf.append('func') f.bar.add_observer(func) Foo.bar(f) assert buf == ['fbar', 'func'] def test_equality(self): """"""Test equalit", https://github.com/datacommonsorg/api-python,6d09ca1557da0af893f008f7302801f12afc2d46,datacommons/test/set_api_key_test.py::TestApiKey::test_query_no_api_key,datacommons/test/set_api_key_test.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2017 Google Inc. # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """""" Data Commons Python API unit tests. Unit tests setting the API Key. """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from unittest import mock import datacommons as dc import datacommons.utils as utils import os import json import unittest import urllib _TEST_API_KEY = 'TEST-API-KEY' _SPARQL_NO_KEY = 'query_no_key' _SPARQL_W_KEY = 'query_w_key' _SEND_REQ_NO_KEY = 'https://send_request_no_key.com' _SEND_REQ_W_KEY = 'https://send_request_w_key.com' def request_mock(*args, **kwargs): """""" A mock urlopen call sent in the urllib package. """""" # Create the mock response object. class MockResponse: def __init__(self, json_data): self.json_data = json_data def read(self): return self.json_data req = args[0] if req.full_url == _SEND_REQ_NO_KEY or json.loads(req.data) == {'sparql': _SPARQL_NO_KEY}: assert 'X-api-key' not in req.headers else: assert req.get_header('X-api-key') == _TEST_API_KEY if req.full_url == utils._API_ROOT + utils._API_ENDPOINTS['query']: # Return a dummy response that will parse into [] by query() return MockResponse(json.dumps({ 'header': [ '?name', '?dcid' ], })) else: # Return a dummy response that will parse into {} by _send_request() return MockResponse(json.dumps({'payload': json.dumps({})})) class TestApiKey(unittest.TestCase): """"""Unit test for setting or not setting the API Key."""""" @mock.patch('six.moves.urllib.request.urlopen', side_effect=request_mock) def test_query_no_api_key(self, urlopen): del os.environ[utils._ENV_VAR_API_KEY] # Issue a dummy SPARQL query that tells the mock to not expect a key self.assertEqual(dc.query(_SPARQL_NO_KEY), []) @mock.patch('six.moves.urllib.request.urlopen', side_effect=request_mock) def test_send_request_no_api_key(self, urlopen): del os.environ[utils._ENV_VAR_API_KEY] # Issue a dummy url that tells the mock to not expect a key self.assertEqual(utils._send_request(_SEND_REQ_NO_KEY, {'foo': ['bar']}), {}) @mock.patch('six.moves.urllib.request.urlopen', side_effect=request_mock) def test_query_w_api_key(self, urlopen): """""" Handles row-less response. """""" # Set the API key dc.set_api_key('make_sure_I_am_replaced') dc.set_api_key(_TEST_API_KEY) # Issue a dummy SPARQL query that tells the mock to expect a key self.assertEqual(dc.query(_SPARQL_W_KEY), []) @mock.patch('six.moves.urllib.request.urlopen', side_effect=request_mock) def test_send_request_w_api_key(self, urlopen): """""" Handles row-less response. """""" # Set the API key dc.set_api_key(_TEST_API_KEY) # Issue a dummy url that tells the mock to expect a key self.assertEqual(utils._send_request(_SEND_REQ_W_KEY), {}) if __name__ == '__main__': unittest.main() ", https://github.com/datacommonsorg/api-python,6d09ca1557da0af893f008f7302801f12afc2d46,datacommons/test/set_api_key_test.py::TestApiKey::test_send_request_no_api_key,datacommons/test/set_api_key_test.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2017 Google Inc. # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """""" Data Commons Python API unit tests. Unit tests setting the API Key. """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from unittest import mock import datacommons as dc import datacommons.utils as utils import os import json import unittest import urllib _TEST_API_KEY = 'TEST-API-KEY' _SPARQL_NO_KEY = 'query_no_key' _SPARQL_W_KEY = 'query_w_key' _SEND_REQ_NO_KEY = 'https://send_request_no_key.com' _SEND_REQ_W_KEY = 'https://send_request_w_key.com' def request_mock(*args, **kwargs): """""" A mock urlopen call sent in the urllib package. """""" # Create the mock response object. class MockResponse: def __init__(self, json_data): self.json_data = json_data def read(self): return self.json_data req = args[0] if req.full_url == _SEND_REQ_NO_KEY or json.loads(req.data) == {'sparql': _SPARQL_NO_KEY}: assert 'X-api-key' not in req.headers else: assert req.get_header('X-api-key') == _TEST_API_KEY if req.full_url == utils._API_ROOT + utils._API_ENDPOINTS['query']: # Return a dummy response that will parse into [] by query() return MockResponse(json.dumps({ 'header': [ '?name', '?dcid' ], })) else: # Return a dummy response that will parse into {} by _send_request() return MockResponse(json.dumps({'payload': json.dumps({})})) class TestApiKey(unittest.TestCase): """"""Unit test for setting or not setting the API Key."""""" @mock.patch('six.moves.urllib.request.urlopen', side_effect=request_mock) def test_query_no_api_key(self, urlopen): del os.environ[utils._ENV_VAR_API_KEY] # Issue a dummy SPARQL query that tells the mock to not expect a key self.assertEqual(dc.query(_SPARQL_NO_KEY), []) @mock.patch('six.moves.urllib.request.urlopen', side_effect=request_mock) def test_send_request_no_api_key(self, urlopen): del os.environ[utils._ENV_VAR_API_KEY] # Issue a dummy url that tells the mock to not expect a key self.assertEqual(utils._send_request(_SEND_REQ_NO_KEY, {'foo': ['bar']}), {}) @mock.patch('six.moves.urllib.request.urlopen', side_effect=request_mock) def test_query_w_api_key(self, urlopen): """""" Handles row-less response. """""" # Set the API key dc.set_api_key('make_sure_I_am_replaced') dc.set_api_key(_TEST_API_KEY) # Issue a dummy SPARQL query that tells the mock to expect a key self.assertEqual(dc.query(_SPARQL_W_KEY), []) @mock.patch('six.moves.urllib.request.urlopen', side_effect=request_mock) def test_send_request_w_api_key(self, urlopen): """""" Handles row-less response. """""" # Set the API key dc.set_api_key(_TEST_API_KEY) # Issue a dummy url that tells the mock to expect a key self.assertEqual(utils._send_request(_SEND_REQ_W_KEY), {}) if __name__ == '__main__': unittest.main() ", https://github.com/davidhalter/parso,6ae0efa415c9790000dba70f87e6ece20d6a4101,test/test_cache.py::test_permission_error,test/test_cache.py,NIO,flaky,Accepted,https://github.com/davidhalter/parso/pull/195,classify;root_cause;fix_proposal,""""""" Test all things related to the ``jedi.cache`` module. """""" import os import pytest import time from pathlib import Path from parso.cache import (_CACHED_FILE_MAXIMUM_SURVIVAL, _VERSION_TAG, _get_cache_clear_lock_path, _get_hashed_path, _load_from_file_system, _NodeCacheItem, _remove_cache_and_update_lock, _save_to_file_system, load_module, parser_cache, try_to_save_module) from parso._compatibility import is_pypy from parso import load_grammar from parso import cache from parso import file_io from parso import parse skip_pypy = pytest.mark.skipif( is_pypy, reason=""pickling in pypy is slow, since we don't pickle,"" ""we never go into path of auto-collecting garbage"" ) @pytest.fixture() def isolated_parso_cache(monkeypatch, tmpdir): """"""Set `parso.cache._default_cache_path` to a temporary directory during the test. """""" cache_path = Path(str(tmpdir), ""__parso_cache"") monkeypatch.setattr(cache, '_default_cache_path', cache_path) return cache_path def test_modulepickling_change_cache_dir(tmpdir): """""" ParserPickling should not save old cache when cache_directory is changed. See: `#168 `_ """""" dir_1 = Path(str(tmpdir.mkdir('first'))) dir_2 = Path(str(tmpdir.mkdir('second'))) item_1 = _NodeCacheItem('bla', []) item_2 = _NodeCacheItem('bla', []) path_1 = Path('fake path 1') path_2 = Path('fake path 2') hashed_grammar = load_grammar()._hashed _save_to_file_system(hashed_grammar, path_1, item_1, cache_path=dir_1) parser_cache.clear() cached = load_stored_item(hashed_grammar, path_1, item_1, cache_path=dir_1) assert cached == item_1.node _save_to_file_system(hashed_grammar, path_2, item_2, cache_path=dir_2) cached = load_stored_item(hashed_grammar, path_1, item_1, cache_path=dir_2) assert cached is None def load_stored_item(hashed_grammar, path, item, cache_path): """"""Load `item` stored at `path` in `cache`."""""" item = _load_from_file_system(hashed_grammar, path, item.change_time - 1, cache_path) return item @pytest.mark.usefixtures(""isolated_parso_cache"") def test_modulepickling_simulate_deleted_cache(tmpdir): """""" Tests loading from a cache file after it is deleted. According to macOS `dev docs`__, Note that the system may delete the Caches/ directory to free up disk space, so your app must be able to re-create or download these files as needed. It is possible that other supported platforms treat cache files the same way. __ https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html """""" # noqa grammar = load_grammar() module = 'fake parser' # Create the file path = Path(str(tmpdir.dirname), 'some_path') with open(path, 'w'): pass io = file_io.FileIO(path) try_to_save_module(grammar._hashed, io, module, lines=[]) assert load_module(grammar._hashed, io) == module os.unlink(_get_hashed_path(grammar._hashed, path)) parser_cache.clear() cached2 = load_module(grammar._hashed, io) assert cached2 is None def test_cache_limit(): def cache_size(): return sum(len(v) for v in parser_cache.values()) try: parser_cache.clear() future_node_cache_item = _NodeCacheItem('bla', [], change_time=time.time() + 10e6) old_node_cache_item = _NodeCacheItem('bla', [], change_time=time.time() - 10e4) parser_cache['some_hash_old'] = { '/path/%s' % i: old_node_cache_item for i in range(300) } parser_cache['some_hash_new'] = { '/path/%s' % i: future_node_cache_item for i in range(300) } assert cache_size() == 600 parse('somecode', cache=True, path='/path/somepath') assert cache_size() == 301 finally: parser_cache.clear() class _FixedTimeFileIO(file_io.KnownContentFileIO): def __init__(self, path, content, last_modified): super().__init__(path, content) self._last_modified = last_modified def get_last_modified(self): return self._last_modified @pytest.mark.parametrize('diff_cache', [False, True]) @pytest.mark.parametrize('use_file_io', [False, True]) def test_cache_last_used_update(diff_cache, use_file_io): p = Path('/path/last-used') parser_cache.clear() # Clear, because then it's easier to find stuff. parse('somecode', cache=True, path=p) node_cache_item = next(iter(parser_cache.values()))[p] now = time.time() assert node_cache_item.last_used < now if use_file_io: f = _FixedTimeFileIO(p, 'code', node_cache_item.last_used - 10) parse(file_io=f, cache=True, diff_cache=diff_cache) else: parse('somecode2', cache=True, path=p, diff_cache=diff_cache) node_cache_item = next(iter(parser_cache.values()))[p] assert now < node_cache_item.last_used < time.time() @skip_pypy def test_inactive_cache(tmpdir, isolated_parso_cache): parser_cache.clear() test_subjects = ""abcdef"" for path in test_subjects: parse('somecode', cache=True, path=os.path.join(str(tmpdir), path)) raw_cache_path = isolated_parso_cache.joinpath(_VERSION_TAG) assert raw_cache_path.exists() dir_names = os.listdir(raw_cache_path) a_while_ago = time.time() - _CACHED_FILE_MAXIMUM_SURVIVAL old_paths = set() for dir_name in dir_names[:len(test_subjects) // 2]: # make certain number of paths old os.utime(raw_cache_path.joinpath(dir_name), (a_while_ago, a_while_ago)) old_paths.add(dir_name) # nothing should be cleared while the lock is on assert _get_cache_clear_lock_path().exists() _remove_cache_and_update_lock() # it shouldn't clear anything assert len(os.listdir(raw_cache_path)) == len(test_subjects) assert old_paths.issubset(os.listdir(raw_cache_path)) os.utime(_get_cache_clear_lock_path(), (a_while_ago, a_while_ago)) _remove_cache_and_update_lock() assert len(os.listdir(raw_cache_path)) == len(test_subjects) // 2 assert not old_paths.intersection(os.listdir(raw_cache_path)) @skip_pypy def test_permission_error(monkeypatch): def save(*args, **kwargs): nonlocal was_called was_called = True raise PermissionError was_called = False monkeypatch.setattr(cache, '_save_to_file_system', save) with pytest.warns(Warning): parse(path=__file__, cache=True, diff_cache=True) assert was_called ", https://github.com/demisto/demisto-sdk,925303e5c54fc3dda985ef19b6d56a956d189f69,demisto_sdk/commands/secrets/tests/secrets_test.py::TestSecrets::test_two_files_with_same_name,demisto_sdk/commands/secrets/tests/secrets_test.py,NIO,flaky,Opened,https://github.com/demisto/demisto-sdk/pull/1574,classify;root_cause,"import io import json import os import shutil from demisto_sdk.commands.common.git_tools import git_path from demisto_sdk.commands.secrets.secrets import SecretsValidator def create_whitelist_secrets_file(file_path, urls=None, ips=None, files=None, generic_strings=None): if files is None: files = [] if urls is None: urls = [] if ips is None: ips = [] if generic_strings is None: generic_strings = [] with io.open(file_path, 'w') as f: secrets_content = dict( files=files, iocs=dict( ips=ips, urls=urls ), generic_strings=generic_strings ) f.write(json.dumps(secrets_content, indent=4)) def create_empty_whitelist_secrets_file(file_path): create_whitelist_secrets_file(file_path) class TestSecrets: FILES_PATH = os.path.normpath(os.path.join(__file__, f'{git_path()}/demisto_sdk/tests', 'test_files')) TEST_BASE_PATH = os.path.join(FILES_PATH, 'fake_integration/') TEST_YML_FILE = TEST_BASE_PATH + 'fake_integration.yml' TEST_PY_FILE = TEST_BASE_PATH + 'fake_integration.py' TEST_WHITELIST_FILE_PACKS = TEST_BASE_PATH + 'fake.secrets-ignore' TEST_WHITELIST_FILE = TEST_BASE_PATH + 'fake_secrets_white_list.json' TEST_BASE_64_STRING = 'OCSn7JGqKehoyIyMCm7gPFjKXpawXvh2M32' * 20 + ' sade' WHITE_LIST_FILE_NAME = 'secrets_white_list.json' FILE_HASH_LIST = ['123c8fc6532ba547d7ef598', '456c8fc6532ba547d7bb5e880a', '789c8fc6532ba57ef5985bb5e'] MAIL_LIST = ['test1@gmail.com', 'test2@gmail.com', 'test3@gmail.com'] IP_LIST = ['ip-172-31-15-237', '1.1.1.1', '12.25.12.14'] TEMP_DIR = os.path.join(FILES_PATH, 'temp') TEST_FILE_WITH_SECRETS = os.path.join(TEMP_DIR, 'file_with_secrets_in_it.yml') validator = SecretsValidator(is_circle=True, white_list_path=os.path.join(FILES_PATH, WHITE_LIST_FILE_NAME)) @classmethod def setup_class(cls): print(""Setups TestSecrets class"") if not os.path.exists(TestSecrets.TEMP_DIR): os.mkdir(TestSecrets.TEMP_DIR) @classmethod def teardown_class(cls): print(""Tearing down TestSecrets class"") if os.path.exists(TestSecrets.TEMP_DIR): shutil.rmtree(TestSecrets.TEMP_DIR, ignore_errors=False, onerror=None) def test_get_diff_text_files(self): changed_files = '''A\tIntegrations/Recorded_Future/Recorded_Future.yml\n D\tIntegrations/integration-Recorded_Future.yml''' get_diff = self.validator.get_diff_text_files(changed_files) assert 'Integrations/Recorded_Future/Recorded_Future.yml' in get_diff def test_is_text_file(self): changed_files = 'Integrations/Recorded_Future/Recorded_Future.yml' is_txt = self.validator.is_text_file(changed_files) assert is_txt is True def test_search_potential_secrets__no_secrets_found(self): secrets_found = self.validator.search_potential_secrets([self.TEST_YML_FILE]) assert not secrets_found def test_search_potential_secrets__secrets_found(self): create_empty_whitelist_secrets_file(os.path.join(TestSecrets.TEMP_DIR, TestSecrets.WHITE_LIST_FILE_NAME)) validator = SecretsValidator(is_circle=True, white_list_path=os.path.join(TestSecrets.TEMP_DIR, TestSecrets.WHITE_LIST_FILE_NAME)) with io.open(self.TEST_FILE_WITH_SECRETS, 'w') as f: f.write(''' print('This is our dummy code') a = 100 b = 300 c = a + b API_KEY = OIifdsnsjkgnj3254nkdfsjKNJD0345 # this is our secret some_dict = { 'some_foo': 100 } print(some_dict.some_foo) ''') secrets_found = validator.search_potential_secrets([self.TEST_FILE_WITH_SECRETS]) assert secrets_found[self.TEST_FILE_WITH_SECRETS] == ['OIifdsnsjkgnj3254nkdfsjKNJD0345'] def test_ignore_entropy(self): """""" - no items in the whitelist - file contains 2 secrets: - email - password - run validate secrets with --ignore-entropy=True - ensure email found - ensure entropy code was not executed - no secrets have found """""" create_empty_whitelist_secrets_file(os.path.join(TestSecrets.TEMP_DIR, TestSecrets.WHITE_LIST_FILE_NAME)) validator = SecretsValidator(is_circle=True, ignore_entropy=True, white_list_path=os.path.join(TestSecrets.TEMP_DIR, TestSecrets.WHITE_LIST_FILE_NAME)) with io.open(self.TEST_FILE_WITH_SECRETS, 'w') as f: f.write(''' print('This is our dummy code') my_email = ""fooo@someorg.com"" API_KEY = OIifdsnsjkgnj3254nkdfsjKNJD0345 # this is our secret some_dict = { 'some_foo': 100 } ''') secrets_found = validator.search_potential_secrets([self.TEST_FILE_WITH_SECRETS], True) assert secrets_found[self.TEST_FILE_WITH_SECRETS] == ['fooo@someorg.com'] def test_two_files_with_same_name(self): """""" - no items in the whitelist - file contains 1 secret: - email - run validate secrets with --ignore-entropy=True - ensure secret is found in two files from different directories with the same base name """""" create_empty_whitelist_secrets_file(os.path.join(TestSecrets.TEMP_DIR, TestSecrets.WHITE_LIST_FILE_NAME)) dir1_path = os.path.join(TestSecrets.TEMP_DIR, ""dir1"") dir2_path = os.path.join(TestSecrets.TEMP_DIR, ""dir2"") os.mkdir(dir1_path) os.mkdir(dir2_path) validator = SecretsValidator(is_circle=True, ignore_entropy=True, white_list_path=os.path.join(TestSecrets.TEMP_DIR, TestSecrets.WHITE_LIST_FILE_NAME)) file_name = 'README.md' file1_path = os.path.join(dir1_path, file_name) file2_path = os.path.join(dir2_path, file_name) for file_path in [file1_path, file2_path]: with io.open(file_path, 'w') as f: f.write(''' print('This is our dummy code') my_email = ""fooo@someorg.com"" ''') secrets_found = validator.search_potential_secrets([file1_path, file2_path], True) assert secrets_found[os.path.join(dir1_path, file_name)] == ['fooo@someorg.com'] assert secrets_found[os.path.join(dir2_path, file_name)] == ['fooo@someorg.com'] def test_remove_white_list_regex(self): white_list = '155.165.45.232' file_contents = ''' boop shmoop 155.165.45.232 ''' file_contents = self.validator.remove_whitelisted_items_from_file(file_contents, {white_list}) assert white_list not in file_contents def test_remove_whitelisted_items_from_file_escaped_whitelist(self): """""" Given - White list with a term that can be regex (***.). - String with no content When - Removing terms containing that regex Then - Ensure secrets that the secret isn't in the output. - Ensure no error raised """""" white_list = '***.url' file_contents = ''' Random and unmeaningful file content a string containing ***.url ''' file_contents = self.validator.remove_whitelisted_items_from_file(file_contents, {white_list}) assert white_list not in file_contents def test_remove_whitelisted_items_from_file_substring(self): white_list = 'url.com' file_contents = ''' url.com boop cool@url.com shmoop https://url.com ''' assert white_list not in self.validator.remove_whitelisted_items_from_file(file_contents, {white_list}) def test_temp_white_list(self): file_contents = self.validator.get_file_contents(self.TEST_YML_FILE, '.yml') temp_white_list = self.validator.create_temp_white_list(file_contents) assert 'sha256' in temp_white_list def test_get_related_yml_contents(self): yml_file_contents = self.validator.retrieve_related_yml(os.path.dirname(self.TEST_PY_FILE)) assert 'Use the Zoom integration manage your Zoom users and meetings' in yml_file_contents def test_regex_for_secrets(self): line = 'dockerimage: demisto/duoadmin:1.0.0.147 199.199.178.199 123e4567-e89b-12d3-a456-426655440000' secrets, false_positives = self.validator.regex_for_secrets(line) assert '1.0.0.147' in false_positives assert '123e4567-e89b-12d3-a456-426655440000' in false_positives assert '199.199.178.199' in secrets def test_calculate_shannon_entropy(self): test_string = 'SADE' entropy = self.validator.calculate_shannon_entropy(test_string) assert entropy == 2.0 def test_get_packs_white_list(self): final_white_list, ioc_white_list, files_while_list = \ self.validator.get_packs_white_list(self.TEST_WHITELIST_FILE_PACKS) assert ioc_white_list == [] assert files_while_list == [] assert final_white_list == ['boop', 'sade', 'sade.txt', 'sade@sade.sade', ''] def test_get_generic_white_list(self): final_white_list, ioc_white_list, files_while_list = \ self.validator.get_generic_white_list(self.TEST_WHITELIST_FILE) assert ioc_white_list == ['sade@sade.sade'] assert files_while_list == ['sade.txt'] assert final_white_list == ['sade@sade.sade', 'aboop', 'asade'] def test_remove_false_positives(self): line = '[I AM MISTER MEESEEKS LOOK AT ME] sade' line = self.validator.remove_false_positives(line) assert line == ' sade' def test_is_secrets_disabled(self): line1 = 'disable-secrets-detectio", https://github.com/den4uk/jsonextra,b548d607e8f196e1e1c456666e4f15f579762ecf,tests/test_jsonextra.py::test_disable_rex,tests/test_jsonextra.py,NIO,flaky,InspiredAFix,https://github.com/den4uk/jsonextra/pull/10,classify;root_cause,"import pytest import io import uuid import secrets import datetime import jsonextra dict_flat = { 'id': uuid.UUID('d05c6319-0944-4dd6-819f-3a2dc6f7a3c2'), 'town': 'Hill Valley', 'episode': 2, 'date': datetime.date(2015, 10, 21), 'datetime': datetime.datetime(2019, 6, 14, 20, 43, 53, 207572), 'time': datetime.time(12, 34, 56), 'secret': b'\xd6aO\x1d\xd71Y\x05', 'anone': None, 'watch_again': True, } json_flat = '''{ ""id"": ""d05c6319-0944-4dd6-819f-3a2dc6f7a3c2"", ""town"": ""Hill Valley"", ""episode"": 2, ""date"": ""2015-10-21"", ""datetime"": ""2019-06-14T20:43:53.207572"", ""time"": ""12:34:56"", ""secret"": ""base64:1mFPHdcxWQU="", ""anone"": null, ""watch_again"": true }''' dict_nested = { 'events': { 'admin': uuid.UUID('c05c6319-0944-4dd6-819f-3a2dc6f7a3c2'), 'dates': [ datetime.date(2016, 11, 22), datetime.date(2017, 12, 23) ], 'uids': { 'other': [ uuid.UUID('b05c6319-0944-4dd6-819f-3a2dc6f7a3c2'), uuid.UUID('a05c6319-0944-4dd6-819f-3a2dc6f7a3c2') ], 'more': [ { 'a': datetime.date(2018, 1, 24) }, { 'b': datetime.date(2019, 2, 25) } ] } } } json_nested = '''{ ""events"": { ""admin"": ""c05c6319-0944-4dd6-819f-3a2dc6f7a3c2"", ""dates"": [ ""2016-11-22"", ""2017-12-23"" ], ""uids"": { ""other"": [ ""b05c6319-0944-4dd6-819f-3a2dc6f7a3c2"", ""a05c6319-0944-4dd6-819f-3a2dc6f7a3c2"" ], ""more"": [ { ""a"": ""2018-01-24"" }, { ""b"": ""2019-02-25"" } ] } } }''' @pytest.mark.parametrize('py_obj, json_obj', [ (dict_flat, json_flat), (dict_nested, json_nested), ]) def test_load(py_obj, json_obj): i = io.StringIO(json_obj) assert jsonextra.load(i) == py_obj @pytest.mark.parametrize('py_obj, json_obj', [ (dict_flat, json_flat), (dict_nested, json_nested), ]) def test_dump(py_obj, json_obj): i = io.StringIO() jsonextra.dump(py_obj, i, indent=2) assert i.getvalue() == json_obj many_list = [ ({'x': 'foo'}, '{""x"": ""foo""}'), ({'x': 2}, '{""x"": 2}'), ({'x': True}, '{""x"": true}'), ({'x': None}, '{""x"": null}'), ({'x': uuid.UUID('98f395f2-6ecb-46d8-98e4-926b8dfdd070')}, '{""x"": ""98f395f2-6ecb-46d8-98e4-926b8dfdd070""}'), ({'x': datetime.date(1991, 2, 16)}, '{""x"": ""1991-02-16""}'), ({'x': datetime.datetime(2001, 12, 1, 14, 58, 17)}, '{""x"": ""2001-12-01T14:58:17""}'), ({'x': datetime.datetime(2001, 12, 1, 14, 58, 17, 123456)}, '{""x"": ""2001-12-01T14:58:17.123456""}'), ({'x': datetime.time(9, 12, 4)}, '{""x"": ""09:12:04""}'), ({'x': datetime.time(23, 52, 43)}, '{""x"": ""23:52:43""}'), ({'x': datetime.time(0)}, '{""x"": ""00:00:00""}'), ({'x': datetime.time(0, 1, 0, 1001)}, '{""x"": ""00:01:00.001001""}'), ({'x': b'hello'}, '{""x"": ""base64:aGVsbG8=""}'), ({'x': b''}, '{""x"": ""base64:""}'), ] @pytest.mark.parametrize('py_obj, json_obj', many_list) def test_dumps_many(py_obj, json_obj): assert jsonextra.dumps(py_obj) == json_obj @pytest.mark.parametrize('py_obj, json_obj', many_list) def test_loads_many(py_obj, json_obj): assert jsonextra.loads(json_obj) == py_obj odd_cases = [ ({'x': datetime.datetime(2019, 6, 16, 13, 31, 37)}, '{""x"": ""2019-06-16 13:31:37""}'), # iso8601 without the `T` ({'x': datetime.datetime(2019, 6, 16, 13, 31, 37, 6399)}, '{""x"": ""2019-06-16 13:31:37.006399""}'), # iso8601 without the `T` ({'x': '24:23:22'}, '{""x"": ""24:23:22""}'), # Incorrect time ({'x': '2020-12-32'}, '{""x"": ""2020-12-32""}'), # Incorrect date ({'x': '2019-13-01 25:64:02'}, '{""x"": ""2019-13-01 25:64:02""}'), # Incorrect date/time ({'x': '2019-06-23 23:48:47 with some text'}, '{""x"": ""2019-06-23 23:48:47 with some text""}'), # Includes tailing text ({'x': '00000000-0000-0000-0000-000000000000'}, '{""x"": ""00000000-0000-0000-0000-000000000000""}'), # Not correctly structured guid ({'x': uuid.UUID('98f395f2-6ecb-46d8-98e4-926b8dfdd070')}, '{""x"": ""98F395F2-6ECB-46D8-98E4-926B8DFDD070""}'), # Uppercase ({'x': uuid.UUID('98f395f2-6ecb-46d8-98e4-926b8dfdd070')}, '{""x"": ""98f395f26ecb46d898e4926b8dfdd070""}'), # No dashes, but is valid ({'x': uuid.UUID('98f395f2-6ecb-46d8-98e4-926b8dfdd070')}, '{""x"": ""98F395F26ECB46D898E4926B8DFDD070""}'), # As above, uppercase ({'x': uuid.UUID('98f395f2-6ecb-46d8-98e4-926b8dfdd070')}, '{""x"": ""98F395F26ecb46d898e4926b8DFDD070""}'), # As above, mixed case ({'x': 'base64:Oly==='}, '{""x"": ""base64:Oly===""}'), # invalid base64 string (too much padding) ] @pytest.mark.parametrize('py_obj, json_obj', odd_cases) def test_loads_odd_cases(py_obj, json_obj): assert jsonextra.loads(json_obj) == py_obj def test_random_bytes(): for n in range(128): my_obj = {'x': secrets.token_bytes(n)} serialized = jsonextra.dumps(my_obj) assert jsonextra.loads(serialized) == my_obj def test_disable_rex(): jsonextra.enable_rex('date_rex', r'^\d{4}\-[01]\d\-[0-3]\d$') assert jsonextra.loads('{""x"": ""1991-02-16""}') == {'x': datetime.date(1991, 2, 16)} jsonextra.disable_rex('date_rex') assert jsonextra.loads('{""x"": ""1991-02-16""}') == {'x': '1991-02-16'} ", https://github.com/den4uk/jsonextra,e82c2e873b4d9c8105fd4e5ed43e7fae4fe6bc2c,tests/test_jsonextra.py::test_disable_rex,tests/test_jsonextra.py,NIO,flaky,InspiredAFix,https://github.com/den4uk/jsonextra/pull/9,classify;root_cause,"import pytest import io import uuid import secrets import datetime import jsonextra dict_flat = { 'id': uuid.UUID('d05c6319-0944-4dd6-819f-3a2dc6f7a3c2'), 'town': 'Hill Valley', 'episode': 2, 'date': datetime.date(2015, 10, 21), 'datetime': datetime.datetime(2019, 6, 14, 20, 43, 53, 207572), 'time': datetime.time(12, 34, 56), 'secret': b'\xd6aO\x1d\xd71Y\x05', 'anone': None, 'watch_again': True, } json_flat = '''{ ""id"": ""d05c6319-0944-4dd6-819f-3a2dc6f7a3c2"", ""town"": ""Hill Valley"", ""episode"": 2, ""date"": ""2015-10-21"", ""datetime"": ""2019-06-14T20:43:53.207572"", ""time"": ""12:34:56"", ""secret"": ""base64:1mFPHdcxWQU="", ""anone"": null, ""watch_again"": true }''' dict_nested = { 'events': { 'admin': uuid.UUID('c05c6319-0944-4dd6-819f-3a2dc6f7a3c2'), 'dates': [ datetime.date(2016, 11, 22), datetime.date(2017, 12, 23) ], 'uids': { 'other': [ uuid.UUID('b05c6319-0944-4dd6-819f-3a2dc6f7a3c2'), uuid.UUID('a05c6319-0944-4dd6-819f-3a2dc6f7a3c2') ], 'more': [ { 'a': datetime.date(2018, 1, 24) }, { 'b': datetime.date(2019, 2, 25) } ] } } } json_nested = '''{ ""events"": { ""admin"": ""c05c6319-0944-4dd6-819f-3a2dc6f7a3c2"", ""dates"": [ ""2016-11-22"", ""2017-12-23"" ], ""uids"": { ""other"": [ ""b05c6319-0944-4dd6-819f-3a2dc6f7a3c2"", ""a05c6319-0944-4dd6-819f-3a2dc6f7a3c2"" ], ""more"": [ { ""a"": ""2018-01-24"" }, { ""b"": ""2019-02-25"" } ] } } }''' @pytest.mark.parametrize('py_obj, json_obj', [ (dict_flat, json_flat), (dict_nested, json_nested), ]) def test_load(py_obj, json_obj): i = io.StringIO(json_obj) assert jsonextra.load(i) == py_obj @pytest.mark.parametrize('py_obj, json_obj', [ (dict_flat, json_flat), (dict_nested, json_nested), ]) def test_dump(py_obj, json_obj): i = io.StringIO() jsonextra.dump(py_obj, i, indent=2) assert i.getvalue() == json_obj many_list = [ ({'x': 'foo'}, '{""x"": ""foo""}'), ({'x': 2}, '{""x"": 2}'), ({'x': True}, '{""x"": true}'), ({'x': None}, '{""x"": null}'), ({'x': uuid.UUID('98f395f2-6ecb-46d8-98e4-926b8dfdd070')}, '{""x"": ""98f395f2-6ecb-46d8-98e4-926b8dfdd070""}'), ({'x': datetime.date(1991, 2, 16)}, '{""x"": ""1991-02-16""}'), ({'x': datetime.datetime(2001, 12, 1, 14, 58, 17)}, '{""x"": ""2001-12-01T14:58:17""}'), ({'x': datetime.datetime(2001, 12, 1, 14, 58, 17, 123456)}, '{""x"": ""2001-12-01T14:58:17.123456""}'), ({'x': datetime.time(9, 12, 4)}, '{""x"": ""09:12:04""}'), ({'x': datetime.time(23, 52, 43)}, '{""x"": ""23:52:43""}'), ({'x': datetime.time(0)}, '{""x"": ""00:00:00""}'), ({'x': datetime.time(0, 1, 0, 1001)}, '{""x"": ""00:01:00.001001""}'), ({'x': b'hello'}, '{""x"": ""base64:aGVsbG8=""}'), ({'x': b''}, '{""x"": ""base64:""}'), ] @pytest.mark.parametrize('py_obj, json_obj', many_list) def test_dumps_many(py_obj, json_obj): assert jsonextra.dumps(py_obj) == json_obj @pytest.mark.parametrize('py_obj, json_obj', many_list) def test_loads_many(py_obj, json_obj): assert jsonextra.loads(json_obj) == py_obj odd_cases = [ ({'x': datetime.datetime(2019, 6, 16, 13, 31, 37)}, '{""x"": ""2019-06-16 13:31:37""}'), # iso8601 without the `T` ({'x': datetime.datetime(2019, 6, 16, 13, 31, 37, 6399)}, '{""x"": ""2019-06-16 13:31:37.006399""}'), # iso8601 without the `T` ({'x': '24:23:22'}, '{""x"": ""24:23:22""}'), # Incorrect time ({'x': '2020-12-32'}, '{""x"": ""2020-12-32""}'), # Incorrect date ({'x': '2019-13-01 25:64:02'}, '{""x"": ""2019-13-01 25:64:02""}'), # Incorrect date/time ({'x': '2019-06-23 23:48:47 with some text'}, '{""x"": ""2019-06-23 23:48:47 with some text""}'), # Includes tailing text ({'x': '00000000-0000-0000-0000-000000000000'}, '{""x"": ""00000000-0000-0000-0000-000000000000""}'), # Not correctly structured guid ({'x': uuid.UUID('98f395f2-6ecb-46d8-98e4-926b8dfdd070')}, '{""x"": ""98F395F2-6ECB-46D8-98E4-926B8DFDD070""}'), # Uppercase ({'x': uuid.UUID('98f395f2-6ecb-46d8-98e4-926b8dfdd070')}, '{""x"": ""98f395f26ecb46d898e4926b8dfdd070""}'), # No dashes, but is valid ({'x': uuid.UUID('98f395f2-6ecb-46d8-98e4-926b8dfdd070')}, '{""x"": ""98F395F26ECB46D898E4926B8DFDD070""}'), # As above, uppercase ({'x': uuid.UUID('98f395f2-6ecb-46d8-98e4-926b8dfdd070')}, '{""x"": ""98F395F26ecb46d898e4926b8DFDD070""}'), # As above, mixed case ({'x': 'base64:Oly==='}, '{""x"": ""base64:Oly===""}'), # invalid base64 string (too much padding) ] @pytest.mark.parametrize('py_obj, json_obj', odd_cases) def test_loads_odd_cases(py_obj, json_obj): assert jsonextra.loads(json_obj) == py_obj def test_random_bytes(): for n in range(128): my_obj = {'x': secrets.token_bytes(n)} serialized = jsonextra.dumps(my_obj) assert jsonextra.loads(serialized) == my_obj def test_disable_rex(): assert jsonextra.loads('{""x"": ""1991-02-16""}') == {'x': datetime.date(1991, 2, 16)} jsonextra.disable_rex('date_rex') assert jsonextra.loads('{""x"": ""1991-02-16""}') == {'x': '1991-02-16'} ", https://github.com/DevKeh/redisqueue,feac4dfc30837e0ab1a55a8479443ea74b2793f2,tests/test_redisqueue.py::test_mock_queue_connection,tests/test_redisqueue.py,NIO,flaky,Opened,https://github.com/jkehler/redisqueue/pull/3,classify;root_cause,"# -*- coding: utf-8 -*- # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the ""Software""), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. __author__ = 'Jeff Kehler' do_live_testing = False live_host = 'localhost' live_port = 6379 live_pass = None from redisqueue import AbstractTask, QueueNotConnectedError from redisqueue import TaskAlreadyInQueueException, RedisQueue from redisqueue.mock import MockRedisQueue import pytest import hashlib class MockTask(AbstractTask): def __init__(self, mock_data=None, unique=False): super(MockTask, self).__init__(mock_data, unique) def unique_hash(self): return hashlib.sha1(self.uri.encode('utf-8')).hexdigest() mock_queue = MockRedisQueue('mock_queue', MockTask) def test_mock_queue_connection(): assert mock_queue.connected is False with pytest.raises(QueueNotConnectedError): mock_queue.put(MockTask()) with pytest.raises(QueueNotConnectedError): mock_queue.get() with pytest.raises(QueueNotConnectedError): mock_queue.qsize() mock_queue.connect() assert mock_queue.connected is True def test_mock_queue_put_get(): assert mock_queue.qsize() == 0 task = MockTask() task.uri = ""my_uri"" mock_queue.put(task) assert mock_queue.qsize() == 1 returned_task = mock_queue.get() assert isinstance(returned_task, MockTask) is True assert returned_task.uid == task.uid assert returned_task.unique_hash() == task.unique_hash() assert returned_task.uri == ""my_uri"" assert mock_queue.qsize() == 0 def test_mock_queue_unique(): assert mock_queue.qsize() == 0 task = MockTask() task.unique = True task.uri = 'unique1' task2 = MockTask() task2.unique = True task2.uri = 'unique1' assert task.unique_hash() == task2.unique_hash() mock_queue.put(task) with pytest.raises(TaskAlreadyInQueueException): mock_queue.put(task2) task3 = MockTask() task3.unique = True task3.uri = 'unique2' mock_queue.put(task3) assert mock_queue.qsize() == 2 def test_mock_queue_get_put_same_task(): mock_queue.clear() task = MockTask() task.test = 'my_test' mock_queue.put(task) assert mock_queue.qsize() == 1 my_task = mock_queue.get() assert my_task.test == 'my_test' assert mock_queue.qsize() == 0 mock_queue.put(my_task) assert mock_queue.qsize() == 1 @pytest.mark.skipif(do_live_testing is False, reason='Live Redis testing not enabled.') def test_live_queue(): live_queue = RedisQueue(""test_queue"", MockTask, namespace=""pytest"") assert live_queue.connected is False task = MockTask() task2 = MockTask() task.uri = 'thisIsUnique' task2.uri = 'thisIsUnique' assert live_queue.connect(host=live_host, port=live_port, password=live_pass) is True assert live_queue.connected is True live_queue.clear() assert live_queue.qsize == 0 live_queue.put(task) assert live_queue.qsize == 1 live_queue.put(task2) assert live_queue.qsize == 2 new_task = live_queue.get() assert isinstance(new_task, MockTask) assert new_task.uid == task.uid assert new_task.uri == 'thisIsUnique' live_queue.clear() task.unique = True task2.unique = True assert task.unique_hash() == task2.unique_hash() live_queue.put(task) with pytest.raises(TaskAlreadyInQueueException): live_queue.put(task2) assert live_queue.qsize == 1 live_queue.clear() # test getting and putting the same task into the queue assert live_queue.qsize == 0 live_queue.put(task) my_task = live_queue.get() live_queue.put(my_task) assert live_queue.qsize == 1 ", https://github.com/DevKeh/redisqueue,feac4dfc30837e0ab1a55a8479443ea74b2793f2,tests/test_redisqueue.py::test_mock_queue_get_put_same_task,tests/test_redisqueue.py,OD-Brit,flaky,Opened,https://github.com/jkehler/redisqueue/pull/4,classify;root_cause,"# -*- coding: utf-8 -*- # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the ""Software""), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. __author__ = 'Jeff Kehler' do_live_testing = False live_host = 'localhost' live_port = 6379 live_pass = None from redisqueue import AbstractTask, QueueNotConnectedError from redisqueue import TaskAlreadyInQueueException, RedisQueue from redisqueue.mock import MockRedisQueue import pytest import hashlib class MockTask(AbstractTask): def __init__(self, mock_data=None, unique=False): super(MockTask, self).__init__(mock_data, unique) def unique_hash(self): return hashlib.sha1(self.uri.encode('utf-8')).hexdigest() mock_queue = MockRedisQueue('mock_queue', MockTask) def test_mock_queue_connection(): assert mock_queue.connected is False with pytest.raises(QueueNotConnectedError): mock_queue.put(MockTask()) with pytest.raises(QueueNotConnectedError): mock_queue.get() with pytest.raises(QueueNotConnectedError): mock_queue.qsize() mock_queue.connect() assert mock_queue.connected is True def test_mock_queue_put_get(): assert mock_queue.qsize() == 0 task = MockTask() task.uri = ""my_uri"" mock_queue.put(task) assert mock_queue.qsize() == 1 returned_task = mock_queue.get() assert isinstance(returned_task, MockTask) is True assert returned_task.uid == task.uid assert returned_task.unique_hash() == task.unique_hash() assert returned_task.uri == ""my_uri"" assert mock_queue.qsize() == 0 def test_mock_queue_unique(): assert mock_queue.qsize() == 0 task = MockTask() task.unique = True task.uri = 'unique1' task2 = MockTask() task2.unique = True task2.uri = 'unique1' assert task.unique_hash() == task2.unique_hash() mock_queue.put(task) with pytest.raises(TaskAlreadyInQueueException): mock_queue.put(task2) task3 = MockTask() task3.unique = True task3.uri = 'unique2' mock_queue.put(task3) assert mock_queue.qsize() == 2 def test_mock_queue_get_put_same_task(): mock_queue.clear() task = MockTask() task.test = 'my_test' mock_queue.put(task) assert mock_queue.qsize() == 1 my_task = mock_queue.get() assert my_task.test == 'my_test' assert mock_queue.qsize() == 0 mock_queue.put(my_task) assert mock_queue.qsize() == 1 @pytest.mark.skipif(do_live_testing is False, reason='Live Redis testing not enabled.') def test_live_queue(): live_queue = RedisQueue(""test_queue"", MockTask, namespace=""pytest"") assert live_queue.connected is False task = MockTask() task2 = MockTask() task.uri = 'thisIsUnique' task2.uri = 'thisIsUnique' assert live_queue.connect(host=live_host, port=live_port, password=live_pass) is True assert live_queue.connected is True live_queue.clear() assert live_queue.qsize == 0 live_queue.put(task) assert live_queue.qsize == 1 live_queue.put(task2) assert live_queue.qsize == 2 new_task = live_queue.get() assert isinstance(new_task, MockTask) assert new_task.uid == task.uid assert new_task.uri == 'thisIsUnique' live_queue.clear() task.unique = True task2.unique = True assert task.unique_hash() == task2.unique_hash() live_queue.put(task) with pytest.raises(TaskAlreadyInQueueException): live_queue.put(task2) assert live_queue.qsize == 1 live_queue.clear() # test getting and putting the same task into the queue assert live_queue.qsize == 0 live_queue.put(task) my_task = live_queue.get() live_queue.put(my_task) assert live_queue.qsize == 1 ", https://github.com/DevKeh/redisqueue,feac4dfc30837e0ab1a55a8479443ea74b2793f2,tests/test_redisqueue.py::test_mock_queue_put_get,tests/test_redisqueue.py,OD-Brit,flaky,Opened,https://github.com/jkehler/redisqueue/pull/4,classify;root_cause,"# -*- coding: utf-8 -*- # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the ""Software""), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. __author__ = 'Jeff Kehler' do_live_testing = False live_host = 'localhost' live_port = 6379 live_pass = None from redisqueue import AbstractTask, QueueNotConnectedError from redisqueue import TaskAlreadyInQueueException, RedisQueue from redisqueue.mock import MockRedisQueue import pytest import hashlib class MockTask(AbstractTask): def __init__(self, mock_data=None, unique=False): super(MockTask, self).__init__(mock_data, unique) def unique_hash(self): return hashlib.sha1(self.uri.encode('utf-8')).hexdigest() mock_queue = MockRedisQueue('mock_queue', MockTask) def test_mock_queue_connection(): assert mock_queue.connected is False with pytest.raises(QueueNotConnectedError): mock_queue.put(MockTask()) with pytest.raises(QueueNotConnectedError): mock_queue.get() with pytest.raises(QueueNotConnectedError): mock_queue.qsize() mock_queue.connect() assert mock_queue.connected is True def test_mock_queue_put_get(): assert mock_queue.qsize() == 0 task = MockTask() task.uri = ""my_uri"" mock_queue.put(task) assert mock_queue.qsize() == 1 returned_task = mock_queue.get() assert isinstance(returned_task, MockTask) is True assert returned_task.uid == task.uid assert returned_task.unique_hash() == task.unique_hash() assert returned_task.uri == ""my_uri"" assert mock_queue.qsize() == 0 def test_mock_queue_unique(): assert mock_queue.qsize() == 0 task = MockTask() task.unique = True task.uri = 'unique1' task2 = MockTask() task2.unique = True task2.uri = 'unique1' assert task.unique_hash() == task2.unique_hash() mock_queue.put(task) with pytest.raises(TaskAlreadyInQueueException): mock_queue.put(task2) task3 = MockTask() task3.unique = True task3.uri = 'unique2' mock_queue.put(task3) assert mock_queue.qsize() == 2 def test_mock_queue_get_put_same_task(): mock_queue.clear() task = MockTask() task.test = 'my_test' mock_queue.put(task) assert mock_queue.qsize() == 1 my_task = mock_queue.get() assert my_task.test == 'my_test' assert mock_queue.qsize() == 0 mock_queue.put(my_task) assert mock_queue.qsize() == 1 @pytest.mark.skipif(do_live_testing is False, reason='Live Redis testing not enabled.') def test_live_queue(): live_queue = RedisQueue(""test_queue"", MockTask, namespace=""pytest"") assert live_queue.connected is False task = MockTask() task2 = MockTask() task.uri = 'thisIsUnique' task2.uri = 'thisIsUnique' assert live_queue.connect(host=live_host, port=live_port, password=live_pass) is True assert live_queue.connected is True live_queue.clear() assert live_queue.qsize == 0 live_queue.put(task) assert live_queue.qsize == 1 live_queue.put(task2) assert live_queue.qsize == 2 new_task = live_queue.get() assert isinstance(new_task, MockTask) assert new_task.uid == task.uid assert new_task.uri == 'thisIsUnique' live_queue.clear() task.unique = True task2.unique = True assert task.unique_hash() == task2.unique_hash() live_queue.put(task) with pytest.raises(TaskAlreadyInQueueException): live_queue.put(task2) assert live_queue.qsize == 1 live_queue.clear() # test getting and putting the same task into the queue assert live_queue.qsize == 0 live_queue.put(task) my_task = live_queue.get() live_queue.put(my_task) assert live_queue.qsize == 1 ", https://github.com/DevKeh/redisqueue,feac4dfc30837e0ab1a55a8479443ea74b2793f2,tests/test_redisqueue.py::test_mock_queue_unique,tests/test_redisqueue.py,OD-Brit,flaky,Opened,https://github.com/jkehler/redisqueue/pull/4,classify;root_cause,"# -*- coding: utf-8 -*- # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the ""Software""), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. __author__ = 'Jeff Kehler' do_live_testing = False live_host = 'localhost' live_port = 6379 live_pass = None from redisqueue import AbstractTask, QueueNotConnectedError from redisqueue import TaskAlreadyInQueueException, RedisQueue from redisqueue.mock import MockRedisQueue import pytest import hashlib class MockTask(AbstractTask): def __init__(self, mock_data=None, unique=False): super(MockTask, self).__init__(mock_data, unique) def unique_hash(self): return hashlib.sha1(self.uri.encode('utf-8')).hexdigest() mock_queue = MockRedisQueue('mock_queue', MockTask) def test_mock_queue_connection(): assert mock_queue.connected is False with pytest.raises(QueueNotConnectedError): mock_queue.put(MockTask()) with pytest.raises(QueueNotConnectedError): mock_queue.get() with pytest.raises(QueueNotConnectedError): mock_queue.qsize() mock_queue.connect() assert mock_queue.connected is True def test_mock_queue_put_get(): assert mock_queue.qsize() == 0 task = MockTask() task.uri = ""my_uri"" mock_queue.put(task) assert mock_queue.qsize() == 1 returned_task = mock_queue.get() assert isinstance(returned_task, MockTask) is True assert returned_task.uid == task.uid assert returned_task.unique_hash() == task.unique_hash() assert returned_task.uri == ""my_uri"" assert mock_queue.qsize() == 0 def test_mock_queue_unique(): assert mock_queue.qsize() == 0 task = MockTask() task.unique = True task.uri = 'unique1' task2 = MockTask() task2.unique = True task2.uri = 'unique1' assert task.unique_hash() == task2.unique_hash() mock_queue.put(task) with pytest.raises(TaskAlreadyInQueueException): mock_queue.put(task2) task3 = MockTask() task3.unique = True task3.uri = 'unique2' mock_queue.put(task3) assert mock_queue.qsize() == 2 def test_mock_queue_get_put_same_task(): mock_queue.clear() task = MockTask() task.test = 'my_test' mock_queue.put(task) assert mock_queue.qsize() == 1 my_task = mock_queue.get() assert my_task.test == 'my_test' assert mock_queue.qsize() == 0 mock_queue.put(my_task) assert mock_queue.qsize() == 1 @pytest.mark.skipif(do_live_testing is False, reason='Live Redis testing not enabled.') def test_live_queue(): live_queue = RedisQueue(""test_queue"", MockTask, namespace=""pytest"") assert live_queue.connected is False task = MockTask() task2 = MockTask() task.uri = 'thisIsUnique' task2.uri = 'thisIsUnique' assert live_queue.connect(host=live_host, port=live_port, password=live_pass) is True assert live_queue.connected is True live_queue.clear() assert live_queue.qsize == 0 live_queue.put(task) assert live_queue.qsize == 1 live_queue.put(task2) assert live_queue.qsize == 2 new_task = live_queue.get() assert isinstance(new_task, MockTask) assert new_task.uid == task.uid assert new_task.uri == 'thisIsUnique' live_queue.clear() task.unique = True task2.unique = True assert task.unique_hash() == task2.unique_hash() live_queue.put(task) with pytest.raises(TaskAlreadyInQueueException): live_queue.put(task2) assert live_queue.qsize == 1 live_queue.clear() # test getting and putting the same task into the queue assert live_queue.qsize == 0 live_queue.put(task) my_task = live_queue.get() live_queue.put(my_task) assert live_queue.qsize == 1 ", https://github.com/dgellerup/laminar,74258f6e6cc8b3b4e9eaf7cc522192a353a3b45e,test/test_laminar.py::test_init_my_lam,test/test_laminar.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from multiprocessing import Queue, Process, cpu_count from context import laminar from context import laminar_examples as le def test_converter(): queue = Queue() def sum_func(sum_list): return sum(sum_list) laminar.__converter(""test12"", sum_func, [2, 4, 6, 8], queue, [], {}) q = queue.get() assert q[0] == ""test12"" assert q[1] == 20 def add_to_list(ls): return ls.append('caboose') laminar.__converter(""testfail"", add_to_list, 7, queue, [], {}) q = queue.get() assert q[0] == ""testfail"" assert str(q[1]).startswith('Traceback') def test_iter_flow(): result = laminar.iter_flow(le.single_total, le.laminar_df['Col1'], cores=8) assert result == { 'data[0-5]': 17, 'data[12-17]': 60, 'data[18-23]': 86, 'data[24-29]': 115, 'data[30-34]': 105, 'data[35-39]': 120, 'data[40-44]': 135, 'data[6-11]': 37, } def sum_func(sum_list): return sum(sum_list) short_list = [1, 2, 3] result = laminar.iter_flow(sum_func, short_list, cores=4) assert result == { 'data[2-2]': 3, 'data[0-0]': 1, 'data[1-1]': 2 } result = laminar.iter_flow(sum_func, short_list, cores=9) assert result == { 'data[2-2]': 3, 'data[0-0]': 1, 'data[1-1]': 2 } result = laminar.iter_flow(le.single_total, short_list, True, cores=1) assert result.get('data[0-2]') == 4 result = laminar.iter_flow(le.multi_tally, le.laminar_df, cores=8) assert result.get('data[0-5]') == 3 result = laminar.iter_flow(lambda x: sum(x), []) assert result == { 'data[empty]': None } def test_list_flow(): result = laminar.list_flow(le.single_total, [le.laminar_df[col] for col in le.laminar_df.columns]) assert result == { 'data_position_0': 675, 'data_position_1': 1800, 'data_position_2': 2925, } def sum_func(sum_list): return sum(sum_list) multi_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] result = laminar.list_flow(sum_func, multi_list, cores=9) assert result == { 'data_position_0': 6, 'data_position_1': 15, 'data_position_2': 24 } def test_init_my_lam(my_lam): assert my_lam.cores == 2 assert my_lam.results == {} def test_my_lam(my_lam): def square(ls): result = [x*x for x in ls] return result def cube(ls): result = [x*x*x for x in ls] return result my_lam.add_process('square1', square, [0, 1, 2, 3]) assert len(my_lam._processes) == 1 my_lam.add_process('cube1', cube, [0, 1, 2, 3]) assert len(my_lam._processes) == 2 my_lam.drop_process('cube1') assert len(my_lam._processes) == 1 my_lam.add_process('cube2', cube, [1, 1, 2, 2]) def sum_func(sum_list): return sum(sum_list) my_lam._Laminar__converter(""test12"", sum_func, [2, 4, 6, 8], [], {}) q = my_lam._queue.get() assert q[0] == ""test12"" assert q[1] == 20 my_lam.add_process('cube3', cube, [1, 2, 1, 2]) assert my_lam._processes.get('cube3') != None proc = my_lam.show_processes() assert proc == None my_lam.launch_processes() result = my_lam.get_results() assert result == {'cube2': [1, 1, 8, 8], 'cube3': [1, 8, 1, 8], 'square1': [0, 1, 4, 9]} assert len(my_lam._processes) == 0 my_lam.add_process('square2', square, [0, 1, 2, 3]) my_lam.add_process('cube3', cube, [1, 1, 2, 2]) assert len(my_lam._processes) == 2 my_lam.clear_processes() assert len(my_lam._processes) == 0 my_lam.add_process('square2', square, [0, 1, 2, 3]) my_lam.launch_processes() assert my_lam.get_results().get('square2') == [0, 1, 4, 9] def add_to_list(ls): return ls.append('caboose') my_lam.add_process('fail', add_to_list, 7) my_lam.launch_processes() result = my_lam.get_results() assert str(result.get('fail')).startswith('Traceback') ", https://github.com/didix21/mdutils,2ef859ecc1e4f1a3e24f4150c96f61180bc57ceb,tests/test_mdutils.py::TestMdUtils::test_create_md_file,tests/test_mdutils.py,OD-Vic,flaky,Accepted,https://github.com/didix21/mdutils/pull/76,classify;root_cause,"# Python # # This module implements tests for MdUtils class. # # This file is part of mdutils. https://github.com/didix21/mdutils # # MIT License: (C) 2018 Dídac Coll from unittest import TestCase from mdutils.mdutils import MdUtils from mdutils.fileutils import MarkDownFile from pathlib import Path import os import re class TestMdUtils(TestCase): def setUp(self) -> None: self.expected_list = ""\n\n\n"" \ ""\n- Item 1\n"" \ ""- Item 2\n"" \ ""- Item 3\n"" \ ""- Item 4\n"" \ "" - Item 4.1\n"" \ "" - Item 4.2\n"" \ "" - Item 4.2.1\n"" \ "" - Item 4.2.2\n"" \ "" - Item 4.3\n"" \ "" - Item 4.3.1\n"" \ ""- Item 5\n"" self.complex_items = [""Item 1"", ""Item 2"", ""Item 3"", ""Item 4"", [""Item 4.1"", ""Item 4.2"", [""Item 4.2.1"", ""Item 4.2.2""], ""Item 4.3"", [""Item 4.3.1""]], ""Item 5""] def tearDown(self): md_file = Path('Test_file.md') if md_file.is_file(): os.remove('Test_file.md') def test_create_md_file(self): md_file = MdUtils(""Test_file"") md_file.create_md_file() md_file_expect = Path('Test_file.md') if md_file_expect.is_file(): os.remove('Test_file.md') pass else: self.fail() def test_new_header(self): file_name = 'Test_file' md_file = MdUtils(file_name) string_headers_expected = ""\n# Header 0\n\n## Header 1\n\n### Header 2\n\n#### Header 3\n\n"" \ ""##### Header 4\n\n###### Header 5\n"" string_headers = """" for x in range(6): string_headers += md_file.new_header(level=(x + 1), title='Header ' + str(x), style='atx') self.assertEqual(string_headers, string_headers_expected) md_file.create_md_file() file_result = md_file.read_md_file(file_name) self.assertEqual(file_result, '\n\n\n' + string_headers_expected) def test_new_table_of_contents(self): # Create headers level 1 and 2. md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) # Testing Depth 1 table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=1) table_of_content_expected = table_of_content_title \ + '\n* [' + list_headers[0] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[0].lower().replace(' ', '-')) + ')' \ + '\n* [' + list_headers[2] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[2].lower().replace(' ', '-')) + ')\n' self.assertEqual(table_of_contents_result, table_of_content_expected) # Testing created file md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') # Testing Depth 2 md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=2) table_of_content_expected = table_of_content_title for x in range(len(list_headers)): if x in (0, 2): table_of_content_expected += '\n* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' else: table_of_content_expected += '\n\t* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' table_of_content_expected += '\n' self.assertEqual(table_of_contents_result, table_of_content_expected) md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') def test_new_paragraph(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_paragraph(""This is a new paragraph created using new_paragraph method."") expected_value = '\n\nThis is a new paragraph created using new_paragraph method.' self.assertEqual(created_value, expected_value) def test_new_line(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_line(""This is a new line created using new_line method."") expected_value = ' \nThis is a new line created using new_line method.' self.assertEqual(created_value, expected_value) def test_insert_code(self): md_file = MdUtils(file_name='Test_file') code = (""mdFile.new_header(level=1, title='Atx Header 1')\n"" ""mdFile.new_header(level=2, title='Atx Header 2')\n"" ""mdFile.new_header(level=3, title='Atx Header 3')\n"" ""mdFile.new_header(level=4, title='Atx Header 4')\n"" ""mdFile.new_header(level=5, title='Atx Header 5')\n"" ""mdFile.new_header(level=6, title='Atx Header 6')\n"") expects = '\n\n```\n' + code + '\n```' self.assertEqual(md_file.insert_code(code), expects) language = 'python' expects = '\n\n```' + language + '\n' + code + '\n```' self.assertEqual(md_file.insert_code(code, language), expects) def test_new_inline_link(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=""https://github.com/didix21/mdutils"", text=""mdutils library"") expected_value = '[mdutils library](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_text_empty(self): link = ""https://github.com/didix21/mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=link, text=link) expected_value = '[https://github.com/didix21/mdutils](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_empty(self): md_file = MdUtils(file_name=""Test_file"", title="""") try: md_file.new_inline_link() except TypeError: return self.fail() def test_new_inline_link_bold_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[**' + text + '**](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""b"") self.assertEqual(expected_value, created_value) def test_new_inline_link_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[*' + text + '*](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""i"") self.assertEqual(expected_value, created_value) def test_new_inline_link_code_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[``' + text + '``](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""c"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[***' + text + '***](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""bi"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_code_format(self): link = ""https://github.com/didix21/mdutils"" text", https://github.com/didix21/mdutils,2ef859ecc1e4f1a3e24f4150c96f61180bc57ceb,tests/test_mdutils.py::TestMdUtils::test_new_checkbox_checked_list,tests/test_mdutils.py,OD-Vic,flaky,Accepted,https://github.com/didix21/mdutils/pull/76,classify;root_cause,"# Python # # This module implements tests for MdUtils class. # # This file is part of mdutils. https://github.com/didix21/mdutils # # MIT License: (C) 2018 Dídac Coll from unittest import TestCase from mdutils.mdutils import MdUtils from mdutils.fileutils import MarkDownFile from pathlib import Path import os import re class TestMdUtils(TestCase): def setUp(self) -> None: self.expected_list = ""\n\n\n"" \ ""\n- Item 1\n"" \ ""- Item 2\n"" \ ""- Item 3\n"" \ ""- Item 4\n"" \ "" - Item 4.1\n"" \ "" - Item 4.2\n"" \ "" - Item 4.2.1\n"" \ "" - Item 4.2.2\n"" \ "" - Item 4.3\n"" \ "" - Item 4.3.1\n"" \ ""- Item 5\n"" self.complex_items = [""Item 1"", ""Item 2"", ""Item 3"", ""Item 4"", [""Item 4.1"", ""Item 4.2"", [""Item 4.2.1"", ""Item 4.2.2""], ""Item 4.3"", [""Item 4.3.1""]], ""Item 5""] def tearDown(self): md_file = Path('Test_file.md') if md_file.is_file(): os.remove('Test_file.md') def test_create_md_file(self): md_file = MdUtils(""Test_file"") md_file.create_md_file() md_file_expect = Path('Test_file.md') if md_file_expect.is_file(): os.remove('Test_file.md') pass else: self.fail() def test_new_header(self): file_name = 'Test_file' md_file = MdUtils(file_name) string_headers_expected = ""\n# Header 0\n\n## Header 1\n\n### Header 2\n\n#### Header 3\n\n"" \ ""##### Header 4\n\n###### Header 5\n"" string_headers = """" for x in range(6): string_headers += md_file.new_header(level=(x + 1), title='Header ' + str(x), style='atx') self.assertEqual(string_headers, string_headers_expected) md_file.create_md_file() file_result = md_file.read_md_file(file_name) self.assertEqual(file_result, '\n\n\n' + string_headers_expected) def test_new_table_of_contents(self): # Create headers level 1 and 2. md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) # Testing Depth 1 table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=1) table_of_content_expected = table_of_content_title \ + '\n* [' + list_headers[0] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[0].lower().replace(' ', '-')) + ')' \ + '\n* [' + list_headers[2] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[2].lower().replace(' ', '-')) + ')\n' self.assertEqual(table_of_contents_result, table_of_content_expected) # Testing created file md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') # Testing Depth 2 md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=2) table_of_content_expected = table_of_content_title for x in range(len(list_headers)): if x in (0, 2): table_of_content_expected += '\n* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' else: table_of_content_expected += '\n\t* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' table_of_content_expected += '\n' self.assertEqual(table_of_contents_result, table_of_content_expected) md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') def test_new_paragraph(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_paragraph(""This is a new paragraph created using new_paragraph method."") expected_value = '\n\nThis is a new paragraph created using new_paragraph method.' self.assertEqual(created_value, expected_value) def test_new_line(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_line(""This is a new line created using new_line method."") expected_value = ' \nThis is a new line created using new_line method.' self.assertEqual(created_value, expected_value) def test_insert_code(self): md_file = MdUtils(file_name='Test_file') code = (""mdFile.new_header(level=1, title='Atx Header 1')\n"" ""mdFile.new_header(level=2, title='Atx Header 2')\n"" ""mdFile.new_header(level=3, title='Atx Header 3')\n"" ""mdFile.new_header(level=4, title='Atx Header 4')\n"" ""mdFile.new_header(level=5, title='Atx Header 5')\n"" ""mdFile.new_header(level=6, title='Atx Header 6')\n"") expects = '\n\n```\n' + code + '\n```' self.assertEqual(md_file.insert_code(code), expects) language = 'python' expects = '\n\n```' + language + '\n' + code + '\n```' self.assertEqual(md_file.insert_code(code, language), expects) def test_new_inline_link(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=""https://github.com/didix21/mdutils"", text=""mdutils library"") expected_value = '[mdutils library](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_text_empty(self): link = ""https://github.com/didix21/mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=link, text=link) expected_value = '[https://github.com/didix21/mdutils](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_empty(self): md_file = MdUtils(file_name=""Test_file"", title="""") try: md_file.new_inline_link() except TypeError: return self.fail() def test_new_inline_link_bold_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[**' + text + '**](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""b"") self.assertEqual(expected_value, created_value) def test_new_inline_link_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[*' + text + '*](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""i"") self.assertEqual(expected_value, created_value) def test_new_inline_link_code_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[``' + text + '``](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""c"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[***' + text + '***](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""bi"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_code_format(self): link = ""https://github.com/didix21/mdutils"" text", https://github.com/didix21/mdutils,2ef859ecc1e4f1a3e24f4150c96f61180bc57ceb,tests/test_mdutils.py::TestMdUtils::test_new_checkbox_list,tests/test_mdutils.py,OD-Vic,flaky,Accepted,https://github.com/didix21/mdutils/pull/76,classify;root_cause,"# Python # # This module implements tests for MdUtils class. # # This file is part of mdutils. https://github.com/didix21/mdutils # # MIT License: (C) 2018 Dídac Coll from unittest import TestCase from mdutils.mdutils import MdUtils from mdutils.fileutils import MarkDownFile from pathlib import Path import os import re class TestMdUtils(TestCase): def setUp(self) -> None: self.expected_list = ""\n\n\n"" \ ""\n- Item 1\n"" \ ""- Item 2\n"" \ ""- Item 3\n"" \ ""- Item 4\n"" \ "" - Item 4.1\n"" \ "" - Item 4.2\n"" \ "" - Item 4.2.1\n"" \ "" - Item 4.2.2\n"" \ "" - Item 4.3\n"" \ "" - Item 4.3.1\n"" \ ""- Item 5\n"" self.complex_items = [""Item 1"", ""Item 2"", ""Item 3"", ""Item 4"", [""Item 4.1"", ""Item 4.2"", [""Item 4.2.1"", ""Item 4.2.2""], ""Item 4.3"", [""Item 4.3.1""]], ""Item 5""] def tearDown(self): md_file = Path('Test_file.md') if md_file.is_file(): os.remove('Test_file.md') def test_create_md_file(self): md_file = MdUtils(""Test_file"") md_file.create_md_file() md_file_expect = Path('Test_file.md') if md_file_expect.is_file(): os.remove('Test_file.md') pass else: self.fail() def test_new_header(self): file_name = 'Test_file' md_file = MdUtils(file_name) string_headers_expected = ""\n# Header 0\n\n## Header 1\n\n### Header 2\n\n#### Header 3\n\n"" \ ""##### Header 4\n\n###### Header 5\n"" string_headers = """" for x in range(6): string_headers += md_file.new_header(level=(x + 1), title='Header ' + str(x), style='atx') self.assertEqual(string_headers, string_headers_expected) md_file.create_md_file() file_result = md_file.read_md_file(file_name) self.assertEqual(file_result, '\n\n\n' + string_headers_expected) def test_new_table_of_contents(self): # Create headers level 1 and 2. md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) # Testing Depth 1 table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=1) table_of_content_expected = table_of_content_title \ + '\n* [' + list_headers[0] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[0].lower().replace(' ', '-')) + ')' \ + '\n* [' + list_headers[2] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[2].lower().replace(' ', '-')) + ')\n' self.assertEqual(table_of_contents_result, table_of_content_expected) # Testing created file md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') # Testing Depth 2 md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=2) table_of_content_expected = table_of_content_title for x in range(len(list_headers)): if x in (0, 2): table_of_content_expected += '\n* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' else: table_of_content_expected += '\n\t* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' table_of_content_expected += '\n' self.assertEqual(table_of_contents_result, table_of_content_expected) md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') def test_new_paragraph(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_paragraph(""This is a new paragraph created using new_paragraph method."") expected_value = '\n\nThis is a new paragraph created using new_paragraph method.' self.assertEqual(created_value, expected_value) def test_new_line(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_line(""This is a new line created using new_line method."") expected_value = ' \nThis is a new line created using new_line method.' self.assertEqual(created_value, expected_value) def test_insert_code(self): md_file = MdUtils(file_name='Test_file') code = (""mdFile.new_header(level=1, title='Atx Header 1')\n"" ""mdFile.new_header(level=2, title='Atx Header 2')\n"" ""mdFile.new_header(level=3, title='Atx Header 3')\n"" ""mdFile.new_header(level=4, title='Atx Header 4')\n"" ""mdFile.new_header(level=5, title='Atx Header 5')\n"" ""mdFile.new_header(level=6, title='Atx Header 6')\n"") expects = '\n\n```\n' + code + '\n```' self.assertEqual(md_file.insert_code(code), expects) language = 'python' expects = '\n\n```' + language + '\n' + code + '\n```' self.assertEqual(md_file.insert_code(code, language), expects) def test_new_inline_link(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=""https://github.com/didix21/mdutils"", text=""mdutils library"") expected_value = '[mdutils library](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_text_empty(self): link = ""https://github.com/didix21/mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=link, text=link) expected_value = '[https://github.com/didix21/mdutils](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_empty(self): md_file = MdUtils(file_name=""Test_file"", title="""") try: md_file.new_inline_link() except TypeError: return self.fail() def test_new_inline_link_bold_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[**' + text + '**](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""b"") self.assertEqual(expected_value, created_value) def test_new_inline_link_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[*' + text + '*](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""i"") self.assertEqual(expected_value, created_value) def test_new_inline_link_code_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[``' + text + '``](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""c"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[***' + text + '***](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""bi"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_code_format(self): link = ""https://github.com/didix21/mdutils"" text", https://github.com/didix21/mdutils,2ef859ecc1e4f1a3e24f4150c96f61180bc57ceb,tests/test_mdutils.py::TestMdUtils::test_new_header,tests/test_mdutils.py,OD-Vic,flaky,Accepted,https://github.com/didix21/mdutils/pull/76,classify;root_cause,"# Python # # This module implements tests for MdUtils class. # # This file is part of mdutils. https://github.com/didix21/mdutils # # MIT License: (C) 2018 Dídac Coll from unittest import TestCase from mdutils.mdutils import MdUtils from mdutils.fileutils import MarkDownFile from pathlib import Path import os import re class TestMdUtils(TestCase): def setUp(self) -> None: self.expected_list = ""\n\n\n"" \ ""\n- Item 1\n"" \ ""- Item 2\n"" \ ""- Item 3\n"" \ ""- Item 4\n"" \ "" - Item 4.1\n"" \ "" - Item 4.2\n"" \ "" - Item 4.2.1\n"" \ "" - Item 4.2.2\n"" \ "" - Item 4.3\n"" \ "" - Item 4.3.1\n"" \ ""- Item 5\n"" self.complex_items = [""Item 1"", ""Item 2"", ""Item 3"", ""Item 4"", [""Item 4.1"", ""Item 4.2"", [""Item 4.2.1"", ""Item 4.2.2""], ""Item 4.3"", [""Item 4.3.1""]], ""Item 5""] def tearDown(self): md_file = Path('Test_file.md') if md_file.is_file(): os.remove('Test_file.md') def test_create_md_file(self): md_file = MdUtils(""Test_file"") md_file.create_md_file() md_file_expect = Path('Test_file.md') if md_file_expect.is_file(): os.remove('Test_file.md') pass else: self.fail() def test_new_header(self): file_name = 'Test_file' md_file = MdUtils(file_name) string_headers_expected = ""\n# Header 0\n\n## Header 1\n\n### Header 2\n\n#### Header 3\n\n"" \ ""##### Header 4\n\n###### Header 5\n"" string_headers = """" for x in range(6): string_headers += md_file.new_header(level=(x + 1), title='Header ' + str(x), style='atx') self.assertEqual(string_headers, string_headers_expected) md_file.create_md_file() file_result = md_file.read_md_file(file_name) self.assertEqual(file_result, '\n\n\n' + string_headers_expected) def test_new_table_of_contents(self): # Create headers level 1 and 2. md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) # Testing Depth 1 table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=1) table_of_content_expected = table_of_content_title \ + '\n* [' + list_headers[0] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[0].lower().replace(' ', '-')) + ')' \ + '\n* [' + list_headers[2] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[2].lower().replace(' ', '-')) + ')\n' self.assertEqual(table_of_contents_result, table_of_content_expected) # Testing created file md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') # Testing Depth 2 md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=2) table_of_content_expected = table_of_content_title for x in range(len(list_headers)): if x in (0, 2): table_of_content_expected += '\n* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' else: table_of_content_expected += '\n\t* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' table_of_content_expected += '\n' self.assertEqual(table_of_contents_result, table_of_content_expected) md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') def test_new_paragraph(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_paragraph(""This is a new paragraph created using new_paragraph method."") expected_value = '\n\nThis is a new paragraph created using new_paragraph method.' self.assertEqual(created_value, expected_value) def test_new_line(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_line(""This is a new line created using new_line method."") expected_value = ' \nThis is a new line created using new_line method.' self.assertEqual(created_value, expected_value) def test_insert_code(self): md_file = MdUtils(file_name='Test_file') code = (""mdFile.new_header(level=1, title='Atx Header 1')\n"" ""mdFile.new_header(level=2, title='Atx Header 2')\n"" ""mdFile.new_header(level=3, title='Atx Header 3')\n"" ""mdFile.new_header(level=4, title='Atx Header 4')\n"" ""mdFile.new_header(level=5, title='Atx Header 5')\n"" ""mdFile.new_header(level=6, title='Atx Header 6')\n"") expects = '\n\n```\n' + code + '\n```' self.assertEqual(md_file.insert_code(code), expects) language = 'python' expects = '\n\n```' + language + '\n' + code + '\n```' self.assertEqual(md_file.insert_code(code, language), expects) def test_new_inline_link(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=""https://github.com/didix21/mdutils"", text=""mdutils library"") expected_value = '[mdutils library](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_text_empty(self): link = ""https://github.com/didix21/mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=link, text=link) expected_value = '[https://github.com/didix21/mdutils](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_empty(self): md_file = MdUtils(file_name=""Test_file"", title="""") try: md_file.new_inline_link() except TypeError: return self.fail() def test_new_inline_link_bold_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[**' + text + '**](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""b"") self.assertEqual(expected_value, created_value) def test_new_inline_link_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[*' + text + '*](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""i"") self.assertEqual(expected_value, created_value) def test_new_inline_link_code_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[``' + text + '``](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""c"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[***' + text + '***](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""bi"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_code_format(self): link = ""https://github.com/didix21/mdutils"" text", https://github.com/didix21/mdutils,2ef859ecc1e4f1a3e24f4150c96f61180bc57ceb,tests/test_mdutils.py::TestMdUtils::test_new_list,tests/test_mdutils.py,OD-Vic,flaky,Accepted,https://github.com/didix21/mdutils/pull/76,classify;root_cause,"# Python # # This module implements tests for MdUtils class. # # This file is part of mdutils. https://github.com/didix21/mdutils # # MIT License: (C) 2018 Dídac Coll from unittest import TestCase from mdutils.mdutils import MdUtils from mdutils.fileutils import MarkDownFile from pathlib import Path import os import re class TestMdUtils(TestCase): def setUp(self) -> None: self.expected_list = ""\n\n\n"" \ ""\n- Item 1\n"" \ ""- Item 2\n"" \ ""- Item 3\n"" \ ""- Item 4\n"" \ "" - Item 4.1\n"" \ "" - Item 4.2\n"" \ "" - Item 4.2.1\n"" \ "" - Item 4.2.2\n"" \ "" - Item 4.3\n"" \ "" - Item 4.3.1\n"" \ ""- Item 5\n"" self.complex_items = [""Item 1"", ""Item 2"", ""Item 3"", ""Item 4"", [""Item 4.1"", ""Item 4.2"", [""Item 4.2.1"", ""Item 4.2.2""], ""Item 4.3"", [""Item 4.3.1""]], ""Item 5""] def tearDown(self): md_file = Path('Test_file.md') if md_file.is_file(): os.remove('Test_file.md') def test_create_md_file(self): md_file = MdUtils(""Test_file"") md_file.create_md_file() md_file_expect = Path('Test_file.md') if md_file_expect.is_file(): os.remove('Test_file.md') pass else: self.fail() def test_new_header(self): file_name = 'Test_file' md_file = MdUtils(file_name) string_headers_expected = ""\n# Header 0\n\n## Header 1\n\n### Header 2\n\n#### Header 3\n\n"" \ ""##### Header 4\n\n###### Header 5\n"" string_headers = """" for x in range(6): string_headers += md_file.new_header(level=(x + 1), title='Header ' + str(x), style='atx') self.assertEqual(string_headers, string_headers_expected) md_file.create_md_file() file_result = md_file.read_md_file(file_name) self.assertEqual(file_result, '\n\n\n' + string_headers_expected) def test_new_table_of_contents(self): # Create headers level 1 and 2. md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) # Testing Depth 1 table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=1) table_of_content_expected = table_of_content_title \ + '\n* [' + list_headers[0] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[0].lower().replace(' ', '-')) + ')' \ + '\n* [' + list_headers[2] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[2].lower().replace(' ', '-')) + ')\n' self.assertEqual(table_of_contents_result, table_of_content_expected) # Testing created file md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') # Testing Depth 2 md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=2) table_of_content_expected = table_of_content_title for x in range(len(list_headers)): if x in (0, 2): table_of_content_expected += '\n* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' else: table_of_content_expected += '\n\t* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' table_of_content_expected += '\n' self.assertEqual(table_of_contents_result, table_of_content_expected) md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') def test_new_paragraph(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_paragraph(""This is a new paragraph created using new_paragraph method."") expected_value = '\n\nThis is a new paragraph created using new_paragraph method.' self.assertEqual(created_value, expected_value) def test_new_line(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_line(""This is a new line created using new_line method."") expected_value = ' \nThis is a new line created using new_line method.' self.assertEqual(created_value, expected_value) def test_insert_code(self): md_file = MdUtils(file_name='Test_file') code = (""mdFile.new_header(level=1, title='Atx Header 1')\n"" ""mdFile.new_header(level=2, title='Atx Header 2')\n"" ""mdFile.new_header(level=3, title='Atx Header 3')\n"" ""mdFile.new_header(level=4, title='Atx Header 4')\n"" ""mdFile.new_header(level=5, title='Atx Header 5')\n"" ""mdFile.new_header(level=6, title='Atx Header 6')\n"") expects = '\n\n```\n' + code + '\n```' self.assertEqual(md_file.insert_code(code), expects) language = 'python' expects = '\n\n```' + language + '\n' + code + '\n```' self.assertEqual(md_file.insert_code(code, language), expects) def test_new_inline_link(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=""https://github.com/didix21/mdutils"", text=""mdutils library"") expected_value = '[mdutils library](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_text_empty(self): link = ""https://github.com/didix21/mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=link, text=link) expected_value = '[https://github.com/didix21/mdutils](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_empty(self): md_file = MdUtils(file_name=""Test_file"", title="""") try: md_file.new_inline_link() except TypeError: return self.fail() def test_new_inline_link_bold_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[**' + text + '**](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""b"") self.assertEqual(expected_value, created_value) def test_new_inline_link_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[*' + text + '*](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""i"") self.assertEqual(expected_value, created_value) def test_new_inline_link_code_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[``' + text + '``](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""c"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[***' + text + '***](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""bi"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_code_format(self): link = ""https://github.com/didix21/mdutils"" text", https://github.com/didix21/mdutils,2ef859ecc1e4f1a3e24f4150c96f61180bc57ceb,tests/test_mdutils.py::TestMdUtils::test_new_reference_image_markdown_data,tests/test_mdutils.py,OD-Vic,flaky,Accepted,https://github.com/didix21/mdutils/pull/76,classify;root_cause,"# Python # # This module implements tests for MdUtils class. # # This file is part of mdutils. https://github.com/didix21/mdutils # # MIT License: (C) 2018 Dídac Coll from unittest import TestCase from mdutils.mdutils import MdUtils from mdutils.fileutils import MarkDownFile from pathlib import Path import os import re class TestMdUtils(TestCase): def setUp(self) -> None: self.expected_list = ""\n\n\n"" \ ""\n- Item 1\n"" \ ""- Item 2\n"" \ ""- Item 3\n"" \ ""- Item 4\n"" \ "" - Item 4.1\n"" \ "" - Item 4.2\n"" \ "" - Item 4.2.1\n"" \ "" - Item 4.2.2\n"" \ "" - Item 4.3\n"" \ "" - Item 4.3.1\n"" \ ""- Item 5\n"" self.complex_items = [""Item 1"", ""Item 2"", ""Item 3"", ""Item 4"", [""Item 4.1"", ""Item 4.2"", [""Item 4.2.1"", ""Item 4.2.2""], ""Item 4.3"", [""Item 4.3.1""]], ""Item 5""] def tearDown(self): md_file = Path('Test_file.md') if md_file.is_file(): os.remove('Test_file.md') def test_create_md_file(self): md_file = MdUtils(""Test_file"") md_file.create_md_file() md_file_expect = Path('Test_file.md') if md_file_expect.is_file(): os.remove('Test_file.md') pass else: self.fail() def test_new_header(self): file_name = 'Test_file' md_file = MdUtils(file_name) string_headers_expected = ""\n# Header 0\n\n## Header 1\n\n### Header 2\n\n#### Header 3\n\n"" \ ""##### Header 4\n\n###### Header 5\n"" string_headers = """" for x in range(6): string_headers += md_file.new_header(level=(x + 1), title='Header ' + str(x), style='atx') self.assertEqual(string_headers, string_headers_expected) md_file.create_md_file() file_result = md_file.read_md_file(file_name) self.assertEqual(file_result, '\n\n\n' + string_headers_expected) def test_new_table_of_contents(self): # Create headers level 1 and 2. md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) # Testing Depth 1 table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=1) table_of_content_expected = table_of_content_title \ + '\n* [' + list_headers[0] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[0].lower().replace(' ', '-')) + ')' \ + '\n* [' + list_headers[2] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[2].lower().replace(' ', '-')) + ')\n' self.assertEqual(table_of_contents_result, table_of_content_expected) # Testing created file md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') # Testing Depth 2 md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=2) table_of_content_expected = table_of_content_title for x in range(len(list_headers)): if x in (0, 2): table_of_content_expected += '\n* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' else: table_of_content_expected += '\n\t* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' table_of_content_expected += '\n' self.assertEqual(table_of_contents_result, table_of_content_expected) md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') def test_new_paragraph(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_paragraph(""This is a new paragraph created using new_paragraph method."") expected_value = '\n\nThis is a new paragraph created using new_paragraph method.' self.assertEqual(created_value, expected_value) def test_new_line(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_line(""This is a new line created using new_line method."") expected_value = ' \nThis is a new line created using new_line method.' self.assertEqual(created_value, expected_value) def test_insert_code(self): md_file = MdUtils(file_name='Test_file') code = (""mdFile.new_header(level=1, title='Atx Header 1')\n"" ""mdFile.new_header(level=2, title='Atx Header 2')\n"" ""mdFile.new_header(level=3, title='Atx Header 3')\n"" ""mdFile.new_header(level=4, title='Atx Header 4')\n"" ""mdFile.new_header(level=5, title='Atx Header 5')\n"" ""mdFile.new_header(level=6, title='Atx Header 6')\n"") expects = '\n\n```\n' + code + '\n```' self.assertEqual(md_file.insert_code(code), expects) language = 'python' expects = '\n\n```' + language + '\n' + code + '\n```' self.assertEqual(md_file.insert_code(code, language), expects) def test_new_inline_link(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=""https://github.com/didix21/mdutils"", text=""mdutils library"") expected_value = '[mdutils library](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_text_empty(self): link = ""https://github.com/didix21/mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=link, text=link) expected_value = '[https://github.com/didix21/mdutils](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_empty(self): md_file = MdUtils(file_name=""Test_file"", title="""") try: md_file.new_inline_link() except TypeError: return self.fail() def test_new_inline_link_bold_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[**' + text + '**](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""b"") self.assertEqual(expected_value, created_value) def test_new_inline_link_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[*' + text + '*](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""i"") self.assertEqual(expected_value, created_value) def test_new_inline_link_code_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[``' + text + '``](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""c"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[***' + text + '***](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""bi"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_code_format(self): link = ""https://github.com/didix21/mdutils"" text", https://github.com/didix21/mdutils,2ef859ecc1e4f1a3e24f4150c96f61180bc57ceb,tests/test_mdutils.py::TestMdUtils::test_new_table_of_contents,tests/test_mdutils.py,OD-Vic,flaky,Accepted,https://github.com/didix21/mdutils/pull/76,classify;root_cause,"# Python # # This module implements tests for MdUtils class. # # This file is part of mdutils. https://github.com/didix21/mdutils # # MIT License: (C) 2018 Dídac Coll from unittest import TestCase from mdutils.mdutils import MdUtils from mdutils.fileutils import MarkDownFile from pathlib import Path import os import re class TestMdUtils(TestCase): def setUp(self) -> None: self.expected_list = ""\n\n\n"" \ ""\n- Item 1\n"" \ ""- Item 2\n"" \ ""- Item 3\n"" \ ""- Item 4\n"" \ "" - Item 4.1\n"" \ "" - Item 4.2\n"" \ "" - Item 4.2.1\n"" \ "" - Item 4.2.2\n"" \ "" - Item 4.3\n"" \ "" - Item 4.3.1\n"" \ ""- Item 5\n"" self.complex_items = [""Item 1"", ""Item 2"", ""Item 3"", ""Item 4"", [""Item 4.1"", ""Item 4.2"", [""Item 4.2.1"", ""Item 4.2.2""], ""Item 4.3"", [""Item 4.3.1""]], ""Item 5""] def tearDown(self): md_file = Path('Test_file.md') if md_file.is_file(): os.remove('Test_file.md') def test_create_md_file(self): md_file = MdUtils(""Test_file"") md_file.create_md_file() md_file_expect = Path('Test_file.md') if md_file_expect.is_file(): os.remove('Test_file.md') pass else: self.fail() def test_new_header(self): file_name = 'Test_file' md_file = MdUtils(file_name) string_headers_expected = ""\n# Header 0\n\n## Header 1\n\n### Header 2\n\n#### Header 3\n\n"" \ ""##### Header 4\n\n###### Header 5\n"" string_headers = """" for x in range(6): string_headers += md_file.new_header(level=(x + 1), title='Header ' + str(x), style='atx') self.assertEqual(string_headers, string_headers_expected) md_file.create_md_file() file_result = md_file.read_md_file(file_name) self.assertEqual(file_result, '\n\n\n' + string_headers_expected) def test_new_table_of_contents(self): # Create headers level 1 and 2. md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) # Testing Depth 1 table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=1) table_of_content_expected = table_of_content_title \ + '\n* [' + list_headers[0] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[0].lower().replace(' ', '-')) + ')' \ + '\n* [' + list_headers[2] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[2].lower().replace(' ', '-')) + ')\n' self.assertEqual(table_of_contents_result, table_of_content_expected) # Testing created file md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') # Testing Depth 2 md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=2) table_of_content_expected = table_of_content_title for x in range(len(list_headers)): if x in (0, 2): table_of_content_expected += '\n* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' else: table_of_content_expected += '\n\t* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' table_of_content_expected += '\n' self.assertEqual(table_of_contents_result, table_of_content_expected) md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') def test_new_paragraph(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_paragraph(""This is a new paragraph created using new_paragraph method."") expected_value = '\n\nThis is a new paragraph created using new_paragraph method.' self.assertEqual(created_value, expected_value) def test_new_line(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_line(""This is a new line created using new_line method."") expected_value = ' \nThis is a new line created using new_line method.' self.assertEqual(created_value, expected_value) def test_insert_code(self): md_file = MdUtils(file_name='Test_file') code = (""mdFile.new_header(level=1, title='Atx Header 1')\n"" ""mdFile.new_header(level=2, title='Atx Header 2')\n"" ""mdFile.new_header(level=3, title='Atx Header 3')\n"" ""mdFile.new_header(level=4, title='Atx Header 4')\n"" ""mdFile.new_header(level=5, title='Atx Header 5')\n"" ""mdFile.new_header(level=6, title='Atx Header 6')\n"") expects = '\n\n```\n' + code + '\n```' self.assertEqual(md_file.insert_code(code), expects) language = 'python' expects = '\n\n```' + language + '\n' + code + '\n```' self.assertEqual(md_file.insert_code(code, language), expects) def test_new_inline_link(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=""https://github.com/didix21/mdutils"", text=""mdutils library"") expected_value = '[mdutils library](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_text_empty(self): link = ""https://github.com/didix21/mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=link, text=link) expected_value = '[https://github.com/didix21/mdutils](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_empty(self): md_file = MdUtils(file_name=""Test_file"", title="""") try: md_file.new_inline_link() except TypeError: return self.fail() def test_new_inline_link_bold_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[**' + text + '**](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""b"") self.assertEqual(expected_value, created_value) def test_new_inline_link_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[*' + text + '*](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""i"") self.assertEqual(expected_value, created_value) def test_new_inline_link_code_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[``' + text + '``](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""c"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[***' + text + '***](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""bi"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_code_format(self): link = ""https://github.com/didix21/mdutils"" text", https://github.com/didix21/mdutils,2ef859ecc1e4f1a3e24f4150c96f61180bc57ceb,tests/test_mdutils.py::TestMdUtils::test_references_placed_in_markdown_file,tests/test_mdutils.py,OD-Vic,flaky,Accepted,https://github.com/didix21/mdutils/pull/76,classify;root_cause,"# Python # # This module implements tests for MdUtils class. # # This file is part of mdutils. https://github.com/didix21/mdutils # # MIT License: (C) 2018 Dídac Coll from unittest import TestCase from mdutils.mdutils import MdUtils from mdutils.fileutils import MarkDownFile from pathlib import Path import os import re class TestMdUtils(TestCase): def setUp(self) -> None: self.expected_list = ""\n\n\n"" \ ""\n- Item 1\n"" \ ""- Item 2\n"" \ ""- Item 3\n"" \ ""- Item 4\n"" \ "" - Item 4.1\n"" \ "" - Item 4.2\n"" \ "" - Item 4.2.1\n"" \ "" - Item 4.2.2\n"" \ "" - Item 4.3\n"" \ "" - Item 4.3.1\n"" \ ""- Item 5\n"" self.complex_items = [""Item 1"", ""Item 2"", ""Item 3"", ""Item 4"", [""Item 4.1"", ""Item 4.2"", [""Item 4.2.1"", ""Item 4.2.2""], ""Item 4.3"", [""Item 4.3.1""]], ""Item 5""] def tearDown(self): md_file = Path('Test_file.md') if md_file.is_file(): os.remove('Test_file.md') def test_create_md_file(self): md_file = MdUtils(""Test_file"") md_file.create_md_file() md_file_expect = Path('Test_file.md') if md_file_expect.is_file(): os.remove('Test_file.md') pass else: self.fail() def test_new_header(self): file_name = 'Test_file' md_file = MdUtils(file_name) string_headers_expected = ""\n# Header 0\n\n## Header 1\n\n### Header 2\n\n#### Header 3\n\n"" \ ""##### Header 4\n\n###### Header 5\n"" string_headers = """" for x in range(6): string_headers += md_file.new_header(level=(x + 1), title='Header ' + str(x), style='atx') self.assertEqual(string_headers, string_headers_expected) md_file.create_md_file() file_result = md_file.read_md_file(file_name) self.assertEqual(file_result, '\n\n\n' + string_headers_expected) def test_new_table_of_contents(self): # Create headers level 1 and 2. md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) # Testing Depth 1 table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=1) table_of_content_expected = table_of_content_title \ + '\n* [' + list_headers[0] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[0].lower().replace(' ', '-')) + ')' \ + '\n* [' + list_headers[2] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[2].lower().replace(' ', '-')) + ')\n' self.assertEqual(table_of_contents_result, table_of_content_expected) # Testing created file md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') # Testing Depth 2 md_file = MdUtils(file_name=""Test_file"", title=""Testing table of contents"") list_headers = [""Header 1"", ""Header 1.1"", ""Header 2"", ""Header 2.2"", ""Header 2.3""] table_of_content_title = MdUtils(file_name='').new_header(level=1, title='Index', style='setext') md_file.new_header(level=1, title=list_headers[0]) md_file.new_header(level=2, title=list_headers[1]) md_file.new_header(level=1, title=list_headers[2]) md_file.new_header(level=2, title=list_headers[3]) md_file.new_header(level=2, title=list_headers[4]) table_of_contents_result = md_file.new_table_of_contents(table_title=""Index"", depth=2) table_of_content_expected = table_of_content_title for x in range(len(list_headers)): if x in (0, 2): table_of_content_expected += '\n* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' else: table_of_content_expected += '\n\t* [' + list_headers[x] + '](#' \ + re.sub('[^a-z0-9_\-]', '', list_headers[x].lower().replace(' ', '-')) \ + ')' table_of_content_expected += '\n' self.assertEqual(table_of_contents_result, table_of_content_expected) md_file.create_md_file() data_file_result = MdUtils('').read_md_file('Test_file') data_file_expected = MdUtils('').new_header(1, ""Testing table of contents"", 'setext') \ + md_file.table_of_contents \ + md_file.file_data_text self.assertEqual(data_file_result, data_file_expected) os.remove('Test_file.md') def test_new_paragraph(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_paragraph(""This is a new paragraph created using new_paragraph method."") expected_value = '\n\nThis is a new paragraph created using new_paragraph method.' self.assertEqual(created_value, expected_value) def test_new_line(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_line(""This is a new line created using new_line method."") expected_value = ' \nThis is a new line created using new_line method.' self.assertEqual(created_value, expected_value) def test_insert_code(self): md_file = MdUtils(file_name='Test_file') code = (""mdFile.new_header(level=1, title='Atx Header 1')\n"" ""mdFile.new_header(level=2, title='Atx Header 2')\n"" ""mdFile.new_header(level=3, title='Atx Header 3')\n"" ""mdFile.new_header(level=4, title='Atx Header 4')\n"" ""mdFile.new_header(level=5, title='Atx Header 5')\n"" ""mdFile.new_header(level=6, title='Atx Header 6')\n"") expects = '\n\n```\n' + code + '\n```' self.assertEqual(md_file.insert_code(code), expects) language = 'python' expects = '\n\n```' + language + '\n' + code + '\n```' self.assertEqual(md_file.insert_code(code, language), expects) def test_new_inline_link(self): md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=""https://github.com/didix21/mdutils"", text=""mdutils library"") expected_value = '[mdutils library](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_text_empty(self): link = ""https://github.com/didix21/mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") created_value = md_file.new_inline_link(link=link, text=link) expected_value = '[https://github.com/didix21/mdutils](https://github.com/didix21/mdutils)' self.assertEqual(expected_value, created_value) def test_new_inline_link_empty(self): md_file = MdUtils(file_name=""Test_file"", title="""") try: md_file.new_inline_link() except TypeError: return self.fail() def test_new_inline_link_bold_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[**' + text + '**](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""b"") self.assertEqual(expected_value, created_value) def test_new_inline_link_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[*' + text + '*](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""i"") self.assertEqual(expected_value, created_value) def test_new_inline_link_code_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[``' + text + '``](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""c"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_format(self): link = ""https://github.com/didix21/mdutils"" text = ""mdutils"" md_file = MdUtils(file_name=""Test_file"", title="""") expected_value = '[***' + text + '***](' + link + ')' created_value = md_file.new_inline_link(link, text, bold_italics_code=""bi"") self.assertEqual(expected_value, created_value) def test_new_inline_link_bold_italic_code_format(self): link = ""https://github.com/didix21/mdutils"" text", https://github.com/django-beam/django-beam,43b2111f5c65937aa1e2cc9704b59796e982a805,tests/test_contrib_reversion.py::test_revision_is_visible_in_list,tests/test_contrib_reversion.py,NOD,flaky,nan,nan,classify;root_cause,"from django.contrib.auth import get_user_model from django.contrib.messages.apps import MessagesConfig from django.contrib.messages.middleware import MessageMiddleware from django.contrib.sessions.middleware import SessionMiddleware from django.core.exceptions import PermissionDenied from django.test import RequestFactory from reversion.models import Version from pytest import mark from reversion import is_registered, set_comment import pytest from beam import RelatedInline from beam.contrib.reversion.viewsets import VersionViewSet from beam.registry import RegistryType from test_views import user_with_perms from testapp.models import Dragonfly, Petaluridae, Sighting registry: RegistryType = {} class SightingInline(RelatedInline): title = ""Title of sightings"" fields = [""name""] model = Sighting foreign_key_field = ""dragonfly"" class VersionedDragonflyViewSet(VersionViewSet): registry = registry inline_classes = [SightingInline] model = Dragonfly list_search_fields = [""name""] fields = [""name"", ""age""] VersionedDragonflyViewSet() def test_versioned_view_set_models_are_registered(): assert is_registered(Dragonfly) def test_inlines_of_versioned_view_set_models_are_registered(): assert is_registered(Sighting) def test_unrelated_models_are_not_registered(): assert not is_registered(Petaluridae) @mark.django_db def test_using_create_view_creates_a_revision(django_user_model): user = user_with_perms(django_user_model, [""testapp.add_dragonfly""]) create_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""create""] ) request = RequestFactory().post( ""/dragonfly/create/"", data={ ""name"": ""versionfly"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 1, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 10, ""sighting_set-0-name"": ""Tokyo"", }, ) request.user = user response = create_view(request) assert response.status_code == 302 fly = Dragonfly.objects.get() fly_version = Version.objects.get_for_object_reference(Dragonfly, fly.pk).get() assert fly_version.object.name == ""versionfly"" assert fly_version.field_dict[""name""] == ""versionfly"" assert fly_version.revision.user == user assert fly_version.revision.comment == ""create"" sighting_version = Version.objects.get_for_object_reference( Sighting, fly.sighting_set.get().pk ).get() assert sighting_version.field_dict[""name""] == ""Tokyo"" @mark.django_db def test_using_update_view_creates_a_revision(django_user_model): user = user_with_perms(django_user_model, [""testapp.change_dragonfly""]) alpha = Dragonfly.objects.create(name=""alpha"", age=47) update_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""update""] ) request = RequestFactory().post( ""/dragonfly/update/{}/"".format(alpha.pk), data={ ""name"": ""beta"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 0, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 10, }, ) request.user = user response = update_view(request, pk=alpha.pk) assert response.status_code == 302 fly_version = Version.objects.get_for_object_reference(Dragonfly, alpha.pk).latest( ""revision__created_date"" ) assert fly_version.object.name == ""beta"" assert fly_version.field_dict[""name""] == ""beta"" assert fly_version.revision.user == user assert fly_version.revision.comment == ""update"" @mark.django_db def test_revision_is_visible_in_list(django_user_model): alpha = Dragonfly.objects.create(name=""alpha"", age=47) request = RequestFactory().get(""/"", {}) request.user = user_with_perms(django_user_model, [""testapp.view_dragonfly""]) with VersionedDragonflyViewSet().create_revision(request): set_comment(""number one"") alpha.save() with VersionedDragonflyViewSet().create_revision(request): set_comment(""number two"") alpha.save() with VersionedDragonflyViewSet().create_revision(request): set_comment(""number three"") alpha.save() assert Version.objects.get_for_object_reference(Dragonfly, alpha.pk).count() == 3 view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""version_list""] ) response = view(request, pk=alpha.pk) response_content = response.rendered_content assert ""number one"" in response_content assert ""number two"" in response_content assert ""number three"" in response_content @mark.django_db def test_version_list_requires_view_permission(django_user_model): alpha = Dragonfly.objects.create(name=""alpha"", age=47) request = RequestFactory().get(""/"", {}) request.user = user_with_perms(django_user_model, []) with VersionedDragonflyViewSet().create_revision(request): set_comment(""number one"") alpha.save() view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""version_list""] ) with pytest.raises(PermissionDenied): view(request, pk=alpha.pk) @mark.django_db def test_show_detail_from_previous_version(django_user_model): alpha = Dragonfly.objects.create(name=""alpha"", age=47) sighting = Sighting.objects.create(name=""Berlin"", dragonfly=alpha) request = RequestFactory().get(""/"", {}) request.user = user_with_perms(django_user_model, [""testapp.view_dragonfly""]) with VersionedDragonflyViewSet().create_revision(request): alpha.save() version = Version.objects.get_for_object_reference(Dragonfly, alpha.pk).latest( ""revision__created_date"" ) alpha.name = ""beta"" alpha.save() sighting.name = ""Tokyo"" sighting.save() detail_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""detail""] ) detail_content = detail_view(request, pk=alpha.pk).rendered_content assert ""beta"" in detail_content assert ""Tokyo"" in detail_content version_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""version_detail""] ) version_content = version_view( request, pk=alpha.pk, version_id=version.pk ).content.decode(""utf-8"") assert ""alpha"" in version_content assert ""Berlin"" in version_content assert ""beta"" not in version_content assert ""Tokyo"" not in version_content @mark.django_db def test_version_detail_requires_view_perm(django_user_model): alpha = Dragonfly.objects.create(name=""alpha"", age=47) sighting = Sighting.objects.create(name=""Berlin"", dragonfly=alpha) request = RequestFactory().get(""/"", {}) request.user = user_with_perms(django_user_model, []) with VersionedDragonflyViewSet().create_revision(request): alpha.save() version = Version.objects.get_for_object_reference(Dragonfly, alpha.pk).latest( ""revision__created_date"" ) detail_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""detail""] ) with pytest.raises(PermissionDenied): detail_view(request, pk=alpha.pk) @mark.django_db def test_revert_to_previous_version(django_user_model): alpha = Dragonfly.objects.create(name=""alpha"", age=47) sighting = Sighting.objects.create(name=""Berlin"", dragonfly=alpha) request = RequestFactory().post(""/"", {}) request.user = user_with_perms(django_user_model, [""testapp.change_dragonfly""]) SessionMiddleware().process_request(request) MessageMiddleware().process_request(request) with VersionedDragonflyViewSet().create_revision(request): alpha.save() version = Version.objects.get_for_object_reference(Dragonfly, alpha.pk).latest( ""revision__created_date"" ) alpha.name = ""beta"" alpha.save() sighting.name = ""Tokyo"" sighting.save() version_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""version_restore""] ) response = version_view(request, pk=alpha.pk, version_id=version.pk) assert response.status_code == 302 alpha.refresh_from_db() sighting.refresh_from_db() assert alpha.name == ""alpha"" assert sighting.name == ""Berlin"" @mark.django_db def test_revert_to_previous_version_requires_change_perm(django_user_model): alpha = Dragonfly.objects.create(name=""alpha"", age=47) sighting = Sighting.objects.create(name=""Berlin"", dragonfly=alpha) request = RequestFactory().post(""/"", {}) request.user = user_with_perms(django_user_model, []) with VersionedDragonflyViewSet().create_revision(request): alpha.save() version = Version.objects.get_for_object_reference(Dragonfly, alpha.pk).latest( ""revision__created_date"" ) version_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""version_restore""] ) with pytest.raises(PermissionDenied): response = version_view(request, pk=alpha.pk, version_id=version.pk) ", https://github.com/django-beam/django-beam,43b2111f5c65937aa1e2cc9704b59796e982a805,tests/test_contrib_reversion.py::test_show_detail_from_previous_version,tests/test_contrib_reversion.py,NOD,flaky,nan,nan,classify;root_cause,"from django.contrib.auth import get_user_model from django.contrib.messages.apps import MessagesConfig from django.contrib.messages.middleware import MessageMiddleware from django.contrib.sessions.middleware import SessionMiddleware from django.core.exceptions import PermissionDenied from django.test import RequestFactory from reversion.models import Version from pytest import mark from reversion import is_registered, set_comment import pytest from beam import RelatedInline from beam.contrib.reversion.viewsets import VersionViewSet from beam.registry import RegistryType from test_views import user_with_perms from testapp.models import Dragonfly, Petaluridae, Sighting registry: RegistryType = {} class SightingInline(RelatedInline): title = ""Title of sightings"" fields = [""name""] model = Sighting foreign_key_field = ""dragonfly"" class VersionedDragonflyViewSet(VersionViewSet): registry = registry inline_classes = [SightingInline] model = Dragonfly list_search_fields = [""name""] fields = [""name"", ""age""] VersionedDragonflyViewSet() def test_versioned_view_set_models_are_registered(): assert is_registered(Dragonfly) def test_inlines_of_versioned_view_set_models_are_registered(): assert is_registered(Sighting) def test_unrelated_models_are_not_registered(): assert not is_registered(Petaluridae) @mark.django_db def test_using_create_view_creates_a_revision(django_user_model): user = user_with_perms(django_user_model, [""testapp.add_dragonfly""]) create_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""create""] ) request = RequestFactory().post( ""/dragonfly/create/"", data={ ""name"": ""versionfly"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 1, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 10, ""sighting_set-0-name"": ""Tokyo"", }, ) request.user = user response = create_view(request) assert response.status_code == 302 fly = Dragonfly.objects.get() fly_version = Version.objects.get_for_object_reference(Dragonfly, fly.pk).get() assert fly_version.object.name == ""versionfly"" assert fly_version.field_dict[""name""] == ""versionfly"" assert fly_version.revision.user == user assert fly_version.revision.comment == ""create"" sighting_version = Version.objects.get_for_object_reference( Sighting, fly.sighting_set.get().pk ).get() assert sighting_version.field_dict[""name""] == ""Tokyo"" @mark.django_db def test_using_update_view_creates_a_revision(django_user_model): user = user_with_perms(django_user_model, [""testapp.change_dragonfly""]) alpha = Dragonfly.objects.create(name=""alpha"", age=47) update_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""update""] ) request = RequestFactory().post( ""/dragonfly/update/{}/"".format(alpha.pk), data={ ""name"": ""beta"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 0, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 10, }, ) request.user = user response = update_view(request, pk=alpha.pk) assert response.status_code == 302 fly_version = Version.objects.get_for_object_reference(Dragonfly, alpha.pk).latest( ""revision__created_date"" ) assert fly_version.object.name == ""beta"" assert fly_version.field_dict[""name""] == ""beta"" assert fly_version.revision.user == user assert fly_version.revision.comment == ""update"" @mark.django_db def test_revision_is_visible_in_list(django_user_model): alpha = Dragonfly.objects.create(name=""alpha"", age=47) request = RequestFactory().get(""/"", {}) request.user = user_with_perms(django_user_model, [""testapp.view_dragonfly""]) with VersionedDragonflyViewSet().create_revision(request): set_comment(""number one"") alpha.save() with VersionedDragonflyViewSet().create_revision(request): set_comment(""number two"") alpha.save() with VersionedDragonflyViewSet().create_revision(request): set_comment(""number three"") alpha.save() assert Version.objects.get_for_object_reference(Dragonfly, alpha.pk).count() == 3 view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""version_list""] ) response = view(request, pk=alpha.pk) response_content = response.rendered_content assert ""number one"" in response_content assert ""number two"" in response_content assert ""number three"" in response_content @mark.django_db def test_version_list_requires_view_permission(django_user_model): alpha = Dragonfly.objects.create(name=""alpha"", age=47) request = RequestFactory().get(""/"", {}) request.user = user_with_perms(django_user_model, []) with VersionedDragonflyViewSet().create_revision(request): set_comment(""number one"") alpha.save() view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""version_list""] ) with pytest.raises(PermissionDenied): view(request, pk=alpha.pk) @mark.django_db def test_show_detail_from_previous_version(django_user_model): alpha = Dragonfly.objects.create(name=""alpha"", age=47) sighting = Sighting.objects.create(name=""Berlin"", dragonfly=alpha) request = RequestFactory().get(""/"", {}) request.user = user_with_perms(django_user_model, [""testapp.view_dragonfly""]) with VersionedDragonflyViewSet().create_revision(request): alpha.save() version = Version.objects.get_for_object_reference(Dragonfly, alpha.pk).latest( ""revision__created_date"" ) alpha.name = ""beta"" alpha.save() sighting.name = ""Tokyo"" sighting.save() detail_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""detail""] ) detail_content = detail_view(request, pk=alpha.pk).rendered_content assert ""beta"" in detail_content assert ""Tokyo"" in detail_content version_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""version_detail""] ) version_content = version_view( request, pk=alpha.pk, version_id=version.pk ).content.decode(""utf-8"") assert ""alpha"" in version_content assert ""Berlin"" in version_content assert ""beta"" not in version_content assert ""Tokyo"" not in version_content @mark.django_db def test_version_detail_requires_view_perm(django_user_model): alpha = Dragonfly.objects.create(name=""alpha"", age=47) sighting = Sighting.objects.create(name=""Berlin"", dragonfly=alpha) request = RequestFactory().get(""/"", {}) request.user = user_with_perms(django_user_model, []) with VersionedDragonflyViewSet().create_revision(request): alpha.save() version = Version.objects.get_for_object_reference(Dragonfly, alpha.pk).latest( ""revision__created_date"" ) detail_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""detail""] ) with pytest.raises(PermissionDenied): detail_view(request, pk=alpha.pk) @mark.django_db def test_revert_to_previous_version(django_user_model): alpha = Dragonfly.objects.create(name=""alpha"", age=47) sighting = Sighting.objects.create(name=""Berlin"", dragonfly=alpha) request = RequestFactory().post(""/"", {}) request.user = user_with_perms(django_user_model, [""testapp.change_dragonfly""]) SessionMiddleware().process_request(request) MessageMiddleware().process_request(request) with VersionedDragonflyViewSet().create_revision(request): alpha.save() version = Version.objects.get_for_object_reference(Dragonfly, alpha.pk).latest( ""revision__created_date"" ) alpha.name = ""beta"" alpha.save() sighting.name = ""Tokyo"" sighting.save() version_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""version_restore""] ) response = version_view(request, pk=alpha.pk, version_id=version.pk) assert response.status_code == 302 alpha.refresh_from_db() sighting.refresh_from_db() assert alpha.name == ""alpha"" assert sighting.name == ""Berlin"" @mark.django_db def test_revert_to_previous_version_requires_change_perm(django_user_model): alpha = Dragonfly.objects.create(name=""alpha"", age=47) sighting = Sighting.objects.create(name=""Berlin"", dragonfly=alpha) request = RequestFactory().post(""/"", {}) request.user = user_with_perms(django_user_model, []) with VersionedDragonflyViewSet().create_revision(request): alpha.save() version = Version.objects.get_for_object_reference(Dragonfly, alpha.pk).latest( ""revision__created_date"" ) version_view = VersionedDragonflyViewSet()._get_view( VersionedDragonflyViewSet().components[""version_restore""] ) with pytest.raises(PermissionDenied): response = version_view(request, pk=alpha.pk, version_id=version.pk) ", https://github.com/django-beam/django-beam,43b2111f5c65937aa1e2cc9704b59796e982a805,tests/test_views.py::test_delete,tests/test_views.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from django.contrib.auth.models import Permission from pytest import mark from testapp.models import Dragonfly, Sighting, CascadingSighting, ProtectedSighting from testapp.views import DragonflyViewSet def user_with_perms(django_user_model, perms, username=""foo"", password=""bar""): user = django_user_model.objects.create_user(username=username, password=password) for perm in perms: app_label, codename = perm.split(""."") user.user_permissions.add( Permission.objects.get(content_type__app_label=app_label, codename=codename) ) return user @pytest.fixture def client_with_perms(client, django_user_model): def get_client_with_perms(*perms): user_with_perms(django_user_model, perms, username=""foo"", password=""bar"") client.login(username=""foo"", password=""bar"") return client return get_client_with_perms @mark.django_db def test_list(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert b""alpha"" in response.content assert b""omega"" in response.content @mark.django_db def test_list_requires_permission(client_with_perms): client = client_with_perms() Dragonfly.objects.create(name=""alpha"", age=12) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert response.status_code == 403 @mark.django_db def test_list_search(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?q=alpha"") assert b""alpha"" in response.content assert b""omega"" not in response.content @mark.django_db def test_list_sort(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=-name"") assert response.content.index(b""alpha"") > response.content.index(b""omega"") response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=name"") assert response.content.index(b""alpha"") < response.content.index(b""omega"") @mark.django_db def test_detail(client_with_perms): client = client_with_perms( ""testapp.view_dragonfly"", ""testapp.change_dragonfly"", ""testapp.delete_dragonfly"" ) alpha = Dragonfly.objects.create(name=""alpha"", age=47) Sighting.objects.create(name=""Berlin"", dragonfly=alpha) Sighting.objects.create(name=""Paris"", dragonfly=alpha) links = DragonflyViewSet().links response_content = client.get(links[""detail""].reverse(alpha)).content.decode( ""utf-8"" ) assert ""alpha"" in response_content assert ""Title of sightings"" assert ""Berlin"" in response_content assert ""Paris"" in response_content assert 'href=""{}""'.format(links[""list""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""update""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""delete""].reverse(alpha)) in response_content @mark.django_db def test_detail_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) links = DragonflyViewSet().links response = client.get(links[""detail""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_update(client_with_perms): client = client_with_perms(""testapp.change_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert b""alpha"" in response.content assert ""form"" in response.context assert response.context[""form""][""name""].value() == ""alpha"" @mark.django_db def test_update_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_delete(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""Are you sure you want to delete"" in response.content assert b""alpha"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_shows_related(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) CascadingSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""The following objects"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_protected_not_allowed(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) ProtectedSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""You can't delete"" in response.content assert b""the following objects depend"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 403 assert Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert response.status_code == 403 response = client.post(delete_url) assert response.status_code == 403 @mark.django_db def test_create_with_inlines(client_with_perms): client = client_with_perms(""testapp.add_dragonfly"") response = client.post( ""/dragonfly/create/"", { ""name"": ""foobar"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 1, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 999, ""sighting_set-0-name"": ""Tokyo"", }, ) dragonfly = Dragonfly.objects.get(name=""foobar"") assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""detail""].reverse(dragonfly) assert dragonfly.age == 81 assert dragonfly.sighting_set.get().name == ""Tokyo"" @mark.django_db def test_only_popup_param_is_preserved_in_detail_links(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"", ""testapp.change_dragonfly"") instance = Dragonfly.objects.create(name=""alpha"", age=12) response = client.get( DragonflyViewSet().links[""list""].reverse(), data={""_popup"": ""id_test"", ""not_preserved"": ""nope""}, ) response_content = response.content.decode(""utf-8"") detail_url = DragonflyViewSet().links[""update""].reverse(instance) assert detail_url in response_content assert detail_url + ""?_popup=id_test"" in response_content assert detail_url + ""?_popup=id_test¬_preserved"" not in response_content ", https://github.com/django-beam/django-beam,43b2111f5c65937aa1e2cc9704b59796e982a805,tests/test_views.py::test_delete_protected_not_allowed,tests/test_views.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from django.contrib.auth.models import Permission from pytest import mark from testapp.models import Dragonfly, Sighting, CascadingSighting, ProtectedSighting from testapp.views import DragonflyViewSet def user_with_perms(django_user_model, perms, username=""foo"", password=""bar""): user = django_user_model.objects.create_user(username=username, password=password) for perm in perms: app_label, codename = perm.split(""."") user.user_permissions.add( Permission.objects.get(content_type__app_label=app_label, codename=codename) ) return user @pytest.fixture def client_with_perms(client, django_user_model): def get_client_with_perms(*perms): user_with_perms(django_user_model, perms, username=""foo"", password=""bar"") client.login(username=""foo"", password=""bar"") return client return get_client_with_perms @mark.django_db def test_list(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert b""alpha"" in response.content assert b""omega"" in response.content @mark.django_db def test_list_requires_permission(client_with_perms): client = client_with_perms() Dragonfly.objects.create(name=""alpha"", age=12) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert response.status_code == 403 @mark.django_db def test_list_search(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?q=alpha"") assert b""alpha"" in response.content assert b""omega"" not in response.content @mark.django_db def test_list_sort(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=-name"") assert response.content.index(b""alpha"") > response.content.index(b""omega"") response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=name"") assert response.content.index(b""alpha"") < response.content.index(b""omega"") @mark.django_db def test_detail(client_with_perms): client = client_with_perms( ""testapp.view_dragonfly"", ""testapp.change_dragonfly"", ""testapp.delete_dragonfly"" ) alpha = Dragonfly.objects.create(name=""alpha"", age=47) Sighting.objects.create(name=""Berlin"", dragonfly=alpha) Sighting.objects.create(name=""Paris"", dragonfly=alpha) links = DragonflyViewSet().links response_content = client.get(links[""detail""].reverse(alpha)).content.decode( ""utf-8"" ) assert ""alpha"" in response_content assert ""Title of sightings"" assert ""Berlin"" in response_content assert ""Paris"" in response_content assert 'href=""{}""'.format(links[""list""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""update""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""delete""].reverse(alpha)) in response_content @mark.django_db def test_detail_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) links = DragonflyViewSet().links response = client.get(links[""detail""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_update(client_with_perms): client = client_with_perms(""testapp.change_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert b""alpha"" in response.content assert ""form"" in response.context assert response.context[""form""][""name""].value() == ""alpha"" @mark.django_db def test_update_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_delete(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""Are you sure you want to delete"" in response.content assert b""alpha"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_shows_related(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) CascadingSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""The following objects"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_protected_not_allowed(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) ProtectedSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""You can't delete"" in response.content assert b""the following objects depend"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 403 assert Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert response.status_code == 403 response = client.post(delete_url) assert response.status_code == 403 @mark.django_db def test_create_with_inlines(client_with_perms): client = client_with_perms(""testapp.add_dragonfly"") response = client.post( ""/dragonfly/create/"", { ""name"": ""foobar"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 1, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 999, ""sighting_set-0-name"": ""Tokyo"", }, ) dragonfly = Dragonfly.objects.get(name=""foobar"") assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""detail""].reverse(dragonfly) assert dragonfly.age == 81 assert dragonfly.sighting_set.get().name == ""Tokyo"" @mark.django_db def test_only_popup_param_is_preserved_in_detail_links(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"", ""testapp.change_dragonfly"") instance = Dragonfly.objects.create(name=""alpha"", age=12) response = client.get( DragonflyViewSet().links[""list""].reverse(), data={""_popup"": ""id_test"", ""not_preserved"": ""nope""}, ) response_content = response.content.decode(""utf-8"") detail_url = DragonflyViewSet().links[""update""].reverse(instance) assert detail_url in response_content assert detail_url + ""?_popup=id_test"" in response_content assert detail_url + ""?_popup=id_test¬_preserved"" not in response_content ", https://github.com/django-beam/django-beam,43b2111f5c65937aa1e2cc9704b59796e982a805,tests/test_views.py::test_delete_shows_related,tests/test_views.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from django.contrib.auth.models import Permission from pytest import mark from testapp.models import Dragonfly, Sighting, CascadingSighting, ProtectedSighting from testapp.views import DragonflyViewSet def user_with_perms(django_user_model, perms, username=""foo"", password=""bar""): user = django_user_model.objects.create_user(username=username, password=password) for perm in perms: app_label, codename = perm.split(""."") user.user_permissions.add( Permission.objects.get(content_type__app_label=app_label, codename=codename) ) return user @pytest.fixture def client_with_perms(client, django_user_model): def get_client_with_perms(*perms): user_with_perms(django_user_model, perms, username=""foo"", password=""bar"") client.login(username=""foo"", password=""bar"") return client return get_client_with_perms @mark.django_db def test_list(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert b""alpha"" in response.content assert b""omega"" in response.content @mark.django_db def test_list_requires_permission(client_with_perms): client = client_with_perms() Dragonfly.objects.create(name=""alpha"", age=12) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert response.status_code == 403 @mark.django_db def test_list_search(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?q=alpha"") assert b""alpha"" in response.content assert b""omega"" not in response.content @mark.django_db def test_list_sort(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=-name"") assert response.content.index(b""alpha"") > response.content.index(b""omega"") response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=name"") assert response.content.index(b""alpha"") < response.content.index(b""omega"") @mark.django_db def test_detail(client_with_perms): client = client_with_perms( ""testapp.view_dragonfly"", ""testapp.change_dragonfly"", ""testapp.delete_dragonfly"" ) alpha = Dragonfly.objects.create(name=""alpha"", age=47) Sighting.objects.create(name=""Berlin"", dragonfly=alpha) Sighting.objects.create(name=""Paris"", dragonfly=alpha) links = DragonflyViewSet().links response_content = client.get(links[""detail""].reverse(alpha)).content.decode( ""utf-8"" ) assert ""alpha"" in response_content assert ""Title of sightings"" assert ""Berlin"" in response_content assert ""Paris"" in response_content assert 'href=""{}""'.format(links[""list""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""update""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""delete""].reverse(alpha)) in response_content @mark.django_db def test_detail_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) links = DragonflyViewSet().links response = client.get(links[""detail""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_update(client_with_perms): client = client_with_perms(""testapp.change_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert b""alpha"" in response.content assert ""form"" in response.context assert response.context[""form""][""name""].value() == ""alpha"" @mark.django_db def test_update_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_delete(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""Are you sure you want to delete"" in response.content assert b""alpha"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_shows_related(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) CascadingSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""The following objects"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_protected_not_allowed(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) ProtectedSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""You can't delete"" in response.content assert b""the following objects depend"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 403 assert Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert response.status_code == 403 response = client.post(delete_url) assert response.status_code == 403 @mark.django_db def test_create_with_inlines(client_with_perms): client = client_with_perms(""testapp.add_dragonfly"") response = client.post( ""/dragonfly/create/"", { ""name"": ""foobar"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 1, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 999, ""sighting_set-0-name"": ""Tokyo"", }, ) dragonfly = Dragonfly.objects.get(name=""foobar"") assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""detail""].reverse(dragonfly) assert dragonfly.age == 81 assert dragonfly.sighting_set.get().name == ""Tokyo"" @mark.django_db def test_only_popup_param_is_preserved_in_detail_links(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"", ""testapp.change_dragonfly"") instance = Dragonfly.objects.create(name=""alpha"", age=12) response = client.get( DragonflyViewSet().links[""list""].reverse(), data={""_popup"": ""id_test"", ""not_preserved"": ""nope""}, ) response_content = response.content.decode(""utf-8"") detail_url = DragonflyViewSet().links[""update""].reverse(instance) assert detail_url in response_content assert detail_url + ""?_popup=id_test"" in response_content assert detail_url + ""?_popup=id_test¬_preserved"" not in response_content ", https://github.com/django-beam/django-beam,43b2111f5c65937aa1e2cc9704b59796e982a805,tests/test_views.py::test_detail,tests/test_views.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from django.contrib.auth.models import Permission from pytest import mark from testapp.models import Dragonfly, Sighting, CascadingSighting, ProtectedSighting from testapp.views import DragonflyViewSet def user_with_perms(django_user_model, perms, username=""foo"", password=""bar""): user = django_user_model.objects.create_user(username=username, password=password) for perm in perms: app_label, codename = perm.split(""."") user.user_permissions.add( Permission.objects.get(content_type__app_label=app_label, codename=codename) ) return user @pytest.fixture def client_with_perms(client, django_user_model): def get_client_with_perms(*perms): user_with_perms(django_user_model, perms, username=""foo"", password=""bar"") client.login(username=""foo"", password=""bar"") return client return get_client_with_perms @mark.django_db def test_list(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert b""alpha"" in response.content assert b""omega"" in response.content @mark.django_db def test_list_requires_permission(client_with_perms): client = client_with_perms() Dragonfly.objects.create(name=""alpha"", age=12) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert response.status_code == 403 @mark.django_db def test_list_search(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?q=alpha"") assert b""alpha"" in response.content assert b""omega"" not in response.content @mark.django_db def test_list_sort(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=-name"") assert response.content.index(b""alpha"") > response.content.index(b""omega"") response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=name"") assert response.content.index(b""alpha"") < response.content.index(b""omega"") @mark.django_db def test_detail(client_with_perms): client = client_with_perms( ""testapp.view_dragonfly"", ""testapp.change_dragonfly"", ""testapp.delete_dragonfly"" ) alpha = Dragonfly.objects.create(name=""alpha"", age=47) Sighting.objects.create(name=""Berlin"", dragonfly=alpha) Sighting.objects.create(name=""Paris"", dragonfly=alpha) links = DragonflyViewSet().links response_content = client.get(links[""detail""].reverse(alpha)).content.decode( ""utf-8"" ) assert ""alpha"" in response_content assert ""Title of sightings"" assert ""Berlin"" in response_content assert ""Paris"" in response_content assert 'href=""{}""'.format(links[""list""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""update""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""delete""].reverse(alpha)) in response_content @mark.django_db def test_detail_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) links = DragonflyViewSet().links response = client.get(links[""detail""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_update(client_with_perms): client = client_with_perms(""testapp.change_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert b""alpha"" in response.content assert ""form"" in response.context assert response.context[""form""][""name""].value() == ""alpha"" @mark.django_db def test_update_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_delete(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""Are you sure you want to delete"" in response.content assert b""alpha"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_shows_related(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) CascadingSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""The following objects"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_protected_not_allowed(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) ProtectedSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""You can't delete"" in response.content assert b""the following objects depend"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 403 assert Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert response.status_code == 403 response = client.post(delete_url) assert response.status_code == 403 @mark.django_db def test_create_with_inlines(client_with_perms): client = client_with_perms(""testapp.add_dragonfly"") response = client.post( ""/dragonfly/create/"", { ""name"": ""foobar"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 1, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 999, ""sighting_set-0-name"": ""Tokyo"", }, ) dragonfly = Dragonfly.objects.get(name=""foobar"") assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""detail""].reverse(dragonfly) assert dragonfly.age == 81 assert dragonfly.sighting_set.get().name == ""Tokyo"" @mark.django_db def test_only_popup_param_is_preserved_in_detail_links(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"", ""testapp.change_dragonfly"") instance = Dragonfly.objects.create(name=""alpha"", age=12) response = client.get( DragonflyViewSet().links[""list""].reverse(), data={""_popup"": ""id_test"", ""not_preserved"": ""nope""}, ) response_content = response.content.decode(""utf-8"") detail_url = DragonflyViewSet().links[""update""].reverse(instance) assert detail_url in response_content assert detail_url + ""?_popup=id_test"" in response_content assert detail_url + ""?_popup=id_test¬_preserved"" not in response_content ", https://github.com/django-beam/django-beam,43b2111f5c65937aa1e2cc9704b59796e982a805,tests/test_views.py::test_list,tests/test_views.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from django.contrib.auth.models import Permission from pytest import mark from testapp.models import Dragonfly, Sighting, CascadingSighting, ProtectedSighting from testapp.views import DragonflyViewSet def user_with_perms(django_user_model, perms, username=""foo"", password=""bar""): user = django_user_model.objects.create_user(username=username, password=password) for perm in perms: app_label, codename = perm.split(""."") user.user_permissions.add( Permission.objects.get(content_type__app_label=app_label, codename=codename) ) return user @pytest.fixture def client_with_perms(client, django_user_model): def get_client_with_perms(*perms): user_with_perms(django_user_model, perms, username=""foo"", password=""bar"") client.login(username=""foo"", password=""bar"") return client return get_client_with_perms @mark.django_db def test_list(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert b""alpha"" in response.content assert b""omega"" in response.content @mark.django_db def test_list_requires_permission(client_with_perms): client = client_with_perms() Dragonfly.objects.create(name=""alpha"", age=12) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert response.status_code == 403 @mark.django_db def test_list_search(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?q=alpha"") assert b""alpha"" in response.content assert b""omega"" not in response.content @mark.django_db def test_list_sort(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=-name"") assert response.content.index(b""alpha"") > response.content.index(b""omega"") response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=name"") assert response.content.index(b""alpha"") < response.content.index(b""omega"") @mark.django_db def test_detail(client_with_perms): client = client_with_perms( ""testapp.view_dragonfly"", ""testapp.change_dragonfly"", ""testapp.delete_dragonfly"" ) alpha = Dragonfly.objects.create(name=""alpha"", age=47) Sighting.objects.create(name=""Berlin"", dragonfly=alpha) Sighting.objects.create(name=""Paris"", dragonfly=alpha) links = DragonflyViewSet().links response_content = client.get(links[""detail""].reverse(alpha)).content.decode( ""utf-8"" ) assert ""alpha"" in response_content assert ""Title of sightings"" assert ""Berlin"" in response_content assert ""Paris"" in response_content assert 'href=""{}""'.format(links[""list""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""update""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""delete""].reverse(alpha)) in response_content @mark.django_db def test_detail_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) links = DragonflyViewSet().links response = client.get(links[""detail""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_update(client_with_perms): client = client_with_perms(""testapp.change_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert b""alpha"" in response.content assert ""form"" in response.context assert response.context[""form""][""name""].value() == ""alpha"" @mark.django_db def test_update_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_delete(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""Are you sure you want to delete"" in response.content assert b""alpha"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_shows_related(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) CascadingSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""The following objects"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_protected_not_allowed(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) ProtectedSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""You can't delete"" in response.content assert b""the following objects depend"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 403 assert Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert response.status_code == 403 response = client.post(delete_url) assert response.status_code == 403 @mark.django_db def test_create_with_inlines(client_with_perms): client = client_with_perms(""testapp.add_dragonfly"") response = client.post( ""/dragonfly/create/"", { ""name"": ""foobar"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 1, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 999, ""sighting_set-0-name"": ""Tokyo"", }, ) dragonfly = Dragonfly.objects.get(name=""foobar"") assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""detail""].reverse(dragonfly) assert dragonfly.age == 81 assert dragonfly.sighting_set.get().name == ""Tokyo"" @mark.django_db def test_only_popup_param_is_preserved_in_detail_links(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"", ""testapp.change_dragonfly"") instance = Dragonfly.objects.create(name=""alpha"", age=12) response = client.get( DragonflyViewSet().links[""list""].reverse(), data={""_popup"": ""id_test"", ""not_preserved"": ""nope""}, ) response_content = response.content.decode(""utf-8"") detail_url = DragonflyViewSet().links[""update""].reverse(instance) assert detail_url in response_content assert detail_url + ""?_popup=id_test"" in response_content assert detail_url + ""?_popup=id_test¬_preserved"" not in response_content ", https://github.com/django-beam/django-beam,43b2111f5c65937aa1e2cc9704b59796e982a805,tests/test_views.py::test_list_search,tests/test_views.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from django.contrib.auth.models import Permission from pytest import mark from testapp.models import Dragonfly, Sighting, CascadingSighting, ProtectedSighting from testapp.views import DragonflyViewSet def user_with_perms(django_user_model, perms, username=""foo"", password=""bar""): user = django_user_model.objects.create_user(username=username, password=password) for perm in perms: app_label, codename = perm.split(""."") user.user_permissions.add( Permission.objects.get(content_type__app_label=app_label, codename=codename) ) return user @pytest.fixture def client_with_perms(client, django_user_model): def get_client_with_perms(*perms): user_with_perms(django_user_model, perms, username=""foo"", password=""bar"") client.login(username=""foo"", password=""bar"") return client return get_client_with_perms @mark.django_db def test_list(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert b""alpha"" in response.content assert b""omega"" in response.content @mark.django_db def test_list_requires_permission(client_with_perms): client = client_with_perms() Dragonfly.objects.create(name=""alpha"", age=12) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert response.status_code == 403 @mark.django_db def test_list_search(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?q=alpha"") assert b""alpha"" in response.content assert b""omega"" not in response.content @mark.django_db def test_list_sort(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=-name"") assert response.content.index(b""alpha"") > response.content.index(b""omega"") response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=name"") assert response.content.index(b""alpha"") < response.content.index(b""omega"") @mark.django_db def test_detail(client_with_perms): client = client_with_perms( ""testapp.view_dragonfly"", ""testapp.change_dragonfly"", ""testapp.delete_dragonfly"" ) alpha = Dragonfly.objects.create(name=""alpha"", age=47) Sighting.objects.create(name=""Berlin"", dragonfly=alpha) Sighting.objects.create(name=""Paris"", dragonfly=alpha) links = DragonflyViewSet().links response_content = client.get(links[""detail""].reverse(alpha)).content.decode( ""utf-8"" ) assert ""alpha"" in response_content assert ""Title of sightings"" assert ""Berlin"" in response_content assert ""Paris"" in response_content assert 'href=""{}""'.format(links[""list""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""update""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""delete""].reverse(alpha)) in response_content @mark.django_db def test_detail_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) links = DragonflyViewSet().links response = client.get(links[""detail""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_update(client_with_perms): client = client_with_perms(""testapp.change_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert b""alpha"" in response.content assert ""form"" in response.context assert response.context[""form""][""name""].value() == ""alpha"" @mark.django_db def test_update_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_delete(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""Are you sure you want to delete"" in response.content assert b""alpha"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_shows_related(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) CascadingSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""The following objects"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_protected_not_allowed(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) ProtectedSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""You can't delete"" in response.content assert b""the following objects depend"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 403 assert Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert response.status_code == 403 response = client.post(delete_url) assert response.status_code == 403 @mark.django_db def test_create_with_inlines(client_with_perms): client = client_with_perms(""testapp.add_dragonfly"") response = client.post( ""/dragonfly/create/"", { ""name"": ""foobar"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 1, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 999, ""sighting_set-0-name"": ""Tokyo"", }, ) dragonfly = Dragonfly.objects.get(name=""foobar"") assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""detail""].reverse(dragonfly) assert dragonfly.age == 81 assert dragonfly.sighting_set.get().name == ""Tokyo"" @mark.django_db def test_only_popup_param_is_preserved_in_detail_links(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"", ""testapp.change_dragonfly"") instance = Dragonfly.objects.create(name=""alpha"", age=12) response = client.get( DragonflyViewSet().links[""list""].reverse(), data={""_popup"": ""id_test"", ""not_preserved"": ""nope""}, ) response_content = response.content.decode(""utf-8"") detail_url = DragonflyViewSet().links[""update""].reverse(instance) assert detail_url in response_content assert detail_url + ""?_popup=id_test"" in response_content assert detail_url + ""?_popup=id_test¬_preserved"" not in response_content ", https://github.com/django-beam/django-beam,43b2111f5c65937aa1e2cc9704b59796e982a805,tests/test_views.py::test_list_sort,tests/test_views.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from django.contrib.auth.models import Permission from pytest import mark from testapp.models import Dragonfly, Sighting, CascadingSighting, ProtectedSighting from testapp.views import DragonflyViewSet def user_with_perms(django_user_model, perms, username=""foo"", password=""bar""): user = django_user_model.objects.create_user(username=username, password=password) for perm in perms: app_label, codename = perm.split(""."") user.user_permissions.add( Permission.objects.get(content_type__app_label=app_label, codename=codename) ) return user @pytest.fixture def client_with_perms(client, django_user_model): def get_client_with_perms(*perms): user_with_perms(django_user_model, perms, username=""foo"", password=""bar"") client.login(username=""foo"", password=""bar"") return client return get_client_with_perms @mark.django_db def test_list(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert b""alpha"" in response.content assert b""omega"" in response.content @mark.django_db def test_list_requires_permission(client_with_perms): client = client_with_perms() Dragonfly.objects.create(name=""alpha"", age=12) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert response.status_code == 403 @mark.django_db def test_list_search(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?q=alpha"") assert b""alpha"" in response.content assert b""omega"" not in response.content @mark.django_db def test_list_sort(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=-name"") assert response.content.index(b""alpha"") > response.content.index(b""omega"") response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=name"") assert response.content.index(b""alpha"") < response.content.index(b""omega"") @mark.django_db def test_detail(client_with_perms): client = client_with_perms( ""testapp.view_dragonfly"", ""testapp.change_dragonfly"", ""testapp.delete_dragonfly"" ) alpha = Dragonfly.objects.create(name=""alpha"", age=47) Sighting.objects.create(name=""Berlin"", dragonfly=alpha) Sighting.objects.create(name=""Paris"", dragonfly=alpha) links = DragonflyViewSet().links response_content = client.get(links[""detail""].reverse(alpha)).content.decode( ""utf-8"" ) assert ""alpha"" in response_content assert ""Title of sightings"" assert ""Berlin"" in response_content assert ""Paris"" in response_content assert 'href=""{}""'.format(links[""list""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""update""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""delete""].reverse(alpha)) in response_content @mark.django_db def test_detail_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) links = DragonflyViewSet().links response = client.get(links[""detail""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_update(client_with_perms): client = client_with_perms(""testapp.change_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert b""alpha"" in response.content assert ""form"" in response.context assert response.context[""form""][""name""].value() == ""alpha"" @mark.django_db def test_update_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_delete(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""Are you sure you want to delete"" in response.content assert b""alpha"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_shows_related(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) CascadingSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""The following objects"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_protected_not_allowed(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) ProtectedSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""You can't delete"" in response.content assert b""the following objects depend"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 403 assert Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert response.status_code == 403 response = client.post(delete_url) assert response.status_code == 403 @mark.django_db def test_create_with_inlines(client_with_perms): client = client_with_perms(""testapp.add_dragonfly"") response = client.post( ""/dragonfly/create/"", { ""name"": ""foobar"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 1, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 999, ""sighting_set-0-name"": ""Tokyo"", }, ) dragonfly = Dragonfly.objects.get(name=""foobar"") assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""detail""].reverse(dragonfly) assert dragonfly.age == 81 assert dragonfly.sighting_set.get().name == ""Tokyo"" @mark.django_db def test_only_popup_param_is_preserved_in_detail_links(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"", ""testapp.change_dragonfly"") instance = Dragonfly.objects.create(name=""alpha"", age=12) response = client.get( DragonflyViewSet().links[""list""].reverse(), data={""_popup"": ""id_test"", ""not_preserved"": ""nope""}, ) response_content = response.content.decode(""utf-8"") detail_url = DragonflyViewSet().links[""update""].reverse(instance) assert detail_url in response_content assert detail_url + ""?_popup=id_test"" in response_content assert detail_url + ""?_popup=id_test¬_preserved"" not in response_content ", https://github.com/django-beam/django-beam,43b2111f5c65937aa1e2cc9704b59796e982a805,tests/test_views.py::test_only_popup_param_is_preserved_in_detail_links,tests/test_views.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from django.contrib.auth.models import Permission from pytest import mark from testapp.models import Dragonfly, Sighting, CascadingSighting, ProtectedSighting from testapp.views import DragonflyViewSet def user_with_perms(django_user_model, perms, username=""foo"", password=""bar""): user = django_user_model.objects.create_user(username=username, password=password) for perm in perms: app_label, codename = perm.split(""."") user.user_permissions.add( Permission.objects.get(content_type__app_label=app_label, codename=codename) ) return user @pytest.fixture def client_with_perms(client, django_user_model): def get_client_with_perms(*perms): user_with_perms(django_user_model, perms, username=""foo"", password=""bar"") client.login(username=""foo"", password=""bar"") return client return get_client_with_perms @mark.django_db def test_list(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert b""alpha"" in response.content assert b""omega"" in response.content @mark.django_db def test_list_requires_permission(client_with_perms): client = client_with_perms() Dragonfly.objects.create(name=""alpha"", age=12) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert response.status_code == 403 @mark.django_db def test_list_search(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?q=alpha"") assert b""alpha"" in response.content assert b""omega"" not in response.content @mark.django_db def test_list_sort(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=-name"") assert response.content.index(b""alpha"") > response.content.index(b""omega"") response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=name"") assert response.content.index(b""alpha"") < response.content.index(b""omega"") @mark.django_db def test_detail(client_with_perms): client = client_with_perms( ""testapp.view_dragonfly"", ""testapp.change_dragonfly"", ""testapp.delete_dragonfly"" ) alpha = Dragonfly.objects.create(name=""alpha"", age=47) Sighting.objects.create(name=""Berlin"", dragonfly=alpha) Sighting.objects.create(name=""Paris"", dragonfly=alpha) links = DragonflyViewSet().links response_content = client.get(links[""detail""].reverse(alpha)).content.decode( ""utf-8"" ) assert ""alpha"" in response_content assert ""Title of sightings"" assert ""Berlin"" in response_content assert ""Paris"" in response_content assert 'href=""{}""'.format(links[""list""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""update""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""delete""].reverse(alpha)) in response_content @mark.django_db def test_detail_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) links = DragonflyViewSet().links response = client.get(links[""detail""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_update(client_with_perms): client = client_with_perms(""testapp.change_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert b""alpha"" in response.content assert ""form"" in response.context assert response.context[""form""][""name""].value() == ""alpha"" @mark.django_db def test_update_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_delete(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""Are you sure you want to delete"" in response.content assert b""alpha"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_shows_related(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) CascadingSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""The following objects"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_protected_not_allowed(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) ProtectedSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""You can't delete"" in response.content assert b""the following objects depend"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 403 assert Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert response.status_code == 403 response = client.post(delete_url) assert response.status_code == 403 @mark.django_db def test_create_with_inlines(client_with_perms): client = client_with_perms(""testapp.add_dragonfly"") response = client.post( ""/dragonfly/create/"", { ""name"": ""foobar"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 1, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 999, ""sighting_set-0-name"": ""Tokyo"", }, ) dragonfly = Dragonfly.objects.get(name=""foobar"") assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""detail""].reverse(dragonfly) assert dragonfly.age == 81 assert dragonfly.sighting_set.get().name == ""Tokyo"" @mark.django_db def test_only_popup_param_is_preserved_in_detail_links(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"", ""testapp.change_dragonfly"") instance = Dragonfly.objects.create(name=""alpha"", age=12) response = client.get( DragonflyViewSet().links[""list""].reverse(), data={""_popup"": ""id_test"", ""not_preserved"": ""nope""}, ) response_content = response.content.decode(""utf-8"") detail_url = DragonflyViewSet().links[""update""].reverse(instance) assert detail_url in response_content assert detail_url + ""?_popup=id_test"" in response_content assert detail_url + ""?_popup=id_test¬_preserved"" not in response_content ", https://github.com/django-beam/django-beam,43b2111f5c65937aa1e2cc9704b59796e982a805,tests/test_views.py::test_update,tests/test_views.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from django.contrib.auth.models import Permission from pytest import mark from testapp.models import Dragonfly, Sighting, CascadingSighting, ProtectedSighting from testapp.views import DragonflyViewSet def user_with_perms(django_user_model, perms, username=""foo"", password=""bar""): user = django_user_model.objects.create_user(username=username, password=password) for perm in perms: app_label, codename = perm.split(""."") user.user_permissions.add( Permission.objects.get(content_type__app_label=app_label, codename=codename) ) return user @pytest.fixture def client_with_perms(client, django_user_model): def get_client_with_perms(*perms): user_with_perms(django_user_model, perms, username=""foo"", password=""bar"") client.login(username=""foo"", password=""bar"") return client return get_client_with_perms @mark.django_db def test_list(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert b""alpha"" in response.content assert b""omega"" in response.content @mark.django_db def test_list_requires_permission(client_with_perms): client = client_with_perms() Dragonfly.objects.create(name=""alpha"", age=12) response = client.get(DragonflyViewSet().links[""list""].reverse()) assert response.status_code == 403 @mark.django_db def test_list_search(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?q=alpha"") assert b""alpha"" in response.content assert b""omega"" not in response.content @mark.django_db def test_list_sort(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"") Dragonfly.objects.create(name=""alpha"", age=12) Dragonfly.objects.create(name=""omega"", age=99) response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=-name"") assert response.content.index(b""alpha"") > response.content.index(b""omega"") response = client.get(DragonflyViewSet().links[""list""].reverse() + ""?o=name"") assert response.content.index(b""alpha"") < response.content.index(b""omega"") @mark.django_db def test_detail(client_with_perms): client = client_with_perms( ""testapp.view_dragonfly"", ""testapp.change_dragonfly"", ""testapp.delete_dragonfly"" ) alpha = Dragonfly.objects.create(name=""alpha"", age=47) Sighting.objects.create(name=""Berlin"", dragonfly=alpha) Sighting.objects.create(name=""Paris"", dragonfly=alpha) links = DragonflyViewSet().links response_content = client.get(links[""detail""].reverse(alpha)).content.decode( ""utf-8"" ) assert ""alpha"" in response_content assert ""Title of sightings"" assert ""Berlin"" in response_content assert ""Paris"" in response_content assert 'href=""{}""'.format(links[""list""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""update""].reverse(alpha)) in response_content assert 'href=""{}""'.format(links[""delete""].reverse(alpha)) in response_content @mark.django_db def test_detail_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) links = DragonflyViewSet().links response = client.get(links[""detail""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_update(client_with_perms): client = client_with_perms(""testapp.change_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert b""alpha"" in response.content assert ""form"" in response.context assert response.context[""form""][""name""].value() == ""alpha"" @mark.django_db def test_update_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) response = client.get(DragonflyViewSet().links[""update""].reverse(alpha)) assert response.status_code == 403 @mark.django_db def test_delete(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""Are you sure you want to delete"" in response.content assert b""alpha"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_shows_related(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) CascadingSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""The following objects"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""list""].reverse() assert not Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_protected_not_allowed(client_with_perms): client = client_with_perms(""testapp.delete_dragonfly"") alpha = Dragonfly.objects.create(name=""alpha"", age=47) ProtectedSighting.objects.create(dragonfly=alpha, name=""A related sighting"") delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert b""You can't delete"" in response.content assert b""the following objects depend"" in response.content assert b""sighting"" in response.content response = client.post(delete_url) assert response.status_code == 403 assert Dragonfly.objects.filter(name=""alpha"").exists() @mark.django_db def test_delete_requires_permission(client_with_perms): client = client_with_perms() alpha = Dragonfly.objects.create(name=""alpha"", age=47) delete_url = DragonflyViewSet().links[""delete""].reverse(alpha) response = client.get(delete_url) assert response.status_code == 403 response = client.post(delete_url) assert response.status_code == 403 @mark.django_db def test_create_with_inlines(client_with_perms): client = client_with_perms(""testapp.add_dragonfly"") response = client.post( ""/dragonfly/create/"", { ""name"": ""foobar"", ""age"": 81, ""sighting_set-TOTAL_FORMS"": 1, ""sighting_set-INITIAL_FORMS"": 0, ""sighting_set-MIN_NUM_FORMS"": 0, ""sighting_set-MAX_NUM_FORMS"": 999, ""sighting_set-0-name"": ""Tokyo"", }, ) dragonfly = Dragonfly.objects.get(name=""foobar"") assert response.status_code == 302 assert response[""location""] == DragonflyViewSet().links[""detail""].reverse(dragonfly) assert dragonfly.age == 81 assert dragonfly.sighting_set.get().name == ""Tokyo"" @mark.django_db def test_only_popup_param_is_preserved_in_detail_links(client_with_perms): client = client_with_perms(""testapp.view_dragonfly"", ""testapp.change_dragonfly"") instance = Dragonfly.objects.create(name=""alpha"", age=12) response = client.get( DragonflyViewSet().links[""list""].reverse(), data={""_popup"": ""id_test"", ""not_preserved"": ""nope""}, ) response_content = response.content.decode(""utf-8"") detail_url = DragonflyViewSet().links[""update""].reverse(instance) assert detail_url in response_content assert detail_url + ""?_popup=id_test"" in response_content assert detail_url + ""?_popup=id_test¬_preserved"" not in response_content ", https://github.com/djrobstep/logx,c53fabbc160fb8d70fa878684ea36f0c22fd5caa,tests/test_logx.py::test_formatted_output,tests/test_logx.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from logx import log, print_diagram import json from datetime import datetime def load_config(path): import io import os import yaml import logging if path and os.path.exists(path): with io.open(path) as f: conf = f.read() logging.config.dictConfig(yaml.load(conf)) def m(caplog): return caplog.records[-1].message def test_plain_output(caplog, capsys): logger_name = log.current_logger_name() assert logger_name == ""tests.test_logx"" FIRST_MESSAGE = ""first message"" log.info(FIRST_MESSAGE) assert m(caplog) == FIRST_MESSAGE out, err = capsys.readouterr() assert out == FIRST_MESSAGE + ""\n"" assert not err def test_formatted_output(caplog, capsys): log.set_default_format() log.debug(""debug"") assert m(caplog) == ""debug"" out, err = capsys.readouterr() assert out.endswith(""debug\n"") assert ""DEBUG [tests.test_logx.test_formatted_output:"" in out assert not err def test_set_format(caplog, capsys): formatstring = '{ ""timestamp"": ""%(asctime)s"", ""severity"": ""%(levelname)s"", ""name"": ""%(name)s"", ""funcName"": ""%(funcName)s"", ""lineNo"": ""%(lineno)d"", ""message"": ""%(message)s""}' datefmt = ""%Y-%m-%dT%I:%M:%SZ"" log.set_format(formatstring, datefmt=datefmt) log.debug(""log stuff"") output = json.loads(capsys.readouterr().out) assert datetime.strptime(output[""timestamp""], datefmt) is not None assert output.keys() == json.loads(formatstring).keys() def test_level_change_output(caplog, capsys): log.set_default_format() log.set_level(""warn"") log.warn(""warn"") log.debug(""debug"") out, err = capsys.readouterr() assert ""warn"" in err assert ""debug"" not in err and ""debug"" not in out def test_null_handler(caplog, capsys): log.set_null_handler() log.warn(""warn"") log.debug(""debug"") out, err = capsys.readouterr() assert ""warn"" not in err and ""warn"" not in out assert ""debug"" not in err and ""debug"" not in out log.clear_null_handler() print_diagram() log.warn(""warn"") log.debug(""debug"") out, err = capsys.readouterr() assert ""warn"" in err and ""warn"" not in out assert ""debug"" not in err and ""debug"" in out ", https://github.com/djrobstep/logx,c53fabbc160fb8d70fa878684ea36f0c22fd5caa,tests/test_logx.py::test_plain_output,tests/test_logx.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from logx import log, print_diagram import json from datetime import datetime def load_config(path): import io import os import yaml import logging if path and os.path.exists(path): with io.open(path) as f: conf = f.read() logging.config.dictConfig(yaml.load(conf)) def m(caplog): return caplog.records[-1].message def test_plain_output(caplog, capsys): logger_name = log.current_logger_name() assert logger_name == ""tests.test_logx"" FIRST_MESSAGE = ""first message"" log.info(FIRST_MESSAGE) assert m(caplog) == FIRST_MESSAGE out, err = capsys.readouterr() assert out == FIRST_MESSAGE + ""\n"" assert not err def test_formatted_output(caplog, capsys): log.set_default_format() log.debug(""debug"") assert m(caplog) == ""debug"" out, err = capsys.readouterr() assert out.endswith(""debug\n"") assert ""DEBUG [tests.test_logx.test_formatted_output:"" in out assert not err def test_set_format(caplog, capsys): formatstring = '{ ""timestamp"": ""%(asctime)s"", ""severity"": ""%(levelname)s"", ""name"": ""%(name)s"", ""funcName"": ""%(funcName)s"", ""lineNo"": ""%(lineno)d"", ""message"": ""%(message)s""}' datefmt = ""%Y-%m-%dT%I:%M:%SZ"" log.set_format(formatstring, datefmt=datefmt) log.debug(""log stuff"") output = json.loads(capsys.readouterr().out) assert datetime.strptime(output[""timestamp""], datefmt) is not None assert output.keys() == json.loads(formatstring).keys() def test_level_change_output(caplog, capsys): log.set_default_format() log.set_level(""warn"") log.warn(""warn"") log.debug(""debug"") out, err = capsys.readouterr() assert ""warn"" in err assert ""debug"" not in err and ""debug"" not in out def test_null_handler(caplog, capsys): log.set_null_handler() log.warn(""warn"") log.debug(""debug"") out, err = capsys.readouterr() assert ""warn"" not in err and ""warn"" not in out assert ""debug"" not in err and ""debug"" not in out log.clear_null_handler() print_diagram() log.warn(""warn"") log.debug(""debug"") out, err = capsys.readouterr() assert ""warn"" in err and ""warn"" not in out assert ""debug"" not in err and ""debug"" in out ", https://github.com/djrobstep/logx,c53fabbc160fb8d70fa878684ea36f0c22fd5caa,tests/test_logx.py::test_set_format,tests/test_logx.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from logx import log, print_diagram import json from datetime import datetime def load_config(path): import io import os import yaml import logging if path and os.path.exists(path): with io.open(path) as f: conf = f.read() logging.config.dictConfig(yaml.load(conf)) def m(caplog): return caplog.records[-1].message def test_plain_output(caplog, capsys): logger_name = log.current_logger_name() assert logger_name == ""tests.test_logx"" FIRST_MESSAGE = ""first message"" log.info(FIRST_MESSAGE) assert m(caplog) == FIRST_MESSAGE out, err = capsys.readouterr() assert out == FIRST_MESSAGE + ""\n"" assert not err def test_formatted_output(caplog, capsys): log.set_default_format() log.debug(""debug"") assert m(caplog) == ""debug"" out, err = capsys.readouterr() assert out.endswith(""debug\n"") assert ""DEBUG [tests.test_logx.test_formatted_output:"" in out assert not err def test_set_format(caplog, capsys): formatstring = '{ ""timestamp"": ""%(asctime)s"", ""severity"": ""%(levelname)s"", ""name"": ""%(name)s"", ""funcName"": ""%(funcName)s"", ""lineNo"": ""%(lineno)d"", ""message"": ""%(message)s""}' datefmt = ""%Y-%m-%dT%I:%M:%SZ"" log.set_format(formatstring, datefmt=datefmt) log.debug(""log stuff"") output = json.loads(capsys.readouterr().out) assert datetime.strptime(output[""timestamp""], datefmt) is not None assert output.keys() == json.loads(formatstring).keys() def test_level_change_output(caplog, capsys): log.set_default_format() log.set_level(""warn"") log.warn(""warn"") log.debug(""debug"") out, err = capsys.readouterr() assert ""warn"" in err assert ""debug"" not in err and ""debug"" not in out def test_null_handler(caplog, capsys): log.set_null_handler() log.warn(""warn"") log.debug(""debug"") out, err = capsys.readouterr() assert ""warn"" not in err and ""warn"" not in out assert ""debug"" not in err and ""debug"" not in out log.clear_null_handler() print_diagram() log.warn(""warn"") log.debug(""debug"") out, err = capsys.readouterr() assert ""warn"" in err and ""warn"" not in out assert ""debug"" not in err and ""debug"" in out ", https://github.com/dmarkey/aiopylimit,b4075a8ac30dbbef59a2243f618a35a2f54b590c,aiopylimit/tests/test_aiopylimit.py::TestPyLimit::test_exception,aiopylimit/tests/test_aiopylimit.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from aiopylimit import AIOPyRateLimit from aiopylimit import AIOPyRateLimitException import asynctest import asyncio class TestPyLimit(asynctest.TestCase): async def test_exception(self): limit = AIOPyRateLimit(10, 10) await self.assertAsyncRaises(AIOPyRateLimitException, limit.attempt('test_namespace')) async def test_throttle(self): AIOPyRateLimit.init(redis_host=""localhost"", redis_port=6379, force_new_connection=True) limit = AIOPyRateLimit(10, 10) for x in range(0, 20): await asyncio.sleep(.5) if x < 10: self.assertTrue(await limit.attempt('test_namespace')) else: self.assertFalse(await limit.attempt('test_namespace')) await asyncio.sleep(6) self.assertTrue(await limit.attempt('test_namespace')) async def test_peek(self): AIOPyRateLimit.init(redis_host=""localhost"", redis_port=6379, force_new_connection=True) limit = AIOPyRateLimit(10, 10) for x in range(0, 10): self.assertTrue(await limit.attempt('test_namespace2')) self.assertTrue(await limit.is_rate_limited('test_namespace2')) await asyncio.sleep(10) self.assertFalse(await limit.is_rate_limited('test_namespace2')) ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_json,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_json_blacklist,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_json_pretty,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_json_pretty_blacklist,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_json_pretty_whitelist,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_json_whitelist,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_simple,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_simple_blacklist,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_simple_whitelist,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_sonar_blacklist,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_sonar_whitelist,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_tabular,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_tabular_blacklist,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmuhs/mythx-cli,f6b3d20d51ca1cfaed3cd45226bd1074388d42b9,tests/test_report.py::test_report_tabular_whitelist,tests/test_report.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from click.testing import CliRunner from mythx_models.response import AnalysisInputResponse, DetectedIssuesResponse from mythx_cli.cli import cli from .common import get_test_case, mock_context INPUT_RESPONSE = get_test_case( ""testdata/analysis-input-response.json"", AnalysisInputResponse ) ISSUES_RESPONSE = get_test_case( ""testdata/detected-issues-response.json"", DetectedIssuesResponse ) ISSUES_SIMPLE = get_test_case(""testdata/detected-issues-simple.txt"", raw=True) ISSUES_TABLE = get_test_case(""testdata/detected-issues-table.txt"", raw=True) ISSUES_SONAR = get_test_case(""testdata/detected-issues-sonar.json"") def test_report_tabular(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2""] ) assert result.output == ISSUES_TABLE assert result.exit_code == 0 def test_report_tabular_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_tabular_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" in result.output ) assert result.exit_code == 0 def test_report_tabular_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert ( ""/home/spoons/diligence/mythx-qa/land/contracts/estate/EstateStorage.sol"" not in result.output ) assert result.exit_code == 0 def test_report_sonar(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output) == ISSUES_SONAR assert result.exit_code == 0 def test_report_sonar_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] == ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_sonar_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""sonar"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all(x[""forRule""] != ""SWC-110"" for x in json.loads(result.output)) assert result.exit_code == 0 def test_report_json(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert json.loads(result.output)[0] == json.loads(ISSUES_RESPONSE.to_json()) assert result.exit_code == 0 def test_report_json_pretty_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--swc-whitelist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] == ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_json_pretty_filter(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""json-pretty"", ""analysis"", ""report"", ""--min-severity"", ""high"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert all( x[""swcID""] != ""SWC-110"" for x in json.loads(result.output)[0][0][""issues""] ) assert result.exit_code == 0 def test_report_simple(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert result.output == ISSUES_SIMPLE assert result.exit_code == 0 def test_report_simple_blacklist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ""simple"", ""analysis"", ""report"", ""--swc-blacklist"", ""SWC-110"", ""ab9092f7-54d0-480f-9b63-1bb1508280e2"", ], ) assert ""Assert Violation"" not in result.output assert result.exit_code == 0 def test_report_simple_whitelist(): runner = CliRunner() with mock_context(): result = runner.invoke( cli, [ ""--format"", ", https://github.com/dmytrostriletskyi/accessify,6b7cf8657ffe18cd6a43c6cfb73b071084f0331e,tests/interfaces/test_implements.py::test_implements_not_cls_convention,tests/interfaces/test_implements.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Provide tests for implements interface implementation. """""" import pytest from accessify.access import ( private, protected, ) from accessify.errors import ( ImplementedInterfaceMemberHasIncorrectAccessModifierException, InterfaceMemberHasNotBeenImplementedException, ) from accessify.interfaces import implements from tests.utils import custom_decorator def test_implements(): """""" Case: implement interfaces. Expect: no errors during class initialization. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass @staticmethod def eat(food, *args, allergy=None, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass @implements(HumanBasicsInterface, HumanNameInterface, HumanSoulInterface) class HumanWithImplementation: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return @staticmethod def eat(food, *args, allergy=None, **kwargs): pass def love(self, who, *args, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert HumanWithImplementation() is not None def test_not_implements_private_access(): """""" Case: do not implement interface member with mismatched private access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass assert 'HumanSoul.dream(about, args, kwargs) mismatches ' \ 'HumanSoulInterface.dream() member access modifier.' == error.value.message def test_not_implements_protected_access(): """""" Case: do not implement interface member with mismatched protected access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert 'HumanSoul.die(cls, when, args, kwargs) mismatches ' \ 'HumanSoulInterface.die() member access modifier.' == error.value.message def test_implements_no_implementation_getter(): """""" Case: do not implement interface member that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_setter(): """""" Case: do not implement interface member that is setter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self, new_name)' == error.value.message def test_implements_no_implementation_deleter(): """""" Case: do not implement interface member that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.deleter def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_static_method(): """""" Case: do not implement interface member that is static method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @staticmethod def eat(food, *args, allergy=None, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.eat(food, args, allergy, kwargs)' == error.value.message def test_implements_no_implementation_instance_method(): """""" Case: do not implement interface member that is method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.love(self, who, args, kwargs)' == error.value.message def test_implements_not_cls_convention(): """""" Case: do not implement interface member, which do not follow naming convention, that is class method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @classmethod def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention(): """""" Case: do not implement interface member, which do not follow naming convention. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention_in_getter(): """""" Case: do not implement interface member, which do not follow naming convention, that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(this)' == error.value.message def test_implements_not_self_convention_in_deleter(): """""" Case: do not implement interface member, which do not follow naming convention, that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return @name.deleter def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(this): return assert 'class HumanWithoutImplementation ", https://github.com/dmytrostriletskyi/accessify,6b7cf8657ffe18cd6a43c6cfb73b071084f0331e,tests/interfaces/test_implements.py::test_implements_not_self_convention,tests/interfaces/test_implements.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Provide tests for implements interface implementation. """""" import pytest from accessify.access import ( private, protected, ) from accessify.errors import ( ImplementedInterfaceMemberHasIncorrectAccessModifierException, InterfaceMemberHasNotBeenImplementedException, ) from accessify.interfaces import implements from tests.utils import custom_decorator def test_implements(): """""" Case: implement interfaces. Expect: no errors during class initialization. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass @staticmethod def eat(food, *args, allergy=None, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass @implements(HumanBasicsInterface, HumanNameInterface, HumanSoulInterface) class HumanWithImplementation: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return @staticmethod def eat(food, *args, allergy=None, **kwargs): pass def love(self, who, *args, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert HumanWithImplementation() is not None def test_not_implements_private_access(): """""" Case: do not implement interface member with mismatched private access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass assert 'HumanSoul.dream(about, args, kwargs) mismatches ' \ 'HumanSoulInterface.dream() member access modifier.' == error.value.message def test_not_implements_protected_access(): """""" Case: do not implement interface member with mismatched protected access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert 'HumanSoul.die(cls, when, args, kwargs) mismatches ' \ 'HumanSoulInterface.die() member access modifier.' == error.value.message def test_implements_no_implementation_getter(): """""" Case: do not implement interface member that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_setter(): """""" Case: do not implement interface member that is setter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self, new_name)' == error.value.message def test_implements_no_implementation_deleter(): """""" Case: do not implement interface member that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.deleter def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_static_method(): """""" Case: do not implement interface member that is static method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @staticmethod def eat(food, *args, allergy=None, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.eat(food, args, allergy, kwargs)' == error.value.message def test_implements_no_implementation_instance_method(): """""" Case: do not implement interface member that is method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.love(self, who, args, kwargs)' == error.value.message def test_implements_not_cls_convention(): """""" Case: do not implement interface member, which do not follow naming convention, that is class method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @classmethod def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention(): """""" Case: do not implement interface member, which do not follow naming convention. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention_in_getter(): """""" Case: do not implement interface member, which do not follow naming convention, that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(this)' == error.value.message def test_implements_not_self_convention_in_deleter(): """""" Case: do not implement interface member, which do not follow naming convention, that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return @name.deleter def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(this): return assert 'class HumanWithoutImplementation ", https://github.com/dmytrostriletskyi/accessify,6b7cf8657ffe18cd6a43c6cfb73b071084f0331e,tests/interfaces/test_implements.py::test_implements_not_self_convention_in_deleter,tests/interfaces/test_implements.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Provide tests for implements interface implementation. """""" import pytest from accessify.access import ( private, protected, ) from accessify.errors import ( ImplementedInterfaceMemberHasIncorrectAccessModifierException, InterfaceMemberHasNotBeenImplementedException, ) from accessify.interfaces import implements from tests.utils import custom_decorator def test_implements(): """""" Case: implement interfaces. Expect: no errors during class initialization. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass @staticmethod def eat(food, *args, allergy=None, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass @implements(HumanBasicsInterface, HumanNameInterface, HumanSoulInterface) class HumanWithImplementation: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return @staticmethod def eat(food, *args, allergy=None, **kwargs): pass def love(self, who, *args, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert HumanWithImplementation() is not None def test_not_implements_private_access(): """""" Case: do not implement interface member with mismatched private access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass assert 'HumanSoul.dream(about, args, kwargs) mismatches ' \ 'HumanSoulInterface.dream() member access modifier.' == error.value.message def test_not_implements_protected_access(): """""" Case: do not implement interface member with mismatched protected access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert 'HumanSoul.die(cls, when, args, kwargs) mismatches ' \ 'HumanSoulInterface.die() member access modifier.' == error.value.message def test_implements_no_implementation_getter(): """""" Case: do not implement interface member that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_setter(): """""" Case: do not implement interface member that is setter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self, new_name)' == error.value.message def test_implements_no_implementation_deleter(): """""" Case: do not implement interface member that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.deleter def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_static_method(): """""" Case: do not implement interface member that is static method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @staticmethod def eat(food, *args, allergy=None, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.eat(food, args, allergy, kwargs)' == error.value.message def test_implements_no_implementation_instance_method(): """""" Case: do not implement interface member that is method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.love(self, who, args, kwargs)' == error.value.message def test_implements_not_cls_convention(): """""" Case: do not implement interface member, which do not follow naming convention, that is class method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @classmethod def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention(): """""" Case: do not implement interface member, which do not follow naming convention. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention_in_getter(): """""" Case: do not implement interface member, which do not follow naming convention, that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(this)' == error.value.message def test_implements_not_self_convention_in_deleter(): """""" Case: do not implement interface member, which do not follow naming convention, that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return @name.deleter def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(this): return assert 'class HumanWithoutImplementation ", https://github.com/dmytrostriletskyi/accessify,6b7cf8657ffe18cd6a43c6cfb73b071084f0331e,tests/interfaces/test_implements.py::test_implements_not_self_convention_in_getter,tests/interfaces/test_implements.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Provide tests for implements interface implementation. """""" import pytest from accessify.access import ( private, protected, ) from accessify.errors import ( ImplementedInterfaceMemberHasIncorrectAccessModifierException, InterfaceMemberHasNotBeenImplementedException, ) from accessify.interfaces import implements from tests.utils import custom_decorator def test_implements(): """""" Case: implement interfaces. Expect: no errors during class initialization. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass @staticmethod def eat(food, *args, allergy=None, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass @implements(HumanBasicsInterface, HumanNameInterface, HumanSoulInterface) class HumanWithImplementation: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return @staticmethod def eat(food, *args, allergy=None, **kwargs): pass def love(self, who, *args, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert HumanWithImplementation() is not None def test_not_implements_private_access(): """""" Case: do not implement interface member with mismatched private access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass assert 'HumanSoul.dream(about, args, kwargs) mismatches ' \ 'HumanSoulInterface.dream() member access modifier.' == error.value.message def test_not_implements_protected_access(): """""" Case: do not implement interface member with mismatched protected access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert 'HumanSoul.die(cls, when, args, kwargs) mismatches ' \ 'HumanSoulInterface.die() member access modifier.' == error.value.message def test_implements_no_implementation_getter(): """""" Case: do not implement interface member that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_setter(): """""" Case: do not implement interface member that is setter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self, new_name)' == error.value.message def test_implements_no_implementation_deleter(): """""" Case: do not implement interface member that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.deleter def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_static_method(): """""" Case: do not implement interface member that is static method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @staticmethod def eat(food, *args, allergy=None, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.eat(food, args, allergy, kwargs)' == error.value.message def test_implements_no_implementation_instance_method(): """""" Case: do not implement interface member that is method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.love(self, who, args, kwargs)' == error.value.message def test_implements_not_cls_convention(): """""" Case: do not implement interface member, which do not follow naming convention, that is class method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @classmethod def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention(): """""" Case: do not implement interface member, which do not follow naming convention. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention_in_getter(): """""" Case: do not implement interface member, which do not follow naming convention, that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(this)' == error.value.message def test_implements_not_self_convention_in_deleter(): """""" Case: do not implement interface member, which do not follow naming convention, that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return @name.deleter def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(this): return assert 'class HumanWithoutImplementation ", https://github.com/dmytrostriletskyi/accessify,6b7cf8657ffe18cd6a43c6cfb73b071084f0331e,tests/interfaces/test_implements.py::test_implements_not_self_convention_in_setter,tests/interfaces/test_implements.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Provide tests for implements interface implementation. """""" import pytest from accessify.access import ( private, protected, ) from accessify.errors import ( ImplementedInterfaceMemberHasIncorrectAccessModifierException, InterfaceMemberHasNotBeenImplementedException, ) from accessify.interfaces import implements from tests.utils import custom_decorator def test_implements(): """""" Case: implement interfaces. Expect: no errors during class initialization. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass @staticmethod def eat(food, *args, allergy=None, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass @implements(HumanBasicsInterface, HumanNameInterface, HumanSoulInterface) class HumanWithImplementation: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return @staticmethod def eat(food, *args, allergy=None, **kwargs): pass def love(self, who, *args, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert HumanWithImplementation() is not None def test_not_implements_private_access(): """""" Case: do not implement interface member with mismatched private access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass assert 'HumanSoul.dream(about, args, kwargs) mismatches ' \ 'HumanSoulInterface.dream() member access modifier.' == error.value.message def test_not_implements_protected_access(): """""" Case: do not implement interface member with mismatched protected access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert 'HumanSoul.die(cls, when, args, kwargs) mismatches ' \ 'HumanSoulInterface.die() member access modifier.' == error.value.message def test_implements_no_implementation_getter(): """""" Case: do not implement interface member that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_setter(): """""" Case: do not implement interface member that is setter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self, new_name)' == error.value.message def test_implements_no_implementation_deleter(): """""" Case: do not implement interface member that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.deleter def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_static_method(): """""" Case: do not implement interface member that is static method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @staticmethod def eat(food, *args, allergy=None, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.eat(food, args, allergy, kwargs)' == error.value.message def test_implements_no_implementation_instance_method(): """""" Case: do not implement interface member that is method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.love(self, who, args, kwargs)' == error.value.message def test_implements_not_cls_convention(): """""" Case: do not implement interface member, which do not follow naming convention, that is class method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @classmethod def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention(): """""" Case: do not implement interface member, which do not follow naming convention. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention_in_getter(): """""" Case: do not implement interface member, which do not follow naming convention, that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(this)' == error.value.message def test_implements_not_self_convention_in_deleter(): """""" Case: do not implement interface member, which do not follow naming convention, that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return @name.deleter def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(this): return assert 'class HumanWithoutImplementation ", https://github.com/dmytrostriletskyi/accessify,6b7cf8657ffe18cd6a43c6cfb73b071084f0331e,tests/interfaces/test_implements.py::test_implements_no_implementation_deleter,tests/interfaces/test_implements.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Provide tests for implements interface implementation. """""" import pytest from accessify.access import ( private, protected, ) from accessify.errors import ( ImplementedInterfaceMemberHasIncorrectAccessModifierException, InterfaceMemberHasNotBeenImplementedException, ) from accessify.interfaces import implements from tests.utils import custom_decorator def test_implements(): """""" Case: implement interfaces. Expect: no errors during class initialization. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass @staticmethod def eat(food, *args, allergy=None, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass @implements(HumanBasicsInterface, HumanNameInterface, HumanSoulInterface) class HumanWithImplementation: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return @staticmethod def eat(food, *args, allergy=None, **kwargs): pass def love(self, who, *args, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert HumanWithImplementation() is not None def test_not_implements_private_access(): """""" Case: do not implement interface member with mismatched private access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass assert 'HumanSoul.dream(about, args, kwargs) mismatches ' \ 'HumanSoulInterface.dream() member access modifier.' == error.value.message def test_not_implements_protected_access(): """""" Case: do not implement interface member with mismatched protected access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert 'HumanSoul.die(cls, when, args, kwargs) mismatches ' \ 'HumanSoulInterface.die() member access modifier.' == error.value.message def test_implements_no_implementation_getter(): """""" Case: do not implement interface member that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_setter(): """""" Case: do not implement interface member that is setter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self, new_name)' == error.value.message def test_implements_no_implementation_deleter(): """""" Case: do not implement interface member that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.deleter def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_static_method(): """""" Case: do not implement interface member that is static method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @staticmethod def eat(food, *args, allergy=None, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.eat(food, args, allergy, kwargs)' == error.value.message def test_implements_no_implementation_instance_method(): """""" Case: do not implement interface member that is method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.love(self, who, args, kwargs)' == error.value.message def test_implements_not_cls_convention(): """""" Case: do not implement interface member, which do not follow naming convention, that is class method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @classmethod def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention(): """""" Case: do not implement interface member, which do not follow naming convention. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention_in_getter(): """""" Case: do not implement interface member, which do not follow naming convention, that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(this)' == error.value.message def test_implements_not_self_convention_in_deleter(): """""" Case: do not implement interface member, which do not follow naming convention, that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return @name.deleter def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(this): return assert 'class HumanWithoutImplementation ", https://github.com/dmytrostriletskyi/accessify,6b7cf8657ffe18cd6a43c6cfb73b071084f0331e,tests/interfaces/test_implements.py::test_implements_no_implementation_getter,tests/interfaces/test_implements.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Provide tests for implements interface implementation. """""" import pytest from accessify.access import ( private, protected, ) from accessify.errors import ( ImplementedInterfaceMemberHasIncorrectAccessModifierException, InterfaceMemberHasNotBeenImplementedException, ) from accessify.interfaces import implements from tests.utils import custom_decorator def test_implements(): """""" Case: implement interfaces. Expect: no errors during class initialization. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass @staticmethod def eat(food, *args, allergy=None, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass @implements(HumanBasicsInterface, HumanNameInterface, HumanSoulInterface) class HumanWithImplementation: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return @staticmethod def eat(food, *args, allergy=None, **kwargs): pass def love(self, who, *args, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert HumanWithImplementation() is not None def test_not_implements_private_access(): """""" Case: do not implement interface member with mismatched private access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass assert 'HumanSoul.dream(about, args, kwargs) mismatches ' \ 'HumanSoulInterface.dream() member access modifier.' == error.value.message def test_not_implements_protected_access(): """""" Case: do not implement interface member with mismatched protected access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert 'HumanSoul.die(cls, when, args, kwargs) mismatches ' \ 'HumanSoulInterface.die() member access modifier.' == error.value.message def test_implements_no_implementation_getter(): """""" Case: do not implement interface member that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_setter(): """""" Case: do not implement interface member that is setter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self, new_name)' == error.value.message def test_implements_no_implementation_deleter(): """""" Case: do not implement interface member that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.deleter def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_static_method(): """""" Case: do not implement interface member that is static method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @staticmethod def eat(food, *args, allergy=None, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.eat(food, args, allergy, kwargs)' == error.value.message def test_implements_no_implementation_instance_method(): """""" Case: do not implement interface member that is method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.love(self, who, args, kwargs)' == error.value.message def test_implements_not_cls_convention(): """""" Case: do not implement interface member, which do not follow naming convention, that is class method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @classmethod def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention(): """""" Case: do not implement interface member, which do not follow naming convention. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention_in_getter(): """""" Case: do not implement interface member, which do not follow naming convention, that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(this)' == error.value.message def test_implements_not_self_convention_in_deleter(): """""" Case: do not implement interface member, which do not follow naming convention, that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return @name.deleter def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(this): return assert 'class HumanWithoutImplementation ", https://github.com/dmytrostriletskyi/accessify,6b7cf8657ffe18cd6a43c6cfb73b071084f0331e,tests/interfaces/test_implements.py::test_implements_no_implementation_instance_method,tests/interfaces/test_implements.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Provide tests for implements interface implementation. """""" import pytest from accessify.access import ( private, protected, ) from accessify.errors import ( ImplementedInterfaceMemberHasIncorrectAccessModifierException, InterfaceMemberHasNotBeenImplementedException, ) from accessify.interfaces import implements from tests.utils import custom_decorator def test_implements(): """""" Case: implement interfaces. Expect: no errors during class initialization. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass @staticmethod def eat(food, *args, allergy=None, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass @implements(HumanBasicsInterface, HumanNameInterface, HumanSoulInterface) class HumanWithImplementation: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return @staticmethod def eat(food, *args, allergy=None, **kwargs): pass def love(self, who, *args, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert HumanWithImplementation() is not None def test_not_implements_private_access(): """""" Case: do not implement interface member with mismatched private access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass assert 'HumanSoul.dream(about, args, kwargs) mismatches ' \ 'HumanSoulInterface.dream() member access modifier.' == error.value.message def test_not_implements_protected_access(): """""" Case: do not implement interface member with mismatched protected access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert 'HumanSoul.die(cls, when, args, kwargs) mismatches ' \ 'HumanSoulInterface.die() member access modifier.' == error.value.message def test_implements_no_implementation_getter(): """""" Case: do not implement interface member that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_setter(): """""" Case: do not implement interface member that is setter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self, new_name)' == error.value.message def test_implements_no_implementation_deleter(): """""" Case: do not implement interface member that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.deleter def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_static_method(): """""" Case: do not implement interface member that is static method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @staticmethod def eat(food, *args, allergy=None, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.eat(food, args, allergy, kwargs)' == error.value.message def test_implements_no_implementation_instance_method(): """""" Case: do not implement interface member that is method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.love(self, who, args, kwargs)' == error.value.message def test_implements_not_cls_convention(): """""" Case: do not implement interface member, which do not follow naming convention, that is class method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @classmethod def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention(): """""" Case: do not implement interface member, which do not follow naming convention. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention_in_getter(): """""" Case: do not implement interface member, which do not follow naming convention, that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(this)' == error.value.message def test_implements_not_self_convention_in_deleter(): """""" Case: do not implement interface member, which do not follow naming convention, that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return @name.deleter def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(this): return assert 'class HumanWithoutImplementation ", https://github.com/dmytrostriletskyi/accessify,6b7cf8657ffe18cd6a43c6cfb73b071084f0331e,tests/interfaces/test_implements.py::test_implements_no_implementation_setter,tests/interfaces/test_implements.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Provide tests for implements interface implementation. """""" import pytest from accessify.access import ( private, protected, ) from accessify.errors import ( ImplementedInterfaceMemberHasIncorrectAccessModifierException, InterfaceMemberHasNotBeenImplementedException, ) from accessify.interfaces import implements from tests.utils import custom_decorator def test_implements(): """""" Case: implement interfaces. Expect: no errors during class initialization. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass @staticmethod def eat(food, *args, allergy=None, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass @implements(HumanBasicsInterface, HumanNameInterface, HumanSoulInterface) class HumanWithImplementation: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return @staticmethod def eat(food, *args, allergy=None, **kwargs): pass def love(self, who, *args, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert HumanWithImplementation() is not None def test_not_implements_private_access(): """""" Case: do not implement interface member with mismatched private access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass assert 'HumanSoul.dream(about, args, kwargs) mismatches ' \ 'HumanSoulInterface.dream() member access modifier.' == error.value.message def test_not_implements_protected_access(): """""" Case: do not implement interface member with mismatched protected access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert 'HumanSoul.die(cls, when, args, kwargs) mismatches ' \ 'HumanSoulInterface.die() member access modifier.' == error.value.message def test_implements_no_implementation_getter(): """""" Case: do not implement interface member that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_setter(): """""" Case: do not implement interface member that is setter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self, new_name)' == error.value.message def test_implements_no_implementation_deleter(): """""" Case: do not implement interface member that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.deleter def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_static_method(): """""" Case: do not implement interface member that is static method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @staticmethod def eat(food, *args, allergy=None, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.eat(food, args, allergy, kwargs)' == error.value.message def test_implements_no_implementation_instance_method(): """""" Case: do not implement interface member that is method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.love(self, who, args, kwargs)' == error.value.message def test_implements_not_cls_convention(): """""" Case: do not implement interface member, which do not follow naming convention, that is class method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @classmethod def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention(): """""" Case: do not implement interface member, which do not follow naming convention. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention_in_getter(): """""" Case: do not implement interface member, which do not follow naming convention, that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(this)' == error.value.message def test_implements_not_self_convention_in_deleter(): """""" Case: do not implement interface member, which do not follow naming convention, that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return @name.deleter def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(this): return assert 'class HumanWithoutImplementation ", https://github.com/dmytrostriletskyi/accessify,6b7cf8657ffe18cd6a43c6cfb73b071084f0331e,tests/interfaces/test_implements.py::test_implements_no_implementation_static_method,tests/interfaces/test_implements.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Provide tests for implements interface implementation. """""" import pytest from accessify.access import ( private, protected, ) from accessify.errors import ( ImplementedInterfaceMemberHasIncorrectAccessModifierException, InterfaceMemberHasNotBeenImplementedException, ) from accessify.interfaces import implements from tests.utils import custom_decorator def test_implements(): """""" Case: implement interfaces. Expect: no errors during class initialization. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass @staticmethod def eat(food, *args, allergy=None, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass @implements(HumanBasicsInterface, HumanNameInterface, HumanSoulInterface) class HumanWithImplementation: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return @staticmethod def eat(food, *args, allergy=None, **kwargs): pass def love(self, who, *args, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert HumanWithImplementation() is not None def test_not_implements_private_access(): """""" Case: do not implement interface member with mismatched private access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass assert 'HumanSoul.dream(about, args, kwargs) mismatches ' \ 'HumanSoulInterface.dream() member access modifier.' == error.value.message def test_not_implements_protected_access(): """""" Case: do not implement interface member with mismatched protected access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert 'HumanSoul.die(cls, when, args, kwargs) mismatches ' \ 'HumanSoulInterface.die() member access modifier.' == error.value.message def test_implements_no_implementation_getter(): """""" Case: do not implement interface member that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_setter(): """""" Case: do not implement interface member that is setter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self, new_name)' == error.value.message def test_implements_no_implementation_deleter(): """""" Case: do not implement interface member that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.deleter def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_static_method(): """""" Case: do not implement interface member that is static method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @staticmethod def eat(food, *args, allergy=None, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.eat(food, args, allergy, kwargs)' == error.value.message def test_implements_no_implementation_instance_method(): """""" Case: do not implement interface member that is method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.love(self, who, args, kwargs)' == error.value.message def test_implements_not_cls_convention(): """""" Case: do not implement interface member, which do not follow naming convention, that is class method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @classmethod def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention(): """""" Case: do not implement interface member, which do not follow naming convention. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention_in_getter(): """""" Case: do not implement interface member, which do not follow naming convention, that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(this)' == error.value.message def test_implements_not_self_convention_in_deleter(): """""" Case: do not implement interface member, which do not follow naming convention, that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return @name.deleter def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(this): return assert 'class HumanWithoutImplementation ", https://github.com/dmytrostriletskyi/accessify,6b7cf8657ffe18cd6a43c6cfb73b071084f0331e,tests/interfaces/test_implements.py::test_not_implements_private_access,tests/interfaces/test_implements.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Provide tests for implements interface implementation. """""" import pytest from accessify.access import ( private, protected, ) from accessify.errors import ( ImplementedInterfaceMemberHasIncorrectAccessModifierException, InterfaceMemberHasNotBeenImplementedException, ) from accessify.interfaces import implements from tests.utils import custom_decorator def test_implements(): """""" Case: implement interfaces. Expect: no errors during class initialization. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass @staticmethod def eat(food, *args, allergy=None, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass @implements(HumanBasicsInterface, HumanNameInterface, HumanSoulInterface) class HumanWithImplementation: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return @staticmethod def eat(food, *args, allergy=None, **kwargs): pass def love(self, who, *args, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert HumanWithImplementation() is not None def test_not_implements_private_access(): """""" Case: do not implement interface member with mismatched private access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass assert 'HumanSoul.dream(about, args, kwargs) mismatches ' \ 'HumanSoulInterface.dream() member access modifier.' == error.value.message def test_not_implements_protected_access(): """""" Case: do not implement interface member with mismatched protected access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert 'HumanSoul.die(cls, when, args, kwargs) mismatches ' \ 'HumanSoulInterface.die() member access modifier.' == error.value.message def test_implements_no_implementation_getter(): """""" Case: do not implement interface member that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_setter(): """""" Case: do not implement interface member that is setter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self, new_name)' == error.value.message def test_implements_no_implementation_deleter(): """""" Case: do not implement interface member that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.deleter def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_static_method(): """""" Case: do not implement interface member that is static method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @staticmethod def eat(food, *args, allergy=None, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.eat(food, args, allergy, kwargs)' == error.value.message def test_implements_no_implementation_instance_method(): """""" Case: do not implement interface member that is method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.love(self, who, args, kwargs)' == error.value.message def test_implements_not_cls_convention(): """""" Case: do not implement interface member, which do not follow naming convention, that is class method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @classmethod def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention(): """""" Case: do not implement interface member, which do not follow naming convention. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention_in_getter(): """""" Case: do not implement interface member, which do not follow naming convention, that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(this)' == error.value.message def test_implements_not_self_convention_in_deleter(): """""" Case: do not implement interface member, which do not follow naming convention, that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return @name.deleter def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(this): return assert 'class HumanWithoutImplementation ", https://github.com/dmytrostriletskyi/accessify,6b7cf8657ffe18cd6a43c6cfb73b071084f0331e,tests/interfaces/test_implements.py::test_not_implements_protected_access,tests/interfaces/test_implements.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Provide tests for implements interface implementation. """""" import pytest from accessify.access import ( private, protected, ) from accessify.errors import ( ImplementedInterfaceMemberHasIncorrectAccessModifierException, InterfaceMemberHasNotBeenImplementedException, ) from accessify.interfaces import implements from tests.utils import custom_decorator def test_implements(): """""" Case: implement interfaces. Expect: no errors during class initialization. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass @staticmethod def eat(food, *args, allergy=None, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass @implements(HumanBasicsInterface, HumanNameInterface, HumanSoulInterface) class HumanWithImplementation: @property def name(self): return @name.setter def name(self, new_name): return @name.deleter def name(self): return @staticmethod def eat(food, *args, allergy=None, **kwargs): pass def love(self, who, *args, **kwargs): pass @classmethod def think(cls, about, *args, **kwargs): pass @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert HumanWithImplementation() is not None def test_not_implements_private_access(): """""" Case: do not implement interface member with mismatched private access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @private @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @staticmethod @custom_decorator def dream(about, *args, **kwargs): pass assert 'HumanSoul.dream(about, args, kwargs) mismatches ' \ 'HumanSoulInterface.dream() member access modifier.' == error.value.message def test_not_implements_protected_access(): """""" Case: do not implement interface member with mismatched protected access. Expect: class mismatches interface member access modifier error message. """""" class HumanSoulInterface: @protected @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass with pytest.raises(ImplementedInterfaceMemberHasIncorrectAccessModifierException) as error: @implements(HumanSoulInterface) class HumanSoul: @classmethod @custom_decorator def die(cls, when, *args, **kwargs): pass assert 'HumanSoul.die(cls, when, args, kwargs) mismatches ' \ 'HumanSoulInterface.die() member access modifier.' == error.value.message def test_implements_no_implementation_getter(): """""" Case: do not implement interface member that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_setter(): """""" Case: do not implement interface member that is setter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.setter def name(self, new_name): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self, new_name)' == error.value.message def test_implements_no_implementation_deleter(): """""" Case: do not implement interface member that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(self): return @name.deleter def name(self): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(self): return assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(self)' == error.value.message def test_implements_no_implementation_static_method(): """""" Case: do not implement interface member that is static method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @staticmethod def eat(food, *args, allergy=None, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.eat(food, args, allergy, kwargs)' == error.value.message def test_implements_no_implementation_instance_method(): """""" Case: do not implement interface member that is method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def love(self, who, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.love(self, who, args, kwargs)' == error.value.message def test_implements_not_cls_convention(): """""" Case: do not implement interface member, which do not follow naming convention, that is class method. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: @classmethod def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention(): """""" Case: do not implement interface member, which do not follow naming convention. Expect: class does not implement interface member error message. """""" class HumanBasicsInterface: def think(this, about, *args, **kwargs): pass with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanBasicsInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanBasicsInterface.think(this, about, args, kwargs)' == error.value.message def test_implements_not_self_convention_in_getter(): """""" Case: do not implement interface member, which do not follow naming convention, that is getter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: pass assert 'class HumanWithoutImplementation does not implement ' \ 'interface member HumanNameInterface.name(this)' == error.value.message def test_implements_not_self_convention_in_deleter(): """""" Case: do not implement interface member, which do not follow naming convention, that is deleter. Expect: class does not implement interface member error message. """""" class HumanNameInterface: @property def name(this): return @name.deleter def name(this): return with pytest.raises(InterfaceMemberHasNotBeenImplementedException) as error: @implements(HumanNameInterface) class HumanWithoutImplementation: @property def name(this): return assert 'class HumanWithoutImplementation ", https://github.com/dowjones/tokendito,f5d8e5171ddcca0c62925263ddb80535f51aded3,tests/functional_test.py::test_package_exists,tests/functional_test.py,NOD,flaky,nan,nan,classify;root_cause,"# vim: set filetype=python ts=4 sw=4 # -*- coding: utf-8 -*- """"""Functional tests, and local fixtures."""""" from __future__ import (absolute_import, division, print_function, unicode_literals) from builtins import (ascii, bytes, chr, dict, filter, hex, input, # noqa: F401 int, list, map, next, object, oct, open, pow, range, round, str, super, zip) from os import path import re import subprocess import sys from future import standard_library import pytest sys.path.insert(0, path.dirname(path.dirname(path.abspath(__file__)))) standard_library.install_aliases() def string_decode(bytestring): """"""Convert a str into a Unicode object. The `decode()` method is only available in byte strings. Calling on other string objects generates a `NameError`, and the same string is returned unmodified. :param bytestring: :return: decoded string """""" decoded_string = bytestring try: decoded_string = bytestring.decode('utf-8') except (NameError, TypeError): pass return decoded_string def run_process(proc): """"""Spawn a child process. Returns a dict with stdout, sdterr, exit status, and command executed. """""" process = subprocess.Popen(proc, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdoutdata, stderrdata) = process.communicate() proc_status = { 'stdout': string_decode(stdoutdata), 'stderr': string_decode(stderrdata), 'name': ' '.join(proc), 'exit_status': process.returncode } return proc_status @pytest.fixture def package_regex(): """"""Get compiled package regex."""""" version_regex = re.compile(r'^\S+/(?P.*?)\s+.*$') return version_regex @pytest.fixture def package_version(): """"""Run test with access to the Tokendito package."""""" from tokendito.__version__ import __version__ as tokendito_version return tokendito_version @pytest.fixture def custom_args(request): """"""Search the custom command-line options and return a list of keys and values."""""" options = ['--username', '--password', '--okta-aws-app-url', '--mfa-method', '--mfa-response', '--role-arn', '--config-file'] arg_list = [] # pytest does not have a method for listing options, so we have look them up. for item in options: if request.config.getoption(item): arg_list.extend([item, request.config.getoption(item)]) return arg_list @pytest.mark.run('first') def test_package_uninstall(): """"""Uninstall tokendito if it is already installed."""""" proc = run_process([sys.executable, '-m', 'pip', 'uninstall', '-q', '-q', '-y', 'tokendito']) assert not proc['stderr'] assert proc['exit_status'] == 0 @pytest.mark.run('second') def test_package_install(): """"""Install tokendito as a python package."""""" repo_root = path.dirname(path.dirname(path.abspath(__file__))) proc = run_process([sys.executable, '-m', 'pip', 'install', '-e', repo_root]) assert not proc['stderr'] assert proc['exit_status'] == 0 def test_package_exists(): """"""Check whether the package is installed."""""" proc = run_process([sys.executable, '-m', 'pip', 'show', 'tokendito']) assert not proc['stderr'] assert proc['exit_status'] == 0 @pytest.mark.parametrize('runnable', [[sys.executable, '-m', 'tokendito', '--version'], [sys.executable, sys.path[0] + '/tokendito/tokendito.py', '--version'], ['tokendito', '--version']]) def test_version(package_version, package_regex, runnable): """"""Check if the package version is the same when running in different ways."""""" proc = run_process(runnable) assert not proc['stderr'] assert proc['exit_status'] == 0 match = re.match(package_regex, proc['stdout']) local_version = match.group('version') assert package_version == local_version @pytest.mark.run('second-to-last') def test_generate_credentials(custom_args): """"""Run the tool and generate credentials."""""" from tokendito import helpers, settings # Emulate helpers.process_options() bypassing interactive portions. tool_args = helpers.setup(custom_args) helpers.process_ini_file(tool_args.config_file, 'default') helpers.process_arguments(tool_args) helpers.process_environment() if settings.role_arn is None or \ settings.okta_aws_app_url is None or \ settings.mfa_method is None or \ not settings.okta_username or \ not settings.okta_password: pytest.skip('Not enough arguments collected to execute non-interactively.') # Rebuild argument list args = ['--role-arn', '{}'.format(settings.role_arn), '--okta-aws-app-url', '{}'.format(settings.okta_aws_app_url), '--mfa-method', '{}'.format(settings.mfa_method), '--mfa-response', '{}'.format(settings.mfa_response), '--username', '{}'.format(settings.okta_username), '--password', '{}'.format(settings.okta_password) ] executable = ['tokendito'] # Can use sys.executable -m tokendito, or parametrize runnable = executable + args proc = run_process(runnable) assert not proc['stderr'] assert proc['exit_status'] == 0 @pytest.mark.run('last') def test_aws_credentials(custom_args): """"""Run the AWS cli to verify whether credentials work."""""" from tokendito import helpers, settings # Emulate helpers.process_options() bypassing interactive portions. tool_args = helpers.setup(custom_args) helpers.process_ini_file(tool_args.config_file, 'default') helpers.process_arguments(tool_args) helpers.process_environment() if settings.role_arn is None: pytest.skip('No AWS profile defined, test will be skipped.') profile = settings.role_arn.split('/')[-1] runnable = ['aws', '--profile', profile, 'sts', 'get-caller-identity'] proc = run_process(runnable) assert not proc['stderr'] assert proc['exit_status'] == 0 ", https://github.com/dowjones/tokendito,f5d8e5171ddcca0c62925263ddb80535f51aded3,tests/functional_test.py::test_version[runnable2],tests/functional_test.py,NOD,flaky,nan,nan,classify;root_cause,"# vim: set filetype=python ts=4 sw=4 # -*- coding: utf-8 -*- """"""Functional tests, and local fixtures."""""" from __future__ import (absolute_import, division, print_function, unicode_literals) from builtins import (ascii, bytes, chr, dict, filter, hex, input, # noqa: F401 int, list, map, next, object, oct, open, pow, range, round, str, super, zip) from os import path import re import subprocess import sys from future import standard_library import pytest sys.path.insert(0, path.dirname(path.dirname(path.abspath(__file__)))) standard_library.install_aliases() def string_decode(bytestring): """"""Convert a str into a Unicode object. The `decode()` method is only available in byte strings. Calling on other string objects generates a `NameError`, and the same string is returned unmodified. :param bytestring: :return: decoded string """""" decoded_string = bytestring try: decoded_string = bytestring.decode('utf-8') except (NameError, TypeError): pass return decoded_string def run_process(proc): """"""Spawn a child process. Returns a dict with stdout, sdterr, exit status, and command executed. """""" process = subprocess.Popen(proc, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdoutdata, stderrdata) = process.communicate() proc_status = { 'stdout': string_decode(stdoutdata), 'stderr': string_decode(stderrdata), 'name': ' '.join(proc), 'exit_status': process.returncode } return proc_status @pytest.fixture def package_regex(): """"""Get compiled package regex."""""" version_regex = re.compile(r'^\S+/(?P.*?)\s+.*$') return version_regex @pytest.fixture def package_version(): """"""Run test with access to the Tokendito package."""""" from tokendito.__version__ import __version__ as tokendito_version return tokendito_version @pytest.fixture def custom_args(request): """"""Search the custom command-line options and return a list of keys and values."""""" options = ['--username', '--password', '--okta-aws-app-url', '--mfa-method', '--mfa-response', '--role-arn', '--config-file'] arg_list = [] # pytest does not have a method for listing options, so we have look them up. for item in options: if request.config.getoption(item): arg_list.extend([item, request.config.getoption(item)]) return arg_list @pytest.mark.run('first') def test_package_uninstall(): """"""Uninstall tokendito if it is already installed."""""" proc = run_process([sys.executable, '-m', 'pip', 'uninstall', '-q', '-q', '-y', 'tokendito']) assert not proc['stderr'] assert proc['exit_status'] == 0 @pytest.mark.run('second') def test_package_install(): """"""Install tokendito as a python package."""""" repo_root = path.dirname(path.dirname(path.abspath(__file__))) proc = run_process([sys.executable, '-m', 'pip', 'install', '-e', repo_root]) assert not proc['stderr'] assert proc['exit_status'] == 0 def test_package_exists(): """"""Check whether the package is installed."""""" proc = run_process([sys.executable, '-m', 'pip', 'show', 'tokendito']) assert not proc['stderr'] assert proc['exit_status'] == 0 @pytest.mark.parametrize('runnable', [[sys.executable, '-m', 'tokendito', '--version'], [sys.executable, sys.path[0] + '/tokendito/tokendito.py', '--version'], ['tokendito', '--version']]) def test_version(package_version, package_regex, runnable): """"""Check if the package version is the same when running in different ways."""""" proc = run_process(runnable) assert not proc['stderr'] assert proc['exit_status'] == 0 match = re.match(package_regex, proc['stdout']) local_version = match.group('version') assert package_version == local_version @pytest.mark.run('second-to-last') def test_generate_credentials(custom_args): """"""Run the tool and generate credentials."""""" from tokendito import helpers, settings # Emulate helpers.process_options() bypassing interactive portions. tool_args = helpers.setup(custom_args) helpers.process_ini_file(tool_args.config_file, 'default') helpers.process_arguments(tool_args) helpers.process_environment() if settings.role_arn is None or \ settings.okta_aws_app_url is None or \ settings.mfa_method is None or \ not settings.okta_username or \ not settings.okta_password: pytest.skip('Not enough arguments collected to execute non-interactively.') # Rebuild argument list args = ['--role-arn', '{}'.format(settings.role_arn), '--okta-aws-app-url', '{}'.format(settings.okta_aws_app_url), '--mfa-method', '{}'.format(settings.mfa_method), '--mfa-response', '{}'.format(settings.mfa_response), '--username', '{}'.format(settings.okta_username), '--password', '{}'.format(settings.okta_password) ] executable = ['tokendito'] # Can use sys.executable -m tokendito, or parametrize runnable = executable + args proc = run_process(runnable) assert not proc['stderr'] assert proc['exit_status'] == 0 @pytest.mark.run('last') def test_aws_credentials(custom_args): """"""Run the AWS cli to verify whether credentials work."""""" from tokendito import helpers, settings # Emulate helpers.process_options() bypassing interactive portions. tool_args = helpers.setup(custom_args) helpers.process_ini_file(tool_args.config_file, 'default') helpers.process_arguments(tool_args) helpers.process_environment() if settings.role_arn is None: pytest.skip('No AWS profile defined, test will be skipped.') profile = settings.role_arn.split('/')[-1] runnable = ['aws', '--profile', profile, 'sts', 'get-caller-identity'] proc = run_process(runnable) assert not proc['stderr'] assert proc['exit_status'] == 0 ", https://github.com/dowjones/tokendito,f5d8e5171ddcca0c62925263ddb80535f51aded3,tests/unit_test.py::test_set_okta_password,tests/unit_test.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# vim: set filetype=python ts=4 sw=4 # -*- coding: utf-8 -*- """"""Unit tests, and local fixtures."""""" from __future__ import (absolute_import, division, print_function, unicode_literals) from builtins import (ascii, bytes, chr, dict, filter, hex, input, # noqa: F401 int, list, map, next, object, oct, open, pow, range, round, str, super, zip) from os import path import sys from future import standard_library import pytest import semver sys.path.insert(0, path.dirname(path.dirname(path.abspath(__file__)))) standard_library.install_aliases() @pytest.fixture def valid_settings(): """"""Return a dict with valid settings for the tokendito.settings module."""""" from tokendito import settings builtins_and_methods = [ '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'absolute_import', 'ascii', 'bytes', 'chr', 'dict', 'division', 'encoding', 'filter', 'hex', 'input', 'int', 'list', 'map', 'next', 'object', 'oct', 'open', 'pow', 'print_function', 'range', 'role_arn', 'round', 'standard_library', 'str', 'super', 'sys', 'unicode_literals', 'zip'] settings_keys = dir(settings) unmatched_keys = list(set(settings_keys) - set(builtins_and_methods)) valid_keys = {str(key): key + '_pytest_patched' for key in unmatched_keys} return valid_keys @pytest.fixture def invalid_settings(): """"""Return a dict with invalid settings for the tokendito.settings module."""""" invalid_keys = { ""okta"": ""okta_pytest_patched"", ""okta_deadbeef"": ""okta_deadbeef_pytest_patched"", ""aws_deadbeef"": ""aws_deadbeef_pytest_patched"", ""pytest_bad_value"": ""pytest_bad_value_pytest_patched"" } return invalid_keys def test_import_location(): """"""Ensure module imported is the local one."""""" import tokendito local_path = path.realpath(path.dirname(path.dirname( path.abspath(__file__))) + '/tokendito/__init__.py') imported_path = path.realpath(tokendito.__file__) assert imported_path.startswith(local_path) def test_semver_version(): """"""Ensure the package version is semver compliant."""""" from tokendito.__version__ import __version__ as version assert semver.parse_version_info(version) def test__version__var_names(): """"""Ensure variables follow the __varname__ convention."""""" from tokendito import __version__ for item in vars(__version__): assert item.startswith('__') assert item.endswith('__') @pytest.mark.parametrize('string', [r'raw_string', u'unicode_string', r'byte_string']) def test_to_unicode(string): """"""Test whether to_unicode returns unicode strings."""""" from tokendito import helpers new_str = helpers.to_unicode(string) assert isinstance(new_str, str) def test_set_okta_username(monkeypatch): """"""Test whether data sent is the same as data returned."""""" from tokendito import helpers, settings monkeypatch.setattr('tokendito.helpers.input', lambda _: 'pytest_patched') val = helpers.set_okta_username() assert val == 'pytest_patched' assert settings.okta_username == 'pytest_patched' def test_set_okta_password(monkeypatch): """"""Test whether data sent is the same as data returned."""""" from tokendito import helpers, settings import getpass monkeypatch.setattr(getpass, 'getpass', lambda: 'pytest_patched') val = helpers.set_okta_password() assert val == 'pytest_patched' assert settings.okta_password == 'pytest_patched' @pytest.mark.parametrize('url,expected', [ ('pytest_deadbeef', False), ('http://acme.org/', False), ('https://acme.okta.org/app/UserHome', False), ('http://login.acme.org/home/amazon_aws/0123456789abcdef0123/456', False), ('https://login.acme.org/home/amazon_aws/0123456789abcdef0123/456', True), ('https://acme.okta.org/home/amazon_aws/0123456789abcdef0123/456?fromHome=true', True)]) def test_validate_okta_aws_app_url(url, expected): """"""Test whether the Okta URL is parsed correctly."""""" from tokendito import helpers assert helpers.validate_okta_aws_app_url(input_url=url) is expected @pytest.mark.parametrize('test,limit,expected', [ (0, 10, True), (5, 10, True), (10, 10, False), (-1, 10, False), (1, 0, False) ]) def test_check_within_range(mocker, test, limit, expected): """"""Test whether a given number is in the range 0 >= num < limit."""""" from tokendito import helpers mocker.patch('logging.error') assert helpers.check_within_range(test, limit) is expected @pytest.mark.parametrize('value,expected', [ ('-1', False), ('0', True), ('1', True), (-1, False), (0, True), (1, True), (3.7, False), ('3.7', False), ('seven', False), ('0xff', False), (None, False)]) def test_check_integer(value, expected, mocker): """"""Test whether the integer testing function works within boundaries."""""" from tokendito import helpers mocker.patch('logging.error') assert helpers.check_integer(value) is expected @pytest.mark.parametrize('test,limit,expected', [ (1, 10, True), (-1, 10, False), ('pytest', 10, False) ]) def test_validate_input(mocker, test, limit, expected): """"""Check if a given input is within the 0 >= num < limit range."""""" from tokendito import helpers mocker.patch('logging.error') assert helpers.validate_input(test, limit) is expected def test_get_input(monkeypatch): """"""Check if provided input is return unmodified."""""" from tokendito import helpers monkeypatch.setattr('tokendito.helpers.input', lambda _: 'pytest_patched') assert helpers.get_input() == 'pytest_patched' @pytest.mark.parametrize('value,expected', [ ('00', 0), ('01', 1), ('5', 5) ]) def test_collect_integer(monkeypatch, value, expected): """"""Check if a given digit or series of digits are properly casted to int."""""" from tokendito import helpers monkeypatch.setattr('tokendito.helpers.input', lambda _: value) assert helpers.collect_integer(10) == expected def test_prepare_payload(): """"""Check if values passed return in a dictionary."""""" from tokendito import helpers assert helpers.prepare_payload(pytest_key='pytest_val') == {'pytest_key': 'pytest_val'} assert helpers.prepare_payload(pytest_key=None) == {'pytest_key': None} assert helpers.prepare_payload(pytest_key1='pytest_val1', pytest_key2='pytest_val2') == { 'pytest_key1': 'pytest_val1', 'pytest_key2': 'pytest_val2'} def test_set_passcode(monkeypatch): """"""Check if numerical passcode can handle leading zero values."""""" from tokendito import duo_helpers monkeypatch.setattr('tokendito.helpers.input', lambda _: '0123456') assert duo_helpers.set_passcode({'factor': 'passcode'}) == '0123456' def test_process_environment(monkeypatch, valid_settings, invalid_settings): """"""Test whether environment variables are set in settings.*."""""" from tokendito import helpers, settings import os # ENV standard is uppercase valid_keys = {key.upper(): val for (key, val) in valid_settings.items()} invalid_keys = {key.upper(): val for (key, val) in invalid_settings.items()} # Python 2.7 does not support {**dict1, **dict2} for concatenation env_keys = valid_keys.copy() env_keys.update(invalid_keys) monkeypatch.setattr(os, 'environ', env_keys) helpers.process_environment() for key in valid_settings: assert getattr(settings, key) == valid_settings[key] for key in invalid_settings: assert getattr(settings, key, 'not_found') == 'not_found' def test_process_arguments(valid_settings, invalid_settings): """"""Test whether arguments are correctly set in settings.*."""""" from tokendito import helpers, settings from argparse import Namespace # Python 2.7 does not support {**dict1, **dict2} for concatenation args = valid_settings.copy() args.update(invalid_settings) helpers.process_arguments(Namespace(**args)) for key_name in valid_settings: assert getattr(settings, key_name) == valid_settings[key_name] for key_name in invalid_settings: assert getattr(settings, key_name, 'not_found') == 'not_found' @pytest.mark.skipif(sys.version_info[:2] == (3, 5), reason=""ConfigParser bug, see https://bugs.python.org/issue29623"") def test_process_ini_file(tmpdir, valid_settings, invalid_settings, mocker): """"""Test whether ini config elements are correctly set in settings.*."""""" from tokendito import helpers, settings # Create a mock config file data = '[default]\nokta_username = pytest\n\n[pytest]\n' data += ''.join('{} = {}\n'.format(key, val) for key, val in valid_settings.items()) data += ''.join('{} = {}\n'.format(key, val) for key, val in invalid_settings.items()) data += '\n[pytest_end]\n' data += ''.join('{} = {}\n'.format(key, val) for key, val in invalid_settings.items()) # Python 3.7 supports patching builtins.open(), which gives us the ability # to bypass file creation with: # mocker.patch('builtins.open', mocker.mock_open(read_data=data), create=True) # There is no (easy) way to achieve the same on earlier versions, so we create # an actual file instead. tmpdir keeps the last 3 files/dirs behind for inspection path = tmpdir.mkdir('pytest').join('pytest_tokendito.ini') path.write(data) # Ensure we fail if the section is not found with pytest.raises(SystemExit) as err: mocker.patch('logging.error') helpers.process_ini_file(path, 'pytest_expected_failure') # assert err.type == SystemExit assert err.value.code == 2 helpers.process_ini_file(path, 'pytest') # Test that correct options are set for key_name in valid_settings: assert getattr(settings, key_name) == valid_settings[key_name] # Test that inc", https://github.com/dowjones/tokendito,f5d8e5171ddcca0c62925263ddb80535f51aded3,tests/unit_test.py::test_set_okta_username,tests/unit_test.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# vim: set filetype=python ts=4 sw=4 # -*- coding: utf-8 -*- """"""Unit tests, and local fixtures."""""" from __future__ import (absolute_import, division, print_function, unicode_literals) from builtins import (ascii, bytes, chr, dict, filter, hex, input, # noqa: F401 int, list, map, next, object, oct, open, pow, range, round, str, super, zip) from os import path import sys from future import standard_library import pytest import semver sys.path.insert(0, path.dirname(path.dirname(path.abspath(__file__)))) standard_library.install_aliases() @pytest.fixture def valid_settings(): """"""Return a dict with valid settings for the tokendito.settings module."""""" from tokendito import settings builtins_and_methods = [ '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'absolute_import', 'ascii', 'bytes', 'chr', 'dict', 'division', 'encoding', 'filter', 'hex', 'input', 'int', 'list', 'map', 'next', 'object', 'oct', 'open', 'pow', 'print_function', 'range', 'role_arn', 'round', 'standard_library', 'str', 'super', 'sys', 'unicode_literals', 'zip'] settings_keys = dir(settings) unmatched_keys = list(set(settings_keys) - set(builtins_and_methods)) valid_keys = {str(key): key + '_pytest_patched' for key in unmatched_keys} return valid_keys @pytest.fixture def invalid_settings(): """"""Return a dict with invalid settings for the tokendito.settings module."""""" invalid_keys = { ""okta"": ""okta_pytest_patched"", ""okta_deadbeef"": ""okta_deadbeef_pytest_patched"", ""aws_deadbeef"": ""aws_deadbeef_pytest_patched"", ""pytest_bad_value"": ""pytest_bad_value_pytest_patched"" } return invalid_keys def test_import_location(): """"""Ensure module imported is the local one."""""" import tokendito local_path = path.realpath(path.dirname(path.dirname( path.abspath(__file__))) + '/tokendito/__init__.py') imported_path = path.realpath(tokendito.__file__) assert imported_path.startswith(local_path) def test_semver_version(): """"""Ensure the package version is semver compliant."""""" from tokendito.__version__ import __version__ as version assert semver.parse_version_info(version) def test__version__var_names(): """"""Ensure variables follow the __varname__ convention."""""" from tokendito import __version__ for item in vars(__version__): assert item.startswith('__') assert item.endswith('__') @pytest.mark.parametrize('string', [r'raw_string', u'unicode_string', r'byte_string']) def test_to_unicode(string): """"""Test whether to_unicode returns unicode strings."""""" from tokendito import helpers new_str = helpers.to_unicode(string) assert isinstance(new_str, str) def test_set_okta_username(monkeypatch): """"""Test whether data sent is the same as data returned."""""" from tokendito import helpers, settings monkeypatch.setattr('tokendito.helpers.input', lambda _: 'pytest_patched') val = helpers.set_okta_username() assert val == 'pytest_patched' assert settings.okta_username == 'pytest_patched' def test_set_okta_password(monkeypatch): """"""Test whether data sent is the same as data returned."""""" from tokendito import helpers, settings import getpass monkeypatch.setattr(getpass, 'getpass', lambda: 'pytest_patched') val = helpers.set_okta_password() assert val == 'pytest_patched' assert settings.okta_password == 'pytest_patched' @pytest.mark.parametrize('url,expected', [ ('pytest_deadbeef', False), ('http://acme.org/', False), ('https://acme.okta.org/app/UserHome', False), ('http://login.acme.org/home/amazon_aws/0123456789abcdef0123/456', False), ('https://login.acme.org/home/amazon_aws/0123456789abcdef0123/456', True), ('https://acme.okta.org/home/amazon_aws/0123456789abcdef0123/456?fromHome=true', True)]) def test_validate_okta_aws_app_url(url, expected): """"""Test whether the Okta URL is parsed correctly."""""" from tokendito import helpers assert helpers.validate_okta_aws_app_url(input_url=url) is expected @pytest.mark.parametrize('test,limit,expected', [ (0, 10, True), (5, 10, True), (10, 10, False), (-1, 10, False), (1, 0, False) ]) def test_check_within_range(mocker, test, limit, expected): """"""Test whether a given number is in the range 0 >= num < limit."""""" from tokendito import helpers mocker.patch('logging.error') assert helpers.check_within_range(test, limit) is expected @pytest.mark.parametrize('value,expected', [ ('-1', False), ('0', True), ('1', True), (-1, False), (0, True), (1, True), (3.7, False), ('3.7', False), ('seven', False), ('0xff', False), (None, False)]) def test_check_integer(value, expected, mocker): """"""Test whether the integer testing function works within boundaries."""""" from tokendito import helpers mocker.patch('logging.error') assert helpers.check_integer(value) is expected @pytest.mark.parametrize('test,limit,expected', [ (1, 10, True), (-1, 10, False), ('pytest', 10, False) ]) def test_validate_input(mocker, test, limit, expected): """"""Check if a given input is within the 0 >= num < limit range."""""" from tokendito import helpers mocker.patch('logging.error') assert helpers.validate_input(test, limit) is expected def test_get_input(monkeypatch): """"""Check if provided input is return unmodified."""""" from tokendito import helpers monkeypatch.setattr('tokendito.helpers.input', lambda _: 'pytest_patched') assert helpers.get_input() == 'pytest_patched' @pytest.mark.parametrize('value,expected', [ ('00', 0), ('01', 1), ('5', 5) ]) def test_collect_integer(monkeypatch, value, expected): """"""Check if a given digit or series of digits are properly casted to int."""""" from tokendito import helpers monkeypatch.setattr('tokendito.helpers.input', lambda _: value) assert helpers.collect_integer(10) == expected def test_prepare_payload(): """"""Check if values passed return in a dictionary."""""" from tokendito import helpers assert helpers.prepare_payload(pytest_key='pytest_val') == {'pytest_key': 'pytest_val'} assert helpers.prepare_payload(pytest_key=None) == {'pytest_key': None} assert helpers.prepare_payload(pytest_key1='pytest_val1', pytest_key2='pytest_val2') == { 'pytest_key1': 'pytest_val1', 'pytest_key2': 'pytest_val2'} def test_set_passcode(monkeypatch): """"""Check if numerical passcode can handle leading zero values."""""" from tokendito import duo_helpers monkeypatch.setattr('tokendito.helpers.input', lambda _: '0123456') assert duo_helpers.set_passcode({'factor': 'passcode'}) == '0123456' def test_process_environment(monkeypatch, valid_settings, invalid_settings): """"""Test whether environment variables are set in settings.*."""""" from tokendito import helpers, settings import os # ENV standard is uppercase valid_keys = {key.upper(): val for (key, val) in valid_settings.items()} invalid_keys = {key.upper(): val for (key, val) in invalid_settings.items()} # Python 2.7 does not support {**dict1, **dict2} for concatenation env_keys = valid_keys.copy() env_keys.update(invalid_keys) monkeypatch.setattr(os, 'environ', env_keys) helpers.process_environment() for key in valid_settings: assert getattr(settings, key) == valid_settings[key] for key in invalid_settings: assert getattr(settings, key, 'not_found') == 'not_found' def test_process_arguments(valid_settings, invalid_settings): """"""Test whether arguments are correctly set in settings.*."""""" from tokendito import helpers, settings from argparse import Namespace # Python 2.7 does not support {**dict1, **dict2} for concatenation args = valid_settings.copy() args.update(invalid_settings) helpers.process_arguments(Namespace(**args)) for key_name in valid_settings: assert getattr(settings, key_name) == valid_settings[key_name] for key_name in invalid_settings: assert getattr(settings, key_name, 'not_found') == 'not_found' @pytest.mark.skipif(sys.version_info[:2] == (3, 5), reason=""ConfigParser bug, see https://bugs.python.org/issue29623"") def test_process_ini_file(tmpdir, valid_settings, invalid_settings, mocker): """"""Test whether ini config elements are correctly set in settings.*."""""" from tokendito import helpers, settings # Create a mock config file data = '[default]\nokta_username = pytest\n\n[pytest]\n' data += ''.join('{} = {}\n'.format(key, val) for key, val in valid_settings.items()) data += ''.join('{} = {}\n'.format(key, val) for key, val in invalid_settings.items()) data += '\n[pytest_end]\n' data += ''.join('{} = {}\n'.format(key, val) for key, val in invalid_settings.items()) # Python 3.7 supports patching builtins.open(), which gives us the ability # to bypass file creation with: # mocker.patch('builtins.open', mocker.mock_open(read_data=data), create=True) # There is no (easy) way to achieve the same on earlier versions, so we create # an actual file instead. tmpdir keeps the last 3 files/dirs behind for inspection path = tmpdir.mkdir('pytest').join('pytest_tokendito.ini') path.write(data) # Ensure we fail if the section is not found with pytest.raises(SystemExit) as err: mocker.patch('logging.error') helpers.process_ini_file(path, 'pytest_expected_failure') # assert err.type == SystemExit assert err.value.code == 2 helpers.process_ini_file(path, 'pytest') # Test that correct options are set for key_name in valid_settings: assert getattr(settings, key_name) == valid_settings[key_name] # Test that inc", https://github.com/dreid/yunomi,1aa3f166843613331b38e231264ffc3ac40e8094,yunomi/tests/test_metrics_registry.py::MetricsRegistryTests::test_count_calls_decorator,yunomi/tests/test_metrics_registry.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from __future__ import division, absolute_import import mock from unittest2 import TestCase from yunomi.compat import xrange from yunomi.core.metrics_registry import (MetricsRegistry, counter, histogram, meter, timer, count_calls, meter_calls, hist_calls, time_calls) from yunomi.tests.util import Clock class MetricsRegistryTests(TestCase): def setUp(self): self.twisted_clock = Clock() self.registry = MetricsRegistry(clock=self.twisted_clock.seconds) def test_empty_registry(self): self.assertEqual(len(self.registry.dump_metrics()), 0) def test_getters_create_metrics(self): self.registry.counter(""counter"") self.registry.histogram(""histogram"") self.registry.meter(""meter"") self.registry.timer(""timer"") dump = self.registry.dump_metrics() self.assertEqual(len(dump), 25) metric_names = (""counter_count"", ""histogram_avg"", ""histogram_max"", ""histogram_min"", ""histogram_std_dev"", ""histogram_75_percentile"", ""histogram_98_percentile"", ""histogram_99_percentile"", ""histogram_999_percentile"", ""meter_15m_rate"", ""meter_5m_rate"", ""meter_1m_rate"", ""meter_mean_rate"", ""timer_avg"", ""timer_max"", ""timer_min"", ""timer_std_dev"", ""timer_75_percentile"", ""timer_98_percentile"", ""timer_99_percentile"", ""timer_999_percentile"", ""timer_15m_rate"", ""timer_5m_rate"", ""timer_1m_rate"", ""timer_mean_rate"") for stat in dump: self.assertTrue(stat[""name""] in metric_names) self.assertEqual(stat[""value""], 0) def test_count_calls_decorator(self): @count_calls def test(): pass for i in xrange(10): test() self.assertEqual(counter(""test_calls"").get_count(), 10) @mock.patch(""yunomi.core.meter.time"") def test_meter_calls_decorator(self, time_mock): time_mock.return_value = 0 @meter_calls def test(): pass for i in xrange(10): test() time_mock.return_value = 10 self.assertAlmostEqual(meter(""test_calls"").get_mean_rate(), 1.0) def test_hist_calls_decorator(self): @hist_calls def test(n): return n for i in xrange(1, 11): test(i) _histogram = histogram(""test_calls"") snapshot = _histogram.get_snapshot() self.assertAlmostEqual(_histogram.get_mean(), 5.5) self.assertEqual(_histogram.get_max(), 10) self.assertEqual(_histogram.get_min(), 1) self.assertAlmostEqual(_histogram.get_std_dev(), 3.02765, places=5) self.assertAlmostEqual(_histogram.get_variance(), 9.16667, places=5) self.assertAlmostEqual(snapshot.get_75th_percentile(), 8.25) self.assertAlmostEqual(snapshot.get_98th_percentile(), 10.0) self.assertAlmostEqual(snapshot.get_99th_percentile(), 10.0) self.assertAlmostEqual(snapshot.get_999th_percentile(), 10.0) @mock.patch(""yunomi.core.metrics_registry.time"") def test_time_calls_decorator(self, time_mock): time_mock.return_value = 0.0 @time_calls def test(): time_mock.return_value += 1.0 for i in xrange(10): test() _timer = timer(""test_calls"") snapshot = _timer.get_snapshot() self.assertEqual(_timer.get_count(), 10) self.assertEqual(_timer.get_max(), 1) self.assertEqual(_timer.get_min(), 1) self.assertAlmostEqual(_timer.get_std_dev(), 0) self.assertAlmostEqual(snapshot.get_75th_percentile(), 1.0) self.assertAlmostEqual(snapshot.get_98th_percentile(), 1.0) self.assertAlmostEqual(snapshot.get_99th_percentile(), 1.0) self.assertAlmostEqual(snapshot.get_999th_percentile(), 1.0) def test_count_calls_decorator_returns_original_return_value(self): @count_calls def test(): return 1 self.assertEqual(test(), 1) def test_meter_calls_decorator_returns_original_return_value(self): @meter_calls def test(): return 1 self.assertEqual(test(), 1) def test_hist_calls_decorator_returns_original_return_value(self): @hist_calls def test(): return 1 self.assertEqual(test(), 1) def test_time_calls_decorator_returns_original_return_value(self): @time_calls def test(): return 1 self.assertEqual(test(), 1) def test_count_calls_decorator_keeps_function_name(self): @count_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_meter_calls_decorator_keeps_function_name(self): @meter_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_hist_calls_decorator_keeps_function_name(self): @hist_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_time_calls_decorator_keeps_function_name(self): @time_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_count_calls_decorator_propagates_errors(self): @count_calls def test(): raise Exception('what') self.assertRaises(Exception, test) def test_meter_calls_decorator_propagates_errors(self): @meter_calls def test(): raise Exception('what') self.assertRaises(Exception, test) def test_hist_calls_decorator_propagates_errors(self): @hist_calls def test(): raise Exception('what') self.assertRaises(Exception, test) def test_time_calls_decorator_propagates_errors(self): @time_calls def test(): raise Exception('what') self.assertRaises(Exception, test) ", https://github.com/dreid/yunomi,1aa3f166843613331b38e231264ffc3ac40e8094,yunomi/tests/test_metrics_registry.py::MetricsRegistryTests::test_hist_calls_decorator,yunomi/tests/test_metrics_registry.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from __future__ import division, absolute_import import mock from unittest2 import TestCase from yunomi.compat import xrange from yunomi.core.metrics_registry import (MetricsRegistry, counter, histogram, meter, timer, count_calls, meter_calls, hist_calls, time_calls) from yunomi.tests.util import Clock class MetricsRegistryTests(TestCase): def setUp(self): self.twisted_clock = Clock() self.registry = MetricsRegistry(clock=self.twisted_clock.seconds) def test_empty_registry(self): self.assertEqual(len(self.registry.dump_metrics()), 0) def test_getters_create_metrics(self): self.registry.counter(""counter"") self.registry.histogram(""histogram"") self.registry.meter(""meter"") self.registry.timer(""timer"") dump = self.registry.dump_metrics() self.assertEqual(len(dump), 25) metric_names = (""counter_count"", ""histogram_avg"", ""histogram_max"", ""histogram_min"", ""histogram_std_dev"", ""histogram_75_percentile"", ""histogram_98_percentile"", ""histogram_99_percentile"", ""histogram_999_percentile"", ""meter_15m_rate"", ""meter_5m_rate"", ""meter_1m_rate"", ""meter_mean_rate"", ""timer_avg"", ""timer_max"", ""timer_min"", ""timer_std_dev"", ""timer_75_percentile"", ""timer_98_percentile"", ""timer_99_percentile"", ""timer_999_percentile"", ""timer_15m_rate"", ""timer_5m_rate"", ""timer_1m_rate"", ""timer_mean_rate"") for stat in dump: self.assertTrue(stat[""name""] in metric_names) self.assertEqual(stat[""value""], 0) def test_count_calls_decorator(self): @count_calls def test(): pass for i in xrange(10): test() self.assertEqual(counter(""test_calls"").get_count(), 10) @mock.patch(""yunomi.core.meter.time"") def test_meter_calls_decorator(self, time_mock): time_mock.return_value = 0 @meter_calls def test(): pass for i in xrange(10): test() time_mock.return_value = 10 self.assertAlmostEqual(meter(""test_calls"").get_mean_rate(), 1.0) def test_hist_calls_decorator(self): @hist_calls def test(n): return n for i in xrange(1, 11): test(i) _histogram = histogram(""test_calls"") snapshot = _histogram.get_snapshot() self.assertAlmostEqual(_histogram.get_mean(), 5.5) self.assertEqual(_histogram.get_max(), 10) self.assertEqual(_histogram.get_min(), 1) self.assertAlmostEqual(_histogram.get_std_dev(), 3.02765, places=5) self.assertAlmostEqual(_histogram.get_variance(), 9.16667, places=5) self.assertAlmostEqual(snapshot.get_75th_percentile(), 8.25) self.assertAlmostEqual(snapshot.get_98th_percentile(), 10.0) self.assertAlmostEqual(snapshot.get_99th_percentile(), 10.0) self.assertAlmostEqual(snapshot.get_999th_percentile(), 10.0) @mock.patch(""yunomi.core.metrics_registry.time"") def test_time_calls_decorator(self, time_mock): time_mock.return_value = 0.0 @time_calls def test(): time_mock.return_value += 1.0 for i in xrange(10): test() _timer = timer(""test_calls"") snapshot = _timer.get_snapshot() self.assertEqual(_timer.get_count(), 10) self.assertEqual(_timer.get_max(), 1) self.assertEqual(_timer.get_min(), 1) self.assertAlmostEqual(_timer.get_std_dev(), 0) self.assertAlmostEqual(snapshot.get_75th_percentile(), 1.0) self.assertAlmostEqual(snapshot.get_98th_percentile(), 1.0) self.assertAlmostEqual(snapshot.get_99th_percentile(), 1.0) self.assertAlmostEqual(snapshot.get_999th_percentile(), 1.0) def test_count_calls_decorator_returns_original_return_value(self): @count_calls def test(): return 1 self.assertEqual(test(), 1) def test_meter_calls_decorator_returns_original_return_value(self): @meter_calls def test(): return 1 self.assertEqual(test(), 1) def test_hist_calls_decorator_returns_original_return_value(self): @hist_calls def test(): return 1 self.assertEqual(test(), 1) def test_time_calls_decorator_returns_original_return_value(self): @time_calls def test(): return 1 self.assertEqual(test(), 1) def test_count_calls_decorator_keeps_function_name(self): @count_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_meter_calls_decorator_keeps_function_name(self): @meter_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_hist_calls_decorator_keeps_function_name(self): @hist_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_time_calls_decorator_keeps_function_name(self): @time_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_count_calls_decorator_propagates_errors(self): @count_calls def test(): raise Exception('what') self.assertRaises(Exception, test) def test_meter_calls_decorator_propagates_errors(self): @meter_calls def test(): raise Exception('what') self.assertRaises(Exception, test) def test_hist_calls_decorator_propagates_errors(self): @hist_calls def test(): raise Exception('what') self.assertRaises(Exception, test) def test_time_calls_decorator_propagates_errors(self): @time_calls def test(): raise Exception('what') self.assertRaises(Exception, test) ", https://github.com/dreid/yunomi,1aa3f166843613331b38e231264ffc3ac40e8094,yunomi/tests/test_metrics_registry.py::MetricsRegistryTests::test_meter_calls_decorator,yunomi/tests/test_metrics_registry.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from __future__ import division, absolute_import import mock from unittest2 import TestCase from yunomi.compat import xrange from yunomi.core.metrics_registry import (MetricsRegistry, counter, histogram, meter, timer, count_calls, meter_calls, hist_calls, time_calls) from yunomi.tests.util import Clock class MetricsRegistryTests(TestCase): def setUp(self): self.twisted_clock = Clock() self.registry = MetricsRegistry(clock=self.twisted_clock.seconds) def test_empty_registry(self): self.assertEqual(len(self.registry.dump_metrics()), 0) def test_getters_create_metrics(self): self.registry.counter(""counter"") self.registry.histogram(""histogram"") self.registry.meter(""meter"") self.registry.timer(""timer"") dump = self.registry.dump_metrics() self.assertEqual(len(dump), 25) metric_names = (""counter_count"", ""histogram_avg"", ""histogram_max"", ""histogram_min"", ""histogram_std_dev"", ""histogram_75_percentile"", ""histogram_98_percentile"", ""histogram_99_percentile"", ""histogram_999_percentile"", ""meter_15m_rate"", ""meter_5m_rate"", ""meter_1m_rate"", ""meter_mean_rate"", ""timer_avg"", ""timer_max"", ""timer_min"", ""timer_std_dev"", ""timer_75_percentile"", ""timer_98_percentile"", ""timer_99_percentile"", ""timer_999_percentile"", ""timer_15m_rate"", ""timer_5m_rate"", ""timer_1m_rate"", ""timer_mean_rate"") for stat in dump: self.assertTrue(stat[""name""] in metric_names) self.assertEqual(stat[""value""], 0) def test_count_calls_decorator(self): @count_calls def test(): pass for i in xrange(10): test() self.assertEqual(counter(""test_calls"").get_count(), 10) @mock.patch(""yunomi.core.meter.time"") def test_meter_calls_decorator(self, time_mock): time_mock.return_value = 0 @meter_calls def test(): pass for i in xrange(10): test() time_mock.return_value = 10 self.assertAlmostEqual(meter(""test_calls"").get_mean_rate(), 1.0) def test_hist_calls_decorator(self): @hist_calls def test(n): return n for i in xrange(1, 11): test(i) _histogram = histogram(""test_calls"") snapshot = _histogram.get_snapshot() self.assertAlmostEqual(_histogram.get_mean(), 5.5) self.assertEqual(_histogram.get_max(), 10) self.assertEqual(_histogram.get_min(), 1) self.assertAlmostEqual(_histogram.get_std_dev(), 3.02765, places=5) self.assertAlmostEqual(_histogram.get_variance(), 9.16667, places=5) self.assertAlmostEqual(snapshot.get_75th_percentile(), 8.25) self.assertAlmostEqual(snapshot.get_98th_percentile(), 10.0) self.assertAlmostEqual(snapshot.get_99th_percentile(), 10.0) self.assertAlmostEqual(snapshot.get_999th_percentile(), 10.0) @mock.patch(""yunomi.core.metrics_registry.time"") def test_time_calls_decorator(self, time_mock): time_mock.return_value = 0.0 @time_calls def test(): time_mock.return_value += 1.0 for i in xrange(10): test() _timer = timer(""test_calls"") snapshot = _timer.get_snapshot() self.assertEqual(_timer.get_count(), 10) self.assertEqual(_timer.get_max(), 1) self.assertEqual(_timer.get_min(), 1) self.assertAlmostEqual(_timer.get_std_dev(), 0) self.assertAlmostEqual(snapshot.get_75th_percentile(), 1.0) self.assertAlmostEqual(snapshot.get_98th_percentile(), 1.0) self.assertAlmostEqual(snapshot.get_99th_percentile(), 1.0) self.assertAlmostEqual(snapshot.get_999th_percentile(), 1.0) def test_count_calls_decorator_returns_original_return_value(self): @count_calls def test(): return 1 self.assertEqual(test(), 1) def test_meter_calls_decorator_returns_original_return_value(self): @meter_calls def test(): return 1 self.assertEqual(test(), 1) def test_hist_calls_decorator_returns_original_return_value(self): @hist_calls def test(): return 1 self.assertEqual(test(), 1) def test_time_calls_decorator_returns_original_return_value(self): @time_calls def test(): return 1 self.assertEqual(test(), 1) def test_count_calls_decorator_keeps_function_name(self): @count_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_meter_calls_decorator_keeps_function_name(self): @meter_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_hist_calls_decorator_keeps_function_name(self): @hist_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_time_calls_decorator_keeps_function_name(self): @time_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_count_calls_decorator_propagates_errors(self): @count_calls def test(): raise Exception('what') self.assertRaises(Exception, test) def test_meter_calls_decorator_propagates_errors(self): @meter_calls def test(): raise Exception('what') self.assertRaises(Exception, test) def test_hist_calls_decorator_propagates_errors(self): @hist_calls def test(): raise Exception('what') self.assertRaises(Exception, test) def test_time_calls_decorator_propagates_errors(self): @time_calls def test(): raise Exception('what') self.assertRaises(Exception, test) ", https://github.com/dreid/yunomi,1aa3f166843613331b38e231264ffc3ac40e8094,yunomi/tests/test_metrics_registry.py::MetricsRegistryTests::test_time_calls_decorator,yunomi/tests/test_metrics_registry.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from __future__ import division, absolute_import import mock from unittest2 import TestCase from yunomi.compat import xrange from yunomi.core.metrics_registry import (MetricsRegistry, counter, histogram, meter, timer, count_calls, meter_calls, hist_calls, time_calls) from yunomi.tests.util import Clock class MetricsRegistryTests(TestCase): def setUp(self): self.twisted_clock = Clock() self.registry = MetricsRegistry(clock=self.twisted_clock.seconds) def test_empty_registry(self): self.assertEqual(len(self.registry.dump_metrics()), 0) def test_getters_create_metrics(self): self.registry.counter(""counter"") self.registry.histogram(""histogram"") self.registry.meter(""meter"") self.registry.timer(""timer"") dump = self.registry.dump_metrics() self.assertEqual(len(dump), 25) metric_names = (""counter_count"", ""histogram_avg"", ""histogram_max"", ""histogram_min"", ""histogram_std_dev"", ""histogram_75_percentile"", ""histogram_98_percentile"", ""histogram_99_percentile"", ""histogram_999_percentile"", ""meter_15m_rate"", ""meter_5m_rate"", ""meter_1m_rate"", ""meter_mean_rate"", ""timer_avg"", ""timer_max"", ""timer_min"", ""timer_std_dev"", ""timer_75_percentile"", ""timer_98_percentile"", ""timer_99_percentile"", ""timer_999_percentile"", ""timer_15m_rate"", ""timer_5m_rate"", ""timer_1m_rate"", ""timer_mean_rate"") for stat in dump: self.assertTrue(stat[""name""] in metric_names) self.assertEqual(stat[""value""], 0) def test_count_calls_decorator(self): @count_calls def test(): pass for i in xrange(10): test() self.assertEqual(counter(""test_calls"").get_count(), 10) @mock.patch(""yunomi.core.meter.time"") def test_meter_calls_decorator(self, time_mock): time_mock.return_value = 0 @meter_calls def test(): pass for i in xrange(10): test() time_mock.return_value = 10 self.assertAlmostEqual(meter(""test_calls"").get_mean_rate(), 1.0) def test_hist_calls_decorator(self): @hist_calls def test(n): return n for i in xrange(1, 11): test(i) _histogram = histogram(""test_calls"") snapshot = _histogram.get_snapshot() self.assertAlmostEqual(_histogram.get_mean(), 5.5) self.assertEqual(_histogram.get_max(), 10) self.assertEqual(_histogram.get_min(), 1) self.assertAlmostEqual(_histogram.get_std_dev(), 3.02765, places=5) self.assertAlmostEqual(_histogram.get_variance(), 9.16667, places=5) self.assertAlmostEqual(snapshot.get_75th_percentile(), 8.25) self.assertAlmostEqual(snapshot.get_98th_percentile(), 10.0) self.assertAlmostEqual(snapshot.get_99th_percentile(), 10.0) self.assertAlmostEqual(snapshot.get_999th_percentile(), 10.0) @mock.patch(""yunomi.core.metrics_registry.time"") def test_time_calls_decorator(self, time_mock): time_mock.return_value = 0.0 @time_calls def test(): time_mock.return_value += 1.0 for i in xrange(10): test() _timer = timer(""test_calls"") snapshot = _timer.get_snapshot() self.assertEqual(_timer.get_count(), 10) self.assertEqual(_timer.get_max(), 1) self.assertEqual(_timer.get_min(), 1) self.assertAlmostEqual(_timer.get_std_dev(), 0) self.assertAlmostEqual(snapshot.get_75th_percentile(), 1.0) self.assertAlmostEqual(snapshot.get_98th_percentile(), 1.0) self.assertAlmostEqual(snapshot.get_99th_percentile(), 1.0) self.assertAlmostEqual(snapshot.get_999th_percentile(), 1.0) def test_count_calls_decorator_returns_original_return_value(self): @count_calls def test(): return 1 self.assertEqual(test(), 1) def test_meter_calls_decorator_returns_original_return_value(self): @meter_calls def test(): return 1 self.assertEqual(test(), 1) def test_hist_calls_decorator_returns_original_return_value(self): @hist_calls def test(): return 1 self.assertEqual(test(), 1) def test_time_calls_decorator_returns_original_return_value(self): @time_calls def test(): return 1 self.assertEqual(test(), 1) def test_count_calls_decorator_keeps_function_name(self): @count_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_meter_calls_decorator_keeps_function_name(self): @meter_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_hist_calls_decorator_keeps_function_name(self): @hist_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_time_calls_decorator_keeps_function_name(self): @time_calls def test(): pass self.assertEqual(test.__name__, 'test') def test_count_calls_decorator_propagates_errors(self): @count_calls def test(): raise Exception('what') self.assertRaises(Exception, test) def test_meter_calls_decorator_propagates_errors(self): @meter_calls def test(): raise Exception('what') self.assertRaises(Exception, test) def test_hist_calls_decorator_propagates_errors(self): @hist_calls def test(): raise Exception('what') self.assertRaises(Exception, test) def test_time_calls_decorator_propagates_errors(self): @time_calls def test(): raise Exception('what') self.assertRaises(Exception, test) ", https://github.com/drtexx/volux,c41339aceeab4295967ea88b2edd05d0d456b2ce,tests/test_operator.py::Test_operator::test_add_module,tests/test_operator.py,NIO,flaky,Opened,https://github.com/DrTexx/Volux/pull/37,classify;root_cause,"import pytest from .context import volux # create Volux Operator object (hub for communication between modules) vlx = volux.VoluxOperator() cli_module = volux.coremodules.VoluxCliPrint() cli_UUID = None class Test_operator: def test_add_module(self): global cli_UUID cli_UUID = vlx.add_module(cli_module) assert cli_UUID in vlx.modules def test_add_module_twice(self): with pytest.raises(Exception): vlx.add_module(cli_module) def test_add_bad_module(self): with pytest.raises(TypeError): vlx.add_module(vlx) def test_remove_module(self): assert cli_UUID in vlx.modules vlx.remove_module(cli_module) assert not (cli_UUID in vlx.modules) def test_remove_missing_module(self): with pytest.raises(AttributeError): vlx.remove_module(cli_module) vlx.add_module(cli_module) def test_validate_module(self): assert vlx.validate_module(vlx) is False assert vlx.validate_module(cli_module) is True def test_get_modules(self): assert vlx.get_modules() == vlx.modules def test_add_connection(self): vlx.add_connection(volux.VoluxConnection(vlx.cli, vlx.core, 30)) def test_start_sync(self): vlx.start_sync() def test_stop_sync(self): vlx.stop_sync() if __name__ == ""__main__"": test_op = Test_operator() test_op.test_add_module() ", https://github.com/drtexx/volux,c41339aceeab4295967ea88b2edd05d0d456b2ce,tests/test_operator.py::Test_operator::test_remove_missing_module,tests/test_operator.py,NIO,flaky,Accepted,https://github.com/DrTexx/Volux/pull/36,classify;root_cause;fix_proposal,"import pytest from .context import volux # create Volux Operator object (hub for communication between modules) vlx = volux.VoluxOperator() cli_module = volux.coremodules.VoluxCliPrint() cli_UUID = None class Test_operator: def test_add_module(self): global cli_UUID cli_UUID = vlx.add_module(cli_module) assert cli_UUID in vlx.modules def test_add_module_twice(self): with pytest.raises(Exception): vlx.add_module(cli_module) def test_add_bad_module(self): with pytest.raises(TypeError): vlx.add_module(vlx) def test_remove_module(self): assert cli_UUID in vlx.modules vlx.remove_module(cli_module) assert not (cli_UUID in vlx.modules) def test_remove_missing_module(self): with pytest.raises(AttributeError): vlx.remove_module(cli_module) vlx.add_module(cli_module) def test_validate_module(self): assert vlx.validate_module(vlx) is False assert vlx.validate_module(cli_module) is True def test_get_modules(self): assert vlx.get_modules() == vlx.modules def test_add_connection(self): vlx.add_connection(volux.VoluxConnection(vlx.cli, vlx.core, 30)) def test_start_sync(self): vlx.start_sync() def test_stop_sync(self): vlx.stop_sync() if __name__ == ""__main__"": test_op = Test_operator() test_op.test_add_module() ", https://github.com/DusanMadar/PySyncDroid,9a217a54f4286af8e7505c184eb2a6775f52d815,tests/test_sync.py::TestSync::test_do_sync,tests/test_sync.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"""""""Tests for synchronization functionality."""""" from io import StringIO import os import unittest from unittest.mock import call, patch import pysyncdroid from pysyncdroid.exceptions import BashException, IgnoredTypeException from pysyncdroid.gvfs import cp, mkdir, rm from pysyncdroid.sync import Sync, readlink, REMOVE, SYNCHRONIZE FAKE_MTP_DETAILS = ( ""mtp://[usb:002,003]/"", ""/run/user//gvfs/mtp:host=%5Busb%3A002%2C003%5D"", ) FAKE_SYNC_DATA = { ""src_dir_abs"": ""/tmp/testdir"", ""src_dir_fls"": [""/tmp/testdir/song.mp3"", ""/tmp/testdir/demo.mp3""], ""dst_dir_fls"": [ ""/run/user//gvfs/mtp:host=%5Busb%3A002%2C003%5D/Card/Music/testdir/demo.mp3"", # noqa ""/run/user//gvfs/mtp:host=%5Busb%3A002%2C003%5D/Card/Music/testdir/oldsong.mp3"", # noqa ], ""dst_dir_abs"": ""/run/user//gvfs/mtp:host=%5Busb%3A002%2C003%5D/Card/Music/testdir"", # noqa } class TestReadLink(unittest.TestCase): def setUp(self): self.patcher = patch(""pysyncdroid.utils.run_bash_cmd"") self.mock_run_bash_cmd = self.patcher.start() def tearDown(self): self.patcher.stop() def test_readlink_no_path(self): """""" Test 'readlink' returns an empty string in case of no path to read. """""" self.assertEqual(readlink(""""), """") @patch(""pysyncdroid.sync.os.path.expanduser"") def test_readlink_tilde(self, mock_expanduser): """""" Test 'readlink' hadles '~' path. """""" mock_expanduser.return_value = ""/home/"" self.mock_run_bash_cmd.return_value = mock_expanduser.return_value self.assertEqual(readlink(""~""), mock_expanduser.return_value) def test_readlink_slash(self): """""" Test 'readlink' hadles '/' path (and doesn't strip it). """""" self.mock_run_bash_cmd.return_value = ""/"" self.assertEqual(readlink(""/""), ""/"") def test_readlink_nonexisting(self): """""" Test 'readlink' is agnostinc to the path existance and simply adds the provided string (without a slash) to the current working directory. """""" self.mock_run_bash_cmd.return_value = os.path.join(os.getcwd(), ""foo"") self.assertEqual(readlink(""foo""), self.mock_run_bash_cmd.return_value) def test_readlink_device_path(self): """""" Test 'readlink' returns the given path if it's unable to follow it. """""" self.mock_run_bash_cmd.return_value = ""Phone/Card"" self.assertEqual(readlink(""Phone/Card""), ""Phone/Card"") class TestSync(unittest.TestCase): def _create_empty_sync_data(self, sync): """""" Create empty sync data dictionary. :argument sync: Sync instance :type sync: object :returns dict """""" src_subdir_abs = os.path.join(sync.source, ""testdir"") dst_subdir_abs = os.path.join(sync.destination, ""testdir"") return sync.sync_data_template(src_subdir_abs, dst_subdir_abs) # # '_verbose()' @patch(""sys.stdout"", new_callable=StringIO) def test_verbose_active(self, mock_stdout): """""" Test '_verbose' prints a given message if Sync is in verbose mode. """""" sync = Sync(FAKE_MTP_DETAILS, """", """", verbose=True) test_message = ""Hello World!"" sync._verbose(test_message) self.assertEqual(test_message, mock_stdout.getvalue().strip()) @patch(""sys.stdout"", new_callable=StringIO) def test_verbose_inactive(self, mock_stdout): """""" Test '_verbose' doesn't print a given message if Sync isn't in verbose mode. """""" sync = Sync(FAKE_MTP_DETAILS, """", """", verbose=False) sync._verbose(""Hello World!"") self.assertEqual("""", mock_stdout.getvalue().strip()) # # 'gvfs_wrapper()' @patch(""pysyncdroid.gvfs.mkdir"") def test_gvfs_wrapper_common_exception(self, mock_mkdir): """""" Test 'gvfs_wrapper' isn't ignoring common exceptions. """""" mock_mkdir.side_effect = ValueError sync = Sync(FAKE_MTP_DETAILS, """", """") with self.assertRaises(ValueError): sync.gvfs_wrapper(mock_mkdir, ""/tmp/dir"") @patch(""pysyncdroid.gvfs.mkdir"") def test_gvfs_wrapper_bash_exception_any(self, mock_mkdir): """""" Test 'gvfs_wrapper' doesn't handle any BashException. """""" mock_mkdir.side_effect = BashException sync = Sync(FAKE_MTP_DETAILS, """", """") with self.assertRaises(BashException): sync.gvfs_wrapper(mock_mkdir, ""/tmp/dir"") @patch(""pysyncdroid.gvfs.mkdir"") @patch(""pysyncdroid.gvfs.mount"") def test_gvfs_wrapper_bash_exception_exact(self, mock_mount, mock_mkdir): """""" Test 'gvfs_wrapper' handles only a specific BashException. """""" mock_mkdir.side_effect = [ BashException(""Connection reset by peer""), None, ] sync = Sync(FAKE_MTP_DETAILS, """", """") sync.gvfs_wrapper(mock_mkdir, ""/tmp/dir"") self.assertEqual(mock_mkdir.call_count, 2) mock_mount.assert_called_once_with(sync.mtp_url) # # 'set_source_abs()' @patch(""pysyncdroid.sync.os.path.isdir"") @patch(""pysyncdroid.sync.os.path.exists"") def test_set_source_abs_absolute_path( self, mock_path_exists, mock_path_isdir ): """""" Test 'set_source_abs' recongizes an absolute path. """""" mock_path_exists.return_value = True mock_path_isdir.return_value = True sync = Sync(FAKE_MTP_DETAILS, ""/an-absolute-path"", """") sync.set_source_abs() self.assertEqual(sync.source, ""/an-absolute-path"") @patch(""pysyncdroid.sync.os.path.isdir"") @patch(""pysyncdroid.sync.os.path.exists"") def test_set_source_abs_computer_relative_path( self, mock_path_exists, mock_path_isdir ): """""" Test 'set_source_abs' creates an absolute path from a relative path when the path exists on computer. """""" mock_path_exists.return_value = True mock_path_isdir.return_value = True sync = Sync(FAKE_MTP_DETAILS, ""a-relative-path/"", """") sync.set_source_abs() expected_abs_path = os.path.join(os.getcwd(), ""a-relative-path"") self.assertEqual(sync.source, expected_abs_path) @patch(""pysyncdroid.sync.os.path.isdir"") @patch(""pysyncdroid.sync.os.path.exists"") def test_set_source_abs_device_relative_path( self, mock_path_exists, mock_path_isdir ): """""" Test 'set_source_abs' creates an absolute path from a relative path when the path exists on device. """""" mock_path_exists.side_effect = [False, True, True] mock_path_isdir.return_value = True sync = Sync(FAKE_MTP_DETAILS, ""Card/Music"", """") sync.set_source_abs() expected_abs_path = os.path.join(sync.mtp_gvfs_path, ""Card/Music"") self.assertEqual(sync.source, expected_abs_path) @patch(""pysyncdroid.sync.os.path.exists"") def test_set_source_abs_nonexistent(self, mock_path_exists): """""" Test 'set_source_abs' raises an OSError when source doesn't exist on the computer or on the device. """""" mock_path_exists.return_value = False with self.assertRaises(OSError) as exc: sync = Sync(FAKE_MTP_DETAILS, ""non-exiting-path"", """") sync.set_source_abs() # Must be called twice - for computer and device. self.assertEqual(mock_path_exists.call_count, 2) self.assertEqual( str(exc.exception), '""non-exiting-path"" does not exist on computer or on device.', ) @patch(""pysyncdroid.sync.os.path.isdir"") @patch(""pysyncdroid.sync.os.path.exists"") def test_set_source_abs_not_directory( self, mock_path_exists, mock_path_isdir ): """""" Test 'set_source_abs' raises an OSError when source exists but is not a directory. """""" mock_path_exists.return_value = True mock_path_isdir.return_value = False with self.assertRaises(OSError) as exc: sync = Sync(FAKE_MTP_DETAILS, ""not-a-directory"", """") sync.set_source_abs() expected_abs_path = os.path.join(os.getcwd(), ""not-a-directory"") err_msg = '""{}"" is not a directory.'.format(expected_abs_path) self.assertEqual(str(exc.exception), err_msg) # # 'set_destination_abs()' def test_set_destination_abs_absolute_path(self): """""" Test 'set_destination_abs' recongizes an absolute path. """""" sync = Sync(FAKE_MTP_DETAILS, """", ""/an-absolute-path"") sync.set_destination_abs() self.assertEqual(sync.destination, ""/an-absolute-path"") def test_set_destination_abs_computer_relative_path(self): """""" Test 'set_destination_abs' creates an absolute path from a relative path when the destination is on computer. """""" sync = Sync( FAKE_MTP_DETAILS, ""/test-mtp:host-path"", ""a-relative-path/"" ) sync.set_destination_abs() expected_abs_path = os.path.join(os.getcwd(), ""a-relative-path"") self.assertEqual(sync.destination, expected_abs_path) def test_set_destination_abs_device_relative_path(self): """""" Test 'set_destination_abs' creates an absolute path from a relative path when the destination is on device. """""" sync = Sync(FAKE_MTP_DETAILS, """", ""Card/Music"") sync.set_destination_abs() expected_abs_path = os.path.join(sync.mtp_gvfs_path, ""Card/Music"") self.assertEqual(sync.destination, expected_abs_path) # # 'set_destination_subdir_abs()' def test_set_destination_subdir_absh(self): """""" Test 'set_destination_subdir_abs' creates an absolute path for a destination subdir. """""" sync = Sync(FAKE_MTP_DETAILS, ""~", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::CurveTests::test_curve,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::DrillTests::test_drill,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::DrillTests::test_drill_offset,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::DrillTests::test_drill_oval,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::LineTests::test_line,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::ModelTests::test_model,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::ModuleTests::test_module,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::NetTests::test_net_auto_numbering,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::PadTests::test_pad,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::PadTests::test_pad_with_drill,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::PadTests::test_pad_with_multiple_layers,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::PolygonTests::test_polygon,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::TextTests::test_text,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::TextTests::test_text_with_hide,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::TextTests::test_text_with_justify,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::TextTests::test_text_with_rotation,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_module.py::TextTests::test_text_with_thickness,tests/test_module.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.module import * class DrillTests(unittest.TestCase): def test_drill(self): drill = Drill.parse('(drill 0.8)') assert drill.size == 0.8 assert Drill.parse(drill.to_string()) == drill def test_drill_oval(self): drill = Drill.parse('(drill oval 0.6 0.8)') assert drill.size == [0.6, 0.8] assert Drill.parse(drill.to_string()) == drill def test_drill_offset(self): drill = Drill.parse('(drill 0.8 (offset 0.1 0.2))') assert drill.size == 0.8 and drill.offset == [0.1, 0.2] assert Drill.parse(drill.to_string()) == drill class PadTests(unittest.TestCase): def test_pad(self): pad = Pad.parse( '(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) (layers F.Cu))') assert pad.name == '1' assert pad.type == 'smd' assert pad.shape == 'rect' assert pad.at == [0.1, 0.1] assert pad.size == [0.2, 0.2] assert pad.layers == ['F.Cu'] assert Pad.parse(pad.to_string()) == pad def test_pad_with_drill(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu) (drill 0.8))') assert pad.drill.size == 0.8 assert Pad.parse(pad.to_string()) == pad def test_pad_with_multiple_layers(self): pad = Pad.parse('(pad 1 smd rect (at 0.1 0.1) (size 0.2 0.2) ' '(layers F.Cu B.Cu))') assert pad.layers == ['F.Cu', 'B.Cu'] assert Pad.parse(pad.to_string()) == pad class TextTests(unittest.TestCase): def test_text(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS))') assert text.type == 'user' assert text.text == 'text' assert text.at == [0.0, 0.0] assert text.layer == 'F.SilkS' assert Text.parse(text.to_string()) == text def test_text_with_rotation(self): text = Text.parse( '(fp_text user text (at 0.0 0.0 0.0) (layer F.SilkS))') assert text.at == [0.0, 0.0, 0.0] assert text.hide == False assert Text.parse(text.to_string()) == text def test_text_with_hide(self): text = Text.parse( '(fp_text user text (at 0.0 0.0) (layer F.SilkS) hide)') assert text.hide == True assert Text.parse(text.to_string()) == text def test_text_with_thickness(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer F.SilkS) ' '(effects (font (size 0.1 0.1) (thickness 0.2))))') assert text.size == [0.1, 0.1] assert text.thickness == 0.2 assert Text.parse(text.to_string()) == text def test_text_with_justify(self): text = Text.parse('(fp_text user text (at 0.0 0.0) (layer layer) ' '(effects (justify mirror)))') assert text.justify == 'mirror' assert Text.parse(text.to_string()) == text class LineTests(unittest.TestCase): def test_line(self): line = Line.parse('(fp_line (start 0 0) (end 1 1) (layer layer))') assert line.start == [0.0, 0.0] assert line.end == [1.0, 1.0] assert line.layer == 'layer' assert Line.parse(line.to_string()) == line class PolygonTests(unittest.TestCase): def test_polygon(self): poly = Polygon.parse('(fp_poly (pts (xy 0 0) (xy 1 0)) (width 0.01))') assert poly.pts == [(0, 0), (1, 0)] assert poly.width == 0.01 assert Polygon.parse(poly.to_string()) == poly class CurveTests(unittest.TestCase): def test_curve(self): curve = Curve.parse( '(fp_curve (pts (xy 0 0) (xy 1 1) (xy 2 2) (xy 3 3)))') assert curve.start == (0, 0) assert curve.bezier1 == (1, 1) assert curve.bezier2 == (2, 2) assert curve.end == (3, 3) class ModelTests(unittest.TestCase): def test_model(self): model_string = ( '(model path' ' (at (xyz 0 0 0))' ' (scale (xyz 0 0 0))' ' (rotate (xyz 0 0 0)))') model = Model.parse(model_string) assert model.at == (0, 0, 0) assert model.scale == (0, 0, 0) assert model.rotate == (0, 0, 0) assert Model.parse(model.to_string()) == model class ModuleTests(unittest.TestCase): def test_module(self): module_string = '(module name (layer F.Cu) %s)' module = Module.parse(module_string % '') assert module.name == 'name' assert module.layer == 'F.Cu' assert Module.parse(module.to_string()) == module pads = '' for i in range(2): pads += Pad(str(i + 1), drill=Drill(0.8)).to_string() module = Module.parse(module_string % pads) assert module.pads[0].name == '1' assert module.pads[0].drill.size == 0.8 assert module.pads[1].name == '2' assert module.pads[1].drill.size == 0.8 assert Module.parse(module.to_string()) == module class NetTests(unittest.TestCase): def test_net_auto_numbering(self): n1, n2, n3 = Net(), Net(), Net() assert n1.code == 1 assert n2.code == 2 assert n3.code == 3 ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_pcb.py::NetClassTests::test_parse,tests/test_pcb.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.pcb import * class NetClassTests(unittest.TestCase): def test_parse(self): nc_string = ""(net_class name description (add_net GND))"" nc = NetClass.parse(nc_string) assert nc.name == 'name' assert nc.description == 'description' assert nc.nets == ['GND'] def test_without_net(self): nc = NetClass('default') assert NetClass.parse(nc.to_string()) == nc def test_with_net(self): nc = NetClass('default') nc.nets.append('GND') assert NetClass.parse(nc.to_string()) == nc def test_with_multiple_nets(self): nc = NetClass('default') nc.nets += ['GND', 'VO'] nc_str = nc.to_string() assert NetClass.parse(nc.to_string()) == nc class ZoneTests(unittest.TestCase): def test_parse(self): zone_string = '(zone (polygon (pts (xy 0 0) (xy 1 1))))' zone = Zone.parse(zone_string) assert zone.polygon[0] == (0, 0) assert zone.polygon[1] == (1, 1) assert Zone.parse(zone.to_string()) == zone class PcbTests(unittest.TestCase): def test_minimal_pcb(self): pcb_string = open('tests/minimal_pcb.kicad_pcb', 'r').read() pcb = Pcb.parse(pcb_string) assert pcb.version == 123 assert pcb.host == ['pcbnew', 'version'] assert len(pcb.nets) == 4 assert len(pcb.modules) == 2 assert Pcb.parse(pcb.to_string()) == pcb def test_page(self): pcb = Pcb() assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = 'A4' assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = [200, 200] assert Pcb.parse(pcb.to_string()) == pcb def test_single_module(self): pcb = Pcb() pcb.modules.append(Module(name='R')) assert Pcb.parse(pcb.to_string()) == pcb def test_comment(self): pcb = Pcb(comment1='hello world', comment2='bye world') assert Pcb.parse(pcb.to_string()) == pcb def test_num_nets(self): pcb = Pcb(num_nets=5) assert Pcb.parse(pcb.to_string()) == pcb pcb.nets.append(Net()) assert Pcb.parse(pcb.to_string()) == pcb def test_layers(self): pcb = Pcb() pcb.layers.append(Layer('B.Cu')) assert Pcb.parse(pcb.to_string()) == pcb pcb.layers.append(Layer('F.Cu')) assert Pcb.parse(pcb.to_string()) == pcb ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_pcb.py::NetClassTests::test_without_net,tests/test_pcb.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.pcb import * class NetClassTests(unittest.TestCase): def test_parse(self): nc_string = ""(net_class name description (add_net GND))"" nc = NetClass.parse(nc_string) assert nc.name == 'name' assert nc.description == 'description' assert nc.nets == ['GND'] def test_without_net(self): nc = NetClass('default') assert NetClass.parse(nc.to_string()) == nc def test_with_net(self): nc = NetClass('default') nc.nets.append('GND') assert NetClass.parse(nc.to_string()) == nc def test_with_multiple_nets(self): nc = NetClass('default') nc.nets += ['GND', 'VO'] nc_str = nc.to_string() assert NetClass.parse(nc.to_string()) == nc class ZoneTests(unittest.TestCase): def test_parse(self): zone_string = '(zone (polygon (pts (xy 0 0) (xy 1 1))))' zone = Zone.parse(zone_string) assert zone.polygon[0] == (0, 0) assert zone.polygon[1] == (1, 1) assert Zone.parse(zone.to_string()) == zone class PcbTests(unittest.TestCase): def test_minimal_pcb(self): pcb_string = open('tests/minimal_pcb.kicad_pcb', 'r').read() pcb = Pcb.parse(pcb_string) assert pcb.version == 123 assert pcb.host == ['pcbnew', 'version'] assert len(pcb.nets) == 4 assert len(pcb.modules) == 2 assert Pcb.parse(pcb.to_string()) == pcb def test_page(self): pcb = Pcb() assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = 'A4' assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = [200, 200] assert Pcb.parse(pcb.to_string()) == pcb def test_single_module(self): pcb = Pcb() pcb.modules.append(Module(name='R')) assert Pcb.parse(pcb.to_string()) == pcb def test_comment(self): pcb = Pcb(comment1='hello world', comment2='bye world') assert Pcb.parse(pcb.to_string()) == pcb def test_num_nets(self): pcb = Pcb(num_nets=5) assert Pcb.parse(pcb.to_string()) == pcb pcb.nets.append(Net()) assert Pcb.parse(pcb.to_string()) == pcb def test_layers(self): pcb = Pcb() pcb.layers.append(Layer('B.Cu')) assert Pcb.parse(pcb.to_string()) == pcb pcb.layers.append(Layer('F.Cu')) assert Pcb.parse(pcb.to_string()) == pcb ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_pcb.py::NetClassTests::test_with_multiple_nets,tests/test_pcb.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.pcb import * class NetClassTests(unittest.TestCase): def test_parse(self): nc_string = ""(net_class name description (add_net GND))"" nc = NetClass.parse(nc_string) assert nc.name == 'name' assert nc.description == 'description' assert nc.nets == ['GND'] def test_without_net(self): nc = NetClass('default') assert NetClass.parse(nc.to_string()) == nc def test_with_net(self): nc = NetClass('default') nc.nets.append('GND') assert NetClass.parse(nc.to_string()) == nc def test_with_multiple_nets(self): nc = NetClass('default') nc.nets += ['GND', 'VO'] nc_str = nc.to_string() assert NetClass.parse(nc.to_string()) == nc class ZoneTests(unittest.TestCase): def test_parse(self): zone_string = '(zone (polygon (pts (xy 0 0) (xy 1 1))))' zone = Zone.parse(zone_string) assert zone.polygon[0] == (0, 0) assert zone.polygon[1] == (1, 1) assert Zone.parse(zone.to_string()) == zone class PcbTests(unittest.TestCase): def test_minimal_pcb(self): pcb_string = open('tests/minimal_pcb.kicad_pcb', 'r').read() pcb = Pcb.parse(pcb_string) assert pcb.version == 123 assert pcb.host == ['pcbnew', 'version'] assert len(pcb.nets) == 4 assert len(pcb.modules) == 2 assert Pcb.parse(pcb.to_string()) == pcb def test_page(self): pcb = Pcb() assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = 'A4' assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = [200, 200] assert Pcb.parse(pcb.to_string()) == pcb def test_single_module(self): pcb = Pcb() pcb.modules.append(Module(name='R')) assert Pcb.parse(pcb.to_string()) == pcb def test_comment(self): pcb = Pcb(comment1='hello world', comment2='bye world') assert Pcb.parse(pcb.to_string()) == pcb def test_num_nets(self): pcb = Pcb(num_nets=5) assert Pcb.parse(pcb.to_string()) == pcb pcb.nets.append(Net()) assert Pcb.parse(pcb.to_string()) == pcb def test_layers(self): pcb = Pcb() pcb.layers.append(Layer('B.Cu')) assert Pcb.parse(pcb.to_string()) == pcb pcb.layers.append(Layer('F.Cu')) assert Pcb.parse(pcb.to_string()) == pcb ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_pcb.py::NetClassTests::test_with_net,tests/test_pcb.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.pcb import * class NetClassTests(unittest.TestCase): def test_parse(self): nc_string = ""(net_class name description (add_net GND))"" nc = NetClass.parse(nc_string) assert nc.name == 'name' assert nc.description == 'description' assert nc.nets == ['GND'] def test_without_net(self): nc = NetClass('default') assert NetClass.parse(nc.to_string()) == nc def test_with_net(self): nc = NetClass('default') nc.nets.append('GND') assert NetClass.parse(nc.to_string()) == nc def test_with_multiple_nets(self): nc = NetClass('default') nc.nets += ['GND', 'VO'] nc_str = nc.to_string() assert NetClass.parse(nc.to_string()) == nc class ZoneTests(unittest.TestCase): def test_parse(self): zone_string = '(zone (polygon (pts (xy 0 0) (xy 1 1))))' zone = Zone.parse(zone_string) assert zone.polygon[0] == (0, 0) assert zone.polygon[1] == (1, 1) assert Zone.parse(zone.to_string()) == zone class PcbTests(unittest.TestCase): def test_minimal_pcb(self): pcb_string = open('tests/minimal_pcb.kicad_pcb', 'r').read() pcb = Pcb.parse(pcb_string) assert pcb.version == 123 assert pcb.host == ['pcbnew', 'version'] assert len(pcb.nets) == 4 assert len(pcb.modules) == 2 assert Pcb.parse(pcb.to_string()) == pcb def test_page(self): pcb = Pcb() assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = 'A4' assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = [200, 200] assert Pcb.parse(pcb.to_string()) == pcb def test_single_module(self): pcb = Pcb() pcb.modules.append(Module(name='R')) assert Pcb.parse(pcb.to_string()) == pcb def test_comment(self): pcb = Pcb(comment1='hello world', comment2='bye world') assert Pcb.parse(pcb.to_string()) == pcb def test_num_nets(self): pcb = Pcb(num_nets=5) assert Pcb.parse(pcb.to_string()) == pcb pcb.nets.append(Net()) assert Pcb.parse(pcb.to_string()) == pcb def test_layers(self): pcb = Pcb() pcb.layers.append(Layer('B.Cu')) assert Pcb.parse(pcb.to_string()) == pcb pcb.layers.append(Layer('F.Cu')) assert Pcb.parse(pcb.to_string()) == pcb ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_pcb.py::PcbTests::test_comment,tests/test_pcb.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.pcb import * class NetClassTests(unittest.TestCase): def test_parse(self): nc_string = ""(net_class name description (add_net GND))"" nc = NetClass.parse(nc_string) assert nc.name == 'name' assert nc.description == 'description' assert nc.nets == ['GND'] def test_without_net(self): nc = NetClass('default') assert NetClass.parse(nc.to_string()) == nc def test_with_net(self): nc = NetClass('default') nc.nets.append('GND') assert NetClass.parse(nc.to_string()) == nc def test_with_multiple_nets(self): nc = NetClass('default') nc.nets += ['GND', 'VO'] nc_str = nc.to_string() assert NetClass.parse(nc.to_string()) == nc class ZoneTests(unittest.TestCase): def test_parse(self): zone_string = '(zone (polygon (pts (xy 0 0) (xy 1 1))))' zone = Zone.parse(zone_string) assert zone.polygon[0] == (0, 0) assert zone.polygon[1] == (1, 1) assert Zone.parse(zone.to_string()) == zone class PcbTests(unittest.TestCase): def test_minimal_pcb(self): pcb_string = open('tests/minimal_pcb.kicad_pcb', 'r').read() pcb = Pcb.parse(pcb_string) assert pcb.version == 123 assert pcb.host == ['pcbnew', 'version'] assert len(pcb.nets) == 4 assert len(pcb.modules) == 2 assert Pcb.parse(pcb.to_string()) == pcb def test_page(self): pcb = Pcb() assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = 'A4' assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = [200, 200] assert Pcb.parse(pcb.to_string()) == pcb def test_single_module(self): pcb = Pcb() pcb.modules.append(Module(name='R')) assert Pcb.parse(pcb.to_string()) == pcb def test_comment(self): pcb = Pcb(comment1='hello world', comment2='bye world') assert Pcb.parse(pcb.to_string()) == pcb def test_num_nets(self): pcb = Pcb(num_nets=5) assert Pcb.parse(pcb.to_string()) == pcb pcb.nets.append(Net()) assert Pcb.parse(pcb.to_string()) == pcb def test_layers(self): pcb = Pcb() pcb.layers.append(Layer('B.Cu')) assert Pcb.parse(pcb.to_string()) == pcb pcb.layers.append(Layer('F.Cu')) assert Pcb.parse(pcb.to_string()) == pcb ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_pcb.py::PcbTests::test_layers,tests/test_pcb.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.pcb import * class NetClassTests(unittest.TestCase): def test_parse(self): nc_string = ""(net_class name description (add_net GND))"" nc = NetClass.parse(nc_string) assert nc.name == 'name' assert nc.description == 'description' assert nc.nets == ['GND'] def test_without_net(self): nc = NetClass('default') assert NetClass.parse(nc.to_string()) == nc def test_with_net(self): nc = NetClass('default') nc.nets.append('GND') assert NetClass.parse(nc.to_string()) == nc def test_with_multiple_nets(self): nc = NetClass('default') nc.nets += ['GND', 'VO'] nc_str = nc.to_string() assert NetClass.parse(nc.to_string()) == nc class ZoneTests(unittest.TestCase): def test_parse(self): zone_string = '(zone (polygon (pts (xy 0 0) (xy 1 1))))' zone = Zone.parse(zone_string) assert zone.polygon[0] == (0, 0) assert zone.polygon[1] == (1, 1) assert Zone.parse(zone.to_string()) == zone class PcbTests(unittest.TestCase): def test_minimal_pcb(self): pcb_string = open('tests/minimal_pcb.kicad_pcb', 'r').read() pcb = Pcb.parse(pcb_string) assert pcb.version == 123 assert pcb.host == ['pcbnew', 'version'] assert len(pcb.nets) == 4 assert len(pcb.modules) == 2 assert Pcb.parse(pcb.to_string()) == pcb def test_page(self): pcb = Pcb() assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = 'A4' assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = [200, 200] assert Pcb.parse(pcb.to_string()) == pcb def test_single_module(self): pcb = Pcb() pcb.modules.append(Module(name='R')) assert Pcb.parse(pcb.to_string()) == pcb def test_comment(self): pcb = Pcb(comment1='hello world', comment2='bye world') assert Pcb.parse(pcb.to_string()) == pcb def test_num_nets(self): pcb = Pcb(num_nets=5) assert Pcb.parse(pcb.to_string()) == pcb pcb.nets.append(Net()) assert Pcb.parse(pcb.to_string()) == pcb def test_layers(self): pcb = Pcb() pcb.layers.append(Layer('B.Cu')) assert Pcb.parse(pcb.to_string()) == pcb pcb.layers.append(Layer('F.Cu')) assert Pcb.parse(pcb.to_string()) == pcb ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_pcb.py::PcbTests::test_minimal_pcb,tests/test_pcb.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.pcb import * class NetClassTests(unittest.TestCase): def test_parse(self): nc_string = ""(net_class name description (add_net GND))"" nc = NetClass.parse(nc_string) assert nc.name == 'name' assert nc.description == 'description' assert nc.nets == ['GND'] def test_without_net(self): nc = NetClass('default') assert NetClass.parse(nc.to_string()) == nc def test_with_net(self): nc = NetClass('default') nc.nets.append('GND') assert NetClass.parse(nc.to_string()) == nc def test_with_multiple_nets(self): nc = NetClass('default') nc.nets += ['GND', 'VO'] nc_str = nc.to_string() assert NetClass.parse(nc.to_string()) == nc class ZoneTests(unittest.TestCase): def test_parse(self): zone_string = '(zone (polygon (pts (xy 0 0) (xy 1 1))))' zone = Zone.parse(zone_string) assert zone.polygon[0] == (0, 0) assert zone.polygon[1] == (1, 1) assert Zone.parse(zone.to_string()) == zone class PcbTests(unittest.TestCase): def test_minimal_pcb(self): pcb_string = open('tests/minimal_pcb.kicad_pcb', 'r').read() pcb = Pcb.parse(pcb_string) assert pcb.version == 123 assert pcb.host == ['pcbnew', 'version'] assert len(pcb.nets) == 4 assert len(pcb.modules) == 2 assert Pcb.parse(pcb.to_string()) == pcb def test_page(self): pcb = Pcb() assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = 'A4' assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = [200, 200] assert Pcb.parse(pcb.to_string()) == pcb def test_single_module(self): pcb = Pcb() pcb.modules.append(Module(name='R')) assert Pcb.parse(pcb.to_string()) == pcb def test_comment(self): pcb = Pcb(comment1='hello world', comment2='bye world') assert Pcb.parse(pcb.to_string()) == pcb def test_num_nets(self): pcb = Pcb(num_nets=5) assert Pcb.parse(pcb.to_string()) == pcb pcb.nets.append(Net()) assert Pcb.parse(pcb.to_string()) == pcb def test_layers(self): pcb = Pcb() pcb.layers.append(Layer('B.Cu')) assert Pcb.parse(pcb.to_string()) == pcb pcb.layers.append(Layer('F.Cu')) assert Pcb.parse(pcb.to_string()) == pcb ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_pcb.py::PcbTests::test_num_nets,tests/test_pcb.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.pcb import * class NetClassTests(unittest.TestCase): def test_parse(self): nc_string = ""(net_class name description (add_net GND))"" nc = NetClass.parse(nc_string) assert nc.name == 'name' assert nc.description == 'description' assert nc.nets == ['GND'] def test_without_net(self): nc = NetClass('default') assert NetClass.parse(nc.to_string()) == nc def test_with_net(self): nc = NetClass('default') nc.nets.append('GND') assert NetClass.parse(nc.to_string()) == nc def test_with_multiple_nets(self): nc = NetClass('default') nc.nets += ['GND', 'VO'] nc_str = nc.to_string() assert NetClass.parse(nc.to_string()) == nc class ZoneTests(unittest.TestCase): def test_parse(self): zone_string = '(zone (polygon (pts (xy 0 0) (xy 1 1))))' zone = Zone.parse(zone_string) assert zone.polygon[0] == (0, 0) assert zone.polygon[1] == (1, 1) assert Zone.parse(zone.to_string()) == zone class PcbTests(unittest.TestCase): def test_minimal_pcb(self): pcb_string = open('tests/minimal_pcb.kicad_pcb', 'r').read() pcb = Pcb.parse(pcb_string) assert pcb.version == 123 assert pcb.host == ['pcbnew', 'version'] assert len(pcb.nets) == 4 assert len(pcb.modules) == 2 assert Pcb.parse(pcb.to_string()) == pcb def test_page(self): pcb = Pcb() assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = 'A4' assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = [200, 200] assert Pcb.parse(pcb.to_string()) == pcb def test_single_module(self): pcb = Pcb() pcb.modules.append(Module(name='R')) assert Pcb.parse(pcb.to_string()) == pcb def test_comment(self): pcb = Pcb(comment1='hello world', comment2='bye world') assert Pcb.parse(pcb.to_string()) == pcb def test_num_nets(self): pcb = Pcb(num_nets=5) assert Pcb.parse(pcb.to_string()) == pcb pcb.nets.append(Net()) assert Pcb.parse(pcb.to_string()) == pcb def test_layers(self): pcb = Pcb() pcb.layers.append(Layer('B.Cu')) assert Pcb.parse(pcb.to_string()) == pcb pcb.layers.append(Layer('F.Cu')) assert Pcb.parse(pcb.to_string()) == pcb ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_pcb.py::PcbTests::test_page,tests/test_pcb.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.pcb import * class NetClassTests(unittest.TestCase): def test_parse(self): nc_string = ""(net_class name description (add_net GND))"" nc = NetClass.parse(nc_string) assert nc.name == 'name' assert nc.description == 'description' assert nc.nets == ['GND'] def test_without_net(self): nc = NetClass('default') assert NetClass.parse(nc.to_string()) == nc def test_with_net(self): nc = NetClass('default') nc.nets.append('GND') assert NetClass.parse(nc.to_string()) == nc def test_with_multiple_nets(self): nc = NetClass('default') nc.nets += ['GND', 'VO'] nc_str = nc.to_string() assert NetClass.parse(nc.to_string()) == nc class ZoneTests(unittest.TestCase): def test_parse(self): zone_string = '(zone (polygon (pts (xy 0 0) (xy 1 1))))' zone = Zone.parse(zone_string) assert zone.polygon[0] == (0, 0) assert zone.polygon[1] == (1, 1) assert Zone.parse(zone.to_string()) == zone class PcbTests(unittest.TestCase): def test_minimal_pcb(self): pcb_string = open('tests/minimal_pcb.kicad_pcb', 'r').read() pcb = Pcb.parse(pcb_string) assert pcb.version == 123 assert pcb.host == ['pcbnew', 'version'] assert len(pcb.nets) == 4 assert len(pcb.modules) == 2 assert Pcb.parse(pcb.to_string()) == pcb def test_page(self): pcb = Pcb() assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = 'A4' assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = [200, 200] assert Pcb.parse(pcb.to_string()) == pcb def test_single_module(self): pcb = Pcb() pcb.modules.append(Module(name='R')) assert Pcb.parse(pcb.to_string()) == pcb def test_comment(self): pcb = Pcb(comment1='hello world', comment2='bye world') assert Pcb.parse(pcb.to_string()) == pcb def test_num_nets(self): pcb = Pcb(num_nets=5) assert Pcb.parse(pcb.to_string()) == pcb pcb.nets.append(Net()) assert Pcb.parse(pcb.to_string()) == pcb def test_layers(self): pcb = Pcb() pcb.layers.append(Layer('B.Cu')) assert Pcb.parse(pcb.to_string()) == pcb pcb.layers.append(Layer('F.Cu')) assert Pcb.parse(pcb.to_string()) == pcb ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_pcb.py::PcbTests::test_single_module,tests/test_pcb.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.pcb import * class NetClassTests(unittest.TestCase): def test_parse(self): nc_string = ""(net_class name description (add_net GND))"" nc = NetClass.parse(nc_string) assert nc.name == 'name' assert nc.description == 'description' assert nc.nets == ['GND'] def test_without_net(self): nc = NetClass('default') assert NetClass.parse(nc.to_string()) == nc def test_with_net(self): nc = NetClass('default') nc.nets.append('GND') assert NetClass.parse(nc.to_string()) == nc def test_with_multiple_nets(self): nc = NetClass('default') nc.nets += ['GND', 'VO'] nc_str = nc.to_string() assert NetClass.parse(nc.to_string()) == nc class ZoneTests(unittest.TestCase): def test_parse(self): zone_string = '(zone (polygon (pts (xy 0 0) (xy 1 1))))' zone = Zone.parse(zone_string) assert zone.polygon[0] == (0, 0) assert zone.polygon[1] == (1, 1) assert Zone.parse(zone.to_string()) == zone class PcbTests(unittest.TestCase): def test_minimal_pcb(self): pcb_string = open('tests/minimal_pcb.kicad_pcb', 'r').read() pcb = Pcb.parse(pcb_string) assert pcb.version == 123 assert pcb.host == ['pcbnew', 'version'] assert len(pcb.nets) == 4 assert len(pcb.modules) == 2 assert Pcb.parse(pcb.to_string()) == pcb def test_page(self): pcb = Pcb() assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = 'A4' assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = [200, 200] assert Pcb.parse(pcb.to_string()) == pcb def test_single_module(self): pcb = Pcb() pcb.modules.append(Module(name='R')) assert Pcb.parse(pcb.to_string()) == pcb def test_comment(self): pcb = Pcb(comment1='hello world', comment2='bye world') assert Pcb.parse(pcb.to_string()) == pcb def test_num_nets(self): pcb = Pcb(num_nets=5) assert Pcb.parse(pcb.to_string()) == pcb pcb.nets.append(Net()) assert Pcb.parse(pcb.to_string()) == pcb def test_layers(self): pcb = Pcb() pcb.layers.append(Layer('B.Cu')) assert Pcb.parse(pcb.to_string()) == pcb pcb.layers.append(Layer('F.Cu')) assert Pcb.parse(pcb.to_string()) == pcb ", https://github.com/dvc94ch/pykicad,cdebcaeb4ab6c8903ebecfd0748f826ea406923f,tests/test_pcb.py::ZoneTests::test_parse,tests/test_pcb.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from pytest import * from pykicad.pcb import * class NetClassTests(unittest.TestCase): def test_parse(self): nc_string = ""(net_class name description (add_net GND))"" nc = NetClass.parse(nc_string) assert nc.name == 'name' assert nc.description == 'description' assert nc.nets == ['GND'] def test_without_net(self): nc = NetClass('default') assert NetClass.parse(nc.to_string()) == nc def test_with_net(self): nc = NetClass('default') nc.nets.append('GND') assert NetClass.parse(nc.to_string()) == nc def test_with_multiple_nets(self): nc = NetClass('default') nc.nets += ['GND', 'VO'] nc_str = nc.to_string() assert NetClass.parse(nc.to_string()) == nc class ZoneTests(unittest.TestCase): def test_parse(self): zone_string = '(zone (polygon (pts (xy 0 0) (xy 1 1))))' zone = Zone.parse(zone_string) assert zone.polygon[0] == (0, 0) assert zone.polygon[1] == (1, 1) assert Zone.parse(zone.to_string()) == zone class PcbTests(unittest.TestCase): def test_minimal_pcb(self): pcb_string = open('tests/minimal_pcb.kicad_pcb', 'r').read() pcb = Pcb.parse(pcb_string) assert pcb.version == 123 assert pcb.host == ['pcbnew', 'version'] assert len(pcb.nets) == 4 assert len(pcb.modules) == 2 assert Pcb.parse(pcb.to_string()) == pcb def test_page(self): pcb = Pcb() assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = 'A4' assert Pcb.parse(pcb.to_string()) == pcb pcb.page_type = [200, 200] assert Pcb.parse(pcb.to_string()) == pcb def test_single_module(self): pcb = Pcb() pcb.modules.append(Module(name='R')) assert Pcb.parse(pcb.to_string()) == pcb def test_comment(self): pcb = Pcb(comment1='hello world', comment2='bye world') assert Pcb.parse(pcb.to_string()) == pcb def test_num_nets(self): pcb = Pcb(num_nets=5) assert Pcb.parse(pcb.to_string()) == pcb pcb.nets.append(Net()) assert Pcb.parse(pcb.to_string()) == pcb def test_layers(self): pcb = Pcb() pcb.layers.append(Layer('B.Cu')) assert Pcb.parse(pcb.to_string()) == pcb pcb.layers.append(Layer('F.Cu')) assert Pcb.parse(pcb.to_string()) == pcb ", https://github.com/earthreader/libearth,71d2f3246eb0b6a3668a64e2242616d20b1d7dcd,tests/stage_test.py::test_get_dir_route,tests/stage_test.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import collections import io import logging import threading from pytest import fixture, raises from libearth.compat import IRON_PYTHON, binary_type from libearth.repository import (FileSystemRepository, Repository, RepositoryKeyError) from libearth.schema import read from libearth.session import MergeableDocumentElement, Session from libearth.stage import (BaseStage, Directory, DirtyBuffer, Route, TransactionError, compile_format_to_pattern) from libearth.tz import now def test_compile_format_to_pattern(): p = compile_format_to_pattern('{0}') p_msg = 'p.pattern = ' + repr(p.pattern) assert p.match('anything'), p_msg assert p.match('something'), p_msg assert p.match('no-match'), p_msg assert p.match('somehow'), p_msg p2 = compile_format_to_pattern('{0}thing') p2_msg = 'p2.pattern = ' + repr(p2.pattern) assert p2.match('anything'), p2_msg assert p2.match('something'), p2_msg assert not p2.match('no-match'), p2_msg assert not p2.match('somehow'), p2_msg p3 = compile_format_to_pattern('some{0}') p3_msg = 'p3.pattern = ' + repr(p3.pattern) assert not p3.match('anything'), p3_msg assert p3.match('something'), p3_msg assert not p3.match('no-match'), p3_msg assert p3.match('somehow'), p3_msg p4 = compile_format_to_pattern('pre{0}post') p4_msg = 'p4.pattern = ' + repr(p4.pattern) assert not p4.match('something'), p4_msg assert not p4.match('no-match'), p4_msg assert not p4.match('preonly'), p4_msg assert not p4.match('onlypost'), p4_msg assert p4.match('preandpost'), p4_msg p5 = compile_format_to_pattern('pre{0}in{1}post') p5_msg = 'p5.pattern = ' + repr(p5.pattern) assert not p5.match('something'), p5_msg assert not p5.match('no-match'), p5_msg assert not p5.match('preonly'), p5_msg assert not p5.match('onlypost'), p5_msg assert not p5.match('preandpost'), p5_msg assert not p5.match('inandpost'), p5_msg assert not p5.match('preandin'), p5_msg assert p5.match('preandinandpost'), p5_msg p6 = compile_format_to_pattern('pre{0}and{{1}}post') p6_msg = 'p6.pattern = ' + repr(p6.pattern) assert not p6.match('something'), p6_msg assert not p6.match('no-match'), p6_msg assert not p6.match('preonly'), p6_msg assert not p6.match('onlypost'), p6_msg assert not p6.match('preandpost'), p6_msg assert not p6.match('inandpost'), p6_msg assert not p6.match('preandin'), p6_msg assert not p6.match('preandinandpost'), p6_msg assert p6.match('pre,and{1}post'), p6_msg p7 = compile_format_to_pattern('pre{0}in{session.identifier}post') p7_msg = 'p7.pattern = ' + repr(p7.pattern) assert not p7.match('something'), p7_msg assert not p7.match('no-match'), p7_msg assert not p7.match('preonly'), p7_msg assert not p7.match('onlypost'), p7_msg assert not p7.match('preandpost'), p7_msg assert not p7.match('inandpost'), p7_msg assert not p7.match('preandin'), p7_msg assert p7.match('preandinandpost'), p7_msg class TestDoc(MergeableDocumentElement): __tag__ = 'test' class TestStage(BaseStage): doc = Route(TestDoc, ['doc.{session.identifier}.xml']) dir_docs = Route( TestDoc, ['dir', '{0}', '{session.identifier}.xml'] ) deep_docs = Route( TestDoc, ['dir2', 'pre{0}', '{1}post', '{session.identifier}.xml'] ) class MemoryRepository(Repository): def __init__(self): self.data = {} def read(self, key): super(MemoryRepository, self).read(key) logger = logging.getLogger(__name__ + '.MemoeryRepository.read') data = self.data for k in key: try: data = data[k] except KeyError as e: logger.debug(e, exc_info=1) raise RepositoryKeyError(key) if isinstance(data, collections.Mapping): logger.debug('RepositoryKeyError(%r)', key) raise RepositoryKeyError(key) logger.debug('%r: %r', key, data) return data, def write(self, key, iterable): super(MemoryRepository, self).write(key, iterable) logger = logging.getLogger(__name__ + '.MemoeryRepository.write') data = self.data for k in key[:-1]: data = data.setdefault(k, {}) buffer_ = io.BytesIO() for chunk in iterable: assert isinstance(chunk, binary_type), 'chunk = ' + repr(chunk) buffer_.write(chunk) logger.debug('%r: %r', key, buffer_.getvalue()) data[key[-1]] = buffer_.getvalue() def exists(self, key): super(MemoryRepository, self).exists(key) logger = logging.getLogger(__name__ + '.MemoeryRepository.exists') data = self.data for k in key: try: data = data[k] except KeyError: logger.debug('%r does not exist', key, exc_info=1) return False logger.debug('%r exists', key, exc_info=1) return True def list(self, key): super(MemoryRepository, self).list(key) logger = logging.getLogger(__name__ + '.MemoeryRepository.list') data = self.data for k in key: try: data = data[k] except KeyError as e: logger.debug(e, exc_info=1) raise RepositoryKeyError(key) if isinstance(data, collections.Mapping): logger.debug('list(%r): %r', key, sorted(frozenset(data))) return frozenset(data) logger.debug('RepositoryKeyError(%r)', key) raise RepositoryKeyError(key) class TestRepository(MemoryRepository): DATA = { 'doc.SESSID.xml': b'', 'dir': { 'abc': {'SESSID.xml': b''}, 'def': {'SESSID.xml': b''} }, 'dir2': { 'preabc': { 'xyzpost': {'SESSID.xml': b''}, 'xxxpost': {'SESSID.xml': b''}, 'invalid': {} }, 'predef': { 'xyzpost': {'SESSID.xml': b''}, 'xxxpost': {'SESSID.xml': b''}, 'invalid': {} }, 'invalid': {} } } def __init__(self): self.data = dict(self.DATA) @fixture def fx_repo(): return TestRepository() @fixture def fx_session(): return Session(identifier='SESSID') @fixture def fx_stage(fx_repo, fx_session): return TestStage(fx_session, fx_repo) @fixture def fx_other_session(): return Session(identifier='SESSID2') @fixture def fx_other_stage(fx_repo, fx_other_session): return TestStage(fx_other_session, fx_repo) def test_stage_sessions(fx_session, fx_stage, fx_other_session, fx_other_stage): # It's empty at its initial state assert fx_stage.sessions == frozenset() assert fx_other_stage.sessions == frozenset() # They get touched when there's any transaction with fx_stage: pass assert fx_stage.sessions == frozenset([fx_session]) assert fx_other_stage.sessions == frozenset([fx_session]) with fx_other_stage: pass assert fx_stage.sessions == frozenset([fx_session, fx_other_session]) assert fx_other_stage.sessions == frozenset([fx_session, fx_other_session]) def test_stage_read(fx_session, fx_stage): with fx_stage: doc = fx_stage.read(TestDoc, ['doc.{0}.xml'.format(fx_session.identifier)]) assert isinstance(doc, TestDoc) assert doc.__revision__.session is fx_session def test_stage_write(fx_repo, fx_session, fx_stage): doc = TestDoc() min_ts = now() with fx_stage: wdoc = fx_stage.write(['doc.{0}.xml'.format(fx_session.identifier)], doc) assert wdoc.__revision__.session is fx_session assert min_ts <= wdoc.__revision__.updated_at <= now() xml = fx_repo.data['doc.{0}.xml'.format(fx_session.identifier)] read_doc = read(TestDoc, [xml]) assert isinstance(read_doc, TestDoc) assert read_doc.__revision__ == wdoc.__revision__ def test_get_flat_route(fx_session, fx_stage): with fx_stage: doc = fx_stage.doc assert isinstance(doc, TestDoc) assert doc.__revision__.session is fx_session with fx_stage: assert fx_stage.doc.__revision__ == doc.__revision__ def test_set_flat_route(fx_session, fx_stage, fx_other_session, fx_other_stage): with fx_stage: fx_stage.doc = TestDoc() doc_a = fx_stage.doc assert doc_a.__revision__.session is fx_session with fx_other_stage: doc_b = fx_other_stage.doc assert doc_b.__revision__.session is fx_other_session fx_session.revise(doc_a) assert (doc_b.__revision__.updated_at == fx_other_stage.doc.__revision__.updated_at) assert (fx_other_stage.doc.__revision__.updated_at <= doc_a.__revision__.updated_at) with fx_stage: fx_stage.doc = doc_a with fx_other_stage: assert (fx_other_stage.doc.__revision__.updated_at >= doc_a.__revision__.updated_at) def test_get_dir_route(fx_session, fx_stage): with fx_stage: dir = fx_stage.dir_docs assert isinstance(dir, Directory) assert len(dir) == 2 assert frozenset(dir) == frozenset(['abc', 'def']) with raises(KeyError): dir['not-exist'] doc = dir['abc'] assert isinstance(doc, TestDoc) assert doc.__revision__.session is fx_session assert dir['abc'].__revision__ == doc.__revision__ def test_set_dir_route(fx_session, fx_stage, fx_other_session, fx_other_stage): with fx_stage: doc_a = fx_stage.dir_docs['abc'] assert doc_a.__revision__.session is fx_session with fx_other_stage: doc_b = fx_other_stage.dir_docs['abc'] ", https://github.com/earthreader/libearth,71d2f3246eb0b6a3668a64e2242616d20b1d7dcd,tests/stage_test.py::test_set_dir_route,tests/stage_test.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"import collections import io import logging import threading from pytest import fixture, raises from libearth.compat import IRON_PYTHON, binary_type from libearth.repository import (FileSystemRepository, Repository, RepositoryKeyError) from libearth.schema import read from libearth.session import MergeableDocumentElement, Session from libearth.stage import (BaseStage, Directory, DirtyBuffer, Route, TransactionError, compile_format_to_pattern) from libearth.tz import now def test_compile_format_to_pattern(): p = compile_format_to_pattern('{0}') p_msg = 'p.pattern = ' + repr(p.pattern) assert p.match('anything'), p_msg assert p.match('something'), p_msg assert p.match('no-match'), p_msg assert p.match('somehow'), p_msg p2 = compile_format_to_pattern('{0}thing') p2_msg = 'p2.pattern = ' + repr(p2.pattern) assert p2.match('anything'), p2_msg assert p2.match('something'), p2_msg assert not p2.match('no-match'), p2_msg assert not p2.match('somehow'), p2_msg p3 = compile_format_to_pattern('some{0}') p3_msg = 'p3.pattern = ' + repr(p3.pattern) assert not p3.match('anything'), p3_msg assert p3.match('something'), p3_msg assert not p3.match('no-match'), p3_msg assert p3.match('somehow'), p3_msg p4 = compile_format_to_pattern('pre{0}post') p4_msg = 'p4.pattern = ' + repr(p4.pattern) assert not p4.match('something'), p4_msg assert not p4.match('no-match'), p4_msg assert not p4.match('preonly'), p4_msg assert not p4.match('onlypost'), p4_msg assert p4.match('preandpost'), p4_msg p5 = compile_format_to_pattern('pre{0}in{1}post') p5_msg = 'p5.pattern = ' + repr(p5.pattern) assert not p5.match('something'), p5_msg assert not p5.match('no-match'), p5_msg assert not p5.match('preonly'), p5_msg assert not p5.match('onlypost'), p5_msg assert not p5.match('preandpost'), p5_msg assert not p5.match('inandpost'), p5_msg assert not p5.match('preandin'), p5_msg assert p5.match('preandinandpost'), p5_msg p6 = compile_format_to_pattern('pre{0}and{{1}}post') p6_msg = 'p6.pattern = ' + repr(p6.pattern) assert not p6.match('something'), p6_msg assert not p6.match('no-match'), p6_msg assert not p6.match('preonly'), p6_msg assert not p6.match('onlypost'), p6_msg assert not p6.match('preandpost'), p6_msg assert not p6.match('inandpost'), p6_msg assert not p6.match('preandin'), p6_msg assert not p6.match('preandinandpost'), p6_msg assert p6.match('pre,and{1}post'), p6_msg p7 = compile_format_to_pattern('pre{0}in{session.identifier}post') p7_msg = 'p7.pattern = ' + repr(p7.pattern) assert not p7.match('something'), p7_msg assert not p7.match('no-match'), p7_msg assert not p7.match('preonly'), p7_msg assert not p7.match('onlypost'), p7_msg assert not p7.match('preandpost'), p7_msg assert not p7.match('inandpost'), p7_msg assert not p7.match('preandin'), p7_msg assert p7.match('preandinandpost'), p7_msg class TestDoc(MergeableDocumentElement): __tag__ = 'test' class TestStage(BaseStage): doc = Route(TestDoc, ['doc.{session.identifier}.xml']) dir_docs = Route( TestDoc, ['dir', '{0}', '{session.identifier}.xml'] ) deep_docs = Route( TestDoc, ['dir2', 'pre{0}', '{1}post', '{session.identifier}.xml'] ) class MemoryRepository(Repository): def __init__(self): self.data = {} def read(self, key): super(MemoryRepository, self).read(key) logger = logging.getLogger(__name__ + '.MemoeryRepository.read') data = self.data for k in key: try: data = data[k] except KeyError as e: logger.debug(e, exc_info=1) raise RepositoryKeyError(key) if isinstance(data, collections.Mapping): logger.debug('RepositoryKeyError(%r)', key) raise RepositoryKeyError(key) logger.debug('%r: %r', key, data) return data, def write(self, key, iterable): super(MemoryRepository, self).write(key, iterable) logger = logging.getLogger(__name__ + '.MemoeryRepository.write') data = self.data for k in key[:-1]: data = data.setdefault(k, {}) buffer_ = io.BytesIO() for chunk in iterable: assert isinstance(chunk, binary_type), 'chunk = ' + repr(chunk) buffer_.write(chunk) logger.debug('%r: %r', key, buffer_.getvalue()) data[key[-1]] = buffer_.getvalue() def exists(self, key): super(MemoryRepository, self).exists(key) logger = logging.getLogger(__name__ + '.MemoeryRepository.exists') data = self.data for k in key: try: data = data[k] except KeyError: logger.debug('%r does not exist', key, exc_info=1) return False logger.debug('%r exists', key, exc_info=1) return True def list(self, key): super(MemoryRepository, self).list(key) logger = logging.getLogger(__name__ + '.MemoeryRepository.list') data = self.data for k in key: try: data = data[k] except KeyError as e: logger.debug(e, exc_info=1) raise RepositoryKeyError(key) if isinstance(data, collections.Mapping): logger.debug('list(%r): %r', key, sorted(frozenset(data))) return frozenset(data) logger.debug('RepositoryKeyError(%r)', key) raise RepositoryKeyError(key) class TestRepository(MemoryRepository): DATA = { 'doc.SESSID.xml': b'', 'dir': { 'abc': {'SESSID.xml': b''}, 'def': {'SESSID.xml': b''} }, 'dir2': { 'preabc': { 'xyzpost': {'SESSID.xml': b''}, 'xxxpost': {'SESSID.xml': b''}, 'invalid': {} }, 'predef': { 'xyzpost': {'SESSID.xml': b''}, 'xxxpost': {'SESSID.xml': b''}, 'invalid': {} }, 'invalid': {} } } def __init__(self): self.data = dict(self.DATA) @fixture def fx_repo(): return TestRepository() @fixture def fx_session(): return Session(identifier='SESSID') @fixture def fx_stage(fx_repo, fx_session): return TestStage(fx_session, fx_repo) @fixture def fx_other_session(): return Session(identifier='SESSID2') @fixture def fx_other_stage(fx_repo, fx_other_session): return TestStage(fx_other_session, fx_repo) def test_stage_sessions(fx_session, fx_stage, fx_other_session, fx_other_stage): # It's empty at its initial state assert fx_stage.sessions == frozenset() assert fx_other_stage.sessions == frozenset() # They get touched when there's any transaction with fx_stage: pass assert fx_stage.sessions == frozenset([fx_session]) assert fx_other_stage.sessions == frozenset([fx_session]) with fx_other_stage: pass assert fx_stage.sessions == frozenset([fx_session, fx_other_session]) assert fx_other_stage.sessions == frozenset([fx_session, fx_other_session]) def test_stage_read(fx_session, fx_stage): with fx_stage: doc = fx_stage.read(TestDoc, ['doc.{0}.xml'.format(fx_session.identifier)]) assert isinstance(doc, TestDoc) assert doc.__revision__.session is fx_session def test_stage_write(fx_repo, fx_session, fx_stage): doc = TestDoc() min_ts = now() with fx_stage: wdoc = fx_stage.write(['doc.{0}.xml'.format(fx_session.identifier)], doc) assert wdoc.__revision__.session is fx_session assert min_ts <= wdoc.__revision__.updated_at <= now() xml = fx_repo.data['doc.{0}.xml'.format(fx_session.identifier)] read_doc = read(TestDoc, [xml]) assert isinstance(read_doc, TestDoc) assert read_doc.__revision__ == wdoc.__revision__ def test_get_flat_route(fx_session, fx_stage): with fx_stage: doc = fx_stage.doc assert isinstance(doc, TestDoc) assert doc.__revision__.session is fx_session with fx_stage: assert fx_stage.doc.__revision__ == doc.__revision__ def test_set_flat_route(fx_session, fx_stage, fx_other_session, fx_other_stage): with fx_stage: fx_stage.doc = TestDoc() doc_a = fx_stage.doc assert doc_a.__revision__.session is fx_session with fx_other_stage: doc_b = fx_other_stage.doc assert doc_b.__revision__.session is fx_other_session fx_session.revise(doc_a) assert (doc_b.__revision__.updated_at == fx_other_stage.doc.__revision__.updated_at) assert (fx_other_stage.doc.__revision__.updated_at <= doc_a.__revision__.updated_at) with fx_stage: fx_stage.doc = doc_a with fx_other_stage: assert (fx_other_stage.doc.__revision__.updated_at >= doc_a.__revision__.updated_at) def test_get_dir_route(fx_session, fx_stage): with fx_stage: dir = fx_stage.dir_docs assert isinstance(dir, Directory) assert len(dir) == 2 assert frozenset(dir) == frozenset(['abc', 'def']) with raises(KeyError): dir['not-exist'] doc = dir['abc'] assert isinstance(doc, TestDoc) assert doc.__revision__.session is fx_session assert dir['abc'].__revision__ == doc.__revision__ def test_set_dir_route(fx_session, fx_stage, fx_other_session, fx_other_stage): with fx_stage: doc_a = fx_stage.dir_docs['abc'] assert doc_a.__revision__.session is fx_session with fx_other_stage: doc_b = fx_other_stage.dir_docs['abc'] ", https://github.com/econ-ark/HARK,f18dc69520db9e15d1d355e604ff712d38ce3d2b,HARK/ConsumptionSaving/tests/test_ConsAggShockModel.py::KrusellSmithAgentTestCase::test_agent,HARK/ConsumptionSaving/tests/test_ConsAggShockModel.py,OD-Vic,flaky,nan,nan,classify;root_cause,"from HARK import distributeParams from HARK.ConsumptionSaving.ConsAggShockModel import ( AggShockConsumerType, CobbDouglasEconomy, AggShockMarkovConsumerType, CobbDouglasMarkovEconomy, KrusellSmithType, KrusellSmithEconomy, ) from HARK.distribution import Uniform import numpy as np import unittest class testAggShockConsumerType(unittest.TestCase): def setUp(self): agent = AggShockConsumerType() agent.AgentCount = 900 # Very low number of agents for the sake of speed agent.cycles = 0 # Make agents heterogeneous in their discount factor self.agents = distributeParams( agent, ""DiscFac"", 3, Uniform(bot=0.90, top=0.94) # Impatient agents ) # Make an economy with those agents living in it self.economy = CobbDouglasEconomy(agents=self.agents) def test_distributeParams(self): self.assertEqual(self.agents[1].AgentCount, 300) def test_agent(self): # Have one consumer type inherit relevant objects from the economy, # then solve their microeconomic problem self.agents[0].getEconomyData(self.economy) self.agents[0].solve() self.assertAlmostEqual( self.agents[0].solution[0].cFunc(10.0, self.economy.MSS), 3.229078148576943 ) def test_macro(self): self.economy.act_T = 400 # Short simulation history self.economy.max_loops = 3 # Give up quickly for the sake of time self.economy.makeAggShkHist() # Simulate a history of aggregate shocks self.economy.verbose = False # Turn off printed messages # Give data about the economy to all the agents in it for this_type in self.economy.agents: this_type.getEconomyData(self.economy) self.economy.solve() # Solve for the general equilibrium of the economy self.economy.AFunc = self.economy.dynamics.AFunc self.assertAlmostEqual(self.economy.AFunc.slope, 1.116259456228145) self.assertAlmostEqual(self.economy.history[""MaggNow""][10], 7.456324335623432) class testAggShockMarkovConsumerType(unittest.TestCase): def setUp(self): # Make one agent type and an economy for it to live in self.agent = AggShockMarkovConsumerType() self.agent.cycles = 0 self.agent.AgentCount = 1000 # Low number of simulated agents self.agent.IncomeDstn[0] = 2 * [self.agent.IncomeDstn[0]] ## see #557 self.economy = CobbDouglasMarkovEconomy(agents=[self.agent]) def test_agent(self): # Have one consumer type inherit relevant objects from the economy, # then solve their microeconomic problem self.agent.getEconomyData(self.economy) self.agent.solve() self.assertAlmostEqual( self.agent.solution[0].cFunc[0](10.0, self.economy.MSS), 2.5635896520991377 ) def test_economy(self): # Adjust the economy so that it (fake) solves quickly self.economy.act_T = 500 # Short simulation history self.economy.max_loops = 3 # Just quiet solving early self.economy.verbose = False # Turn off printed messages self.agent.getEconomyData(self.economy) self.economy.makeAggShkHist() # Make a simulated history of aggregate shocks self.economy.solve() # Solve for the general equilibrium of the economy self.economy.AFunc = self.economy.dynamics.AFunc self.assertAlmostEqual(self.economy.AFunc[0].slope, 1.0904698841958917) self.assertAlmostEqual(self.economy.history[""AaggNow""][5], 9.467758924955897) class KrusellSmithTestCase(unittest.TestCase): def setUp(self): # Make one agent type and an economy for it to live in self.agent = KrusellSmithType() self.agent.cycles = 0 self.agent.AgentCount = 1000 # Low number of simulated agents self.economy = KrusellSmithEconomy(agents=[self.agent]) self.economy.max_loops = 2 # Quit early self.economy.act_T = 1100 # Shorter simulated period self.economy.discard_periods = 100 self.economy.verbose = False # Turn off printed messages class KrusellSmithAgentTestCase(KrusellSmithTestCase): def test_agent(self): self.agent.getEconomyData(self.economy) self.agent.solve() self.assertAlmostEqual( self.agent.solution[0].cFunc[0](10.0, self.economy.MSS), 1.23867751 ) class KrusellSmithMethodsTestCase(KrusellSmithTestCase): def test_methods(self): self.agent.getEconomyData(self.economy) self.assertAlmostEqual( self.agent.AFunc[0].slope, 1.0 ) self.assertAlmostEqual( self.agent.AFunc[1].slope, 1.0 ) self.agent.reset() self.economy.reset() self.agent.track_vars += ['EmpNow'] # self.economy.track_vars += ['EmpNow'] self.assertEqual( np.sum(self.agent.EmpNow & self.agent.EmpNow[-1]), 900 ) self.assertEqual( np.sum(self.agent.EmpNow[:-1] & self.agent.EmpNow[1:]), 816 ) self.economy.makeMrkvHist() # Make a simulated history of aggregate shocks self.assertAlmostEqual( np.mean(self.economy.MrkvNow_hist), 0.4818181818181818 ) # object attributes that are conditions # for preComputeArrays self.assertEqual( self.agent.aGrid.size, 32 ) self.assertAlmostEqual( self.agent.aGrid[5], 0.3426040963137289 ) self.economy.solveAgents() # testing preComputeArrays() self.assertAlmostEqual( self.agent.mNextArray[5,2,3,0], 0.34879574548563563 ) # testing makeGrid() self.assertAlmostEqual( self.agent.aGrid[1], 0.05531643953496124 ) self.assertEqual( self.economy.MSS, 13.327225348792547 ) # testing updateSolutionTerminal() self.assertEqual( self.agent.solution_terminal.cFunc[0](10,self.economy.MSS), 10 ) self.assertAlmostEqual( self.economy.agents[0].solution[0].cFunc[0]( 10,self.economy.MSS ).tolist(), 1.2386775112633517 ) self.assertAlmostEqual( self.agent.AFunc[1].slope, 1.0 ) self.economy.makeHistory() emp_totals = np.sum(self.agent.history['EmpNow'], axis = 0) self.assertEqual(emp_totals[0], 1011) self.assertEqual(emp_totals[2], 1009) self.assertEqual(emp_totals[9], 1042) class KrusellSmithEconomyTestCase(KrusellSmithTestCase): def test_economy(self): self.assertAlmostEqual( self.economy.AFunc[0].slope, 1.0 ) self.assertAlmostEqual( self.economy.AFunc[1].slope, 1.0 ) self.agent.getEconomyData(self.economy) self.economy.makeMrkvHist() # Make a simulated history of aggregate shocks self.economy.solve() # Solve for the general equilibrium of the economy self.economy.AFunc = self.economy.dynamics.AFunc self.assertAlmostEqual(self.economy.AFunc[0].slope, 1.0014463644834282) self.assertAlmostEqual(self.economy.history[""Aprev""][4], 11.009107526443584) self.assertAlmostEqual(self.economy.history[""MrkvNow""][40], 1) self.assertAlmostEqual(self.economy.history[""Urate""][12], 0.040000000000000036) ", https://github.com/econ-ark/HARK,c9138af45fd0a0d97c718c8558f3ea8d9f2ae820,HARK/ConsumptionSaving/tests/test_ConsAggShockModel.py::testAggShockMarkovConsumerType::test_agent,HARK/ConsumptionSaving/tests/test_ConsAggShockModel.py,OD-Vic,flaky,nan,nan,classify;root_cause,"from HARK import distributeParams from HARK.ConsumptionSaving.ConsAggShockModel import AggShockConsumerType, CobbDouglasEconomy, \ AggShockMarkovConsumerType, CobbDouglasMarkovEconomy, KrusellSmithType, KrusellSmithEconomy from HARK.distribution import Uniform import numpy as np import unittest class testAggShockConsumerType(unittest.TestCase): def setUp(self): agent = AggShockConsumerType() agent.AgentCount = 900 # Very low number of agents for the sake of speed agent.cycles = 0 # Make agents heterogeneous in their discount factor self.agents = distributeParams(agent, 'DiscFac', 3, Uniform(bot=.90, top=.94) # Impatient agents ) # Make an economy with those agents living in it self.economy = CobbDouglasEconomy(agents=self.agents) def test_distributeParams(self): self.assertEqual(self.agents[1].AgentCount, 300) def test_agent(self): # Have one consumer type inherit relevant objects from the economy, # then solve their microeconomic problem self.agents[0].getEconomyData(self.economy) self.agents[0].solve() self.assertAlmostEqual(self.agents[0].solution[0].cFunc(10., self.economy.MSS), 3.229078148576943) def test_macro(self): self.economy.act_T = 400 # Short simulation history self.economy.max_loops = 3 # Give up quickly for the sake of time self.economy.makeAggShkHist() # Simulate a history of aggregate shocks self.economy.verbose = False # Turn off printed messages # Give data about the economy to all the agents in it for this_type in self.economy.agents: this_type.getEconomyData(self.economy) self.economy.solve() # Solve for the general equilibrium of the economy self.economy.AFunc = self.economy.dynamics.AFunc self.assertAlmostEqual(self.economy.AFunc.slope, 1.116259456228145) self.assertAlmostEqual(self.economy.history['MaggNow'][10], 7.456324335623432) class testAggShockMarkovConsumerType(unittest.TestCase): def setUp(self): # Make one agent type and an economy for it to live in self.agent = AggShockMarkovConsumerType() self.agent.cycles = 0 self.agent.AgentCount = 1000 # Low number of simulated agents self.agent.IncomeDstn[0] = 2*[self.agent.IncomeDstn[0]] ## see #557 self.economy = CobbDouglasMarkovEconomy( agents = [self.agent]) def test_agent(self): # Have one consumer type inherit relevant objects from the economy, # then solve their microeconomic problem self.agent.getEconomyData(self.economy) self.agent.solve() self.assertAlmostEqual(self.agent.solution[0].cFunc[0](10., self.economy.MSS), 2.5635896520991377) def test_economy(self): # Adjust the economy so that it (fake) solves quickly self.economy.act_T = 500 # Short simulation history self.economy.max_loops = 3 # Just quiet solving early self.economy.verbose = False # Turn off printed messages self.agent.getEconomyData(self.economy) self.economy.makeAggShkHist() # Make a simulated history of aggregate shocks self.economy.solve() # Solve for the general equilibrium of the economy self.economy.AFunc = self.economy.dynamics.AFunc self.assertAlmostEqual(self.economy.AFunc[0].slope, 1.0904698841958917) self.assertAlmostEqual(self.economy.history['AaggNow'][5], 9.467758924955897) class testKrusellSmith(unittest.TestCase): def setUp(self): # Make one agent type and an economy for it to live in self.agent = KrusellSmithType() self.agent.cycles = 0 self.agent.AgentCount = 1000 # Low number of simulated agents self.economy = KrusellSmithEconomy(agents = [self.agent]) self.economy.max_loops = 2 # Quit early self.economy.act_T = 1100 # Shorter simulated period self.economy.discard_periods = 100 self.economy.verbose = False # Turn off printed messages def test_agent(self): self.agent.getEconomyData(self.economy) self.agent.solve() self.assertAlmostEqual(self.agent.solution[0].cFunc[0](10., self.economy.MSS), 1.23867751) def test_economy(self): self.agent.getEconomyData(self.economy) self.economy.makeMrkvHist() # Make a simulated history of aggregate shocks self.economy.solve() # Solve for the general equilibrium of the economy self.economy.AFunc = self.economy.dynamics.AFunc self.assertAlmostEqual(self.economy.AFunc[0].slope, 1.0014463644834282) self.assertAlmostEqual(self.economy.history['Aprev'][4], 11.009107526443584) self.assertAlmostEqual(self.economy.history['MrkvNow'][40], 1 ) self.assertAlmostEqual(self.economy.history['Urate'][12], 0.040000000000000036) ", https://github.com/econ-ark/HARK,c9138af45fd0a0d97c718c8558f3ea8d9f2ae820,HARK/ConsumptionSaving/tests/test_ConsAggShockModel.py::testKrusellSmith::test_agent,HARK/ConsumptionSaving/tests/test_ConsAggShockModel.py,OD-Vic,flaky,MovedOrRenamed,nan,classify;root_cause,"from HARK import distributeParams from HARK.ConsumptionSaving.ConsAggShockModel import AggShockConsumerType, CobbDouglasEconomy, \ AggShockMarkovConsumerType, CobbDouglasMarkovEconomy, KrusellSmithType, KrusellSmithEconomy from HARK.distribution import Uniform import numpy as np import unittest class testAggShockConsumerType(unittest.TestCase): def setUp(self): agent = AggShockConsumerType() agent.AgentCount = 900 # Very low number of agents for the sake of speed agent.cycles = 0 # Make agents heterogeneous in their discount factor self.agents = distributeParams(agent, 'DiscFac', 3, Uniform(bot=.90, top=.94) # Impatient agents ) # Make an economy with those agents living in it self.economy = CobbDouglasEconomy(agents=self.agents) def test_distributeParams(self): self.assertEqual(self.agents[1].AgentCount, 300) def test_agent(self): # Have one consumer type inherit relevant objects from the economy, # then solve their microeconomic problem self.agents[0].getEconomyData(self.economy) self.agents[0].solve() self.assertAlmostEqual(self.agents[0].solution[0].cFunc(10., self.economy.MSS), 3.229078148576943) def test_macro(self): self.economy.act_T = 400 # Short simulation history self.economy.max_loops = 3 # Give up quickly for the sake of time self.economy.makeAggShkHist() # Simulate a history of aggregate shocks self.economy.verbose = False # Turn off printed messages # Give data about the economy to all the agents in it for this_type in self.economy.agents: this_type.getEconomyData(self.economy) self.economy.solve() # Solve for the general equilibrium of the economy self.economy.AFunc = self.economy.dynamics.AFunc self.assertAlmostEqual(self.economy.AFunc.slope, 1.116259456228145) self.assertAlmostEqual(self.economy.history['MaggNow'][10], 7.456324335623432) class testAggShockMarkovConsumerType(unittest.TestCase): def setUp(self): # Make one agent type and an economy for it to live in self.agent = AggShockMarkovConsumerType() self.agent.cycles = 0 self.agent.AgentCount = 1000 # Low number of simulated agents self.agent.IncomeDstn[0] = 2*[self.agent.IncomeDstn[0]] ## see #557 self.economy = CobbDouglasMarkovEconomy( agents = [self.agent]) def test_agent(self): # Have one consumer type inherit relevant objects from the economy, # then solve their microeconomic problem self.agent.getEconomyData(self.economy) self.agent.solve() self.assertAlmostEqual(self.agent.solution[0].cFunc[0](10., self.economy.MSS), 2.5635896520991377) def test_economy(self): # Adjust the economy so that it (fake) solves quickly self.economy.act_T = 500 # Short simulation history self.economy.max_loops = 3 # Just quiet solving early self.economy.verbose = False # Turn off printed messages self.agent.getEconomyData(self.economy) self.economy.makeAggShkHist() # Make a simulated history of aggregate shocks self.economy.solve() # Solve for the general equilibrium of the economy self.economy.AFunc = self.economy.dynamics.AFunc self.assertAlmostEqual(self.economy.AFunc[0].slope, 1.0904698841958917) self.assertAlmostEqual(self.economy.history['AaggNow'][5], 9.467758924955897) class testKrusellSmith(unittest.TestCase): def setUp(self): # Make one agent type and an economy for it to live in self.agent = KrusellSmithType() self.agent.cycles = 0 self.agent.AgentCount = 1000 # Low number of simulated agents self.economy = KrusellSmithEconomy(agents = [self.agent]) self.economy.max_loops = 2 # Quit early self.economy.act_T = 1100 # Shorter simulated period self.economy.discard_periods = 100 self.economy.verbose = False # Turn off printed messages def test_agent(self): self.agent.getEconomyData(self.economy) self.agent.solve() self.assertAlmostEqual(self.agent.solution[0].cFunc[0](10., self.economy.MSS), 1.23867751) def test_economy(self): self.agent.getEconomyData(self.economy) self.economy.makeMrkvHist() # Make a simulated history of aggregate shocks self.economy.solve() # Solve for the general equilibrium of the economy self.economy.AFunc = self.economy.dynamics.AFunc self.assertAlmostEqual(self.economy.AFunc[0].slope, 1.0014463644834282) self.assertAlmostEqual(self.economy.history['Aprev'][4], 11.009107526443584) self.assertAlmostEqual(self.economy.history['MrkvNow'][40], 1 ) self.assertAlmostEqual(self.economy.history['Urate'][12], 0.040000000000000036) ", https://github.com/econ-ark/HARK,c9138af45fd0a0d97c718c8558f3ea8d9f2ae820,HARK/ConsumptionSaving/tests/test_IndShockConsumerType.py::testIndShockConsumerType::test_simulated_values,HARK/ConsumptionSaving/tests/test_IndShockConsumerType.py,OD-Vic,flaky,nan,nan,classify;root_cause,"from HARK.ConsumptionSaving.ConsIndShockModel import \ IndShockConsumerType, ConsIndShockSolverBasic, \ init_lifecycle, init_idiosyncratic_shocks import numpy as np import unittest class testIndShockConsumerType(unittest.TestCase): def setUp(self): self.agent = IndShockConsumerType( AgentCount = 2, T_sim = 10 ) self.agent.solve() def test_getShocks(self): self.agent.initializeSim() self.agent.simBirth(np.array([True,False])) self.agent.simOnePeriod() self.agent.simBirth(np.array([False,True])) self.agent.getShocks() self.assertEqual(self.agent.PermShkNow[0], 1.0427376294215103) self.assertEqual(self.agent.PermShkNow[1], 0.9278094171517413) self.assertEqual(self.agent.TranShkNow[0], 0.881761797501595) def test_ConsIndShockSolverBasic(self): LifecycleExample = IndShockConsumerType( **init_lifecycle) LifecycleExample.cycles = 1 LifecycleExample.solve() # test the solution_terminal self.assertAlmostEqual( LifecycleExample.solution[10].cFunc(2).tolist(), 2) self.assertAlmostEqual(LifecycleExample.solution[9].cFunc(1), 0.97769632) self.assertAlmostEqual(LifecycleExample.solution[8].cFunc(1), 0.96624445) self.assertAlmostEqual(LifecycleExample.solution[7].cFunc(1), 0.95691449) self.assertAlmostEqual( LifecycleExample.solution[0].cFunc(1).tolist(), 0.87362789) self.assertAlmostEqual( LifecycleExample.solution[1].cFunc(1).tolist(), 0.9081621) self.assertAlmostEqual( LifecycleExample.solution[2].cFunc(1).tolist(), 0.9563899) solver = ConsIndShockSolverBasic( LifecycleExample.solution[1], LifecycleExample.IncomeDstn[0], LifecycleExample.LivPrb[0], LifecycleExample.DiscFac, LifecycleExample.CRRA, LifecycleExample.Rfree, LifecycleExample.PermGroFac[0], LifecycleExample.BoroCnstArt, LifecycleExample.aXtraGrid, LifecycleExample.vFuncBool, LifecycleExample.CubicBool) solver.prepareToSolve() self.assertAlmostEqual(solver.DiscFacEff, 0.9503999999999999) self.assertAlmostEqual(solver.PermShkMinNext, 0.850430160026919) self.assertAlmostEqual(solver.cFuncNowCnst(4).tolist(), 4.0) self.assertAlmostEqual(solver.prepareToCalcEndOfPrdvP()[0], -0.2491750859108316) self.assertAlmostEqual(solver.prepareToCalcEndOfPrdvP()[-1], 19.74982491408914) EndOfPrdvP = solver.calcEndOfPrdvP() self.assertAlmostEqual(EndOfPrdvP[0], 6622.251864311334) self.assertAlmostEqual(EndOfPrdvP[-1], 0.026301061207747087) solution = solver.makeBasicSolution(EndOfPrdvP, solver.aNrmNow, solver.makeLinearcFunc) solver.addMPCandHumanWealth(solution) self.assertAlmostEqual(solution.cFunc(4).tolist(), 1.7391265696400773) def test_simulated_values(self): self.agent.initializeSim() self.agent.simulate() self.assertAlmostEqual(self.agent.MPCnow[1], 0.5711503906043797) self.assertAlmostEqual(self.agent.aLvlNow[1], 0.18438326264597635) class testBufferStock(unittest.TestCase): """""" Tests of the results of the BufferStock REMARK. """""" def setUp(self): # Make a dictionary containing all parameters needed to solve the model self.base_params = init_idiosyncratic_shocks # Set the parameters for the baseline results in the paper # using the variable values defined in the cell above self.base_params['PermGroFac'] = [1.03] self.base_params['Rfree'] = 1.04 self.base_params['DiscFac'] = 0.96 self.base_params['CRRA'] = 2.00 self.base_params['UnempPrb'] = 0.005 self.base_params['IncUnemp'] = 0.0 self.base_params['PermShkStd'] = [0.1] self.base_params['TranShkStd'] = [0.1] self.base_params['LivPrb'] = [1.0] self.base_params['CubicBool'] = True self.base_params['T_cycle'] = 1 self.base_params['BoroCnstArt'] = None def test_baseEx(self): baseEx = IndShockConsumerType(**self.base_params) baseEx.cycles = 100 # Make this type have a finite horizon (Set T = 100) baseEx.solve() baseEx.unpack('cFunc') m = np.linspace(0,9.5,1000) c_m = baseEx.cFunc[0](m) c_t1 = baseEx.cFunc[-2](m) c_t5 = baseEx.cFunc[-6](m) c_t10 = baseEx.cFunc[-11](m) self.assertAlmostEqual(c_m[500], 1.4008090582203356) self.assertAlmostEqual(c_t1[500], 2.9227437159255216) self.assertAlmostEqual(c_t5[500], 1.7350607327187664) self.assertAlmostEqual(c_t10[500], 1.4991390649979213) self.assertAlmostEqual(c_t10[600], 1.6101476268581576) self.assertAlmostEqual(c_t10[700], 1.7196531041366991) def test_GICFails(self): GIC_fail_dictionary = dict(self.base_params) GIC_fail_dictionary['Rfree'] = 1.08 GIC_fail_dictionary['PermGroFac'] = [1.00] GICFailExample = IndShockConsumerType( cycles=0, # cycles=0 makes this an infinite horizon consumer **GIC_fail_dictionary) GICFailExample.solve() GICFailExample.unpack('cFunc') m = np.linspace(0,5,1000) c_m = GICFailExample.cFunc[0](m) self.assertAlmostEqual(c_m[500], 0.7772637042393458) self.assertAlmostEqual(c_m[700], 0.8392649061916746) self.assertFalse(GICFailExample.conditions['GIC']) def test_infinite_horizon(self): baseEx_inf = IndShockConsumerType(cycles=0, **self.base_params) baseEx_inf.solve() baseEx_inf.unpack('cFunc') m1 = np.linspace(1,baseEx_inf.solution[0].mNrmSS,50) # m1 defines the plot range on the left of target m value (e.g. m <= target m) c_m1 = baseEx_inf.cFunc[0](m1) self.assertAlmostEqual(c_m1[0], 0.8527887545025995) self.assertAlmostEqual(c_m1[-1], 1.0036279936408656) x1 = np.linspace(0,25,1000) cfunc_m = baseEx_inf.cFunc[0](x1) self.assertAlmostEqual(cfunc_m[500], 1.8902146173138235) self.assertAlmostEqual(cfunc_m[700], 2.1591451850267176) m = np.linspace(0.001,8,1000) # Use the HARK method derivative to get the derivative of cFunc, and the values are just the MPC MPC = baseEx_inf.cFunc[0].derivative(m) self.assertAlmostEqual(MPC[500], 0.08415000641504392) self.assertAlmostEqual(MPC[700], 0.07173144137912524) IdiosyncDict={ # Parameters shared with the perfect foresight model ""CRRA"": 2.0, # Coefficient of relative risk aversion ""Rfree"": 1.03, # Interest factor on assets ""DiscFac"": 0.96, # Intertemporal discount factor ""LivPrb"" : [0.98], # Survival probability ""PermGroFac"" :[1.01], # Permanent income growth factor # Parameters that specify the income distribution over the lifecycle ""PermShkStd"" : [0.1], # Standard deviation of log permanent shocks to income ""PermShkCount"" : 7, # Number of points in discrete approximation to permanent income shocks ""TranShkStd"" : [0.2], # Standard deviation of log transitory shocks to income ""TranShkCount"" : 7, # Number of points in discrete approximation to transitory income shocks ""UnempPrb"" : 0.05, # Probability of unemployment while working ""IncUnemp"" : 0.3, # Unemployment benefits replacement rate ""UnempPrbRet"" : 0.0005, # Probability of ""unemployment"" while retired ""IncUnempRet"" : 0.0, # ""Unemployment"" benefits when retired ""T_retire"" : 0, # Period of retirement (0 --> no retirement) ""tax_rate"" : 0.0, # Flat income tax rate (legacy parameter, will be removed in future) # Parameters for constructing the ""assets above minimum"" grid ""aXtraMin"" : 0.001, # Minimum end-of-period ""assets above minimum"" value ""aXtraMax"" : 20, # Maximum end-of-period ""assets above minimum"" value ""aXtraCount"" : 48, # Number of points in the base grid of ""assets above minimum"" ""aXtraNestFac"" : 3, # Exponential nesting factor when constructing ""assets above minimum"" grid ""aXtraExtra"" : [None], # Additional values to add to aXtraGrid # A few other paramaters ""BoroCnstArt"" : 0.0, # Artificial borrowing constraint; imposed minimum level of end-of period assets ""vFuncBool"" : True, # Whether to calculate the value function during solution ""CubicBool"" : False, # Preference shocks currently only compatible with linear cFunc ""T_cycle"" : 1, # Number of periods in the cycle for this agent type # Parameters only used in simulation ""AgentCount"" : 10000, # Number of agents ", https://github.com/ECSHackWeek/impedance.py,d1019c161be9e967ae227117b72acae4351e198f,impedance/tests/test_circuit_elements.py::test_each_element,impedance/tests/test_circuit_elements.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import string import numpy as np import pytest from impedance.models.circuits.elements import circuit_elements, s, p def test_each_element(): freqs = [0.001, 1.0, 1000] correct_vals = {'R': [0.1, 0.1, 0.1], 'C': [-1591.5494309189532j, -1.5915494309189535j, -0.0015915494309189533j], 'L': [0.000628319j, 0.628319j, 628.319j], 'W': [(1.26156626-1.26156626j), (0.03989423-0.03989423j), (0.00126157-0.00126157j)], 'Wo': [(0.033333332999112786-79.57747433847442j), (0.03300437046673635-0.08232866785870396j), (0.0019947114020071634-0.0019947114020071634j)], 'Ws': [(0.09999998-4.18878913e-05j), (0.08327519-3.33838167e-02j), (0.00199471-1.99471140e-03j)], 'CPE': [(26.216236841407248-8.51817171087997j), (6.585220960717244-2.139667994182814j), (1.6541327179718126-0.537460300252313j)], 'La': [(0.21769191+0.07073239j), (0.86664712+0.28159072j), (3.45018434+1.12103285j)], 'G': [(0.09999994078244179-0.00006283179105931961j), (0.07107755021941357-0.03427465211788068j), (0.00199550459845528-0.0019939172581851707j)], 'Gs': [(0.3432733166533134-0.00041895248193532704j), (0.1391819314527732-0.16248466787637972j), (0.0019955029598887875-0.0019939170758457437j)], 'T': [(1.00041-0.00837309j), (0.0156037-0.114062j), (0.00141056-0.00141039j)], 'K': [(0.099999842086579 - 0.000125663507704j), (0.038772663673915 - 0.048723166143232j), (6.332569967499333e-08 - 7.957742115295703e-05j)]} input_vals = [0.1, 0.2, 0.3, 0.4] for key, f in circuit_elements.items(): # don't test the outputs of series and parallel functions if key not in ['s', 'p']: num_inputs = f.num_params val = f(input_vals[:num_inputs], freqs) assert np.isclose(val, correct_vals[key]).all() # check for typing: with pytest.raises(AssertionError): f = circuit_elements['R'] f(1, 2) # test for handling more wrong inputs with pytest.raises(AssertionError): f(['hi'], ['yes', 'hello']) # Test no overflow in T at high frequencies with pytest.warns(None) as record: circuit_elements['T']([1, 2, 50, 100], [10000]) assert not record def test_s(): a = np.array([5 + 6*1j, 2 + 3*1j]) b = np.array([5 + 6*1j, 2 + 3*1j]) answer = np.array([10 + 12*1j, 4 + 6*1j]) assert np.isclose(s([a, b]), answer).all() def test_p(): a = np.array([5 + 6*1j, 2 + 3*1j]) b = np.array([5 + 6*1j, 2 + 3*1j]) answer = np.array([2.5 + 3*1j, 1 + 1.5*1j]) assert np.isclose(p([a, b]), answer).all() def test_element_function_names(): # run a simple check to ensure there are no integers # in the function names letters = string.ascii_uppercase + string.ascii_lowercase for elem in circuit_elements.keys(): for char in elem: assert char in letters, \ f'{char} in {elem} is not in the allowed set of {letters}' ", https://github.com/ECSHackWeek/impedance.py,d1019c161be9e967ae227117b72acae4351e198f,impedance/tests/test_circuit_elements.py::test_element_function_names,impedance/tests/test_circuit_elements.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import string import numpy as np import pytest from impedance.models.circuits.elements import circuit_elements, s, p def test_each_element(): freqs = [0.001, 1.0, 1000] correct_vals = {'R': [0.1, 0.1, 0.1], 'C': [-1591.5494309189532j, -1.5915494309189535j, -0.0015915494309189533j], 'L': [0.000628319j, 0.628319j, 628.319j], 'W': [(1.26156626-1.26156626j), (0.03989423-0.03989423j), (0.00126157-0.00126157j)], 'Wo': [(0.033333332999112786-79.57747433847442j), (0.03300437046673635-0.08232866785870396j), (0.0019947114020071634-0.0019947114020071634j)], 'Ws': [(0.09999998-4.18878913e-05j), (0.08327519-3.33838167e-02j), (0.00199471-1.99471140e-03j)], 'CPE': [(26.216236841407248-8.51817171087997j), (6.585220960717244-2.139667994182814j), (1.6541327179718126-0.537460300252313j)], 'La': [(0.21769191+0.07073239j), (0.86664712+0.28159072j), (3.45018434+1.12103285j)], 'G': [(0.09999994078244179-0.00006283179105931961j), (0.07107755021941357-0.03427465211788068j), (0.00199550459845528-0.0019939172581851707j)], 'Gs': [(0.3432733166533134-0.00041895248193532704j), (0.1391819314527732-0.16248466787637972j), (0.0019955029598887875-0.0019939170758457437j)], 'T': [(1.00041-0.00837309j), (0.0156037-0.114062j), (0.00141056-0.00141039j)], 'K': [(0.099999842086579 - 0.000125663507704j), (0.038772663673915 - 0.048723166143232j), (6.332569967499333e-08 - 7.957742115295703e-05j)]} input_vals = [0.1, 0.2, 0.3, 0.4] for key, f in circuit_elements.items(): # don't test the outputs of series and parallel functions if key not in ['s', 'p']: num_inputs = f.num_params val = f(input_vals[:num_inputs], freqs) assert np.isclose(val, correct_vals[key]).all() # check for typing: with pytest.raises(AssertionError): f = circuit_elements['R'] f(1, 2) # test for handling more wrong inputs with pytest.raises(AssertionError): f(['hi'], ['yes', 'hello']) # Test no overflow in T at high frequencies with pytest.warns(None) as record: circuit_elements['T']([1, 2, 50, 100], [10000]) assert not record def test_s(): a = np.array([5 + 6*1j, 2 + 3*1j]) b = np.array([5 + 6*1j, 2 + 3*1j]) answer = np.array([10 + 12*1j, 4 + 6*1j]) assert np.isclose(s([a, b]), answer).all() def test_p(): a = np.array([5 + 6*1j, 2 + 3*1j]) b = np.array([5 + 6*1j, 2 + 3*1j]) answer = np.array([2.5 + 3*1j, 1 + 1.5*1j]) assert np.isclose(p([a, b]), answer).all() def test_element_function_names(): # run a simple check to ensure there are no integers # in the function names letters = string.ascii_uppercase + string.ascii_lowercase for elem in circuit_elements.keys(): for char in elem: assert char in letters, \ f'{char} in {elem} is not in the allowed set of {letters}' ", https://github.com/edasi/kool,efe847a05c1a07af5eddeeeb700fdefcb6c9ced9,tests/contrib/quizzes/test_question.py::TestQuestion::test_add_choices,tests/contrib/quizzes/test_question.py,NOD,flaky,Opened,https://github.com/edasi/kool/pull/51,classify;root_cause,"import os import pytest from kool.db.models import Model from kool.db.flatfile import FlatFileDB from kool.contrib.auth import User from kool.contrib.courses import Course from kool.contrib.quizzes import Quiz, Question TESTS_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) class TestQuestion(object): @classmethod def setup_class(cls): testdb = os.path.join(TESTS_DIR, 'fixtures') Model.db = FlatFileDB(path=testdb) cls.user = User(first_name='John', last_name='Doe', email='john@doe.com', password='secretpwd') cls.user_id = cls.user.save() cls.course_id = Course('Testers', cls.user_id).save() cls.quiz = Quiz('quiz test', cls.course_id) cls.quiz_id = cls.quiz.save() cls.question = Question('favourite pet?', cls.quiz_id) cls.question_id = cls.question.save() def test_add_choices(self): self.question.add_choices(choices=['cat', 'dog', 'bird'], correct_answers=['dog']) assert self.question.choices == {1: 'cat', 2: 'dog', 3: 'bird'} assert self.question.correct_answers == [(2, 'dog')] def test_add_answers(self): self.question.add_answers(2) assert isinstance(self.question.answers, list) assert self.question.answers[0] == 2 with pytest.raises(KeyError): self.question.add_answers(4) def test_check_answer(self): response = self.question.check_answer() assert response == {2: 'Correct'} ", https://github.com/edasi/kool,efe847a05c1a07af5eddeeeb700fdefcb6c9ced9,tests/db/flatfile/test_database.py::TestFlatFile::test_tables,tests/db/flatfile/test_database.py,OD,flaky,Opened,https://github.com/edasi/kool/pull/50,classify;root_cause,"import os import pytest from kool.db.flatfile import FlatFileDB, Table, where TESTS_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) class TestFlatFile(object): @classmethod def setup_class(cls): testdb = os.path.join(TESTS_DIR, 'fixtures') cls.db = FlatFileDB(path=testdb) cls.db.purge_table('.meta') def test_one_table(self): table1 = self.db.create_table('table1') table1.insert_multiple({'int': 1, 'char': c} for c in 'abc') assert table1.get(where('int') == '1')['char'] == 'a' assert table1.get(where('char') == 'b')['char'] == 'b' table1.purge() assert len(table1) == 0 def test_multiple_tables(self): table1 = self.db.create_table('table1') table2 = self.db.create_table('table2') table3 = self.db.create_table('table3') table1.insert({'int': 1, 'char': 'a'}) table2.insert({'int': 1, 'char': 'b'}) table3.insert({'int': 1, 'char': 'c'}) assert table1.count(where('char') == 'a') == 1 assert table2.count(where('char') == 'b') == 1 assert table3.count(where('char') == 'c') == 1 self.db.purge_table('table1') self.db.purge_table('table2') self.db.purge_table('table3') assert len(table1) == 0 assert len(table2) == 0 assert len(table3) == 0 def test_table(self): table = self.db.table('table1') assert isinstance(table, Table) def test_tables(self): assert len(self.db.tables()) == 3 assert isinstance(self.db.tables()[0], Table) def test_table_is_iterable(self): table = self.db.table('table1') table.insert_multiple({'int': i} for i in range(3)) assert [r for r in table] == table.all() table.purge() assert len(table) == 0 def test_table_get(self): table = self.db.table('table1') table.insert({'int': 1, 'char': 'a'}) table.insert({'int': 2, 'char': 'b'}) table.insert({'int': 3, 'char': 'c'}) assert table.get(rid=2)['char'] == 'b' assert table.get(cond=(where('char') == 'a'))['int'] == '1' table.purge() assert len(table) == 0 ", https://github.com/EPAENERGYSTAR/epathermostat,98aaf571fe8e15e1a372567776081fd9dae7e872,tests/test_core.py::test_multiple_same_key,tests/test_core.py,NIO,flaky,DevelopersDoNotWantFix,https://github.com/EPAENERGYSTAR/epathermostat/pull/33,classify;root_cause,"from thermostat.importers import from_csv from thermostat.util.testing import get_data_path import numpy as np import pandas as pd from datetime import datetime import pytest from .fixtures.thermostats import thermostat_type_1 from .fixtures.thermostats import thermostat_type_2 from .fixtures.thermostats import thermostat_type_3 from .fixtures.thermostats import thermostat_type_4 from .fixtures.thermostats import thermostat_type_5 from .fixtures.thermostats import core_heating_day_set_type_1_entire from .fixtures.thermostats import core_heating_day_set_type_2 from .fixtures.thermostats import core_heating_day_set_type_3 from .fixtures.thermostats import core_heating_day_set_type_4 from .fixtures.thermostats import core_cooling_day_set_type_1_entire from .fixtures.thermostats import core_cooling_day_set_type_2 from .fixtures.thermostats import core_cooling_day_set_type_3 from .fixtures.thermostats import core_cooling_day_set_type_5 from .fixtures.thermostats import metrics_type_1_data from .fixtures.thermostats import thermostat_zero_days from .fixtures.thermostats import thermostats_multiple_same_key from numpy.testing import assert_allclose from numpy import isnan def test_zero_days_warning(thermostat_zero_days): output = thermostat_zero_days.calculate_epa_field_savings_metrics() assert isnan(output[0]['daily_mean_core_cooling_runtime']) def test_multiple_same_key(thermostats_multiple_same_key): metrics = [] for thermostat in thermostats_multiple_same_key: outputs = thermostat.calculate_epa_field_savings_metrics() metrics.extend(outputs) assert len(metrics) == 4 for key in metrics[0]: assert(metrics[0][key] == metrics[2][key]) for key in metrics[1]: # nan can never be equal, so skip nan if not (isinstance(metrics[1][key], float) and np.isnan(metrics[1][key])): assert(metrics[1][key] == metrics[3][key]) def test_interpolate_empty(thermostat_type_1): s1 = pd.Series([]) s1_intp = thermostat_type_1._interpolate(s1) np.testing.assert_allclose(s1_intp, []) def test_interpolate_full(thermostat_type_1): s2 = pd.Series([1]) s2_intp = thermostat_type_1._interpolate(s2) np.testing.assert_allclose(s2_intp, [1]) np.testing.assert_allclose(s2_intp, [1]) def test_interpolate_forward(thermostat_type_1): s3 = pd.Series([1, np.nan]) s3_intp = thermostat_type_1._interpolate(s3) np.testing.assert_allclose(s3_intp, [1, 1]) def test_interpolate_backward(thermostat_type_1): s4 = pd.Series([np.nan, 1]) s4_intp = thermostat_type_1._interpolate(s4) np.testing.assert_allclose(s4_intp, [1, 1]) def test_interpolate_one_missing(thermostat_type_1): s5 = pd.Series([8, 3, np.nan, 1, 7]) s5_intp = thermostat_type_1._interpolate(s5) np.testing.assert_allclose(s5_intp, [8,3,2,1,7]) def test_interpolate_two_missing(thermostat_type_1): s6 = pd.Series([8, np.nan, np.nan, 1, 7]) s6_intp = thermostat_type_1._interpolate(s6) np.testing.assert_allclose(s6_intp, [8,5.666,3.333,1,7], rtol=1e-3) def test_interpolate_three_missing(thermostat_type_1): s = pd.Series([8, np.nan, np.nan, np.nan, 1, 7]) s_intp = thermostat_type_1._interpolate(s) np.testing.assert_allclose(s_intp, [8,6.25,np.nan,2.75,1,7]) def test_thermostat_type_1_get_core_heating_days(thermostat_type_1): core_heating_day_sets = thermostat_type_1.get_core_heating_days( method=""year_mid_to_mid"") assert len(core_heating_day_sets) == 5 def test_thermostat_type_1_get_core_cooling_days(thermostat_type_1): core_cooling_day_sets = thermostat_type_1.get_core_cooling_days( method=""year_end_to_end"") assert len(core_cooling_day_sets) == 4 def test_thermostat_type_2_get_core_heating_days(thermostat_type_2): core_heating_day_sets = thermostat_type_2.get_core_heating_days( method=""year_mid_to_mid"") assert len(core_heating_day_sets) == 5 def test_thermostat_type_2_get_core_cooling_days(thermostat_type_2): core_cooling_day_sets = thermostat_type_2.get_core_cooling_days( method=""year_end_to_end"") assert len(core_cooling_day_sets) == 4 def test_thermostat_type_3_get_core_heating_days(thermostat_type_3): core_heating_day_sets = thermostat_type_3.get_core_heating_days( method=""year_mid_to_mid"") assert len(core_heating_day_sets) == 5 def test_thermostat_type_3_get_core_cooling_days(thermostat_type_3): core_cooling_day_sets = thermostat_type_3.get_core_cooling_days( method=""year_end_to_end"") assert len(core_cooling_day_sets) == 4 def test_thermostat_type_4_get_core_heating_days(thermostat_type_4): core_heating_day_sets = thermostat_type_4.get_core_heating_days( method=""year_mid_to_mid"") assert len(core_heating_day_sets) == 5 def test_thermostat_type_4_get_core_cooling_days(thermostat_type_4): with pytest.raises(ValueError): core_cooling_day_sets = thermostat_type_4.get_core_cooling_days( method=""year_end_to_end"") def test_thermostat_type_5_get_core_heating_days(thermostat_type_5): with pytest.raises(ValueError): core_heating_day_sets = thermostat_type_5.get_core_heating_days( method=""year_mid_to_mid"") def test_thermostat_type_5_get_core_cooling_days(thermostat_type_5): core_cooling_day_sets = thermostat_type_5.get_core_cooling_days( method=""year_end_to_end"") assert len(core_cooling_day_sets) == 4 def test_thermostat_type_1_get_core_heating_days_with_params(thermostat_type_1): core_heating_day_sets = thermostat_type_1.get_core_heating_days( min_minutes_heating=0, max_minutes_cooling=0, method=""year_mid_to_mid"") assert len(core_heating_day_sets) == 5 def test_thermostat_type_1_get_core_cooling_days_with_params(thermostat_type_1): core_heating_day_sets = thermostat_type_1.get_core_cooling_days( min_minutes_cooling=0, max_minutes_heating=0, method=""year_end_to_end"") assert len(core_heating_day_sets) == 4 def test_thermostat_core_heating_day_set_attributes(core_heating_day_set_type_1_entire): assert isinstance(core_heating_day_set_type_1_entire.name, str) assert isinstance(core_heating_day_set_type_1_entire.daily, pd.Series) assert isinstance(core_heating_day_set_type_1_entire.hourly, pd.Series) assert core_heating_day_set_type_1_entire.daily.shape == (1461,) assert core_heating_day_set_type_1_entire.hourly.shape == (35064,) assert ( isinstance(core_heating_day_set_type_1_entire.start_date, datetime) or isinstance(core_heating_day_set_type_1_entire.start_date, np.datetime64) ) assert ( isinstance(core_heating_day_set_type_1_entire.end_date, datetime) or isinstance(core_heating_day_set_type_1_entire.end_date, np.datetime64) ) def test_thermostat_core_cooling_day_set_attributes(core_cooling_day_set_type_1_entire): assert isinstance(core_cooling_day_set_type_1_entire.name, str) assert isinstance(core_cooling_day_set_type_1_entire.daily, pd.Series) assert isinstance(core_cooling_day_set_type_1_entire.hourly, pd.Series) assert core_cooling_day_set_type_1_entire.daily.shape == (1461,) assert core_cooling_day_set_type_1_entire.hourly.shape == (35064,) assert ( isinstance(core_cooling_day_set_type_1_entire.start_date, datetime) or isinstance(core_cooling_day_set_type_1_entire.start_date, np.datetime64) ) assert ( isinstance(core_cooling_day_set_type_1_entire.end_date, datetime) or isinstance(core_cooling_day_set_type_1_entire.end_date, np.datetime64) ) def test_thermostat_type_1_total_heating_runtime(thermostat_type_1, core_heating_day_set_type_1_entire, metrics_type_1_data): total_runtime = thermostat_type_1.total_heating_runtime(core_heating_day_set_type_1_entire) assert_allclose(total_runtime, metrics_type_1_data[1][""total_core_heating_runtime""], rtol=1e-3) def test_thermostat_type_1_total_emergency_heating_runtime(thermostat_type_1, core_heating_day_set_type_1_entire, metrics_type_1_data): total_runtime = thermostat_type_1.total_emergency_heating_runtime(core_heating_day_set_type_1_entire) assert_allclose(total_runtime, metrics_type_1_data[1][""total_emergency_heating_core_day_runtime""], rtol=1e-3) def test_thermostat_type_1_total_auxiliary_heating_runtime(thermostat_type_1, core_heating_day_set_type_1_entire, metrics_type_1_data): total_runtime = thermostat_type_1.total_auxiliary_heating_runtime(core_heating_day_set_type_1_entire) assert_allclose(total_runtime, metrics_type_1_data[1][""total_auxiliary_heating_core_day_runtime""], rtol=1e-3) def test_thermostat_type_1_total_cooling_runtime(thermostat_type_1, core_cooling_day_set_type_1_entire, metrics_type_1_data): total_runtime = thermostat_type_1.total_cooling_runtime(core_cooling_day_set_type_1_entire) assert_allclose(total_runtime, metrics_type_1_data[0][""total_core_cooling_runtime""], rtol=1e-3) def test_thermostat_type_1_get_resistance_heat_utilization_bins_rhu1(thermostat_type_1, core_heating_day_set_type_1_entire, metrics_type_1_data): start = 0 stop = 60 step = 5 temperature_bins = list(t for t in range(start, stop+step, step)) rhu_runtime = thermostat_type_1.get_resistance_heat_utilization_runtime( core_heating_day_set_type_1_entire) rhu = thermostat_type_1.get_resistance_heat_utilization_bins( rhu_runtime, temperature_bins, core_heating_day_set_type_1_entire) assert len(rhu) == 12 for item in rhu.itertuples(): bin_name = thermostat_type_1._format_rhu('rhu1', item.Index.left, item.Index.right, duty_cycle=None) bin_value = item.rhu assert_allclose(bin_value, metrics_type_1_data[1][bin_name], rtol=1e-3) def test_thermostat_type_1_get_resistance_heat_", https://github.com/epsagon/epsagon-python,b985d8412848f9a0c98e2f15ae0967a6dba3517f,tests/wrappers/test_python_function.py::test_function_wrapper_function_exception,tests/wrappers/test_python_function.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import mock import pytest from epsagon import trace_factory import epsagon.constants trace_mock = mock.MagicMock() def setup_function(func): trace_factory.use_single_trace = True def test_function_wrapper_sanity(trace_transport, ): retval = 'success' @epsagon.python_wrapper(name='test-func') def wrapped_function(event, context): return retval assert wrapped_function('a', 'b') == 'success' assert len(trace_transport.last_trace.events) == 1 event = trace_transport.last_trace.events[0] assert event.resource['type'] == 'python_function' assert event.resource['name'] == 'test-func' assert event.resource['metadata']['python.function.return_value'] == retval assert event.error_code == 0 def test_function_wrapper_function_exception(trace_transport): @epsagon.python_wrapper() def wrapped_function(event, context): raise TypeError('test') with pytest.raises(TypeError): wrapped_function('a', 'b') assert len(trace_transport.last_trace.events) == 1 event = trace_transport.last_trace.events[0] assert event.exception['type'] == 'TypeError' assert event.resource['metadata']['python.function.return_value'] is None assert not trace_transport.last_trace.exceptions @mock.patch( 'epsagon.trace.trace_factory.get_or_create_trace', side_effect=lambda: trace_mock ) def test_python_wrapper_python_runner_factory_failed(_): @epsagon.python_wrapper def wrapped_function(event, context): return 'success' with mock.patch( 'epsagon.runners.python_function.PythonRunner', side_effect=TypeError() ): assert wrapped_function('a', 'b') == 'success' trace_mock.prepare.assert_called_once() trace_mock.send_traces.assert_not_called() trace_mock.set_runner.assert_not_called() ", https://github.com/epsagon/epsagon-python,b985d8412848f9a0c98e2f15ae0967a6dba3517f,tests/wrappers/test_python_function.py::test_function_wrapper_sanity,tests/wrappers/test_python_function.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import mock import pytest from epsagon import trace_factory import epsagon.constants trace_mock = mock.MagicMock() def setup_function(func): trace_factory.use_single_trace = True def test_function_wrapper_sanity(trace_transport, ): retval = 'success' @epsagon.python_wrapper(name='test-func') def wrapped_function(event, context): return retval assert wrapped_function('a', 'b') == 'success' assert len(trace_transport.last_trace.events) == 1 event = trace_transport.last_trace.events[0] assert event.resource['type'] == 'python_function' assert event.resource['name'] == 'test-func' assert event.resource['metadata']['python.function.return_value'] == retval assert event.error_code == 0 def test_function_wrapper_function_exception(trace_transport): @epsagon.python_wrapper() def wrapped_function(event, context): raise TypeError('test') with pytest.raises(TypeError): wrapped_function('a', 'b') assert len(trace_transport.last_trace.events) == 1 event = trace_transport.last_trace.events[0] assert event.exception['type'] == 'TypeError' assert event.resource['metadata']['python.function.return_value'] is None assert not trace_transport.last_trace.exceptions @mock.patch( 'epsagon.trace.trace_factory.get_or_create_trace', side_effect=lambda: trace_mock ) def test_python_wrapper_python_runner_factory_failed(_): @epsagon.python_wrapper def wrapped_function(event, context): return 'success' with mock.patch( 'epsagon.runners.python_function.PythonRunner', side_effect=TypeError() ): assert wrapped_function('a', 'b') == 'success' trace_mock.prepare.assert_called_once() trace_mock.send_traces.assert_not_called() trace_mock.set_runner.assert_not_called() ", https://github.com/epsagon/epsagon-python,b985d8412848f9a0c98e2f15ae0967a6dba3517f,tests/wrappers/test_python_function.py::test_python_wrapper_python_runner_factory_failed,tests/wrappers/test_python_function.py,NIO,flaky,Accepted,https://github.com/epsagon/epsagon-python/pull/378,classify;root_cause;fix_proposal,"import mock import pytest from epsagon import trace_factory import epsagon.constants trace_mock = mock.MagicMock() def setup_function(func): trace_factory.use_single_trace = True def test_function_wrapper_sanity(trace_transport, ): retval = 'success' @epsagon.python_wrapper(name='test-func') def wrapped_function(event, context): return retval assert wrapped_function('a', 'b') == 'success' assert len(trace_transport.last_trace.events) == 1 event = trace_transport.last_trace.events[0] assert event.resource['type'] == 'python_function' assert event.resource['name'] == 'test-func' assert event.resource['metadata']['python.function.return_value'] == retval assert event.error_code == 0 def test_function_wrapper_function_exception(trace_transport): @epsagon.python_wrapper() def wrapped_function(event, context): raise TypeError('test') with pytest.raises(TypeError): wrapped_function('a', 'b') assert len(trace_transport.last_trace.events) == 1 event = trace_transport.last_trace.events[0] assert event.exception['type'] == 'TypeError' assert event.resource['metadata']['python.function.return_value'] is None assert not trace_transport.last_trace.exceptions @mock.patch( 'epsagon.trace.trace_factory.get_or_create_trace', side_effect=lambda: trace_mock ) def test_python_wrapper_python_runner_factory_failed(_): @epsagon.python_wrapper def wrapped_function(event, context): return 'success' with mock.patch( 'epsagon.runners.python_function.PythonRunner', side_effect=TypeError() ): assert wrapped_function('a', 'b') == 'success' trace_mock.prepare.assert_called_once() trace_mock.send_traces.assert_not_called() trace_mock.set_runner.assert_not_called() ", https://github.com/ericmjl/nxviz,d2a2a622e834148a9f4337dbe1021a05c5f18548,tests/test_geometry.py::test_correct_negative_angle,tests/test_geometry.py,NOD,flaky,nan,nan,classify;root_cause,"""""""Tests for geometry module."""""" import numpy as np from random import choice import nxviz.polcart as polcart from hypothesis import assume, given, settings from hypothesis.strategies import floats, integers, lists from nxviz.geometry import ( circos_radius, correct_negative_angle, get_cartesian, node_theta, ) def test_circos_radius(): """""" Check radius correctness. Uses the other triangle geometry rule to check that the radius is correct. """""" n_nodes = 10 node_r = 1 A = 2 * np.pi / n_nodes # noqa circ_r = 2 * node_r / np.sqrt(2 * (1 - np.cos(A))) assert np.allclose(circ_r, circos_radius(n_nodes, node_r)) # @settings(perform_health_check=False) @given(lists(integers())) def test_node_theta(nodelist): """"""Tests node_theta function."""""" assume(len(nodelist) > 0) node = choice(nodelist) theta_obs = node_theta(nodelist, node) i = nodelist.index(node) theta_exp = -np.pi + i * 2 * np.pi / len(nodelist) if theta_exp > np.pi: theta_exp = np.pi - theta_exp assert np.allclose(theta_obs, theta_exp) @given(floats(), floats()) def test_get_cartesian(r, theta): """""" Test for get_cartesian. Makes sure that `get_cartesian` remains a wrapper around polcart's `to_cartesian`. """""" assume(np.isfinite(theta)) assume(np.isfinite(r)) assert get_cartesian(r, theta) == polcart.to_cartesian(r, theta) # @settings(perform_health_check=False) @given(floats()) def test_correct_negative_angle(angle): """"""Test for correct calculation of negative angle."""""" assume(angle < 0) assume(angle >= -2 * np.pi) exp = 2 * np.pi + angle obs = correct_negative_angle(angle) assert np.allclose(obs, exp) assert obs <= 2 * np.pi assert obs >= 0 ", https://github.com/ericmjl/nxviz,d2a2a622e834148a9f4337dbe1021a05c5f18548,tests/test_plots.py::test_matrix_plot,tests/test_plots.py,NOD,flaky,nan,nan,classify;root_cause,""""""" Some rudimentary tests. Instantiates a Plot object, but doesn't test plotting. Discovered that this set of rudimentary tests might be necessary after seeing the following issue: https://github.com/ericmjl/nxviz/issues/160 """""" from .test_utils import diff_plots, corresponding_lists from random import random import networkx as nx from nxviz import ArcPlot, CircosPlot, GeoPlot, MatrixPlot from matplotlib.testing.decorators import _image_directories G = nx.erdos_renyi_graph(n=20, p=0.2) G_geo = G.copy() for n, d in G_geo.nodes(data=True): G_geo.nodes[n][""latitude""] = random() G_geo.nodes[n][""longitude""] = random() G_geo.nodes[n][""dpcapacity""] = random() baseline_dir, result_dir = _image_directories(lambda: ""dummy func"") def test_circos_plot(): c = CircosPlot(G) # noqa: F841 diff = diff_plots(c, ""circos.png"", baseline_dir, result_dir) assert diff is None def test_matrix_plot(): m = MatrixPlot(G) # noqa: F841 diff = diff_plots(m, ""matrix.png"", baseline_dir, result_dir) assert diff is None def test_arc_plot(): a = ArcPlot(G) # noqa: F841 diff = diff_plots(a, ""arc.png"", baseline_dir, result_dir) assert diff is None def test_geo_plot(): g = GeoPlot( G_geo, node_lat=""latitude"", node_lon=""longitude"", # noqa: F841 color=""dpcapacity"", ) diff = diff_plots(g, ""geo.png"", baseline_dir, result_dir) assert diff is None def test_plot_size(): c = CircosPlot(G, figsize=(3, 3)) # noqa: F841 diff = diff_plots(c, ""circos33.png"", baseline_dir, result_dir) assert diff is None def test_edge_widths(): # add weight as attribute and fill with random numbers edges = G.edges() for u, v in edges: G[u][v][""weight""] = random() # also extract list for testing weights = [G[u][v][""weight""] for u, v in edges] # add weights as property c = CircosPlot(G, edge_width=""weight"") assert c.edge_widths == weights a = ArcPlot(G, edge_width=""weight"") assert a.edge_widths == weights # add weights as list c = CircosPlot(G, edge_width=weights) assert c.edge_widths == weights a = ArcPlot(G, edge_width=weights) assert a.edge_widths == weights def test_edge_color(): # add color as attribute and fill with random numbers edges = G.edges() for u, v in edges: G[u][v][""type""] = ""a"" if random() < 0.5 else ""b"" # also extract list for testing types = [G[u][v][""type""] for u, v in edges] # add color as property c = CircosPlot(G, edge_color=""type"") assert corresponding_lists(c.edge_colors, types) a = ArcPlot(G, edge_color=""type"") assert corresponding_lists(a.edge_colors, types) def test_node_size(): # add size as attribute and fill with random numbers nodes = G.nodes() for u in nodes: G.nodes[u][""score""] = random() # also extract list for testing scores = [G.nodes[u][""score""] for u in nodes] # add color as property a = ArcPlot(G, node_size=""score"") assert a.node_sizes == scores # add types as list a = ArcPlot(G, node_size=scores) assert a.node_sizes == scores ", https://github.com/Erik-White/ColonyScanalyser,38034b7d62a7c4f72a9f6981d0218c23f22cc7c8,tests/unit/test_base.py::TestUnique::test_init,tests/unit/test_base.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest from unittest import mock from datetime import datetime from colonyscanalyser.base import ( Identified, IdentifiedCollection, Named, Unique, TimeStamped, TimeStampElapsed ) @pytest.fixture(params = [1, 2, 10000000]) def id(request): yield request.param @pytest.fixture(params = [-1, 0, 0.5, ""one""]) def id_invalid(request): yield request.param class TestIdentified: def test_init(self, id): assert Identified(id).id == id def test_id(self, id): obj = Identified(id) obj.id = id * 3 assert obj.id == id * 3 def test_id_invalid(self, id_invalid): with pytest.raises(ValueError): Identified(id_invalid) class TestIdentifiedCollection: @staticmethod def IdentifiedMock(id): identified = mock.Mock(spec = Identified) identified.id = id return identified @pytest.fixture def identified_items(self): items = list() for i in range(2, 10): items.append(self.IdentifiedMock(i)) return items @pytest.fixture(scope = ""function"") def item_rand_id(self, request, identified_items): from random import randint yield randint(2, len(identified_items)) class TestInitialize: def test_init(self): assert IdentifiedCollection().items == list() def test_init_list(self, identified_items): collection = IdentifiedCollection(identified_items) assert collection.items == identified_items class TestProperties: def test_count(self, identified_items): collection = IdentifiedCollection(identified_items) assert collection.count == len(identified_items) def test_items_none(self): collection = IdentifiedCollection(None) collection.items = None assert collection.items is not None assert isinstance(collection.items, list) def test_items_sorted(self, identified_items): from random import sample identified_items_shuffled = sample(identified_items, len(identified_items)) collection = IdentifiedCollection(identified_items_shuffled) assert collection.items != identified_items_shuffled assert collection.items == identified_items @pytest.mark.parametrize(""items"", [list(), dict()]) def test_items_iterable(self, items): collection = IdentifiedCollection() if isinstance(items, dict): items[1] = TestIdentifiedCollection.IdentifiedMock(1) else: items.append(TestIdentifiedCollection.IdentifiedMock(1)) collection.items = items assert isinstance(collection.items, list) assert len(collection.items) == len(items) @pytest.mark.parametrize(""items"", [1, ""1""]) def test_items_iterable_invalid(self, items): collection = IdentifiedCollection() with pytest.raises((TypeError, ValueError)): collection.items = items class TestMethods: def test_add_item(self, identified_items): collection = IdentifiedCollection(identified_items) item_new = collection.add(id = 1) assert collection.count == len(identified_items) + 1 assert item_new in collection.items assert collection.items[0] == item_new def test_append_item(self): collection = IdentifiedCollection() identified_item = TestIdentifiedCollection.IdentifiedMock(1) collection.append(identified_item) assert collection.count == 1 assert any(identified_item.id == item.id for item in collection.items) with pytest.raises(ValueError): collection.append(identified_item) def test_exists(self, identified_items): collection = IdentifiedCollection(identified_items) assert collection.exists(identified_items[0]) def test_id_exists(self, identified_items): collection = IdentifiedCollection(identified_items) assert collection.id_exists(identified_items[0].id) def test_get_colony(self, identified_items, item_rand_id): collection = IdentifiedCollection(identified_items) item = collection.get_item(item_rand_id) assert item is not None assert item.id == item_rand_id def test_remove_item(self, identified_items, item_rand_id): collection = IdentifiedCollection(identified_items) item = collection.get_item(item_rand_id) assert item is not None collection.remove(item_rand_id) item = collection.get_item(item_rand_id) assert item is None def test_remove_item_invalid(self, identified_items): collection = IdentifiedCollection(identified_items) with pytest.raises(KeyError): collection.remove(-1) class TestNamed: @pytest.fixture(params = [""name"", ""name with spaces"", 1, 0, -1, 1.1]) def name(self, request): yield request.param def test_init(self, name): assert Named(name).name == str(name) class TestUnique: @pytest.fixture(scope = ""class"") def unique(self): yield Unique() def test_init(self, unique): assert unique.id == 1 def test_unique(self, unique): assert unique.id == 1 unique = None assert Unique().id != 1 def test_increment(self, unique): id_count = unique.id_count items_total = 10 items = list() for i in range(items_total): items.append(Unique()) assert len(items) == items_total for i in range(items_total): assert items[i].id == i + 1 + id_count for item in items: assert (item.id != existing_item.id for existing_item in items) def test_id(self, id): obj = Unique() original_id = obj.id obj.id = id assert obj.id == original_id class TestTimeStamped: def test_init(self): timestamp = datetime(1, 1, 1) timestamped = TimeStamped(timestamp) assert timestamped.timestamp == timestamp def test_init_auto(self): assert TimeStamped().timestamp is not None class TestTimeStampElapsed: def test_init(self): timestamp = datetime(1, 1, 1) timestampelapsed = TimeStampElapsed(timestamp, timestamp) assert timestampelapsed.timestamp == timestamp assert timestampelapsed.timestamp_initial == timestamp def test_init_auto(self): assert TimeStampElapsed().timestamp is not None assert TimeStampElapsed().timestamp_initial is not None def test_timestamp_elapsed(self): timestamp_inital = datetime(1, 1, 1, 0, 0) timestamp = datetime(1, 1, 1, 1, 1) timestampelapsed = TimeStampElapsed(timestamp, timestamp_inital) timestamp_diff = timestamp - timestamp_inital assert timestampelapsed.timestamp_elapsed == timestamp_diff assert timestampelapsed.timestamp_elapsed_hours == timestamp_diff.total_seconds() / 3600 assert timestampelapsed.timestamp_elapsed_minutes == int(timestamp_diff.total_seconds() / 60)", https://github.com/Erik-White/ColonyScanalyser,38034b7d62a7c4f72a9f6981d0218c23f22cc7c8,tests/unit/test_base.py::TestUnique::test_unique,tests/unit/test_base.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest from unittest import mock from datetime import datetime from colonyscanalyser.base import ( Identified, IdentifiedCollection, Named, Unique, TimeStamped, TimeStampElapsed ) @pytest.fixture(params = [1, 2, 10000000]) def id(request): yield request.param @pytest.fixture(params = [-1, 0, 0.5, ""one""]) def id_invalid(request): yield request.param class TestIdentified: def test_init(self, id): assert Identified(id).id == id def test_id(self, id): obj = Identified(id) obj.id = id * 3 assert obj.id == id * 3 def test_id_invalid(self, id_invalid): with pytest.raises(ValueError): Identified(id_invalid) class TestIdentifiedCollection: @staticmethod def IdentifiedMock(id): identified = mock.Mock(spec = Identified) identified.id = id return identified @pytest.fixture def identified_items(self): items = list() for i in range(2, 10): items.append(self.IdentifiedMock(i)) return items @pytest.fixture(scope = ""function"") def item_rand_id(self, request, identified_items): from random import randint yield randint(2, len(identified_items)) class TestInitialize: def test_init(self): assert IdentifiedCollection().items == list() def test_init_list(self, identified_items): collection = IdentifiedCollection(identified_items) assert collection.items == identified_items class TestProperties: def test_count(self, identified_items): collection = IdentifiedCollection(identified_items) assert collection.count == len(identified_items) def test_items_none(self): collection = IdentifiedCollection(None) collection.items = None assert collection.items is not None assert isinstance(collection.items, list) def test_items_sorted(self, identified_items): from random import sample identified_items_shuffled = sample(identified_items, len(identified_items)) collection = IdentifiedCollection(identified_items_shuffled) assert collection.items != identified_items_shuffled assert collection.items == identified_items @pytest.mark.parametrize(""items"", [list(), dict()]) def test_items_iterable(self, items): collection = IdentifiedCollection() if isinstance(items, dict): items[1] = TestIdentifiedCollection.IdentifiedMock(1) else: items.append(TestIdentifiedCollection.IdentifiedMock(1)) collection.items = items assert isinstance(collection.items, list) assert len(collection.items) == len(items) @pytest.mark.parametrize(""items"", [1, ""1""]) def test_items_iterable_invalid(self, items): collection = IdentifiedCollection() with pytest.raises((TypeError, ValueError)): collection.items = items class TestMethods: def test_add_item(self, identified_items): collection = IdentifiedCollection(identified_items) item_new = collection.add(id = 1) assert collection.count == len(identified_items) + 1 assert item_new in collection.items assert collection.items[0] == item_new def test_append_item(self): collection = IdentifiedCollection() identified_item = TestIdentifiedCollection.IdentifiedMock(1) collection.append(identified_item) assert collection.count == 1 assert any(identified_item.id == item.id for item in collection.items) with pytest.raises(ValueError): collection.append(identified_item) def test_exists(self, identified_items): collection = IdentifiedCollection(identified_items) assert collection.exists(identified_items[0]) def test_id_exists(self, identified_items): collection = IdentifiedCollection(identified_items) assert collection.id_exists(identified_items[0].id) def test_get_colony(self, identified_items, item_rand_id): collection = IdentifiedCollection(identified_items) item = collection.get_item(item_rand_id) assert item is not None assert item.id == item_rand_id def test_remove_item(self, identified_items, item_rand_id): collection = IdentifiedCollection(identified_items) item = collection.get_item(item_rand_id) assert item is not None collection.remove(item_rand_id) item = collection.get_item(item_rand_id) assert item is None def test_remove_item_invalid(self, identified_items): collection = IdentifiedCollection(identified_items) with pytest.raises(KeyError): collection.remove(-1) class TestNamed: @pytest.fixture(params = [""name"", ""name with spaces"", 1, 0, -1, 1.1]) def name(self, request): yield request.param def test_init(self, name): assert Named(name).name == str(name) class TestUnique: @pytest.fixture(scope = ""class"") def unique(self): yield Unique() def test_init(self, unique): assert unique.id == 1 def test_unique(self, unique): assert unique.id == 1 unique = None assert Unique().id != 1 def test_increment(self, unique): id_count = unique.id_count items_total = 10 items = list() for i in range(items_total): items.append(Unique()) assert len(items) == items_total for i in range(items_total): assert items[i].id == i + 1 + id_count for item in items: assert (item.id != existing_item.id for existing_item in items) def test_id(self, id): obj = Unique() original_id = obj.id obj.id = id assert obj.id == original_id class TestTimeStamped: def test_init(self): timestamp = datetime(1, 1, 1) timestamped = TimeStamped(timestamp) assert timestamped.timestamp == timestamp def test_init_auto(self): assert TimeStamped().timestamp is not None class TestTimeStampElapsed: def test_init(self): timestamp = datetime(1, 1, 1) timestampelapsed = TimeStampElapsed(timestamp, timestamp) assert timestampelapsed.timestamp == timestamp assert timestampelapsed.timestamp_initial == timestamp def test_init_auto(self): assert TimeStampElapsed().timestamp is not None assert TimeStampElapsed().timestamp_initial is not None def test_timestamp_elapsed(self): timestamp_inital = datetime(1, 1, 1, 0, 0) timestamp = datetime(1, 1, 1, 1, 1) timestampelapsed = TimeStampElapsed(timestamp, timestamp_inital) timestamp_diff = timestamp - timestamp_inital assert timestampelapsed.timestamp_elapsed == timestamp_diff assert timestampelapsed.timestamp_elapsed_hours == timestamp_diff.total_seconds() / 3600 assert timestampelapsed.timestamp_elapsed_minutes == int(timestamp_diff.total_seconds() / 60)", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_check_filters.py::TestCheckFilters::test_have_cpu_only,unittests/test_check_filters.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import pytest import unittest import reframe.core.runtime as rt import reframe.frontend.check_filters as filters import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.pipeline import RegressionTest from reframe.core.exceptions import ReframeError class TestCheckFilters(unittest.TestCase): def create_check(self, attrs): ret = RegressionTest() for k, v in attrs.items(): setattr(ret, k, v) return ret def setUp(self): self.checks = [ self.create_check({ 'name': 'check1', 'tags': {'a', 'b', 'c', 'd'}, 'valid_prog_environs': ['env1', 'env2'], 'valid_systems': ['testsys:gpu', 'testsys2:mc'], 'num_gpus_per_node': 1}), self.create_check({ 'name': 'check2', 'tags': {'x', 'y', 'z'}, 'valid_prog_environs': ['env3'], 'valid_systems': ['testsys:mc', 'testsys2:mc'], 'num_gpus_per_node': 0}), self.create_check({ 'name': 'check3', 'tags': {'a', 'z'}, 'valid_prog_environs': ['env3', 'env4'], 'valid_systems': ['testsys:gpu'], 'num_gpus_per_node': 1}) ] def count_checks(self, filter_fn): return sn.count(filter(filter_fn, self.checks)) def test_have_name(self): assert 1 == self.count_checks(filters.have_name('check1')) assert 3 == self.count_checks(filters.have_name('check')) assert 2 == self.count_checks(filters.have_name(r'\S*1|\S*3')) assert 0 == self.count_checks(filters.have_name('Check')) assert 3 == self.count_checks(filters.have_name('(?i)Check')) assert 2 == self.count_checks(filters.have_name('(?i)check1|CHECK2')) def test_have_not_name(self): assert 2 == self.count_checks(filters.have_not_name('check1')) assert 1 == self.count_checks(filters.have_not_name('check1|check3')) assert 0 == self.count_checks(filters.have_not_name( 'check1|check2|check3')) assert 3 == self.count_checks(filters.have_not_name('Check1')) assert 2 == self.count_checks(filters.have_not_name('(?i)Check1')) def test_have_tags(self): assert 2 == self.count_checks(filters.have_tag('a|c')) assert 0 == self.count_checks(filters.have_tag('p|q')) assert 2 == self.count_checks(filters.have_tag('z')) def test_have_prgenv(self): assert 1 == self.count_checks(filters.have_prgenv('env1|env2')) assert 2 == self.count_checks(filters.have_prgenv('env3')) assert 1 == self.count_checks(filters.have_prgenv('env4')) assert 3 == self.count_checks(filters.have_prgenv('env1|env3')) @rt.switch_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') def test_partition(self): p = fixtures.partition_by_name('gpu') assert 2 == self.count_checks(filters.have_partition([p])) p = fixtures.partition_by_name('login') assert 0 == self.count_checks(filters.have_partition([p])) def test_have_gpu_only(self): assert 2 == self.count_checks(filters.have_gpu_only()) def test_have_cpu_only(self): assert 1 == self.count_checks(filters.have_cpu_only()) def test_invalid_regex(self): # We need to explicitly call `evaluate` to make sure the exception # is triggered in all cases with pytest.raises(ReframeError): self.count_checks(filters.have_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_not_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_tag('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_prgenv('*foo')).evaluate() ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_check_filters.py::TestCheckFilters::test_have_gpu_only,unittests/test_check_filters.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import pytest import unittest import reframe.core.runtime as rt import reframe.frontend.check_filters as filters import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.pipeline import RegressionTest from reframe.core.exceptions import ReframeError class TestCheckFilters(unittest.TestCase): def create_check(self, attrs): ret = RegressionTest() for k, v in attrs.items(): setattr(ret, k, v) return ret def setUp(self): self.checks = [ self.create_check({ 'name': 'check1', 'tags': {'a', 'b', 'c', 'd'}, 'valid_prog_environs': ['env1', 'env2'], 'valid_systems': ['testsys:gpu', 'testsys2:mc'], 'num_gpus_per_node': 1}), self.create_check({ 'name': 'check2', 'tags': {'x', 'y', 'z'}, 'valid_prog_environs': ['env3'], 'valid_systems': ['testsys:mc', 'testsys2:mc'], 'num_gpus_per_node': 0}), self.create_check({ 'name': 'check3', 'tags': {'a', 'z'}, 'valid_prog_environs': ['env3', 'env4'], 'valid_systems': ['testsys:gpu'], 'num_gpus_per_node': 1}) ] def count_checks(self, filter_fn): return sn.count(filter(filter_fn, self.checks)) def test_have_name(self): assert 1 == self.count_checks(filters.have_name('check1')) assert 3 == self.count_checks(filters.have_name('check')) assert 2 == self.count_checks(filters.have_name(r'\S*1|\S*3')) assert 0 == self.count_checks(filters.have_name('Check')) assert 3 == self.count_checks(filters.have_name('(?i)Check')) assert 2 == self.count_checks(filters.have_name('(?i)check1|CHECK2')) def test_have_not_name(self): assert 2 == self.count_checks(filters.have_not_name('check1')) assert 1 == self.count_checks(filters.have_not_name('check1|check3')) assert 0 == self.count_checks(filters.have_not_name( 'check1|check2|check3')) assert 3 == self.count_checks(filters.have_not_name('Check1')) assert 2 == self.count_checks(filters.have_not_name('(?i)Check1')) def test_have_tags(self): assert 2 == self.count_checks(filters.have_tag('a|c')) assert 0 == self.count_checks(filters.have_tag('p|q')) assert 2 == self.count_checks(filters.have_tag('z')) def test_have_prgenv(self): assert 1 == self.count_checks(filters.have_prgenv('env1|env2')) assert 2 == self.count_checks(filters.have_prgenv('env3')) assert 1 == self.count_checks(filters.have_prgenv('env4')) assert 3 == self.count_checks(filters.have_prgenv('env1|env3')) @rt.switch_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') def test_partition(self): p = fixtures.partition_by_name('gpu') assert 2 == self.count_checks(filters.have_partition([p])) p = fixtures.partition_by_name('login') assert 0 == self.count_checks(filters.have_partition([p])) def test_have_gpu_only(self): assert 2 == self.count_checks(filters.have_gpu_only()) def test_have_cpu_only(self): assert 1 == self.count_checks(filters.have_cpu_only()) def test_invalid_regex(self): # We need to explicitly call `evaluate` to make sure the exception # is triggered in all cases with pytest.raises(ReframeError): self.count_checks(filters.have_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_not_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_tag('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_prgenv('*foo')).evaluate() ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_check_filters.py::TestCheckFilters::test_have_name,unittests/test_check_filters.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import pytest import unittest import reframe.core.runtime as rt import reframe.frontend.check_filters as filters import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.pipeline import RegressionTest from reframe.core.exceptions import ReframeError class TestCheckFilters(unittest.TestCase): def create_check(self, attrs): ret = RegressionTest() for k, v in attrs.items(): setattr(ret, k, v) return ret def setUp(self): self.checks = [ self.create_check({ 'name': 'check1', 'tags': {'a', 'b', 'c', 'd'}, 'valid_prog_environs': ['env1', 'env2'], 'valid_systems': ['testsys:gpu', 'testsys2:mc'], 'num_gpus_per_node': 1}), self.create_check({ 'name': 'check2', 'tags': {'x', 'y', 'z'}, 'valid_prog_environs': ['env3'], 'valid_systems': ['testsys:mc', 'testsys2:mc'], 'num_gpus_per_node': 0}), self.create_check({ 'name': 'check3', 'tags': {'a', 'z'}, 'valid_prog_environs': ['env3', 'env4'], 'valid_systems': ['testsys:gpu'], 'num_gpus_per_node': 1}) ] def count_checks(self, filter_fn): return sn.count(filter(filter_fn, self.checks)) def test_have_name(self): assert 1 == self.count_checks(filters.have_name('check1')) assert 3 == self.count_checks(filters.have_name('check')) assert 2 == self.count_checks(filters.have_name(r'\S*1|\S*3')) assert 0 == self.count_checks(filters.have_name('Check')) assert 3 == self.count_checks(filters.have_name('(?i)Check')) assert 2 == self.count_checks(filters.have_name('(?i)check1|CHECK2')) def test_have_not_name(self): assert 2 == self.count_checks(filters.have_not_name('check1')) assert 1 == self.count_checks(filters.have_not_name('check1|check3')) assert 0 == self.count_checks(filters.have_not_name( 'check1|check2|check3')) assert 3 == self.count_checks(filters.have_not_name('Check1')) assert 2 == self.count_checks(filters.have_not_name('(?i)Check1')) def test_have_tags(self): assert 2 == self.count_checks(filters.have_tag('a|c')) assert 0 == self.count_checks(filters.have_tag('p|q')) assert 2 == self.count_checks(filters.have_tag('z')) def test_have_prgenv(self): assert 1 == self.count_checks(filters.have_prgenv('env1|env2')) assert 2 == self.count_checks(filters.have_prgenv('env3')) assert 1 == self.count_checks(filters.have_prgenv('env4')) assert 3 == self.count_checks(filters.have_prgenv('env1|env3')) @rt.switch_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') def test_partition(self): p = fixtures.partition_by_name('gpu') assert 2 == self.count_checks(filters.have_partition([p])) p = fixtures.partition_by_name('login') assert 0 == self.count_checks(filters.have_partition([p])) def test_have_gpu_only(self): assert 2 == self.count_checks(filters.have_gpu_only()) def test_have_cpu_only(self): assert 1 == self.count_checks(filters.have_cpu_only()) def test_invalid_regex(self): # We need to explicitly call `evaluate` to make sure the exception # is triggered in all cases with pytest.raises(ReframeError): self.count_checks(filters.have_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_not_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_tag('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_prgenv('*foo')).evaluate() ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_check_filters.py::TestCheckFilters::test_have_not_name,unittests/test_check_filters.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import pytest import unittest import reframe.core.runtime as rt import reframe.frontend.check_filters as filters import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.pipeline import RegressionTest from reframe.core.exceptions import ReframeError class TestCheckFilters(unittest.TestCase): def create_check(self, attrs): ret = RegressionTest() for k, v in attrs.items(): setattr(ret, k, v) return ret def setUp(self): self.checks = [ self.create_check({ 'name': 'check1', 'tags': {'a', 'b', 'c', 'd'}, 'valid_prog_environs': ['env1', 'env2'], 'valid_systems': ['testsys:gpu', 'testsys2:mc'], 'num_gpus_per_node': 1}), self.create_check({ 'name': 'check2', 'tags': {'x', 'y', 'z'}, 'valid_prog_environs': ['env3'], 'valid_systems': ['testsys:mc', 'testsys2:mc'], 'num_gpus_per_node': 0}), self.create_check({ 'name': 'check3', 'tags': {'a', 'z'}, 'valid_prog_environs': ['env3', 'env4'], 'valid_systems': ['testsys:gpu'], 'num_gpus_per_node': 1}) ] def count_checks(self, filter_fn): return sn.count(filter(filter_fn, self.checks)) def test_have_name(self): assert 1 == self.count_checks(filters.have_name('check1')) assert 3 == self.count_checks(filters.have_name('check')) assert 2 == self.count_checks(filters.have_name(r'\S*1|\S*3')) assert 0 == self.count_checks(filters.have_name('Check')) assert 3 == self.count_checks(filters.have_name('(?i)Check')) assert 2 == self.count_checks(filters.have_name('(?i)check1|CHECK2')) def test_have_not_name(self): assert 2 == self.count_checks(filters.have_not_name('check1')) assert 1 == self.count_checks(filters.have_not_name('check1|check3')) assert 0 == self.count_checks(filters.have_not_name( 'check1|check2|check3')) assert 3 == self.count_checks(filters.have_not_name('Check1')) assert 2 == self.count_checks(filters.have_not_name('(?i)Check1')) def test_have_tags(self): assert 2 == self.count_checks(filters.have_tag('a|c')) assert 0 == self.count_checks(filters.have_tag('p|q')) assert 2 == self.count_checks(filters.have_tag('z')) def test_have_prgenv(self): assert 1 == self.count_checks(filters.have_prgenv('env1|env2')) assert 2 == self.count_checks(filters.have_prgenv('env3')) assert 1 == self.count_checks(filters.have_prgenv('env4')) assert 3 == self.count_checks(filters.have_prgenv('env1|env3')) @rt.switch_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') def test_partition(self): p = fixtures.partition_by_name('gpu') assert 2 == self.count_checks(filters.have_partition([p])) p = fixtures.partition_by_name('login') assert 0 == self.count_checks(filters.have_partition([p])) def test_have_gpu_only(self): assert 2 == self.count_checks(filters.have_gpu_only()) def test_have_cpu_only(self): assert 1 == self.count_checks(filters.have_cpu_only()) def test_invalid_regex(self): # We need to explicitly call `evaluate` to make sure the exception # is triggered in all cases with pytest.raises(ReframeError): self.count_checks(filters.have_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_not_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_tag('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_prgenv('*foo')).evaluate() ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_check_filters.py::TestCheckFilters::test_have_prgenv,unittests/test_check_filters.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import pytest import unittest import reframe.core.runtime as rt import reframe.frontend.check_filters as filters import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.pipeline import RegressionTest from reframe.core.exceptions import ReframeError class TestCheckFilters(unittest.TestCase): def create_check(self, attrs): ret = RegressionTest() for k, v in attrs.items(): setattr(ret, k, v) return ret def setUp(self): self.checks = [ self.create_check({ 'name': 'check1', 'tags': {'a', 'b', 'c', 'd'}, 'valid_prog_environs': ['env1', 'env2'], 'valid_systems': ['testsys:gpu', 'testsys2:mc'], 'num_gpus_per_node': 1}), self.create_check({ 'name': 'check2', 'tags': {'x', 'y', 'z'}, 'valid_prog_environs': ['env3'], 'valid_systems': ['testsys:mc', 'testsys2:mc'], 'num_gpus_per_node': 0}), self.create_check({ 'name': 'check3', 'tags': {'a', 'z'}, 'valid_prog_environs': ['env3', 'env4'], 'valid_systems': ['testsys:gpu'], 'num_gpus_per_node': 1}) ] def count_checks(self, filter_fn): return sn.count(filter(filter_fn, self.checks)) def test_have_name(self): assert 1 == self.count_checks(filters.have_name('check1')) assert 3 == self.count_checks(filters.have_name('check')) assert 2 == self.count_checks(filters.have_name(r'\S*1|\S*3')) assert 0 == self.count_checks(filters.have_name('Check')) assert 3 == self.count_checks(filters.have_name('(?i)Check')) assert 2 == self.count_checks(filters.have_name('(?i)check1|CHECK2')) def test_have_not_name(self): assert 2 == self.count_checks(filters.have_not_name('check1')) assert 1 == self.count_checks(filters.have_not_name('check1|check3')) assert 0 == self.count_checks(filters.have_not_name( 'check1|check2|check3')) assert 3 == self.count_checks(filters.have_not_name('Check1')) assert 2 == self.count_checks(filters.have_not_name('(?i)Check1')) def test_have_tags(self): assert 2 == self.count_checks(filters.have_tag('a|c')) assert 0 == self.count_checks(filters.have_tag('p|q')) assert 2 == self.count_checks(filters.have_tag('z')) def test_have_prgenv(self): assert 1 == self.count_checks(filters.have_prgenv('env1|env2')) assert 2 == self.count_checks(filters.have_prgenv('env3')) assert 1 == self.count_checks(filters.have_prgenv('env4')) assert 3 == self.count_checks(filters.have_prgenv('env1|env3')) @rt.switch_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') def test_partition(self): p = fixtures.partition_by_name('gpu') assert 2 == self.count_checks(filters.have_partition([p])) p = fixtures.partition_by_name('login') assert 0 == self.count_checks(filters.have_partition([p])) def test_have_gpu_only(self): assert 2 == self.count_checks(filters.have_gpu_only()) def test_have_cpu_only(self): assert 1 == self.count_checks(filters.have_cpu_only()) def test_invalid_regex(self): # We need to explicitly call `evaluate` to make sure the exception # is triggered in all cases with pytest.raises(ReframeError): self.count_checks(filters.have_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_not_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_tag('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_prgenv('*foo')).evaluate() ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_check_filters.py::TestCheckFilters::test_have_tags,unittests/test_check_filters.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import pytest import unittest import reframe.core.runtime as rt import reframe.frontend.check_filters as filters import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.pipeline import RegressionTest from reframe.core.exceptions import ReframeError class TestCheckFilters(unittest.TestCase): def create_check(self, attrs): ret = RegressionTest() for k, v in attrs.items(): setattr(ret, k, v) return ret def setUp(self): self.checks = [ self.create_check({ 'name': 'check1', 'tags': {'a', 'b', 'c', 'd'}, 'valid_prog_environs': ['env1', 'env2'], 'valid_systems': ['testsys:gpu', 'testsys2:mc'], 'num_gpus_per_node': 1}), self.create_check({ 'name': 'check2', 'tags': {'x', 'y', 'z'}, 'valid_prog_environs': ['env3'], 'valid_systems': ['testsys:mc', 'testsys2:mc'], 'num_gpus_per_node': 0}), self.create_check({ 'name': 'check3', 'tags': {'a', 'z'}, 'valid_prog_environs': ['env3', 'env4'], 'valid_systems': ['testsys:gpu'], 'num_gpus_per_node': 1}) ] def count_checks(self, filter_fn): return sn.count(filter(filter_fn, self.checks)) def test_have_name(self): assert 1 == self.count_checks(filters.have_name('check1')) assert 3 == self.count_checks(filters.have_name('check')) assert 2 == self.count_checks(filters.have_name(r'\S*1|\S*3')) assert 0 == self.count_checks(filters.have_name('Check')) assert 3 == self.count_checks(filters.have_name('(?i)Check')) assert 2 == self.count_checks(filters.have_name('(?i)check1|CHECK2')) def test_have_not_name(self): assert 2 == self.count_checks(filters.have_not_name('check1')) assert 1 == self.count_checks(filters.have_not_name('check1|check3')) assert 0 == self.count_checks(filters.have_not_name( 'check1|check2|check3')) assert 3 == self.count_checks(filters.have_not_name('Check1')) assert 2 == self.count_checks(filters.have_not_name('(?i)Check1')) def test_have_tags(self): assert 2 == self.count_checks(filters.have_tag('a|c')) assert 0 == self.count_checks(filters.have_tag('p|q')) assert 2 == self.count_checks(filters.have_tag('z')) def test_have_prgenv(self): assert 1 == self.count_checks(filters.have_prgenv('env1|env2')) assert 2 == self.count_checks(filters.have_prgenv('env3')) assert 1 == self.count_checks(filters.have_prgenv('env4')) assert 3 == self.count_checks(filters.have_prgenv('env1|env3')) @rt.switch_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') def test_partition(self): p = fixtures.partition_by_name('gpu') assert 2 == self.count_checks(filters.have_partition([p])) p = fixtures.partition_by_name('login') assert 0 == self.count_checks(filters.have_partition([p])) def test_have_gpu_only(self): assert 2 == self.count_checks(filters.have_gpu_only()) def test_have_cpu_only(self): assert 1 == self.count_checks(filters.have_cpu_only()) def test_invalid_regex(self): # We need to explicitly call `evaluate` to make sure the exception # is triggered in all cases with pytest.raises(ReframeError): self.count_checks(filters.have_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_not_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_tag('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_prgenv('*foo')).evaluate() ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_check_filters.py::TestCheckFilters::test_invalid_regex,unittests/test_check_filters.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import pytest import unittest import reframe.core.runtime as rt import reframe.frontend.check_filters as filters import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.pipeline import RegressionTest from reframe.core.exceptions import ReframeError class TestCheckFilters(unittest.TestCase): def create_check(self, attrs): ret = RegressionTest() for k, v in attrs.items(): setattr(ret, k, v) return ret def setUp(self): self.checks = [ self.create_check({ 'name': 'check1', 'tags': {'a', 'b', 'c', 'd'}, 'valid_prog_environs': ['env1', 'env2'], 'valid_systems': ['testsys:gpu', 'testsys2:mc'], 'num_gpus_per_node': 1}), self.create_check({ 'name': 'check2', 'tags': {'x', 'y', 'z'}, 'valid_prog_environs': ['env3'], 'valid_systems': ['testsys:mc', 'testsys2:mc'], 'num_gpus_per_node': 0}), self.create_check({ 'name': 'check3', 'tags': {'a', 'z'}, 'valid_prog_environs': ['env3', 'env4'], 'valid_systems': ['testsys:gpu'], 'num_gpus_per_node': 1}) ] def count_checks(self, filter_fn): return sn.count(filter(filter_fn, self.checks)) def test_have_name(self): assert 1 == self.count_checks(filters.have_name('check1')) assert 3 == self.count_checks(filters.have_name('check')) assert 2 == self.count_checks(filters.have_name(r'\S*1|\S*3')) assert 0 == self.count_checks(filters.have_name('Check')) assert 3 == self.count_checks(filters.have_name('(?i)Check')) assert 2 == self.count_checks(filters.have_name('(?i)check1|CHECK2')) def test_have_not_name(self): assert 2 == self.count_checks(filters.have_not_name('check1')) assert 1 == self.count_checks(filters.have_not_name('check1|check3')) assert 0 == self.count_checks(filters.have_not_name( 'check1|check2|check3')) assert 3 == self.count_checks(filters.have_not_name('Check1')) assert 2 == self.count_checks(filters.have_not_name('(?i)Check1')) def test_have_tags(self): assert 2 == self.count_checks(filters.have_tag('a|c')) assert 0 == self.count_checks(filters.have_tag('p|q')) assert 2 == self.count_checks(filters.have_tag('z')) def test_have_prgenv(self): assert 1 == self.count_checks(filters.have_prgenv('env1|env2')) assert 2 == self.count_checks(filters.have_prgenv('env3')) assert 1 == self.count_checks(filters.have_prgenv('env4')) assert 3 == self.count_checks(filters.have_prgenv('env1|env3')) @rt.switch_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') def test_partition(self): p = fixtures.partition_by_name('gpu') assert 2 == self.count_checks(filters.have_partition([p])) p = fixtures.partition_by_name('login') assert 0 == self.count_checks(filters.have_partition([p])) def test_have_gpu_only(self): assert 2 == self.count_checks(filters.have_gpu_only()) def test_have_cpu_only(self): assert 1 == self.count_checks(filters.have_cpu_only()) def test_invalid_regex(self): # We need to explicitly call `evaluate` to make sure the exception # is triggered in all cases with pytest.raises(ReframeError): self.count_checks(filters.have_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_not_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_tag('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_prgenv('*foo')).evaluate() ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_check_filters.py::TestCheckFilters::test_partition,unittests/test_check_filters.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import pytest import unittest import reframe.core.runtime as rt import reframe.frontend.check_filters as filters import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.pipeline import RegressionTest from reframe.core.exceptions import ReframeError class TestCheckFilters(unittest.TestCase): def create_check(self, attrs): ret = RegressionTest() for k, v in attrs.items(): setattr(ret, k, v) return ret def setUp(self): self.checks = [ self.create_check({ 'name': 'check1', 'tags': {'a', 'b', 'c', 'd'}, 'valid_prog_environs': ['env1', 'env2'], 'valid_systems': ['testsys:gpu', 'testsys2:mc'], 'num_gpus_per_node': 1}), self.create_check({ 'name': 'check2', 'tags': {'x', 'y', 'z'}, 'valid_prog_environs': ['env3'], 'valid_systems': ['testsys:mc', 'testsys2:mc'], 'num_gpus_per_node': 0}), self.create_check({ 'name': 'check3', 'tags': {'a', 'z'}, 'valid_prog_environs': ['env3', 'env4'], 'valid_systems': ['testsys:gpu'], 'num_gpus_per_node': 1}) ] def count_checks(self, filter_fn): return sn.count(filter(filter_fn, self.checks)) def test_have_name(self): assert 1 == self.count_checks(filters.have_name('check1')) assert 3 == self.count_checks(filters.have_name('check')) assert 2 == self.count_checks(filters.have_name(r'\S*1|\S*3')) assert 0 == self.count_checks(filters.have_name('Check')) assert 3 == self.count_checks(filters.have_name('(?i)Check')) assert 2 == self.count_checks(filters.have_name('(?i)check1|CHECK2')) def test_have_not_name(self): assert 2 == self.count_checks(filters.have_not_name('check1')) assert 1 == self.count_checks(filters.have_not_name('check1|check3')) assert 0 == self.count_checks(filters.have_not_name( 'check1|check2|check3')) assert 3 == self.count_checks(filters.have_not_name('Check1')) assert 2 == self.count_checks(filters.have_not_name('(?i)Check1')) def test_have_tags(self): assert 2 == self.count_checks(filters.have_tag('a|c')) assert 0 == self.count_checks(filters.have_tag('p|q')) assert 2 == self.count_checks(filters.have_tag('z')) def test_have_prgenv(self): assert 1 == self.count_checks(filters.have_prgenv('env1|env2')) assert 2 == self.count_checks(filters.have_prgenv('env3')) assert 1 == self.count_checks(filters.have_prgenv('env4')) assert 3 == self.count_checks(filters.have_prgenv('env1|env3')) @rt.switch_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') def test_partition(self): p = fixtures.partition_by_name('gpu') assert 2 == self.count_checks(filters.have_partition([p])) p = fixtures.partition_by_name('login') assert 0 == self.count_checks(filters.have_partition([p])) def test_have_gpu_only(self): assert 2 == self.count_checks(filters.have_gpu_only()) def test_have_cpu_only(self): assert 1 == self.count_checks(filters.have_cpu_only()) def test_invalid_regex(self): # We need to explicitly call `evaluate` to make sure the exception # is triggered in all cases with pytest.raises(ReframeError): self.count_checks(filters.have_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_not_name('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_tag('*foo')).evaluate() with pytest.raises(ReframeError): self.count_checks(filters.have_prgenv('*foo')).evaluate() ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_cli.py::test_check_success,unittests/test_cli.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import itertools import os import pathlib import pytest import re import sys from contextlib import redirect_stdout, redirect_stderr, suppress from io import StringIO import reframe.core.config as config import reframe.core.environments as env import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures def run_command_inline(argv, funct, *args, **kwargs): # Save current execution context argv_save = sys.argv environ_save = env.snapshot() sys.argv = argv exitcode = None captured_stdout = StringIO() captured_stderr = StringIO() print(*sys.argv) with redirect_stdout(captured_stdout): with redirect_stderr(captured_stderr): try: with rt.temp_runtime(None): exitcode = funct(*args, **kwargs) except SystemExit as e: exitcode = e.code finally: # Restore execution context environ_save.restore() sys.argv = argv_save return (exitcode, captured_stdout.getvalue(), captured_stderr.getvalue()) @pytest.fixture def logfile(): path = pathlib.PosixPath('.rfm_unittest.log') yield path with suppress(FileNotFoundError): path.unlink() @pytest.fixture def perflogdir(tmp_path): dirname = tmp_path / '.rfm-perflogs' yield dirname @pytest.fixture def run_reframe(tmp_path, logfile, perflogdir): def _run_reframe(system='generic:default', checkpath=['unittests/resources/checks/hellocheck.py'], environs=['builtin-gcc'], local=True, action='run', more_options=None, mode=None, config_file='unittests/resources/settings.py', logfile=str(logfile), ignore_check_conflicts=True, perflogdir=str(perflogdir)): import reframe.frontend.cli as cli # We always pass the --report-file option, because we don't want to # pollute the user's home directory argv = ['./bin/reframe', '--prefix', str(tmp_path), '--nocolor', f'--report-file={tmp_path / ""report.json""}'] if mode: argv += ['--mode', mode] if system: argv += ['--system', system] if config_file: argv += ['-C', config_file] argv += itertools.chain(*(['-c', c] for c in checkpath)) argv += itertools.chain(*(['-p', e] for e in environs)) if local: argv += ['--force-local'] if action == 'run': argv += ['-r'] elif action == 'list': argv += ['-l'] elif action == 'list_detailed': argv += ['-L'] elif action == 'help': argv += ['-h'] if ignore_check_conflicts: argv += ['--ignore-check-conflicts'] if perflogdir: argv += ['--perflogdir', perflogdir] if more_options: argv += more_options return run_command_inline(argv, cli.main) return _run_reframe @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def user_exec_ctx(temp_runtime): if fixtures.USER_CONFIG_FILE is None: pytest.skip('no user configuration file supplied') yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) @pytest.fixture def remote_exec_ctx(user_exec_ctx): partition = fixtures.partition_by_scheduler() if not partition: pytest.skip('job submission not supported') return partition, partition.environs[0] def test_check_success(run_reframe, tmp_path, logfile): returncode, stdout, _ = run_reframe(more_options=['--save-log-files']) assert 'PASSED' in stdout assert 'FAILED' not in stdout assert returncode == 0 assert os.path.exists(tmp_path / 'output' / logfile) assert os.path.exists(tmp_path / 'report.json') def test_report_file_with_sessionid(run_reframe, tmp_path): returncode, stdout, _ = run_reframe( more_options=[ f'--save-log-files', f'--report-file={tmp_path / ""rfm-report-{sessionid}.json""}' ] ) assert returncode == 0 assert os.path.exists(tmp_path / 'rfm-report-0.json') def test_check_submit_success(run_reframe, remote_exec_ctx): # This test will run on the auto-detected system partition, environ = remote_exec_ctx returncode, stdout, _ = run_reframe( config_file=fixtures.USER_CONFIG_FILE, local=False, system=partition.fullname, # Pick up the programming environment of the partition # Prepend ^ and append $ so as to much exactly the given name environs=[f'^{environ.name}$'] ) assert 'FAILED' not in stdout assert 'PASSED' in stdout # Assert that we have run only one test case assert 'Ran 1 test case(s)' in stdout assert 0 == returncode def test_check_failure(run_reframe): returncode, stdout, _ = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'BadSetupCheck'] ) assert 'FAILED' in stdout assert returncode != 0 def test_check_setup_failure(run_reframe): returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'BadSetupCheckEarly'], local=False, ) assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert 'FAILED' in stdout assert returncode != 0 def test_check_kbd_interrupt(run_reframe): returncode, stdout, stderr = run_reframe( checkpath=[ 'unittests/resources/checks_unlisted/kbd_interrupt.py' ], more_options=['-t', 'KeyboardInterruptCheck'], local=False, ) assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert 'FAILED' in stdout assert returncode != 0 def test_check_sanity_failure(run_reframe, tmp_path): returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'SanityFailureCheck'] ) assert 'FAILED' in stdout # This is a normal failure, it should not raise any exception assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert returncode != 0 assert os.path.exists( tmp_path / 'stage' / 'generic' / 'default' / 'builtin-gcc' / 'SanityFailureCheck' ) def test_dont_restage(run_reframe, tmp_path): run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'SanityFailureCheck'] ) # Place a random file in the test's stage directory and rerun with # `--dont-restage` and `--max-retries` stagedir = (tmp_path / 'stage' / 'generic' / 'default' / 'builtin-gcc' / 'SanityFailureCheck') (stagedir / 'foobar').touch() returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'SanityFailureCheck', '--dont-restage', '--max-retries=1'] ) assert os.path.exists(stagedir / 'foobar') assert not os.path.exists(f'{stagedir}_retry1') # And some standard assertions assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert returncode != 0 def test_checkpath_symlink(run_reframe, tmp_path): # FIXME: This should move to test_loader.py checks_symlink = tmp_path / 'checks_symlink' os.symlink(os.path.abspath('unittests/resources/checks'), checks_symlink) returncode, stdout, _ = run_reframe( action='list', more_options=['-R'], checkpath=['unittests/resources/checks', str(checks_symlink)] ) num_checks_default = re.search( r'Found (\d+) check', stdout, re.MULTILINE).group(1) num_checks_in_checkdir = re.search( r'Found (\d+) check', stdout, re.MULTILINE).group(1) assert num_checks_in_checkdir == num_checks_default def test_performance_check_failure(run_reframe, tmp_path, perflogdir): returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'PerformanceFailureCheck'] ) assert 'FAILED' in stdout # This is a normal failure, it should not raise any exception assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert returncode != 0 assert os.path.exists( tmp_path / 'stage' / 'generic' / 'default' / 'builtin-gcc' / 'PerformanceFailureCheck' ) assert os.path.exists(perflogdir / 'generic' / 'default' / 'PerformanceFailureCheck.log') def test_performance_report(run_reframe): returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'PerformanceFailureCheck', '--performance-report'] ) assert r'PERFORMANCE REPORT' in stdout assert r'perf: 10 Gflop/s' in stdout def test_skip_system_check_option(run_reframe): returncode, stdout, _ = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['--skip-system-check', '-t', 'NoSystemCheck'] ) assert 'PASSED' in stdout assert returncode == 0 def test_skip_prgenv_check_option(run_reframe): re", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_cli.py::test_performance_check_failure,unittests/test_cli.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import itertools import os import pathlib import pytest import re import sys from contextlib import redirect_stdout, redirect_stderr, suppress from io import StringIO import reframe.core.config as config import reframe.core.environments as env import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures def run_command_inline(argv, funct, *args, **kwargs): # Save current execution context argv_save = sys.argv environ_save = env.snapshot() sys.argv = argv exitcode = None captured_stdout = StringIO() captured_stderr = StringIO() print(*sys.argv) with redirect_stdout(captured_stdout): with redirect_stderr(captured_stderr): try: with rt.temp_runtime(None): exitcode = funct(*args, **kwargs) except SystemExit as e: exitcode = e.code finally: # Restore execution context environ_save.restore() sys.argv = argv_save return (exitcode, captured_stdout.getvalue(), captured_stderr.getvalue()) @pytest.fixture def logfile(): path = pathlib.PosixPath('.rfm_unittest.log') yield path with suppress(FileNotFoundError): path.unlink() @pytest.fixture def perflogdir(tmp_path): dirname = tmp_path / '.rfm-perflogs' yield dirname @pytest.fixture def run_reframe(tmp_path, logfile, perflogdir): def _run_reframe(system='generic:default', checkpath=['unittests/resources/checks/hellocheck.py'], environs=['builtin-gcc'], local=True, action='run', more_options=None, mode=None, config_file='unittests/resources/settings.py', logfile=str(logfile), ignore_check_conflicts=True, perflogdir=str(perflogdir)): import reframe.frontend.cli as cli # We always pass the --report-file option, because we don't want to # pollute the user's home directory argv = ['./bin/reframe', '--prefix', str(tmp_path), '--nocolor', f'--report-file={tmp_path / ""report.json""}'] if mode: argv += ['--mode', mode] if system: argv += ['--system', system] if config_file: argv += ['-C', config_file] argv += itertools.chain(*(['-c', c] for c in checkpath)) argv += itertools.chain(*(['-p', e] for e in environs)) if local: argv += ['--force-local'] if action == 'run': argv += ['-r'] elif action == 'list': argv += ['-l'] elif action == 'list_detailed': argv += ['-L'] elif action == 'help': argv += ['-h'] if ignore_check_conflicts: argv += ['--ignore-check-conflicts'] if perflogdir: argv += ['--perflogdir', perflogdir] if more_options: argv += more_options return run_command_inline(argv, cli.main) return _run_reframe @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def user_exec_ctx(temp_runtime): if fixtures.USER_CONFIG_FILE is None: pytest.skip('no user configuration file supplied') yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) @pytest.fixture def remote_exec_ctx(user_exec_ctx): partition = fixtures.partition_by_scheduler() if not partition: pytest.skip('job submission not supported') return partition, partition.environs[0] def test_check_success(run_reframe, tmp_path, logfile): returncode, stdout, _ = run_reframe(more_options=['--save-log-files']) assert 'PASSED' in stdout assert 'FAILED' not in stdout assert returncode == 0 assert os.path.exists(tmp_path / 'output' / logfile) assert os.path.exists(tmp_path / 'report.json') def test_report_file_with_sessionid(run_reframe, tmp_path): returncode, stdout, _ = run_reframe( more_options=[ f'--save-log-files', f'--report-file={tmp_path / ""rfm-report-{sessionid}.json""}' ] ) assert returncode == 0 assert os.path.exists(tmp_path / 'rfm-report-0.json') def test_check_submit_success(run_reframe, remote_exec_ctx): # This test will run on the auto-detected system partition, environ = remote_exec_ctx returncode, stdout, _ = run_reframe( config_file=fixtures.USER_CONFIG_FILE, local=False, system=partition.fullname, # Pick up the programming environment of the partition # Prepend ^ and append $ so as to much exactly the given name environs=[f'^{environ.name}$'] ) assert 'FAILED' not in stdout assert 'PASSED' in stdout # Assert that we have run only one test case assert 'Ran 1 test case(s)' in stdout assert 0 == returncode def test_check_failure(run_reframe): returncode, stdout, _ = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'BadSetupCheck'] ) assert 'FAILED' in stdout assert returncode != 0 def test_check_setup_failure(run_reframe): returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'BadSetupCheckEarly'], local=False, ) assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert 'FAILED' in stdout assert returncode != 0 def test_check_kbd_interrupt(run_reframe): returncode, stdout, stderr = run_reframe( checkpath=[ 'unittests/resources/checks_unlisted/kbd_interrupt.py' ], more_options=['-t', 'KeyboardInterruptCheck'], local=False, ) assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert 'FAILED' in stdout assert returncode != 0 def test_check_sanity_failure(run_reframe, tmp_path): returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'SanityFailureCheck'] ) assert 'FAILED' in stdout # This is a normal failure, it should not raise any exception assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert returncode != 0 assert os.path.exists( tmp_path / 'stage' / 'generic' / 'default' / 'builtin-gcc' / 'SanityFailureCheck' ) def test_dont_restage(run_reframe, tmp_path): run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'SanityFailureCheck'] ) # Place a random file in the test's stage directory and rerun with # `--dont-restage` and `--max-retries` stagedir = (tmp_path / 'stage' / 'generic' / 'default' / 'builtin-gcc' / 'SanityFailureCheck') (stagedir / 'foobar').touch() returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'SanityFailureCheck', '--dont-restage', '--max-retries=1'] ) assert os.path.exists(stagedir / 'foobar') assert not os.path.exists(f'{stagedir}_retry1') # And some standard assertions assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert returncode != 0 def test_checkpath_symlink(run_reframe, tmp_path): # FIXME: This should move to test_loader.py checks_symlink = tmp_path / 'checks_symlink' os.symlink(os.path.abspath('unittests/resources/checks'), checks_symlink) returncode, stdout, _ = run_reframe( action='list', more_options=['-R'], checkpath=['unittests/resources/checks', str(checks_symlink)] ) num_checks_default = re.search( r'Found (\d+) check', stdout, re.MULTILINE).group(1) num_checks_in_checkdir = re.search( r'Found (\d+) check', stdout, re.MULTILINE).group(1) assert num_checks_in_checkdir == num_checks_default def test_performance_check_failure(run_reframe, tmp_path, perflogdir): returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'PerformanceFailureCheck'] ) assert 'FAILED' in stdout # This is a normal failure, it should not raise any exception assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert returncode != 0 assert os.path.exists( tmp_path / 'stage' / 'generic' / 'default' / 'builtin-gcc' / 'PerformanceFailureCheck' ) assert os.path.exists(perflogdir / 'generic' / 'default' / 'PerformanceFailureCheck.log') def test_performance_report(run_reframe): returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'PerformanceFailureCheck', '--performance-report'] ) assert r'PERFORMANCE REPORT' in stdout assert r'perf: 10 Gflop/s' in stdout def test_skip_system_check_option(run_reframe): returncode, stdout, _ = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['--skip-system-check', '-t', 'NoSystemCheck'] ) assert 'PASSED' in stdout assert returncode == 0 def test_skip_prgenv_check_option(run_reframe): re", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_cli.py::test_skip_system_check_option,unittests/test_cli.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import itertools import os import pathlib import pytest import re import sys from contextlib import redirect_stdout, redirect_stderr, suppress from io import StringIO import reframe.core.config as config import reframe.core.environments as env import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures def run_command_inline(argv, funct, *args, **kwargs): # Save current execution context argv_save = sys.argv environ_save = env.snapshot() sys.argv = argv exitcode = None captured_stdout = StringIO() captured_stderr = StringIO() print(*sys.argv) with redirect_stdout(captured_stdout): with redirect_stderr(captured_stderr): try: with rt.temp_runtime(None): exitcode = funct(*args, **kwargs) except SystemExit as e: exitcode = e.code finally: # Restore execution context environ_save.restore() sys.argv = argv_save return (exitcode, captured_stdout.getvalue(), captured_stderr.getvalue()) @pytest.fixture def logfile(): path = pathlib.PosixPath('.rfm_unittest.log') yield path with suppress(FileNotFoundError): path.unlink() @pytest.fixture def perflogdir(tmp_path): dirname = tmp_path / '.rfm-perflogs' yield dirname @pytest.fixture def run_reframe(tmp_path, logfile, perflogdir): def _run_reframe(system='generic:default', checkpath=['unittests/resources/checks/hellocheck.py'], environs=['builtin-gcc'], local=True, action='run', more_options=None, mode=None, config_file='unittests/resources/settings.py', logfile=str(logfile), ignore_check_conflicts=True, perflogdir=str(perflogdir)): import reframe.frontend.cli as cli # We always pass the --report-file option, because we don't want to # pollute the user's home directory argv = ['./bin/reframe', '--prefix', str(tmp_path), '--nocolor', f'--report-file={tmp_path / ""report.json""}'] if mode: argv += ['--mode', mode] if system: argv += ['--system', system] if config_file: argv += ['-C', config_file] argv += itertools.chain(*(['-c', c] for c in checkpath)) argv += itertools.chain(*(['-p', e] for e in environs)) if local: argv += ['--force-local'] if action == 'run': argv += ['-r'] elif action == 'list': argv += ['-l'] elif action == 'list_detailed': argv += ['-L'] elif action == 'help': argv += ['-h'] if ignore_check_conflicts: argv += ['--ignore-check-conflicts'] if perflogdir: argv += ['--perflogdir', perflogdir] if more_options: argv += more_options return run_command_inline(argv, cli.main) return _run_reframe @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def user_exec_ctx(temp_runtime): if fixtures.USER_CONFIG_FILE is None: pytest.skip('no user configuration file supplied') yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) @pytest.fixture def remote_exec_ctx(user_exec_ctx): partition = fixtures.partition_by_scheduler() if not partition: pytest.skip('job submission not supported') return partition, partition.environs[0] def test_check_success(run_reframe, tmp_path, logfile): returncode, stdout, _ = run_reframe(more_options=['--save-log-files']) assert 'PASSED' in stdout assert 'FAILED' not in stdout assert returncode == 0 assert os.path.exists(tmp_path / 'output' / logfile) assert os.path.exists(tmp_path / 'report.json') def test_report_file_with_sessionid(run_reframe, tmp_path): returncode, stdout, _ = run_reframe( more_options=[ f'--save-log-files', f'--report-file={tmp_path / ""rfm-report-{sessionid}.json""}' ] ) assert returncode == 0 assert os.path.exists(tmp_path / 'rfm-report-0.json') def test_check_submit_success(run_reframe, remote_exec_ctx): # This test will run on the auto-detected system partition, environ = remote_exec_ctx returncode, stdout, _ = run_reframe( config_file=fixtures.USER_CONFIG_FILE, local=False, system=partition.fullname, # Pick up the programming environment of the partition # Prepend ^ and append $ so as to much exactly the given name environs=[f'^{environ.name}$'] ) assert 'FAILED' not in stdout assert 'PASSED' in stdout # Assert that we have run only one test case assert 'Ran 1 test case(s)' in stdout assert 0 == returncode def test_check_failure(run_reframe): returncode, stdout, _ = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'BadSetupCheck'] ) assert 'FAILED' in stdout assert returncode != 0 def test_check_setup_failure(run_reframe): returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'BadSetupCheckEarly'], local=False, ) assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert 'FAILED' in stdout assert returncode != 0 def test_check_kbd_interrupt(run_reframe): returncode, stdout, stderr = run_reframe( checkpath=[ 'unittests/resources/checks_unlisted/kbd_interrupt.py' ], more_options=['-t', 'KeyboardInterruptCheck'], local=False, ) assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert 'FAILED' in stdout assert returncode != 0 def test_check_sanity_failure(run_reframe, tmp_path): returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'SanityFailureCheck'] ) assert 'FAILED' in stdout # This is a normal failure, it should not raise any exception assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert returncode != 0 assert os.path.exists( tmp_path / 'stage' / 'generic' / 'default' / 'builtin-gcc' / 'SanityFailureCheck' ) def test_dont_restage(run_reframe, tmp_path): run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'SanityFailureCheck'] ) # Place a random file in the test's stage directory and rerun with # `--dont-restage` and `--max-retries` stagedir = (tmp_path / 'stage' / 'generic' / 'default' / 'builtin-gcc' / 'SanityFailureCheck') (stagedir / 'foobar').touch() returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'SanityFailureCheck', '--dont-restage', '--max-retries=1'] ) assert os.path.exists(stagedir / 'foobar') assert not os.path.exists(f'{stagedir}_retry1') # And some standard assertions assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert returncode != 0 def test_checkpath_symlink(run_reframe, tmp_path): # FIXME: This should move to test_loader.py checks_symlink = tmp_path / 'checks_symlink' os.symlink(os.path.abspath('unittests/resources/checks'), checks_symlink) returncode, stdout, _ = run_reframe( action='list', more_options=['-R'], checkpath=['unittests/resources/checks', str(checks_symlink)] ) num_checks_default = re.search( r'Found (\d+) check', stdout, re.MULTILINE).group(1) num_checks_in_checkdir = re.search( r'Found (\d+) check', stdout, re.MULTILINE).group(1) assert num_checks_in_checkdir == num_checks_default def test_performance_check_failure(run_reframe, tmp_path, perflogdir): returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'PerformanceFailureCheck'] ) assert 'FAILED' in stdout # This is a normal failure, it should not raise any exception assert 'Traceback' not in stdout assert 'Traceback' not in stderr assert returncode != 0 assert os.path.exists( tmp_path / 'stage' / 'generic' / 'default' / 'builtin-gcc' / 'PerformanceFailureCheck' ) assert os.path.exists(perflogdir / 'generic' / 'default' / 'PerformanceFailureCheck.log') def test_performance_report(run_reframe): returncode, stdout, stderr = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['-t', 'PerformanceFailureCheck', '--performance-report'] ) assert r'PERFORMANCE REPORT' in stdout assert r'perf: 10 Gflop/s' in stdout def test_skip_system_check_option(run_reframe): returncode, stdout, _ = run_reframe( checkpath=['unittests/resources/checks/frontend_checks.py'], more_options=['--skip-system-check', '-t', 'NoSystemCheck'] ) assert 'PASSED' in stdout assert returncode == 0 def test_skip_prgenv_check_option(run_reframe): re", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_config.py::test_convert_old_config,unittests/test_config.py,NIO,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import os import pytest import reframe.core.config as config import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (ConfigError, ReframeDeprecationWarning) from reframe.core.systems import System def test_load_config_fallback(monkeypatch): monkeypatch.setattr(config, '_find_config_file', lambda: None) site_config = config.load_config() assert site_config.filename == '' def test_load_config_python(): config.load_config('reframe/core/settings.py') def test_load_config_python_old_syntax(): with pytest.raises(ReframeDeprecationWarning): site_config = config.load_config( 'unittests/resources/settings_old_syntax.py' ) def test_convert_old_config(): converted = config.convert_old_config( 'unittests/resources/settings_old_syntax.py' ) site_config = config.load_config(converted) site_config.validate() assert len(site_config.get('systems')) == 3 assert len(site_config.get('logging')) == 2 site_config.select_subconfig('testsys') assert len(site_config.get('systems/0/partitions')) == 2 assert len(site_config.get('modes')) == 1 assert len(site_config['environments']) == 6 def test_load_config_python_invalid(tmp_path): pyfile = tmp_path / 'settings.py' pyfile.write_text('x = 1\n') with pytest.raises(ConfigError, match=r'not a valid Python configuration file'): config.load_config(pyfile) def test_load_config_json(tmp_path): import reframe.core.settings as settings json_file = tmp_path / 'settings.json' json_file.write_text(json.dumps(settings.site_configuration, indent=4)) site_config = config.load_config(json_file) assert site_config.filename == json_file def test_load_config_json_invalid_syntax(tmp_path): json_file = tmp_path / 'settings.json' json_file.write_text('foo') with pytest.raises(ConfigError, match=r'invalid JSON syntax'): config.load_config(json_file) def test_load_config_unknown_file(tmp_path): with pytest.raises(OSError): config.load_config(tmp_path / 'foo.json') def test_load_config_import_error(): # If the configuration file is relative to ReFrame and ImportError is # raised, which should be wrapped inside ConfigError with pytest.raises(ConfigError, match=r'could not load Python configuration file'): config.load_config('reframe/core/foo.py') def test_load_config_unknown_filetype(tmp_path): import reframe.core.settings as settings json_file = tmp_path / 'foo' json_file.write_text(json.dumps(settings.site_configuration, indent=4)) with pytest.raises(ConfigError, match=r'unknown configuration file type'): config.load_config(json_file) def test_validate_fallback_config(): site_config = config.load_config('reframe/core/settings.py') site_config.validate() def test_validate_unittest_config(): site_config = config.load_config('unittests/resources/settings.py') site_config.validate() def test_validate_config_invalid_syntax(): site_config = config.load_config('reframe/core/settings.py') site_config['systems'][0]['name'] = 123 with pytest.raises(ConfigError, match=r'could not validate configuration file'): site_config.validate() def test_validate_config_duplicate_systems(): site_config = config.load_config('reframe/core/settings.py') site_config['systems'].append(site_config['systems'][0]) with pytest.raises(ConfigError, match=r""system 'generic' already defined""): site_config.validate() def test_validate_config_duplicate_partitions(): site_config = config.load_config('reframe/core/settings.py') site_config['systems'][0]['partitions'].append( site_config['systems'][0]['partitions'][0] ) with pytest.raises(ConfigError, match=r""partition 'default' already defined""): site_config.validate() def test_select_subconfig_autodetect(): site_config = config.load_config('reframe/core/settings.py') site_config.select_subconfig() assert site_config['systems'][0]['name'] == 'generic' def test_select_subconfig_autodetect_failure(): site_config = config.load_config('reframe/core/settings.py') site_config['systems'][0]['hostnames'] = ['$^'] with pytest.raises( ConfigError, match=(r'could not find a configuration entry ' 'for the current system') ): site_config.select_subconfig() def test_select_subconfig_unknown_system(): site_config = config.load_config('reframe/core/settings.py') with pytest.raises( ConfigError, match=(r'could not find a configuration entry ' 'for the requested system') ): site_config.select_subconfig('foo') def test_select_subconfig_unknown_partition(): site_config = config.load_config('reframe/core/settings.py') with pytest.raises( ConfigError, match=(r'could not find a configuration entry ' 'for the requested system/partition') ): site_config.select_subconfig('generic:foo') def test_select_subconfig_no_logging(): site_config = config.load_config('reframe/core/settings.py') site_config['logging'][0]['target_systems'] = ['foo'] with pytest.raises(ConfigError, match=r""section 'logging' not defined""): site_config.select_subconfig() def test_select_subconfig_no_environments(): site_config = config.load_config('reframe/core/settings.py') site_config['environments'][0]['target_systems'] = ['foo'] with pytest.raises(ConfigError, match=r""section 'environments' not defined""): site_config.select_subconfig() def test_select_subconfig_undefined_environment(): site_config = config.load_config('reframe/core/settings.py') site_config['systems'][0]['partitions'][0]['environs'] += ['foo', 'bar'] with pytest.raises( ConfigError, match=r""environments ('foo', 'bar')|('bar', 'foo') are not defined"" ): site_config.select_subconfig() def test_select_subconfig(): site_config = config.load_config('unittests/resources/settings.py') site_config.select_subconfig('testsys') assert len(site_config['systems']) == 1 assert len(site_config['systems'][0]['partitions']) == 2 assert len(site_config['modes']) == 1 assert site_config.get('systems/0/name') == 'testsys' assert site_config.get('systems/0/descr') == 'Fake system for unit tests' assert site_config.get('systems/0/hostnames') == ['testsys'] assert site_config.get('systems/0/prefix') == '.rfm_testing' assert (site_config.get('systems/0/resourcesdir') == '.rfm_testing/resources') assert site_config.get('systems/0/modules') == ['foo/1.0'] assert site_config.get('systems/0/variables') == [['FOO_CMD', 'foobar']] assert site_config.get('systems/0/modules_system') == 'nomod' assert site_config.get('systems/0/outputdir') == '' assert site_config.get('systems/0/stagedir') == '' assert len(site_config.get('systems/0/partitions')) == 2 assert site_config.get('systems/0/partitions/@gpu/max_jobs') == 10 assert site_config.get('modes/0/name') == 'unittest' assert site_config.get('modes/@unittest/name') == 'unittest' assert len(site_config.get('logging/0/handlers')) == 2 assert len(site_config.get('logging/0/handlers_perflog')) == 1 assert site_config.get('logging/0/handlers/0/timestamp') is False assert site_config.get('logging/0/handlers/0/level') == 'debug' assert site_config.get('logging/0/handlers/1/level') == 'info' assert site_config.get('logging/0/handlers/2/level') is None site_config.select_subconfig('testsys:login') assert len(site_config.get('systems/0/partitions')) == 1 assert site_config.get('systems/0/partitions/0/scheduler') == 'local' assert site_config.get('systems/0/partitions/0/launcher') == 'local' assert (site_config.get('systems/0/partitions/0/environs') == ['PrgEnv-cray', 'PrgEnv-gnu', 'builtin-gcc']) assert site_config.get('systems/0/partitions/0/descr') == 'Login nodes' assert site_config.get('systems/0/partitions/0/resources') == [] assert site_config.get('systems/0/partitions/0/access') == [] assert site_config.get('systems/0/partitions/0/container_platforms') == [] assert site_config.get('systems/0/partitions/0/modules') == [] assert site_config.get('systems/0/partitions/0/variables') == [] assert site_config.get('systems/0/partitions/0/max_jobs') == 8 assert len(site_config['environments']) == 6 assert site_config.get('environments/@PrgEnv-gnu/cc') == 'gcc' assert site_config.get('environments/0/cxx') == 'g++' assert site_config.get('environments/@PrgEnv-cray/cc') == 'cc' assert site_config.get('environments/1/cxx') == 'CC' assert (site_config.get('environments/@PrgEnv-cray/modules') == ['PrgEnv-cray']) assert len(site_config.get('general')) == 1 assert site_config.get('general/0/check_search_path') == ['a:b'] site_config.select_subconfig('testsys:gpu') assert site_config.get('systems/0/partitions/@gpu/scheduler') == 'slurm' assert site_config.get('systems/0/partitions/0/launcher') == 'srun' assert (site_config.get('systems/0/partitions/0/environs') == ['PrgEnv-gnu', 'builtin-gcc']) assert site_config.get('systems/0/partitions/0/descr') == 'GPU partition' assert len(site_config.get('systems/0/partitions/0/resources')) == 2 assert (site_config.get('systems/0/partitions/0/resources/@gpu/name') == 'gpu') assert si", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_dependencies.py::test_build_deps_unknown_test,unittests/test_dependencies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import itertools import pytest import reframe as rfm import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.utility as util from reframe.core.environments import Environment from reframe.core.exceptions import DependencyError from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures class Node: '''A node in the test case graph. It's simply a wrapper to a (test_name, partition, environment) tuple that can interact seemlessly with a real test case. It's meant for convenience in unit testing. ''' def __init__(self, cname, pname, ename): self.cname, self.pname, self.ename = cname, pname, ename def __eq__(self, other): if isinstance(other, type(self)): return (self.cname == other.cname and self.pname == other.pname and self.ename == other.ename) if isinstance(other, executors.TestCase): return (self.cname == other.check.name and self.pname == other.partition.fullname and self.ename == other.environ.name) return NotImplemented def __hash__(self): return hash(self.cname) ^ hash(self.pname) ^ hash(self.ename) def __repr__(self): return 'Node(%r, %r, %r)' % (self.cname, self.pname, self.ename) def has_edge(graph, src, dst): return dst in graph[src] def num_deps(graph, cname): return sum(len(deps) for c, deps in graph.items() if c.check.name == cname) def in_degree(graph, node): for v in graph.keys(): if v == node: return v.num_dependents def find_check(name, checks): for c in checks: if c.name == name: return c return None def find_case(cname, ename, cases): for c in cases: if c.check.name == cname and c.environ.name == ename: return c @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'sys0') @pytest.fixture def loader(): return RegressionCheckLoader([ 'unittests/resources/checks_unlisted/deps_simple.py' ]) def test_eq_hash(loader, exec_ctx): cases = executors.generate_testcases(loader.load_all()) case0 = find_case('Test0', 'e0', cases) case1 = find_case('Test0', 'e1', cases) case0_copy = case0.clone() assert case0 == case0_copy assert hash(case0) == hash(case0_copy) assert case1 != case0 assert hash(case1) != hash(case0) def test_build_deps(loader, exec_ctx): checks = loader.load_all() cases = executors.generate_testcases(checks) # Test calling getdep() before having built the graph t = find_check('Test1_exact', checks) with pytest.raises(DependencyError): t.getdep('Test0', 'e0') # Build dependencies and continue testing deps = dependency.build_deps(cases) dependency.validate_deps(deps) # Check DEPEND_FULLY dependencies assert num_deps(deps, 'Test1_fully') == 8 for p in ['sys0:p0', 'sys0:p1']: for e0 in ['e0', 'e1']: for e1 in ['e0', 'e1']: assert has_edge(deps, Node('Test1_fully', p, e0), Node('Test0', p, e1)) # Check DEPEND_BY_ENV assert num_deps(deps, 'Test1_by_env') == 4 assert num_deps(deps, 'Test1_default') == 4 for p in ['sys0:p0', 'sys0:p1']: for e in ['e0', 'e1']: assert has_edge(deps, Node('Test1_by_env', p, e), Node('Test0', p, e)) assert has_edge(deps, Node('Test1_default', p, e), Node('Test0', p, e)) # Check DEPEND_EXACT assert num_deps(deps, 'Test1_exact') == 6 for p in ['sys0:p0', 'sys0:p1']: assert has_edge(deps, Node('Test1_exact', p, 'e0'), Node('Test0', p, 'e0')) assert has_edge(deps, Node('Test1_exact', p, 'e0'), Node('Test0', p, 'e1')) assert has_edge(deps, Node('Test1_exact', p, 'e1'), Node('Test0', p, 'e1')) # Check in-degree of Test0 # 2 from Test1_fully, # 1 from Test1_by_env, # 1 from Test1_exact, # 1 from Test1_default assert in_degree(deps, Node('Test0', 'sys0:p0', 'e0')) == 5 assert in_degree(deps, Node('Test0', 'sys0:p1', 'e0')) == 5 # 2 from Test1_fully, # 1 from Test1_by_env, # 2 from Test1_exact, # 1 from Test1_default assert in_degree(deps, Node('Test0', 'sys0:p0', 'e1')) == 6 assert in_degree(deps, Node('Test0', 'sys0:p1', 'e1')) == 6 # Pick a check to test getdep() check_e0 = find_case('Test1_exact', 'e0', cases).check check_e1 = find_case('Test1_exact', 'e1', cases).check with pytest.raises(DependencyError): check_e0.getdep('Test0') # Set the current environment check_e0._current_environ = Environment('e0') check_e1._current_environ = Environment('e1') assert check_e0.getdep('Test0', 'e0').name == 'Test0' assert check_e0.getdep('Test0', 'e1').name == 'Test0' assert check_e1.getdep('Test0', 'e1').name == 'Test0' with pytest.raises(DependencyError): check_e0.getdep('TestX', 'e0') with pytest.raises(DependencyError): check_e0.getdep('Test0', 'eX') with pytest.raises(DependencyError): check_e1.getdep('Test0', 'e0') def test_build_deps_unknown_test(loader, exec_ctx): checks = loader.load_all() # Add some inexistent dependencies test0 = find_check('Test0', checks) for depkind in ('default', 'fully', 'by_env', 'exact'): test1 = find_check('Test1_' + depkind, checks) if depkind == 'default': test1.depends_on('TestX') elif depkind == 'exact': test1.depends_on('TestX', rfm.DEPEND_EXACT, {'e0': ['e0']}) elif depkind == 'fully': test1.depends_on('TestX', rfm.DEPEND_FULLY) elif depkind == 'by_env': test1.depends_on('TestX', rfm.DEPEND_BY_ENV) with pytest.raises(DependencyError): dependency.build_deps(executors.generate_testcases(checks)) def test_build_deps_unknown_target_env(loader, exec_ctx): checks = loader.load_all() # Add some inexistent dependencies test0 = find_check('Test0', checks) test1 = find_check('Test1_default', checks) test1.depends_on('Test0', rfm.DEPEND_EXACT, {'e0': ['eX']}) with pytest.raises(DependencyError): dependency.build_deps(executors.generate_testcases(checks)) def test_build_deps_unknown_source_env(loader, exec_ctx): checks = loader.load_all() # Add some inexistent dependencies test0 = find_check('Test0', checks) test1 = find_check('Test1_default', checks) test1.depends_on('Test0', rfm.DEPEND_EXACT, {'eX': ['e0']}) # Unknown source is ignored, because it might simply be that the test # is not executed for eX deps = dependency.build_deps(executors.generate_testcases(checks)) assert num_deps(deps, 'Test1_default') == 4 def test_build_deps_empty(exec_ctx): assert {} == dependency.build_deps([]) @pytest.fixture def make_test(): class MyTest(rfm.RegressionTest): def __init__(self, name): self.name = name self.valid_systems = ['*'] self.valid_prog_environs = ['*'] self.executable = 'echo' self.executable_opts = [name] def _make_test(name): return MyTest(name) return _make_test def test_valid_deps(make_test, exec_ctx): # # t0 +-->t5<--+ # ^ | | # | | | # +-->t1<--+ t6 t7 # | | ^ # t2<------t3 | # ^ ^ | # | | t8 # +---t4---+ # t0 = make_test('t0') t1 = make_test('t1') t2 = make_test('t2') t3 = make_test('t3') t4 = make_test('t4') t5 = make_test('t5') t6 = make_test('t6') t7 = make_test('t7') t8 = make_test('t8') t1.depends_on('t0') t2.depends_on('t1') t3.depends_on('t1') t3.depends_on('t2') t4.depends_on('t2') t4.depends_on('t3') t6.depends_on('t5') t7.depends_on('t5') t8.depends_on('t7') dependency.validate_deps( dependency.build_deps( executors.generate_testcases([t0, t1, t2, t3, t4, t5, t6, t7, t8]) ) ) def test_cyclic_deps(make_test, exec_ctx): # # t0 +-->t5<--+ # ^ | | # | | | # +-->t1<--+ t6 t7 # | | | ^ # t2 | t3 | # ^ | ^ | # | v | t8 # +---t4---+ # t0 = make_test('t0') t1 = make_test('t1') t2 = make_test('t2') t3 = make_test('t3') t4 = make_test('t4') t5 = make_test('t5') t6 = make_test('t6') t7 = make_test('t7') t8 = make_test('t8') t1.depends_on('t0') t1.depends_on('t4') t2.depends_on('t1') t3.depends_on('t1') t4.depends_on('t2') t4.depends_on('t3') t6.depends_on('t5') t7.depends_on('t5') t8.depends_on('t7') deps = dependency.build_deps( executors.generate_testca", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_dependencies.py::test_toposort_subgraph,unittests/test_dependencies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import itertools import pytest import reframe as rfm import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.utility as util from reframe.core.environments import Environment from reframe.core.exceptions import DependencyError from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures class Node: '''A node in the test case graph. It's simply a wrapper to a (test_name, partition, environment) tuple that can interact seemlessly with a real test case. It's meant for convenience in unit testing. ''' def __init__(self, cname, pname, ename): self.cname, self.pname, self.ename = cname, pname, ename def __eq__(self, other): if isinstance(other, type(self)): return (self.cname == other.cname and self.pname == other.pname and self.ename == other.ename) if isinstance(other, executors.TestCase): return (self.cname == other.check.name and self.pname == other.partition.fullname and self.ename == other.environ.name) return NotImplemented def __hash__(self): return hash(self.cname) ^ hash(self.pname) ^ hash(self.ename) def __repr__(self): return 'Node(%r, %r, %r)' % (self.cname, self.pname, self.ename) def has_edge(graph, src, dst): return dst in graph[src] def num_deps(graph, cname): return sum(len(deps) for c, deps in graph.items() if c.check.name == cname) def in_degree(graph, node): for v in graph.keys(): if v == node: return v.num_dependents def find_check(name, checks): for c in checks: if c.name == name: return c return None def find_case(cname, ename, cases): for c in cases: if c.check.name == cname and c.environ.name == ename: return c @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'sys0') @pytest.fixture def loader(): return RegressionCheckLoader([ 'unittests/resources/checks_unlisted/deps_simple.py' ]) def test_eq_hash(loader, exec_ctx): cases = executors.generate_testcases(loader.load_all()) case0 = find_case('Test0', 'e0', cases) case1 = find_case('Test0', 'e1', cases) case0_copy = case0.clone() assert case0 == case0_copy assert hash(case0) == hash(case0_copy) assert case1 != case0 assert hash(case1) != hash(case0) def test_build_deps(loader, exec_ctx): checks = loader.load_all() cases = executors.generate_testcases(checks) # Test calling getdep() before having built the graph t = find_check('Test1_exact', checks) with pytest.raises(DependencyError): t.getdep('Test0', 'e0') # Build dependencies and continue testing deps = dependency.build_deps(cases) dependency.validate_deps(deps) # Check DEPEND_FULLY dependencies assert num_deps(deps, 'Test1_fully') == 8 for p in ['sys0:p0', 'sys0:p1']: for e0 in ['e0', 'e1']: for e1 in ['e0', 'e1']: assert has_edge(deps, Node('Test1_fully', p, e0), Node('Test0', p, e1)) # Check DEPEND_BY_ENV assert num_deps(deps, 'Test1_by_env') == 4 assert num_deps(deps, 'Test1_default') == 4 for p in ['sys0:p0', 'sys0:p1']: for e in ['e0', 'e1']: assert has_edge(deps, Node('Test1_by_env', p, e), Node('Test0', p, e)) assert has_edge(deps, Node('Test1_default', p, e), Node('Test0', p, e)) # Check DEPEND_EXACT assert num_deps(deps, 'Test1_exact') == 6 for p in ['sys0:p0', 'sys0:p1']: assert has_edge(deps, Node('Test1_exact', p, 'e0'), Node('Test0', p, 'e0')) assert has_edge(deps, Node('Test1_exact', p, 'e0'), Node('Test0', p, 'e1')) assert has_edge(deps, Node('Test1_exact', p, 'e1'), Node('Test0', p, 'e1')) # Check in-degree of Test0 # 2 from Test1_fully, # 1 from Test1_by_env, # 1 from Test1_exact, # 1 from Test1_default assert in_degree(deps, Node('Test0', 'sys0:p0', 'e0')) == 5 assert in_degree(deps, Node('Test0', 'sys0:p1', 'e0')) == 5 # 2 from Test1_fully, # 1 from Test1_by_env, # 2 from Test1_exact, # 1 from Test1_default assert in_degree(deps, Node('Test0', 'sys0:p0', 'e1')) == 6 assert in_degree(deps, Node('Test0', 'sys0:p1', 'e1')) == 6 # Pick a check to test getdep() check_e0 = find_case('Test1_exact', 'e0', cases).check check_e1 = find_case('Test1_exact', 'e1', cases).check with pytest.raises(DependencyError): check_e0.getdep('Test0') # Set the current environment check_e0._current_environ = Environment('e0') check_e1._current_environ = Environment('e1') assert check_e0.getdep('Test0', 'e0').name == 'Test0' assert check_e0.getdep('Test0', 'e1').name == 'Test0' assert check_e1.getdep('Test0', 'e1').name == 'Test0' with pytest.raises(DependencyError): check_e0.getdep('TestX', 'e0') with pytest.raises(DependencyError): check_e0.getdep('Test0', 'eX') with pytest.raises(DependencyError): check_e1.getdep('Test0', 'e0') def test_build_deps_unknown_test(loader, exec_ctx): checks = loader.load_all() # Add some inexistent dependencies test0 = find_check('Test0', checks) for depkind in ('default', 'fully', 'by_env', 'exact'): test1 = find_check('Test1_' + depkind, checks) if depkind == 'default': test1.depends_on('TestX') elif depkind == 'exact': test1.depends_on('TestX', rfm.DEPEND_EXACT, {'e0': ['e0']}) elif depkind == 'fully': test1.depends_on('TestX', rfm.DEPEND_FULLY) elif depkind == 'by_env': test1.depends_on('TestX', rfm.DEPEND_BY_ENV) with pytest.raises(DependencyError): dependency.build_deps(executors.generate_testcases(checks)) def test_build_deps_unknown_target_env(loader, exec_ctx): checks = loader.load_all() # Add some inexistent dependencies test0 = find_check('Test0', checks) test1 = find_check('Test1_default', checks) test1.depends_on('Test0', rfm.DEPEND_EXACT, {'e0': ['eX']}) with pytest.raises(DependencyError): dependency.build_deps(executors.generate_testcases(checks)) def test_build_deps_unknown_source_env(loader, exec_ctx): checks = loader.load_all() # Add some inexistent dependencies test0 = find_check('Test0', checks) test1 = find_check('Test1_default', checks) test1.depends_on('Test0', rfm.DEPEND_EXACT, {'eX': ['e0']}) # Unknown source is ignored, because it might simply be that the test # is not executed for eX deps = dependency.build_deps(executors.generate_testcases(checks)) assert num_deps(deps, 'Test1_default') == 4 def test_build_deps_empty(exec_ctx): assert {} == dependency.build_deps([]) @pytest.fixture def make_test(): class MyTest(rfm.RegressionTest): def __init__(self, name): self.name = name self.valid_systems = ['*'] self.valid_prog_environs = ['*'] self.executable = 'echo' self.executable_opts = [name] def _make_test(name): return MyTest(name) return _make_test def test_valid_deps(make_test, exec_ctx): # # t0 +-->t5<--+ # ^ | | # | | | # +-->t1<--+ t6 t7 # | | ^ # t2<------t3 | # ^ ^ | # | | t8 # +---t4---+ # t0 = make_test('t0') t1 = make_test('t1') t2 = make_test('t2') t3 = make_test('t3') t4 = make_test('t4') t5 = make_test('t5') t6 = make_test('t6') t7 = make_test('t7') t8 = make_test('t8') t1.depends_on('t0') t2.depends_on('t1') t3.depends_on('t1') t3.depends_on('t2') t4.depends_on('t2') t4.depends_on('t3') t6.depends_on('t5') t7.depends_on('t5') t8.depends_on('t7') dependency.validate_deps( dependency.build_deps( executors.generate_testcases([t0, t1, t2, t3, t4, t5, t6, t7, t8]) ) ) def test_cyclic_deps(make_test, exec_ctx): # # t0 +-->t5<--+ # ^ | | # | | | # +-->t1<--+ t6 t7 # | | | ^ # t2 | t3 | # ^ | ^ | # | v | t8 # +---t4---+ # t0 = make_test('t0') t1 = make_test('t1') t2 = make_test('t2') t3 = make_test('t3') t4 = make_test('t4') t5 = make_test('t5') t6 = make_test('t6') t7 = make_test('t7') t8 = make_test('t8') t1.depends_on('t0') t1.depends_on('t4') t2.depends_on('t1') t3.depends_on('t1') t4.depends_on('t2') t4.depends_on('t3') t6.depends_on('t5') t7.depends_on('t5') t8.depends_on('t7') deps = dependency.build_deps( executors.generate_testca", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_environments.py::TestEnvironment::test_environ_snapshot,unittests/test_environments.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe.core.environments as env import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.exceptions import EnvironError class TestEnvironment(unittest.TestCase): def assertModulesLoaded(self, modules): for m in modules: assert self.modules_system.is_module_loaded(m) def assertModulesNotLoaded(self, modules): for m in modules: assert not self.modules_system.is_module_loaded(m) def setup_modules_system(self): if not fixtures.has_sane_modules_system(): pytest.skip('no modules system configured') self.modules_system = rt.runtime().modules_system self.modules_system.searchpath_add(fixtures.TEST_MODULES) # Always add a base module; this is a workaround for the modules # environment's inconsistent behaviour, that starts with an empty # LOADEDMODULES variable and ends up removing it completely if all # present modules are removed. self.modules_system.load_module('testmod_base') def setUp(self): self.modules_system = None os.environ['_var0'] = 'val0' os.environ['_var1'] = 'val1' self.environ_save = env.snapshot() self.environ = env.Environment(name='TestEnv1', modules=['testmod_foo'], variables=[('_var0', 'val1'), ('_var2', '$_var0'), ('_var3', '${_var1}')]) self.environ_other = env.Environment(name='TestEnv2', modules=['testmod_boo'], variables={'_var4': 'val4'}) def tearDown(self): if self.modules_system is not None: self.modules_system.searchpath_remove(fixtures.TEST_MODULES) self.environ_save.restore() def test_setup(self): if fixtures.has_sane_modules_system(): assert len(self.environ.modules) == 1 assert 'testmod_foo' in self.environ.modules assert len(self.environ.variables.keys()) == 3 assert self.environ.variables['_var0'] == 'val1' # No variable expansion, if environment is not loaded self.environ.variables['_var2'] == '$_var0' self.environ.variables['_var3'] == '${_var1}' def test_environ_snapshot(self): rt.loadenv(self.environ, self.environ_other) self.environ_save.restore() assert self.environ_save == env.snapshot() assert not rt.is_env_loaded(self.environ) assert not rt.is_env_loaded(self.environ_other) def test_load_restore(self): snapshot, _ = rt.loadenv(self.environ) os.environ['_var0'] == 'val1' os.environ['_var1'] == 'val1' os.environ['_var2'] == 'val1' os.environ['_var3'] == 'val1' if fixtures.has_sane_modules_system(): self.assertModulesLoaded(self.environ.modules) assert rt.is_env_loaded(self.environ) snapshot.restore() self.environ_save == env.snapshot() os.environ['_var0'], 'val0' if fixtures.has_sane_modules_system(): assert not self.modules_system.is_module_loaded('testmod_foo') assert not rt.is_env_loaded(self.environ) @fixtures.switch_to_user_runtime def test_temp_environment(self): self.setup_modules_system() with rt.temp_environment( ['testmod_foo'], {'_var0': 'val2', '_var3': 'val3'} ) as environ: assert rt.is_env_loaded(environ) assert not rt.is_env_loaded(environ) @fixtures.switch_to_user_runtime def test_load_already_present(self): self.setup_modules_system() self.modules_system.load_module('testmod_boo') snapshot, _ = rt.loadenv(self.environ) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_boo') def test_load_non_overlapping(self): e0 = env.Environment(name='e0', variables=[('a', '1'), ('b', '2')]) e1 = env.Environment(name='e1', variables=[('c', '3'), ('d', '4')]) rt.loadenv(e0, e1) assert rt.is_env_loaded(e0) assert rt.is_env_loaded(e1) def test_load_overlapping(self): e0 = env.Environment(name='e0', variables=[('a', '1'), ('b', '2')]) e1 = env.Environment(name='e1', variables=[('b', '3'), ('c', '4')]) rt.loadenv(e0, e1) assert not rt.is_env_loaded(e0) assert rt.is_env_loaded(e1) def test_equal(self): env1 = env.Environment('env1', modules=['foo', 'bar']) env2 = env.Environment('env1', modules=['bar', 'foo']) assert env1 == env2 assert env2 == env1 def test_not_equal(self): env1 = env.Environment('env1', modules=['foo', 'bar']) env2 = env.Environment('env2', modules=['foo', 'bar']) assert env1 != env2 # Variables are ordered, because they might depend on each other env1 = env.Environment('env1', variables=[('a', 1), ('b', 2)]) env2 = env.Environment('env1', variables=[('b', 2), ('a', 1)]) assert env1 != env2 @fixtures.switch_to_user_runtime def test_conflicting_environments(self): self.setup_modules_system() envfoo = env.Environment(name='envfoo', modules=['testmod_foo', 'testmod_boo']) envbar = env.Environment(name='envbar', modules=['testmod_bar']) rt.loadenv(envfoo, envbar) for m in envbar.modules: assert self.modules_system.is_module_loaded(m) for m in envfoo.modules: assert not self.modules_system.is_module_loaded(m) @fixtures.switch_to_user_runtime def test_conflict_environ_after_module_load(self): self.setup_modules_system() self.modules_system.load_module('testmod_foo') envfoo = env.Environment(name='envfoo', modules=['testmod_foo']) snapshot, _ = rt.loadenv(envfoo) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_foo') @fixtures.switch_to_user_runtime def test_conflict_environ_after_module_force_load(self): self.setup_modules_system() self.modules_system.load_module('testmod_foo') envbar = env.Environment(name='envbar', modules=['testmod_bar']) snapshot, _ = rt.loadenv(envbar) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_foo') def test_immutability(self): # Check emit_load_commands() _, commands = rt.loadenv(self.environ) # Try to modify the returned list of commands commands.append('foo') assert 'foo' not in rt.loadenv(self.environ)[1] # Test ProgEnvironment prgenv = env.ProgEnvironment('foo_prgenv') assert isinstance(prgenv, env.Environment) with pytest.raises(AttributeError): prgenv.cc = 'gcc' with pytest.raises(AttributeError): prgenv.cxx = 'g++' with pytest.raises(AttributeError): prgenv.ftn = 'gfortran' with pytest.raises(AttributeError): prgenv.nvcc = 'clang' with pytest.raises(AttributeError): prgenv.cppflags = ['-DFOO'] with pytest.raises(AttributeError): prgenv.cflags = ['-O1'] with pytest.raises(AttributeError): prgenv.cxxflags = ['-O1'] with pytest.raises(AttributeError): prgenv.fflags = ['-O1'] with pytest.raises(AttributeError): prgenv.ldflags = ['-lm'] @fixtures.switch_to_user_runtime def test_emit_load_commands(self): self.setup_modules_system() ms = rt.runtime().modules_system expected_commands = [ ms.emit_load_commands('testmod_foo')[0], 'export _var0=val1', 'export _var2=$_var0', 'export _var3=${_var1}', ] assert expected_commands == rt.emit_loadenv_commands(self.environ) @fixtures.switch_to_user_runtime def test_emit_load_commands_with_confict(self): self.setup_modules_system() # Load a conflicting module self.modules_system.load_module('testmod_bar') ms = rt.runtime().modules_system expected_commands = [ ms.emit_unload_commands('testmod_bar')[0], ms.emit_load_commands('testmod_foo')[0], 'export _var0=val1', 'export _var2=$_var0', 'export _var3=${_var1}', ] assert expected_commands == rt.emit_loadenv_commands(self.environ) ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_environments.py::TestEnvironment::test_immutability,unittests/test_environments.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe.core.environments as env import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.exceptions import EnvironError class TestEnvironment(unittest.TestCase): def assertModulesLoaded(self, modules): for m in modules: assert self.modules_system.is_module_loaded(m) def assertModulesNotLoaded(self, modules): for m in modules: assert not self.modules_system.is_module_loaded(m) def setup_modules_system(self): if not fixtures.has_sane_modules_system(): pytest.skip('no modules system configured') self.modules_system = rt.runtime().modules_system self.modules_system.searchpath_add(fixtures.TEST_MODULES) # Always add a base module; this is a workaround for the modules # environment's inconsistent behaviour, that starts with an empty # LOADEDMODULES variable and ends up removing it completely if all # present modules are removed. self.modules_system.load_module('testmod_base') def setUp(self): self.modules_system = None os.environ['_var0'] = 'val0' os.environ['_var1'] = 'val1' self.environ_save = env.snapshot() self.environ = env.Environment(name='TestEnv1', modules=['testmod_foo'], variables=[('_var0', 'val1'), ('_var2', '$_var0'), ('_var3', '${_var1}')]) self.environ_other = env.Environment(name='TestEnv2', modules=['testmod_boo'], variables={'_var4': 'val4'}) def tearDown(self): if self.modules_system is not None: self.modules_system.searchpath_remove(fixtures.TEST_MODULES) self.environ_save.restore() def test_setup(self): if fixtures.has_sane_modules_system(): assert len(self.environ.modules) == 1 assert 'testmod_foo' in self.environ.modules assert len(self.environ.variables.keys()) == 3 assert self.environ.variables['_var0'] == 'val1' # No variable expansion, if environment is not loaded self.environ.variables['_var2'] == '$_var0' self.environ.variables['_var3'] == '${_var1}' def test_environ_snapshot(self): rt.loadenv(self.environ, self.environ_other) self.environ_save.restore() assert self.environ_save == env.snapshot() assert not rt.is_env_loaded(self.environ) assert not rt.is_env_loaded(self.environ_other) def test_load_restore(self): snapshot, _ = rt.loadenv(self.environ) os.environ['_var0'] == 'val1' os.environ['_var1'] == 'val1' os.environ['_var2'] == 'val1' os.environ['_var3'] == 'val1' if fixtures.has_sane_modules_system(): self.assertModulesLoaded(self.environ.modules) assert rt.is_env_loaded(self.environ) snapshot.restore() self.environ_save == env.snapshot() os.environ['_var0'], 'val0' if fixtures.has_sane_modules_system(): assert not self.modules_system.is_module_loaded('testmod_foo') assert not rt.is_env_loaded(self.environ) @fixtures.switch_to_user_runtime def test_temp_environment(self): self.setup_modules_system() with rt.temp_environment( ['testmod_foo'], {'_var0': 'val2', '_var3': 'val3'} ) as environ: assert rt.is_env_loaded(environ) assert not rt.is_env_loaded(environ) @fixtures.switch_to_user_runtime def test_load_already_present(self): self.setup_modules_system() self.modules_system.load_module('testmod_boo') snapshot, _ = rt.loadenv(self.environ) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_boo') def test_load_non_overlapping(self): e0 = env.Environment(name='e0', variables=[('a', '1'), ('b', '2')]) e1 = env.Environment(name='e1', variables=[('c', '3'), ('d', '4')]) rt.loadenv(e0, e1) assert rt.is_env_loaded(e0) assert rt.is_env_loaded(e1) def test_load_overlapping(self): e0 = env.Environment(name='e0', variables=[('a', '1'), ('b', '2')]) e1 = env.Environment(name='e1', variables=[('b', '3'), ('c', '4')]) rt.loadenv(e0, e1) assert not rt.is_env_loaded(e0) assert rt.is_env_loaded(e1) def test_equal(self): env1 = env.Environment('env1', modules=['foo', 'bar']) env2 = env.Environment('env1', modules=['bar', 'foo']) assert env1 == env2 assert env2 == env1 def test_not_equal(self): env1 = env.Environment('env1', modules=['foo', 'bar']) env2 = env.Environment('env2', modules=['foo', 'bar']) assert env1 != env2 # Variables are ordered, because they might depend on each other env1 = env.Environment('env1', variables=[('a', 1), ('b', 2)]) env2 = env.Environment('env1', variables=[('b', 2), ('a', 1)]) assert env1 != env2 @fixtures.switch_to_user_runtime def test_conflicting_environments(self): self.setup_modules_system() envfoo = env.Environment(name='envfoo', modules=['testmod_foo', 'testmod_boo']) envbar = env.Environment(name='envbar', modules=['testmod_bar']) rt.loadenv(envfoo, envbar) for m in envbar.modules: assert self.modules_system.is_module_loaded(m) for m in envfoo.modules: assert not self.modules_system.is_module_loaded(m) @fixtures.switch_to_user_runtime def test_conflict_environ_after_module_load(self): self.setup_modules_system() self.modules_system.load_module('testmod_foo') envfoo = env.Environment(name='envfoo', modules=['testmod_foo']) snapshot, _ = rt.loadenv(envfoo) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_foo') @fixtures.switch_to_user_runtime def test_conflict_environ_after_module_force_load(self): self.setup_modules_system() self.modules_system.load_module('testmod_foo') envbar = env.Environment(name='envbar', modules=['testmod_bar']) snapshot, _ = rt.loadenv(envbar) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_foo') def test_immutability(self): # Check emit_load_commands() _, commands = rt.loadenv(self.environ) # Try to modify the returned list of commands commands.append('foo') assert 'foo' not in rt.loadenv(self.environ)[1] # Test ProgEnvironment prgenv = env.ProgEnvironment('foo_prgenv') assert isinstance(prgenv, env.Environment) with pytest.raises(AttributeError): prgenv.cc = 'gcc' with pytest.raises(AttributeError): prgenv.cxx = 'g++' with pytest.raises(AttributeError): prgenv.ftn = 'gfortran' with pytest.raises(AttributeError): prgenv.nvcc = 'clang' with pytest.raises(AttributeError): prgenv.cppflags = ['-DFOO'] with pytest.raises(AttributeError): prgenv.cflags = ['-O1'] with pytest.raises(AttributeError): prgenv.cxxflags = ['-O1'] with pytest.raises(AttributeError): prgenv.fflags = ['-O1'] with pytest.raises(AttributeError): prgenv.ldflags = ['-lm'] @fixtures.switch_to_user_runtime def test_emit_load_commands(self): self.setup_modules_system() ms = rt.runtime().modules_system expected_commands = [ ms.emit_load_commands('testmod_foo')[0], 'export _var0=val1', 'export _var2=$_var0', 'export _var3=${_var1}', ] assert expected_commands == rt.emit_loadenv_commands(self.environ) @fixtures.switch_to_user_runtime def test_emit_load_commands_with_confict(self): self.setup_modules_system() # Load a conflicting module self.modules_system.load_module('testmod_bar') ms = rt.runtime().modules_system expected_commands = [ ms.emit_unload_commands('testmod_bar')[0], ms.emit_load_commands('testmod_foo')[0], 'export _var0=val1', 'export _var2=$_var0', 'export _var3=${_var1}', ] assert expected_commands == rt.emit_loadenv_commands(self.environ) ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_environments.py::TestEnvironment::test_load_non_overlapping,unittests/test_environments.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe.core.environments as env import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.exceptions import EnvironError class TestEnvironment(unittest.TestCase): def assertModulesLoaded(self, modules): for m in modules: assert self.modules_system.is_module_loaded(m) def assertModulesNotLoaded(self, modules): for m in modules: assert not self.modules_system.is_module_loaded(m) def setup_modules_system(self): if not fixtures.has_sane_modules_system(): pytest.skip('no modules system configured') self.modules_system = rt.runtime().modules_system self.modules_system.searchpath_add(fixtures.TEST_MODULES) # Always add a base module; this is a workaround for the modules # environment's inconsistent behaviour, that starts with an empty # LOADEDMODULES variable and ends up removing it completely if all # present modules are removed. self.modules_system.load_module('testmod_base') def setUp(self): self.modules_system = None os.environ['_var0'] = 'val0' os.environ['_var1'] = 'val1' self.environ_save = env.snapshot() self.environ = env.Environment(name='TestEnv1', modules=['testmod_foo'], variables=[('_var0', 'val1'), ('_var2', '$_var0'), ('_var3', '${_var1}')]) self.environ_other = env.Environment(name='TestEnv2', modules=['testmod_boo'], variables={'_var4': 'val4'}) def tearDown(self): if self.modules_system is not None: self.modules_system.searchpath_remove(fixtures.TEST_MODULES) self.environ_save.restore() def test_setup(self): if fixtures.has_sane_modules_system(): assert len(self.environ.modules) == 1 assert 'testmod_foo' in self.environ.modules assert len(self.environ.variables.keys()) == 3 assert self.environ.variables['_var0'] == 'val1' # No variable expansion, if environment is not loaded self.environ.variables['_var2'] == '$_var0' self.environ.variables['_var3'] == '${_var1}' def test_environ_snapshot(self): rt.loadenv(self.environ, self.environ_other) self.environ_save.restore() assert self.environ_save == env.snapshot() assert not rt.is_env_loaded(self.environ) assert not rt.is_env_loaded(self.environ_other) def test_load_restore(self): snapshot, _ = rt.loadenv(self.environ) os.environ['_var0'] == 'val1' os.environ['_var1'] == 'val1' os.environ['_var2'] == 'val1' os.environ['_var3'] == 'val1' if fixtures.has_sane_modules_system(): self.assertModulesLoaded(self.environ.modules) assert rt.is_env_loaded(self.environ) snapshot.restore() self.environ_save == env.snapshot() os.environ['_var0'], 'val0' if fixtures.has_sane_modules_system(): assert not self.modules_system.is_module_loaded('testmod_foo') assert not rt.is_env_loaded(self.environ) @fixtures.switch_to_user_runtime def test_temp_environment(self): self.setup_modules_system() with rt.temp_environment( ['testmod_foo'], {'_var0': 'val2', '_var3': 'val3'} ) as environ: assert rt.is_env_loaded(environ) assert not rt.is_env_loaded(environ) @fixtures.switch_to_user_runtime def test_load_already_present(self): self.setup_modules_system() self.modules_system.load_module('testmod_boo') snapshot, _ = rt.loadenv(self.environ) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_boo') def test_load_non_overlapping(self): e0 = env.Environment(name='e0', variables=[('a', '1'), ('b', '2')]) e1 = env.Environment(name='e1', variables=[('c', '3'), ('d', '4')]) rt.loadenv(e0, e1) assert rt.is_env_loaded(e0) assert rt.is_env_loaded(e1) def test_load_overlapping(self): e0 = env.Environment(name='e0', variables=[('a', '1'), ('b', '2')]) e1 = env.Environment(name='e1', variables=[('b', '3'), ('c', '4')]) rt.loadenv(e0, e1) assert not rt.is_env_loaded(e0) assert rt.is_env_loaded(e1) def test_equal(self): env1 = env.Environment('env1', modules=['foo', 'bar']) env2 = env.Environment('env1', modules=['bar', 'foo']) assert env1 == env2 assert env2 == env1 def test_not_equal(self): env1 = env.Environment('env1', modules=['foo', 'bar']) env2 = env.Environment('env2', modules=['foo', 'bar']) assert env1 != env2 # Variables are ordered, because they might depend on each other env1 = env.Environment('env1', variables=[('a', 1), ('b', 2)]) env2 = env.Environment('env1', variables=[('b', 2), ('a', 1)]) assert env1 != env2 @fixtures.switch_to_user_runtime def test_conflicting_environments(self): self.setup_modules_system() envfoo = env.Environment(name='envfoo', modules=['testmod_foo', 'testmod_boo']) envbar = env.Environment(name='envbar', modules=['testmod_bar']) rt.loadenv(envfoo, envbar) for m in envbar.modules: assert self.modules_system.is_module_loaded(m) for m in envfoo.modules: assert not self.modules_system.is_module_loaded(m) @fixtures.switch_to_user_runtime def test_conflict_environ_after_module_load(self): self.setup_modules_system() self.modules_system.load_module('testmod_foo') envfoo = env.Environment(name='envfoo', modules=['testmod_foo']) snapshot, _ = rt.loadenv(envfoo) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_foo') @fixtures.switch_to_user_runtime def test_conflict_environ_after_module_force_load(self): self.setup_modules_system() self.modules_system.load_module('testmod_foo') envbar = env.Environment(name='envbar', modules=['testmod_bar']) snapshot, _ = rt.loadenv(envbar) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_foo') def test_immutability(self): # Check emit_load_commands() _, commands = rt.loadenv(self.environ) # Try to modify the returned list of commands commands.append('foo') assert 'foo' not in rt.loadenv(self.environ)[1] # Test ProgEnvironment prgenv = env.ProgEnvironment('foo_prgenv') assert isinstance(prgenv, env.Environment) with pytest.raises(AttributeError): prgenv.cc = 'gcc' with pytest.raises(AttributeError): prgenv.cxx = 'g++' with pytest.raises(AttributeError): prgenv.ftn = 'gfortran' with pytest.raises(AttributeError): prgenv.nvcc = 'clang' with pytest.raises(AttributeError): prgenv.cppflags = ['-DFOO'] with pytest.raises(AttributeError): prgenv.cflags = ['-O1'] with pytest.raises(AttributeError): prgenv.cxxflags = ['-O1'] with pytest.raises(AttributeError): prgenv.fflags = ['-O1'] with pytest.raises(AttributeError): prgenv.ldflags = ['-lm'] @fixtures.switch_to_user_runtime def test_emit_load_commands(self): self.setup_modules_system() ms = rt.runtime().modules_system expected_commands = [ ms.emit_load_commands('testmod_foo')[0], 'export _var0=val1', 'export _var2=$_var0', 'export _var3=${_var1}', ] assert expected_commands == rt.emit_loadenv_commands(self.environ) @fixtures.switch_to_user_runtime def test_emit_load_commands_with_confict(self): self.setup_modules_system() # Load a conflicting module self.modules_system.load_module('testmod_bar') ms = rt.runtime().modules_system expected_commands = [ ms.emit_unload_commands('testmod_bar')[0], ms.emit_load_commands('testmod_foo')[0], 'export _var0=val1', 'export _var2=$_var0', 'export _var3=${_var1}', ] assert expected_commands == rt.emit_loadenv_commands(self.environ) ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_environments.py::TestEnvironment::test_load_overlapping,unittests/test_environments.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe.core.environments as env import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.exceptions import EnvironError class TestEnvironment(unittest.TestCase): def assertModulesLoaded(self, modules): for m in modules: assert self.modules_system.is_module_loaded(m) def assertModulesNotLoaded(self, modules): for m in modules: assert not self.modules_system.is_module_loaded(m) def setup_modules_system(self): if not fixtures.has_sane_modules_system(): pytest.skip('no modules system configured') self.modules_system = rt.runtime().modules_system self.modules_system.searchpath_add(fixtures.TEST_MODULES) # Always add a base module; this is a workaround for the modules # environment's inconsistent behaviour, that starts with an empty # LOADEDMODULES variable and ends up removing it completely if all # present modules are removed. self.modules_system.load_module('testmod_base') def setUp(self): self.modules_system = None os.environ['_var0'] = 'val0' os.environ['_var1'] = 'val1' self.environ_save = env.snapshot() self.environ = env.Environment(name='TestEnv1', modules=['testmod_foo'], variables=[('_var0', 'val1'), ('_var2', '$_var0'), ('_var3', '${_var1}')]) self.environ_other = env.Environment(name='TestEnv2', modules=['testmod_boo'], variables={'_var4': 'val4'}) def tearDown(self): if self.modules_system is not None: self.modules_system.searchpath_remove(fixtures.TEST_MODULES) self.environ_save.restore() def test_setup(self): if fixtures.has_sane_modules_system(): assert len(self.environ.modules) == 1 assert 'testmod_foo' in self.environ.modules assert len(self.environ.variables.keys()) == 3 assert self.environ.variables['_var0'] == 'val1' # No variable expansion, if environment is not loaded self.environ.variables['_var2'] == '$_var0' self.environ.variables['_var3'] == '${_var1}' def test_environ_snapshot(self): rt.loadenv(self.environ, self.environ_other) self.environ_save.restore() assert self.environ_save == env.snapshot() assert not rt.is_env_loaded(self.environ) assert not rt.is_env_loaded(self.environ_other) def test_load_restore(self): snapshot, _ = rt.loadenv(self.environ) os.environ['_var0'] == 'val1' os.environ['_var1'] == 'val1' os.environ['_var2'] == 'val1' os.environ['_var3'] == 'val1' if fixtures.has_sane_modules_system(): self.assertModulesLoaded(self.environ.modules) assert rt.is_env_loaded(self.environ) snapshot.restore() self.environ_save == env.snapshot() os.environ['_var0'], 'val0' if fixtures.has_sane_modules_system(): assert not self.modules_system.is_module_loaded('testmod_foo') assert not rt.is_env_loaded(self.environ) @fixtures.switch_to_user_runtime def test_temp_environment(self): self.setup_modules_system() with rt.temp_environment( ['testmod_foo'], {'_var0': 'val2', '_var3': 'val3'} ) as environ: assert rt.is_env_loaded(environ) assert not rt.is_env_loaded(environ) @fixtures.switch_to_user_runtime def test_load_already_present(self): self.setup_modules_system() self.modules_system.load_module('testmod_boo') snapshot, _ = rt.loadenv(self.environ) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_boo') def test_load_non_overlapping(self): e0 = env.Environment(name='e0', variables=[('a', '1'), ('b', '2')]) e1 = env.Environment(name='e1', variables=[('c', '3'), ('d', '4')]) rt.loadenv(e0, e1) assert rt.is_env_loaded(e0) assert rt.is_env_loaded(e1) def test_load_overlapping(self): e0 = env.Environment(name='e0', variables=[('a', '1'), ('b', '2')]) e1 = env.Environment(name='e1', variables=[('b', '3'), ('c', '4')]) rt.loadenv(e0, e1) assert not rt.is_env_loaded(e0) assert rt.is_env_loaded(e1) def test_equal(self): env1 = env.Environment('env1', modules=['foo', 'bar']) env2 = env.Environment('env1', modules=['bar', 'foo']) assert env1 == env2 assert env2 == env1 def test_not_equal(self): env1 = env.Environment('env1', modules=['foo', 'bar']) env2 = env.Environment('env2', modules=['foo', 'bar']) assert env1 != env2 # Variables are ordered, because they might depend on each other env1 = env.Environment('env1', variables=[('a', 1), ('b', 2)]) env2 = env.Environment('env1', variables=[('b', 2), ('a', 1)]) assert env1 != env2 @fixtures.switch_to_user_runtime def test_conflicting_environments(self): self.setup_modules_system() envfoo = env.Environment(name='envfoo', modules=['testmod_foo', 'testmod_boo']) envbar = env.Environment(name='envbar', modules=['testmod_bar']) rt.loadenv(envfoo, envbar) for m in envbar.modules: assert self.modules_system.is_module_loaded(m) for m in envfoo.modules: assert not self.modules_system.is_module_loaded(m) @fixtures.switch_to_user_runtime def test_conflict_environ_after_module_load(self): self.setup_modules_system() self.modules_system.load_module('testmod_foo') envfoo = env.Environment(name='envfoo', modules=['testmod_foo']) snapshot, _ = rt.loadenv(envfoo) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_foo') @fixtures.switch_to_user_runtime def test_conflict_environ_after_module_force_load(self): self.setup_modules_system() self.modules_system.load_module('testmod_foo') envbar = env.Environment(name='envbar', modules=['testmod_bar']) snapshot, _ = rt.loadenv(envbar) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_foo') def test_immutability(self): # Check emit_load_commands() _, commands = rt.loadenv(self.environ) # Try to modify the returned list of commands commands.append('foo') assert 'foo' not in rt.loadenv(self.environ)[1] # Test ProgEnvironment prgenv = env.ProgEnvironment('foo_prgenv') assert isinstance(prgenv, env.Environment) with pytest.raises(AttributeError): prgenv.cc = 'gcc' with pytest.raises(AttributeError): prgenv.cxx = 'g++' with pytest.raises(AttributeError): prgenv.ftn = 'gfortran' with pytest.raises(AttributeError): prgenv.nvcc = 'clang' with pytest.raises(AttributeError): prgenv.cppflags = ['-DFOO'] with pytest.raises(AttributeError): prgenv.cflags = ['-O1'] with pytest.raises(AttributeError): prgenv.cxxflags = ['-O1'] with pytest.raises(AttributeError): prgenv.fflags = ['-O1'] with pytest.raises(AttributeError): prgenv.ldflags = ['-lm'] @fixtures.switch_to_user_runtime def test_emit_load_commands(self): self.setup_modules_system() ms = rt.runtime().modules_system expected_commands = [ ms.emit_load_commands('testmod_foo')[0], 'export _var0=val1', 'export _var2=$_var0', 'export _var3=${_var1}', ] assert expected_commands == rt.emit_loadenv_commands(self.environ) @fixtures.switch_to_user_runtime def test_emit_load_commands_with_confict(self): self.setup_modules_system() # Load a conflicting module self.modules_system.load_module('testmod_bar') ms = rt.runtime().modules_system expected_commands = [ ms.emit_unload_commands('testmod_bar')[0], ms.emit_load_commands('testmod_foo')[0], 'export _var0=val1', 'export _var2=$_var0', 'export _var3=${_var1}', ] assert expected_commands == rt.emit_loadenv_commands(self.environ) ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_environments.py::TestEnvironment::test_load_restore,unittests/test_environments.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe.core.environments as env import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.exceptions import EnvironError class TestEnvironment(unittest.TestCase): def assertModulesLoaded(self, modules): for m in modules: assert self.modules_system.is_module_loaded(m) def assertModulesNotLoaded(self, modules): for m in modules: assert not self.modules_system.is_module_loaded(m) def setup_modules_system(self): if not fixtures.has_sane_modules_system(): pytest.skip('no modules system configured') self.modules_system = rt.runtime().modules_system self.modules_system.searchpath_add(fixtures.TEST_MODULES) # Always add a base module; this is a workaround for the modules # environment's inconsistent behaviour, that starts with an empty # LOADEDMODULES variable and ends up removing it completely if all # present modules are removed. self.modules_system.load_module('testmod_base') def setUp(self): self.modules_system = None os.environ['_var0'] = 'val0' os.environ['_var1'] = 'val1' self.environ_save = env.snapshot() self.environ = env.Environment(name='TestEnv1', modules=['testmod_foo'], variables=[('_var0', 'val1'), ('_var2', '$_var0'), ('_var3', '${_var1}')]) self.environ_other = env.Environment(name='TestEnv2', modules=['testmod_boo'], variables={'_var4': 'val4'}) def tearDown(self): if self.modules_system is not None: self.modules_system.searchpath_remove(fixtures.TEST_MODULES) self.environ_save.restore() def test_setup(self): if fixtures.has_sane_modules_system(): assert len(self.environ.modules) == 1 assert 'testmod_foo' in self.environ.modules assert len(self.environ.variables.keys()) == 3 assert self.environ.variables['_var0'] == 'val1' # No variable expansion, if environment is not loaded self.environ.variables['_var2'] == '$_var0' self.environ.variables['_var3'] == '${_var1}' def test_environ_snapshot(self): rt.loadenv(self.environ, self.environ_other) self.environ_save.restore() assert self.environ_save == env.snapshot() assert not rt.is_env_loaded(self.environ) assert not rt.is_env_loaded(self.environ_other) def test_load_restore(self): snapshot, _ = rt.loadenv(self.environ) os.environ['_var0'] == 'val1' os.environ['_var1'] == 'val1' os.environ['_var2'] == 'val1' os.environ['_var3'] == 'val1' if fixtures.has_sane_modules_system(): self.assertModulesLoaded(self.environ.modules) assert rt.is_env_loaded(self.environ) snapshot.restore() self.environ_save == env.snapshot() os.environ['_var0'], 'val0' if fixtures.has_sane_modules_system(): assert not self.modules_system.is_module_loaded('testmod_foo') assert not rt.is_env_loaded(self.environ) @fixtures.switch_to_user_runtime def test_temp_environment(self): self.setup_modules_system() with rt.temp_environment( ['testmod_foo'], {'_var0': 'val2', '_var3': 'val3'} ) as environ: assert rt.is_env_loaded(environ) assert not rt.is_env_loaded(environ) @fixtures.switch_to_user_runtime def test_load_already_present(self): self.setup_modules_system() self.modules_system.load_module('testmod_boo') snapshot, _ = rt.loadenv(self.environ) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_boo') def test_load_non_overlapping(self): e0 = env.Environment(name='e0', variables=[('a', '1'), ('b', '2')]) e1 = env.Environment(name='e1', variables=[('c', '3'), ('d', '4')]) rt.loadenv(e0, e1) assert rt.is_env_loaded(e0) assert rt.is_env_loaded(e1) def test_load_overlapping(self): e0 = env.Environment(name='e0', variables=[('a', '1'), ('b', '2')]) e1 = env.Environment(name='e1', variables=[('b', '3'), ('c', '4')]) rt.loadenv(e0, e1) assert not rt.is_env_loaded(e0) assert rt.is_env_loaded(e1) def test_equal(self): env1 = env.Environment('env1', modules=['foo', 'bar']) env2 = env.Environment('env1', modules=['bar', 'foo']) assert env1 == env2 assert env2 == env1 def test_not_equal(self): env1 = env.Environment('env1', modules=['foo', 'bar']) env2 = env.Environment('env2', modules=['foo', 'bar']) assert env1 != env2 # Variables are ordered, because they might depend on each other env1 = env.Environment('env1', variables=[('a', 1), ('b', 2)]) env2 = env.Environment('env1', variables=[('b', 2), ('a', 1)]) assert env1 != env2 @fixtures.switch_to_user_runtime def test_conflicting_environments(self): self.setup_modules_system() envfoo = env.Environment(name='envfoo', modules=['testmod_foo', 'testmod_boo']) envbar = env.Environment(name='envbar', modules=['testmod_bar']) rt.loadenv(envfoo, envbar) for m in envbar.modules: assert self.modules_system.is_module_loaded(m) for m in envfoo.modules: assert not self.modules_system.is_module_loaded(m) @fixtures.switch_to_user_runtime def test_conflict_environ_after_module_load(self): self.setup_modules_system() self.modules_system.load_module('testmod_foo') envfoo = env.Environment(name='envfoo', modules=['testmod_foo']) snapshot, _ = rt.loadenv(envfoo) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_foo') @fixtures.switch_to_user_runtime def test_conflict_environ_after_module_force_load(self): self.setup_modules_system() self.modules_system.load_module('testmod_foo') envbar = env.Environment(name='envbar', modules=['testmod_bar']) snapshot, _ = rt.loadenv(envbar) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_foo') def test_immutability(self): # Check emit_load_commands() _, commands = rt.loadenv(self.environ) # Try to modify the returned list of commands commands.append('foo') assert 'foo' not in rt.loadenv(self.environ)[1] # Test ProgEnvironment prgenv = env.ProgEnvironment('foo_prgenv') assert isinstance(prgenv, env.Environment) with pytest.raises(AttributeError): prgenv.cc = 'gcc' with pytest.raises(AttributeError): prgenv.cxx = 'g++' with pytest.raises(AttributeError): prgenv.ftn = 'gfortran' with pytest.raises(AttributeError): prgenv.nvcc = 'clang' with pytest.raises(AttributeError): prgenv.cppflags = ['-DFOO'] with pytest.raises(AttributeError): prgenv.cflags = ['-O1'] with pytest.raises(AttributeError): prgenv.cxxflags = ['-O1'] with pytest.raises(AttributeError): prgenv.fflags = ['-O1'] with pytest.raises(AttributeError): prgenv.ldflags = ['-lm'] @fixtures.switch_to_user_runtime def test_emit_load_commands(self): self.setup_modules_system() ms = rt.runtime().modules_system expected_commands = [ ms.emit_load_commands('testmod_foo')[0], 'export _var0=val1', 'export _var2=$_var0', 'export _var3=${_var1}', ] assert expected_commands == rt.emit_loadenv_commands(self.environ) @fixtures.switch_to_user_runtime def test_emit_load_commands_with_confict(self): self.setup_modules_system() # Load a conflicting module self.modules_system.load_module('testmod_bar') ms = rt.runtime().modules_system expected_commands = [ ms.emit_unload_commands('testmod_bar')[0], ms.emit_load_commands('testmod_foo')[0], 'export _var0=val1', 'export _var2=$_var0', 'export _var3=${_var1}', ] assert expected_commands == rt.emit_loadenv_commands(self.environ) ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_environments.py::TestEnvironment::test_setup,unittests/test_environments.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe.core.environments as env import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.exceptions import EnvironError class TestEnvironment(unittest.TestCase): def assertModulesLoaded(self, modules): for m in modules: assert self.modules_system.is_module_loaded(m) def assertModulesNotLoaded(self, modules): for m in modules: assert not self.modules_system.is_module_loaded(m) def setup_modules_system(self): if not fixtures.has_sane_modules_system(): pytest.skip('no modules system configured') self.modules_system = rt.runtime().modules_system self.modules_system.searchpath_add(fixtures.TEST_MODULES) # Always add a base module; this is a workaround for the modules # environment's inconsistent behaviour, that starts with an empty # LOADEDMODULES variable and ends up removing it completely if all # present modules are removed. self.modules_system.load_module('testmod_base') def setUp(self): self.modules_system = None os.environ['_var0'] = 'val0' os.environ['_var1'] = 'val1' self.environ_save = env.snapshot() self.environ = env.Environment(name='TestEnv1', modules=['testmod_foo'], variables=[('_var0', 'val1'), ('_var2', '$_var0'), ('_var3', '${_var1}')]) self.environ_other = env.Environment(name='TestEnv2', modules=['testmod_boo'], variables={'_var4': 'val4'}) def tearDown(self): if self.modules_system is not None: self.modules_system.searchpath_remove(fixtures.TEST_MODULES) self.environ_save.restore() def test_setup(self): if fixtures.has_sane_modules_system(): assert len(self.environ.modules) == 1 assert 'testmod_foo' in self.environ.modules assert len(self.environ.variables.keys()) == 3 assert self.environ.variables['_var0'] == 'val1' # No variable expansion, if environment is not loaded self.environ.variables['_var2'] == '$_var0' self.environ.variables['_var3'] == '${_var1}' def test_environ_snapshot(self): rt.loadenv(self.environ, self.environ_other) self.environ_save.restore() assert self.environ_save == env.snapshot() assert not rt.is_env_loaded(self.environ) assert not rt.is_env_loaded(self.environ_other) def test_load_restore(self): snapshot, _ = rt.loadenv(self.environ) os.environ['_var0'] == 'val1' os.environ['_var1'] == 'val1' os.environ['_var2'] == 'val1' os.environ['_var3'] == 'val1' if fixtures.has_sane_modules_system(): self.assertModulesLoaded(self.environ.modules) assert rt.is_env_loaded(self.environ) snapshot.restore() self.environ_save == env.snapshot() os.environ['_var0'], 'val0' if fixtures.has_sane_modules_system(): assert not self.modules_system.is_module_loaded('testmod_foo') assert not rt.is_env_loaded(self.environ) @fixtures.switch_to_user_runtime def test_temp_environment(self): self.setup_modules_system() with rt.temp_environment( ['testmod_foo'], {'_var0': 'val2', '_var3': 'val3'} ) as environ: assert rt.is_env_loaded(environ) assert not rt.is_env_loaded(environ) @fixtures.switch_to_user_runtime def test_load_already_present(self): self.setup_modules_system() self.modules_system.load_module('testmod_boo') snapshot, _ = rt.loadenv(self.environ) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_boo') def test_load_non_overlapping(self): e0 = env.Environment(name='e0', variables=[('a', '1'), ('b', '2')]) e1 = env.Environment(name='e1', variables=[('c', '3'), ('d', '4')]) rt.loadenv(e0, e1) assert rt.is_env_loaded(e0) assert rt.is_env_loaded(e1) def test_load_overlapping(self): e0 = env.Environment(name='e0', variables=[('a', '1'), ('b', '2')]) e1 = env.Environment(name='e1', variables=[('b', '3'), ('c', '4')]) rt.loadenv(e0, e1) assert not rt.is_env_loaded(e0) assert rt.is_env_loaded(e1) def test_equal(self): env1 = env.Environment('env1', modules=['foo', 'bar']) env2 = env.Environment('env1', modules=['bar', 'foo']) assert env1 == env2 assert env2 == env1 def test_not_equal(self): env1 = env.Environment('env1', modules=['foo', 'bar']) env2 = env.Environment('env2', modules=['foo', 'bar']) assert env1 != env2 # Variables are ordered, because they might depend on each other env1 = env.Environment('env1', variables=[('a', 1), ('b', 2)]) env2 = env.Environment('env1', variables=[('b', 2), ('a', 1)]) assert env1 != env2 @fixtures.switch_to_user_runtime def test_conflicting_environments(self): self.setup_modules_system() envfoo = env.Environment(name='envfoo', modules=['testmod_foo', 'testmod_boo']) envbar = env.Environment(name='envbar', modules=['testmod_bar']) rt.loadenv(envfoo, envbar) for m in envbar.modules: assert self.modules_system.is_module_loaded(m) for m in envfoo.modules: assert not self.modules_system.is_module_loaded(m) @fixtures.switch_to_user_runtime def test_conflict_environ_after_module_load(self): self.setup_modules_system() self.modules_system.load_module('testmod_foo') envfoo = env.Environment(name='envfoo', modules=['testmod_foo']) snapshot, _ = rt.loadenv(envfoo) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_foo') @fixtures.switch_to_user_runtime def test_conflict_environ_after_module_force_load(self): self.setup_modules_system() self.modules_system.load_module('testmod_foo') envbar = env.Environment(name='envbar', modules=['testmod_bar']) snapshot, _ = rt.loadenv(envbar) snapshot.restore() assert self.modules_system.is_module_loaded('testmod_foo') def test_immutability(self): # Check emit_load_commands() _, commands = rt.loadenv(self.environ) # Try to modify the returned list of commands commands.append('foo') assert 'foo' not in rt.loadenv(self.environ)[1] # Test ProgEnvironment prgenv = env.ProgEnvironment('foo_prgenv') assert isinstance(prgenv, env.Environment) with pytest.raises(AttributeError): prgenv.cc = 'gcc' with pytest.raises(AttributeError): prgenv.cxx = 'g++' with pytest.raises(AttributeError): prgenv.ftn = 'gfortran' with pytest.raises(AttributeError): prgenv.nvcc = 'clang' with pytest.raises(AttributeError): prgenv.cppflags = ['-DFOO'] with pytest.raises(AttributeError): prgenv.cflags = ['-O1'] with pytest.raises(AttributeError): prgenv.cxxflags = ['-O1'] with pytest.raises(AttributeError): prgenv.fflags = ['-O1'] with pytest.raises(AttributeError): prgenv.ldflags = ['-lm'] @fixtures.switch_to_user_runtime def test_emit_load_commands(self): self.setup_modules_system() ms = rt.runtime().modules_system expected_commands = [ ms.emit_load_commands('testmod_foo')[0], 'export _var0=val1', 'export _var2=$_var0', 'export _var3=${_var1}', ] assert expected_commands == rt.emit_loadenv_commands(self.environ) @fixtures.switch_to_user_runtime def test_emit_load_commands_with_confict(self): self.setup_modules_system() # Load a conflicting module self.modules_system.load_module('testmod_bar') ms = rt.runtime().modules_system expected_commands = [ ms.emit_unload_commands('testmod_bar')[0], ms.emit_load_commands('testmod_foo')[0], 'export _var0=val1', 'export _var2=$_var0', 'export _var3=${_var1}', ] assert expected_commands == rt.emit_loadenv_commands(self.environ) ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_loader.py::TestRegressionCheckLoader::test_conflicted_checks,unittests/test_loader.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe as rfm from reframe.core.exceptions import (ConfigError, NameConflictError, ReframeDeprecationWarning, RegressionTestLoadError) from reframe.core.systems import System from reframe.frontend.loader import RegressionCheckLoader class TestRegressionCheckLoader(unittest.TestCase): def setUp(self): self.loader = RegressionCheckLoader(['.'], ignore_conflicts=True) self.loader_with_path = RegressionCheckLoader( ['unittests/resources/checks', 'unittests/foobar'], ignore_conflicts=True) def test_load_file_relative(self): checks = self.loader.load_from_file( 'unittests/resources/checks/emptycheck.py') assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_file_absolute(self): checks = self.loader.load_from_file( os.path.abspath('unittests/resources/checks/emptycheck.py')) assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_recursive(self): checks = self.loader.load_from_dir('unittests/resources/checks', recurse=True) assert 12 == len(checks) def test_load_all(self): checks = self.loader_with_path.load_all() assert 11 == len(checks) def test_load_new_syntax(self): checks = self.loader.load_from_file( 'unittests/resources/checks_unlisted/good.py') assert 13 == len(checks) def test_conflicted_checks(self): self.loader_with_path._ignore_conflicts = False with pytest.raises(NameConflictError): self.loader_with_path.load_all() def test_load_error(self): with pytest.raises(OSError): self.loader.load_from_file('unittests/resources/checks/foo.py') def test_load_bad_required_version(self): with pytest.raises(ValueError): self.loader.load_from_file('unittests/resources/checks_unlisted/' 'no_required_version.py') def test_load_bad_init(self): tests = self.loader.load_from_file( 'unittests/resources/checks_unlisted/bad_init_check.py') assert 0 == len(tests) def test_special_test(self): with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecated(rfm.RegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedRunOnly(rfm.RunOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnly(rfm.CompileOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnlyDerived(TestDeprecatedCompileOnly): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(None) as warnings: @rfm.simple_test class TestSimple(rfm.RegressionTest): def __init__(self): pass @rfm.simple_test class TestSpecial(rfm.RegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) @rfm.simple_test class TestSpecialRunOnly(rfm.RunOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() @rfm.simple_test class TestSpecialCompileOnly(rfm.CompileOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert not any(isinstance(w.message, ReframeDeprecationWarning) for w in warnings) with pytest.warns(ReframeDeprecationWarning) as warnings: @rfm.simple_test class TestSpecialDerived(TestSpecial): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert len(warnings) == 2 @rfm.simple_test class TestFinal(rfm.RegressionTest): def __init__(self): pass @rfm.final def my_new_final(seld): pass with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestFinalDerived(TestFinal): def my_new_final(self, a, b): pass ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_loader.py::TestRegressionCheckLoader::test_load_all,unittests/test_loader.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe as rfm from reframe.core.exceptions import (ConfigError, NameConflictError, ReframeDeprecationWarning, RegressionTestLoadError) from reframe.core.systems import System from reframe.frontend.loader import RegressionCheckLoader class TestRegressionCheckLoader(unittest.TestCase): def setUp(self): self.loader = RegressionCheckLoader(['.'], ignore_conflicts=True) self.loader_with_path = RegressionCheckLoader( ['unittests/resources/checks', 'unittests/foobar'], ignore_conflicts=True) def test_load_file_relative(self): checks = self.loader.load_from_file( 'unittests/resources/checks/emptycheck.py') assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_file_absolute(self): checks = self.loader.load_from_file( os.path.abspath('unittests/resources/checks/emptycheck.py')) assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_recursive(self): checks = self.loader.load_from_dir('unittests/resources/checks', recurse=True) assert 12 == len(checks) def test_load_all(self): checks = self.loader_with_path.load_all() assert 11 == len(checks) def test_load_new_syntax(self): checks = self.loader.load_from_file( 'unittests/resources/checks_unlisted/good.py') assert 13 == len(checks) def test_conflicted_checks(self): self.loader_with_path._ignore_conflicts = False with pytest.raises(NameConflictError): self.loader_with_path.load_all() def test_load_error(self): with pytest.raises(OSError): self.loader.load_from_file('unittests/resources/checks/foo.py') def test_load_bad_required_version(self): with pytest.raises(ValueError): self.loader.load_from_file('unittests/resources/checks_unlisted/' 'no_required_version.py') def test_load_bad_init(self): tests = self.loader.load_from_file( 'unittests/resources/checks_unlisted/bad_init_check.py') assert 0 == len(tests) def test_special_test(self): with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecated(rfm.RegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedRunOnly(rfm.RunOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnly(rfm.CompileOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnlyDerived(TestDeprecatedCompileOnly): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(None) as warnings: @rfm.simple_test class TestSimple(rfm.RegressionTest): def __init__(self): pass @rfm.simple_test class TestSpecial(rfm.RegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) @rfm.simple_test class TestSpecialRunOnly(rfm.RunOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() @rfm.simple_test class TestSpecialCompileOnly(rfm.CompileOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert not any(isinstance(w.message, ReframeDeprecationWarning) for w in warnings) with pytest.warns(ReframeDeprecationWarning) as warnings: @rfm.simple_test class TestSpecialDerived(TestSpecial): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert len(warnings) == 2 @rfm.simple_test class TestFinal(rfm.RegressionTest): def __init__(self): pass @rfm.final def my_new_final(seld): pass with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestFinalDerived(TestFinal): def my_new_final(self, a, b): pass ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_loader.py::TestRegressionCheckLoader::test_load_bad_init,unittests/test_loader.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe as rfm from reframe.core.exceptions import (ConfigError, NameConflictError, ReframeDeprecationWarning, RegressionTestLoadError) from reframe.core.systems import System from reframe.frontend.loader import RegressionCheckLoader class TestRegressionCheckLoader(unittest.TestCase): def setUp(self): self.loader = RegressionCheckLoader(['.'], ignore_conflicts=True) self.loader_with_path = RegressionCheckLoader( ['unittests/resources/checks', 'unittests/foobar'], ignore_conflicts=True) def test_load_file_relative(self): checks = self.loader.load_from_file( 'unittests/resources/checks/emptycheck.py') assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_file_absolute(self): checks = self.loader.load_from_file( os.path.abspath('unittests/resources/checks/emptycheck.py')) assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_recursive(self): checks = self.loader.load_from_dir('unittests/resources/checks', recurse=True) assert 12 == len(checks) def test_load_all(self): checks = self.loader_with_path.load_all() assert 11 == len(checks) def test_load_new_syntax(self): checks = self.loader.load_from_file( 'unittests/resources/checks_unlisted/good.py') assert 13 == len(checks) def test_conflicted_checks(self): self.loader_with_path._ignore_conflicts = False with pytest.raises(NameConflictError): self.loader_with_path.load_all() def test_load_error(self): with pytest.raises(OSError): self.loader.load_from_file('unittests/resources/checks/foo.py') def test_load_bad_required_version(self): with pytest.raises(ValueError): self.loader.load_from_file('unittests/resources/checks_unlisted/' 'no_required_version.py') def test_load_bad_init(self): tests = self.loader.load_from_file( 'unittests/resources/checks_unlisted/bad_init_check.py') assert 0 == len(tests) def test_special_test(self): with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecated(rfm.RegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedRunOnly(rfm.RunOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnly(rfm.CompileOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnlyDerived(TestDeprecatedCompileOnly): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(None) as warnings: @rfm.simple_test class TestSimple(rfm.RegressionTest): def __init__(self): pass @rfm.simple_test class TestSpecial(rfm.RegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) @rfm.simple_test class TestSpecialRunOnly(rfm.RunOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() @rfm.simple_test class TestSpecialCompileOnly(rfm.CompileOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert not any(isinstance(w.message, ReframeDeprecationWarning) for w in warnings) with pytest.warns(ReframeDeprecationWarning) as warnings: @rfm.simple_test class TestSpecialDerived(TestSpecial): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert len(warnings) == 2 @rfm.simple_test class TestFinal(rfm.RegressionTest): def __init__(self): pass @rfm.final def my_new_final(seld): pass with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestFinalDerived(TestFinal): def my_new_final(self, a, b): pass ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_loader.py::TestRegressionCheckLoader::test_load_file_absolute,unittests/test_loader.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe as rfm from reframe.core.exceptions import (ConfigError, NameConflictError, ReframeDeprecationWarning, RegressionTestLoadError) from reframe.core.systems import System from reframe.frontend.loader import RegressionCheckLoader class TestRegressionCheckLoader(unittest.TestCase): def setUp(self): self.loader = RegressionCheckLoader(['.'], ignore_conflicts=True) self.loader_with_path = RegressionCheckLoader( ['unittests/resources/checks', 'unittests/foobar'], ignore_conflicts=True) def test_load_file_relative(self): checks = self.loader.load_from_file( 'unittests/resources/checks/emptycheck.py') assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_file_absolute(self): checks = self.loader.load_from_file( os.path.abspath('unittests/resources/checks/emptycheck.py')) assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_recursive(self): checks = self.loader.load_from_dir('unittests/resources/checks', recurse=True) assert 12 == len(checks) def test_load_all(self): checks = self.loader_with_path.load_all() assert 11 == len(checks) def test_load_new_syntax(self): checks = self.loader.load_from_file( 'unittests/resources/checks_unlisted/good.py') assert 13 == len(checks) def test_conflicted_checks(self): self.loader_with_path._ignore_conflicts = False with pytest.raises(NameConflictError): self.loader_with_path.load_all() def test_load_error(self): with pytest.raises(OSError): self.loader.load_from_file('unittests/resources/checks/foo.py') def test_load_bad_required_version(self): with pytest.raises(ValueError): self.loader.load_from_file('unittests/resources/checks_unlisted/' 'no_required_version.py') def test_load_bad_init(self): tests = self.loader.load_from_file( 'unittests/resources/checks_unlisted/bad_init_check.py') assert 0 == len(tests) def test_special_test(self): with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecated(rfm.RegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedRunOnly(rfm.RunOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnly(rfm.CompileOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnlyDerived(TestDeprecatedCompileOnly): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(None) as warnings: @rfm.simple_test class TestSimple(rfm.RegressionTest): def __init__(self): pass @rfm.simple_test class TestSpecial(rfm.RegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) @rfm.simple_test class TestSpecialRunOnly(rfm.RunOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() @rfm.simple_test class TestSpecialCompileOnly(rfm.CompileOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert not any(isinstance(w.message, ReframeDeprecationWarning) for w in warnings) with pytest.warns(ReframeDeprecationWarning) as warnings: @rfm.simple_test class TestSpecialDerived(TestSpecial): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert len(warnings) == 2 @rfm.simple_test class TestFinal(rfm.RegressionTest): def __init__(self): pass @rfm.final def my_new_final(seld): pass with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestFinalDerived(TestFinal): def my_new_final(self, a, b): pass ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_loader.py::TestRegressionCheckLoader::test_load_file_relative,unittests/test_loader.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe as rfm from reframe.core.exceptions import (ConfigError, NameConflictError, ReframeDeprecationWarning, RegressionTestLoadError) from reframe.core.systems import System from reframe.frontend.loader import RegressionCheckLoader class TestRegressionCheckLoader(unittest.TestCase): def setUp(self): self.loader = RegressionCheckLoader(['.'], ignore_conflicts=True) self.loader_with_path = RegressionCheckLoader( ['unittests/resources/checks', 'unittests/foobar'], ignore_conflicts=True) def test_load_file_relative(self): checks = self.loader.load_from_file( 'unittests/resources/checks/emptycheck.py') assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_file_absolute(self): checks = self.loader.load_from_file( os.path.abspath('unittests/resources/checks/emptycheck.py')) assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_recursive(self): checks = self.loader.load_from_dir('unittests/resources/checks', recurse=True) assert 12 == len(checks) def test_load_all(self): checks = self.loader_with_path.load_all() assert 11 == len(checks) def test_load_new_syntax(self): checks = self.loader.load_from_file( 'unittests/resources/checks_unlisted/good.py') assert 13 == len(checks) def test_conflicted_checks(self): self.loader_with_path._ignore_conflicts = False with pytest.raises(NameConflictError): self.loader_with_path.load_all() def test_load_error(self): with pytest.raises(OSError): self.loader.load_from_file('unittests/resources/checks/foo.py') def test_load_bad_required_version(self): with pytest.raises(ValueError): self.loader.load_from_file('unittests/resources/checks_unlisted/' 'no_required_version.py') def test_load_bad_init(self): tests = self.loader.load_from_file( 'unittests/resources/checks_unlisted/bad_init_check.py') assert 0 == len(tests) def test_special_test(self): with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecated(rfm.RegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedRunOnly(rfm.RunOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnly(rfm.CompileOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnlyDerived(TestDeprecatedCompileOnly): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(None) as warnings: @rfm.simple_test class TestSimple(rfm.RegressionTest): def __init__(self): pass @rfm.simple_test class TestSpecial(rfm.RegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) @rfm.simple_test class TestSpecialRunOnly(rfm.RunOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() @rfm.simple_test class TestSpecialCompileOnly(rfm.CompileOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert not any(isinstance(w.message, ReframeDeprecationWarning) for w in warnings) with pytest.warns(ReframeDeprecationWarning) as warnings: @rfm.simple_test class TestSpecialDerived(TestSpecial): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert len(warnings) == 2 @rfm.simple_test class TestFinal(rfm.RegressionTest): def __init__(self): pass @rfm.final def my_new_final(seld): pass with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestFinalDerived(TestFinal): def my_new_final(self, a, b): pass ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_loader.py::TestRegressionCheckLoader::test_load_new_syntax,unittests/test_loader.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe as rfm from reframe.core.exceptions import (ConfigError, NameConflictError, ReframeDeprecationWarning, RegressionTestLoadError) from reframe.core.systems import System from reframe.frontend.loader import RegressionCheckLoader class TestRegressionCheckLoader(unittest.TestCase): def setUp(self): self.loader = RegressionCheckLoader(['.'], ignore_conflicts=True) self.loader_with_path = RegressionCheckLoader( ['unittests/resources/checks', 'unittests/foobar'], ignore_conflicts=True) def test_load_file_relative(self): checks = self.loader.load_from_file( 'unittests/resources/checks/emptycheck.py') assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_file_absolute(self): checks = self.loader.load_from_file( os.path.abspath('unittests/resources/checks/emptycheck.py')) assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_recursive(self): checks = self.loader.load_from_dir('unittests/resources/checks', recurse=True) assert 12 == len(checks) def test_load_all(self): checks = self.loader_with_path.load_all() assert 11 == len(checks) def test_load_new_syntax(self): checks = self.loader.load_from_file( 'unittests/resources/checks_unlisted/good.py') assert 13 == len(checks) def test_conflicted_checks(self): self.loader_with_path._ignore_conflicts = False with pytest.raises(NameConflictError): self.loader_with_path.load_all() def test_load_error(self): with pytest.raises(OSError): self.loader.load_from_file('unittests/resources/checks/foo.py') def test_load_bad_required_version(self): with pytest.raises(ValueError): self.loader.load_from_file('unittests/resources/checks_unlisted/' 'no_required_version.py') def test_load_bad_init(self): tests = self.loader.load_from_file( 'unittests/resources/checks_unlisted/bad_init_check.py') assert 0 == len(tests) def test_special_test(self): with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecated(rfm.RegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedRunOnly(rfm.RunOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnly(rfm.CompileOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnlyDerived(TestDeprecatedCompileOnly): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(None) as warnings: @rfm.simple_test class TestSimple(rfm.RegressionTest): def __init__(self): pass @rfm.simple_test class TestSpecial(rfm.RegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) @rfm.simple_test class TestSpecialRunOnly(rfm.RunOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() @rfm.simple_test class TestSpecialCompileOnly(rfm.CompileOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert not any(isinstance(w.message, ReframeDeprecationWarning) for w in warnings) with pytest.warns(ReframeDeprecationWarning) as warnings: @rfm.simple_test class TestSpecialDerived(TestSpecial): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert len(warnings) == 2 @rfm.simple_test class TestFinal(rfm.RegressionTest): def __init__(self): pass @rfm.final def my_new_final(seld): pass with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestFinalDerived(TestFinal): def my_new_final(self, a, b): pass ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_loader.py::TestRegressionCheckLoader::test_load_recursive,unittests/test_loader.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pytest import unittest import reframe as rfm from reframe.core.exceptions import (ConfigError, NameConflictError, ReframeDeprecationWarning, RegressionTestLoadError) from reframe.core.systems import System from reframe.frontend.loader import RegressionCheckLoader class TestRegressionCheckLoader(unittest.TestCase): def setUp(self): self.loader = RegressionCheckLoader(['.'], ignore_conflicts=True) self.loader_with_path = RegressionCheckLoader( ['unittests/resources/checks', 'unittests/foobar'], ignore_conflicts=True) def test_load_file_relative(self): checks = self.loader.load_from_file( 'unittests/resources/checks/emptycheck.py') assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_file_absolute(self): checks = self.loader.load_from_file( os.path.abspath('unittests/resources/checks/emptycheck.py')) assert 1 == len(checks) assert checks[0].name == 'EmptyTest' def test_load_recursive(self): checks = self.loader.load_from_dir('unittests/resources/checks', recurse=True) assert 12 == len(checks) def test_load_all(self): checks = self.loader_with_path.load_all() assert 11 == len(checks) def test_load_new_syntax(self): checks = self.loader.load_from_file( 'unittests/resources/checks_unlisted/good.py') assert 13 == len(checks) def test_conflicted_checks(self): self.loader_with_path._ignore_conflicts = False with pytest.raises(NameConflictError): self.loader_with_path.load_all() def test_load_error(self): with pytest.raises(OSError): self.loader.load_from_file('unittests/resources/checks/foo.py') def test_load_bad_required_version(self): with pytest.raises(ValueError): self.loader.load_from_file('unittests/resources/checks_unlisted/' 'no_required_version.py') def test_load_bad_init(self): tests = self.loader.load_from_file( 'unittests/resources/checks_unlisted/bad_init_check.py') assert 0 == len(tests) def test_special_test(self): with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecated(rfm.RegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedRunOnly(rfm.RunOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnly(rfm.CompileOnlyRegressionTest): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestDeprecatedCompileOnlyDerived(TestDeprecatedCompileOnly): def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) with pytest.warns(None) as warnings: @rfm.simple_test class TestSimple(rfm.RegressionTest): def __init__(self): pass @rfm.simple_test class TestSpecial(rfm.RegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) @rfm.simple_test class TestSpecialRunOnly(rfm.RunOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() @rfm.simple_test class TestSpecialCompileOnly(rfm.CompileOnlyRegressionTest, special=True): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert not any(isinstance(w.message, ReframeDeprecationWarning) for w in warnings) with pytest.warns(ReframeDeprecationWarning) as warnings: @rfm.simple_test class TestSpecialDerived(TestSpecial): def __init__(self): pass def setup(self, partition, environ, **job_opts): super().setup(system, environ, **job_opts) def run(self): super().run() assert len(warnings) == 2 @rfm.simple_test class TestFinal(rfm.RegressionTest): def __init__(self): pass @rfm.final def my_new_final(seld): pass with pytest.warns(ReframeDeprecationWarning): @rfm.simple_test class TestFinalDerived(TestFinal): def my_new_final(self, a, b): pass ", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_logging.py::TestLogger::test_check_logger,unittests/test_logging.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import copy import logging import logging.handlers import os import pytest import re import sys import tempfile import time import unittest from datetime import datetime import reframe as rfm import reframe.core.logging as rlog import reframe.core.runtime as rt import reframe.core.settings as settings import reframe.utility as util from reframe.core.exceptions import ConfigError, ReframeError from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.schedulers import Job class _FakeCheck(rfm.RegressionTest): pass def _setup_fake_check(): # A bit hacky, but we don't want to run a full test every time test = _FakeCheck() test._job = Job.create(getscheduler('local')(), getlauncher('local')(), 'fakejob') test.job._completion_time = time.time() return test class TestLogger(unittest.TestCase): def setUp(self): tmpfd, self.logfile = tempfile.mkstemp() os.close(tmpfd) self.logger = rlog.Logger('reframe') self.handler = logging.handlers.RotatingFileHandler(self.logfile) self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T') self.handler.setFormatter(self.formatter) self.logger.addHandler(self.handler) # Use the logger adapter that defines check_name self.logger_without_check = rlog.LoggerAdapter(self.logger) # Logger adapter with an associated check self.logger_with_check = rlog.LoggerAdapter(self.logger, _setup_fake_check()) def tearDown(self): os.remove(self.logfile) def found_in_logfile(self, pattern): found = False with open(self.logfile, 'rt') as fp: found = re.search(pattern, fp.read()) is not None return found def test_invalid_loglevel(self): with pytest.raises(ValueError): self.logger.setLevel('level') with pytest.raises(ValueError): rlog.Logger('logger', 'level') def test_custom_loglevels(self): self.logger_without_check.info('foo') self.logger_without_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('reframe') def test_check_logger(self): self.logger_with_check.info('foo') self.logger_with_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('_FakeCheck') def test_handler_types(self): assert issubclass(logging.Handler, rlog.Handler) assert issubclass(logging.StreamHandler, rlog.Handler) assert issubclass(logging.FileHandler, rlog.Handler) assert issubclass(logging.handlers.RotatingFileHandler, rlog.Handler) # Try to instantiate rlog.Handler with pytest.raises(TypeError): rlog.Handler() def test_custom_handler_levels(self): self.handler.setLevel('verbose') self.handler.setLevel(rlog.VERBOSE) self.logger_with_check.debug('foo') self.logger_with_check.verbose('bar') assert not self.found_in_logfile('foo') assert self.found_in_logfile('bar') def test_logger_levels(self): self.logger_with_check.setLevel('verbose') self.logger_with_check.setLevel(rlog.VERBOSE) self.logger_with_check.debug('bar') self.logger_with_check.verbose('foo') assert not self.found_in_logfile('bar') assert self.found_in_logfile('foo') def test_rfc3339_timezone_extension(self): self.formatter = rlog.RFC3339Formatter( fmt=('[%(asctime)s] %(levelname)s: %(check_name)s: ' 'ct:%(check_job_completion_time)s: %(message)s'), datefmt='%FT%T%:z') self.handler.setFormatter(self.formatter) self.logger_with_check.info('foo') self.logger_without_check.info('foo') assert not self.found_in_logfile(r'%%:z') assert self.found_in_logfile(r'\[.+(\+|-)\d\d:\d\d\]') assert self.found_in_logfile(r'ct:.+(\+|-)\d\d:\d\d') def test_rfc3339_timezone_wrong_directive(self): self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T:z') self.handler.setFormatter(self.formatter) self.logger_without_check.info('foo') assert self.found_in_logfile(':z') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(logging_config): site_config = copy.deepcopy(settings.site_configuration) site_config['logging'] = [logging_config] with tempfile.NamedTemporaryFile(mode='w+t', dir=str(tmp_path), suffix='.py', delete=False) as fp: fp.write(f'site_configuration = {util.ppretty(site_config)}') with rt.temp_runtime(fp.name): yield rt.runtime() return _temp_runtime @pytest.fixture def logfile(tmp_path): return str(tmp_path / 'test.log') @pytest.fixture def basic_config(temp_runtime, logfile): yield from temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'append': True, }, ], 'handlers_perflog': [] }) def _flush_handlers(): for h in rlog.getlogger().logger.handlers: h.flush() def _close_handlers(): for h in rlog.getlogger().logger.handlers: h.close() def _found_in_logfile(string, filename): _flush_handlers() _close_handlers() found = False with open(filename, 'rt') as fp: found = string in fp.read() return found def test_valid_level(basic_config): rlog.configure_logging(rt.runtime().site_config) assert rlog.INFO == rlog.getlogger().getEffectiveLevel() def test_handler_level(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().info('foo') rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_append(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_noappend(temp_runtime, logfile): runtime = temp_runtime( { 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: %(message)s', 'datefmt': '%F', 'append': False, } ], 'handlers_perflog': [] } ) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_date_format(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') assert _found_in_logfile(datetime.now().strftime('%F'), logfile) @pytest.fixture(params=['stdout', 'stderr']) def stream(request): return request.param def test_stream_handler(temp_runtime, logfile, stream): runtime = temp_runtime({ 'level': 'info', 'handlers': [{'type': 'stream', 'name': stream}], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) raw_logger = rlog.getlogger().logger assert len(raw_logger.handlers) == 1 handler = raw_logger.handlers[0] assert isinstance(handler, logging.StreamHandler) stream = sys.stdout if stream == 'stdout' else sys.stderr assert handler.stream == stream def test_multiple_handlers(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ {'type': 'stream', 'name': 'stderr'}, {'type': 'file', 'name': logfile}, {'type': 'syslog', 'address': '/dev/log'} ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) assert len(rlog.getlogger().logger.handlers) == 3 def test_file_handler_timestamp(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'timestamp': '%F', 'append': True, }, ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') base, ext = os.path.splitext(logfile) filename = f""{base}", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_logging.py::TestLogger::test_custom_handler_levels,unittests/test_logging.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import copy import logging import logging.handlers import os import pytest import re import sys import tempfile import time import unittest from datetime import datetime import reframe as rfm import reframe.core.logging as rlog import reframe.core.runtime as rt import reframe.core.settings as settings import reframe.utility as util from reframe.core.exceptions import ConfigError, ReframeError from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.schedulers import Job class _FakeCheck(rfm.RegressionTest): pass def _setup_fake_check(): # A bit hacky, but we don't want to run a full test every time test = _FakeCheck() test._job = Job.create(getscheduler('local')(), getlauncher('local')(), 'fakejob') test.job._completion_time = time.time() return test class TestLogger(unittest.TestCase): def setUp(self): tmpfd, self.logfile = tempfile.mkstemp() os.close(tmpfd) self.logger = rlog.Logger('reframe') self.handler = logging.handlers.RotatingFileHandler(self.logfile) self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T') self.handler.setFormatter(self.formatter) self.logger.addHandler(self.handler) # Use the logger adapter that defines check_name self.logger_without_check = rlog.LoggerAdapter(self.logger) # Logger adapter with an associated check self.logger_with_check = rlog.LoggerAdapter(self.logger, _setup_fake_check()) def tearDown(self): os.remove(self.logfile) def found_in_logfile(self, pattern): found = False with open(self.logfile, 'rt') as fp: found = re.search(pattern, fp.read()) is not None return found def test_invalid_loglevel(self): with pytest.raises(ValueError): self.logger.setLevel('level') with pytest.raises(ValueError): rlog.Logger('logger', 'level') def test_custom_loglevels(self): self.logger_without_check.info('foo') self.logger_without_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('reframe') def test_check_logger(self): self.logger_with_check.info('foo') self.logger_with_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('_FakeCheck') def test_handler_types(self): assert issubclass(logging.Handler, rlog.Handler) assert issubclass(logging.StreamHandler, rlog.Handler) assert issubclass(logging.FileHandler, rlog.Handler) assert issubclass(logging.handlers.RotatingFileHandler, rlog.Handler) # Try to instantiate rlog.Handler with pytest.raises(TypeError): rlog.Handler() def test_custom_handler_levels(self): self.handler.setLevel('verbose') self.handler.setLevel(rlog.VERBOSE) self.logger_with_check.debug('foo') self.logger_with_check.verbose('bar') assert not self.found_in_logfile('foo') assert self.found_in_logfile('bar') def test_logger_levels(self): self.logger_with_check.setLevel('verbose') self.logger_with_check.setLevel(rlog.VERBOSE) self.logger_with_check.debug('bar') self.logger_with_check.verbose('foo') assert not self.found_in_logfile('bar') assert self.found_in_logfile('foo') def test_rfc3339_timezone_extension(self): self.formatter = rlog.RFC3339Formatter( fmt=('[%(asctime)s] %(levelname)s: %(check_name)s: ' 'ct:%(check_job_completion_time)s: %(message)s'), datefmt='%FT%T%:z') self.handler.setFormatter(self.formatter) self.logger_with_check.info('foo') self.logger_without_check.info('foo') assert not self.found_in_logfile(r'%%:z') assert self.found_in_logfile(r'\[.+(\+|-)\d\d:\d\d\]') assert self.found_in_logfile(r'ct:.+(\+|-)\d\d:\d\d') def test_rfc3339_timezone_wrong_directive(self): self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T:z') self.handler.setFormatter(self.formatter) self.logger_without_check.info('foo') assert self.found_in_logfile(':z') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(logging_config): site_config = copy.deepcopy(settings.site_configuration) site_config['logging'] = [logging_config] with tempfile.NamedTemporaryFile(mode='w+t', dir=str(tmp_path), suffix='.py', delete=False) as fp: fp.write(f'site_configuration = {util.ppretty(site_config)}') with rt.temp_runtime(fp.name): yield rt.runtime() return _temp_runtime @pytest.fixture def logfile(tmp_path): return str(tmp_path / 'test.log') @pytest.fixture def basic_config(temp_runtime, logfile): yield from temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'append': True, }, ], 'handlers_perflog': [] }) def _flush_handlers(): for h in rlog.getlogger().logger.handlers: h.flush() def _close_handlers(): for h in rlog.getlogger().logger.handlers: h.close() def _found_in_logfile(string, filename): _flush_handlers() _close_handlers() found = False with open(filename, 'rt') as fp: found = string in fp.read() return found def test_valid_level(basic_config): rlog.configure_logging(rt.runtime().site_config) assert rlog.INFO == rlog.getlogger().getEffectiveLevel() def test_handler_level(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().info('foo') rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_append(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_noappend(temp_runtime, logfile): runtime = temp_runtime( { 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: %(message)s', 'datefmt': '%F', 'append': False, } ], 'handlers_perflog': [] } ) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_date_format(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') assert _found_in_logfile(datetime.now().strftime('%F'), logfile) @pytest.fixture(params=['stdout', 'stderr']) def stream(request): return request.param def test_stream_handler(temp_runtime, logfile, stream): runtime = temp_runtime({ 'level': 'info', 'handlers': [{'type': 'stream', 'name': stream}], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) raw_logger = rlog.getlogger().logger assert len(raw_logger.handlers) == 1 handler = raw_logger.handlers[0] assert isinstance(handler, logging.StreamHandler) stream = sys.stdout if stream == 'stdout' else sys.stderr assert handler.stream == stream def test_multiple_handlers(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ {'type': 'stream', 'name': 'stderr'}, {'type': 'file', 'name': logfile}, {'type': 'syslog', 'address': '/dev/log'} ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) assert len(rlog.getlogger().logger.handlers) == 3 def test_file_handler_timestamp(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'timestamp': '%F', 'append': True, }, ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') base, ext = os.path.splitext(logfile) filename = f""{base}", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_logging.py::TestLogger::test_custom_loglevels,unittests/test_logging.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import copy import logging import logging.handlers import os import pytest import re import sys import tempfile import time import unittest from datetime import datetime import reframe as rfm import reframe.core.logging as rlog import reframe.core.runtime as rt import reframe.core.settings as settings import reframe.utility as util from reframe.core.exceptions import ConfigError, ReframeError from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.schedulers import Job class _FakeCheck(rfm.RegressionTest): pass def _setup_fake_check(): # A bit hacky, but we don't want to run a full test every time test = _FakeCheck() test._job = Job.create(getscheduler('local')(), getlauncher('local')(), 'fakejob') test.job._completion_time = time.time() return test class TestLogger(unittest.TestCase): def setUp(self): tmpfd, self.logfile = tempfile.mkstemp() os.close(tmpfd) self.logger = rlog.Logger('reframe') self.handler = logging.handlers.RotatingFileHandler(self.logfile) self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T') self.handler.setFormatter(self.formatter) self.logger.addHandler(self.handler) # Use the logger adapter that defines check_name self.logger_without_check = rlog.LoggerAdapter(self.logger) # Logger adapter with an associated check self.logger_with_check = rlog.LoggerAdapter(self.logger, _setup_fake_check()) def tearDown(self): os.remove(self.logfile) def found_in_logfile(self, pattern): found = False with open(self.logfile, 'rt') as fp: found = re.search(pattern, fp.read()) is not None return found def test_invalid_loglevel(self): with pytest.raises(ValueError): self.logger.setLevel('level') with pytest.raises(ValueError): rlog.Logger('logger', 'level') def test_custom_loglevels(self): self.logger_without_check.info('foo') self.logger_without_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('reframe') def test_check_logger(self): self.logger_with_check.info('foo') self.logger_with_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('_FakeCheck') def test_handler_types(self): assert issubclass(logging.Handler, rlog.Handler) assert issubclass(logging.StreamHandler, rlog.Handler) assert issubclass(logging.FileHandler, rlog.Handler) assert issubclass(logging.handlers.RotatingFileHandler, rlog.Handler) # Try to instantiate rlog.Handler with pytest.raises(TypeError): rlog.Handler() def test_custom_handler_levels(self): self.handler.setLevel('verbose') self.handler.setLevel(rlog.VERBOSE) self.logger_with_check.debug('foo') self.logger_with_check.verbose('bar') assert not self.found_in_logfile('foo') assert self.found_in_logfile('bar') def test_logger_levels(self): self.logger_with_check.setLevel('verbose') self.logger_with_check.setLevel(rlog.VERBOSE) self.logger_with_check.debug('bar') self.logger_with_check.verbose('foo') assert not self.found_in_logfile('bar') assert self.found_in_logfile('foo') def test_rfc3339_timezone_extension(self): self.formatter = rlog.RFC3339Formatter( fmt=('[%(asctime)s] %(levelname)s: %(check_name)s: ' 'ct:%(check_job_completion_time)s: %(message)s'), datefmt='%FT%T%:z') self.handler.setFormatter(self.formatter) self.logger_with_check.info('foo') self.logger_without_check.info('foo') assert not self.found_in_logfile(r'%%:z') assert self.found_in_logfile(r'\[.+(\+|-)\d\d:\d\d\]') assert self.found_in_logfile(r'ct:.+(\+|-)\d\d:\d\d') def test_rfc3339_timezone_wrong_directive(self): self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T:z') self.handler.setFormatter(self.formatter) self.logger_without_check.info('foo') assert self.found_in_logfile(':z') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(logging_config): site_config = copy.deepcopy(settings.site_configuration) site_config['logging'] = [logging_config] with tempfile.NamedTemporaryFile(mode='w+t', dir=str(tmp_path), suffix='.py', delete=False) as fp: fp.write(f'site_configuration = {util.ppretty(site_config)}') with rt.temp_runtime(fp.name): yield rt.runtime() return _temp_runtime @pytest.fixture def logfile(tmp_path): return str(tmp_path / 'test.log') @pytest.fixture def basic_config(temp_runtime, logfile): yield from temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'append': True, }, ], 'handlers_perflog': [] }) def _flush_handlers(): for h in rlog.getlogger().logger.handlers: h.flush() def _close_handlers(): for h in rlog.getlogger().logger.handlers: h.close() def _found_in_logfile(string, filename): _flush_handlers() _close_handlers() found = False with open(filename, 'rt') as fp: found = string in fp.read() return found def test_valid_level(basic_config): rlog.configure_logging(rt.runtime().site_config) assert rlog.INFO == rlog.getlogger().getEffectiveLevel() def test_handler_level(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().info('foo') rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_append(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_noappend(temp_runtime, logfile): runtime = temp_runtime( { 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: %(message)s', 'datefmt': '%F', 'append': False, } ], 'handlers_perflog': [] } ) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_date_format(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') assert _found_in_logfile(datetime.now().strftime('%F'), logfile) @pytest.fixture(params=['stdout', 'stderr']) def stream(request): return request.param def test_stream_handler(temp_runtime, logfile, stream): runtime = temp_runtime({ 'level': 'info', 'handlers': [{'type': 'stream', 'name': stream}], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) raw_logger = rlog.getlogger().logger assert len(raw_logger.handlers) == 1 handler = raw_logger.handlers[0] assert isinstance(handler, logging.StreamHandler) stream = sys.stdout if stream == 'stdout' else sys.stderr assert handler.stream == stream def test_multiple_handlers(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ {'type': 'stream', 'name': 'stderr'}, {'type': 'file', 'name': logfile}, {'type': 'syslog', 'address': '/dev/log'} ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) assert len(rlog.getlogger().logger.handlers) == 3 def test_file_handler_timestamp(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'timestamp': '%F', 'append': True, }, ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') base, ext = os.path.splitext(logfile) filename = f""{base}", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_logging.py::TestLogger::test_handler_types,unittests/test_logging.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import copy import logging import logging.handlers import os import pytest import re import sys import tempfile import time import unittest from datetime import datetime import reframe as rfm import reframe.core.logging as rlog import reframe.core.runtime as rt import reframe.core.settings as settings import reframe.utility as util from reframe.core.exceptions import ConfigError, ReframeError from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.schedulers import Job class _FakeCheck(rfm.RegressionTest): pass def _setup_fake_check(): # A bit hacky, but we don't want to run a full test every time test = _FakeCheck() test._job = Job.create(getscheduler('local')(), getlauncher('local')(), 'fakejob') test.job._completion_time = time.time() return test class TestLogger(unittest.TestCase): def setUp(self): tmpfd, self.logfile = tempfile.mkstemp() os.close(tmpfd) self.logger = rlog.Logger('reframe') self.handler = logging.handlers.RotatingFileHandler(self.logfile) self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T') self.handler.setFormatter(self.formatter) self.logger.addHandler(self.handler) # Use the logger adapter that defines check_name self.logger_without_check = rlog.LoggerAdapter(self.logger) # Logger adapter with an associated check self.logger_with_check = rlog.LoggerAdapter(self.logger, _setup_fake_check()) def tearDown(self): os.remove(self.logfile) def found_in_logfile(self, pattern): found = False with open(self.logfile, 'rt') as fp: found = re.search(pattern, fp.read()) is not None return found def test_invalid_loglevel(self): with pytest.raises(ValueError): self.logger.setLevel('level') with pytest.raises(ValueError): rlog.Logger('logger', 'level') def test_custom_loglevels(self): self.logger_without_check.info('foo') self.logger_without_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('reframe') def test_check_logger(self): self.logger_with_check.info('foo') self.logger_with_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('_FakeCheck') def test_handler_types(self): assert issubclass(logging.Handler, rlog.Handler) assert issubclass(logging.StreamHandler, rlog.Handler) assert issubclass(logging.FileHandler, rlog.Handler) assert issubclass(logging.handlers.RotatingFileHandler, rlog.Handler) # Try to instantiate rlog.Handler with pytest.raises(TypeError): rlog.Handler() def test_custom_handler_levels(self): self.handler.setLevel('verbose') self.handler.setLevel(rlog.VERBOSE) self.logger_with_check.debug('foo') self.logger_with_check.verbose('bar') assert not self.found_in_logfile('foo') assert self.found_in_logfile('bar') def test_logger_levels(self): self.logger_with_check.setLevel('verbose') self.logger_with_check.setLevel(rlog.VERBOSE) self.logger_with_check.debug('bar') self.logger_with_check.verbose('foo') assert not self.found_in_logfile('bar') assert self.found_in_logfile('foo') def test_rfc3339_timezone_extension(self): self.formatter = rlog.RFC3339Formatter( fmt=('[%(asctime)s] %(levelname)s: %(check_name)s: ' 'ct:%(check_job_completion_time)s: %(message)s'), datefmt='%FT%T%:z') self.handler.setFormatter(self.formatter) self.logger_with_check.info('foo') self.logger_without_check.info('foo') assert not self.found_in_logfile(r'%%:z') assert self.found_in_logfile(r'\[.+(\+|-)\d\d:\d\d\]') assert self.found_in_logfile(r'ct:.+(\+|-)\d\d:\d\d') def test_rfc3339_timezone_wrong_directive(self): self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T:z') self.handler.setFormatter(self.formatter) self.logger_without_check.info('foo') assert self.found_in_logfile(':z') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(logging_config): site_config = copy.deepcopy(settings.site_configuration) site_config['logging'] = [logging_config] with tempfile.NamedTemporaryFile(mode='w+t', dir=str(tmp_path), suffix='.py', delete=False) as fp: fp.write(f'site_configuration = {util.ppretty(site_config)}') with rt.temp_runtime(fp.name): yield rt.runtime() return _temp_runtime @pytest.fixture def logfile(tmp_path): return str(tmp_path / 'test.log') @pytest.fixture def basic_config(temp_runtime, logfile): yield from temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'append': True, }, ], 'handlers_perflog': [] }) def _flush_handlers(): for h in rlog.getlogger().logger.handlers: h.flush() def _close_handlers(): for h in rlog.getlogger().logger.handlers: h.close() def _found_in_logfile(string, filename): _flush_handlers() _close_handlers() found = False with open(filename, 'rt') as fp: found = string in fp.read() return found def test_valid_level(basic_config): rlog.configure_logging(rt.runtime().site_config) assert rlog.INFO == rlog.getlogger().getEffectiveLevel() def test_handler_level(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().info('foo') rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_append(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_noappend(temp_runtime, logfile): runtime = temp_runtime( { 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: %(message)s', 'datefmt': '%F', 'append': False, } ], 'handlers_perflog': [] } ) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_date_format(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') assert _found_in_logfile(datetime.now().strftime('%F'), logfile) @pytest.fixture(params=['stdout', 'stderr']) def stream(request): return request.param def test_stream_handler(temp_runtime, logfile, stream): runtime = temp_runtime({ 'level': 'info', 'handlers': [{'type': 'stream', 'name': stream}], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) raw_logger = rlog.getlogger().logger assert len(raw_logger.handlers) == 1 handler = raw_logger.handlers[0] assert isinstance(handler, logging.StreamHandler) stream = sys.stdout if stream == 'stdout' else sys.stderr assert handler.stream == stream def test_multiple_handlers(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ {'type': 'stream', 'name': 'stderr'}, {'type': 'file', 'name': logfile}, {'type': 'syslog', 'address': '/dev/log'} ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) assert len(rlog.getlogger().logger.handlers) == 3 def test_file_handler_timestamp(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'timestamp': '%F', 'append': True, }, ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') base, ext = os.path.splitext(logfile) filename = f""{base}", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_logging.py::TestLogger::test_invalid_loglevel,unittests/test_logging.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import copy import logging import logging.handlers import os import pytest import re import sys import tempfile import time import unittest from datetime import datetime import reframe as rfm import reframe.core.logging as rlog import reframe.core.runtime as rt import reframe.core.settings as settings import reframe.utility as util from reframe.core.exceptions import ConfigError, ReframeError from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.schedulers import Job class _FakeCheck(rfm.RegressionTest): pass def _setup_fake_check(): # A bit hacky, but we don't want to run a full test every time test = _FakeCheck() test._job = Job.create(getscheduler('local')(), getlauncher('local')(), 'fakejob') test.job._completion_time = time.time() return test class TestLogger(unittest.TestCase): def setUp(self): tmpfd, self.logfile = tempfile.mkstemp() os.close(tmpfd) self.logger = rlog.Logger('reframe') self.handler = logging.handlers.RotatingFileHandler(self.logfile) self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T') self.handler.setFormatter(self.formatter) self.logger.addHandler(self.handler) # Use the logger adapter that defines check_name self.logger_without_check = rlog.LoggerAdapter(self.logger) # Logger adapter with an associated check self.logger_with_check = rlog.LoggerAdapter(self.logger, _setup_fake_check()) def tearDown(self): os.remove(self.logfile) def found_in_logfile(self, pattern): found = False with open(self.logfile, 'rt') as fp: found = re.search(pattern, fp.read()) is not None return found def test_invalid_loglevel(self): with pytest.raises(ValueError): self.logger.setLevel('level') with pytest.raises(ValueError): rlog.Logger('logger', 'level') def test_custom_loglevels(self): self.logger_without_check.info('foo') self.logger_without_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('reframe') def test_check_logger(self): self.logger_with_check.info('foo') self.logger_with_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('_FakeCheck') def test_handler_types(self): assert issubclass(logging.Handler, rlog.Handler) assert issubclass(logging.StreamHandler, rlog.Handler) assert issubclass(logging.FileHandler, rlog.Handler) assert issubclass(logging.handlers.RotatingFileHandler, rlog.Handler) # Try to instantiate rlog.Handler with pytest.raises(TypeError): rlog.Handler() def test_custom_handler_levels(self): self.handler.setLevel('verbose') self.handler.setLevel(rlog.VERBOSE) self.logger_with_check.debug('foo') self.logger_with_check.verbose('bar') assert not self.found_in_logfile('foo') assert self.found_in_logfile('bar') def test_logger_levels(self): self.logger_with_check.setLevel('verbose') self.logger_with_check.setLevel(rlog.VERBOSE) self.logger_with_check.debug('bar') self.logger_with_check.verbose('foo') assert not self.found_in_logfile('bar') assert self.found_in_logfile('foo') def test_rfc3339_timezone_extension(self): self.formatter = rlog.RFC3339Formatter( fmt=('[%(asctime)s] %(levelname)s: %(check_name)s: ' 'ct:%(check_job_completion_time)s: %(message)s'), datefmt='%FT%T%:z') self.handler.setFormatter(self.formatter) self.logger_with_check.info('foo') self.logger_without_check.info('foo') assert not self.found_in_logfile(r'%%:z') assert self.found_in_logfile(r'\[.+(\+|-)\d\d:\d\d\]') assert self.found_in_logfile(r'ct:.+(\+|-)\d\d:\d\d') def test_rfc3339_timezone_wrong_directive(self): self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T:z') self.handler.setFormatter(self.formatter) self.logger_without_check.info('foo') assert self.found_in_logfile(':z') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(logging_config): site_config = copy.deepcopy(settings.site_configuration) site_config['logging'] = [logging_config] with tempfile.NamedTemporaryFile(mode='w+t', dir=str(tmp_path), suffix='.py', delete=False) as fp: fp.write(f'site_configuration = {util.ppretty(site_config)}') with rt.temp_runtime(fp.name): yield rt.runtime() return _temp_runtime @pytest.fixture def logfile(tmp_path): return str(tmp_path / 'test.log') @pytest.fixture def basic_config(temp_runtime, logfile): yield from temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'append': True, }, ], 'handlers_perflog': [] }) def _flush_handlers(): for h in rlog.getlogger().logger.handlers: h.flush() def _close_handlers(): for h in rlog.getlogger().logger.handlers: h.close() def _found_in_logfile(string, filename): _flush_handlers() _close_handlers() found = False with open(filename, 'rt') as fp: found = string in fp.read() return found def test_valid_level(basic_config): rlog.configure_logging(rt.runtime().site_config) assert rlog.INFO == rlog.getlogger().getEffectiveLevel() def test_handler_level(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().info('foo') rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_append(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_noappend(temp_runtime, logfile): runtime = temp_runtime( { 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: %(message)s', 'datefmt': '%F', 'append': False, } ], 'handlers_perflog': [] } ) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_date_format(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') assert _found_in_logfile(datetime.now().strftime('%F'), logfile) @pytest.fixture(params=['stdout', 'stderr']) def stream(request): return request.param def test_stream_handler(temp_runtime, logfile, stream): runtime = temp_runtime({ 'level': 'info', 'handlers': [{'type': 'stream', 'name': stream}], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) raw_logger = rlog.getlogger().logger assert len(raw_logger.handlers) == 1 handler = raw_logger.handlers[0] assert isinstance(handler, logging.StreamHandler) stream = sys.stdout if stream == 'stdout' else sys.stderr assert handler.stream == stream def test_multiple_handlers(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ {'type': 'stream', 'name': 'stderr'}, {'type': 'file', 'name': logfile}, {'type': 'syslog', 'address': '/dev/log'} ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) assert len(rlog.getlogger().logger.handlers) == 3 def test_file_handler_timestamp(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'timestamp': '%F', 'append': True, }, ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') base, ext = os.path.splitext(logfile) filename = f""{base}", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_logging.py::TestLogger::test_logger_levels,unittests/test_logging.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import copy import logging import logging.handlers import os import pytest import re import sys import tempfile import time import unittest from datetime import datetime import reframe as rfm import reframe.core.logging as rlog import reframe.core.runtime as rt import reframe.core.settings as settings import reframe.utility as util from reframe.core.exceptions import ConfigError, ReframeError from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.schedulers import Job class _FakeCheck(rfm.RegressionTest): pass def _setup_fake_check(): # A bit hacky, but we don't want to run a full test every time test = _FakeCheck() test._job = Job.create(getscheduler('local')(), getlauncher('local')(), 'fakejob') test.job._completion_time = time.time() return test class TestLogger(unittest.TestCase): def setUp(self): tmpfd, self.logfile = tempfile.mkstemp() os.close(tmpfd) self.logger = rlog.Logger('reframe') self.handler = logging.handlers.RotatingFileHandler(self.logfile) self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T') self.handler.setFormatter(self.formatter) self.logger.addHandler(self.handler) # Use the logger adapter that defines check_name self.logger_without_check = rlog.LoggerAdapter(self.logger) # Logger adapter with an associated check self.logger_with_check = rlog.LoggerAdapter(self.logger, _setup_fake_check()) def tearDown(self): os.remove(self.logfile) def found_in_logfile(self, pattern): found = False with open(self.logfile, 'rt') as fp: found = re.search(pattern, fp.read()) is not None return found def test_invalid_loglevel(self): with pytest.raises(ValueError): self.logger.setLevel('level') with pytest.raises(ValueError): rlog.Logger('logger', 'level') def test_custom_loglevels(self): self.logger_without_check.info('foo') self.logger_without_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('reframe') def test_check_logger(self): self.logger_with_check.info('foo') self.logger_with_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('_FakeCheck') def test_handler_types(self): assert issubclass(logging.Handler, rlog.Handler) assert issubclass(logging.StreamHandler, rlog.Handler) assert issubclass(logging.FileHandler, rlog.Handler) assert issubclass(logging.handlers.RotatingFileHandler, rlog.Handler) # Try to instantiate rlog.Handler with pytest.raises(TypeError): rlog.Handler() def test_custom_handler_levels(self): self.handler.setLevel('verbose') self.handler.setLevel(rlog.VERBOSE) self.logger_with_check.debug('foo') self.logger_with_check.verbose('bar') assert not self.found_in_logfile('foo') assert self.found_in_logfile('bar') def test_logger_levels(self): self.logger_with_check.setLevel('verbose') self.logger_with_check.setLevel(rlog.VERBOSE) self.logger_with_check.debug('bar') self.logger_with_check.verbose('foo') assert not self.found_in_logfile('bar') assert self.found_in_logfile('foo') def test_rfc3339_timezone_extension(self): self.formatter = rlog.RFC3339Formatter( fmt=('[%(asctime)s] %(levelname)s: %(check_name)s: ' 'ct:%(check_job_completion_time)s: %(message)s'), datefmt='%FT%T%:z') self.handler.setFormatter(self.formatter) self.logger_with_check.info('foo') self.logger_without_check.info('foo') assert not self.found_in_logfile(r'%%:z') assert self.found_in_logfile(r'\[.+(\+|-)\d\d:\d\d\]') assert self.found_in_logfile(r'ct:.+(\+|-)\d\d:\d\d') def test_rfc3339_timezone_wrong_directive(self): self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T:z') self.handler.setFormatter(self.formatter) self.logger_without_check.info('foo') assert self.found_in_logfile(':z') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(logging_config): site_config = copy.deepcopy(settings.site_configuration) site_config['logging'] = [logging_config] with tempfile.NamedTemporaryFile(mode='w+t', dir=str(tmp_path), suffix='.py', delete=False) as fp: fp.write(f'site_configuration = {util.ppretty(site_config)}') with rt.temp_runtime(fp.name): yield rt.runtime() return _temp_runtime @pytest.fixture def logfile(tmp_path): return str(tmp_path / 'test.log') @pytest.fixture def basic_config(temp_runtime, logfile): yield from temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'append': True, }, ], 'handlers_perflog': [] }) def _flush_handlers(): for h in rlog.getlogger().logger.handlers: h.flush() def _close_handlers(): for h in rlog.getlogger().logger.handlers: h.close() def _found_in_logfile(string, filename): _flush_handlers() _close_handlers() found = False with open(filename, 'rt') as fp: found = string in fp.read() return found def test_valid_level(basic_config): rlog.configure_logging(rt.runtime().site_config) assert rlog.INFO == rlog.getlogger().getEffectiveLevel() def test_handler_level(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().info('foo') rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_append(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_noappend(temp_runtime, logfile): runtime = temp_runtime( { 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: %(message)s', 'datefmt': '%F', 'append': False, } ], 'handlers_perflog': [] } ) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_date_format(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') assert _found_in_logfile(datetime.now().strftime('%F'), logfile) @pytest.fixture(params=['stdout', 'stderr']) def stream(request): return request.param def test_stream_handler(temp_runtime, logfile, stream): runtime = temp_runtime({ 'level': 'info', 'handlers': [{'type': 'stream', 'name': stream}], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) raw_logger = rlog.getlogger().logger assert len(raw_logger.handlers) == 1 handler = raw_logger.handlers[0] assert isinstance(handler, logging.StreamHandler) stream = sys.stdout if stream == 'stdout' else sys.stderr assert handler.stream == stream def test_multiple_handlers(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ {'type': 'stream', 'name': 'stderr'}, {'type': 'file', 'name': logfile}, {'type': 'syslog', 'address': '/dev/log'} ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) assert len(rlog.getlogger().logger.handlers) == 3 def test_file_handler_timestamp(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'timestamp': '%F', 'append': True, }, ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') base, ext = os.path.splitext(logfile) filename = f""{base}", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_logging.py::TestLogger::test_rfc3339_timezone_extension,unittests/test_logging.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import copy import logging import logging.handlers import os import pytest import re import sys import tempfile import time import unittest from datetime import datetime import reframe as rfm import reframe.core.logging as rlog import reframe.core.runtime as rt import reframe.core.settings as settings import reframe.utility as util from reframe.core.exceptions import ConfigError, ReframeError from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.schedulers import Job class _FakeCheck(rfm.RegressionTest): pass def _setup_fake_check(): # A bit hacky, but we don't want to run a full test every time test = _FakeCheck() test._job = Job.create(getscheduler('local')(), getlauncher('local')(), 'fakejob') test.job._completion_time = time.time() return test class TestLogger(unittest.TestCase): def setUp(self): tmpfd, self.logfile = tempfile.mkstemp() os.close(tmpfd) self.logger = rlog.Logger('reframe') self.handler = logging.handlers.RotatingFileHandler(self.logfile) self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T') self.handler.setFormatter(self.formatter) self.logger.addHandler(self.handler) # Use the logger adapter that defines check_name self.logger_without_check = rlog.LoggerAdapter(self.logger) # Logger adapter with an associated check self.logger_with_check = rlog.LoggerAdapter(self.logger, _setup_fake_check()) def tearDown(self): os.remove(self.logfile) def found_in_logfile(self, pattern): found = False with open(self.logfile, 'rt') as fp: found = re.search(pattern, fp.read()) is not None return found def test_invalid_loglevel(self): with pytest.raises(ValueError): self.logger.setLevel('level') with pytest.raises(ValueError): rlog.Logger('logger', 'level') def test_custom_loglevels(self): self.logger_without_check.info('foo') self.logger_without_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('reframe') def test_check_logger(self): self.logger_with_check.info('foo') self.logger_with_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('_FakeCheck') def test_handler_types(self): assert issubclass(logging.Handler, rlog.Handler) assert issubclass(logging.StreamHandler, rlog.Handler) assert issubclass(logging.FileHandler, rlog.Handler) assert issubclass(logging.handlers.RotatingFileHandler, rlog.Handler) # Try to instantiate rlog.Handler with pytest.raises(TypeError): rlog.Handler() def test_custom_handler_levels(self): self.handler.setLevel('verbose') self.handler.setLevel(rlog.VERBOSE) self.logger_with_check.debug('foo') self.logger_with_check.verbose('bar') assert not self.found_in_logfile('foo') assert self.found_in_logfile('bar') def test_logger_levels(self): self.logger_with_check.setLevel('verbose') self.logger_with_check.setLevel(rlog.VERBOSE) self.logger_with_check.debug('bar') self.logger_with_check.verbose('foo') assert not self.found_in_logfile('bar') assert self.found_in_logfile('foo') def test_rfc3339_timezone_extension(self): self.formatter = rlog.RFC3339Formatter( fmt=('[%(asctime)s] %(levelname)s: %(check_name)s: ' 'ct:%(check_job_completion_time)s: %(message)s'), datefmt='%FT%T%:z') self.handler.setFormatter(self.formatter) self.logger_with_check.info('foo') self.logger_without_check.info('foo') assert not self.found_in_logfile(r'%%:z') assert self.found_in_logfile(r'\[.+(\+|-)\d\d:\d\d\]') assert self.found_in_logfile(r'ct:.+(\+|-)\d\d:\d\d') def test_rfc3339_timezone_wrong_directive(self): self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T:z') self.handler.setFormatter(self.formatter) self.logger_without_check.info('foo') assert self.found_in_logfile(':z') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(logging_config): site_config = copy.deepcopy(settings.site_configuration) site_config['logging'] = [logging_config] with tempfile.NamedTemporaryFile(mode='w+t', dir=str(tmp_path), suffix='.py', delete=False) as fp: fp.write(f'site_configuration = {util.ppretty(site_config)}') with rt.temp_runtime(fp.name): yield rt.runtime() return _temp_runtime @pytest.fixture def logfile(tmp_path): return str(tmp_path / 'test.log') @pytest.fixture def basic_config(temp_runtime, logfile): yield from temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'append': True, }, ], 'handlers_perflog': [] }) def _flush_handlers(): for h in rlog.getlogger().logger.handlers: h.flush() def _close_handlers(): for h in rlog.getlogger().logger.handlers: h.close() def _found_in_logfile(string, filename): _flush_handlers() _close_handlers() found = False with open(filename, 'rt') as fp: found = string in fp.read() return found def test_valid_level(basic_config): rlog.configure_logging(rt.runtime().site_config) assert rlog.INFO == rlog.getlogger().getEffectiveLevel() def test_handler_level(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().info('foo') rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_append(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_noappend(temp_runtime, logfile): runtime = temp_runtime( { 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: %(message)s', 'datefmt': '%F', 'append': False, } ], 'handlers_perflog': [] } ) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_date_format(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') assert _found_in_logfile(datetime.now().strftime('%F'), logfile) @pytest.fixture(params=['stdout', 'stderr']) def stream(request): return request.param def test_stream_handler(temp_runtime, logfile, stream): runtime = temp_runtime({ 'level': 'info', 'handlers': [{'type': 'stream', 'name': stream}], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) raw_logger = rlog.getlogger().logger assert len(raw_logger.handlers) == 1 handler = raw_logger.handlers[0] assert isinstance(handler, logging.StreamHandler) stream = sys.stdout if stream == 'stdout' else sys.stderr assert handler.stream == stream def test_multiple_handlers(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ {'type': 'stream', 'name': 'stderr'}, {'type': 'file', 'name': logfile}, {'type': 'syslog', 'address': '/dev/log'} ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) assert len(rlog.getlogger().logger.handlers) == 3 def test_file_handler_timestamp(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'timestamp': '%F', 'append': True, }, ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') base, ext = os.path.splitext(logfile) filename = f""{base}", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_logging.py::TestLogger::test_rfc3339_timezone_wrong_directive,unittests/test_logging.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import copy import logging import logging.handlers import os import pytest import re import sys import tempfile import time import unittest from datetime import datetime import reframe as rfm import reframe.core.logging as rlog import reframe.core.runtime as rt import reframe.core.settings as settings import reframe.utility as util from reframe.core.exceptions import ConfigError, ReframeError from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.schedulers import Job class _FakeCheck(rfm.RegressionTest): pass def _setup_fake_check(): # A bit hacky, but we don't want to run a full test every time test = _FakeCheck() test._job = Job.create(getscheduler('local')(), getlauncher('local')(), 'fakejob') test.job._completion_time = time.time() return test class TestLogger(unittest.TestCase): def setUp(self): tmpfd, self.logfile = tempfile.mkstemp() os.close(tmpfd) self.logger = rlog.Logger('reframe') self.handler = logging.handlers.RotatingFileHandler(self.logfile) self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T') self.handler.setFormatter(self.formatter) self.logger.addHandler(self.handler) # Use the logger adapter that defines check_name self.logger_without_check = rlog.LoggerAdapter(self.logger) # Logger adapter with an associated check self.logger_with_check = rlog.LoggerAdapter(self.logger, _setup_fake_check()) def tearDown(self): os.remove(self.logfile) def found_in_logfile(self, pattern): found = False with open(self.logfile, 'rt') as fp: found = re.search(pattern, fp.read()) is not None return found def test_invalid_loglevel(self): with pytest.raises(ValueError): self.logger.setLevel('level') with pytest.raises(ValueError): rlog.Logger('logger', 'level') def test_custom_loglevels(self): self.logger_without_check.info('foo') self.logger_without_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('reframe') def test_check_logger(self): self.logger_with_check.info('foo') self.logger_with_check.verbose('bar') assert os.path.exists(self.logfile) assert self.found_in_logfile('info') assert self.found_in_logfile('verbose') assert self.found_in_logfile('_FakeCheck') def test_handler_types(self): assert issubclass(logging.Handler, rlog.Handler) assert issubclass(logging.StreamHandler, rlog.Handler) assert issubclass(logging.FileHandler, rlog.Handler) assert issubclass(logging.handlers.RotatingFileHandler, rlog.Handler) # Try to instantiate rlog.Handler with pytest.raises(TypeError): rlog.Handler() def test_custom_handler_levels(self): self.handler.setLevel('verbose') self.handler.setLevel(rlog.VERBOSE) self.logger_with_check.debug('foo') self.logger_with_check.verbose('bar') assert not self.found_in_logfile('foo') assert self.found_in_logfile('bar') def test_logger_levels(self): self.logger_with_check.setLevel('verbose') self.logger_with_check.setLevel(rlog.VERBOSE) self.logger_with_check.debug('bar') self.logger_with_check.verbose('foo') assert not self.found_in_logfile('bar') assert self.found_in_logfile('foo') def test_rfc3339_timezone_extension(self): self.formatter = rlog.RFC3339Formatter( fmt=('[%(asctime)s] %(levelname)s: %(check_name)s: ' 'ct:%(check_job_completion_time)s: %(message)s'), datefmt='%FT%T%:z') self.handler.setFormatter(self.formatter) self.logger_with_check.info('foo') self.logger_without_check.info('foo') assert not self.found_in_logfile(r'%%:z') assert self.found_in_logfile(r'\[.+(\+|-)\d\d:\d\d\]') assert self.found_in_logfile(r'ct:.+(\+|-)\d\d:\d\d') def test_rfc3339_timezone_wrong_directive(self): self.formatter = rlog.RFC3339Formatter( fmt='[%(asctime)s] %(levelname)s: %(check_name)s: %(message)s', datefmt='%FT%T:z') self.handler.setFormatter(self.formatter) self.logger_without_check.info('foo') assert self.found_in_logfile(':z') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(logging_config): site_config = copy.deepcopy(settings.site_configuration) site_config['logging'] = [logging_config] with tempfile.NamedTemporaryFile(mode='w+t', dir=str(tmp_path), suffix='.py', delete=False) as fp: fp.write(f'site_configuration = {util.ppretty(site_config)}') with rt.temp_runtime(fp.name): yield rt.runtime() return _temp_runtime @pytest.fixture def logfile(tmp_path): return str(tmp_path / 'test.log') @pytest.fixture def basic_config(temp_runtime, logfile): yield from temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'append': True, }, ], 'handlers_perflog': [] }) def _flush_handlers(): for h in rlog.getlogger().logger.handlers: h.flush() def _close_handlers(): for h in rlog.getlogger().logger.handlers: h.close() def _found_in_logfile(string, filename): _flush_handlers() _close_handlers() found = False with open(filename, 'rt') as fp: found = string in fp.read() return found def test_valid_level(basic_config): rlog.configure_logging(rt.runtime().site_config) assert rlog.INFO == rlog.getlogger().getEffectiveLevel() def test_handler_level(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().info('foo') rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_append(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_handler_noappend(temp_runtime, logfile): runtime = temp_runtime( { 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: %(message)s', 'datefmt': '%F', 'append': False, } ], 'handlers_perflog': [] } ) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') _close_handlers() # Reload logger rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('bar') assert not _found_in_logfile('foo', logfile) assert _found_in_logfile('bar', logfile) def test_date_format(basic_config, logfile): rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') assert _found_in_logfile(datetime.now().strftime('%F'), logfile) @pytest.fixture(params=['stdout', 'stderr']) def stream(request): return request.param def test_stream_handler(temp_runtime, logfile, stream): runtime = temp_runtime({ 'level': 'info', 'handlers': [{'type': 'stream', 'name': stream}], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) raw_logger = rlog.getlogger().logger assert len(raw_logger.handlers) == 1 handler = raw_logger.handlers[0] assert isinstance(handler, logging.StreamHandler) stream = sys.stdout if stream == 'stdout' else sys.stderr assert handler.stream == stream def test_multiple_handlers(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ {'type': 'stream', 'name': 'stderr'}, {'type': 'file', 'name': logfile}, {'type': 'syslog', 'address': '/dev/log'} ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) assert len(rlog.getlogger().logger.handlers) == 3 def test_file_handler_timestamp(temp_runtime, logfile): runtime = temp_runtime({ 'level': 'info', 'handlers': [ { 'type': 'file', 'name': logfile, 'level': 'warning', 'format': '[%(asctime)s] %(levelname)s: ' '%(check_name)s: %(message)s', 'datefmt': '%F', 'timestamp': '%F', 'append': True, }, ], 'handlers_perflog': [] }) next(runtime) rlog.configure_logging(rt.runtime().site_config) rlog.getlogger().warning('foo') base, ext = os.path.splitext(logfile) filename = f""{base}", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_pipeline.py::test_environ_setup,unittests/test_pipeline.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pathlib import pytest import re import reframe as rfm import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.exceptions import (BuildError, PipelineError, ReframeError, ReframeSyntaxError, PerformanceError, SanityError) from reframe.frontend.loader import RegressionCheckLoader from unittests.resources.checks.hellocheck import HelloTest def _run(test, partition, prgenv): test.setup(partition, prgenv) test.compile() test.compile_wait() test.run() test.wait() test.check_sanity() test.check_performance() test.cleanup(remove_files=True) def load_test(testfile): loader = RegressionCheckLoader(['unittests/resources/checks']) return loader.load_from_file(testfile) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(config_file, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(config_file, system, options): yield rt.runtime() yield _temp_runtime @pytest.fixture def generic_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture def testsys_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') @pytest.fixture def user_system(temp_runtime): if fixtures.USER_CONFIG_FILE: yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: yield generic_system @pytest.fixture def hellotest(): yield load_test('unittests/resources/checks/hellocheck.py')[0] @pytest.fixture def local_exec_ctx(generic_system): partition = fixtures.partition_by_name('default') environ = fixtures.environment_by_name('builtin-gcc', partition) yield partition, environ @pytest.fixture def local_user_exec_ctx(user_system): partition = fixtures.partition_by_scheduler('local') if partition is None: pytest.skip('no local jobs are supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def container_remote_exec_ctx(remote_exec_ctx): def _container_exec_ctx(platform): partition = remote_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from remote_exec_ctx return _container_exec_ctx @pytest.fixture def container_local_exec_ctx(local_user_exec_ctx): def _container_exec_ctx(platform): partition = local_user_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from local_user_exec_ctx return _container_exec_ctx def test_environ_setup(hellotest, local_exec_ctx): # Use test environment for the regression check hellotest.variables = {'_FOO_': '1', '_BAR_': '2'} hellotest.setup(*local_exec_ctx) for k in hellotest.variables.keys(): assert k not in os.environ def test_hellocheck(hellotest, remote_exec_ctx): _run(hellotest, *remote_exec_ctx) def test_hellocheck_make(remote_exec_ctx): test = load_test('unittests/resources/checks/hellocheck_make.py')[0] _run(test, *remote_exec_ctx) def test_hellocheck_local(hellotest, local_exec_ctx): # Test also the prebuild/postbuild functionality hellotest.prebuild_cmds = ['touch prebuild', 'mkdir prebuild_dir'] hellotest.postbuild_cmds = ['touch postbuild', 'mkdir postbuild_dir'] hellotest.keep_files = ['prebuild', 'postbuild', 'prebuild_dir', 'postbuild_dir'] # Force local execution of the test; just for testing .local hellotest.local = True _run(hellotest, *local_exec_ctx) must_keep = [ hellotest.stdout.evaluate(), hellotest.stderr.evaluate(), hellotest.build_stdout.evaluate(), hellotest.build_stderr.evaluate(), hellotest.job.script_filename, *hellotest.keep_files ] for f in must_keep: assert os.path.exists(os.path.join(hellotest.outputdir, f)) def test_hellocheck_local_prepost_run(hellotest, local_exec_ctx): @sn.sanity_function def stagedir(test): return test.stagedir # Test also the prebuild/postbuild functionality hellotest.prerun_cmds = ['echo prerun: `pwd`'] hellotest.postrun_cmds = ['echo postrun: `pwd`'] pre_run_path = sn.extractsingle(r'^prerun: (\S+)', hellotest.stdout, 1) post_run_path = sn.extractsingle(r'^postrun: (\S+)', hellotest.stdout, 1) hellotest.sanity_patterns = sn.all([ sn.assert_eq(stagedir(hellotest), pre_run_path), sn.assert_eq(stagedir(hellotest), post_run_path), ]) _run(hellotest, *local_exec_ctx) def test_run_only_sanity(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = './hello.sh' self.executable_opts = ['Hello, World!'] self.local = True self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found( r'Hello, World\!', self.stdout) _run(MyTest(), *local_exec_ctx) def test_run_only_no_srcdir(local_exec_ctx): @fixtures.custom_prefix('foo/bar/') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = 'echo' self.executable_opts = ['hello'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'hello', self.stdout) test = MyTest() assert test.sourcesdir is None _run(test, *local_exec_ctx) def test_compile_only_failure(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.CompileOnlyRegressionTest): def __init__(self): self.sourcepath = 'compiler_failure.c' self.valid_prog_environs = ['*'] self.valid_systems = ['*'] test = MyTest() test.setup(*local_exec_ctx) test.compile() with pytest.raises(BuildError): test.compile_wait() def test_compile_only_warning(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.build_system = 'SingleSource' self.build_system.srcfile = 'compiler_warning.c' self.build_system.cflags = ['-Wall'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'warning', self.stderr) _run(MyTest(), *local_exec_ctx) def test_supports_system(hellotest, testsys_system): hellotest.valid_systems = ['*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:gpu'] assert hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:login'] assert not hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['foo'] assert not hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:gpu'] assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('foo:gpu') assert not hellotest.supports_system('testsys:cpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:*'] assert hellotest.supports_system('testsys:login') assert hellotest.supports_system('gpu') assert no", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_pipeline.py::test_hellocheck_local,unittests/test_pipeline.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pathlib import pytest import re import reframe as rfm import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.exceptions import (BuildError, PipelineError, ReframeError, ReframeSyntaxError, PerformanceError, SanityError) from reframe.frontend.loader import RegressionCheckLoader from unittests.resources.checks.hellocheck import HelloTest def _run(test, partition, prgenv): test.setup(partition, prgenv) test.compile() test.compile_wait() test.run() test.wait() test.check_sanity() test.check_performance() test.cleanup(remove_files=True) def load_test(testfile): loader = RegressionCheckLoader(['unittests/resources/checks']) return loader.load_from_file(testfile) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(config_file, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(config_file, system, options): yield rt.runtime() yield _temp_runtime @pytest.fixture def generic_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture def testsys_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') @pytest.fixture def user_system(temp_runtime): if fixtures.USER_CONFIG_FILE: yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: yield generic_system @pytest.fixture def hellotest(): yield load_test('unittests/resources/checks/hellocheck.py')[0] @pytest.fixture def local_exec_ctx(generic_system): partition = fixtures.partition_by_name('default') environ = fixtures.environment_by_name('builtin-gcc', partition) yield partition, environ @pytest.fixture def local_user_exec_ctx(user_system): partition = fixtures.partition_by_scheduler('local') if partition is None: pytest.skip('no local jobs are supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def container_remote_exec_ctx(remote_exec_ctx): def _container_exec_ctx(platform): partition = remote_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from remote_exec_ctx return _container_exec_ctx @pytest.fixture def container_local_exec_ctx(local_user_exec_ctx): def _container_exec_ctx(platform): partition = local_user_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from local_user_exec_ctx return _container_exec_ctx def test_environ_setup(hellotest, local_exec_ctx): # Use test environment for the regression check hellotest.variables = {'_FOO_': '1', '_BAR_': '2'} hellotest.setup(*local_exec_ctx) for k in hellotest.variables.keys(): assert k not in os.environ def test_hellocheck(hellotest, remote_exec_ctx): _run(hellotest, *remote_exec_ctx) def test_hellocheck_make(remote_exec_ctx): test = load_test('unittests/resources/checks/hellocheck_make.py')[0] _run(test, *remote_exec_ctx) def test_hellocheck_local(hellotest, local_exec_ctx): # Test also the prebuild/postbuild functionality hellotest.prebuild_cmds = ['touch prebuild', 'mkdir prebuild_dir'] hellotest.postbuild_cmds = ['touch postbuild', 'mkdir postbuild_dir'] hellotest.keep_files = ['prebuild', 'postbuild', 'prebuild_dir', 'postbuild_dir'] # Force local execution of the test; just for testing .local hellotest.local = True _run(hellotest, *local_exec_ctx) must_keep = [ hellotest.stdout.evaluate(), hellotest.stderr.evaluate(), hellotest.build_stdout.evaluate(), hellotest.build_stderr.evaluate(), hellotest.job.script_filename, *hellotest.keep_files ] for f in must_keep: assert os.path.exists(os.path.join(hellotest.outputdir, f)) def test_hellocheck_local_prepost_run(hellotest, local_exec_ctx): @sn.sanity_function def stagedir(test): return test.stagedir # Test also the prebuild/postbuild functionality hellotest.prerun_cmds = ['echo prerun: `pwd`'] hellotest.postrun_cmds = ['echo postrun: `pwd`'] pre_run_path = sn.extractsingle(r'^prerun: (\S+)', hellotest.stdout, 1) post_run_path = sn.extractsingle(r'^postrun: (\S+)', hellotest.stdout, 1) hellotest.sanity_patterns = sn.all([ sn.assert_eq(stagedir(hellotest), pre_run_path), sn.assert_eq(stagedir(hellotest), post_run_path), ]) _run(hellotest, *local_exec_ctx) def test_run_only_sanity(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = './hello.sh' self.executable_opts = ['Hello, World!'] self.local = True self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found( r'Hello, World\!', self.stdout) _run(MyTest(), *local_exec_ctx) def test_run_only_no_srcdir(local_exec_ctx): @fixtures.custom_prefix('foo/bar/') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = 'echo' self.executable_opts = ['hello'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'hello', self.stdout) test = MyTest() assert test.sourcesdir is None _run(test, *local_exec_ctx) def test_compile_only_failure(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.CompileOnlyRegressionTest): def __init__(self): self.sourcepath = 'compiler_failure.c' self.valid_prog_environs = ['*'] self.valid_systems = ['*'] test = MyTest() test.setup(*local_exec_ctx) test.compile() with pytest.raises(BuildError): test.compile_wait() def test_compile_only_warning(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.build_system = 'SingleSource' self.build_system.srcfile = 'compiler_warning.c' self.build_system.cflags = ['-Wall'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'warning', self.stderr) _run(MyTest(), *local_exec_ctx) def test_supports_system(hellotest, testsys_system): hellotest.valid_systems = ['*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:gpu'] assert hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:login'] assert not hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['foo'] assert not hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:gpu'] assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('foo:gpu') assert not hellotest.supports_system('testsys:cpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:*'] assert hellotest.supports_system('testsys:login') assert hellotest.supports_system('gpu') assert no", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_pipeline.py::test_hellocheck_local_prepost_run,unittests/test_pipeline.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pathlib import pytest import re import reframe as rfm import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.exceptions import (BuildError, PipelineError, ReframeError, ReframeSyntaxError, PerformanceError, SanityError) from reframe.frontend.loader import RegressionCheckLoader from unittests.resources.checks.hellocheck import HelloTest def _run(test, partition, prgenv): test.setup(partition, prgenv) test.compile() test.compile_wait() test.run() test.wait() test.check_sanity() test.check_performance() test.cleanup(remove_files=True) def load_test(testfile): loader = RegressionCheckLoader(['unittests/resources/checks']) return loader.load_from_file(testfile) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(config_file, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(config_file, system, options): yield rt.runtime() yield _temp_runtime @pytest.fixture def generic_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture def testsys_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') @pytest.fixture def user_system(temp_runtime): if fixtures.USER_CONFIG_FILE: yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: yield generic_system @pytest.fixture def hellotest(): yield load_test('unittests/resources/checks/hellocheck.py')[0] @pytest.fixture def local_exec_ctx(generic_system): partition = fixtures.partition_by_name('default') environ = fixtures.environment_by_name('builtin-gcc', partition) yield partition, environ @pytest.fixture def local_user_exec_ctx(user_system): partition = fixtures.partition_by_scheduler('local') if partition is None: pytest.skip('no local jobs are supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def container_remote_exec_ctx(remote_exec_ctx): def _container_exec_ctx(platform): partition = remote_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from remote_exec_ctx return _container_exec_ctx @pytest.fixture def container_local_exec_ctx(local_user_exec_ctx): def _container_exec_ctx(platform): partition = local_user_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from local_user_exec_ctx return _container_exec_ctx def test_environ_setup(hellotest, local_exec_ctx): # Use test environment for the regression check hellotest.variables = {'_FOO_': '1', '_BAR_': '2'} hellotest.setup(*local_exec_ctx) for k in hellotest.variables.keys(): assert k not in os.environ def test_hellocheck(hellotest, remote_exec_ctx): _run(hellotest, *remote_exec_ctx) def test_hellocheck_make(remote_exec_ctx): test = load_test('unittests/resources/checks/hellocheck_make.py')[0] _run(test, *remote_exec_ctx) def test_hellocheck_local(hellotest, local_exec_ctx): # Test also the prebuild/postbuild functionality hellotest.prebuild_cmds = ['touch prebuild', 'mkdir prebuild_dir'] hellotest.postbuild_cmds = ['touch postbuild', 'mkdir postbuild_dir'] hellotest.keep_files = ['prebuild', 'postbuild', 'prebuild_dir', 'postbuild_dir'] # Force local execution of the test; just for testing .local hellotest.local = True _run(hellotest, *local_exec_ctx) must_keep = [ hellotest.stdout.evaluate(), hellotest.stderr.evaluate(), hellotest.build_stdout.evaluate(), hellotest.build_stderr.evaluate(), hellotest.job.script_filename, *hellotest.keep_files ] for f in must_keep: assert os.path.exists(os.path.join(hellotest.outputdir, f)) def test_hellocheck_local_prepost_run(hellotest, local_exec_ctx): @sn.sanity_function def stagedir(test): return test.stagedir # Test also the prebuild/postbuild functionality hellotest.prerun_cmds = ['echo prerun: `pwd`'] hellotest.postrun_cmds = ['echo postrun: `pwd`'] pre_run_path = sn.extractsingle(r'^prerun: (\S+)', hellotest.stdout, 1) post_run_path = sn.extractsingle(r'^postrun: (\S+)', hellotest.stdout, 1) hellotest.sanity_patterns = sn.all([ sn.assert_eq(stagedir(hellotest), pre_run_path), sn.assert_eq(stagedir(hellotest), post_run_path), ]) _run(hellotest, *local_exec_ctx) def test_run_only_sanity(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = './hello.sh' self.executable_opts = ['Hello, World!'] self.local = True self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found( r'Hello, World\!', self.stdout) _run(MyTest(), *local_exec_ctx) def test_run_only_no_srcdir(local_exec_ctx): @fixtures.custom_prefix('foo/bar/') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = 'echo' self.executable_opts = ['hello'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'hello', self.stdout) test = MyTest() assert test.sourcesdir is None _run(test, *local_exec_ctx) def test_compile_only_failure(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.CompileOnlyRegressionTest): def __init__(self): self.sourcepath = 'compiler_failure.c' self.valid_prog_environs = ['*'] self.valid_systems = ['*'] test = MyTest() test.setup(*local_exec_ctx) test.compile() with pytest.raises(BuildError): test.compile_wait() def test_compile_only_warning(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.build_system = 'SingleSource' self.build_system.srcfile = 'compiler_warning.c' self.build_system.cflags = ['-Wall'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'warning', self.stderr) _run(MyTest(), *local_exec_ctx) def test_supports_system(hellotest, testsys_system): hellotest.valid_systems = ['*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:gpu'] assert hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:login'] assert not hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['foo'] assert not hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:gpu'] assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('foo:gpu') assert not hellotest.supports_system('testsys:cpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:*'] assert hellotest.supports_system('testsys:login') assert hellotest.supports_system('gpu') assert no", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_pipeline.py::test_name_compileonly_test,unittests/test_pipeline.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pathlib import pytest import re import reframe as rfm import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.exceptions import (BuildError, PipelineError, ReframeError, ReframeSyntaxError, PerformanceError, SanityError) from reframe.frontend.loader import RegressionCheckLoader from unittests.resources.checks.hellocheck import HelloTest def _run(test, partition, prgenv): test.setup(partition, prgenv) test.compile() test.compile_wait() test.run() test.wait() test.check_sanity() test.check_performance() test.cleanup(remove_files=True) def load_test(testfile): loader = RegressionCheckLoader(['unittests/resources/checks']) return loader.load_from_file(testfile) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(config_file, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(config_file, system, options): yield rt.runtime() yield _temp_runtime @pytest.fixture def generic_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture def testsys_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') @pytest.fixture def user_system(temp_runtime): if fixtures.USER_CONFIG_FILE: yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: yield generic_system @pytest.fixture def hellotest(): yield load_test('unittests/resources/checks/hellocheck.py')[0] @pytest.fixture def local_exec_ctx(generic_system): partition = fixtures.partition_by_name('default') environ = fixtures.environment_by_name('builtin-gcc', partition) yield partition, environ @pytest.fixture def local_user_exec_ctx(user_system): partition = fixtures.partition_by_scheduler('local') if partition is None: pytest.skip('no local jobs are supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def container_remote_exec_ctx(remote_exec_ctx): def _container_exec_ctx(platform): partition = remote_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from remote_exec_ctx return _container_exec_ctx @pytest.fixture def container_local_exec_ctx(local_user_exec_ctx): def _container_exec_ctx(platform): partition = local_user_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from local_user_exec_ctx return _container_exec_ctx def test_environ_setup(hellotest, local_exec_ctx): # Use test environment for the regression check hellotest.variables = {'_FOO_': '1', '_BAR_': '2'} hellotest.setup(*local_exec_ctx) for k in hellotest.variables.keys(): assert k not in os.environ def test_hellocheck(hellotest, remote_exec_ctx): _run(hellotest, *remote_exec_ctx) def test_hellocheck_make(remote_exec_ctx): test = load_test('unittests/resources/checks/hellocheck_make.py')[0] _run(test, *remote_exec_ctx) def test_hellocheck_local(hellotest, local_exec_ctx): # Test also the prebuild/postbuild functionality hellotest.prebuild_cmds = ['touch prebuild', 'mkdir prebuild_dir'] hellotest.postbuild_cmds = ['touch postbuild', 'mkdir postbuild_dir'] hellotest.keep_files = ['prebuild', 'postbuild', 'prebuild_dir', 'postbuild_dir'] # Force local execution of the test; just for testing .local hellotest.local = True _run(hellotest, *local_exec_ctx) must_keep = [ hellotest.stdout.evaluate(), hellotest.stderr.evaluate(), hellotest.build_stdout.evaluate(), hellotest.build_stderr.evaluate(), hellotest.job.script_filename, *hellotest.keep_files ] for f in must_keep: assert os.path.exists(os.path.join(hellotest.outputdir, f)) def test_hellocheck_local_prepost_run(hellotest, local_exec_ctx): @sn.sanity_function def stagedir(test): return test.stagedir # Test also the prebuild/postbuild functionality hellotest.prerun_cmds = ['echo prerun: `pwd`'] hellotest.postrun_cmds = ['echo postrun: `pwd`'] pre_run_path = sn.extractsingle(r'^prerun: (\S+)', hellotest.stdout, 1) post_run_path = sn.extractsingle(r'^postrun: (\S+)', hellotest.stdout, 1) hellotest.sanity_patterns = sn.all([ sn.assert_eq(stagedir(hellotest), pre_run_path), sn.assert_eq(stagedir(hellotest), post_run_path), ]) _run(hellotest, *local_exec_ctx) def test_run_only_sanity(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = './hello.sh' self.executable_opts = ['Hello, World!'] self.local = True self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found( r'Hello, World\!', self.stdout) _run(MyTest(), *local_exec_ctx) def test_run_only_no_srcdir(local_exec_ctx): @fixtures.custom_prefix('foo/bar/') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = 'echo' self.executable_opts = ['hello'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'hello', self.stdout) test = MyTest() assert test.sourcesdir is None _run(test, *local_exec_ctx) def test_compile_only_failure(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.CompileOnlyRegressionTest): def __init__(self): self.sourcepath = 'compiler_failure.c' self.valid_prog_environs = ['*'] self.valid_systems = ['*'] test = MyTest() test.setup(*local_exec_ctx) test.compile() with pytest.raises(BuildError): test.compile_wait() def test_compile_only_warning(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.build_system = 'SingleSource' self.build_system.srcfile = 'compiler_warning.c' self.build_system.cflags = ['-Wall'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'warning', self.stderr) _run(MyTest(), *local_exec_ctx) def test_supports_system(hellotest, testsys_system): hellotest.valid_systems = ['*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:gpu'] assert hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:login'] assert not hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['foo'] assert not hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:gpu'] assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('foo:gpu') assert not hellotest.supports_system('testsys:cpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:*'] assert hellotest.supports_system('testsys:login') assert hellotest.supports_system('gpu') assert no", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_pipeline.py::test_name_runonly_test,unittests/test_pipeline.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pathlib import pytest import re import reframe as rfm import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.exceptions import (BuildError, PipelineError, ReframeError, ReframeSyntaxError, PerformanceError, SanityError) from reframe.frontend.loader import RegressionCheckLoader from unittests.resources.checks.hellocheck import HelloTest def _run(test, partition, prgenv): test.setup(partition, prgenv) test.compile() test.compile_wait() test.run() test.wait() test.check_sanity() test.check_performance() test.cleanup(remove_files=True) def load_test(testfile): loader = RegressionCheckLoader(['unittests/resources/checks']) return loader.load_from_file(testfile) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(config_file, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(config_file, system, options): yield rt.runtime() yield _temp_runtime @pytest.fixture def generic_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture def testsys_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') @pytest.fixture def user_system(temp_runtime): if fixtures.USER_CONFIG_FILE: yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: yield generic_system @pytest.fixture def hellotest(): yield load_test('unittests/resources/checks/hellocheck.py')[0] @pytest.fixture def local_exec_ctx(generic_system): partition = fixtures.partition_by_name('default') environ = fixtures.environment_by_name('builtin-gcc', partition) yield partition, environ @pytest.fixture def local_user_exec_ctx(user_system): partition = fixtures.partition_by_scheduler('local') if partition is None: pytest.skip('no local jobs are supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def container_remote_exec_ctx(remote_exec_ctx): def _container_exec_ctx(platform): partition = remote_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from remote_exec_ctx return _container_exec_ctx @pytest.fixture def container_local_exec_ctx(local_user_exec_ctx): def _container_exec_ctx(platform): partition = local_user_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from local_user_exec_ctx return _container_exec_ctx def test_environ_setup(hellotest, local_exec_ctx): # Use test environment for the regression check hellotest.variables = {'_FOO_': '1', '_BAR_': '2'} hellotest.setup(*local_exec_ctx) for k in hellotest.variables.keys(): assert k not in os.environ def test_hellocheck(hellotest, remote_exec_ctx): _run(hellotest, *remote_exec_ctx) def test_hellocheck_make(remote_exec_ctx): test = load_test('unittests/resources/checks/hellocheck_make.py')[0] _run(test, *remote_exec_ctx) def test_hellocheck_local(hellotest, local_exec_ctx): # Test also the prebuild/postbuild functionality hellotest.prebuild_cmds = ['touch prebuild', 'mkdir prebuild_dir'] hellotest.postbuild_cmds = ['touch postbuild', 'mkdir postbuild_dir'] hellotest.keep_files = ['prebuild', 'postbuild', 'prebuild_dir', 'postbuild_dir'] # Force local execution of the test; just for testing .local hellotest.local = True _run(hellotest, *local_exec_ctx) must_keep = [ hellotest.stdout.evaluate(), hellotest.stderr.evaluate(), hellotest.build_stdout.evaluate(), hellotest.build_stderr.evaluate(), hellotest.job.script_filename, *hellotest.keep_files ] for f in must_keep: assert os.path.exists(os.path.join(hellotest.outputdir, f)) def test_hellocheck_local_prepost_run(hellotest, local_exec_ctx): @sn.sanity_function def stagedir(test): return test.stagedir # Test also the prebuild/postbuild functionality hellotest.prerun_cmds = ['echo prerun: `pwd`'] hellotest.postrun_cmds = ['echo postrun: `pwd`'] pre_run_path = sn.extractsingle(r'^prerun: (\S+)', hellotest.stdout, 1) post_run_path = sn.extractsingle(r'^postrun: (\S+)', hellotest.stdout, 1) hellotest.sanity_patterns = sn.all([ sn.assert_eq(stagedir(hellotest), pre_run_path), sn.assert_eq(stagedir(hellotest), post_run_path), ]) _run(hellotest, *local_exec_ctx) def test_run_only_sanity(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = './hello.sh' self.executable_opts = ['Hello, World!'] self.local = True self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found( r'Hello, World\!', self.stdout) _run(MyTest(), *local_exec_ctx) def test_run_only_no_srcdir(local_exec_ctx): @fixtures.custom_prefix('foo/bar/') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = 'echo' self.executable_opts = ['hello'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'hello', self.stdout) test = MyTest() assert test.sourcesdir is None _run(test, *local_exec_ctx) def test_compile_only_failure(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.CompileOnlyRegressionTest): def __init__(self): self.sourcepath = 'compiler_failure.c' self.valid_prog_environs = ['*'] self.valid_systems = ['*'] test = MyTest() test.setup(*local_exec_ctx) test.compile() with pytest.raises(BuildError): test.compile_wait() def test_compile_only_warning(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.build_system = 'SingleSource' self.build_system.srcfile = 'compiler_warning.c' self.build_system.cflags = ['-Wall'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'warning', self.stderr) _run(MyTest(), *local_exec_ctx) def test_supports_system(hellotest, testsys_system): hellotest.valid_systems = ['*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:gpu'] assert hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:login'] assert not hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['foo'] assert not hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:gpu'] assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('foo:gpu') assert not hellotest.supports_system('testsys:cpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:*'] assert hellotest.supports_system('testsys:login') assert hellotest.supports_system('gpu') assert no", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_pipeline.py::test_name_user_inheritance,unittests/test_pipeline.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pathlib import pytest import re import reframe as rfm import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.exceptions import (BuildError, PipelineError, ReframeError, ReframeSyntaxError, PerformanceError, SanityError) from reframe.frontend.loader import RegressionCheckLoader from unittests.resources.checks.hellocheck import HelloTest def _run(test, partition, prgenv): test.setup(partition, prgenv) test.compile() test.compile_wait() test.run() test.wait() test.check_sanity() test.check_performance() test.cleanup(remove_files=True) def load_test(testfile): loader = RegressionCheckLoader(['unittests/resources/checks']) return loader.load_from_file(testfile) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(config_file, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(config_file, system, options): yield rt.runtime() yield _temp_runtime @pytest.fixture def generic_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture def testsys_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') @pytest.fixture def user_system(temp_runtime): if fixtures.USER_CONFIG_FILE: yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: yield generic_system @pytest.fixture def hellotest(): yield load_test('unittests/resources/checks/hellocheck.py')[0] @pytest.fixture def local_exec_ctx(generic_system): partition = fixtures.partition_by_name('default') environ = fixtures.environment_by_name('builtin-gcc', partition) yield partition, environ @pytest.fixture def local_user_exec_ctx(user_system): partition = fixtures.partition_by_scheduler('local') if partition is None: pytest.skip('no local jobs are supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def container_remote_exec_ctx(remote_exec_ctx): def _container_exec_ctx(platform): partition = remote_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from remote_exec_ctx return _container_exec_ctx @pytest.fixture def container_local_exec_ctx(local_user_exec_ctx): def _container_exec_ctx(platform): partition = local_user_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from local_user_exec_ctx return _container_exec_ctx def test_environ_setup(hellotest, local_exec_ctx): # Use test environment for the regression check hellotest.variables = {'_FOO_': '1', '_BAR_': '2'} hellotest.setup(*local_exec_ctx) for k in hellotest.variables.keys(): assert k not in os.environ def test_hellocheck(hellotest, remote_exec_ctx): _run(hellotest, *remote_exec_ctx) def test_hellocheck_make(remote_exec_ctx): test = load_test('unittests/resources/checks/hellocheck_make.py')[0] _run(test, *remote_exec_ctx) def test_hellocheck_local(hellotest, local_exec_ctx): # Test also the prebuild/postbuild functionality hellotest.prebuild_cmds = ['touch prebuild', 'mkdir prebuild_dir'] hellotest.postbuild_cmds = ['touch postbuild', 'mkdir postbuild_dir'] hellotest.keep_files = ['prebuild', 'postbuild', 'prebuild_dir', 'postbuild_dir'] # Force local execution of the test; just for testing .local hellotest.local = True _run(hellotest, *local_exec_ctx) must_keep = [ hellotest.stdout.evaluate(), hellotest.stderr.evaluate(), hellotest.build_stdout.evaluate(), hellotest.build_stderr.evaluate(), hellotest.job.script_filename, *hellotest.keep_files ] for f in must_keep: assert os.path.exists(os.path.join(hellotest.outputdir, f)) def test_hellocheck_local_prepost_run(hellotest, local_exec_ctx): @sn.sanity_function def stagedir(test): return test.stagedir # Test also the prebuild/postbuild functionality hellotest.prerun_cmds = ['echo prerun: `pwd`'] hellotest.postrun_cmds = ['echo postrun: `pwd`'] pre_run_path = sn.extractsingle(r'^prerun: (\S+)', hellotest.stdout, 1) post_run_path = sn.extractsingle(r'^postrun: (\S+)', hellotest.stdout, 1) hellotest.sanity_patterns = sn.all([ sn.assert_eq(stagedir(hellotest), pre_run_path), sn.assert_eq(stagedir(hellotest), post_run_path), ]) _run(hellotest, *local_exec_ctx) def test_run_only_sanity(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = './hello.sh' self.executable_opts = ['Hello, World!'] self.local = True self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found( r'Hello, World\!', self.stdout) _run(MyTest(), *local_exec_ctx) def test_run_only_no_srcdir(local_exec_ctx): @fixtures.custom_prefix('foo/bar/') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = 'echo' self.executable_opts = ['hello'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'hello', self.stdout) test = MyTest() assert test.sourcesdir is None _run(test, *local_exec_ctx) def test_compile_only_failure(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.CompileOnlyRegressionTest): def __init__(self): self.sourcepath = 'compiler_failure.c' self.valid_prog_environs = ['*'] self.valid_systems = ['*'] test = MyTest() test.setup(*local_exec_ctx) test.compile() with pytest.raises(BuildError): test.compile_wait() def test_compile_only_warning(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.build_system = 'SingleSource' self.build_system.srcfile = 'compiler_warning.c' self.build_system.cflags = ['-Wall'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'warning', self.stderr) _run(MyTest(), *local_exec_ctx) def test_supports_system(hellotest, testsys_system): hellotest.valid_systems = ['*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:gpu'] assert hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:login'] assert not hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['foo'] assert not hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:gpu'] assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('foo:gpu') assert not hellotest.supports_system('testsys:cpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:*'] assert hellotest.supports_system('testsys:login') assert hellotest.supports_system('gpu') assert no", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_pipeline.py::test_registration_of_tests,unittests/test_pipeline.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pathlib import pytest import re import reframe as rfm import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.exceptions import (BuildError, PipelineError, ReframeError, ReframeSyntaxError, PerformanceError, SanityError) from reframe.frontend.loader import RegressionCheckLoader from unittests.resources.checks.hellocheck import HelloTest def _run(test, partition, prgenv): test.setup(partition, prgenv) test.compile() test.compile_wait() test.run() test.wait() test.check_sanity() test.check_performance() test.cleanup(remove_files=True) def load_test(testfile): loader = RegressionCheckLoader(['unittests/resources/checks']) return loader.load_from_file(testfile) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(config_file, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(config_file, system, options): yield rt.runtime() yield _temp_runtime @pytest.fixture def generic_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture def testsys_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') @pytest.fixture def user_system(temp_runtime): if fixtures.USER_CONFIG_FILE: yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: yield generic_system @pytest.fixture def hellotest(): yield load_test('unittests/resources/checks/hellocheck.py')[0] @pytest.fixture def local_exec_ctx(generic_system): partition = fixtures.partition_by_name('default') environ = fixtures.environment_by_name('builtin-gcc', partition) yield partition, environ @pytest.fixture def local_user_exec_ctx(user_system): partition = fixtures.partition_by_scheduler('local') if partition is None: pytest.skip('no local jobs are supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def container_remote_exec_ctx(remote_exec_ctx): def _container_exec_ctx(platform): partition = remote_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from remote_exec_ctx return _container_exec_ctx @pytest.fixture def container_local_exec_ctx(local_user_exec_ctx): def _container_exec_ctx(platform): partition = local_user_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from local_user_exec_ctx return _container_exec_ctx def test_environ_setup(hellotest, local_exec_ctx): # Use test environment for the regression check hellotest.variables = {'_FOO_': '1', '_BAR_': '2'} hellotest.setup(*local_exec_ctx) for k in hellotest.variables.keys(): assert k not in os.environ def test_hellocheck(hellotest, remote_exec_ctx): _run(hellotest, *remote_exec_ctx) def test_hellocheck_make(remote_exec_ctx): test = load_test('unittests/resources/checks/hellocheck_make.py')[0] _run(test, *remote_exec_ctx) def test_hellocheck_local(hellotest, local_exec_ctx): # Test also the prebuild/postbuild functionality hellotest.prebuild_cmds = ['touch prebuild', 'mkdir prebuild_dir'] hellotest.postbuild_cmds = ['touch postbuild', 'mkdir postbuild_dir'] hellotest.keep_files = ['prebuild', 'postbuild', 'prebuild_dir', 'postbuild_dir'] # Force local execution of the test; just for testing .local hellotest.local = True _run(hellotest, *local_exec_ctx) must_keep = [ hellotest.stdout.evaluate(), hellotest.stderr.evaluate(), hellotest.build_stdout.evaluate(), hellotest.build_stderr.evaluate(), hellotest.job.script_filename, *hellotest.keep_files ] for f in must_keep: assert os.path.exists(os.path.join(hellotest.outputdir, f)) def test_hellocheck_local_prepost_run(hellotest, local_exec_ctx): @sn.sanity_function def stagedir(test): return test.stagedir # Test also the prebuild/postbuild functionality hellotest.prerun_cmds = ['echo prerun: `pwd`'] hellotest.postrun_cmds = ['echo postrun: `pwd`'] pre_run_path = sn.extractsingle(r'^prerun: (\S+)', hellotest.stdout, 1) post_run_path = sn.extractsingle(r'^postrun: (\S+)', hellotest.stdout, 1) hellotest.sanity_patterns = sn.all([ sn.assert_eq(stagedir(hellotest), pre_run_path), sn.assert_eq(stagedir(hellotest), post_run_path), ]) _run(hellotest, *local_exec_ctx) def test_run_only_sanity(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = './hello.sh' self.executable_opts = ['Hello, World!'] self.local = True self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found( r'Hello, World\!', self.stdout) _run(MyTest(), *local_exec_ctx) def test_run_only_no_srcdir(local_exec_ctx): @fixtures.custom_prefix('foo/bar/') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = 'echo' self.executable_opts = ['hello'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'hello', self.stdout) test = MyTest() assert test.sourcesdir is None _run(test, *local_exec_ctx) def test_compile_only_failure(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.CompileOnlyRegressionTest): def __init__(self): self.sourcepath = 'compiler_failure.c' self.valid_prog_environs = ['*'] self.valid_systems = ['*'] test = MyTest() test.setup(*local_exec_ctx) test.compile() with pytest.raises(BuildError): test.compile_wait() def test_compile_only_warning(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.build_system = 'SingleSource' self.build_system.srcfile = 'compiler_warning.c' self.build_system.cflags = ['-Wall'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'warning', self.stderr) _run(MyTest(), *local_exec_ctx) def test_supports_system(hellotest, testsys_system): hellotest.valid_systems = ['*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:gpu'] assert hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:login'] assert not hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['foo'] assert not hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:gpu'] assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('foo:gpu') assert not hellotest.supports_system('testsys:cpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:*'] assert hellotest.supports_system('testsys:login') assert hellotest.supports_system('gpu') assert no", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_pipeline.py::test_regression_test_name,unittests/test_pipeline.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pathlib import pytest import re import reframe as rfm import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.exceptions import (BuildError, PipelineError, ReframeError, ReframeSyntaxError, PerformanceError, SanityError) from reframe.frontend.loader import RegressionCheckLoader from unittests.resources.checks.hellocheck import HelloTest def _run(test, partition, prgenv): test.setup(partition, prgenv) test.compile() test.compile_wait() test.run() test.wait() test.check_sanity() test.check_performance() test.cleanup(remove_files=True) def load_test(testfile): loader = RegressionCheckLoader(['unittests/resources/checks']) return loader.load_from_file(testfile) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(config_file, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(config_file, system, options): yield rt.runtime() yield _temp_runtime @pytest.fixture def generic_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture def testsys_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') @pytest.fixture def user_system(temp_runtime): if fixtures.USER_CONFIG_FILE: yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: yield generic_system @pytest.fixture def hellotest(): yield load_test('unittests/resources/checks/hellocheck.py')[0] @pytest.fixture def local_exec_ctx(generic_system): partition = fixtures.partition_by_name('default') environ = fixtures.environment_by_name('builtin-gcc', partition) yield partition, environ @pytest.fixture def local_user_exec_ctx(user_system): partition = fixtures.partition_by_scheduler('local') if partition is None: pytest.skip('no local jobs are supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def container_remote_exec_ctx(remote_exec_ctx): def _container_exec_ctx(platform): partition = remote_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from remote_exec_ctx return _container_exec_ctx @pytest.fixture def container_local_exec_ctx(local_user_exec_ctx): def _container_exec_ctx(platform): partition = local_user_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from local_user_exec_ctx return _container_exec_ctx def test_environ_setup(hellotest, local_exec_ctx): # Use test environment for the regression check hellotest.variables = {'_FOO_': '1', '_BAR_': '2'} hellotest.setup(*local_exec_ctx) for k in hellotest.variables.keys(): assert k not in os.environ def test_hellocheck(hellotest, remote_exec_ctx): _run(hellotest, *remote_exec_ctx) def test_hellocheck_make(remote_exec_ctx): test = load_test('unittests/resources/checks/hellocheck_make.py')[0] _run(test, *remote_exec_ctx) def test_hellocheck_local(hellotest, local_exec_ctx): # Test also the prebuild/postbuild functionality hellotest.prebuild_cmds = ['touch prebuild', 'mkdir prebuild_dir'] hellotest.postbuild_cmds = ['touch postbuild', 'mkdir postbuild_dir'] hellotest.keep_files = ['prebuild', 'postbuild', 'prebuild_dir', 'postbuild_dir'] # Force local execution of the test; just for testing .local hellotest.local = True _run(hellotest, *local_exec_ctx) must_keep = [ hellotest.stdout.evaluate(), hellotest.stderr.evaluate(), hellotest.build_stdout.evaluate(), hellotest.build_stderr.evaluate(), hellotest.job.script_filename, *hellotest.keep_files ] for f in must_keep: assert os.path.exists(os.path.join(hellotest.outputdir, f)) def test_hellocheck_local_prepost_run(hellotest, local_exec_ctx): @sn.sanity_function def stagedir(test): return test.stagedir # Test also the prebuild/postbuild functionality hellotest.prerun_cmds = ['echo prerun: `pwd`'] hellotest.postrun_cmds = ['echo postrun: `pwd`'] pre_run_path = sn.extractsingle(r'^prerun: (\S+)', hellotest.stdout, 1) post_run_path = sn.extractsingle(r'^postrun: (\S+)', hellotest.stdout, 1) hellotest.sanity_patterns = sn.all([ sn.assert_eq(stagedir(hellotest), pre_run_path), sn.assert_eq(stagedir(hellotest), post_run_path), ]) _run(hellotest, *local_exec_ctx) def test_run_only_sanity(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = './hello.sh' self.executable_opts = ['Hello, World!'] self.local = True self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found( r'Hello, World\!', self.stdout) _run(MyTest(), *local_exec_ctx) def test_run_only_no_srcdir(local_exec_ctx): @fixtures.custom_prefix('foo/bar/') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = 'echo' self.executable_opts = ['hello'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'hello', self.stdout) test = MyTest() assert test.sourcesdir is None _run(test, *local_exec_ctx) def test_compile_only_failure(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.CompileOnlyRegressionTest): def __init__(self): self.sourcepath = 'compiler_failure.c' self.valid_prog_environs = ['*'] self.valid_systems = ['*'] test = MyTest() test.setup(*local_exec_ctx) test.compile() with pytest.raises(BuildError): test.compile_wait() def test_compile_only_warning(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.build_system = 'SingleSource' self.build_system.srcfile = 'compiler_warning.c' self.build_system.cflags = ['-Wall'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'warning', self.stderr) _run(MyTest(), *local_exec_ctx) def test_supports_system(hellotest, testsys_system): hellotest.valid_systems = ['*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:gpu'] assert hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:login'] assert not hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['foo'] assert not hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:gpu'] assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('foo:gpu') assert not hellotest.supports_system('testsys:cpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:*'] assert hellotest.supports_system('testsys:login') assert hellotest.supports_system('gpu') assert no", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_pipeline.py::test_strange_test_names,unittests/test_pipeline.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pathlib import pytest import re import reframe as rfm import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.exceptions import (BuildError, PipelineError, ReframeError, ReframeSyntaxError, PerformanceError, SanityError) from reframe.frontend.loader import RegressionCheckLoader from unittests.resources.checks.hellocheck import HelloTest def _run(test, partition, prgenv): test.setup(partition, prgenv) test.compile() test.compile_wait() test.run() test.wait() test.check_sanity() test.check_performance() test.cleanup(remove_files=True) def load_test(testfile): loader = RegressionCheckLoader(['unittests/resources/checks']) return loader.load_from_file(testfile) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(config_file, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(config_file, system, options): yield rt.runtime() yield _temp_runtime @pytest.fixture def generic_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture def testsys_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') @pytest.fixture def user_system(temp_runtime): if fixtures.USER_CONFIG_FILE: yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: yield generic_system @pytest.fixture def hellotest(): yield load_test('unittests/resources/checks/hellocheck.py')[0] @pytest.fixture def local_exec_ctx(generic_system): partition = fixtures.partition_by_name('default') environ = fixtures.environment_by_name('builtin-gcc', partition) yield partition, environ @pytest.fixture def local_user_exec_ctx(user_system): partition = fixtures.partition_by_scheduler('local') if partition is None: pytest.skip('no local jobs are supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def container_remote_exec_ctx(remote_exec_ctx): def _container_exec_ctx(platform): partition = remote_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from remote_exec_ctx return _container_exec_ctx @pytest.fixture def container_local_exec_ctx(local_user_exec_ctx): def _container_exec_ctx(platform): partition = local_user_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from local_user_exec_ctx return _container_exec_ctx def test_environ_setup(hellotest, local_exec_ctx): # Use test environment for the regression check hellotest.variables = {'_FOO_': '1', '_BAR_': '2'} hellotest.setup(*local_exec_ctx) for k in hellotest.variables.keys(): assert k not in os.environ def test_hellocheck(hellotest, remote_exec_ctx): _run(hellotest, *remote_exec_ctx) def test_hellocheck_make(remote_exec_ctx): test = load_test('unittests/resources/checks/hellocheck_make.py')[0] _run(test, *remote_exec_ctx) def test_hellocheck_local(hellotest, local_exec_ctx): # Test also the prebuild/postbuild functionality hellotest.prebuild_cmds = ['touch prebuild', 'mkdir prebuild_dir'] hellotest.postbuild_cmds = ['touch postbuild', 'mkdir postbuild_dir'] hellotest.keep_files = ['prebuild', 'postbuild', 'prebuild_dir', 'postbuild_dir'] # Force local execution of the test; just for testing .local hellotest.local = True _run(hellotest, *local_exec_ctx) must_keep = [ hellotest.stdout.evaluate(), hellotest.stderr.evaluate(), hellotest.build_stdout.evaluate(), hellotest.build_stderr.evaluate(), hellotest.job.script_filename, *hellotest.keep_files ] for f in must_keep: assert os.path.exists(os.path.join(hellotest.outputdir, f)) def test_hellocheck_local_prepost_run(hellotest, local_exec_ctx): @sn.sanity_function def stagedir(test): return test.stagedir # Test also the prebuild/postbuild functionality hellotest.prerun_cmds = ['echo prerun: `pwd`'] hellotest.postrun_cmds = ['echo postrun: `pwd`'] pre_run_path = sn.extractsingle(r'^prerun: (\S+)', hellotest.stdout, 1) post_run_path = sn.extractsingle(r'^postrun: (\S+)', hellotest.stdout, 1) hellotest.sanity_patterns = sn.all([ sn.assert_eq(stagedir(hellotest), pre_run_path), sn.assert_eq(stagedir(hellotest), post_run_path), ]) _run(hellotest, *local_exec_ctx) def test_run_only_sanity(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = './hello.sh' self.executable_opts = ['Hello, World!'] self.local = True self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found( r'Hello, World\!', self.stdout) _run(MyTest(), *local_exec_ctx) def test_run_only_no_srcdir(local_exec_ctx): @fixtures.custom_prefix('foo/bar/') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = 'echo' self.executable_opts = ['hello'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'hello', self.stdout) test = MyTest() assert test.sourcesdir is None _run(test, *local_exec_ctx) def test_compile_only_failure(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.CompileOnlyRegressionTest): def __init__(self): self.sourcepath = 'compiler_failure.c' self.valid_prog_environs = ['*'] self.valid_systems = ['*'] test = MyTest() test.setup(*local_exec_ctx) test.compile() with pytest.raises(BuildError): test.compile_wait() def test_compile_only_warning(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.build_system = 'SingleSource' self.build_system.srcfile = 'compiler_warning.c' self.build_system.cflags = ['-Wall'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'warning', self.stderr) _run(MyTest(), *local_exec_ctx) def test_supports_system(hellotest, testsys_system): hellotest.valid_systems = ['*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:gpu'] assert hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:login'] assert not hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['foo'] assert not hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:gpu'] assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('foo:gpu') assert not hellotest.supports_system('testsys:cpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:*'] assert hellotest.supports_system('testsys:login') assert hellotest.supports_system('gpu') assert no", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_pipeline.py::test_supports_environ,unittests/test_pipeline.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pathlib import pytest import re import reframe as rfm import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.exceptions import (BuildError, PipelineError, ReframeError, ReframeSyntaxError, PerformanceError, SanityError) from reframe.frontend.loader import RegressionCheckLoader from unittests.resources.checks.hellocheck import HelloTest def _run(test, partition, prgenv): test.setup(partition, prgenv) test.compile() test.compile_wait() test.run() test.wait() test.check_sanity() test.check_performance() test.cleanup(remove_files=True) def load_test(testfile): loader = RegressionCheckLoader(['unittests/resources/checks']) return loader.load_from_file(testfile) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(config_file, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(config_file, system, options): yield rt.runtime() yield _temp_runtime @pytest.fixture def generic_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture def testsys_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') @pytest.fixture def user_system(temp_runtime): if fixtures.USER_CONFIG_FILE: yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: yield generic_system @pytest.fixture def hellotest(): yield load_test('unittests/resources/checks/hellocheck.py')[0] @pytest.fixture def local_exec_ctx(generic_system): partition = fixtures.partition_by_name('default') environ = fixtures.environment_by_name('builtin-gcc', partition) yield partition, environ @pytest.fixture def local_user_exec_ctx(user_system): partition = fixtures.partition_by_scheduler('local') if partition is None: pytest.skip('no local jobs are supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def container_remote_exec_ctx(remote_exec_ctx): def _container_exec_ctx(platform): partition = remote_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from remote_exec_ctx return _container_exec_ctx @pytest.fixture def container_local_exec_ctx(local_user_exec_ctx): def _container_exec_ctx(platform): partition = local_user_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from local_user_exec_ctx return _container_exec_ctx def test_environ_setup(hellotest, local_exec_ctx): # Use test environment for the regression check hellotest.variables = {'_FOO_': '1', '_BAR_': '2'} hellotest.setup(*local_exec_ctx) for k in hellotest.variables.keys(): assert k not in os.environ def test_hellocheck(hellotest, remote_exec_ctx): _run(hellotest, *remote_exec_ctx) def test_hellocheck_make(remote_exec_ctx): test = load_test('unittests/resources/checks/hellocheck_make.py')[0] _run(test, *remote_exec_ctx) def test_hellocheck_local(hellotest, local_exec_ctx): # Test also the prebuild/postbuild functionality hellotest.prebuild_cmds = ['touch prebuild', 'mkdir prebuild_dir'] hellotest.postbuild_cmds = ['touch postbuild', 'mkdir postbuild_dir'] hellotest.keep_files = ['prebuild', 'postbuild', 'prebuild_dir', 'postbuild_dir'] # Force local execution of the test; just for testing .local hellotest.local = True _run(hellotest, *local_exec_ctx) must_keep = [ hellotest.stdout.evaluate(), hellotest.stderr.evaluate(), hellotest.build_stdout.evaluate(), hellotest.build_stderr.evaluate(), hellotest.job.script_filename, *hellotest.keep_files ] for f in must_keep: assert os.path.exists(os.path.join(hellotest.outputdir, f)) def test_hellocheck_local_prepost_run(hellotest, local_exec_ctx): @sn.sanity_function def stagedir(test): return test.stagedir # Test also the prebuild/postbuild functionality hellotest.prerun_cmds = ['echo prerun: `pwd`'] hellotest.postrun_cmds = ['echo postrun: `pwd`'] pre_run_path = sn.extractsingle(r'^prerun: (\S+)', hellotest.stdout, 1) post_run_path = sn.extractsingle(r'^postrun: (\S+)', hellotest.stdout, 1) hellotest.sanity_patterns = sn.all([ sn.assert_eq(stagedir(hellotest), pre_run_path), sn.assert_eq(stagedir(hellotest), post_run_path), ]) _run(hellotest, *local_exec_ctx) def test_run_only_sanity(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = './hello.sh' self.executable_opts = ['Hello, World!'] self.local = True self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found( r'Hello, World\!', self.stdout) _run(MyTest(), *local_exec_ctx) def test_run_only_no_srcdir(local_exec_ctx): @fixtures.custom_prefix('foo/bar/') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = 'echo' self.executable_opts = ['hello'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'hello', self.stdout) test = MyTest() assert test.sourcesdir is None _run(test, *local_exec_ctx) def test_compile_only_failure(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.CompileOnlyRegressionTest): def __init__(self): self.sourcepath = 'compiler_failure.c' self.valid_prog_environs = ['*'] self.valid_systems = ['*'] test = MyTest() test.setup(*local_exec_ctx) test.compile() with pytest.raises(BuildError): test.compile_wait() def test_compile_only_warning(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.build_system = 'SingleSource' self.build_system.srcfile = 'compiler_warning.c' self.build_system.cflags = ['-Wall'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'warning', self.stderr) _run(MyTest(), *local_exec_ctx) def test_supports_system(hellotest, testsys_system): hellotest.valid_systems = ['*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:gpu'] assert hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:login'] assert not hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['foo'] assert not hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:gpu'] assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('foo:gpu') assert not hellotest.supports_system('testsys:cpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:*'] assert hellotest.supports_system('testsys:login') assert hellotest.supports_system('gpu') assert no", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_pipeline.py::test_supports_system,unittests/test_pipeline.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import os import pathlib import pytest import re import reframe as rfm import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import reframe.utility.sanity as sn import unittests.fixtures as fixtures from reframe.core.exceptions import (BuildError, PipelineError, ReframeError, ReframeSyntaxError, PerformanceError, SanityError) from reframe.frontend.loader import RegressionCheckLoader from unittests.resources.checks.hellocheck import HelloTest def _run(test, partition, prgenv): test.setup(partition, prgenv) test.compile() test.compile_wait() test.run() test.wait() test.check_sanity() test.check_performance() test.cleanup(remove_files=True) def load_test(testfile): loader = RegressionCheckLoader(['unittests/resources/checks']) return loader.load_from_file(testfile) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(config_file, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(config_file, system, options): yield rt.runtime() yield _temp_runtime @pytest.fixture def generic_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture def testsys_system(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'testsys') @pytest.fixture def user_system(temp_runtime): if fixtures.USER_CONFIG_FILE: yield from temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: yield generic_system @pytest.fixture def hellotest(): yield load_test('unittests/resources/checks/hellocheck.py')[0] @pytest.fixture def local_exec_ctx(generic_system): partition = fixtures.partition_by_name('default') environ = fixtures.environment_by_name('builtin-gcc', partition) yield partition, environ @pytest.fixture def local_user_exec_ctx(user_system): partition = fixtures.partition_by_scheduler('local') if partition is None: pytest.skip('no local jobs are supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def remote_exec_ctx(user_system): partition = fixtures.partition_by_scheduler() if partition is None: pytest.skip('job submission not supported') try: environ = partition.environs[0] except IndexError: pytest.skip('no environments configured for partition: %s' % partition.fullname) yield partition, environ @pytest.fixture def container_remote_exec_ctx(remote_exec_ctx): def _container_exec_ctx(platform): partition = remote_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from remote_exec_ctx return _container_exec_ctx @pytest.fixture def container_local_exec_ctx(local_user_exec_ctx): def _container_exec_ctx(platform): partition = local_user_exec_ctx[0] if platform not in partition.container_environs.keys(): pytest.skip('%s is not configured on the system' % platform) yield from local_user_exec_ctx return _container_exec_ctx def test_environ_setup(hellotest, local_exec_ctx): # Use test environment for the regression check hellotest.variables = {'_FOO_': '1', '_BAR_': '2'} hellotest.setup(*local_exec_ctx) for k in hellotest.variables.keys(): assert k not in os.environ def test_hellocheck(hellotest, remote_exec_ctx): _run(hellotest, *remote_exec_ctx) def test_hellocheck_make(remote_exec_ctx): test = load_test('unittests/resources/checks/hellocheck_make.py')[0] _run(test, *remote_exec_ctx) def test_hellocheck_local(hellotest, local_exec_ctx): # Test also the prebuild/postbuild functionality hellotest.prebuild_cmds = ['touch prebuild', 'mkdir prebuild_dir'] hellotest.postbuild_cmds = ['touch postbuild', 'mkdir postbuild_dir'] hellotest.keep_files = ['prebuild', 'postbuild', 'prebuild_dir', 'postbuild_dir'] # Force local execution of the test; just for testing .local hellotest.local = True _run(hellotest, *local_exec_ctx) must_keep = [ hellotest.stdout.evaluate(), hellotest.stderr.evaluate(), hellotest.build_stdout.evaluate(), hellotest.build_stderr.evaluate(), hellotest.job.script_filename, *hellotest.keep_files ] for f in must_keep: assert os.path.exists(os.path.join(hellotest.outputdir, f)) def test_hellocheck_local_prepost_run(hellotest, local_exec_ctx): @sn.sanity_function def stagedir(test): return test.stagedir # Test also the prebuild/postbuild functionality hellotest.prerun_cmds = ['echo prerun: `pwd`'] hellotest.postrun_cmds = ['echo postrun: `pwd`'] pre_run_path = sn.extractsingle(r'^prerun: (\S+)', hellotest.stdout, 1) post_run_path = sn.extractsingle(r'^postrun: (\S+)', hellotest.stdout, 1) hellotest.sanity_patterns = sn.all([ sn.assert_eq(stagedir(hellotest), pre_run_path), sn.assert_eq(stagedir(hellotest), post_run_path), ]) _run(hellotest, *local_exec_ctx) def test_run_only_sanity(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = './hello.sh' self.executable_opts = ['Hello, World!'] self.local = True self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found( r'Hello, World\!', self.stdout) _run(MyTest(), *local_exec_ctx) def test_run_only_no_srcdir(local_exec_ctx): @fixtures.custom_prefix('foo/bar/') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.executable = 'echo' self.executable_opts = ['hello'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'hello', self.stdout) test = MyTest() assert test.sourcesdir is None _run(test, *local_exec_ctx) def test_compile_only_failure(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.CompileOnlyRegressionTest): def __init__(self): self.sourcepath = 'compiler_failure.c' self.valid_prog_environs = ['*'] self.valid_systems = ['*'] test = MyTest() test.setup(*local_exec_ctx) test.compile() with pytest.raises(BuildError): test.compile_wait() def test_compile_only_warning(local_exec_ctx): @fixtures.custom_prefix('unittests/resources/checks') class MyTest(rfm.RunOnlyRegressionTest): def __init__(self): self.build_system = 'SingleSource' self.build_system.srcfile = 'compiler_warning.c' self.build_system.cflags = ['-Wall'] self.valid_prog_environs = ['*'] self.valid_systems = ['*'] self.sanity_patterns = sn.assert_found(r'warning', self.stderr) _run(MyTest(), *local_exec_ctx) def test_supports_system(hellotest, testsys_system): hellotest.valid_systems = ['*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:*'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys'] assert hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:gpu'] assert hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:login'] assert not hellotest.supports_system('gpu') assert hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('testsys:login') hellotest.valid_systems = ['foo'] assert not hellotest.supports_system('gpu') assert not hellotest.supports_system('login') assert not hellotest.supports_system('testsys:gpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['*:gpu'] assert hellotest.supports_system('testsys:gpu') assert hellotest.supports_system('foo:gpu') assert not hellotest.supports_system('testsys:cpu') assert not hellotest.supports_system('testsys:login') hellotest.valid_systems = ['testsys:*'] assert hellotest.supports_system('testsys:login') assert hellotest.supports_system('gpu') assert no", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_concurrency_limited,unittests/test_policies.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_concurrency_unlimited,unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_dependencies[AsynchronousExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_dependencies[SerialExecutionPolicy],unittests/test_policies.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_dependencies_with_retries[AsynchronousExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_dependencies_with_retries[SerialExecutionPolicy],unittests/test_policies.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_kbd_interrupt_in_wait_with_concurrency,unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_pass_in_retries[AsynchronousExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_pass_in_retries[SerialExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_retries_bad_check[AsynchronousExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_retries_bad_check[SerialExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_runall[AsynchronousExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_runall[SerialExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_runall_skip_performance_check[AsynchronousExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_runall_skip_performance_check[SerialExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_runall_skip_prgenv_check[AsynchronousExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_runall_skip_prgenv_check[SerialExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_runall_skip_sanity_check[AsynchronousExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_runall_skip_sanity_check[SerialExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_runall_skip_system_check[AsynchronousExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_runall_skip_system_check[SerialExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_sigterm_handling[AsynchronousExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_strict_performance_check[AsynchronousExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_policies.py::test_strict_performance_check[SerialExecutionPolicy],unittests/test_policies.py,NOD,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import json import jsonschema import os import pytest import socket import sys import time import reframe import reframe.core.runtime as rt import reframe.frontend.dependency as dependency import reframe.frontend.executors as executors import reframe.frontend.executors.policies as policies import reframe.utility as util import reframe.utility.os_ext as os_ext from reframe.core.exceptions import (JobNotStartedError, ReframeForceExitError, TaskDependencyError) from reframe.frontend.loader import RegressionCheckLoader import unittests.fixtures as fixtures from unittests.resources.checks.hellocheck import HelloTest from unittests.resources.checks.frontend_checks import ( BadSetupCheck, BadSetupCheckEarly, CompileFailureCheck, KeyboardInterruptCheck, RetriesCheck, SelfKillCheck, SleepCheck, SleepCheckPollFail, SleepCheckPollFailLate, SystemExitCheck, ) @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': str(tmp_path)}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def make_loader(): def _make_loader(check_search_path): return RegressionCheckLoader(check_search_path, ignore_conflicts=True) return _make_loader @pytest.fixture def common_exec_ctx(temp_runtime): yield from temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') @pytest.fixture(params=[policies.SerialExecutionPolicy, policies.AsynchronousExecutionPolicy]) def make_runner(request): def _make_runner(*args, **kwargs): return executors.Runner(request.param(), *args, **kwargs) return _make_runner @pytest.fixture def make_cases(make_loader): def _make_cases(checks=None, sort=False, *args, **kwargs): if checks is None: checks = make_loader(['unittests/resources/checks']).load_all() cases = executors.generate_testcases(checks, *args, **kwargs) if sort: depgraph = dependency.build_deps(cases) dependency.validate_deps(depgraph) cases = dependency.toposort(depgraph) return cases return _make_cases def assert_runall(runner): # Make sure that all cases finished or failed for t in runner.stats.tasks(): assert t.succeeded or t.failed def assert_all_dead(runner): stats = runner.stats for t in runner.stats.tasks(): try: finished = t.check.poll() except JobNotStartedError: finished = True assert finished def num_failures_stage(runner, stage): stats = runner.stats return len([t for t in stats.failures() if t.failed_stage == stage]) def _validate_runreport(report): schema_filename = 'reframe/schemas/runreport.json' with open(schema_filename) as fp: schema = json.loads(fp.read()) jsonschema.validate(report, schema) def test_runall(make_runner, make_cases, common_exec_ctx): runner = make_runner() time_start = time.time() runner.runall(make_cases()) time_end = time.time() assert 8 == runner.stats.num_cases() assert_runall(runner) assert 5 == len(runner.stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') # Create a run report and validate it run_stats = runner.stats.json() report = { 'session_info': { 'cmdline': ' '.join(sys.argv), 'config_file': rt.runtime().site_config.filename, 'data_version': '1.0', 'hostname': socket.gethostname(), 'num_cases': run_stats[0]['num_cases'], 'num_failures': run_stats[-1]['num_failures'], 'prefix_output': rt.runtime().output_prefix, 'prefix_stage': rt.runtime().stage_prefix, 'time_elapsed': time_end - time_start, 'time_end': time.strftime( '%FT%T%z', time.localtime(time_end), ), 'time_start': time.strftime( '%FT%T%z', time.localtime(time_start), ), 'user': os_ext.osuser(), 'version': os_ext.reframe_version(), 'workdir': os.getcwd() }, 'runs': run_stats } _validate_runreport(report) def test_runall_skip_system_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_system_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_prgenv_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.runall(make_cases(skip_environ_check=True)) stats = runner.stats assert 9 == stats.num_cases() assert_runall(runner) assert 5 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_sanity_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_sanity_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 0 == num_failures_stage(runner, 'sanity') assert 1 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_runall_skip_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.skip_performance_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 4 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 0 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_strict_performance_check(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.strict_check = True runner.runall(make_cases()) stats = runner.stats assert 8 == stats.num_cases() assert_runall(runner) assert 6 == len(stats.failures()) assert 2 == num_failures_stage(runner, 'setup') assert 1 == num_failures_stage(runner, 'sanity') assert 2 == num_failures_stage(runner, 'performance') assert 1 == num_failures_stage(runner, 'cleanup') def test_force_local_execution(make_runner, make_cases, common_exec_ctx): runner = make_runner() runner.policy.force_local = True runner.runall(make_cases([HelloTest()])) assert_runall(runner) stats = runner.stats for t in stats.tasks(): assert t.check.local def test_kbd_interrupt_within_test(make_runner, make_cases, common_exec_ctx): runner = make_runner() check = KeyboardInterruptCheck() with pytest.raises(KeyboardInterrupt): runner.runall(make_cases([KeyboardInterruptCheck()])) stats = runner.stats assert 1 == len(stats.failures()) assert_all_dead(runner) def test_system_exit_within_test(make_runner, make_cases, common_exec_ctx): # This should not raise and should not exit runner = make_runner() runner.runall(make_cases([SystemExitCheck()])) stats = runner.stats assert 1 == len(stats.failures()) def test_retries_bad_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([BadSetupCheck(), BadSetupCheckEarly()])) # Ensure that the test was retried #max_retries times and failed assert 2 == runner.stats.num_cases() assert_runall(runner) assert runner.max_retries == rt.runtime().current_run assert 2 == len(runner.stats.failures()) # Ensure that the report does not raise any exception runner.stats.retry_report() def test_retries_good_check(make_runner, make_cases, common_exec_ctx): runner = make_runner(max_retries=2) runner.runall(make_cases([HelloTest()])) # Ensure that the test passed without retries. assert 1 == runner.stats.num_cases() assert_runall(runner) assert 0 == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_pass_in_retries(make_runner, make_cases, tmp_path, common_exec_ctx): tmpfile = tmp_path / 'out.txt' tmpfile.write_text('0\n') runner = make_runner(max_retries=3) pass_run_no = 2 runner.runall(make_cases([RetriesCheck(pass_run_no, tmpfile)])) # Ensure that the test passed after retries in run `pass_run_no` assert 1 == runner.stats.num_cases() assert_runall(runner) assert 1 == len(runner.stats.failures(run=0)) assert pass_run_no == rt.runtime().current_run assert 0 == len(runner.stats.failures()) def test_sigterm_handling(make_runner, make_cases, common_exec_ctx): runner = make_runner() with pytest.raises(ReframeForceExitError, match='received TERM signal'): runner.runall(make_cases([SelfKillCheck()])) assert_all_de", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_cancel[local],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_cancel_before_submit[local],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_cancel_before_submit[pbs],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_cancel_before_submit[slurm],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_cancel_before_submit[squeue],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_cancel_before_submit[torque],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_cancel_term_ignore[local],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_cancel_with_grace[local],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_combined_access_constraint[slurm],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_combined_access_constraint[squeue],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_combined_access_multiple_constraints[slurm],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_combined_access_verbatim_constraint[slurm],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_constraint_idle,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_default_partition_all,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_enough_nodes_constraint_partition,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_exclude_nodes_cmd,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_exclude_nodes_opt,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_invalid_constraint,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_invalid_partition_opt,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_maintenance_nodes,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_nodes_negative,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_nodes_positive,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_not_enough_idle_nodes,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_not_enough_nodes_constraint_partition,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_no_num_tasks_per_node,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_partition_idle,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_sched_access_idle,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_sched_access_idle_sequence_view,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_sched_access_partition,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_valid_constraint_opt,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_valid_constraint_partition,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_valid_multiple_constraints,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_valid_multiple_partitions,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_valid_partition_cmd,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_valid_partition_opt,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_valid_reservation_cmd,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_flex_alloc_valid_reservation_option,unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_guess_num_tasks[slurm],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_guess_num_tasks[squeue],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_guess_num_tasks[torque],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_no_empty_lines_in_preamble[pbs],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_no_empty_lines_in_preamble[slurm],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_no_empty_lines_in_preamble[squeue],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_no_empty_lines_in_preamble[torque],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_poll_before_submit[local],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_poll_before_submit[pbs],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_poll_before_submit[slurm],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_poll_before_submit[squeue],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_poll_before_submit[torque],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_prepare[local],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_prepare[pbs],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_prepare[slurm],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_prepare[squeue],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_prepare[torque],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_prepare_no_exclusive[slurm],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_prepare_no_exclusive[squeue],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_prepare_no_smt[squeue],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_prepare_without_smt[squeue],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_prepare_with_smt[slurm],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_prepare_with_smt[squeue],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_submit[local],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_submit_timelimit[local],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_wait_before_submit[local],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_wait_before_submit[pbs],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_wait_before_submit[slurm],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_wait_before_submit[squeue],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eth-cscs/reframe,576eb3f1dcc015d1e6d7a10602c748d4f810da68,unittests/test_schedulers.py::test_wait_before_submit[torque],unittests/test_schedulers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich) # ReFrame Project Developers. See the top-level LICENSE file for details. # # SPDX-License-Identifier: BSD-3-Clause import abc import functools import os import pytest import re import socket import tempfile import time import unittest from datetime import datetime, timedelta import reframe.core.runtime as rt import reframe.utility.os_ext as os_ext import unittests.fixtures as fixtures from reframe.core.backends import (getlauncher, getscheduler) from reframe.core.environments import Environment from reframe.core.exceptions import JobError, JobNotStartedError from reframe.core.launchers.local import LocalLauncher from reframe.core.schedulers import Job from reframe.core.schedulers.slurm import _SlurmNode, _create_nodes @pytest.fixture def launcher(): return getlauncher('local') @pytest.fixture(params=['slurm', 'squeue', 'local', 'pbs', 'torque']) def scheduler(request): return getscheduler(request.param) @pytest.fixture def slurm_only(scheduler): if scheduler.registered_name not in ('slurm', 'squeue'): pytest.skip(f'test is relevant only for Slurm backends') @pytest.fixture def local_only(scheduler): if scheduler.registered_name != 'local': pytest.skip(f'test is relevant only for the local scheduler') @pytest.fixture def temp_runtime(tmp_path): def _temp_runtime(site_config, system=None, options={}): options.update({'systems/prefix': tmp_path}) with rt.temp_runtime(site_config, system, options): yield rt.runtime yield _temp_runtime @pytest.fixture def exec_ctx(temp_runtime, scheduler): if fixtures.USER_CONFIG_FILE and scheduler.registered_name != 'local': rt = temp_runtime(fixtures.USER_CONFIG_FILE, fixtures.USER_SYSTEM) else: rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic') next(rt) if scheduler.registered_name == 'squeue': # slurm backend fulfills the functionality of the squeue backend, so # if squeue is not configured, use slurm instead partition = (fixtures.partition_by_scheduler('squeue') or fixtures.partition_by_scheduler('slurm')) else: partition = fixtures.partition_by_scheduler(scheduler.registered_name) if partition is None: pytest.skip( f""scheduler '{scheduler.registered_name}' not configured"" ) return partition @pytest.fixture def make_job(scheduler, launcher, tmp_path): def _make_job(**jobargs): return Job.create( scheduler(), launcher(), name='testjob', workdir=tmp_path, script_filename=str(tmp_path / 'job.sh'), stdout=str(tmp_path / 'job.out'), stderr=str(tmp_path / 'job.err'), **jobargs ) return _make_job @pytest.fixture def minimal_job(make_job): return make_job() @pytest.fixture def fake_job(make_job): ret = make_job( sched_nodelist='nid000[00-17]', sched_exclude_nodelist='nid00016', sched_partition='foo', sched_reservation='bar', sched_account='spam', sched_exclusive_access=True ) ret.time_limit = '5m' ret.num_tasks = 16 ret.num_tasks_per_node = 2 ret.num_tasks_per_core = 1 ret.num_tasks_per_socket = 1 ret.num_cpus_per_task = 18 ret.use_smt = True ret.options = ['--gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo'] return ret def prepare_job(job, command='hostname', pre_run=None, post_run=None): environs = [Environment(name='foo', modules=['testmod_foo'])] pre_run = pre_run or ['echo prerun'] post_run = post_run or ['echo postrun'] with rt.module_use('unittests/modules'): job.prepare( [ *pre_run, job.launcher.run_command(job) + ' ' + command, post_run ], environs ) def assert_job_script_sanity(job): '''Assert the sanity of the produced script file.''' with open(job.script_filename) as fp: matches = re.findall(r'echo prerun|echo postrun|hostname', fp.read()) assert ['echo prerun', 'hostname', 'echo postrun'] == matches def _expected_slurm_directives(job): return set([ '#SBATCH --job-name=""testjob""', '#SBATCH --time=0:5:0', '#SBATCH --output=%s' % job.stdout, '#SBATCH --error=%s' % job.stderr, '#SBATCH --ntasks=%s' % job.num_tasks, '#SBATCH --ntasks-per-node=%s' % job.num_tasks_per_node, '#SBATCH --ntasks-per-core=%s' % job.num_tasks_per_core, '#SBATCH --ntasks-per-socket=%s' % job.num_tasks_per_socket, '#SBATCH --cpus-per-task=%s' % job.num_cpus_per_task, '#SBATCH --hint=multithread', '#SBATCH --nodelist=%s' % job.sched_nodelist, '#SBATCH --exclude=%s' % job.sched_exclude_nodelist, '#SBATCH --partition=%s' % job.sched_partition, '#SBATCH --reservation=%s' % job.sched_reservation, '#SBATCH --account=%s' % job.sched_account, '#SBATCH --exclusive', # Custom options and directives '#SBATCH --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) _expected_squeue_directives = _expected_slurm_directives def _expected_pbs_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l select=%s:mpiprocs=%s:ncpus=%s' ':mem=100GB:cpu_type=haswell' % (num_nodes, job.num_tasks_per_node, num_cpus_per_node), '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_torque_directives(job): num_nodes = job.num_tasks // job.num_tasks_per_node num_cpus_per_node = job.num_cpus_per_task * job.num_tasks_per_node return set([ '#PBS -N ""testjob""', '#PBS -l walltime=0:5:0', '#PBS -o %s' % job.stdout, '#PBS -e %s' % job.stderr, '#PBS -l nodes=%s:ppn=%s:haswell' % (num_nodes, num_cpus_per_node), '#PBS -l mem=100GB', '#PBS -q %s' % job.sched_partition, '#PBS --gres=gpu:4', '#DW jobdw capacity=100GB', '#DW stage_in source=/foo' ]) def _expected_local_directives(job): return set() def test_prepare(fake_job): sched_name = fake_job.scheduler.registered_name if sched_name == 'pbs': fake_job.options += ['mem=100GB', 'cpu_type=haswell'] elif sched_name == 'torque': fake_job.options += ['-l mem=100GB', 'haswell'] prepare_job(fake_job) with open(fake_job.script_filename) as fp: found_directives = set(re.findall(r'^\#\w+ .*', fp.read(), re.MULTILINE)) expected_directives = globals()[f'_expected_{sched_name}_directives'] assert_job_script_sanity(fake_job) assert expected_directives(fake_job) == found_directives def test_prepare_no_exclusive(make_job, slurm_only): job = make_job(sched_exclusive_access=False) prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--exclusive', fp.read()) is None def test_prepare_no_smt(fake_job, slurm_only): fake_job.use_smt = None prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint', fp.read()) is None def test_prepare_with_smt(fake_job, slurm_only): fake_job.use_smt = True prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=multithread', fp.read()) is not None def test_prepare_without_smt(fake_job, slurm_only): fake_job.use_smt = False prepare_job(fake_job) with open(fake_job.script_filename) as fp: assert re.search(r'--hint=nomultithread', fp.read()) is not None def test_prepare_nodes_option(temp_runtime, make_job, slurm_only): rt = temp_runtime(fixtures.TEST_CONFIG_FILE, 'generic', {'schedulers/use_nodes_option': True}) next(rt) job = make_job() job.num_tasks = 16 job.num_tasks_per_node = 2 prepare_job(job) with open(job.script_filename) as fp: assert re.search(r'--nodes=8', fp.read()) is not None def test_submit(make_job, exec_ctx): minimal_job = make_job(sched_access=exec_ctx.access) prepare_job(minimal_job) assert minimal_job.nodelist is None minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() # Additional scheduler-specific checks sched_name = minimal_job.scheduler.registered_name if sched_name == 'local': assert [socket.gethostname()] == minimal_job.nodelist assert 0 == minimal_job.exitcode elif sched_name == ('slurm', 'squeue', 'pbs', 'torque'): num_tasks_per_node = minimal_job.num_tasks_per_node or 1 num_nodes = minimal_job.num_tasks // num_tasks_per_node assert num_nodes == len(minimal_job.nodelist) assert 0 == minimal_job.exitcode def test_submit_timelimit(minimal_job, local_only): minimal_job.time_limit = '2s' prepare_job(minimal_job, 'sleep 10') t_job = datetime.now() minimal_job.submit() assert minimal_job.jobid is not None minimal_job.wait() t_job = datetime.now() - t_job assert t_job.total_seconds() >= 2 assert t_job.total_seconds() < 3 with open(minimal_job.stdout) as fp: assert re.search('postrun', fp.read()) is None assert minimal_job.state == 'TIMEOUT' def test_submit_job_array(make_job, slurm_only, exec_ctx): job =", https://github.com/eukaryote/uberdict,852b9d8b80ab032ce7eb5c439ea8084336b69d04,tests/test_uberdict.py::test_pickle_dumpsloads_dotted,tests/test_uberdict.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from collections import Mapping from functools import partial import sys import pytest from uberdict import _descend, _get, udict try: import cPickle as pickle except ImportError: import pickle try: import mock except ImportError: from unittest import mock from collections import namedtuple version_info = namedtuple('version_info', 'major,minor,micro,releaselevel,serial') if sys.version_info[0] < 3: def b(s): return s.encode('utf8') if not isinstance(s, str) else s def u(s): return s.decode('utf8') if isinstance(s, str) else s else: def b(s): return s.encode('utf8') if not isinstance(s, bytes) else s def u(s): return s.decode('utf8') if isinstance(s, bytes) else s class DefaultDict(udict): def __init__(self, default_factory, *args, **kwargs): assert callable(default_factory) self.default_factory = default_factory udict.__init__(self, *args, **kwargs) def __missing__(self, key): return self.default_factory() def fail_factory(*args, **kwargs): pytest.fail() # test utility for getting at the ""real"" mappings of a dict def items(d): """""" Get the items of `d` (a dict/udict) in sorted order as they would be returned if `d` were a plain dict. """""" try: elems = dict.iteritems(d) except AttributeError: elems = dict.items(d) return sorted(elems) def mkdict(cls, *args, **kwargs): # defaultdict needs type as first arg; we use None for testing if cls is DefaultDict: cls = partial(cls, type(None)) return cls(*args, **kwargs) @pytest.fixture def dict_(): return { 'a': {'b': 'a->b'}, '1': {'2': {'3': '1->2->3'}}, 'a.b': 'a.b', 'c': 'c', } @pytest.fixture(params=[dict, udict, DefaultDict]) def alldictfactory(request): return request.param @pytest.fixture(params=[udict, DefaultDict]) def customdictfactory(request): return request.param @pytest.fixture def udict_(dict_): return udict.fromdict(dict_) @pytest.fixture def defaultdict_(dict_): return DefaultDict(lambda: None, dict_) def test_get_bad_type(customdictfactory): with pytest.raises(TypeError): _get(None, None) with pytest.raises(TypeError): _get({}, {}) def test_get_present(customdictfactory): dct = mkdict(customdictfactory, {'a': 1}) assert _get(dct, 'a') == 1 def test_get_custom_getitem(): class C(object): def __getitem__(self, key): if key == 'sentinel': return mock.sentinel raise KeyError(key) c = C() assert _get(c, 'sentinel') is mock.sentinel with pytest.raises(KeyError): _get(c, 'missing') def test_descend_top_level(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) with pytest.raises(ValueError): _descend(dct, 'c') def test_descend_one_dot_match_all(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, 'a.b') assert val is dct['a'] assert token == 'b' def test_descend_one_dot_match_allbutlast(dict_, alldictfactory): # matches all but last token dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, 'a.z') assert val is dct['a'] assert token == 'z' def test_descend_two_dot_match_all(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, '1.2.3') assert val is dct['1']['2'] assert token == '3' def test_descend_two_dot_match_allbutlast(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, '1.2.X') assert val is dct['1']['2'] assert token == 'X' def test_descend_two_dot_match_first(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) with pytest.raises(KeyError) as err: _descend(dct, '1.Z.X') assert err.value.args[0] == 'Z' def test_init_noargs(): assert items(udict()) == [] def test_init_kwargs(): ud = udict(one=1, two=2) assert items(ud) == [('one', 1), ('two', 2)] def test_init_iterable_arg(): lst = [('one', 1), ('two', 2), ('three', 3)] ud = udict(iter(lst)) assert items(ud) == sorted(lst) def test_init_dict_arg(): d = { 'a': 1, 'b': 2, 'c': 3 } ud = udict(d) assert items(ud) == items(d) def test_init_type(): assert isinstance(udict(), udict) assert isinstance(udict(), dict) def test_udict_is_dict_subclass(): assert issubclass(udict, dict) def test_udict_is_mapping(): assert isinstance(udict(), Mapping) def test_udict_is_mapping_subclass(): assert issubclass(udict, Mapping) def test_init_dict_arg_nested_dicts(): d = {'foo': {'foo': 'bar'}} ud = udict(d) assert type(ud['foo']) is dict assert items(d) == [('foo', udict(foo='bar'))] def test_init_dict_arg_dotted_key(): d = {'a.b': 'a.b', 'a': 'a'} ud = udict(d) assert items(ud) == items(d) def test_init_dict_arg_dotted_key_and_nested(): d = { 'a.b': 'a.b', 'c': 'cc', 'a': {'b': 'a->b'} } ud = udict(d) elems = items(ud) assert elems == [ ('a', {'b': 'a->b'}), ('a.b', 'a.b'), ('c', 'cc') ] assert type(elems[0][1]) is dict # not a udict! def test_init_udict_arg(): orig = udict({ 'a': {'b': 'a->b'}, 'c': udict({'d': 'c->d'}) }) ud = udict(orig) elems = items(ud) assert elems == [ ('a', {'b': 'a->b'}), ('c', udict(d='c->d')) ] assert type(elems[0][1]) is dict assert type(elems[1][1]) is udict def test_equality_with_dict(): assert {} == udict() assert udict() == {} d = {'foo': {'bar': 'barbar'}} ud = udict(foo=udict(bar='barbar')) assert d == ud assert ud == d assert {} != udict(x='x') assert {'x': 'x'} != udict() assert udict(a=None) != udict(a={}) def test_equality_with_dict_dotted_key(): d = {'foo.bar': ''} ud = udict({'foo.bar': ''}) assert d == ud assert ud == d def test_equality(): assert udict() == udict() assert udict() != udict(x='') assert udict(x='') != udict() assert udict(a=0) == udict(a=0) assert udict(a=0) == udict(a=0.0) assert udict(a=0) != udict(b=0) assert udict(a=0) != udict(a='0') assert udict(a=0) != udict(a=1) assert udict(a=None) != udict(a={}) assert udict(a={}) == udict(a=udict()) ud1, ud2 = udict(a=udict()), udict(a=udict(a=None)) assert ud1 != ud2 assert ud2 != ud1 ud1, ud2 = udict({'a.b': ''}), udict(a=udict(b='')) assert ud1 != ud2 assert ud2 != ud1 def test_getitem_nonstring_key(): ud = udict() ud[1] = 'one' assert ud[1] == 'one' def test_getitem_none_key(): ud = udict() ud[None] = 'nope' assert ud[None] == 'nope' # @pytest.mark.skipif('sys.version_info[0] > 2') def test_getitem_bytes_key(): ud = udict() ud[b('one')] = 1 assert ud[b('one')] == 1 def test_getitem_unicode_key(): ud = udict() ud[u('one')] = 1 assert ud[u('one')] == 1 def test_getitem_top_level_returns_value(): obj = object() ud = udict(one=obj) assert ud['one'] is obj def test_getitem_top_level_fail_raises_keyerror(): ud = udict(one=1) with pytest.raises(KeyError): ud['two'] def test_getitem_multilevel_returns_value(): ud = udict.fromdict({ 'one': { 'two': 'one->two' }, 'a': { 'b': {'c': 'a->b->c'} } }) assert ud['one.two'] == 'one->two' assert ud['a.b'] == {'c': 'a->b->c'} assert ud['a.b.c'] == 'a->b->c' def test_getitem_multilevel_fail(): ud = udict.fromdict({ 'one': { 'two': 'one->two' }, 'a': { 'b': {'c': 'a->b->c'} } }) try: ud['one.three'] pytest.fail() except KeyError as e: # success # verify args contains first failing token assert e.args == ('three',) try: ud['a.b.x'] pytest.fail() except KeyError as e: assert e.args == ('x',) with pytest.raises(TypeError): # ud['a.b.c'] doesn't support indexing ud['a.b.c.d'] def test_getitem_nested_through_non_dict_typeerror(): """""" TypeError should be raised when trying to traverse through an object that doesn't support `__getitem__`. """""" ud = udict(one=udict(two=2)) with pytest.raises(TypeError): ud['one.two.three'] class BadDict(object): def __init__(self, **kwargs): self.d = kwargs def __getitem__(self, key): return self.d[key] def test_baddict(): bd = BadDict(a=BadDict(b='a->b')) assert bd['a']['b'] == 'a->b' with pytest.raises(KeyError): bd['missing'] def test_getitem_nested_through_non_dict_keyerror(): """""" KeyError should be raised when trying to traverse through an object that does support `__getitem__` if the object raises KeyError due to no such key. """""" ud = udict( a=BadDict() ) with pytest.raises(KeyError): ud['a.b'] def test_getitem_nested_through_non_dict_success(): ud = udict(a=BadDict(b=udict(c='a->b->c'))) assert ud['a.b'] == udict(c='a->b->c') assert ud['a.b.c'] == 'a->b->c' def test_getitem_dotted_key_top_level_miss(): ud = udict({'a.b': 2}) with pytest.raises(KeyError): ud['a.b'] def test_getitem_subclass_missing(): ud = DefaultDict(int) assert ud['missing'] == 0 ud = DefaultDict(lambda: None) # not type(None) for py2 compability assert ud['missing'] is None def test_setitem_int_key(): ud = udict() ud[1] = 'one' assert items(ud) == [(1, 'one')] def test_setitem_none_key(): ud = udict() ud[None] = 'None' assert items(ud) == [(None, 'None')] def test_setitem_top_level(): ud = udict() ud['one'] = 1 assert ite", https://github.com/eukaryote/uberdict,852b9d8b80ab032ce7eb5c439ea8084336b69d04,tests/test_uberdict.py::test_pickle_dumpsloads_nested,tests/test_uberdict.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from collections import Mapping from functools import partial import sys import pytest from uberdict import _descend, _get, udict try: import cPickle as pickle except ImportError: import pickle try: import mock except ImportError: from unittest import mock from collections import namedtuple version_info = namedtuple('version_info', 'major,minor,micro,releaselevel,serial') if sys.version_info[0] < 3: def b(s): return s.encode('utf8') if not isinstance(s, str) else s def u(s): return s.decode('utf8') if isinstance(s, str) else s else: def b(s): return s.encode('utf8') if not isinstance(s, bytes) else s def u(s): return s.decode('utf8') if isinstance(s, bytes) else s class DefaultDict(udict): def __init__(self, default_factory, *args, **kwargs): assert callable(default_factory) self.default_factory = default_factory udict.__init__(self, *args, **kwargs) def __missing__(self, key): return self.default_factory() def fail_factory(*args, **kwargs): pytest.fail() # test utility for getting at the ""real"" mappings of a dict def items(d): """""" Get the items of `d` (a dict/udict) in sorted order as they would be returned if `d` were a plain dict. """""" try: elems = dict.iteritems(d) except AttributeError: elems = dict.items(d) return sorted(elems) def mkdict(cls, *args, **kwargs): # defaultdict needs type as first arg; we use None for testing if cls is DefaultDict: cls = partial(cls, type(None)) return cls(*args, **kwargs) @pytest.fixture def dict_(): return { 'a': {'b': 'a->b'}, '1': {'2': {'3': '1->2->3'}}, 'a.b': 'a.b', 'c': 'c', } @pytest.fixture(params=[dict, udict, DefaultDict]) def alldictfactory(request): return request.param @pytest.fixture(params=[udict, DefaultDict]) def customdictfactory(request): return request.param @pytest.fixture def udict_(dict_): return udict.fromdict(dict_) @pytest.fixture def defaultdict_(dict_): return DefaultDict(lambda: None, dict_) def test_get_bad_type(customdictfactory): with pytest.raises(TypeError): _get(None, None) with pytest.raises(TypeError): _get({}, {}) def test_get_present(customdictfactory): dct = mkdict(customdictfactory, {'a': 1}) assert _get(dct, 'a') == 1 def test_get_custom_getitem(): class C(object): def __getitem__(self, key): if key == 'sentinel': return mock.sentinel raise KeyError(key) c = C() assert _get(c, 'sentinel') is mock.sentinel with pytest.raises(KeyError): _get(c, 'missing') def test_descend_top_level(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) with pytest.raises(ValueError): _descend(dct, 'c') def test_descend_one_dot_match_all(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, 'a.b') assert val is dct['a'] assert token == 'b' def test_descend_one_dot_match_allbutlast(dict_, alldictfactory): # matches all but last token dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, 'a.z') assert val is dct['a'] assert token == 'z' def test_descend_two_dot_match_all(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, '1.2.3') assert val is dct['1']['2'] assert token == '3' def test_descend_two_dot_match_allbutlast(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, '1.2.X') assert val is dct['1']['2'] assert token == 'X' def test_descend_two_dot_match_first(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) with pytest.raises(KeyError) as err: _descend(dct, '1.Z.X') assert err.value.args[0] == 'Z' def test_init_noargs(): assert items(udict()) == [] def test_init_kwargs(): ud = udict(one=1, two=2) assert items(ud) == [('one', 1), ('two', 2)] def test_init_iterable_arg(): lst = [('one', 1), ('two', 2), ('three', 3)] ud = udict(iter(lst)) assert items(ud) == sorted(lst) def test_init_dict_arg(): d = { 'a': 1, 'b': 2, 'c': 3 } ud = udict(d) assert items(ud) == items(d) def test_init_type(): assert isinstance(udict(), udict) assert isinstance(udict(), dict) def test_udict_is_dict_subclass(): assert issubclass(udict, dict) def test_udict_is_mapping(): assert isinstance(udict(), Mapping) def test_udict_is_mapping_subclass(): assert issubclass(udict, Mapping) def test_init_dict_arg_nested_dicts(): d = {'foo': {'foo': 'bar'}} ud = udict(d) assert type(ud['foo']) is dict assert items(d) == [('foo', udict(foo='bar'))] def test_init_dict_arg_dotted_key(): d = {'a.b': 'a.b', 'a': 'a'} ud = udict(d) assert items(ud) == items(d) def test_init_dict_arg_dotted_key_and_nested(): d = { 'a.b': 'a.b', 'c': 'cc', 'a': {'b': 'a->b'} } ud = udict(d) elems = items(ud) assert elems == [ ('a', {'b': 'a->b'}), ('a.b', 'a.b'), ('c', 'cc') ] assert type(elems[0][1]) is dict # not a udict! def test_init_udict_arg(): orig = udict({ 'a': {'b': 'a->b'}, 'c': udict({'d': 'c->d'}) }) ud = udict(orig) elems = items(ud) assert elems == [ ('a', {'b': 'a->b'}), ('c', udict(d='c->d')) ] assert type(elems[0][1]) is dict assert type(elems[1][1]) is udict def test_equality_with_dict(): assert {} == udict() assert udict() == {} d = {'foo': {'bar': 'barbar'}} ud = udict(foo=udict(bar='barbar')) assert d == ud assert ud == d assert {} != udict(x='x') assert {'x': 'x'} != udict() assert udict(a=None) != udict(a={}) def test_equality_with_dict_dotted_key(): d = {'foo.bar': ''} ud = udict({'foo.bar': ''}) assert d == ud assert ud == d def test_equality(): assert udict() == udict() assert udict() != udict(x='') assert udict(x='') != udict() assert udict(a=0) == udict(a=0) assert udict(a=0) == udict(a=0.0) assert udict(a=0) != udict(b=0) assert udict(a=0) != udict(a='0') assert udict(a=0) != udict(a=1) assert udict(a=None) != udict(a={}) assert udict(a={}) == udict(a=udict()) ud1, ud2 = udict(a=udict()), udict(a=udict(a=None)) assert ud1 != ud2 assert ud2 != ud1 ud1, ud2 = udict({'a.b': ''}), udict(a=udict(b='')) assert ud1 != ud2 assert ud2 != ud1 def test_getitem_nonstring_key(): ud = udict() ud[1] = 'one' assert ud[1] == 'one' def test_getitem_none_key(): ud = udict() ud[None] = 'nope' assert ud[None] == 'nope' # @pytest.mark.skipif('sys.version_info[0] > 2') def test_getitem_bytes_key(): ud = udict() ud[b('one')] = 1 assert ud[b('one')] == 1 def test_getitem_unicode_key(): ud = udict() ud[u('one')] = 1 assert ud[u('one')] == 1 def test_getitem_top_level_returns_value(): obj = object() ud = udict(one=obj) assert ud['one'] is obj def test_getitem_top_level_fail_raises_keyerror(): ud = udict(one=1) with pytest.raises(KeyError): ud['two'] def test_getitem_multilevel_returns_value(): ud = udict.fromdict({ 'one': { 'two': 'one->two' }, 'a': { 'b': {'c': 'a->b->c'} } }) assert ud['one.two'] == 'one->two' assert ud['a.b'] == {'c': 'a->b->c'} assert ud['a.b.c'] == 'a->b->c' def test_getitem_multilevel_fail(): ud = udict.fromdict({ 'one': { 'two': 'one->two' }, 'a': { 'b': {'c': 'a->b->c'} } }) try: ud['one.three'] pytest.fail() except KeyError as e: # success # verify args contains first failing token assert e.args == ('three',) try: ud['a.b.x'] pytest.fail() except KeyError as e: assert e.args == ('x',) with pytest.raises(TypeError): # ud['a.b.c'] doesn't support indexing ud['a.b.c.d'] def test_getitem_nested_through_non_dict_typeerror(): """""" TypeError should be raised when trying to traverse through an object that doesn't support `__getitem__`. """""" ud = udict(one=udict(two=2)) with pytest.raises(TypeError): ud['one.two.three'] class BadDict(object): def __init__(self, **kwargs): self.d = kwargs def __getitem__(self, key): return self.d[key] def test_baddict(): bd = BadDict(a=BadDict(b='a->b')) assert bd['a']['b'] == 'a->b' with pytest.raises(KeyError): bd['missing'] def test_getitem_nested_through_non_dict_keyerror(): """""" KeyError should be raised when trying to traverse through an object that does support `__getitem__` if the object raises KeyError due to no such key. """""" ud = udict( a=BadDict() ) with pytest.raises(KeyError): ud['a.b'] def test_getitem_nested_through_non_dict_success(): ud = udict(a=BadDict(b=udict(c='a->b->c'))) assert ud['a.b'] == udict(c='a->b->c') assert ud['a.b.c'] == 'a->b->c' def test_getitem_dotted_key_top_level_miss(): ud = udict({'a.b': 2}) with pytest.raises(KeyError): ud['a.b'] def test_getitem_subclass_missing(): ud = DefaultDict(int) assert ud['missing'] == 0 ud = DefaultDict(lambda: None) # not type(None) for py2 compability assert ud['missing'] is None def test_setitem_int_key(): ud = udict() ud[1] = 'one' assert items(ud) == [(1, 'one')] def test_setitem_none_key(): ud = udict() ud[None] = 'None' assert items(ud) == [(None, 'None')] def test_setitem_top_level(): ud = udict() ud['one'] = 1 assert ite", https://github.com/eukaryote/uberdict,852b9d8b80ab032ce7eb5c439ea8084336b69d04,tests/test_uberdict.py::test_pickle_dumpsloads_nested_dotted,tests/test_uberdict.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from collections import Mapping from functools import partial import sys import pytest from uberdict import _descend, _get, udict try: import cPickle as pickle except ImportError: import pickle try: import mock except ImportError: from unittest import mock from collections import namedtuple version_info = namedtuple('version_info', 'major,minor,micro,releaselevel,serial') if sys.version_info[0] < 3: def b(s): return s.encode('utf8') if not isinstance(s, str) else s def u(s): return s.decode('utf8') if isinstance(s, str) else s else: def b(s): return s.encode('utf8') if not isinstance(s, bytes) else s def u(s): return s.decode('utf8') if isinstance(s, bytes) else s class DefaultDict(udict): def __init__(self, default_factory, *args, **kwargs): assert callable(default_factory) self.default_factory = default_factory udict.__init__(self, *args, **kwargs) def __missing__(self, key): return self.default_factory() def fail_factory(*args, **kwargs): pytest.fail() # test utility for getting at the ""real"" mappings of a dict def items(d): """""" Get the items of `d` (a dict/udict) in sorted order as they would be returned if `d` were a plain dict. """""" try: elems = dict.iteritems(d) except AttributeError: elems = dict.items(d) return sorted(elems) def mkdict(cls, *args, **kwargs): # defaultdict needs type as first arg; we use None for testing if cls is DefaultDict: cls = partial(cls, type(None)) return cls(*args, **kwargs) @pytest.fixture def dict_(): return { 'a': {'b': 'a->b'}, '1': {'2': {'3': '1->2->3'}}, 'a.b': 'a.b', 'c': 'c', } @pytest.fixture(params=[dict, udict, DefaultDict]) def alldictfactory(request): return request.param @pytest.fixture(params=[udict, DefaultDict]) def customdictfactory(request): return request.param @pytest.fixture def udict_(dict_): return udict.fromdict(dict_) @pytest.fixture def defaultdict_(dict_): return DefaultDict(lambda: None, dict_) def test_get_bad_type(customdictfactory): with pytest.raises(TypeError): _get(None, None) with pytest.raises(TypeError): _get({}, {}) def test_get_present(customdictfactory): dct = mkdict(customdictfactory, {'a': 1}) assert _get(dct, 'a') == 1 def test_get_custom_getitem(): class C(object): def __getitem__(self, key): if key == 'sentinel': return mock.sentinel raise KeyError(key) c = C() assert _get(c, 'sentinel') is mock.sentinel with pytest.raises(KeyError): _get(c, 'missing') def test_descend_top_level(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) with pytest.raises(ValueError): _descend(dct, 'c') def test_descend_one_dot_match_all(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, 'a.b') assert val is dct['a'] assert token == 'b' def test_descend_one_dot_match_allbutlast(dict_, alldictfactory): # matches all but last token dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, 'a.z') assert val is dct['a'] assert token == 'z' def test_descend_two_dot_match_all(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, '1.2.3') assert val is dct['1']['2'] assert token == '3' def test_descend_two_dot_match_allbutlast(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, '1.2.X') assert val is dct['1']['2'] assert token == 'X' def test_descend_two_dot_match_first(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) with pytest.raises(KeyError) as err: _descend(dct, '1.Z.X') assert err.value.args[0] == 'Z' def test_init_noargs(): assert items(udict()) == [] def test_init_kwargs(): ud = udict(one=1, two=2) assert items(ud) == [('one', 1), ('two', 2)] def test_init_iterable_arg(): lst = [('one', 1), ('two', 2), ('three', 3)] ud = udict(iter(lst)) assert items(ud) == sorted(lst) def test_init_dict_arg(): d = { 'a': 1, 'b': 2, 'c': 3 } ud = udict(d) assert items(ud) == items(d) def test_init_type(): assert isinstance(udict(), udict) assert isinstance(udict(), dict) def test_udict_is_dict_subclass(): assert issubclass(udict, dict) def test_udict_is_mapping(): assert isinstance(udict(), Mapping) def test_udict_is_mapping_subclass(): assert issubclass(udict, Mapping) def test_init_dict_arg_nested_dicts(): d = {'foo': {'foo': 'bar'}} ud = udict(d) assert type(ud['foo']) is dict assert items(d) == [('foo', udict(foo='bar'))] def test_init_dict_arg_dotted_key(): d = {'a.b': 'a.b', 'a': 'a'} ud = udict(d) assert items(ud) == items(d) def test_init_dict_arg_dotted_key_and_nested(): d = { 'a.b': 'a.b', 'c': 'cc', 'a': {'b': 'a->b'} } ud = udict(d) elems = items(ud) assert elems == [ ('a', {'b': 'a->b'}), ('a.b', 'a.b'), ('c', 'cc') ] assert type(elems[0][1]) is dict # not a udict! def test_init_udict_arg(): orig = udict({ 'a': {'b': 'a->b'}, 'c': udict({'d': 'c->d'}) }) ud = udict(orig) elems = items(ud) assert elems == [ ('a', {'b': 'a->b'}), ('c', udict(d='c->d')) ] assert type(elems[0][1]) is dict assert type(elems[1][1]) is udict def test_equality_with_dict(): assert {} == udict() assert udict() == {} d = {'foo': {'bar': 'barbar'}} ud = udict(foo=udict(bar='barbar')) assert d == ud assert ud == d assert {} != udict(x='x') assert {'x': 'x'} != udict() assert udict(a=None) != udict(a={}) def test_equality_with_dict_dotted_key(): d = {'foo.bar': ''} ud = udict({'foo.bar': ''}) assert d == ud assert ud == d def test_equality(): assert udict() == udict() assert udict() != udict(x='') assert udict(x='') != udict() assert udict(a=0) == udict(a=0) assert udict(a=0) == udict(a=0.0) assert udict(a=0) != udict(b=0) assert udict(a=0) != udict(a='0') assert udict(a=0) != udict(a=1) assert udict(a=None) != udict(a={}) assert udict(a={}) == udict(a=udict()) ud1, ud2 = udict(a=udict()), udict(a=udict(a=None)) assert ud1 != ud2 assert ud2 != ud1 ud1, ud2 = udict({'a.b': ''}), udict(a=udict(b='')) assert ud1 != ud2 assert ud2 != ud1 def test_getitem_nonstring_key(): ud = udict() ud[1] = 'one' assert ud[1] == 'one' def test_getitem_none_key(): ud = udict() ud[None] = 'nope' assert ud[None] == 'nope' # @pytest.mark.skipif('sys.version_info[0] > 2') def test_getitem_bytes_key(): ud = udict() ud[b('one')] = 1 assert ud[b('one')] == 1 def test_getitem_unicode_key(): ud = udict() ud[u('one')] = 1 assert ud[u('one')] == 1 def test_getitem_top_level_returns_value(): obj = object() ud = udict(one=obj) assert ud['one'] is obj def test_getitem_top_level_fail_raises_keyerror(): ud = udict(one=1) with pytest.raises(KeyError): ud['two'] def test_getitem_multilevel_returns_value(): ud = udict.fromdict({ 'one': { 'two': 'one->two' }, 'a': { 'b': {'c': 'a->b->c'} } }) assert ud['one.two'] == 'one->two' assert ud['a.b'] == {'c': 'a->b->c'} assert ud['a.b.c'] == 'a->b->c' def test_getitem_multilevel_fail(): ud = udict.fromdict({ 'one': { 'two': 'one->two' }, 'a': { 'b': {'c': 'a->b->c'} } }) try: ud['one.three'] pytest.fail() except KeyError as e: # success # verify args contains first failing token assert e.args == ('three',) try: ud['a.b.x'] pytest.fail() except KeyError as e: assert e.args == ('x',) with pytest.raises(TypeError): # ud['a.b.c'] doesn't support indexing ud['a.b.c.d'] def test_getitem_nested_through_non_dict_typeerror(): """""" TypeError should be raised when trying to traverse through an object that doesn't support `__getitem__`. """""" ud = udict(one=udict(two=2)) with pytest.raises(TypeError): ud['one.two.three'] class BadDict(object): def __init__(self, **kwargs): self.d = kwargs def __getitem__(self, key): return self.d[key] def test_baddict(): bd = BadDict(a=BadDict(b='a->b')) assert bd['a']['b'] == 'a->b' with pytest.raises(KeyError): bd['missing'] def test_getitem_nested_through_non_dict_keyerror(): """""" KeyError should be raised when trying to traverse through an object that does support `__getitem__` if the object raises KeyError due to no such key. """""" ud = udict( a=BadDict() ) with pytest.raises(KeyError): ud['a.b'] def test_getitem_nested_through_non_dict_success(): ud = udict(a=BadDict(b=udict(c='a->b->c'))) assert ud['a.b'] == udict(c='a->b->c') assert ud['a.b.c'] == 'a->b->c' def test_getitem_dotted_key_top_level_miss(): ud = udict({'a.b': 2}) with pytest.raises(KeyError): ud['a.b'] def test_getitem_subclass_missing(): ud = DefaultDict(int) assert ud['missing'] == 0 ud = DefaultDict(lambda: None) # not type(None) for py2 compability assert ud['missing'] is None def test_setitem_int_key(): ud = udict() ud[1] = 'one' assert items(ud) == [(1, 'one')] def test_setitem_none_key(): ud = udict() ud[None] = 'None' assert items(ud) == [(None, 'None')] def test_setitem_top_level(): ud = udict() ud['one'] = 1 assert ite", https://github.com/eukaryote/uberdict,852b9d8b80ab032ce7eb5c439ea8084336b69d04,tests/test_uberdict.py::test_pickle_dumpsloads_simple,tests/test_uberdict.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from collections import Mapping from functools import partial import sys import pytest from uberdict import _descend, _get, udict try: import cPickle as pickle except ImportError: import pickle try: import mock except ImportError: from unittest import mock from collections import namedtuple version_info = namedtuple('version_info', 'major,minor,micro,releaselevel,serial') if sys.version_info[0] < 3: def b(s): return s.encode('utf8') if not isinstance(s, str) else s def u(s): return s.decode('utf8') if isinstance(s, str) else s else: def b(s): return s.encode('utf8') if not isinstance(s, bytes) else s def u(s): return s.decode('utf8') if isinstance(s, bytes) else s class DefaultDict(udict): def __init__(self, default_factory, *args, **kwargs): assert callable(default_factory) self.default_factory = default_factory udict.__init__(self, *args, **kwargs) def __missing__(self, key): return self.default_factory() def fail_factory(*args, **kwargs): pytest.fail() # test utility for getting at the ""real"" mappings of a dict def items(d): """""" Get the items of `d` (a dict/udict) in sorted order as they would be returned if `d` were a plain dict. """""" try: elems = dict.iteritems(d) except AttributeError: elems = dict.items(d) return sorted(elems) def mkdict(cls, *args, **kwargs): # defaultdict needs type as first arg; we use None for testing if cls is DefaultDict: cls = partial(cls, type(None)) return cls(*args, **kwargs) @pytest.fixture def dict_(): return { 'a': {'b': 'a->b'}, '1': {'2': {'3': '1->2->3'}}, 'a.b': 'a.b', 'c': 'c', } @pytest.fixture(params=[dict, udict, DefaultDict]) def alldictfactory(request): return request.param @pytest.fixture(params=[udict, DefaultDict]) def customdictfactory(request): return request.param @pytest.fixture def udict_(dict_): return udict.fromdict(dict_) @pytest.fixture def defaultdict_(dict_): return DefaultDict(lambda: None, dict_) def test_get_bad_type(customdictfactory): with pytest.raises(TypeError): _get(None, None) with pytest.raises(TypeError): _get({}, {}) def test_get_present(customdictfactory): dct = mkdict(customdictfactory, {'a': 1}) assert _get(dct, 'a') == 1 def test_get_custom_getitem(): class C(object): def __getitem__(self, key): if key == 'sentinel': return mock.sentinel raise KeyError(key) c = C() assert _get(c, 'sentinel') is mock.sentinel with pytest.raises(KeyError): _get(c, 'missing') def test_descend_top_level(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) with pytest.raises(ValueError): _descend(dct, 'c') def test_descend_one_dot_match_all(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, 'a.b') assert val is dct['a'] assert token == 'b' def test_descend_one_dot_match_allbutlast(dict_, alldictfactory): # matches all but last token dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, 'a.z') assert val is dct['a'] assert token == 'z' def test_descend_two_dot_match_all(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, '1.2.3') assert val is dct['1']['2'] assert token == '3' def test_descend_two_dot_match_allbutlast(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) val, token = _descend(dct, '1.2.X') assert val is dct['1']['2'] assert token == 'X' def test_descend_two_dot_match_first(dict_, alldictfactory): dct = mkdict(alldictfactory, dict_) with pytest.raises(KeyError) as err: _descend(dct, '1.Z.X') assert err.value.args[0] == 'Z' def test_init_noargs(): assert items(udict()) == [] def test_init_kwargs(): ud = udict(one=1, two=2) assert items(ud) == [('one', 1), ('two', 2)] def test_init_iterable_arg(): lst = [('one', 1), ('two', 2), ('three', 3)] ud = udict(iter(lst)) assert items(ud) == sorted(lst) def test_init_dict_arg(): d = { 'a': 1, 'b': 2, 'c': 3 } ud = udict(d) assert items(ud) == items(d) def test_init_type(): assert isinstance(udict(), udict) assert isinstance(udict(), dict) def test_udict_is_dict_subclass(): assert issubclass(udict, dict) def test_udict_is_mapping(): assert isinstance(udict(), Mapping) def test_udict_is_mapping_subclass(): assert issubclass(udict, Mapping) def test_init_dict_arg_nested_dicts(): d = {'foo': {'foo': 'bar'}} ud = udict(d) assert type(ud['foo']) is dict assert items(d) == [('foo', udict(foo='bar'))] def test_init_dict_arg_dotted_key(): d = {'a.b': 'a.b', 'a': 'a'} ud = udict(d) assert items(ud) == items(d) def test_init_dict_arg_dotted_key_and_nested(): d = { 'a.b': 'a.b', 'c': 'cc', 'a': {'b': 'a->b'} } ud = udict(d) elems = items(ud) assert elems == [ ('a', {'b': 'a->b'}), ('a.b', 'a.b'), ('c', 'cc') ] assert type(elems[0][1]) is dict # not a udict! def test_init_udict_arg(): orig = udict({ 'a': {'b': 'a->b'}, 'c': udict({'d': 'c->d'}) }) ud = udict(orig) elems = items(ud) assert elems == [ ('a', {'b': 'a->b'}), ('c', udict(d='c->d')) ] assert type(elems[0][1]) is dict assert type(elems[1][1]) is udict def test_equality_with_dict(): assert {} == udict() assert udict() == {} d = {'foo': {'bar': 'barbar'}} ud = udict(foo=udict(bar='barbar')) assert d == ud assert ud == d assert {} != udict(x='x') assert {'x': 'x'} != udict() assert udict(a=None) != udict(a={}) def test_equality_with_dict_dotted_key(): d = {'foo.bar': ''} ud = udict({'foo.bar': ''}) assert d == ud assert ud == d def test_equality(): assert udict() == udict() assert udict() != udict(x='') assert udict(x='') != udict() assert udict(a=0) == udict(a=0) assert udict(a=0) == udict(a=0.0) assert udict(a=0) != udict(b=0) assert udict(a=0) != udict(a='0') assert udict(a=0) != udict(a=1) assert udict(a=None) != udict(a={}) assert udict(a={}) == udict(a=udict()) ud1, ud2 = udict(a=udict()), udict(a=udict(a=None)) assert ud1 != ud2 assert ud2 != ud1 ud1, ud2 = udict({'a.b': ''}), udict(a=udict(b='')) assert ud1 != ud2 assert ud2 != ud1 def test_getitem_nonstring_key(): ud = udict() ud[1] = 'one' assert ud[1] == 'one' def test_getitem_none_key(): ud = udict() ud[None] = 'nope' assert ud[None] == 'nope' # @pytest.mark.skipif('sys.version_info[0] > 2') def test_getitem_bytes_key(): ud = udict() ud[b('one')] = 1 assert ud[b('one')] == 1 def test_getitem_unicode_key(): ud = udict() ud[u('one')] = 1 assert ud[u('one')] == 1 def test_getitem_top_level_returns_value(): obj = object() ud = udict(one=obj) assert ud['one'] is obj def test_getitem_top_level_fail_raises_keyerror(): ud = udict(one=1) with pytest.raises(KeyError): ud['two'] def test_getitem_multilevel_returns_value(): ud = udict.fromdict({ 'one': { 'two': 'one->two' }, 'a': { 'b': {'c': 'a->b->c'} } }) assert ud['one.two'] == 'one->two' assert ud['a.b'] == {'c': 'a->b->c'} assert ud['a.b.c'] == 'a->b->c' def test_getitem_multilevel_fail(): ud = udict.fromdict({ 'one': { 'two': 'one->two' }, 'a': { 'b': {'c': 'a->b->c'} } }) try: ud['one.three'] pytest.fail() except KeyError as e: # success # verify args contains first failing token assert e.args == ('three',) try: ud['a.b.x'] pytest.fail() except KeyError as e: assert e.args == ('x',) with pytest.raises(TypeError): # ud['a.b.c'] doesn't support indexing ud['a.b.c.d'] def test_getitem_nested_through_non_dict_typeerror(): """""" TypeError should be raised when trying to traverse through an object that doesn't support `__getitem__`. """""" ud = udict(one=udict(two=2)) with pytest.raises(TypeError): ud['one.two.three'] class BadDict(object): def __init__(self, **kwargs): self.d = kwargs def __getitem__(self, key): return self.d[key] def test_baddict(): bd = BadDict(a=BadDict(b='a->b')) assert bd['a']['b'] == 'a->b' with pytest.raises(KeyError): bd['missing'] def test_getitem_nested_through_non_dict_keyerror(): """""" KeyError should be raised when trying to traverse through an object that does support `__getitem__` if the object raises KeyError due to no such key. """""" ud = udict( a=BadDict() ) with pytest.raises(KeyError): ud['a.b'] def test_getitem_nested_through_non_dict_success(): ud = udict(a=BadDict(b=udict(c='a->b->c'))) assert ud['a.b'] == udict(c='a->b->c') assert ud['a.b.c'] == 'a->b->c' def test_getitem_dotted_key_top_level_miss(): ud = udict({'a.b': 2}) with pytest.raises(KeyError): ud['a.b'] def test_getitem_subclass_missing(): ud = DefaultDict(int) assert ud['missing'] == 0 ud = DefaultDict(lambda: None) # not type(None) for py2 compability assert ud['missing'] is None def test_setitem_int_key(): ud = udict() ud[1] = 'one' assert items(ud) == [(1, 'one')] def test_setitem_none_key(): ud = udict() ud[None] = 'None' assert items(ud) == [(None, 'None')] def test_setitem_top_level(): ud = udict() ud['one'] = 1 assert ite", https://github.com/femueller/python-n26,22cb609efbf5125e36879441ea20d5a1c2a51e98,tests/test_cards.py::CardsTests::test_block_card_cli_all,tests/test_cards.py,OD-Brit,flaky,RepoArchived,nan,classify;root_cause,"from n26.api import GET, POST from tests.test_api_base import N26TestBase, mock_requests class CardsTests(N26TestBase): """"""Cards tests"""""" @mock_requests(method=GET, response_file=""cards.json"") def test_cards_cli(self): from n26.cli import cards result = self._run_cli_cmd(cards) self.assertIn('MASTERCARD', result.output) self.assertIn('MAESTRO', result.output) self.assertIn('active', result.output) self.assertIn('123456******1234', result.output) @mock_requests(method=GET, response_file=""cards.json"") @mock_requests(method=POST, response_file=""card_block_single.json"") def test_block_card_cli_single(self): from n26.cli import card_block card_id = ""12345678-1234-abcd-abcd-1234567890ab"" result = self._run_cli_cmd(card_block, [""--card"", card_id]) self.assertEqual(result.output, ""Blocked card: {}\n"".format(card_id)) @mock_requests(method=GET, response_file=""cards.json"") @mock_requests(method=POST, response_file=""card_block_single.json"") def test_block_card_cli_all(self): from n26.cli import card_block card_id_1 = ""12345678-1234-abcd-abcd-1234567890ab"" card_id_2 = ""22345678-1234-abcd-abcd-1234567890ab"" result = self._run_cli_cmd(card_block) self.assertEqual(result.output, ""Blocked card: {}\nBlocked card: {}\n"".format(card_id_1, card_id_2)) @mock_requests(method=GET, response_file=""cards.json"") @mock_requests(method=POST, response_file=""card_unblock_single.json"") def test_unblock_card_cli_single(self): from n26.cli import card_unblock card_id = ""12345678-1234-abcd-abcd-1234567890ab"" result = self._run_cli_cmd(card_unblock, [""--card"", card_id]) self.assertEqual(result.output, ""Unblocked card: {}\n"".format(card_id)) @mock_requests(method=GET, response_file=""cards.json"") @mock_requests(method=POST, response_file=""card_unblock_single.json"") def test_unblock_card_cli_all(self): from n26.cli import card_unblock card_id_1 = ""12345678-1234-abcd-abcd-1234567890ab"" card_id_2 = ""22345678-1234-abcd-abcd-1234567890ab"" result = self._run_cli_cmd(card_unblock) self.assertEqual(result.output, ""Unblocked card: {}\nUnblocked card: {}\n"".format(card_id_1, card_id_2)) ", https://github.com/femueller/python-n26,22cb609efbf5125e36879441ea20d5a1c2a51e98,tests/test_cards.py::CardsTests::test_block_card_cli_single,tests/test_cards.py,OD-Brit,flaky,RepoArchived,nan,classify;root_cause,"from n26.api import GET, POST from tests.test_api_base import N26TestBase, mock_requests class CardsTests(N26TestBase): """"""Cards tests"""""" @mock_requests(method=GET, response_file=""cards.json"") def test_cards_cli(self): from n26.cli import cards result = self._run_cli_cmd(cards) self.assertIn('MASTERCARD', result.output) self.assertIn('MAESTRO', result.output) self.assertIn('active', result.output) self.assertIn('123456******1234', result.output) @mock_requests(method=GET, response_file=""cards.json"") @mock_requests(method=POST, response_file=""card_block_single.json"") def test_block_card_cli_single(self): from n26.cli import card_block card_id = ""12345678-1234-abcd-abcd-1234567890ab"" result = self._run_cli_cmd(card_block, [""--card"", card_id]) self.assertEqual(result.output, ""Blocked card: {}\n"".format(card_id)) @mock_requests(method=GET, response_file=""cards.json"") @mock_requests(method=POST, response_file=""card_block_single.json"") def test_block_card_cli_all(self): from n26.cli import card_block card_id_1 = ""12345678-1234-abcd-abcd-1234567890ab"" card_id_2 = ""22345678-1234-abcd-abcd-1234567890ab"" result = self._run_cli_cmd(card_block) self.assertEqual(result.output, ""Blocked card: {}\nBlocked card: {}\n"".format(card_id_1, card_id_2)) @mock_requests(method=GET, response_file=""cards.json"") @mock_requests(method=POST, response_file=""card_unblock_single.json"") def test_unblock_card_cli_single(self): from n26.cli import card_unblock card_id = ""12345678-1234-abcd-abcd-1234567890ab"" result = self._run_cli_cmd(card_unblock, [""--card"", card_id]) self.assertEqual(result.output, ""Unblocked card: {}\n"".format(card_id)) @mock_requests(method=GET, response_file=""cards.json"") @mock_requests(method=POST, response_file=""card_unblock_single.json"") def test_unblock_card_cli_all(self): from n26.cli import card_unblock card_id_1 = ""12345678-1234-abcd-abcd-1234567890ab"" card_id_2 = ""22345678-1234-abcd-abcd-1234567890ab"" result = self._run_cli_cmd(card_unblock) self.assertEqual(result.output, ""Unblocked card: {}\nUnblocked card: {}\n"".format(card_id_1, card_id_2)) ", https://github.com/femueller/python-n26,22cb609efbf5125e36879441ea20d5a1c2a51e98,tests/test_cards.py::CardsTests::test_unblock_card_cli_single,tests/test_cards.py,OD-Brit,flaky,RepoArchived,nan,classify;root_cause,"from n26.api import GET, POST from tests.test_api_base import N26TestBase, mock_requests class CardsTests(N26TestBase): """"""Cards tests"""""" @mock_requests(method=GET, response_file=""cards.json"") def test_cards_cli(self): from n26.cli import cards result = self._run_cli_cmd(cards) self.assertIn('MASTERCARD', result.output) self.assertIn('MAESTRO', result.output) self.assertIn('active', result.output) self.assertIn('123456******1234', result.output) @mock_requests(method=GET, response_file=""cards.json"") @mock_requests(method=POST, response_file=""card_block_single.json"") def test_block_card_cli_single(self): from n26.cli import card_block card_id = ""12345678-1234-abcd-abcd-1234567890ab"" result = self._run_cli_cmd(card_block, [""--card"", card_id]) self.assertEqual(result.output, ""Blocked card: {}\n"".format(card_id)) @mock_requests(method=GET, response_file=""cards.json"") @mock_requests(method=POST, response_file=""card_block_single.json"") def test_block_card_cli_all(self): from n26.cli import card_block card_id_1 = ""12345678-1234-abcd-abcd-1234567890ab"" card_id_2 = ""22345678-1234-abcd-abcd-1234567890ab"" result = self._run_cli_cmd(card_block) self.assertEqual(result.output, ""Blocked card: {}\nBlocked card: {}\n"".format(card_id_1, card_id_2)) @mock_requests(method=GET, response_file=""cards.json"") @mock_requests(method=POST, response_file=""card_unblock_single.json"") def test_unblock_card_cli_single(self): from n26.cli import card_unblock card_id = ""12345678-1234-abcd-abcd-1234567890ab"" result = self._run_cli_cmd(card_unblock, [""--card"", card_id]) self.assertEqual(result.output, ""Unblocked card: {}\n"".format(card_id)) @mock_requests(method=GET, response_file=""cards.json"") @mock_requests(method=POST, response_file=""card_unblock_single.json"") def test_unblock_card_cli_all(self): from n26.cli import card_unblock card_id_1 = ""12345678-1234-abcd-abcd-1234567890ab"" card_id_2 = ""22345678-1234-abcd-abcd-1234567890ab"" result = self._run_cli_cmd(card_unblock) self.assertEqual(result.output, ""Unblocked card: {}\nUnblocked card: {}\n"".format(card_id_1, card_id_2)) ", https://github.com/ferdinandvwyk/pyfilm,20688f59536c713650516f09e1873fcb5b506a17,tests/test_pyfilm.py::TestClass::test_calculate_cbar_ticks,tests/test_pyfilm.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest import numpy as np from PIL import Image import matplotlib import warnings matplotlib.use('Agg') # specifically for Travis CI to avoid backend errors from pyfilm.pyfilm import * class TestClass(object): """""" Class containing methods which test pyfilm. """""" def teardown_class(self): os.system('rm -rf films') os.system('rm -rf __pycache__') def test_set_default_options(self): options = {} options = set_default_options(options) assert options['aspect'] == 'auto' assert options['crop'] == True assert options['cbar_label'] == 'f(x,y)' assert options['encoder'] == None assert options['fps'] == 10 assert options['file_name'] == 'f' assert options['grid'] == False assert options['img_fmt'] == 'png' assert options['ncontours'] == 11 assert type(options['nprocs']) == int assert options['title'] == '' assert options['video_fmt'] == 'mp4' assert options['xlabel'] == 'x' assert options['xlim'] == None assert options['xticks'] == None assert options['ylabel'] == 'y' assert options['ylim'] == None assert options['yticks'] == None def test_set_user_options(self): options = {} options = set_default_options(options) user_options = {'xlabel':'test'} options = set_user_options(options, user_options) assert options['xlabel'] == 'test' def test_set_up_dirs(self): options = {} options = set_default_options(options) set_up_dirs(options) assert os.path.exists('films') == True assert os.path.exists('films/film_frames') == True def test_find_encoder(self): options = find_encoder({}) assert (options['encoder'] == 'avconv' or options['encoder'] == 'ffmpeg') def test_set_ylim(self): y = np.array([range(5)]*5) options = {} set_ylim(y, options) assert(options['ylim'] == [0,4]) y = np.array([range(5)]*5) - 3 set_ylim(y, options) assert(options['ylim'] == [-3,3]) y = np.array([range(5)]*5) - 4 set_ylim(y, options) assert(options['ylim'] == [-4,0]) def test_check_data_1d_with_wrong_size(self): x = np.arange(5) y = np.random.rand(5,4) pytest.raises(ValueError, ""check_data_1d(x,y)"") def test_check_data_1d_with_wrong_shapes(self): x = np.random.rand(5,5) y = np.random.rand(5,5) pytest.raises(IndexError, ""check_data_1d(x,y)"") x = np.arange(5) y = np.random.rand(5,5,4) pytest.raises(IndexError, ""check_data_1d(x,y)"") def test_plot_1d(self): x = np.arange(2) y = np.random.rand(2,2) os.system('rm films/film_frames/*.png') options = {} options = set_default_options(options) set_up_dirs(options) make_plot_titles(2, options) args = (0, x, y[0,:], {}, options) plot_1d(args) assert ('f_00000.png' in os.listdir('films/film_frames/')) def test_1d_1_arg(self): y = np.random.rand(2,2) make_film_1d(y) assert ('f_00000.png' in os.listdir('films/film_frames/')) assert ('f.mp4' in os.listdir('films/')) def test_1d_2_arg(self): x = np.arange(2) y = np.random.rand(2,2) make_film_1d(x, y) assert ('f_00000.png' in os.listdir('films/film_frames/')) assert ('f.mp4' in os.listdir('films/')) def test_1d_argumets(self): x = np.arange(2) y = np.random.rand(2,2) pytest.raises(ValueError, ""make_film_1d(x, x, y)"") def test_crop_images(self): x = np.arange(2) y = np.random.rand(2,2) make_film_1d(x, y) im = Image.open('films/film_frames/f_00000.png') w = im.size[0] h = im.size[1] assert (w%2 == 0 and h%2 == 0) def test_2d_1_arg(self): z = np.random.rand(2,2,2) make_film_2d(z) assert ('f_00000.png' in os.listdir('films/film_frames/')) assert ('f.mp4' in os.listdir('films/')) def test_2d_1_arg_negative(self): z = np.array([[[-10, -6], [ 1, -5]],[[ 0, -8],[ -1, -3]]]) make_film_2d(z) assert ('f_00000.png' in os.listdir('films/film_frames/')) assert ('f.mp4' in os.listdir('films/')) def test_2d_3_arg(self): x = np.random.rand(2) y = np.random.rand(2) z = np.random.rand(2,2,2) make_film_2d(x, y, z) assert ('f_00000.png' in os.listdir('films/film_frames/')) assert ('f.mp4' in os.listdir('films/')) def test_plot_2d(self): x = np.arange(2) y = np.arange(2) z = np.random.rand(2,2,2) os.system('rm films/film_frames/*.png') options = {} options = set_default_options(options) set_up_dirs(options) make_plot_titles(2, options) args = (0, x, y, z[0,:,:], {}, options) plot_2d(args) assert ('f_00000.png' in os.listdir('films/film_frames/')) def test_check_data_2d_with_wrong_sizes(self): x = np.arange(5) y = np.arange(5) z = np.random.rand(5,4,5) pytest.raises(ValueError, ""check_data_2d(x,y,z)"") z = np.random.rand(5,5,4) pytest.raises(ValueError, ""check_data_2d(x,y,z)"") def test_check_data_2d_with_wrong_shapes(self): x = np.random.rand(5,5) y = np.random.rand(5) z = np.random.rand(5,5,5) pytest.raises(IndexError, ""check_data_2d(x,y,z)"") x = np.random.rand(5) y = np.random.rand(5,5) z = np.random.rand(5,5,5) pytest.raises(IndexError, ""check_data_2d(x,y,z)"") x = np.random.rand(5) y = np.random.rand(5) z = np.random.rand(5,5) pytest.raises(IndexError, ""check_data_2d(x,y,z)"") def test_2d_argumets(self): x = np.arange(2) y = np.arange(2) z = np.random.rand(2,2,2) pytest.raises(ValueError, ""make_film_2d(x, z)"") pytest.raises(ValueError, ""make_film_2d(x, y, y, z)"") def test_make_plot_titles(self, recwarn): options = {} options = set_default_options(options) options = make_plot_titles(10, options) assert len(options['title']) == 10 options['title'] = ['test']*9 pytest.raises(ValueError, ""make_plot_titles(10, options)"") def test_img_fmt(self): x = np.arange(2) y = np.random.rand(2,2) make_film_1d(x, y, options={'img_fmt':'jpg'}) assert ('f_00000.jpg' in os.listdir('films/film_frames/')) assert ('f.mp4' in os.listdir('films/')) def test_calculate_cbar_ticks(self): options = {} options = set_default_options(options) z = np.random.randint(low=0, high=2, size=[2,2,2]) options = calculate_cbar_ticks(z, options) assert np.sum(options['cbar_ticks'] - np.linspace(0, 1, 5)) < 1e-5 options = {} options = set_default_options(options) options['cbar_ticks'] = 7 z = np.random.randint(low=0, high=2, size=[2,2,2]) options = calculate_cbar_ticks(z, options) assert np.sum(options['cbar_ticks'] - np.linspace(0, 1, 7)) < 1e-5 def test_calculate_contours(self): options = {} options = set_default_options(options) plot_options = {} z = np.reshape(np.arange(8), [2,2,2]) options = calculate_contours(z, options, plot_options) assert np.sum(plot_options['levels'] - np.linspace(0, 7, 11)) < 1e-5 options = {} options = set_default_options(options) options['ncontours'] = 21 plot_options = {} z = np.reshape(np.arange(8), [2,2,2]) options = calculate_contours(z, options, plot_options) assert np.sum(plot_options['levels'] - np.linspace(0, 7, 21)) < 1e-5 ", https://github.com/fifman/tangle,a36cff74d7f09bf8f24a91929b844ea1be7b1bcd,tests/test_bean.py::test_instance,tests/test_bean.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"#! /usr/bin/env python # -*- coding: utf-8 -*- import pytest from tangle.m_annotation import Annotated from tangle.m_bean import get_fields, Field from tests.common_utils import EventRegister _register = EventRegister() class Alpha(object): pass @Annotated class TestBean(object): @Field() def field1(self): _register.register(1) @Field(Alpha, True) def field2(self): _register.register(2) @Field(autowire=True) def field3(self): _register.register(3) @field1.setter def set_field1(self, value): _register.register(4, value) def test_field(): field1, field2, field3 = get_fields(TestBean) assert field1.name == ""field1"" assert not field1.klass assert not field1.autowire assert field2.klass == Alpha assert field2.name == ""field2"" assert field2.autowire assert field3.autowire assert not field3.klass _register.clear() def test_instance(): tb = TestBean() _register.uncheck(4) tb.field1 = 1 _register.check(4, 1) _register.uncheck(1) assert tb.field1 == 1 _register.check(1) with pytest.raises(TypeError): tb.field2 = True alpha = Alpha() tb.field2 = alpha _register.uncheck(2) assert tb.field2 == alpha _register.check(2) tb.field3 = ""test"" _register.uncheck(3) assert tb.field3 == ""test"" _register.check(3) ", https://github.com/fifman/tangle,a36cff74d7f09bf8f24a91929b844ea1be7b1bcd,tests/test_context.py::test_aspect,tests/test_context.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"#! /usr/bin/env python # -*- coding: utf-8 -*- import pytest from tangle.m_annotation import Annotated from tangle.m_bean import Field from tangle.m_container import Bean from tangle.m_support import BaseApplicationContext from tests import common_utils from tests.demo.context import TestPostProcessor from tests.user.user import TestChair, ChairAspect, temp_cache class BeanA(object): pass class BeanB(object): def __init__(self, bean_a): self.bean_a = bean_a @Annotated class BeanC(object): @Field(BeanA, True) def test(self): pass @Field(BeanB) def test2(self): pass @Annotated class TestConfig(object): @Bean() def beana(self): return BeanA() @Bean() def beanb(self): return BeanB(self.beana()) @Bean(scope=Bean.Prototype) def beanc(self): return BeanC() @Bean() def container(self): return self @Bean() def bean_post_test(self): return TestPostProcessor() test_config = TestConfig() context = BaseApplicationContext([test_config]) context.build() @Annotated class TestAspect(object): @Bean() def test_chair(self): return TestChair() @Bean(Bean.Prototype) def test_chair2(self): return TestChair() @Bean() def chair_aspect(self): return ChairAspect() test_aspect_inst = TestAspect() aspect_context = BaseApplicationContext([test_aspect_inst], parent=context) aspect_context.build() @common_utils.test_deco def test_config_factory(): assert test_config.beanc().test == test_config.beana() assert test_config.beana() == test_config.beanb().bean_a instc = test_config.beanc() assert not instc == test_config.beanc() assert getattr(instc, ""_test_post_init"") assert instc.test == test_config.beana() with pytest.raises(AttributeError) as ex: print(test_config.beanc().test2) assert ""not set yet"" in ex.value.args[0] @common_utils.test_deco def test_aspect(): test_chair = test_aspect_inst.test_chair() assert test_chair == aspect_context.get(""test_chair"") test_chair.add_leg() assert not temp_cache.get(""before__add_leg"") assert temp_cache.get(""after__add_leg"") == 1 test_chair.add_leg() assert temp_cache.get(""after__add_leg"") == 2 test_chair.change_name(""xxx"") assert temp_cache.get(""before__change_name"") == (""xxx"",) with pytest.raises(ValueError) as ex: test_chair.increase(-3) assert ""larger than 0"" in ex.value.args[0] test_chair.construct(""chair2"", ""steel"", ""leg3"", 2) assert temp_cache[""result""][3] == 8 assert test_chair._test_post_init test_chair2 = test_aspect_inst.test_chair2() assert test_chair2._test_post_init with pytest.raises(ValueError) as ex: test_chair2.increase(-3) assert ""larger than 0"" in ex.value.args[0] ", https://github.com/filestack/filestack-python,1bd6749735e5c78dde767e0685ddfdf0cd7423e2,tests/intelligent_ingestion_test.py::test_upload_part_success,tests/intelligent_ingestion_test.py,OD-Vic,flaky,nan,nan,classify;root_cause,"from unittest.mock import patch, call, ANY from collections import defaultdict import pytest from filestack import Security from filestack.uploads.intelligent_ingestion import upload_part, upload from tests.helpers import DummyHttpResponse @patch('filestack.uploads.intelligent_ingestion.requests.put') @patch('filestack.uploads.intelligent_ingestion.requests.post') def test_upload_part_success(post_mock, put_mock): post_mock.side_effect = [ DummyHttpResponse(json_dict={'url': 'http://upload.url', 'headers': {'upload': 'headers'}}), DummyHttpResponse() ] put_mock.return_value = DummyHttpResponse() part = {'seek_point': 0, 'num': 1} upload_part( 'Aaaaapikey', 'file.txt', 'tests/data/doom.mp4', 1234, 's3', defaultdict(lambda: 'fs-upload.com'), part ) assert post_mock.call_args_list == [ call( 'https://fs-upload.com/multipart/upload', json={ 'apikey': 'Aaaaapikey', 'uri': 'fs-upload.com', 'region': 'fs-upload.com', 'upload_id': 'fs-upload.com', 'store': {'location': 's3'}, 'part': 1, 'size': 5415034, 'md5': 'IuNjhgPo2wbzGFo6f7WhUA==', 'offset': 0, 'fii': True }, ), call( 'https://fs-upload.com/multipart/commit', json={ 'apikey': 'Aaaaapikey', 'uri': 'fs-upload.com', 'region': 'fs-upload.com', 'upload_id': 'fs-upload.com', 'store': {'location': 's3'}, 'part': 1, 'size': 1234 }, ) ] put_mock.assert_called_once_with( 'http://upload.url', data=ANY, headers={'upload': 'headers'} ) @patch('filestack.uploads.intelligent_ingestion.requests.put') @patch('filestack.uploads.intelligent_ingestion.requests.post') def test_upload_part_with_resize(post_mock, put_mock): # this mock will work fine for commit request too post_mock.return_value = DummyHttpResponse( ok=True, json_dict={'url': 'http://upload.url', 'headers': {'upload': 'headers'}} ) put_mock.side_effect = [ DummyHttpResponse(ok=False), # fail first attempt, should split file part DummyHttpResponse(), # part 1, chunk 1 DummyHttpResponse(), # part 1, chunk 2 ] part = {'seek_point': 0, 'num': 1} upload_part( 'Aaaaapikey', 'file.txt', 'tests/data/doom.mp4', 5415034, 's3', defaultdict(lambda: 'fs-upload.com'), part ) assert post_mock.call_count == 4 # 3x upload, 1 commit # 1st attempt req_args, req_kwargs = post_mock.call_args_list[0] assert req_kwargs['json']['size'] == 5415034 # 2nd attempt req_args, req_kwargs = post_mock.call_args_list[1] assert req_kwargs['json']['size'] == 4194304 # 3rd attempt req_args, req_kwargs = post_mock.call_args_list[2] assert req_kwargs['json']['size'] == 1220730 @patch('filestack.uploads.intelligent_ingestion.requests.put') @patch('filestack.uploads.intelligent_ingestion.requests.post') def test_min_chunk_size_exception(post_mock, put_mock): post_mock.return_value = DummyHttpResponse( ok=True, json_dict={'url': 'http://upload.url', 'headers': {'upload': 'headers'}} ) put_mock.return_value = DummyHttpResponse(ok=False) part = {'seek_point': 0, 'num': 1} with pytest.raises(Exception, match='Minimal chunk size failed'): upload_part( 'Aaaaapikey', 'file.txt', 'tests/data/doom.mp4', 5415034, 's3', defaultdict(lambda: 'fs-upload.com'), part ) ", https://github.com/filestack/filestack-python,1bd6749735e5c78dde767e0685ddfdf0cd7423e2,tests/intelligent_ingestion_test.py::test_upload_part_with_resize,tests/intelligent_ingestion_test.py,NIO,flaky,nan,nan,classify;root_cause,"from unittest.mock import patch, call, ANY from collections import defaultdict import pytest from filestack import Security from filestack.uploads.intelligent_ingestion import upload_part, upload from tests.helpers import DummyHttpResponse @patch('filestack.uploads.intelligent_ingestion.requests.put') @patch('filestack.uploads.intelligent_ingestion.requests.post') def test_upload_part_success(post_mock, put_mock): post_mock.side_effect = [ DummyHttpResponse(json_dict={'url': 'http://upload.url', 'headers': {'upload': 'headers'}}), DummyHttpResponse() ] put_mock.return_value = DummyHttpResponse() part = {'seek_point': 0, 'num': 1} upload_part( 'Aaaaapikey', 'file.txt', 'tests/data/doom.mp4', 1234, 's3', defaultdict(lambda: 'fs-upload.com'), part ) assert post_mock.call_args_list == [ call( 'https://fs-upload.com/multipart/upload', json={ 'apikey': 'Aaaaapikey', 'uri': 'fs-upload.com', 'region': 'fs-upload.com', 'upload_id': 'fs-upload.com', 'store': {'location': 's3'}, 'part': 1, 'size': 5415034, 'md5': 'IuNjhgPo2wbzGFo6f7WhUA==', 'offset': 0, 'fii': True }, ), call( 'https://fs-upload.com/multipart/commit', json={ 'apikey': 'Aaaaapikey', 'uri': 'fs-upload.com', 'region': 'fs-upload.com', 'upload_id': 'fs-upload.com', 'store': {'location': 's3'}, 'part': 1, 'size': 1234 }, ) ] put_mock.assert_called_once_with( 'http://upload.url', data=ANY, headers={'upload': 'headers'} ) @patch('filestack.uploads.intelligent_ingestion.requests.put') @patch('filestack.uploads.intelligent_ingestion.requests.post') def test_upload_part_with_resize(post_mock, put_mock): # this mock will work fine for commit request too post_mock.return_value = DummyHttpResponse( ok=True, json_dict={'url': 'http://upload.url', 'headers': {'upload': 'headers'}} ) put_mock.side_effect = [ DummyHttpResponse(ok=False), # fail first attempt, should split file part DummyHttpResponse(), # part 1, chunk 1 DummyHttpResponse(), # part 1, chunk 2 ] part = {'seek_point': 0, 'num': 1} upload_part( 'Aaaaapikey', 'file.txt', 'tests/data/doom.mp4', 5415034, 's3', defaultdict(lambda: 'fs-upload.com'), part ) assert post_mock.call_count == 4 # 3x upload, 1 commit # 1st attempt req_args, req_kwargs = post_mock.call_args_list[0] assert req_kwargs['json']['size'] == 5415034 # 2nd attempt req_args, req_kwargs = post_mock.call_args_list[1] assert req_kwargs['json']['size'] == 4194304 # 3rd attempt req_args, req_kwargs = post_mock.call_args_list[2] assert req_kwargs['json']['size'] == 1220730 @patch('filestack.uploads.intelligent_ingestion.requests.put') @patch('filestack.uploads.intelligent_ingestion.requests.post') def test_min_chunk_size_exception(post_mock, put_mock): post_mock.return_value = DummyHttpResponse( ok=True, json_dict={'url': 'http://upload.url', 'headers': {'upload': 'headers'}} ) put_mock.return_value = DummyHttpResponse(ok=False) part = {'seek_point': 0, 'num': 1} with pytest.raises(Exception, match='Minimal chunk size failed'): upload_part( 'Aaaaapikey', 'file.txt', 'tests/data/doom.mp4', 5415034, 's3', defaultdict(lambda: 'fs-upload.com'), part ) ", https://github.com/fooof-tools/fooof,14d6196e0b60c7e6da95b5cf858b20adcc5fc0ac,fooof/tests/utils/test_download.py::test_check_data_file,fooof/tests/utils/test_download.py,NOD,flaky,Rejected,https://github.com/fooof-tools/fooof/pull/242,classify;root_cause,"""""""Test functions for fooof.utils.download."""""" import os import shutil import numpy as np from fooof.utils.download import * ################################################################################################### ################################################################################################### TEST_FOLDER = 'test_data' def clean_up_downloads(): shutil.rmtree(TEST_FOLDER) ################################################################################################### ################################################################################################### def test_check_data_folder(): check_data_folder(TEST_FOLDER) assert os.path.isdir(TEST_FOLDER) def test_check_data_file(): filename = 'freqs.npy' check_data_file(filename, TEST_FOLDER) assert os.path.isfile(os.path.join(TEST_FOLDER, filename)) def test_fetch_fooof_data(): filename = 'spectrum.npy' fetch_fooof_data(filename, folder=TEST_FOLDER) assert os.path.isfile(os.path.join(TEST_FOLDER, filename)) clean_up_downloads() def test_load_fooof_data(): filename = 'freqs.npy' data = load_fooof_data(filename, folder=TEST_FOLDER) assert isinstance(data, np.ndarray) clean_up_downloads() ", https://github.com/FoteinosMerg/pymerkle,5318009e04b2ae44e194b8435ef181aa67ab8b9a,tests/tree/test_structure.py::test_1_leaves,tests/tree/test_structure.py,NIO,flaky,nan,nan,classify;root_cause,""""""" Utilizes hash comparison in order to verify that the the .update() method of the tree.MerkleTree class behaves as prescribed (preserves the tree's property of being binary-balanced) """""" import pytest from pymerkle import MerkleTree from pymerkle.hashing import HashMachine from pymerkle.exceptions import EmptyTreeException tree = MerkleTree() machine = HashMachine() update = tree.update hash = machine.hash t_1, t_2, t_3, t_4, t_5, t_6, t_7, t_8, t_9, t_10, t_11 = \ 'ingi', 'rum', 'imus', 'noc', 'te', 'et', 'con', 'su', 'mi', 'mur', 'igni' def test_0_leaves(): with pytest.raises(EmptyTreeException): tree.rootHash def test_1_leaves(): update(record=t_1) assert tree.rootHash == hash(t_1) def test_2_leaves(): update(record=t_2) assert tree.rootHash == hash( hash(t_1), hash(t_2) ) def test_3_leaves(): update(record=t_3) assert tree.rootHash == hash( hash( hash(t_1), hash(t_2) ), hash(t_3) ) def test_4_leaves(): update(record=t_4) assert tree.rootHash == hash( hash( hash(t_1), hash(t_2) ), hash( hash(t_3), hash(t_4) ) ) def test_5_leaves(): update(record=t_5) assert tree.rootHash == hash( hash( hash( hash(t_1), hash(t_2) ), hash( hash(t_3), hash(t_4) ) ), hash(t_5) ) def test_7_leaves(): update(record=t_6) update(record=t_7) assert tree.rootHash == hash( hash( hash( hash(t_1), hash(t_2) ), hash( hash(t_3), hash(t_4) ) ), hash( hash( hash(t_5), hash(t_6) ), hash(t_7) ) ) def test_11_leaves(): update(record=t_8) update(record=t_9) update(record=t_10) update(record=t_11) assert tree.rootHash == hash( hash( hash( hash( hash(t_1), hash(t_2) ), hash( hash(t_3), hash(t_4) ) ), hash( hash( hash(t_5), hash(t_6) ), hash( hash(t_7), hash(t_8) ) ) ), hash( hash( hash(t_9), hash(t_10) ), hash(t_11) ) ) ", https://github.com/ganehag/pyMeterBus,bc853aa38ac6b10301bdf97f13ac25b36985316f,tests/test_globals.py::TestSequenceFunctions::test_debug_default_value,tests/test_globals.py,OD-Vic,flaky,Opened,https://github.com/ganehag/pyMeterBus/pull/25,classify;root_cause,"# -*- coding: utf-8 -*- import os import sys myPath = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, myPath + '/../') import unittest import meterbus from meterbus.exceptions import * class TestSequenceFunctions(unittest.TestCase): def setUp(self): pass def test_debug_default_value(self): self.assertEqual(meterbus.g.debug, False) def test_debug_set_true(self): meterbus.debug(True) self.assertEqual(meterbus.g.debug, True) if __name__ == '__main__': unittest.main() ", https://github.com/gdoumenc/coworks,c574654a1e3d1cd4ad56b6f5912631230c37fdf3,tests/example/test_example.py::TestClass::test_simple_example,tests/example/test_example.py,NIO,flaky,Deleted,nan,classify;root_cause,"import threading import time from pathlib import Path import requests from coworks.config import Config from coworks.cws.runner import ThreadedLocalServer from coworks.utils import import_attr from .example import TechMS class TestClass: def test_simple_example(self, local_server_factory): local_server = local_server_factory(TechMS()) response = local_server.make_call(requests.get, '/') assert response.status_code == 200 assert response.text == ""Simple microservice for test.\n"" response = local_server.make_call(requests.get, '/', params={""usage"": ""demo""}) assert response.status_code == 200 assert response.text == ""Simple microservice for demo.\n"" response = local_server.make_call(requests.get, '/value/1') assert response.status_code == 200 assert response.text == ""0\n"" response = local_server.make_call(requests.put, '/value/1', json={""value"": 456}) assert response.status_code == 200 assert response.text == ""456"" response = local_server.make_call(requests.get, '/value/1') assert response.status_code == 200 assert response.text == ""456\n"" def test_params(self, local_server_factory): local_server = local_server_factory(TechMS()) response = local_server.make_call(requests.put, '/value/1', json=456) assert response.status_code == 200 assert response.text == ""456"" response = local_server.make_call(requests.get, '/value/1') assert response.status_code == 200 assert response.text == ""456\n"" def test_env(self, local_server_factory, example_dir): config = Config(environment_variables_file=Path(example_dir) / ""config"" / ""vars_dev.json"") local_server = local_server_factory(TechMS(configs=[config])) response = local_server.make_call(requests.get, '/env', timeout=500) assert response.status_code == 200 assert response.text == ""Simple microservice for test dev environment variable.\n"" def test_init(self, local_server_factory, example_dir): config = Config(environment_variables_file=Path(example_dir) / ""config"" / ""vars_dev.json"") local_server = local_server_factory(TechMS(configs=[config])) response = local_server.make_call(requests.get, '/init', timeout=500) assert response.status_code == 200 assert response.text == ""Initial value is test.\n"" def test_run_example(self, example_dir): app = import_attr('example', 'app', cwd=example_dir) port = ThreadedLocalServer.unused_tcp_port() server = threading.Thread(target=run_server_example, args=(app, port, example_dir), daemon=True) server.start() counter = 1 time.sleep(counter) while not server.is_alive() and counter < 3: time.sleep(counter) counter += 1 response = requests.get(f'http://localhost:{port}/') assert response.text == ""Simple microservice for test.\n"" response = requests.get(f'http://localhost:{port}/', params={""usage"": ""demo""}) assert response.status_code == 200 assert response.text == ""Simple microservice for demo.\n"" def run_server_example(app, port, example_dir): print(f""Server starting on port {port}"") app.execute('run', project_dir=example_dir, module='example', workspace='dev', host='localhost', port=port, ) ", https://github.com/gdoumenc/coworks,c574654a1e3d1cd4ad56b6f5912631230c37fdf3,tests/src/cws/test_scons.py::TestClass::test_zip,tests/src/cws/test_scons.py,NIO,flaky,Deleted,nan,classify;root_cause,"import os import sys from dataclasses import dataclass class Dir: def __init__(self, dir): self.dir = dir def get_abspath(self): return os.path.join(os.getcwd(), self.dir) @dataclass class Target: dir: Dir name: str class TestClass: def test_zip(self, monkeypatch, zipfile_mock, example_dir): # hack for strange installation of scons for path in [f""{p}/scons"" for p in sys.path if 'site-package' in p]: monkeypatch.syspath_prepend(path) from coworks.cws.layers import Layer env = { 'modules': [""chalice"", ""aws_xray_sdk""], 'source_dirs': [example_dir], 'exclude_dirs': [os.path.join(example_dir, 'config')], 'exclude_file_pattern': r""(Pipfile)|(.*\.tf)|(.*\.http)"", } layer = Layer('test', **env) layer.generate_zip_file([Target(Dir('test'), 'test')], None) zipfile_mock.assert_called_with(os.path.join(os.getcwd(), 'test', 'test'), mode='w') filename_args = ([c[1][1].as_posix() for c in zipfile_mock.file.write.mock_calls]) assert 'python/chalice/app.py' in filename_args assert 'python/quickstart2.py' in filename_args assert 'python/config/vars_dev.json' not in filename_args assert 'python/Pipfile' not in filename_args assert 'python/quickstart2.tf' not in filename_args assert 'python/request.http' not in filename_args ", https://github.com/giganticode/dataprep,bb1d3731a39d221f2a037aaadc747a29a5e76c8d,tests/infrastructure/test_bperegistry.py::test_none,tests/infrastructure/test_bperegistry.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"# SPDX-FileCopyrightText: 2020 Hlib Babii # # SPDX-License-Identifier: Apache-2.0 import os from unittest.mock import patch from codeprep.pipeline.bperegistry import get_max_merges, format_available_merge_list_ids, get_min_merges from codeprep.pipeline.dataset import create_new_id_from PATH_TO_DATASET_BPE_DIR_STUB = os.path.join('/', 'path', 'to', 'dataset', 'bpe', 'dir') PATH_TO_DATASET_STUB = os.path.join('/', 'path', 'to', 'dataset') HLIB_PATH = '/home/hlib/path' @patch(""codeprep.bpepkg.bpe_config.BpeConfig"", autospec=True) def test_with_predefined_id(bpe_config_mock): bpe_config_mock.to_suffix.return_value = '' assert create_new_id_from(PATH_TO_DATASET_STUB, bpe_config_mock, 'id23') == 'id23' @patch(""codeprep.bpepkg.bpe_config.BpeConfig"", autospec=True) @patch('codeprep.pipeline.bperegistry._get_all_custom_bpe_codes_and_max_merges', autospec=True, return_value={}) def test_no_existing_bpe_codes(mock, bpe_config_mock): bpe_config_mock.to_suffix.return_value = '' assert create_new_id_from(PATH_TO_DATASET_STUB, bpe_config_mock) == 'dataset' @patch(""codeprep.bpepkg.bpe_config.BpeConfig"", autospec=True) @patch('codeprep.pipeline.bperegistry._get_all_custom_bpe_codes_and_max_merges', autospec=True, return_value={'dataset': 10, 'dataset4': 20, 'dataset_3': 30}) def test_ids_for_same_dataset_exist(mock, bpe_config_mock): bpe_config_mock.to_suffix.return_value = '' assert create_new_id_from(PATH_TO_DATASET_STUB, bpe_config_mock) == 'dataset_4' @patch(""codeprep.bpepkg.bpe_config.BpeConfig"", autospec=True) def test_with_predefined_codes_id(bpe_config_mock): bpe_config_mock.to_suffix.return_value = """" assert create_new_id_from(HLIB_PATH, bpe_config_mock, 'my-id') == 'my-id' @patch(""codeprep.bpepkg.bpe_config.BpeConfig"", autospec=True) @patch('codeprep.pipeline.bperegistry._get_all_custom_bpe_codes_and_max_merges', autospec=True, return_value="""") def test_simple(mock, bpe_config_mock): # given bpe_config_mock.to_suffix.return_value = """" assert create_new_id_from(HLIB_PATH, bpe_config_mock) == 'path' @patch(""codeprep.bpepkg.bpe_config.BpeConfig"", autospec=True) @patch('codeprep.pipeline.bperegistry._get_all_custom_bpe_codes_and_max_merges', autospec=True, return_value={'path': 1000}) def test_same_path_exists(mock, bpe_config_mock): # given bpe_config_mock.to_suffix.return_value = """" assert create_new_id_from(HLIB_PATH, bpe_config_mock) == 'path_1' @patch(""codeprep.bpepkg.bpe_config.BpeConfig"", autospec=True) @patch('codeprep.pipeline.bperegistry._get_all_custom_bpe_codes_and_max_merges', autospec=True, return_value={'path': 1000, 'path_1': 2000}) def test_same_path_and_next_one_exist(mock, bpe_config_mock): # given bpe_config_mock.to_suffix.return_value = """" assert create_new_id_from(HLIB_PATH, bpe_config_mock) == 'path_2' @patch(""codeprep.bpepkg.bpe_config.BpeConfig"", autospec=True) @patch('codeprep.pipeline.bperegistry._get_all_custom_bpe_codes_and_max_merges', autospec=True, return_value={'path': 1000, 'path_28': 2000}) def test_same_path_and_one_more_exist(mock, bpe_config_mock): # given bpe_config_mock.to_suffix.return_value = """" assert create_new_id_from(HLIB_PATH, bpe_config_mock) == 'path_29' @patch('codeprep.pipeline.bperegistry.os.walk', return_value=iter([('', [], [])])) def test_none(mocked_walk): assert get_max_merges('.') is None @patch('codeprep.pipeline.bperegistry._get_all_custom_bpe_codes_and_max_merges', autospec=True, return_value={}) def test_no_available_merge_lists(bpe_registry_mock): assert format_available_merge_list_ids() == """" @patch('codeprep.pipeline.bperegistry._get_all_custom_bpe_codes_and_max_merges', autospec=True, return_value={""a"": 1000, ""b"": 500}) def test_simple(mock): assert format_available_merge_list_ids() == ""a-[1..1000]\nb-[1..500]\n"" @patch('codeprep.pipeline.bperegistry._get_all_bpe_merges_dirs', autospec=True, return_value=[]) def test_max_no_folders(mock): assert get_max_merges(PATH_TO_DATASET_BPE_DIR_STUB) is None @patch('codeprep.pipeline.bperegistry._get_all_bpe_merges_dirs', autospec=True, return_value=[]) def test_min_no_folders(mock): assert get_min_merges(PATH_TO_DATASET_BPE_DIR_STUB) is None @patch('codeprep.pipeline.bperegistry._get_all_bpe_merges_dirs', autospec=True, return_value=['part_vocab']) def test_max_with_non_number_folder(mock): assert get_max_merges(PATH_TO_DATASET_BPE_DIR_STUB) is None @patch('codeprep.pipeline.bperegistry._get_all_bpe_merges_dirs', autospec=True, return_value=['part_vocab']) def test_min_with_non_number_folder(mock): assert get_min_merges(PATH_TO_DATASET_BPE_DIR_STUB) is None @patch('codeprep.pipeline.bperegistry._get_all_bpe_merges_dirs', autospec=True, return_value=['10', '20']) def test_max_all_folders_above_limit(mock): assert get_max_merges(PATH_TO_DATASET_BPE_DIR_STUB, 5) is None @patch('codeprep.pipeline.bperegistry._get_all_bpe_merges_dirs', autospec=True, return_value=['10', '20']) def test_min_all_folders_below_limit(mock): assert get_min_merges(PATH_TO_DATASET_BPE_DIR_STUB) == 10 @patch('codeprep.pipeline.bperegistry._get_all_bpe_merges_dirs', autospec=True, return_value=['10', 'partvocab']) def test_max_one_folder_available(mock): assert get_max_merges(PATH_TO_DATASET_BPE_DIR_STUB) == 10 @patch('codeprep.pipeline.bperegistry._get_all_bpe_merges_dirs', autospec=True, return_value=['10', 'partvocab']) def test_min_one_folder_available(mock): assert get_max_merges(PATH_TO_DATASET_BPE_DIR_STUB) == 10 @patch('codeprep.pipeline.bperegistry._get_all_bpe_merges_dirs', autospec=True, return_value=['10', '20', '15', '30', 'partvocab']) def test_max_simple(mock): assert get_max_merges(PATH_TO_DATASET_BPE_DIR_STUB, 20) == 20 @patch('codeprep.pipeline.bperegistry._get_all_bpe_merges_dirs', autospec=True, return_value=['10', '20', '15', '30', 'partvocab']) def test_min_simple(mock): assert get_min_merges(PATH_TO_DATASET_BPE_DIR_STUB, 15) == 15", https://github.com/gjvnq/LabIFSC,cb56b75f51eb54680d0b81f4c87aa0234538a2a8,tests/medida_test.py::test_medida_si_2,tests/medida_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- import pytest from math import fabs from LabIFSC import M, Medida, unidades_em_texto def test_medida_eq_1(): m1 = Medida((0.0, 0.0)) m2 = Medida((0.0, 0.0)) assert m1 == m2 assert not m1 != m2 def test_medida_eq_2(): m1 = Medida((0.0, 0.0)) m2 = Medida((1.0, 0.0)) assert not m1 == m2 assert m1 != m2 def test_medida_eq_3(): m1 = Medida((0, 0.5)) m2 = Medida((1, 0.5)) assert m1 == m2 assert not m1 != m2 def test_medida_eq_4(): m1 = Medida((0, 0.5)) m2 = Medida((2.1, 0.5)) assert not m1 == m2 assert not m1 != m2 def test_medida_repr_1(): m = Medida(1, ""ft"") assert m.__repr__() == ""<1.0±0.0 ft = 0.3048±0.0 L1>"" def test_medida_add_1(): m1 = Medida(1, ""m"") m2 = Medida(2, ""m"") m = m1+m2 assert m.nominal == 3 assert m.incerteza == 0 def test_medida_add_2(): m1 = Medida(1, ""m"") m2 = Medida(""2+/-0.1"", ""m"") m = m1+m2 assert m.nominal == 3 assert m.incerteza == 0.1 def test_medida_add_3(): m1 = Medida(""1+-0.1"", ""m"") m2 = Medida(""1+-0.1"", ""ft"") m3 = m1+m2 m4 = m2+m1 assert m3.nominal == 1.3048 assert m3.incerteza == 0.13048 assert unidades_em_texto(m3.unidades_originais) == ""m"" assert m4.nominal == 4.2808398950131235 assert m4.incerteza == 0.4280839895013123 assert unidades_em_texto(m4.unidades_originais) == ""ft"" def test_medida_si_1(): m = Medida(""1+-0.1"", ""ft"").SI() assert m.nominal - 0.3048 < 1E-4 assert m.incerteza - 0.03048 < 1E-4 assert unidades_em_texto(m.unidades_originais) == ""m"" def test_medida_si_2(): m = Medida(""1+-0.1"", ""ft²"").SI() assert m.nominal - 0.092903 < 1E-4 assert m.incerteza - 0.0092903 < 1E-4 assert unidades_em_texto(m.unidades_originais) == ""m²"" def test_medida_si_3(): m = Medida(""1+-0.1"", ""ft² deg lb h °F A mol^-1"").SI() assert unidades_em_texto(m.unidades_originais) == ""m² rad kg s K A mol⁻¹"" def test_medida_sub_1(): m1 = Medida(1, ""m"") m2 = Medida(2, ""m"") m = m1-m2 assert m.nominal == -1 assert m.incerteza == 0 def test_medida_sub_2(): m1 = Medida(""1+/-0.1"", ""m"") m2 = Medida(""2+/-0.01"", ""m"") m = m1-m2 assert m.nominal == -1 assert m.incerteza == 0.11 def test_medida_mul_1(): m1 = Medida(""1+-0.1"", ""kg^2/m^3"") print(m1.dimensao) m2 = Medida(""1+-0.1"", ""lb/kg"") print(m2.dimensao) m3 = m1*m2 m4 = m2*m1 assert m3.nominal == 1 assert m3.incerteza == 0.2 assert unidades_em_texto(m3.unidades_originais) == ""kg lb m⁻³"" assert m4.nominal == 1 assert m4.incerteza == 0.2 assert unidades_em_texto(m4.unidades_originais) == ""kg lb m⁻³"" def test_medida_div_1(): m1 = Medida(""3"", ""kg/m"") m2 = Medida(""2"", ""L*Pa/m"") m3 = m1/m2 m4 = m2/m1 assert m3.nominal == 3.0/2.0 assert m3.incerteza == 0 assert unidades_em_texto(m3.unidades_originais) == ""kg L⁻¹ Pa⁻¹"" assert m4.nominal == 2.0/3.0 assert m4.incerteza == 0 assert unidades_em_texto(m4.unidades_originais) == ""L Pa kg⁻¹"" def test_medida_div_2(): m1 = Medida(""5+-2"", ""kg"") m2 = 0.5 m3 = m1/m2 m4 = m2/m1 assert m3.nominal == 10.0 assert m3.incerteza == 4.0 assert unidades_em_texto(m3.unidades_originais) == ""kg"" assert m4.nominal == 0.1 assert m4.incerteza == 0.04 assert unidades_em_texto(m4.unidades_originais) == ""kg⁻¹"" def test_medida_abs_1(): m1 = Medida(""-3+-1"", ""kg/m"") m2 = abs(m1) assert m2.nominal == 3 assert m2.incerteza == 1 assert unidades_em_texto(m2.unidades_originais) == ""kg m⁻¹"" def test_medida_divmod_1(): m1 = Medida(""11+-1"", ""kg/m"") m2, m3 = divmod(m1, 2) assert m2.nominal == 5 assert m2.incerteza == 0.25 assert unidades_em_texto(m2.unidades_originais) == ""kg m⁻¹"" assert m3.nominal == 1 assert m3.incerteza == 0.25 assert unidades_em_texto(m3.unidades_originais) == ""kg m⁻¹"" def test_medida_pow_1(): m = Medida(""4+/-0.1"", ""m^2"")**0.5 assert m.nominal == 2 assert m.incerteza == 0.025 assert unidades_em_texto(m.unidades_originais) == ""m"" def test_medida_pow_2(): m = Medida(""4+/-0.1"", ""m^2"")**2 assert m.nominal == 16 assert m.incerteza == 0.8 assert unidades_em_texto(m.unidades_originais) == ""m⁴"" def test_medida_pow_3(): m1 = Medida(""2+/-0.1"", ""m^2"") m2 = Medida(""3+/-0.5"", """") m3 = m1**m2 assert m3.nominal == 2**3 assert m3.incerteza == 3.9725887222397813 assert unidades_em_texto(m3.unidades_originais) == ""m⁶"" def test_medida_pow_4(): m1 = Medida(""2+/-0.1"", ""m^2"") m2 = Medida(""3.1+/-0.5"", """") m3 = m1**m2 assert m3.nominal == 2**3.1 assert m3.incerteza == 4.300586108569011 assert unidades_em_texto(m3.unidades_originais) == ""m²"" def test_medida_pow_5(): m1 = Medida(""-2+/-0.1"", ""m^2"") m2 = Medida(""-3+/-0.5"", """") m3 = m1**m2 assert m3.nominal == (-2)**-3 assert m3.incerteza == 0.06207169878499658 assert unidades_em_texto(m3.unidades_originais) == ""m⁻⁶"" def test_medida_array_1(): m = M([1,2,3,4,5], incerteza=0.5, unidade=""ft"") assert len(m) == 5 for i in range(len(m)): assert m[i].nominal == i + 1 assert m[i].incerteza == 0.5 assert m[i].si_nominal == 0.3048*(i + 1) assert m[i].si_incerteza == 0.1524 def test_medida_array_2(): m = Medida(""100+-10"", ""libra"") l = M([1,2,m,4,5], incerteza=0.5, unidade=""ft"") print(l) assert len(l) == 5 for i in range(len(l)): if i == 2: continue assert l[i].nominal == i + 1 assert l[i].incerteza == 0.5 assert l[i].si_nominal == 0.3048*(i + 1) assert l[i].si_incerteza == 0.1524 assert l[2].nominal == 100 assert l[2].incerteza == 10 assert fabs(l[2].si_nominal - 45.359237) < 0.001 assert fabs(l[2].si_incerteza - 4.5359237) < 0.001", https://github.com/gjvnq/LabIFSC,cb56b75f51eb54680d0b81f4c87aa0234538a2a8,tests/medida_test.py::test_medida_si_3,tests/medida_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- import pytest from math import fabs from LabIFSC import M, Medida, unidades_em_texto def test_medida_eq_1(): m1 = Medida((0.0, 0.0)) m2 = Medida((0.0, 0.0)) assert m1 == m2 assert not m1 != m2 def test_medida_eq_2(): m1 = Medida((0.0, 0.0)) m2 = Medida((1.0, 0.0)) assert not m1 == m2 assert m1 != m2 def test_medida_eq_3(): m1 = Medida((0, 0.5)) m2 = Medida((1, 0.5)) assert m1 == m2 assert not m1 != m2 def test_medida_eq_4(): m1 = Medida((0, 0.5)) m2 = Medida((2.1, 0.5)) assert not m1 == m2 assert not m1 != m2 def test_medida_repr_1(): m = Medida(1, ""ft"") assert m.__repr__() == ""<1.0±0.0 ft = 0.3048±0.0 L1>"" def test_medida_add_1(): m1 = Medida(1, ""m"") m2 = Medida(2, ""m"") m = m1+m2 assert m.nominal == 3 assert m.incerteza == 0 def test_medida_add_2(): m1 = Medida(1, ""m"") m2 = Medida(""2+/-0.1"", ""m"") m = m1+m2 assert m.nominal == 3 assert m.incerteza == 0.1 def test_medida_add_3(): m1 = Medida(""1+-0.1"", ""m"") m2 = Medida(""1+-0.1"", ""ft"") m3 = m1+m2 m4 = m2+m1 assert m3.nominal == 1.3048 assert m3.incerteza == 0.13048 assert unidades_em_texto(m3.unidades_originais) == ""m"" assert m4.nominal == 4.2808398950131235 assert m4.incerteza == 0.4280839895013123 assert unidades_em_texto(m4.unidades_originais) == ""ft"" def test_medida_si_1(): m = Medida(""1+-0.1"", ""ft"").SI() assert m.nominal - 0.3048 < 1E-4 assert m.incerteza - 0.03048 < 1E-4 assert unidades_em_texto(m.unidades_originais) == ""m"" def test_medida_si_2(): m = Medida(""1+-0.1"", ""ft²"").SI() assert m.nominal - 0.092903 < 1E-4 assert m.incerteza - 0.0092903 < 1E-4 assert unidades_em_texto(m.unidades_originais) == ""m²"" def test_medida_si_3(): m = Medida(""1+-0.1"", ""ft² deg lb h °F A mol^-1"").SI() assert unidades_em_texto(m.unidades_originais) == ""m² rad kg s K A mol⁻¹"" def test_medida_sub_1(): m1 = Medida(1, ""m"") m2 = Medida(2, ""m"") m = m1-m2 assert m.nominal == -1 assert m.incerteza == 0 def test_medida_sub_2(): m1 = Medida(""1+/-0.1"", ""m"") m2 = Medida(""2+/-0.01"", ""m"") m = m1-m2 assert m.nominal == -1 assert m.incerteza == 0.11 def test_medida_mul_1(): m1 = Medida(""1+-0.1"", ""kg^2/m^3"") print(m1.dimensao) m2 = Medida(""1+-0.1"", ""lb/kg"") print(m2.dimensao) m3 = m1*m2 m4 = m2*m1 assert m3.nominal == 1 assert m3.incerteza == 0.2 assert unidades_em_texto(m3.unidades_originais) == ""kg lb m⁻³"" assert m4.nominal == 1 assert m4.incerteza == 0.2 assert unidades_em_texto(m4.unidades_originais) == ""kg lb m⁻³"" def test_medida_div_1(): m1 = Medida(""3"", ""kg/m"") m2 = Medida(""2"", ""L*Pa/m"") m3 = m1/m2 m4 = m2/m1 assert m3.nominal == 3.0/2.0 assert m3.incerteza == 0 assert unidades_em_texto(m3.unidades_originais) == ""kg L⁻¹ Pa⁻¹"" assert m4.nominal == 2.0/3.0 assert m4.incerteza == 0 assert unidades_em_texto(m4.unidades_originais) == ""L Pa kg⁻¹"" def test_medida_div_2(): m1 = Medida(""5+-2"", ""kg"") m2 = 0.5 m3 = m1/m2 m4 = m2/m1 assert m3.nominal == 10.0 assert m3.incerteza == 4.0 assert unidades_em_texto(m3.unidades_originais) == ""kg"" assert m4.nominal == 0.1 assert m4.incerteza == 0.04 assert unidades_em_texto(m4.unidades_originais) == ""kg⁻¹"" def test_medida_abs_1(): m1 = Medida(""-3+-1"", ""kg/m"") m2 = abs(m1) assert m2.nominal == 3 assert m2.incerteza == 1 assert unidades_em_texto(m2.unidades_originais) == ""kg m⁻¹"" def test_medida_divmod_1(): m1 = Medida(""11+-1"", ""kg/m"") m2, m3 = divmod(m1, 2) assert m2.nominal == 5 assert m2.incerteza == 0.25 assert unidades_em_texto(m2.unidades_originais) == ""kg m⁻¹"" assert m3.nominal == 1 assert m3.incerteza == 0.25 assert unidades_em_texto(m3.unidades_originais) == ""kg m⁻¹"" def test_medida_pow_1(): m = Medida(""4+/-0.1"", ""m^2"")**0.5 assert m.nominal == 2 assert m.incerteza == 0.025 assert unidades_em_texto(m.unidades_originais) == ""m"" def test_medida_pow_2(): m = Medida(""4+/-0.1"", ""m^2"")**2 assert m.nominal == 16 assert m.incerteza == 0.8 assert unidades_em_texto(m.unidades_originais) == ""m⁴"" def test_medida_pow_3(): m1 = Medida(""2+/-0.1"", ""m^2"") m2 = Medida(""3+/-0.5"", """") m3 = m1**m2 assert m3.nominal == 2**3 assert m3.incerteza == 3.9725887222397813 assert unidades_em_texto(m3.unidades_originais) == ""m⁶"" def test_medida_pow_4(): m1 = Medida(""2+/-0.1"", ""m^2"") m2 = Medida(""3.1+/-0.5"", """") m3 = m1**m2 assert m3.nominal == 2**3.1 assert m3.incerteza == 4.300586108569011 assert unidades_em_texto(m3.unidades_originais) == ""m²"" def test_medida_pow_5(): m1 = Medida(""-2+/-0.1"", ""m^2"") m2 = Medida(""-3+/-0.5"", """") m3 = m1**m2 assert m3.nominal == (-2)**-3 assert m3.incerteza == 0.06207169878499658 assert unidades_em_texto(m3.unidades_originais) == ""m⁻⁶"" def test_medida_array_1(): m = M([1,2,3,4,5], incerteza=0.5, unidade=""ft"") assert len(m) == 5 for i in range(len(m)): assert m[i].nominal == i + 1 assert m[i].incerteza == 0.5 assert m[i].si_nominal == 0.3048*(i + 1) assert m[i].si_incerteza == 0.1524 def test_medida_array_2(): m = Medida(""100+-10"", ""libra"") l = M([1,2,m,4,5], incerteza=0.5, unidade=""ft"") print(l) assert len(l) == 5 for i in range(len(l)): if i == 2: continue assert l[i].nominal == i + 1 assert l[i].incerteza == 0.5 assert l[i].si_nominal == 0.3048*(i + 1) assert l[i].si_incerteza == 0.1524 assert l[2].nominal == 100 assert l[2].incerteza == 10 assert fabs(l[2].si_nominal - 45.359237) < 0.001 assert fabs(l[2].si_incerteza - 4.5359237) < 0.001", https://github.com/goodmami/penman,7770dfe14b3d0d197cedc6640f3ff7e3bd695726,tests/test_layout.py::test_rearrange,tests/test_layout.py,NOD,flaky,Accepted,https://github.com/goodmami/penman/pull/102,classify;root_cause;fix_proposal," import random import logging import pytest from penman.exceptions import LayoutError from penman.model import Model from penman.tree import Tree from penman.graph import Graph from penman.codec import PENMANCodec from penman import layout from penman.layout import ( interpret, rearrange, configure, reconfigure, get_pushed_variable, appears_inverted, node_contexts, ) random.seed(1) codec = PENMANCodec() model = Model() @pytest.fixture(scope='module') def amr_model(mini_amr): return Model.from_dict(mini_amr) def test_interpret(amr_model): t = codec.parse('(a / A)') assert interpret(t) == Graph([('a', ':instance', 'A')], top='a') t = codec.parse('(a / A :consist-of (b / B))') assert interpret(t) == Graph( [('a', ':instance', 'A'), ('b', ':consist', 'a'), ('b', ':instance', 'B')], top='a') assert interpret(t, model=amr_model) == Graph( [('a', ':instance', 'A'), ('a', ':consist-of', 'b'), ('b', ':instance', 'B')], top='a') def test_rearrange(): random.seed(1) t = codec.parse(''' (a / alpha :ARG0 (b / beta :ARG0 (g / gamma) :ARG1 (d / delta)) :ARG0-of d :ARG1 (e / epsilon))''') rearrange(t, model.original_order) assert codec.format(t) == ( '(a / alpha\n' ' :ARG0 (b / beta\n' ' :ARG0 (g / gamma)\n' ' :ARG1 (d / delta))\n' ' :ARG0-of d\n' ' :ARG1 (e / epsilon))') rearrange(t, model.random_order) assert codec.format(t) == ( '(a / alpha\n' ' :ARG0-of d\n' ' :ARG1 (e / epsilon)\n' ' :ARG0 (b / beta\n' ' :ARG0 (g / gamma)\n' ' :ARG1 (d / delta)))') rearrange(t, model.canonical_order) assert codec.format(t) == ( '(a / alpha\n' ' :ARG0 (b / beta\n' ' :ARG0 (g / gamma)\n' ' :ARG1 (d / delta))\n' ' :ARG1 (e / epsilon)\n' ' :ARG0-of d)') def test_configure(amr_model): g = codec.decode('(a / A)') assert configure(g) == Tree(('a', [('/', 'A')])) with pytest.raises(LayoutError): configure(g, top='A') g = codec.decode('(a / A :consist-of (b / B))') assert configure(g) == Tree( ('a', [('/', 'A'), (':consist-of', ('b', [('/', 'B')]))])) assert configure(g, top='b') == Tree( ('b', [('/', 'B'), (':consist', ('a', [('/', 'A')]))])) amr_codec = PENMANCodec(model=amr_model) g = amr_codec.decode('(a / A :consist-of (b / B))') assert configure(g, model=amr_model) == Tree( ('a', [('/', 'A'), (':consist-of', ('b', [('/', 'B')]))])) assert configure(g, top='b', model=amr_model) == Tree( ('b', [('/', 'B'), (':consist-of-of', ('a', [('/', 'A')]))])) def test_issue_34(): # https://github.com/goodmami/penman/issues/34 g = codec.decode(''' # ::snt I think you failed to not not act. (t / think :ARG0 (i / i) :ARG1 (f / fail :ARG0 (y / you) :ARG1 (a / act :polarity - :polarity -)))''') assert configure(g) == Tree( ('t', [('/', 'think'), (':ARG0', ('i', [('/', 'i')])), (':ARG1', ('f', [('/', 'fail'), (':ARG0', ('y', [('/', 'you')])), (':ARG1', ('a', [('/', 'act'), (':polarity', '-'), (':polarity', '-')]))]))])) def test_issue_85(monkeypatch, caplog): # https://github.com/goodmami/penman/issues/85 # Emulate multiprocessing by reassigning POP with monkeypatch.context() as m: m.setattr(layout, 'POP', layout.Pop()) g = codec.decode('(a / alpha :ARG0 (b / beta))') caplog.set_level(logging.WARNING) codec.encode(g, indent=None) assert 'epigraphical marker ignored: POP' not in caplog.text def test_reconfigure(): g = codec.decode(''' (a / alpha :ARG0 b :ARG1 (g / gamma :ARG0-of (b / beta)))''') # original order reconfiguration puts node definitions at first # appearance of a variable assert reconfigure(g) == Tree( ('a', [('/', 'alpha'), (':ARG0', ('b', [('/', 'beta')])), (':ARG1', ('g', [('/', 'gamma'), (':ARG0-of', 'b')]))])) # canonical order reconfiguration can also shift things like # inverted arguments assert reconfigure(g, key=model.canonical_order) == Tree( ('a', [('/', 'alpha'), (':ARG0', ('b', [('/', 'beta'), (':ARG0', ('g', [('/', 'gamma')]))])), (':ARG1', 'g')])) def test_issue_90(): # https://github.com/goodmami/penman/issues/90 g = Graph([('i', ':instance', 'iota'), ('i2', ':instance', 'i'), ('i', ':ARG0', 'i2')], top='i') assert reconfigure(g) == Tree( ('i', [('/', 'iota'), (':ARG0', ('i2', [('/', 'i')]))])) def test_get_pushed_variable(): g = codec.decode(''' (a / alpha :ARG0 (b / beta) :ARG1-of (g / gamma))''') assert get_pushed_variable(g, ('a', ':instance', 'alpha')) is None assert get_pushed_variable(g, ('a', ':ARG0', 'b')) == 'b' assert get_pushed_variable(g, ('g', ':ARG1', 'a')) == 'g' def test_appears_inverted(): g = codec.decode(''' (a / alpha :ARG0 (b / beta) :ARG1-of (g / gamma))''') assert not appears_inverted(g, ('a', ':instance', 'alpha')) assert not appears_inverted(g, ('a', ':ARG0', 'b')) assert appears_inverted(g, ('g', ':ARG1', 'a')) def test_issue_47(): # https://github.com/goodmami/penman/issues/47 g = codec.decode(''' (a / alpha :ARG0 (b / beta) :ARG1 (g / gamma :ARG0 (d / delta) :ARG1-of (e / epsilon) :ARG1-of b))''') assert not appears_inverted(g, ('a', ':ARG0', 'b')) assert not appears_inverted(g, ('g', ':ARG0', 'd')) assert appears_inverted(g, ('e', ':ARG1', 'g')) assert appears_inverted(g, ('b', ':ARG1', 'g')) def test_issue_87(): # https://github.com/goodmami/penman/issues/87 # The duplicate triple (i, :ARG0, c) below means the graph is bad # so the output is not guaranteed. Just check for errors. g = codec.decode('(c / company :ARG0-of (i / insure-02 :ARG0 c))') appears_inverted(g, ('i', ':ARG0', 'c')) codec.encode(g) g = codec.decode('(c / company :ARG0-of i :ARG0-of (i / insure-02))') appears_inverted(g, ('i', ':ARG0', 'c')) codec.encode(g) def test_node_contexts(): g = codec.decode('(a / alpha)') assert node_contexts(g) == ['a'] # note here and below: the first 'a' is for ('a', ':instance', None) g = codec.decode('(a :ARG0 (b / beta))') assert node_contexts(g) == ['a', 'a', 'b'] g = codec.decode('(a :ARG0-of (b / beta))') assert node_contexts(g) == ['a', 'a', 'b'] # also ('b', ':instance', None) here g = codec.decode('(a :ARG0 (b) :ARG1 (g / gamma))') assert node_contexts(g) == ['a', 'a', 'b', 'a', 'g'] def test_issue_92(): # https://github.com/goodmami/penman/issues/92 g = codec.decode('(a / alpha :ARG0~e.0 (b / beta))') assert configure(g) == Tree( ('a', [('/', 'alpha'), (':ARG0~e.0', ('b', [('/', 'beta')]))])) assert configure(g, top='b') == Tree( ('b', [('/', 'beta'), (':ARG0-of~e.0', ('a', [('/', 'alpha')]))])) def test_issue_93(): # https://github.com/goodmami/penman/issues/93 g = codec.decode('(a / alpha :ARG0 b~1)') g.triples.append(('b', ':instance', 'beta')) assert configure(g) == Tree( ('a', [('/', 'alpha'), (':ARG0', ('b', [('/', 'beta')]))])) ", https://github.com/goodmami/penman,e83cf6d006724d72a3e19a955aad94412da912b7,tests/test_layout.py::test_rearrange,tests/test_layout.py,NIO,flaky,nan,nan,classify;root_cause," import random import logging import pytest from penman.exceptions import LayoutError from penman.model import Model from penman.tree import Tree from penman.graph import Graph from penman.codec import PENMANCodec from penman import layout from penman.layout import ( interpret, rearrange, configure, reconfigure, get_pushed_variable, appears_inverted, node_contexts, ) random.seed(1) codec = PENMANCodec() model = Model() @pytest.fixture(scope='module') def amr_model(mini_amr): return Model.from_dict(mini_amr) def test_interpret(amr_model): t = codec.parse('(a / A)') assert interpret(t) == Graph([('a', ':instance', 'A')], top='a') t = codec.parse('(a / A :consist-of (b / B))') assert interpret(t) == Graph( [('a', ':instance', 'A'), ('b', ':consist', 'a'), ('b', ':instance', 'B')], top='a') assert interpret(t, model=amr_model) == Graph( [('a', ':instance', 'A'), ('a', ':consist-of', 'b'), ('b', ':instance', 'B')], top='a') def test_rearrange(): t = codec.parse(''' (a / alpha :ARG0 (b / beta :ARG0 (g / gamma) :ARG1 (d / delta)) :ARG0-of d :ARG1 (e / epsilon))''') rearrange(t, model.original_order) assert codec.format(t) == ( '(a / alpha\n' ' :ARG0 (b / beta\n' ' :ARG0 (g / gamma)\n' ' :ARG1 (d / delta))\n' ' :ARG0-of d\n' ' :ARG1 (e / epsilon))') rearrange(t, model.random_order) assert codec.format(t) == ( '(a / alpha\n' ' :ARG0-of d\n' ' :ARG1 (e / epsilon)\n' ' :ARG0 (b / beta\n' ' :ARG0 (g / gamma)\n' ' :ARG1 (d / delta)))') rearrange(t, model.canonical_order) assert codec.format(t) == ( '(a / alpha\n' ' :ARG0 (b / beta\n' ' :ARG0 (g / gamma)\n' ' :ARG1 (d / delta))\n' ' :ARG1 (e / epsilon)\n' ' :ARG0-of d)') def test_configure(amr_model): g = codec.decode('(a / A)') assert configure(g) == Tree(('a', [('/', 'A')])) with pytest.raises(LayoutError): configure(g, top='A') g = codec.decode('(a / A :consist-of (b / B))') assert configure(g) == Tree( ('a', [('/', 'A'), (':consist-of', ('b', [('/', 'B')]))])) assert configure(g, top='b') == Tree( ('b', [('/', 'B'), (':consist', ('a', [('/', 'A')]))])) amr_codec = PENMANCodec(model=amr_model) g = amr_codec.decode('(a / A :consist-of (b / B))') assert configure(g, model=amr_model) == Tree( ('a', [('/', 'A'), (':consist-of', ('b', [('/', 'B')]))])) assert configure(g, top='b', model=amr_model) == Tree( ('b', [('/', 'B'), (':consist-of-of', ('a', [('/', 'A')]))])) def test_issue_34(): # https://github.com/goodmami/penman/issues/34 g = codec.decode(''' # ::snt I think you failed to not not act. (t / think :ARG0 (i / i) :ARG1 (f / fail :ARG0 (y / you) :ARG1 (a / act :polarity - :polarity -)))''') assert configure(g) == Tree( ('t', [('/', 'think'), (':ARG0', ('i', [('/', 'i')])), (':ARG1', ('f', [('/', 'fail'), (':ARG0', ('y', [('/', 'you')])), (':ARG1', ('a', [('/', 'act'), (':polarity', '-'), (':polarity', '-')]))]))])) def test_issue_85(monkeypatch, caplog): # https://github.com/goodmami/penman/issues/85 # Emulate multiprocessing by reassigning POP with monkeypatch.context() as m: m.setattr(layout, 'POP', layout.Pop()) g = codec.decode('(a / alpha :ARG0 (b / beta))') caplog.set_level(logging.WARNING) codec.encode(g, indent=None) assert 'epigraphical marker ignored: POP' not in caplog.text def test_reconfigure(): g = codec.decode(''' (a / alpha :ARG0 b :ARG1 (g / gamma :ARG0-of (b / beta)))''') # original order reconfiguration puts node definitions at first # appearance of a variable assert reconfigure(g) == Tree( ('a', [('/', 'alpha'), (':ARG0', ('b', [('/', 'beta')])), (':ARG1', ('g', [('/', 'gamma'), (':ARG0-of', 'b')]))])) # canonical order reconfiguration can also shift things like # inverted arguments assert reconfigure(g, key=model.canonical_order) == Tree( ('a', [('/', 'alpha'), (':ARG0', ('b', [('/', 'beta'), (':ARG0', ('g', [('/', 'gamma')]))])), (':ARG1', 'g')])) def test_get_pushed_variable(): g = codec.decode(''' (a / alpha :ARG0 (b / beta) :ARG1-of (g / gamma))''') assert get_pushed_variable(g, ('a', ':instance', 'alpha')) is None assert get_pushed_variable(g, ('a', ':ARG0', 'b')) == 'b' assert get_pushed_variable(g, ('g', ':ARG1', 'a')) == 'g' def test_appears_inverted(): g = codec.decode(''' (a / alpha :ARG0 (b / beta) :ARG1-of (g / gamma))''') assert not appears_inverted(g, ('a', ':instance', 'alpha')) assert not appears_inverted(g, ('a', ':ARG0', 'b')) assert appears_inverted(g, ('g', ':ARG1', 'a')) def test_issue_47(): # https://github.com/goodmami/penman/issues/47 g = codec.decode(''' (a / alpha :ARG0 (b / beta) :ARG1 (g / gamma :ARG0 (d / delta) :ARG1-of (e / epsilon) :ARG1-of b))''') assert not appears_inverted(g, ('a', ':ARG0', 'b')) assert not appears_inverted(g, ('g', ':ARG0', 'd')) assert appears_inverted(g, ('e', ':ARG1', 'g')) assert appears_inverted(g, ('b', ':ARG1', 'g')) def test_node_contexts(): g = codec.decode('(a / alpha)') assert node_contexts(g) == ['a'] # note here and below: the first 'a' is for ('a', ':instance', None) g = codec.decode('(a :ARG0 (b / beta))') assert node_contexts(g) == ['a', 'a', 'b'] g = codec.decode('(a :ARG0-of (b / beta))') assert node_contexts(g) == ['a', 'a', 'b'] # also ('b', ':instance', None) here g = codec.decode('(a :ARG0 (b) :ARG1 (g / gamma))') assert node_contexts(g) == ['a', 'a', 'b', 'a', 'g'] ", https://github.com/googkit/googkit,cacb37bf65e5ac19379b329beb02af907240aa60,test/commands/test_candidates.py::TestCandidatesCommand::test_run_internal,test/commands/test_candidates.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import unittest from test.stub_config import * from test.stub_environment import StubEnvironment from test.stub_stdout import StubStdout from googkit.commands.candidates import CandidatesCommand from googkit.commands.command import Command from googkit.compat.unittest import mock from googkit.lib.argument import ArgumentParser from googkit.lib.command_tree import CommandTree class TestCandidatesCommand(unittest.TestCase): def setUp(self): self.env = StubEnvironment() self.env.config = StubConfig() self.env.tree = CommandTree() self.cmd = CandidatesCommand(self.env) def test_init(self): self.assertTrue(isinstance(self.cmd, Command)) def _arg(self, arg_text): return ArgumentParser.parse(arg_text.split(' ')) def test_run(self): self.cmd._validate_options = mock.MagicMock() self.cmd.run_internal = mock.MagicMock() self.cmd.run() self.assertFalse(self.cmd._validate_options.called) self.assertTrue(self.cmd.run_internal.called) def test_run_internal(self): self.env.argument = self._arg('googkit.py _candidates deps') with mock.patch('sys.stdout', new_callable=StubStdout) as mock_stdout: self.cmd.run_internal() candidates = mock_stdout.getvalue().split('\n') self.assertFalse('deps' in candidates) self.assertTrue('update' in candidates) self.assertFalse('--verbose' in candidates) self.env.argument = self._arg('googkit.py _candidates deps update') with mock.patch('sys.stdout', new_callable=StubStdout) as mock_stdout: self.cmd.run_internal() candidates = mock_stdout.getvalue().split('\n') self.assertFalse('deps' in candidates) self.assertFalse('update' in candidates) self.assertTrue('--verbose' in candidates) self.env.argument = self._arg('googkit.py _candidates deps update --verbose') with mock.patch('sys.stdout', new_callable=StubStdout) as mock_stdout: self.cmd.run_internal() candidates = mock_stdout.getvalue().split('\n') self.assertFalse('deps' in candidates) self.assertFalse('update' in candidates) self.assertFalse('--verbose' in candidates) ", https://github.com/grabbles/grabbit,83ff93df36019eaaee9d4e31f816a518e46cae07,grabbit/tests/test_core.py::TestLayout::test_init_with_exclude_arg[local],grabbit/tests/test_core.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from grabbit import File, Entity, Layout, Tag, merge_layouts import os from os.path import join import posixpath as psp import tempfile import json from copy import copy DIRNAME = os.path.dirname(__file__) @pytest.fixture def file(tmpdir): testfile = 'sub-03_ses-2_task-rest_acq-fullbrain_run-2_bold.nii.gz' fn = tmpdir.mkdir(""tmp"").join(testfile) fn.write('###') return File(join(str(fn))) @pytest.fixture(scope='module', params=['local', 'hdfs']) def bids_layout(request): if request.param == 'local': root = join(DIRNAME, 'data', '7t_trt') # note about test.json: # in this test.json 'subject' regex was left to contain possible # leading 0; the other fields (run, session) has leading 0 stripped config = join(DIRNAME, 'specs', 'test.json') return Layout([(root, config)], regex_search=True) else: hdfs = pytest.importorskip(""hdfs"") from grabbit.extensions import HDFSLayout client = hdfs.Config().get_client() root = psp.join('hdfs://localhost:9000{0}'.format( client.root), 'data', '7t_trt') config = psp.join('hdfs://localhost:9000{0}'.format( client.root), 'specs', 'test.json') return HDFSLayout([(root, config)], regex_search=True) @pytest.fixture(scope='module') def stamp_layout(): root = join(DIRNAME, 'data', 'valuable_stamps') config = join(DIRNAME, 'specs', 'stamps.json') return Layout([(root, config)], config_filename='dir_config.json') @pytest.fixture(scope='module') def layout_include(request): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test_include.json') return Layout([(root, config)], regex_search=True) class TestFile: def test_init(self, tmpdir): fn = tmpdir.mkdir(""tmp"").join('tmp.txt') fn.write('###') f = File(str(fn)) assert os.path.exists(f.path) assert f.filename is not None assert os.path.isdir(f.dirname) assert f.entities == {} def test_matches(self, file): file = copy(file) assert file._matches() assert file._matches(extensions='nii.gz') assert not file._matches(extensions=['.txt', '.rtf']) file.tags = {'task': Tag(None, 'rest'), 'run': Tag(None, '2')} assert file._matches(entities={'task': 'rest', 'run': 2}) assert not file._matches(entities={'task': 'rest', 'run': 4}) assert not file._matches(entities={'task': 'st'}) assert file._matches(entities={'task': 'st'}, regex_search=True) # With list of matches assert not file._matches(entities={'task': ['A', 'B', 'C']}) assert file._matches(entities={'task': ['A', 'B', 'rest']}) assert file._matches(entities={'task': ['A', 'B', 'st']}, regex_search=True) def test_named_tuple(self, file): file = copy(file) file.tags = {'attrA': Tag(None, 'apple'), 'attrB': Tag(None, 'banana')} tup = file.as_named_tuple() assert(tup.filename == file.path) assert isinstance(tup, tuple) assert not hasattr(tup, 'task') assert tup.attrA == 'apple' def test_named_tuple_with_reserved_name(self, file): file = copy(file) file.tags['class'] = Tag(None, 'invalid') with pytest.warns(UserWarning) as w: res = file.as_named_tuple() assert w[0].message.args[0].startswith('Entity names cannot') assert hasattr(res, 'class_') assert not hasattr(res, 'class') class TestEntity: def test_init(self): e = Entity('avaricious', 'aardvark-(\d+)') assert e.name == 'avaricious' assert e.pattern == 'aardvark-(\d+)' assert not e.mandatory assert e.directory is None assert e.files == {} def test_matches(self, tmpdir): filename = ""aardvark-4-reporting-for-duty.txt"" tmpdir.mkdir(""tmp"").join(filename).write(""###"") f = File(join(str(tmpdir), filename)) e = Entity('avaricious', 'aardvark-(\d+)') result = e.match_file(f) assert result == '4' def test_unique_and_count(self): e = Entity('prop', '-(\d+)') e.files = { 'test1-10.txt': '10', 'test2-7.txt': '7', 'test3-7.txt': '7' } assert sorted(e.unique()) == ['10', '7'] assert e.count() == 2 assert e.count(files=True) == 3 def test_add_file(self): e = Entity('prop', '-(\d+)') e.add_file('a', '1') assert e.files['a'] == '1' class TestLayout: def test_init(self, bids_layout): assert isinstance(bids_layout.files, dict) assert isinstance(bids_layout.entities, dict) assert isinstance(bids_layout.mandatory, set) assert not bids_layout.dynamic_getters def test_init_with_include_arg(self, bids_layout): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], regex_search=True, include='sub-\d*') target = join(root, ""dataset_description.json"") assert target in bids_layout.files assert target not in layout.files assert join(root, ""sub-01"", ""sub-01_sessions.tsv"") in layout.files with pytest.raises(ValueError): layout = Layout([(root, config)], include='sub-\d*', exclude=""meh"") def test_init_with_exclude_arg(self, bids_layout): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], regex_search=True, exclude='sub-\d*') target = join(root, ""dataset_description.json"") assert target in bids_layout.files assert target in layout.files sub_file = join(root, ""sub-01"", ""sub-01_sessions.tsv"") assert sub_file in bids_layout.files assert sub_file not in layout.files def test_init_with_config_options(self): root = join(DIRNAME, 'data') dir1 = join(root, 'valuable_stamps') dir2 = join(root, 'ordinary_stamps') config1 = join(DIRNAME, 'specs', 'stamps.json') config2 = join(dir1, 'USA', 'dir_config.json') # # Fails because Domain usa_stamps is included twice # with pytest.raises(ValueError) as e: # layout = Layout([(root, [config1, config2])], exclude=['7t_trt'], # config_filename='dir_config.json') # print(dir(e)) # assert e.value.message.startswith('Config with name') # Test with two configs layout = Layout([(root, [config1, config2])], exclude=['7t_trt']) files = [f.filename for f in layout.files.values()] assert 'name=Inverted_Jenny#value=75000#country=USA.txt' in files assert 'name=5c_Francis_E_Willard#value=1dollar.txt' in files assert 'name=1_Lotus#value=1#country=Canada.txt' in files # Test with two configs and on-the-fly directory remapping layout = Layout([dir1, ([dir1, dir2], config1)], exclude=['USA/']) files = [f.filename for f in layout.files.values()] assert 'name=Inverted_Jenny#value=75000#country=USA.txt' in files assert 'name=5c_Francis_E_Willard#value=1dollar.txt' not in files assert 'name=1_Lotus#value=1#country=Canada.txt' in files def test_absolute_paths(self, bids_layout): if not hasattr(bids_layout, '_hdfs_client'): root = join(DIRNAME, 'data', '7t_trt') root = os.path.relpath(root) config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], absolute_paths=True) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) layout = Layout([(root, config)], absolute_paths=False) result = layout.get(subject=1, run=1, session=1) assert result assert not any([os.path.isabs(f.filename) for f in result]) # Should always be absolute paths on HDFS else: root = psp.join('hdfs://localhost:9000{0}'.format( layout._hdfs_client.root), 'data', '7t_trt') config = psp.join('hdfs://localhost:9000{0}'.format( layout._hdfs_client.root), 'specs', 'test.json') layout = Layout([(root, config)], absolute_paths=False) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) layout = Layout([(root, config)], absolute_paths=True) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) @pytest.mark.parametrize('data_dir, config', [(join(DIRNAME, 'data', '7t_trt'), join(DIRNAME, 'specs', 'test.json')), (psp.join('hdfs://localhost:9000/grabbit/test/', 'data', '7t_trt'), psp.join('hdfs://localhost:9000/grabbit/test/', 'specs', 'test.json'))]) def test_dynamic_getters(self, data_dir, config): if ('hdfs' in data_dir or 'hdfs' in config): pytest.importorskip('hdfs') layout = Layout([(data_dir, config)], dynamic_getters=True) assert hasattr(layout, 'get_subjects') assert '01' in getattr(layout, 'get_subjects')() assert 1 in getattr(layout, 'get_runs')() def test_querying(self, bids_layout): # With regex_search = True (as set in Layout()) result = bids_layout.get(subject=1, run=1, session=1, extensions='nii.gz') assert len(result) == 8 result = bids_l", https://github.com/grabbles/grabbit,83ff93df36019eaaee9d4e31f816a518e46cae07,grabbit/tests/test_core.py::TestLayout::test_init_with_include_arg[local],grabbit/tests/test_core.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from grabbit import File, Entity, Layout, Tag, merge_layouts import os from os.path import join import posixpath as psp import tempfile import json from copy import copy DIRNAME = os.path.dirname(__file__) @pytest.fixture def file(tmpdir): testfile = 'sub-03_ses-2_task-rest_acq-fullbrain_run-2_bold.nii.gz' fn = tmpdir.mkdir(""tmp"").join(testfile) fn.write('###') return File(join(str(fn))) @pytest.fixture(scope='module', params=['local', 'hdfs']) def bids_layout(request): if request.param == 'local': root = join(DIRNAME, 'data', '7t_trt') # note about test.json: # in this test.json 'subject' regex was left to contain possible # leading 0; the other fields (run, session) has leading 0 stripped config = join(DIRNAME, 'specs', 'test.json') return Layout([(root, config)], regex_search=True) else: hdfs = pytest.importorskip(""hdfs"") from grabbit.extensions import HDFSLayout client = hdfs.Config().get_client() root = psp.join('hdfs://localhost:9000{0}'.format( client.root), 'data', '7t_trt') config = psp.join('hdfs://localhost:9000{0}'.format( client.root), 'specs', 'test.json') return HDFSLayout([(root, config)], regex_search=True) @pytest.fixture(scope='module') def stamp_layout(): root = join(DIRNAME, 'data', 'valuable_stamps') config = join(DIRNAME, 'specs', 'stamps.json') return Layout([(root, config)], config_filename='dir_config.json') @pytest.fixture(scope='module') def layout_include(request): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test_include.json') return Layout([(root, config)], regex_search=True) class TestFile: def test_init(self, tmpdir): fn = tmpdir.mkdir(""tmp"").join('tmp.txt') fn.write('###') f = File(str(fn)) assert os.path.exists(f.path) assert f.filename is not None assert os.path.isdir(f.dirname) assert f.entities == {} def test_matches(self, file): file = copy(file) assert file._matches() assert file._matches(extensions='nii.gz') assert not file._matches(extensions=['.txt', '.rtf']) file.tags = {'task': Tag(None, 'rest'), 'run': Tag(None, '2')} assert file._matches(entities={'task': 'rest', 'run': 2}) assert not file._matches(entities={'task': 'rest', 'run': 4}) assert not file._matches(entities={'task': 'st'}) assert file._matches(entities={'task': 'st'}, regex_search=True) # With list of matches assert not file._matches(entities={'task': ['A', 'B', 'C']}) assert file._matches(entities={'task': ['A', 'B', 'rest']}) assert file._matches(entities={'task': ['A', 'B', 'st']}, regex_search=True) def test_named_tuple(self, file): file = copy(file) file.tags = {'attrA': Tag(None, 'apple'), 'attrB': Tag(None, 'banana')} tup = file.as_named_tuple() assert(tup.filename == file.path) assert isinstance(tup, tuple) assert not hasattr(tup, 'task') assert tup.attrA == 'apple' def test_named_tuple_with_reserved_name(self, file): file = copy(file) file.tags['class'] = Tag(None, 'invalid') with pytest.warns(UserWarning) as w: res = file.as_named_tuple() assert w[0].message.args[0].startswith('Entity names cannot') assert hasattr(res, 'class_') assert not hasattr(res, 'class') class TestEntity: def test_init(self): e = Entity('avaricious', 'aardvark-(\d+)') assert e.name == 'avaricious' assert e.pattern == 'aardvark-(\d+)' assert not e.mandatory assert e.directory is None assert e.files == {} def test_matches(self, tmpdir): filename = ""aardvark-4-reporting-for-duty.txt"" tmpdir.mkdir(""tmp"").join(filename).write(""###"") f = File(join(str(tmpdir), filename)) e = Entity('avaricious', 'aardvark-(\d+)') result = e.match_file(f) assert result == '4' def test_unique_and_count(self): e = Entity('prop', '-(\d+)') e.files = { 'test1-10.txt': '10', 'test2-7.txt': '7', 'test3-7.txt': '7' } assert sorted(e.unique()) == ['10', '7'] assert e.count() == 2 assert e.count(files=True) == 3 def test_add_file(self): e = Entity('prop', '-(\d+)') e.add_file('a', '1') assert e.files['a'] == '1' class TestLayout: def test_init(self, bids_layout): assert isinstance(bids_layout.files, dict) assert isinstance(bids_layout.entities, dict) assert isinstance(bids_layout.mandatory, set) assert not bids_layout.dynamic_getters def test_init_with_include_arg(self, bids_layout): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], regex_search=True, include='sub-\d*') target = join(root, ""dataset_description.json"") assert target in bids_layout.files assert target not in layout.files assert join(root, ""sub-01"", ""sub-01_sessions.tsv"") in layout.files with pytest.raises(ValueError): layout = Layout([(root, config)], include='sub-\d*', exclude=""meh"") def test_init_with_exclude_arg(self, bids_layout): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], regex_search=True, exclude='sub-\d*') target = join(root, ""dataset_description.json"") assert target in bids_layout.files assert target in layout.files sub_file = join(root, ""sub-01"", ""sub-01_sessions.tsv"") assert sub_file in bids_layout.files assert sub_file not in layout.files def test_init_with_config_options(self): root = join(DIRNAME, 'data') dir1 = join(root, 'valuable_stamps') dir2 = join(root, 'ordinary_stamps') config1 = join(DIRNAME, 'specs', 'stamps.json') config2 = join(dir1, 'USA', 'dir_config.json') # # Fails because Domain usa_stamps is included twice # with pytest.raises(ValueError) as e: # layout = Layout([(root, [config1, config2])], exclude=['7t_trt'], # config_filename='dir_config.json') # print(dir(e)) # assert e.value.message.startswith('Config with name') # Test with two configs layout = Layout([(root, [config1, config2])], exclude=['7t_trt']) files = [f.filename for f in layout.files.values()] assert 'name=Inverted_Jenny#value=75000#country=USA.txt' in files assert 'name=5c_Francis_E_Willard#value=1dollar.txt' in files assert 'name=1_Lotus#value=1#country=Canada.txt' in files # Test with two configs and on-the-fly directory remapping layout = Layout([dir1, ([dir1, dir2], config1)], exclude=['USA/']) files = [f.filename for f in layout.files.values()] assert 'name=Inverted_Jenny#value=75000#country=USA.txt' in files assert 'name=5c_Francis_E_Willard#value=1dollar.txt' not in files assert 'name=1_Lotus#value=1#country=Canada.txt' in files def test_absolute_paths(self, bids_layout): if not hasattr(bids_layout, '_hdfs_client'): root = join(DIRNAME, 'data', '7t_trt') root = os.path.relpath(root) config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], absolute_paths=True) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) layout = Layout([(root, config)], absolute_paths=False) result = layout.get(subject=1, run=1, session=1) assert result assert not any([os.path.isabs(f.filename) for f in result]) # Should always be absolute paths on HDFS else: root = psp.join('hdfs://localhost:9000{0}'.format( layout._hdfs_client.root), 'data', '7t_trt') config = psp.join('hdfs://localhost:9000{0}'.format( layout._hdfs_client.root), 'specs', 'test.json') layout = Layout([(root, config)], absolute_paths=False) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) layout = Layout([(root, config)], absolute_paths=True) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) @pytest.mark.parametrize('data_dir, config', [(join(DIRNAME, 'data', '7t_trt'), join(DIRNAME, 'specs', 'test.json')), (psp.join('hdfs://localhost:9000/grabbit/test/', 'data', '7t_trt'), psp.join('hdfs://localhost:9000/grabbit/test/', 'specs', 'test.json'))]) def test_dynamic_getters(self, data_dir, config): if ('hdfs' in data_dir or 'hdfs' in config): pytest.importorskip('hdfs') layout = Layout([(data_dir, config)], dynamic_getters=True) assert hasattr(layout, 'get_subjects') assert '01' in getattr(layout, 'get_subjects')() assert 1 in getattr(layout, 'get_runs')() def test_querying(self, bids_layout): # With regex_search = True (as set in Layout()) result = bids_layout.get(subject=1, run=1, session=1, extensions='nii.gz') assert len(result) == 8 result = bids_l", https://github.com/grabbles/grabbit,83ff93df36019eaaee9d4e31f816a518e46cae07,grabbit/tests/test_core.py::TestLayout::test_natsort[local],grabbit/tests/test_core.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from grabbit import File, Entity, Layout, Tag, merge_layouts import os from os.path import join import posixpath as psp import tempfile import json from copy import copy DIRNAME = os.path.dirname(__file__) @pytest.fixture def file(tmpdir): testfile = 'sub-03_ses-2_task-rest_acq-fullbrain_run-2_bold.nii.gz' fn = tmpdir.mkdir(""tmp"").join(testfile) fn.write('###') return File(join(str(fn))) @pytest.fixture(scope='module', params=['local', 'hdfs']) def bids_layout(request): if request.param == 'local': root = join(DIRNAME, 'data', '7t_trt') # note about test.json: # in this test.json 'subject' regex was left to contain possible # leading 0; the other fields (run, session) has leading 0 stripped config = join(DIRNAME, 'specs', 'test.json') return Layout([(root, config)], regex_search=True) else: hdfs = pytest.importorskip(""hdfs"") from grabbit.extensions import HDFSLayout client = hdfs.Config().get_client() root = psp.join('hdfs://localhost:9000{0}'.format( client.root), 'data', '7t_trt') config = psp.join('hdfs://localhost:9000{0}'.format( client.root), 'specs', 'test.json') return HDFSLayout([(root, config)], regex_search=True) @pytest.fixture(scope='module') def stamp_layout(): root = join(DIRNAME, 'data', 'valuable_stamps') config = join(DIRNAME, 'specs', 'stamps.json') return Layout([(root, config)], config_filename='dir_config.json') @pytest.fixture(scope='module') def layout_include(request): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test_include.json') return Layout([(root, config)], regex_search=True) class TestFile: def test_init(self, tmpdir): fn = tmpdir.mkdir(""tmp"").join('tmp.txt') fn.write('###') f = File(str(fn)) assert os.path.exists(f.path) assert f.filename is not None assert os.path.isdir(f.dirname) assert f.entities == {} def test_matches(self, file): file = copy(file) assert file._matches() assert file._matches(extensions='nii.gz') assert not file._matches(extensions=['.txt', '.rtf']) file.tags = {'task': Tag(None, 'rest'), 'run': Tag(None, '2')} assert file._matches(entities={'task': 'rest', 'run': 2}) assert not file._matches(entities={'task': 'rest', 'run': 4}) assert not file._matches(entities={'task': 'st'}) assert file._matches(entities={'task': 'st'}, regex_search=True) # With list of matches assert not file._matches(entities={'task': ['A', 'B', 'C']}) assert file._matches(entities={'task': ['A', 'B', 'rest']}) assert file._matches(entities={'task': ['A', 'B', 'st']}, regex_search=True) def test_named_tuple(self, file): file = copy(file) file.tags = {'attrA': Tag(None, 'apple'), 'attrB': Tag(None, 'banana')} tup = file.as_named_tuple() assert(tup.filename == file.path) assert isinstance(tup, tuple) assert not hasattr(tup, 'task') assert tup.attrA == 'apple' def test_named_tuple_with_reserved_name(self, file): file = copy(file) file.tags['class'] = Tag(None, 'invalid') with pytest.warns(UserWarning) as w: res = file.as_named_tuple() assert w[0].message.args[0].startswith('Entity names cannot') assert hasattr(res, 'class_') assert not hasattr(res, 'class') class TestEntity: def test_init(self): e = Entity('avaricious', 'aardvark-(\d+)') assert e.name == 'avaricious' assert e.pattern == 'aardvark-(\d+)' assert not e.mandatory assert e.directory is None assert e.files == {} def test_matches(self, tmpdir): filename = ""aardvark-4-reporting-for-duty.txt"" tmpdir.mkdir(""tmp"").join(filename).write(""###"") f = File(join(str(tmpdir), filename)) e = Entity('avaricious', 'aardvark-(\d+)') result = e.match_file(f) assert result == '4' def test_unique_and_count(self): e = Entity('prop', '-(\d+)') e.files = { 'test1-10.txt': '10', 'test2-7.txt': '7', 'test3-7.txt': '7' } assert sorted(e.unique()) == ['10', '7'] assert e.count() == 2 assert e.count(files=True) == 3 def test_add_file(self): e = Entity('prop', '-(\d+)') e.add_file('a', '1') assert e.files['a'] == '1' class TestLayout: def test_init(self, bids_layout): assert isinstance(bids_layout.files, dict) assert isinstance(bids_layout.entities, dict) assert isinstance(bids_layout.mandatory, set) assert not bids_layout.dynamic_getters def test_init_with_include_arg(self, bids_layout): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], regex_search=True, include='sub-\d*') target = join(root, ""dataset_description.json"") assert target in bids_layout.files assert target not in layout.files assert join(root, ""sub-01"", ""sub-01_sessions.tsv"") in layout.files with pytest.raises(ValueError): layout = Layout([(root, config)], include='sub-\d*', exclude=""meh"") def test_init_with_exclude_arg(self, bids_layout): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], regex_search=True, exclude='sub-\d*') target = join(root, ""dataset_description.json"") assert target in bids_layout.files assert target in layout.files sub_file = join(root, ""sub-01"", ""sub-01_sessions.tsv"") assert sub_file in bids_layout.files assert sub_file not in layout.files def test_init_with_config_options(self): root = join(DIRNAME, 'data') dir1 = join(root, 'valuable_stamps') dir2 = join(root, 'ordinary_stamps') config1 = join(DIRNAME, 'specs', 'stamps.json') config2 = join(dir1, 'USA', 'dir_config.json') # # Fails because Domain usa_stamps is included twice # with pytest.raises(ValueError) as e: # layout = Layout([(root, [config1, config2])], exclude=['7t_trt'], # config_filename='dir_config.json') # print(dir(e)) # assert e.value.message.startswith('Config with name') # Test with two configs layout = Layout([(root, [config1, config2])], exclude=['7t_trt']) files = [f.filename for f in layout.files.values()] assert 'name=Inverted_Jenny#value=75000#country=USA.txt' in files assert 'name=5c_Francis_E_Willard#value=1dollar.txt' in files assert 'name=1_Lotus#value=1#country=Canada.txt' in files # Test with two configs and on-the-fly directory remapping layout = Layout([dir1, ([dir1, dir2], config1)], exclude=['USA/']) files = [f.filename for f in layout.files.values()] assert 'name=Inverted_Jenny#value=75000#country=USA.txt' in files assert 'name=5c_Francis_E_Willard#value=1dollar.txt' not in files assert 'name=1_Lotus#value=1#country=Canada.txt' in files def test_absolute_paths(self, bids_layout): if not hasattr(bids_layout, '_hdfs_client'): root = join(DIRNAME, 'data', '7t_trt') root = os.path.relpath(root) config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], absolute_paths=True) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) layout = Layout([(root, config)], absolute_paths=False) result = layout.get(subject=1, run=1, session=1) assert result assert not any([os.path.isabs(f.filename) for f in result]) # Should always be absolute paths on HDFS else: root = psp.join('hdfs://localhost:9000{0}'.format( layout._hdfs_client.root), 'data', '7t_trt') config = psp.join('hdfs://localhost:9000{0}'.format( layout._hdfs_client.root), 'specs', 'test.json') layout = Layout([(root, config)], absolute_paths=False) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) layout = Layout([(root, config)], absolute_paths=True) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) @pytest.mark.parametrize('data_dir, config', [(join(DIRNAME, 'data', '7t_trt'), join(DIRNAME, 'specs', 'test.json')), (psp.join('hdfs://localhost:9000/grabbit/test/', 'data', '7t_trt'), psp.join('hdfs://localhost:9000/grabbit/test/', 'specs', 'test.json'))]) def test_dynamic_getters(self, data_dir, config): if ('hdfs' in data_dir or 'hdfs' in config): pytest.importorskip('hdfs') layout = Layout([(data_dir, config)], dynamic_getters=True) assert hasattr(layout, 'get_subjects') assert '01' in getattr(layout, 'get_subjects')() assert 1 in getattr(layout, 'get_runs')() def test_querying(self, bids_layout): # With regex_search = True (as set in Layout()) result = bids_layout.get(subject=1, run=1, session=1, extensions='nii.gz') assert len(result) == 8 result = bids_l", https://github.com/grabbles/grabbit,83ff93df36019eaaee9d4e31f816a518e46cae07,grabbit/tests/test_core.py::TestLayout::test_querying[local],grabbit/tests/test_core.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from grabbit import File, Entity, Layout, Tag, merge_layouts import os from os.path import join import posixpath as psp import tempfile import json from copy import copy DIRNAME = os.path.dirname(__file__) @pytest.fixture def file(tmpdir): testfile = 'sub-03_ses-2_task-rest_acq-fullbrain_run-2_bold.nii.gz' fn = tmpdir.mkdir(""tmp"").join(testfile) fn.write('###') return File(join(str(fn))) @pytest.fixture(scope='module', params=['local', 'hdfs']) def bids_layout(request): if request.param == 'local': root = join(DIRNAME, 'data', '7t_trt') # note about test.json: # in this test.json 'subject' regex was left to contain possible # leading 0; the other fields (run, session) has leading 0 stripped config = join(DIRNAME, 'specs', 'test.json') return Layout([(root, config)], regex_search=True) else: hdfs = pytest.importorskip(""hdfs"") from grabbit.extensions import HDFSLayout client = hdfs.Config().get_client() root = psp.join('hdfs://localhost:9000{0}'.format( client.root), 'data', '7t_trt') config = psp.join('hdfs://localhost:9000{0}'.format( client.root), 'specs', 'test.json') return HDFSLayout([(root, config)], regex_search=True) @pytest.fixture(scope='module') def stamp_layout(): root = join(DIRNAME, 'data', 'valuable_stamps') config = join(DIRNAME, 'specs', 'stamps.json') return Layout([(root, config)], config_filename='dir_config.json') @pytest.fixture(scope='module') def layout_include(request): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test_include.json') return Layout([(root, config)], regex_search=True) class TestFile: def test_init(self, tmpdir): fn = tmpdir.mkdir(""tmp"").join('tmp.txt') fn.write('###') f = File(str(fn)) assert os.path.exists(f.path) assert f.filename is not None assert os.path.isdir(f.dirname) assert f.entities == {} def test_matches(self, file): file = copy(file) assert file._matches() assert file._matches(extensions='nii.gz') assert not file._matches(extensions=['.txt', '.rtf']) file.tags = {'task': Tag(None, 'rest'), 'run': Tag(None, '2')} assert file._matches(entities={'task': 'rest', 'run': 2}) assert not file._matches(entities={'task': 'rest', 'run': 4}) assert not file._matches(entities={'task': 'st'}) assert file._matches(entities={'task': 'st'}, regex_search=True) # With list of matches assert not file._matches(entities={'task': ['A', 'B', 'C']}) assert file._matches(entities={'task': ['A', 'B', 'rest']}) assert file._matches(entities={'task': ['A', 'B', 'st']}, regex_search=True) def test_named_tuple(self, file): file = copy(file) file.tags = {'attrA': Tag(None, 'apple'), 'attrB': Tag(None, 'banana')} tup = file.as_named_tuple() assert(tup.filename == file.path) assert isinstance(tup, tuple) assert not hasattr(tup, 'task') assert tup.attrA == 'apple' def test_named_tuple_with_reserved_name(self, file): file = copy(file) file.tags['class'] = Tag(None, 'invalid') with pytest.warns(UserWarning) as w: res = file.as_named_tuple() assert w[0].message.args[0].startswith('Entity names cannot') assert hasattr(res, 'class_') assert not hasattr(res, 'class') class TestEntity: def test_init(self): e = Entity('avaricious', 'aardvark-(\d+)') assert e.name == 'avaricious' assert e.pattern == 'aardvark-(\d+)' assert not e.mandatory assert e.directory is None assert e.files == {} def test_matches(self, tmpdir): filename = ""aardvark-4-reporting-for-duty.txt"" tmpdir.mkdir(""tmp"").join(filename).write(""###"") f = File(join(str(tmpdir), filename)) e = Entity('avaricious', 'aardvark-(\d+)') result = e.match_file(f) assert result == '4' def test_unique_and_count(self): e = Entity('prop', '-(\d+)') e.files = { 'test1-10.txt': '10', 'test2-7.txt': '7', 'test3-7.txt': '7' } assert sorted(e.unique()) == ['10', '7'] assert e.count() == 2 assert e.count(files=True) == 3 def test_add_file(self): e = Entity('prop', '-(\d+)') e.add_file('a', '1') assert e.files['a'] == '1' class TestLayout: def test_init(self, bids_layout): assert isinstance(bids_layout.files, dict) assert isinstance(bids_layout.entities, dict) assert isinstance(bids_layout.mandatory, set) assert not bids_layout.dynamic_getters def test_init_with_include_arg(self, bids_layout): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], regex_search=True, include='sub-\d*') target = join(root, ""dataset_description.json"") assert target in bids_layout.files assert target not in layout.files assert join(root, ""sub-01"", ""sub-01_sessions.tsv"") in layout.files with pytest.raises(ValueError): layout = Layout([(root, config)], include='sub-\d*', exclude=""meh"") def test_init_with_exclude_arg(self, bids_layout): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], regex_search=True, exclude='sub-\d*') target = join(root, ""dataset_description.json"") assert target in bids_layout.files assert target in layout.files sub_file = join(root, ""sub-01"", ""sub-01_sessions.tsv"") assert sub_file in bids_layout.files assert sub_file not in layout.files def test_init_with_config_options(self): root = join(DIRNAME, 'data') dir1 = join(root, 'valuable_stamps') dir2 = join(root, 'ordinary_stamps') config1 = join(DIRNAME, 'specs', 'stamps.json') config2 = join(dir1, 'USA', 'dir_config.json') # # Fails because Domain usa_stamps is included twice # with pytest.raises(ValueError) as e: # layout = Layout([(root, [config1, config2])], exclude=['7t_trt'], # config_filename='dir_config.json') # print(dir(e)) # assert e.value.message.startswith('Config with name') # Test with two configs layout = Layout([(root, [config1, config2])], exclude=['7t_trt']) files = [f.filename for f in layout.files.values()] assert 'name=Inverted_Jenny#value=75000#country=USA.txt' in files assert 'name=5c_Francis_E_Willard#value=1dollar.txt' in files assert 'name=1_Lotus#value=1#country=Canada.txt' in files # Test with two configs and on-the-fly directory remapping layout = Layout([dir1, ([dir1, dir2], config1)], exclude=['USA/']) files = [f.filename for f in layout.files.values()] assert 'name=Inverted_Jenny#value=75000#country=USA.txt' in files assert 'name=5c_Francis_E_Willard#value=1dollar.txt' not in files assert 'name=1_Lotus#value=1#country=Canada.txt' in files def test_absolute_paths(self, bids_layout): if not hasattr(bids_layout, '_hdfs_client'): root = join(DIRNAME, 'data', '7t_trt') root = os.path.relpath(root) config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], absolute_paths=True) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) layout = Layout([(root, config)], absolute_paths=False) result = layout.get(subject=1, run=1, session=1) assert result assert not any([os.path.isabs(f.filename) for f in result]) # Should always be absolute paths on HDFS else: root = psp.join('hdfs://localhost:9000{0}'.format( layout._hdfs_client.root), 'data', '7t_trt') config = psp.join('hdfs://localhost:9000{0}'.format( layout._hdfs_client.root), 'specs', 'test.json') layout = Layout([(root, config)], absolute_paths=False) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) layout = Layout([(root, config)], absolute_paths=True) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) @pytest.mark.parametrize('data_dir, config', [(join(DIRNAME, 'data', '7t_trt'), join(DIRNAME, 'specs', 'test.json')), (psp.join('hdfs://localhost:9000/grabbit/test/', 'data', '7t_trt'), psp.join('hdfs://localhost:9000/grabbit/test/', 'specs', 'test.json'))]) def test_dynamic_getters(self, data_dir, config): if ('hdfs' in data_dir or 'hdfs' in config): pytest.importorskip('hdfs') layout = Layout([(data_dir, config)], dynamic_getters=True) assert hasattr(layout, 'get_subjects') assert '01' in getattr(layout, 'get_subjects')() assert 1 in getattr(layout, 'get_runs')() def test_querying(self, bids_layout): # With regex_search = True (as set in Layout()) result = bids_layout.get(subject=1, run=1, session=1, extensions='nii.gz') assert len(result) == 8 result = bids_l", https://github.com/grabbles/grabbit,83ff93df36019eaaee9d4e31f816a518e46cae07,grabbit/tests/test_core.py::TestLayout::test_unique_and_count[local],grabbit/tests/test_core.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from grabbit import File, Entity, Layout, Tag, merge_layouts import os from os.path import join import posixpath as psp import tempfile import json from copy import copy DIRNAME = os.path.dirname(__file__) @pytest.fixture def file(tmpdir): testfile = 'sub-03_ses-2_task-rest_acq-fullbrain_run-2_bold.nii.gz' fn = tmpdir.mkdir(""tmp"").join(testfile) fn.write('###') return File(join(str(fn))) @pytest.fixture(scope='module', params=['local', 'hdfs']) def bids_layout(request): if request.param == 'local': root = join(DIRNAME, 'data', '7t_trt') # note about test.json: # in this test.json 'subject' regex was left to contain possible # leading 0; the other fields (run, session) has leading 0 stripped config = join(DIRNAME, 'specs', 'test.json') return Layout([(root, config)], regex_search=True) else: hdfs = pytest.importorskip(""hdfs"") from grabbit.extensions import HDFSLayout client = hdfs.Config().get_client() root = psp.join('hdfs://localhost:9000{0}'.format( client.root), 'data', '7t_trt') config = psp.join('hdfs://localhost:9000{0}'.format( client.root), 'specs', 'test.json') return HDFSLayout([(root, config)], regex_search=True) @pytest.fixture(scope='module') def stamp_layout(): root = join(DIRNAME, 'data', 'valuable_stamps') config = join(DIRNAME, 'specs', 'stamps.json') return Layout([(root, config)], config_filename='dir_config.json') @pytest.fixture(scope='module') def layout_include(request): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test_include.json') return Layout([(root, config)], regex_search=True) class TestFile: def test_init(self, tmpdir): fn = tmpdir.mkdir(""tmp"").join('tmp.txt') fn.write('###') f = File(str(fn)) assert os.path.exists(f.path) assert f.filename is not None assert os.path.isdir(f.dirname) assert f.entities == {} def test_matches(self, file): file = copy(file) assert file._matches() assert file._matches(extensions='nii.gz') assert not file._matches(extensions=['.txt', '.rtf']) file.tags = {'task': Tag(None, 'rest'), 'run': Tag(None, '2')} assert file._matches(entities={'task': 'rest', 'run': 2}) assert not file._matches(entities={'task': 'rest', 'run': 4}) assert not file._matches(entities={'task': 'st'}) assert file._matches(entities={'task': 'st'}, regex_search=True) # With list of matches assert not file._matches(entities={'task': ['A', 'B', 'C']}) assert file._matches(entities={'task': ['A', 'B', 'rest']}) assert file._matches(entities={'task': ['A', 'B', 'st']}, regex_search=True) def test_named_tuple(self, file): file = copy(file) file.tags = {'attrA': Tag(None, 'apple'), 'attrB': Tag(None, 'banana')} tup = file.as_named_tuple() assert(tup.filename == file.path) assert isinstance(tup, tuple) assert not hasattr(tup, 'task') assert tup.attrA == 'apple' def test_named_tuple_with_reserved_name(self, file): file = copy(file) file.tags['class'] = Tag(None, 'invalid') with pytest.warns(UserWarning) as w: res = file.as_named_tuple() assert w[0].message.args[0].startswith('Entity names cannot') assert hasattr(res, 'class_') assert not hasattr(res, 'class') class TestEntity: def test_init(self): e = Entity('avaricious', 'aardvark-(\d+)') assert e.name == 'avaricious' assert e.pattern == 'aardvark-(\d+)' assert not e.mandatory assert e.directory is None assert e.files == {} def test_matches(self, tmpdir): filename = ""aardvark-4-reporting-for-duty.txt"" tmpdir.mkdir(""tmp"").join(filename).write(""###"") f = File(join(str(tmpdir), filename)) e = Entity('avaricious', 'aardvark-(\d+)') result = e.match_file(f) assert result == '4' def test_unique_and_count(self): e = Entity('prop', '-(\d+)') e.files = { 'test1-10.txt': '10', 'test2-7.txt': '7', 'test3-7.txt': '7' } assert sorted(e.unique()) == ['10', '7'] assert e.count() == 2 assert e.count(files=True) == 3 def test_add_file(self): e = Entity('prop', '-(\d+)') e.add_file('a', '1') assert e.files['a'] == '1' class TestLayout: def test_init(self, bids_layout): assert isinstance(bids_layout.files, dict) assert isinstance(bids_layout.entities, dict) assert isinstance(bids_layout.mandatory, set) assert not bids_layout.dynamic_getters def test_init_with_include_arg(self, bids_layout): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], regex_search=True, include='sub-\d*') target = join(root, ""dataset_description.json"") assert target in bids_layout.files assert target not in layout.files assert join(root, ""sub-01"", ""sub-01_sessions.tsv"") in layout.files with pytest.raises(ValueError): layout = Layout([(root, config)], include='sub-\d*', exclude=""meh"") def test_init_with_exclude_arg(self, bids_layout): root = join(DIRNAME, 'data', '7t_trt') config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], regex_search=True, exclude='sub-\d*') target = join(root, ""dataset_description.json"") assert target in bids_layout.files assert target in layout.files sub_file = join(root, ""sub-01"", ""sub-01_sessions.tsv"") assert sub_file in bids_layout.files assert sub_file not in layout.files def test_init_with_config_options(self): root = join(DIRNAME, 'data') dir1 = join(root, 'valuable_stamps') dir2 = join(root, 'ordinary_stamps') config1 = join(DIRNAME, 'specs', 'stamps.json') config2 = join(dir1, 'USA', 'dir_config.json') # # Fails because Domain usa_stamps is included twice # with pytest.raises(ValueError) as e: # layout = Layout([(root, [config1, config2])], exclude=['7t_trt'], # config_filename='dir_config.json') # print(dir(e)) # assert e.value.message.startswith('Config with name') # Test with two configs layout = Layout([(root, [config1, config2])], exclude=['7t_trt']) files = [f.filename for f in layout.files.values()] assert 'name=Inverted_Jenny#value=75000#country=USA.txt' in files assert 'name=5c_Francis_E_Willard#value=1dollar.txt' in files assert 'name=1_Lotus#value=1#country=Canada.txt' in files # Test with two configs and on-the-fly directory remapping layout = Layout([dir1, ([dir1, dir2], config1)], exclude=['USA/']) files = [f.filename for f in layout.files.values()] assert 'name=Inverted_Jenny#value=75000#country=USA.txt' in files assert 'name=5c_Francis_E_Willard#value=1dollar.txt' not in files assert 'name=1_Lotus#value=1#country=Canada.txt' in files def test_absolute_paths(self, bids_layout): if not hasattr(bids_layout, '_hdfs_client'): root = join(DIRNAME, 'data', '7t_trt') root = os.path.relpath(root) config = join(DIRNAME, 'specs', 'test.json') layout = Layout([(root, config)], absolute_paths=True) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) layout = Layout([(root, config)], absolute_paths=False) result = layout.get(subject=1, run=1, session=1) assert result assert not any([os.path.isabs(f.filename) for f in result]) # Should always be absolute paths on HDFS else: root = psp.join('hdfs://localhost:9000{0}'.format( layout._hdfs_client.root), 'data', '7t_trt') config = psp.join('hdfs://localhost:9000{0}'.format( layout._hdfs_client.root), 'specs', 'test.json') layout = Layout([(root, config)], absolute_paths=False) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) layout = Layout([(root, config)], absolute_paths=True) result = layout.get(subject=1, run=1, session=1) assert result assert all([os.path.isabs(f.filename) for f in result]) @pytest.mark.parametrize('data_dir, config', [(join(DIRNAME, 'data', '7t_trt'), join(DIRNAME, 'specs', 'test.json')), (psp.join('hdfs://localhost:9000/grabbit/test/', 'data', '7t_trt'), psp.join('hdfs://localhost:9000/grabbit/test/', 'specs', 'test.json'))]) def test_dynamic_getters(self, data_dir, config): if ('hdfs' in data_dir or 'hdfs' in config): pytest.importorskip('hdfs') layout = Layout([(data_dir, config)], dynamic_getters=True) assert hasattr(layout, 'get_subjects') assert '01' in getattr(layout, 'get_subjects')() assert 1 in getattr(layout, 'get_runs')() def test_querying(self, bids_layout): # With regex_search = True (as set in Layout()) result = bids_layout.get(subject=1, run=1, session=1, extensions='nii.gz') assert len(result) == 8 result = bids_l", https://github.com/gradam/lol_api,33ea96f5eeac581737949b733d8af1c4436e94b9,tests/test_daemon.py::TestDaemon::test_different_api_key,tests/test_daemon.py,NIO,flaky,RepoArchived,nan,classify;root_cause,"# encoding: utf-8 import multiprocessing from time import sleep import pytest import requests from lol_api.daemon import ApiDaemon from lol_api.utils import count_request from lol_api.exceptions import RateLimitExceededError from lol_api.settings import settings t = None def setup_module(module): global t settings.DAEMON_SERVER = ('localhost', 8877) daemon = ApiDaemon(log=False) t = multiprocessing.Process(target=daemon.run) class TestDaemon: message = 'test' api_key = 'testkey' @classmethod def setup_class(cls): t.start() sleep(0.05) @count_request def api_func(self, region=None): return self.message def test_short_request_unavailable(self): with pytest.raises(RateLimitExceededError): for _ in range(11): self.api_func(region='euw') def test_short(self): for _ in range(9): self.api_func(region='eune') def test_different_api_key(self): for _ in range(10): self.api_func(region='las') settings.API_KEY = 'testkey2' for _ in range(10): self.api_func(region='las') def teardown_module(module): t.terminate() settings.DAEMON_SERVER = () ", https://github.com/gradam/lol_api,33ea96f5eeac581737949b733d8af1c4436e94b9,tests/test_daemon.py::TestDaemon::test_short,tests/test_daemon.py,NIO,flaky,RepoArchived,nan,classify;root_cause,"# encoding: utf-8 import multiprocessing from time import sleep import pytest import requests from lol_api.daemon import ApiDaemon from lol_api.utils import count_request from lol_api.exceptions import RateLimitExceededError from lol_api.settings import settings t = None def setup_module(module): global t settings.DAEMON_SERVER = ('localhost', 8877) daemon = ApiDaemon(log=False) t = multiprocessing.Process(target=daemon.run) class TestDaemon: message = 'test' api_key = 'testkey' @classmethod def setup_class(cls): t.start() sleep(0.05) @count_request def api_func(self, region=None): return self.message def test_short_request_unavailable(self): with pytest.raises(RateLimitExceededError): for _ in range(11): self.api_func(region='euw') def test_short(self): for _ in range(9): self.api_func(region='eune') def test_different_api_key(self): for _ in range(10): self.api_func(region='las') settings.API_KEY = 'testkey2' for _ in range(10): self.api_func(region='las') def teardown_module(module): t.terminate() settings.DAEMON_SERVER = () ", https://github.com/Gsllchb/DotDot,64c8c5b0523211f243eef5dc6ea7c22dec5383cc,tests/test_dot.py::test_dot,tests/test_dot.py,OD,flaky,Rejected,https://github.com/Gsllchb/DotDot/pull/1,classify;root_cause,"# coding: utf-8 import os.path import sys DOT = sys.path[0] DOTDOT = os.path.dirname(DOT) DOTDOTDOT = os.path.dirname(DOTDOT) DOTDOTDOTDOT = os.path.dirname(DOTDOTDOT) sys.path.append(DOTDOT) def test_dot(): import dot import dotdot import dotdotdot import dotdotdotdot assert sys.path[0] == DOT ", https://github.com/Gsllchb/DotDot,89a6f7a86866de9c348f7e91da57c359c94568db,tests/test_dot.py::test_dot,tests/test_dot.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# coding: utf-8 import os.path import sys DOT = sys.path[0] DOTDOT = os.path.dirname(DOT) DOTDOTDOT = os.path.dirname(DOTDOT) DOTDOTDOTDOT = os.path.dirname(DOTDOTDOT) sys.path.append(DOTDOT) def test_dot(): import dot import dotdot import dotdotdot import dotdotdotdot assert sys.path[0] == DOT ", https://github.com/Gsllchb/DotDot,64c8c5b0523211f243eef5dc6ea7c22dec5383cc,tests/test_dotdot.py::test_dotdot,tests/test_dotdot.py,OD,flaky,Rejected,https://github.com/Gsllchb/DotDot/pull/1,classify;root_cause,"# coding: utf-8 import os.path import sys DOT = sys.path[0] DOTDOT = os.path.dirname(DOT) DOTDOTDOT = os.path.dirname(DOTDOT) DOTDOTDOTDOT = os.path.dirname(DOTDOTDOT) sys.path.append(DOTDOT) def test_dotdot(): import dotdot import dot import dotdotdot import dotdotdotdot assert sys.path[0] == DOTDOT ", https://github.com/Gsllchb/DotDot,89a6f7a86866de9c348f7e91da57c359c94568db,tests/test_dotdot.py::test_dotdot,tests/test_dotdot.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# coding: utf-8 import os.path import sys DOT = sys.path[0] DOTDOT = os.path.dirname(DOT) DOTDOTDOT = os.path.dirname(DOTDOT) DOTDOTDOTDOT = os.path.dirname(DOTDOTDOT) sys.path.append(DOTDOT) def test_dotdot(): import dotdot import dot import dotdotdot import dotdotdotdot assert sys.path[0] == DOTDOT ", https://github.com/Gsllchb/DotDot,64c8c5b0523211f243eef5dc6ea7c22dec5383cc,tests/test_dotdotdot.py::test_dotdotdot,tests/test_dotdotdot.py,OD,flaky,Rejected,https://github.com/Gsllchb/DotDot/pull/1,classify;root_cause,"# coding: utf-8 import os.path import sys DOT = sys.path[0] DOTDOT = os.path.dirname(DOT) DOTDOTDOT = os.path.dirname(DOTDOT) DOTDOTDOTDOT = os.path.dirname(DOTDOTDOT) sys.path.append(DOTDOT) def test_dotdotdot(): import dotdotdot import dotdot import dot import dotdotdot import dotdotdotdot assert sys.path[0] == DOTDOTDOT ", https://github.com/Gsllchb/DotDot,89a6f7a86866de9c348f7e91da57c359c94568db,tests/test_dotdotdot.py::test_dotdotdot,tests/test_dotdotdot.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# coding: utf-8 import os.path import sys DOT = sys.path[0] DOTDOT = os.path.dirname(DOT) DOTDOTDOT = os.path.dirname(DOTDOT) DOTDOTDOTDOT = os.path.dirname(DOTDOTDOT) sys.path.append(DOTDOT) def test_dotdotdot(): import dotdotdot import dotdot import dot import dotdotdot import dotdotdotdot assert sys.path[0] == DOTDOTDOT ", https://github.com/Gsllchb/DotDot,64c8c5b0523211f243eef5dc6ea7c22dec5383cc,tests/test_dotdotdotdot.py::test_dotdotdotdot,tests/test_dotdotdotdot.py,OD,flaky,Rejected,https://github.com/Gsllchb/DotDot/pull/1,classify;root_cause,"# coding: utf-8 import os.path import sys DOT = sys.path[0] DOTDOT = os.path.dirname(DOT) DOTDOTDOT = os.path.dirname(DOTDOT) DOTDOTDOTDOT = os.path.dirname(DOTDOTDOT) sys.path.append(DOTDOT) def test_dotdotdotdot(): import dotdotdotdot import dotdotdot import dotdot import dot import dotdotdot assert sys.path[0] == DOTDOTDOTDOT ", https://github.com/Gsllchb/DotDot,89a6f7a86866de9c348f7e91da57c359c94568db,tests/test_dotdotdotdot.py::test_dotdotdotdot,tests/test_dotdotdotdot.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# coding: utf-8 import os.path import sys DOT = sys.path[0] DOTDOT = os.path.dirname(DOT) DOTDOTDOT = os.path.dirname(DOTDOT) DOTDOTDOTDOT = os.path.dirname(DOTDOTDOT) sys.path.append(DOTDOT) def test_dotdotdotdot(): import dotdotdotdot import dotdotdot import dotdot import dot import dotdotdot assert sys.path[0] == DOTDOTDOTDOT ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_command.py::TestCommands::test_command_add,tests/test_command.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.centreon import Webservice from centreonapi.webservice.configuration.command import Command from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestCommands: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_command_list(self, centreon_con): with open(resource_dir / 'test_commands_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.commands.get('OS-Linux-SNMP-Memory') assert res.id == ""111"" @responses.activate def test_command_not_exist(self, centreon_con): with open(resource_dir / 'test_commands_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') state, cmd = centreon_con.commands.get(""empty"") assert state == False def test_command_add(self, centreon_con): values = [ 'command_test', 'check', '/my/plugins my command' ] data = {} data['action'] = 'add' data['object'] = 'CMD' data['values'] = values with patch('requests.post') as patched_post: centreon_con.commands.add(""command_test"", ""check"", ""/my/plugins my command"", post_refresh=False ) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) def test_command_delete(self, centreon_con): data = {} data['action'] = 'del' data['object'] = 'CMD' data['values'] = 'command_test' with patch('requests.post') as patched_post: centreon_con.commands.delete('command_test', post_refresh=False) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) @responses.activate def test_command_setparam(self): with open(resource_dir / 'test_commands_1.json') as data: cmd = Command(json.load(data)) values = [ cmd.name, 'type', 'notif', ] data = {} data['action'] = 'setparam' data['object'] = 'CMD' data['values'] = values with patch('requests.post') as patched_post: cmd.setparam('type', 'notif') patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_command.py::TestCommands::test_command_delete,tests/test_command.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.centreon import Webservice from centreonapi.webservice.configuration.command import Command from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestCommands: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_command_list(self, centreon_con): with open(resource_dir / 'test_commands_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.commands.get('OS-Linux-SNMP-Memory') assert res.id == ""111"" @responses.activate def test_command_not_exist(self, centreon_con): with open(resource_dir / 'test_commands_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') state, cmd = centreon_con.commands.get(""empty"") assert state == False def test_command_add(self, centreon_con): values = [ 'command_test', 'check', '/my/plugins my command' ] data = {} data['action'] = 'add' data['object'] = 'CMD' data['values'] = values with patch('requests.post') as patched_post: centreon_con.commands.add(""command_test"", ""check"", ""/my/plugins my command"", post_refresh=False ) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) def test_command_delete(self, centreon_con): data = {} data['action'] = 'del' data['object'] = 'CMD' data['values'] = 'command_test' with patch('requests.post') as patched_post: centreon_con.commands.delete('command_test', post_refresh=False) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) @responses.activate def test_command_setparam(self): with open(resource_dir / 'test_commands_1.json') as data: cmd = Command(json.load(data)) values = [ cmd.name, 'type', 'notif', ] data = {} data['action'] = 'setparam' data['object'] = 'CMD' data['values'] = values with patch('requests.post') as patched_post: cmd.setparam('type', 'notif') patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_command.py::TestCommands::test_command_list,tests/test_command.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.centreon import Webservice from centreonapi.webservice.configuration.command import Command from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestCommands: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_command_list(self, centreon_con): with open(resource_dir / 'test_commands_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.commands.get('OS-Linux-SNMP-Memory') assert res.id == ""111"" @responses.activate def test_command_not_exist(self, centreon_con): with open(resource_dir / 'test_commands_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') state, cmd = centreon_con.commands.get(""empty"") assert state == False def test_command_add(self, centreon_con): values = [ 'command_test', 'check', '/my/plugins my command' ] data = {} data['action'] = 'add' data['object'] = 'CMD' data['values'] = values with patch('requests.post') as patched_post: centreon_con.commands.add(""command_test"", ""check"", ""/my/plugins my command"", post_refresh=False ) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) def test_command_delete(self, centreon_con): data = {} data['action'] = 'del' data['object'] = 'CMD' data['values'] = 'command_test' with patch('requests.post') as patched_post: centreon_con.commands.delete('command_test', post_refresh=False) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) @responses.activate def test_command_setparam(self): with open(resource_dir / 'test_commands_1.json') as data: cmd = Command(json.load(data)) values = [ cmd.name, 'type', 'notif', ] data = {} data['action'] = 'setparam' data['object'] = 'CMD' data['values'] = values with patch('requests.post') as patched_post: cmd.setparam('type', 'notif') patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_command.py::TestCommands::test_command_not_exist,tests/test_command.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.centreon import Webservice from centreonapi.webservice.configuration.command import Command from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestCommands: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_command_list(self, centreon_con): with open(resource_dir / 'test_commands_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.commands.get('OS-Linux-SNMP-Memory') assert res.id == ""111"" @responses.activate def test_command_not_exist(self, centreon_con): with open(resource_dir / 'test_commands_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') state, cmd = centreon_con.commands.get(""empty"") assert state == False def test_command_add(self, centreon_con): values = [ 'command_test', 'check', '/my/plugins my command' ] data = {} data['action'] = 'add' data['object'] = 'CMD' data['values'] = values with patch('requests.post') as patched_post: centreon_con.commands.add(""command_test"", ""check"", ""/my/plugins my command"", post_refresh=False ) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) def test_command_delete(self, centreon_con): data = {} data['action'] = 'del' data['object'] = 'CMD' data['values'] = 'command_test' with patch('requests.post') as patched_post: centreon_con.commands.delete('command_test', post_refresh=False) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) @responses.activate def test_command_setparam(self): with open(resource_dir / 'test_commands_1.json') as data: cmd = Command(json.load(data)) values = [ cmd.name, 'type', 'notif', ] data = {} data['action'] = 'setparam' data['object'] = 'CMD' data['values'] = values with patch('requests.post') as patched_post: cmd.setparam('type', 'notif') patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_command.py::TestCommands::test_command_setparam,tests/test_command.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.centreon import Webservice from centreonapi.webservice.configuration.command import Command from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestCommands: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_command_list(self, centreon_con): with open(resource_dir / 'test_commands_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.commands.get('OS-Linux-SNMP-Memory') assert res.id == ""111"" @responses.activate def test_command_not_exist(self, centreon_con): with open(resource_dir / 'test_commands_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') state, cmd = centreon_con.commands.get(""empty"") assert state == False def test_command_add(self, centreon_con): values = [ 'command_test', 'check', '/my/plugins my command' ] data = {} data['action'] = 'add' data['object'] = 'CMD' data['values'] = values with patch('requests.post') as patched_post: centreon_con.commands.add(""command_test"", ""check"", ""/my/plugins my command"", post_refresh=False ) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) def test_command_delete(self, centreon_con): data = {} data['action'] = 'del' data['object'] = 'CMD' data['values'] = 'command_test' with patch('requests.post') as patched_post: centreon_con.commands.delete('command_test', post_refresh=False) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) @responses.activate def test_command_setparam(self): with open(resource_dir / 'test_commands_1.json') as data: cmd = Command(json.load(data)) values = [ cmd.name, 'type', 'notif', ] data = {} data['action'] = 'setparam' data['object'] = 'CMD' data['values'] = values with patch('requests.post') as patched_post: cmd.setparam('type', 'notif') patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_addcontact,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_addcontactgroupt,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_addparent,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_addtemplate,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_applytemplate,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_deletecontact,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_deletecontactgroup,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_deletehostgroup,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_delmacro,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_deteletemplate,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_disable,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_enable,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_getcontact,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_getcontactgroup,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_gethostgroup,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_getmacro,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_getparents,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_gettemplate,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_setcontact,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_setcontactgroup,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_setinstance,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_setmacro,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_setparam,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_setparent,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHost::test_host_settemplate,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHosts::test_hosts_add,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHosts::test_hosts_delete,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHosts::test_hosts_delete_with_obj,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHosts::test_hosts_list,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_hosts.py::TestHosts::test_hosts_not_exist,tests/test_hosts.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.host import Hosts, Host, HostMacro, HostTemplate from centreonapi.webservice.configuration.poller import Poller from centreonapi.webservice.configuration.hostgroups import HostGroup from centreonapi.webservice.configuration.contact import ContactGroup, Contact from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add( responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestHosts: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_hosts_list(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.hosts.get('mail-uranus-frontend') assert res.id == ""12"" @responses.activate def test_hosts_not_exist(self, centreon_con): with open(resource_dir / 'test_hosts_list.json') as data: wsresponses = json.load(data) responses.add( responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.hosts.get('empty') assert state == False assert res == None def test_hosts_add(self, centreon_con): values = [ ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-disk"", ""Central"", ""hg"" ] data = dict() data['action'] = 'add' data['object'] = 'HOST' data['values'] = values with patch('requests.post') as patched_post: centreon_con.hosts.add( ""new_host.tld"", ""new_host"", ""127.0.0.7"", ""Central"", [""OS-Linux-SNMP-custom"", ""OS-Linux-SNMP-disk""], [""hg""], post_refresh=False ) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_hosts_delete(self, centreon_con): data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'my_deleted_host' with patch('requests.post') as patched_post: centreon_con.hosts.delete('my_deleted_host', post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) def test_hosts_delete_with_obj(self, centreon_con, host_load_data): host = host_load_data data = dict() data['action'] = 'del' data['object'] = 'HOST' data['values'] = 'mail-uranus-frontend' with patch('requests.post') as patched_post: centreon_con.hosts.delete(host, post_refresh=False) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) class TestHost(): clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @pytest.fixture() def host_load_data(self): with open(resource_dir / 'test_host_obj.json') as hdata: return Host(json.load(hdata)) @responses.activate def test_host_getmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macros.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.getmacro() assert res[""$_HOSTMATTERMOST_CHAN$""].name == ""MATTERMOST_CHAN"" def test_host_setmacro(self, host_load_data): host = host_load_data data = dict() data['action'] = 'setmacro' data['object'] = 'HOST' data['values'] = [host.name, 'MACRO_TEST', 'VALUE_TEST', '0', 'DESC'] with patch('requests.post') as patched_post: host.setmacro('MACRO_TEST', 'VALUE_TEST', '0', 'DESC') patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_delmacro(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_macro.json') as m: macro = HostMacro(json.load(m)) data = dict() data['action'] = 'delmacro' data['object'] = 'HOST' data['values'] = [host.name, 'NRPEPORT'] with patch('requests.post') as patched_post: host.deletemacro(macro) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) @responses.activate def test_host_gettemplate(self, host_load_data): host = host_load_data with open(resource_dir / 'test_host_templates.json') as data: wsresponses = json.load(data) responses.add(responses.POST, self.clapi_url, json=wsresponses, status=200, content_type='application/json') _, res = host.gettemplate() print(res) assert res[""OS-Linux-SNMP-custom""].id == ""6"" def test_host_settemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'settemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.settemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_addtemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: tmp = json.load(htlp) for tlp in tmp: print(tlp) templates.append(HostTemplate(tlp)) data = dict() data['action'] = 'addtemplate' data['object'] = 'HOST' data['values'] = [ ""mail-uranus-frontend"", ""OS-Linux-SNMP-custom|OS-Linux-SNMP-Disk-/""] with patch('requests.post') as patched_post: host.addtemplate(templates) patched_post.assert_called_with( self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True ) def test_host_deteletemplate(self, host_load_data): host = host_load_data templates = list() with open(resource_dir / 'test_host_template.json') as htlp: ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_resourcecfg.py::TestResourceCFG::test_resourcecfg_add,tests/test_resourcecfg.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.resourcecfg import ResourceCFG from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestResourceCFG: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_resourcecfg_get_one(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.resourcecfgs.get('$_HOSTSNMPVERSION$') assert res.name == ""$_HOSTSNMPVERSION$"" @responses.activate def test_resourcecfg_building_line(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.resourcecfgs.get('_HOSTSNMPVERSION') assert res.name == ""$_HOSTSNMPVERSION$"" @responses.activate def test_resourcecfg_not_exist(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.resourcecfgs.get('empty') assert state == False assert res == None clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } def test_resourcecfg_add(self, centreon_con): values = [ 'resource_test', 'ressource_value', 'Central', 'comment' ] data = {} data['action'] = 'add' data['object'] = 'RESOURCECFG' data['values'] = values with patch('requests.post') as patched_post: centreon_con.resourcecfgs.add(""resource_test"", ""ressource_value"", ""Central"", ""comment"", post_refresh=False ) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) def test_resourcecfg_delete(self, centreon_con): data = {} data['action'] = 'del' data['object'] = 'RESOURCECFG' data['values'] = '42' with patch('requests.post') as patched_post: centreon_con.resourcecfgs.delete('42', post_refresh=False) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) @responses.activate def test_resourcecfg_setparam(self): with open(resource_dir / 'test_resourcecfg_1.json') as data: res = ResourceCFG(json.load(data)) values = [ res.id, 'instance', 'Central', ] data = {} data['action'] = 'setparam' data['object'] = 'RESOURCECFG' data['values'] = values with patch('requests.post') as patched_post: res.setparam('instance', 'Central') patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_resourcecfg.py::TestResourceCFG::test_resourcecfg_building_line,tests/test_resourcecfg.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.resourcecfg import ResourceCFG from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestResourceCFG: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_resourcecfg_get_one(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.resourcecfgs.get('$_HOSTSNMPVERSION$') assert res.name == ""$_HOSTSNMPVERSION$"" @responses.activate def test_resourcecfg_building_line(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.resourcecfgs.get('_HOSTSNMPVERSION') assert res.name == ""$_HOSTSNMPVERSION$"" @responses.activate def test_resourcecfg_not_exist(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.resourcecfgs.get('empty') assert state == False assert res == None clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } def test_resourcecfg_add(self, centreon_con): values = [ 'resource_test', 'ressource_value', 'Central', 'comment' ] data = {} data['action'] = 'add' data['object'] = 'RESOURCECFG' data['values'] = values with patch('requests.post') as patched_post: centreon_con.resourcecfgs.add(""resource_test"", ""ressource_value"", ""Central"", ""comment"", post_refresh=False ) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) def test_resourcecfg_delete(self, centreon_con): data = {} data['action'] = 'del' data['object'] = 'RESOURCECFG' data['values'] = '42' with patch('requests.post') as patched_post: centreon_con.resourcecfgs.delete('42', post_refresh=False) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) @responses.activate def test_resourcecfg_setparam(self): with open(resource_dir / 'test_resourcecfg_1.json') as data: res = ResourceCFG(json.load(data)) values = [ res.id, 'instance', 'Central', ] data = {} data['action'] = 'setparam' data['object'] = 'RESOURCECFG' data['values'] = values with patch('requests.post') as patched_post: res.setparam('instance', 'Central') patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_resourcecfg.py::TestResourceCFG::test_resourcecfg_delete,tests/test_resourcecfg.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.resourcecfg import ResourceCFG from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestResourceCFG: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_resourcecfg_get_one(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.resourcecfgs.get('$_HOSTSNMPVERSION$') assert res.name == ""$_HOSTSNMPVERSION$"" @responses.activate def test_resourcecfg_building_line(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.resourcecfgs.get('_HOSTSNMPVERSION') assert res.name == ""$_HOSTSNMPVERSION$"" @responses.activate def test_resourcecfg_not_exist(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.resourcecfgs.get('empty') assert state == False assert res == None clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } def test_resourcecfg_add(self, centreon_con): values = [ 'resource_test', 'ressource_value', 'Central', 'comment' ] data = {} data['action'] = 'add' data['object'] = 'RESOURCECFG' data['values'] = values with patch('requests.post') as patched_post: centreon_con.resourcecfgs.add(""resource_test"", ""ressource_value"", ""Central"", ""comment"", post_refresh=False ) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) def test_resourcecfg_delete(self, centreon_con): data = {} data['action'] = 'del' data['object'] = 'RESOURCECFG' data['values'] = '42' with patch('requests.post') as patched_post: centreon_con.resourcecfgs.delete('42', post_refresh=False) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) @responses.activate def test_resourcecfg_setparam(self): with open(resource_dir / 'test_resourcecfg_1.json') as data: res = ResourceCFG(json.load(data)) values = [ res.id, 'instance', 'Central', ] data = {} data['action'] = 'setparam' data['object'] = 'RESOURCECFG' data['values'] = values with patch('requests.post') as patched_post: res.setparam('instance', 'Central') patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_resourcecfg.py::TestResourceCFG::test_resourcecfg_get_one,tests/test_resourcecfg.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.resourcecfg import ResourceCFG from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestResourceCFG: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_resourcecfg_get_one(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.resourcecfgs.get('$_HOSTSNMPVERSION$') assert res.name == ""$_HOSTSNMPVERSION$"" @responses.activate def test_resourcecfg_building_line(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.resourcecfgs.get('_HOSTSNMPVERSION') assert res.name == ""$_HOSTSNMPVERSION$"" @responses.activate def test_resourcecfg_not_exist(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.resourcecfgs.get('empty') assert state == False assert res == None clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } def test_resourcecfg_add(self, centreon_con): values = [ 'resource_test', 'ressource_value', 'Central', 'comment' ] data = {} data['action'] = 'add' data['object'] = 'RESOURCECFG' data['values'] = values with patch('requests.post') as patched_post: centreon_con.resourcecfgs.add(""resource_test"", ""ressource_value"", ""Central"", ""comment"", post_refresh=False ) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) def test_resourcecfg_delete(self, centreon_con): data = {} data['action'] = 'del' data['object'] = 'RESOURCECFG' data['values'] = '42' with patch('requests.post') as patched_post: centreon_con.resourcecfgs.delete('42', post_refresh=False) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) @responses.activate def test_resourcecfg_setparam(self): with open(resource_dir / 'test_resourcecfg_1.json') as data: res = ResourceCFG(json.load(data)) values = [ res.id, 'instance', 'Central', ] data = {} data['action'] = 'setparam' data['object'] = 'RESOURCECFG' data['values'] = values with patch('requests.post') as patched_post: res.setparam('instance', 'Central') patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_resourcecfg.py::TestResourceCFG::test_resourcecfg_not_exist,tests/test_resourcecfg.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.resourcecfg import ResourceCFG from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestResourceCFG: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_resourcecfg_get_one(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.resourcecfgs.get('$_HOSTSNMPVERSION$') assert res.name == ""$_HOSTSNMPVERSION$"" @responses.activate def test_resourcecfg_building_line(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.resourcecfgs.get('_HOSTSNMPVERSION') assert res.name == ""$_HOSTSNMPVERSION$"" @responses.activate def test_resourcecfg_not_exist(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.resourcecfgs.get('empty') assert state == False assert res == None clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } def test_resourcecfg_add(self, centreon_con): values = [ 'resource_test', 'ressource_value', 'Central', 'comment' ] data = {} data['action'] = 'add' data['object'] = 'RESOURCECFG' data['values'] = values with patch('requests.post') as patched_post: centreon_con.resourcecfgs.add(""resource_test"", ""ressource_value"", ""Central"", ""comment"", post_refresh=False ) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) def test_resourcecfg_delete(self, centreon_con): data = {} data['action'] = 'del' data['object'] = 'RESOURCECFG' data['values'] = '42' with patch('requests.post') as patched_post: centreon_con.resourcecfgs.delete('42', post_refresh=False) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) @responses.activate def test_resourcecfg_setparam(self): with open(resource_dir / 'test_resourcecfg_1.json') as data: res = ResourceCFG(json.load(data)) values = [ res.id, 'instance', 'Central', ] data = {} data['action'] = 'setparam' data['object'] = 'RESOURCECFG' data['values'] = values with patch('requests.post') as patched_post: res.setparam('instance', 'Central') patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) ", https://github.com/guillaumewatteeux/centreon-sdk-python,9f207d5458afc3304e24f16988069467b19eab81,tests/test_resourcecfg.py::TestResourceCFG::test_resourcecfg_setparam,tests/test_resourcecfg.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for `centreonapi` module. """""" import pytest import responses import json import os from centreonapi.centreon import Centreon from centreonapi.webservice import Webservice from centreonapi.webservice.configuration.resourcecfg import ResourceCFG from mock import patch from path import Path if os.path.isdir('tests'): config_dir = Path('tests') resource_dir = Path('tests/resources') else: config_dir = Path('.') resource_dir = Path('./resources') class TestConnect: @responses.activate def test_connection(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" mytoken = ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc="" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') myconn = Webservice.getInstance(url, username, password) myconn.auth() assert mytoken == myconn.auth_token class TestResourceCFG: clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } @pytest.fixture() @responses.activate def centreon_con(self): url = ""http://api.domain.tld/centreon"" username = ""mytest"" password = ""mypass"" wsresponses = '{""authToken"": ""NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=""}' responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=authenticate', body=wsresponses, status=200, content_type='application/json') return Centreon(url, username, password) @responses.activate def test_resourcecfg_get_one(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.resourcecfgs.get('$_HOSTSNMPVERSION$') assert res.name == ""$_HOSTSNMPVERSION$"" @responses.activate def test_resourcecfg_building_line(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') _, res = centreon_con.resourcecfgs.get('_HOSTSNMPVERSION') assert res.name == ""$_HOSTSNMPVERSION$"" @responses.activate def test_resourcecfg_not_exist(self, centreon_con): with open(resource_dir / 'test_resourcecfg_list.json') as data: wsresponses = json.load(data) responses.add(responses.POST, 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi', json=wsresponses, status=200, content_type='application/json') state, res = centreon_con.resourcecfgs.get('empty') assert state == False assert res == None clapi_url = 'http://api.domain.tld/centreon/api/index.php?action=action&object=centreon_clapi' headers = { 'Content-Type': 'application/json', 'centreon-auth-token': 'NTc1MDU3MGE3M2JiODIuMjA4OTA2OTc=' } def test_resourcecfg_add(self, centreon_con): values = [ 'resource_test', 'ressource_value', 'Central', 'comment' ] data = {} data['action'] = 'add' data['object'] = 'RESOURCECFG' data['values'] = values with patch('requests.post') as patched_post: centreon_con.resourcecfgs.add(""resource_test"", ""ressource_value"", ""Central"", ""comment"", post_refresh=False ) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) def test_resourcecfg_delete(self, centreon_con): data = {} data['action'] = 'del' data['object'] = 'RESOURCECFG' data['values'] = '42' with patch('requests.post') as patched_post: centreon_con.resourcecfgs.delete('42', post_refresh=False) patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) @responses.activate def test_resourcecfg_setparam(self): with open(resource_dir / 'test_resourcecfg_1.json') as data: res = ResourceCFG(json.load(data)) values = [ res.id, 'instance', 'Central', ] data = {} data['action'] = 'setparam' data['object'] = 'RESOURCECFG' data['values'] = values with patch('requests.post') as patched_post: res.setparam('instance', 'Central') patched_post.assert_called_with(self.clapi_url, headers=self.headers, data=json.dumps(data), verify=True) ", https://github.com/gwpy/gwosc,06b56ce506fda3af4857c8a1aae7bb60fb1925e9,gwosc/tests/test_datasets.py::test_dataset_type_local,gwosc/tests/test_datasets.py,NIO,flaky,nan,nan,classify;root_cause,"# -*- coding: utf-8 -*- # Copyright (C) Cardiff University, 2018-2020 # # This file is part of GWOSC. # # GWOSC is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # GWOSC is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with GWOSC. If not, see . """"""Tests for :mod:`gwosc.datasets` """""" import re from unittest import mock import pytest from .. import datasets __author__ = 'Duncan Macleod ' DATASET_JSON = { 'events': { 'GW150914': {'GPStime': 12345, 'detectors': ['H1', 'L1']}, 'GW151226': {'GPStime': 12347, 'detectors': ['H1', 'L1']}, }, 'runs': { 'S1': {'GPSstart': 0, 'GPSend': 1, 'detectors': ['H1', 'L1', 'V1']}, 'tenyear': None, }, } CATALOG_JSON = { 'data': { 'GW150914': { 'files': { 'DataRevisionNum': 'R1', 'OperatingIFOs': ""H1 L1"", 'H1': {}, 'L1': {}, }, }, } } @pytest.mark.remote def test_find_datasets(): sets = datasets.find_datasets() for dset in ('S6', 'O1', 'GW150914-v1', 'GW170817-v3'): assert dset in sets assert 'tenyear' not in sets assert 'history' not in sets @pytest.mark.remote def test_find_datasets_detector(): v1sets = datasets.find_datasets('V1') assert 'GW170817-v3' in v1sets assert 'GW150914-v1' not in v1sets assert datasets.find_datasets('X1', type=""run"") == [] @pytest.mark.remote def test_find_datasets_type(): runsets = datasets.find_datasets(type='run') assert 'O1' in runsets run_regex = re.compile(r'\A([OS]\d+|BKGW\d{6})(_\d+KHZ)?(_[RV]\d+)?\Z') for dset in runsets: assert run_regex.match(dset) assert datasets.find_datasets(type='badtype') == [] @pytest.mark.remote def test_find_datasets_segment(): sets = datasets.find_datasets(segment=(1126051217, 1137254417)) assert ""GW150914-v1"" in sets assert ""GW170817"" not in sets @pytest.mark.remote def test_find_datasets_match(): assert ""O1"" not in datasets.find_datasets(match=""GW"") @pytest.mark.remote def test_find_datasets_event_version_detector(): # this raises a ValueError with gwosc-0.5.0 sets = datasets.find_datasets(type='event', version=1, detector='L1') assert ""GW150914-v1"" in sets assert ""GW150914-v3"" not in sets # v3 @pytest.mark.remote def test_event_gps(): assert datasets.event_gps('GW170817') == 1187008882.4 with pytest.raises(ValueError) as exc: datasets.event_gps('GW123456') assert str(exc.value) == 'no event dataset found for \'GW123456\'' @mock.patch( 'gwosc.api._fetch_allevents_event_json', return_value={""events"": {""GW150914"": { 'GPS': 12345, 'something else': None, }}}, ) def test_event_gps_local(fetch): assert datasets.event_gps('GW150914') == 12345 fetch.side_effect = ValueError('test') with pytest.raises(ValueError) as exc: datasets.event_gps('something') assert str(exc.value) == ""no event dataset found for 'something'"" @pytest.mark.remote def test_event_segment(): assert datasets.event_segment(""GW170817"") == (1187006835, 1187010931) @mock.patch( 'gwosc.api._fetch_allevents_event_json', mock.MagicMock(return_value={""events"": {""GW150914"": { ""GPS"": 12345, ""something else"": None, ""strain"": [ { ""GPSstart"": 0, ""duration"": 32, ""detector"": ""X1"", }, { ""GPSstart"": 10, ""duration"": 32, ""detector"": ""Y1"", }, ], }}}), ) def test_event_segment_local(): assert datasets.event_segment(""GW170817"") == (0, 42) assert datasets.event_segment(""GW170817"", detector=""Y1"") == (10, 42) @pytest.mark.remote def test_event_at_gps(): assert datasets.event_at_gps(1187008882) == 'GW170817' with pytest.raises(ValueError) as exc: datasets.event_at_gps(1187008882, tol=.1) assert str(exc.value) == 'no event found within 0.1 seconds of 1187008882' @mock.patch( 'gwosc.api.fetch_allevents_json', mock.MagicMock(return_value={""events"": { ""GW150914"": {""GPS"": 12345.5, ""commonName"": ""GW150914""}, ""GW150915"": {""GPS"": 12346.5, ""commonName"": ""GW150915""}, }}), ) def test_event_at_gps_local(): assert datasets.event_at_gps(12345) == 'GW150914' with pytest.raises(ValueError): datasets.event_at_gps(12349) @pytest.mark.remote def test_event_detectors(): assert datasets.event_detectors(""GW150914"") == {""H1"", ""L1""} assert datasets.event_detectors(""GW170814"") == {""H1"", ""L1"", ""V1""} @mock.patch( ""gwosc.api._fetch_allevents_event_json"", mock.MagicMock(return_value={ ""events"": {""test"": {""strain"": [ {""detector"": ""A1""}, {""detector"": ""B1""}, ]}}, }), ) def test_event_detectors_local(): assert datasets.event_detectors(""test"") == {""A1"", ""B1""} @pytest.mark.remote def test_run_segment(): assert datasets.run_segment('O1') == (1126051217, 1137254417) with pytest.raises(ValueError) as exc: datasets.run_segment('S7') assert str(exc.value) == 'no run dataset found for \'S7\'' @mock.patch( 'gwosc.api.fetch_dataset_json', mock.MagicMock(return_value=DATASET_JSON), ) def test_run_segment_local(): assert datasets.run_segment('S1') == (0, 1) with pytest.raises(ValueError): datasets.run_segment('S2') @pytest.mark.remote def test_run_at_gps(): assert datasets.run_at_gps(1135136350) in {'O1', 'O1_16KHZ'} with pytest.raises(ValueError) as exc: datasets.run_at_gps(0) assert str(exc.value) == 'no run dataset found containing GPS 0' @mock.patch( 'gwosc.api.fetch_dataset_json', mock.MagicMock(return_value=DATASET_JSON), ) def test_run_at_gps_local(): assert datasets.run_at_gps(0) == 'S1' with pytest.raises(ValueError): datasets.run_at_gps(10) @pytest.mark.remote def test_dataset_type(): assert datasets.dataset_type(""O1"") == ""run"" assert datasets.dataset_type(""GW150914-v1"") == ""event"" assert datasets.dataset_type(""GWTC-1-confident"") == ""catalog"" with pytest.raises(ValueError): datasets.dataset_type(""invalid"") @mock.patch( 'gwosc.datasets.find_datasets', mock.MagicMock(side_effect=[[""testrun""], [], [""testevent""], [], [], []]), ) def test_dataset_type_local(): assert datasets.dataset_type(""testevent"") == ""event"" with pytest.raises(ValueError): datasets.dataset_type(""invalid"") ", https://github.com/h2non/pook,2071da27701c82ce02b015e01e2aa6fd203e7bb5,tests/unit/api_test.py::test_activate,tests/unit/api_test.py,OD-Brit,flaky,Opened,https://github.com/h2non/pook/pull/169,classify;root_cause,"# -*- coding: utf-8 -*- import pytest from pook import api @pytest.fixture def engine(): return api.engine() def test_engine(engine): assert engine == api._engine def test_activate(engine): assert engine.active is False api.activate() assert engine.active is True api.disable() assert engine.active is False def test_on(engine): assert engine.active is False api.on() assert engine.active is True api.off() assert engine.active is False def test_use(engine): assert engine.active is False with api.use() as engine: assert engine.active is True assert engine.active is True assert engine.active is False def test_mock_contructors(engine): assert engine.active is False assert engine.isdone() is True api.mock('foo.com') assert engine.isdone() is False assert len(engine.mocks) == 1 api.off() assert len(engine.mocks) == 0 assert engine.active is False ", https://github.com/h2non/pook,2071da27701c82ce02b015e01e2aa6fd203e7bb5,tests/unit/api_test.py::test_mock_contructors,tests/unit/api_test.py,OD-Vic,flaky,Opened,https://github.com/h2non/pook/pull/169,classify;root_cause,"# -*- coding: utf-8 -*- import pytest from pook import api @pytest.fixture def engine(): return api.engine() def test_engine(engine): assert engine == api._engine def test_activate(engine): assert engine.active is False api.activate() assert engine.active is True api.disable() assert engine.active is False def test_on(engine): assert engine.active is False api.on() assert engine.active is True api.off() assert engine.active is False def test_use(engine): assert engine.active is False with api.use() as engine: assert engine.active is True assert engine.active is True assert engine.active is False def test_mock_contructors(engine): assert engine.active is False assert engine.isdone() is True api.mock('foo.com') assert engine.isdone() is False assert len(engine.mocks) == 1 api.off() assert len(engine.mocks) == 0 assert engine.active is False ", https://github.com/h2non/pook,2071da27701c82ce02b015e01e2aa6fd203e7bb5,tests/unit/api_test.py::test_on,tests/unit/api_test.py,OD-Brit,flaky,Opened,https://github.com/h2non/pook/pull/169,classify;root_cause,"# -*- coding: utf-8 -*- import pytest from pook import api @pytest.fixture def engine(): return api.engine() def test_engine(engine): assert engine == api._engine def test_activate(engine): assert engine.active is False api.activate() assert engine.active is True api.disable() assert engine.active is False def test_on(engine): assert engine.active is False api.on() assert engine.active is True api.off() assert engine.active is False def test_use(engine): assert engine.active is False with api.use() as engine: assert engine.active is True assert engine.active is True assert engine.active is False def test_mock_contructors(engine): assert engine.active is False assert engine.isdone() is True api.mock('foo.com') assert engine.isdone() is False assert len(engine.mocks) == 1 api.off() assert len(engine.mocks) == 0 assert engine.active is False ", https://github.com/h2non/pook,2071da27701c82ce02b015e01e2aa6fd203e7bb5,tests/unit/api_test.py::test_use,tests/unit/api_test.py,OD-Brit,flaky,Opened,https://github.com/h2non/pook/pull/169,classify;root_cause,"# -*- coding: utf-8 -*- import pytest from pook import api @pytest.fixture def engine(): return api.engine() def test_engine(engine): assert engine == api._engine def test_activate(engine): assert engine.active is False api.activate() assert engine.active is True api.disable() assert engine.active is False def test_on(engine): assert engine.active is False api.on() assert engine.active is True api.off() assert engine.active is False def test_use(engine): assert engine.active is False with api.use() as engine: assert engine.active is True assert engine.active is True assert engine.active is False def test_mock_contructors(engine): assert engine.active is False assert engine.isdone() is True api.mock('foo.com') assert engine.isdone() is False assert len(engine.mocks) == 1 api.off() assert len(engine.mocks) == 0 assert engine.active is False ", https://github.com/haney/python-ansel,afb2b28d8595beeb1f87e355b611f01e13b8e664,tests/test_incremental.py::test_decode_invalid_raising_error_handler[+],tests/test_incremental.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest import ansel.incremental from .conftest import EncodingError class IncrementalDecoder(ansel.incremental.IncrementalDecoder): name = ""test"" encode_char_map = {u""a"": b""1"", ""b"": b""23"", u""?"": b""?""} decode_char_map = {ord(b""a""): u""1"", ord(b""b""): u""23""} encode_modifier_map = {u""n"": b""5"", u""o"": b""67""} decode_modifier_map = {ord(b""n""): u""5"", ord(b""o""): u""67""} class IncrementalEncoder(ansel.incremental.IncrementalEncoder): name = ""test"" encode_char_map = {u""a"": b""1"", u""b"": b""23"", u""?"": b""?""} decode_char_map = {ord(b""a""): u""1"", ord(b""b""): u""23""} encode_modifier_map = {u""n"": b""5"", u""o"": b""67""} decode_modifier_map = {ord(b""n""): u""5"", ord(b""o""): u""67""} @pytest.mark.parametrize( ""input, state"", [ (u"""", 0), (u""a"", 0x131), (u""ab"", 0x13233), (u""n"", 0x135), (u""ao"", 0x1363731), (u""annn"", 0x135353531), (u""annnb"", 0x13233), ], ) def test_encode_getstate(input, state): encoder = IncrementalEncoder() encoder.encode(input) assert state == encoder.getstate() @pytest.mark.parametrize( ""state, input, expected"", [ (0, u""a"", b""1""), (0x131, u""b"", b""123""), (0x131, u""nb"", b""5123""), (0x1353531, u""nb"", b""555123""), ], ) def test_encode_setstate(state, input, expected): encoder = IncrementalEncoder() encoder.setstate(state) assert state == encoder.getstate() output = encoder.encode(input, final=True) assert expected == output assert 0 == encoder.getstate() @pytest.mark.parametrize( ""input, expected, expected_len"", [(u"""", b"""", 0), (u""a"", b""1"", 1), (u""b"", b""23"", 1), (u""ab"", b""123"", 2)], ) def test_encode_valid(input, expected, expected_len): encoder = IncrementalEncoder() output = encoder.encode(input, final=True) assert expected == output assert 0 == encoder.getstate() @pytest.mark.parametrize( ""partials"", [ [u""a""], [u""a"", u""b""], [u""ab"", u""""], [u""a"", u""nb""], [u""an"", u""nb""], [u""a"", u""n"", u""n"", u""b""], [u""n"", u""o""], ], ) def test_encode_incremental(partials): encoder = IncrementalEncoder() expected = encoder.encode("""".join(partials), final=True) actual = b"""".join(encoder.encode(partial) for partial in partials[0:-1]) actual += encoder.encode(partials[-1], final=True) assert expected == actual @pytest.mark.parametrize( ""input, expected, expected_len"", [ (u""n"", b""5"", 1), (u""na"", b""51"", 2), (u""nb"", b""523"", 2), (u""an"", b""51"", 2), (u""aan"", b""151"", 3), (u""ano"", b""6751"", 3), (u""bon"", b""56723"", 3), ], ) def test_encode_valid_with_modifiers(input, expected, expected_len): encoder = IncrementalEncoder() output = encoder.encode(input, final=True) assert expected == output assert 0 == encoder.getstate() @pytest.mark.parametrize( ""input, start, end, reason"", [ (u""+"", 0, 1, ""character maps to ""), (u""ab+"", 2, 3, ""character maps to ""), ], ) def test_encode_invalid(input, start, end, reason): encoder = IncrementalEncoder() with pytest.raises(UnicodeEncodeError) as exc_info: encoder.encode(input, final=True) assert ""test"" == exc_info.value.encoding assert reason == exc_info.value.reason assert input == exc_info.value.object assert start == exc_info.value.start assert end == exc_info.value.end assert 0 == encoder.getstate() @pytest.mark.parametrize(""input"", [u""+""]) def test_encode_invalid_raising_error_handler(error_handler, input): encoder = IncrementalEncoder(errors=""raises"") with pytest.raises(EncodingError): encoder.encode(input, final=True) assert 0 == encoder.getstate() @pytest.mark.parametrize( ""input, expected, expected_len"", [(u""+"", b""?"", 1), (u""a+b"", b""1?23"", 3), (u""a+n"", b""15?"", 3)], ) def test_encode_invalid_with_replacement(input, expected, expected_len): encoder = IncrementalEncoder(errors=""replace"") output = encoder.encode(input, final=True) assert expected == output assert 0 == encoder.getstate() @pytest.mark.parametrize( ""input, state"", [ (b"""", (b"""", 0)), (b""a"", (b"""", 0)), (b""n"", (b"""", 0x1000035)), (b""no"", (b"""", 0x1000036000037000035)), (b""nob"", (b"""", 0)), (b""ano"", (b"""", 0x1000036000037000035)), ], ) def test_decode_getstate(input, state): decoder = IncrementalDecoder() decoder.decode(input) assert state == decoder.getstate() @pytest.mark.parametrize( ""state, input, expected"", [ ((b"""", 0), b"""", u""""), ((b"""", 0x1000035), b""a"", u""15""), ((b"""", 0x1000035), b""na"", u""155""), ((b"""", 0x1000036000037), b""na"", u""1567""), ], ) def test_decode_setstate(state, input, expected): decoder = IncrementalDecoder() decoder.setstate(state) assert state == decoder.getstate() output = decoder.decode(input, final=True) assert expected == output assert (b"""", 0) == decoder.getstate() @pytest.mark.parametrize( ""input, expected, expected_len"", [(b"""", u"""", 0), (b""a"", u""1"", 1), (b""b"", u""23"", 1), (b""ab"", u""123"", 2)], ) def test_decode_valid(input, expected, expected_len): decoder = IncrementalDecoder() output = decoder.decode(input) assert expected == output assert (b"""", 0) == decoder.getstate() @pytest.mark.parametrize( ""partials"", [ [b""a""], [b""a"", b""b""], [b""ab"", b""""], [b""an"", b""b""], [b""an"", b""nb""], [b""a"", b""n"", b""n"", b""b""], [b""n"", b""o""], ], ) def test_decode_incremental(partials): decoder = IncrementalDecoder() expected = decoder.decode(b"""".join(partials), final=True) actual = """".join(decoder.decode(partial) for partial in partials[0:-1]) actual += decoder.decode(partials[-1], final=True) assert expected == actual @pytest.mark.parametrize( ""input, expected, expected_len"", [ (b""n"", u""5"", 1), (b""an"", u""15"", 2), (b""bn"", u""235"", 2), (b""na"", u""15"", 2), (b""naa"", u""151"", 3), (b""noa"", u""1675"", 3), (b""onb"", u""23567"", 3), ], ) def test_decode_valid_with_modifiers(input, expected, expected_len): decoder = IncrementalDecoder() output = decoder.decode(input, final=True) assert expected == output assert (b"""", 0) == decoder.getstate() @pytest.mark.parametrize( ""input, start, end, reason"", [ (b""+"", 0, 1, ""character maps to ""), (b""ab+"", 2, 3, ""character maps to ""), ], ) def test_decode_invalid(input, start, end, reason): decoder = IncrementalDecoder() with pytest.raises(UnicodeDecodeError) as exc_info: decoder.decode(input) assert ""test"" == exc_info.value.encoding assert reason == exc_info.value.reason assert input == exc_info.value.object assert start == exc_info.value.start assert end == exc_info.value.end assert (b"""", 0) == decoder.getstate() @pytest.mark.parametrize(""input"", [b""+""]) def test_decode_invalid_raising_error_handler(input): decoder = IncrementalDecoder(errors=""raises"") with pytest.raises(EncodingError): decoder.decode(input) assert (b"""", 0) == decoder.getstate() @pytest.mark.parametrize( ""input, expected, expected_len"", [(b""+"", u""\uFFFD"", 1), (b""a+b"", u""1\uFFFD23"", 3), (b""an+"", u""1\uFFFD5"", 3)], ) def test_decode_invalid_with_replacement(input, expected, expected_len): decoder = IncrementalDecoder(errors=""replace"") output = decoder.decode(input) assert expected == output assert (b"""", 0) == decoder.getstate() ", https://github.com/hbradleyiii/ext_pylib,15a9b5a80db87b5f20e03ef6bfa015acf4bf8543,"tests/files/test_node.py::test_node_repr[atts0-{'path' : None, 'group' : None}]",tests/files/test_node.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- # # name: test_node.py # author: Harold Bradley III # email: harold@bradleystudio.net # created on: 11/08/2015 # pylint: disable=invalid-name,line-too-long """""" A unit test for ext_pylib file module's Node class and methods. """""" import pytest from mock import patch from ext_pylib.files.node import Node from ext_pylib.user import get_current_username, get_current_groupname CURRENT_USER = get_current_username() CURRENT_GROUP = get_current_groupname() DEFUALT_ATTS = {'path' : '/etc/path/file'} INIT_ARGS = [ ({'path' : None}, {'path' : '', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this/path/file'}, {'path' : '/this/path/file', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this/path/'}, {'path' : '/this/path/', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this//path/'}, {'path' : '/this/path/', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this//path//'}, {'path' : '/this/path/', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this///path////'}, {'path' : '/this/path/', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/etc/path/file'}, {'path' : '/etc/path/file', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/etc/path/file'}, {'path' : '/etc/path/file', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/etc/path/file', 'perms' : 0o655}, {'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : None, 'group' : None}), ({'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}, {'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}), ] @pytest.mark.parametrize((""atts"", ""expected""), INIT_ARGS) def test_node_initialize(atts, expected): """"""Test initialize Node."""""" node = Node(atts) assert str(node) == expected['path'] assert node.perms == expected['perms'] assert node.owner == expected['owner'] assert node.group == expected['group'] REPR_ARGS = [ ({'path' : None}, ""{'path' : None, 'perms' : None, 'owner' : None, 'group' : None}""), ({'path' : '/this/path/file'}, ""{'path' : '/this/path/file', 'perms' : None, 'owner' : None, 'group' : None}""), ({'path' : '/etc/path/file', 'perms' : 0o655}, ""{'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : None, 'group' : None}""), ({'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}, ""{'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}""), ({'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}, ""{'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}""), ] @pytest.mark.parametrize((""atts"", ""expected""), REPR_ARGS) def test_node_repr(atts, expected): """"""Test Node repr."""""" node = Node(atts) assert node.__repr__() == ""Node("" + expected + "")"" @pytest.mark.parametrize((""atts"", ""expected""), REPR_ARGS) def test_node_get_atts(atts, expected): """"""Test Node get_atts method."""""" node = Node(atts) if 'perms' not in atts: atts['perms'] = None if 'owner' not in atts: atts['owner'] = None if 'group' not in atts: atts['group'] = None assert node.get_atts(string=True) == expected assert node.get_atts(string=False) == atts CONCAT_ARGS = [ ({'path' : None}, ''), ({'path' : '/this/path/'}, '/this/path/'), ({'path' : '/etc/path/file'}, '/etc/path/file'), ({'path' : 'relative/path/file'}, 'relative/path/file'), ] @pytest.mark.parametrize((""atts"", ""expected""), CONCAT_ARGS) def test_node_concatenate(atts, expected): """"""Test concatenate Node objects."""""" node = Node(atts) assert node + 'string' == expected + 'string' assert 'string' + node == 'string' + expected def test_node_create(): """"""Test that Node throws an error when calling create()."""""" node = Node({'path' : '/the/path'}) with pytest.raises(NotImplementedError): node.create() def test_node_remove(): """"""Test that Node throws an error when calling remove()."""""" node = Node({'path' : '/the/path'}) with pytest.raises(NotImplementedError): node.remove() VERIFY_ARGS = [ ({'path' : None}), ({'path' : '/this/path/file'}), ({'path' : '/this/path/', 'perms' : 0o655}), ({'path' : '/this/path/', 'owner' : 'root'}), ({'path' : '/this/path/', 'group' : 'root'}), ({'path' : '/this/path/', 'perms' : 0o655, 'owner' : 'root'}), ({'path' : '/this/path/', 'perms' : 0o655, 'group' : 'root'}), ({'path' : '/this/path/', 'owner' : 'root', 'group' : 'root'}), ({'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}), ({'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}), ] @pytest.mark.parametrize((""atts""), VERIFY_ARGS) @patch('ext_pylib.files.node.Node.actual_group') @patch('ext_pylib.files.node.Node.actual_owner') @patch('ext_pylib.files.node.Node.actual_perms') @patch('ext_pylib.files.node.Node.exists') def test_node_verify(mock_exists, mock_actual_perms, mock_actual_owner, mock_actual_group, atts): """"""Test Node's method verify."""""" # Test passing verification mock_exists.return_value = True node = Node(atts) node.actual_perms = None if 'perms' not in atts else atts['perms'] node.actual_owner = None if 'owner' not in atts else atts['owner'] node.actual_group = None if 'group' not in atts else atts['group'] assert node.verify(False) # Test failing verification if atts['path']: # If there is no path, it's a stub and should always verify true. mock_exists.return_value = False mock_actual_perms.return_value = None mock_actual_owner.return_value = None mock_actual_group.return_value = None bad_node = Node(atts) assert not bad_node.verify(False) @patch('ext_pylib.files.node.Node.verify') def test_node_repair(mock_verify): """"""Test that repair() method calls verify(True)."""""" node = Node() node.repair() mock_verify.assert_called_once_with(True) CHMOD_ARGS = [ ({'path' : None, 'perms' : 0o600}, True), ({'path' : '/this/path/file', 'perms' : 0o700}, True), ({'path' : '/this/path/file'}, True), ({'path' : 'relative/path/file'}, True), ] @pytest.mark.parametrize((""atts"", ""expected""), CHMOD_ARGS) @patch('ext_pylib.files.node.Node.exists') @patch('os.chmod') def test_node_chmod(mock_chmod, mock_path_exists, atts, expected): """"""Tests Node's chmod method."""""" mock_path_exists.return_value = True # Assume this is working for this test node = Node(atts) assert expected == node.chmod() if atts['path'] and node.perms: mock_chmod.assert_called_once_with(atts['path'], atts['perms']) else: assert not mock_chmod.called @patch('ext_pylib.files.node.Node.exists') def test_node_chmod_nonexisting(mock_path_exists): """"""Tests Node's chown method with a nonexisting node."""""" mock_path_exists.return_value = False node = Node(DEFUALT_ATTS) with pytest.raises(IOError): node.chmod() CHOWN_ARGS = [ ({'path' : None, 'owner' : 'www-data', 'group' : 'root'}, True), ({'path' : '/this/path/file', 'owner' : 'www-data', 'group' : 'root'}, True), ({'path' : '/this/path/file', 'owner' : None, 'group' : 'root'}, True), ({'path' : '/this/path/file', 'owner' : 'www-data', 'group' : None}, True), ({'path' : '/this/path/file', 'owner' : None, 'group' : None}, True), ({'path' : '/this/path/file'}, True), ({'path' : '/this/path/file', 'owner' : 'www-data'}, True), ({'path' : '/this/path/file', 'group' : 'www-data'}, True), ({'path' : '/this/path/file', 'owner' : None, 'group' : 'www-data'}, True), ({'path' : 'relative/path/file', 'owner' : None, 'group' : 'www-data'}, True), ] @pytest.mark.parametrize((""atts"", ""expected""), CHOWN_ARGS) @patch('ext_pylib.files.node.Node.exists') @patch('pwd.getpwnam') @patch('grp.getgrnam') @patch('os.chown') def test_node_chown(mock_chown, mock_getgrnam, mock_getpwnam, mock_path_exists, atts, expected): # pylint: disable=too-many-arguments """"""Tests Node's chown method."""""" node = Node(atts) if 'owner' not in atts: atts['owner'] = CURRENT_USER if 'group' not in atts: atts['group'] = 'nogroup' if atts['owner'] == 'nobody' else CURRENT_GROUP mock_path_exists.return_value = True # Assume this is working for this test mock_getpwnam(atts['owner']).pw_uid = 123 # Just a number to use for mocking mock_getgrnam(atts['group']).gr_gid = 123 assert expected == node.chown() if atts['path'] is not None: mock_getpwnam.assert_called_with(CURRENT_USER if not atts['owner'] else atts['owner']) mock_getgrnam.assert_called_with(CURRENT_GROUP if not atts['group'] else atts['group']) mock_chown.assert_called_once_with(atts['path'], 123, 123) @patch('ext_pylib.files.node.Node.exists') def test_node_chown_nonexisting(mock_path_exists): """"""Tests Node's chown method with a nonexisting node."""""" mock_path_exists.return_value = False node = Node(DEFUALT_ATTS) with pytest.raises(IOError): node.chown() EXISTS_ARGS = [ ({'path' : None}, False), ({'path' : '/this/path/file'}, False), ({'path' : '/this/path/file'}, True), ({'path' : 'relative/path/file'}, True), ] @pytest.mark.parametrize((""atts"", ""expected""), EXISTS_ARGS) @patch('os.path.exists') def test_node_exists(mock_path_exists, atts, expected): """"""Tests node's exist method."""""" mock_path_exists.return_value = expected node = Node(atts) assert expected == node.exists() if atts['path'] is not None: mock_path_exists.assert_called_once_with(atts['path']) def test_node_set_path_empty():", https://github.com/hbradleyiii/ext_pylib,15a9b5a80db87b5f20e03ef6bfa015acf4bf8543,"tests/files/test_node.py::test_node_repr[atts1-{'path' : '/this/path/file', 'group' : None}]",tests/files/test_node.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- # # name: test_node.py # author: Harold Bradley III # email: harold@bradleystudio.net # created on: 11/08/2015 # pylint: disable=invalid-name,line-too-long """""" A unit test for ext_pylib file module's Node class and methods. """""" import pytest from mock import patch from ext_pylib.files.node import Node from ext_pylib.user import get_current_username, get_current_groupname CURRENT_USER = get_current_username() CURRENT_GROUP = get_current_groupname() DEFUALT_ATTS = {'path' : '/etc/path/file'} INIT_ARGS = [ ({'path' : None}, {'path' : '', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this/path/file'}, {'path' : '/this/path/file', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this/path/'}, {'path' : '/this/path/', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this//path/'}, {'path' : '/this/path/', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this//path//'}, {'path' : '/this/path/', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this///path////'}, {'path' : '/this/path/', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/etc/path/file'}, {'path' : '/etc/path/file', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/etc/path/file'}, {'path' : '/etc/path/file', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/etc/path/file', 'perms' : 0o655}, {'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : None, 'group' : None}), ({'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}, {'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}), ] @pytest.mark.parametrize((""atts"", ""expected""), INIT_ARGS) def test_node_initialize(atts, expected): """"""Test initialize Node."""""" node = Node(atts) assert str(node) == expected['path'] assert node.perms == expected['perms'] assert node.owner == expected['owner'] assert node.group == expected['group'] REPR_ARGS = [ ({'path' : None}, ""{'path' : None, 'perms' : None, 'owner' : None, 'group' : None}""), ({'path' : '/this/path/file'}, ""{'path' : '/this/path/file', 'perms' : None, 'owner' : None, 'group' : None}""), ({'path' : '/etc/path/file', 'perms' : 0o655}, ""{'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : None, 'group' : None}""), ({'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}, ""{'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}""), ({'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}, ""{'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}""), ] @pytest.mark.parametrize((""atts"", ""expected""), REPR_ARGS) def test_node_repr(atts, expected): """"""Test Node repr."""""" node = Node(atts) assert node.__repr__() == ""Node("" + expected + "")"" @pytest.mark.parametrize((""atts"", ""expected""), REPR_ARGS) def test_node_get_atts(atts, expected): """"""Test Node get_atts method."""""" node = Node(atts) if 'perms' not in atts: atts['perms'] = None if 'owner' not in atts: atts['owner'] = None if 'group' not in atts: atts['group'] = None assert node.get_atts(string=True) == expected assert node.get_atts(string=False) == atts CONCAT_ARGS = [ ({'path' : None}, ''), ({'path' : '/this/path/'}, '/this/path/'), ({'path' : '/etc/path/file'}, '/etc/path/file'), ({'path' : 'relative/path/file'}, 'relative/path/file'), ] @pytest.mark.parametrize((""atts"", ""expected""), CONCAT_ARGS) def test_node_concatenate(atts, expected): """"""Test concatenate Node objects."""""" node = Node(atts) assert node + 'string' == expected + 'string' assert 'string' + node == 'string' + expected def test_node_create(): """"""Test that Node throws an error when calling create()."""""" node = Node({'path' : '/the/path'}) with pytest.raises(NotImplementedError): node.create() def test_node_remove(): """"""Test that Node throws an error when calling remove()."""""" node = Node({'path' : '/the/path'}) with pytest.raises(NotImplementedError): node.remove() VERIFY_ARGS = [ ({'path' : None}), ({'path' : '/this/path/file'}), ({'path' : '/this/path/', 'perms' : 0o655}), ({'path' : '/this/path/', 'owner' : 'root'}), ({'path' : '/this/path/', 'group' : 'root'}), ({'path' : '/this/path/', 'perms' : 0o655, 'owner' : 'root'}), ({'path' : '/this/path/', 'perms' : 0o655, 'group' : 'root'}), ({'path' : '/this/path/', 'owner' : 'root', 'group' : 'root'}), ({'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}), ({'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}), ] @pytest.mark.parametrize((""atts""), VERIFY_ARGS) @patch('ext_pylib.files.node.Node.actual_group') @patch('ext_pylib.files.node.Node.actual_owner') @patch('ext_pylib.files.node.Node.actual_perms') @patch('ext_pylib.files.node.Node.exists') def test_node_verify(mock_exists, mock_actual_perms, mock_actual_owner, mock_actual_group, atts): """"""Test Node's method verify."""""" # Test passing verification mock_exists.return_value = True node = Node(atts) node.actual_perms = None if 'perms' not in atts else atts['perms'] node.actual_owner = None if 'owner' not in atts else atts['owner'] node.actual_group = None if 'group' not in atts else atts['group'] assert node.verify(False) # Test failing verification if atts['path']: # If there is no path, it's a stub and should always verify true. mock_exists.return_value = False mock_actual_perms.return_value = None mock_actual_owner.return_value = None mock_actual_group.return_value = None bad_node = Node(atts) assert not bad_node.verify(False) @patch('ext_pylib.files.node.Node.verify') def test_node_repair(mock_verify): """"""Test that repair() method calls verify(True)."""""" node = Node() node.repair() mock_verify.assert_called_once_with(True) CHMOD_ARGS = [ ({'path' : None, 'perms' : 0o600}, True), ({'path' : '/this/path/file', 'perms' : 0o700}, True), ({'path' : '/this/path/file'}, True), ({'path' : 'relative/path/file'}, True), ] @pytest.mark.parametrize((""atts"", ""expected""), CHMOD_ARGS) @patch('ext_pylib.files.node.Node.exists') @patch('os.chmod') def test_node_chmod(mock_chmod, mock_path_exists, atts, expected): """"""Tests Node's chmod method."""""" mock_path_exists.return_value = True # Assume this is working for this test node = Node(atts) assert expected == node.chmod() if atts['path'] and node.perms: mock_chmod.assert_called_once_with(atts['path'], atts['perms']) else: assert not mock_chmod.called @patch('ext_pylib.files.node.Node.exists') def test_node_chmod_nonexisting(mock_path_exists): """"""Tests Node's chown method with a nonexisting node."""""" mock_path_exists.return_value = False node = Node(DEFUALT_ATTS) with pytest.raises(IOError): node.chmod() CHOWN_ARGS = [ ({'path' : None, 'owner' : 'www-data', 'group' : 'root'}, True), ({'path' : '/this/path/file', 'owner' : 'www-data', 'group' : 'root'}, True), ({'path' : '/this/path/file', 'owner' : None, 'group' : 'root'}, True), ({'path' : '/this/path/file', 'owner' : 'www-data', 'group' : None}, True), ({'path' : '/this/path/file', 'owner' : None, 'group' : None}, True), ({'path' : '/this/path/file'}, True), ({'path' : '/this/path/file', 'owner' : 'www-data'}, True), ({'path' : '/this/path/file', 'group' : 'www-data'}, True), ({'path' : '/this/path/file', 'owner' : None, 'group' : 'www-data'}, True), ({'path' : 'relative/path/file', 'owner' : None, 'group' : 'www-data'}, True), ] @pytest.mark.parametrize((""atts"", ""expected""), CHOWN_ARGS) @patch('ext_pylib.files.node.Node.exists') @patch('pwd.getpwnam') @patch('grp.getgrnam') @patch('os.chown') def test_node_chown(mock_chown, mock_getgrnam, mock_getpwnam, mock_path_exists, atts, expected): # pylint: disable=too-many-arguments """"""Tests Node's chown method."""""" node = Node(atts) if 'owner' not in atts: atts['owner'] = CURRENT_USER if 'group' not in atts: atts['group'] = 'nogroup' if atts['owner'] == 'nobody' else CURRENT_GROUP mock_path_exists.return_value = True # Assume this is working for this test mock_getpwnam(atts['owner']).pw_uid = 123 # Just a number to use for mocking mock_getgrnam(atts['group']).gr_gid = 123 assert expected == node.chown() if atts['path'] is not None: mock_getpwnam.assert_called_with(CURRENT_USER if not atts['owner'] else atts['owner']) mock_getgrnam.assert_called_with(CURRENT_GROUP if not atts['group'] else atts['group']) mock_chown.assert_called_once_with(atts['path'], 123, 123) @patch('ext_pylib.files.node.Node.exists') def test_node_chown_nonexisting(mock_path_exists): """"""Tests Node's chown method with a nonexisting node."""""" mock_path_exists.return_value = False node = Node(DEFUALT_ATTS) with pytest.raises(IOError): node.chown() EXISTS_ARGS = [ ({'path' : None}, False), ({'path' : '/this/path/file'}, False), ({'path' : '/this/path/file'}, True), ({'path' : 'relative/path/file'}, True), ] @pytest.mark.parametrize((""atts"", ""expected""), EXISTS_ARGS) @patch('os.path.exists') def test_node_exists(mock_path_exists, atts, expected): """"""Tests node's exist method."""""" mock_path_exists.return_value = expected node = Node(atts) assert expected == node.exists() if atts['path'] is not None: mock_path_exists.assert_called_once_with(atts['path']) def test_node_set_path_empty():", https://github.com/hbradleyiii/ext_pylib,15a9b5a80db87b5f20e03ef6bfa015acf4bf8543,"tests/files/test_node.py::test_node_repr[atts2-{'path' : '/etc/path/file', 'group' : None}]",tests/files/test_node.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- # # name: test_node.py # author: Harold Bradley III # email: harold@bradleystudio.net # created on: 11/08/2015 # pylint: disable=invalid-name,line-too-long """""" A unit test for ext_pylib file module's Node class and methods. """""" import pytest from mock import patch from ext_pylib.files.node import Node from ext_pylib.user import get_current_username, get_current_groupname CURRENT_USER = get_current_username() CURRENT_GROUP = get_current_groupname() DEFUALT_ATTS = {'path' : '/etc/path/file'} INIT_ARGS = [ ({'path' : None}, {'path' : '', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this/path/file'}, {'path' : '/this/path/file', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this/path/'}, {'path' : '/this/path/', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this//path/'}, {'path' : '/this/path/', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this//path//'}, {'path' : '/this/path/', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/this///path////'}, {'path' : '/this/path/', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/etc/path/file'}, {'path' : '/etc/path/file', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/etc/path/file'}, {'path' : '/etc/path/file', 'perms' : None, 'owner' : None, 'group' : None}), ({'path' : '/etc/path/file', 'perms' : 0o655}, {'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : None, 'group' : None}), ({'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}, {'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}), ] @pytest.mark.parametrize((""atts"", ""expected""), INIT_ARGS) def test_node_initialize(atts, expected): """"""Test initialize Node."""""" node = Node(atts) assert str(node) == expected['path'] assert node.perms == expected['perms'] assert node.owner == expected['owner'] assert node.group == expected['group'] REPR_ARGS = [ ({'path' : None}, ""{'path' : None, 'perms' : None, 'owner' : None, 'group' : None}""), ({'path' : '/this/path/file'}, ""{'path' : '/this/path/file', 'perms' : None, 'owner' : None, 'group' : None}""), ({'path' : '/etc/path/file', 'perms' : 0o655}, ""{'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : None, 'group' : None}""), ({'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}, ""{'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}""), ({'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}, ""{'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}""), ] @pytest.mark.parametrize((""atts"", ""expected""), REPR_ARGS) def test_node_repr(atts, expected): """"""Test Node repr."""""" node = Node(atts) assert node.__repr__() == ""Node("" + expected + "")"" @pytest.mark.parametrize((""atts"", ""expected""), REPR_ARGS) def test_node_get_atts(atts, expected): """"""Test Node get_atts method."""""" node = Node(atts) if 'perms' not in atts: atts['perms'] = None if 'owner' not in atts: atts['owner'] = None if 'group' not in atts: atts['group'] = None assert node.get_atts(string=True) == expected assert node.get_atts(string=False) == atts CONCAT_ARGS = [ ({'path' : None}, ''), ({'path' : '/this/path/'}, '/this/path/'), ({'path' : '/etc/path/file'}, '/etc/path/file'), ({'path' : 'relative/path/file'}, 'relative/path/file'), ] @pytest.mark.parametrize((""atts"", ""expected""), CONCAT_ARGS) def test_node_concatenate(atts, expected): """"""Test concatenate Node objects."""""" node = Node(atts) assert node + 'string' == expected + 'string' assert 'string' + node == 'string' + expected def test_node_create(): """"""Test that Node throws an error when calling create()."""""" node = Node({'path' : '/the/path'}) with pytest.raises(NotImplementedError): node.create() def test_node_remove(): """"""Test that Node throws an error when calling remove()."""""" node = Node({'path' : '/the/path'}) with pytest.raises(NotImplementedError): node.remove() VERIFY_ARGS = [ ({'path' : None}), ({'path' : '/this/path/file'}), ({'path' : '/this/path/', 'perms' : 0o655}), ({'path' : '/this/path/', 'owner' : 'root'}), ({'path' : '/this/path/', 'group' : 'root'}), ({'path' : '/this/path/', 'perms' : 0o655, 'owner' : 'root'}), ({'path' : '/this/path/', 'perms' : 0o655, 'group' : 'root'}), ({'path' : '/this/path/', 'owner' : 'root', 'group' : 'root'}), ({'path' : '/etc/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}), ({'path' : 'relative/path/file', 'perms' : 0o655, 'owner' : 'root', 'group' : 'root'}), ] @pytest.mark.parametrize((""atts""), VERIFY_ARGS) @patch('ext_pylib.files.node.Node.actual_group') @patch('ext_pylib.files.node.Node.actual_owner') @patch('ext_pylib.files.node.Node.actual_perms') @patch('ext_pylib.files.node.Node.exists') def test_node_verify(mock_exists, mock_actual_perms, mock_actual_owner, mock_actual_group, atts): """"""Test Node's method verify."""""" # Test passing verification mock_exists.return_value = True node = Node(atts) node.actual_perms = None if 'perms' not in atts else atts['perms'] node.actual_owner = None if 'owner' not in atts else atts['owner'] node.actual_group = None if 'group' not in atts else atts['group'] assert node.verify(False) # Test failing verification if atts['path']: # If there is no path, it's a stub and should always verify true. mock_exists.return_value = False mock_actual_perms.return_value = None mock_actual_owner.return_value = None mock_actual_group.return_value = None bad_node = Node(atts) assert not bad_node.verify(False) @patch('ext_pylib.files.node.Node.verify') def test_node_repair(mock_verify): """"""Test that repair() method calls verify(True)."""""" node = Node() node.repair() mock_verify.assert_called_once_with(True) CHMOD_ARGS = [ ({'path' : None, 'perms' : 0o600}, True), ({'path' : '/this/path/file', 'perms' : 0o700}, True), ({'path' : '/this/path/file'}, True), ({'path' : 'relative/path/file'}, True), ] @pytest.mark.parametrize((""atts"", ""expected""), CHMOD_ARGS) @patch('ext_pylib.files.node.Node.exists') @patch('os.chmod') def test_node_chmod(mock_chmod, mock_path_exists, atts, expected): """"""Tests Node's chmod method."""""" mock_path_exists.return_value = True # Assume this is working for this test node = Node(atts) assert expected == node.chmod() if atts['path'] and node.perms: mock_chmod.assert_called_once_with(atts['path'], atts['perms']) else: assert not mock_chmod.called @patch('ext_pylib.files.node.Node.exists') def test_node_chmod_nonexisting(mock_path_exists): """"""Tests Node's chown method with a nonexisting node."""""" mock_path_exists.return_value = False node = Node(DEFUALT_ATTS) with pytest.raises(IOError): node.chmod() CHOWN_ARGS = [ ({'path' : None, 'owner' : 'www-data', 'group' : 'root'}, True), ({'path' : '/this/path/file', 'owner' : 'www-data', 'group' : 'root'}, True), ({'path' : '/this/path/file', 'owner' : None, 'group' : 'root'}, True), ({'path' : '/this/path/file', 'owner' : 'www-data', 'group' : None}, True), ({'path' : '/this/path/file', 'owner' : None, 'group' : None}, True), ({'path' : '/this/path/file'}, True), ({'path' : '/this/path/file', 'owner' : 'www-data'}, True), ({'path' : '/this/path/file', 'group' : 'www-data'}, True), ({'path' : '/this/path/file', 'owner' : None, 'group' : 'www-data'}, True), ({'path' : 'relative/path/file', 'owner' : None, 'group' : 'www-data'}, True), ] @pytest.mark.parametrize((""atts"", ""expected""), CHOWN_ARGS) @patch('ext_pylib.files.node.Node.exists') @patch('pwd.getpwnam') @patch('grp.getgrnam') @patch('os.chown') def test_node_chown(mock_chown, mock_getgrnam, mock_getpwnam, mock_path_exists, atts, expected): # pylint: disable=too-many-arguments """"""Tests Node's chown method."""""" node = Node(atts) if 'owner' not in atts: atts['owner'] = CURRENT_USER if 'group' not in atts: atts['group'] = 'nogroup' if atts['owner'] == 'nobody' else CURRENT_GROUP mock_path_exists.return_value = True # Assume this is working for this test mock_getpwnam(atts['owner']).pw_uid = 123 # Just a number to use for mocking mock_getgrnam(atts['group']).gr_gid = 123 assert expected == node.chown() if atts['path'] is not None: mock_getpwnam.assert_called_with(CURRENT_USER if not atts['owner'] else atts['owner']) mock_getgrnam.assert_called_with(CURRENT_GROUP if not atts['group'] else atts['group']) mock_chown.assert_called_once_with(atts['path'], 123, 123) @patch('ext_pylib.files.node.Node.exists') def test_node_chown_nonexisting(mock_path_exists): """"""Tests Node's chown method with a nonexisting node."""""" mock_path_exists.return_value = False node = Node(DEFUALT_ATTS) with pytest.raises(IOError): node.chown() EXISTS_ARGS = [ ({'path' : None}, False), ({'path' : '/this/path/file'}, False), ({'path' : '/this/path/file'}, True), ({'path' : 'relative/path/file'}, True), ] @pytest.mark.parametrize((""atts"", ""expected""), EXISTS_ARGS) @patch('os.path.exists') def test_node_exists(mock_path_exists, atts, expected): """"""Tests node's exist method."""""" mock_path_exists.return_value = expected node = Node(atts) assert expected == node.exists() if atts['path'] is not None: mock_path_exists.assert_called_once_with(atts['path']) def test_node_set_path_empty():", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,examples/test_gen_each_case.py::TestUnittest::test_foo_bar,examples/test_gen_each_case.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_unittest ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Autodoc for UnitTest. :copyright: (c) 2014-2015 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" from unittest import TestCase from webtest import TestApp from autodoc import autodoc from tests.app import create_app class TestUnittest(TestCase): def setUp(self): app = create_app self.client = TestApp(app) @classmethod def tearDownClass(cls): pass @autodoc.generate('var/test_get.rst') @autodoc.describe('GET /') def test_get(self): """""" GET / """""" res = self.client.get('/') self.assertEqual(res.status_code, 200) return res @autodoc.generate('var/test_post.rst') @autodoc.describe('POST /') def test_post(self): """""" POST / """""" res = self.client.post_json('/', params={'id': 1, 'message': 'foo'}) self.assertEqual(res.status_code, 200) return res @autodoc.generate('var/test_foo_bar.rst') @autodoc.describe('POST /foo/bar') def test_foo_bar(self): """""" POST /foo/bar """""" res = self.client.post_json('/foo/bar', params={ 'id': 1, 'message': 'foo' }) self.assertEqual(res.status_code, 200) return res ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,examples/test_gen_each_case.py::TestUnittest::test_get,examples/test_gen_each_case.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_unittest ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Autodoc for UnitTest. :copyright: (c) 2014-2015 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" from unittest import TestCase from webtest import TestApp from autodoc import autodoc from tests.app import create_app class TestUnittest(TestCase): def setUp(self): app = create_app self.client = TestApp(app) @classmethod def tearDownClass(cls): pass @autodoc.generate('var/test_get.rst') @autodoc.describe('GET /') def test_get(self): """""" GET / """""" res = self.client.get('/') self.assertEqual(res.status_code, 200) return res @autodoc.generate('var/test_post.rst') @autodoc.describe('POST /') def test_post(self): """""" POST / """""" res = self.client.post_json('/', params={'id': 1, 'message': 'foo'}) self.assertEqual(res.status_code, 200) return res @autodoc.generate('var/test_foo_bar.rst') @autodoc.describe('POST /foo/bar') def test_foo_bar(self): """""" POST /foo/bar """""" res = self.client.post_json('/foo/bar', params={ 'id': 1, 'message': 'foo' }) self.assertEqual(res.status_code, 200) return res ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,examples/test_gen_each_case.py::TestUnittest::test_post,examples/test_gen_each_case.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_unittest ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Autodoc for UnitTest. :copyright: (c) 2014-2015 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" from unittest import TestCase from webtest import TestApp from autodoc import autodoc from tests.app import create_app class TestUnittest(TestCase): def setUp(self): app = create_app self.client = TestApp(app) @classmethod def tearDownClass(cls): pass @autodoc.generate('var/test_get.rst') @autodoc.describe('GET /') def test_get(self): """""" GET / """""" res = self.client.get('/') self.assertEqual(res.status_code, 200) return res @autodoc.generate('var/test_post.rst') @autodoc.describe('POST /') def test_post(self): """""" POST / """""" res = self.client.post_json('/', params={'id': 1, 'message': 'foo'}) self.assertEqual(res.status_code, 200) return res @autodoc.generate('var/test_foo_bar.rst') @autodoc.describe('POST /foo/bar') def test_foo_bar(self): """""" POST /foo/bar """""" res = self.client.post_json('/foo/bar', params={ 'id': 1, 'message': 'foo' }) self.assertEqual(res.status_code, 200) return res ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,examples/test_pytest.py::test_index,examples/test_pytest.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.tests_pytest ~~~~~~~~~~~~~~~~~~~~~~~~~~ Autodoc for PyTest. :copyright: (c) 2014-2015 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" import sys from webtest import TestApp from autodoc import autodoc from tests.app import create_app try: import pytest except ImportError: sys.exit(1) @pytest.fixture def setup(): setup = TestApp(create_app) return setup @autodoc.generate('var/test_pytest.md', template='templates/markdown.md') def teardown_module(module): pass @autodoc.describe('GET /') def test_index(): app = TestApp(create_app) res = app.get('/') assert res.status_code == 200 return res @autodoc.describe('POST /') def test_post(setup): res = setup.post_json('/', params={'id': 1, 'message': 'foo'}) assert res.status_code == 200 return res ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,examples/test_pytest.py::test_post,examples/test_pytest.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.tests_pytest ~~~~~~~~~~~~~~~~~~~~~~~~~~ Autodoc for PyTest. :copyright: (c) 2014-2015 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" import sys from webtest import TestApp from autodoc import autodoc from tests.app import create_app try: import pytest except ImportError: sys.exit(1) @pytest.fixture def setup(): setup = TestApp(create_app) return setup @autodoc.generate('var/test_pytest.md', template='templates/markdown.md') def teardown_module(module): pass @autodoc.describe('GET /') def test_index(): app = TestApp(create_app) res = app.get('/') assert res.status_code == 200 return res @autodoc.describe('POST /') def test_post(setup): res = setup.post_json('/', params={'id': 1, 'message': 'foo'}) assert res.status_code == 200 return res ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,examples/test_requests.py::TestUnittest::test_foo_bar,examples/test_requests.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_requests ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Autodoc for UnitTest. :copyright: (c) 2014-2015 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" import os from unittest import TestCase from mock import patch from requests import Request, Session, Response from autodoc import autodoc from autodoc._compat import urlencode def dummy_response(m, request, params, filename=None): response = Response() response.status_code = 200 response.headers['Content-Type'] = 'application/json' response.request = request response.request.body = urlencode(params) response.request.path_url = '/' if filename is None: response._content = '' else: root_path = os.path.dirname(os.path.abspath(__file__)) file_path = root_path + '/' + filename with open(file_path, 'r') as f: data = f.read() response._content = data m.return_value = response class TestUnittest(TestCase): def create_request(self, url, method, data=None): headers = {'content-type': 'application/json'} request = Request(method, url, data=data, headers=headers) return request @patch('requests.sessions.Session.send') def send(self, request, params, file_path, m): dummy_response(m, request, params, file_path) session = Session() res = session.send(request.prepare()) return res @classmethod @autodoc.generate('var/test_requests.rst') def tearDownClass(cls): pass @autodoc.describe('GET /') def test_get(self): """""" GET / """""" req = self.create_request('http://localhost:5000/', 'GET') res = self.send(req, '', '../tests/data/get.json') self.assertEqual(res.status_code, 200) return res @autodoc.describe('POST /') def test_post(self): """""" POST / """""" params = {'id': 1, 'message': 'foo'} req = self.create_request('http://localhost:5000/', 'POST', params) res = self.send(req, params, '../tests/data/post.json') self.assertEqual(res.status_code, 200) return res @autodoc.describe('POST /foo/bar') def test_foo_bar(self): """""" POST /foo/bar """""" params = {'id': 1, 'message': 'foo'} req = self.create_request('http://localhost:5000/foo/bar', 'POST', params) res = self.send(req, params, '../tests/data/post.json') self.assertEqual(res.status_code, 200) return res ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,examples/test_requests.py::TestUnittest::test_get,examples/test_requests.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_requests ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Autodoc for UnitTest. :copyright: (c) 2014-2015 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" import os from unittest import TestCase from mock import patch from requests import Request, Session, Response from autodoc import autodoc from autodoc._compat import urlencode def dummy_response(m, request, params, filename=None): response = Response() response.status_code = 200 response.headers['Content-Type'] = 'application/json' response.request = request response.request.body = urlencode(params) response.request.path_url = '/' if filename is None: response._content = '' else: root_path = os.path.dirname(os.path.abspath(__file__)) file_path = root_path + '/' + filename with open(file_path, 'r') as f: data = f.read() response._content = data m.return_value = response class TestUnittest(TestCase): def create_request(self, url, method, data=None): headers = {'content-type': 'application/json'} request = Request(method, url, data=data, headers=headers) return request @patch('requests.sessions.Session.send') def send(self, request, params, file_path, m): dummy_response(m, request, params, file_path) session = Session() res = session.send(request.prepare()) return res @classmethod @autodoc.generate('var/test_requests.rst') def tearDownClass(cls): pass @autodoc.describe('GET /') def test_get(self): """""" GET / """""" req = self.create_request('http://localhost:5000/', 'GET') res = self.send(req, '', '../tests/data/get.json') self.assertEqual(res.status_code, 200) return res @autodoc.describe('POST /') def test_post(self): """""" POST / """""" params = {'id': 1, 'message': 'foo'} req = self.create_request('http://localhost:5000/', 'POST', params) res = self.send(req, params, '../tests/data/post.json') self.assertEqual(res.status_code, 200) return res @autodoc.describe('POST /foo/bar') def test_foo_bar(self): """""" POST /foo/bar """""" params = {'id': 1, 'message': 'foo'} req = self.create_request('http://localhost:5000/foo/bar', 'POST', params) res = self.send(req, params, '../tests/data/post.json') self.assertEqual(res.status_code, 200) return res ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,examples/test_requests.py::TestUnittest::test_post,examples/test_requests.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_requests ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Autodoc for UnitTest. :copyright: (c) 2014-2015 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" import os from unittest import TestCase from mock import patch from requests import Request, Session, Response from autodoc import autodoc from autodoc._compat import urlencode def dummy_response(m, request, params, filename=None): response = Response() response.status_code = 200 response.headers['Content-Type'] = 'application/json' response.request = request response.request.body = urlencode(params) response.request.path_url = '/' if filename is None: response._content = '' else: root_path = os.path.dirname(os.path.abspath(__file__)) file_path = root_path + '/' + filename with open(file_path, 'r') as f: data = f.read() response._content = data m.return_value = response class TestUnittest(TestCase): def create_request(self, url, method, data=None): headers = {'content-type': 'application/json'} request = Request(method, url, data=data, headers=headers) return request @patch('requests.sessions.Session.send') def send(self, request, params, file_path, m): dummy_response(m, request, params, file_path) session = Session() res = session.send(request.prepare()) return res @classmethod @autodoc.generate('var/test_requests.rst') def tearDownClass(cls): pass @autodoc.describe('GET /') def test_get(self): """""" GET / """""" req = self.create_request('http://localhost:5000/', 'GET') res = self.send(req, '', '../tests/data/get.json') self.assertEqual(res.status_code, 200) return res @autodoc.describe('POST /') def test_post(self): """""" POST / """""" params = {'id': 1, 'message': 'foo'} req = self.create_request('http://localhost:5000/', 'POST', params) res = self.send(req, params, '../tests/data/post.json') self.assertEqual(res.status_code, 200) return res @autodoc.describe('POST /foo/bar') def test_foo_bar(self): """""" POST /foo/bar """""" params = {'id': 1, 'message': 'foo'} req = self.create_request('http://localhost:5000/foo/bar', 'POST', params) res = self.send(req, params, '../tests/data/post.json') self.assertEqual(res.status_code, 200) return res ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,examples/test_unittest.py::TestUnittest::test_foo_bar,examples/test_unittest.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_unittest ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Autodoc for UnitTest. :copyright: (c) 2014-2015 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" from unittest import TestCase from webtest import TestApp from autodoc import autodoc from tests.app import create_app class TestUnittest(TestCase): def setUp(self): app = create_app self.client = TestApp(app) @classmethod @autodoc.generate('var/test_unittest.rst') def tearDownClass(cls): pass @autodoc.describe('GET /') def test_get(self): """""" GET / """""" res = self.client.get('/') self.assertEqual(res.status_code, 200) return res @autodoc.describe('POST /') def test_post(self): """""" POST / """""" res = self.client.post_json('/', params={'id': 1, 'message': 'foo'}) self.assertEqual(res.status_code, 200) return res @autodoc.describe('POST /foo/bar') def test_foo_bar(self): """""" POST /foo/bar """""" res = self.client.post_json('/foo/bar', params={ 'id': 1, 'message': 'foo' }) self.assertEqual(res.status_code, 200) return res ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,examples/test_unittest.py::TestUnittest::test_get,examples/test_unittest.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_unittest ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Autodoc for UnitTest. :copyright: (c) 2014-2015 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" from unittest import TestCase from webtest import TestApp from autodoc import autodoc from tests.app import create_app class TestUnittest(TestCase): def setUp(self): app = create_app self.client = TestApp(app) @classmethod @autodoc.generate('var/test_unittest.rst') def tearDownClass(cls): pass @autodoc.describe('GET /') def test_get(self): """""" GET / """""" res = self.client.get('/') self.assertEqual(res.status_code, 200) return res @autodoc.describe('POST /') def test_post(self): """""" POST / """""" res = self.client.post_json('/', params={'id': 1, 'message': 'foo'}) self.assertEqual(res.status_code, 200) return res @autodoc.describe('POST /foo/bar') def test_foo_bar(self): """""" POST /foo/bar """""" res = self.client.post_json('/foo/bar', params={ 'id': 1, 'message': 'foo' }) self.assertEqual(res.status_code, 200) return res ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,examples/test_unittest.py::TestUnittest::test_post,examples/test_unittest.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_unittest ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Autodoc for UnitTest. :copyright: (c) 2014-2015 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" from unittest import TestCase from webtest import TestApp from autodoc import autodoc from tests.app import create_app class TestUnittest(TestCase): def setUp(self): app = create_app self.client = TestApp(app) @classmethod @autodoc.generate('var/test_unittest.rst') def tearDownClass(cls): pass @autodoc.describe('GET /') def test_get(self): """""" GET / """""" res = self.client.get('/') self.assertEqual(res.status_code, 200) return res @autodoc.describe('POST /') def test_post(self): """""" POST / """""" res = self.client.post_json('/', params={'id': 1, 'message': 'foo'}) self.assertEqual(res.status_code, 200) return res @autodoc.describe('POST /foo/bar') def test_foo_bar(self): """""" POST /foo/bar """""" res = self.client.post_json('/foo/bar', params={ 'id': 1, 'message': 'foo' }) self.assertEqual(res.status_code, 200) return res ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,tests/test_autodoc.py::TestAutodoc::test_create_document,tests/test_autodoc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_autodoc ~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests for Autodoc. :copyright: (c) 2014-2018 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" import os import shutil from unittest import TestCase from webtest import TestApp from autodoc import autodoc from autodoc._compat import iteritems from tests.app import create_app root_path = os.path.dirname(os.path.abspath(__file__)) var_path = os.path.join(root_path, 'var') os.environ['PYAUTODOC'] = '1' def clear_generated_file(var_path): if os.path.exists(var_path): shutil.rmtree(var_path) os.mkdir(var_path) with open('{0}/.gitkeep'.format(var_path), 'w') as f: f.write('') class TestAutodoc(TestCase): def setUp(self): app = create_app self.client = TestApp(app) self.root_path = root_path clear_generated_file(var_path) def test_parse_response(self): """""" Should parse WebTest response. """""" res = self.client.post_json('/', params={'message': 'foo'}) autodoc.parse('POST /', res) var = { 'describe': 'POST /', 'describe_separators': '======', 'target_url': 'http://localhost:80', 'status_code': 200, 'request': 'POST /', 'response_body': '{\n ""response"": ""create""\n}', 'response_content_type': 'application/json', 'params': '{\n ""message"": ""foo""\n}' } for k, v in iteritems(autodoc.vars[0]): self.assertEqual(v, var[k]) autodoc.clear() def test_parse_responses(self): """""" Should stack responses. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.parse('GET /', res) var = { 'response_content_type': 'application/json', 'response_body': '{\n ""response"": ""index""\n}', 'describe': 'GET /', 'request': 'GET /', 'params': '', 'status_code': 200, 'target_url': 'http://localhost:80', 'describe_separators': '=====' } vars = [var, var] self.assertEqual(autodoc.vars, vars) autodoc.clear() def test_clear_responses(self): """""" Should clear stacked WebTest responses. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.parse('GET /', res) autodoc.clear() self.assertEqual(autodoc.vars, []) def test_create_document(self): """""" Should create reST document. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.create_document(os.path.join(self.root_path, 'var/test_autodoc.rst')) self.assertTrue(os.path.exists(os.path.join(self.root_path, 'var/test_autodoc.rst'))) autodoc.clear() def test_create_markdown_document(self): """""" Should create markdown document. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.template_path = os.path.join(self.root_path, 'templates/markdown.md') output = os.path.join(self.root_path, 'var/test_autodoc.md') autodoc.create_document(output) ret = os.path.exists(output) self.assertTrue(ret) autodoc.clear() def test_should_change_separators(self): """""" Should change separators. """""" res = self.client.get('/') autodoc.separators = '*' autodoc.parse('GET /', res) var = { 'response_content_type': 'application/json', 'response_body': '{\n ""response"": ""index""\n}', 'describe': 'GET /', 'request': 'GET /', 'params': '', 'status_code': 200, 'target_url': 'http://localhost:80', 'describe_separators': '*****' } for k, v in iteritems(autodoc.vars[0]): self.assertEqual(v, var[k]) autodoc.clear() class TestWebTestResponse(TestCase): def _getTarget(self): from autodoc import WebTestResponse return WebTestResponse def setUp(self): app = create_app self.client = TestApp(app) clear_generated_file(var_path) def test_should_parse_get_request_result(self): """""" Should parse WebTest GET response. """""" WebTestResponse = self._getTarget() res = self.client.get('/') ret = WebTestResponse().parse(res) var = { 'status_code': 200, 'response_body': '{\n ""response"": ""index""\n}', 'response_content_type': 'application/json', 'request': 'GET /', 'params': '', 'target_url': 'http://localhost:80' } self.assertEqual(ret, var) def test_should_parse_post_request_result(self): """""" Should parse WebTest POST response. """""" WebTestResponse = self._getTarget() res = self.client.post_json('/', params={'message': 'foo'}) ret = WebTestResponse().parse(res) var = { 'target_url': 'http://localhost:80', 'status_code': 200, 'request': 'POST /', 'response_body': '{\n ""response"": ""create""\n}', 'response_content_type': 'application/json', 'params': '{\n ""message"": ""foo""\n}' } self.assertEqual(ret, var) ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,tests/test_autodoc.py::TestAutodoc::test_parse_response,tests/test_autodoc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_autodoc ~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests for Autodoc. :copyright: (c) 2014-2018 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" import os import shutil from unittest import TestCase from webtest import TestApp from autodoc import autodoc from autodoc._compat import iteritems from tests.app import create_app root_path = os.path.dirname(os.path.abspath(__file__)) var_path = os.path.join(root_path, 'var') os.environ['PYAUTODOC'] = '1' def clear_generated_file(var_path): if os.path.exists(var_path): shutil.rmtree(var_path) os.mkdir(var_path) with open('{0}/.gitkeep'.format(var_path), 'w') as f: f.write('') class TestAutodoc(TestCase): def setUp(self): app = create_app self.client = TestApp(app) self.root_path = root_path clear_generated_file(var_path) def test_parse_response(self): """""" Should parse WebTest response. """""" res = self.client.post_json('/', params={'message': 'foo'}) autodoc.parse('POST /', res) var = { 'describe': 'POST /', 'describe_separators': '======', 'target_url': 'http://localhost:80', 'status_code': 200, 'request': 'POST /', 'response_body': '{\n ""response"": ""create""\n}', 'response_content_type': 'application/json', 'params': '{\n ""message"": ""foo""\n}' } for k, v in iteritems(autodoc.vars[0]): self.assertEqual(v, var[k]) autodoc.clear() def test_parse_responses(self): """""" Should stack responses. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.parse('GET /', res) var = { 'response_content_type': 'application/json', 'response_body': '{\n ""response"": ""index""\n}', 'describe': 'GET /', 'request': 'GET /', 'params': '', 'status_code': 200, 'target_url': 'http://localhost:80', 'describe_separators': '=====' } vars = [var, var] self.assertEqual(autodoc.vars, vars) autodoc.clear() def test_clear_responses(self): """""" Should clear stacked WebTest responses. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.parse('GET /', res) autodoc.clear() self.assertEqual(autodoc.vars, []) def test_create_document(self): """""" Should create reST document. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.create_document(os.path.join(self.root_path, 'var/test_autodoc.rst')) self.assertTrue(os.path.exists(os.path.join(self.root_path, 'var/test_autodoc.rst'))) autodoc.clear() def test_create_markdown_document(self): """""" Should create markdown document. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.template_path = os.path.join(self.root_path, 'templates/markdown.md') output = os.path.join(self.root_path, 'var/test_autodoc.md') autodoc.create_document(output) ret = os.path.exists(output) self.assertTrue(ret) autodoc.clear() def test_should_change_separators(self): """""" Should change separators. """""" res = self.client.get('/') autodoc.separators = '*' autodoc.parse('GET /', res) var = { 'response_content_type': 'application/json', 'response_body': '{\n ""response"": ""index""\n}', 'describe': 'GET /', 'request': 'GET /', 'params': '', 'status_code': 200, 'target_url': 'http://localhost:80', 'describe_separators': '*****' } for k, v in iteritems(autodoc.vars[0]): self.assertEqual(v, var[k]) autodoc.clear() class TestWebTestResponse(TestCase): def _getTarget(self): from autodoc import WebTestResponse return WebTestResponse def setUp(self): app = create_app self.client = TestApp(app) clear_generated_file(var_path) def test_should_parse_get_request_result(self): """""" Should parse WebTest GET response. """""" WebTestResponse = self._getTarget() res = self.client.get('/') ret = WebTestResponse().parse(res) var = { 'status_code': 200, 'response_body': '{\n ""response"": ""index""\n}', 'response_content_type': 'application/json', 'request': 'GET /', 'params': '', 'target_url': 'http://localhost:80' } self.assertEqual(ret, var) def test_should_parse_post_request_result(self): """""" Should parse WebTest POST response. """""" WebTestResponse = self._getTarget() res = self.client.post_json('/', params={'message': 'foo'}) ret = WebTestResponse().parse(res) var = { 'target_url': 'http://localhost:80', 'status_code': 200, 'request': 'POST /', 'response_body': '{\n ""response"": ""create""\n}', 'response_content_type': 'application/json', 'params': '{\n ""message"": ""foo""\n}' } self.assertEqual(ret, var) ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,tests/test_autodoc.py::TestAutodoc::test_parse_responses,tests/test_autodoc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_autodoc ~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests for Autodoc. :copyright: (c) 2014-2018 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" import os import shutil from unittest import TestCase from webtest import TestApp from autodoc import autodoc from autodoc._compat import iteritems from tests.app import create_app root_path = os.path.dirname(os.path.abspath(__file__)) var_path = os.path.join(root_path, 'var') os.environ['PYAUTODOC'] = '1' def clear_generated_file(var_path): if os.path.exists(var_path): shutil.rmtree(var_path) os.mkdir(var_path) with open('{0}/.gitkeep'.format(var_path), 'w') as f: f.write('') class TestAutodoc(TestCase): def setUp(self): app = create_app self.client = TestApp(app) self.root_path = root_path clear_generated_file(var_path) def test_parse_response(self): """""" Should parse WebTest response. """""" res = self.client.post_json('/', params={'message': 'foo'}) autodoc.parse('POST /', res) var = { 'describe': 'POST /', 'describe_separators': '======', 'target_url': 'http://localhost:80', 'status_code': 200, 'request': 'POST /', 'response_body': '{\n ""response"": ""create""\n}', 'response_content_type': 'application/json', 'params': '{\n ""message"": ""foo""\n}' } for k, v in iteritems(autodoc.vars[0]): self.assertEqual(v, var[k]) autodoc.clear() def test_parse_responses(self): """""" Should stack responses. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.parse('GET /', res) var = { 'response_content_type': 'application/json', 'response_body': '{\n ""response"": ""index""\n}', 'describe': 'GET /', 'request': 'GET /', 'params': '', 'status_code': 200, 'target_url': 'http://localhost:80', 'describe_separators': '=====' } vars = [var, var] self.assertEqual(autodoc.vars, vars) autodoc.clear() def test_clear_responses(self): """""" Should clear stacked WebTest responses. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.parse('GET /', res) autodoc.clear() self.assertEqual(autodoc.vars, []) def test_create_document(self): """""" Should create reST document. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.create_document(os.path.join(self.root_path, 'var/test_autodoc.rst')) self.assertTrue(os.path.exists(os.path.join(self.root_path, 'var/test_autodoc.rst'))) autodoc.clear() def test_create_markdown_document(self): """""" Should create markdown document. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.template_path = os.path.join(self.root_path, 'templates/markdown.md') output = os.path.join(self.root_path, 'var/test_autodoc.md') autodoc.create_document(output) ret = os.path.exists(output) self.assertTrue(ret) autodoc.clear() def test_should_change_separators(self): """""" Should change separators. """""" res = self.client.get('/') autodoc.separators = '*' autodoc.parse('GET /', res) var = { 'response_content_type': 'application/json', 'response_body': '{\n ""response"": ""index""\n}', 'describe': 'GET /', 'request': 'GET /', 'params': '', 'status_code': 200, 'target_url': 'http://localhost:80', 'describe_separators': '*****' } for k, v in iteritems(autodoc.vars[0]): self.assertEqual(v, var[k]) autodoc.clear() class TestWebTestResponse(TestCase): def _getTarget(self): from autodoc import WebTestResponse return WebTestResponse def setUp(self): app = create_app self.client = TestApp(app) clear_generated_file(var_path) def test_should_parse_get_request_result(self): """""" Should parse WebTest GET response. """""" WebTestResponse = self._getTarget() res = self.client.get('/') ret = WebTestResponse().parse(res) var = { 'status_code': 200, 'response_body': '{\n ""response"": ""index""\n}', 'response_content_type': 'application/json', 'request': 'GET /', 'params': '', 'target_url': 'http://localhost:80' } self.assertEqual(ret, var) def test_should_parse_post_request_result(self): """""" Should parse WebTest POST response. """""" WebTestResponse = self._getTarget() res = self.client.post_json('/', params={'message': 'foo'}) ret = WebTestResponse().parse(res) var = { 'target_url': 'http://localhost:80', 'status_code': 200, 'request': 'POST /', 'response_body': '{\n ""response"": ""create""\n}', 'response_content_type': 'application/json', 'params': '{\n ""message"": ""foo""\n}' } self.assertEqual(ret, var) ", https://github.com/heavenshell/py-autodoc,001d5ff03d41a5d714075eb08e666c413d50f0ac,tests/test_autodoc.py::TestAutodoc::test_should_change_separators,tests/test_autodoc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" autodoc.tests.test_autodoc ~~~~~~~~~~~~~~~~~~~~~~~~~~ Tests for Autodoc. :copyright: (c) 2014-2018 Shinya Ohyanagi, All rights reserved. :license: BSD, see LICENSE for more details. """""" import os import shutil from unittest import TestCase from webtest import TestApp from autodoc import autodoc from autodoc._compat import iteritems from tests.app import create_app root_path = os.path.dirname(os.path.abspath(__file__)) var_path = os.path.join(root_path, 'var') os.environ['PYAUTODOC'] = '1' def clear_generated_file(var_path): if os.path.exists(var_path): shutil.rmtree(var_path) os.mkdir(var_path) with open('{0}/.gitkeep'.format(var_path), 'w') as f: f.write('') class TestAutodoc(TestCase): def setUp(self): app = create_app self.client = TestApp(app) self.root_path = root_path clear_generated_file(var_path) def test_parse_response(self): """""" Should parse WebTest response. """""" res = self.client.post_json('/', params={'message': 'foo'}) autodoc.parse('POST /', res) var = { 'describe': 'POST /', 'describe_separators': '======', 'target_url': 'http://localhost:80', 'status_code': 200, 'request': 'POST /', 'response_body': '{\n ""response"": ""create""\n}', 'response_content_type': 'application/json', 'params': '{\n ""message"": ""foo""\n}' } for k, v in iteritems(autodoc.vars[0]): self.assertEqual(v, var[k]) autodoc.clear() def test_parse_responses(self): """""" Should stack responses. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.parse('GET /', res) var = { 'response_content_type': 'application/json', 'response_body': '{\n ""response"": ""index""\n}', 'describe': 'GET /', 'request': 'GET /', 'params': '', 'status_code': 200, 'target_url': 'http://localhost:80', 'describe_separators': '=====' } vars = [var, var] self.assertEqual(autodoc.vars, vars) autodoc.clear() def test_clear_responses(self): """""" Should clear stacked WebTest responses. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.parse('GET /', res) autodoc.clear() self.assertEqual(autodoc.vars, []) def test_create_document(self): """""" Should create reST document. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.create_document(os.path.join(self.root_path, 'var/test_autodoc.rst')) self.assertTrue(os.path.exists(os.path.join(self.root_path, 'var/test_autodoc.rst'))) autodoc.clear() def test_create_markdown_document(self): """""" Should create markdown document. """""" res = self.client.get('/') autodoc.parse('GET /', res) autodoc.template_path = os.path.join(self.root_path, 'templates/markdown.md') output = os.path.join(self.root_path, 'var/test_autodoc.md') autodoc.create_document(output) ret = os.path.exists(output) self.assertTrue(ret) autodoc.clear() def test_should_change_separators(self): """""" Should change separators. """""" res = self.client.get('/') autodoc.separators = '*' autodoc.parse('GET /', res) var = { 'response_content_type': 'application/json', 'response_body': '{\n ""response"": ""index""\n}', 'describe': 'GET /', 'request': 'GET /', 'params': '', 'status_code': 200, 'target_url': 'http://localhost:80', 'describe_separators': '*****' } for k, v in iteritems(autodoc.vars[0]): self.assertEqual(v, var[k]) autodoc.clear() class TestWebTestResponse(TestCase): def _getTarget(self): from autodoc import WebTestResponse return WebTestResponse def setUp(self): app = create_app self.client = TestApp(app) clear_generated_file(var_path) def test_should_parse_get_request_result(self): """""" Should parse WebTest GET response. """""" WebTestResponse = self._getTarget() res = self.client.get('/') ret = WebTestResponse().parse(res) var = { 'status_code': 200, 'response_body': '{\n ""response"": ""index""\n}', 'response_content_type': 'application/json', 'request': 'GET /', 'params': '', 'target_url': 'http://localhost:80' } self.assertEqual(ret, var) def test_should_parse_post_request_result(self): """""" Should parse WebTest POST response. """""" WebTestResponse = self._getTarget() res = self.client.post_json('/', params={'message': 'foo'}) ret = WebTestResponse().parse(res) var = { 'target_url': 'http://localhost:80', 'status_code': 200, 'request': 'POST /', 'response_body': '{\n ""response"": ""create""\n}', 'response_content_type': 'application/json', 'params': '{\n ""message"": ""foo""\n}' } self.assertEqual(ret, var) ", https://github.com/hefnawi/json-storage-manager,c7521fc4a576cf23a8c2454106bed6fb8c951b8d,tests/test_main.py::test_get_item,tests/test_main.py,OD-Brit,flaky,Opened,https://github.com/hefnawi/json-storage-manager/pull/1,classify;root_cause,"# -*- coding: utf-8 -*- import json import pytest import random import uuid from json_storage_manager import atomic, utils def test_is_file_not_ok(): invalid_filename = ""tests/data.json"" assert not utils.is_file(invalid_filename) def test_is_file_ok(): invalid_filename = ""tests/test_main.py"" assert utils.is_file(invalid_filename) @pytest.fixture(scope='session') def create_json_data(): def _create_json_data(product_name): return { ""uuid"": str(uuid.uuid4()), ""name"": str(product_name), ""price"": str(round(random.uniform(5.0, 6000.0), 2)) } return _create_json_data @pytest.fixture(scope='session') def json_file(tmpdir_factory, create_json_data): json_1 = create_json_data(""Alpha Product MK-1000"") json_2 = create_json_data(""Bravo Product MK-2000"") json_3 = create_json_data(""Charlie Product MK-3000"") json_list = [] json_list.append(json_1) json_list.append(json_2) json_list.append(json_3) fn = tmpdir_factory.mktemp('data').join('products.json') fn.write(json.dumps(json_list)) return fn # read contents of json file def test_read_json(json_file): with open(str(json_file)) as f: products_data = json.load(f) for item in products_data: assert item[""uuid""] assert item[""name""] assert item[""price""] # add to contents of json file def test_write_json(json_file): with atomic.atomic_write(str(json_file)) as temp_file: with open(str(json_file)) as products_file: # get the JSON data into memory products_data = json.load(products_file) # now process the JSON data products_data.append( {'uuid': ""2299d69e-deba-11e8-bded-680715cce955"", 'price': 111.0, 'name': ""Test Product"" }) json.dump(products_data, temp_file) with open(str(json_file)) as f: products_data = json.load(f) test = [i for i in products_data if i[""uuid""] == ""2299d69e-deba-11e8-bded-680715cce955""] assert test[0][""name""] == 'Test Product' def test_get_item(json_file): results = atomic.get_item( str(json_file), ""2299d69e-deba-11e8-bded-680715cce955"") assert results assert results[0][""name""] == 'Test Product' def test_get_no_item(json_file): results = atomic.get_item( str(json_file), ""xxxxxxxx-deba-11e8-bded-680715cce955"") assert not results def test_set_item(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via set_item' def test_update_item(json_file): mod_item = {'price': 777.0, 'name': ""Test Product via update_item"" } results = atomic.update_item(str(json_file), mod_item, ""1144d69e-joya-33e8-bdfd-680688cce955"") results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via update_item' assert results_get[0][""price""] == 777.0 def test_set_item_fail(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) assert not results ", https://github.com/hefnawi/json-storage-manager,c7521fc4a576cf23a8c2454106bed6fb8c951b8d,tests/test_main.py::test_get_no_item,tests/test_main.py,OD-Vic,flaky,Opened,https://github.com/hefnawi/json-storage-manager/pull/1,classify;root_cause,"# -*- coding: utf-8 -*- import json import pytest import random import uuid from json_storage_manager import atomic, utils def test_is_file_not_ok(): invalid_filename = ""tests/data.json"" assert not utils.is_file(invalid_filename) def test_is_file_ok(): invalid_filename = ""tests/test_main.py"" assert utils.is_file(invalid_filename) @pytest.fixture(scope='session') def create_json_data(): def _create_json_data(product_name): return { ""uuid"": str(uuid.uuid4()), ""name"": str(product_name), ""price"": str(round(random.uniform(5.0, 6000.0), 2)) } return _create_json_data @pytest.fixture(scope='session') def json_file(tmpdir_factory, create_json_data): json_1 = create_json_data(""Alpha Product MK-1000"") json_2 = create_json_data(""Bravo Product MK-2000"") json_3 = create_json_data(""Charlie Product MK-3000"") json_list = [] json_list.append(json_1) json_list.append(json_2) json_list.append(json_3) fn = tmpdir_factory.mktemp('data').join('products.json') fn.write(json.dumps(json_list)) return fn # read contents of json file def test_read_json(json_file): with open(str(json_file)) as f: products_data = json.load(f) for item in products_data: assert item[""uuid""] assert item[""name""] assert item[""price""] # add to contents of json file def test_write_json(json_file): with atomic.atomic_write(str(json_file)) as temp_file: with open(str(json_file)) as products_file: # get the JSON data into memory products_data = json.load(products_file) # now process the JSON data products_data.append( {'uuid': ""2299d69e-deba-11e8-bded-680715cce955"", 'price': 111.0, 'name': ""Test Product"" }) json.dump(products_data, temp_file) with open(str(json_file)) as f: products_data = json.load(f) test = [i for i in products_data if i[""uuid""] == ""2299d69e-deba-11e8-bded-680715cce955""] assert test[0][""name""] == 'Test Product' def test_get_item(json_file): results = atomic.get_item( str(json_file), ""2299d69e-deba-11e8-bded-680715cce955"") assert results assert results[0][""name""] == 'Test Product' def test_get_no_item(json_file): results = atomic.get_item( str(json_file), ""xxxxxxxx-deba-11e8-bded-680715cce955"") assert not results def test_set_item(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via set_item' def test_update_item(json_file): mod_item = {'price': 777.0, 'name': ""Test Product via update_item"" } results = atomic.update_item(str(json_file), mod_item, ""1144d69e-joya-33e8-bdfd-680688cce955"") results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via update_item' assert results_get[0][""price""] == 777.0 def test_set_item_fail(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) assert not results ", https://github.com/hefnawi/json-storage-manager,c7521fc4a576cf23a8c2454106bed6fb8c951b8d,tests/test_main.py::test_read_json,tests/test_main.py,OD-Vic,flaky,Opened,https://github.com/hefnawi/json-storage-manager/pull/1,classify;root_cause,"# -*- coding: utf-8 -*- import json import pytest import random import uuid from json_storage_manager import atomic, utils def test_is_file_not_ok(): invalid_filename = ""tests/data.json"" assert not utils.is_file(invalid_filename) def test_is_file_ok(): invalid_filename = ""tests/test_main.py"" assert utils.is_file(invalid_filename) @pytest.fixture(scope='session') def create_json_data(): def _create_json_data(product_name): return { ""uuid"": str(uuid.uuid4()), ""name"": str(product_name), ""price"": str(round(random.uniform(5.0, 6000.0), 2)) } return _create_json_data @pytest.fixture(scope='session') def json_file(tmpdir_factory, create_json_data): json_1 = create_json_data(""Alpha Product MK-1000"") json_2 = create_json_data(""Bravo Product MK-2000"") json_3 = create_json_data(""Charlie Product MK-3000"") json_list = [] json_list.append(json_1) json_list.append(json_2) json_list.append(json_3) fn = tmpdir_factory.mktemp('data').join('products.json') fn.write(json.dumps(json_list)) return fn # read contents of json file def test_read_json(json_file): with open(str(json_file)) as f: products_data = json.load(f) for item in products_data: assert item[""uuid""] assert item[""name""] assert item[""price""] # add to contents of json file def test_write_json(json_file): with atomic.atomic_write(str(json_file)) as temp_file: with open(str(json_file)) as products_file: # get the JSON data into memory products_data = json.load(products_file) # now process the JSON data products_data.append( {'uuid': ""2299d69e-deba-11e8-bded-680715cce955"", 'price': 111.0, 'name': ""Test Product"" }) json.dump(products_data, temp_file) with open(str(json_file)) as f: products_data = json.load(f) test = [i for i in products_data if i[""uuid""] == ""2299d69e-deba-11e8-bded-680715cce955""] assert test[0][""name""] == 'Test Product' def test_get_item(json_file): results = atomic.get_item( str(json_file), ""2299d69e-deba-11e8-bded-680715cce955"") assert results assert results[0][""name""] == 'Test Product' def test_get_no_item(json_file): results = atomic.get_item( str(json_file), ""xxxxxxxx-deba-11e8-bded-680715cce955"") assert not results def test_set_item(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via set_item' def test_update_item(json_file): mod_item = {'price': 777.0, 'name': ""Test Product via update_item"" } results = atomic.update_item(str(json_file), mod_item, ""1144d69e-joya-33e8-bdfd-680688cce955"") results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via update_item' assert results_get[0][""price""] == 777.0 def test_set_item_fail(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) assert not results ", https://github.com/hefnawi/json-storage-manager,c7521fc4a576cf23a8c2454106bed6fb8c951b8d,tests/test_main.py::test_set_item,tests/test_main.py,NIO,flaky,Opened,https://github.com/hefnawi/json-storage-manager/pull/2,classify;root_cause,"# -*- coding: utf-8 -*- import json import pytest import random import uuid from json_storage_manager import atomic, utils def test_is_file_not_ok(): invalid_filename = ""tests/data.json"" assert not utils.is_file(invalid_filename) def test_is_file_ok(): invalid_filename = ""tests/test_main.py"" assert utils.is_file(invalid_filename) @pytest.fixture(scope='session') def create_json_data(): def _create_json_data(product_name): return { ""uuid"": str(uuid.uuid4()), ""name"": str(product_name), ""price"": str(round(random.uniform(5.0, 6000.0), 2)) } return _create_json_data @pytest.fixture(scope='session') def json_file(tmpdir_factory, create_json_data): json_1 = create_json_data(""Alpha Product MK-1000"") json_2 = create_json_data(""Bravo Product MK-2000"") json_3 = create_json_data(""Charlie Product MK-3000"") json_list = [] json_list.append(json_1) json_list.append(json_2) json_list.append(json_3) fn = tmpdir_factory.mktemp('data').join('products.json') fn.write(json.dumps(json_list)) return fn # read contents of json file def test_read_json(json_file): with open(str(json_file)) as f: products_data = json.load(f) for item in products_data: assert item[""uuid""] assert item[""name""] assert item[""price""] # add to contents of json file def test_write_json(json_file): with atomic.atomic_write(str(json_file)) as temp_file: with open(str(json_file)) as products_file: # get the JSON data into memory products_data = json.load(products_file) # now process the JSON data products_data.append( {'uuid': ""2299d69e-deba-11e8-bded-680715cce955"", 'price': 111.0, 'name': ""Test Product"" }) json.dump(products_data, temp_file) with open(str(json_file)) as f: products_data = json.load(f) test = [i for i in products_data if i[""uuid""] == ""2299d69e-deba-11e8-bded-680715cce955""] assert test[0][""name""] == 'Test Product' def test_get_item(json_file): results = atomic.get_item( str(json_file), ""2299d69e-deba-11e8-bded-680715cce955"") assert results assert results[0][""name""] == 'Test Product' def test_get_no_item(json_file): results = atomic.get_item( str(json_file), ""xxxxxxxx-deba-11e8-bded-680715cce955"") assert not results def test_set_item(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via set_item' def test_update_item(json_file): mod_item = {'price': 777.0, 'name': ""Test Product via update_item"" } results = atomic.update_item(str(json_file), mod_item, ""1144d69e-joya-33e8-bdfd-680688cce955"") results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via update_item' assert results_get[0][""price""] == 777.0 def test_set_item_fail(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) assert not results ", https://github.com/hefnawi/json-storage-manager,c7521fc4a576cf23a8c2454106bed6fb8c951b8d,tests/test_main.py::test_set_item_fail,tests/test_main.py,OD-Brit,flaky,Opened,https://github.com/hefnawi/json-storage-manager/pull/1,classify;root_cause,"# -*- coding: utf-8 -*- import json import pytest import random import uuid from json_storage_manager import atomic, utils def test_is_file_not_ok(): invalid_filename = ""tests/data.json"" assert not utils.is_file(invalid_filename) def test_is_file_ok(): invalid_filename = ""tests/test_main.py"" assert utils.is_file(invalid_filename) @pytest.fixture(scope='session') def create_json_data(): def _create_json_data(product_name): return { ""uuid"": str(uuid.uuid4()), ""name"": str(product_name), ""price"": str(round(random.uniform(5.0, 6000.0), 2)) } return _create_json_data @pytest.fixture(scope='session') def json_file(tmpdir_factory, create_json_data): json_1 = create_json_data(""Alpha Product MK-1000"") json_2 = create_json_data(""Bravo Product MK-2000"") json_3 = create_json_data(""Charlie Product MK-3000"") json_list = [] json_list.append(json_1) json_list.append(json_2) json_list.append(json_3) fn = tmpdir_factory.mktemp('data').join('products.json') fn.write(json.dumps(json_list)) return fn # read contents of json file def test_read_json(json_file): with open(str(json_file)) as f: products_data = json.load(f) for item in products_data: assert item[""uuid""] assert item[""name""] assert item[""price""] # add to contents of json file def test_write_json(json_file): with atomic.atomic_write(str(json_file)) as temp_file: with open(str(json_file)) as products_file: # get the JSON data into memory products_data = json.load(products_file) # now process the JSON data products_data.append( {'uuid': ""2299d69e-deba-11e8-bded-680715cce955"", 'price': 111.0, 'name': ""Test Product"" }) json.dump(products_data, temp_file) with open(str(json_file)) as f: products_data = json.load(f) test = [i for i in products_data if i[""uuid""] == ""2299d69e-deba-11e8-bded-680715cce955""] assert test[0][""name""] == 'Test Product' def test_get_item(json_file): results = atomic.get_item( str(json_file), ""2299d69e-deba-11e8-bded-680715cce955"") assert results assert results[0][""name""] == 'Test Product' def test_get_no_item(json_file): results = atomic.get_item( str(json_file), ""xxxxxxxx-deba-11e8-bded-680715cce955"") assert not results def test_set_item(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via set_item' def test_update_item(json_file): mod_item = {'price': 777.0, 'name': ""Test Product via update_item"" } results = atomic.update_item(str(json_file), mod_item, ""1144d69e-joya-33e8-bdfd-680688cce955"") results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via update_item' assert results_get[0][""price""] == 777.0 def test_set_item_fail(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) assert not results ", https://github.com/hefnawi/json-storage-manager,c7521fc4a576cf23a8c2454106bed6fb8c951b8d,tests/test_main.py::test_update_item,tests/test_main.py,OD-Brit,flaky,Opened,https://github.com/hefnawi/json-storage-manager/pull/1,classify;root_cause,"# -*- coding: utf-8 -*- import json import pytest import random import uuid from json_storage_manager import atomic, utils def test_is_file_not_ok(): invalid_filename = ""tests/data.json"" assert not utils.is_file(invalid_filename) def test_is_file_ok(): invalid_filename = ""tests/test_main.py"" assert utils.is_file(invalid_filename) @pytest.fixture(scope='session') def create_json_data(): def _create_json_data(product_name): return { ""uuid"": str(uuid.uuid4()), ""name"": str(product_name), ""price"": str(round(random.uniform(5.0, 6000.0), 2)) } return _create_json_data @pytest.fixture(scope='session') def json_file(tmpdir_factory, create_json_data): json_1 = create_json_data(""Alpha Product MK-1000"") json_2 = create_json_data(""Bravo Product MK-2000"") json_3 = create_json_data(""Charlie Product MK-3000"") json_list = [] json_list.append(json_1) json_list.append(json_2) json_list.append(json_3) fn = tmpdir_factory.mktemp('data').join('products.json') fn.write(json.dumps(json_list)) return fn # read contents of json file def test_read_json(json_file): with open(str(json_file)) as f: products_data = json.load(f) for item in products_data: assert item[""uuid""] assert item[""name""] assert item[""price""] # add to contents of json file def test_write_json(json_file): with atomic.atomic_write(str(json_file)) as temp_file: with open(str(json_file)) as products_file: # get the JSON data into memory products_data = json.load(products_file) # now process the JSON data products_data.append( {'uuid': ""2299d69e-deba-11e8-bded-680715cce955"", 'price': 111.0, 'name': ""Test Product"" }) json.dump(products_data, temp_file) with open(str(json_file)) as f: products_data = json.load(f) test = [i for i in products_data if i[""uuid""] == ""2299d69e-deba-11e8-bded-680715cce955""] assert test[0][""name""] == 'Test Product' def test_get_item(json_file): results = atomic.get_item( str(json_file), ""2299d69e-deba-11e8-bded-680715cce955"") assert results assert results[0][""name""] == 'Test Product' def test_get_no_item(json_file): results = atomic.get_item( str(json_file), ""xxxxxxxx-deba-11e8-bded-680715cce955"") assert not results def test_set_item(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via set_item' def test_update_item(json_file): mod_item = {'price': 777.0, 'name': ""Test Product via update_item"" } results = atomic.update_item(str(json_file), mod_item, ""1144d69e-joya-33e8-bdfd-680688cce955"") results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via update_item' assert results_get[0][""price""] == 777.0 def test_set_item_fail(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) assert not results ", https://github.com/hefnawi/json-storage-manager,c7521fc4a576cf23a8c2454106bed6fb8c951b8d,tests/test_main.py::test_write_json,tests/test_main.py,OD-Vic,flaky,Opened,https://github.com/hefnawi/json-storage-manager/pull/1,classify;root_cause,"# -*- coding: utf-8 -*- import json import pytest import random import uuid from json_storage_manager import atomic, utils def test_is_file_not_ok(): invalid_filename = ""tests/data.json"" assert not utils.is_file(invalid_filename) def test_is_file_ok(): invalid_filename = ""tests/test_main.py"" assert utils.is_file(invalid_filename) @pytest.fixture(scope='session') def create_json_data(): def _create_json_data(product_name): return { ""uuid"": str(uuid.uuid4()), ""name"": str(product_name), ""price"": str(round(random.uniform(5.0, 6000.0), 2)) } return _create_json_data @pytest.fixture(scope='session') def json_file(tmpdir_factory, create_json_data): json_1 = create_json_data(""Alpha Product MK-1000"") json_2 = create_json_data(""Bravo Product MK-2000"") json_3 = create_json_data(""Charlie Product MK-3000"") json_list = [] json_list.append(json_1) json_list.append(json_2) json_list.append(json_3) fn = tmpdir_factory.mktemp('data').join('products.json') fn.write(json.dumps(json_list)) return fn # read contents of json file def test_read_json(json_file): with open(str(json_file)) as f: products_data = json.load(f) for item in products_data: assert item[""uuid""] assert item[""name""] assert item[""price""] # add to contents of json file def test_write_json(json_file): with atomic.atomic_write(str(json_file)) as temp_file: with open(str(json_file)) as products_file: # get the JSON data into memory products_data = json.load(products_file) # now process the JSON data products_data.append( {'uuid': ""2299d69e-deba-11e8-bded-680715cce955"", 'price': 111.0, 'name': ""Test Product"" }) json.dump(products_data, temp_file) with open(str(json_file)) as f: products_data = json.load(f) test = [i for i in products_data if i[""uuid""] == ""2299d69e-deba-11e8-bded-680715cce955""] assert test[0][""name""] == 'Test Product' def test_get_item(json_file): results = atomic.get_item( str(json_file), ""2299d69e-deba-11e8-bded-680715cce955"") assert results assert results[0][""name""] == 'Test Product' def test_get_no_item(json_file): results = atomic.get_item( str(json_file), ""xxxxxxxx-deba-11e8-bded-680715cce955"") assert not results def test_set_item(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via set_item' def test_update_item(json_file): mod_item = {'price': 777.0, 'name': ""Test Product via update_item"" } results = atomic.update_item(str(json_file), mod_item, ""1144d69e-joya-33e8-bdfd-680688cce955"") results_get = atomic.get_item( str(json_file), ""1144d69e-joya-33e8-bdfd-680688cce955"") assert results assert results_get[0][""name""] == 'Test Product via update_item' assert results_get[0][""price""] == 777.0 def test_set_item_fail(json_file): new_item = {'uuid': ""1144d69e-joya-33e8-bdfd-680688cce955"", 'price': 333.0, 'name': ""Test Product via set_item"" } results = atomic.set_item(str(json_file), new_item) assert not results ", https://github.com/holzkohlengrill/SCout,8fd1eca0607b27d6e0c537b2ea1211d23356edff,tests/test_header.py::TestHeaderLogger::testSingleArg,tests/test_header.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" SCout | Special Character Out Copyright 2017-2019, Marcel Schmalzl (MSc) This code is licensed under a MIT license You should have received a copy of the MIT licence along with this program. If not see: * https://github.com/holzkohlengrill/SCout/blob/master/LICENSE * https://opensource.org/licenses/MIT """""" import unittest from unittest import TestCase import pypiscout.SCout as sc_simple from pypiscout.SCout_Logger import Logger as sc TEXTLIST = [ ""Lorem ipsum dolor sit amet, consetetur sadipscing elitr,"", "" sed diam nonumy eirmod tempor invidunt ut labore et dolore magna"", "" aliquyam erat, sed diam voluptua."", "" At vero eos et accusam et justo duo dolores et ea rebum."", "" Stet clita kasd gubergren, no sea takimata "" ] TEXTSINGLE = ""Lorem ipsum dolor sit amet, consetetur sadipscing elitr,"" ################## HEADEROUTPUT_EXPECTED = """"""//////////////////////////////////////////////////////////////// /// Lorem ipsum dolor sit amet, consetetur sadipscing elitr, /// //////////////////////////////////////////////////////////////// """""" # This newline is important HEADEROUTPUT_MULTILINE_EXPECTED = [ ""Lorem ipsum dolor sit amet, consetetur sadipscing elitr,"", "" sed diam nonumy eirmod tempor invidunt ut labore et dolore magna"", "" aliquyam erat, sed diam voluptua."", "" At vero eos et accusam et justo duo dolores et ea rebum."", "" Stet clita kasd gubergren, no sea takimata "" ] class TestHeaderSimple(TestCase): def testSingleArg(self): singleHeader = sc_simple.Header.gen(TEXTSINGLE, symbol=""/"") print(singleHeader) self.assertMultiLineEqual(HEADEROUTPUT_EXPECTED, singleHeader) # def testMultiArg(self): # """""" # Test header multiline support (NOT IMPLEMENTED SO FAR) # :return: # """""" # print(""Conducting test:"", ""testMultiArg"") # singleHeader = sc_simple.Header.gen(TEXTLIST, symbol=""/"") # self.assertMultiLineEqual(HEADEROUTPUT_EXPECTED, singleHeader) class TestHeaderLogger(TestCase): def testSingleArg(self): singleHeader = sc().header(TEXTSINGLE, symbol=""/"", disableColour=True) self.assertMultiLineEqual(HEADEROUTPUT_EXPECTED, singleHeader) if __name__ == '__main__': unittest.main() ", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_auth_with_invalid_pubkey_for_user_robey,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_auth_with_pubkey_cannot_be_decoded_by_multipart_form,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_auth_with_pubkey_exceeds_key_max_size,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_auth_with_valid_pubkey_by_multipart_form,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_auth_with_valid_pubkey_by_urlencoded_form,tests/test_app.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_post_form_with_large_body_size_by_multipart_form,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_post_form_with_large_body_size_by_urlencoded_form,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_correct_credentials,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_correct_credentials_but_empty_id,tests/test_app.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_correct_credentials_but_ip_not_matched,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_correct_credentials_but_without_id_argument,tests/test_app.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_correct_credentials_but_with_no_port,tests/test_app.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_correct_credentials_but_wrong_id,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_correct_credentials_timeout,tests/test_app.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_correct_credentials_user_bar,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_correct_credentials_user_robey,tests/test_app.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_invalid_form_for_invalid_value,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_invalid_form_for_missing_argument,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_user_keyonly_for_bad_authentication_type,tests/test_app.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_user_pass2fa_with_correct_passwords,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_user_pass2fa_with_wrong_pkey_correct_passwords,tests/test_app.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_user_pkey2fa_with_correct_passwords,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_user_pkey2fa_with_empty_passcode,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_user_pkey2fa_with_wrong_passcode,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_user_pkey2fa_with_wrong_password,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_wrong_credentials,tests/test_app.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_wrong_hostname_domain,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_wrong_hostname_ip,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/huashengdun/webssh,51d527fe75a62aed239126a6697749a30baecb30,tests/test_app.py::TestAppBasic::test_app_with_wrong_port,tests/test_app.py,NOD,flaky,nan,nan,classify;root_cause,"import json import random import threading import tornado.websocket import tornado.gen from tornado.testing import AsyncHTTPTestCase from tornado.httpclient import HTTPError from tornado.options import options from tests.sshserver import run_ssh_server, banner, Server from tests.utils import encode_multipart_formdata, read_file, make_tests_data_path # noqa from webssh import handler from webssh.main import make_app, make_handlers from webssh.settings import ( get_app_settings, get_server_settings, max_body_size ) from webssh.utils import to_str from webssh.worker import clients try: from urllib.parse import urlencode except ImportError: from urllib import urlencode handler.DELAY = 0.1 swallow_http_errors = handler.swallow_http_errors server_encodings = {e.strip() for e in Server.encodings} class TestAppBase(AsyncHTTPTestCase): def get_httpserver_options(self): return get_server_settings(options) def assert_response(self, bstr, response): if swallow_http_errors: self.assertEqual(response.code, 200) self.assertIn(bstr, response.body) else: self.assertEqual(response.code, 400) self.assertIn(b'Bad Request', response.body) def assert_status_in(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertIn(status, data['status']) def assert_status_equal(self, status, data): self.assertIsNone(data['encoding']) self.assertIsNone(data['id']) self.assertEqual(status, data['status']) def assert_status_none(self, data): self.assertIsNotNone(data['encoding']) self.assertIsNotNone(data['id']) self.assertIsNone(data['status']) def fetch_request(self, url, method='GET', body='', headers={}, sync=True): if not sync and url.startswith('/'): url = self.get_url(url) if isinstance(body, dict): body = urlencode(body) if not headers: headers = self.headers else: headers.update(self.headers) client = self if sync else self.get_http_client() return client.fetch(url, method=method, body=body, headers=headers) def sync_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers) def async_post(self, url, body, headers={}): return self.fetch_request(url, 'POST', body, headers, sync=False) class TestAppBasic(TestAppBase): running = [True] sshserver_port = 2200 body = 'hostname=127.0.0.1&port={}&_xsrf=yummy&username=robey&password=foo'.format(sshserver_port) # noqa headers = {'Cookie': '_xsrf=yummy'} def get_app(self): self.body_dict = { 'hostname': '127.0.0.1', 'port': str(self.sshserver_port), 'username': 'robey', 'password': '', '_xsrf': 'yummy' } loop = self.io_loop options.debug = False options.policy = random.choice(['warning', 'autoadd']) options.hostfile = '' options.syshostfile = '' options.tdstream = '' app = make_app(make_handlers(loop, options), get_app_settings(options)) return app @classmethod def setUpClass(cls): print('='*20) t = threading.Thread( target=run_ssh_server, args=(cls.sshserver_port, cls.running) ) t.setDaemon(True) t.start() @classmethod def tearDownClass(cls): cls.running.pop() print('='*20) def test_app_with_invalid_form_for_missing_argument(self): response = self.fetch('/') self.assertEqual(response.code, 200) body = 'port=7000&username=admin&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument hostname', response) body = 'hostname=127.0.0.1&port=7000&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing argument username', response) body = 'hostname=&port=&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value hostname', response) body = 'hostname=127.0.0.1&port=7000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Missing value username', response) def test_app_with_invalid_form_for_invalid_value(self): body = 'hostname=127.0.0&port=22&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=http://www.googe.com&port=22&username=&password&_xsrf=yummy' # noqa response = self.sync_post('/', body) self.assert_response(b'Invalid hostname', response) body = 'hostname=127.0.0.1&port=port&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) body = 'hostname=127.0.0.1&port=70000&username=&password&_xsrf=yummy' response = self.sync_post('/', body) self.assert_response(b'Invalid port', response) def test_app_with_wrong_hostname_ip(self): body = 'hostname=127.0.0.2&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_hostname_domain(self): body = 'hostname=xxxxxxxxxxxx&port=2200&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_port(self): body = 'hostname=127.0.0.1&port=7000&username=admin&_xsrf=yummy' response = self.sync_post('/', body) self.assertEqual(response.code, 200) self.assertIn(b'Unable to connect to', response.body) def test_app_with_wrong_credentials(self): response = self.sync_post('/', self.body + 's') self.assert_status_in('Authentication failed.', json.loads(to_str(response.body))) # noqa def test_app_with_correct_credentials(self): response = self.sync_post('/', self.body) self.assert_status_none(json.loads(to_str(response.body))) def test_app_with_correct_credentials_but_with_no_port(self): default_port = handler.DEFAULT_PORT handler.DEFAULT_PORT = self.sshserver_port # with no port value body = self.body.replace(str(self.sshserver_port), '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) # with no port argument body = body.replace('port=&', '') response = self.sync_post('/', body) self.assert_status_none(json.loads(to_str(response.body))) handler.DEFAULT_PORT = default_port @tornado.testing.gen_test def test_app_with_correct_credentials_timeout(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] yield tornado.gen.sleep(handler.DELAY + 0.1) ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') @tornado.testing.gen_test def test_app_with_correct_credentials_but_ip_not_matched(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) clients = handler.clients handler.clients = {} url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertEqual(ws.close_reason, 'Websocket authentication failed.') handler.clients = clients @tornado.testing.gen_test def test_app_with_correct_credentials_user_robey(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' + data['id'] ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertEqual(to_str(msg, data['encoding']), banner) ws.close() @tornado.testing.gen_test def test_app_with_correct_credentials_but_without_id_argument(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing argument id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_but_empty_id(self): url = self.get_url('/') response = yield self.async_post(url, self.body) data = json.loads(to_str(response.body)) self.assert_status_none(data) url = url.replace('http', 'ws') ws_url = url + 'ws?id=' ws = yield tornado.websocket.websocket_connect(ws_url) msg = yield ws.read_message() self.assertIsNone(msg) self.assertIn('Missing value id', ws.close_reason) @tornado.testing.gen_test def test_app_with_correct_credentials_", https://github.com/hyperledger/aries-staticagent-python,62f0e4e435e4e324f76a8fb724d0f733192bc0c3,tests/test_pack_unpack.py::test_pack_unpack_anon,tests/test_pack_unpack.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,""""""" Test Pack and Unpack. """""" import pytest from aries_staticagent import StaticConnection, Keys, Message, crypto, utils # pylint: disable=redefined-outer-name @pytest.fixture(scope='module') def alice_keys(): """"""Generate alice's keys."""""" yield Keys(*crypto.create_keypair()) @pytest.fixture(scope='module') def bob_keys(): """"""Generate bob's keys."""""" yield Keys(*crypto.create_keypair()) @pytest.fixture(scope='module') def alice(alice_keys, bob_keys): """""" Create Alice's StaticConnection. """""" yield StaticConnection.from_parts(alice_keys, their_vk=bob_keys.verkey) @pytest.fixture(scope='module') def bob(bob_keys, alice_keys): """""" Create Bob's StaticConnection. """""" yield StaticConnection.from_parts(bob_keys, their_vk=alice_keys.verkey) def test_pack_unpack_auth(alice, bob): """""" Test the pack-unpack loop with authcrypt. """""" msg = Message({'@type': 'doc;protocol/1.0/name'}) packed_msg = alice.pack(msg) assert isinstance(packed_msg, bytes) unpacked_msg = bob.unpack(packed_msg) assert isinstance(unpacked_msg, Message) assert hasattr(unpacked_msg, 'mtc') assert unpacked_msg.mtc.is_authcrypted() assert unpacked_msg.mtc.sender == alice.verkey_b58 assert unpacked_msg.mtc.recipient == bob.verkey_b58 def test_pack_unpack_anon(alice, bob): """""" Test the pack-unpack loop with anoncrypt. """""" msg = {'@type': 'doc;protocol/1.0/name'} packed_msg = alice.pack(msg, anoncrypt=True) assert isinstance(packed_msg, bytes) unpacked_msg = bob.unpack(packed_msg) assert isinstance(unpacked_msg, Message) assert hasattr(unpacked_msg, 'mtc') assert unpacked_msg.mtc.is_anoncrypted() assert unpacked_msg.mtc.sender is None assert unpacked_msg.mtc.recipient == bob.verkey_b58 def test_pack_unpack_plaintext(alice, bob): """"""Test pack/unpack in plaintext."""""" msg = {'@type': 'doc;protocol/1.0/name'} packed_msg = alice.pack(msg, plaintext=True) assert isinstance(packed_msg, bytes) unpacked_msg = bob.unpack(packed_msg) assert isinstance(unpacked_msg, Message) assert hasattr(unpacked_msg, 'mtc') assert unpacked_msg.mtc.is_plaintext() assert unpacked_msg.mtc.sender is None assert unpacked_msg.mtc.recipient is None def test_plaintext_and_anoncrypt_raises_error(alice): """"""Test specifying both plaintext and anoncrypt raises an error."""""" with pytest.raises(ValueError): alice.pack({'test': 'test'}, plaintext=True, anoncrypt=True) def test_pack_unpack_with_routing_keys(alice, bob): """"""Test packing for a connection with routing keys."""""" route1 = StaticConnection.from_parts(crypto.create_keypair()) route2 = StaticConnection.from_parts(crypto.create_keypair()) alice.target.update(routing_keys=[route1.verkey, route2.verkey]) packed_message = alice.pack({'@type': 'doc;protocol/1.0/name'}) route2_msg = route2.unpack(packed_message) assert route2_msg.type == utils.FORWARD assert route2_msg['to'] == route1.verkey_b58 assert route2_msg.mtc.is_anoncrypted() assert route2_msg.mtc.sender is None route1_msg = route1.unpack(route2_msg['msg']) assert route1_msg.type == utils.FORWARD assert route1_msg['to'] == bob.verkey_b58 assert route1_msg.mtc.is_anoncrypted() assert route1_msg.mtc.sender is None bob_msg = bob.unpack(route1_msg['msg']) assert bob_msg.type == 'doc;protocol/1.0/name' assert bob_msg.mtc.is_authcrypted() assert bob_msg.mtc.sender == alice.verkey_b58 assert bob_msg.mtc.recipient == bob.verkey_b58 def test_bad_input(alice): """""" Test that bad input raises an error in pack. """""" with pytest.raises(TypeError): alice.pack('blah') ", https://github.com/hyperledger/aries-staticagent-python,62f0e4e435e4e324f76a8fb724d0f733192bc0c3,tests/test_pack_unpack.py::test_pack_unpack_auth,tests/test_pack_unpack.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,""""""" Test Pack and Unpack. """""" import pytest from aries_staticagent import StaticConnection, Keys, Message, crypto, utils # pylint: disable=redefined-outer-name @pytest.fixture(scope='module') def alice_keys(): """"""Generate alice's keys."""""" yield Keys(*crypto.create_keypair()) @pytest.fixture(scope='module') def bob_keys(): """"""Generate bob's keys."""""" yield Keys(*crypto.create_keypair()) @pytest.fixture(scope='module') def alice(alice_keys, bob_keys): """""" Create Alice's StaticConnection. """""" yield StaticConnection.from_parts(alice_keys, their_vk=bob_keys.verkey) @pytest.fixture(scope='module') def bob(bob_keys, alice_keys): """""" Create Bob's StaticConnection. """""" yield StaticConnection.from_parts(bob_keys, their_vk=alice_keys.verkey) def test_pack_unpack_auth(alice, bob): """""" Test the pack-unpack loop with authcrypt. """""" msg = Message({'@type': 'doc;protocol/1.0/name'}) packed_msg = alice.pack(msg) assert isinstance(packed_msg, bytes) unpacked_msg = bob.unpack(packed_msg) assert isinstance(unpacked_msg, Message) assert hasattr(unpacked_msg, 'mtc') assert unpacked_msg.mtc.is_authcrypted() assert unpacked_msg.mtc.sender == alice.verkey_b58 assert unpacked_msg.mtc.recipient == bob.verkey_b58 def test_pack_unpack_anon(alice, bob): """""" Test the pack-unpack loop with anoncrypt. """""" msg = {'@type': 'doc;protocol/1.0/name'} packed_msg = alice.pack(msg, anoncrypt=True) assert isinstance(packed_msg, bytes) unpacked_msg = bob.unpack(packed_msg) assert isinstance(unpacked_msg, Message) assert hasattr(unpacked_msg, 'mtc') assert unpacked_msg.mtc.is_anoncrypted() assert unpacked_msg.mtc.sender is None assert unpacked_msg.mtc.recipient == bob.verkey_b58 def test_pack_unpack_plaintext(alice, bob): """"""Test pack/unpack in plaintext."""""" msg = {'@type': 'doc;protocol/1.0/name'} packed_msg = alice.pack(msg, plaintext=True) assert isinstance(packed_msg, bytes) unpacked_msg = bob.unpack(packed_msg) assert isinstance(unpacked_msg, Message) assert hasattr(unpacked_msg, 'mtc') assert unpacked_msg.mtc.is_plaintext() assert unpacked_msg.mtc.sender is None assert unpacked_msg.mtc.recipient is None def test_plaintext_and_anoncrypt_raises_error(alice): """"""Test specifying both plaintext and anoncrypt raises an error."""""" with pytest.raises(ValueError): alice.pack({'test': 'test'}, plaintext=True, anoncrypt=True) def test_pack_unpack_with_routing_keys(alice, bob): """"""Test packing for a connection with routing keys."""""" route1 = StaticConnection.from_parts(crypto.create_keypair()) route2 = StaticConnection.from_parts(crypto.create_keypair()) alice.target.update(routing_keys=[route1.verkey, route2.verkey]) packed_message = alice.pack({'@type': 'doc;protocol/1.0/name'}) route2_msg = route2.unpack(packed_message) assert route2_msg.type == utils.FORWARD assert route2_msg['to'] == route1.verkey_b58 assert route2_msg.mtc.is_anoncrypted() assert route2_msg.mtc.sender is None route1_msg = route1.unpack(route2_msg['msg']) assert route1_msg.type == utils.FORWARD assert route1_msg['to'] == bob.verkey_b58 assert route1_msg.mtc.is_anoncrypted() assert route1_msg.mtc.sender is None bob_msg = bob.unpack(route1_msg['msg']) assert bob_msg.type == 'doc;protocol/1.0/name' assert bob_msg.mtc.is_authcrypted() assert bob_msg.mtc.sender == alice.verkey_b58 assert bob_msg.mtc.recipient == bob.verkey_b58 def test_bad_input(alice): """""" Test that bad input raises an error in pack. """""" with pytest.raises(TypeError): alice.pack('blah') ", https://github.com/hypoport/python-aws-dataclasses,30bfe90457a83957d10ec18fe7d61439b5b74280,tests/test_sns_event.py::test_can_handle_missing_fields,tests/test_sns_event.py,NIO,flaky,Opened,https://github.com/hypoport/python-aws-dataclasses/pull/13,classify;root_cause,"import datetime import pytest from dateutil.tz import tzutc from aws_dataclasses.sns_event import SnsEvent, SnsMessage from .util import get_event_dict @pytest.fixture(scope=""module"") def sns_event_raw(): return get_event_dict(""sns-event.json"") @pytest.fixture(scope=""module"") def sns_event(sns_event_raw): return SnsEvent.from_event(sns_event_raw) def test_sns_record(sns_event): assert sns_event.first_record is not None evt = sns_event.first_record assert evt.event_version == ""1.0"" assert evt.event_source == ""aws:sns"" def test_sns(sns_event): assert sns_event.first_record.sns is not None sns = sns_event.first_record.sns assert sns.message == ""Hello from SNS!"" assert sns.timestamp == datetime.datetime(1970, 1, 1, 0, 0, 12, tzinfo=tzutc()) assert int(sns.timestamp.timestamp()) == 12 assert sns.topic_arn == ""arn:aws:sns:EXAMPLE"" assert sns.signature == ""EXAMPLE"" def test_message_attributes(sns_event): msg_attr = sns_event.first_record.sns.message_attributes assert msg_attr.get(""Test"").value == ""TestString"" assert msg_attr.get(""Test"").type == ""String"" def test_can_handle_missing_fields(sns_event_raw): sns = sns_event_raw[""Records""][0][""Sns""] sns.pop(""MessageAttributes"") res = SnsMessage.from_json(sns) assert res.message_attributes is None def test_can_handle_extra_fields(sns_event_raw): sns = sns_event_raw[""Records""][0][""Sns""] sns[""TestAttr""] = 1234 res = SnsMessage.from_json(sns) assert res is not None ", https://github.com/hypoport/python-aws-dataclasses,30bfe90457a83957d10ec18fe7d61439b5b74280,tests/test_sns_event.py::test_message_attributes,tests/test_sns_event.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import datetime import pytest from dateutil.tz import tzutc from aws_dataclasses.sns_event import SnsEvent, SnsMessage from .util import get_event_dict @pytest.fixture(scope=""module"") def sns_event_raw(): return get_event_dict(""sns-event.json"") @pytest.fixture(scope=""module"") def sns_event(sns_event_raw): return SnsEvent.from_event(sns_event_raw) def test_sns_record(sns_event): assert sns_event.first_record is not None evt = sns_event.first_record assert evt.event_version == ""1.0"" assert evt.event_source == ""aws:sns"" def test_sns(sns_event): assert sns_event.first_record.sns is not None sns = sns_event.first_record.sns assert sns.message == ""Hello from SNS!"" assert sns.timestamp == datetime.datetime(1970, 1, 1, 0, 0, 12, tzinfo=tzutc()) assert int(sns.timestamp.timestamp()) == 12 assert sns.topic_arn == ""arn:aws:sns:EXAMPLE"" assert sns.signature == ""EXAMPLE"" def test_message_attributes(sns_event): msg_attr = sns_event.first_record.sns.message_attributes assert msg_attr.get(""Test"").value == ""TestString"" assert msg_attr.get(""Test"").type == ""String"" def test_can_handle_missing_fields(sns_event_raw): sns = sns_event_raw[""Records""][0][""Sns""] sns.pop(""MessageAttributes"") res = SnsMessage.from_json(sns) assert res.message_attributes is None def test_can_handle_extra_fields(sns_event_raw): sns = sns_event_raw[""Records""][0][""Sns""] sns[""TestAttr""] = 1234 res = SnsMessage.from_json(sns) assert res is not None ", https://github.com/identixone/identixone-python,232e5dfcf98ebe91a3ed433a265be161ee965a5e,tests/api/test_api_client.py::TestAPIClient::test_no_token_raises_exception,tests/api/test_api_client.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import unittest import uuid from identixone.api.client import Client from identixone.base.exceptions import IdentixOneException class TestAPIClient(unittest.TestCase): def __init__(self, *args, **kwargs): self.token = uuid.uuid4().hex super(TestAPIClient, self).__init__(*args, **kwargs) def test_unsupported_version_raises_exception(self): with self.assertRaises(IdentixOneException): Client(token=self.token, version=None) def test_no_version_raises_exception(self): with self.assertRaises(IdentixOneException): Client(token=self.token, version=9999) def test_no_token_raises_exception(self): with self.assertRaises(IdentixOneException): Client(token=None, version=1) def test_token_get_from_environ(self): os.environ['IDENTIXONE_TOKEN'] = '123lskdfjldksjf123' client = Client(version='1') self.assertEqual(client.token, '123lskdfjldksjf123') def test_version_get_from_environ(self): os.environ['IDENTIXONE_VERSION'] = '1' client = Client(token='adflsdkfjsakldfj') self.assertEqual(client.version, '1') def test_create_instance_http_client(self): class A: def __init__(self, auth_token): self.token = auth_token client = Client(token='sdfdsf', version='1', http_client=A) self.assertTrue(isinstance(client.http_client, A)) def test_http_client_raises_exception(self): a = 'http_client' with self.assertRaises(IdentixOneException): Client(token='sdfdsf', version='1', http_client=a) ", https://github.com/identixone/identixone-python,232e5dfcf98ebe91a3ed433a265be161ee965a5e,tests/api/test_api_client.py::TestAPIClient::test_unsupported_version_raises_exception,tests/api/test_api_client.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import unittest import uuid from identixone.api.client import Client from identixone.base.exceptions import IdentixOneException class TestAPIClient(unittest.TestCase): def __init__(self, *args, **kwargs): self.token = uuid.uuid4().hex super(TestAPIClient, self).__init__(*args, **kwargs) def test_unsupported_version_raises_exception(self): with self.assertRaises(IdentixOneException): Client(token=self.token, version=None) def test_no_version_raises_exception(self): with self.assertRaises(IdentixOneException): Client(token=self.token, version=9999) def test_no_token_raises_exception(self): with self.assertRaises(IdentixOneException): Client(token=None, version=1) def test_token_get_from_environ(self): os.environ['IDENTIXONE_TOKEN'] = '123lskdfjldksjf123' client = Client(version='1') self.assertEqual(client.token, '123lskdfjldksjf123') def test_version_get_from_environ(self): os.environ['IDENTIXONE_VERSION'] = '1' client = Client(token='adflsdkfjsakldfj') self.assertEqual(client.version, '1') def test_create_instance_http_client(self): class A: def __init__(self, auth_token): self.token = auth_token client = Client(token='sdfdsf', version='1', http_client=A) self.assertTrue(isinstance(client.http_client, A)) def test_http_client_raises_exception(self): a = 'http_client' with self.assertRaises(IdentixOneException): Client(token='sdfdsf', version='1', http_client=a) ", https://github.com/identixone/identixone-python,232e5dfcf98ebe91a3ed433a265be161ee965a5e,tests/utils/test_environment.py::TestEnvironmentVar::test_dict_environ_invalid_without_prefix,tests/utils/test_environment.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import unittest from identixone.base.exceptions import ImproperlyConfigured from identixone.utils.environment import env_var, ENV_BASE_PREFIX class TestEnvironmentVar(unittest.TestCase): def test_os_environ_valid_var(self): key = 'KEY' value = 'VALUE' os.environ['{}{}'.format(ENV_BASE_PREFIX, key)] = value self.assertEqual(env_var(os.environ, key), value) def test_os_environ_invalid_without_prefix(self): key = 'KEY' value = 'VALUE' os.environ[key] = value self.assertIsNone(env_var(os.environ, key)) def test_dict_environ_valid_var(self): environ = {} key = 'KEY' value = 'VALUE' environ['{}{}'.format(ENV_BASE_PREFIX, key)] = value self.assertEqual(env_var(environ, key), value) def test_dict_environ_invalid_without_prefix(self): environ = {} key = 'KEY' value = 'VALUE' environ[key] = value self.assertIsNone(env_var(os.environ, key)) def test_invalid_environ_raises_exception(self): key = 'KEY' with self.assertRaises(ImproperlyConfigured): env_var([], key) ", https://github.com/identixone/identixone-python,232e5dfcf98ebe91a3ed433a265be161ee965a5e,tests/utils/test_environment.py::TestEnvironmentVar::test_os_environ_invalid_without_prefix,tests/utils/test_environment.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import unittest from identixone.base.exceptions import ImproperlyConfigured from identixone.utils.environment import env_var, ENV_BASE_PREFIX class TestEnvironmentVar(unittest.TestCase): def test_os_environ_valid_var(self): key = 'KEY' value = 'VALUE' os.environ['{}{}'.format(ENV_BASE_PREFIX, key)] = value self.assertEqual(env_var(os.environ, key), value) def test_os_environ_invalid_without_prefix(self): key = 'KEY' value = 'VALUE' os.environ[key] = value self.assertIsNone(env_var(os.environ, key)) def test_dict_environ_valid_var(self): environ = {} key = 'KEY' value = 'VALUE' environ['{}{}'.format(ENV_BASE_PREFIX, key)] = value self.assertEqual(env_var(environ, key), value) def test_dict_environ_invalid_without_prefix(self): environ = {} key = 'KEY' value = 'VALUE' environ[key] = value self.assertIsNone(env_var(os.environ, key)) def test_invalid_environ_raises_exception(self): key = 'KEY' with self.assertRaises(ImproperlyConfigured): env_var([], key) ", https://github.com/irmen/Tale,a2a26443465ab6978b32d9253e833471500e7b68,tests/test_commands.py::TestCommandDecorators::test_cmd_decorator,tests/test_commands.py,NIO,flaky,RepoArchived,nan,classify;root_cause,""""""" Unit tests for commands definitions and decorators 'Tale' mud driver, mudlib and interactive fiction framework Copyright by Irmen de Jong (irmen@razorvine.net) """""" import pytest import inspect from typing import Generator import tale.cmds from tale.cmds import cmdfunc_signature_valid, disable_notify_action, disabled_in_gamemode, overrides_soul, no_soul_parse, cmd, wizcmd from tale.story import GameMode from tale.player import Player from tale.util import Context from tale.errors import TaleError from tale.base import ParseResult class TestCommandDecorators: def test_simple_decorators(self): @disable_notify_action @disabled_in_gamemode(GameMode.IF) @overrides_soul @no_soul_parse def func(): pass assert func.enable_notify_action == False assert func.disabled_in_mode == GameMode.IF assert func.overrides_soul == True assert func.no_soul_parse == True def test_signature_checker(self): def wrong_func(a, b): pass def ok_func(player: Player, parsed: ParseResult, ctx: Context) -> None: """"""some docstring"""""" pass def ok_func2(player, parsed, ctx): """"""some docstring"""""" pass def wrong_func2(player: Player, parsed: ParseResult, ctx: Context) -> None: pass # no docstring, otherwise ok def wrong_func3(player, parsed, context): pass assert cmdfunc_signature_valid(ok_func) assert cmdfunc_signature_valid(ok_func2) assert not cmdfunc_signature_valid(wrong_func) assert not cmdfunc_signature_valid(wrong_func2) assert not cmdfunc_signature_valid(wrong_func3) def test_decorators_arg_check(self): with pytest.raises(TypeError): cmd() with pytest.raises(TypeError): wizcmd() with pytest.raises(TypeError): cmd(44) with pytest.raises(TypeError): wizcmd(44) with pytest.raises(TypeError): cmd(""name"", ""alias1"", 42) with pytest.raises(TypeError): wizcmd(""name"", ""alias1"", 42) def test_cmd_decorator(self): x = cmd(""something"") assert inspect.isfunction(x) x = cmd(""something"", ""alias1"", ""alias2"") assert inspect.isfunction(x) with pytest.raises(TaleError): def wrongfunc(): pass cmd(""wrongfunc"")(wrongfunc) def cmdfunc(player: Player, parsed: ParseResult, ctx: Context) -> None: """"""some docstring"""""" pass def cmdgenerator(player: Player, parsed: ParseResult, ctx: Context) -> Generator: """"""some docstring"""""" yield 42 result = cmd(""cmdfunc"")(cmdfunc) assert not result.is_generator result = cmd(""cmdgenerator"")(cmdgenerator) assert result.is_generator assert ""cmdfunc"" in tale.cmds._all_commands assert ""cmdfunc"" not in tale.cmds._all_wizard_commands def test_wizcmd_decorator(self): x = wizcmd(""something"") assert inspect.isfunction(x) x = wizcmd(""something"", ""alias1"", ""alias2"") assert inspect.isfunction(x) with pytest.raises(TaleError): def wrongfunc(): pass wizcmd(""wrongfunc"")(wrongfunc) def wizfunc(player: Player, parsed: ParseResult, ctx: Context) -> None: """"""some docstring"""""" pass def wizgenerator(player: Player, parsed: ParseResult, ctx: Context) -> Generator: """"""some docstring"""""" yield 42 result = wizcmd(""wizfunc"")(wizfunc) assert not result.is_generator result = wizcmd(""wizgenerator"")(wizgenerator) assert result.is_generator assert ""!wizfunc"" in tale.cmds._all_wizard_commands assert ""wizfunc"" not in tale.cmds._all_wizard_commands assert ""!wizfunc"" not in tale.cmds._all_commands assert ""wizfunc"" not in tale.cmds._all_commands def test_no_duplicates(self): def testfunc(player: Player, parsed: ParseResult, ctx: Context) -> None: """"""some docstring"""""" pass cmd(""name1"")(testfunc) with pytest.raises(ValueError) as x: cmd(""name1"")(testfunc) assert str(x.value) == ""command defined more than once: name1"" wizcmd(""name1"")(testfunc) wizcmd(""name1"")(testfunc) class TestNormalCommandFunctions(): # @todo tests for normal command functions # normal.replace_items() # normal.take_stuff() # normal.give_stuff() # normal.print_item_removal() # normal.remove_is_are_args() pass class TestWizardCommandFunctions(): # @todo tests for wizard command functions pass ", https://github.com/irmen/Tale,a2a26443465ab6978b32d9253e833471500e7b68,tests/test_commands.py::TestCommandDecorators::test_no_duplicates,tests/test_commands.py,NIO,flaky,RepoArchived,nan,classify;root_cause,""""""" Unit tests for commands definitions and decorators 'Tale' mud driver, mudlib and interactive fiction framework Copyright by Irmen de Jong (irmen@razorvine.net) """""" import pytest import inspect from typing import Generator import tale.cmds from tale.cmds import cmdfunc_signature_valid, disable_notify_action, disabled_in_gamemode, overrides_soul, no_soul_parse, cmd, wizcmd from tale.story import GameMode from tale.player import Player from tale.util import Context from tale.errors import TaleError from tale.base import ParseResult class TestCommandDecorators: def test_simple_decorators(self): @disable_notify_action @disabled_in_gamemode(GameMode.IF) @overrides_soul @no_soul_parse def func(): pass assert func.enable_notify_action == False assert func.disabled_in_mode == GameMode.IF assert func.overrides_soul == True assert func.no_soul_parse == True def test_signature_checker(self): def wrong_func(a, b): pass def ok_func(player: Player, parsed: ParseResult, ctx: Context) -> None: """"""some docstring"""""" pass def ok_func2(player, parsed, ctx): """"""some docstring"""""" pass def wrong_func2(player: Player, parsed: ParseResult, ctx: Context) -> None: pass # no docstring, otherwise ok def wrong_func3(player, parsed, context): pass assert cmdfunc_signature_valid(ok_func) assert cmdfunc_signature_valid(ok_func2) assert not cmdfunc_signature_valid(wrong_func) assert not cmdfunc_signature_valid(wrong_func2) assert not cmdfunc_signature_valid(wrong_func3) def test_decorators_arg_check(self): with pytest.raises(TypeError): cmd() with pytest.raises(TypeError): wizcmd() with pytest.raises(TypeError): cmd(44) with pytest.raises(TypeError): wizcmd(44) with pytest.raises(TypeError): cmd(""name"", ""alias1"", 42) with pytest.raises(TypeError): wizcmd(""name"", ""alias1"", 42) def test_cmd_decorator(self): x = cmd(""something"") assert inspect.isfunction(x) x = cmd(""something"", ""alias1"", ""alias2"") assert inspect.isfunction(x) with pytest.raises(TaleError): def wrongfunc(): pass cmd(""wrongfunc"")(wrongfunc) def cmdfunc(player: Player, parsed: ParseResult, ctx: Context) -> None: """"""some docstring"""""" pass def cmdgenerator(player: Player, parsed: ParseResult, ctx: Context) -> Generator: """"""some docstring"""""" yield 42 result = cmd(""cmdfunc"")(cmdfunc) assert not result.is_generator result = cmd(""cmdgenerator"")(cmdgenerator) assert result.is_generator assert ""cmdfunc"" in tale.cmds._all_commands assert ""cmdfunc"" not in tale.cmds._all_wizard_commands def test_wizcmd_decorator(self): x = wizcmd(""something"") assert inspect.isfunction(x) x = wizcmd(""something"", ""alias1"", ""alias2"") assert inspect.isfunction(x) with pytest.raises(TaleError): def wrongfunc(): pass wizcmd(""wrongfunc"")(wrongfunc) def wizfunc(player: Player, parsed: ParseResult, ctx: Context) -> None: """"""some docstring"""""" pass def wizgenerator(player: Player, parsed: ParseResult, ctx: Context) -> Generator: """"""some docstring"""""" yield 42 result = wizcmd(""wizfunc"")(wizfunc) assert not result.is_generator result = wizcmd(""wizgenerator"")(wizgenerator) assert result.is_generator assert ""!wizfunc"" in tale.cmds._all_wizard_commands assert ""wizfunc"" not in tale.cmds._all_wizard_commands assert ""!wizfunc"" not in tale.cmds._all_commands assert ""wizfunc"" not in tale.cmds._all_commands def test_no_duplicates(self): def testfunc(player: Player, parsed: ParseResult, ctx: Context) -> None: """"""some docstring"""""" pass cmd(""name1"")(testfunc) with pytest.raises(ValueError) as x: cmd(""name1"")(testfunc) assert str(x.value) == ""command defined more than once: name1"" wizcmd(""name1"")(testfunc) wizcmd(""name1"")(testfunc) class TestNormalCommandFunctions(): # @todo tests for normal command functions # normal.replace_items() # normal.take_stuff() # normal.give_stuff() # normal.print_item_removal() # normal.remove_is_are_args() pass class TestWizardCommandFunctions(): # @todo tests for wizard command functions pass ", https://github.com/irmen/Tale,a2a26443465ab6978b32d9253e833471500e7b68,tests/test_pubsub.py::TestPubsub::test_idletime,tests/test_pubsub.py,NOD,flaky,RepoArchived,nan,classify;root_cause,""""""" Unittests for Pubsub 'Tale' mud driver, mudlib and interactive fiction framework Copyright by Irmen de Jong (irmen@razorvine.net) """""" import gc import time import unittest from tale.pubsub import topic, unsubscribe_all, Listener, sync, pending class Subber(Listener): def __init__(self, name): self.messages = [] self.name = name def pubsub_event(self, topicname, event): self.messages.append((topicname, event)) return self.name def clear(self): self.messages = [] class RefusingSubber(Subber): def pubsub_event(self, topicname, event): raise Listener.NotYet class TestPubsub(unittest.TestCase): def test_global_namespace(self): s1 = topic(""s1"") s2 = topic(""s2"") s3 = topic(""s1"") self.assertTrue(s1 is s3) self.assertFalse(s1 is s2) def test_aggregate_topic_name(self): s1 = topic((""t"", 42)) s2 = topic((""t"", 55)) s3 = topic((""t"", 42)) self.assertTrue(s1 is s3) self.assertFalse(s1 is s2) def test_pubsub_sync(self): sync() s = topic(""testsync"") subber = Subber(""sub1"") subber2 = Subber(""sub2"") s.subscribe(subber) s.subscribe(subber) s.subscribe(subber2) s.subscribe(subber2) result = s.send([1, 2, 3], True) self.assertEqual([(""testsync"", [1, 2, 3])], subber.messages) self.assertEqual([(""testsync"", [1, 2, 3])], subber2.messages) self.assertEqual(2, len(result)) self.assertTrue(""sub1"" in result) self.assertTrue(""sub2"" in result) # check explicit unsubscribe s.unsubscribe(subber) s.unsubscribe(subber) s.unsubscribe(subber2) result = s.send(""after unsubscribing"", True) self.assertEqual(0, len(result)) def test_pubsub_async(self): sync() s = topic(""test1async"") subber = Subber(""sub1"") subber2 = Subber(""sub2"") s.subscribe(subber) s.subscribe(subber2) s2 = topic(""test1async"") result = s2.send(""event1"") self.assertIsNone(result) self.assertEqual([], subber.messages) self.assertEqual([], subber2.messages) events, idle, subbers = pending()[""test1async""] self.assertEqual(1, events) result = sync() events, idle, subbers = pending()[""test1async""] self.assertEqual(0, events) self.assertEqual([], result) self.assertEqual([(""test1async"", ""event1"")], subber.messages) self.assertEqual([(""test1async"", ""event1"")], subber2.messages) subber.clear() subber2.clear() s2.send(""event2"") result = sync(""test1async"") self.assertEqual(2, len(result)) self.assertTrue(""sub1"" in result) self.assertTrue(""sub2"" in result) self.assertEqual([(""test1async"", ""event2"")], subber.messages) self.assertEqual([(""test1async"", ""event2"")], subber2.messages) def test_notyet(self): s = topic(""notyet"") subber = RefusingSubber(""refuser"") s.subscribe(subber) s.send(""event"", True) self.assertEqual([], subber.messages) def test_weakrefs(self): s = topic(""test222"") subber = Subber(""sub1"") s.subscribe(subber) del subber gc.collect() result = s.send(""after gc"", True) self.assertEqual(0, len(result)) def test_weakrefs2(self): class Wiretap(Listener): def __init__(self): self.messages = [] def create_tap(self): tap = topic(""wiretaptest"") tap.subscribe(self) def pubsub_event(self, topicname, event): self.messages.append((topicname, event)) return 99 wiretap = Wiretap() wiretap.create_tap() t = topic(""wiretaptest"") result = t.send(""hi"", True) self.assertEqual(1, len(result)) self.assertEqual([('wiretaptest', 'hi')], wiretap.messages) del wiretap gc.collect() result = t.send(""after gc"", True) self.assertEqual(0, len(result)) def test_unsubscribe_all(self): s1 = topic(""testA"") s2 = topic(""testB"") s3 = topic(""testC"") subber = Subber(""sub1"") s1.subscribe(subber) s2.subscribe(subber) s3.subscribe(subber) s1.send(""one"") s2.send(""two"") s3.send(""three"") sync() self.assertEqual({('testA', 'one'), ('testB', 'two'), ('testC', 'three')}, set(subber.messages)) subber.clear() unsubscribe_all(subber) unsubscribe_all(subber) s1.send(""one"") s2.send(""two"") s3.send(""three"") sync() self.assertEqual([], subber.messages) def test_destroy(self): sync() s1 = topic(""testA"") s2 = topic(""testB"") s1.send(""123"") p = pending() self.assertIn(""testA"", p) self.assertIn(""testB"", p) s1.destroy() self.assertEqual("""", s1.name) p = pending() self.assertNotIn(""testA"", p) self.assertIn(""testB"", p) s2.destroy() p = pending() self.assertNotIn(""testA"", p) self.assertNotIn(""testB"", p) def test_idletime(self): sync() s = topic(""testA"") self.assertLess(s.idle_time, 0.1) time.sleep(0.2) self.assertGreater(s.idle_time, 0.1) s.send(""event"") self.assertLess(s.idle_time, 0.1) if __name__ == '__main__': unittest.main() ", https://github.com/irmen/Tale,a2a26443465ab6978b32d9253e833471500e7b68,tests/test_pubsub.py::TestPubsub::test_unsubscribe_all,tests/test_pubsub.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,""""""" Unittests for Pubsub 'Tale' mud driver, mudlib and interactive fiction framework Copyright by Irmen de Jong (irmen@razorvine.net) """""" import gc import time import unittest from tale.pubsub import topic, unsubscribe_all, Listener, sync, pending class Subber(Listener): def __init__(self, name): self.messages = [] self.name = name def pubsub_event(self, topicname, event): self.messages.append((topicname, event)) return self.name def clear(self): self.messages = [] class RefusingSubber(Subber): def pubsub_event(self, topicname, event): raise Listener.NotYet class TestPubsub(unittest.TestCase): def test_global_namespace(self): s1 = topic(""s1"") s2 = topic(""s2"") s3 = topic(""s1"") self.assertTrue(s1 is s3) self.assertFalse(s1 is s2) def test_aggregate_topic_name(self): s1 = topic((""t"", 42)) s2 = topic((""t"", 55)) s3 = topic((""t"", 42)) self.assertTrue(s1 is s3) self.assertFalse(s1 is s2) def test_pubsub_sync(self): sync() s = topic(""testsync"") subber = Subber(""sub1"") subber2 = Subber(""sub2"") s.subscribe(subber) s.subscribe(subber) s.subscribe(subber2) s.subscribe(subber2) result = s.send([1, 2, 3], True) self.assertEqual([(""testsync"", [1, 2, 3])], subber.messages) self.assertEqual([(""testsync"", [1, 2, 3])], subber2.messages) self.assertEqual(2, len(result)) self.assertTrue(""sub1"" in result) self.assertTrue(""sub2"" in result) # check explicit unsubscribe s.unsubscribe(subber) s.unsubscribe(subber) s.unsubscribe(subber2) result = s.send(""after unsubscribing"", True) self.assertEqual(0, len(result)) def test_pubsub_async(self): sync() s = topic(""test1async"") subber = Subber(""sub1"") subber2 = Subber(""sub2"") s.subscribe(subber) s.subscribe(subber2) s2 = topic(""test1async"") result = s2.send(""event1"") self.assertIsNone(result) self.assertEqual([], subber.messages) self.assertEqual([], subber2.messages) events, idle, subbers = pending()[""test1async""] self.assertEqual(1, events) result = sync() events, idle, subbers = pending()[""test1async""] self.assertEqual(0, events) self.assertEqual([], result) self.assertEqual([(""test1async"", ""event1"")], subber.messages) self.assertEqual([(""test1async"", ""event1"")], subber2.messages) subber.clear() subber2.clear() s2.send(""event2"") result = sync(""test1async"") self.assertEqual(2, len(result)) self.assertTrue(""sub1"" in result) self.assertTrue(""sub2"" in result) self.assertEqual([(""test1async"", ""event2"")], subber.messages) self.assertEqual([(""test1async"", ""event2"")], subber2.messages) def test_notyet(self): s = topic(""notyet"") subber = RefusingSubber(""refuser"") s.subscribe(subber) s.send(""event"", True) self.assertEqual([], subber.messages) def test_weakrefs(self): s = topic(""test222"") subber = Subber(""sub1"") s.subscribe(subber) del subber gc.collect() result = s.send(""after gc"", True) self.assertEqual(0, len(result)) def test_weakrefs2(self): class Wiretap(Listener): def __init__(self): self.messages = [] def create_tap(self): tap = topic(""wiretaptest"") tap.subscribe(self) def pubsub_event(self, topicname, event): self.messages.append((topicname, event)) return 99 wiretap = Wiretap() wiretap.create_tap() t = topic(""wiretaptest"") result = t.send(""hi"", True) self.assertEqual(1, len(result)) self.assertEqual([('wiretaptest', 'hi')], wiretap.messages) del wiretap gc.collect() result = t.send(""after gc"", True) self.assertEqual(0, len(result)) def test_unsubscribe_all(self): s1 = topic(""testA"") s2 = topic(""testB"") s3 = topic(""testC"") subber = Subber(""sub1"") s1.subscribe(subber) s2.subscribe(subber) s3.subscribe(subber) s1.send(""one"") s2.send(""two"") s3.send(""three"") sync() self.assertEqual({('testA', 'one'), ('testB', 'two'), ('testC', 'three')}, set(subber.messages)) subber.clear() unsubscribe_all(subber) unsubscribe_all(subber) s1.send(""one"") s2.send(""two"") s3.send(""three"") sync() self.assertEqual([], subber.messages) def test_destroy(self): sync() s1 = topic(""testA"") s2 = topic(""testB"") s1.send(""123"") p = pending() self.assertIn(""testA"", p) self.assertIn(""testB"", p) s1.destroy() self.assertEqual("""", s1.name) p = pending() self.assertNotIn(""testA"", p) self.assertIn(""testB"", p) s2.destroy() p = pending() self.assertNotIn(""testA"", p) self.assertNotIn(""testB"", p) def test_idletime(self): sync() s = topic(""testA"") self.assertLess(s.idle_time, 0.1) time.sleep(0.2) self.assertGreater(s.idle_time, 0.1) s.send(""event"") self.assertLess(s.idle_time, 0.1) if __name__ == '__main__': unittest.main() ", https://github.com/isac322/linux_aio,007d033b42703e9bb4d7cd99ca50e54108215d46,test/test_non_vector_rw.py::TestRW::test_02_read,test/test_non_vector_rw.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"# coding: UTF-8 import unittest import os from linux_aio_bind import IOCBPriorityClass, IOCBRWFlag from linux_aio import AIOContext, ReadBlock, WriteBlock class TestRW(unittest.TestCase): _content = 'content\n' _TEST_FILE_NAME = 'test.txt' @classmethod def tearDownClass(cls) -> None: super().tearDownClass() os.remove('test.txt') def test_01_write_bytes(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w') as fp: block = WriteBlock(fp, self._content.encode()) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_non_zero_offset(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.offset = 1 self.assertEqual(1, block.offset) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode()[1:], event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode()[1:], event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content) - 1, event.response) self.assertEqual(0, event.response2) def test_02_read_replace_file(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.file = fp.fileno() submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_rw_flags(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.rw_flag |= IOCBRWFlag.HIPRI submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_io_priority(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64), priority_class=IOCBPriorityClass.RT, priority_value=1) self.assertEqual(IOCBPriorityClass.RT, block.priority_class) self.assertEqual(1, block.priority_value) block.set_priority(IOCBPriorityClass.IDLE, 2) self.assertEqual(IOCBPriorityClass.IDLE, block.priority_class) self.assertEqual(2, block.priority_value) block.priority_value = 3 block.priority_class = IOCBPriorityClass.BE self.assertEqual(IOCBPriorityClass.BE, block.priority_class) self.assertEqual(3, block.priority_value) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_2_str(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: buffer = 'buffer__' block = ReadBlock(fp, buffer) block.length = len(buffer) self.assertEqual(len(buffer), block.length) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_write_n_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block = WriteBlock(fp, self._content) submit_ret = ctx.submit(write_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_2write_n_read(self): _SECOND_CONTENT = 'another\ninput' with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block1 = WriteBlock(fp, self._content) write_block2 = WriteBlock(fp, _SECOND_CONTENT) submit_ret = ctx.submit(write_block1, write_block2) self.assertEqual(2, submit_ret) events = ctx.get_events(2, 2) self.assertEqual(2, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block1, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(_SECOND_CONTENT.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(_SECOND_CONTENT.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(_SECOND_CONTENT), event.response) self.assertEqual(0, event.response2) ", https://github.com/isac322/linux_aio,007d033b42703e9bb4d7cd99ca50e54108215d46,test/test_non_vector_rw.py::TestRW::test_02_read_2_str,test/test_non_vector_rw.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"# coding: UTF-8 import unittest import os from linux_aio_bind import IOCBPriorityClass, IOCBRWFlag from linux_aio import AIOContext, ReadBlock, WriteBlock class TestRW(unittest.TestCase): _content = 'content\n' _TEST_FILE_NAME = 'test.txt' @classmethod def tearDownClass(cls) -> None: super().tearDownClass() os.remove('test.txt') def test_01_write_bytes(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w') as fp: block = WriteBlock(fp, self._content.encode()) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_non_zero_offset(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.offset = 1 self.assertEqual(1, block.offset) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode()[1:], event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode()[1:], event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content) - 1, event.response) self.assertEqual(0, event.response2) def test_02_read_replace_file(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.file = fp.fileno() submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_rw_flags(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.rw_flag |= IOCBRWFlag.HIPRI submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_io_priority(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64), priority_class=IOCBPriorityClass.RT, priority_value=1) self.assertEqual(IOCBPriorityClass.RT, block.priority_class) self.assertEqual(1, block.priority_value) block.set_priority(IOCBPriorityClass.IDLE, 2) self.assertEqual(IOCBPriorityClass.IDLE, block.priority_class) self.assertEqual(2, block.priority_value) block.priority_value = 3 block.priority_class = IOCBPriorityClass.BE self.assertEqual(IOCBPriorityClass.BE, block.priority_class) self.assertEqual(3, block.priority_value) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_2_str(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: buffer = 'buffer__' block = ReadBlock(fp, buffer) block.length = len(buffer) self.assertEqual(len(buffer), block.length) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_write_n_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block = WriteBlock(fp, self._content) submit_ret = ctx.submit(write_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_2write_n_read(self): _SECOND_CONTENT = 'another\ninput' with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block1 = WriteBlock(fp, self._content) write_block2 = WriteBlock(fp, _SECOND_CONTENT) submit_ret = ctx.submit(write_block1, write_block2) self.assertEqual(2, submit_ret) events = ctx.get_events(2, 2) self.assertEqual(2, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block1, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(_SECOND_CONTENT.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(_SECOND_CONTENT.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(_SECOND_CONTENT), event.response) self.assertEqual(0, event.response2) ", https://github.com/isac322/linux_aio,007d033b42703e9bb4d7cd99ca50e54108215d46,test/test_non_vector_rw.py::TestRW::test_02_read_replace_file,test/test_non_vector_rw.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"# coding: UTF-8 import unittest import os from linux_aio_bind import IOCBPriorityClass, IOCBRWFlag from linux_aio import AIOContext, ReadBlock, WriteBlock class TestRW(unittest.TestCase): _content = 'content\n' _TEST_FILE_NAME = 'test.txt' @classmethod def tearDownClass(cls) -> None: super().tearDownClass() os.remove('test.txt') def test_01_write_bytes(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w') as fp: block = WriteBlock(fp, self._content.encode()) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_non_zero_offset(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.offset = 1 self.assertEqual(1, block.offset) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode()[1:], event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode()[1:], event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content) - 1, event.response) self.assertEqual(0, event.response2) def test_02_read_replace_file(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.file = fp.fileno() submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_rw_flags(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.rw_flag |= IOCBRWFlag.HIPRI submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_io_priority(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64), priority_class=IOCBPriorityClass.RT, priority_value=1) self.assertEqual(IOCBPriorityClass.RT, block.priority_class) self.assertEqual(1, block.priority_value) block.set_priority(IOCBPriorityClass.IDLE, 2) self.assertEqual(IOCBPriorityClass.IDLE, block.priority_class) self.assertEqual(2, block.priority_value) block.priority_value = 3 block.priority_class = IOCBPriorityClass.BE self.assertEqual(IOCBPriorityClass.BE, block.priority_class) self.assertEqual(3, block.priority_value) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_2_str(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: buffer = 'buffer__' block = ReadBlock(fp, buffer) block.length = len(buffer) self.assertEqual(len(buffer), block.length) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_write_n_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block = WriteBlock(fp, self._content) submit_ret = ctx.submit(write_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_2write_n_read(self): _SECOND_CONTENT = 'another\ninput' with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block1 = WriteBlock(fp, self._content) write_block2 = WriteBlock(fp, _SECOND_CONTENT) submit_ret = ctx.submit(write_block1, write_block2) self.assertEqual(2, submit_ret) events = ctx.get_events(2, 2) self.assertEqual(2, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block1, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(_SECOND_CONTENT.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(_SECOND_CONTENT.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(_SECOND_CONTENT), event.response) self.assertEqual(0, event.response2) ", https://github.com/isac322/linux_aio,007d033b42703e9bb4d7cd99ca50e54108215d46,test/test_non_vector_rw.py::TestRW::test_02_read_w_io_priority,test/test_non_vector_rw.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"# coding: UTF-8 import unittest import os from linux_aio_bind import IOCBPriorityClass, IOCBRWFlag from linux_aio import AIOContext, ReadBlock, WriteBlock class TestRW(unittest.TestCase): _content = 'content\n' _TEST_FILE_NAME = 'test.txt' @classmethod def tearDownClass(cls) -> None: super().tearDownClass() os.remove('test.txt') def test_01_write_bytes(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w') as fp: block = WriteBlock(fp, self._content.encode()) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_non_zero_offset(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.offset = 1 self.assertEqual(1, block.offset) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode()[1:], event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode()[1:], event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content) - 1, event.response) self.assertEqual(0, event.response2) def test_02_read_replace_file(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.file = fp.fileno() submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_rw_flags(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.rw_flag |= IOCBRWFlag.HIPRI submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_io_priority(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64), priority_class=IOCBPriorityClass.RT, priority_value=1) self.assertEqual(IOCBPriorityClass.RT, block.priority_class) self.assertEqual(1, block.priority_value) block.set_priority(IOCBPriorityClass.IDLE, 2) self.assertEqual(IOCBPriorityClass.IDLE, block.priority_class) self.assertEqual(2, block.priority_value) block.priority_value = 3 block.priority_class = IOCBPriorityClass.BE self.assertEqual(IOCBPriorityClass.BE, block.priority_class) self.assertEqual(3, block.priority_value) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_2_str(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: buffer = 'buffer__' block = ReadBlock(fp, buffer) block.length = len(buffer) self.assertEqual(len(buffer), block.length) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_write_n_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block = WriteBlock(fp, self._content) submit_ret = ctx.submit(write_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_2write_n_read(self): _SECOND_CONTENT = 'another\ninput' with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block1 = WriteBlock(fp, self._content) write_block2 = WriteBlock(fp, _SECOND_CONTENT) submit_ret = ctx.submit(write_block1, write_block2) self.assertEqual(2, submit_ret) events = ctx.get_events(2, 2) self.assertEqual(2, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block1, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(_SECOND_CONTENT.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(_SECOND_CONTENT.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(_SECOND_CONTENT), event.response) self.assertEqual(0, event.response2) ", https://github.com/isac322/linux_aio,007d033b42703e9bb4d7cd99ca50e54108215d46,test/test_non_vector_rw.py::TestRW::test_02_read_w_non_zero_offset,test/test_non_vector_rw.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"# coding: UTF-8 import unittest import os from linux_aio_bind import IOCBPriorityClass, IOCBRWFlag from linux_aio import AIOContext, ReadBlock, WriteBlock class TestRW(unittest.TestCase): _content = 'content\n' _TEST_FILE_NAME = 'test.txt' @classmethod def tearDownClass(cls) -> None: super().tearDownClass() os.remove('test.txt') def test_01_write_bytes(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w') as fp: block = WriteBlock(fp, self._content.encode()) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_non_zero_offset(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.offset = 1 self.assertEqual(1, block.offset) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode()[1:], event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode()[1:], event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content) - 1, event.response) self.assertEqual(0, event.response2) def test_02_read_replace_file(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.file = fp.fileno() submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_rw_flags(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.rw_flag |= IOCBRWFlag.HIPRI submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_io_priority(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64), priority_class=IOCBPriorityClass.RT, priority_value=1) self.assertEqual(IOCBPriorityClass.RT, block.priority_class) self.assertEqual(1, block.priority_value) block.set_priority(IOCBPriorityClass.IDLE, 2) self.assertEqual(IOCBPriorityClass.IDLE, block.priority_class) self.assertEqual(2, block.priority_value) block.priority_value = 3 block.priority_class = IOCBPriorityClass.BE self.assertEqual(IOCBPriorityClass.BE, block.priority_class) self.assertEqual(3, block.priority_value) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_2_str(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: buffer = 'buffer__' block = ReadBlock(fp, buffer) block.length = len(buffer) self.assertEqual(len(buffer), block.length) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_write_n_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block = WriteBlock(fp, self._content) submit_ret = ctx.submit(write_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_2write_n_read(self): _SECOND_CONTENT = 'another\ninput' with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block1 = WriteBlock(fp, self._content) write_block2 = WriteBlock(fp, _SECOND_CONTENT) submit_ret = ctx.submit(write_block1, write_block2) self.assertEqual(2, submit_ret) events = ctx.get_events(2, 2) self.assertEqual(2, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block1, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(_SECOND_CONTENT.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(_SECOND_CONTENT.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(_SECOND_CONTENT), event.response) self.assertEqual(0, event.response2) ", https://github.com/isac322/linux_aio,007d033b42703e9bb4d7cd99ca50e54108215d46,test/test_non_vector_rw.py::TestRW::test_02_read_w_rw_flags,test/test_non_vector_rw.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"# coding: UTF-8 import unittest import os from linux_aio_bind import IOCBPriorityClass, IOCBRWFlag from linux_aio import AIOContext, ReadBlock, WriteBlock class TestRW(unittest.TestCase): _content = 'content\n' _TEST_FILE_NAME = 'test.txt' @classmethod def tearDownClass(cls) -> None: super().tearDownClass() os.remove('test.txt') def test_01_write_bytes(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w') as fp: block = WriteBlock(fp, self._content.encode()) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_non_zero_offset(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.offset = 1 self.assertEqual(1, block.offset) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode()[1:], event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode()[1:], event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content) - 1, event.response) self.assertEqual(0, event.response2) def test_02_read_replace_file(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.file = fp.fileno() submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_rw_flags(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64)) block.rw_flag |= IOCBRWFlag.HIPRI submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_w_io_priority(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: block = ReadBlock(fp, bytearray(64), priority_class=IOCBPriorityClass.RT, priority_value=1) self.assertEqual(IOCBPriorityClass.RT, block.priority_class) self.assertEqual(1, block.priority_value) block.set_priority(IOCBPriorityClass.IDLE, 2) self.assertEqual(IOCBPriorityClass.IDLE, block.priority_class) self.assertEqual(2, block.priority_value) block.priority_value = 3 block.priority_class = IOCBPriorityClass.BE self.assertEqual(IOCBPriorityClass.BE, block.priority_class) self.assertEqual(3, block.priority_value) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_02_read_2_str(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME) as fp: buffer = 'buffer__' block = ReadBlock(fp, buffer) block.length = len(buffer) self.assertEqual(len(buffer), block.length) submit_ret = ctx.submit(block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_write_n_read(self): with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block = WriteBlock(fp, self._content) submit_ret = ctx.submit(write_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(self._content.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) def test_2write_n_read(self): _SECOND_CONTENT = 'another\ninput' with AIOContext(1) as ctx, open(self._TEST_FILE_NAME, 'w+') as fp: write_block1 = WriteBlock(fp, self._content) write_block2 = WriteBlock(fp, _SECOND_CONTENT) submit_ret = ctx.submit(write_block1, write_block2) self.assertEqual(2, submit_ret) events = ctx.get_events(2, 2) self.assertEqual(2, len(events)) event = events[0] self.assertEqual(self._content.encode(), event.buffer) self.assertEqual(write_block1, event.aio_block) self.assertEqual(len(self._content), event.response) self.assertEqual(0, event.response2) read_block = ReadBlock(fp, bytearray(64)) submit_ret = ctx.submit(read_block) self.assertEqual(1, submit_ret) events = ctx.get_events(1, 1) self.assertEqual(1, len(events)) event = events[0] self.assertEqual(_SECOND_CONTENT.encode(), event.buffer.rstrip(b'\0')) self.assertEqual(_SECOND_CONTENT.encode(), event.stripped_buffer()) self.assertEqual(read_block, event.aio_block) self.assertEqual(len(_SECOND_CONTENT), event.response) self.assertEqual(0, event.response2) ", https://github.com/isnok/data-pypes,dcfe6feac2b617f9f8624cff81c44f72c0313ee6,pypes/logsetup.py::pypes.logsetup.get_logconfig,pypes/logsetup.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Logging for the Pypes Framework. Can also be used 'stand-alone'. Logging can be configured through a bunch of environment variables: LOGLEVEL: the general loglevel STDOUT_LOGLEVEL: the loglevel of the console output _LOGFILE: if such a variable is defined, a filelogger for that level is created and added to the logging facility If you run your_program.py like this: STDOUT_LOGLEVEL=debug LOGLEVEL=info ERROR_LOGFILE=error.log your_program.py Then error.log will contain all log messages of level ERROR and higher. On stdout you will still just see all messages from INFO on, since the general LOGLEVEL is set to this level, and thus messages with a lower loglevel will just not be handled. This is also why you should avoid formatting log messages right away, but let the logging facility take care of that instead, because then you can save the performance cost of formatting large data structures (say on loglevel DEBUG), by setting the general loglevel (when not debugging) to some higher level. Importing this module will also add a new level name: SUCCESS [25] The numeric values of that level and ""it's neighbours"" will then be: ... 10 - DEBUG 20 - INFO 25 - SUCCESS 30 - WARNING 40 - ERROR ... For that loglevel a convenience method (logger.success) will be added to every logger created through setup_logger. >>> logger = setup_logger('test') >>> logger.info('Result invisible to doctest...') [20] - test - Result invisible to doctest... Color will be used for stdout, if sys.stdout.isatty(). """""" import os import sys import logging from functools import partial SUCCESS = 25 logging.addLevelName(SUCCESS, 'SUCCESS') PYTHON_3 = sys.version_info[0] == 3 if PYTHON_3: levelnames = logging._nameToLevel # pylint: disable=E1101 else: levelnames = logging._levelNames # pylint: disable=E1101 def get_logconfig(level=None): """""" Get logging configuration from environment. Returns: level, stdout_loglevel, files >>> get_logconfig() (20, 20, {}) >>> get_logconfig(20) (20, 20, {}) >>> get_logconfig('20') (20, 20, {}) >>> get_logconfig('INFO') (20, 20, {}) >>> import os >>> os.environ['STDOUT_LOGLEVEL'] = 'INFO' >>> get_logconfig() (20, 20, {}) """""" if level is None: level = os.environ.get('LOGLEVEL') if level is None: level = logging.INFO elif isinstance(level, (int, float)): level = int(level) elif level == str(level) and level.isdigit(): level = int(level) else: level = logging._checkLevel(level.upper()) files = {} for levelname in levelnames: if levelname == str(levelname): env_varname = levelname.upper() + '_LOGFILE' if env_varname in os.environ: files[levelname] = os.environ[env_varname] if 'STDOUT_LOGLEVEL' in os.environ: stdout_loglevel = logging._checkLevel( os.environ['STDOUT_LOGLEVEL'].upper() ) else: stdout_loglevel = level # color_stdout = 'COLOR_STDOUT' in os.environ return level, stdout_loglevel, files reset_color = '\033[0m' termcolors = dict( off=reset_color, black='\033[90m', red='\033[91m', green='\033[92m', yellow='\033[93m', blue='\033[94m', purple='\033[95m', cyan='\033[96m', white='\033[97m', grey='\033[98m', bold='\033[1m', underline='\033[4m', ) def colored(color, string): """""" Add color sequences to a string. >>> colored('blue', 'jazzjazzjazzjazz...') '\\x1b[94mjazzjazzjazzjazz...\\x1b[0m' """""" sequence = termcolors.get(color, reset_color) return '{}{}{}'.format(sequence, string, reset_color) def stdout_log_format(name, use_color=None): """""" Return the log format for stdout. If use_color is None this auto-adjusts. >>> colored = stdout_log_format('test', use_color=True) >>> 'test' in colored True >>> '%(message)s' in colored True >>> no_color = stdout_log_format('test', use_color=False) >>> 'test' in no_color True >>> '%(message)s' in no_color True >>> no_color != colored True """""" use_color = sys.stdout.isatty() if use_color is None else use_color if use_color: fmt = colored('purple', ' - ').join([ # colored('blue', '%(asctime)s'), # colored('yellow', '[%(levelno)d] %(levelname)-8.8s'), colored('yellow', '[%(levelno)d]'), colored('green', name), colored('white', '%(message)s'), ]) else: fmt = ' - '.join([ # '%(asctime)s', # '[%(levelno)d] %(levelname)-8.8s', '[%(levelno)d]', name, '%(message)s', ]) return fmt def file_log_format(name): fmt = ' - '.join([ '%(asctime)s', '%(processName)s', # '%(levelname)-8s', '[%(levelno)d] %(levelname)-7s', '%(module)s:%(lineno)d', name, '%(message)s', ]) return fmt loggers = {} def setup_logger(name): """""" Set up a custom logger with handlers for stdout and possibly some files. The name will be included in all it's log outputs. To prevent duplicate adding of handlers to already existing loggers, the set of created loggers is cached in the global loggers dict. >>> import os >>> os.environ['ERROR_LOGFILE'] = '/tmp/pypes_logging_test.log' >>> logger = setup_logger('test') >>> logger.success('Result invisible to doctest...') [25] - test - Result invisible to doctest... """""" if name in loggers: return loggers[name] level, stdout_level, files = get_logconfig() logger = logging.getLogger(name) logger.setLevel(level) stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setLevel(stdout_level) stdout_format = stdout_log_format(name) readable_formatter = logging.Formatter(stdout_format) stdout_handler.setFormatter(readable_formatter) logger.addHandler(stdout_handler) if files: logfile_formatter = logging.Formatter(file_log_format(name)) for levelname, filename in files.items(): handler = logging.FileHandler(filename, encoding='utf-8') handler.setLevel(levelname) handler.setFormatter(logfile_formatter) logger.addHandler(handler) logger.success = partial(logger.log, SUCCESS) return loggers.setdefault(name, logger) ", https://github.com/ivanprjcts/sdklib,5384b1c7f05abd6350151c08ad6a4a8b5855f8f0,tests/test_http_context.py::TestHttpContext::test_http_context_singleton_clear,tests/test_http_context.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from sdklib.http import HttpRequestContextSingleton, HttpRequestContext class TestHttpContext(unittest.TestCase): @classmethod def setUpClass(cls): pass @classmethod def tearDownClass(cls): pass def test_http_context_sigleton(self): ctxt_singleton = HttpRequestContextSingleton.get_instance() ctxt_singleton.method = ""POST"" ctxt_singleton2 = HttpRequestContextSingleton.get_instance() self.assertEqual(ctxt_singleton.method, ctxt_singleton2.method) def test_http_context_singleton_clear(self): ctxt_singleton = HttpRequestContextSingleton.get_instance() ctxt_singleton.method = ""POST"" self.assertEqual(""POST"", ctxt_singleton.method) ctxt_singleton.clear() self.assertNotEqual(""POST"", ctxt_singleton.method) ctxt_singleton2 = HttpRequestContextSingleton.get_instance() self.assertNotEqual(""POST"", ctxt_singleton2.method) def test_http_context_singleton_fields_to_clear(self): ctxt_singleton = HttpRequestContextSingleton.get_instance() ctxt_singleton.fields_to_clear = ['proxy'] ctxt_singleton.proxy = ""http://localhost:8080"" ctxt_singleton.method = ""PUT"" self.assertEqual(""http://localhost:8080"", ctxt_singleton.proxy) ctxt_singleton.clear() self.assertNotEqual(""http://localhost:8080"", ctxt_singleton.proxy) self.assertEqual(""PUT"", ctxt_singleton.method) ctxt_singleton2 = HttpRequestContextSingleton.get_instance() self.assertNotEqual(""http://localhost:8080"", ctxt_singleton2.proxy) self.assertEqual(""PUT"", ctxt_singleton2.method) def test_http_context(self): ctxt_singleton = HttpRequestContext() ctxt_singleton.method = ""POST"" self.assertEqual(""POST"", ctxt_singleton.method) def test_http_context_clear(self): ctxt_singleton = HttpRequestContext() ctxt_singleton.method = ""POST"" self.assertEqual(""POST"", ctxt_singleton.method) ctxt_singleton.clear() self.assertNotEqual(""POST"", ctxt_singleton.method) ctxt_singleton2 = HttpRequestContext() self.assertNotEqual(""POST"", ctxt_singleton2.method) def test_http_context_fields_to_clear(self): ctxt_singleton = HttpRequestContext() ctxt_singleton.fields_to_clear = ['proxy'] ctxt_singleton.proxy = ""http://localhost:8080"" ctxt_singleton.method = ""PUT"" self.assertEqual(""http://localhost:8080"", ctxt_singleton.proxy) ctxt_singleton.clear() self.assertNotEqual(""http://localhost:8080"", ctxt_singleton.proxy) self.assertEqual(""PUT"", ctxt_singleton.method) def test_http_context_clear_by_arg(self): ctxt_singleton = HttpRequestContext() ctxt_singleton.fields_to_clear = [] ctxt_singleton.proxy = ""http://localhost:8080"" ctxt_singleton.method = ""PUT"" self.assertEqual(""http://localhost:8080"", ctxt_singleton.proxy) ctxt_singleton.clear(""proxy"") self.assertNotEqual(""http://localhost:8080"", ctxt_singleton.proxy) self.assertEqual(""PUT"", ctxt_singleton.method) def test_http_context_headers_none(self): ctxt = HttpRequestContext() ctxt.headers = None self.assertEqual({}, ctxt.headers) ", https://github.com/ivoire/ReactOBus,841d5c0442ffe92fcaec278e07053afe5b93d925,tests/test_db.py::test_run,tests/test_db.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- # vim: set ts=4 # Copyright 2016 Rémi Duraffort # This file is part of ReactOBus. # # ReactOBus is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # ReactOBus is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with ReactOBus. If not, see import datetime import imp import json import pytest from sqlalchemy.exc import SQLAlchemyError import sqlalchemy.orm import uuid import zmq from . import mock def test_run(monkeypatch, tmpdir): zmq_instance = mock.ZMQContextInstance() monkeypatch.setattr(zmq.Context, ""instance"", zmq_instance) monkeypatch.setattr(zmq, ""Poller"", mock.ZMQPoller) from reactobus.db import DB, Message dbname = tmpdir.join(""testing.sqlite3"") db_url = ""sqlite:///%s"" % dbname db = DB({""url"": db_url}, ""inproc://test_run"") with pytest.raises(IndexError): db.run() sub = zmq_instance.socks[zmq.SUB] assert len(sub.recv) == 0 assert sub.connected is True assert sub.opts == {zmq.SUBSCRIBE: b""""} # Test that wrong message will not make the process crash sub.recv = [[]] with pytest.raises(IndexError): db.run() assert len(sub.recv) == 0 # Check that the db is empty session = db.sessions() assert session.query(Message).count() == 0 # Test that wrong message will not make the process crash sub.recv = [ [ ""org.reactobus.1"", str(uuid.uuid1()), datetime.datetime.utcnow().isoformat(), ""lavaserver"", json.dumps({}), ], [ ""org.reactobus.2"", str(uuid.uuid1()), datetime.datetime.utcnow().isoformat(), ""lavaserver"", json.dumps({}), ], [ ""org.reactobus.3"", str(uuid.uuid1()), datetime.datetime.utcnow().isoformat(), ""lavaserver"", json.dumps({}), ], [ ""org.reactobus.4"", str(uuid.uuid1()), ""2016/01/01"", ""lavaserver"", json.dumps({}), ], [ ""org.reactobus.5"", str(uuid.uuid1()), datetime.datetime.utcnow().isoformat(), ""lavaserver"", json.dumps({}), ], ] with pytest.raises(IndexError): db.run() # Force the databse flush db.save_to_db() assert len(sub.recv) == 0 # Check that the db is empty session = db.sessions() assert session.query(Message).count() == 4 assert session.query(Message).get(1).topic == ""org.reactobus.1"" assert session.query(Message).get(2).topic == ""org.reactobus.2"" assert session.query(Message).get(3).topic == ""org.reactobus.3"" assert session.query(Message).get(4).topic == ""org.reactobus.5"" class SessionMock(object): def __init__(self): self.messages = 0 self.commits = 0 self.raise_on_bulk = False def bulk_save_objects(self, messages): if self.raise_on_bulk: self.messages = 0 raise SQLAlchemyError else: self.messages = len(messages) def commit(self): self.commits += 1 from sqlalchemy.exc import SQLAlchemyError raise SQLAlchemyError class SessionsMock(object): def __init__(self): self.session_mock = SessionMock() def __call__(self): return self.session_mock def test_errors(monkeypatch, tmpdir): zmq_instance = mock.ZMQContextInstance() monkeypatch.setattr(zmq.Context, ""instance"", zmq_instance) sessions_mock = SessionsMock() def mock_sessionmaker(bind): return sessions_mock monkeypatch.setattr(sqlalchemy.orm, ""sessionmaker"", mock_sessionmaker) monkeypatch.setattr(zmq, ""Poller"", mock.ZMQPoller) # Reload the module to apply the monkey patching import reactobus.db from reactobus.db import DB imp.reload(reactobus.db) # Create the DB dbname = tmpdir.join(""testing.sqlite3"") db_url = ""sqlite:///%s"" % dbname db = DB({""url"": db_url}, ""inproc://test_run"") # Run a first time to do the setup with pytest.raises(IndexError): db.run() sub = zmq_instance.socks[zmq.SUB] # Run with two messages sub.recv = [ [ ""org.reactobus.1"", str(uuid.uuid1()), datetime.datetime.utcnow().isoformat(), ""lavaserver"", json.dumps({}), ], [ ""org.reactobus.1"", str(uuid.uuid1()), datetime.datetime.utcnow().isoformat(), ""lavaserver"", json.dumps({}), ], ] with pytest.raises(IndexError): db.run() db.save_to_db() assert len(sub.recv) == 0 assert sessions_mock.session_mock.messages == 2 assert sessions_mock.session_mock.commits == 3 # Re-run with two messages but raise on session.add() sub.recv = [ [ ""org.reactobus.1"", str(uuid.uuid1()), datetime.datetime.utcnow().isoformat(), ""lavaserver"", json.dumps({}), ], [ ""org.reactobus.1"", str(uuid.uuid1()), datetime.datetime.utcnow().isoformat(), ""lavaserver"", json.dumps({}), ], ] sessions_mock.session_mock.raise_on_bulk = True sessions_mock.session_mock.messages = 4212 sessions_mock.session_mock.commits = 0 with pytest.raises(IndexError): db.run() db.save_to_db() assert len(sub.recv) == 0 assert sessions_mock.session_mock.messages == 0 assert sessions_mock.session_mock.commits == 0 # Re-run with two invalid messages sub.recv = [ [ ""org.reactobus.1"", str(uuid.uuid1()), datetime.datetime.utcnow().isoformat(), ""lavaserver"", ], [ ""org.reactobus.1"", str(uuid.uuid1()), datetime.datetime.utcnow().isoformat(), ""lavaserver"", json.dumps({}), ], ] sessions_mock.session_mock.raise_on_bulk = False sessions_mock.session_mock.messages = 0 sessions_mock.session_mock.commits = 0 with pytest.raises(IndexError): db.run() db.save_to_db() assert len(sub.recv) == 0 assert sessions_mock.session_mock.messages == 1 assert sessions_mock.session_mock.commits == 3 ", https://github.com/izar/pytm,724df0c6346706e9c3678bee11e23d9609d8fa4c,tests/test_private_func.py::TestMethod::test_defaults,tests/test_private_func.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import sys sys.path.append("".."") import unittest import random from pytm.pytm import Actor, Boundary, Dataflow, Datastore, Process, Server, TM, Threat class TestUniqueNames(unittest.TestCase): def test_duplicate_boundary_names_have_different_unique_names(self): random.seed(0) object_1 = Boundary(""foo"") object_2 = Boundary(""foo"") object_1_uniq_name = object_1._uniq_name() object_2_uniq_name = object_2._uniq_name() self.assertNotEqual(object_1_uniq_name, object_2_uniq_name) self.assertEqual(object_1_uniq_name, ""boundary_foo_acf3059e70"") self.assertEqual(object_2_uniq_name, ""boundary_foo_88f2d9c06f"") class TestAttributes(unittest.TestCase): def test_write_once(self): user = Actor(""User"") with self.assertRaises(ValueError): user.name = ""Computer"" def test_kwargs(self): user = Actor(""User"", isAdmin=True) self.assertEqual(user.isAdmin, True) user = Actor(""User"") self.assertEqual(user.isAdmin, False) user.isAdmin = True self.assertEqual(user.isAdmin, True) def test_load_threats(self): tm = TM(""TM"") self.assertNotEqual(len(TM._BagOfThreats), 0) with self.assertRaises(FileNotFoundError): tm.threatsFile = ""threats.json"" with self.assertRaises(FileNotFoundError): TM(""TM"", threatsFile=""threats.json"") def test_responses(self): tm = TM(""my test tm"", description=""aa"", isOrdered=True) user = Actor(""User"") web = Server(""Web Server"") db = Datastore(""SQL Database"") http_req = Dataflow(user, web, ""http req"") insert = Dataflow(web, db, ""insert data"") query = Dataflow(web, db, ""query"") query_resp = Dataflow(db, web, ""query results"", responseTo=query) http_resp = Dataflow(web, user, ""http resp"") http_resp.responseTo = http_req self.assertTrue(tm.check()) self.assertEqual(http_req.response, http_resp) self.assertIs(http_resp.isResponse, True) self.assertIs(query_resp.isResponse, True) self.assertEqual(query_resp.responseTo, query) self.assertEqual(query.response, query_resp) self.assertIsNone(insert.response) self.assertIs(insert.isResponse, False) def test_defaults(self): tm = TM(""TM"") user = Actor(""User"", data=""HTTP"", authenticatesDestination=True) server = Server( ""Server"", port=443, protocol=""HTTPS"", isEncrypted=True, data=""JSON"" ) db = Datastore( ""PostgreSQL"", isSQL=True, port=5432, protocol=""PostgreSQL"", isEncrypted=False, data=""SQL resp"", ) worker = Process(""Task queue worker"") req_get = Dataflow(user, server, ""HTTP GET"") server_query = Dataflow(server, db, ""Query"", data=""SQL"") result = Dataflow(db, server, ""Results"", isResponse=True) resp_get = Dataflow(server, user, ""HTTP Response"", isResponse=True) req_post = Dataflow(user, server, ""HTTP POST"", data=""JSON"") resp_post = Dataflow(server, user, ""HTTP Response"", isResponse=True) worker_query = Dataflow(worker, db, ""Query"", data=""SQL"") Dataflow(db, worker, ""Results"", isResponse=True) self.assertTrue(tm.check()) self.assertEqual(req_get.srcPort, -1) self.assertEqual(req_get.dstPort, server.port) self.assertEqual(req_get.isEncrypted, server.isEncrypted) self.assertEqual(req_get.authenticatesDestination, user.authenticatesDestination) self.assertEqual(req_get.protocol, server.protocol) self.assertEqual(req_get.data, user.data) self.assertEqual(server_query.srcPort, -1) self.assertEqual(server_query.dstPort, db.port) self.assertEqual(server_query.isEncrypted, db.isEncrypted) self.assertEqual(server_query.authenticatesDestination, server.authenticatesDestination) self.assertEqual(server_query.protocol, db.protocol) self.assertNotEqual(server_query.data, server.data) self.assertEqual(result.srcPort, db.port) self.assertEqual(result.dstPort, -1) self.assertEqual(result.isEncrypted, db.isEncrypted) self.assertEqual(result.authenticatesDestination, False) self.assertEqual(result.protocol, db.protocol) self.assertEqual(result.data, db.data) self.assertEqual(resp_get.srcPort, server.port) self.assertEqual(resp_get.dstPort, -1) self.assertEqual(resp_get.isEncrypted, server.isEncrypted) self.assertEqual(resp_get.authenticatesDestination, False) self.assertEqual(resp_get.protocol, server.protocol) self.assertEqual(resp_get.data, server.data) self.assertEqual(req_post.srcPort, -1) self.assertEqual(req_post.dstPort, server.port) self.assertEqual(req_post.isEncrypted, server.isEncrypted) self.assertEqual(req_post.authenticatesDestination, user.authenticatesDestination) self.assertEqual(req_post.protocol, server.protocol) self.assertNotEqual(req_post.data, user.data) self.assertEqual(resp_post.srcPort, server.port) self.assertEqual(resp_post.dstPort, -1) self.assertEqual(resp_post.isEncrypted, server.isEncrypted) self.assertEqual(resp_post.authenticatesDestination, False) self.assertEqual(resp_post.protocol, server.protocol) self.assertEqual(resp_post.data, server.data) self.assertListEqual(server.inputs, [req_get, req_post]) self.assertListEqual(server.outputs, [server_query]) self.assertListEqual(worker.inputs, []) self.assertListEqual(worker.outputs, [worker_query]) class TestMethod(unittest.TestCase): def test_defaults(self): tm = TM(""my test tm"", description=""aa"", isOrdered=True) internet = Boundary(""Internet"") cloud = Boundary(""Cloud"") user = Actor(""User"", inBoundary=internet) server = Server(""Server"") db = Datastore(""DB"", inBoundary=cloud, isSQL=True) func = Datastore(""Lambda function"", inBoundary=cloud) request = Dataflow(user, server, ""request"") response = Dataflow(server, user, ""response"", isResponse=True) user_query = Dataflow(user, db, ""user query"") server_query = Dataflow(server, db, ""server query"") func_query = Dataflow(func, db, ""func query"") default_target = [""Actor"", ""Boundary"", ""Dataflow"", ""Datastore"", ""Server""] testCases = [ {""target"": server, ""condition"": ""target.oneOf(Server, Datastore)""}, {""target"": server, ""condition"": ""not target.oneOf(Actor, Dataflow)""}, {""target"": request, ""condition"": ""target.crosses(Boundary)""}, {""target"": user_query, ""condition"": ""target.crosses(Boundary)""}, {""target"": server_query, ""condition"": ""target.crosses(Boundary)""}, {""target"": func_query, ""condition"": ""not target.crosses(Boundary)""}, {""target"": func_query, ""condition"": ""not target.enters(Boundary)""}, {""target"": func_query, ""condition"": ""not target.exits(Boundary)""}, {""target"": request, ""condition"": ""not target.enters(Boundary)""}, {""target"": request, ""condition"": ""target.exits(Boundary)""}, {""target"": response, ""condition"": ""target.enters(Boundary)""}, {""target"": response, ""condition"": ""not target.exits(Boundary)""}, {""target"": user, ""condition"": ""target.inside(Boundary)""}, {""target"": func, ""condition"": ""not any(target.inputs)""}, { ""target"": server, ""condition"": ""any(f.sink.oneOf(Datastore) and f.sink.isSQL "" ""for f in target.outputs)"", }, ] self.assertTrue(tm.check()) for case in testCases: t = Threat(SID="""", target=default_target, condition=case[""condition""]) self.assertTrue( t.apply(case[""target""]), ""Failed to match {} against {}"".format( case[""target""], case[""condition""], ), ) ", https://github.com/izar/pytm,724df0c6346706e9c3678bee11e23d9609d8fa4c,tests/test_private_func.py::TestUniqueNames::test_duplicate_boundary_names_have_different_unique_names,tests/test_private_func.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import sys sys.path.append("".."") import unittest import random from pytm.pytm import Actor, Boundary, Dataflow, Datastore, Process, Server, TM, Threat class TestUniqueNames(unittest.TestCase): def test_duplicate_boundary_names_have_different_unique_names(self): random.seed(0) object_1 = Boundary(""foo"") object_2 = Boundary(""foo"") object_1_uniq_name = object_1._uniq_name() object_2_uniq_name = object_2._uniq_name() self.assertNotEqual(object_1_uniq_name, object_2_uniq_name) self.assertEqual(object_1_uniq_name, ""boundary_foo_acf3059e70"") self.assertEqual(object_2_uniq_name, ""boundary_foo_88f2d9c06f"") class TestAttributes(unittest.TestCase): def test_write_once(self): user = Actor(""User"") with self.assertRaises(ValueError): user.name = ""Computer"" def test_kwargs(self): user = Actor(""User"", isAdmin=True) self.assertEqual(user.isAdmin, True) user = Actor(""User"") self.assertEqual(user.isAdmin, False) user.isAdmin = True self.assertEqual(user.isAdmin, True) def test_load_threats(self): tm = TM(""TM"") self.assertNotEqual(len(TM._BagOfThreats), 0) with self.assertRaises(FileNotFoundError): tm.threatsFile = ""threats.json"" with self.assertRaises(FileNotFoundError): TM(""TM"", threatsFile=""threats.json"") def test_responses(self): tm = TM(""my test tm"", description=""aa"", isOrdered=True) user = Actor(""User"") web = Server(""Web Server"") db = Datastore(""SQL Database"") http_req = Dataflow(user, web, ""http req"") insert = Dataflow(web, db, ""insert data"") query = Dataflow(web, db, ""query"") query_resp = Dataflow(db, web, ""query results"", responseTo=query) http_resp = Dataflow(web, user, ""http resp"") http_resp.responseTo = http_req self.assertTrue(tm.check()) self.assertEqual(http_req.response, http_resp) self.assertIs(http_resp.isResponse, True) self.assertIs(query_resp.isResponse, True) self.assertEqual(query_resp.responseTo, query) self.assertEqual(query.response, query_resp) self.assertIsNone(insert.response) self.assertIs(insert.isResponse, False) def test_defaults(self): tm = TM(""TM"") user = Actor(""User"", data=""HTTP"", authenticatesDestination=True) server = Server( ""Server"", port=443, protocol=""HTTPS"", isEncrypted=True, data=""JSON"" ) db = Datastore( ""PostgreSQL"", isSQL=True, port=5432, protocol=""PostgreSQL"", isEncrypted=False, data=""SQL resp"", ) worker = Process(""Task queue worker"") req_get = Dataflow(user, server, ""HTTP GET"") server_query = Dataflow(server, db, ""Query"", data=""SQL"") result = Dataflow(db, server, ""Results"", isResponse=True) resp_get = Dataflow(server, user, ""HTTP Response"", isResponse=True) req_post = Dataflow(user, server, ""HTTP POST"", data=""JSON"") resp_post = Dataflow(server, user, ""HTTP Response"", isResponse=True) worker_query = Dataflow(worker, db, ""Query"", data=""SQL"") Dataflow(db, worker, ""Results"", isResponse=True) self.assertTrue(tm.check()) self.assertEqual(req_get.srcPort, -1) self.assertEqual(req_get.dstPort, server.port) self.assertEqual(req_get.isEncrypted, server.isEncrypted) self.assertEqual(req_get.authenticatesDestination, user.authenticatesDestination) self.assertEqual(req_get.protocol, server.protocol) self.assertEqual(req_get.data, user.data) self.assertEqual(server_query.srcPort, -1) self.assertEqual(server_query.dstPort, db.port) self.assertEqual(server_query.isEncrypted, db.isEncrypted) self.assertEqual(server_query.authenticatesDestination, server.authenticatesDestination) self.assertEqual(server_query.protocol, db.protocol) self.assertNotEqual(server_query.data, server.data) self.assertEqual(result.srcPort, db.port) self.assertEqual(result.dstPort, -1) self.assertEqual(result.isEncrypted, db.isEncrypted) self.assertEqual(result.authenticatesDestination, False) self.assertEqual(result.protocol, db.protocol) self.assertEqual(result.data, db.data) self.assertEqual(resp_get.srcPort, server.port) self.assertEqual(resp_get.dstPort, -1) self.assertEqual(resp_get.isEncrypted, server.isEncrypted) self.assertEqual(resp_get.authenticatesDestination, False) self.assertEqual(resp_get.protocol, server.protocol) self.assertEqual(resp_get.data, server.data) self.assertEqual(req_post.srcPort, -1) self.assertEqual(req_post.dstPort, server.port) self.assertEqual(req_post.isEncrypted, server.isEncrypted) self.assertEqual(req_post.authenticatesDestination, user.authenticatesDestination) self.assertEqual(req_post.protocol, server.protocol) self.assertNotEqual(req_post.data, user.data) self.assertEqual(resp_post.srcPort, server.port) self.assertEqual(resp_post.dstPort, -1) self.assertEqual(resp_post.isEncrypted, server.isEncrypted) self.assertEqual(resp_post.authenticatesDestination, False) self.assertEqual(resp_post.protocol, server.protocol) self.assertEqual(resp_post.data, server.data) self.assertListEqual(server.inputs, [req_get, req_post]) self.assertListEqual(server.outputs, [server_query]) self.assertListEqual(worker.inputs, []) self.assertListEqual(worker.outputs, [worker_query]) class TestMethod(unittest.TestCase): def test_defaults(self): tm = TM(""my test tm"", description=""aa"", isOrdered=True) internet = Boundary(""Internet"") cloud = Boundary(""Cloud"") user = Actor(""User"", inBoundary=internet) server = Server(""Server"") db = Datastore(""DB"", inBoundary=cloud, isSQL=True) func = Datastore(""Lambda function"", inBoundary=cloud) request = Dataflow(user, server, ""request"") response = Dataflow(server, user, ""response"", isResponse=True) user_query = Dataflow(user, db, ""user query"") server_query = Dataflow(server, db, ""server query"") func_query = Dataflow(func, db, ""func query"") default_target = [""Actor"", ""Boundary"", ""Dataflow"", ""Datastore"", ""Server""] testCases = [ {""target"": server, ""condition"": ""target.oneOf(Server, Datastore)""}, {""target"": server, ""condition"": ""not target.oneOf(Actor, Dataflow)""}, {""target"": request, ""condition"": ""target.crosses(Boundary)""}, {""target"": user_query, ""condition"": ""target.crosses(Boundary)""}, {""target"": server_query, ""condition"": ""target.crosses(Boundary)""}, {""target"": func_query, ""condition"": ""not target.crosses(Boundary)""}, {""target"": func_query, ""condition"": ""not target.enters(Boundary)""}, {""target"": func_query, ""condition"": ""not target.exits(Boundary)""}, {""target"": request, ""condition"": ""not target.enters(Boundary)""}, {""target"": request, ""condition"": ""target.exits(Boundary)""}, {""target"": response, ""condition"": ""target.enters(Boundary)""}, {""target"": response, ""condition"": ""not target.exits(Boundary)""}, {""target"": user, ""condition"": ""target.inside(Boundary)""}, {""target"": func, ""condition"": ""not any(target.inputs)""}, { ""target"": server, ""condition"": ""any(f.sink.oneOf(Datastore) and f.sink.isSQL "" ""for f in target.outputs)"", }, ] self.assertTrue(tm.check()) for case in testCases: t = Threat(SID="""", target=default_target, condition=case[""condition""]) self.assertTrue( t.apply(case[""target""]), ""Failed to match {} against {}"".format( case[""target""], case[""condition""], ), ) ", https://github.com/JakubTesarek/colltools,e50f98ccda33ab409fcdba277a39f071824e24c7,tests/test_name_registry.py::test_name_registry_has_no_instances,tests/test_name_registry.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest from colltools import NameRegistry @pytest.fixture(autouse=True) def clean_registry(): NameRegistry.__items__ = {} def test_name_registry_has_no_instances(): assert NameRegistry.__members__ == {} def test_validates_name(): with pytest.raises(KeyError): NameRegistry('doesnt_exist') def test_raises_error_when_name_doesnt_exist(): class Registry(NameRegistry): __values__ = ['xxx'] with pytest.raises(KeyError): Registry('doesnt_exist') def test_gets_instance(): class Registry(NameRegistry): __values__ = ['xxx'] assert isinstance(Registry.get('xxx'), Registry) def test_gets_instance_from_superclass(): class Registry(NameRegistry): __values__ = ['xxx'] assert isinstance(NameRegistry.get('xxx'), Registry) def test_singleton_instances(): class Registry(NameRegistry): __values__ = ['xxx'] val1 = Registry.get('xxx') val2 = Registry.get('xxx') assert val1 is val2 def test_cascade(): class Registry1(NameRegistry): __values__ = ['xxx'] class Registry2(Registry1): __values__ = ['yyy'] class Registry3(Registry2): __values__ = ['zzz'] assert set(NameRegistry.__members__.keys()) == set(['xxx', 'yyy', 'zzz']) assert set(Registry1.__members__.keys()) == set(['xxx', 'yyy', 'zzz']) assert set(Registry2.__members__.keys()) == set(['yyy', 'zzz']) assert set(Registry3.__members__.keys()) == set(['zzz']) def test_repr(): class Registry(NameRegistry): __values__ = ['xxx'] assert repr(Registry.get('xxx')) == '' def test_name_attribute(): class Registry(NameRegistry): __values__ = ['xxx'] assert Registry.get('xxx').name == 'xxx'", https://github.com/jamesevickery/l293d,c026a9b58769f23d3e999234ba9df30fb021f949,tests/test_l293d.py::L293DTestCase::test_create_motor_with_force_selection,tests/test_l293d.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from sys import version_info # Python 2: reload is built-in if version_info.major == 3: if version_info.minor < 4: # Python 3.0 - 3.3: deprecated since Python 3.4 in favour of importlib from imp import reload else: # Python 3.4+ from importlib import reload class L293DTestCase(unittest.TestCase): def test_import(self): """""" Import driver module, then assertTrue. If the import fails, an exception is raised and the assertion is never reached """""" import l293d as d print(str(d)) self.assertTrue(True) reload(d.driver) # Revert changes def test_create_motor_with_force_selection(self): """""" Test DC class instance creation with explicity force_selection parameter """""" import l293d as d cases = (([33, 36, 37], False), ([19, 21, 23], True)) for pins, force_selection in cases: motor = d.DC(*pins, force_selection=force_selection) self.assertEqual(d.pins_in_use, pins) motor.remove() reload(d.driver) def test_pins_string_list(self): """""" Test that the list of pins is returned correctly """""" import l293d as d print(d.pins_in_use) motor = d.DC(29, 7, 13) self.assertEqual(motor.pins_string_list(), '[29, 7 and 13]') reload(d.driver) def test_pins_are_valid_board_1(self): """""" Test for valid pins when all others are in use """""" import l293d as d d.pins_in_use = [7, 11, 12, 13, 15, 29, 31, 32, 33, 36, 37] self.assertTrue(d.pins_are_valid([22, 18, 16])) reload(d.driver) def test_pins_are_valid_board_2(self): """""" Test for valid pins when a pin is already in use """""" import l293d as d d.pins_in_use = [7, 11, 12] try: valid = d.pins_are_valid([31, 36, 11]) if valid: self.assertFalse(True) except Exception: self.assertFalse(False) reload(d.driver) def test_motor_can_be_removed(self): """""" Test that a motor can be created and removed """""" import l293d as d original_pins = d.pins_in_use motor = d.DC(29, 7, 13) motor.remove() self.assertEqual(d.pins_in_use, original_pins) reload(d.driver) def test_pin_numbering_lock(self): """""" Test that pin_numbering can't be changed after a motor's definition """""" import l293d as d d.Config.pin_numbering = 'BcM' m1 = d.DC(4, 5, 6) error = 'No error' try: d.Config.pin_numbering = 'BoaRD' except ValueError as e: error = str(e) self.assertEqual( error, 'Pin numbering format cannot be changed ' 'if motors already exist. Set this at ' 'the start of your script.') m1.remove() d.Config.pin_numbering = 'BOARD' reload(d.driver) def test_getting_config(self): """""" Test that Config supports both get_ and properties """""" import l293d as d self.assertEqual(d.Config.pin_numbering, d.Config.get_pin_numbering()) def test_setting_config(self): """""" Test that Config supports both set_ and properties """""" import l293d as d d.Config.set_verbose(False) self.assertEqual(d.Config.verbose, d.Config.get_verbose()) reload(d.driver) d.Config.verbose = False self.assertEqual(d.Config.verbose, d.Config.get_verbose()) if __name__ == '__main__': unittest.main() ", https://github.com/jamesevickery/l293d,c026a9b58769f23d3e999234ba9df30fb021f949,tests/test_l293d.py::L293DTestCase::test_motor_can_be_removed,tests/test_l293d.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from sys import version_info # Python 2: reload is built-in if version_info.major == 3: if version_info.minor < 4: # Python 3.0 - 3.3: deprecated since Python 3.4 in favour of importlib from imp import reload else: # Python 3.4+ from importlib import reload class L293DTestCase(unittest.TestCase): def test_import(self): """""" Import driver module, then assertTrue. If the import fails, an exception is raised and the assertion is never reached """""" import l293d as d print(str(d)) self.assertTrue(True) reload(d.driver) # Revert changes def test_create_motor_with_force_selection(self): """""" Test DC class instance creation with explicity force_selection parameter """""" import l293d as d cases = (([33, 36, 37], False), ([19, 21, 23], True)) for pins, force_selection in cases: motor = d.DC(*pins, force_selection=force_selection) self.assertEqual(d.pins_in_use, pins) motor.remove() reload(d.driver) def test_pins_string_list(self): """""" Test that the list of pins is returned correctly """""" import l293d as d print(d.pins_in_use) motor = d.DC(29, 7, 13) self.assertEqual(motor.pins_string_list(), '[29, 7 and 13]') reload(d.driver) def test_pins_are_valid_board_1(self): """""" Test for valid pins when all others are in use """""" import l293d as d d.pins_in_use = [7, 11, 12, 13, 15, 29, 31, 32, 33, 36, 37] self.assertTrue(d.pins_are_valid([22, 18, 16])) reload(d.driver) def test_pins_are_valid_board_2(self): """""" Test for valid pins when a pin is already in use """""" import l293d as d d.pins_in_use = [7, 11, 12] try: valid = d.pins_are_valid([31, 36, 11]) if valid: self.assertFalse(True) except Exception: self.assertFalse(False) reload(d.driver) def test_motor_can_be_removed(self): """""" Test that a motor can be created and removed """""" import l293d as d original_pins = d.pins_in_use motor = d.DC(29, 7, 13) motor.remove() self.assertEqual(d.pins_in_use, original_pins) reload(d.driver) def test_pin_numbering_lock(self): """""" Test that pin_numbering can't be changed after a motor's definition """""" import l293d as d d.Config.pin_numbering = 'BcM' m1 = d.DC(4, 5, 6) error = 'No error' try: d.Config.pin_numbering = 'BoaRD' except ValueError as e: error = str(e) self.assertEqual( error, 'Pin numbering format cannot be changed ' 'if motors already exist. Set this at ' 'the start of your script.') m1.remove() d.Config.pin_numbering = 'BOARD' reload(d.driver) def test_getting_config(self): """""" Test that Config supports both get_ and properties """""" import l293d as d self.assertEqual(d.Config.pin_numbering, d.Config.get_pin_numbering()) def test_setting_config(self): """""" Test that Config supports both set_ and properties """""" import l293d as d d.Config.set_verbose(False) self.assertEqual(d.Config.verbose, d.Config.get_verbose()) reload(d.driver) d.Config.verbose = False self.assertEqual(d.Config.verbose, d.Config.get_verbose()) if __name__ == '__main__': unittest.main() ", https://github.com/jamesevickery/l293d,c026a9b58769f23d3e999234ba9df30fb021f949,tests/test_l293d.py::L293DTestCase::test_pin_numbering_lock,tests/test_l293d.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from sys import version_info # Python 2: reload is built-in if version_info.major == 3: if version_info.minor < 4: # Python 3.0 - 3.3: deprecated since Python 3.4 in favour of importlib from imp import reload else: # Python 3.4+ from importlib import reload class L293DTestCase(unittest.TestCase): def test_import(self): """""" Import driver module, then assertTrue. If the import fails, an exception is raised and the assertion is never reached """""" import l293d as d print(str(d)) self.assertTrue(True) reload(d.driver) # Revert changes def test_create_motor_with_force_selection(self): """""" Test DC class instance creation with explicity force_selection parameter """""" import l293d as d cases = (([33, 36, 37], False), ([19, 21, 23], True)) for pins, force_selection in cases: motor = d.DC(*pins, force_selection=force_selection) self.assertEqual(d.pins_in_use, pins) motor.remove() reload(d.driver) def test_pins_string_list(self): """""" Test that the list of pins is returned correctly """""" import l293d as d print(d.pins_in_use) motor = d.DC(29, 7, 13) self.assertEqual(motor.pins_string_list(), '[29, 7 and 13]') reload(d.driver) def test_pins_are_valid_board_1(self): """""" Test for valid pins when all others are in use """""" import l293d as d d.pins_in_use = [7, 11, 12, 13, 15, 29, 31, 32, 33, 36, 37] self.assertTrue(d.pins_are_valid([22, 18, 16])) reload(d.driver) def test_pins_are_valid_board_2(self): """""" Test for valid pins when a pin is already in use """""" import l293d as d d.pins_in_use = [7, 11, 12] try: valid = d.pins_are_valid([31, 36, 11]) if valid: self.assertFalse(True) except Exception: self.assertFalse(False) reload(d.driver) def test_motor_can_be_removed(self): """""" Test that a motor can be created and removed """""" import l293d as d original_pins = d.pins_in_use motor = d.DC(29, 7, 13) motor.remove() self.assertEqual(d.pins_in_use, original_pins) reload(d.driver) def test_pin_numbering_lock(self): """""" Test that pin_numbering can't be changed after a motor's definition """""" import l293d as d d.Config.pin_numbering = 'BcM' m1 = d.DC(4, 5, 6) error = 'No error' try: d.Config.pin_numbering = 'BoaRD' except ValueError as e: error = str(e) self.assertEqual( error, 'Pin numbering format cannot be changed ' 'if motors already exist. Set this at ' 'the start of your script.') m1.remove() d.Config.pin_numbering = 'BOARD' reload(d.driver) def test_getting_config(self): """""" Test that Config supports both get_ and properties """""" import l293d as d self.assertEqual(d.Config.pin_numbering, d.Config.get_pin_numbering()) def test_setting_config(self): """""" Test that Config supports both set_ and properties """""" import l293d as d d.Config.set_verbose(False) self.assertEqual(d.Config.verbose, d.Config.get_verbose()) reload(d.driver) d.Config.verbose = False self.assertEqual(d.Config.verbose, d.Config.get_verbose()) if __name__ == '__main__': unittest.main() ", https://github.com/JBielan/py_ev,0a2d48235b8ff2268254c151a179a2ece40cbd37,tests/test_py_ev.py::test_reset,tests/test_py_ev.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from py_ev.py_ev import Evaluator ev = Evaluator() def test_reset(): ev.reset() assert len(ev.deck) == 52 assert ev.board == [] def test_build_deck(): ev.reset() assert len(ev.build_deck()) == 52 assert ev.new_deck == [(2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4), (5, 1), (5, 2), (5, 3), (5, 4), (6, 1), (6, 2), (6, 3), (6, 4), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4), (11, 1), (11, 2), (11, 3), (11, 4), (12, 1), (12, 2), (12, 3), (12, 4), (13, 1), (13, 2), (13, 3), (13, 4), (14, 1), (14, 2), (14, 3), (14, 4)] def test_deal(): ev.reset() assert len(ev.deal(5)) == 5 assert len(ev.deck) == 47 def test_set_cards(): ev.reset() ev.board = ev.set_cards((14, 3), (2, 1), (12, 2)) assert ev.board == [(14, 3), (2, 1), (12, 2)] assert len(ev.deck) == 49 def test_analyze_board(): ev.reset() cards = [(6, 2), (6, 3), (3, 3), (2, 1)] board = [(7, 4), (5, 2), (12, 1), (13, 4), (7, 2)] total, pairness, suitness = ev.analyze_board(cards, board) assert pairness == {14: 0, 13: 1, 12: 1, 11: 0, 10: 0, 9: 0, 8: 0, 7: 2, 6: 2, 5: 1, 4: 0, 3: 1, 2: 1} assert suitness == {1: 2, 2: 3, 3: 2, 4: 2} assert total == [(13, 4), (12, 1), (7, 4), (7, 2), (6, 2), (6, 3), (5, 2), (3, 3), (2, 1)] def test_is_str8(): ev = Evaluator() cards, pairness, suitness = ev.analyze_board([(11, 2), (10, 3), (9, 4), (14, 3)], [(2, 2), (3, 3), (8, 1), (7, 2), (4, 3)]) result = ev.is_str8(cards, pairness, suitness) assert result == (5, 11, 'Straight') cards, pairness, suitness = ev.analyze_board([(11, 2), (10, 3), (12, 4), (14, 3)], [(2, 2), (3, 3), (13, 1), (7, 2), (4, 3)]) result = ev.is_str8(cards, pairness, suitness) assert result == (5, 14, 'Straight') cards, pairness, suitness = ev.analyze_board([(11, 2), (10, 3), (5, 4), (14, 3)], [(2, 2), (3, 3), (13, 1), (7, 2), (4, 3)]) result = ev.is_str8(cards, pairness, suitness) assert result == (5, 5, 'Straight') cards, pairness, suitness = ev.analyze_board([(11, 2), (10, 3), (5, 4), (14, 3)], [(2, 2), (3, 3), (13, 1), (7, 2), (10, 3)]) result = ev.is_str8(cards, pairness, suitness) assert result == (False, 0, None) def test_is_flush(): ev = Evaluator() cards, pairness, suitness = ev.analyze_board([(11, 2), (9, 2)], [(2, 2), (3, 2), (8, 1), (7, 2), (4, 3)]) result = ev.is_flush(cards, pairness, suitness) assert result == (6, 110000+9000+700+30+2, 'Flush') cards, pairness, suitness = ev.analyze_board([(11, 2), (10, 2)], [(2, 2), (9, 2), (14, 2), (7, 2), (4, 2)]) result = ev.is_flush(cards, pairness, suitness) assert result == (6, 140000+11000+1000+90+7, 'Flush') cards, pairness, suitness = ev.analyze_board([(11, 2), (9, 2)], [(2, 2), (10, 2), (14, 2), (7, 3), (4, 3)]) result = ev.is_flush(cards, pairness, suitness) assert result == (6, 140000+11000+1000+90+2, 'Flush') cards, pairness, suitness = ev.analyze_board([(2, 2), (4, 2)], [(5, 2), (7, 2), (3, 2), (7, 4), (14, 2)]) result = ev.is_flush(cards, pairness, suitness) assert result == (6, 140000+7000+500+40+3, 'Flush') cards, pairness, suitness = ev.analyze_board([(2, 2), (3, 2)], [(5, 2), (7, 4), (8, 3), (7, 4), (4, 3)]) result = ev.is_flush(cards, pairness, suitness) assert result == (False, 0, None) def test_is_quad(): ev = Evaluator() cards, pairness, suitness = ev.analyze_board([(11, 2), (10, 3), (9, 2), (14, 3)], [(2, 2), (3, 2), (8, 1), (7, 2), (4, 3)]) result = ev.is_quad(cards, pairness, suitness) assert result == (False, 0, None) cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3), (9, 2), (14, 3)], [(2, 2), (3, 2), (11, 1), (7, 2), (4, 3)]) result = ev.is_quad(cards, pairness, suitness) assert result == (False, 0, None) cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3), (9, 2), (14, 3)], [(2, 2), (3, 2), (11, 1), (7, 2), (11, 3)]) result = ev.is_quad(cards, pairness, suitness) assert result == (8, 110+14, 'Four of a Kind') cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3), (14, 2), (14, 3)], [(2, 2), (14, 2), (11, 1), (14, 2), (11, 3)]) result = ev.is_quad(cards, pairness, suitness) assert result == (8, 140+11, 'Four of a Kind') cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(2, 2), (11, 4), (11, 1)]) result = ev.is_quad(cards, pairness, suitness) assert result == (8, 110+2, 'Four of a Kind') def test_is_fullhouse(): ev = Evaluator() cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(2, 2), (11, 1), (8, 1), (4, 2), (4, 3)]) result = ev.is_fullhouse(cards, pairness, suitness) assert result == (7, 110+4, 'Full House') cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(12, 2), (11, 1), (12, 1), (12, 4), (4, 3)]) result = ev.is_fullhouse(cards, pairness, suitness) assert result == (7, 120+11, 'Full House') cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(11, 4), (11, 1), (5, 1), (12, 4), (4, 3)]) result = ev.is_fullhouse(cards, pairness, suitness) assert result == (False, 0, None) cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(11, 4), (10, 1), (5, 1), (12, 4), (4, 3)]) result = ev.is_fullhouse(cards, pairness, suitness) assert result == (False, 0, None) cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(10, 4), (10, 1), (5, 1), (12, 4), (4, 3)]) result = ev.is_fullhouse(cards, pairness, suitness) assert result == (False, 0, None) def test_is_3_of_a_kind(): ev = Evaluator() cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(2, 2), (11, 1), (8, 1), (3, 2), (4, 3)]) result = ev.is_3_of_a_kind(cards, pairness, suitness) assert result == (4, 1100+80+4, 'Three of a kind') cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(2, 2), (10, 1), (8, 1), (3, 2), (4, 3)]) result = ev.is_3_of_a_kind(cards, pairness, suitness) assert result == (False, 0, None) cards, pairness, suitness = ev.analyze_board([(11, 2), (12, 3)], [(2, 2), (2, 1), (2, 3), (3, 2), (4, 3)]) result = ev.is_3_of_a_kind(cards, pairness, suitness) assert result == (4, 200+120+11, 'Three of a kind') cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(2, 2), (2, 1), (5, 3), (3, 2), (4, 3)]) result = ev.is_3_of_a_kind(cards, pairness, suitness) assert result == (False, 0, None) cards, pairness, suitness = ev.analyze_board([(12, 4), (10, 1)], [(10, 4), (10, 2), (4, 1), (3, 2), (2, 3)]) result = ev.is_3_of_a_kind(cards, pairness, suitness) assert result == (4, 1000+120+4, 'Three of a kind') def test_is_2_pairs(): ev = Evaluator() cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(12, 2), (12, 1), (3, 1), (3, 2), (4, 3)]) result = ev.is_2_pairs(cards, pairness, suitness) assert result == (3, 1314, 'Two pair') cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(12, 2), (12, 1), (3, 1), (3, 2), (14, 3)]) result = ev.is_2_pairs(cards, pairness, suitness) assert result == (3, 1324, 'Two pair') cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(10, 2), (12, 1), (3, 1), (3, 2), (14, 3)]) result = ev.is_2_pairs(cards, pairness, suitness) assert result == (3, 1144, 'Two pair') cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(10, 2), (12, 1), (2, 1), (3, 2), (14, 3)]) result = ev.is_2_pairs(cards, pairness, suitness) assert result == (False, 0, None) cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(10, 2), (11, 1), (2, 1), (3, 2), (14, 3)]) result = ev.is_2_pairs(cards, pairness, suitness) assert result == (False, 0, None) cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(11, 2), (11, 1), (2, 1), (3, 2), (14, 3)]) result = ev.is_2_pairs(cards, pairness, suitness) assert result == (False, 0, None) def test_is_pair(): ev = Evaluator() cards, pairness, suitness = ev.analyze_board([(11, 2), (11, 3)], [(13, 2), (14, 1), (2, 1), (3, 2), (4,", https://github.com/jedie/bootstrap_env,ab68025d8f6b9a17d8feeed83e8aae26e3f28769,bootstrap_env/tests/test_boot.py::BootstrapEnvTestCase::test_setup,bootstrap_env/tests/test_boot.py,NOD,flaky,Unmaintained,nan,classify;root_cause," """""" :created: 11.03.2018 by Jens Diemer, www.jensdiemer.de :copyleft: 2018 by the bootstrap_env team, see AUTHORS for more details. :license: GNU General Public License v3 or later (GPLv3+), see LICENSE for more details. """""" import os import subprocess import unittest from pathlib import Path # Bootstrap-Env from bootstrap_env import boot_bootstrap_env from bootstrap_env.boot_bootstrap_env import VerboseSubprocess, DisplayErrors from bootstrap_env.tests.base import BootstrapEnvTestCase from bootstrap_env.tests.utils import IsolatedFilesystem, path_helper class TestBootstrapEnvBoot(BootstrapEnvTestCase): """""" Tests for bootstrap_env/boot_bootstrap_env.py Note: Travis-CI used boot_bootstrap_env.py to bootstrap into ""normal"" and ""develop"" mode! No need to test is here again ;) Unfortunately, however, the coverage for bootstrapping are missing. """""" @unittest.skipIf(path_helper.normal_mode, ""Executeable is not set by PyPi installation"") def test_executable(self): file_path = Path(boot_bootstrap_env.__file__).resolve() self.assertTrue(file_path.is_file()) self.assertTrue( os.access(str(file_path), os.X_OK), ""File '%s' not executeable!"" % file_path ) def test_subprocess_accept_pathlib_args(self): self.assertRaises(AssertionError, VerboseSubprocess, Path(""/foo/bar"")) def test_subprocess_accept_pathlib_kwargs(self): self.assertRaises(AssertionError, VerboseSubprocess, foo=Path(""/foo/bar"")) def boot_bootstrap_env_run(self, *args): return self._call(*args, filename=""boot_bootstrap_env.py"") def test_help(self): output = self.boot_bootstrap_env_run(""help"") print(output) self.assertIn(""boot_bootstrap_env.py shell"", output) self.assertIn(""Available commands (type help ):"", output) self.assertIn(""boot"", output) self.assertIn('Bootstrap bootstrap_env virtualenv in ""normal"" mode.', output) self.assertIn(""boot_developer"", output) self.assertIn('Bootstrap bootstrap_env virtualenv in ""developer"" mode.', output) # If DocString is missing in do_(): self.assertNotIn(""Undocumented"", output) def test_boot_into_existing_path(self): with IsolatedFilesystem(prefix=""test_boot_into_existing_path""): temp_path = Path().cwd() # isolated_filesystem does made a chdir to /tmp/... with self.assertRaises(subprocess.CalledProcessError) as cm: output = self.boot_bootstrap_env_run(""boot"", str(temp_path)) print(output) caller_process_error = cm.exception output = caller_process_error.output print(output) self.assertIn(""ERROR: Path '%s' already exists!"" % temp_path, output) def test_boot_with_activated_venv(self): with IsolatedFilesystem(prefix=""test_boot_with_activated_venv""): temp_path = Path().cwd() # isolated_filesystem does made a chdir to /tmp/... destination = Path(temp_path, ""test"") # a not existing path try: output = self.boot_bootstrap_env_run(""boot"", str(destination)) print(output) except subprocess.CalledProcessError as err: print(err) output = err.output print(output) self.assertIn(""Don't call me in a activated virtualenv!"", output) self.assertIn(""ERROR: Creating virtualenv!"", output) else: self.fail(""Doesn't abort!"") # def test_display_errors(self): # # @DisplayErrors # def test(foo): # print(""foo==%r"" % foo) # if not foo==""bar"": # raise AssertionError(""Whoop..."") # else: # print(""OK"") # # test(foo=""bar"") # with self.assertRaises(AssertionError) as cm: # test(foo=""foo"") # # output = ""\n"".join(cm.exception.args) # print(output) ", https://github.com/jedie/bootstrap_env,ab68025d8f6b9a17d8feeed83e8aae26e3f28769,bootstrap_env/tests/test_test_utils.py::BootstrapEnvTestCase::test_setup,bootstrap_env/tests/test_test_utils.py,NOD,flaky,Unmaintained,nan,classify;root_cause," """""" :created: 23.03.2018 by Jens Diemer, www.jensdiemer.de :copyleft: 2018 by the bootstrap_env team, see AUTHORS for more details. :license: GNU General Public License v3 or later (GPLv3+), see LICENSE for more details. """""" # Bootstrap-Env import sys from bootstrap_env.tests.base import BootstrapEnvTestCase class TestUtilsTestCase(BootstrapEnvTestCase): def test_assert_equal_unified_diff(self): first = [""Line %i\n"" % no for no in range(100)] second = first.copy() self.assert_equal_unified_diff(first, second) second.insert(50, ""Inserted at 50:"") with self.assertRaises(AssertionError) as cm: self.assert_equal_unified_diff(first, second) exception = cm.exception exception_text = exception.args[0] print(repr(exception_text)) self.assertEqual(exception_text, ( ""Content is not equal, unified diff:\n"" ""--- first\n"" ""+++ second\n"" ""@@ -48,6 +48,7 @@\n"" "" Line 47\n"" "" Line 48\n"" "" Line 49\n"" ""+Inserted at 50: Line 50\n"" "" Line 51\n"" "" Line 52\n"" )) def test_call(self): output = self._call(""--test"", filename=__file__) print(output) self.assertIn(""test_test_utils.py --test"", output) if __name__ == '__main__': print(""sys.argv:"", "" "".join(sys.argv)) ", https://github.com/JGoutin/compilertools,7b898cb6970f5f25c5356b15a2efd0cb2e54d566,tests/test_build.py::tests_patch___new__,tests/test_build.py,OD-Brit,flaky,nan,nan,classify;root_cause,"""""""Tests for building"""""" # Get methods references before compilertools.build path them from distutils.command.build_ext import build_ext BUILD_EXTENSION = build_ext.build_extension GET_EXT_FILENAME = build_ext.get_ext_filename GET_EXT_FULLNAME = build_ext.get_ext_fullname GET_OUTPUTS = build_ext.get_outputs BUILD_EXT_NEW = build_ext.__new__ def tests_get_build_compile_args(): """"""Test get_build_compile_args"""""" from distutils.sysconfig import get_config_var from compilertools.compilers import CompilerBase from compilertools.build import get_build_compile_args from compilertools._config_build import ConfigBuild # File extension ext_suffix = get_config_var(""EXT_SUFFIX"") # Initialize compiler raise_exception = False class Compiler(CompilerBase): """"""Dummy Compiler"""""" def __init__(self, current_compiler=False): CompilerBase.__init__(self, current_compiler=current_compiler) self[""api""][""api_name""] = {""compile"": ""--api-compile"", ""link"": ""--api-link""} self[""option""][""option_name""] = { ""compile"": ""--option-compile"", ""link"": ""--option-link"", } def _compile_args_matrix(self, arch, cpu): """"""Return test args matrix"""""" return [ [ self.Arg( args=""--arch1"", suffix=""arch1"", build_if=(arch == ""arch1"") ), self.Arg( args=""--arch2"", suffix=""arch2"", build_if=(arch == ""arch2"") ), self.Arg( args=""--arch2_opt"", suffix=""arch2_opt"", build_if=(arch == ""arch2""), ), ] ] def _compile_args_current_machine(self, arch, cpu): """"""return current machine args"""""" if raise_exception: raise OSError return ""--native"" compiler = Compiler(current_compiler=True) # Test default values assert get_build_compile_args(compiler, ""arch1"") == { "".arch1%s"" % ext_suffix: [""--arch1""] } # Test current_machine assert get_build_compile_args(compiler, current_machine=True) == { ext_suffix: [""--native""] } # Test current_machine, no exception in case of error raise_exception = True assert get_build_compile_args(compiler, current_machine=True) == {ext_suffix: []} raise_exception = False # Test current_machine from CONFIG_BUILD ConfigBuild.current_machine = True assert get_build_compile_args(compiler) == {ext_suffix: [""--native""]} ConfigBuild.current_machine = False # Test ext_suffix assert get_build_compile_args(compiler, ""arch1"", ext_suffix="".ext"") == { "".arch1.ext"": [""--arch1""] } # Test use_api assert get_build_compile_args(compiler, ""arch1"", use_api=[""api_name""]) == { "".arch1%s"" % ext_suffix: [""--arch1"", ""--api-compile""] } # Test use_option assert get_build_compile_args(compiler, ""arch1"", use_option=[""option_name""]) == { "".arch1%s"" % ext_suffix: [""--arch1"", ""--option-compile""] } # Test filtering suffixes assert get_build_compile_args(compiler, ""arch2"") == { "".arch2%s"" % ext_suffix: [""--arch2""], "".arch2_opt%s"" % ext_suffix: [""--arch2_opt""], } ConfigBuild.suffixes_excludes.add(""arch2_opt"") assert get_build_compile_args(compiler, ""arch2"") == { "".arch2%s"" % ext_suffix: [""--arch2""] } ConfigBuild.suffixes_excludes.remove(""arch2_opt"") ConfigBuild.suffixes_includes.add(""arch2"") assert get_build_compile_args(compiler, ""arch2"") == { "".arch2%s"" % ext_suffix: [""--arch2""] } ConfigBuild.suffixes_includes.remove(""arch2"") def tests_get_build_link_args(): """"""Test get_build_link_args"""""" from compilertools.compilers import CompilerBase from compilertools.build import get_build_link_args class Compiler(CompilerBase): """"""Dummy Compiler"""""" def __init__(self): CompilerBase.__init__(self) self[""api""][""api_name""] = {""compile"": ""--api-compile"", ""link"": ""--api-link""} self[""option""][""option_name""] = { ""compile"": ""--option-compile"", ""link"": ""--option-link"", } compiler = Compiler() # Test default values assert get_build_link_args(compiler) == [] # Test use_api assert get_build_link_args(compiler, use_api=[""api_name""]) == [""--api-link""] # Test use_option assert get_build_link_args(compiler, use_option=[""option_name""]) == [ ""--option-link"" ] def tests_find_if_current_machine(): """"""Test _find_if_current_machine"""""" import os from compilertools.build import _find_if_current_machine from compilertools._config_build import ConfigBuild w_dir = os.getcwd() def dummy_getcwd(): """"""Dummy os.getcwd"""""" return w_dir os_getcwd = os.getcwd os.getcwd = dummy_getcwd # Set by configuration ConfigBuild.current_machine = False assert _find_if_current_machine() is False ConfigBuild.current_machine = True assert _find_if_current_machine() is True # Pip detection ConfigBuild.current_machine = ""autodetect"" w_dir = ""dir/not_current_machine"" assert _find_if_current_machine() is False w_dir = ""dir/pip-‌​current_machine"" assert _find_if_current_machine() is True # Cleaning os.getcwd = os_getcwd ConfigBuild.current_machine = False def tests_add_args(): """"""Test _add_args"""""" from compilertools.compilers import CompilerBase from compilertools.build import _add_args class Compiler(CompilerBase): """"""Dummy Compiler"""""" def __init__(self): CompilerBase.__init__(self) self[""api""][""api_name""] = {""compile"": ""--api-compile""} compiler = Compiler() # API & category exists args = [] _add_args(compiler, args, ""api"", ""compile"", [""api_name""]) assert args == [""--api-compile""] # API exists, category not exists args = [] _add_args(compiler, args, ""api"", ""link"", [""api_name""]) assert args == [] # API not exists, category exists args = [] _add_args(compiler, args, ""api"", ""compile"", [""not_exist""]) assert args == [] def tests_update_extension(): """"""Test _update_extension, _patch_build_extension.patched and _patch_get_ext_filename.patched"""""" from os.path import join from os import makedirs from tempfile import TemporaryDirectory from distutils.sysconfig import get_config_var from compilertools.compilers import CompilerBase from compilertools._config_build import ConfigBuild from compilertools.build import ( _update_extension, _patch_build_extension, _patch_get_ext_filename, _patch_get_ext_fullname, _patch_get_outputs, _String, ) # File extension ext_suffix = get_config_var(""EXT_SUFFIX"") # Initialize compiler class Compiler(CompilerBase): """"""Dummy Compiler"""""" def __init__(self): CompilerBase.__init__(self) self[""api""][""api_name""] = {""compile"": ""--api-compile"", ""link"": ""--api-link""} self[""option""][""option_name""] = { ""compile"": ""--option-compile"", ""link"": ""--option-link"", } def _compile_args_matrix(self, arch, cpu): """"""Return test args matrix"""""" return [ [self.Arg(args=""--inst"", suffix=""inst""), self.Arg()], [self.Arg(args=""--arch"", suffix=""arch""), self.Arg()], ] compiler = Compiler() # Create dummy distutils classes class DummyCompiler: """"""Dummy distutils.ccompiler.CCompiler"""""" def __init__(self): # Replace compiler type str by Compiler instance # This force the use of the testing compiler self.compiler_type = compiler class DummyExtension: """"""Dummy distutils.extension.Extension"""""" def __init__(self): self.sources = [] self.extra_compile_args = [""--extra_compile""] self.extra_link_args = [""--extra_link""] self.name = ""package.module"" class DummyBuildExt: """"""Dummy distutils.command.build_ext.build_ext"""""" def __init__(self): self.package = None self.extensions = [] self.compiler = DummyCompiler() self.plat_name = ""arch"" self.inplace = False # Use build_ext.get_ext_filename directly get_ext_filename = GET_EXT_FILENAME get_ext_fullname = GET_EXT_FULLNAME def get_outputs(self): """"""Dummy get_outputs"""""" return [] def build_extension(self, _): """"""Dummy build_extension"""""" # Patch dummy build_ext DummyBuildExt.build_extension = _patch_build_extension( DummyBuildExt.build_extension ) DummyBuildExt.get_ext_filename = _patch_get_ext_filename( DummyBuildExt.get_ext_filename ) DummyBuildExt.build_extension = _patch_build_extension( DummyBuildExt.build_extension ) DummyBuildExt.get_ext_fullname = _patch_get_ext_fullname( DummyBuildExt.get_ext_fullname ) DummyBuildExt.get_outputs = _patch_get_outputs(DummyBuildExt.get_outputs) # Test with patched build_extension dummy_build_ext = DummyBuildExt() dummy_ext = DummyExtension() dummy_build_ext.build_extension(dummy_ext) results = dummy_build_ext.extensions # Check result count excepted_args = compiler.compile_args() assert len(excepted_args) != 0 assert len(results) == len(excepted_args) # Check results details results.sort( key=lambda x: getattr(x, ""compilertools_extended_suffix"") if hasattr(x, ""compilertools_extended_s", https://github.com/JGoutin/compilertools,7b898cb6970f5f25c5356b15a2efd0cb2e54d566,tests/test___init__.py::tests_init,tests/test___init__.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test module import"""""" def tests_init(): """"""Test __version__ presence and format"""""" from pytest import raises from collections import namedtuple import sys sys_version_info = sys.version_info version_info = namedtuple( ""Version_Info"", [""major"", ""minor"", ""micro"", ""releaselevel"", ""serial""] ) try: with raises(ImportError): sys.version_info = version_info(3, 3, 0, ""final"", 0) import compilertools # noqa: F401 # Cleaning finally: sys.version_info = sys_version_info def tests_get_processor(): """""" Test function redirection. """""" import compilertools from compilertools.processors import get_processor as _get_processor assert compilertools.get_processor() == _get_processor( arch=None, current_machine=True ) def tests_get_compiler(): """""" Test function redirection. """""" import compilertools from compilertools.compilers import get_compiler as _get_compiler assert compilertools.get_compiler() == _get_compiler(current_compiler=True) ", https://github.com/jnoortheen/dynamic-conf,42257f06a66765ac4f5de1c6a4637f4db779c5ab,tests/test_conf.py::test_cofig_writing[.env-ARG1=VAL1\nARG2=VAL2],tests/test_conf.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest def create_config_cls(): import uuid from dynamic_conf import REQUIRED, Config return type( ""CONFIG"" + str(uuid.uuid4().hex), (Config,), dict( NUM=1, NONE_VAL=None, OVERLOADED=""load"", VAR=None, FROM_FILE=REQUIRED, MISSING=REQUIRED, ), ) @pytest.mark.parametrize(""file"", [""env.py"", "".env""]) def test_config_reading(file, config_factory): CONF = config_factory(file) assert CONF.NUM == 1 # check type casting assert CONF.BOOL == False # check type casting assert CONF.NONE_VAL is None assert CONF.VAR == ""variable"" assert CONF.OVERLOADED == ""over-loaded"" assert CONF.FROM_FILE == ""file"" with pytest.raises(LookupError): print(CONF.MISSING) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) # start write env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result with pytest.raises(Exception) as ex: _main([conf_file, ""ARG2=VAL2""]) # start write again assert ""Found"" in str(ex) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing_with_filter_prefix(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""PRE_ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_PREFIX"", ""PRE_"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result @pytest.mark.parametrize( ""file, result"", [ (""env.py"", ""ARG1 = 'VAL1'\nARG3 = 3\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG3=3\nARG2=VAL2""), ], ) def test_cofig_writing_with_dump(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_DUMP"", ""True"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1, ARG3=3) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result def test_singleton(clean_config): create_config_cls() with pytest.raises(NotImplementedError): create_config_cls() ", https://github.com/jnoortheen/dynamic-conf,42257f06a66765ac4f5de1c6a4637f4db779c5ab,tests/test_conf.py::test_cofig_writing[env.py-ARG1 = 'VAL1'\nARG2 = 'VAL2'],tests/test_conf.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest def create_config_cls(): import uuid from dynamic_conf import REQUIRED, Config return type( ""CONFIG"" + str(uuid.uuid4().hex), (Config,), dict( NUM=1, NONE_VAL=None, OVERLOADED=""load"", VAR=None, FROM_FILE=REQUIRED, MISSING=REQUIRED, ), ) @pytest.mark.parametrize(""file"", [""env.py"", "".env""]) def test_config_reading(file, config_factory): CONF = config_factory(file) assert CONF.NUM == 1 # check type casting assert CONF.BOOL == False # check type casting assert CONF.NONE_VAL is None assert CONF.VAR == ""variable"" assert CONF.OVERLOADED == ""over-loaded"" assert CONF.FROM_FILE == ""file"" with pytest.raises(LookupError): print(CONF.MISSING) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) # start write env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result with pytest.raises(Exception) as ex: _main([conf_file, ""ARG2=VAL2""]) # start write again assert ""Found"" in str(ex) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing_with_filter_prefix(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""PRE_ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_PREFIX"", ""PRE_"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result @pytest.mark.parametrize( ""file, result"", [ (""env.py"", ""ARG1 = 'VAL1'\nARG3 = 3\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG3=3\nARG2=VAL2""), ], ) def test_cofig_writing_with_dump(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_DUMP"", ""True"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1, ARG3=3) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result def test_singleton(clean_config): create_config_cls() with pytest.raises(NotImplementedError): create_config_cls() ", https://github.com/jnoortheen/dynamic-conf,42257f06a66765ac4f5de1c6a4637f4db779c5ab,tests/test_conf.py::test_cofig_writing_with_dump[.env-ARG1=VAL1\nARG3=3\nARG2=VAL2],tests/test_conf.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest def create_config_cls(): import uuid from dynamic_conf import REQUIRED, Config return type( ""CONFIG"" + str(uuid.uuid4().hex), (Config,), dict( NUM=1, NONE_VAL=None, OVERLOADED=""load"", VAR=None, FROM_FILE=REQUIRED, MISSING=REQUIRED, ), ) @pytest.mark.parametrize(""file"", [""env.py"", "".env""]) def test_config_reading(file, config_factory): CONF = config_factory(file) assert CONF.NUM == 1 # check type casting assert CONF.BOOL == False # check type casting assert CONF.NONE_VAL is None assert CONF.VAR == ""variable"" assert CONF.OVERLOADED == ""over-loaded"" assert CONF.FROM_FILE == ""file"" with pytest.raises(LookupError): print(CONF.MISSING) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) # start write env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result with pytest.raises(Exception) as ex: _main([conf_file, ""ARG2=VAL2""]) # start write again assert ""Found"" in str(ex) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing_with_filter_prefix(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""PRE_ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_PREFIX"", ""PRE_"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result @pytest.mark.parametrize( ""file, result"", [ (""env.py"", ""ARG1 = 'VAL1'\nARG3 = 3\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG3=3\nARG2=VAL2""), ], ) def test_cofig_writing_with_dump(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_DUMP"", ""True"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1, ARG3=3) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result def test_singleton(clean_config): create_config_cls() with pytest.raises(NotImplementedError): create_config_cls() ", https://github.com/jnoortheen/dynamic-conf,42257f06a66765ac4f5de1c6a4637f4db779c5ab,tests/test_conf.py::test_cofig_writing_with_dump[env.py-ARG1 = 'VAL1'\nARG3 = 3\nARG2 = 'VAL2'],tests/test_conf.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest def create_config_cls(): import uuid from dynamic_conf import REQUIRED, Config return type( ""CONFIG"" + str(uuid.uuid4().hex), (Config,), dict( NUM=1, NONE_VAL=None, OVERLOADED=""load"", VAR=None, FROM_FILE=REQUIRED, MISSING=REQUIRED, ), ) @pytest.mark.parametrize(""file"", [""env.py"", "".env""]) def test_config_reading(file, config_factory): CONF = config_factory(file) assert CONF.NUM == 1 # check type casting assert CONF.BOOL == False # check type casting assert CONF.NONE_VAL is None assert CONF.VAR == ""variable"" assert CONF.OVERLOADED == ""over-loaded"" assert CONF.FROM_FILE == ""file"" with pytest.raises(LookupError): print(CONF.MISSING) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) # start write env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result with pytest.raises(Exception) as ex: _main([conf_file, ""ARG2=VAL2""]) # start write again assert ""Found"" in str(ex) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing_with_filter_prefix(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""PRE_ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_PREFIX"", ""PRE_"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result @pytest.mark.parametrize( ""file, result"", [ (""env.py"", ""ARG1 = 'VAL1'\nARG3 = 3\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG3=3\nARG2=VAL2""), ], ) def test_cofig_writing_with_dump(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_DUMP"", ""True"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1, ARG3=3) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result def test_singleton(clean_config): create_config_cls() with pytest.raises(NotImplementedError): create_config_cls() ", https://github.com/jnoortheen/dynamic-conf,42257f06a66765ac4f5de1c6a4637f4db779c5ab,tests/test_conf.py::test_cofig_writing_with_filter_prefix[.env-ARG1=VAL1\nARG2=VAL2],tests/test_conf.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest def create_config_cls(): import uuid from dynamic_conf import REQUIRED, Config return type( ""CONFIG"" + str(uuid.uuid4().hex), (Config,), dict( NUM=1, NONE_VAL=None, OVERLOADED=""load"", VAR=None, FROM_FILE=REQUIRED, MISSING=REQUIRED, ), ) @pytest.mark.parametrize(""file"", [""env.py"", "".env""]) def test_config_reading(file, config_factory): CONF = config_factory(file) assert CONF.NUM == 1 # check type casting assert CONF.BOOL == False # check type casting assert CONF.NONE_VAL is None assert CONF.VAR == ""variable"" assert CONF.OVERLOADED == ""over-loaded"" assert CONF.FROM_FILE == ""file"" with pytest.raises(LookupError): print(CONF.MISSING) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) # start write env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result with pytest.raises(Exception) as ex: _main([conf_file, ""ARG2=VAL2""]) # start write again assert ""Found"" in str(ex) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing_with_filter_prefix(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""PRE_ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_PREFIX"", ""PRE_"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result @pytest.mark.parametrize( ""file, result"", [ (""env.py"", ""ARG1 = 'VAL1'\nARG3 = 3\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG3=3\nARG2=VAL2""), ], ) def test_cofig_writing_with_dump(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_DUMP"", ""True"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1, ARG3=3) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result def test_singleton(clean_config): create_config_cls() with pytest.raises(NotImplementedError): create_config_cls() ", https://github.com/jnoortheen/dynamic-conf,42257f06a66765ac4f5de1c6a4637f4db779c5ab,tests/test_conf.py::test_cofig_writing_with_filter_prefix[env.py-ARG1 = 'VAL1'\nARG2 = 'VAL2'],tests/test_conf.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest def create_config_cls(): import uuid from dynamic_conf import REQUIRED, Config return type( ""CONFIG"" + str(uuid.uuid4().hex), (Config,), dict( NUM=1, NONE_VAL=None, OVERLOADED=""load"", VAR=None, FROM_FILE=REQUIRED, MISSING=REQUIRED, ), ) @pytest.mark.parametrize(""file"", [""env.py"", "".env""]) def test_config_reading(file, config_factory): CONF = config_factory(file) assert CONF.NUM == 1 # check type casting assert CONF.BOOL == False # check type casting assert CONF.NONE_VAL is None assert CONF.VAR == ""variable"" assert CONF.OVERLOADED == ""over-loaded"" assert CONF.FROM_FILE == ""file"" with pytest.raises(LookupError): print(CONF.MISSING) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) # start write env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result with pytest.raises(Exception) as ex: _main([conf_file, ""ARG2=VAL2""]) # start write again assert ""Found"" in str(ex) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing_with_filter_prefix(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""PRE_ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_PREFIX"", ""PRE_"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result @pytest.mark.parametrize( ""file, result"", [ (""env.py"", ""ARG1 = 'VAL1'\nARG3 = 3\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG3=3\nARG2=VAL2""), ], ) def test_cofig_writing_with_dump(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_DUMP"", ""True"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1, ARG3=3) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result def test_singleton(clean_config): create_config_cls() with pytest.raises(NotImplementedError): create_config_cls() ", https://github.com/jnoortheen/dynamic-conf,42257f06a66765ac4f5de1c6a4637f4db779c5ab,tests/test_conf.py::test_config_reading[.env],tests/test_conf.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest def create_config_cls(): import uuid from dynamic_conf import REQUIRED, Config return type( ""CONFIG"" + str(uuid.uuid4().hex), (Config,), dict( NUM=1, NONE_VAL=None, OVERLOADED=""load"", VAR=None, FROM_FILE=REQUIRED, MISSING=REQUIRED, ), ) @pytest.mark.parametrize(""file"", [""env.py"", "".env""]) def test_config_reading(file, config_factory): CONF = config_factory(file) assert CONF.NUM == 1 # check type casting assert CONF.BOOL == False # check type casting assert CONF.NONE_VAL is None assert CONF.VAR == ""variable"" assert CONF.OVERLOADED == ""over-loaded"" assert CONF.FROM_FILE == ""file"" with pytest.raises(LookupError): print(CONF.MISSING) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) # start write env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result with pytest.raises(Exception) as ex: _main([conf_file, ""ARG2=VAL2""]) # start write again assert ""Found"" in str(ex) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing_with_filter_prefix(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""PRE_ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_PREFIX"", ""PRE_"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result @pytest.mark.parametrize( ""file, result"", [ (""env.py"", ""ARG1 = 'VAL1'\nARG3 = 3\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG3=3\nARG2=VAL2""), ], ) def test_cofig_writing_with_dump(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_DUMP"", ""True"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1, ARG3=3) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result def test_singleton(clean_config): create_config_cls() with pytest.raises(NotImplementedError): create_config_cls() ", https://github.com/jnoortheen/dynamic-conf,42257f06a66765ac4f5de1c6a4637f4db779c5ab,tests/test_conf.py::test_config_reading[env.py],tests/test_conf.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest def create_config_cls(): import uuid from dynamic_conf import REQUIRED, Config return type( ""CONFIG"" + str(uuid.uuid4().hex), (Config,), dict( NUM=1, NONE_VAL=None, OVERLOADED=""load"", VAR=None, FROM_FILE=REQUIRED, MISSING=REQUIRED, ), ) @pytest.mark.parametrize(""file"", [""env.py"", "".env""]) def test_config_reading(file, config_factory): CONF = config_factory(file) assert CONF.NUM == 1 # check type casting assert CONF.BOOL == False # check type casting assert CONF.NONE_VAL is None assert CONF.VAR == ""variable"" assert CONF.OVERLOADED == ""over-loaded"" assert CONF.FROM_FILE == ""file"" with pytest.raises(LookupError): print(CONF.MISSING) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) # start write env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result with pytest.raises(Exception) as ex: _main([conf_file, ""ARG2=VAL2""]) # start write again assert ""Found"" in str(ex) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing_with_filter_prefix(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""PRE_ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_PREFIX"", ""PRE_"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result @pytest.mark.parametrize( ""file, result"", [ (""env.py"", ""ARG1 = 'VAL1'\nARG3 = 3\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG3=3\nARG2=VAL2""), ], ) def test_cofig_writing_with_dump(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_DUMP"", ""True"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1, ARG3=3) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result def test_singleton(clean_config): create_config_cls() with pytest.raises(NotImplementedError): create_config_cls() ", https://github.com/jnoortheen/dynamic-conf,42257f06a66765ac4f5de1c6a4637f4db779c5ab,tests/test_conf.py::test_singleton,tests/test_conf.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest def create_config_cls(): import uuid from dynamic_conf import REQUIRED, Config return type( ""CONFIG"" + str(uuid.uuid4().hex), (Config,), dict( NUM=1, NONE_VAL=None, OVERLOADED=""load"", VAR=None, FROM_FILE=REQUIRED, MISSING=REQUIRED, ), ) @pytest.mark.parametrize(""file"", [""env.py"", "".env""]) def test_config_reading(file, config_factory): CONF = config_factory(file) assert CONF.NUM == 1 # check type casting assert CONF.BOOL == False # check type casting assert CONF.NONE_VAL is None assert CONF.VAR == ""variable"" assert CONF.OVERLOADED == ""over-loaded"" assert CONF.FROM_FILE == ""file"" with pytest.raises(LookupError): print(CONF.MISSING) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) # start write env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result with pytest.raises(Exception) as ex: _main([conf_file, ""ARG2=VAL2""]) # start write again assert ""Found"" in str(ex) @pytest.mark.parametrize( ""file, result"", [(""env.py"", ""ARG1 = 'VAL1'\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG2=VAL2""),], ) def test_cofig_writing_with_filter_prefix(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""PRE_ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_PREFIX"", ""PRE_"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result @pytest.mark.parametrize( ""file, result"", [ (""env.py"", ""ARG1 = 'VAL1'\nARG3 = 3\nARG2 = 'VAL2'""), ("".env"", ""ARG1=VAL1\nARG3=3\nARG2=VAL2""), ], ) def test_cofig_writing_with_dump(file, result, create_conf_file, monkeypatch): monkeypatch.setenv(""ARG1"", ""VAL1"") monkeypatch.setenv(""VARS_DUMP"", ""True"") from dynamic_conf import _main conf_file = create_conf_file(_file_name=repr(file), ARG1=1, ARG3=3) _main([conf_file, ""ARG2=VAL2""]) env_file = os.path.join(os.path.dirname(conf_file), file) assert os.path.exists(env_file) with open(env_file) as f: content = f.read() assert content == result def test_singleton(clean_config): create_config_cls() with pytest.raises(NotImplementedError): create_config_cls() ", https://github.com/joblib/joblib,440b1b899d357a9db6526ca87051a77963b33631,joblib/test/test_memory.py::test_argument_change,joblib/test/test_memory.py,NIO,flaky,Opened,https://github.com/joblib/joblib/pull/1631,classify;root_cause,""""""" Test the memory module. """""" # Author: Gael Varoquaux # Copyright (c) 2009 Gael Varoquaux # License: BSD Style, 3 clauses. import gc import shutil import os import os.path import pathlib import pickle import sys import time import datetime import textwrap import pytest from joblib.memory import Memory from joblib.memory import MemorizedFunc, NotMemorizedFunc from joblib.memory import MemorizedResult, NotMemorizedResult from joblib.memory import _FUNCTION_HASHES from joblib.memory import register_store_backend, _STORE_BACKENDS from joblib.memory import _build_func_identifier, _store_backend_factory from joblib.memory import JobLibCollisionWarning from joblib.parallel import Parallel, delayed from joblib._store_backends import StoreBackendBase, FileSystemStoreBackend from joblib.test.common import with_numpy, np from joblib.test.common import with_multiprocessing from joblib.testing import parametrize, raises, warns from joblib.hashing import hash ############################################################################### # Module-level variables for the tests def f(x, y=1): """""" A module-level function for testing purposes. """""" return x ** 2 + y ############################################################################### # Helper function for the tests def check_identity_lazy(func, accumulator, location): """""" Given a function and an accumulator (a list that grows every time the function is called), check that the function can be decorated by memory to be a lazy identity. """""" # Call each function with several arguments, and check that it is # evaluated only once per argument. memory = Memory(location=location, verbose=0) func = memory.cache(func) for i in range(3): for _ in range(2): assert func(i) == i assert len(accumulator) == i + 1 def corrupt_single_cache_item(memory): single_cache_item, = memory.store_backend.get_items() output_filename = os.path.join(single_cache_item.path, 'output.pkl') with open(output_filename, 'w') as f: f.write('garbage') def monkeypatch_cached_func_warn(func, monkeypatch_fixture): # Need monkeypatch because pytest does not # capture stdlib logging output (see # https://github.com/pytest-dev/pytest/issues/2079) recorded = [] def append_to_record(item): recorded.append(item) monkeypatch_fixture.setattr(func, 'warn', append_to_record) return recorded ############################################################################### # Tests def test_memory_integration(tmpdir): """""" Simple test of memory lazy evaluation. """""" accumulator = list() # Rmk: this function has the same name than a module-level function, # thus it serves as a test to see that both are identified # as different. def f(l): accumulator.append(1) return l check_identity_lazy(f, accumulator, tmpdir.strpath) # Now test clearing for compress in (False, True): for mmap_mode in ('r', None): memory = Memory(location=tmpdir.strpath, verbose=10, mmap_mode=mmap_mode, compress=compress) # First clear the cache directory, to check that our code can # handle that # NOTE: this line would raise an exception, as the database file is # still open; we ignore the error since we want to test what # happens if the directory disappears shutil.rmtree(tmpdir.strpath, ignore_errors=True) g = memory.cache(f) g(1) g.clear(warn=False) current_accumulator = len(accumulator) out = g(1) assert len(accumulator) == current_accumulator + 1 # Also, check that Memory.eval works similarly assert memory.eval(f, 1) == out assert len(accumulator) == current_accumulator + 1 # Now do a smoke test with a function defined in __main__, as the name # mangling rules are more complex f.__module__ = '__main__' memory = Memory(location=tmpdir.strpath, verbose=0) memory.cache(f)(1) @parametrize(""call_before_reducing"", [True, False]) def test_parallel_call_cached_function_defined_in_jupyter( tmpdir, call_before_reducing ): # Calling an interactively defined memory.cache()'d function inside a # Parallel call used to clear the existing cache related to the said # function (https://github.com/joblib/joblib/issues/1035) # This tests checks that this is no longer the case. # TODO: test that the cache related to the function cache persists across # ipython sessions (provided that no code change were made to the # function's source)? # The first part of the test makes the necessary low-level calls to emulate # the definition of a function in an jupyter notebook cell. Joblib has # some custom code to treat functions defined specifically in jupyter # notebooks/ipython session -- we want to test this code, which requires # the emulation to be rigorous. for session_no in [0, 1]: ipython_cell_source = ''' def f(x): return x ''' ipython_cell_id = ''.format(session_no) exec( compile( textwrap.dedent(ipython_cell_source), filename=ipython_cell_id, mode='exec' ) ) # f is now accessible in the locals mapping - but for some unknown # reason, f = locals()['f'] throws a KeyError at runtime, we need to # bind locals()['f'] to a different name in the local namespace aliased_f = locals()['f'] aliased_f.__module__ = ""__main__"" # Preliminary sanity checks, and tests checking that joblib properly # identified f as an interactive function defined in a jupyter notebook assert aliased_f(1) == 1 assert aliased_f.__code__.co_filename == ipython_cell_id memory = Memory(location=tmpdir.strpath, verbose=0) cached_f = memory.cache(aliased_f) assert len(os.listdir(tmpdir / 'joblib')) == 1 f_cache_relative_directory = os.listdir(tmpdir / 'joblib')[0] assert '__ipython-input__' in f_cache_relative_directory f_cache_directory = tmpdir / 'joblib' / f_cache_relative_directory if session_no == 0: # The cache should be empty as cached_f has not been called yet. assert os.listdir(f_cache_directory) == ['f'] assert os.listdir(f_cache_directory / 'f') == [] if call_before_reducing: cached_f(3) # Two files were just created, func_code.py, and a folder # containing the informations (inputs hash/ouptput) of # cached_f(3) assert len(os.listdir(f_cache_directory / 'f')) == 2 # Now, testing #1035: when calling a cached function, joblib # used to dynamically inspect the underlying function to # extract its source code (to verify it matches the source code # of the function as last inspected by joblib) -- however, # source code introspection fails for dynamic functions sent to # child processes - which would eventually make joblib clear # the cache associated to f res = Parallel(n_jobs=2)(delayed(cached_f)(i) for i in [1, 2]) else: # Submit the function to the joblib child processes, although # the function has never been called in the parent yet. This # triggers a specific code branch inside # MemorizedFunc.__reduce__. res = Parallel(n_jobs=2)(delayed(cached_f)(i) for i in [1, 2]) assert len(os.listdir(f_cache_directory / 'f')) == 3 cached_f(3) # Making sure f's cache does not get cleared after the parallel # calls, and contains ALL cached functions calls (f(1), f(2), f(3)) # and 'func_code.py' assert len(os.listdir(f_cache_directory / 'f')) == 4 else: # For the second session, there should be an already existing cache assert len(os.listdir(f_cache_directory / 'f')) == 4 cached_f(3) # The previous cache should not be invalidated after calling the # function in a new session assert len(os.listdir(f_cache_directory / 'f')) == 4 def test_no_memory(): """""" Test memory with location=None: no memoize """""" accumulator = list() def ff(l): accumulator.append(1) return l memory = Memory(location=None, verbose=0) gg = memory.cache(ff) for _ in range(4): current_accumulator = len(accumulator) gg(1) assert len(accumulator) == current_accumulator + 1 def test_memory_kwarg(tmpdir): "" Test memory with a function with keyword arguments."" accumulator = list() def g(l=None, m=1): accumulator.append(1) return l check_identity_lazy(g, accumulator, tmpdir.strpath) memory = Memory(location=tmpdir.strpath, verbose=0) g = memory.cache(g) # Smoke test with an explicit keyword argument: assert g(l=30, m=2) == 30 def test_memory_lambda(tmpdir): "" Test memory with a function with a lambda."" accumulator = list() def helper(x): """""" A helper function to define l as a lambda. """""" accumulator.append(1) return x l = lambda x: helper(x) check_identity_lazy(l, accumulator, tmpdir.strpath) def test_memory_name_collision(tmpdir): "" Check that name collisions with functions will raise warnings"" memory = Memory(location=tmpdir.strpath, verbose=0) @memory.cache def name_collision(x): """""" A first function calle", https://github.com/joblib/joblib,440b1b899d357a9db6526ca87051a77963b33631,joblib/test/test_memory.py::test_memory_file_modification,joblib/test/test_memory.py,NIO,flaky,Opened,https://github.com/joblib/joblib/pull/1631,classify;root_cause,""""""" Test the memory module. """""" # Author: Gael Varoquaux # Copyright (c) 2009 Gael Varoquaux # License: BSD Style, 3 clauses. import gc import shutil import os import os.path import pathlib import pickle import sys import time import datetime import textwrap import pytest from joblib.memory import Memory from joblib.memory import MemorizedFunc, NotMemorizedFunc from joblib.memory import MemorizedResult, NotMemorizedResult from joblib.memory import _FUNCTION_HASHES from joblib.memory import register_store_backend, _STORE_BACKENDS from joblib.memory import _build_func_identifier, _store_backend_factory from joblib.memory import JobLibCollisionWarning from joblib.parallel import Parallel, delayed from joblib._store_backends import StoreBackendBase, FileSystemStoreBackend from joblib.test.common import with_numpy, np from joblib.test.common import with_multiprocessing from joblib.testing import parametrize, raises, warns from joblib.hashing import hash ############################################################################### # Module-level variables for the tests def f(x, y=1): """""" A module-level function for testing purposes. """""" return x ** 2 + y ############################################################################### # Helper function for the tests def check_identity_lazy(func, accumulator, location): """""" Given a function and an accumulator (a list that grows every time the function is called), check that the function can be decorated by memory to be a lazy identity. """""" # Call each function with several arguments, and check that it is # evaluated only once per argument. memory = Memory(location=location, verbose=0) func = memory.cache(func) for i in range(3): for _ in range(2): assert func(i) == i assert len(accumulator) == i + 1 def corrupt_single_cache_item(memory): single_cache_item, = memory.store_backend.get_items() output_filename = os.path.join(single_cache_item.path, 'output.pkl') with open(output_filename, 'w') as f: f.write('garbage') def monkeypatch_cached_func_warn(func, monkeypatch_fixture): # Need monkeypatch because pytest does not # capture stdlib logging output (see # https://github.com/pytest-dev/pytest/issues/2079) recorded = [] def append_to_record(item): recorded.append(item) monkeypatch_fixture.setattr(func, 'warn', append_to_record) return recorded ############################################################################### # Tests def test_memory_integration(tmpdir): """""" Simple test of memory lazy evaluation. """""" accumulator = list() # Rmk: this function has the same name than a module-level function, # thus it serves as a test to see that both are identified # as different. def f(l): accumulator.append(1) return l check_identity_lazy(f, accumulator, tmpdir.strpath) # Now test clearing for compress in (False, True): for mmap_mode in ('r', None): memory = Memory(location=tmpdir.strpath, verbose=10, mmap_mode=mmap_mode, compress=compress) # First clear the cache directory, to check that our code can # handle that # NOTE: this line would raise an exception, as the database file is # still open; we ignore the error since we want to test what # happens if the directory disappears shutil.rmtree(tmpdir.strpath, ignore_errors=True) g = memory.cache(f) g(1) g.clear(warn=False) current_accumulator = len(accumulator) out = g(1) assert len(accumulator) == current_accumulator + 1 # Also, check that Memory.eval works similarly assert memory.eval(f, 1) == out assert len(accumulator) == current_accumulator + 1 # Now do a smoke test with a function defined in __main__, as the name # mangling rules are more complex f.__module__ = '__main__' memory = Memory(location=tmpdir.strpath, verbose=0) memory.cache(f)(1) @parametrize(""call_before_reducing"", [True, False]) def test_parallel_call_cached_function_defined_in_jupyter( tmpdir, call_before_reducing ): # Calling an interactively defined memory.cache()'d function inside a # Parallel call used to clear the existing cache related to the said # function (https://github.com/joblib/joblib/issues/1035) # This tests checks that this is no longer the case. # TODO: test that the cache related to the function cache persists across # ipython sessions (provided that no code change were made to the # function's source)? # The first part of the test makes the necessary low-level calls to emulate # the definition of a function in an jupyter notebook cell. Joblib has # some custom code to treat functions defined specifically in jupyter # notebooks/ipython session -- we want to test this code, which requires # the emulation to be rigorous. for session_no in [0, 1]: ipython_cell_source = ''' def f(x): return x ''' ipython_cell_id = ''.format(session_no) exec( compile( textwrap.dedent(ipython_cell_source), filename=ipython_cell_id, mode='exec' ) ) # f is now accessible in the locals mapping - but for some unknown # reason, f = locals()['f'] throws a KeyError at runtime, we need to # bind locals()['f'] to a different name in the local namespace aliased_f = locals()['f'] aliased_f.__module__ = ""__main__"" # Preliminary sanity checks, and tests checking that joblib properly # identified f as an interactive function defined in a jupyter notebook assert aliased_f(1) == 1 assert aliased_f.__code__.co_filename == ipython_cell_id memory = Memory(location=tmpdir.strpath, verbose=0) cached_f = memory.cache(aliased_f) assert len(os.listdir(tmpdir / 'joblib')) == 1 f_cache_relative_directory = os.listdir(tmpdir / 'joblib')[0] assert '__ipython-input__' in f_cache_relative_directory f_cache_directory = tmpdir / 'joblib' / f_cache_relative_directory if session_no == 0: # The cache should be empty as cached_f has not been called yet. assert os.listdir(f_cache_directory) == ['f'] assert os.listdir(f_cache_directory / 'f') == [] if call_before_reducing: cached_f(3) # Two files were just created, func_code.py, and a folder # containing the informations (inputs hash/ouptput) of # cached_f(3) assert len(os.listdir(f_cache_directory / 'f')) == 2 # Now, testing #1035: when calling a cached function, joblib # used to dynamically inspect the underlying function to # extract its source code (to verify it matches the source code # of the function as last inspected by joblib) -- however, # source code introspection fails for dynamic functions sent to # child processes - which would eventually make joblib clear # the cache associated to f res = Parallel(n_jobs=2)(delayed(cached_f)(i) for i in [1, 2]) else: # Submit the function to the joblib child processes, although # the function has never been called in the parent yet. This # triggers a specific code branch inside # MemorizedFunc.__reduce__. res = Parallel(n_jobs=2)(delayed(cached_f)(i) for i in [1, 2]) assert len(os.listdir(f_cache_directory / 'f')) == 3 cached_f(3) # Making sure f's cache does not get cleared after the parallel # calls, and contains ALL cached functions calls (f(1), f(2), f(3)) # and 'func_code.py' assert len(os.listdir(f_cache_directory / 'f')) == 4 else: # For the second session, there should be an already existing cache assert len(os.listdir(f_cache_directory / 'f')) == 4 cached_f(3) # The previous cache should not be invalidated after calling the # function in a new session assert len(os.listdir(f_cache_directory / 'f')) == 4 def test_no_memory(): """""" Test memory with location=None: no memoize """""" accumulator = list() def ff(l): accumulator.append(1) return l memory = Memory(location=None, verbose=0) gg = memory.cache(ff) for _ in range(4): current_accumulator = len(accumulator) gg(1) assert len(accumulator) == current_accumulator + 1 def test_memory_kwarg(tmpdir): "" Test memory with a function with keyword arguments."" accumulator = list() def g(l=None, m=1): accumulator.append(1) return l check_identity_lazy(g, accumulator, tmpdir.strpath) memory = Memory(location=tmpdir.strpath, verbose=0) g = memory.cache(g) # Smoke test with an explicit keyword argument: assert g(l=30, m=2) == 30 def test_memory_lambda(tmpdir): "" Test memory with a function with a lambda."" accumulator = list() def helper(x): """""" A helper function to define l as a lambda. """""" accumulator.append(1) return x l = lambda x: helper(x) check_identity_lazy(l, accumulator, tmpdir.strpath) def test_memory_name_collision(tmpdir): "" Check that name collisions with functions will raise warnings"" memory = Memory(location=tmpdir.strpath, verbose=0) @memory.cache def name_collision(x): """""" A first function calle", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_api_named_routes,tests/test_routing.py,NOD,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_before_middleware,tests/test_routing.py,NOD,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_dynamic_params,tests/test_routing.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_handle_pre_flight_request,tests/test_routing.py,NOD,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_ignored_routes,tests/test_routing.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_ignored_route_path,tests/test_routing.py,NOD,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_jwt_route[jwt_router_client0-None-200],tests/test_routing.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_jwt_route[jwt_router_client1-None-401],tests/test_routing.py,NOD,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_jwt_route[jwt_router_client2-None-401],tests/test_routing.py,NOD,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_jwt_route[jwt_router_client3-None-401],tests/test_routing.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_static_client,tests/test_routing.py,NOD,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_static_routes,tests/test_routing.py,NOD,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/joegasewicz/Flask-JWT-Router,0fb1e256c81409be6f08d8be12f07e5f58b4f914,tests/test_routing.py::TestRouting::test_sub_paths,tests/test_routing.py,NOD,flaky,Unmaintained,nan,classify;root_cause,""""""" Unit Tests for: - Routing class: that deal with encoding & decoding tokens & authorising routes. - Entity class: This covers the get_id_from_token method. Extension values used in routing: ""WHITE_LIST_ROUTES"" = [(""PUT"", ""/banana"")] ""IGNORED_ROUTES"" = [(""GET"", ""/"")] ""JWT_ROUTER_API_NAME"" = ""/api/v1"" """""" from flask import Flask from typing import Any import pytest from flask_jwt_router._routing import Routing from flask_jwt_router._extensions import Extensions from flask_jwt_router._entity import Entity from tests.fixtures.token_fixture import mock_token from tests.fixtures.model_fixtures import TestMockEntity from tests.fixtures.app_fixtures import jwt_router_client, test_client_static from tests.fixtures.main_fixture import test_client class MockArgs: def __init__(self, token=None, headers=False): self.token = token self.headers = headers def get(self, t): if self.headers: return f""Bearer {self.token}"" else: return self.token class TestRouting: app = None extensions = { ""WHITE_LIST_ROUTES"": [(""PUT"", ""/banana"")], ""IGNORED_ROUTES"": [(""GET"", ""/"")], ""JWT_ROUTER_API_NAME"": ""/api/v1"", ""SECRET_KEY"": ""TEST_SECRET"", } ext = Extensions().init_extensions(extensions) def test_before_middleware(self, monkeypatch, TestMockEntity, mock_token): app = Flask(__name__) @app.route(""/test"", methods=[""GET""]) def test_one(): return ""/test"" # Manually set the primary key entity = TestMockEntity(id=1, user_name=""joe"") ctx = app.test_request_context(""/test"") ctx.push() assert entity.user_name == ""joe"" assert entity.id == 1 self.ext.entity_models = [TestMockEntity] entity = Entity(self.ext) routing = Routing(app, self.ext, entity) with ctx: # token from args monkeypatch.setattr(""flask.request.args"", MockArgs(mock_token)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] with ctx: # token from headers monkeypatch.setattr(""flask.request.args"", MockArgs()) monkeypatch.setattr(""flask.request.headers"", MockArgs(mock_token, True)) routing.before_middleware() assert ctx.g.test_entities == [(1, 'joe')] @pytest.mark.parametrize( ""jwt_router_client,entity_model,expected"", [ ({""WHITE_LIST_ROUTES"": [(""GET"", ""/test"")]}, None, ""200""), ({""WHITE_LIST_ROUTES"": []}, None, ""401""), ({""WHITE_LIST_ROUTES"": [(""POST"", ""/test"")]}, None, ""401""), ({}, None, ""401""), ], indirect=[""jwt_router_client""] ) def test_jwt_route(self, jwt_router_client, entity_model, expected): rv = jwt_router_client.get(""/test"") assert expected in str(rv.status) def test_api_named_routes(self, test_client): rv = test_client.get(""/api/v1/test"") assert ""200"" in str(rv.status) def test_sub_paths(self, test_client): rv = test_client.get(""/api/v1/bananas/sub"") assert ""200"" in str(rv.status) assert rv.get_json()[""data""] == ""sub"" rv = test_client.get(""/api/v1/test/sub_two"") assert ""401"" in str(rv.status) def test_dynamic_params(self, test_client): rv = test_client.put(""/api/v1/apples/sub/1"") assert ""200"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/"") assert ""404"" in str(rv.status) rv = test_client.get(""/api/v1/apples/sub/hello"") assert ""404"" in str(rv.status) def test_static_routes(self, test_client): """""" Tests if the static path is handled both by default and if the path is past to the static_folder kwarg """""" rv = test_client.get(""/static/images/Group.jpg"") assert ""200"" in str(rv.status) rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_static_client(self, test_client_static): rv = test_client_static.get(""/static_copy/images/Group.jpg"") assert ""200"" in str(rv.status) def test_ignored_routes(self, test_client): rv = test_client.get(""/ignore"") assert ""200"" in str(rv.status) def test_ignored_route_path(self, test_client): rv = test_client.get(""/"") assert ""200"" in str(rv.status) def test_handle_pre_flight_request(self, test_client): rv = test_client.options(""/"") assert ""200"" in str(rv.status) ", https://github.com/JonathanVusich/litedb,2694bc39676d8398f669c0aed96aef89623dbc9a,tests/test_database/test_memory_database_hypothesis.py::TestMemoryDatabase::test_database_retrieve,tests/test_database/test_memory_database_hypothesis.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pytest from hypothesis import given from hypothesis.strategies import integers from litedb.database import MemoryDatabase from ..test_table.table_test_objects import BadAndGoodObject, GoodObject, BadObject, GoodIndex class TestMemoryDatabase(unittest.TestCase): @classmethod def setUpClass(cls) -> None: cls.database = MemoryDatabase() cls.length = 0 cls.class_choices = [BadObject, BadAndGoodObject, GoodObject] @given( value=integers() ) def test_database_insert(self, value): self.database.insert(BadAndGoodObject(value)) self.database.insert(GoodObject(value)) self.database.insert(BadObject(value)) self.length += 1 assert len(self.database._tables[BadAndGoodObject]) == self.length assert len(self.database._tables[GoodObject]) == self.length assert len(self.database._tables[BadObject]) == self.length assert len(self.database) == self.length * 3 @given( value=integers() ) def test_database_retrieve(self, value): class_choice = self.class_choices[value % 3] with pytest.raises(IndexError): self.database.select(class_choice).retrieve(bad_index=value) if class_choice == BadObject: with pytest.raises(IndexError): self.database.select(class_choice).retrieve(good_index=value) elif class_choice == GoodObject: results = self.database.select(class_choice).retrieve(good_index=GoodIndex(value)) if results: for result in results: assert isinstance(result, GoodObject) assert result.good_index == GoodIndex(value) elif class_choice == BadAndGoodObject: results = self.database.select(class_choice).retrieve(good_index=value) if results: for result in results: assert isinstance(result, BadAndGoodObject) assert result.good_index == value @given( value=integers() ) def test_database_zelete(self, value): class_choice = self.class_choices[value % 3] with pytest.raises(IndexError): self.database.select(class_choice).retrieve(bad_index=value) if class_choice == BadObject: with pytest.raises(IndexError): self.database.select(class_choice).delete(good_index=value) elif class_choice == GoodObject: self.database.select(class_choice).delete(good_index=GoodIndex(value)) assert not self.database.select(class_choice).retrieve(good_index=GoodIndex(value)) elif class_choice == BadAndGoodObject: self.database.select(class_choice).delete(good_index=value) assert not self.database.select(class_choice).retrieve(good_index=value) ", https://github.com/josiest/geom,d12b381706ee620cba1c6a6cb45ab7815e5a1dc9,test_geom.py::test_epsilon,test_geom.py,NIO,flaky,Rejected,https://github.com/josiest/geom/pull/1,classify;root_cause,"import geom import pytest import math def test_epsilon(): assert(abs(geom.eps - 0.0001) < 0.0001) geom.set_tolerance(0.01) assert(abs(geom.eps - 0.01) < 0.00001) with pytest.raises(TypeError): geom.set_tolerance('0.001') with pytest.raises(ValueError): geom.set_tolerance(0) with pytest.raises(ValueError): geom.set_tolerance(-1) def test_is_numeric(): assert(geom.is_numeric(3)) assert(not geom.is_numeric('3')) assert(geom.is_numeric((1, 2, 3))) assert(geom.is_numeric([1.0, 2, 3.0012])) assert(not geom.is_numeric(('1', 2, 3.0))) assert(geom.is_numeric({1.0, 2.0, 3.0})) assert(not geom.is_numeric(True)) def test_vecinit(): vc = object.__new__(geom.Vector) vc._components = [1, 2, 3] cases = [(1, 2, 3), [1.0, 2.0, 3.0], {1, 2, 3.0}, range(1,4), vc] expected = [1, 2, 3] for v in cases: assert(geom.Vector(v)._components == expected) with pytest.raises(ValueError): geom.Vector(()) with pytest.raises(TypeError): geom.Vector(('1', 2.0, 3)) with pytest.raises(TypeError): geom.Vector(1) def test_veclen(): cases = ((0,), (0,0,0,0,0)) expected = (1, 5) for v, n in zip(cases, expected): assert(len(geom.Vector(v)) == n) def test_vecgetitem(): I = (0, 1, 2, 19) A = ('x', 'y', 'z') V = [[2*i for i in range(n)] for n in range(1,4)] V.append([3*i for i in range(20)]) expected = {'index': ((0,), (0, 2), (0, 2, 4), (0, 3, 6, 57)), 'attr': ((0,), (0, 2), (0, 2, 4), (0, 3, 6))} for i, components in enumerate(V): v = geom.Vector(components) for j, index in enumerate(I): if j >= len(expected['index'][i]): with pytest.raises(IndexError): v[index] else: assert(v[index] == expected['index'][i][j]) for j, attr in enumerate(A): if j >= len(expected['attr'][i]): with pytest.raises(IndexError): getattr(v, attr) else: assert(getattr(v, attr) == expected['attr'][i][j]) def test_vecmag(): cases = ((0,), (1,), (3, 4), (1.0, 1.0), (0.1, 4.0, 79.0)) expected = (0, 1, 5, 2**0.5, 79.1013) for components, e in zip(cases, expected): v = geom.Vector(components) assert(abs(v)-e < 0.0001) assert(v.mag()-e < 0.0001) assert(v.magSq()-(e**2) < 0.0001) def test_vecnorm(): cases = ((1,), (3, 4), (0.1, 10, 100)) for components in cases: v = geom.Vector(components) n1 = ~v n2 = v.norm() n3 = geom.Vector(components) n3.normalize() for n in (n1, n2, n3): assert n is not None assert(abs(abs(n)-1.0) < 0.0001) m = v.x/n.x assert(m > 0) assert(False not in [abs(m-i/j) < 0.0001 for i, j in zip(v, n)]) with pytest.raises(ValueError): ~geom.Vector((0, 0)) def test_veceq(): A = ((0,), (3.33334,), (-12000, -57.42)) B = ((0,), (3.33333,), (-12000, -57.42)) for a, b in zip(A, B): assert(geom.Vector(a) == geom.Vector(b)) geom.set_tolerance(0.000001) A = ((-10000, 10000), (3.33334,), (2, 3, 4)) B = ((10000, -10000), (3.33333,), (3, 3, 3)) for a, b in zip(A, B): assert(geom.Vector(a) != geom.Vector(b)) def test_vecneg(): cases = ((0.0,), (1,), (-2, -3.0), (34.5, -22, 130)) expected = ((0.0,), (-1,), (2, 3.0), (-34.5, 22, -130)) for cv, ce in zip(cases, expected): v = geom.Vector(cv) e = geom.Vector(ce) assert(e == -v) def test_vecadd(): A = ((0,), (1.34,), (-4, 6), (1900, 2000, 3000)) B = ((0,), (3,), (6, -4), (0.01, 1.01, 0.5)) esum = ((0,), (4.34,), (2, 2), (1900.01, 2001.01, 3000.5)) edifa = ((0,), (-1.66,), (-10, 10), (1899.99, 1998.99, 2999.5)) edifb = ((0,), (1.66,), (10, -10), (-1899.99, -1998.99, -2999.5)) for ca, cb, ces, ceda, cedb in zip(A, B, esum, edifa, edifb): a = geom.Vector(ca) b = geom.Vector(cb) ms1 = geom.Vector(ca) ms1.addOn(cb) ms2 = geom.Vector(cb) ms2.addOn(a) mda1 = geom.Vector(ca) mda2 = geom.Vector(ca) mda1.takeAway(b) mda2.takeAway(cb) mdb1 = geom.Vector(cb) mdb2 = geom.Vector(cb) mdb1.takeAway(a) mdb2.takeAway(ca) es = geom.Vector(ces) eda = geom.Vector(ceda) edb = geom.Vector(cedb) sums = (a+b, b+a, a+cb, ca+b, a.add(b), b.add(ca), ms1, ms2) diffa = (a-b, a + -b, a-cb, ca-b, a.sub(b), a.sub(cb), mda1, mda2) diffb = (b-a, b + -a, b-ca, cb-a, b.sub(a), b.sub(ca), mdb1, mdb2) for s, da, db in zip(sums, diffa, diffb): assert(s == es) assert(da == eda) assert(db == edb) a = geom.Vector((1, 2)) bt = (3, '4') bl = ((3, 4, 5), (), (2,)) tests = ['a + %s', '%s + a', 'a - %s', 'a.add(%s)', 'a.sub(%s)', 'a.addOn(%s)', 'a.takeAway(%s)'] for test in tests: with pytest.raises(TypeError): eval(test % 'bt') for v in bl: with pytest.raises(ValueError): eval(test % 'v') def test_vecmul(): C = ((0,), (133333,), (3, -4.5), (-2.0, -1.5, -1.0)) E = ((0,), (-266666,), (0, 0), (66.6, 49.95, 33.3)) M = (30, -2, 0, -33.3) for c, e, m in zip(C, E, M): v = geom.Vector(c) mv = geom.Vector(c) mv.mulBy(m) assert(m*v == e) assert(v*m == e) assert(v.mul(m) == e) assert(mv == e) E = ((0,), (1.0,), (-6, 9), (-0.5, -0.375, -0.25)) D = (1, 133333, -0.5, 4) for c, e, d in zip(C, E, D): v = geom.Vector(c) dv = geom.Vector(c) dv.divBy(d) assert(v/d == e) assert(v.div(d) == e) assert(dv == e) v = geom.Vector(range(1, 4)) tests = (""v * %s"", ""%s * v"", ""v.add(%s)"", ""v.addOn(%s)"", ""v / %s"", ""v.div(%s)"", ""v.divBy(%s)"") bad = (""'3'"", 'True', 'None', (3, 4)) for bt in bad: for test in tests: with pytest.raises(TypeError): eval(test % bt) tests = (""v / %s"", ""v.div(%s)"", ""v.divBy(%s)"") for test in tests: with pytest.raises(ZeroDivisionError): eval(test % '0') def test_vecdot(): A = ((0,), (1,), (0.003, 0.004), (4123213.0, 12093201, 3298928)) B = ((30,), (0.5,), (-0.008, 0.006), (2, 3.3, 4.01)) expected = (0, 0.5, 0, 61382690.58) for a, b, e in zip(A, B, expected): v = geom.Vector(a); assert(abs((v @ b) - e) < 0.001) assert(abs((b @ v) - e) < 0.001) assert(abs(v.dot(b) - e) < 0.001) A = ((1,), (1, 1), (1, 1, 1)) BT = ((""zero"",), ((1, 2), (3, 4)), (True, 0, ""False"")) tests = (""{0} @ {1}"", ""{1} @ {0}"", ""{0}.dot({1})"") for a, bt in zip(A, BT): av = geom.Vector(a) for test in tests: with pytest.raises(TypeError): eval(test.format('av', 'bt')) A = ((1,), (1, 1, 1)) b = (1, 1) for a in A: av = geom.Vector(a) for test in tests: with pytest.raises(ValueError): eval(test.format('av', 'b')) def test_veccross(): A = ((0, 0, 0), (1.0, 0, 0), (-300000, 20003, -0.020012)) b = (3, 4, 5) geom.set_tolerance(0.001) expected = ((0, 0, 0), (0, -5.0, 4.0), (100015.08005, 1499999.939964, -1260009)) for a, e in zip(A, expected): av = geom.Vector(a); ev = geom.Vector(e); assert(av*b == ev) assert(av.cross(b) == ev) bv = geom.Vector(b) assert(bv*A[1] != A[1]*bv) BT = ((True, True, 1.0), False, (""3.0"", 4.2, 12), ""{3.3, 1.0 5.9}"", ((3, 4), (5, 6), (7, 8))) tests = (""{0} * {1}"", ""{1} * {0}"", ""{0}.cross({1})"") for bt in BT: for test in tests: with pytest.raises(TypeError): eval(test.format('bv', 'bt')) BL = ((1,), (1, 2), (1, 2, 3, 4)) for bl in BL: for test in tests: with pytest.raises(ValueError): eval(test.format('bv', 'bl')) BL2 = ((2,), (3, 4), (5 ,6, 7, 8)) for bl, bl2 in zip(BL, BL2): blv = geom.Vector(bl) for test in tests: with pytest.raises(ValueError): eval(test.format('blv', 'bl2')) def test_circinit(): geom.set_tolerance(0.000001) centers = ((0, 0), (-0.0001, -0.00023), (3002.0002, -4003.2093)) radii = (0, 0.00032, 5.0021) for p, r in zip(centers, radii): c1 = object.__new__(geom.Circle) c1._center = geom.Vector(p) c1._radius = r c2 = geom.Circle(p, r) assert(c1.center == c2.center) assert(abs(c1.radius-c2.radius) < geom.eps) with pytest.raises(TypeError): geom.Circle('{1, 2}', 3) class Test(object): __slots__ = ['components'] def __iter__(self): yield from self.components a = object.__new__(Test) a.components = [1, 2] with pytest.raises(AttributeError): geom.Circle(a, 3.00002) with pytest.raises(ValueError): geom.Circle((1,), 30000) with pytest.raises(ValueError): geom.Circle((1, 1, 1), 0.0001) with pytest.raises(TypeError): geom.Circle((1, 2), '3') with pytest.raises(ValueError): geom.Circle((20, 30), -39) def test_circarea(): geom.set_tolerance(0.000001) centers = ((0, 0), (-1.0, 1.0), (10000, 0.000001), (-7892822, 1902383)) radii = (33.19, 0, 0.02003, 78892) areas = (0, 16*math.pi, 0.000025, 3423423) eas = (3460.7033830094, 0, 0.0012604098, 19553108257.54975484) ers = (0, 4, 0.00282095, 1043.8914625) for center, r, area, ea, er in zip(centers, radii, areas, eas, ers): c = geom.Circle(center, r) assert(abs(c.area-ea) < geom.eps) c.area = area assert(abs(c.radi", https://github.com/josiest/geom,d12b381706ee620cba1c6a6cb45ab7815e5a1dc9,test_geom.py::test_veceq,test_geom.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import geom import pytest import math def test_epsilon(): assert(abs(geom.eps - 0.0001) < 0.0001) geom.set_tolerance(0.01) assert(abs(geom.eps - 0.01) < 0.00001) with pytest.raises(TypeError): geom.set_tolerance('0.001') with pytest.raises(ValueError): geom.set_tolerance(0) with pytest.raises(ValueError): geom.set_tolerance(-1) def test_is_numeric(): assert(geom.is_numeric(3)) assert(not geom.is_numeric('3')) assert(geom.is_numeric((1, 2, 3))) assert(geom.is_numeric([1.0, 2, 3.0012])) assert(not geom.is_numeric(('1', 2, 3.0))) assert(geom.is_numeric({1.0, 2.0, 3.0})) assert(not geom.is_numeric(True)) def test_vecinit(): vc = object.__new__(geom.Vector) vc._components = [1, 2, 3] cases = [(1, 2, 3), [1.0, 2.0, 3.0], {1, 2, 3.0}, range(1,4), vc] expected = [1, 2, 3] for v in cases: assert(geom.Vector(v)._components == expected) with pytest.raises(ValueError): geom.Vector(()) with pytest.raises(TypeError): geom.Vector(('1', 2.0, 3)) with pytest.raises(TypeError): geom.Vector(1) def test_veclen(): cases = ((0,), (0,0,0,0,0)) expected = (1, 5) for v, n in zip(cases, expected): assert(len(geom.Vector(v)) == n) def test_vecgetitem(): I = (0, 1, 2, 19) A = ('x', 'y', 'z') V = [[2*i for i in range(n)] for n in range(1,4)] V.append([3*i for i in range(20)]) expected = {'index': ((0,), (0, 2), (0, 2, 4), (0, 3, 6, 57)), 'attr': ((0,), (0, 2), (0, 2, 4), (0, 3, 6))} for i, components in enumerate(V): v = geom.Vector(components) for j, index in enumerate(I): if j >= len(expected['index'][i]): with pytest.raises(IndexError): v[index] else: assert(v[index] == expected['index'][i][j]) for j, attr in enumerate(A): if j >= len(expected['attr'][i]): with pytest.raises(IndexError): getattr(v, attr) else: assert(getattr(v, attr) == expected['attr'][i][j]) def test_vecmag(): cases = ((0,), (1,), (3, 4), (1.0, 1.0), (0.1, 4.0, 79.0)) expected = (0, 1, 5, 2**0.5, 79.1013) for components, e in zip(cases, expected): v = geom.Vector(components) assert(abs(v)-e < 0.0001) assert(v.mag()-e < 0.0001) assert(v.magSq()-(e**2) < 0.0001) def test_vecnorm(): cases = ((1,), (3, 4), (0.1, 10, 100)) for components in cases: v = geom.Vector(components) n1 = ~v n2 = v.norm() n3 = geom.Vector(components) n3.normalize() for n in (n1, n2, n3): assert n is not None assert(abs(abs(n)-1.0) < 0.0001) m = v.x/n.x assert(m > 0) assert(False not in [abs(m-i/j) < 0.0001 for i, j in zip(v, n)]) with pytest.raises(ValueError): ~geom.Vector((0, 0)) def test_veceq(): A = ((0,), (3.33334,), (-12000, -57.42)) B = ((0,), (3.33333,), (-12000, -57.42)) for a, b in zip(A, B): assert(geom.Vector(a) == geom.Vector(b)) geom.set_tolerance(0.000001) A = ((-10000, 10000), (3.33334,), (2, 3, 4)) B = ((10000, -10000), (3.33333,), (3, 3, 3)) for a, b in zip(A, B): assert(geom.Vector(a) != geom.Vector(b)) def test_vecneg(): cases = ((0.0,), (1,), (-2, -3.0), (34.5, -22, 130)) expected = ((0.0,), (-1,), (2, 3.0), (-34.5, 22, -130)) for cv, ce in zip(cases, expected): v = geom.Vector(cv) e = geom.Vector(ce) assert(e == -v) def test_vecadd(): A = ((0,), (1.34,), (-4, 6), (1900, 2000, 3000)) B = ((0,), (3,), (6, -4), (0.01, 1.01, 0.5)) esum = ((0,), (4.34,), (2, 2), (1900.01, 2001.01, 3000.5)) edifa = ((0,), (-1.66,), (-10, 10), (1899.99, 1998.99, 2999.5)) edifb = ((0,), (1.66,), (10, -10), (-1899.99, -1998.99, -2999.5)) for ca, cb, ces, ceda, cedb in zip(A, B, esum, edifa, edifb): a = geom.Vector(ca) b = geom.Vector(cb) ms1 = geom.Vector(ca) ms1.addOn(cb) ms2 = geom.Vector(cb) ms2.addOn(a) mda1 = geom.Vector(ca) mda2 = geom.Vector(ca) mda1.takeAway(b) mda2.takeAway(cb) mdb1 = geom.Vector(cb) mdb2 = geom.Vector(cb) mdb1.takeAway(a) mdb2.takeAway(ca) es = geom.Vector(ces) eda = geom.Vector(ceda) edb = geom.Vector(cedb) sums = (a+b, b+a, a+cb, ca+b, a.add(b), b.add(ca), ms1, ms2) diffa = (a-b, a + -b, a-cb, ca-b, a.sub(b), a.sub(cb), mda1, mda2) diffb = (b-a, b + -a, b-ca, cb-a, b.sub(a), b.sub(ca), mdb1, mdb2) for s, da, db in zip(sums, diffa, diffb): assert(s == es) assert(da == eda) assert(db == edb) a = geom.Vector((1, 2)) bt = (3, '4') bl = ((3, 4, 5), (), (2,)) tests = ['a + %s', '%s + a', 'a - %s', 'a.add(%s)', 'a.sub(%s)', 'a.addOn(%s)', 'a.takeAway(%s)'] for test in tests: with pytest.raises(TypeError): eval(test % 'bt') for v in bl: with pytest.raises(ValueError): eval(test % 'v') def test_vecmul(): C = ((0,), (133333,), (3, -4.5), (-2.0, -1.5, -1.0)) E = ((0,), (-266666,), (0, 0), (66.6, 49.95, 33.3)) M = (30, -2, 0, -33.3) for c, e, m in zip(C, E, M): v = geom.Vector(c) mv = geom.Vector(c) mv.mulBy(m) assert(m*v == e) assert(v*m == e) assert(v.mul(m) == e) assert(mv == e) E = ((0,), (1.0,), (-6, 9), (-0.5, -0.375, -0.25)) D = (1, 133333, -0.5, 4) for c, e, d in zip(C, E, D): v = geom.Vector(c) dv = geom.Vector(c) dv.divBy(d) assert(v/d == e) assert(v.div(d) == e) assert(dv == e) v = geom.Vector(range(1, 4)) tests = (""v * %s"", ""%s * v"", ""v.add(%s)"", ""v.addOn(%s)"", ""v / %s"", ""v.div(%s)"", ""v.divBy(%s)"") bad = (""'3'"", 'True', 'None', (3, 4)) for bt in bad: for test in tests: with pytest.raises(TypeError): eval(test % bt) tests = (""v / %s"", ""v.div(%s)"", ""v.divBy(%s)"") for test in tests: with pytest.raises(ZeroDivisionError): eval(test % '0') def test_vecdot(): A = ((0,), (1,), (0.003, 0.004), (4123213.0, 12093201, 3298928)) B = ((30,), (0.5,), (-0.008, 0.006), (2, 3.3, 4.01)) expected = (0, 0.5, 0, 61382690.58) for a, b, e in zip(A, B, expected): v = geom.Vector(a); assert(abs((v @ b) - e) < 0.001) assert(abs((b @ v) - e) < 0.001) assert(abs(v.dot(b) - e) < 0.001) A = ((1,), (1, 1), (1, 1, 1)) BT = ((""zero"",), ((1, 2), (3, 4)), (True, 0, ""False"")) tests = (""{0} @ {1}"", ""{1} @ {0}"", ""{0}.dot({1})"") for a, bt in zip(A, BT): av = geom.Vector(a) for test in tests: with pytest.raises(TypeError): eval(test.format('av', 'bt')) A = ((1,), (1, 1, 1)) b = (1, 1) for a in A: av = geom.Vector(a) for test in tests: with pytest.raises(ValueError): eval(test.format('av', 'b')) def test_veccross(): A = ((0, 0, 0), (1.0, 0, 0), (-300000, 20003, -0.020012)) b = (3, 4, 5) geom.set_tolerance(0.001) expected = ((0, 0, 0), (0, -5.0, 4.0), (100015.08005, 1499999.939964, -1260009)) for a, e in zip(A, expected): av = geom.Vector(a); ev = geom.Vector(e); assert(av*b == ev) assert(av.cross(b) == ev) bv = geom.Vector(b) assert(bv*A[1] != A[1]*bv) BT = ((True, True, 1.0), False, (""3.0"", 4.2, 12), ""{3.3, 1.0 5.9}"", ((3, 4), (5, 6), (7, 8))) tests = (""{0} * {1}"", ""{1} * {0}"", ""{0}.cross({1})"") for bt in BT: for test in tests: with pytest.raises(TypeError): eval(test.format('bv', 'bt')) BL = ((1,), (1, 2), (1, 2, 3, 4)) for bl in BL: for test in tests: with pytest.raises(ValueError): eval(test.format('bv', 'bl')) BL2 = ((2,), (3, 4), (5 ,6, 7, 8)) for bl, bl2 in zip(BL, BL2): blv = geom.Vector(bl) for test in tests: with pytest.raises(ValueError): eval(test.format('blv', 'bl2')) def test_circinit(): geom.set_tolerance(0.000001) centers = ((0, 0), (-0.0001, -0.00023), (3002.0002, -4003.2093)) radii = (0, 0.00032, 5.0021) for p, r in zip(centers, radii): c1 = object.__new__(geom.Circle) c1._center = geom.Vector(p) c1._radius = r c2 = geom.Circle(p, r) assert(c1.center == c2.center) assert(abs(c1.radius-c2.radius) < geom.eps) with pytest.raises(TypeError): geom.Circle('{1, 2}', 3) class Test(object): __slots__ = ['components'] def __iter__(self): yield from self.components a = object.__new__(Test) a.components = [1, 2] with pytest.raises(AttributeError): geom.Circle(a, 3.00002) with pytest.raises(ValueError): geom.Circle((1,), 30000) with pytest.raises(ValueError): geom.Circle((1, 1, 1), 0.0001) with pytest.raises(TypeError): geom.Circle((1, 2), '3') with pytest.raises(ValueError): geom.Circle((20, 30), -39) def test_circarea(): geom.set_tolerance(0.000001) centers = ((0, 0), (-1.0, 1.0), (10000, 0.000001), (-7892822, 1902383)) radii = (33.19, 0, 0.02003, 78892) areas = (0, 16*math.pi, 0.000025, 3423423) eas = (3460.7033830094, 0, 0.0012604098, 19553108257.54975484) ers = (0, 4, 0.00282095, 1043.8914625) for center, r, area, ea, er in zip(centers, radii, areas, eas, ers): c = geom.Circle(center, r) assert(abs(c.area-ea) < geom.eps) c.area = area assert(abs(c.radi", https://github.com/jounderwood/django-hug,778b16f568ce9fdb01f360b7bff89c9d8b6c2ef7,tests/test_api.py::test_simple_post_ok,tests/test_api.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json import pytest from django.http import HttpResponse from pydantic import PositiveFloat import djhug from djhug.arguments import Body def test_simple_get_ok(client, with_urlpatterns, routes: djhug.Routes): @routes.get(""//"") def view(request, year: int, name: str, q1: float = 0, q2: str = ""firefire""): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.get(""/123/alarm/?q1=23.2000&wat=meh"") assert resp.status_code == 200, resp.content assert json.loads(resp.content) == {""year"": 123, ""name"": ""alarm"", ""q1"": 23.2, ""q2"": ""firefire""} def test_simple_get_regex_ok(client, with_urlpatterns, routes: djhug.Routes): @routes.get(""(?P[0-9]{4})/"", re=True) def view(request, year: int, name: str): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.get(""/000/"") assert resp.status_code == 404, resp.content resp: HttpResponse = client.get(""/1911/?name=ouch"") assert resp.status_code == 200, resp.content assert json.loads(resp.content) == {""year"": 1911, ""name"": ""ouch""} def test_simple_post_ok(client, with_urlpatterns, routes: djhug.Routes): @routes.post(""/"") def view(request, name: str, product_id: int, quantity: int, q: str = None): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.post(""/purchase/?q=param"", data={""product_id"": 999, ""quantity"": 20}) assert resp.status_code == 201, resp.content assert json.loads(resp.content) == {""name"": ""purchase"", ""q"": ""param"", ""product_id"": 999, ""quantity"": 20} def test_whole_body_post_ok(client, with_urlpatterns, routes: djhug.Routes): class RespModel(Body): product_id: int quantity: int @routes.post(""/"") def view(request, name: str, body: RespModel): return {""name"": name, ""body"": body.dict()} with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.post(""/purchase/"", data={""product_id"": 123}) assert resp.status_code == 400, resp.content resp: HttpResponse = client.post(""/purchase/"", data={""product_id"": 123, ""quantity"": 3}) assert resp.status_code == 201, resp.content assert json.loads(resp.content) == {""name"": ""purchase"", ""body"": {""product_id"": 123, ""quantity"": 3}} def test_custom_headers(client, with_urlpatterns, routes: djhug.Routes): @djhug.response.add_headers({""X-Accel-Expires"": 20, ""content-type"": ""text/html""}) @routes.get(""test/"") def view(request, name: str = None): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.get(""/test/"", data={""name"": ""gizmo""}) assert resp.status_code == 200, resp.content assert json.loads(resp.content) == {""name"": ""gizmo""} assert resp[""Content-Type""] == ""text/html"" assert resp[""X-Accel-Expires""] == ""20"" def test_simple_validation_errors_ok(client, with_urlpatterns, routes: djhug.Routes): @routes.get(""test/"") def view(request, year: int, month: int = 1, day: int = None): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.get(""/test/?month=unknown&day=1"") assert resp.status_code == 400, resp.content assert json.loads(resp.content) == { ""errors"": { ""month"": [{""loc"": [""__root__""], ""msg"": ""value is not a valid integer"", ""type"": ""type_error.integer""}], ""year"": [{""loc"": [""year""], ""msg"": ""field required"", ""type"": ""value_error.missing""}], } } def test_with_body_model_validation_errors_ok(client, with_urlpatterns, routes: djhug.Routes): class RespBody(Body): id: int quantity: int @routes.post(""test/"") def view(request, body: RespBody, q1: int, q2: int, q3: PositiveFloat): return {} with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.post(""/test/?q2=1&q3=aaa"", data={""id"": 123}) assert resp.status_code == 400, resp.content assert json.loads(resp.content) == { ""errors"": { ""body"": [{""loc"": [""quantity""], ""msg"": ""field required"", ""type"": ""value_error.missing""}], ""q1"": [{""loc"": [""q1""], ""msg"": ""field required"", ""type"": ""value_error.missing""}], ""q3"": [{""loc"": [""__root__""], ""msg"": ""value is not a valid float"", ""type"": ""type_error.float""}], } } @pytest.mark.parametrize(""path"", (""test/"", ""/test/"")) @pytest.mark.parametrize(""prefix"", (""api"", ""/api"", ""api/"", ""/api/"")) def test_routes_prefix(client, with_urlpatterns, prefix, path): routes = djhug.Routes(prefix=prefix) @routes.get(path) def view(request, name: str = None): return {} with_urlpatterns(routes.get_urlpatterns()) assert client.get(""/api/test/"").status_code == 200 def test_no_annotation(client, with_urlpatterns, routes: djhug.Routes): @routes.get(""test/"") def view(request, year, day: int = None): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.get(""/test/?day=1"") assert resp.status_code == 400, resp.content assert json.loads(resp.content) == { ""errors"": {""year"": [{""loc"": [""year""], ""msg"": ""field required"", ""type"": ""value_error.missing""}]} } resp: HttpResponse = client.get(""/test/?day=1&year=111"") assert resp.status_code == 200, resp.content assert json.loads(resp.content) == {""day"": 1, ""year"": ""111""} def test_args_kwargs_ok(client, with_urlpatterns, routes: djhug.Routes): @routes.get(""test/"") def view(request, name, *args, **kwargs): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.get(""/test/?name=John"") assert resp.status_code == 200, resp.content assert json.loads(resp.content) == {""name"": ""John"", ""args"": [], ""kwargs"": {}} ", https://github.com/jounderwood/django-hug,778b16f568ce9fdb01f360b7bff89c9d8b6c2ef7,tests/test_api.py::test_whole_body_post_ok,tests/test_api.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json import pytest from django.http import HttpResponse from pydantic import PositiveFloat import djhug from djhug.arguments import Body def test_simple_get_ok(client, with_urlpatterns, routes: djhug.Routes): @routes.get(""//"") def view(request, year: int, name: str, q1: float = 0, q2: str = ""firefire""): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.get(""/123/alarm/?q1=23.2000&wat=meh"") assert resp.status_code == 200, resp.content assert json.loads(resp.content) == {""year"": 123, ""name"": ""alarm"", ""q1"": 23.2, ""q2"": ""firefire""} def test_simple_get_regex_ok(client, with_urlpatterns, routes: djhug.Routes): @routes.get(""(?P[0-9]{4})/"", re=True) def view(request, year: int, name: str): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.get(""/000/"") assert resp.status_code == 404, resp.content resp: HttpResponse = client.get(""/1911/?name=ouch"") assert resp.status_code == 200, resp.content assert json.loads(resp.content) == {""year"": 1911, ""name"": ""ouch""} def test_simple_post_ok(client, with_urlpatterns, routes: djhug.Routes): @routes.post(""/"") def view(request, name: str, product_id: int, quantity: int, q: str = None): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.post(""/purchase/?q=param"", data={""product_id"": 999, ""quantity"": 20}) assert resp.status_code == 201, resp.content assert json.loads(resp.content) == {""name"": ""purchase"", ""q"": ""param"", ""product_id"": 999, ""quantity"": 20} def test_whole_body_post_ok(client, with_urlpatterns, routes: djhug.Routes): class RespModel(Body): product_id: int quantity: int @routes.post(""/"") def view(request, name: str, body: RespModel): return {""name"": name, ""body"": body.dict()} with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.post(""/purchase/"", data={""product_id"": 123}) assert resp.status_code == 400, resp.content resp: HttpResponse = client.post(""/purchase/"", data={""product_id"": 123, ""quantity"": 3}) assert resp.status_code == 201, resp.content assert json.loads(resp.content) == {""name"": ""purchase"", ""body"": {""product_id"": 123, ""quantity"": 3}} def test_custom_headers(client, with_urlpatterns, routes: djhug.Routes): @djhug.response.add_headers({""X-Accel-Expires"": 20, ""content-type"": ""text/html""}) @routes.get(""test/"") def view(request, name: str = None): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.get(""/test/"", data={""name"": ""gizmo""}) assert resp.status_code == 200, resp.content assert json.loads(resp.content) == {""name"": ""gizmo""} assert resp[""Content-Type""] == ""text/html"" assert resp[""X-Accel-Expires""] == ""20"" def test_simple_validation_errors_ok(client, with_urlpatterns, routes: djhug.Routes): @routes.get(""test/"") def view(request, year: int, month: int = 1, day: int = None): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.get(""/test/?month=unknown&day=1"") assert resp.status_code == 400, resp.content assert json.loads(resp.content) == { ""errors"": { ""month"": [{""loc"": [""__root__""], ""msg"": ""value is not a valid integer"", ""type"": ""type_error.integer""}], ""year"": [{""loc"": [""year""], ""msg"": ""field required"", ""type"": ""value_error.missing""}], } } def test_with_body_model_validation_errors_ok(client, with_urlpatterns, routes: djhug.Routes): class RespBody(Body): id: int quantity: int @routes.post(""test/"") def view(request, body: RespBody, q1: int, q2: int, q3: PositiveFloat): return {} with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.post(""/test/?q2=1&q3=aaa"", data={""id"": 123}) assert resp.status_code == 400, resp.content assert json.loads(resp.content) == { ""errors"": { ""body"": [{""loc"": [""quantity""], ""msg"": ""field required"", ""type"": ""value_error.missing""}], ""q1"": [{""loc"": [""q1""], ""msg"": ""field required"", ""type"": ""value_error.missing""}], ""q3"": [{""loc"": [""__root__""], ""msg"": ""value is not a valid float"", ""type"": ""type_error.float""}], } } @pytest.mark.parametrize(""path"", (""test/"", ""/test/"")) @pytest.mark.parametrize(""prefix"", (""api"", ""/api"", ""api/"", ""/api/"")) def test_routes_prefix(client, with_urlpatterns, prefix, path): routes = djhug.Routes(prefix=prefix) @routes.get(path) def view(request, name: str = None): return {} with_urlpatterns(routes.get_urlpatterns()) assert client.get(""/api/test/"").status_code == 200 def test_no_annotation(client, with_urlpatterns, routes: djhug.Routes): @routes.get(""test/"") def view(request, year, day: int = None): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.get(""/test/?day=1"") assert resp.status_code == 400, resp.content assert json.loads(resp.content) == { ""errors"": {""year"": [{""loc"": [""year""], ""msg"": ""field required"", ""type"": ""value_error.missing""}]} } resp: HttpResponse = client.get(""/test/?day=1&year=111"") assert resp.status_code == 200, resp.content assert json.loads(resp.content) == {""day"": 1, ""year"": ""111""} def test_args_kwargs_ok(client, with_urlpatterns, routes: djhug.Routes): @routes.get(""test/"") def view(request, name, *args, **kwargs): loc = locals() del loc[""request""] return loc with_urlpatterns(list(routes.get_urlpatterns())) resp: HttpResponse = client.get(""/test/?name=John"") assert resp.status_code == 200, resp.content assert json.loads(resp.content) == {""name"": ""John"", ""args"": [], ""kwargs"": {}} ", https://github.com/jounderwood/django-hug,778b16f568ce9fdb01f360b7bff89c9d8b6c2ef7,tests/test_settings.py::test_camelcased_response_data_ok,tests/test_settings.py,NIO,flaky,Opened,https://github.com/jounderwood/django-hug/pull/2,classify;root_cause,"from django.test import override_settings from djhug import request_parser, response_renderer from djhug.content_negotiation import get_request_parsers, get_response_renderers from djhug.settings import Settings @request_parser(""application/x-test"") def request_formatter_test(request): return request.POST @response_renderer(""application/x-test"") def response_formatter_test(data): return data def test_register_request_formatters_ok(): assert Settings().request_parsers_modules is None with override_settings(DJHUG_REQUEST_PARSERS_MODULES=[""tests.test_settings""]): assert Settings().request_parsers_modules == [""tests.test_settings""] assert ""application/x-test"" in get_request_parsers() assert get_request_parsers()[""application/x-test""] == request_formatter_test def test_register_response_formatters_ok(): assert Settings().response_renderers_modules is None with override_settings(DJHUG_RESPONSE_RENDERERS_MODULES=[""tests.test_settings""]): assert Settings().response_renderers_modules == [""tests.test_settings""] assert ""application/x-test"" in get_response_renderers() assert get_response_renderers()[""application/x-test""] == response_formatter_test def test_response_additional_headers_ok(): assert Settings().response_additional_headers == {} with override_settings(DJHUG_RESPONSE_ADDITIONAL_HEADERS={""Access-Control-Allow-Origin"": ""*""}): assert Settings().response_additional_headers == {""Access-Control-Allow-Origin"": ""*""} def test_camelcased_response_data_ok(): assert not Settings().camelcased_response_data with override_settings(DJHUG_CAMELCASED_RESPONSE_DATA=True): assert Settings().camelcased_response_data ", https://github.com/jounderwood/django-hug,778b16f568ce9fdb01f360b7bff89c9d8b6c2ef7,tests/test_settings.py::test_register_request_formatters_ok,tests/test_settings.py,NIO,flaky,Opened,https://github.com/jounderwood/django-hug/pull/2,classify;root_cause,"from django.test import override_settings from djhug import request_parser, response_renderer from djhug.content_negotiation import get_request_parsers, get_response_renderers from djhug.settings import Settings @request_parser(""application/x-test"") def request_formatter_test(request): return request.POST @response_renderer(""application/x-test"") def response_formatter_test(data): return data def test_register_request_formatters_ok(): assert Settings().request_parsers_modules is None with override_settings(DJHUG_REQUEST_PARSERS_MODULES=[""tests.test_settings""]): assert Settings().request_parsers_modules == [""tests.test_settings""] assert ""application/x-test"" in get_request_parsers() assert get_request_parsers()[""application/x-test""] == request_formatter_test def test_register_response_formatters_ok(): assert Settings().response_renderers_modules is None with override_settings(DJHUG_RESPONSE_RENDERERS_MODULES=[""tests.test_settings""]): assert Settings().response_renderers_modules == [""tests.test_settings""] assert ""application/x-test"" in get_response_renderers() assert get_response_renderers()[""application/x-test""] == response_formatter_test def test_response_additional_headers_ok(): assert Settings().response_additional_headers == {} with override_settings(DJHUG_RESPONSE_ADDITIONAL_HEADERS={""Access-Control-Allow-Origin"": ""*""}): assert Settings().response_additional_headers == {""Access-Control-Allow-Origin"": ""*""} def test_camelcased_response_data_ok(): assert not Settings().camelcased_response_data with override_settings(DJHUG_CAMELCASED_RESPONSE_DATA=True): assert Settings().camelcased_response_data ", https://github.com/jounderwood/django-hug,778b16f568ce9fdb01f360b7bff89c9d8b6c2ef7,tests/test_settings.py::test_register_response_formatters_ok,tests/test_settings.py,NIO,flaky,Opened,https://github.com/jounderwood/django-hug/pull/2,classify;root_cause,"from django.test import override_settings from djhug import request_parser, response_renderer from djhug.content_negotiation import get_request_parsers, get_response_renderers from djhug.settings import Settings @request_parser(""application/x-test"") def request_formatter_test(request): return request.POST @response_renderer(""application/x-test"") def response_formatter_test(data): return data def test_register_request_formatters_ok(): assert Settings().request_parsers_modules is None with override_settings(DJHUG_REQUEST_PARSERS_MODULES=[""tests.test_settings""]): assert Settings().request_parsers_modules == [""tests.test_settings""] assert ""application/x-test"" in get_request_parsers() assert get_request_parsers()[""application/x-test""] == request_formatter_test def test_register_response_formatters_ok(): assert Settings().response_renderers_modules is None with override_settings(DJHUG_RESPONSE_RENDERERS_MODULES=[""tests.test_settings""]): assert Settings().response_renderers_modules == [""tests.test_settings""] assert ""application/x-test"" in get_response_renderers() assert get_response_renderers()[""application/x-test""] == response_formatter_test def test_response_additional_headers_ok(): assert Settings().response_additional_headers == {} with override_settings(DJHUG_RESPONSE_ADDITIONAL_HEADERS={""Access-Control-Allow-Origin"": ""*""}): assert Settings().response_additional_headers == {""Access-Control-Allow-Origin"": ""*""} def test_camelcased_response_data_ok(): assert not Settings().camelcased_response_data with override_settings(DJHUG_CAMELCASED_RESPONSE_DATA=True): assert Settings().camelcased_response_data ", https://github.com/jounderwood/django-hug,778b16f568ce9fdb01f360b7bff89c9d8b6c2ef7,tests/test_settings.py::test_response_additional_headers_ok,tests/test_settings.py,NIO,flaky,Opened,https://github.com/jounderwood/django-hug/pull/2,classify;root_cause,"from django.test import override_settings from djhug import request_parser, response_renderer from djhug.content_negotiation import get_request_parsers, get_response_renderers from djhug.settings import Settings @request_parser(""application/x-test"") def request_formatter_test(request): return request.POST @response_renderer(""application/x-test"") def response_formatter_test(data): return data def test_register_request_formatters_ok(): assert Settings().request_parsers_modules is None with override_settings(DJHUG_REQUEST_PARSERS_MODULES=[""tests.test_settings""]): assert Settings().request_parsers_modules == [""tests.test_settings""] assert ""application/x-test"" in get_request_parsers() assert get_request_parsers()[""application/x-test""] == request_formatter_test def test_register_response_formatters_ok(): assert Settings().response_renderers_modules is None with override_settings(DJHUG_RESPONSE_RENDERERS_MODULES=[""tests.test_settings""]): assert Settings().response_renderers_modules == [""tests.test_settings""] assert ""application/x-test"" in get_response_renderers() assert get_response_renderers()[""application/x-test""] == response_formatter_test def test_response_additional_headers_ok(): assert Settings().response_additional_headers == {} with override_settings(DJHUG_RESPONSE_ADDITIONAL_HEADERS={""Access-Control-Allow-Origin"": ""*""}): assert Settings().response_additional_headers == {""Access-Control-Allow-Origin"": ""*""} def test_camelcased_response_data_ok(): assert not Settings().camelcased_response_data with override_settings(DJHUG_CAMELCASED_RESPONSE_DATA=True): assert Settings().camelcased_response_data ", https://github.com/jpoullet2000/atlasclient,02b3e7d9a6596d1123fc22b6bfed08d60105d19e,tests/test_models.py::TestDiscoveryREST::test_search_attribute_get,tests/test_models.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from pytest_mock import mocker from pkg_resources import resource_filename import json import pytest from atlasclient import client from atlasclient import exceptions GUID = '8bbea92b-d98c-4613-ae6e-1a9d0b4f344b' RESPONSE_JSON_DIR = 'response_json' QUERY_JSON_DIR = 'query_json' @pytest.fixture(scope='class') def entity_post_response(): with open('{}/entity_post.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def entity_bulk_response(): with open('{}/entitybulk_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def entity_bulk_classification_response(): with open('{}/entitybulkclassification_post.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def entity_guid_response(): with open('{}/entityguid_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def entity_guid_delete_response(): with open('{}/entityguid_delete.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='function') def entity_guid(atlas_client): entity_guid = atlas_client.entity_guid(GUID) return(entity_guid) @pytest.fixture(scope='class') def entity_guid_classification_response(): with open('{}/entityguid_classification_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def entity_guid_classifications_post(): with open('{}/entityguid_classifications_post.json'.format(resource_filename(__name__, QUERY_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def typedefs_response(): with open('{}/typedefs_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def typedefs_headers_response(): with open('{}/typedefs_headers_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def relationshipdef_guid_response(): with open('{}/relationshipdef_guid_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def lineage_guid_response(): with open('{}/lineage_guid_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def relationship_guid_response(): with open('{}/relationship_guid_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def search_attribute_response(): with open('{}/search_attribute_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def search_saved_response(): with open('{}/search_saved_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def search_saved_name_response(): with open('{}/search_saved_name_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def search_saved_guid_response(): with open('{}/search_saved_guid_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def search_saved_update(): with open('{}/search_saved_update.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def admin_metrics_response(): with open('{}/admin_metrics.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return response class TestEntityREST(): def test_entity_post(self, mocker, atlas_client, entity_guid_response, entity_post_response): mocker.patch.object(atlas_client.client, 'get') atlas_client.client.get.return_value = entity_guid_response mocker.patch.object(atlas_client.entity_post.client, 'post') atlas_client.entity_post.client.post.return_value = entity_post_response atlas_client.entity_post.create(data=entity_guid_response) atlas_client.entity_post.client.post.assert_called_with(atlas_client.entity_post.url, data=entity_guid_response) def test_entity_bulk_get(self, mocker, atlas_client, entity_bulk_response): mocker.patch.object(atlas_client.client, 'get') atlas_client.entity_bulk.client.get.return_value = entity_bulk_response params = {'guid': [GUID, '92b3a92b-d98c-4613-ae6e-1a9d0b4f344b']} bulk_collection = atlas_client.entity_bulk(**params) for bulk in bulk_collection: atlas_client.entity_bulk.client.get.assert_called_with(bulk_collection.url, params=params) for entity in bulk.entities: assert entity.version == 12345 def test_entity_bulk_get_with_relationships(self, mocker, atlas_client, entity_bulk_response): mocker.patch.object(atlas_client.client, 'get') atlas_client.entity_bulk.client.get.return_value = entity_bulk_response params = {'guid': [GUID, '92b3a92b-d98c-4613-ae6e-1a9d0b4f344b']} bulk_collection = atlas_client.entity_bulk(**params) for bulk in bulk_collection: atlas_client.entity_bulk.client.get.assert_called_with(bulk_collection.url, params=params) for entity in bulk.entities_with_relationships(): assert entity.version == 12345 def test_entity_bulk_delete(self, mocker, atlas_client, entity_bulk_response): mocker.patch.object(atlas_client.client, 'get') atlas_client.entity_bulk.client.get.return_value = entity_bulk_response mocker.patch.object(atlas_client.client, 'delete') atlas_client.entity_bulk.client.delete.return_value = entity_bulk_response params = {'guid': [GUID, '92b3a92b-d98c-4613-ae6e-1a9d0b4f344b']} bulk_collection = atlas_client.entity_bulk(**params) for bulk in bulk_collection: bulk.delete(**params) atlas_client.entity_bulk.client.delete.assert_called_with(bulk_collection.url, params=params) def test_entity_bulk_create(self, mocker, atlas_client, entity_bulk_response): mocker.patch.object(atlas_client.client, 'get') atlas_client.entity_bulk.client.get.return_value = entity_bulk_response mocker.patch.object(atlas_client.client, 'post') atlas_client.entity_bulk.client.post.return_value = entity_bulk_response params = {'guid': [GUID, '92b3a92b-d98c-4613-ae6e-1a9d0b4f344b']} bulk_collection = atlas_client.entity_bulk(**params) for bulk in bulk_collection: bulk.create() atlas_client.entity_bulk.client.post.assert_called_with(bulk_collection.url, data=bulk._data) def test_entity_bulk_classification_create(self, mocker, atlas_client, entity_bulk_classification_response): mocker.patch.object(atlas_client.entity_bulk_classification.client, 'post') atlas_client.entity_bulk_classification.client.post.return_value = entity_bulk_classification_response params = {'guid': [GUID, '92b3a92b-d98c-4613-ae6e-1a9d0b4f344b']} atlas_client.entity_bulk_classification.create(data=entity_bulk_classification_response) atlas_client.entity_bulk_classification.client.post.assert_called_with(atlas_client.entity_bulk_classification.url, data=entity_bulk_classification_response) def test_get_entity_by_guid(self, mocker, entity_guid_response, entity_guid): mocker.patch.object(entity_guid.client.client, 'request') entity_guid.client.client.request.return_value = entity_guid_response assert '/entity/guid/{}'.format(GUID) in entity_guid._href # Loading the data because before that no data were actually loaded (lazy loading) entity_guid.entity assert entity_guid._data['entity']['guid'] == GUID entity_guid.entity['status'] = 'ACTIVE' assert entity_guid._data['entity']['status'] == 'ACTIVE' def test_update_entity_by_guid(self, mocker, entity_guid_response, entity_guid): mocker.patch.object(entity_guid.client, 'request') entity_guid.client.request.return_value = entity_guid_response mocker.patch.object(entity_guid.client, 'put') attribute = 'description' entity_guid.update(attribute=attribute) entity_guid.client.put.assert_called_with(entity_guid._href + '?name={}'.format(attribute), data=entity_guid.entity['attributes'][attribute]) def test_create_entity_by_guid(self, mocker, entity_guid_response, entity_guid): mocker.patch.object(entity_guid.client, 'post') entity_guid.create() entity_guid.client.post.assert_called_with(entit", https://github.com/june3474/skelpy,0e64eccdc5211dbec664e7d3f9d07c6020651b63,tests/test_helpers.py::test_remove_comment_line_in_file,tests/test_helpers.py,NIO,flaky,Opened,https://github.com/june3474/skelpy/pull/4,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""test_utils - pytest module for tools in helpers.py """""" from __future__ import absolute_import, print_function import os import sys from tempfile import gettempdir import pytest import skelpy.utils.helpers as helpers from . import mock test_data = (""#!/usr/bin/env python - shebang will remain.\n"" ""# -*- coding: utf-8 -*-\n"" ""# This line will go.\n"" ""\t! And this line too. but not the empty line below.\n"" ""\n"" ""## this line will survive. make sure that there's a empty line above.\n"" ""!! this line will too.\n"" ""[metadata] # keep this normal line and this comment, too.\n"" ""name = ${project} # comments after special tokens are OK as well.\n"" ""\n"" ""classifiers=\n"" "" Environment :: Console # _indent will be kept\n"" "" ## Environment :: Win32 (MS Windows) # what about this?\n"") expected = (""#!/usr/bin/env python - shebang will remain.\n"" ""# -*- coding: utf-8 -*-\n"" ""\n"" ""# this line will survive. make sure that there's a empty line above.\n"" ""! this line will too.\n"" ""[metadata] # keep this normal line and this comment, too.\n"" ""name = ${project} # comments after special tokens are OK as well.\n"" ""\n"" ""classifiers=\n"" "" Environment :: Console # _indent will be kept\n"" "" # Environment :: Win32 (MS Windows) # what about this?\n"") @pytest.fixture(scope='module') def test_file(): testFile = os.path.join(gettempdir(), 'test_file') with open(testFile, 'wt') as f: f.write(test_data) yield testFile os.remove(testFile) def test_get_gitConfig_with_gitconfig(): git_data = '[user]\n\tname = june3474\n\temail = june3474@email\n[core]\n\tautocrlf = true\n' with mock.patch.object(helpers, 'open', mock.mock_open(read_data=git_data), create=True): gitConfig = helpers.get_gitConfig() assert gitConfig.__class__.__name__ == 'ConfigParser' assert gitConfig.get('user', 'name') == 'june3474' assert gitConfig.get('user', 'email') == 'june3474@email' @mock.patch('os.path.join', return_value='NOTEXIST') def test_get_gitConfig_without_gitconfig(mocked_join): gitConfig = helpers.get_gitConfig() assert gitConfig is None def test_get_userName(): assert helpers.get_userName() not in ('', None) def test_get_email(): assert helpers.get_email() not in ('', None) def test_remove_comment_line_in_str(): assert helpers.remove_comment_lines_in_str(test_data) == expected def test_remove_comment_line_in_file(test_file): destFile = os.path.join(gettempdir(), 'destFile') helpers.remove_comment_lines_in_file(test_file, destFile) with open(test_file, 'r') as f: data = f.read() assert data == test_data with open(destFile, 'r') as f: data = f.read() assert data == expected # overwrite helpers.remove_comment_lines_in_file(test_file) with open(test_file, 'r') as f: data = f.read() assert data == expected os.remove(destFile) def test_read_setup_cfg(): # try to read an not-existing file assert helpers.read_setup_cfg('invalid.cfg') == {} cfgFile = os.path.join(os.path.dirname(__file__), '..', 'setup.cfg') conf_dict = helpers.read_setup_cfg(cfgFile) assert conf_dict.get('name') == 'skelpy' assert conf_dict.get('license') == 'MIT' def test_is_rootDir(): cwd = os.getcwd() assert helpers.is_rootDir('') is False assert helpers.is_rootDir('dks') is False if sys.platform == 'win32': assert helpers.is_rootDir('c:\\') is True assert helpers.is_rootDir('c:\\dks') is False os.chdir('C:\\Windows') assert helpers.is_rootDir(os.getcwd()) is False os.chdir('C:\\') assert helpers.is_rootDir(os.getcwd()) is True os.chdir(cwd) else: assert helpers.is_rootDir('/') is True assert helpers.is_rootDir('//') is True assert helpers.is_rootDir('dks') is False os.chdir('/tmp') assert helpers.is_rootDir(os.getcwd()) is False os.chdir('/') assert helpers.is_rootDir(os.getcwd()) is True os.chdir(cwd) ", https://github.com/june3474/skelpy,0e64eccdc5211dbec664e7d3f9d07c6020651b63,tests/test_package.py::test_create_package_dir,tests/test_package.py,NIO,flaky,Accepted,https://github.com/june3474/skelpy/pull/2,classify;root_cause;fix_proposal,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""test_package - pytest module for PackageMaker """""" from __future__ import absolute_import, print_function import os import pytest from tempfile import gettempdir, tempdir from skelpy.makers import base, package, settings from . import mock @pytest.fixture(scope='module') def maker1(): settings.clear() info = { 'projectDir': gettempdir(), 'projectName': 'project', 'format': 'basic', 'merge': False, 'force': False, } return package.Maker(**info) @pytest.fixture(scope='module') def maker2(): settings.clear() info = { 'projectDir': gettempdir(), 'projectName': 'project', 'format': 'src', 'merge': False, 'force': False, } return package.Maker(**info) def test_init(maker1, maker2): assert maker1.packageDir == os.path.join(gettempdir(), maker1.projectName) assert maker2.packageDir == os.path.join(gettempdir(), 'src', maker2.projectName) def test_udpate_settings(maker1): maker1._update_settings() assert settings.get('packageDir') == os.path.join(maker1.projectDir, maker1.projectName) @mock.patch('os.makedirs') @mock.patch('os.mkdir') def test_create_package_dir(mocked_mkdir, mocked_makedirs, maker1, maker2): with mock.patch('os.path.exists', return_value=True): # merge == False && format == 'basic' assert maker1._create_package_dir() == 0 mocked_mkdir.assert_not_called() mocked_makedirs.assert_not_called() # merge == True && format == 'basic' maker1.merge = True assert maker1._create_package_dir() == -1 mocked_mkdir.assert_not_called() mocked_makedirs.assert_not_called() with mock.patch('os.path.exists', return_value=False): # merge == False && format == 'src' assert maker2._create_package_dir() == 1 mocked_mkdir.assert_not_called() assert mocked_makedirs.called # merge == True && format == 'src' maker2.merge = True assert maker2._create_package_dir() == 1 mocked_mkdir.assert_not_called() assert mocked_makedirs.called @mock.patch('os.fsync') def test_write_init(mocked_fsync, maker1): with mock.patch.object(base, 'open', mock.mock_open(), create=True) as mocked_open: with mock.patch('os.path.exists', return_value=True): # exist && force == False assert not maker1._write_init() mocked_open().write.assert_not_called() # exist && force == True maker1.force = True assert maker1._write_init() assert mocked_open().write.called with mock.patch('os.path.exists', return_value=False): # not exist assert maker1._write_init() content = ('# -*- coding: utf-8 -*-' '\n' '\n' ""__version__ = '${version}'"" '\n') mocked_open().write.assert_called_with(content) ", https://github.com/june3474/skelpy,0e64eccdc5211dbec664e7d3f9d07c6020651b63,tests/test_package.py::test_write_init,tests/test_package.py,NIO,flaky,Accepted,https://github.com/june3474/skelpy/pull/3,classify;root_cause;fix_proposal,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""test_package - pytest module for PackageMaker """""" from __future__ import absolute_import, print_function import os import pytest from tempfile import gettempdir, tempdir from skelpy.makers import base, package, settings from . import mock @pytest.fixture(scope='module') def maker1(): settings.clear() info = { 'projectDir': gettempdir(), 'projectName': 'project', 'format': 'basic', 'merge': False, 'force': False, } return package.Maker(**info) @pytest.fixture(scope='module') def maker2(): settings.clear() info = { 'projectDir': gettempdir(), 'projectName': 'project', 'format': 'src', 'merge': False, 'force': False, } return package.Maker(**info) def test_init(maker1, maker2): assert maker1.packageDir == os.path.join(gettempdir(), maker1.projectName) assert maker2.packageDir == os.path.join(gettempdir(), 'src', maker2.projectName) def test_udpate_settings(maker1): maker1._update_settings() assert settings.get('packageDir') == os.path.join(maker1.projectDir, maker1.projectName) @mock.patch('os.makedirs') @mock.patch('os.mkdir') def test_create_package_dir(mocked_mkdir, mocked_makedirs, maker1, maker2): with mock.patch('os.path.exists', return_value=True): # merge == False && format == 'basic' assert maker1._create_package_dir() == 0 mocked_mkdir.assert_not_called() mocked_makedirs.assert_not_called() # merge == True && format == 'basic' maker1.merge = True assert maker1._create_package_dir() == -1 mocked_mkdir.assert_not_called() mocked_makedirs.assert_not_called() with mock.patch('os.path.exists', return_value=False): # merge == False && format == 'src' assert maker2._create_package_dir() == 1 mocked_mkdir.assert_not_called() assert mocked_makedirs.called # merge == True && format == 'src' maker2.merge = True assert maker2._create_package_dir() == 1 mocked_mkdir.assert_not_called() assert mocked_makedirs.called @mock.patch('os.fsync') def test_write_init(mocked_fsync, maker1): with mock.patch.object(base, 'open', mock.mock_open(), create=True) as mocked_open: with mock.patch('os.path.exists', return_value=True): # exist && force == False assert not maker1._write_init() mocked_open().write.assert_not_called() # exist && force == True maker1.force = True assert maker1._write_init() assert mocked_open().write.called with mock.patch('os.path.exists', return_value=False): # not exist assert maker1._write_init() content = ('# -*- coding: utf-8 -*-' '\n' '\n' ""__version__ = '${version}'"" '\n') mocked_open().write.assert_called_with(content) ", https://github.com/june3474/skelpy,0e64eccdc5211dbec664e7d3f9d07c6020651b63,tests/test_test.py::test_create_config_files,tests/test_test.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""test_test - pytest module for TestMaker """""" from __future__ import absolute_import, print_function import os import pytest from tempfile import gettempdir, tempdir from skelpy.makers import tests, settings from . import mock @pytest.fixture(scope='module') def maker(): settings.clear() opts = { 'projectDir': gettempdir(), 'force': True, 'merge': False, 'test': 'unittest' } return tests.TestMaker(**opts) def test_update_settings(maker): assert settings['testsDir'] == os.path.join(maker.projectDir, 'tests') def test_create_config_files(maker): with mock.patch.object(maker, 'write_file') as mocked_write: mocked_write.reset_mock() maker._create_config_files() mocked_write.assert_called_with('test_init', os.path.join(maker.testsDir, '__init__.py')) mocked_write.assert_any_call('test_main_unittest', os.path.join(maker.testsDir, 'test_main.py')) mocked_write.reset_mock() maker.test = 'pytest' maker._create_config_files() mocked_write.assert_any_call('test_main_pytest', os.path.join(maker.testsDir, 'test_main.py')) @mock.patch('os.mkdir') def test_generate(mocked_mkdir, maker): with mock.patch.object(maker, 'write_file') as mocked_write: # test create_dir part # dir exist with mock.patch('os.path.exists', return_value=True): # exist & not merge maker.merge = False maker.generate() mocked_mkdir.assert_not_called() mocked_write.assert_not_called() # exist & merge maker.merge = True maker.generate() mocked_mkdir.assert_not_called() assert mocked_write.called # not exist with mock.patch('os.path.exists', return_value=False): maker.merge = False maker.generate() mocked_mkdir.assert_any_call(maker.testsDir, 0o755) ", https://github.com/juntossomosmais/regex4ocr,9f96e9c6fe6ffc3e1452b23103fdfa6a0f1dcede,tests/integration/test_type_casting.py::test_validate_types_no_removal,tests/integration/test_type_casting.py,NIO,flaky,Accepted,https://github.com/juntossomosmais/regex4ocr/pull/1,classify;root_cause;fix_proposal,""""""" Module with unit tests for the data type casting functions """""" import copy from unittest import mock import pytest from regex4ocr.parser.type_casting import cast_type from regex4ocr.parser.type_casting import remove_wrong_types from regex4ocr.parser.type_casting import validate_types from regex4ocr.parser.yml_parser import parse_yml from tests.data.aux import open_file @pytest.fixture(scope=""module"") def pre_processed_tax_coupon_2(): """""" OCR test data for tax coupon 3."""""" OCR_TEST_RESULT_FOLDER = ""./tests/data/ocr_results/"" ocr_result = open_file( OCR_TEST_RESULT_FOLDER + ""tax_coupon_preprocessed_3.txt"" ) return ocr_result @pytest.fixture(scope=""module"") def drm_model_tax_coupon_with_inline_groups(): """""" DRM dict model for tax coupon. """""" DRM_TEST_YML_FOLDER = ""./tests/data/drms/"" drm = parse_yml(DRM_TEST_YML_FOLDER + ""drm_inline_named_groups_1.yml"") return drm @pytest.fixture(scope=""module"") def extracted_data_dict_1(): return { ""fields"": { ""cnpj"": ""10.549.937/000174"", ""date"": ""10/08/1991"", ""coo"": ""047621"", ""some_int"": ""1234"", }, ""table"": { ""header"": ""iten codigo descricao qid1un vl1unit r$ ) st vl item(r$)"", ""all_rows"": '\n17273 breit grossa 7mts"" 3unx373 ft 288 026\n2 $17 pedra 1 (ht) 2unx84 694 f1\n169 38g\n003 515 cimento votoran todas as obras 50 kg\ncred)\n30un x 26.489 f1\n794,676\n', ""footer"": ""total r$"", ""rows"": [ { ""row"": '17273 breit grossa 7mts"" 3unx373 ft 288 0262 $17 pedra 1 (ht) 2unx84 694 f1', ""data"": { ""description"": '17273 breit grossa 7mts"" ', ""qty"": ""3"", ""unit"": ""un"", }, }, {""row"": ""169 38g"", ""data"": {}}, { ""row"": ""003 515 cimento votoran todas as obras 50 kgcred)30un x 26.489 f1794,676"", ""data"": { ""description"": ""003 515 cimento votoran todas as obras 50 kgcred)"", ""qty"": ""30"", ""unit"": ""un"", }, }, ], }, } @pytest.fixture(scope=""module"") def expected_extracted_data_dict_no_removal(): return { ""fields"": { ""cnpj"": ""10.549.937/000174"", ""coo"": ""047621"", ""date"": ""1991-08-10T00:00:00"", # cast to iso format ""some_int"": 1234, # casting }, ""table"": { ""header"": ""iten codigo descricao qid1un vl1unit r$ ) st vl item(r$)"", ""all_rows"": '\n17273 breit grossa 7mts"" 3unx373 ft 288 026\n2 $17 pedra 1 (ht) 2unx84 694 f1\n169 38g\n003 515 cimento votoran todas as obras 50 kg\ncred)\n30un x 26.489 f1\n794,676\n', ""footer"": ""total r$"", ""rows"": [ { ""row"": '17273 breit grossa 7mts"" 3unx373 ft 288 0262 $17 pedra 1 (ht) 2unx84 694 f1', ""data"": { ""description"": '17273 breit grossa 7mts"" ', ""qty"": 3, ""unit"": ""un"", }, }, {""row"": ""169 38g"", ""data"": {}}, { ""row"": ""003 515 cimento votoran todas as obras 50 kgcred)30un x 26.489 f1794,676"", ""data"": { ""description"": ""003 515 cimento votoran todas as obras 50 kgcred)"", ""qty"": 30, ""unit"": ""un"", }, }, ], }, } @pytest.fixture(scope=""module"") def expected_extracted_data_dict_with_removal(): return { ""fields"": { ""cnpj"": ""10.549.937/000174"", ""coo"": ""047621"", # ""some_int"": ""1234"", # ""date"": ""10/08/1991"", }, ""table"": { ""header"": ""iten codigo descricao qid1un vl1unit r$ ) st vl item(r$)"", ""all_rows"": '\n17273 breit grossa 7mts"" 3unx373 ft 288 026\n2 $17 pedra 1 (ht) 2unx84 694 f1\n169 38g\n003 515 cimento votoran todas as obras 50 kg\ncred)\n30un x 26.489 f1\n794,676\n', ""footer"": ""total r$"", ""rows"": [ { ""row"": '17273 breit grossa 7mts"" 3unx373 ft 288 0262 $17 pedra 1 (ht) 2unx84 694 f1', ""data"": { ""description"": '17273 breit grossa 7mts"" ', ""qty"": 3, ""unit"": ""un"", }, }, {""row"": ""169 38g"", ""data"": {}}, { ""row"": ""003 515 cimento votoran todas as obras 50 kgcred)30un x 26.489 f1794,676"", ""data"": { ""description"": ""003 515 cimento votoran todas as obras 50 kgcred)"", ""qty"": 30, ""unit"": ""un"", }, }, ], }, } def test_cast_type_exception_unknown_drm_type(): """""" Unit: tests cast_type when there's an unsupported or unknown type at the DRM yml file. """""" with pytest.raises(BaseException): cast_type(""1234"", ""unknown_type"") def test_cast_type_int_type(): """""" Unit: tests cast_type when there's a correct cast to an int type. """""" assert cast_type(""1234"", ""int"") == 1234 def test_cast_type_datetime_type_exception(): """""" Unit: tests cast_type when there's an impossible cast to datetime. """""" assert cast_type(""10/08/1991"", [""datetime"", ""abc""]) is None def test_cast_type_datetime_type_ok(): """""" Unit: tests cast_type when there's a correct cast to a datetime type. """""" assert ( cast_type(""10/08/1991"", [""datetime"", ""%d/%m/%Y""]) == ""1991-08-10T00:00:00"" ) def test_cast_type_datetime_wrong_list_length(): """""" Unit: tests cast_type when there's missing datetime metadata for the type cast. """""" with pytest.raises(BaseException): cast_type(""10/08/1991"", [""datetime""]) def test_cast_type_datetime_no_timestamp(): """""" Unit: tests cast_type when the datetime metadata contains no timestamp info. """""" assert cast_type(""10/08/1991"", [""datetime"", ""%d/%m/%Y %H:%M:%S""]) is None def test_cast_type_metadata_unknown(): """""" Unit: tests cast_type when the desired type is a list with unknown type. """""" with pytest.raises(BaseException): cast_type(""10/08/1991"", [""unknown_type"", ""whatever""]) def test_cast_type_value_exception(): """""" Unit: tests cast_type when there's raised ValueError. """""" assert cast_type(""123.144-5566/12345-6"", ""int"") is None def test_remove_wrong_types_no_removal( extracted_data_dict_1, drm_model_tax_coupon_with_inline_groups ): """""" Unit: tests cast_type when all the type casts run smoothly. """""" # sends only the fields section extracted_data_section = { ""cnpj"": ""10.549.937/000174"", ""coo"": ""047621"", ""date"": ""10/08/1991"", ""some_int"": ""1234"", } # types > fields drm_types_section = { ""cnpj"": ""str"", # ""coo"": ""047621"", # not inserted ""some_int"": ""int"", ""date"": [""datetime"", ""%d/%m/%Y""], } expected_extracted_data_dict = { ""cnpj"": ""10.549.937/000174"", ""coo"": ""047621"", # since there was no typing, remains as is ""date"": ""1991-08-10T00:00:00"", ""some_int"": 1234, } # function invocation which mutates extracted_data_section remove_wrong_types(extracted_data_section, drm_types_section) assert extracted_data_section == expected_extracted_data_dict def test_remove_wrong_types_with_removal( drm_model_tax_coupon_with_inline_groups ): """""" Unit: tests cast_type when there's an unsupported or unknown type at the DRM yml file. """""" extracted_data_section = { ""cnpj"": ""10.549.937/000174"", ""coo"": ""047621"", ""some_int"": ""1234xxnotanint"", # not an integer ""date"": ""123/10/2018"", # broken date } drm_types_section = { ""cnpj"": ""str"", ""coo"": ""str"", ""some_int"": ""int"", ""date"": [""datetime"", ""%d/%m/%Y""], } expected_extracted_data_dict = { ""cnpj"": ""10.549.937/000174"", ""coo"": ""047621"", # ""some_int"": ""1234"", # raises ValueError and must be removed } # function invocation remove_wrong_types(extracted_data_section, drm_types_section) assert extracted_data_section == expected_extracted_data_dict def test_validate_types_no_removal( extracted_data_dict_1, expected_extracted_data_dict_no_removal, drm_model_tax_coupon_with_inline_groups, ): """""" Unit: tests validate_types when there are not type conflicts. """""" # function invocation validate_types( extracted_data_dict_1, drm_model_tax_coupon_with_inline_groups ) assert extracted_data_dict_1 == expected_extracted_data_dict_no_removal def test_validate_types_removal( extracted_data_dict_1, expected_extracted_data_dict_with_removal, drm_model_tax_coupon_with_inline_groups, ): """""" Unit: tests validate_types when there are type conflicts and some fields are removed. """""" # creates a deep copy of the fixture so mutations wont affect next tests extracted_data_dict = copy.deepcopy(extracted_data_dict_1) extracted_data_dict[""fields""][""some_int""] = ""abc123"" # messes int casting # function invocation validate_types(extracted_data_dict, drm_model_tax_coupon_with_inline_groups) assert extracted_data_dict == expected_extracted_data_dict_with_removal ", https://github.com/juntossomosmais/regex4ocr,9f96e9c6fe6ffc3e1452b23103fdfa6a0f1dcede,tests/integration/test_type_casting.py::test_validate_types_removal,tests/integration/test_type_casting.py,OD-Brit,flaky,RepoArchived,nan,classify;root_cause,""""""" Module with unit tests for the data type casting functions """""" import copy from unittest import mock import pytest from regex4ocr.parser.type_casting import cast_type from regex4ocr.parser.type_casting import remove_wrong_types from regex4ocr.parser.type_casting import validate_types from regex4ocr.parser.yml_parser import parse_yml from tests.data.aux import open_file @pytest.fixture(scope=""module"") def pre_processed_tax_coupon_2(): """""" OCR test data for tax coupon 3."""""" OCR_TEST_RESULT_FOLDER = ""./tests/data/ocr_results/"" ocr_result = open_file( OCR_TEST_RESULT_FOLDER + ""tax_coupon_preprocessed_3.txt"" ) return ocr_result @pytest.fixture(scope=""module"") def drm_model_tax_coupon_with_inline_groups(): """""" DRM dict model for tax coupon. """""" DRM_TEST_YML_FOLDER = ""./tests/data/drms/"" drm = parse_yml(DRM_TEST_YML_FOLDER + ""drm_inline_named_groups_1.yml"") return drm @pytest.fixture(scope=""module"") def extracted_data_dict_1(): return { ""fields"": { ""cnpj"": ""10.549.937/000174"", ""date"": ""10/08/1991"", ""coo"": ""047621"", ""some_int"": ""1234"", }, ""table"": { ""header"": ""iten codigo descricao qid1un vl1unit r$ ) st vl item(r$)"", ""all_rows"": '\n17273 breit grossa 7mts"" 3unx373 ft 288 026\n2 $17 pedra 1 (ht) 2unx84 694 f1\n169 38g\n003 515 cimento votoran todas as obras 50 kg\ncred)\n30un x 26.489 f1\n794,676\n', ""footer"": ""total r$"", ""rows"": [ { ""row"": '17273 breit grossa 7mts"" 3unx373 ft 288 0262 $17 pedra 1 (ht) 2unx84 694 f1', ""data"": { ""description"": '17273 breit grossa 7mts"" ', ""qty"": ""3"", ""unit"": ""un"", }, }, {""row"": ""169 38g"", ""data"": {}}, { ""row"": ""003 515 cimento votoran todas as obras 50 kgcred)30un x 26.489 f1794,676"", ""data"": { ""description"": ""003 515 cimento votoran todas as obras 50 kgcred)"", ""qty"": ""30"", ""unit"": ""un"", }, }, ], }, } @pytest.fixture(scope=""module"") def expected_extracted_data_dict_no_removal(): return { ""fields"": { ""cnpj"": ""10.549.937/000174"", ""coo"": ""047621"", ""date"": ""1991-08-10T00:00:00"", # cast to iso format ""some_int"": 1234, # casting }, ""table"": { ""header"": ""iten codigo descricao qid1un vl1unit r$ ) st vl item(r$)"", ""all_rows"": '\n17273 breit grossa 7mts"" 3unx373 ft 288 026\n2 $17 pedra 1 (ht) 2unx84 694 f1\n169 38g\n003 515 cimento votoran todas as obras 50 kg\ncred)\n30un x 26.489 f1\n794,676\n', ""footer"": ""total r$"", ""rows"": [ { ""row"": '17273 breit grossa 7mts"" 3unx373 ft 288 0262 $17 pedra 1 (ht) 2unx84 694 f1', ""data"": { ""description"": '17273 breit grossa 7mts"" ', ""qty"": 3, ""unit"": ""un"", }, }, {""row"": ""169 38g"", ""data"": {}}, { ""row"": ""003 515 cimento votoran todas as obras 50 kgcred)30un x 26.489 f1794,676"", ""data"": { ""description"": ""003 515 cimento votoran todas as obras 50 kgcred)"", ""qty"": 30, ""unit"": ""un"", }, }, ], }, } @pytest.fixture(scope=""module"") def expected_extracted_data_dict_with_removal(): return { ""fields"": { ""cnpj"": ""10.549.937/000174"", ""coo"": ""047621"", # ""some_int"": ""1234"", # ""date"": ""10/08/1991"", }, ""table"": { ""header"": ""iten codigo descricao qid1un vl1unit r$ ) st vl item(r$)"", ""all_rows"": '\n17273 breit grossa 7mts"" 3unx373 ft 288 026\n2 $17 pedra 1 (ht) 2unx84 694 f1\n169 38g\n003 515 cimento votoran todas as obras 50 kg\ncred)\n30un x 26.489 f1\n794,676\n', ""footer"": ""total r$"", ""rows"": [ { ""row"": '17273 breit grossa 7mts"" 3unx373 ft 288 0262 $17 pedra 1 (ht) 2unx84 694 f1', ""data"": { ""description"": '17273 breit grossa 7mts"" ', ""qty"": 3, ""unit"": ""un"", }, }, {""row"": ""169 38g"", ""data"": {}}, { ""row"": ""003 515 cimento votoran todas as obras 50 kgcred)30un x 26.489 f1794,676"", ""data"": { ""description"": ""003 515 cimento votoran todas as obras 50 kgcred)"", ""qty"": 30, ""unit"": ""un"", }, }, ], }, } def test_cast_type_exception_unknown_drm_type(): """""" Unit: tests cast_type when there's an unsupported or unknown type at the DRM yml file. """""" with pytest.raises(BaseException): cast_type(""1234"", ""unknown_type"") def test_cast_type_int_type(): """""" Unit: tests cast_type when there's a correct cast to an int type. """""" assert cast_type(""1234"", ""int"") == 1234 def test_cast_type_datetime_type_exception(): """""" Unit: tests cast_type when there's an impossible cast to datetime. """""" assert cast_type(""10/08/1991"", [""datetime"", ""abc""]) is None def test_cast_type_datetime_type_ok(): """""" Unit: tests cast_type when there's a correct cast to a datetime type. """""" assert ( cast_type(""10/08/1991"", [""datetime"", ""%d/%m/%Y""]) == ""1991-08-10T00:00:00"" ) def test_cast_type_datetime_wrong_list_length(): """""" Unit: tests cast_type when there's missing datetime metadata for the type cast. """""" with pytest.raises(BaseException): cast_type(""10/08/1991"", [""datetime""]) def test_cast_type_datetime_no_timestamp(): """""" Unit: tests cast_type when the datetime metadata contains no timestamp info. """""" assert cast_type(""10/08/1991"", [""datetime"", ""%d/%m/%Y %H:%M:%S""]) is None def test_cast_type_metadata_unknown(): """""" Unit: tests cast_type when the desired type is a list with unknown type. """""" with pytest.raises(BaseException): cast_type(""10/08/1991"", [""unknown_type"", ""whatever""]) def test_cast_type_value_exception(): """""" Unit: tests cast_type when there's raised ValueError. """""" assert cast_type(""123.144-5566/12345-6"", ""int"") is None def test_remove_wrong_types_no_removal( extracted_data_dict_1, drm_model_tax_coupon_with_inline_groups ): """""" Unit: tests cast_type when all the type casts run smoothly. """""" # sends only the fields section extracted_data_section = { ""cnpj"": ""10.549.937/000174"", ""coo"": ""047621"", ""date"": ""10/08/1991"", ""some_int"": ""1234"", } # types > fields drm_types_section = { ""cnpj"": ""str"", # ""coo"": ""047621"", # not inserted ""some_int"": ""int"", ""date"": [""datetime"", ""%d/%m/%Y""], } expected_extracted_data_dict = { ""cnpj"": ""10.549.937/000174"", ""coo"": ""047621"", # since there was no typing, remains as is ""date"": ""1991-08-10T00:00:00"", ""some_int"": 1234, } # function invocation which mutates extracted_data_section remove_wrong_types(extracted_data_section, drm_types_section) assert extracted_data_section == expected_extracted_data_dict def test_remove_wrong_types_with_removal( drm_model_tax_coupon_with_inline_groups ): """""" Unit: tests cast_type when there's an unsupported or unknown type at the DRM yml file. """""" extracted_data_section = { ""cnpj"": ""10.549.937/000174"", ""coo"": ""047621"", ""some_int"": ""1234xxnotanint"", # not an integer ""date"": ""123/10/2018"", # broken date } drm_types_section = { ""cnpj"": ""str"", ""coo"": ""str"", ""some_int"": ""int"", ""date"": [""datetime"", ""%d/%m/%Y""], } expected_extracted_data_dict = { ""cnpj"": ""10.549.937/000174"", ""coo"": ""047621"", # ""some_int"": ""1234"", # raises ValueError and must be removed } # function invocation remove_wrong_types(extracted_data_section, drm_types_section) assert extracted_data_section == expected_extracted_data_dict def test_validate_types_no_removal( extracted_data_dict_1, expected_extracted_data_dict_no_removal, drm_model_tax_coupon_with_inline_groups, ): """""" Unit: tests validate_types when there are not type conflicts. """""" # function invocation validate_types( extracted_data_dict_1, drm_model_tax_coupon_with_inline_groups ) assert extracted_data_dict_1 == expected_extracted_data_dict_no_removal def test_validate_types_removal( extracted_data_dict_1, expected_extracted_data_dict_with_removal, drm_model_tax_coupon_with_inline_groups, ): """""" Unit: tests validate_types when there are type conflicts and some fields are removed. """""" # creates a deep copy of the fixture so mutations wont affect next tests extracted_data_dict = copy.deepcopy(extracted_data_dict_1) extracted_data_dict[""fields""][""some_int""] = ""abc123"" # messes int casting # function invocation validate_types(extracted_data_dict, drm_model_tax_coupon_with_inline_groups) assert extracted_data_dict == expected_extracted_data_dict_with_removal ", https://github.com/jyn514/configparse,d7a4ee5b121de2be1858104723bdb7ac47df64ce,test/test_all.py::test_positional,test/test_all.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import atexit import json import sys import os path = os.path import toml import yaml sys.path.append(path.join(path.dirname(__file__), '..')) import configparse import pytest NAME = 'parsertest' HOME = path.expanduser('~') CONFIG_DIR = path.join(HOME, '.config', NAME) CREATED_FILES = set() def cleanup(): for filename in CREATED_FILES: os.remove(filename) CREATED_FILES.clear() atexit.register(cleanup) def write(filename, data): os.makedirs(CONFIG_DIR, exist_ok=True) with open(filename, 'w') as fd: CREATED_FILES.add(filename) fd.write(data) def write_home(ext, data): if ext is None: ext = '' elif ext[0] != '.': ext = '.' + ext write(path.join(HOME, '.' + NAME + ext), data) def write_config(ext, data): write(path.join(CONFIG_DIR, 'config.' + ext), data) def parser(): return configparse.Parser(prog=NAME) def test_basic(): write_home('json', json.dumps({ ""apples"": ""some"" })) write_config('yml', yaml.dump({ ""bananas"": ""more than none""})) write_config('toml', toml.dumps({ ""coconuts"": ""less than 5"" })) write(path.join(HOME, '.config', NAME + '.json'), json.dumps({ ""figs"": ""just one"" })) p = configparse.Parser(prog=NAME) p.add_argument(""-a"", ""--apples"") p.add_argument(""-b"", ""--bananas"") p.add_argument(""-c"", ""--coconuts"") p.add_argument(""-f"", ""--figs"") args = p.parse_args() assert args.apples == 'some' assert args.bananas == 'more than none' assert args.coconuts == 'less than 5' assert args.figs == 'just one' cleanup() def test_infer_ext(): write_home(None, json.dumps({ ""durians"": ""exactly 6""})) p = configparse.Parser(prog=NAME) p.add_argument(""-d"", ""--durians"") args = p.parse_args() assert args.durians == ""exactly 6"" cleanup() def test_default_ext(): write_home(None, yaml.dump({ ""elderberries"": ""your father smells of them""})) p = configparse.Parser(prog=NAME) p.add_argument(""-e"", ""--elderberries"") # with leading . p.set_default_ext("".yml"") args = p.parse_args() assert args.elderberries == ""your father smells of them"" write_home(None, json.dumps({ ""elderberries"": ""your father smells of them""})) # without leading . p.set_default_ext(""json"") args = p.parse_args() assert args.elderberries == ""your father smells of them"" cleanup() def test_positional(): ""make sure we didn't break existing functionality of argparse"" p = configparse.Parser(prog=NAME) p.add_argument(""positional"") assert p.parse_args([""first arg""]).positional == 'first arg' with pytest.raises(SystemExit): p.parse_args([]) def test_unknown_config(): ""make sure we give a warning for unknown options"" write_home(None, json.dumps({ ""grapefruit"": ""big and juicy""})) with pytest.warns(UserWarning): configparse.Parser(prog=NAME).parse_args([]) cleanup() def test_type(): ""make sure type= works properly"" write_home(None, json.dumps({ ""honeydew"": 5 })) p = parser() p.add_argument(""--honeydew"", type=int) with pytest.warns(UserWarning): assert p.parse_args().honeydew == 5 write_home(None, json.dumps({ ""honeydew"": ""5"" })) assert p.parse_args().honeydew == 5 def test_import_error(): write(""src/backends/blah.py"", ""import nonexistent_package"") write_home(None, json.dumps({ ""iceberg"": ""5"" })) p = parser() p.add_argument(""--iceberg"") assert p.parse_args().iceberg == ""5"" ", https://github.com/kade-robertson/config-better,c14638da3fe95a669a04db2602e1f6ea15591c36,tests/test___init__.py::TestLinuxNoXDG::test_noxdg_cache,tests/test___init__.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil import sys import tempfile import unittest import configbetter class TestMakedirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = sys.platform cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_dir(self): self.assertTrue(os.path.exists(self.conf.data)) def test_config_dir(self): self.assertTrue(os.path.exists(self.conf.config)) def test_cache_dir(self): self.assertTrue(os.path.exists(self.conf.cache)) class TestRmdirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() cls.conf.rmdirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_rmdir(self): self.assertFalse(os.path.exists(self.conf.data)) def test_config_rmdir(self): self.assertFalse(os.path.exists(self.conf.config)) def test_cache_rmdir(self): self.assertFalse(os.path.exists(self.conf.cache)) def test_windows_edgecase_rmdir(self): self.assertFalse(os.path.exists(os.path.join(self.tempdir, 'notarealapp'))) class TestWindowsNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['APPDATA'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'fakeapp', 'Data') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'fakeapp', 'Config') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'fakeapp', 'Cache') self.assertEqual(self.conf.cache, checkcache) class TestLinuxNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'linux' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'Library', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'Library', 'Preferences', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'Library', 'Caches', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacForceUnix(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp', force_unix = True) @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.datatempdir = tempfile.mkdtemp() cls.configtempdir = tempfile.mkdtemp() cls.cachetempdir = tempfile.mkdtemp() configbetter.os.environ['XDG_DATA_HOME'] = cls.datatempdir configbetter.os.environ['XDG_CONFIG_HOME'] = cls.configtempdir configbetter.os.environ['XDG_CACHE_HOME'] = cls.cachetempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.datatempdir) os.rmdir(cls.configtempdir) os.rmdir(cls.cachetempdir) def test_xdg_data(self): checkdata = os.path.join(self.datatempdir, 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_xdg_config(self): checkconfig = os.path.join(self.configtempdir, 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_xdg_cache(self): checkcache = os.path.join(self.cachetempdir, 'fakeapp') self.assertEqual(self.conf.cache, checkcache) if __name__ == '__main__': unittest.main() ", https://github.com/kade-robertson/config-better,c14638da3fe95a669a04db2602e1f6ea15591c36,tests/test___init__.py::TestLinuxNoXDG::test_noxdg_config,tests/test___init__.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil import sys import tempfile import unittest import configbetter class TestMakedirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = sys.platform cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_dir(self): self.assertTrue(os.path.exists(self.conf.data)) def test_config_dir(self): self.assertTrue(os.path.exists(self.conf.config)) def test_cache_dir(self): self.assertTrue(os.path.exists(self.conf.cache)) class TestRmdirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() cls.conf.rmdirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_rmdir(self): self.assertFalse(os.path.exists(self.conf.data)) def test_config_rmdir(self): self.assertFalse(os.path.exists(self.conf.config)) def test_cache_rmdir(self): self.assertFalse(os.path.exists(self.conf.cache)) def test_windows_edgecase_rmdir(self): self.assertFalse(os.path.exists(os.path.join(self.tempdir, 'notarealapp'))) class TestWindowsNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['APPDATA'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'fakeapp', 'Data') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'fakeapp', 'Config') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'fakeapp', 'Cache') self.assertEqual(self.conf.cache, checkcache) class TestLinuxNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'linux' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'Library', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'Library', 'Preferences', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'Library', 'Caches', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacForceUnix(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp', force_unix = True) @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.datatempdir = tempfile.mkdtemp() cls.configtempdir = tempfile.mkdtemp() cls.cachetempdir = tempfile.mkdtemp() configbetter.os.environ['XDG_DATA_HOME'] = cls.datatempdir configbetter.os.environ['XDG_CONFIG_HOME'] = cls.configtempdir configbetter.os.environ['XDG_CACHE_HOME'] = cls.cachetempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.datatempdir) os.rmdir(cls.configtempdir) os.rmdir(cls.cachetempdir) def test_xdg_data(self): checkdata = os.path.join(self.datatempdir, 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_xdg_config(self): checkconfig = os.path.join(self.configtempdir, 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_xdg_cache(self): checkcache = os.path.join(self.cachetempdir, 'fakeapp') self.assertEqual(self.conf.cache, checkcache) if __name__ == '__main__': unittest.main() ", https://github.com/kade-robertson/config-better,c14638da3fe95a669a04db2602e1f6ea15591c36,tests/test___init__.py::TestLinuxNoXDG::test_noxdg_data,tests/test___init__.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil import sys import tempfile import unittest import configbetter class TestMakedirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = sys.platform cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_dir(self): self.assertTrue(os.path.exists(self.conf.data)) def test_config_dir(self): self.assertTrue(os.path.exists(self.conf.config)) def test_cache_dir(self): self.assertTrue(os.path.exists(self.conf.cache)) class TestRmdirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() cls.conf.rmdirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_rmdir(self): self.assertFalse(os.path.exists(self.conf.data)) def test_config_rmdir(self): self.assertFalse(os.path.exists(self.conf.config)) def test_cache_rmdir(self): self.assertFalse(os.path.exists(self.conf.cache)) def test_windows_edgecase_rmdir(self): self.assertFalse(os.path.exists(os.path.join(self.tempdir, 'notarealapp'))) class TestWindowsNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['APPDATA'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'fakeapp', 'Data') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'fakeapp', 'Config') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'fakeapp', 'Cache') self.assertEqual(self.conf.cache, checkcache) class TestLinuxNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'linux' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'Library', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'Library', 'Preferences', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'Library', 'Caches', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacForceUnix(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp', force_unix = True) @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.datatempdir = tempfile.mkdtemp() cls.configtempdir = tempfile.mkdtemp() cls.cachetempdir = tempfile.mkdtemp() configbetter.os.environ['XDG_DATA_HOME'] = cls.datatempdir configbetter.os.environ['XDG_CONFIG_HOME'] = cls.configtempdir configbetter.os.environ['XDG_CACHE_HOME'] = cls.cachetempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.datatempdir) os.rmdir(cls.configtempdir) os.rmdir(cls.cachetempdir) def test_xdg_data(self): checkdata = os.path.join(self.datatempdir, 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_xdg_config(self): checkconfig = os.path.join(self.configtempdir, 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_xdg_cache(self): checkcache = os.path.join(self.cachetempdir, 'fakeapp') self.assertEqual(self.conf.cache, checkcache) if __name__ == '__main__': unittest.main() ", https://github.com/kade-robertson/config-better,c14638da3fe95a669a04db2602e1f6ea15591c36,tests/test___init__.py::TestMacForceUnix::test_noxdg_cache,tests/test___init__.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil import sys import tempfile import unittest import configbetter class TestMakedirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = sys.platform cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_dir(self): self.assertTrue(os.path.exists(self.conf.data)) def test_config_dir(self): self.assertTrue(os.path.exists(self.conf.config)) def test_cache_dir(self): self.assertTrue(os.path.exists(self.conf.cache)) class TestRmdirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() cls.conf.rmdirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_rmdir(self): self.assertFalse(os.path.exists(self.conf.data)) def test_config_rmdir(self): self.assertFalse(os.path.exists(self.conf.config)) def test_cache_rmdir(self): self.assertFalse(os.path.exists(self.conf.cache)) def test_windows_edgecase_rmdir(self): self.assertFalse(os.path.exists(os.path.join(self.tempdir, 'notarealapp'))) class TestWindowsNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['APPDATA'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'fakeapp', 'Data') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'fakeapp', 'Config') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'fakeapp', 'Cache') self.assertEqual(self.conf.cache, checkcache) class TestLinuxNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'linux' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'Library', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'Library', 'Preferences', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'Library', 'Caches', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacForceUnix(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp', force_unix = True) @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.datatempdir = tempfile.mkdtemp() cls.configtempdir = tempfile.mkdtemp() cls.cachetempdir = tempfile.mkdtemp() configbetter.os.environ['XDG_DATA_HOME'] = cls.datatempdir configbetter.os.environ['XDG_CONFIG_HOME'] = cls.configtempdir configbetter.os.environ['XDG_CACHE_HOME'] = cls.cachetempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.datatempdir) os.rmdir(cls.configtempdir) os.rmdir(cls.cachetempdir) def test_xdg_data(self): checkdata = os.path.join(self.datatempdir, 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_xdg_config(self): checkconfig = os.path.join(self.configtempdir, 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_xdg_cache(self): checkcache = os.path.join(self.cachetempdir, 'fakeapp') self.assertEqual(self.conf.cache, checkcache) if __name__ == '__main__': unittest.main() ", https://github.com/kade-robertson/config-better,c14638da3fe95a669a04db2602e1f6ea15591c36,tests/test___init__.py::TestMacForceUnix::test_noxdg_config,tests/test___init__.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil import sys import tempfile import unittest import configbetter class TestMakedirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = sys.platform cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_dir(self): self.assertTrue(os.path.exists(self.conf.data)) def test_config_dir(self): self.assertTrue(os.path.exists(self.conf.config)) def test_cache_dir(self): self.assertTrue(os.path.exists(self.conf.cache)) class TestRmdirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() cls.conf.rmdirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_rmdir(self): self.assertFalse(os.path.exists(self.conf.data)) def test_config_rmdir(self): self.assertFalse(os.path.exists(self.conf.config)) def test_cache_rmdir(self): self.assertFalse(os.path.exists(self.conf.cache)) def test_windows_edgecase_rmdir(self): self.assertFalse(os.path.exists(os.path.join(self.tempdir, 'notarealapp'))) class TestWindowsNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['APPDATA'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'fakeapp', 'Data') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'fakeapp', 'Config') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'fakeapp', 'Cache') self.assertEqual(self.conf.cache, checkcache) class TestLinuxNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'linux' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'Library', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'Library', 'Preferences', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'Library', 'Caches', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacForceUnix(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp', force_unix = True) @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.datatempdir = tempfile.mkdtemp() cls.configtempdir = tempfile.mkdtemp() cls.cachetempdir = tempfile.mkdtemp() configbetter.os.environ['XDG_DATA_HOME'] = cls.datatempdir configbetter.os.environ['XDG_CONFIG_HOME'] = cls.configtempdir configbetter.os.environ['XDG_CACHE_HOME'] = cls.cachetempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.datatempdir) os.rmdir(cls.configtempdir) os.rmdir(cls.cachetempdir) def test_xdg_data(self): checkdata = os.path.join(self.datatempdir, 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_xdg_config(self): checkconfig = os.path.join(self.configtempdir, 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_xdg_cache(self): checkcache = os.path.join(self.cachetempdir, 'fakeapp') self.assertEqual(self.conf.cache, checkcache) if __name__ == '__main__': unittest.main() ", https://github.com/kade-robertson/config-better,c14638da3fe95a669a04db2602e1f6ea15591c36,tests/test___init__.py::TestMacForceUnix::test_noxdg_data,tests/test___init__.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil import sys import tempfile import unittest import configbetter class TestMakedirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = sys.platform cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_dir(self): self.assertTrue(os.path.exists(self.conf.data)) def test_config_dir(self): self.assertTrue(os.path.exists(self.conf.config)) def test_cache_dir(self): self.assertTrue(os.path.exists(self.conf.cache)) class TestRmdirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() cls.conf.rmdirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_rmdir(self): self.assertFalse(os.path.exists(self.conf.data)) def test_config_rmdir(self): self.assertFalse(os.path.exists(self.conf.config)) def test_cache_rmdir(self): self.assertFalse(os.path.exists(self.conf.cache)) def test_windows_edgecase_rmdir(self): self.assertFalse(os.path.exists(os.path.join(self.tempdir, 'notarealapp'))) class TestWindowsNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['APPDATA'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'fakeapp', 'Data') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'fakeapp', 'Config') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'fakeapp', 'Cache') self.assertEqual(self.conf.cache, checkcache) class TestLinuxNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'linux' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'Library', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'Library', 'Preferences', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'Library', 'Caches', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacForceUnix(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp', force_unix = True) @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.datatempdir = tempfile.mkdtemp() cls.configtempdir = tempfile.mkdtemp() cls.cachetempdir = tempfile.mkdtemp() configbetter.os.environ['XDG_DATA_HOME'] = cls.datatempdir configbetter.os.environ['XDG_CONFIG_HOME'] = cls.configtempdir configbetter.os.environ['XDG_CACHE_HOME'] = cls.cachetempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.datatempdir) os.rmdir(cls.configtempdir) os.rmdir(cls.cachetempdir) def test_xdg_data(self): checkdata = os.path.join(self.datatempdir, 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_xdg_config(self): checkconfig = os.path.join(self.configtempdir, 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_xdg_cache(self): checkcache = os.path.join(self.cachetempdir, 'fakeapp') self.assertEqual(self.conf.cache, checkcache) if __name__ == '__main__': unittest.main() ", https://github.com/kade-robertson/config-better,c14638da3fe95a669a04db2602e1f6ea15591c36,tests/test___init__.py::TestMacNoXDG::test_noxdg_cache,tests/test___init__.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil import sys import tempfile import unittest import configbetter class TestMakedirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = sys.platform cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_dir(self): self.assertTrue(os.path.exists(self.conf.data)) def test_config_dir(self): self.assertTrue(os.path.exists(self.conf.config)) def test_cache_dir(self): self.assertTrue(os.path.exists(self.conf.cache)) class TestRmdirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() cls.conf.rmdirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_rmdir(self): self.assertFalse(os.path.exists(self.conf.data)) def test_config_rmdir(self): self.assertFalse(os.path.exists(self.conf.config)) def test_cache_rmdir(self): self.assertFalse(os.path.exists(self.conf.cache)) def test_windows_edgecase_rmdir(self): self.assertFalse(os.path.exists(os.path.join(self.tempdir, 'notarealapp'))) class TestWindowsNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['APPDATA'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'fakeapp', 'Data') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'fakeapp', 'Config') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'fakeapp', 'Cache') self.assertEqual(self.conf.cache, checkcache) class TestLinuxNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'linux' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'Library', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'Library', 'Preferences', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'Library', 'Caches', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacForceUnix(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp', force_unix = True) @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.datatempdir = tempfile.mkdtemp() cls.configtempdir = tempfile.mkdtemp() cls.cachetempdir = tempfile.mkdtemp() configbetter.os.environ['XDG_DATA_HOME'] = cls.datatempdir configbetter.os.environ['XDG_CONFIG_HOME'] = cls.configtempdir configbetter.os.environ['XDG_CACHE_HOME'] = cls.cachetempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.datatempdir) os.rmdir(cls.configtempdir) os.rmdir(cls.cachetempdir) def test_xdg_data(self): checkdata = os.path.join(self.datatempdir, 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_xdg_config(self): checkconfig = os.path.join(self.configtempdir, 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_xdg_cache(self): checkcache = os.path.join(self.cachetempdir, 'fakeapp') self.assertEqual(self.conf.cache, checkcache) if __name__ == '__main__': unittest.main() ", https://github.com/kade-robertson/config-better,c14638da3fe95a669a04db2602e1f6ea15591c36,tests/test___init__.py::TestMacNoXDG::test_noxdg_config,tests/test___init__.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil import sys import tempfile import unittest import configbetter class TestMakedirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = sys.platform cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_dir(self): self.assertTrue(os.path.exists(self.conf.data)) def test_config_dir(self): self.assertTrue(os.path.exists(self.conf.config)) def test_cache_dir(self): self.assertTrue(os.path.exists(self.conf.cache)) class TestRmdirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() cls.conf.rmdirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_rmdir(self): self.assertFalse(os.path.exists(self.conf.data)) def test_config_rmdir(self): self.assertFalse(os.path.exists(self.conf.config)) def test_cache_rmdir(self): self.assertFalse(os.path.exists(self.conf.cache)) def test_windows_edgecase_rmdir(self): self.assertFalse(os.path.exists(os.path.join(self.tempdir, 'notarealapp'))) class TestWindowsNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['APPDATA'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'fakeapp', 'Data') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'fakeapp', 'Config') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'fakeapp', 'Cache') self.assertEqual(self.conf.cache, checkcache) class TestLinuxNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'linux' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'Library', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'Library', 'Preferences', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'Library', 'Caches', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacForceUnix(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp', force_unix = True) @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.datatempdir = tempfile.mkdtemp() cls.configtempdir = tempfile.mkdtemp() cls.cachetempdir = tempfile.mkdtemp() configbetter.os.environ['XDG_DATA_HOME'] = cls.datatempdir configbetter.os.environ['XDG_CONFIG_HOME'] = cls.configtempdir configbetter.os.environ['XDG_CACHE_HOME'] = cls.cachetempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.datatempdir) os.rmdir(cls.configtempdir) os.rmdir(cls.cachetempdir) def test_xdg_data(self): checkdata = os.path.join(self.datatempdir, 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_xdg_config(self): checkconfig = os.path.join(self.configtempdir, 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_xdg_cache(self): checkcache = os.path.join(self.cachetempdir, 'fakeapp') self.assertEqual(self.conf.cache, checkcache) if __name__ == '__main__': unittest.main() ", https://github.com/kade-robertson/config-better,c14638da3fe95a669a04db2602e1f6ea15591c36,tests/test___init__.py::TestMacNoXDG::test_noxdg_data,tests/test___init__.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil import sys import tempfile import unittest import configbetter class TestMakedirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = sys.platform cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_dir(self): self.assertTrue(os.path.exists(self.conf.data)) def test_config_dir(self): self.assertTrue(os.path.exists(self.conf.config)) def test_cache_dir(self): self.assertTrue(os.path.exists(self.conf.cache)) class TestRmdirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() cls.conf.rmdirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_rmdir(self): self.assertFalse(os.path.exists(self.conf.data)) def test_config_rmdir(self): self.assertFalse(os.path.exists(self.conf.config)) def test_cache_rmdir(self): self.assertFalse(os.path.exists(self.conf.cache)) def test_windows_edgecase_rmdir(self): self.assertFalse(os.path.exists(os.path.join(self.tempdir, 'notarealapp'))) class TestWindowsNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['APPDATA'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'fakeapp', 'Data') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'fakeapp', 'Config') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'fakeapp', 'Cache') self.assertEqual(self.conf.cache, checkcache) class TestLinuxNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'linux' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'Library', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'Library', 'Preferences', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'Library', 'Caches', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacForceUnix(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp', force_unix = True) @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.datatempdir = tempfile.mkdtemp() cls.configtempdir = tempfile.mkdtemp() cls.cachetempdir = tempfile.mkdtemp() configbetter.os.environ['XDG_DATA_HOME'] = cls.datatempdir configbetter.os.environ['XDG_CONFIG_HOME'] = cls.configtempdir configbetter.os.environ['XDG_CACHE_HOME'] = cls.cachetempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.datatempdir) os.rmdir(cls.configtempdir) os.rmdir(cls.cachetempdir) def test_xdg_data(self): checkdata = os.path.join(self.datatempdir, 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_xdg_config(self): checkconfig = os.path.join(self.configtempdir, 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_xdg_cache(self): checkcache = os.path.join(self.cachetempdir, 'fakeapp') self.assertEqual(self.conf.cache, checkcache) if __name__ == '__main__': unittest.main() ", https://github.com/kade-robertson/config-better,c14638da3fe95a669a04db2602e1f6ea15591c36,tests/test___init__.py::TestWindowsNoXDG::test_noxdg_cache,tests/test___init__.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil import sys import tempfile import unittest import configbetter class TestMakedirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = sys.platform cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_dir(self): self.assertTrue(os.path.exists(self.conf.data)) def test_config_dir(self): self.assertTrue(os.path.exists(self.conf.config)) def test_cache_dir(self): self.assertTrue(os.path.exists(self.conf.cache)) class TestRmdirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() cls.conf.rmdirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_rmdir(self): self.assertFalse(os.path.exists(self.conf.data)) def test_config_rmdir(self): self.assertFalse(os.path.exists(self.conf.config)) def test_cache_rmdir(self): self.assertFalse(os.path.exists(self.conf.cache)) def test_windows_edgecase_rmdir(self): self.assertFalse(os.path.exists(os.path.join(self.tempdir, 'notarealapp'))) class TestWindowsNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['APPDATA'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'fakeapp', 'Data') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'fakeapp', 'Config') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'fakeapp', 'Cache') self.assertEqual(self.conf.cache, checkcache) class TestLinuxNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'linux' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'Library', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'Library', 'Preferences', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'Library', 'Caches', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacForceUnix(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp', force_unix = True) @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.datatempdir = tempfile.mkdtemp() cls.configtempdir = tempfile.mkdtemp() cls.cachetempdir = tempfile.mkdtemp() configbetter.os.environ['XDG_DATA_HOME'] = cls.datatempdir configbetter.os.environ['XDG_CONFIG_HOME'] = cls.configtempdir configbetter.os.environ['XDG_CACHE_HOME'] = cls.cachetempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.datatempdir) os.rmdir(cls.configtempdir) os.rmdir(cls.cachetempdir) def test_xdg_data(self): checkdata = os.path.join(self.datatempdir, 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_xdg_config(self): checkconfig = os.path.join(self.configtempdir, 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_xdg_cache(self): checkcache = os.path.join(self.cachetempdir, 'fakeapp') self.assertEqual(self.conf.cache, checkcache) if __name__ == '__main__': unittest.main() ", https://github.com/kade-robertson/config-better,c14638da3fe95a669a04db2602e1f6ea15591c36,tests/test___init__.py::TestWindowsNoXDG::test_noxdg_config,tests/test___init__.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil import sys import tempfile import unittest import configbetter class TestMakedirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = sys.platform cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_dir(self): self.assertTrue(os.path.exists(self.conf.data)) def test_config_dir(self): self.assertTrue(os.path.exists(self.conf.config)) def test_cache_dir(self): self.assertTrue(os.path.exists(self.conf.cache)) class TestRmdirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() cls.conf.rmdirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_rmdir(self): self.assertFalse(os.path.exists(self.conf.data)) def test_config_rmdir(self): self.assertFalse(os.path.exists(self.conf.config)) def test_cache_rmdir(self): self.assertFalse(os.path.exists(self.conf.cache)) def test_windows_edgecase_rmdir(self): self.assertFalse(os.path.exists(os.path.join(self.tempdir, 'notarealapp'))) class TestWindowsNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['APPDATA'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'fakeapp', 'Data') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'fakeapp', 'Config') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'fakeapp', 'Cache') self.assertEqual(self.conf.cache, checkcache) class TestLinuxNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'linux' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'Library', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'Library', 'Preferences', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'Library', 'Caches', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacForceUnix(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp', force_unix = True) @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.datatempdir = tempfile.mkdtemp() cls.configtempdir = tempfile.mkdtemp() cls.cachetempdir = tempfile.mkdtemp() configbetter.os.environ['XDG_DATA_HOME'] = cls.datatempdir configbetter.os.environ['XDG_CONFIG_HOME'] = cls.configtempdir configbetter.os.environ['XDG_CACHE_HOME'] = cls.cachetempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.datatempdir) os.rmdir(cls.configtempdir) os.rmdir(cls.cachetempdir) def test_xdg_data(self): checkdata = os.path.join(self.datatempdir, 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_xdg_config(self): checkconfig = os.path.join(self.configtempdir, 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_xdg_cache(self): checkcache = os.path.join(self.cachetempdir, 'fakeapp') self.assertEqual(self.conf.cache, checkcache) if __name__ == '__main__': unittest.main() ", https://github.com/kade-robertson/config-better,c14638da3fe95a669a04db2602e1f6ea15591c36,tests/test___init__.py::TestWindowsNoXDG::test_noxdg_data,tests/test___init__.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil import sys import tempfile import unittest import configbetter class TestMakedirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = sys.platform cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_dir(self): self.assertTrue(os.path.exists(self.conf.data)) def test_config_dir(self): self.assertTrue(os.path.exists(self.conf.config)) def test_cache_dir(self): self.assertTrue(os.path.exists(self.conf.cache)) class TestRmdirs(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() if sys.platform in ['linux', 'darwin']: configbetter.os.environ['HOME'] = cls.tempdir elif sys.platform == 'win32': configbetter.os.environ['APPDATA'] = cls.tempdir if 'XDG_DATA_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_DATA_HOME'] if 'XDG_CONFIG_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CONFIG_HOME'] if 'XDG_CACHE_HOME' in configbetter.os.environ: del configbetter.os.environ['XDG_CACHE_HOME'] cls.conf = configbetter.Config('notarealapp') cls.conf.makedirs() cls.conf.rmdirs() @classmethod def tearDownClass(cls): shutil.rmtree(cls.tempdir) def test_data_rmdir(self): self.assertFalse(os.path.exists(self.conf.data)) def test_config_rmdir(self): self.assertFalse(os.path.exists(self.conf.config)) def test_cache_rmdir(self): self.assertFalse(os.path.exists(self.conf.cache)) def test_windows_edgecase_rmdir(self): self.assertFalse(os.path.exists(os.path.join(self.tempdir, 'notarealapp'))) class TestWindowsNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['APPDATA'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'fakeapp', 'Data') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'fakeapp', 'Config') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'fakeapp', 'Cache') self.assertEqual(self.conf.cache, checkcache) class TestLinuxNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'linux' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacNoXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, 'Library', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, 'Library', 'Preferences', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, 'Library', 'Caches', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestMacForceUnix(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'darwin' cls.tempdir = tempfile.mkdtemp() configbetter.os.environ['HOME'] = cls.tempdir cls.conf = configbetter.Config('fakeapp', force_unix = True) @classmethod def tearDownClass(cls): os.rmdir(cls.tempdir) def test_noxdg_data(self): checkdata = os.path.join(self.tempdir, '.local', 'share', 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_noxdg_config(self): checkconfig = os.path.join(self.tempdir, '.config', 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_noxdg_cache(self): checkcache = os.path.join(self.tempdir, '.cache', 'fakeapp') self.assertEqual(self.conf.cache, checkcache) class TestXDG(unittest.TestCase): @classmethod def setUpClass(cls): configbetter.sys.platform = 'win32' cls.datatempdir = tempfile.mkdtemp() cls.configtempdir = tempfile.mkdtemp() cls.cachetempdir = tempfile.mkdtemp() configbetter.os.environ['XDG_DATA_HOME'] = cls.datatempdir configbetter.os.environ['XDG_CONFIG_HOME'] = cls.configtempdir configbetter.os.environ['XDG_CACHE_HOME'] = cls.cachetempdir cls.conf = configbetter.Config('fakeapp') @classmethod def tearDownClass(cls): os.rmdir(cls.datatempdir) os.rmdir(cls.configtempdir) os.rmdir(cls.cachetempdir) def test_xdg_data(self): checkdata = os.path.join(self.datatempdir, 'fakeapp') self.assertEqual(self.conf.data, checkdata) def test_xdg_config(self): checkconfig = os.path.join(self.configtempdir, 'fakeapp') self.assertEqual(self.conf.config, checkconfig) def test_xdg_cache(self): checkcache = os.path.join(self.cachetempdir, 'fakeapp') self.assertEqual(self.conf.cache, checkcache) if __name__ == '__main__': unittest.main() ", https://github.com/kellylab/Fireworks,ff027cd8d1b8ce5eec6a37d786e7de675d8c0849,fireworks/core/message_test.py::test_Message_set_get,fireworks/core/message_test.py,NIO,flaky,Opened,https://github.com/kellylab/Fireworks/pull/80,classify;root_cause,"from fireworks import message as messi from fireworks import Message, TensorMessage import torch import os import numpy as np from itertools import product import pandas as pd from itertools import count from io import BytesIO import pickle tensors = { 'a': torch.Tensor([1,2,3]), 'b': torch.Tensor([4,5,6]), } vectors = { 'c': np.array([7,8,9]), 'd': np.array([10,11,12]), } dtensors = { 'a': torch.Tensor([[1,2,3],[4,5,6],[7,8,9]]), 'b': torch.Tensor([[-1,-2,-3],[-4,-5,-6], [-7,-8,-9]]), } def test_compute_length(): l = messi.compute_length(tensors) assert l == 3 l = messi.compute_length(vectors) assert l == 3 def test_extract_tensors(): target = {**tensors, **vectors} t, v = messi.extract_tensors(target) assert t == tensors assert v == vectors t, v = messi.extract_tensors(tensors) assert t == tensors assert v == {} t, v = messi.extract_tensors(vectors) assert t == {} assert v == vectors def test_complement(): n = 10 index = 7 complement = messi.complement(index, n) assert complement == [0,1,2,3,4,5,6,8,9] index = slice(2,5) complement = messi.complement(index, n) assert complement == [0,1,5,6,7,8,9] index = [2,4,6] complement = messi.complement(index, n) assert complement == [0,1,3,5,7,8,9] def test_Message(): """""" Test init, getitem, and len methopl. """""" def attribute_test(message, length = 3): assert len(message) == length assert message[0].tensors() == { 'a': torch.Tensor([1]), 'b': torch.Tensor([4]), } assert message[0].dataframe().equals(pd.DataFrame({ 'c': np.array([7]), 'd': np.array([10]), })) assert message[0] == Message({'a': torch.Tensor([1]),'b': torch.Tensor([4])}, pd.DataFrame({'c': np.array([7]),'d': np.array([10]),})) assert message[1:3].tensors() == { 'a': torch.Tensor([2,3]), 'b': torch.Tensor([5,6]), } assert message[1:3].dataframe().equals(pd.DataFrame({ 'c': np.array([8,9]), 'd': np.array([11,12]), })) assert (message['a'] == torch.Tensor([1,2,3])).all() assert message[['a','c']] == Message({'a': torch.Tensor([1,2,3]), 'c': np.array([7,8,9])}) assert message[1:3] == Message({'a': torch.Tensor([2,3]),'b': torch.Tensor([5,6])}, pd.DataFrame({'c': np.array([8,9]),'d': np.array([11,12])})) # Test length assert len(message) == length # Test __getitem__ # Init empty message m = Message() assert len(m) == 0 # Init message from tensor_dict / TensorMessage and dict of arrays / dataframe using positional arguments. tensor_message = TensorMessage(tensors) tensor_as_message = Message(tensors = tensors) df = pd.DataFrame(vectors) df_as_message = Message(df = vectors) # Try every combination tensor_options = [tensors, tensor_message, tensor_as_message] vector_options = [vectors, df, df_as_message] for t, v in product(tensor_options, vector_options): m = Message(t, v) attribute_test(m) m = Message(tensors = t, df = v) attribute_test(m) # Test one sided Messages for t in tensor_options: m = Message(t, None) assert len(m) == 3 assert m == Message(tensors) for v in vector_options: m = Message(None, v) assert len(m) == 3 assert m == Message(vectors) # Init message from a single dict everything = {**tensors, **vectors} m = Message(everything) attribute_test(m) def test_Message_from_objects(): v = vectors.copy() t = tensors.copy() v['c'] = np.array([1.,2.]) v['r'] = 'howdy' t['a'] = torch.randn(5) t['q'] = torch.randn([4,3]) combined = {**t, **v} m = Message.from_objects(t, v) assert (set(m.keys()) == set(['c','d','r','b','a','q'])) for key in ['c','d','b','a','q']: assert (m[key][0] == combined[key]).all() assert m['r'][0] == combined['r'] assert len(m) == 1 def test_getitem(): m = Message(tensors, vectors) assert m[0] == Message({'a': torch.Tensor([1]), 'b': torch.Tensor([4])}, {'c': np.array([7]), 'd': np.array([10])}) assert m[[0,2]] == Message({'a': torch.Tensor([1,3]), 'b': torch.Tensor([4,6])}, {'c': np.array([7,9]), 'd': np.array([10,12])}) # Check that out of bounds index calls raise errors try: m[3] assert False except IndexError: assert True try: m[3:5] assert False except IndexError: assert True def test_cache(): pass def test_tensors(): m = Message(tensors, vectors) t = m.tensors() assert t == TensorMessage(tensors) t = m.tensors(keys=['a']) assert t == TensorMessage({'a': tensors['a']}) t = m.tensors(keys=['a','c']) assert t == TensorMessage({'a': tensors['a'], 'c': torch.Tensor(vectors['c'])}) def test_df(): m = Message(tensors, vectors) df = m.dataframe() assert df.equals(pd.DataFrame(vectors)) df = m.dataframe(keys=['c']) assert df.equals(pd.DataFrame({'c': vectors['c']})) df = m.dataframe(keys=['c','a']) assert (df == (pd.DataFrame({'c': vectors['c'], 'a': np.array(tensors['a'])}))).all().all() def test_to_dataframe(): mo = Message(tensors,vectors) # no = mo.to_dataframe() # assert no.tensor_message == {} # assert (no['a'] == mo['a']).all() # assert (no['b'] == mo['b']).all() # for letter in ['a','b','c','d']: # assert letter in no.df lo = Message(dtensors, vectors) ok = lo.to_dataframe() for i in range(3): assert (ok['a'][i] == dtensors['a'][i].numpy()).all() assert (ok['b'][i] == dtensors['b'][i].numpy()).all() def test_cpu_gpu(): m = Message(tensors, vectors) m.cpu() assert set(m.tensors().keys()) == set(['a','b']) for key, tensor in m.tensors().items(): assert tensor.device.type == 'cpu' if torch.cuda.is_available(): m.cuda() for key, tensor in m.tensors().items(): assert tensor.device.type == 'cuda' m.cpu() for key, tensor in m.tensors().items(): assert tensor.device.type == 'cpu' def test_append(): t = tensors v = vectors m1 = Message(t, v) m2 = Message(t, v) m3 = Message(t) m4 = TensorMessage(t) m5 = Message(pd.DataFrame(v)) m6 = pd.DataFrame(v) m0 = Message() assert(len(m0) == 0) m = m0.append(Message(t)) assert m == Message(t) m = m0.append(Message(v)) assert m == Message(v) m = m0.append(Message(t,v)) assert m == Message(t,v) m = m1.append(m2) assert len(m) == 6 assert m == Message({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])}, {'c': np.array([7,8,9,7,8,9]), 'd': np.array([10,11,12,10,11,12])}) m = m3.append(t) assert len(m) == 6 assert m == Message({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])}) m = m3.append(m3) assert len(m) == 6 assert m == Message({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])}) m = m3.append(m4) assert len(m) == 6 assert m == Message({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])}) m = m4.append(t) assert len(m) == 6 assert m == TensorMessage({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])}) m = m4.append(m3) assert len(m) == 6 assert m == TensorMessage({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])}) m = m4.append(m4) assert len(m) == 6 assert m == TensorMessage({'a': torch.Tensor([1,2,3,1,2,3]), 'b': torch.Tensor([4,5,6,4,5,6])}) m = m5.append(v) assert len(m) == 6 assert m == Message({'c': np.array([7,8,9,7,8,9]), 'd': np.array([10,11,12,10,11,12])}) m = m5.append(m5) assert len(m) == 6 assert m == Message({'c': np.array([7,8,9,7,8,9]), 'd': np.array([10,11,12,10,11,12])}) m = m5.append(m6) assert len(m) == 6 assert m == Message({'c': np.array([7,8,9,7,8,9]), 'd': np.array([10,11,12,10,11,12])}) # Test type conversions on appending to TensorMessage m = m4.append({'a': np.array([42]), 'b': np.array([24])}) assert len(m) == 4 assert m == TensorMessage({'a': torch.Tensor([1,2,3,42]), 'b': torch.Tensor([4,5,6,24])}) def test_join(): t = tensors v = vectors t2 = {'d': torch.Tensor([13,14,15])} v2 = {'e': np.array([16,17,18])} m1 = Message(t,v) m2 = Message(t) m2_t = TensorMessage(t) m3 = Message(v) m4 = Message(t2,v2) m5 = Message(t2) m5_t = TensorMessage(t2) m6 = Message(v2) m7 = Message(t,v2) m8 = Message(t2, v) # Test if a tensor message can be merged into a message and vice versa assert m2.merge(m3) == m1 assert m3.merge(m2) == m1 assert m3.merge(m2_t) == m1 assert m3.merge(t) == m1 # Test if the tensors in messages can be merged assert m2.merge(t2) == Message({**t, **t2}) assert m2.merge(m5) == Message({**t, **t2}) assert m2.merge(m5_t) == Message({**t, **t2}) assert m2_t.merge(t2) == TensorMessage({**t, **t2}) assert m2_t.merge(m5) == TensorMessage({**t, **t2}) assert m2_t.merge(m5_t) == TensorMessage({**t, **t2}) # Test if the dataframes in messages can be merged assert m3.merge(m6) == Message({**v, **v2}) assert m6.merge(m3) == Message({**v, **v2}) assert m3.merge(v2) == Message({**v, **v2}) def test_Message_set_get(): # Test point updates email = Message(tensors, vectors) gmail = Message({'a':torch.Tensor([1,42,3]), 'b':torch.Tensor([4,43,6]), 'c': np.array([7,99,9]), 'd': np.array([10,100,12])}) replacement = {'a': torch.Tensor([42]), 'b': torch.Tensor([43]), 'c': np.array([99]), 'd': np.array([100])} assert len(email) == 3 assert email != gmail email[1] = replacement assert email == gmail # Test range", https://github.com/kenkundert/engfmt,b908ef69aa7941e561019e24f969703f3234753b,test_constants.py::test_constants,test_constants.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from engfmt import Quantity, quant_to_eng, set_preferences set_preferences(spacer=' ') def test_constants(): assert '{:.12q}'.format(Quantity('h')) == '662.606957e-36 J-s' assert '{:.12q}'.format(Quantity('k')) == '13.806488e-24 J/K' assert '{:.12q}'.format(Quantity('q')) == '160.2176565e-21 C' assert '{:.12q}'.format(Quantity('c')) == '299.792458 Mm/s' assert '{:.12q}'.format(Quantity('C0')) == '273.15 K' assert '{:.12q}'.format(Quantity('eps0')) == '8.854187817 pF/m' assert '{:.12q}'.format(Quantity('mu0')) == '1.256637061436 uH/m' assert '{:.12q}'.format(Quantity('Z0')) == '376.730313461 Ohms' assert quant_to_eng('h') == '662.61e-36 J-s' assert quant_to_eng('k') == '13.806e-24 J/K' assert quant_to_eng('q') == '160.22e-21 C' assert quant_to_eng('c') == '299.79 Mm/s' assert quant_to_eng('C0') == '273.15 K' assert quant_to_eng('eps0') == '8.8542 pF/m' assert quant_to_eng('mu0') == '1.2566 uH/m' assert quant_to_eng('Z0') == '376.73 Ohms' ", https://github.com/kenkundert/engfmt,b908ef69aa7941e561019e24f969703f3234753b,test_format.py::test_format,test_format.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from engfmt import Quantity, set_preferences set_preferences(spacer=' ') def test_format(): q=Quantity('1420.405751786 MHz') q.add_name('f') q.add_desc('frequency of hydrogen line') assert '{}'.format(q) == '1.4204 GHz' assert '{:.8q}'.format(q) == '1.42040575 GHz' assert '{:.8}'.format(q) == '1.42040575 GHz' assert '{:r}'.format(q) == '1.4204G' assert '{:u}'.format(q) == 'Hz' assert '{:f}'.format(q) == '1420405751.786000' assert '{:e}'.format(q) == '1.420406e+09' assert '{:g}'.format(q) == '1.42041e+09' assert '{:n}'.format(q) == 'f' assert '{:d}'.format(q) == 'frequency of hydrogen line' assert '{:Q}'.format(q) == 'f = 1.4204 GHz' assert '{:R}'.format(q) == 'f = 1.4204G' assert '{:X}'.format(q) == '1.4204 GHz' q=Quantity('2n') assert float(q) == 2e-9 ", https://github.com/kenkundert/engfmt,b908ef69aa7941e561019e24f969703f3234753b,test_misc.py::test_misc,test_misc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from engfmt import * import pytest set_preferences(spacer=' ') def test_misc(): q=Quantity(1420405751.786, 'Hz') assert q.strip() == '1.42040575e+09' t=quant_to_tuple('1420405751.786 Hz') assert t == (1420405751.786, 'Hz') t=quant_to_eng('1420405751.786 Hz') assert t == '1.4204 GHz' s=quant_to_str('1420405751.786 Hz') assert s == '1420405751.786 Hz' f=quant_to_float('1420405751.786 Hz') assert f == 1420405751.786 t=quant_to_unitless_eng('1420405751.786 Hz') assert t == '1.4204G' s=quant_to_unitless_str('1420405751.786 Hz') assert s == '1420405751.786' s=quant_to_unitless_str(1420405751.786, 'Hz') assert s == '1.42040575e+09' f=quant_strip('1420405751.786 Hz') assert f == '1420405751.786' f=quant_strip('14204.05751786MHz') assert f == '14204.05751786M' q=Quantity('1420405751.786 Hz', 'Hz') assert q.strip() == '1420405751.786' q=Quantity('1420405751.786 Hz') assert q.is_nan() == False q=Quantity('1420405751.786 Hz') assert q.is_infinite() == False q=Quantity('NaN Hz') assert q.is_nan() == True q=Quantity('NaN Hz') assert q.is_infinite() == False q=Quantity('inf Hz') assert q.is_nan() == False q=Quantity('inf Hz') assert q.is_infinite() == True with pytest.raises(AssertionError): q=Quantity('1420405751.786 Hz', 'Ohms') with pytest.raises(ValueError): add_to_namespace('1ns') with pytest.raises(ValueError): add_to_namespace('x*y = z') ", https://github.com/kenkundert/engfmt,b908ef69aa7941e561019e24f969703f3234753b,test_namespace.py::test_namespace,test_namespace.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from engfmt import add_to_namespace, set_preferences set_preferences(spacer=' ') def test_namespace(): add_to_namespace(''' h_line = 1420.405751786 MHz -- Frequency of the hydrogen line k = 13.806488e-24 J/K -- Boltzmann's constant Temp = 300_K -- Temperature M = 2 -- Divide ratio of HF divider N = 8 -- Divide ratio of MF divider F = 2 -- Divide ratio of LF divider Fref = 156MHz -- Reference frequency Kdet = 88.3uA -- Gain of phase detector (Imax) Kvco = 9.07GHz/V -- Gain of VCO Cs = 1.41pF -- Shunt capacitance Cp = 59.7pF -- Pole capacitance Rz = 2.24KOhms -- Zero resistance Fstart = 1KHz -- Lower frequency bound Fstop = 1GHz -- Upper frequency bound Spd = 1.47E-24 A^2/Hz -- Spectral density of the output noise of the PFD/CP FcorPD = 1.5MHz -- PFD/CP flicker noise corner frequency JdivM = 2.43E-18 s/rt(Hz) -- Spectral density of the output jitter of divM FcorDivM = 7MHz -- divM flicker noise corner frequency JdivN = 4.47E-18 s/rt(Hz) -- Spectral density of the output jitter of divN FcorDivN = 1.5MHz -- divN flicker noise corner frequency JdivF = 1.82E-17 s/rt(Hz) -- Spectral density of the output jitter of divF FcorDivF = 2MHz -- divF flicker noise corner frequency Jbuf = 3.70E-18 s/rt(Hz) -- Spectral density of the output jitter of output buffer FcorBuf = 2.5MHz -- buf flicker noise corner frequency Lvco = -125.00 dBc/Hz -- Oscillator phase noise injected after VCO FcorVCO = 3MHz -- VCO flicker noise corner frequency Lref = -110.00 dbc/Hz -- Oscillator phase noise injected at reference input FcorRef = 0_Hz -- Freq. reference flicker noise corner frequency FmaskLFcor = 12kHz -- Jitter generation mask low frequency corner FmaskHFbound = 5MHz -- Jitter generation mask high frequency bound -- The remainder are built in constants plank = h -- Plank's constant boltz = k -- Boltzmann's constant ec = q -- Elementary charge speed_of_light = c -- Speed of light zero_celsius = C0 -- Zero degree Celcius in Kelvin epsilon0 = eps0 -- Permittivity of free space mu0 = mu0 -- Permeability of free space Z0 = Z0 -- Characteristic impedance of free space c = c -- speed of light ''') assert str(h_line) == '1.4204 GHz' assert str(k) == '13.806e-24 J/K' assert str(Temp) == '300 K' assert str(M) == '2' assert str(N) == '8' assert str(F) == '2' assert str(Fref) == '156 MHz' assert str(Kdet) == '88.3 uA' assert str(Kvco) == '9.07 GHz/V' assert str(Cs) == '1.41 pF' assert str(Cp) == '59.7 pF' assert str(Rz) == '2.24 kOhms' assert str(Fstart) == '1 kHz' assert str(Fstop) == '1 GHz' assert str(Spd) == '1.47e-24 A^2/Hz' assert str(FcorPD) == '1.5 MHz' assert str(JdivM) == '2.43 as/rt(Hz)' assert str(FcorDivM) == '7 MHz' assert str(JdivN) == '4.47 as/rt(Hz)' assert str(FcorDivN) == '1.5 MHz' assert str(JdivF) == '18.2 as/rt(Hz)' assert str(FcorDivF) == '2 MHz' assert str(Jbuf) == '3.7 as/rt(Hz)' assert str(FcorBuf) == '2.5 MHz' assert str(Lvco) == '-125 dBc/Hz' assert str(FcorVCO) == '3 MHz' assert str(Lref) == '-110 dbc/Hz' assert str(FcorRef) == '0 Hz' assert str(FmaskLFcor) == '12 kHz' assert str(FmaskHFbound) == '5 MHz' assert str(plank) == '662.61e-36 J-s' assert str(boltz) == '13.806e-24 J/K' assert str(ec) == '160.22e-21 C' assert str(speed_of_light) == '299.79 Mm/s' assert str(zero_celsius) == '273.15 K' assert str(epsilon0) == '8.8542 pF/m' assert str(mu0) == '1.2566 uH/m' assert str(Z0) == '376.73 Ohms' ", https://github.com/kenkundert/engfmt,b908ef69aa7941e561019e24f969703f3234753b,test_quantity.py::test_number_recognition,test_quantity.py,NIO,flaky,Accepted,https://github.com/KenKundert/engfmt/pull/2,classify;root_cause;fix_proposal,"from engfmt import Quantity, set_preferences class Case: def __init__(self, name, text, raw, formatted, prefs=None): self.name = name self.text = text self.raw = raw self.formatted = formatted self.prefs = prefs test_cases = [ Case('grange', '0', ('0', ''), '0'), Case('waltz', '0s', ('0', 's'), '0s'), Case('allay', '0 s', ('0', 's'), '0s'), Case('tribute', '$0', ('0', '$'), '$0'), Case('lunatic', '1', ('1', ''), '1'), Case('seafront', '1s', ('1', 's'), '1s'), Case('birthday', '1 s', ('1', 's'), '1s'), Case('energy', '$1', ('1', '$'), '$1'), # test all the scale factors Case('quill', '1ys', ('1e-24', 's'), '1e-24s'), Case('joust', '1zs', ('1e-21', 's'), '1e-21s'), Case('streak', '1as', ('1e-18', 's'), '1as'), Case('mutiny', '1fs', ('1e-15', 's'), '1fs'), Case('banker', '1ps', ('1e-12', 's'), '1ps'), Case('conquer', '1ns', ('1e-9', 's'), '1ns'), Case('share', '1us', ('1e-6', 's'), '1us'), Case('witch', '1ms', ('1e-3', 's'), '1ms'), Case('finance', '1_s', ('1', 's'), '1s'), Case('ecologist', '1ks', ('1e3', 's'), '1ks'), Case('insulate', '1Ks', ('1e3', 's'), '1ks'), Case('apprehend', '1Ms', ('1e6', 's'), '1Ms'), Case('hoarding', '1Gs', ('1e9', 's'), '1Gs'), Case('scrum', '1Ts', ('1e12', 's'), '1Ts'), Case('tissue', '1Ps', ('1e15', 's'), '1e15s'), Case('panorama', '1Es', ('1e18', 's'), '1e18s'), Case('quest', '1Zs', ('1e21', 's'), '1e21s'), Case('suture', '1Ys', ('1e24', 's'), '1e24s'), # test various forms of the mantissa when using scale factors Case('delicacy', '1ns', ('1e-9', 's'), '1ns'), Case('huntsman', '1 ns', ('1e-9', 's'), '1ns'), Case('weighty', '10ns', ('10e-9', 's'), '10ns'), Case('madrigal', '100ns', ('100e-9', 's'), '100ns'), Case('comport', '.1ns', ('.1e-9', 's'), '100ps'), Case('character', '.1 ns', ('.1e-9', 's'), '100ps'), Case('sharpen', '.10ns', ('.10e-9', 's'), '100ps'), Case('resonate', '.100ns', ('.100e-9', 's'), '100ps'), Case('replica', '1.ns', None, '1ns'), Case('parachute', '1. ns', None, '1ns'), Case('merger', '10.ns', None, '10ns'), Case('grating', '100.ns', None, '100ns'), Case('enjoyment', '1.0ns', ('1.0e-9', 's'), '1ns'), Case('refit', '1.0 ns', ('1.0e-9', 's'), '1ns'), Case('thread', '10.0ns', ('10.0e-9', 's'), '10ns'), Case('upright', '100.0ns', ('100.0e-9', 's'), '100ns'), Case('inscribe', '1.00ns', ('1.00e-9', 's'), '1ns'), Case('warrior', '1.00 ns', ('1.00e-9', 's'), '1ns'), Case('paranoiac', '10.00ns', ('10.00e-9', 's'), '10ns'), Case('genie', '100.00ns', ('100.00e-9', 's'), '100ns'), Case('persimmon', '-1.00ns', ('-1.00e-9', 's'), '-1ns'), Case('barnacle', '-1.00 ns', ('-1.00e-9', 's'), '-1ns'), Case('dialog', '-10.00ns', ('-10.00e-9', 's'), '-10ns'), Case('bright', '-100.00ns', ('-100.00e-9', 's'), '-100ns'), Case('mutate', '+1.00ns', ('+1.00e-9', 's'), '1ns'), Case('session', '+1.00 ns', ('+1.00e-9', 's'), '1ns'), Case('capillary', '+10.00ns', ('+10.00e-9', 's'), '10ns'), Case('twinkle', '+100.00ns', ('+100.00e-9', 's'), '100ns'), # test various forms of the mantissa when using exponents Case('hairpiece', '1e-9s', ('1e-9', 's'), '1ns'), Case('marble', '10E-9s', ('10e-9', 's'), '10ns'), Case('boomerang', '100e-9s', ('100e-9', 's'), '100ns'), Case('antiquity', '.1e-9s', ('.1e-9', 's'), '100ps'), Case('redhead', '.10E-9s', ('.10e-9', 's'), '100ps'), Case('rarity', '.100e-9s', ('.100e-9', 's'), '100ps'), Case('latecomer', '1.e-9s', None, '1ns'), Case('blackball', '10.E-9s', None, '10ns'), Case('sweetener', '100.e-9s', None, '100ns'), Case('kidney', '1.0E-9s', ('1.0e-9', 's'), '1ns'), Case('erode', '10.0e-9s', ('10.0e-9', 's'), '10ns'), Case('omelet', '100.0E-9s', ('100.0e-9', 's'), '100ns'), Case('mealy', '1.00e-9s', ('1.00e-9', 's'), '1ns'), Case('chaser', '10.00E-9s', ('10.00e-9', 's'), '10ns'), Case('skitter', '100.00e-9s', ('100.00e-9', 's'), '100ns'), Case('romantic', '-1.00E-9s', ('-1.00e-9', 's'), '-1ns'), Case('bohemian', '-10.00e-9s', ('-10.00e-9', 's'), '-10ns'), Case('forbid', '-100.00E-9s', ('-100.00e-9', 's'), '-100ns'), Case('quartet', '+1.00e-9s', ('+1.00e-9', 's'), '1ns'), Case('presume', '+10.00E-9s', ('+10.00e-9', 's'), '10ns'), Case('trouper', '+100.00e-9s', ('+100.00e-9', 's'), '100ns'), Case('particle', '+.1E-9s', ('+.1e-9', 's'), '100ps'), Case('defeat', '+.10e-9s', ('+.10e-9', 's'), '100ps'), Case('oxcart', '+.100E-9s', ('+.100e-9', 's'), '100ps'), Case('creaky', '-.1e-9s', ('-.1e-9', 's'), '-100ps'), Case('gentleman', '-.10E-9s', ('-.10e-9', 's'), '-100ps'), Case('spangle', '-.100e-9s', ('-.100e-9', 's'), '-100ps'), # test various forms of the mantissa alone Case('educate', '100000.0s', ('100000.0', 's'), '100ks'), Case('headline', '100000 s', ('100000', 's'), '100ks'), Case('protein', '10000s', ('10000', 's'), '10ks'), Case('increase', '10000.0 s', ('10000.0', 's'), '10ks'), Case('response', '1000.0s', ('1000.0', 's'), '1ks'), Case('parodist', '1000 s', ('1000', 's'), '1ks'), Case('speck', '100s', ('100', 's'), '100s'), Case('chihuahua', '100.0 s', ('100.0', 's'), '100s'), Case('couch', '10.0s', ('10.0', 's'), '10s'), Case('highbrow', '10 s', ('10', 's'), '10s'), Case('haughty', '1s', ('1', 's'), '1s'), Case('break', '1.0 s', ('1.0', 's'), '1s'), Case('gutter', '0.1s', ('0.1', 's'), '100ms'), Case('ability', '0.1 s', ('0.1', 's'), '100ms'), Case('atone', '0.01s', ('0.01', 's'), '10ms'), Case('essential', '0.01 s', ('0.01', 's'), '10ms'), Case('godmother', '0.001s', ('0.001', 's'), '1ms'), Case('temper', '0.001 s', ('0.001', 's'), '1ms'), Case('verse', '0.0001s', ('0.0001', 's'), '100us'), Case('fifth', '0.0001 s', ('0.0001', 's'), '100us'), Case('horsewhip', '0.00001s', ('0.00001', 's'), '10us'), Case('larch', '0.00001 s', ('0.00001', 's'), '10us'), # test various forms of units Case('impute', '1ns', ('1e-9', 's'), '1ns'), Case('eyesore', '1e-9s', ('1e-9', 's'), '1ns'), Case('aspirant', '1n', ('1e-9', ''), '1n'), Case('delete', '1e-9', ('1e-9', ''), '1n'), Case('flummox', '1 ns', ('1e-9', 's'), '1ns'), Case('foster', '1 e-9 s', None, '1ns'), Case('deforest', '1n s', None, '1ns'), Case('fortune', '1e-9 s', ('1e-9', 's'), '1ns'), Case('starchy', '1nm/s', ('1e-9', 'm/s'), '1nm/s'), Case('preamble', '1e-9m/s', ('1e-9', 'm/s'), '1nm/s'), Case('haversack', '1 nm/s', ('1e-9', 'm/s'), '1nm/s'), Case('sprinter', '1e-9 m/s', ('1e-9', 'm/s'), '1nm/s'), Case('descend', '1nJ-s', ('1e-9', 'J-s'), '1nJ-s'), Case('milieu', '1e-9J-s', ('1e-9', 'J-s'), '1nJ-s'), Case('force', '1 nJ-s', ('1e-9', 'J-s'), '1nJ-s'), Case('athletic', '1e-9 J-s', ('1e-9', 'J-s'), '1nJ-s'), Case('scaffold', '1nm(s^-1)', ('1e-9', 'm(s^-1)'), '1nm(s^-1)'), Case('incur', '1e-9m(s^-1)', ('1e-9', 'm(s^-1)'), '1nm(s^-1)'), Case('hornet', '1 nm(s^-1)', ('1e-9', 'm(s^-1)'), '1nm(s^-1)'), Case('fledgling', '1e-9 m(s^-1)', ('1e-9', 'm(s^-1)'), '1nm(s^-1)'), Case('amnesty', '1nm/s^2', ('1e-9', 'm/s^2'), '1nm/s^2'), Case('carpet', '1e-9m/s^2', ('1e-9', 'm/s^2'), '1nm/s^2'), Case('intrigue', '1 nm/s^2', ('1e-9', 'm/s^2'), '1nm/s^2'), Case('picky', '1e-9 m/s^2', ('1e-9', 'm/s^2'), '1nm/s^2'), # test currency Case('bishop', '$10K', ('10e3', '$'), '$10k'), Case('colonnade', '$10', ('10', '$'), '$10'), Case('wizard', '$10.00', ('10.00', '$'), '$10'), Case('stork', '$10e9', ('10e9', '$'), '$10G'), Case('walkover', '$0.01', ('0.01', '$'), '$10m'), Case('kinswoman', '$.01', ('.01', '$'), '$10m'), Case('valuable', '$1.', None, '$1'), Case('kiddie', '-$10K', ('-10e3', '$'), '-$10k'), Case('breather', '-$10', ('-10', '$'), '-$10'), Case('recoil', '-$10.00', ('-10.00', '$'), '-$10'), Case('wrestle', '-$10e9', ('-10e9', '$'), '-$10G'), Case('theorist', '-$0.01', ('-0.01', '$'), '-$10m'), Case('neurone', '-$.01', ('-.01', '$'), '-$10m'), Case('crevice', '-$1.', None, '-$1'), Case('bodice', '+$10K', ('+10e3', '$'), '$10k'), Case('homicide', '+$10', ('+10', '$'), '$10'), Case('resurface', '+$10.00', ('+10.00', '$'), '$10'), Case('guidebook', '+$10e9', ('+10e9', '$'), '$10G'), Case('weaken', '+$0.01', ('+0.01', '$'), '$10m'), Case('subtlety', '+$.01', ('+.01', '$'), '$10m'), Case('flywheel', '+$1.', None, '$1'), # unusual numbers Case('sheathe', 'inf', ('inf', ''), 'inf'), Case('integrate', 'inf Hz', ('inf', 'Hz'), 'inf Hz'), Case('witter', '$inf', ('inf', '$'), '$inf'), Case('smoker', '-inf', ('-inf', ''), '-inf'), Case('spittoon', '-inf Hz', ('-inf', 'Hz'), '-inf Hz'), Case('outcome', '-$inf', ('-inf', '$'), '-$inf'), Case('baroness', 'nan', ('nan', ''), 'nan'), Case('province', 'nan Hz', ('nan', 'Hz'), 'nan Hz'), Case('infidel', '$nan', ('nan', '$'), '$nan'), Case('honey', '+nan', ('+nan', ''), 'nan'), Case('frighten', '+nan Hz', ('+nan', 'Hz'), 'nan Hz'), Case('acrobat', '+$nan', ('+nan', '$'), '$nan'), Case('firefly', 'INF', ('inf', ''), 'inf'), Case('farmland', 'INF Hz', ('inf', 'Hz'), 'inf Hz'), Case('osteopath', '$INF', ('inf', '$'), '$inf'), Case('chickpea', '-INF', ('-inf', ''), '-inf'), Case('bawdy', '-INF Hz', ('-inf', 'Hz'), '-inf Hz'), Case('pursuer', '-$INF', ('-inf', '$'), '-$inf'), Case('suffuse', 'NAN', ('nan', ''), 'nan'), Case('vacillate', 'NAN Hz', ('nan', 'Hz'), 'nan Hz'), Case('tangerine', '$NAN', ('nan', '$'), '$nan'), Case('southward', '+NAN', ('+nan', ''), 'nan'), Case('wander', '+NAN Hz', ('+nan', 'Hz'), 'nan Hz'), Case('stack', '+$NAN', ('+nan', '$'), '$nan'), # preferences Case('flotation', '1420.405751786 MHz', ('1420.405751786e6", https://github.com/kenkundert/engfmt,b908ef69aa7941e561019e24f969703f3234753b,test_text_processing.py::test_text_processing,test_text_processing.py,NIO,flaky,Accepted,https://github.com/KenKundert/engfmt/pull/3,classify;root_cause;fix_proposal,"from engfmt import all_to_eng_fmt, all_from_eng_fmt, set_preferences class Case: def __init__(self, name, eng, flt): self.name = name self.eng = eng self.flt = flt test_cases = [ Case('chirp', '1mA', '1e-3A'), Case('bungler', 'blah 1mA', 'blah 1e-3A'), Case('dilute', 'blah 1mA.', 'blah 1e-3A.'), Case('pelvis', 'blah 1mA blah', 'blah 1e-3A blah'), Case('tickle', '-1mA', '-1e-3A'), Case('wrangle', 'blah -1mA', 'blah -1e-3A'), Case('accessory', 'blah -1mA.', 'blah -1e-3A.'), Case('observer', 'blah -1mA blah', 'blah -1e-3A blah'), Case('meadow', 'blah ipn_250nA.', 'blah ipn_250nA.'), ] names = set() def test_text_processing(): set_preferences(spacer='', output=None) for case in test_cases: assert case.name not in names names.add(case.name) assert case.eng == all_to_eng_fmt(case.flt), case.name assert all_from_eng_fmt(case.eng) == case.flt, case.name ", https://github.com/kevinarpe/kevinarpe-rambutan3,5d7469912b559338dc15f5c7fb2abc91103ea0c5,tests/check_args/base/test_RNotNoneTypeMatcher.py::test_eq_ne_hash,tests/check_args/base/test_RNotNoneTypeMatcher.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import pytest from rambutan3 import RTestUtil from rambutan3.check_args.RCheckArgsError import RCheckArgsError from rambutan3.check_args.annotation.INT import INT from rambutan3.check_args.base.RNotNoneTypeMatcher import RNotNoneTypeMatcher def test_ctor(): RNotNoneTypeMatcher() with pytest.raises(TypeError): RNotNoneTypeMatcher(123) def test_check_arg(): __check_arg('abc', is_ok=True) __check_arg(123, is_ok=True) __check_arg(True, is_ok=True) __check_arg(123.456, is_ok=True) __check_arg([123], is_ok=True) __check_arg(None, is_ok=False) __check_arg([None], is_ok=True) def __check_arg(value, *, is_ok: bool): m = RNotNoneTypeMatcher() if is_ok: assert m.check_arg(value, 'dummy_arg_name') else: with pytest.raises(RCheckArgsError): m.check_arg(value, 'dummy_arg_name') def test_eq_ne_hash(): RTestUtil.test_eq_ne_hash(RNotNoneTypeMatcher(), RNotNoneTypeMatcher(), is_equal=True) RTestUtil.test_eq_ne_hash(RNotNoneTypeMatcher(), INT | RNotNoneTypeMatcher(), is_equal=False) def test__str__(): assert str(RNotNoneTypeMatcher()) == 'not None' ", https://github.com/kivio/pysllo,8bc4d17f9a668e9aff3c77f688f0c06ed2e7686b,tests/test_propagation_logger.py::test_forcing_level_by_dict,tests/test_propagation_logger.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest import logging def test_forcing_level_by_level_value(propagation_logger, handler): msg = ""TEST"" propagation_logger.setLevel(logging.INFO) propagation_logger.force_level(logging.DEBUG) propagation_logger.debug(msg) record = handler.pop() assert record.msg == msg assert record.levelname == logging.getLevelName(logging.DEBUG) with pytest.raises(IndexError): handler.pop() def test_forcing_level_by_level_name(propagation_logger, handler): msg = ""TEST"" propagation_logger.setLevel(logging.INFO) propagation_logger.force_level(logging.getLevelName(logging.DEBUG)) propagation_logger.debug(msg) record = handler.pop() assert record.msg == msg assert record.levelname == logging.getLevelName(logging.DEBUG) with pytest.raises(IndexError): handler.pop() def test_level_propagation(propagation_logger, handler): msg1 = ""TEST1"" msg2 = ""TEST2"" propagation_logger.set_propagation(True) propagation_logger.setLevel(logging.INFO) def test_second_level(): propagation_logger.debug(msg2) @propagation_logger.level_propagation(logging.DEBUG) def test_first_level(): propagation_logger.debug(msg1) test_second_level() test_first_level() test_second_level() record2 = handler.pop() record1 = handler.pop() assert record1.levelname == logging.getLevelName(logging.DEBUG) assert record2.levelname == logging.getLevelName(logging.DEBUG) with pytest.raises(IndexError): handler.pop() def test_reset_level(propagation_logger, handler): msg = ""TEST"" propagation_logger.setLevel(logging.INFO) propagation_logger.force_level(logging.getLevelName(logging.DEBUG)) propagation_logger.reset_level() propagation_logger.debug(msg) with pytest.raises(IndexError): handler.pop() def test_forcing_level_with_kwargs_by_level(propagation_logger, handler): msg1 = ""TEST1"" msg2 = ""TEST2"" propagation_logger.setLevel(logging.INFO) propagation_logger.force_level(logger_1=logging.DEBUG) additional_logger = logging.getLogger('logger_1') additional_logger.setLevel(logging.INFO) additional_logger.addHandler(handler) propagation_logger.debug(msg1) additional_logger.debug(msg2) record = handler.pop() assert record.msg == msg2 assert record.levelname == logging.getLevelName(logging.DEBUG) with pytest.raises(IndexError): handler.pop() def test_forcing_level_with_kwargs_by_level(propagation_logger, handler): msg1 = ""TEST1"" msg2 = ""TEST2"" propagation_logger.setLevel(logging.INFO) propagation_logger.force_level(logger_1=""DEBUG"") additional_logger = logging.getLogger('logger_1') additional_logger.setLevel(logging.INFO) additional_logger.addHandler(handler) propagation_logger.debug(msg1) additional_logger.debug(msg2) record = handler.pop() assert record.msg == msg2 assert record.levelname == logging.getLevelName(logging.DEBUG) with pytest.raises(IndexError): handler.pop() def test_forcing_level_by_dict(propagation_logger, handler): msg1 = ""TEST1"" msg2 = ""TEST2"" propagation_logger.setLevel(logging.INFO) levels = { 'logger_1': logging.DEBUG } propagation_logger.force_level(levels) additional_logger = logging.getLogger('logger_1') additional_logger.setLevel(logging.INFO) additional_logger.addHandler(handler) propagation_logger.debug(msg1) additional_logger.debug(msg2) record = handler.pop() assert record.msg == msg2 assert record.levelname == logging.getLevelName(logging.DEBUG) with pytest.raises(IndexError): handler.pop() def test_forcing_level_without_args_and_kwargs(propagation_logger, handler): with pytest.raises(TypeError) as exc_info: propagation_logger.force_level() assert '0 given' in str(exc_info.value) def test_forcing_level_with_too_many_args(propagation_logger, handler): with pytest.raises(TypeError) as exc_info: propagation_logger.force_level(logging.DEBUG, logging.DEBUG) assert '2 given' in str(exc_info.value) ", https://github.com/kivio/pysllo,8bc4d17f9a668e9aff3c77f688f0c06ed2e7686b,tests/test_propagation_logger.py::test_forcing_level_with_kwargs_by_level,tests/test_propagation_logger.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest import logging def test_forcing_level_by_level_value(propagation_logger, handler): msg = ""TEST"" propagation_logger.setLevel(logging.INFO) propagation_logger.force_level(logging.DEBUG) propagation_logger.debug(msg) record = handler.pop() assert record.msg == msg assert record.levelname == logging.getLevelName(logging.DEBUG) with pytest.raises(IndexError): handler.pop() def test_forcing_level_by_level_name(propagation_logger, handler): msg = ""TEST"" propagation_logger.setLevel(logging.INFO) propagation_logger.force_level(logging.getLevelName(logging.DEBUG)) propagation_logger.debug(msg) record = handler.pop() assert record.msg == msg assert record.levelname == logging.getLevelName(logging.DEBUG) with pytest.raises(IndexError): handler.pop() def test_level_propagation(propagation_logger, handler): msg1 = ""TEST1"" msg2 = ""TEST2"" propagation_logger.set_propagation(True) propagation_logger.setLevel(logging.INFO) def test_second_level(): propagation_logger.debug(msg2) @propagation_logger.level_propagation(logging.DEBUG) def test_first_level(): propagation_logger.debug(msg1) test_second_level() test_first_level() test_second_level() record2 = handler.pop() record1 = handler.pop() assert record1.levelname == logging.getLevelName(logging.DEBUG) assert record2.levelname == logging.getLevelName(logging.DEBUG) with pytest.raises(IndexError): handler.pop() def test_reset_level(propagation_logger, handler): msg = ""TEST"" propagation_logger.setLevel(logging.INFO) propagation_logger.force_level(logging.getLevelName(logging.DEBUG)) propagation_logger.reset_level() propagation_logger.debug(msg) with pytest.raises(IndexError): handler.pop() def test_forcing_level_with_kwargs_by_level(propagation_logger, handler): msg1 = ""TEST1"" msg2 = ""TEST2"" propagation_logger.setLevel(logging.INFO) propagation_logger.force_level(logger_1=logging.DEBUG) additional_logger = logging.getLogger('logger_1') additional_logger.setLevel(logging.INFO) additional_logger.addHandler(handler) propagation_logger.debug(msg1) additional_logger.debug(msg2) record = handler.pop() assert record.msg == msg2 assert record.levelname == logging.getLevelName(logging.DEBUG) with pytest.raises(IndexError): handler.pop() def test_forcing_level_with_kwargs_by_level(propagation_logger, handler): msg1 = ""TEST1"" msg2 = ""TEST2"" propagation_logger.setLevel(logging.INFO) propagation_logger.force_level(logger_1=""DEBUG"") additional_logger = logging.getLogger('logger_1') additional_logger.setLevel(logging.INFO) additional_logger.addHandler(handler) propagation_logger.debug(msg1) additional_logger.debug(msg2) record = handler.pop() assert record.msg == msg2 assert record.levelname == logging.getLevelName(logging.DEBUG) with pytest.raises(IndexError): handler.pop() def test_forcing_level_by_dict(propagation_logger, handler): msg1 = ""TEST1"" msg2 = ""TEST2"" propagation_logger.setLevel(logging.INFO) levels = { 'logger_1': logging.DEBUG } propagation_logger.force_level(levels) additional_logger = logging.getLogger('logger_1') additional_logger.setLevel(logging.INFO) additional_logger.addHandler(handler) propagation_logger.debug(msg1) additional_logger.debug(msg2) record = handler.pop() assert record.msg == msg2 assert record.levelname == logging.getLevelName(logging.DEBUG) with pytest.raises(IndexError): handler.pop() def test_forcing_level_without_args_and_kwargs(propagation_logger, handler): with pytest.raises(TypeError) as exc_info: propagation_logger.force_level() assert '0 given' in str(exc_info.value) def test_forcing_level_with_too_many_args(propagation_logger, handler): with pytest.raises(TypeError) as exc_info: propagation_logger.force_level(logging.DEBUG, logging.DEBUG) assert '2 given' in str(exc_info.value) ", https://github.com/klieret/RandomFileTree,aaaa975ae61afa6f0f5b481ebafcfa0f0216e471,randomfiletree/test/test_core.py::TestTreeCreation::test_create_random_dirs,randomfiletree/test/test_core.py,NOD,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python3 import unittest import tempfile import pathlib from randomfiletree.core import * import random random.seed(0) class TestHelperFunctions(unittest.TestCase): def setUp(self): self.itries = 10 def test_get_random_string(self): for i in range(self.itries): mi = random.randint(0, 10) ma = mi + random.randint(0, 10) ras = random_string(mi, ma) self.assertGreaterEqual(len(ras), mi) self.assertLessEqual(len(ras), ma) class TestTreeCreation(unittest.TestCase): def setUp(self): self.basedir = tempfile.TemporaryDirectory() def tearDown(self): self.basedir.cleanup() def get_content(self): alldirs = [] allfiles = [] for root, dirs, files in os.walk(self.basedir.name): for d in dirs: alldirs.append(os.path.join(root, d)) for file in files: allfiles.append(os.path.join(root, file)) return alldirs, allfiles def test_create_random_tree_empty(self): iterative_gaussian_tree(self.basedir.name, -10, -10, 3, None) dirs, files = self.get_content() self.assertEqual(len(dirs) + len(files), 0) def test_create_random_files(self): iterative_gaussian_tree(self.basedir.name, 5, -10, 3, None) dirs, files = self.get_content() self.assertEqual(len(dirs), 0) self.assertGreater(len(files), 1) def test_create_random_dirs(self): iterative_gaussian_tree(self.basedir.name, -10, 2, 3, None) dirs, files = self.get_content() self.assertEqual(len(files), 0) self.assertGreater(len(dirs), 1) def test_create_both(self): iterative_gaussian_tree(self.basedir.name, 3, 3, 3, None) dirs, files = self.get_content() self.assertGreater(len(files), 1) self.assertGreater(len(dirs), 1) def test_limit_depth(self): iterative_gaussian_tree(self.basedir.name, 3, 2, 5, maxdepth=3) dirs, files = self.get_content() max_depth = \ max(map(lambda x: x.count(os.sep), dirs)) - \ self.basedir.name.count(os.sep) self.assertLessEqual(max_depth, 4) def test_fname(self): suffix = '.jpg' iterative_gaussian_tree( self.basedir.name, 3, 2, 5, maxdepth=3, filename=lambda: random_string()+suffix ) _, files = self.get_content() for file in files: self.assertEqual(pathlib.Path(file).suffix, suffix) def test_payload(self): content = 'testtest' suffix = '.txt' def callback(target_dir: pathlib.Path) -> pathlib.Path: while True: path = target_dir / (random_string() + suffix) with path.open('w') as f: f.write(content) yield path iterative_gaussian_tree( self.basedir.name, 3, 2, 5, maxdepth=3, payload=callback ) _, files = self.get_content() for file in files: self.assertEqual(pathlib.Path(file).suffix, suffix) with open(file, 'r') as f: self.assertEqual(f.read(), content) class TestChooseSample(unittest.TestCase): def setUp(self): self.basedir = tempfile.TemporaryDirectory() def reset(self): self.basedir.cleanup() self.basedir = tempfile.TemporaryDirectory() def tearDown(self): self.basedir.cleanup() def test_sample(self): self.reset() iterative_gaussian_tree(self.basedir.name, 5, 5, 4) dirs, files = sample_random_elements(self.basedir.name, 2, 2) self.assertEqual(len(set(dirs)), 2) self.assertEqual(len(set(files)), 2) def test_sample_raises(self): self.reset() with self.assertRaises(ValueError): sample_random_elements(self.basedir.name, 2, 2) def test_sample_ignore(self): self.reset() dirs, files = sample_random_elements(self.basedir.name, 2, 2, ""ignore"") self.assertEqual(len(set(dirs)), 0) self.assertEqual(len(set(files)), 0) self.reset() os.mkdir(os.path.join(self.basedir.name, ""test"")) dirs, files = sample_random_elements(self.basedir.name, 2, 2, ""ignore"") self.assertEqual(len(set(dirs)), 1) self.assertEqual(len(set(files)), 0) def test_choose(self): self.reset() iterative_gaussian_tree(self.basedir.name, 5, 5, 3) dirs, files = choose_random_elements(self.basedir.name, 5, 3) self.assertEqual(len(dirs), 5) self.assertEqual(len(files), 3) def test_choose_raise(self): self.reset() with self.assertRaises(ValueError): # noinspection PyUnusedLocal dirs, files = choose_random_elements(self.basedir.name, 5, 3) def test_choose_ignore(self): self.reset() dirs, files = choose_random_elements(self.basedir.name, 2, 2, ""ignore"") self.assertEqual(len(dirs), 0) self.assertEqual(len(files), 0) if __name__ == ""__main__"": unittest.main() ", https://github.com/ktnrg45/pyps4-2ndscreen,457dc9c3f04017569d52bae7390ea2fc54e96ce5,tests/test_media_art.py::test_result_item_missing_data,tests/test_media_art.py,NIO,flaky,Opened,https://github.com/ktnrg45/pyps4-2ndscreen/pull/56,classify;root_cause,"""""""Tests for pyps4_2ndscreen.media_art."""""" import html import urllib import pytest import logging import asyncio from unittest.mock import MagicMock, patch from asynctest import CoroutineMock as mock_coro, Mock from pyps4_2ndscreen import media_art as media logging.basicConfig(level=logging.DEBUG) _LOGGER = logging.getLogger(__name__) MOCK_LANG = ""en"" MOCK_REGION = ""US/en"" MOCK_REGION_NAME = ""United States"" MOCK_URL = ( ""https://store.playstation.com/store/api/chihiro/00_09_000/container/"" ""US/en/19/UT0007-CUSA00129_00-NETFLIXPOLLUX001/1580172945000/image"" ) MOCK_TYPE = ""App"" MOCK_TITLE = ""Netflix"" MOCK_TITLE_ID = ""CUSA00129"" MOCK_FULL_ID = ""UT0007-CUSA00129_00-NETFLIXPOLLUX001-U099"" MOCK_DATA = { ""included"": [ { ""attributes"": { ""badge-info"": {""non-plus-user"": None, ""plus-user"": None}, ""cero-z-status"": {""is-allowed-in-cart"": False, ""is-on"": False}, ""content-rating"": { ""content-descriptors"": [], ""contentInteractiveElement"": [], ""rating-system"": """", ""url"": """", }, ""content-type"": ""1"", ""default-sku-id"": MOCK_FULL_ID, ""dob-required"": False, ""file-size"": {""unit"": ""MB"", ""value"": 26.21}, ""game-content-type"": MOCK_TYPE, ""genres"": [], ""is-igc-upsell"": False, ""is-multiplayer-upsell"": False, ""kamaji-relationship"": ""extras"", ""legal-text"": """", ""long-description"": ""Some extremely long description."", ""macross-brain-context"": ""game"", ""media-list"": { ""preview"": [], ""promo"": {""images"": [], ""videos"": []}, ""screenshots"": [{""url"": ""https://someimage.jpg""}], }, ""name"": ""Netflix"", ""nsx-confirm-message"": """", ""parent"": None, ""platforms"": [""PS4""], ""plus-reward-description"": None, ""primary-classification"": ""NON_GAME_RELATED"", ""provider-name"": ""Netflix"", ""ps-camera-compatibility"": ""incompatible"", ""ps-move-compatibility"": ""incompatible"", ""ps-vr-compatibility"": ""incompatible"", ""release-date"": ""2013-11-12T00:00:00Z"", ""secondary-classification"": ""APPLICATION"", ""skus"": [ { ""entitlements"": [{""duration"": 0, ""exp-after-first-use"": 0}], ""id"": ""UT0007-CUSA00129_00-NETFLIXPOLLUX001-U099"", ""is-preorder"": False, ""multibuy"": None, ""playability-date"": """", ""plus-reward-description"": None, ""prices"": { ""non-plus-user"": { ""actual-price"": {""display"": ""Free"", ""value"": 0}, ""availability"": {""end-date"": None, ""start-date"": None}, ""discount-percentage"": 0, ""is-plus"": False, ""strikethrough-price"": None, ""upsell-price"": None, }, ""plus-user"": { ""actual-price"": {""display"": ""Free"", ""value"": 0}, ""availability"": {""end-date"": None, ""start-date"": None}, ""discount-percentage"": 0, ""is-plus"": False, ""strikethrough-price"": None, ""upsell-price"": None, }, }, } ], ""star-rating"": {""score"": 4.49, ""total"": 435164}, ""subtitle-language-codes"": [], ""tertiary-classification"": ""NA"", ""thumbnail-url-base"": MOCK_URL, ""top-category"": ""application"", ""upsell-info"": None, ""voice-language-codes"": [], } } ] } MOCK_PARENT_NAME = ""Parent"" MOCK_PARENT_URL = ""https://parent_url.com/"" MOCK_PARENT = { ""name"": MOCK_PARENT_NAME, ""id"": MOCK_FULL_ID, ""url"": MOCK_PARENT_URL, } def test_get_pinned_item(): """"""Test Pinned Item retrieval."""""" title_id = next(iter(media.PINNED_TITLES)) title_data = media.PINNED_TITLES[title_id] result = media.get_pinned_item(title_data) assert result.name == title_data[""name""] assert result.game_type == title_data[""type""] assert result.cover_art == ""{}{}{}"".format( media.BASE_IMAGE_URL, title_data[""sku_id""], title_data[""url""] ) assert result.sku_id == title_id def test_get_region(): """"""Test region retrieval."""""" valid_region = media.get_region(next(iter(media.COUNTRIES))) assert valid_region in media.COUNTRIES.values() deprecated_region = media.get_region(next(iter(media.DEPRECATED_REGIONS))) assert deprecated_region in media.DEPRECATED_REGIONS.values() invalid_region = media.get_region(""Invalid"") assert invalid_region is None def test_get_lang(): """"""Test lang retrieval."""""" lang = media.get_lang(next(iter(media.COUNTRIES))) assert len(lang) == 2 assert lang.isalpha() def test_get_url(): """"""Test building of search url."""""" mock_input = ""Random Game's 2©: The-Title™"" # Test Tumbler url. result_url = media.get_ps_store_url(mock_input, MOCK_REGION, reformat=""tumbler"") assert ""tumbler-search"" in result_url[0] # Test no formatting. mock_final = urllib.parse.quote(html.escape(mock_input).encode()) result_url = media.get_ps_store_url( mock_input, MOCK_REGION, reformat=""orig"", legacy=True ) result_url = result_url[0].split(""/faceted-search/"") result_url = result_url[1].split(""?"") result_title = result_url[0] assert result_title == mock_final # Test chars only. mock_title = ""Random Games 2 TheTitle"" mock_final = urllib.parse.quote(html.escape(mock_title).encode()) result_url = media.get_ps_store_url( mock_input, MOCK_REGION, reformat=""chars"", legacy=True ) result_url = result_url[0].split(""/faceted-search/"") result_url = result_url[1].split(""?"") result_title = result_url[0] assert result_title == mock_final # Test chars with ""', -, :"". mock_title = ""Random Game's 2: The-Title"" mock_final = urllib.parse.quote(html.escape(mock_title).encode()) result_url = media.get_ps_store_url( mock_input, MOCK_REGION, reformat=""chars+"", legacy=True ) result_url = result_url[0].split(""/faceted-search/"") result_url = result_url[1].split(""?"") result_title = result_url[0] assert result_title == mock_final @pytest.mark.asyncio async def test_search_ps_store(): """"""Test PS store search."""""" with patch( ""pyps4_2ndscreen.media_art.fetch"", new=mock_coro(return_value=MOCK_DATA) ): result = await media.async_search_ps_store( MOCK_TITLE, MOCK_TITLE_ID, MOCK_REGION_NAME ) assert result.name == MOCK_TITLE assert MOCK_TITLE_ID in result.cover_art assert result.game_type == MOCK_TYPE assert result.sku_id == MOCK_TITLE_ID assert result.parent is None @pytest.mark.asyncio async def test_search_all(): """"""Test PS store search all."""""" with patch( ""pyps4_2ndscreen.media_art.fetch"", new=mock_coro(return_value=None) ), patch( ""pyps4_2ndscreen.media_art.async_get_ps_store_requests_tumbler"", new=mock_coro(return_value=[]), ) as mock_call: result = await media.async_search_ps_store( MOCK_TITLE, MOCK_TITLE_ID, MOCK_REGION_NAME, search_all=True ) assert result is None assert len(mock_call.mock_calls) > len(media.COUNTRIES) @pytest.mark.asyncio async def test_parse_errors(): """"""Test PS store search errors."""""" with patch( ""pyps4_2ndscreen.media_art.parse_data"", side_effect=(AttributeError, TypeError) ), pytest.raises(media.PSDataIncomplete): result = await media.async_search_ps_store( MOCK_TITLE, MOCK_TITLE_ID, MOCK_REGION_NAME ) assert result is None @pytest.mark.asyncio async def test_fetch(): """"""Test fetch coro."""""" session = Mock() mock_json = Mock() mock_json.json.return_value = asyncio.Future() mock_json.json.return_value.set_result(MOCK_DATA) session.get.return_value = asyncio.Future() session.get.return_value.set_result(mock_json) result = await media.fetch(MagicMock(), MagicMock(), session) assert result == MOCK_DATA @pytest.mark.asyncio async def test_fetch_errors(): """"""Test fetch errors."""""" session = Mock() mock_json = Mock() mock_json.json.return_value = asyncio.Future() mock_json.json.side_effect = ( media.asyncio.TimeoutError, media.aiohttp.client_exceptions.ContentTypeError, media.SSLError, ) session.get.return_value = asyncio.Future() session.get.return_value.set_result(mock_json) result = await media.fetch(MagicMock(), MagicMock(), session) assert result is None def test_parent_item(): """"""Test parent item is returned if present."""""" data = MOCK_DATA data[""included""][0][""attributes""][""parent""] = MOCK_PARENT result = media.parse_data(data, MOCK_TITLE_ID, MOCK_REGION_NAME) assert result.name == MOCK_PARENT_NAME assert result.sku_id == MOCK_TITLE_ID assert result.game_type == MOCK_TYPE assert result.cover_art.split(""/image"")[0] == MOCK_PARENT_URL def test_result_item_missing_data(): """"""Test Parsing with missing keys returns None."""""" data = MOCK_DATA _data = data[""included""][0][""attributes""] ", https://github.com/kytos/python-openflow,a3387a7b28d529a3605aa1506a028e03394e4526,tests/unit/v0x04/test_controller2switch/test_flow_mod.py::TestFlowMod::test_minimum_size,tests/unit/v0x04/test_controller2switch/test_flow_mod.py,OD-Brit,flaky,RepoArchived,nan,classify;root_cause,"""""""FlowMod test."""""" from pyof.v0x04.common.action import ActionOutput, ListOfActions from pyof.v0x04.common.flow_instructions import ( InstructionApplyAction, ListOfInstruction) from pyof.v0x04.common.flow_match import ( Match, MatchType, OxmClass, OxmOfbMatchField, OxmTLV) from pyof.v0x04.common.port import PortNo from pyof.v0x04.controller2switch.flow_mod import FlowMod, FlowModCommand from tests.unit.test_struct import TestStruct class TestFlowMod(TestStruct): """"""FlowMod test."""""" def test_min_size(self): """"""Test struct minimum size."""""" super().set_raw_dump_file('v0x04', 'ofpt_flow_mod') super().set_raw_dump_object(FlowMod, xid=2219910763, command=FlowModCommand.OFPFC_ADD, priority=1000, match=_new_match(), instructions=_new_list_of_instructions()) super().set_minimum_size(56) def _new_match(): """"""Crate new Match instance."""""" oxm_tlv1 = OxmTLV(oxm_class=OxmClass.OFPXMC_OPENFLOW_BASIC, oxm_field=OxmOfbMatchField.OFPXMT_OFB_ETH_TYPE, oxm_hasmask=False, oxm_value=b'\x88\xcc') oxmt_lv2 = OxmTLV(oxm_class=OxmClass.OFPXMC_OPENFLOW_BASIC, oxm_field=OxmOfbMatchField.OFPXMT_OFB_VLAN_VID, oxm_hasmask=False, oxm_value=b'\x1e\xd7') return Match(match_type=MatchType.OFPMT_OXM, oxm_match_fields=[oxm_tlv1, oxmt_lv2]) def _new_list_of_instructions(): """"""Crate new ListOfInstruction."""""" output = ActionOutput(port=PortNo.OFPP_CONTROLLER) loa = ListOfActions([output]) instruction = InstructionApplyAction(loa) return ListOfInstruction([instruction]) ", https://github.com/laike9m/pdir2,ef5e2200074d68e2ca3df4ee441a1b9ea6ca5abc,tests/test_user_config.py::test_invalid_config_1,tests/test_user_config.py,OD-Brit,flaky,nan,nan,classify;root_cause,""""""" Test user configuration. """""" import os import shutil from sys import modules import pytest from pdir.color import COLORS @pytest.fixture def clean(): DEFAULT_CONFIG_FILE = os.path.join(os.path.expanduser('~'), '.pdir2config') yield DEFAULT_CONFIG_FILE if 'PDIR2_CONFIG_FILE' in os.environ: if os.path.exists(os.environ['PDIR2_CONFIG_FILE']): os.remove(os.environ['PDIR2_CONFIG_FILE']) del os.environ['PDIR2_CONFIG_FILE'] if os.path.exists(DEFAULT_CONFIG_FILE): os.remove(DEFAULT_CONFIG_FILE) for module in ( 'pdir', 'pdir.api', 'pdir.constants', 'pdir.format', 'pdir.configuration', 'pdir.color', 'pdir.utils', ): try: del modules[module] except KeyError: pass def test_default_env_without_config(clean): import pdir pdir() def test_set_env_without_config(clean): os.environ['PDIR2_CONFIG_FILE'] = 'aaa' with pytest.raises(OSError, match='Config file not exist: aaa'): import pdir pdir() def test_read_config(clean): # 'clean' is the DEFAULT_CONFIG_FILE yielded from fixture. shutil.copyfile('tests/data/config_1.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['bright yellow'] assert comma == '\033[1;32m, \033[0m' assert attribute_color == COLORS['cyan'] def test_read_config_from_custom_location(clean): os.environ['PDIR2_CONFIG_FILE'] = os.path.join(os.path.expanduser('~'), '.myconfig') shutil.copyfile('tests/data/config_1.ini', os.environ['PDIR2_CONFIG_FILE']) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['bright yellow'] assert comma == '\033[1;32m, \033[0m' assert attribute_color == COLORS['cyan'] def test_uniform_color(clean): shutil.copyfile('tests/data/config_2.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['white'] assert comma == '\033[0;37m, \033[0m' assert attribute_color == COLORS['white'] def test_empty_config(clean): shutil.copyfile('tests/data/empty_config.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['grey'] assert category_color == COLORS['yellow'] assert comma == '\033[1;30m, \033[0m' assert attribute_color == COLORS['cyan'] def test_invalid_config_1(clean): shutil.copyfile('tests/data/error_config_1.ini', clean) with pytest.raises(ValueError, match='Invalid key: doc-color1'): import pdir pdir() def test_invalid_config_2(clean): shutil.copyfile('tests/data/error_config_2.ini', clean) with pytest.raises(ValueError, match='Invalid color value: 42'): import pdir pdir() ", https://github.com/laike9m/pdir2,ef5e2200074d68e2ca3df4ee441a1b9ea6ca5abc,tests/test_user_config.py::test_invalid_config_2,tests/test_user_config.py,OD-Brit,flaky,nan,nan,classify;root_cause,""""""" Test user configuration. """""" import os import shutil from sys import modules import pytest from pdir.color import COLORS @pytest.fixture def clean(): DEFAULT_CONFIG_FILE = os.path.join(os.path.expanduser('~'), '.pdir2config') yield DEFAULT_CONFIG_FILE if 'PDIR2_CONFIG_FILE' in os.environ: if os.path.exists(os.environ['PDIR2_CONFIG_FILE']): os.remove(os.environ['PDIR2_CONFIG_FILE']) del os.environ['PDIR2_CONFIG_FILE'] if os.path.exists(DEFAULT_CONFIG_FILE): os.remove(DEFAULT_CONFIG_FILE) for module in ( 'pdir', 'pdir.api', 'pdir.constants', 'pdir.format', 'pdir.configuration', 'pdir.color', 'pdir.utils', ): try: del modules[module] except KeyError: pass def test_default_env_without_config(clean): import pdir pdir() def test_set_env_without_config(clean): os.environ['PDIR2_CONFIG_FILE'] = 'aaa' with pytest.raises(OSError, match='Config file not exist: aaa'): import pdir pdir() def test_read_config(clean): # 'clean' is the DEFAULT_CONFIG_FILE yielded from fixture. shutil.copyfile('tests/data/config_1.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['bright yellow'] assert comma == '\033[1;32m, \033[0m' assert attribute_color == COLORS['cyan'] def test_read_config_from_custom_location(clean): os.environ['PDIR2_CONFIG_FILE'] = os.path.join(os.path.expanduser('~'), '.myconfig') shutil.copyfile('tests/data/config_1.ini', os.environ['PDIR2_CONFIG_FILE']) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['bright yellow'] assert comma == '\033[1;32m, \033[0m' assert attribute_color == COLORS['cyan'] def test_uniform_color(clean): shutil.copyfile('tests/data/config_2.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['white'] assert comma == '\033[0;37m, \033[0m' assert attribute_color == COLORS['white'] def test_empty_config(clean): shutil.copyfile('tests/data/empty_config.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['grey'] assert category_color == COLORS['yellow'] assert comma == '\033[1;30m, \033[0m' assert attribute_color == COLORS['cyan'] def test_invalid_config_1(clean): shutil.copyfile('tests/data/error_config_1.ini', clean) with pytest.raises(ValueError, match='Invalid key: doc-color1'): import pdir pdir() def test_invalid_config_2(clean): shutil.copyfile('tests/data/error_config_2.ini', clean) with pytest.raises(ValueError, match='Invalid color value: 42'): import pdir pdir() ", https://github.com/laike9m/pdir2,ef5e2200074d68e2ca3df4ee441a1b9ea6ca5abc,tests/test_user_config.py::test_read_config,tests/test_user_config.py,OD-Brit,flaky,nan,nan,classify;root_cause,""""""" Test user configuration. """""" import os import shutil from sys import modules import pytest from pdir.color import COLORS @pytest.fixture def clean(): DEFAULT_CONFIG_FILE = os.path.join(os.path.expanduser('~'), '.pdir2config') yield DEFAULT_CONFIG_FILE if 'PDIR2_CONFIG_FILE' in os.environ: if os.path.exists(os.environ['PDIR2_CONFIG_FILE']): os.remove(os.environ['PDIR2_CONFIG_FILE']) del os.environ['PDIR2_CONFIG_FILE'] if os.path.exists(DEFAULT_CONFIG_FILE): os.remove(DEFAULT_CONFIG_FILE) for module in ( 'pdir', 'pdir.api', 'pdir.constants', 'pdir.format', 'pdir.configuration', 'pdir.color', 'pdir.utils', ): try: del modules[module] except KeyError: pass def test_default_env_without_config(clean): import pdir pdir() def test_set_env_without_config(clean): os.environ['PDIR2_CONFIG_FILE'] = 'aaa' with pytest.raises(OSError, match='Config file not exist: aaa'): import pdir pdir() def test_read_config(clean): # 'clean' is the DEFAULT_CONFIG_FILE yielded from fixture. shutil.copyfile('tests/data/config_1.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['bright yellow'] assert comma == '\033[1;32m, \033[0m' assert attribute_color == COLORS['cyan'] def test_read_config_from_custom_location(clean): os.environ['PDIR2_CONFIG_FILE'] = os.path.join(os.path.expanduser('~'), '.myconfig') shutil.copyfile('tests/data/config_1.ini', os.environ['PDIR2_CONFIG_FILE']) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['bright yellow'] assert comma == '\033[1;32m, \033[0m' assert attribute_color == COLORS['cyan'] def test_uniform_color(clean): shutil.copyfile('tests/data/config_2.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['white'] assert comma == '\033[0;37m, \033[0m' assert attribute_color == COLORS['white'] def test_empty_config(clean): shutil.copyfile('tests/data/empty_config.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['grey'] assert category_color == COLORS['yellow'] assert comma == '\033[1;30m, \033[0m' assert attribute_color == COLORS['cyan'] def test_invalid_config_1(clean): shutil.copyfile('tests/data/error_config_1.ini', clean) with pytest.raises(ValueError, match='Invalid key: doc-color1'): import pdir pdir() def test_invalid_config_2(clean): shutil.copyfile('tests/data/error_config_2.ini', clean) with pytest.raises(ValueError, match='Invalid color value: 42'): import pdir pdir() ", https://github.com/laike9m/pdir2,ef5e2200074d68e2ca3df4ee441a1b9ea6ca5abc,tests/test_user_config.py::test_read_config_from_custom_location,tests/test_user_config.py,OD-Brit,flaky,nan,nan,classify;root_cause,""""""" Test user configuration. """""" import os import shutil from sys import modules import pytest from pdir.color import COLORS @pytest.fixture def clean(): DEFAULT_CONFIG_FILE = os.path.join(os.path.expanduser('~'), '.pdir2config') yield DEFAULT_CONFIG_FILE if 'PDIR2_CONFIG_FILE' in os.environ: if os.path.exists(os.environ['PDIR2_CONFIG_FILE']): os.remove(os.environ['PDIR2_CONFIG_FILE']) del os.environ['PDIR2_CONFIG_FILE'] if os.path.exists(DEFAULT_CONFIG_FILE): os.remove(DEFAULT_CONFIG_FILE) for module in ( 'pdir', 'pdir.api', 'pdir.constants', 'pdir.format', 'pdir.configuration', 'pdir.color', 'pdir.utils', ): try: del modules[module] except KeyError: pass def test_default_env_without_config(clean): import pdir pdir() def test_set_env_without_config(clean): os.environ['PDIR2_CONFIG_FILE'] = 'aaa' with pytest.raises(OSError, match='Config file not exist: aaa'): import pdir pdir() def test_read_config(clean): # 'clean' is the DEFAULT_CONFIG_FILE yielded from fixture. shutil.copyfile('tests/data/config_1.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['bright yellow'] assert comma == '\033[1;32m, \033[0m' assert attribute_color == COLORS['cyan'] def test_read_config_from_custom_location(clean): os.environ['PDIR2_CONFIG_FILE'] = os.path.join(os.path.expanduser('~'), '.myconfig') shutil.copyfile('tests/data/config_1.ini', os.environ['PDIR2_CONFIG_FILE']) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['bright yellow'] assert comma == '\033[1;32m, \033[0m' assert attribute_color == COLORS['cyan'] def test_uniform_color(clean): shutil.copyfile('tests/data/config_2.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['white'] assert comma == '\033[0;37m, \033[0m' assert attribute_color == COLORS['white'] def test_empty_config(clean): shutil.copyfile('tests/data/empty_config.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['grey'] assert category_color == COLORS['yellow'] assert comma == '\033[1;30m, \033[0m' assert attribute_color == COLORS['cyan'] def test_invalid_config_1(clean): shutil.copyfile('tests/data/error_config_1.ini', clean) with pytest.raises(ValueError, match='Invalid key: doc-color1'): import pdir pdir() def test_invalid_config_2(clean): shutil.copyfile('tests/data/error_config_2.ini', clean) with pytest.raises(ValueError, match='Invalid color value: 42'): import pdir pdir() ", https://github.com/laike9m/pdir2,ef5e2200074d68e2ca3df4ee441a1b9ea6ca5abc,tests/test_user_config.py::test_set_env_without_config,tests/test_user_config.py,OD-Brit,flaky,nan,nan,classify;root_cause,""""""" Test user configuration. """""" import os import shutil from sys import modules import pytest from pdir.color import COLORS @pytest.fixture def clean(): DEFAULT_CONFIG_FILE = os.path.join(os.path.expanduser('~'), '.pdir2config') yield DEFAULT_CONFIG_FILE if 'PDIR2_CONFIG_FILE' in os.environ: if os.path.exists(os.environ['PDIR2_CONFIG_FILE']): os.remove(os.environ['PDIR2_CONFIG_FILE']) del os.environ['PDIR2_CONFIG_FILE'] if os.path.exists(DEFAULT_CONFIG_FILE): os.remove(DEFAULT_CONFIG_FILE) for module in ( 'pdir', 'pdir.api', 'pdir.constants', 'pdir.format', 'pdir.configuration', 'pdir.color', 'pdir.utils', ): try: del modules[module] except KeyError: pass def test_default_env_without_config(clean): import pdir pdir() def test_set_env_without_config(clean): os.environ['PDIR2_CONFIG_FILE'] = 'aaa' with pytest.raises(OSError, match='Config file not exist: aaa'): import pdir pdir() def test_read_config(clean): # 'clean' is the DEFAULT_CONFIG_FILE yielded from fixture. shutil.copyfile('tests/data/config_1.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['bright yellow'] assert comma == '\033[1;32m, \033[0m' assert attribute_color == COLORS['cyan'] def test_read_config_from_custom_location(clean): os.environ['PDIR2_CONFIG_FILE'] = os.path.join(os.path.expanduser('~'), '.myconfig') shutil.copyfile('tests/data/config_1.ini', os.environ['PDIR2_CONFIG_FILE']) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['bright yellow'] assert comma == '\033[1;32m, \033[0m' assert attribute_color == COLORS['cyan'] def test_uniform_color(clean): shutil.copyfile('tests/data/config_2.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['white'] assert comma == '\033[0;37m, \033[0m' assert attribute_color == COLORS['white'] def test_empty_config(clean): shutil.copyfile('tests/data/empty_config.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['grey'] assert category_color == COLORS['yellow'] assert comma == '\033[1;30m, \033[0m' assert attribute_color == COLORS['cyan'] def test_invalid_config_1(clean): shutil.copyfile('tests/data/error_config_1.ini', clean) with pytest.raises(ValueError, match='Invalid key: doc-color1'): import pdir pdir() def test_invalid_config_2(clean): shutil.copyfile('tests/data/error_config_2.ini', clean) with pytest.raises(ValueError, match='Invalid color value: 42'): import pdir pdir() ", https://github.com/laike9m/pdir2,ef5e2200074d68e2ca3df4ee441a1b9ea6ca5abc,tests/test_user_config.py::test_uniform_color,tests/test_user_config.py,OD-Brit,flaky,nan,nan,classify;root_cause,""""""" Test user configuration. """""" import os import shutil from sys import modules import pytest from pdir.color import COLORS @pytest.fixture def clean(): DEFAULT_CONFIG_FILE = os.path.join(os.path.expanduser('~'), '.pdir2config') yield DEFAULT_CONFIG_FILE if 'PDIR2_CONFIG_FILE' in os.environ: if os.path.exists(os.environ['PDIR2_CONFIG_FILE']): os.remove(os.environ['PDIR2_CONFIG_FILE']) del os.environ['PDIR2_CONFIG_FILE'] if os.path.exists(DEFAULT_CONFIG_FILE): os.remove(DEFAULT_CONFIG_FILE) for module in ( 'pdir', 'pdir.api', 'pdir.constants', 'pdir.format', 'pdir.configuration', 'pdir.color', 'pdir.utils', ): try: del modules[module] except KeyError: pass def test_default_env_without_config(clean): import pdir pdir() def test_set_env_without_config(clean): os.environ['PDIR2_CONFIG_FILE'] = 'aaa' with pytest.raises(OSError, match='Config file not exist: aaa'): import pdir pdir() def test_read_config(clean): # 'clean' is the DEFAULT_CONFIG_FILE yielded from fixture. shutil.copyfile('tests/data/config_1.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['bright yellow'] assert comma == '\033[1;32m, \033[0m' assert attribute_color == COLORS['cyan'] def test_read_config_from_custom_location(clean): os.environ['PDIR2_CONFIG_FILE'] = os.path.join(os.path.expanduser('~'), '.myconfig') shutil.copyfile('tests/data/config_1.ini', os.environ['PDIR2_CONFIG_FILE']) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['bright yellow'] assert comma == '\033[1;32m, \033[0m' assert attribute_color == COLORS['cyan'] def test_uniform_color(clean): shutil.copyfile('tests/data/config_2.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['white'] assert category_color == COLORS['white'] assert comma == '\033[0;37m, \033[0m' assert attribute_color == COLORS['white'] def test_empty_config(clean): shutil.copyfile('tests/data/empty_config.ini', clean) from pdir.format import doc_color, category_color, attribute_color, comma assert doc_color == COLORS['grey'] assert category_color == COLORS['yellow'] assert comma == '\033[1;30m, \033[0m' assert attribute_color == COLORS['cyan'] def test_invalid_config_1(clean): shutil.copyfile('tests/data/error_config_1.ini', clean) with pytest.raises(ValueError, match='Invalid key: doc-color1'): import pdir pdir() def test_invalid_config_2(clean): shutil.copyfile('tests/data/error_config_2.ini', clean) with pytest.raises(ValueError, match='Invalid color value: 42'): import pdir pdir() ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_experiment.py::test_counts,tunacell/tests/test_experiment.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python2 # -*- coding: utf-8 -*- """""" Testing Experiment features. """""" from __future__ import print_function import pytest import os import shutil import tunacell from tunacell.base.experiment import Experiment from tunacell.base.container import Container path_data = os.path.join(os.path.dirname(tunacell.__file__), 'data') path_fake_exp = os.path.join(path_data, 'fake') @pytest.fixture(scope='module') def fake_exp(): exp = Experiment(path_fake_exp, count_items=True) yield exp analysis_path = os.path.join(path_fake_exp, 'analysis') if os.path.exists(analysis_path): shutil.rmtree(analysis_path) def test_load_experiment(fake_exp): assert isinstance(fake_exp, Experiment) def test_experiment_attributes(fake_exp): attrs = ['label', 'abspath', 'metadata', 'datatype', 'filetype', 'containers'] for attr in attrs: assert hasattr(fake_exp, attr) def test_experiment_label(fake_exp): assert fake_exp.label == 'fake' def test_experiment_abspath(fake_exp): assert fake_exp.abspath == path_fake_exp def test_experiment_number_container(fake_exp): assert len(fake_exp.containers) == 4 def test_experiment_metadata(fake_exp): assert fake_exp.metadata['author'] == 'Joachim Rambeau' def test_experiment_datatype(fake_exp): assert len(fake_exp.datatype) == 4 assert 'cellID' in list(zip(*fake_exp.datatype))[0] def test_experiment_iter(fake_exp): meths = ['iter_containers'] for meth in meths: method = getattr(fake_exp, meth) assert callable(method) def test_experiment_get_container(fake_exp): container = fake_exp.get_container('container_01') assert isinstance(container, Container) def test_counts(fake_exp): counts = fake_exp._counts assert counts['containers'] == 4 assert counts['cells'] == 24 assert counts['colonies'] == 4 assert counts['lineages'] == 12 # number of leaves when no filter is applied ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_experiment.py::test_experiment_abspath,tunacell/tests/test_experiment.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python2 # -*- coding: utf-8 -*- """""" Testing Experiment features. """""" from __future__ import print_function import pytest import os import shutil import tunacell from tunacell.base.experiment import Experiment from tunacell.base.container import Container path_data = os.path.join(os.path.dirname(tunacell.__file__), 'data') path_fake_exp = os.path.join(path_data, 'fake') @pytest.fixture(scope='module') def fake_exp(): exp = Experiment(path_fake_exp, count_items=True) yield exp analysis_path = os.path.join(path_fake_exp, 'analysis') if os.path.exists(analysis_path): shutil.rmtree(analysis_path) def test_load_experiment(fake_exp): assert isinstance(fake_exp, Experiment) def test_experiment_attributes(fake_exp): attrs = ['label', 'abspath', 'metadata', 'datatype', 'filetype', 'containers'] for attr in attrs: assert hasattr(fake_exp, attr) def test_experiment_label(fake_exp): assert fake_exp.label == 'fake' def test_experiment_abspath(fake_exp): assert fake_exp.abspath == path_fake_exp def test_experiment_number_container(fake_exp): assert len(fake_exp.containers) == 4 def test_experiment_metadata(fake_exp): assert fake_exp.metadata['author'] == 'Joachim Rambeau' def test_experiment_datatype(fake_exp): assert len(fake_exp.datatype) == 4 assert 'cellID' in list(zip(*fake_exp.datatype))[0] def test_experiment_iter(fake_exp): meths = ['iter_containers'] for meth in meths: method = getattr(fake_exp, meth) assert callable(method) def test_experiment_get_container(fake_exp): container = fake_exp.get_container('container_01') assert isinstance(container, Container) def test_counts(fake_exp): counts = fake_exp._counts assert counts['containers'] == 4 assert counts['cells'] == 24 assert counts['colonies'] == 4 assert counts['lineages'] == 12 # number of leaves when no filter is applied ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_experiment.py::test_experiment_attributes,tunacell/tests/test_experiment.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python2 # -*- coding: utf-8 -*- """""" Testing Experiment features. """""" from __future__ import print_function import pytest import os import shutil import tunacell from tunacell.base.experiment import Experiment from tunacell.base.container import Container path_data = os.path.join(os.path.dirname(tunacell.__file__), 'data') path_fake_exp = os.path.join(path_data, 'fake') @pytest.fixture(scope='module') def fake_exp(): exp = Experiment(path_fake_exp, count_items=True) yield exp analysis_path = os.path.join(path_fake_exp, 'analysis') if os.path.exists(analysis_path): shutil.rmtree(analysis_path) def test_load_experiment(fake_exp): assert isinstance(fake_exp, Experiment) def test_experiment_attributes(fake_exp): attrs = ['label', 'abspath', 'metadata', 'datatype', 'filetype', 'containers'] for attr in attrs: assert hasattr(fake_exp, attr) def test_experiment_label(fake_exp): assert fake_exp.label == 'fake' def test_experiment_abspath(fake_exp): assert fake_exp.abspath == path_fake_exp def test_experiment_number_container(fake_exp): assert len(fake_exp.containers) == 4 def test_experiment_metadata(fake_exp): assert fake_exp.metadata['author'] == 'Joachim Rambeau' def test_experiment_datatype(fake_exp): assert len(fake_exp.datatype) == 4 assert 'cellID' in list(zip(*fake_exp.datatype))[0] def test_experiment_iter(fake_exp): meths = ['iter_containers'] for meth in meths: method = getattr(fake_exp, meth) assert callable(method) def test_experiment_get_container(fake_exp): container = fake_exp.get_container('container_01') assert isinstance(container, Container) def test_counts(fake_exp): counts = fake_exp._counts assert counts['containers'] == 4 assert counts['cells'] == 24 assert counts['colonies'] == 4 assert counts['lineages'] == 12 # number of leaves when no filter is applied ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_experiment.py::test_experiment_datatype,tunacell/tests/test_experiment.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python2 # -*- coding: utf-8 -*- """""" Testing Experiment features. """""" from __future__ import print_function import pytest import os import shutil import tunacell from tunacell.base.experiment import Experiment from tunacell.base.container import Container path_data = os.path.join(os.path.dirname(tunacell.__file__), 'data') path_fake_exp = os.path.join(path_data, 'fake') @pytest.fixture(scope='module') def fake_exp(): exp = Experiment(path_fake_exp, count_items=True) yield exp analysis_path = os.path.join(path_fake_exp, 'analysis') if os.path.exists(analysis_path): shutil.rmtree(analysis_path) def test_load_experiment(fake_exp): assert isinstance(fake_exp, Experiment) def test_experiment_attributes(fake_exp): attrs = ['label', 'abspath', 'metadata', 'datatype', 'filetype', 'containers'] for attr in attrs: assert hasattr(fake_exp, attr) def test_experiment_label(fake_exp): assert fake_exp.label == 'fake' def test_experiment_abspath(fake_exp): assert fake_exp.abspath == path_fake_exp def test_experiment_number_container(fake_exp): assert len(fake_exp.containers) == 4 def test_experiment_metadata(fake_exp): assert fake_exp.metadata['author'] == 'Joachim Rambeau' def test_experiment_datatype(fake_exp): assert len(fake_exp.datatype) == 4 assert 'cellID' in list(zip(*fake_exp.datatype))[0] def test_experiment_iter(fake_exp): meths = ['iter_containers'] for meth in meths: method = getattr(fake_exp, meth) assert callable(method) def test_experiment_get_container(fake_exp): container = fake_exp.get_container('container_01') assert isinstance(container, Container) def test_counts(fake_exp): counts = fake_exp._counts assert counts['containers'] == 4 assert counts['cells'] == 24 assert counts['colonies'] == 4 assert counts['lineages'] == 12 # number of leaves when no filter is applied ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_experiment.py::test_experiment_get_container,tunacell/tests/test_experiment.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python2 # -*- coding: utf-8 -*- """""" Testing Experiment features. """""" from __future__ import print_function import pytest import os import shutil import tunacell from tunacell.base.experiment import Experiment from tunacell.base.container import Container path_data = os.path.join(os.path.dirname(tunacell.__file__), 'data') path_fake_exp = os.path.join(path_data, 'fake') @pytest.fixture(scope='module') def fake_exp(): exp = Experiment(path_fake_exp, count_items=True) yield exp analysis_path = os.path.join(path_fake_exp, 'analysis') if os.path.exists(analysis_path): shutil.rmtree(analysis_path) def test_load_experiment(fake_exp): assert isinstance(fake_exp, Experiment) def test_experiment_attributes(fake_exp): attrs = ['label', 'abspath', 'metadata', 'datatype', 'filetype', 'containers'] for attr in attrs: assert hasattr(fake_exp, attr) def test_experiment_label(fake_exp): assert fake_exp.label == 'fake' def test_experiment_abspath(fake_exp): assert fake_exp.abspath == path_fake_exp def test_experiment_number_container(fake_exp): assert len(fake_exp.containers) == 4 def test_experiment_metadata(fake_exp): assert fake_exp.metadata['author'] == 'Joachim Rambeau' def test_experiment_datatype(fake_exp): assert len(fake_exp.datatype) == 4 assert 'cellID' in list(zip(*fake_exp.datatype))[0] def test_experiment_iter(fake_exp): meths = ['iter_containers'] for meth in meths: method = getattr(fake_exp, meth) assert callable(method) def test_experiment_get_container(fake_exp): container = fake_exp.get_container('container_01') assert isinstance(container, Container) def test_counts(fake_exp): counts = fake_exp._counts assert counts['containers'] == 4 assert counts['cells'] == 24 assert counts['colonies'] == 4 assert counts['lineages'] == 12 # number of leaves when no filter is applied ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_experiment.py::test_experiment_iter,tunacell/tests/test_experiment.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python2 # -*- coding: utf-8 -*- """""" Testing Experiment features. """""" from __future__ import print_function import pytest import os import shutil import tunacell from tunacell.base.experiment import Experiment from tunacell.base.container import Container path_data = os.path.join(os.path.dirname(tunacell.__file__), 'data') path_fake_exp = os.path.join(path_data, 'fake') @pytest.fixture(scope='module') def fake_exp(): exp = Experiment(path_fake_exp, count_items=True) yield exp analysis_path = os.path.join(path_fake_exp, 'analysis') if os.path.exists(analysis_path): shutil.rmtree(analysis_path) def test_load_experiment(fake_exp): assert isinstance(fake_exp, Experiment) def test_experiment_attributes(fake_exp): attrs = ['label', 'abspath', 'metadata', 'datatype', 'filetype', 'containers'] for attr in attrs: assert hasattr(fake_exp, attr) def test_experiment_label(fake_exp): assert fake_exp.label == 'fake' def test_experiment_abspath(fake_exp): assert fake_exp.abspath == path_fake_exp def test_experiment_number_container(fake_exp): assert len(fake_exp.containers) == 4 def test_experiment_metadata(fake_exp): assert fake_exp.metadata['author'] == 'Joachim Rambeau' def test_experiment_datatype(fake_exp): assert len(fake_exp.datatype) == 4 assert 'cellID' in list(zip(*fake_exp.datatype))[0] def test_experiment_iter(fake_exp): meths = ['iter_containers'] for meth in meths: method = getattr(fake_exp, meth) assert callable(method) def test_experiment_get_container(fake_exp): container = fake_exp.get_container('container_01') assert isinstance(container, Container) def test_counts(fake_exp): counts = fake_exp._counts assert counts['containers'] == 4 assert counts['cells'] == 24 assert counts['colonies'] == 4 assert counts['lineages'] == 12 # number of leaves when no filter is applied ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_experiment.py::test_experiment_label,tunacell/tests/test_experiment.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python2 # -*- coding: utf-8 -*- """""" Testing Experiment features. """""" from __future__ import print_function import pytest import os import shutil import tunacell from tunacell.base.experiment import Experiment from tunacell.base.container import Container path_data = os.path.join(os.path.dirname(tunacell.__file__), 'data') path_fake_exp = os.path.join(path_data, 'fake') @pytest.fixture(scope='module') def fake_exp(): exp = Experiment(path_fake_exp, count_items=True) yield exp analysis_path = os.path.join(path_fake_exp, 'analysis') if os.path.exists(analysis_path): shutil.rmtree(analysis_path) def test_load_experiment(fake_exp): assert isinstance(fake_exp, Experiment) def test_experiment_attributes(fake_exp): attrs = ['label', 'abspath', 'metadata', 'datatype', 'filetype', 'containers'] for attr in attrs: assert hasattr(fake_exp, attr) def test_experiment_label(fake_exp): assert fake_exp.label == 'fake' def test_experiment_abspath(fake_exp): assert fake_exp.abspath == path_fake_exp def test_experiment_number_container(fake_exp): assert len(fake_exp.containers) == 4 def test_experiment_metadata(fake_exp): assert fake_exp.metadata['author'] == 'Joachim Rambeau' def test_experiment_datatype(fake_exp): assert len(fake_exp.datatype) == 4 assert 'cellID' in list(zip(*fake_exp.datatype))[0] def test_experiment_iter(fake_exp): meths = ['iter_containers'] for meth in meths: method = getattr(fake_exp, meth) assert callable(method) def test_experiment_get_container(fake_exp): container = fake_exp.get_container('container_01') assert isinstance(container, Container) def test_counts(fake_exp): counts = fake_exp._counts assert counts['containers'] == 4 assert counts['cells'] == 24 assert counts['colonies'] == 4 assert counts['lineages'] == 12 # number of leaves when no filter is applied ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_experiment.py::test_experiment_metadata,tunacell/tests/test_experiment.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python2 # -*- coding: utf-8 -*- """""" Testing Experiment features. """""" from __future__ import print_function import pytest import os import shutil import tunacell from tunacell.base.experiment import Experiment from tunacell.base.container import Container path_data = os.path.join(os.path.dirname(tunacell.__file__), 'data') path_fake_exp = os.path.join(path_data, 'fake') @pytest.fixture(scope='module') def fake_exp(): exp = Experiment(path_fake_exp, count_items=True) yield exp analysis_path = os.path.join(path_fake_exp, 'analysis') if os.path.exists(analysis_path): shutil.rmtree(analysis_path) def test_load_experiment(fake_exp): assert isinstance(fake_exp, Experiment) def test_experiment_attributes(fake_exp): attrs = ['label', 'abspath', 'metadata', 'datatype', 'filetype', 'containers'] for attr in attrs: assert hasattr(fake_exp, attr) def test_experiment_label(fake_exp): assert fake_exp.label == 'fake' def test_experiment_abspath(fake_exp): assert fake_exp.abspath == path_fake_exp def test_experiment_number_container(fake_exp): assert len(fake_exp.containers) == 4 def test_experiment_metadata(fake_exp): assert fake_exp.metadata['author'] == 'Joachim Rambeau' def test_experiment_datatype(fake_exp): assert len(fake_exp.datatype) == 4 assert 'cellID' in list(zip(*fake_exp.datatype))[0] def test_experiment_iter(fake_exp): meths = ['iter_containers'] for meth in meths: method = getattr(fake_exp, meth) assert callable(method) def test_experiment_get_container(fake_exp): container = fake_exp.get_container('container_01') assert isinstance(container, Container) def test_counts(fake_exp): counts = fake_exp._counts assert counts['containers'] == 4 assert counts['cells'] == 24 assert counts['colonies'] == 4 assert counts['lineages'] == 12 # number of leaves when no filter is applied ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_experiment.py::test_experiment_number_container,tunacell/tests/test_experiment.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python2 # -*- coding: utf-8 -*- """""" Testing Experiment features. """""" from __future__ import print_function import pytest import os import shutil import tunacell from tunacell.base.experiment import Experiment from tunacell.base.container import Container path_data = os.path.join(os.path.dirname(tunacell.__file__), 'data') path_fake_exp = os.path.join(path_data, 'fake') @pytest.fixture(scope='module') def fake_exp(): exp = Experiment(path_fake_exp, count_items=True) yield exp analysis_path = os.path.join(path_fake_exp, 'analysis') if os.path.exists(analysis_path): shutil.rmtree(analysis_path) def test_load_experiment(fake_exp): assert isinstance(fake_exp, Experiment) def test_experiment_attributes(fake_exp): attrs = ['label', 'abspath', 'metadata', 'datatype', 'filetype', 'containers'] for attr in attrs: assert hasattr(fake_exp, attr) def test_experiment_label(fake_exp): assert fake_exp.label == 'fake' def test_experiment_abspath(fake_exp): assert fake_exp.abspath == path_fake_exp def test_experiment_number_container(fake_exp): assert len(fake_exp.containers) == 4 def test_experiment_metadata(fake_exp): assert fake_exp.metadata['author'] == 'Joachim Rambeau' def test_experiment_datatype(fake_exp): assert len(fake_exp.datatype) == 4 assert 'cellID' in list(zip(*fake_exp.datatype))[0] def test_experiment_iter(fake_exp): meths = ['iter_containers'] for meth in meths: method = getattr(fake_exp, meth) assert callable(method) def test_experiment_get_container(fake_exp): container = fake_exp.get_container('container_01') assert isinstance(container, Container) def test_counts(fake_exp): counts = fake_exp._counts assert counts['containers'] == 4 assert counts['cells'] == 24 assert counts['colonies'] == 4 assert counts['lineages'] == 12 # number of leaves when no filter is applied ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_experiment.py::test_load_experiment,tunacell/tests/test_experiment.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python2 # -*- coding: utf-8 -*- """""" Testing Experiment features. """""" from __future__ import print_function import pytest import os import shutil import tunacell from tunacell.base.experiment import Experiment from tunacell.base.container import Container path_data = os.path.join(os.path.dirname(tunacell.__file__), 'data') path_fake_exp = os.path.join(path_data, 'fake') @pytest.fixture(scope='module') def fake_exp(): exp = Experiment(path_fake_exp, count_items=True) yield exp analysis_path = os.path.join(path_fake_exp, 'analysis') if os.path.exists(analysis_path): shutil.rmtree(analysis_path) def test_load_experiment(fake_exp): assert isinstance(fake_exp, Experiment) def test_experiment_attributes(fake_exp): attrs = ['label', 'abspath', 'metadata', 'datatype', 'filetype', 'containers'] for attr in attrs: assert hasattr(fake_exp, attr) def test_experiment_label(fake_exp): assert fake_exp.label == 'fake' def test_experiment_abspath(fake_exp): assert fake_exp.abspath == path_fake_exp def test_experiment_number_container(fake_exp): assert len(fake_exp.containers) == 4 def test_experiment_metadata(fake_exp): assert fake_exp.metadata['author'] == 'Joachim Rambeau' def test_experiment_datatype(fake_exp): assert len(fake_exp.datatype) == 4 assert 'cellID' in list(zip(*fake_exp.datatype))[0] def test_experiment_iter(fake_exp): meths = ['iter_containers'] for meth in meths: method = getattr(fake_exp, meth) assert callable(method) def test_experiment_get_container(fake_exp): container = fake_exp.get_container('container_01') assert isinstance(container, Container) def test_counts(fake_exp): counts = fake_exp._counts assert counts['containers'] == 4 assert counts['cells'] == 24 assert counts['colonies'] == 4 assert counts['lineages'] == 12 # number of leaves when no filter is applied ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_filters.py::test_cell_any,tunacell/tests/test_filters.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python3 # -*- coding: utf-8 -*- """""" Testing filtering features. """""" from __future__ import print_function import pytest from tunacell import Observable from tunacell.filters.main import FilterSet from tunacell.filters.cells import (FilterCellAny, FilterCellIDparity, FilterObservableBound ) from tunacell.filters.lineages import FilterLineageWithCellProperty def test_cell_any(): filt = FilterCellAny() assert filt._type == 'CELL' assert filt.label == 'CELL, Always True' assert filt._obs == [] assert filt('whatever') # always True def test_unroll(): o1 = Observable(name='test-obs-1-used-for-cells', raw='length') o2 = Observable(name='test-obs-2-used-for-lineages', raw='width') f1 = FilterObservableBound(obs=o1, lower_bound=1, upper_bound=2) # cells f2 = FilterObservableBound(obs=o2, lower_bound=3, upper_bound=4) # cells lf = FilterLineageWithCellProperty(cell_filter=f2) # lineages fset = FilterSet(label='new-idea', filtercell=f1, filterlineage=lf) assert set(fset.obs) == set([o1, o2]) ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_filters.py::test_unroll,tunacell/tests/test_filters.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python3 # -*- coding: utf-8 -*- """""" Testing filtering features. """""" from __future__ import print_function import pytest from tunacell import Observable from tunacell.filters.main import FilterSet from tunacell.filters.cells import (FilterCellAny, FilterCellIDparity, FilterObservableBound ) from tunacell.filters.lineages import FilterLineageWithCellProperty def test_cell_any(): filt = FilterCellAny() assert filt._type == 'CELL' assert filt.label == 'CELL, Always True' assert filt._obs == [] assert filt('whatever') # always True def test_unroll(): o1 = Observable(name='test-obs-1-used-for-cells', raw='length') o2 = Observable(name='test-obs-2-used-for-lineages', raw='width') f1 = FilterObservableBound(obs=o1, lower_bound=1, upper_bound=2) # cells f2 = FilterObservableBound(obs=o2, lower_bound=3, upper_bound=4) # cells lf = FilterLineageWithCellProperty(cell_filter=f2) # lineages fset = FilterSet(label='new-idea', filtercell=f1, filterlineage=lf) assert set(fset.obs) == set([o1, o2]) ", https://github.com/LeBarbouze/tunacell,837d57e1e6caeddf2e6bcdd2db2986fb456dd605,tunacell/tests/test_observable.py::test_observable_str,tunacell/tests/test_observable.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python2 # -*- coding: utf-8 -*- """""" Testing Observable features. """""" from __future__ import print_function import pytest from itertools import product from tunacell.base.observable import (Observable, ObservableStringError, FunctionalObservable, set_observable_list, unroll_func_obs, unroll_raw_obs) t_params = (('name', ['mysoup', 'donald']), ('raw', ['soup', 'duck']), ('scale', ['linear', 'log']), ('mode', ['dynamics', 'birth', 'division', 'net-increase-additive', 'net-increase-multiplicative', 'rate', 'average']), ('join_points', [3, 10, 100]), ('timing', ['t', 'b', 'd', 'g']), ('tref', [None, 0., 30, 300.]), ('differentiate', [False, True]), ('local_fit', [False, True]), ('time_window', [0., 3.14, 15.]) ) #@pytest.fixture #def random_strings(): # average_string_length = 100 # number_of_samples = 20 # lengths = np.random.poisson(lam=average_string_length, # size=number_of_samples) # # def gen_random_strings(sizes): # chars = list(string.printable) # for size in sizes: # array = np.random.randint(0, high=len(chars), size=size) # yield ''.join([chars[k] for k in array]) # # return gen_random_strings(lengths) @pytest.fixture(scope='module') def all_params(): """"""Fixture to return an iterator among all parameter sets"""""" def iter_params(params): keys, values = zip(*params) for items in product(*values): yield {key: value for key, value in zip(keys, items)} return iter_params(t_params) def test_observable_init(all_params): """"""Test Observable initialization"""""" for kwargs in all_params: obs = Observable(**kwargs) for attr in obs._attr_names: assert getattr(obs, attr) == kwargs[attr] def test_observable_str(all_params): """"""This test checks codestring obtained using __str__ Randomly instantiate a given observable Check that second instance made from str(obs) is identical. """""" for kwargs in all_params: obs = Observable(**kwargs) nobs = Observable(from_string=str(obs)) for attr in obs._attr_names: if attr == 'time_window' and not obs.local_fit: # check only if local_fit is True, # since when it's false, time_window is not printed in str continue assert hasattr(nobs, attr) assert getattr(nobs, attr) == getattr(obs, attr) def test_observable_repr(all_params): """"""This test checks string representation obtained using __repr__ Randomly instantiate a given observable Check that second instance made from str(obs) is identical. """""" for kwargs in all_params: obs = Observable(**kwargs) nobs = eval(repr(obs)) for attr in obs._attr_names: assert hasattr(nobs, attr) assert getattr(nobs, attr) == getattr(obs, attr) def test_unrolling(): length = Observable(name='length') width = Observable(name='width') area = FunctionalObservable(name='square-area', f=lambda x, y: x*y, observables=[length, width]) vol = FunctionalObservable(name='volume', f=lambda x, y: x*y, observables=[area, width]) raw_obs, func_obs = set_observable_list(vol, filters=[]) assert length in raw_obs assert length not in func_obs assert width in raw_obs assert width not in func_obs assert area in func_obs assert area not in raw_obs assert vol in func_obs assert vol not in raw_obs def test_unroll_func(): length = Observable(name='length') width = Observable(name='width') area = FunctionalObservable(name='square-area', f=lambda x, y: x*y, observables=[length, width]) vol = FunctionalObservable(name='volume', f=lambda x, y: x*y, observables=[area, width]) func_obs = list(unroll_func_obs(vol)) assert func_obs == [area, vol] # oredering matters for functionalObs func_obs = list(unroll_func_obs([vol, area])) assert func_obs == [area, vol, area] ", https://github.com/leopepe/GOApy,a8e0dfe5f0f593107884ad54f89a27f67ee28129,tests/Sensor_test.py::SensorsTest::test_remove_sensor_success,tests/Sensor_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import unittest from Goap.Sensor import Sensors class SensorsTest(unittest.TestCase): def setUp(self): # ACTIONS self.sensors = Sensors() def test_add_success(self): self.sensors = Sensors() self.sensors.add( name='SenseTmpDirState', shell='if [ -d ""/tmp/goap_tmp"" ]; then echo -n ""exist""; else echo -n ""not_exist""; fi', binding='tmp_dir_state') self.sensors.add( name='SenseTmpDirContent', shell='[ -f /tmp/goap_tmp/.token ] && echo -n ""token_found"" || echo -n ""token_not_found""', binding='tmp_dir_content') assert 'SenseTmpDirState' == str( self.sensors.get(name='SenseTmpDirState')) assert 'SenseTmpDirContent' == str( self.sensors.get(name='SenseTmpDirContent')) def test_remove_sensor_success(self): assert self.sensors.remove(name='SenseTmpDirContent') is True def test_remove_sensor_error(self): assert self.sensors.remove(name='CreateAPP') is False ", https://github.com/levi-rs/instabrade,73d027ce2e3b0141c7f1c83567151b5638905355,tests/test_instagram.py::test_app_store_center_popup_displayed,tests/test_instagram.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"try: import mock except ImportError: from unittest import mock import pytest from selenium.common.exceptions import ( StaleElementReferenceException as SERE, WebDriverException ) from instabrade import Instagram, PageID from instabrade.log_in_page import LogInPage from instabrade.instagram import ( CENTER_POPUP_CLOSE_XPATH, CENTER_POPUP_DISPLAYED_XPATH, FOOTER_POPUP_CLOSE_XPATH, ) call = mock.call patch = mock.patch PropertyMock = mock.PropertyMock def test___init__(driver): """""" Verify Instagram object accepts webdriver as first arg """""" ig = Instagram(driver) assert ig.driver is driver assert call.get('http://www.instagram.com') in driver.method_calls # Validate pages are instantiated assert isinstance(ig.log_in_page, LogInPage) assert hasattr(ig.log_in_page, 'PAGE_IDENTIFIER') assert isinstance(ig.log_in_page.PAGE_IDENTIFIER, PageID) def test___init___no_default(driver): """""" Verify Instagram object accepts bool as second arg """""" ig = Instagram(driver, False) assert ig.driver is driver assert call.get('http://www.instagram.com') not in driver.method_calls def test_app_store_center_popup_close(instagram, element): """""" Verify the center app store popup can be closed """""" instagram.driver.find_element.return_value = element element.is_enabled.side_effect = [None, SERE] instagram.app_store_center_popup_close() exp_driver_call = call.find_element('xpath', CENTER_POPUP_CLOSE_XPATH) assert exp_driver_call in instagram.driver.method_calls exp_elem_calls = [call.click(), call.is_enabled(), call.is_enabled()] assert exp_elem_calls == element.method_calls def test_app_store_center_popup_displayed(instagram): """""" Verify the center app store popup display check works """""" instagram.driver.find_elements_by_xpath.side_effect = [[], [1, 2]] assert instagram.app_store_center_popup_displayed is False assert instagram.app_store_center_popup_displayed is True exp_call = call.find_elements_by_xpath(CENTER_POPUP_DISPLAYED_XPATH) assert exp_call in instagram.driver.method_calls def test_app_store_footer_popup_close(instagram, element): """""" Verify the footer app store popup can be closed """""" instagram.driver.find_element.return_value = element element.is_enabled.side_effect = [None, SERE] instagram.app_store_footer_popup_close() exp_driver_call = call.find_element('xpath', FOOTER_POPUP_CLOSE_XPATH) assert exp_driver_call in instagram.driver.method_calls exp_elem_calls = [call.click(), call.is_enabled(), call.is_enabled()] assert exp_elem_calls == element.method_calls def test_app_store_footer_popup_displayed(instagram, element): """""" Verify footer popup detection works """""" instagram.driver.execute_script.return_value = 100 side_effects = [[], [element, ], [element, ], [element, ]] instagram.driver.find_elements_by_css_selector.side_effect = side_effects element.is_displayed.side_effect = [False, True, True, True] loc_side_effect = [SERE, {'y': 110}, {'y': 90}] type(element).location = PropertyMock(side_effect=loc_side_effect) instagram.app_store_footer_popup_displayed is False instagram.app_store_footer_popup_displayed is False instagram.app_store_footer_popup_displayed is False instagram.app_store_footer_popup_displayed is True def test_close_popups(instagram): """""" Test the popup closing functionality """""" with patch.object(instagram, 'app_store_center_popup_close') as cc_mock: with patch.object(instagram, 'app_store_footer_popup_close') as fc_mock: cd_prop = PropertyMock(side_effect=[False, True]) type(instagram).app_store_center_popup_displayed = cd_prop fd_prop = PropertyMock(side_effect=[False, True]) type(instagram).app_store_footer_popup_displayed = fd_prop exc_text = ""Other element would receive the click"" fc_mock.side_effect = [WebDriverException(exc_text), None] instagram.close_popups() assert cc_mock.call_args_list == [call()] assert fc_mock.call_args_list == [call(), call()] assert cd_prop.call_args_list == [call(), call()] assert fd_prop.call_args_list == [call(), call()] def test_close_popups_exception(instagram): """""" Test the popup closing reraises exception """""" with patch.object(instagram, 'app_store_center_popup_close'): with patch.object(instagram, 'app_store_footer_popup_close') as fc_mock: cd_prop = PropertyMock(side_effect=[False, True]) type(instagram).app_store_center_popup_displayed = cd_prop fd_prop = PropertyMock(side_effect=[False, True]) type(instagram).app_store_footer_popup_displayed = fd_prop exc_text = ""Not the expected text"" fc_mock.side_effect = [WebDriverException(exc_text), None] with pytest.raises(WebDriverException): instagram.close_popups() def test_scroll_to_bottom(instagram): """""" Test the scroll_to_bottom method call """""" instagram.scroll_to_bottom() assert instagram.driver.execute_script.called assert ""0, document.body.scrollHeight"" in str(instagram.driver.execute_script.call_args) def test_scroll_to_center(instagram): """""" Test the scroll_to_center method call """""" instagram.scroll_to_center() assert instagram.driver.execute_script.called assert ""0, document.body.scrollHeight/2"" in str(instagram.driver.execute_script.call_args) def test_scroll_to_top(instagram): """""" Test the scroll_to_top method call """""" instagram.scroll_to_top() assert instagram.driver.execute_script.called assert ""scrollTo(0, 0)"" in str(instagram.driver.execute_script.call_args) ", https://github.com/levi-rs/instabrade,73d027ce2e3b0141c7f1c83567151b5638905355,tests/test_instagram.py::test_app_store_footer_popup_displayed,tests/test_instagram.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"try: import mock except ImportError: from unittest import mock import pytest from selenium.common.exceptions import ( StaleElementReferenceException as SERE, WebDriverException ) from instabrade import Instagram, PageID from instabrade.log_in_page import LogInPage from instabrade.instagram import ( CENTER_POPUP_CLOSE_XPATH, CENTER_POPUP_DISPLAYED_XPATH, FOOTER_POPUP_CLOSE_XPATH, ) call = mock.call patch = mock.patch PropertyMock = mock.PropertyMock def test___init__(driver): """""" Verify Instagram object accepts webdriver as first arg """""" ig = Instagram(driver) assert ig.driver is driver assert call.get('http://www.instagram.com') in driver.method_calls # Validate pages are instantiated assert isinstance(ig.log_in_page, LogInPage) assert hasattr(ig.log_in_page, 'PAGE_IDENTIFIER') assert isinstance(ig.log_in_page.PAGE_IDENTIFIER, PageID) def test___init___no_default(driver): """""" Verify Instagram object accepts bool as second arg """""" ig = Instagram(driver, False) assert ig.driver is driver assert call.get('http://www.instagram.com') not in driver.method_calls def test_app_store_center_popup_close(instagram, element): """""" Verify the center app store popup can be closed """""" instagram.driver.find_element.return_value = element element.is_enabled.side_effect = [None, SERE] instagram.app_store_center_popup_close() exp_driver_call = call.find_element('xpath', CENTER_POPUP_CLOSE_XPATH) assert exp_driver_call in instagram.driver.method_calls exp_elem_calls = [call.click(), call.is_enabled(), call.is_enabled()] assert exp_elem_calls == element.method_calls def test_app_store_center_popup_displayed(instagram): """""" Verify the center app store popup display check works """""" instagram.driver.find_elements_by_xpath.side_effect = [[], [1, 2]] assert instagram.app_store_center_popup_displayed is False assert instagram.app_store_center_popup_displayed is True exp_call = call.find_elements_by_xpath(CENTER_POPUP_DISPLAYED_XPATH) assert exp_call in instagram.driver.method_calls def test_app_store_footer_popup_close(instagram, element): """""" Verify the footer app store popup can be closed """""" instagram.driver.find_element.return_value = element element.is_enabled.side_effect = [None, SERE] instagram.app_store_footer_popup_close() exp_driver_call = call.find_element('xpath', FOOTER_POPUP_CLOSE_XPATH) assert exp_driver_call in instagram.driver.method_calls exp_elem_calls = [call.click(), call.is_enabled(), call.is_enabled()] assert exp_elem_calls == element.method_calls def test_app_store_footer_popup_displayed(instagram, element): """""" Verify footer popup detection works """""" instagram.driver.execute_script.return_value = 100 side_effects = [[], [element, ], [element, ], [element, ]] instagram.driver.find_elements_by_css_selector.side_effect = side_effects element.is_displayed.side_effect = [False, True, True, True] loc_side_effect = [SERE, {'y': 110}, {'y': 90}] type(element).location = PropertyMock(side_effect=loc_side_effect) instagram.app_store_footer_popup_displayed is False instagram.app_store_footer_popup_displayed is False instagram.app_store_footer_popup_displayed is False instagram.app_store_footer_popup_displayed is True def test_close_popups(instagram): """""" Test the popup closing functionality """""" with patch.object(instagram, 'app_store_center_popup_close') as cc_mock: with patch.object(instagram, 'app_store_footer_popup_close') as fc_mock: cd_prop = PropertyMock(side_effect=[False, True]) type(instagram).app_store_center_popup_displayed = cd_prop fd_prop = PropertyMock(side_effect=[False, True]) type(instagram).app_store_footer_popup_displayed = fd_prop exc_text = ""Other element would receive the click"" fc_mock.side_effect = [WebDriverException(exc_text), None] instagram.close_popups() assert cc_mock.call_args_list == [call()] assert fc_mock.call_args_list == [call(), call()] assert cd_prop.call_args_list == [call(), call()] assert fd_prop.call_args_list == [call(), call()] def test_close_popups_exception(instagram): """""" Test the popup closing reraises exception """""" with patch.object(instagram, 'app_store_center_popup_close'): with patch.object(instagram, 'app_store_footer_popup_close') as fc_mock: cd_prop = PropertyMock(side_effect=[False, True]) type(instagram).app_store_center_popup_displayed = cd_prop fd_prop = PropertyMock(side_effect=[False, True]) type(instagram).app_store_footer_popup_displayed = fd_prop exc_text = ""Not the expected text"" fc_mock.side_effect = [WebDriverException(exc_text), None] with pytest.raises(WebDriverException): instagram.close_popups() def test_scroll_to_bottom(instagram): """""" Test the scroll_to_bottom method call """""" instagram.scroll_to_bottom() assert instagram.driver.execute_script.called assert ""0, document.body.scrollHeight"" in str(instagram.driver.execute_script.call_args) def test_scroll_to_center(instagram): """""" Test the scroll_to_center method call """""" instagram.scroll_to_center() assert instagram.driver.execute_script.called assert ""0, document.body.scrollHeight/2"" in str(instagram.driver.execute_script.call_args) def test_scroll_to_top(instagram): """""" Test the scroll_to_top method call """""" instagram.scroll_to_top() assert instagram.driver.execute_script.called assert ""scrollTo(0, 0)"" in str(instagram.driver.execute_script.call_args) ", https://github.com/leVirve/dcard-spider,ac64cbcfe7ef6be7e554cef422b1a8a6bb968d46,tests/test_forums.py::TestForums::test_with_none_name_in_dcard_instance,tests/test_forums.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"_forum_name = 'photography' class TestForums: def test_with_none_name_in_dcard_instance(self, dcard): f = dcard.forums assert f.name is None def test_with_name_param_through__call__(self, dcard): f = dcard.forums(_forum_name) assert _forum_name == f.name def test_get_all_forums(self, dcard): all_fourums = dcard.forums.get() assert all_fourums def test_get_only_normal_forums(self, dcard): no_school_fourums = dcard.forums.get(no_school=True) assert no_school_fourums def test_extract_general_forums(self, dcard): all_fourums = dcard.forums.get() no_school_fourums = dcard.forums.get(no_school=True) assert len(all_fourums) > len(no_school_fourums) def test_get_metas_with_sort_param(self, dcard): forum = dcard.forums(_forum_name) assert len(forum.get_metas(sort='popular')) <= 30 assert len(forum.get_metas(sort='new')) == 30 def test_get_metas_with_num_param(self, dcard): forum = dcard.forums(_forum_name) assert len(forum.get_metas()) == 30 assert len(forum.get_metas(num=0)) == 0 assert len(forum.get_metas(num=90)) == 90 assert len(forum.get_metas(num=33)) == 33 assert len(forum.get_metas(num=87)) == 87 def test_get_metas_with_before_param(self, dcard): forum = dcard.forums(_forum_name) assert len(forum.get_metas(before=24561)) != 0 def test_get_metas_with_time_bound_param(self, dcard, boundary_date): forum = dcard.forums(_forum_name) assert len(forum.get_metas()) == 30 assert len(forum.get_metas(timebound='')) == 30 assert len(forum.get_metas(timebound=boundary_date.isoformat())) >= 0 def test_get_metas_with_infinite_pages(self, dcard): # TODO(salas) not able to test infinite in Travis. """""" forum = dcard.forums(_forum_name) assert len(forum.get_metas(num=forum.infinite_page)) >= 30 """""" return def test_get_metas_with_callback_collect_ids(self, dcard): forum = dcard.forums(_forum_name) def collect_ids(metas): return [meta['id'] for meta in metas] ids = forum(_forum_name).get_metas(callback=collect_ids) assert len(ids) == 30 assert all([isinstance(x, int) for x in ids]) def test_get_metas_with_callback_no_return(self, dcard): forum = dcard.forums(_forum_name) def with_no_return(metas): return None no_return = forum(_forum_name).get_metas(callback=with_no_return) assert no_return is None def test_get_metas_with_callback_store_db(self, dcard): forum = dcard.forums(_forum_name) def simulate_store_into_db(metas): some_ids = [9487] * len(metas) return some_ids result_ids = forum.get_metas(callback=simulate_store_into_db) assert len(result_ids) == 30 ", https://github.com/leVirve/dcard-spider,ac64cbcfe7ef6be7e554cef422b1a8a6bb968d46,tests/test_posts.py::TestPosts::test_with_none_in_dcard_instance,tests/test_posts.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- from __future__ import unicode_literals _post_id = 230004779 class TestPosts: def test_with_none_in_dcard_instance(self, dcard): posts = dcard.posts assert not posts.ids assert not posts.metas assert not posts.only_id def test_with_metas_through__call__(self, dcard, metas): posts = dcard.posts(metas) assert posts.ids assert posts.metas assert not posts.only_id def test_with_ids_through__call__(self, dcard, metas): ids = [m['id'] for m in metas] posts = dcard.posts(ids) assert posts.ids assert not posts.metas assert posts.only_id def test_gen_content_reqs(self, dcard, client): reqs = dcard.posts.gen_content_reqs([_post_id]) result = list(client.imap(reqs))[0] assert isinstance(result.json(), dict) def test_gen_links_reqs(self, dcard, client): reqs = dcard.posts.gen_links_reqs([_post_id]) result = list(client.imap(reqs))[0] assert isinstance(result.json(), dict) def test_get_comments_serial(self, dcard): reqs = dcard.posts.get_comments_serial(_post_id) result = reqs assert isinstance(result, list) def test_get_comments_parallel(self, dcard, client, metas): comments_count = 87 reqs = dcard.posts.get_comments_parallel(_post_id, comments_count) result = list(client.imap(reqs))[0] assert isinstance(result.json(), list) def test_get_post_bundle(self, dcard): posts = dcard.posts(_post_id).get() first_post = posts.result()[0] comment_count = first_post['commentCount'] assert comment_count == len(first_post['comments']) def test_get_post_bundles(self, dcard, metas): ids = [m['id'] for m in metas] posts1 = dcard.posts(metas).get(comments=False, links=False).result() posts2 = dcard.posts(ids).get(comments=False, links=False).result() titles = [post['title'] for post in posts1] assert len(posts1) == len(posts2) assert all(titles) class TestPostsResult: def test_postsresult_from_post_with_ids(self, dcard): posts = dcard.posts(_post_id).get(comments=False, links=False).result() assert len(posts) == 1 assert posts[0] is not None def test_postsresult_from_post_with_metas(self, dcard, metas): posts = dcard.posts(metas[0]).get(comments=False, links=False).result() assert len(posts) == 1 assert posts[0] is not None def test_parse_resourses_in_content(self, dcard): posts = dcard.posts(_post_id).get(comments=False, links=False) resources = posts.parse_resources() assert len(resources) > 0 def test_parse_resourses_in_content_and_comments(self, dcard): posts = dcard.posts(_post_id).get(links=False) resources = posts.parse_resources() assert len(resources) >= 0 def test_download_resourses(self, dcard): posts = dcard.posts(_post_id).get(comments=False, links=False) resources = posts.parse_resources() status, fails = posts.download(resources) assert type(status) is int assert len(fails) == 0 ", https://github.com/LGSInnovations/jupyter-sigplot,498cda8f3648282cba84745d97e8973d2a02ed89,test/test_jupyter_sigplot.py::test_empty_object,test/test_jupyter_sigplot.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env pytest import os from mock import patch import numpy as np import pytest from IPython.testing.globalipapp import get_ipython ip = get_ipython() from jupyter_sigplot.sigplot import Plot # noqa: E402 from .testutil import EnvironmentVariable # noqa: E402 ########################################################################### # Basic tests. Can we create a plot? ########################################################################### def test_empty_object(): plot = Plot() # instance variables assert plot.data_dir == '' assert plot.path_resolvers == [] # traitlets assert plot.command_and_arguments == {} assert plot.plot_options == {} assert plot.progress == 0.0 assert not plot.done def test_non_empty_object(): data_dir = ""/tmp"" path_resolvers = [""/data""] options = {'noyaxis': True, 'noxaxis': True} plot = Plot( data_dir=data_dir, path_resolvers=path_resolvers, **options ) # instance variables assert plot.data_dir == data_dir assert plot.path_resolvers == path_resolvers # traitlets assert plot.command_and_arguments == {} assert plot.plot_options == options assert plot.progress == 0.0 assert not plot.done def test_available_commands(): plot = Plot() available_commands = [ 'change_settings', 'overlay_href', 'overlay_array', ] assert plot.available_commands == available_commands ########################################################################### # Command tests ########################################################################### def test_getattr_change_settings(): plot = Plot() options = {'autol': 1000} plot.change_settings(options) assert plot.command_and_arguments == { 'command': 'change_settings', 'arguments': [options] } def test_uppercase_getattr_change_settings(): plot = Plot() options = {'autol': 1000} with pytest.raises(AttributeError): plot.CHANGE_SETTINGS(options) def test_attr_error(): plot = Plot() with pytest.raises(AttributeError): plot.foobar('blah') def test_overlay_array(): plot = Plot() lst = [1, 2, 3] plot.overlay_array(lst) assert plot.command_and_arguments == { 'command': 'overlay_array', 'arguments': [memoryview(np.array(lst, dtype=np.float32))], } def test_overlay_array_bad_type(): plot = Plot() lst = ['foo', 'bar', 'baz'] with pytest.raises(TypeError): plot.overlay_array(lst) def test_overlay_array_numpy(): plot = Plot() lst = np.array([1, 2, 3]) plot.overlay_array(lst) assert plot.command_and_arguments == { 'command': 'overlay_array', 'arguments': [memoryview(lst.astype(np.float32))], } def test_overlay_array_numpy_bad_dtype(): plot = Plot() lst = np.array(['foo', 'bar', 'baz']) with pytest.raises(TypeError): plot.overlay_array(lst) @patch('jupyter_sigplot.sigplot.Plot.sync_command_and_arguments') def test_overlay_href(traitlet_set_mock): plot = Plot() plot.overlay_href('bar|baz') assert traitlet_set_mock.call_count == 2 for call_args in traitlet_set_mock.call_args_list: assert call_args[0][0]['command'] == 'overlay_href' assert len(call_args[0][0]['arguments']) == 1 assert call_args[0][0]['arguments'][0] in ('bar', 'baz') ########################################################################### # Other tests ########################################################################### def test_unravel_path_no_resolvers(): import time from jupyter_sigplot.sigplot import _unravel_path tilde_full = os.path.expanduser('~') # Go all the way through the environment instead of a mock to be sure the # whole thing works end to end with EnvironmentVariable('TEST_UNRAVEL', str(time.time())) as envvar: env_key = envvar.key env_val = envvar.new_value cases = [ # input # expected output ('', ''), ('.', '.'), ('~', tilde_full), ('$%s' % env_key, env_val), ('/$%s' % env_key, os.path.join('/', env_val)), ('~/$%s' % env_key, os.path.join(tilde_full, env_val)), ] for (input, expected) in cases: actual = _unravel_path(input) assert(actual == expected) @patch('os.path.expanduser') # noqa: C901 @patch('os.path.expandvars') def test_unravel_path_resolvers(expandvars_mock, expanduser_mock): # This test isolates just the behavior of the `resolvers` argument to # _unravel_path. The set of test cases grows rather quickly if you cross # resolver equivalence classes with input equivalence classes, so we'll # just trust that testing each axis independently is sufficient. from jupyter_sigplot.sigplot import _unravel_path # Set up scaffolding to record the names of functions, in order, as they # are called. call_list = [] def reset(): call_list[:] = [] # Note that all functions take a single argument def make_recorder(name): def f(a): call_list.append(name) return a return f def recordit(f): r = make_recorder(f.__name__) def wrapped(a): r(a) return f(a) wrapped.__name__ = f.__name__ return wrapped expandvars_mock.side_effect = make_recorder('expandvars') expanduser_mock.side_effect = make_recorder('expanduser') @recordit def one(p): return p + '1' @recordit def two(p): return p + '2' @recordit def three(p): return p + '3' for resolvers, expected in [ ([], ''), ([one], '1'), ([one, two], '12'), ([two, one], '21'), ([one, two, three], '123'), ([three, two, one, one], '3211'), ]: reset() expected_names = ['expandvars', 'expanduser'] + \ [f.__name__ for f in resolvers] actual = _unravel_path('', resolvers) # Ensure that resolvers are called in the right sequence relative to # one another and relative to expandvars/expanduser assert(call_list == expected_names) # Ensure that resolvers are composed (output of rN is input of rN+1) assert(actual == expected) # Check that we get an error when resolvers arg is not as expected for resolvers in [ 3, # 0, Equivalent to resolvers=None, i.e., no resolvers 'a string', [3, 0], ['a string'], ]: with pytest.raises(TypeError): _unravel_path('foo.tmp', resolvers) @patch('os.mkdir') def test_require_dir_good_inputs(mkdir_mock): from jupyter_sigplot.sigplot import _require_dir inputs = ( '.', 'data', ) for d in inputs: _require_dir(d) args, kwargs = mkdir_mock.call_args assert args[0] == d assert mkdir_mock.call_count == len(inputs) # Special case: '' means '.' _require_dir('') assert mkdir_mock.call_args[0][0] == '.' def test_local_name_for_href_good_inputs(): from jupyter_sigplot.sigplot import _local_name_for_href cases = [ # input # expected output ('http://www.example.com/foo.tmp', 'foo.tmp'), ('http://www.example.com/dat/foo.tmp', 'foo.tmp'), ('https://localhost/foo.tmp', 'foo.tmp'), ('https://localhost/dat/foo.tmp', 'foo.tmp'), ] local_dirs = ['.', 'data', 'files/data', '/path/to/data', ] for ld in local_dirs: for (input, expected) in cases: actual = _local_name_for_href(input, ld) expected = os.path.join(ld, expected) assert(actual == expected) def test_local_name_for_href_bad_inputs(): from jupyter_sigplot.sigplot import _local_name_for_href cases = [ # url local_dir exception ('http://localhost/foo.tmp', None, TypeError), (None, '.', TypeError), ('', '.', ValueError), ] for url, local_dir, etype in cases: with pytest.raises(etype): _local_name_for_href(url, local_dir) # TODO (sat 2018-11-19): Decide whether we want _local_name_for_href to # validate URLs more strictly; if so, add tests here. def test_local_name_for_file_local_good_inputs(): from jupyter_sigplot.sigplot import _local_name_for_file cases = [ # input # expected output ('foo.tmp', 'foo.tmp'), ('dat/foo.tmp', 'dat/foo.tmp'), ] local_dirs = ['.', 'data', 'files/data', '/path/to/data', ] # None of these should require symlinks for ld in local_dirs: for (input, expected) in cases: # This test could definitely be stronger. The current idea is just # to ensure that some basic cases work right. input = os.path.join(ld, input) actual, is_local = _local_name_for_file(input, ld) assert is_local expected = os.path.join(ld, expected) assert(actual == expected) def test_local_name_for_file_nonlocal_good_inputs(): from jupyter_sigplot.sigplot import _local_name_for_file cases = [ # input # expected output ('/data/foo.tmp', 'foo.tmp'), ('../dat/foo.tmp', 'foo.tmp'), ('../foo.tmp', 'foo.tmp'), ('data/../../foo.tmp', 'foo.tmp'), ] local_dirs = ['.', 'data', 'files/data', '/path/to/data', ] # All these should require symlinks ", https://github.com/LGSInnovations/jupyter-sigplot,498cda8f3648282cba84745d97e8973d2a02ed89,test/test_jupyter_sigplot.py::test_overlay_href,test/test_jupyter_sigplot.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env pytest import os from mock import patch import numpy as np import pytest from IPython.testing.globalipapp import get_ipython ip = get_ipython() from jupyter_sigplot.sigplot import Plot # noqa: E402 from .testutil import EnvironmentVariable # noqa: E402 ########################################################################### # Basic tests. Can we create a plot? ########################################################################### def test_empty_object(): plot = Plot() # instance variables assert plot.data_dir == '' assert plot.path_resolvers == [] # traitlets assert plot.command_and_arguments == {} assert plot.plot_options == {} assert plot.progress == 0.0 assert not plot.done def test_non_empty_object(): data_dir = ""/tmp"" path_resolvers = [""/data""] options = {'noyaxis': True, 'noxaxis': True} plot = Plot( data_dir=data_dir, path_resolvers=path_resolvers, **options ) # instance variables assert plot.data_dir == data_dir assert plot.path_resolvers == path_resolvers # traitlets assert plot.command_and_arguments == {} assert plot.plot_options == options assert plot.progress == 0.0 assert not plot.done def test_available_commands(): plot = Plot() available_commands = [ 'change_settings', 'overlay_href', 'overlay_array', ] assert plot.available_commands == available_commands ########################################################################### # Command tests ########################################################################### def test_getattr_change_settings(): plot = Plot() options = {'autol': 1000} plot.change_settings(options) assert plot.command_and_arguments == { 'command': 'change_settings', 'arguments': [options] } def test_uppercase_getattr_change_settings(): plot = Plot() options = {'autol': 1000} with pytest.raises(AttributeError): plot.CHANGE_SETTINGS(options) def test_attr_error(): plot = Plot() with pytest.raises(AttributeError): plot.foobar('blah') def test_overlay_array(): plot = Plot() lst = [1, 2, 3] plot.overlay_array(lst) assert plot.command_and_arguments == { 'command': 'overlay_array', 'arguments': [memoryview(np.array(lst, dtype=np.float32))], } def test_overlay_array_bad_type(): plot = Plot() lst = ['foo', 'bar', 'baz'] with pytest.raises(TypeError): plot.overlay_array(lst) def test_overlay_array_numpy(): plot = Plot() lst = np.array([1, 2, 3]) plot.overlay_array(lst) assert plot.command_and_arguments == { 'command': 'overlay_array', 'arguments': [memoryview(lst.astype(np.float32))], } def test_overlay_array_numpy_bad_dtype(): plot = Plot() lst = np.array(['foo', 'bar', 'baz']) with pytest.raises(TypeError): plot.overlay_array(lst) @patch('jupyter_sigplot.sigplot.Plot.sync_command_and_arguments') def test_overlay_href(traitlet_set_mock): plot = Plot() plot.overlay_href('bar|baz') assert traitlet_set_mock.call_count == 2 for call_args in traitlet_set_mock.call_args_list: assert call_args[0][0]['command'] == 'overlay_href' assert len(call_args[0][0]['arguments']) == 1 assert call_args[0][0]['arguments'][0] in ('bar', 'baz') ########################################################################### # Other tests ########################################################################### def test_unravel_path_no_resolvers(): import time from jupyter_sigplot.sigplot import _unravel_path tilde_full = os.path.expanduser('~') # Go all the way through the environment instead of a mock to be sure the # whole thing works end to end with EnvironmentVariable('TEST_UNRAVEL', str(time.time())) as envvar: env_key = envvar.key env_val = envvar.new_value cases = [ # input # expected output ('', ''), ('.', '.'), ('~', tilde_full), ('$%s' % env_key, env_val), ('/$%s' % env_key, os.path.join('/', env_val)), ('~/$%s' % env_key, os.path.join(tilde_full, env_val)), ] for (input, expected) in cases: actual = _unravel_path(input) assert(actual == expected) @patch('os.path.expanduser') # noqa: C901 @patch('os.path.expandvars') def test_unravel_path_resolvers(expandvars_mock, expanduser_mock): # This test isolates just the behavior of the `resolvers` argument to # _unravel_path. The set of test cases grows rather quickly if you cross # resolver equivalence classes with input equivalence classes, so we'll # just trust that testing each axis independently is sufficient. from jupyter_sigplot.sigplot import _unravel_path # Set up scaffolding to record the names of functions, in order, as they # are called. call_list = [] def reset(): call_list[:] = [] # Note that all functions take a single argument def make_recorder(name): def f(a): call_list.append(name) return a return f def recordit(f): r = make_recorder(f.__name__) def wrapped(a): r(a) return f(a) wrapped.__name__ = f.__name__ return wrapped expandvars_mock.side_effect = make_recorder('expandvars') expanduser_mock.side_effect = make_recorder('expanduser') @recordit def one(p): return p + '1' @recordit def two(p): return p + '2' @recordit def three(p): return p + '3' for resolvers, expected in [ ([], ''), ([one], '1'), ([one, two], '12'), ([two, one], '21'), ([one, two, three], '123'), ([three, two, one, one], '3211'), ]: reset() expected_names = ['expandvars', 'expanduser'] + \ [f.__name__ for f in resolvers] actual = _unravel_path('', resolvers) # Ensure that resolvers are called in the right sequence relative to # one another and relative to expandvars/expanduser assert(call_list == expected_names) # Ensure that resolvers are composed (output of rN is input of rN+1) assert(actual == expected) # Check that we get an error when resolvers arg is not as expected for resolvers in [ 3, # 0, Equivalent to resolvers=None, i.e., no resolvers 'a string', [3, 0], ['a string'], ]: with pytest.raises(TypeError): _unravel_path('foo.tmp', resolvers) @patch('os.mkdir') def test_require_dir_good_inputs(mkdir_mock): from jupyter_sigplot.sigplot import _require_dir inputs = ( '.', 'data', ) for d in inputs: _require_dir(d) args, kwargs = mkdir_mock.call_args assert args[0] == d assert mkdir_mock.call_count == len(inputs) # Special case: '' means '.' _require_dir('') assert mkdir_mock.call_args[0][0] == '.' def test_local_name_for_href_good_inputs(): from jupyter_sigplot.sigplot import _local_name_for_href cases = [ # input # expected output ('http://www.example.com/foo.tmp', 'foo.tmp'), ('http://www.example.com/dat/foo.tmp', 'foo.tmp'), ('https://localhost/foo.tmp', 'foo.tmp'), ('https://localhost/dat/foo.tmp', 'foo.tmp'), ] local_dirs = ['.', 'data', 'files/data', '/path/to/data', ] for ld in local_dirs: for (input, expected) in cases: actual = _local_name_for_href(input, ld) expected = os.path.join(ld, expected) assert(actual == expected) def test_local_name_for_href_bad_inputs(): from jupyter_sigplot.sigplot import _local_name_for_href cases = [ # url local_dir exception ('http://localhost/foo.tmp', None, TypeError), (None, '.', TypeError), ('', '.', ValueError), ] for url, local_dir, etype in cases: with pytest.raises(etype): _local_name_for_href(url, local_dir) # TODO (sat 2018-11-19): Decide whether we want _local_name_for_href to # validate URLs more strictly; if so, add tests here. def test_local_name_for_file_local_good_inputs(): from jupyter_sigplot.sigplot import _local_name_for_file cases = [ # input # expected output ('foo.tmp', 'foo.tmp'), ('dat/foo.tmp', 'dat/foo.tmp'), ] local_dirs = ['.', 'data', 'files/data', '/path/to/data', ] # None of these should require symlinks for ld in local_dirs: for (input, expected) in cases: # This test could definitely be stronger. The current idea is just # to ensure that some basic cases work right. input = os.path.join(ld, input) actual, is_local = _local_name_for_file(input, ld) assert is_local expected = os.path.join(ld, expected) assert(actual == expected) def test_local_name_for_file_nonlocal_good_inputs(): from jupyter_sigplot.sigplot import _local_name_for_file cases = [ # input # expected output ('/data/foo.tmp', 'foo.tmp'), ('../dat/foo.tmp', 'foo.tmp'), ('../foo.tmp', 'foo.tmp'), ('data/../../foo.tmp', 'foo.tmp'), ] local_dirs = ['.', 'data', 'files/data', '/path/to/data', ] # All these should require symlinks ", https://github.com/ljvmiranda921/pyswarms,08756526f39699eef28e515cac2ead17cef55710,tests/optimizers/test_local_best.py::TestLocalBestOptimizer::test_obj_with_kwargs,tests/optimizers/test_local_best.py,NOD,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- # Import modules import pytest import numpy as np # Import from pyswarms from pyswarms.single import LocalBestPSO from pyswarms.utils.functions.single_obj import sphere from .abc_test_optimizer import ABCTestOptimizer class TestLocalBestOptimizer(ABCTestOptimizer): @pytest.fixture def optimizer(self): return LocalBestPSO @pytest.fixture def optimizer_history(self, options): opt = LocalBestPSO(10, 2, options) opt.optimize(sphere, 1000) return opt @pytest.fixture def optimizer_reset(self, options): opt = LocalBestPSO(10, 2, options) opt.optimize(sphere, 10) opt.reset() return opt def test_local_correct_pos(self, options): """""" Test to check local optimiser returns the correct position corresponding to the best cost """""" opt = LocalBestPSO(n_particles=10, dimensions=2, options=options) cost, pos = opt.optimize(sphere, iters=5) # find best pos from history min_cost_idx = np.argmin(opt.cost_history) min_pos_idx = np.argmin(sphere(opt.pos_history[min_cost_idx])) assert np.array_equal(opt.pos_history[min_cost_idx][min_pos_idx], pos) ", https://github.com/ljvmiranda921/pyswarms,08756526f39699eef28e515cac2ead17cef55710,tests/optimizers/test_tolerance.py::TestToleranceOptions::test_ftol_effect[GeneralOptimizerPSO],tests/optimizers/test_tolerance.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- # Import standard libraries import pytest import random random.seed(0) # Import modules import numpy as np # Import from pyswarms from pyswarms.backend.topology import Star from pyswarms.single import GlobalBestPSO, LocalBestPSO, GeneralOptimizerPSO # Knapsack parameters capacity = 50 number_of_items = 10 item_range = range(number_of_items) # PARAMETERS value = [random.randint(1, number_of_items) for i in item_range] weight = [random.randint(1, number_of_items) for i in item_range] # PSO parameters n_particles = 10 iterations = 1000 options = {""c1"": 2, ""c2"": 2, ""w"": 0.7, ""k"": 3, ""p"": 2} dim = number_of_items LB = [0] * dim UB = [1] * dim constraints = (np.array(LB), np.array(UB)) kwargs = {""value"": value, ""weight"": weight, ""capacity"": capacity} def get_particle_obj(X, **kwargs): """""" Calculates the objective function value which is total revenue minus penalty of capacity violations"""""" # X is the decision variable. X is vector in the lenght of number of items # $ value of items value = kwargs[""value""] # weight of items weight = kwargs[""weight""] # Total revenue revenue = sum([value[i] * np.round(X[i]) for i in item_range]) # Total weight of selected items used_capacity = sum( [kwargs[""weight""][i] * np.round(X[i]) for i in item_range] ) # Total capacity violation with 100 as a penalty cofficient capacity_violation = 100 * min(0, capacity - used_capacity) # the objective function minimizes the negative revenue, which is the same # as maximizing the positive revenue return -1 * (revenue + capacity_violation) def objective_function(X, **kwargs): """"""Objective function with arguments"""""" n_particles_ = X.shape[0] dist = [get_particle_obj(X[i], **kwargs) for i in range(n_particles_)] return np.array(dist) # Instantiate optimizers optimizers = [GlobalBestPSO, LocalBestPSO, GeneralOptimizerPSO] parameters = dict( n_particles=n_particles, dimensions=dim, options=options, bounds=constraints, bh_strategy=""periodic"", velocity_clamp=(-0.5, 0.5), vh_strategy=""invert"", ) class TestToleranceOptions: @pytest.fixture(params=optimizers) def optimizer(self, request): global parameters if request.param.__name__ == ""GeneralOptimizerPSO"": return request.param, {**parameters, **{""topology"": Star()}} return request.param, parameters def test_no_ftol(self, optimizer): """"""Test complete run"""""" optm, params = optimizer opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) == iterations def test_ftol_effect(self, optimizer): """"""Test early stopping with ftol"""""" optm, params = optimizer params[""ftol""] = 0.01 opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) <= iterations def test_ftol_iter_assertion(self, optimizer): """"""Assert ftol_iter type and value"""""" with pytest.raises(AssertionError): optm, params = optimizer params[""ftol_iter""] = 0 opt = optm(**params) def test_ftol_iter_effect(self, optimizer): """"""Test early stopping with ftol and ftol_iter; must run for a minimum of ftol_iter iterations"""""" optm, params = optimizer params[""ftol_iter""] = 50 opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) >= opt.ftol_iter ", https://github.com/ljvmiranda921/pyswarms,08756526f39699eef28e515cac2ead17cef55710,tests/optimizers/test_tolerance.py::TestToleranceOptions::test_ftol_effect[GlobalBestPSO],tests/optimizers/test_tolerance.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- # Import standard libraries import pytest import random random.seed(0) # Import modules import numpy as np # Import from pyswarms from pyswarms.backend.topology import Star from pyswarms.single import GlobalBestPSO, LocalBestPSO, GeneralOptimizerPSO # Knapsack parameters capacity = 50 number_of_items = 10 item_range = range(number_of_items) # PARAMETERS value = [random.randint(1, number_of_items) for i in item_range] weight = [random.randint(1, number_of_items) for i in item_range] # PSO parameters n_particles = 10 iterations = 1000 options = {""c1"": 2, ""c2"": 2, ""w"": 0.7, ""k"": 3, ""p"": 2} dim = number_of_items LB = [0] * dim UB = [1] * dim constraints = (np.array(LB), np.array(UB)) kwargs = {""value"": value, ""weight"": weight, ""capacity"": capacity} def get_particle_obj(X, **kwargs): """""" Calculates the objective function value which is total revenue minus penalty of capacity violations"""""" # X is the decision variable. X is vector in the lenght of number of items # $ value of items value = kwargs[""value""] # weight of items weight = kwargs[""weight""] # Total revenue revenue = sum([value[i] * np.round(X[i]) for i in item_range]) # Total weight of selected items used_capacity = sum( [kwargs[""weight""][i] * np.round(X[i]) for i in item_range] ) # Total capacity violation with 100 as a penalty cofficient capacity_violation = 100 * min(0, capacity - used_capacity) # the objective function minimizes the negative revenue, which is the same # as maximizing the positive revenue return -1 * (revenue + capacity_violation) def objective_function(X, **kwargs): """"""Objective function with arguments"""""" n_particles_ = X.shape[0] dist = [get_particle_obj(X[i], **kwargs) for i in range(n_particles_)] return np.array(dist) # Instantiate optimizers optimizers = [GlobalBestPSO, LocalBestPSO, GeneralOptimizerPSO] parameters = dict( n_particles=n_particles, dimensions=dim, options=options, bounds=constraints, bh_strategy=""periodic"", velocity_clamp=(-0.5, 0.5), vh_strategy=""invert"", ) class TestToleranceOptions: @pytest.fixture(params=optimizers) def optimizer(self, request): global parameters if request.param.__name__ == ""GeneralOptimizerPSO"": return request.param, {**parameters, **{""topology"": Star()}} return request.param, parameters def test_no_ftol(self, optimizer): """"""Test complete run"""""" optm, params = optimizer opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) == iterations def test_ftol_effect(self, optimizer): """"""Test early stopping with ftol"""""" optm, params = optimizer params[""ftol""] = 0.01 opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) <= iterations def test_ftol_iter_assertion(self, optimizer): """"""Assert ftol_iter type and value"""""" with pytest.raises(AssertionError): optm, params = optimizer params[""ftol_iter""] = 0 opt = optm(**params) def test_ftol_iter_effect(self, optimizer): """"""Test early stopping with ftol and ftol_iter; must run for a minimum of ftol_iter iterations"""""" optm, params = optimizer params[""ftol_iter""] = 50 opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) >= opt.ftol_iter ", https://github.com/ljvmiranda921/pyswarms,08756526f39699eef28e515cac2ead17cef55710,tests/optimizers/test_tolerance.py::TestToleranceOptions::test_ftol_effect[LocalBestPSO],tests/optimizers/test_tolerance.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- # Import standard libraries import pytest import random random.seed(0) # Import modules import numpy as np # Import from pyswarms from pyswarms.backend.topology import Star from pyswarms.single import GlobalBestPSO, LocalBestPSO, GeneralOptimizerPSO # Knapsack parameters capacity = 50 number_of_items = 10 item_range = range(number_of_items) # PARAMETERS value = [random.randint(1, number_of_items) for i in item_range] weight = [random.randint(1, number_of_items) for i in item_range] # PSO parameters n_particles = 10 iterations = 1000 options = {""c1"": 2, ""c2"": 2, ""w"": 0.7, ""k"": 3, ""p"": 2} dim = number_of_items LB = [0] * dim UB = [1] * dim constraints = (np.array(LB), np.array(UB)) kwargs = {""value"": value, ""weight"": weight, ""capacity"": capacity} def get_particle_obj(X, **kwargs): """""" Calculates the objective function value which is total revenue minus penalty of capacity violations"""""" # X is the decision variable. X is vector in the lenght of number of items # $ value of items value = kwargs[""value""] # weight of items weight = kwargs[""weight""] # Total revenue revenue = sum([value[i] * np.round(X[i]) for i in item_range]) # Total weight of selected items used_capacity = sum( [kwargs[""weight""][i] * np.round(X[i]) for i in item_range] ) # Total capacity violation with 100 as a penalty cofficient capacity_violation = 100 * min(0, capacity - used_capacity) # the objective function minimizes the negative revenue, which is the same # as maximizing the positive revenue return -1 * (revenue + capacity_violation) def objective_function(X, **kwargs): """"""Objective function with arguments"""""" n_particles_ = X.shape[0] dist = [get_particle_obj(X[i], **kwargs) for i in range(n_particles_)] return np.array(dist) # Instantiate optimizers optimizers = [GlobalBestPSO, LocalBestPSO, GeneralOptimizerPSO] parameters = dict( n_particles=n_particles, dimensions=dim, options=options, bounds=constraints, bh_strategy=""periodic"", velocity_clamp=(-0.5, 0.5), vh_strategy=""invert"", ) class TestToleranceOptions: @pytest.fixture(params=optimizers) def optimizer(self, request): global parameters if request.param.__name__ == ""GeneralOptimizerPSO"": return request.param, {**parameters, **{""topology"": Star()}} return request.param, parameters def test_no_ftol(self, optimizer): """"""Test complete run"""""" optm, params = optimizer opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) == iterations def test_ftol_effect(self, optimizer): """"""Test early stopping with ftol"""""" optm, params = optimizer params[""ftol""] = 0.01 opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) <= iterations def test_ftol_iter_assertion(self, optimizer): """"""Assert ftol_iter type and value"""""" with pytest.raises(AssertionError): optm, params = optimizer params[""ftol_iter""] = 0 opt = optm(**params) def test_ftol_iter_effect(self, optimizer): """"""Test early stopping with ftol and ftol_iter; must run for a minimum of ftol_iter iterations"""""" optm, params = optimizer params[""ftol_iter""] = 50 opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) >= opt.ftol_iter ", https://github.com/ljvmiranda921/pyswarms,08756526f39699eef28e515cac2ead17cef55710,tests/optimizers/test_tolerance.py::TestToleranceOptions::test_no_ftol[GeneralOptimizerPSO],tests/optimizers/test_tolerance.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- # Import standard libraries import pytest import random random.seed(0) # Import modules import numpy as np # Import from pyswarms from pyswarms.backend.topology import Star from pyswarms.single import GlobalBestPSO, LocalBestPSO, GeneralOptimizerPSO # Knapsack parameters capacity = 50 number_of_items = 10 item_range = range(number_of_items) # PARAMETERS value = [random.randint(1, number_of_items) for i in item_range] weight = [random.randint(1, number_of_items) for i in item_range] # PSO parameters n_particles = 10 iterations = 1000 options = {""c1"": 2, ""c2"": 2, ""w"": 0.7, ""k"": 3, ""p"": 2} dim = number_of_items LB = [0] * dim UB = [1] * dim constraints = (np.array(LB), np.array(UB)) kwargs = {""value"": value, ""weight"": weight, ""capacity"": capacity} def get_particle_obj(X, **kwargs): """""" Calculates the objective function value which is total revenue minus penalty of capacity violations"""""" # X is the decision variable. X is vector in the lenght of number of items # $ value of items value = kwargs[""value""] # weight of items weight = kwargs[""weight""] # Total revenue revenue = sum([value[i] * np.round(X[i]) for i in item_range]) # Total weight of selected items used_capacity = sum( [kwargs[""weight""][i] * np.round(X[i]) for i in item_range] ) # Total capacity violation with 100 as a penalty cofficient capacity_violation = 100 * min(0, capacity - used_capacity) # the objective function minimizes the negative revenue, which is the same # as maximizing the positive revenue return -1 * (revenue + capacity_violation) def objective_function(X, **kwargs): """"""Objective function with arguments"""""" n_particles_ = X.shape[0] dist = [get_particle_obj(X[i], **kwargs) for i in range(n_particles_)] return np.array(dist) # Instantiate optimizers optimizers = [GlobalBestPSO, LocalBestPSO, GeneralOptimizerPSO] parameters = dict( n_particles=n_particles, dimensions=dim, options=options, bounds=constraints, bh_strategy=""periodic"", velocity_clamp=(-0.5, 0.5), vh_strategy=""invert"", ) class TestToleranceOptions: @pytest.fixture(params=optimizers) def optimizer(self, request): global parameters if request.param.__name__ == ""GeneralOptimizerPSO"": return request.param, {**parameters, **{""topology"": Star()}} return request.param, parameters def test_no_ftol(self, optimizer): """"""Test complete run"""""" optm, params = optimizer opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) == iterations def test_ftol_effect(self, optimizer): """"""Test early stopping with ftol"""""" optm, params = optimizer params[""ftol""] = 0.01 opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) <= iterations def test_ftol_iter_assertion(self, optimizer): """"""Assert ftol_iter type and value"""""" with pytest.raises(AssertionError): optm, params = optimizer params[""ftol_iter""] = 0 opt = optm(**params) def test_ftol_iter_effect(self, optimizer): """"""Test early stopping with ftol and ftol_iter; must run for a minimum of ftol_iter iterations"""""" optm, params = optimizer params[""ftol_iter""] = 50 opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) >= opt.ftol_iter ", https://github.com/ljvmiranda921/pyswarms,08756526f39699eef28e515cac2ead17cef55710,tests/optimizers/test_tolerance.py::TestToleranceOptions::test_no_ftol[GlobalBestPSO],tests/optimizers/test_tolerance.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- # Import standard libraries import pytest import random random.seed(0) # Import modules import numpy as np # Import from pyswarms from pyswarms.backend.topology import Star from pyswarms.single import GlobalBestPSO, LocalBestPSO, GeneralOptimizerPSO # Knapsack parameters capacity = 50 number_of_items = 10 item_range = range(number_of_items) # PARAMETERS value = [random.randint(1, number_of_items) for i in item_range] weight = [random.randint(1, number_of_items) for i in item_range] # PSO parameters n_particles = 10 iterations = 1000 options = {""c1"": 2, ""c2"": 2, ""w"": 0.7, ""k"": 3, ""p"": 2} dim = number_of_items LB = [0] * dim UB = [1] * dim constraints = (np.array(LB), np.array(UB)) kwargs = {""value"": value, ""weight"": weight, ""capacity"": capacity} def get_particle_obj(X, **kwargs): """""" Calculates the objective function value which is total revenue minus penalty of capacity violations"""""" # X is the decision variable. X is vector in the lenght of number of items # $ value of items value = kwargs[""value""] # weight of items weight = kwargs[""weight""] # Total revenue revenue = sum([value[i] * np.round(X[i]) for i in item_range]) # Total weight of selected items used_capacity = sum( [kwargs[""weight""][i] * np.round(X[i]) for i in item_range] ) # Total capacity violation with 100 as a penalty cofficient capacity_violation = 100 * min(0, capacity - used_capacity) # the objective function minimizes the negative revenue, which is the same # as maximizing the positive revenue return -1 * (revenue + capacity_violation) def objective_function(X, **kwargs): """"""Objective function with arguments"""""" n_particles_ = X.shape[0] dist = [get_particle_obj(X[i], **kwargs) for i in range(n_particles_)] return np.array(dist) # Instantiate optimizers optimizers = [GlobalBestPSO, LocalBestPSO, GeneralOptimizerPSO] parameters = dict( n_particles=n_particles, dimensions=dim, options=options, bounds=constraints, bh_strategy=""periodic"", velocity_clamp=(-0.5, 0.5), vh_strategy=""invert"", ) class TestToleranceOptions: @pytest.fixture(params=optimizers) def optimizer(self, request): global parameters if request.param.__name__ == ""GeneralOptimizerPSO"": return request.param, {**parameters, **{""topology"": Star()}} return request.param, parameters def test_no_ftol(self, optimizer): """"""Test complete run"""""" optm, params = optimizer opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) == iterations def test_ftol_effect(self, optimizer): """"""Test early stopping with ftol"""""" optm, params = optimizer params[""ftol""] = 0.01 opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) <= iterations def test_ftol_iter_assertion(self, optimizer): """"""Assert ftol_iter type and value"""""" with pytest.raises(AssertionError): optm, params = optimizer params[""ftol_iter""] = 0 opt = optm(**params) def test_ftol_iter_effect(self, optimizer): """"""Test early stopping with ftol and ftol_iter; must run for a minimum of ftol_iter iterations"""""" optm, params = optimizer params[""ftol_iter""] = 50 opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) >= opt.ftol_iter ", https://github.com/ljvmiranda921/pyswarms,08756526f39699eef28e515cac2ead17cef55710,tests/optimizers/test_tolerance.py::TestToleranceOptions::test_no_ftol[LocalBestPSO],tests/optimizers/test_tolerance.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- # Import standard libraries import pytest import random random.seed(0) # Import modules import numpy as np # Import from pyswarms from pyswarms.backend.topology import Star from pyswarms.single import GlobalBestPSO, LocalBestPSO, GeneralOptimizerPSO # Knapsack parameters capacity = 50 number_of_items = 10 item_range = range(number_of_items) # PARAMETERS value = [random.randint(1, number_of_items) for i in item_range] weight = [random.randint(1, number_of_items) for i in item_range] # PSO parameters n_particles = 10 iterations = 1000 options = {""c1"": 2, ""c2"": 2, ""w"": 0.7, ""k"": 3, ""p"": 2} dim = number_of_items LB = [0] * dim UB = [1] * dim constraints = (np.array(LB), np.array(UB)) kwargs = {""value"": value, ""weight"": weight, ""capacity"": capacity} def get_particle_obj(X, **kwargs): """""" Calculates the objective function value which is total revenue minus penalty of capacity violations"""""" # X is the decision variable. X is vector in the lenght of number of items # $ value of items value = kwargs[""value""] # weight of items weight = kwargs[""weight""] # Total revenue revenue = sum([value[i] * np.round(X[i]) for i in item_range]) # Total weight of selected items used_capacity = sum( [kwargs[""weight""][i] * np.round(X[i]) for i in item_range] ) # Total capacity violation with 100 as a penalty cofficient capacity_violation = 100 * min(0, capacity - used_capacity) # the objective function minimizes the negative revenue, which is the same # as maximizing the positive revenue return -1 * (revenue + capacity_violation) def objective_function(X, **kwargs): """"""Objective function with arguments"""""" n_particles_ = X.shape[0] dist = [get_particle_obj(X[i], **kwargs) for i in range(n_particles_)] return np.array(dist) # Instantiate optimizers optimizers = [GlobalBestPSO, LocalBestPSO, GeneralOptimizerPSO] parameters = dict( n_particles=n_particles, dimensions=dim, options=options, bounds=constraints, bh_strategy=""periodic"", velocity_clamp=(-0.5, 0.5), vh_strategy=""invert"", ) class TestToleranceOptions: @pytest.fixture(params=optimizers) def optimizer(self, request): global parameters if request.param.__name__ == ""GeneralOptimizerPSO"": return request.param, {**parameters, **{""topology"": Star()}} return request.param, parameters def test_no_ftol(self, optimizer): """"""Test complete run"""""" optm, params = optimizer opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) == iterations def test_ftol_effect(self, optimizer): """"""Test early stopping with ftol"""""" optm, params = optimizer params[""ftol""] = 0.01 opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) <= iterations def test_ftol_iter_assertion(self, optimizer): """"""Assert ftol_iter type and value"""""" with pytest.raises(AssertionError): optm, params = optimizer params[""ftol_iter""] = 0 opt = optm(**params) def test_ftol_iter_effect(self, optimizer): """"""Test early stopping with ftol and ftol_iter; must run for a minimum of ftol_iter iterations"""""" optm, params = optimizer params[""ftol_iter""] = 50 opt = optm(**params) opt.optimize(objective_function, iters=iterations, **kwargs) assert len(opt.cost_history) >= opt.ftol_iter ", https://github.com/lrowe/pytest_exact_fixtures,b72bf9d7661c65e1535e5fbbddc343fd6120e15c,test_exact_fixtures.py::test_alternative1,test_exact_fixtures.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest active = set() @pytest.yield_fixture(scope='session') def alternative1(): active.add('alternative1') yield active.remove('alternative1') @pytest.yield_fixture(scope='session') def alternative2(): active.add('alternative2') yield active.remove('alternative2') def test_alternative1(alternative1): assert active == set(['alternative1']) def test_alternative2(alternative2): assert active == set(['alternative2']) ", https://github.com/lrowe/pytest_exact_fixtures,b72bf9d7661c65e1535e5fbbddc343fd6120e15c,test_exact_fixtures.py::test_alternative2,test_exact_fixtures.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest active = set() @pytest.yield_fixture(scope='session') def alternative1(): active.add('alternative1') yield active.remove('alternative1') @pytest.yield_fixture(scope='session') def alternative2(): active.add('alternative2') yield active.remove('alternative2') def test_alternative1(alternative1): assert active == set(['alternative1']) def test_alternative2(alternative2): assert active == set(['alternative2']) ", https://github.com/maboualidev/PyJsonFriendly,8b1ee498bc7a27024851b5101bf09913031e2477,tests/test_pyJsonFriendly.py::TestPyJsonFriendly::test_01,tests/test_pyJsonFriendly.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import json from unittest import TestCase # import PyJsonFriendly from PyJsonFriendly import JsonFriendly class NotJsonFriendly: """""" Using ``json.dumps(o)`` where ``o`` is an object of ``NotJsonFriendly`` will fail with a ``TypeError`` exception. """""" def __init__(self, field1, field2): self._field1 = field1 self._field2 = field2 class DerivedJsonFriendly(JsonFriendly): """""" Using ``json.dumps(o)`` where ``o`` is an object of ``DerivedJsonFriendly`` will not fail. Since this class is derived from ``PyJsonFriendly`` you would be forced to implement ``_as_json_friendly_obj(self)`` within the class; or you must declare the class as an Abstract class, which means you would not be able to instantiate an object of it directly. you would need to add ``from PyJsonFriendly import PyJsonFriendly`` to your code as well. This is the preferred approach. """""" def __init__(self, field1, field2): self._field1 = field1 self._field2 = field2 def _as_json_friendly_obj(self): return {""field1"": self._field1, ""field2"": self._field2} class NonDerivedJsonFriendly: """""" Using ``json.dumps(o)`` where ``o`` is an object of ``NonDerivedJsonFriendly`` will not fail, as long as you have ``from PyJsonFriendly import PyJsonFriendly`` in your code and the object has implemented a method called: ``_as_json_friendly_obj(self):``. Unlike the ``DerivedJsonFriendly`` class, this class is not extending ``PyJsonFriendly``. Hence, you are not forced to implement the ``_as_json_friendly_obj(self):``. However, forgetting to implement this method in your class will result in failing calls to ``json.dumps(o)`` if ``o`` is an object of ``NonDerivedJsonFriendly`` """""" def __init__(self, field1, field2): self._field1 = field1 self._field2 = field2 def _as_json_friendly_obj(self): return {""field1"": self._field1, ""field2"": self._field2} class JsonFriendlyNoneDictOutput(JsonFriendly): def __init__(self, field1, field2): self._field1 = field1 self._field2 = field2 def _as_json_friendly_obj(self): return [self._field1, self._field2] class TestPyJsonFriendly(TestCase): def test_01(self): o = NotJsonFriendly(1, 2) with self.assertRaises(TypeError): json.dumps(o) def test_02(self): o = DerivedJsonFriendly(3, 4) self.assertEqual('{""field1"": 3, ""field2"": 4}', json.dumps(o)) def test_03(self): o = NonDerivedJsonFriendly(5, 6) self.assertEqual('{""field1"": 5, ""field2"": 6}', json.dumps(o)) def test_04(self): o = NotJsonFriendly(7, 8) with self.assertRaises(TypeError): json.dumps(o) # you could add the necessary function during the runtime as well. (A python thing) o.__class__._as_json_friendly_obj = lambda _: {""field1"": _._field1, ""field2"": _._field2} self.assertEqual('{""field1"": 7, ""field2"": 8}', json.dumps(o)) def test_05(self): o = JsonFriendlyNoneDictOutput(9, 10) self.assertEqual('[9, 10]', json.dumps(o)) ", https://github.com/mahmoud/lithoxyl,9961553a065a1ad8bb7a2407fe74f2cfd6c79a20,lithoxyl/tests/test_stats.py::test_acc_random,lithoxyl/tests/test_stats.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- import os import random from lithoxyl.moment import MomentAccumulator from lithoxyl.quantile import ReservoirAccumulator, P2Accumulator from boltons import statsutils as _statsutils random.seed(8675309) test_sets = {'urandom 0-255': [x if isinstance(x, int) else ord(x) for x in os.urandom(16000)], # conditional for py23 'random.random 0.0-1.0': [random.random() for i in range(100000)]} def _assert_round_cmp(a, b, mag=3, name=None): thresh = 1.0 / (10 ** mag) abs_diff = round(abs(a - b), mag + 1) tmpl = 'round-compare failed at %d digits (%f - %f = %f > %f)' rel_diff = (2 * abs_diff) / (a + b) err_msg = tmpl % (mag, a, b, abs_diff, thresh) if name: err_msg = '%r %s' % (name, err_msg) assert rel_diff < thresh, err_msg return True def test_momentacc_basic(): for name, data in test_sets.items(): ma = MomentAccumulator() for v in data: ma.add(v) for m_name in ('mean', 'variance', 'std_dev', 'skewness', 'kurtosis'): ma_val = getattr(ma, m_name) ctl_val = getattr(_statsutils, m_name)(data) _assert_round_cmp(ctl_val, ma_val, mag=4, name=m_name) return True def test_momentacc_norm(): ma = MomentAccumulator() for v in [random.gauss(10, 4) for i in range(5000)]: ma.add(v) _assert_round_cmp(10, abs(ma.mean), mag=1) _assert_round_cmp(4, ma.std_dev, mag=1) _assert_round_cmp(0, ma.skewness, mag=1) _assert_round_cmp(3, ma.kurtosis, mag=1) def test_quantacc_basic(data=None): data = data or range(31) qa = ReservoirAccumulator() for v in data: qa.add(v) assert qa.median == _statsutils.median(data) q1, q2, q3 = qa.quartiles assert q1 < q2 < q3 return True def test_quantacc(): for name, data in test_sets.items(): qa = ReservoirAccumulator() for v in data: qa.add(v) _assert_round_cmp(qa.median, _statsutils.median(data), mag=6) q1, q2, q3 = qa.quartiles assert q1 < q2 < q3 hist = qa.get_histogram() assert hist #print; import pprint; pprint.pprint(hist) #print sum([x.count for x in hist]), 'histogram item count' def test_p2quantacc(): for name, data in test_sets.items(): qa = ReservoirAccumulator() p2qa = P2Accumulator() for i, v in enumerate(data): p2qa.add(v) qa.add(v) if i and i % 1000 == 0: _assert_round_cmp(qa.median, p2qa.median, mag=1, name='%s median' % name) #print i, qa.median, p2qa.median _assert_round_cmp(qa.median, p2qa.median, mag=2, name='%s median' % name) def test_acc_random(): data = test_sets['random.random 0.0-1.0'] qa = ReservoirAccumulator(data) capqa = ReservoirAccumulator(data, cap=True) p2qa = P2Accumulator(data) for acc in (qa, capqa, p2qa): for qp, v in acc.get_quantiles(): if qp > 0: assert 0.95 < (v / qp) < 1.05 ", https://github.com/mahmoud/lithoxyl,9961553a065a1ad8bb7a2407fe74f2cfd6c79a20,lithoxyl/tests/test_stats.py::test_momentacc_norm,lithoxyl/tests/test_stats.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- import os import random from lithoxyl.moment import MomentAccumulator from lithoxyl.quantile import ReservoirAccumulator, P2Accumulator from boltons import statsutils as _statsutils random.seed(8675309) test_sets = {'urandom 0-255': [x if isinstance(x, int) else ord(x) for x in os.urandom(16000)], # conditional for py23 'random.random 0.0-1.0': [random.random() for i in range(100000)]} def _assert_round_cmp(a, b, mag=3, name=None): thresh = 1.0 / (10 ** mag) abs_diff = round(abs(a - b), mag + 1) tmpl = 'round-compare failed at %d digits (%f - %f = %f > %f)' rel_diff = (2 * abs_diff) / (a + b) err_msg = tmpl % (mag, a, b, abs_diff, thresh) if name: err_msg = '%r %s' % (name, err_msg) assert rel_diff < thresh, err_msg return True def test_momentacc_basic(): for name, data in test_sets.items(): ma = MomentAccumulator() for v in data: ma.add(v) for m_name in ('mean', 'variance', 'std_dev', 'skewness', 'kurtosis'): ma_val = getattr(ma, m_name) ctl_val = getattr(_statsutils, m_name)(data) _assert_round_cmp(ctl_val, ma_val, mag=4, name=m_name) return True def test_momentacc_norm(): ma = MomentAccumulator() for v in [random.gauss(10, 4) for i in range(5000)]: ma.add(v) _assert_round_cmp(10, abs(ma.mean), mag=1) _assert_round_cmp(4, ma.std_dev, mag=1) _assert_round_cmp(0, ma.skewness, mag=1) _assert_round_cmp(3, ma.kurtosis, mag=1) def test_quantacc_basic(data=None): data = data or range(31) qa = ReservoirAccumulator() for v in data: qa.add(v) assert qa.median == _statsutils.median(data) q1, q2, q3 = qa.quartiles assert q1 < q2 < q3 return True def test_quantacc(): for name, data in test_sets.items(): qa = ReservoirAccumulator() for v in data: qa.add(v) _assert_round_cmp(qa.median, _statsutils.median(data), mag=6) q1, q2, q3 = qa.quartiles assert q1 < q2 < q3 hist = qa.get_histogram() assert hist #print; import pprint; pprint.pprint(hist) #print sum([x.count for x in hist]), 'histogram item count' def test_p2quantacc(): for name, data in test_sets.items(): qa = ReservoirAccumulator() p2qa = P2Accumulator() for i, v in enumerate(data): p2qa.add(v) qa.add(v) if i and i % 1000 == 0: _assert_round_cmp(qa.median, p2qa.median, mag=1, name='%s median' % name) #print i, qa.median, p2qa.median _assert_round_cmp(qa.median, p2qa.median, mag=2, name='%s median' % name) def test_acc_random(): data = test_sets['random.random 0.0-1.0'] qa = ReservoirAccumulator(data) capqa = ReservoirAccumulator(data, cap=True) p2qa = P2Accumulator(data) for acc in (qa, capqa, p2qa): for qp, v in acc.get_quantiles(): if qp > 0: assert 0.95 < (v / qp) < 1.05 ", https://github.com/malongge/exceltojson,6724c7de5153a7b125011e879419273589206d0d,tests/test_excel_2_json.py::TestProcessExcel::test_excel_process_not_patch_sheet_names,tests/test_excel_2_json.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# -*- coding: utf-8 -*- from __future__ import unicode_literals import os import json from functools import partial import pytest from exceltojson.excel2json import (_RowProcess, _ColProcess, _SheetProcess, ProcessExcel) from exceltojson.utils import (get_sheets, get_data_path, clear_json_files) from exceltojson.excel2json import open def test_row_process(): col_headers = ['col1', 'col2', 'col3'] start_col = 1 sheet = get_sheets(get_data_path('test_row_process.xlsx'))[0] row_process = _RowProcess(sheet, col_headers, start_col) assert row_process(1) == {'col1': u""内容1"", ""col2"": u""内容2"", ""col3"": u""内容3""} assert row_process(2) is None assert row_process(3) == {'col1': ""content1"", ""col2"": ""content2"", ""col3"": ""content3""} def test_col_process(): sheet = get_sheets(get_data_path('test_col_process.xlsx'))[0] alias = {u'头部': 'header3'} col_process = _ColProcess(sheet, alias, 15) ret = col_process() assert ret[0] == 3 assert ret[1] == ['header1', 'header3', 'header2'] # alias set invalid alias = {u'头': 'header3'} col_process = _ColProcess(sheet, alias, 15) with pytest.raises(ValueError): col_process() # header index not invalid with pytest.raises(ValueError): _ColProcess(sheet, alias, 16)() # header duplicate alias = {u'头部': 'header2'} with pytest.raises(ValueError): _ColProcess(sheet, alias, 15)() def test_col_process_header_invalid(): sheet = get_sheets(get_data_path('test_col_process_invalid_header.xlsx'))[0] with pytest.raises(ValueError): _ColProcess(sheet, {'头部': 'header3'}, 15)() def test_sheet_process(): sheet = get_sheets(get_data_path('test_sheet_process.xlsx'))[0] alias = {'头部': 'header3'} sheet_process = _SheetProcess(sheet, alias, merge_cell=True) data = [value for value in sheet_process()] assert data[0][0] == 10 assert data[0][1] == {'header1': '内容1', 'header3': '内容2', 'header2': '内容3'} assert data[2][0] == 13 assert data[2][1] == {'header1': 'test1', 'header3': 'conten2', 'header2': 'test3'} def test_time_cell_process(): sheet = get_sheets(get_data_path('test_time_cell_process.xlsx'))[0] sheet_process = _SheetProcess(sheet, merge_cell=True) data = [value for value in sheet_process()] assert data[0][0] == 10 assert data[0][1] == {'time': '2016/11/16', 'header3': '内容2', 'header2': '内容3'} class TestProcessExcel: @classmethod def setup_class(cls): """"""simple ProcessExcel instance :return: """""" cls.process_excel = partial(ProcessExcel, get_data_path('test_excel_process.xlsx'), get_data_path('.')) def setup_method(self, method): """""" every test should clear the json files """""" clear_json_files() @classmethod def teardown_class(cls): """""" after test clear the temporary test files """""" clear_json_files() def test_excel_process_with_show_row(self): excel = self.process_excel() # sheet small should not split excel(100) assert os.path.exists(get_data_path('sheet-20.json')) is False excel(max_row=5) # big excel file should be split assert os.path.exists(get_data_path('sheet-20.json')) assert os.path.exists(get_data_path('sheet-2.json')) # split file should be right json with open(get_data_path('sheet-20.json'), encoding='utf-8') as f: assert json.load(f) == { '9': { 'header1': u'内容6', 'header3': u'内容7', 'header2': u'内容8' }, '10': { 'header1': u'内容7', 'header3': u'内容8', 'header2': u'内容9' } } # other sheet should be convert to json file assert os.path.exists(get_data_path('sheet-0.json')) assert os.path.exists(get_data_path('sheet-1.json')) with open(get_data_path('sheet-0.json'), encoding='utf-8') as f: assert json.load(f) == { '2': { 'header1': u'内容1', u'头部': u'内容2', 'header2': u'内容3' } } def test_excel_process_with_no_show_row(self): excel = self.process_excel(show_row=False) excel(10) with open(get_data_path('sheet-0.json'), encoding='utf-8') as f: assert json.load(f) == [ { 'header1': u'内容1', u'头部': u'内容2', 'header2': u'内容3' } ] def test_excel_process_with_alias(self): excel = self.process_excel(show_row=False, index_sheets={'0': { u'头部': 'header3' }, 1: None}, patch_sheet_alias=False) excel(10) with open(get_data_path('sheet-0.json'), encoding='utf-8') as f: assert json.load(f) == [ { 'header1': u'内容1', 'header3': u'内容2', 'header2': u'内容3' } ] # now sheet index 2 not exist assert os.path.exists(get_data_path('sheet-2.json')) is False def test_excel_process_patch_alias(self): excel = self.process_excel(show_row=False, index_sheets={0: { u'头部': 'header3' }}, patch_sheet_alias=True) excel(10) # now sheet index 2 should exist assert os.path.exists(get_data_path('sheet-2.json')) def test_excel_process_with_sheet_names(self): excel = self.process_excel(show_row=False, name_sheets={u'名字': { u'头部': 'header3' }}, patch_sheet_alias=True) excel(10) # json file name should be sheet name assert os.path.exists(get_data_path(u'名字.json')) assert os.path.exists(get_data_path('Sheet2.json')) assert os.path.exists(get_data_path('Sheet3.json')) def test_excel_process_not_patch_sheet_names(self): excel = self.process_excel(show_row=False, name_sheets={u'名字': { u'头部': 'header3' }}, patch_sheet_alias=False) excel(10) assert os.path.exists(get_data_path(u'名字.json')) assert os.path.exists(get_data_path('Sheet2.json')) is False def test_excel_process_sheet_index_not_invalid(self): # sheet index large not invalid with pytest.raises(ValueError): self.process_excel(show_row=False, index_sheets={5: { u'头部': 'header3' }}, patch_sheet_alias=True) # sheet index not int value or int str with pytest.raises(ValueError): self.process_excel(show_row=False, index_sheets={'a': { u'头部': 'header3' }}, patch_sheet_alias=True) def test_excel_process_sheet_name_not_invalid(self): # sheet index large not invalid with pytest.raises(ValueError): self.process_excel(show_row=False, name_sheets={'a': { u'头部': 'header3' }}, patch_sheet_alias=True) ", https://github.com/malongge/exceltojson,6724c7de5153a7b125011e879419273589206d0d,tests/test_excel_2_json.py::TestProcessExcel::test_excel_process_with_sheet_names,tests/test_excel_2_json.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# -*- coding: utf-8 -*- from __future__ import unicode_literals import os import json from functools import partial import pytest from exceltojson.excel2json import (_RowProcess, _ColProcess, _SheetProcess, ProcessExcel) from exceltojson.utils import (get_sheets, get_data_path, clear_json_files) from exceltojson.excel2json import open def test_row_process(): col_headers = ['col1', 'col2', 'col3'] start_col = 1 sheet = get_sheets(get_data_path('test_row_process.xlsx'))[0] row_process = _RowProcess(sheet, col_headers, start_col) assert row_process(1) == {'col1': u""内容1"", ""col2"": u""内容2"", ""col3"": u""内容3""} assert row_process(2) is None assert row_process(3) == {'col1': ""content1"", ""col2"": ""content2"", ""col3"": ""content3""} def test_col_process(): sheet = get_sheets(get_data_path('test_col_process.xlsx'))[0] alias = {u'头部': 'header3'} col_process = _ColProcess(sheet, alias, 15) ret = col_process() assert ret[0] == 3 assert ret[1] == ['header1', 'header3', 'header2'] # alias set invalid alias = {u'头': 'header3'} col_process = _ColProcess(sheet, alias, 15) with pytest.raises(ValueError): col_process() # header index not invalid with pytest.raises(ValueError): _ColProcess(sheet, alias, 16)() # header duplicate alias = {u'头部': 'header2'} with pytest.raises(ValueError): _ColProcess(sheet, alias, 15)() def test_col_process_header_invalid(): sheet = get_sheets(get_data_path('test_col_process_invalid_header.xlsx'))[0] with pytest.raises(ValueError): _ColProcess(sheet, {'头部': 'header3'}, 15)() def test_sheet_process(): sheet = get_sheets(get_data_path('test_sheet_process.xlsx'))[0] alias = {'头部': 'header3'} sheet_process = _SheetProcess(sheet, alias, merge_cell=True) data = [value for value in sheet_process()] assert data[0][0] == 10 assert data[0][1] == {'header1': '内容1', 'header3': '内容2', 'header2': '内容3'} assert data[2][0] == 13 assert data[2][1] == {'header1': 'test1', 'header3': 'conten2', 'header2': 'test3'} def test_time_cell_process(): sheet = get_sheets(get_data_path('test_time_cell_process.xlsx'))[0] sheet_process = _SheetProcess(sheet, merge_cell=True) data = [value for value in sheet_process()] assert data[0][0] == 10 assert data[0][1] == {'time': '2016/11/16', 'header3': '内容2', 'header2': '内容3'} class TestProcessExcel: @classmethod def setup_class(cls): """"""simple ProcessExcel instance :return: """""" cls.process_excel = partial(ProcessExcel, get_data_path('test_excel_process.xlsx'), get_data_path('.')) def setup_method(self, method): """""" every test should clear the json files """""" clear_json_files() @classmethod def teardown_class(cls): """""" after test clear the temporary test files """""" clear_json_files() def test_excel_process_with_show_row(self): excel = self.process_excel() # sheet small should not split excel(100) assert os.path.exists(get_data_path('sheet-20.json')) is False excel(max_row=5) # big excel file should be split assert os.path.exists(get_data_path('sheet-20.json')) assert os.path.exists(get_data_path('sheet-2.json')) # split file should be right json with open(get_data_path('sheet-20.json'), encoding='utf-8') as f: assert json.load(f) == { '9': { 'header1': u'内容6', 'header3': u'内容7', 'header2': u'内容8' }, '10': { 'header1': u'内容7', 'header3': u'内容8', 'header2': u'内容9' } } # other sheet should be convert to json file assert os.path.exists(get_data_path('sheet-0.json')) assert os.path.exists(get_data_path('sheet-1.json')) with open(get_data_path('sheet-0.json'), encoding='utf-8') as f: assert json.load(f) == { '2': { 'header1': u'内容1', u'头部': u'内容2', 'header2': u'内容3' } } def test_excel_process_with_no_show_row(self): excel = self.process_excel(show_row=False) excel(10) with open(get_data_path('sheet-0.json'), encoding='utf-8') as f: assert json.load(f) == [ { 'header1': u'内容1', u'头部': u'内容2', 'header2': u'内容3' } ] def test_excel_process_with_alias(self): excel = self.process_excel(show_row=False, index_sheets={'0': { u'头部': 'header3' }, 1: None}, patch_sheet_alias=False) excel(10) with open(get_data_path('sheet-0.json'), encoding='utf-8') as f: assert json.load(f) == [ { 'header1': u'内容1', 'header3': u'内容2', 'header2': u'内容3' } ] # now sheet index 2 not exist assert os.path.exists(get_data_path('sheet-2.json')) is False def test_excel_process_patch_alias(self): excel = self.process_excel(show_row=False, index_sheets={0: { u'头部': 'header3' }}, patch_sheet_alias=True) excel(10) # now sheet index 2 should exist assert os.path.exists(get_data_path('sheet-2.json')) def test_excel_process_with_sheet_names(self): excel = self.process_excel(show_row=False, name_sheets={u'名字': { u'头部': 'header3' }}, patch_sheet_alias=True) excel(10) # json file name should be sheet name assert os.path.exists(get_data_path(u'名字.json')) assert os.path.exists(get_data_path('Sheet2.json')) assert os.path.exists(get_data_path('Sheet3.json')) def test_excel_process_not_patch_sheet_names(self): excel = self.process_excel(show_row=False, name_sheets={u'名字': { u'头部': 'header3' }}, patch_sheet_alias=False) excel(10) assert os.path.exists(get_data_path(u'名字.json')) assert os.path.exists(get_data_path('Sheet2.json')) is False def test_excel_process_sheet_index_not_invalid(self): # sheet index large not invalid with pytest.raises(ValueError): self.process_excel(show_row=False, index_sheets={5: { u'头部': 'header3' }}, patch_sheet_alias=True) # sheet index not int value or int str with pytest.raises(ValueError): self.process_excel(show_row=False, index_sheets={'a': { u'头部': 'header3' }}, patch_sheet_alias=True) def test_excel_process_sheet_name_not_invalid(self): # sheet index large not invalid with pytest.raises(ValueError): self.process_excel(show_row=False, name_sheets={'a': { u'头部': 'header3' }}, patch_sheet_alias=True) ", https://github.com/malpunek/dame,d6568d3088dd0a8b87eb5d278e04ab857a622418,tests/test_versionable.py::test_basic,tests/test_versionable.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"# TODO show some love to these tests, rethink, cleanup and write the docs! from tempfile import TemporaryDirectory from pathlib import Path from importlib import import_module, reload import fileinput from dame.versionable import Versionable, Unversionable code_base = """""" from dame.versionable import Versionable class TestT(Versionable): {} {} def __init__(self, **kwargs): self.register_params(**kwargs) for key, value in kwargs.items(): setattr(self, key, value) {} """""" dir_path = Path(__file__).resolve().parent tmp_dir = TemporaryDirectory(dir=dir_path) open(Path(tmp_dir.name) / ""__init__.py"", ""w"").close() def make_versionable(doc, attrs, apply): def indent(code, spaces): return """".join(f""{' ' * spaces}{line}\n"" for line in code.split(""\n"")) doc_code = f'""""""{doc}""""""' attr_code = """".join(f""{key} = {value}\n"" for key, value in attrs.items()) attr_code = indent(attr_code, 4) apply_code = indent(apply, 8)[4:] return code_base.format(doc_code, attr_code, apply_code) class tmp_module: def __init__(self, name, content): self.fname = f""{name}.py"" self.mod_name = Path(tmp_dir.name).name self.path = Path(tmp_dir.name) / self.fname self.content = content def __enter__(self): self.f = open(self.path, ""w+"") self.f.write(self.content) self.f.close() imp_name = f"".{self.mod_name}.{self.fname[:-3]}"" self.mod = import_module(imp_name, package=""tests"") return self.mod def __exit__(self, exc_type, exc_value, traceback): return False def test_basic(): with tmp_module(""transformA"", make_versionable("""", {}, """")) as ta: tae1 = ta.TestT() tae2 = ta.TestT() assert tae1.version() == tae2.version() def test_params(): with tmp_module(""transformA"", make_versionable("""", {}, """")) as ta: tak1 = ta.TestT(ala=""kota"") tak2 = ta.TestT(basia=""kota"") tak3 = ta.TestT(ala=""psa"") tak4 = ta.TestT(ala=""kota"") assert tak1.version() == tak4.version() assert tak1.version() != tak2.version() assert tak1.version() != tak3.version() assert tak2.version() != tak3.version() def test_code(): code = ""def whatever():\npass"" with tmp_module(""transA"", make_versionable("""", {}, code)) as ta: tt = ta.TestT() ver1 = tt.version() for line in fileinput.input(files=(ta.__file__,), inplace=True): if ""whatever"" in line: print("" def whatever2():"") else: print(line[:-1]) ta = reload(ta) tt2 = ta.TestT() ver2 = tt2.version() assert ver1 != ver2 class X(Versionable): def x(): pass class Y(Versionable): def y(): pass def test_version_str(): x, y = X(), Y() assert x.version() != y.version() x.__version_str__ = ""1"" y.__version_str__ = ""1"" assert x.version() == y.version() class Z(Unversionable): pass def test_unversionable(): p, q = Z(), Z() assert p.version() != q.version() ", https://github.com/malpunek/dame,d6568d3088dd0a8b87eb5d278e04ab857a622418,tests/test_versionable.py::test_params,tests/test_versionable.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"# TODO show some love to these tests, rethink, cleanup and write the docs! from tempfile import TemporaryDirectory from pathlib import Path from importlib import import_module, reload import fileinput from dame.versionable import Versionable, Unversionable code_base = """""" from dame.versionable import Versionable class TestT(Versionable): {} {} def __init__(self, **kwargs): self.register_params(**kwargs) for key, value in kwargs.items(): setattr(self, key, value) {} """""" dir_path = Path(__file__).resolve().parent tmp_dir = TemporaryDirectory(dir=dir_path) open(Path(tmp_dir.name) / ""__init__.py"", ""w"").close() def make_versionable(doc, attrs, apply): def indent(code, spaces): return """".join(f""{' ' * spaces}{line}\n"" for line in code.split(""\n"")) doc_code = f'""""""{doc}""""""' attr_code = """".join(f""{key} = {value}\n"" for key, value in attrs.items()) attr_code = indent(attr_code, 4) apply_code = indent(apply, 8)[4:] return code_base.format(doc_code, attr_code, apply_code) class tmp_module: def __init__(self, name, content): self.fname = f""{name}.py"" self.mod_name = Path(tmp_dir.name).name self.path = Path(tmp_dir.name) / self.fname self.content = content def __enter__(self): self.f = open(self.path, ""w+"") self.f.write(self.content) self.f.close() imp_name = f"".{self.mod_name}.{self.fname[:-3]}"" self.mod = import_module(imp_name, package=""tests"") return self.mod def __exit__(self, exc_type, exc_value, traceback): return False def test_basic(): with tmp_module(""transformA"", make_versionable("""", {}, """")) as ta: tae1 = ta.TestT() tae2 = ta.TestT() assert tae1.version() == tae2.version() def test_params(): with tmp_module(""transformA"", make_versionable("""", {}, """")) as ta: tak1 = ta.TestT(ala=""kota"") tak2 = ta.TestT(basia=""kota"") tak3 = ta.TestT(ala=""psa"") tak4 = ta.TestT(ala=""kota"") assert tak1.version() == tak4.version() assert tak1.version() != tak2.version() assert tak1.version() != tak3.version() assert tak2.version() != tak3.version() def test_code(): code = ""def whatever():\npass"" with tmp_module(""transA"", make_versionable("""", {}, code)) as ta: tt = ta.TestT() ver1 = tt.version() for line in fileinput.input(files=(ta.__file__,), inplace=True): if ""whatever"" in line: print("" def whatever2():"") else: print(line[:-1]) ta = reload(ta) tt2 = ta.TestT() ver2 = tt2.version() assert ver1 != ver2 class X(Versionable): def x(): pass class Y(Versionable): def y(): pass def test_version_str(): x, y = X(), Y() assert x.version() != y.version() x.__version_str__ = ""1"" y.__version_str__ = ""1"" assert x.version() == y.version() class Z(Unversionable): pass def test_unversionable(): p, q = Z(), Z() assert p.version() != q.version() ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_max_page_size[app_fixture0-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_max_page_size[app_fixture1-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_max_page_size[app_fixture2-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_max_page_size[app_fixture3-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_max_page_size[app_fixture4-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_max_page_size[app_fixture5-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_max_page_size[app_fixture6-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_max_page_size[app_fixture7-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_min_page_page_size[app_fixture0-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_min_page_page_size[app_fixture1-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_min_page_page_size[app_fixture2-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_min_page_page_size[app_fixture3-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_min_page_page_size[app_fixture4-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_min_page_page_size[app_fixture5-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_min_page_page_size[app_fixture6-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/marshmallow-code/flask-smorest,02e9e84def5ace80522c76b830fbef9930a28771,tests/test_pagination.py::TestPagination::test_pagination_min_page_page_size[app_fixture7-AppConfig-1000],tests/test_pagination.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Test pagination feature"""""" from itertools import product from collections import namedtuple import json from unittest import mock import pytest from flask.views import MethodView from flask_smorest import Api, Blueprint, Page from flask_smorest.pagination import PaginationParameters from .utils import get_schemas CUSTOM_PAGINATION_PARAMS = (2, 5, 10) def pagination_blueprint(collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get(self, pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate( page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(pagination_parameters): pagination_parameters.item_count = len(collection.items) return collection.items[ pagination_parameters.first_item: pagination_parameters.last_item + 1 ] return blp def post_pagination_blueprint( collection, schemas, as_method_view, custom_params): """"""Return a basic API sample with post-pagination"""""" blp = Blueprint('test', __name__, url_prefix='/test') if custom_params: page, page_size, max_page_size = CUSTOM_PAGINATION_PARAMS else: page, page_size, max_page_size = None, None, None if as_method_view: @blp.route('/') class Resource(MethodView): @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get(self): return collection.items else: @blp.route('/') @blp.response(schemas.DocSchema(many=True)) @blp.paginate(Page, page=page, page_size=page_size, max_page_size=max_page_size) def get_resources(): return collection.items return blp @pytest.fixture(params=product( # Pagination in function/ post-pagination (pagination_blueprint, post_pagination_blueprint), # MethodView (True, False), # Custom parameters (True, False), )) def app_fixture(request, collection, schemas, app): """"""Return an app client for each configuration - pagination in function / post-pagination - function / method view - default / custom pagination parameters """""" blp_factory, as_method_view, custom_params = request.param blueprint = blp_factory(collection, schemas, as_method_view, custom_params) api = Api(app) api.register_blueprint(blueprint) return namedtuple('AppFixture', ('client', 'custom_params'))( app.test_client(), custom_params) class TestPagination: def test_pagination_parameters_repr(self): assert(repr(PaginationParameters(1, 10)) == ""PaginationParameters(page=1,page_size=10)"") def test_page_repr(self): page_params = PaginationParameters(1, 2) assert (repr(Page([1, 2, 3, 4, 5], page_params)) == ""Page(collection=[1, 2, 3, 4, 5],page_params={})"" .format(repr(page_params))) @pytest.mark.parametrize('header_name', ('X-Dummy-Name', None)) def test_pagination_custom_header_field_name(self, app, header_name): """"""Test PAGINATION_HEADER_FIELD_NAME overriding"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is not None: assert response.headers[header_name] == ( '{""total"": 2, ""total_pages"": 1, ' '""first_page"": 1, ""last_page"": 1, ""page"": 1}' ) # Also check there is only one pagination header assert len(response.headers.getlist(header_name)) == 1 def test_pagination_header_documentation(self, app): """"""Test pagination header is documented"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = 'X-Custom-Pagination-Header' blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): """"""Dummy view func"""""" api.register_blueprint(blp) spec = api.spec.to_dict() get = spec['paths']['/test/']['get'] assert 'PaginationMetadata' in get_schemas(api.spec) assert get['responses']['200']['headers'] == { 'X-Custom-Pagination-Header': { 'description': 'Pagination metadata', 'schema': {'$ref': '#/components/schemas/PaginationMetadata'}, } } @pytest.mark.parametrize('header_name', ('X-Pagination', None)) def test_pagination_item_count_missing(self, app, header_name): """"""If item_count was not set, pass and warn"""""" api = Api(app) class CustomBlueprint(Blueprint): PAGINATION_HEADER_FIELD_NAME = header_name blp = CustomBlueprint('test', __name__, url_prefix='/test') @blp.route('/') @blp.response() @blp.paginate() def func(pagination_parameters): # Here, we purposely forget to set item_count # pagination_parameters.item_count = 2 return [1, 2] api.register_blueprint(blp) client = app.test_client() with mock.patch.object(app.logger, 'warning') as mock_warning: response = client.get('/test/') assert response.status_code == 200 assert 'X-Pagination' not in response.headers if header_name is None: assert mock_warning.call_count == 0 else: assert mock_warning.call_count == 1 assert mock_warning.call_args == ( ('item_count not set in endpoint test.func',), ) @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters(self, app_fixture): # page = 2, page_size = 5 response = app_fixture.client.get( '/test/', query_string={'page': 2, 'page_size': 5}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } # page = 334, page_size = 3 # last page is incomplete if total not multiple of page_size response = app_fixture.client.get( '/test/', query_string={'page': 334, 'page_size': 3}) assert response.status_code == 200 data = response.json headers = response.headers assert len(data) == 1 assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 334, 'page': 334, 'first_page': 1, 'last_page': 334, 'previous_page': 333, } @pytest.mark.parametrize('collection', [1000, ], indirect=True) def test_pagination_parameters_default_page_page_size(self, app_fixture): # Default: page = 1, page_size = 10 # Custom: page = 2, page_size = 5 response = app_fixture.client.get('/test/') assert response.status_code == 200 data = response.json headers = response.headers if app_fixture.custom_params is False: assert len(data) == 10 assert data[0] == {'field': 0, 'item_id': 1} assert data[9] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 100, 'page': 1, 'first_page': 1, 'last_page': 100, 'next_page': 2, } else: assert len(data) == 5 assert data[0] == {'field': 5, 'item_id': 6} assert data[4] == {'field': 9, 'item_id': 10} assert json.loads(headers['X-Pagination']) == { 'total': 1000, 'total_pages': 200, 'page': 2, 'first_page': 1, 'last_page': 200, 'previous_page': 1, 'next_page': 3, } def test_pagination_empty_collection(self, app_fixture): # empty collection -> 200 with empty list, partial pagination metadata response = app_fixture.client.get('/test/') assert response.status_code == 200 assert json.loads(response.headers['X-Pagination']) == { 'total': 0, 'total_pages': 0, } assert response.json == [] ", https://github.com/martyni/autoyaml,be3f450667bcca477683e63bcc49c67c5d170855,tests/autoyaml_test.py::TestAutoyamlMethods::test_b_decrypt_config,tests/autoyaml_test.py,OD-Brit,flaky,nan,nan,classify;root_cause,"import unittest import tempfile import string import os from random import choice, randint from autoyaml import load_config, write_config from autoyaml.encryptor import encrypt_file, decrypt_file def random_string(stringLength=10, selection=string.ascii_lowercase): """"""Generate a random string of fixed length """""" return ''.join(choice(selection) for i in range(stringLength)) class TestAutoyamlMethods(unittest.TestCase): password = random_string() app = random_string() config = {""appname"": app} enc_app = random_string() enc_config = {""appname"": enc_app} def writer(self, name, encrypted=False): suffix = '' if not encrypted else '.enc' file_path = os.path.expanduser('~/.{}'.format(name)) + suffix if encrypted: return file_path with open(file_path,'w') as tmp: header = '---\n' file_contents = header + ""{name}: {name}"".format(name=name) tmp.write(file_contents) return file_path def password_function(self): return self.password def test_load_config(self): fname = self.writer(self.app) self.assertEqual(load_config(self.app),{self.app:self.app}) os.remove(fname) def test_a_encrypt_config(self): write_config(self.enc_config, self.enc_app, encrypted=True, password_function=self.password_function) self.filename = self.writer(self.enc_app, encrypted=True) with open(self.filename) as filename: self.assertNotIn(self.enc_app, filename.read()) def test_b_decrypt_config(self): loaded_config = load_config(self.enc_app, password_function=self.password_function) self.assertEqual(loaded_config, self.enc_config) loaded_config[""extra_field""] = random_string() write_config(loaded_config, self.enc_app, encrypted=True, password_function=self.password_function) loaded_config2 = load_config(self.enc_app, password_function=self.password_function) self.assertEqual(loaded_config, loaded_config2) os.remove(self.writer(self.enc_app, encrypted=True)) def test_write_config(self): write_config({self.app:self.app}, self.app) write_config({self.app:'overwrite'}, self.app, overwrite=False) fname = os.path.expanduser('~/.{}'.format(self.app)) with open(fname) as tmp: test_str = '---\n{app}: {app}\n'.format(app=self.app) read_str = tmp.read() self.assertEqual(test_str, read_str) os.remove(fname) class TestEncryptorMethods(unittest.TestCase): file_name = '/tmp/test' contents = random_string() password = random_string() salt = random_string(selection='0123456789') def test_a_encrypt(self): with open(self.file_name,'w') as test_file: test_file.write(self.contents) encrypt_file(self.file_name, self.password, self.salt) with open(self.file_name) as test_file: self.assertNotEqual(self.contents, test_file.read()) def test_b_decrypt(self): decrypt_file(self.file_name, self.password, self.salt) with open(self.file_name) as test_file: self.assertEqual(self.contents, test_file.read()) if __name__ == '__main__': unittest.main() ", https://github.com/martyni/autoyaml,be3f450667bcca477683e63bcc49c67c5d170855,tests/autoyaml_test.py::TestEncryptorMethods::test_b_decrypt,tests/autoyaml_test.py,OD-Brit,flaky,nan,nan,classify;root_cause,"import unittest import tempfile import string import os from random import choice, randint from autoyaml import load_config, write_config from autoyaml.encryptor import encrypt_file, decrypt_file def random_string(stringLength=10, selection=string.ascii_lowercase): """"""Generate a random string of fixed length """""" return ''.join(choice(selection) for i in range(stringLength)) class TestAutoyamlMethods(unittest.TestCase): password = random_string() app = random_string() config = {""appname"": app} enc_app = random_string() enc_config = {""appname"": enc_app} def writer(self, name, encrypted=False): suffix = '' if not encrypted else '.enc' file_path = os.path.expanduser('~/.{}'.format(name)) + suffix if encrypted: return file_path with open(file_path,'w') as tmp: header = '---\n' file_contents = header + ""{name}: {name}"".format(name=name) tmp.write(file_contents) return file_path def password_function(self): return self.password def test_load_config(self): fname = self.writer(self.app) self.assertEqual(load_config(self.app),{self.app:self.app}) os.remove(fname) def test_a_encrypt_config(self): write_config(self.enc_config, self.enc_app, encrypted=True, password_function=self.password_function) self.filename = self.writer(self.enc_app, encrypted=True) with open(self.filename) as filename: self.assertNotIn(self.enc_app, filename.read()) def test_b_decrypt_config(self): loaded_config = load_config(self.enc_app, password_function=self.password_function) self.assertEqual(loaded_config, self.enc_config) loaded_config[""extra_field""] = random_string() write_config(loaded_config, self.enc_app, encrypted=True, password_function=self.password_function) loaded_config2 = load_config(self.enc_app, password_function=self.password_function) self.assertEqual(loaded_config, loaded_config2) os.remove(self.writer(self.enc_app, encrypted=True)) def test_write_config(self): write_config({self.app:self.app}, self.app) write_config({self.app:'overwrite'}, self.app, overwrite=False) fname = os.path.expanduser('~/.{}'.format(self.app)) with open(fname) as tmp: test_str = '---\n{app}: {app}\n'.format(app=self.app) read_str = tmp.read() self.assertEqual(test_str, read_str) os.remove(fname) class TestEncryptorMethods(unittest.TestCase): file_name = '/tmp/test' contents = random_string() password = random_string() salt = random_string(selection='0123456789') def test_a_encrypt(self): with open(self.file_name,'w') as test_file: test_file.write(self.contents) encrypt_file(self.file_name, self.password, self.salt) with open(self.file_name) as test_file: self.assertNotEqual(self.contents, test_file.read()) def test_b_decrypt(self): decrypt_file(self.file_name, self.password, self.salt) with open(self.file_name) as test_file: self.assertEqual(self.contents, test_file.read()) if __name__ == '__main__': unittest.main() ", https://github.com/matheusbsilva/plai,1d3bfb0438bb6bd2f8d8e42f17207f9449319763,tests/test_evaluation.py::TestPipeline::test_pipeline_raise_error_on_undeclared_dataframe,tests/test_evaluation.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pandas as pd import pytest from plai.interpreter import run from plai.modules import Col from plai.modules import drop from plai.modules import read_file from plai.environment import env from plai.symbol import Symbol class TestAssigment: def test_assign_variable_add_to_env(self): e = env() run('x = 1', e) assert e[Symbol('x')] == 1 def test_assing_variable_using_expr(self): e = env() run('x = 1 + 2 + 3', e) assert e[Symbol('x')] == 6 class TestExpressions: def test_number_sum_expressions(self): assert run('1 + 2') == 3 assert run('1 + 2 + 3') == 6 def test_number_sub_expression(self): assert run('1 - 2') == -1 assert run('10 - 2 - 1') == 7 def test_number_mult_expression(self): assert run('2 * 3') == 6 assert run('2 * 3 * 4') == 24 def test_number_div_expression(self): assert run('6 / 2') == 3 assert run('12 / 2 / 3') == 2 def test_precedence_number_expression(self): assert run('2 * 3 + 5') == 11 assert run('2 / 2 + 3') == 4 def test_precedence_with_parentheses_number_expression(self): assert run('(2 + 3) * 5') == 25 assert run('(6 / 2) * 6') == 18 class TestPipeline: def test_pipeline_raise_error_on_undeclared_dataframe(self): with pytest.raises(NameError): run('pipeline(df): {drop(.name)}') def test_pipeline_execute_stmts(self, dataframe): e = env() e[Symbol('df')] = dataframe assert run('pipeline(df): {drop(.name)}', env=e).equals( drop(dataframe, Col('name'))) def test_pipeline_execute_multiple_stmts(self, dataframe): e = env() e[Symbol('df')] = dataframe assert run('pipeline(df): {drop(.name) drop(.floats)}', env=e).equals( drop(dataframe, [Col('name'), Col('floats')])) class TestColEvaluation: def test_sugar_col_returns_Col_instance(self): assert isinstance(run('.col'), Col) def test_sugar_col_returns_Col_with_right_name(self): col = run('.col') assert col.name == 'col' class TestMultipleStmts: def test_multiple_stmts_with_pipeline(self, dataframe, csv_file_comma): path = str(csv_file_comma) src = """""" df = read_file(""%s"") pipeline(df): { drop(.name)} """""" % path df_res = read_file(path) result = drop(df_res, Col('name')) assert run(src).equals(result) class TestAttrCall: def test_attr_call_on_var(self, dataframe): e = env() e[Symbol('df')] = dataframe assert run('df.columns').equals(dataframe.columns) ", https://github.com/matheuswhite/bluebees,ca18ee6dae60393480bac3893038790957727748,tests/test_application_data.py::test_cleanup,tests/test_application_data.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from bluebees.common.file import file_helper from bluebees.client.application.application_data import ApplicationData from bluebees.client.data_paths import base_dir, app_dir from Crypto.Random import get_random_bytes import pathlib def test_application_data(): name = 'test_app' key = get_random_bytes(16) key_index = get_random_bytes(2) network = f'test_net' num_nodes = 10 nodes = [] for x in range(num_nodes): nodes.append(f'test_node{x}') data = ApplicationData(name=name, key=key, key_index=key_index, network=network, nodes=nodes) assert file_helper.file_exist(base_dir + app_dir + name + '.yml') is \ False data.save() assert file_helper.file_exist(base_dir + app_dir + name + '.yml') is \ True r_data = ApplicationData.load(base_dir + app_dir + name + '.yml') assert data == r_data def test_cleanup(): pathlib.Path(base_dir + app_dir + 'test_app.yml').unlink() ", https://github.com/matheuswhite/bluebees,ca18ee6dae60393480bac3893038790957727748,tests/test_file_helper.py::test_cleanup,tests/test_file_helper.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from bluebees.common.file import file_helper import pathlib def test_file_exist(): assert file_helper.file_exist('non_exist_file.yml') is False def test_load(): assert file_helper.file_exist('load_file.yml') is False content = file_helper.load('load_file.yml', default_content={ 'val_int': 95, 'val_str': 'october', 'val_bytes': b'\x24', 'val_bool': True }) assert file_helper.file_exist('load_file.yml') is True assert content['val_int'] == 95 assert content['val_str'] == 'october' assert content['val_bytes'] == b'\x24' assert content['val_bool'] is True content = file_helper.load('load_file.yml', default_content={}) assert content['val_int'] == 95 assert content['val_str'] == 'october' assert content['val_bytes'] == b'\x24' assert content['val_bool'] is True def test_write_read(): content = { 'val_int': 95, 'val_str': 'october', 'val_bytes': b'\x24', 'val_bool': True } assert file_helper.file_exist('write_read_file.yml') is False file_helper.write('write_read_file.yml', content) assert file_helper.file_exist('write_read_file.yml') is True r_content = file_helper.read('write_read_file.yml') assert r_content['val_int'] == 95 assert r_content['val_str'] == 'october' assert r_content['val_bytes'] == b'\x24' assert r_content['val_bool'] is True def test_dir_exist(): empty_content = {} dir_path = 'test_dir/' assert file_helper.dir_exist(dir_path) is False file_helper.write(dir_path + 'empty_file.yml', empty_content) assert file_helper.dir_exist(dir_path) is True def test_list_files(): base_content = { 'val_int': 1, 'val_str': 'str', 'val_bytes': b'\x00', 'val_bool': True } base_dir = 'list_dir/' num_files = 100 all_contents = {} for x in range(num_files): base_content['val_int'] += 1 base_content['val_str'] += str(x) base_content['val_bytes'] += x.to_bytes(1, 'big') base_content['val_bool'] = not base_content['val_bool'] all_contents[f'file_{x}.yml'] = dict(base_content) for x in range(num_files): file_name = f'file_{x}.yml' file_helper.write(base_dir + file_name, all_contents[file_name]) filenames = file_helper.list_files(base_dir) for x in range(num_files): assert file_helper.read(base_dir + filenames[x]) == \ all_contents[filenames[x]] def test_cleanup(): def delete_folder(pth): for sub in pth.iterdir(): if sub.is_dir(): delete_folder(sub) else: sub.unlink() pth.rmdir() delete_folder(pathlib.Path('list_dir/')) delete_folder(pathlib.Path('test_dir/')) pathlib.Path('load_file.yml').unlink() pathlib.Path('write_read_file.yml').unlink() ", https://github.com/matheuswhite/bluebees,ca18ee6dae60393480bac3893038790957727748,tests/test_network_data.py::test_cleanup,tests/test_network_data.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from bluebees.common.file import file_helper from bluebees.client.network.network_data import NetworkData from bluebees.client.data_paths import base_dir, net_dir from Crypto.Random import get_random_bytes import pathlib def test_network_data(): name = 'test_net' key = get_random_bytes(16) key_index = get_random_bytes(2) iv_index = get_random_bytes(4) num_apps = 10 num_nodes = 15 apps = [] for x in range(num_apps): apps.append(f'test_app{x}') nodes = [] for x in range(num_nodes): nodes.append(f'test_node{x}') data = NetworkData(name=name, key=key, key_index=key_index, iv_index=iv_index, apps=apps, nodes=nodes) assert file_helper.file_exist(base_dir + net_dir + name + '.yml') is \ False data.save() assert file_helper.file_exist(base_dir + net_dir + name + '.yml') is \ True r_data = NetworkData.load(base_dir + net_dir + name + '.yml') assert data == r_data def test_cleanup(): pathlib.Path(base_dir + net_dir + 'test_net.yml').unlink() ", https://github.com/matheuswhite/bluebees,ca18ee6dae60393480bac3893038790957727748,tests/test_node_data.py::test_cleanup,tests/test_node_data.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from bluebees.common.file import file_helper from bluebees.client.node.node_data import NodeData from bluebees.client.data_paths import base_dir, node_dir from Crypto.Random import get_random_bytes import pathlib def test_application_data(): name = 'test_node' addr = get_random_bytes(2) devkey = get_random_bytes(16) device_uuid = get_random_bytes(16) network = f'test_net' num_apps = 10 apps = [] for x in range(num_apps): apps.append(f'test_app{x}') data = NodeData(name=name, addr=addr, network=network, device_uuid=device_uuid, devkey=devkey, apps=apps) assert file_helper.file_exist(base_dir + node_dir + name + '.yml') is \ False data.save() assert file_helper.file_exist(base_dir + node_dir + name + '.yml') is \ True r_data = NodeData.load(base_dir + node_dir + name + '.yml') assert data == r_data def test_cleanup(): pathlib.Path(base_dir + node_dir + 'test_node.yml').unlink() ", https://github.com/matlink/gplaycli,b161dbffa64ddc7081ca7c880ce27f84c2f0a0d8,tests/test_init.py::test_default_settings,tests/test_init.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import sys import os sys.path.insert(0, os.path.abspath('.')) from gplaycli import gplaycli gpc = gplaycli.GPlaycli() token_url = ""https://matlink.fr/token"" def test_default_settings(): assert gpc.yes == False assert gpc.verbose == False assert gpc.progress_bar == False assert gpc.device_codename == 'bacon' def test_connection_credentials(): try: # You are travis if os.environ['TRAVIS_PULL_REQUEST'] != ""false"": # If current job is a Pull Request print(""Job is pull request. Won't check credentials"") return except KeyError: # You are not travis pass gpc.token_enable = False gpc.gmail_address = os.environ['GMAIL_ADDR'] gpc.gmail_password = os.environ['GMAIL_PWD'] success, error = gpc.connect() assert error is None assert success == True def test_connection_token(): gpc.token_enable = True gpc.token_url = token_url gpc.retrieve_token(force_new=True) success, error = gpc.connect() assert error is None assert success == True def test_download_focus(): gpc.progress_bar = True gpc.download_folder = os.path.abspath('.') gpc.download(['org.mozilla.focus']) ", https://github.com/mattdavis90/base10,93577e85215e4cd7f02a504ff6a23501d56a1748,base10/test/test_helpers.py::TestMetricHelper::test_metric_helper_fields_exception,base10/test/test_helpers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest from time import time from base10 import MetricHelper, MetricHandler from base10.base import Metric from base10.exceptions import Base10Error class TestMetricHelper: def setup_method(self): self.metric_name = 'metric' self.metric_fields = ['value'] self.metric_metadata = ['hostname'] self.metric_values = {'value': 0, 'hostname': 'test', 'time': time()} def test_metric_helper(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields _metadata = self.metric_metadata metric = MyMetric(**self.metric_values) assert isinstance(metric, Metric) assert metric.name == self.metric_name assert metric.fields == self.metric_fields assert metric.metadata == self.metric_metadata assert metric.values == self.metric_values def test_metric_helper_name_exception(self): class MyMetric(MetricHelper): _fields = self.metric_fields _metadata = self.metric_metadata with pytest.raises(Base10Error) as exc: metric = MyMetric(**self.metric_values) assert '_name is required' == str(exc.value) def test_metric_helper_fields_exception(self): class MyMetric(MetricHelper): _name = self.metric_name _metadata = self.metric_metadata with pytest.raises(Base10Error) as exc: metric = MyMetric(**self.metric_values) assert '_fields is required' == str(exc.value) def test_metric_helper_metadata_exception(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields with pytest.raises(Base10Error) as exc: metric = MyMetric(**self.metric_values) assert '_metadata is required' == str(exc.value) def test_metric_helper_handles_time_field(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields + ['time'] _metadata = self.metric_metadata metric = MyMetric(**self.metric_values) assert metric.fields == self.metric_fields def test_metric_helper_kwargs(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields _metadata = self.metric_metadata alternative_name = 'test1' alternative_fields = ['value1'] alternative_metadata = ['hostname1'] alternative_values = {'value1': 0, 'hostname1': 'test', 'time': time()} metric = MetricHelper( name=alternative_name, fields=alternative_fields, metadata=alternative_metadata, **alternative_values ) assert isinstance(metric, Metric) assert metric.name == alternative_name assert metric.fields == alternative_fields assert metric.metadata == alternative_metadata assert metric.values == alternative_values class TestMetricHandler: def test_metric_handler_no_reader_writer(self): class Handler(MetricHandler): pass with pytest.raises(Base10Error) as exc: handler = Handler() assert 'Either _reader or _writer is required' == str(exc.value) def test_metric_handler_no_dialect_with_reader(self): class Handler(MetricHandler): _reader = None with pytest.raises(Base10Error) as exc: handler = Handler() assert '_dialect is required' == str(exc.value) def test_metric_handler_no_dialect_with_writer(self): class Handler(MetricHandler): _writer = None with pytest.raises(Base10Error) as exc: handler = Handler() assert '_dialect is required' == str(exc.value) def test_metric_handler_write_to_read_only(self): class Handler(MetricHandler): _reader = None _dialect = None with pytest.raises(Base10Error) as exc: handler = Handler() handler.write(None) assert 'Attempt to write to a read-only MetricHandler' in str(exc.value) def test_metric_handler_read_from_write_only(self): class Handler(MetricHandler): _writer = None _dialect = None with pytest.raises(Base10Error) as exc: handler = Handler() metric = next(handler.read()) assert 'Attempt to read from a write-only MetricHandler' in str( exc.value ) ", https://github.com/mattdavis90/base10,93577e85215e4cd7f02a504ff6a23501d56a1748,base10/test/test_helpers.py::TestMetricHelper::test_metric_helper_handles_time_field,base10/test/test_helpers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest from time import time from base10 import MetricHelper, MetricHandler from base10.base import Metric from base10.exceptions import Base10Error class TestMetricHelper: def setup_method(self): self.metric_name = 'metric' self.metric_fields = ['value'] self.metric_metadata = ['hostname'] self.metric_values = {'value': 0, 'hostname': 'test', 'time': time()} def test_metric_helper(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields _metadata = self.metric_metadata metric = MyMetric(**self.metric_values) assert isinstance(metric, Metric) assert metric.name == self.metric_name assert metric.fields == self.metric_fields assert metric.metadata == self.metric_metadata assert metric.values == self.metric_values def test_metric_helper_name_exception(self): class MyMetric(MetricHelper): _fields = self.metric_fields _metadata = self.metric_metadata with pytest.raises(Base10Error) as exc: metric = MyMetric(**self.metric_values) assert '_name is required' == str(exc.value) def test_metric_helper_fields_exception(self): class MyMetric(MetricHelper): _name = self.metric_name _metadata = self.metric_metadata with pytest.raises(Base10Error) as exc: metric = MyMetric(**self.metric_values) assert '_fields is required' == str(exc.value) def test_metric_helper_metadata_exception(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields with pytest.raises(Base10Error) as exc: metric = MyMetric(**self.metric_values) assert '_metadata is required' == str(exc.value) def test_metric_helper_handles_time_field(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields + ['time'] _metadata = self.metric_metadata metric = MyMetric(**self.metric_values) assert metric.fields == self.metric_fields def test_metric_helper_kwargs(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields _metadata = self.metric_metadata alternative_name = 'test1' alternative_fields = ['value1'] alternative_metadata = ['hostname1'] alternative_values = {'value1': 0, 'hostname1': 'test', 'time': time()} metric = MetricHelper( name=alternative_name, fields=alternative_fields, metadata=alternative_metadata, **alternative_values ) assert isinstance(metric, Metric) assert metric.name == alternative_name assert metric.fields == alternative_fields assert metric.metadata == alternative_metadata assert metric.values == alternative_values class TestMetricHandler: def test_metric_handler_no_reader_writer(self): class Handler(MetricHandler): pass with pytest.raises(Base10Error) as exc: handler = Handler() assert 'Either _reader or _writer is required' == str(exc.value) def test_metric_handler_no_dialect_with_reader(self): class Handler(MetricHandler): _reader = None with pytest.raises(Base10Error) as exc: handler = Handler() assert '_dialect is required' == str(exc.value) def test_metric_handler_no_dialect_with_writer(self): class Handler(MetricHandler): _writer = None with pytest.raises(Base10Error) as exc: handler = Handler() assert '_dialect is required' == str(exc.value) def test_metric_handler_write_to_read_only(self): class Handler(MetricHandler): _reader = None _dialect = None with pytest.raises(Base10Error) as exc: handler = Handler() handler.write(None) assert 'Attempt to write to a read-only MetricHandler' in str(exc.value) def test_metric_handler_read_from_write_only(self): class Handler(MetricHandler): _writer = None _dialect = None with pytest.raises(Base10Error) as exc: handler = Handler() metric = next(handler.read()) assert 'Attempt to read from a write-only MetricHandler' in str( exc.value ) ", https://github.com/mattdavis90/base10,93577e85215e4cd7f02a504ff6a23501d56a1748,base10/test/test_helpers.py::TestMetricHelper::test_metric_helper_metadata_exception,base10/test/test_helpers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest from time import time from base10 import MetricHelper, MetricHandler from base10.base import Metric from base10.exceptions import Base10Error class TestMetricHelper: def setup_method(self): self.metric_name = 'metric' self.metric_fields = ['value'] self.metric_metadata = ['hostname'] self.metric_values = {'value': 0, 'hostname': 'test', 'time': time()} def test_metric_helper(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields _metadata = self.metric_metadata metric = MyMetric(**self.metric_values) assert isinstance(metric, Metric) assert metric.name == self.metric_name assert metric.fields == self.metric_fields assert metric.metadata == self.metric_metadata assert metric.values == self.metric_values def test_metric_helper_name_exception(self): class MyMetric(MetricHelper): _fields = self.metric_fields _metadata = self.metric_metadata with pytest.raises(Base10Error) as exc: metric = MyMetric(**self.metric_values) assert '_name is required' == str(exc.value) def test_metric_helper_fields_exception(self): class MyMetric(MetricHelper): _name = self.metric_name _metadata = self.metric_metadata with pytest.raises(Base10Error) as exc: metric = MyMetric(**self.metric_values) assert '_fields is required' == str(exc.value) def test_metric_helper_metadata_exception(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields with pytest.raises(Base10Error) as exc: metric = MyMetric(**self.metric_values) assert '_metadata is required' == str(exc.value) def test_metric_helper_handles_time_field(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields + ['time'] _metadata = self.metric_metadata metric = MyMetric(**self.metric_values) assert metric.fields == self.metric_fields def test_metric_helper_kwargs(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields _metadata = self.metric_metadata alternative_name = 'test1' alternative_fields = ['value1'] alternative_metadata = ['hostname1'] alternative_values = {'value1': 0, 'hostname1': 'test', 'time': time()} metric = MetricHelper( name=alternative_name, fields=alternative_fields, metadata=alternative_metadata, **alternative_values ) assert isinstance(metric, Metric) assert metric.name == alternative_name assert metric.fields == alternative_fields assert metric.metadata == alternative_metadata assert metric.values == alternative_values class TestMetricHandler: def test_metric_handler_no_reader_writer(self): class Handler(MetricHandler): pass with pytest.raises(Base10Error) as exc: handler = Handler() assert 'Either _reader or _writer is required' == str(exc.value) def test_metric_handler_no_dialect_with_reader(self): class Handler(MetricHandler): _reader = None with pytest.raises(Base10Error) as exc: handler = Handler() assert '_dialect is required' == str(exc.value) def test_metric_handler_no_dialect_with_writer(self): class Handler(MetricHandler): _writer = None with pytest.raises(Base10Error) as exc: handler = Handler() assert '_dialect is required' == str(exc.value) def test_metric_handler_write_to_read_only(self): class Handler(MetricHandler): _reader = None _dialect = None with pytest.raises(Base10Error) as exc: handler = Handler() handler.write(None) assert 'Attempt to write to a read-only MetricHandler' in str(exc.value) def test_metric_handler_read_from_write_only(self): class Handler(MetricHandler): _writer = None _dialect = None with pytest.raises(Base10Error) as exc: handler = Handler() metric = next(handler.read()) assert 'Attempt to read from a write-only MetricHandler' in str( exc.value ) ", https://github.com/mattdavis90/base10,93577e85215e4cd7f02a504ff6a23501d56a1748,base10/test/test_helpers.py::TestMetricHelper::test_metric_helper_name_exception,base10/test/test_helpers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest from time import time from base10 import MetricHelper, MetricHandler from base10.base import Metric from base10.exceptions import Base10Error class TestMetricHelper: def setup_method(self): self.metric_name = 'metric' self.metric_fields = ['value'] self.metric_metadata = ['hostname'] self.metric_values = {'value': 0, 'hostname': 'test', 'time': time()} def test_metric_helper(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields _metadata = self.metric_metadata metric = MyMetric(**self.metric_values) assert isinstance(metric, Metric) assert metric.name == self.metric_name assert metric.fields == self.metric_fields assert metric.metadata == self.metric_metadata assert metric.values == self.metric_values def test_metric_helper_name_exception(self): class MyMetric(MetricHelper): _fields = self.metric_fields _metadata = self.metric_metadata with pytest.raises(Base10Error) as exc: metric = MyMetric(**self.metric_values) assert '_name is required' == str(exc.value) def test_metric_helper_fields_exception(self): class MyMetric(MetricHelper): _name = self.metric_name _metadata = self.metric_metadata with pytest.raises(Base10Error) as exc: metric = MyMetric(**self.metric_values) assert '_fields is required' == str(exc.value) def test_metric_helper_metadata_exception(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields with pytest.raises(Base10Error) as exc: metric = MyMetric(**self.metric_values) assert '_metadata is required' == str(exc.value) def test_metric_helper_handles_time_field(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields + ['time'] _metadata = self.metric_metadata metric = MyMetric(**self.metric_values) assert metric.fields == self.metric_fields def test_metric_helper_kwargs(self): class MyMetric(MetricHelper): _name = self.metric_name _fields = self.metric_fields _metadata = self.metric_metadata alternative_name = 'test1' alternative_fields = ['value1'] alternative_metadata = ['hostname1'] alternative_values = {'value1': 0, 'hostname1': 'test', 'time': time()} metric = MetricHelper( name=alternative_name, fields=alternative_fields, metadata=alternative_metadata, **alternative_values ) assert isinstance(metric, Metric) assert metric.name == alternative_name assert metric.fields == alternative_fields assert metric.metadata == alternative_metadata assert metric.values == alternative_values class TestMetricHandler: def test_metric_handler_no_reader_writer(self): class Handler(MetricHandler): pass with pytest.raises(Base10Error) as exc: handler = Handler() assert 'Either _reader or _writer is required' == str(exc.value) def test_metric_handler_no_dialect_with_reader(self): class Handler(MetricHandler): _reader = None with pytest.raises(Base10Error) as exc: handler = Handler() assert '_dialect is required' == str(exc.value) def test_metric_handler_no_dialect_with_writer(self): class Handler(MetricHandler): _writer = None with pytest.raises(Base10Error) as exc: handler = Handler() assert '_dialect is required' == str(exc.value) def test_metric_handler_write_to_read_only(self): class Handler(MetricHandler): _reader = None _dialect = None with pytest.raises(Base10Error) as exc: handler = Handler() handler.write(None) assert 'Attempt to write to a read-only MetricHandler' in str(exc.value) def test_metric_handler_read_from_write_only(self): class Handler(MetricHandler): _writer = None _dialect = None with pytest.raises(Base10Error) as exc: handler = Handler() metric = next(handler.read()) assert 'Attempt to read from a write-only MetricHandler' in str( exc.value ) ", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_aggregated_delitem_method,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_aggregated_getitem_method,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_aggregated_setitem_method,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_aggregator_delete_method,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_aggregator_get_method,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_aggregator_keys_method,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_aggregator_keys_method_with_empty_nodes,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_aggregator_scan_iter_method,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_aggregator_set_method,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_aggregator_strategy,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_attributes_transmission_from_aggregated_node,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_attributes_transmission_from_loadbalanced_node,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_attributes_transmission_if_aggregated_has_no_host,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_constructor,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_custom_default_config,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_custom_default_ssl,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_custom_node,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_custom_prefix,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_extension_registration_if_app_has_no_extensions,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_init_app,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_loadbalanced_delitem_method,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_loadbalanced_getitem_method,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_loadbalanced_setitem_method,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_methods_transmission_from_aggregator,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_methods_transmission_from_redis,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/max-k/flask-multi-redis,fa781d3598448a6429309a686de9a8adb53f9f34,test/integration/test_flask_multi_redis.py::test_task_runner,test/integration/test_flask_multi_redis.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Integration tests for Flask-Multi-Redis."""""" import flask from flask_multi_redis.aggregator import Aggregator from flask_multi_redis.main import FlaskMultiRedis import pytest from redis import StrictRedis @pytest.fixture def app(): return flask.Flask(__name__) @pytest.fixture def loadbalanced(app): return FlaskMultiRedis(app) @pytest.fixture def aggregated(app): return FlaskMultiRedis(app, strategy='aggregate') @pytest.fixture def app_custom_default(app): app.config['REDIS_DEFAULT_PORT'] = 16379 app.config['REDIS_DEFAULT_DB'] = 9 app.config['REDIS_DEFAULT_PASSWORD'] = 'password' app.config['REDIS_DEFAULT_SOCKET_TIMEOUT'] = 2 return app @pytest.fixture def app_custom_default_ssl(app): app.config['REDIS_DEFAULT_SSL'] = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } return app @pytest.fixture def app_custom_node(app): ssl = { 'ssl_keyfile': 'ssl/rediskey.pem', 'ssl_certfile': 'ssl/rediscert.pem', 'ssl_ca_certs': 'ssl/rediscert.pem', 'ssl_cert_reqs': 'required' } app.config['REDIS_NODES'] = [{ 'host': 'localhost', 'port': 16379, 'db': 9, 'password': 'password', 'socket_timeout': 2, 'ssl': ssl }] return app @pytest.fixture def fake_node(): class Node(object): def __init__(self, name): self.config = {'socket_timeout': 2} self.name = name def get(self, pattern): return self.name def ttl(self, pattern): if int(self.name[-1]) == 3: return None return int(self.name[-1]) def keys(self, pattern): if pattern == 'empty': return [] if int(self.name[-1]) == 3: return [self.name, pattern] return [pattern] def set(self, key, pattern): setattr(self, key, pattern) return True def scan_iter(self, pattern): return iter([getattr(self, pattern)]) def delete(self, pattern): delattr(self, 'name') return Node @pytest.fixture def mocked_loadbalanced(loadbalanced, fake_node): loadbalanced._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return loadbalanced @pytest.fixture def mocked_aggregated(aggregated, fake_node): aggregated._aggregator._redis_nodes = [ fake_node('node1'), fake_node('node2'), fake_node('node3') ] return aggregated def test_constructor(loadbalanced): """"""Test that a constructor with app instance will initialize the connection."""""" assert loadbalanced._redis_client is not None assert hasattr(loadbalanced._redis_client, 'connection_pool') assert hasattr(loadbalanced._app, 'extensions') assert 'redis' in loadbalanced._app.extensions assert loadbalanced._app.extensions['redis'] is loadbalanced def test_aggregator_strategy(aggregated): """"""Test that a constructor with aggregate strategy will initialize the connection."""""" assert aggregated._redis_client is not None assert hasattr(aggregated._redis_client, 'connection_pool') assert hasattr(aggregated._app, 'extensions') assert 'redis' in aggregated._app.extensions assert aggregated._app.extensions['redis'] == aggregated def test_extension_registration_if_app_has_no_extensions(app): """"""Test that the constructor is able to register FlaskMultiRedis as an extension even if app has no extensions attribute."""""" delattr(app, 'extensions') redis = FlaskMultiRedis(app) assert hasattr(app, 'extensions') assert 'redis' in app.extensions assert app.extensions['redis'] == redis def test_init_app(app): """"""Test that a constructor without app instance will not initialize the connection. After FlaskMultiRedis.init_app(app) is called, the connection will be initialized."""""" redis = FlaskMultiRedis() assert redis._app is None assert len(redis._redis_nodes) == 0 redis.init_app(app) assert redis._app is app assert len(redis._redis_nodes) == 1 assert hasattr(redis._redis_client, 'connection_pool') def test_custom_prefix(app): """"""Test that config prefixes enable distinct connections."""""" app.config['DBA_NODES'] = [{'host': 'localhost', 'db': 1}] app.config['DBB_NODES'] = [{'host': 'localhost', 'db': 2}] redis_a = FlaskMultiRedis(app, config_prefix='DBA') redis_b = FlaskMultiRedis(app, config_prefix='DBB') assert redis_a.connection_pool.connection_kwargs['db'] == 1 assert redis_b.connection_pool.connection_kwargs['db'] == 2 def test_strict_parameter(app): """"""Test that initializing with the strict parameter set to True will use StrictRedis, and that False will keep using the old Redis class."""""" redis = FlaskMultiRedis(app, strict=True) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'StrictRedis' redis = FlaskMultiRedis(app, strict=False) assert redis._redis_client is not None assert type(redis._redis_client).__name__ == 'Redis' def test_custom_provider(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, like StrictRedis."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is None redis.init_app(app) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_with_app(app): """"""Test that FlaskMultiRedis can be instructed to use a different Redis client, using an already existing Flask app."""""" class FakeProvider(object): def __init__(self, **kwargs): pass redis = FlaskMultiRedis.from_custom_provider(FakeProvider, app=app) assert isinstance(redis, FlaskMultiRedis) assert redis._redis_client is not None assert isinstance(redis._redis_client, FakeProvider) def test_custom_provider_is_none(app): """"""Test that FlaskMultiRedis cannot be instructed to use a Redis Client wich is None."""""" with pytest.raises(AssertionError) as excinfo: FlaskMultiRedis.from_custom_provider(None) assert excinfo.value == 'your custom provider is None, come on' def test_custom_default_config(app_custom_default): """"""Test that we can pass a custom default configuration."""""" redis = FlaskMultiRedis(app_custom_default) assert redis.connection_pool.connection_kwargs['port'] == 16379 assert redis.connection_pool.connection_kwargs['db'] == 9 assert redis.connection_pool.connection_kwargs['password'] == 'password' assert redis.connection_pool.connection_kwargs['socket_timeout'] == 2 def test_custom_default_ssl(app_custom_default_ssl): """"""Test that we can pass a custom default ssl configuration."""""" redis = FlaskMultiRedis(app_custom_default_ssl) kwargs = redis.connection_pool.connection_kwargs assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_custom_node(app_custom_node): """"""Test that we can pass a custom node configuration."""""" redis = FlaskMultiRedis(app_custom_node) kwargs = redis.connection_pool.connection_kwargs assert kwargs['port'] == 16379 assert kwargs['db'] == 9 assert kwargs['password'] == 'password' assert kwargs['socket_timeout'] == 2 assert kwargs['ssl_keyfile'] == 'ssl/rediskey.pem' assert kwargs['ssl_certfile'] == 'ssl/rediscert.pem' assert kwargs['ssl_ca_certs'] == 'ssl/rediscert.pem' assert kwargs['ssl_cert_reqs'] == 'required' def test_attributes_transmission_from_loadbalanced_node(loadbalanced): """"""Test that attributes from loadbalanced nodes are available directly from FlaskMultiRedis object."""""" node = loadbalanced._redis_nodes[0] assert loadbalanced.connection_pool is node.connection_pool def test_attributes_transmission_from_aggregated_node(aggregated): """"""Test that attributes from aggregated nodes are available directly from FlaskMultiRedis object."""""" node = aggregated._aggregator._redis_nodes[0] assert aggregated.connection_pool is node.connection_pool def test_attributes_transmission_if_aggregated_has_no_host(aggregated): """"""Test that attributes transmission return None if Aggregator has an empty node list."""""" aggregated._aggregator._redis_nodes = [] assert aggregated._redis_client is None def test_methods_transmission_from_redis(loadbalanced): """"""Test that methods from redis are available direcly from FlaskMultiRedis object (loadbalancing)."""""" assert isinstance(loadbalanced.keys.__self__, StrictRedis) def test_methods_transmission_from_aggregator(aggregated): """"""Test that methods from aggregator are available directly from FlaskMultiRedis ob", https://github.com/mehdisadeghi/clashogram,5b8cc18adfc92ad381040ee25b750df462122939,test_everything.py::MessageFactoryTestCase::test_format_time_default,test_everything.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"'''Clashogram tests.''' import os import json import gettext import unittest from unittest.mock import MagicMock from clashogram._clashogram import CoCAPI, ClanInfo, WarInfo, WarStats, \ MessageFactory, WarMonitor, TelegramNotifier class ClanInfoTestCase(unittest.TestCase): def setUp(self): self.claninfo = ClanInfo({'location': {'name': 'Iran', 'isCountry': 'true', 'countryCode': 'IR'}, 'warWinStreak': 0, 'isWarLogPublic': True}) def test_location(self): assert self.claninfo.location == 'Iran' def test_country_imoji(self): assert self.claninfo.country_flag_imoji == '🇮🇷' def test_winstreak(self): assert self.claninfo.winstreak == 0 def test_is_warlog_public(self): assert self.claninfo.is_warlog_public == True class WarInfoTestCase(unittest.TestCase): def setUp(self): self.warinfo = WarInfo( json.loads(open(os.path.join('data', 'inWar_40.json'), 'r', encoding='utf8').read())) self.op_member = { ""tag"": ""#2GCR2YLP8"", ""name"": ""captain spock"", ""townhallLevel"": 9, ""mapPosition"": 18, ""opponentAttacks"": 2, ""bestOpponentAttack"": { ""attackerTag"": ""#G0QPL0LQ"", ""defenderTag"": ""#2GCR2YLP8"", ""stars"": 3, ""destructionPercentage"": 100, ""order"": 78 } } self.clan_member = { ""tag"": ""#9QVR8R29C"", ""name"": ""VAHID"", ""townhallLevel"": 7, ""mapPosition"": 35, ""opponentAttacks"": 2, ""bestOpponentAttack"": { ""attackerTag"": ""#P0C92YP99"", ""defenderTag"": ""#9QVR8R29C"", ""stars"": 3, ""destructionPercentage"": 100, ""order"": 3 } } def test_start_time(self): assert self.warinfo.start_time == '20170603T191148.000Z' def test_team_size(self): assert self.warinfo.team_size == 40 def test_get_ordered_attacks(self): ordered_attacks = self.warinfo.get_ordered_attacks() assert len(ordered_attacks) == 126 def test_player_count(self): assert len(self.warinfo.players) == 80 def test_get_player_attacks(self): player = self.warinfo.players['#2GCR2YLP8'] assert self.warinfo.get_player_attacks(player) == [] def test_get_player_info(self): with self.assertRaises(Exception): self.warinfo.players['#2GCZZZZP8'] def test_is_not_in_war(self): assert not self.warinfo.is_not_in_war() def test_is_in_preparation(self): assert not self.warinfo.is_in_preparation() def test_is_in_war(self): assert self.warinfo.is_in_war() def test_is_war_over(self): assert not self.warinfo.is_war_over() def test_is_clan_member(self): self.assertFalse(self.warinfo.is_clan_member(self.op_member)) self.assertTrue(self.warinfo.is_clan_member(self.clan_member)) def test_is_win(self): self.assertTrue(self.warinfo.is_win()) def test_is_draw(self): self.assertFalse(self.warinfo.is_draw()) def test_create_war_id(self): self.assertEqual(self.warinfo.create_war_id(), ""#YVL0C8UY#JC0L922Y20170602T201148.000Z"") class WarInfoNotInWarTestCase(unittest.TestCase): def setUp(self): self.warinfo = WarInfo(json.loads( open(os.path.join('data', 'notInWar.json'), 'r', encoding='utf8').read())) def test_clan_stats(self): self.assertEqual(self.warinfo.clan_level, 0) self.assertEqual(self.warinfo.clan_destruction, 0) self.assertEqual(self.warinfo.clan_stars, 0) self.assertEqual(self.warinfo.clan_attacks, 0) def test_op_stats(self): self.assertEqual(self.warinfo.op_level, 0) self.assertEqual(self.warinfo.op_destruction, 0) self.assertEqual(self.warinfo.op_stars, 0) self.assertEqual(self.warinfo.op_attacks, 0) def test_players(self): self.assertEqual(self.warinfo.players, {}) class WarStatsTestCase(unittest.TestCase): def setUp(self): warinfo = WarInfo(json.loads( open(os.path.join('data', 'warEnded_50.json'), 'r', encoding='utf8').read())) self.stats = WarStats(warinfo) self.attack161 = { ""destructionPercentage"": 53, ""attackerTag"": ""#9YUVL0CU"", ""order"": 161, ""stars"": 2, ""defenderTag"": ""#228U8G88L"" } self.attack150 = { ""destructionPercentage"": 100, ""attackerTag"": ""#2Q02GYCYV"", ""order"": 150, ""stars"": 3, ""defenderTag"": ""#2Y0C8YPYU"" } def test_first_attack_stats(self): stats = self.stats.calculate_war_stats_sofar(1) self.assertEqual(stats['clan_destruction'], 0) self.assertEqual(stats['op_destruction'], 1.76) self.assertEqual(stats['clan_stars'], 0) self.assertEqual(stats['op_stars'], 2) self.assertEqual(stats['clan_used_attacks'], 0) self.assertEqual(stats['op_used_attacks'], 1) def test_42th_attack_stats(self): stats = self.stats.calculate_war_stats_sofar(42) self.assertEqual(stats['clan_destruction'], 44.16) self.assertEqual(stats['op_destruction'], 26.56) self.assertEqual(stats['clan_stars'], 61) self.assertEqual(stats['op_stars'], 37) self.assertEqual(stats['clan_used_attacks'], 27) self.assertEqual(stats['op_used_attacks'], 15) def test_last_attack_stats(self): stats = self.stats.calculate_war_stats_sofar(162) self.assertEqual(stats['clan_destruction'], 96.72) self.assertEqual(stats['op_destruction'], 98.90) self.assertEqual(stats['clan_stars'], 142) self.assertEqual(stats['op_stars'], 147) self.assertEqual(stats['clan_used_attacks'], 87) self.assertEqual(stats['op_used_attacks'], 75) def test_attack_destruction(self): self.assertEqual( self.stats.get_attack_new_destruction(self.attack161), 0) self.assertEqual( self.stats.get_attack_new_destruction(self.attack150), 3) def test_attack_new_stars(self): self.assertEqual(self.stats.get_attack_new_stars(self.attack161), 0) self.assertEqual(self.stats.get_attack_new_stars(self.attack150), 1) class MessageFactoryTestCase(unittest.TestCase): def setUp(self): self.msg_factory = MessageFactory(None, None) self.setlocale_en() def _setlocale(self, language): os.environ['LANGUAGE'] = language gettext.bindtextdomain('messages', localedir=os.path.join(os.curdir, 'locales')) gettext.textdomain('messages') def setlocale_en(self): self._setlocale('en_US.UTF-8') def setlocale_fa(self): self._setlocale('fa_IR.UTF-8') def test_format_time_default(self): os.environ['LANGUAGE'] = 'en' timestr = self.msg_factory.format_time('20170603T191148.000Z') self.assertEqual(timestr, 'Sat, 03 Jun 2017 19:11:48') def test_format_time_fa(self): os.environ['LANG'] = 'fa' timestr = self.msg_factory.format_time('20170603T191148.000Z') self.assertEqual(timestr, 'شنبه، ۱۳ خرداد ۱۳۹۶ ۲۳:۴۱:۴۸') def test_format_time_fa_IR(self): os.environ['LANGUAGE'] = 'fa_IR' timestr = self.msg_factory.format_time('20170603T191148.000Z') self.assertEqual(timestr, 'شنبه، ۱۳ خرداد ۱۳۹۶ ۲۳:۴۱:۴۸') def test_format_time_fa_IR_locale(self): self.setlocale_fa() timestr = self.msg_factory.format_time('20170603T191148.000Z') self.assertEqual(timestr, 'شنبه، ۱۳ خرداد ۱۳۹۶ ۲۳:۴۱:۴۸') class WarMonitorTestCase(unittest.TestCase): def setUp(self): coc_api = CoCAPI(None) self.warinfo = self.get_warinfo() our_claninfo = ClanInfo({'location': {'name': 'Iran', 'isCountry': 'true', 'countryCode': 'IR'}, 'warWinStreak': 0}) coc_api.get_currentwar = MagicMock(return_value=self.warinfo) coc_api.get_claninfo = MagicMock(return_value=our_claninfo) notifier = TelegramNotifier(None, None) notifier.send = MagicMock() self.monitor = WarMonitor({}, coc_api, '', notifier) self.monitor.update() self.clan_attack = { ""attackerTag"": ""#98VVJ8LV8"", ""defenderTag"": ""#8CCLRP2JC"", ""stars"": 3, ""destructionPercentage"": 100, ""order"": 10 } def get_warinfo(self): raise NotImplementedError() class WarMonitorInWarTestCase(WarMonitorTestCase): def get_warinfo(self): return WarInfo(json.loads( open(os.path.join('data', 'inWar_40.json'), 'r', encoding='utf8').read())) def test_send_preparation_msg(self): self.monitor.send_preparation_msg() self.assertTrue(self.monitor.is_msg_sent('preparation_msg')) self.assertTrue(self.monitor.is_msg_sent('players_msg')) def test_send_war_msg(self): self.monitor.send_war_msg() self.assertTrue(self.monitor.is_msg_sent('war_msg')) def test_is_attack_msg_sent(self): self.assertTrue(self.monitor.is_msg_sent( self.monitor.get_attack_id(self.clan_attack))) def test_get_attack_id(self): self.assertEqual(self.monitor.get_attack_id(self.clan_attack), 'attack98VVJ8LV88CCLRP2JC') def test_is_war_over_msg_sent(self): self.assertFalse", https://github.com/mehdisadeghi/clashogram,6ea9eef543201f117749fa290df800c32eb18f54,test_everything.py::MessageFactoryTestCase::test_format_time_default,test_everything.py,OD-Vic,flaky,Accepted,https://github.com/mehdisadeghi/clashogram/pull/32,classify;root_cause,"'''Clashogram tests.''' import os import json import gettext import unittest from unittest.mock import MagicMock from clashogram.__main__ import WarMonitor from clashogram.models import WarStats, ClanInfo, WarInfo, WarStats from clashogram.formatters import MessageFactory from clashogram.api import CoCAPI from clashogram.notifiers import TelegramNotifier class ClanInfoTestCase(unittest.TestCase): def setUp(self): self.claninfo = ClanInfo({'location': {'name': 'Iran', 'isCountry': 'true', 'countryCode': 'IR'}, 'warWinStreak': 0, 'isWarLogPublic': True}) def test_location(self): assert self.claninfo.location == 'Iran' def test_notset_location(self): claninfo = ClanInfo({}) assert claninfo.location == '' assert claninfo.country_flag_imoji == '' def test_country_imoji(self): assert self.claninfo.country_flag_imoji == '🇮🇷' def test_winstreak(self): assert self.claninfo.winstreak == 0 def test_is_warlog_public(self): assert self.claninfo.is_warlog_public == True class WarInfoTestCase(unittest.TestCase): def setUp(self): self.warinfo = WarInfo( json.loads(open(os.path.join('data', 'inWar_40.json'), 'r', encoding='utf8').read())) self.op_member = { ""tag"": ""#2GCR2YLP8"", ""name"": ""captain spock"", ""townhallLevel"": 9, ""mapPosition"": 18, ""opponentAttacks"": 2, ""bestOpponentAttack"": { ""attackerTag"": ""#G0QPL0LQ"", ""defenderTag"": ""#2GCR2YLP8"", ""stars"": 3, ""destructionPercentage"": 100, ""order"": 78 } } self.clan_member = { ""tag"": ""#9QVR8R29C"", ""name"": ""VAHID"", ""townhallLevel"": 7, ""mapPosition"": 35, ""opponentAttacks"": 2, ""bestOpponentAttack"": { ""attackerTag"": ""#P0C92YP99"", ""defenderTag"": ""#9QVR8R29C"", ""stars"": 3, ""destructionPercentage"": 100, ""order"": 3 } } def test_start_time(self): assert self.warinfo.start_time == '20170603T191148.000Z' def test_team_size(self): assert self.warinfo.team_size == 40 def test_get_ordered_attacks(self): ordered_attacks = self.warinfo.get_ordered_attacks() assert len(ordered_attacks) == 126 def test_player_count(self): assert len(self.warinfo.players) == 80 def test_get_player_attacks(self): player = self.warinfo.players['#2GCR2YLP8'] assert self.warinfo.get_player_attacks(player) == [] def test_get_player_info(self): with self.assertRaises(Exception): self.warinfo.players['#2GCZZZZP8'] def test_is_not_in_war(self): assert not self.warinfo.is_not_in_war() def test_is_in_preparation(self): assert not self.warinfo.is_in_preparation() def test_is_in_war(self): assert self.warinfo.is_in_war() def test_is_war_over(self): assert not self.warinfo.is_war_over() def test_is_clan_member(self): self.assertFalse(self.warinfo.is_clan_member(self.op_member)) self.assertTrue(self.warinfo.is_clan_member(self.clan_member)) def test_is_win(self): self.assertTrue(self.warinfo.is_win()) def test_is_draw(self): self.assertFalse(self.warinfo.is_draw()) def test_create_war_id(self): self.assertEqual(self.warinfo.create_war_id(), ""#YVL0C8UY#JC0L922Y20170602T201148.000Z"") class WarInfoNotInWarTestCase(unittest.TestCase): def setUp(self): self.warinfo = WarInfo(json.loads( open(os.path.join('data', 'notInWar.json'), 'r', encoding='utf8').read())) def test_clan_stats(self): self.assertEqual(self.warinfo.clan_level, 0) self.assertEqual(self.warinfo.clan_destruction, 0) self.assertEqual(self.warinfo.clan_stars, 0) self.assertEqual(self.warinfo.clan_attacks, 0) def test_op_stats(self): self.assertEqual(self.warinfo.op_level, 0) self.assertEqual(self.warinfo.op_destruction, 0) self.assertEqual(self.warinfo.op_stars, 0) self.assertEqual(self.warinfo.op_attacks, 0) def test_players(self): self.assertEqual(self.warinfo.players, {}) class WarStatsTestCase(unittest.TestCase): def setUp(self): warinfo = WarInfo(json.loads( open(os.path.join('data', 'warEnded_50.json'), 'r', encoding='utf8').read())) self.stats = WarStats(warinfo) self.attack161 = { ""destructionPercentage"": 53, ""attackerTag"": ""#9YUVL0CU"", ""order"": 161, ""stars"": 2, ""defenderTag"": ""#228U8G88L"" } self.attack150 = { ""destructionPercentage"": 100, ""attackerTag"": ""#2Q02GYCYV"", ""order"": 150, ""stars"": 3, ""defenderTag"": ""#2Y0C8YPYU"" } def test_first_attack_stats(self): stats = self.stats.calculate_war_stats_sofar(1) self.assertEqual(stats['clan_destruction'], 0) self.assertEqual(stats['op_destruction'], 1.76) self.assertEqual(stats['clan_stars'], 0) self.assertEqual(stats['op_stars'], 2) self.assertEqual(stats['clan_used_attacks'], 0) self.assertEqual(stats['op_used_attacks'], 1) def test_42th_attack_stats(self): stats = self.stats.calculate_war_stats_sofar(42) self.assertEqual(stats['clan_destruction'], 44.16) self.assertEqual(stats['op_destruction'], 26.56) self.assertEqual(stats['clan_stars'], 61) self.assertEqual(stats['op_stars'], 37) self.assertEqual(stats['clan_used_attacks'], 27) self.assertEqual(stats['op_used_attacks'], 15) def test_last_attack_stats(self): stats = self.stats.calculate_war_stats_sofar(162) self.assertEqual(stats['clan_destruction'], 96.72) self.assertEqual(stats['op_destruction'], 98.90) self.assertEqual(stats['clan_stars'], 142) self.assertEqual(stats['op_stars'], 147) self.assertEqual(stats['clan_used_attacks'], 87) self.assertEqual(stats['op_used_attacks'], 75) def test_attack_destruction(self): self.assertEqual( self.stats.get_attack_new_destruction(self.attack161), 0) self.assertEqual( self.stats.get_attack_new_destruction(self.attack150), 3) def test_attack_new_stars(self): self.assertEqual(self.stats.get_attack_new_stars(self.attack161), 0) self.assertEqual(self.stats.get_attack_new_stars(self.attack150), 1) class MessageFactoryTestCase(unittest.TestCase): def setUp(self): self.msg_factory = MessageFactory(None, None) self.setlocale_en() def _setlocale(self, language): os.environ['LANGUAGE'] = language gettext.bindtextdomain('messages', localedir=os.path.join(os.curdir, 'locales')) gettext.textdomain('messages') def setlocale_en(self): self._setlocale('en_US.UTF-8') def setlocale_fa(self): self._setlocale('fa_IR.UTF-8') def test_format_time_default(self): os.environ['LANG'] = 'en' os.environ['LANGUAGE'] = 'en' timestr = self.msg_factory.format_time('20170603T191148.000Z') self.assertEqual(timestr, 'Sat, 03 Jun 2017 19:11:48') def test_format_time_fa(self): os.environ['LANG'] = 'fa' timestr = self.msg_factory.format_time('20170603T191148.000Z') self.assertEqual(timestr, 'شنبه، ۱۳ خرداد ۱۳۹۶ ۲۳:۴۱:۴۸') def test_format_time_fa_IR(self): os.environ['LANGUAGE'] = 'fa_IR' timestr = self.msg_factory.format_time('20170603T191148.000Z') self.assertEqual(timestr, 'شنبه، ۱۳ خرداد ۱۳۹۶ ۲۳:۴۱:۴۸') def test_format_time_fa_IR_locale(self): self.setlocale_fa() timestr = self.msg_factory.format_time('20170603T191148.000Z') self.assertEqual(timestr, 'شنبه، ۱۳ خرداد ۱۳۹۶ ۲۳:۴۱:۴۸') class WarMonitorTestCase(unittest.TestCase): def setUp(self): coc_api = CoCAPI(None) self.warinfo = self.get_warinfo() our_claninfo = ClanInfo({'location': {'name': 'Iran', 'isCountry': 'true', 'countryCode': 'IR'}, 'warWinStreak': 0}) coc_api.get_currentwar = MagicMock(return_value=self.warinfo) coc_api.get_claninfo = MagicMock(return_value=our_claninfo) notifier = TelegramNotifier(None, None) notifier.send = MagicMock() self.monitor = WarMonitor({}, coc_api, '', notifier) self.monitor.update() self.clan_attack = { ""attackerTag"": ""#98VVJ8LV8"", ""defenderTag"": ""#8CCLRP2JC"", ""stars"": 3, ""destructionPercentage"": 100, ""order"": 10 } def get_warinfo(self): raise NotImplementedError() class WarMonitorInWarTestCase(WarMonitorTestCase): def get_warinfo(self): return WarInfo(json.loads( open(os.path.join('data', 'inWar_40.json'), 'r', encoding='utf8').read())) def test_send_preparation_msg(self): self.monitor.send_preparation_msg() self.assertTrue(self.monitor.is_msg_sent('preparation_msg')) self.assertTrue(self.monitor.is_msg_sent('players_msg')) def test_send_war_msg(self): self.monitor.send_war_msg() self.assertTrue(self.monitor.is_msg_sent('war_msg')) def test_is_attack_msg_sent(self): self.assertTrue", https://github.com/metachris/logzero,b5d49fc2b118c370994c4ae5360d7c246d43ddc8,tests/test_logzero.py::test_default_logger_stderr_output,tests/test_logzero.py,OD-Brit,flaky,RepoArchived,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """""" test_logzero ---------------------------------- Tests for `logzero` module. """""" import os import tempfile import logging import logzero import pytest def test_write_to_logfile_and_stderr(capsys): """""" Should log to a file. """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logger = logzero.setup_logger(logfile=temp.name) logger.info(""test log output"") _out, err = capsys.readouterr() assert "" test_logzero:"" in err assert err.endswith(""test log output\n"") with open(temp.name) as f: content = f.read() assert "" test_logzero:"" in content assert content.endswith(""test log output\n"") finally: temp.close() def test_custom_formatter(): """""" Should work with a custom formatter. """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: log_format = '%(color)s[%(levelname)1.1s %(asctime)s customnametest:%(lineno)d]%(end_color)s %(message)s' formatter = logzero.LogFormatter(fmt=log_format) logger = logzero.setup_logger(logfile=temp.name, formatter=formatter) logger.info(""test log output"") with open(temp.name) as f: content = f.read() assert "" customnametest:"" in content assert content.endswith(""test log output\n"") finally: temp.close() def test_loglevel(): """""" Should not log any debug messages if minimum level is set to INFO """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logger = logzero.setup_logger(logfile=temp.name, level=logging.INFO) logger.debug(""test log output"") with open(temp.name) as f: content = f.read() assert len(content.strip()) == 0 finally: temp.close() def test_bytes(): """""" Should properly log bytes """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logger = logzero.setup_logger(logfile=temp.name) testbytes = os.urandom(20) logger.debug(testbytes) logger.debug(None) # with open(temp.name) as f: # content = f.read() # # assert str(testbytes) in content finally: temp.close() def test_unicode(): """""" Should log unicode """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logger = logzero.setup_logger(logfile=temp.name) logger.debug(""😄 😁 😆 😅 😂"") with open(temp.name, ""rb"") as f: content = f.read() assert ""\\xf0\\x9f\\x98\\x84 \\xf0\\x9f\\x98\\x81 \\xf0\\x9f\\x98\\x86 \\xf0\\x9f\\x98\\x85 \\xf0\\x9f\\x98\\x82\\n"" in repr(content) finally: temp.close() def test_multiple_loggers_one_logfile(): """""" Should properly log bytes """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logger1 = logzero.setup_logger(name=""logger1"", logfile=temp.name) logger2 = logzero.setup_logger(name=""logger2"", logfile=temp.name) logger3 = logzero.setup_logger(name=""logger3"", logfile=temp.name) logger1.info(""logger1"") logger2.info(""logger2"") logger3.info(""logger3"") with open(temp.name) as f: content = f.read().strip() assert ""logger1"" in content assert ""logger2"" in content assert ""logger3"" in content assert len(content.split(""\n"")) == 3 finally: temp.close() def test_default_logger(disableStdErrorLogger=False): """""" Default logger should work and be able to be reconfigured. """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logzero.setup_default_logger(logfile=temp.name, disableStderrLogger=disableStdErrorLogger) logzero.logger.debug(""debug1"") # will be logged # Reconfigure with loglevel INFO logzero.setup_default_logger(logfile=temp.name, level=logging.INFO, disableStderrLogger=disableStdErrorLogger) logzero.logger.debug(""debug2"") # will not be logged logzero.logger.info(""info1"") # will be logged # Reconfigure with a different formatter log_format = '%(color)s[xxx]%(end_color)s %(message)s' formatter = logzero.LogFormatter(fmt=log_format) logzero.setup_default_logger(logfile=temp.name, level=logging.INFO, formatter=formatter, disableStderrLogger=disableStdErrorLogger) logzero.logger.info(""info2"") # will be logged with new formatter logzero.logger.debug(""debug3"") # will not be logged with open(temp.name) as f: content = f.read() test_default_logger_output(content) finally: temp.close() @pytest.mark.skip(reason=""not a standalone test"") def test_default_logger_output(content): assert ""] debug1"" in content assert ""] debug2"" not in content assert ""] info1"" in content assert ""xxx] info2"" in content assert ""] debug3"" not in content def test_setup_logger_reconfiguration(): """""" Should be able to reconfigure without loosing custom handlers """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() temp2 = tempfile.NamedTemporaryFile() try: logzero.setup_default_logger(logfile=temp.name) # Add a custom file handler filehandler = logging.FileHandler(temp2.name) filehandler.setLevel(logging.DEBUG) filehandler.setFormatter(logzero.LogFormatter(color=False)) logzero.logger.addHandler(filehandler) # First debug message goes to both files logzero.logger.debug(""debug1"") # Reconfigure logger to remove logfile logzero.setup_default_logger() logzero.logger.debug(""debug2"") # Reconfigure logger to add logfile logzero.setup_default_logger(logfile=temp.name) logzero.logger.debug(""debug3"") # Reconfigure logger to set minimum loglevel to INFO logzero.setup_default_logger(logfile=temp.name, level=logging.INFO) logzero.logger.debug(""debug4"") logzero.logger.info(""info1"") # Reconfigure logger to set minimum loglevel back to DEBUG logzero.setup_default_logger(logfile=temp.name, level=logging.DEBUG) logzero.logger.debug(""debug5"") with open(temp.name) as f: content = f.read() assert ""] debug1"" in content assert ""] debug2"" not in content assert ""] debug3"" in content assert ""] debug4"" not in content assert ""] info1"" in content assert ""] debug5"" in content with open(temp2.name) as f: content = f.read() assert ""] debug1"" in content assert ""] debug2"" in content assert ""] debug3"" in content assert ""] debug4"" not in content assert ""] info1"" in content assert ""] debug5"" in content finally: temp.close() def test_setup_logger_logfile_custom_loglevel(capsys): """""" setup_logger(..) with filelogger and custom loglevel """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logger = logzero.setup_logger(logfile=temp.name, fileLoglevel=logging.WARN) logger.info(""info1"") logger.warn(""warn1"") with open(temp.name) as f: content = f.read() assert ""] info1"" not in content assert ""] warn1"" in content finally: temp.close() def test_log_function_call(): @logzero.log_function_call def example(): """"""example doc"""""" pass assert example.__name__ == ""example"" assert example.__doc__ == ""example doc"" def test_default_logger_logfile_only(capsys): """""" Run the ``test_default_logger`` with ``disableStdErrorLogger`` set to ``True`` and confirm that no data is written to stderr """""" test_default_logger(disableStdErrorLogger=True) out, err = capsys.readouterr() assert err == '' def test_default_logger_stderr_output(capsys): """""" Run the ``test_default_logger`` and confirm that the proper data is written to stderr """""" test_default_logger() out, err = capsys.readouterr() test_default_logger_output(err) def test_default_logger_syslog_only(capsys): """""" Run a test logging to ``syslog`` and confirm that no data is written to stderr. Note that the output in syslog is not currently being captured or checked. """""" logzero.reset_default_logger() logzero.syslog() logzero.logger.error('debug') out, err = capsys.readouterr() assert out == '' and err == '' ", https://github.com/metachris/logzero,b5d49fc2b118c370994c4ae5360d7c246d43ddc8,tests/test_logzero.py::test_write_to_logfile_and_stderr,tests/test_logzero.py,NIO,flaky,RepoArchived,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """""" test_logzero ---------------------------------- Tests for `logzero` module. """""" import os import tempfile import logging import logzero import pytest def test_write_to_logfile_and_stderr(capsys): """""" Should log to a file. """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logger = logzero.setup_logger(logfile=temp.name) logger.info(""test log output"") _out, err = capsys.readouterr() assert "" test_logzero:"" in err assert err.endswith(""test log output\n"") with open(temp.name) as f: content = f.read() assert "" test_logzero:"" in content assert content.endswith(""test log output\n"") finally: temp.close() def test_custom_formatter(): """""" Should work with a custom formatter. """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: log_format = '%(color)s[%(levelname)1.1s %(asctime)s customnametest:%(lineno)d]%(end_color)s %(message)s' formatter = logzero.LogFormatter(fmt=log_format) logger = logzero.setup_logger(logfile=temp.name, formatter=formatter) logger.info(""test log output"") with open(temp.name) as f: content = f.read() assert "" customnametest:"" in content assert content.endswith(""test log output\n"") finally: temp.close() def test_loglevel(): """""" Should not log any debug messages if minimum level is set to INFO """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logger = logzero.setup_logger(logfile=temp.name, level=logging.INFO) logger.debug(""test log output"") with open(temp.name) as f: content = f.read() assert len(content.strip()) == 0 finally: temp.close() def test_bytes(): """""" Should properly log bytes """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logger = logzero.setup_logger(logfile=temp.name) testbytes = os.urandom(20) logger.debug(testbytes) logger.debug(None) # with open(temp.name) as f: # content = f.read() # # assert str(testbytes) in content finally: temp.close() def test_unicode(): """""" Should log unicode """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logger = logzero.setup_logger(logfile=temp.name) logger.debug(""😄 😁 😆 😅 😂"") with open(temp.name, ""rb"") as f: content = f.read() assert ""\\xf0\\x9f\\x98\\x84 \\xf0\\x9f\\x98\\x81 \\xf0\\x9f\\x98\\x86 \\xf0\\x9f\\x98\\x85 \\xf0\\x9f\\x98\\x82\\n"" in repr(content) finally: temp.close() def test_multiple_loggers_one_logfile(): """""" Should properly log bytes """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logger1 = logzero.setup_logger(name=""logger1"", logfile=temp.name) logger2 = logzero.setup_logger(name=""logger2"", logfile=temp.name) logger3 = logzero.setup_logger(name=""logger3"", logfile=temp.name) logger1.info(""logger1"") logger2.info(""logger2"") logger3.info(""logger3"") with open(temp.name) as f: content = f.read().strip() assert ""logger1"" in content assert ""logger2"" in content assert ""logger3"" in content assert len(content.split(""\n"")) == 3 finally: temp.close() def test_default_logger(disableStdErrorLogger=False): """""" Default logger should work and be able to be reconfigured. """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logzero.setup_default_logger(logfile=temp.name, disableStderrLogger=disableStdErrorLogger) logzero.logger.debug(""debug1"") # will be logged # Reconfigure with loglevel INFO logzero.setup_default_logger(logfile=temp.name, level=logging.INFO, disableStderrLogger=disableStdErrorLogger) logzero.logger.debug(""debug2"") # will not be logged logzero.logger.info(""info1"") # will be logged # Reconfigure with a different formatter log_format = '%(color)s[xxx]%(end_color)s %(message)s' formatter = logzero.LogFormatter(fmt=log_format) logzero.setup_default_logger(logfile=temp.name, level=logging.INFO, formatter=formatter, disableStderrLogger=disableStdErrorLogger) logzero.logger.info(""info2"") # will be logged with new formatter logzero.logger.debug(""debug3"") # will not be logged with open(temp.name) as f: content = f.read() test_default_logger_output(content) finally: temp.close() @pytest.mark.skip(reason=""not a standalone test"") def test_default_logger_output(content): assert ""] debug1"" in content assert ""] debug2"" not in content assert ""] info1"" in content assert ""xxx] info2"" in content assert ""] debug3"" not in content def test_setup_logger_reconfiguration(): """""" Should be able to reconfigure without loosing custom handlers """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() temp2 = tempfile.NamedTemporaryFile() try: logzero.setup_default_logger(logfile=temp.name) # Add a custom file handler filehandler = logging.FileHandler(temp2.name) filehandler.setLevel(logging.DEBUG) filehandler.setFormatter(logzero.LogFormatter(color=False)) logzero.logger.addHandler(filehandler) # First debug message goes to both files logzero.logger.debug(""debug1"") # Reconfigure logger to remove logfile logzero.setup_default_logger() logzero.logger.debug(""debug2"") # Reconfigure logger to add logfile logzero.setup_default_logger(logfile=temp.name) logzero.logger.debug(""debug3"") # Reconfigure logger to set minimum loglevel to INFO logzero.setup_default_logger(logfile=temp.name, level=logging.INFO) logzero.logger.debug(""debug4"") logzero.logger.info(""info1"") # Reconfigure logger to set minimum loglevel back to DEBUG logzero.setup_default_logger(logfile=temp.name, level=logging.DEBUG) logzero.logger.debug(""debug5"") with open(temp.name) as f: content = f.read() assert ""] debug1"" in content assert ""] debug2"" not in content assert ""] debug3"" in content assert ""] debug4"" not in content assert ""] info1"" in content assert ""] debug5"" in content with open(temp2.name) as f: content = f.read() assert ""] debug1"" in content assert ""] debug2"" in content assert ""] debug3"" in content assert ""] debug4"" not in content assert ""] info1"" in content assert ""] debug5"" in content finally: temp.close() def test_setup_logger_logfile_custom_loglevel(capsys): """""" setup_logger(..) with filelogger and custom loglevel """""" logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() try: logger = logzero.setup_logger(logfile=temp.name, fileLoglevel=logging.WARN) logger.info(""info1"") logger.warn(""warn1"") with open(temp.name) as f: content = f.read() assert ""] info1"" not in content assert ""] warn1"" in content finally: temp.close() def test_log_function_call(): @logzero.log_function_call def example(): """"""example doc"""""" pass assert example.__name__ == ""example"" assert example.__doc__ == ""example doc"" def test_default_logger_logfile_only(capsys): """""" Run the ``test_default_logger`` with ``disableStdErrorLogger`` set to ``True`` and confirm that no data is written to stderr """""" test_default_logger(disableStdErrorLogger=True) out, err = capsys.readouterr() assert err == '' def test_default_logger_stderr_output(capsys): """""" Run the ``test_default_logger`` and confirm that the proper data is written to stderr """""" test_default_logger() out, err = capsys.readouterr() test_default_logger_output(err) def test_default_logger_syslog_only(capsys): """""" Run a test logging to ``syslog`` and confirm that no data is written to stderr. Note that the output in syslog is not currently being captured or checked. """""" logzero.reset_default_logger() logzero.syslog() logzero.logger.error('debug') out, err = capsys.readouterr() assert out == '' and err == '' ", https://github.com/mfreeborn/fastapi-sqlalchemy,8ddf0edab22873365441757107ce21234bc09f8f,tests/test_session.py::test_inside_route_without_middleware_fails,tests/test_session.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest from sqlalchemy import create_engine from sqlalchemy.orm import Session from starlette.middleware.base import BaseHTTPMiddleware from fastapi_sqlalchemy.exceptions import MissingSessionError, SessionNotInitialisedError db_url = ""sqlite://"" def test_init(app, DBSessionMiddleware): mw = DBSessionMiddleware(app, db_url=db_url) assert isinstance(mw, BaseHTTPMiddleware) def test_init_required_args(app, DBSessionMiddleware): with pytest.raises(ValueError) as exc_info: DBSessionMiddleware(app) assert exc_info.value.args[0] == ""You need to pass a db_url or a custom_engine parameter."" def test_init_required_args_custom_engine(app, db, DBSessionMiddleware): custom_engine = create_engine(db_url) DBSessionMiddleware(app, custom_engine=custom_engine) def test_init_correct_optional_args(app, db, DBSessionMiddleware): engine_args = {""echo"": True} session_args = {""expire_on_commit"": False} DBSessionMiddleware(app, db_url, engine_args=engine_args, session_args=session_args) with db(): assert not db.session.expire_on_commit engine = db.session.bind assert engine.echo def test_init_incorrect_optional_args(app, DBSessionMiddleware): with pytest.raises(TypeError) as exc_info: DBSessionMiddleware(app, db_url=db_url, invalid_args=""test"") assert exc_info.value.args[0] == ""__init__() got an unexpected keyword argument 'invalid_args'"" def test_inside_route(app, client, db, DBSessionMiddleware): app.add_middleware(DBSessionMiddleware, db_url=db_url) @app.get(""/"") def test_get(): assert isinstance(db.session, Session) client.get(""/"") def test_inside_route_without_middleware_fails(app, client, db): @app.get(""/"") def test_get(): with pytest.raises(SessionNotInitialisedError): db.session client.get(""/"") def test_outside_of_route(app, db, DBSessionMiddleware): app.add_middleware(DBSessionMiddleware, db_url=db_url) with db(): assert isinstance(db.session, Session) def test_outside_of_route_without_middleware_fails(db): with pytest.raises(SessionNotInitialisedError): db.session with pytest.raises(SessionNotInitialisedError): with db(): pass def test_outside_of_route_without_context_fails(app, db, DBSessionMiddleware): app.add_middleware(DBSessionMiddleware, db_url=db_url) with pytest.raises(MissingSessionError): db.session def test_db_context_temporary_session_args(app, db, DBSessionMiddleware): app.add_middleware(DBSessionMiddleware, db_url=db_url) session_args = {} with db(session_args=session_args): assert isinstance(db.session, Session) assert db.session.expire_on_commit session_args = {""expire_on_commit"": False} with db(session_args=session_args): assert not db.session.expire_on_commit def test_rollback(app, db, DBSessionMiddleware): # pytest-cov shows that the line in db.__exit__() rolling back the db session # when there is an Exception is run correctly. However, it would be much better # if we could demonstrate somehow that db.session.rollback() was called e.g. once app.add_middleware(DBSessionMiddleware, db_url=db_url) with pytest.raises(Exception): with db(): raise Exception ", https://github.com/mfreeborn/fastapi-sqlalchemy,8ddf0edab22873365441757107ce21234bc09f8f,tests/test_session.py::test_outside_of_route_without_middleware_fails,tests/test_session.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest from sqlalchemy import create_engine from sqlalchemy.orm import Session from starlette.middleware.base import BaseHTTPMiddleware from fastapi_sqlalchemy.exceptions import MissingSessionError, SessionNotInitialisedError db_url = ""sqlite://"" def test_init(app, DBSessionMiddleware): mw = DBSessionMiddleware(app, db_url=db_url) assert isinstance(mw, BaseHTTPMiddleware) def test_init_required_args(app, DBSessionMiddleware): with pytest.raises(ValueError) as exc_info: DBSessionMiddleware(app) assert exc_info.value.args[0] == ""You need to pass a db_url or a custom_engine parameter."" def test_init_required_args_custom_engine(app, db, DBSessionMiddleware): custom_engine = create_engine(db_url) DBSessionMiddleware(app, custom_engine=custom_engine) def test_init_correct_optional_args(app, db, DBSessionMiddleware): engine_args = {""echo"": True} session_args = {""expire_on_commit"": False} DBSessionMiddleware(app, db_url, engine_args=engine_args, session_args=session_args) with db(): assert not db.session.expire_on_commit engine = db.session.bind assert engine.echo def test_init_incorrect_optional_args(app, DBSessionMiddleware): with pytest.raises(TypeError) as exc_info: DBSessionMiddleware(app, db_url=db_url, invalid_args=""test"") assert exc_info.value.args[0] == ""__init__() got an unexpected keyword argument 'invalid_args'"" def test_inside_route(app, client, db, DBSessionMiddleware): app.add_middleware(DBSessionMiddleware, db_url=db_url) @app.get(""/"") def test_get(): assert isinstance(db.session, Session) client.get(""/"") def test_inside_route_without_middleware_fails(app, client, db): @app.get(""/"") def test_get(): with pytest.raises(SessionNotInitialisedError): db.session client.get(""/"") def test_outside_of_route(app, db, DBSessionMiddleware): app.add_middleware(DBSessionMiddleware, db_url=db_url) with db(): assert isinstance(db.session, Session) def test_outside_of_route_without_middleware_fails(db): with pytest.raises(SessionNotInitialisedError): db.session with pytest.raises(SessionNotInitialisedError): with db(): pass def test_outside_of_route_without_context_fails(app, db, DBSessionMiddleware): app.add_middleware(DBSessionMiddleware, db_url=db_url) with pytest.raises(MissingSessionError): db.session def test_db_context_temporary_session_args(app, db, DBSessionMiddleware): app.add_middleware(DBSessionMiddleware, db_url=db_url) session_args = {} with db(session_args=session_args): assert isinstance(db.session, Session) assert db.session.expire_on_commit session_args = {""expire_on_commit"": False} with db(session_args=session_args): assert not db.session.expire_on_commit def test_rollback(app, db, DBSessionMiddleware): # pytest-cov shows that the line in db.__exit__() rolling back the db session # when there is an Exception is run correctly. However, it would be much better # if we could demonstrate somehow that db.session.rollback() was called e.g. once app.add_middleware(DBSessionMiddleware, db_url=db_url) with pytest.raises(Exception): with db(): raise Exception ", https://github.com/michaelpb/stowage,7c619be755b9b73fd202367bcf9014472f318834,test/test_stowage.py::TestPathGenerators::test_directory_walk,test/test_stowage.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"''' Tests for `stowage` module. ''' import os from os.path import join, exists import pytest import tempfile from stowage import stowage class BlankEnvironBase: @classmethod def setup_class(cls): # Save & delete cls.original_source = os.environ.get('STOWAGE_SOURCE') if cls.original_source is not None: del os.environ['STOWAGE_SOURCE'] cls.original_dest = os.environ.get('STOWAGE_DESTINATION') if cls.original_source is not None: del os.environ['STOWAGE_DESTINATION'] cls.original_bup = os.environ.get('STOWAGE_BACKUP') if cls.original_bup is not None: del os.environ['STOWAGE_BACKUP'] @classmethod def teardown_class(cls): # Restore environmental variables if cls.original_source is not None: os.environ['STOWAGE_SOURCE'] = cls.original_source if cls.original_dest is not None: os.environ['STOWAGE_DESTINATION'] = cls.original_dest if cls.original_bup is not None: os.environ['STOWAGE_BACKUP'] = cls.original_bup stowage._is_verbose = False class TestParseArgs(BlankEnvironBase): def test_parse_args_default(self): args = stowage.parse_args([]) assert args.packages == [] assert args.source == '~/dotfiles' assert args.destination == '~' assert args.backup == '~/.config/stowage/backup/' assert not args.dryrun assert not args.verbose assert not args.skip_backup def test_parse_args_all(self): args = stowage.parse_args([ '-d', 'dest', '-s', 'src', '-b', 'backup', '--dryrun', '--verbose', '--skip-backup', 'pkg1', 'pkg2', 'pkg3', ]) assert args.packages == ['pkg1', 'pkg2', 'pkg3'] assert args.source == 'src' assert args.destination == 'dest' assert args.backup == 'backup' assert args.dryrun assert args.verbose assert args.skip_backup def test_environ_defaults(self): os.environ['STOWAGE_SOURCE'] = 'src' os.environ['STOWAGE_DESTINATION'] = 'dest' os.environ['STOWAGE_BACKUP'] = 'bup' args = stowage.parse_args([]) assert args.source == 'src' assert args.destination == 'dest' assert args.backup == 'bup' del os.environ['STOWAGE_SOURCE'] del os.environ['STOWAGE_DESTINATION'] del os.environ['STOWAGE_BACKUP'] def test_check_args(self): args = stowage.parse_args([]) with pytest.raises(ValueError): stowage.check_args(args) args = stowage.parse_args(['-s', '~/stuff', '-v', 'pkg']) stowage.check_args(args) assert stowage._is_verbose # ensure expands home assert ( 'home' in args.source or # linux 'Users' in args.source # macOS ) def test_arg_source_directories(self): args = stowage.parse_args([ '--source', 'src', '--dest', 'dest', 'pkg1', 'pkg2', ]) dirs = list(stowage.source_directories(args)) assert dirs == ['src/pkg1', 'src/pkg2'] def write_tmp_file(path): try: os.makedirs(os.path.dirname(path)) except OSError: pass open(path, 'w+').write('%s contents' % path) def gen_tmp_files(root, files): for fn in files: write_tmp_file(join(root, fn)) def clear_tmp_files(root, files): for fn in files: path = join(root, fn) if exists(path): os.remove(path) try: os.removedirs(os.path.dirname(path)) except OSError: pass class TestPathGenerators: FILES = [ '_vimrc', '_config/openbox/openbox.xml', ] @classmethod def setup_class(cls): cls.dir = tempfile.mkdtemp(prefix='tmp_stowage_test_') gen_tmp_files(cls.dir, cls.FILES) cls.results = set([ (join(cls.dir, '_vimrc'), join('test_out', '.vimrc')), (join(cls.dir, '_config/openbox/openbox.xml'), join('test_out', '.config/openbox/openbox.xml')), ]) @classmethod def teardown_class(cls): clear_tmp_files(cls.dir, cls.FILES + ['.vimrc']) def test_directory_walk(self): results = list(stowage.directory_walk(self.dir, 'test_out')) results_set = set(results) assert len(results_set) == len(results) # ensure no dupes assert results_set == self.results def test_needed_symlink_walk(self): results = list(stowage.needed_symlink_walk(self.dir, 'test_out')) results_set = set(results) assert len(results_set) == len(results) # ensure no dupes assert results_set == self.results def test_partially_needed_symlink_walk(self): os.symlink(__file__, join(self.dir, '.vimrc')) results = list(stowage.needed_symlink_walk(self.dir, self.dir)) results_set = set(results) assert len(results_set) == len(results) # ensure no dupes assert results_set == set([ (join(self.dir, '_config/openbox/openbox.xml'), join(self.dir, '.config/openbox/openbox.xml')), ]) class TestFullBehavior: FILES = [ '_vimrc', '_config/openbox/openbox.xml', ] def setup_method(self, method): self.dir = tempfile.mkdtemp(prefix='tmp_stowage_test_IN_') self.out_dir = tempfile.mkdtemp(prefix='tmp_stowage_test_OUT_') gen_tmp_files(join(self.dir, 'vim'), self.FILES) def teardown_method(self, method): clear_tmp_files(join(self.dir, 'vim'), self.FILES) clear_tmp_files(self.out_dir, self.FILES) def test_main(self): args = stowage.parse_args([ '--source', self.dir, '--destination', self.out_dir, '--backup', join(self.dir, 'path/to/bup'), 'vim', ]) stowage.main(args) assert exists(join(self.out_dir, '.vimrc')) assert exists(join(self.out_dir, '.config', 'openbox', 'openbox.xml')) contents = open(join(self.out_dir, '.vimrc')).read() assert contents == '%s contents' % join(self.dir, 'vim', '_vimrc') def test_backup(self): args = stowage.parse_args([ '--source', self.dir, '--destination', self.out_dir, '--backup', join(self.dir, 'path/to/bup'), 'vim', ]) open(join(self.out_dir, '.vimrc'), 'w+').write('original') stowage.main(args) assert exists(join(self.out_dir, '.vimrc')) assert exists(join(self.out_dir, '.config', 'openbox', 'openbox.xml')) assert exists(join(self.dir, 'path', 'to', 'bup', '.vimrc')) contents = open(join(self.dir, 'path', 'to', 'bup', '.vimrc')).read() assert contents == 'original' contents = open(join(self.out_dir, '.vimrc')).read() assert contents == '%s contents' % join(self.dir, 'vim', '_vimrc') ", https://github.com/michaelpb/stowage,7c619be755b9b73fd202367bcf9014472f318834,test/test_stowage.py::TestPathGenerators::test_needed_symlink_walk,test/test_stowage.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"''' Tests for `stowage` module. ''' import os from os.path import join, exists import pytest import tempfile from stowage import stowage class BlankEnvironBase: @classmethod def setup_class(cls): # Save & delete cls.original_source = os.environ.get('STOWAGE_SOURCE') if cls.original_source is not None: del os.environ['STOWAGE_SOURCE'] cls.original_dest = os.environ.get('STOWAGE_DESTINATION') if cls.original_source is not None: del os.environ['STOWAGE_DESTINATION'] cls.original_bup = os.environ.get('STOWAGE_BACKUP') if cls.original_bup is not None: del os.environ['STOWAGE_BACKUP'] @classmethod def teardown_class(cls): # Restore environmental variables if cls.original_source is not None: os.environ['STOWAGE_SOURCE'] = cls.original_source if cls.original_dest is not None: os.environ['STOWAGE_DESTINATION'] = cls.original_dest if cls.original_bup is not None: os.environ['STOWAGE_BACKUP'] = cls.original_bup stowage._is_verbose = False class TestParseArgs(BlankEnvironBase): def test_parse_args_default(self): args = stowage.parse_args([]) assert args.packages == [] assert args.source == '~/dotfiles' assert args.destination == '~' assert args.backup == '~/.config/stowage/backup/' assert not args.dryrun assert not args.verbose assert not args.skip_backup def test_parse_args_all(self): args = stowage.parse_args([ '-d', 'dest', '-s', 'src', '-b', 'backup', '--dryrun', '--verbose', '--skip-backup', 'pkg1', 'pkg2', 'pkg3', ]) assert args.packages == ['pkg1', 'pkg2', 'pkg3'] assert args.source == 'src' assert args.destination == 'dest' assert args.backup == 'backup' assert args.dryrun assert args.verbose assert args.skip_backup def test_environ_defaults(self): os.environ['STOWAGE_SOURCE'] = 'src' os.environ['STOWAGE_DESTINATION'] = 'dest' os.environ['STOWAGE_BACKUP'] = 'bup' args = stowage.parse_args([]) assert args.source == 'src' assert args.destination == 'dest' assert args.backup == 'bup' del os.environ['STOWAGE_SOURCE'] del os.environ['STOWAGE_DESTINATION'] del os.environ['STOWAGE_BACKUP'] def test_check_args(self): args = stowage.parse_args([]) with pytest.raises(ValueError): stowage.check_args(args) args = stowage.parse_args(['-s', '~/stuff', '-v', 'pkg']) stowage.check_args(args) assert stowage._is_verbose # ensure expands home assert ( 'home' in args.source or # linux 'Users' in args.source # macOS ) def test_arg_source_directories(self): args = stowage.parse_args([ '--source', 'src', '--dest', 'dest', 'pkg1', 'pkg2', ]) dirs = list(stowage.source_directories(args)) assert dirs == ['src/pkg1', 'src/pkg2'] def write_tmp_file(path): try: os.makedirs(os.path.dirname(path)) except OSError: pass open(path, 'w+').write('%s contents' % path) def gen_tmp_files(root, files): for fn in files: write_tmp_file(join(root, fn)) def clear_tmp_files(root, files): for fn in files: path = join(root, fn) if exists(path): os.remove(path) try: os.removedirs(os.path.dirname(path)) except OSError: pass class TestPathGenerators: FILES = [ '_vimrc', '_config/openbox/openbox.xml', ] @classmethod def setup_class(cls): cls.dir = tempfile.mkdtemp(prefix='tmp_stowage_test_') gen_tmp_files(cls.dir, cls.FILES) cls.results = set([ (join(cls.dir, '_vimrc'), join('test_out', '.vimrc')), (join(cls.dir, '_config/openbox/openbox.xml'), join('test_out', '.config/openbox/openbox.xml')), ]) @classmethod def teardown_class(cls): clear_tmp_files(cls.dir, cls.FILES + ['.vimrc']) def test_directory_walk(self): results = list(stowage.directory_walk(self.dir, 'test_out')) results_set = set(results) assert len(results_set) == len(results) # ensure no dupes assert results_set == self.results def test_needed_symlink_walk(self): results = list(stowage.needed_symlink_walk(self.dir, 'test_out')) results_set = set(results) assert len(results_set) == len(results) # ensure no dupes assert results_set == self.results def test_partially_needed_symlink_walk(self): os.symlink(__file__, join(self.dir, '.vimrc')) results = list(stowage.needed_symlink_walk(self.dir, self.dir)) results_set = set(results) assert len(results_set) == len(results) # ensure no dupes assert results_set == set([ (join(self.dir, '_config/openbox/openbox.xml'), join(self.dir, '.config/openbox/openbox.xml')), ]) class TestFullBehavior: FILES = [ '_vimrc', '_config/openbox/openbox.xml', ] def setup_method(self, method): self.dir = tempfile.mkdtemp(prefix='tmp_stowage_test_IN_') self.out_dir = tempfile.mkdtemp(prefix='tmp_stowage_test_OUT_') gen_tmp_files(join(self.dir, 'vim'), self.FILES) def teardown_method(self, method): clear_tmp_files(join(self.dir, 'vim'), self.FILES) clear_tmp_files(self.out_dir, self.FILES) def test_main(self): args = stowage.parse_args([ '--source', self.dir, '--destination', self.out_dir, '--backup', join(self.dir, 'path/to/bup'), 'vim', ]) stowage.main(args) assert exists(join(self.out_dir, '.vimrc')) assert exists(join(self.out_dir, '.config', 'openbox', 'openbox.xml')) contents = open(join(self.out_dir, '.vimrc')).read() assert contents == '%s contents' % join(self.dir, 'vim', '_vimrc') def test_backup(self): args = stowage.parse_args([ '--source', self.dir, '--destination', self.out_dir, '--backup', join(self.dir, 'path/to/bup'), 'vim', ]) open(join(self.out_dir, '.vimrc'), 'w+').write('original') stowage.main(args) assert exists(join(self.out_dir, '.vimrc')) assert exists(join(self.out_dir, '.config', 'openbox', 'openbox.xml')) assert exists(join(self.dir, 'path', 'to', 'bup', '.vimrc')) contents = open(join(self.dir, 'path', 'to', 'bup', '.vimrc')).read() assert contents == 'original' contents = open(join(self.out_dir, '.vimrc')).read() assert contents == '%s contents' % join(self.dir, 'vim', '_vimrc') ", https://github.com/michaelpb/stowage,7c619be755b9b73fd202367bcf9014472f318834,test/test_stowage.py::TestPathGenerators::test_partially_needed_symlink_walk,test/test_stowage.py,NIO,flaky,Opened,https://github.com/michaelpb/stowage/pull/1,classify;root_cause,"''' Tests for `stowage` module. ''' import os from os.path import join, exists import pytest import tempfile from stowage import stowage class BlankEnvironBase: @classmethod def setup_class(cls): # Save & delete cls.original_source = os.environ.get('STOWAGE_SOURCE') if cls.original_source is not None: del os.environ['STOWAGE_SOURCE'] cls.original_dest = os.environ.get('STOWAGE_DESTINATION') if cls.original_source is not None: del os.environ['STOWAGE_DESTINATION'] cls.original_bup = os.environ.get('STOWAGE_BACKUP') if cls.original_bup is not None: del os.environ['STOWAGE_BACKUP'] @classmethod def teardown_class(cls): # Restore environmental variables if cls.original_source is not None: os.environ['STOWAGE_SOURCE'] = cls.original_source if cls.original_dest is not None: os.environ['STOWAGE_DESTINATION'] = cls.original_dest if cls.original_bup is not None: os.environ['STOWAGE_BACKUP'] = cls.original_bup stowage._is_verbose = False class TestParseArgs(BlankEnvironBase): def test_parse_args_default(self): args = stowage.parse_args([]) assert args.packages == [] assert args.source == '~/dotfiles' assert args.destination == '~' assert args.backup == '~/.config/stowage/backup/' assert not args.dryrun assert not args.verbose assert not args.skip_backup def test_parse_args_all(self): args = stowage.parse_args([ '-d', 'dest', '-s', 'src', '-b', 'backup', '--dryrun', '--verbose', '--skip-backup', 'pkg1', 'pkg2', 'pkg3', ]) assert args.packages == ['pkg1', 'pkg2', 'pkg3'] assert args.source == 'src' assert args.destination == 'dest' assert args.backup == 'backup' assert args.dryrun assert args.verbose assert args.skip_backup def test_environ_defaults(self): os.environ['STOWAGE_SOURCE'] = 'src' os.environ['STOWAGE_DESTINATION'] = 'dest' os.environ['STOWAGE_BACKUP'] = 'bup' args = stowage.parse_args([]) assert args.source == 'src' assert args.destination == 'dest' assert args.backup == 'bup' del os.environ['STOWAGE_SOURCE'] del os.environ['STOWAGE_DESTINATION'] del os.environ['STOWAGE_BACKUP'] def test_check_args(self): args = stowage.parse_args([]) with pytest.raises(ValueError): stowage.check_args(args) args = stowage.parse_args(['-s', '~/stuff', '-v', 'pkg']) stowage.check_args(args) assert stowage._is_verbose # ensure expands home assert ( 'home' in args.source or # linux 'Users' in args.source # macOS ) def test_arg_source_directories(self): args = stowage.parse_args([ '--source', 'src', '--dest', 'dest', 'pkg1', 'pkg2', ]) dirs = list(stowage.source_directories(args)) assert dirs == ['src/pkg1', 'src/pkg2'] def write_tmp_file(path): try: os.makedirs(os.path.dirname(path)) except OSError: pass open(path, 'w+').write('%s contents' % path) def gen_tmp_files(root, files): for fn in files: write_tmp_file(join(root, fn)) def clear_tmp_files(root, files): for fn in files: path = join(root, fn) if exists(path): os.remove(path) try: os.removedirs(os.path.dirname(path)) except OSError: pass class TestPathGenerators: FILES = [ '_vimrc', '_config/openbox/openbox.xml', ] @classmethod def setup_class(cls): cls.dir = tempfile.mkdtemp(prefix='tmp_stowage_test_') gen_tmp_files(cls.dir, cls.FILES) cls.results = set([ (join(cls.dir, '_vimrc'), join('test_out', '.vimrc')), (join(cls.dir, '_config/openbox/openbox.xml'), join('test_out', '.config/openbox/openbox.xml')), ]) @classmethod def teardown_class(cls): clear_tmp_files(cls.dir, cls.FILES + ['.vimrc']) def test_directory_walk(self): results = list(stowage.directory_walk(self.dir, 'test_out')) results_set = set(results) assert len(results_set) == len(results) # ensure no dupes assert results_set == self.results def test_needed_symlink_walk(self): results = list(stowage.needed_symlink_walk(self.dir, 'test_out')) results_set = set(results) assert len(results_set) == len(results) # ensure no dupes assert results_set == self.results def test_partially_needed_symlink_walk(self): os.symlink(__file__, join(self.dir, '.vimrc')) results = list(stowage.needed_symlink_walk(self.dir, self.dir)) results_set = set(results) assert len(results_set) == len(results) # ensure no dupes assert results_set == set([ (join(self.dir, '_config/openbox/openbox.xml'), join(self.dir, '.config/openbox/openbox.xml')), ]) class TestFullBehavior: FILES = [ '_vimrc', '_config/openbox/openbox.xml', ] def setup_method(self, method): self.dir = tempfile.mkdtemp(prefix='tmp_stowage_test_IN_') self.out_dir = tempfile.mkdtemp(prefix='tmp_stowage_test_OUT_') gen_tmp_files(join(self.dir, 'vim'), self.FILES) def teardown_method(self, method): clear_tmp_files(join(self.dir, 'vim'), self.FILES) clear_tmp_files(self.out_dir, self.FILES) def test_main(self): args = stowage.parse_args([ '--source', self.dir, '--destination', self.out_dir, '--backup', join(self.dir, 'path/to/bup'), 'vim', ]) stowage.main(args) assert exists(join(self.out_dir, '.vimrc')) assert exists(join(self.out_dir, '.config', 'openbox', 'openbox.xml')) contents = open(join(self.out_dir, '.vimrc')).read() assert contents == '%s contents' % join(self.dir, 'vim', '_vimrc') def test_backup(self): args = stowage.parse_args([ '--source', self.dir, '--destination', self.out_dir, '--backup', join(self.dir, 'path/to/bup'), 'vim', ]) open(join(self.out_dir, '.vimrc'), 'w+').write('original') stowage.main(args) assert exists(join(self.out_dir, '.vimrc')) assert exists(join(self.out_dir, '.config', 'openbox', 'openbox.xml')) assert exists(join(self.dir, 'path', 'to', 'bup', '.vimrc')) contents = open(join(self.dir, 'path', 'to', 'bup', '.vimrc')).read() assert contents == 'original' contents = open(join(self.out_dir, '.vimrc')).read() assert contents == '%s contents' % join(self.dir, 'vim', '_vimrc') ", https://github.com/MiCHiLU/fanstatic-tools,c075a2195c8138d103b72ef8445ef08f2a865984,fanstatic/test_core.py::test_convenience_clear_not_initialized,fanstatic/test_core.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import with_statement import os import re import pytest import time import six from fanstatic import (Library, Resource, NeededResources, Group, init_needed, del_needed, get_needed, clear_needed, register_inclusion_renderer, ConfigurationError, bundle_resources, LibraryDependencyCycleError, NEEDED, UnknownResourceExtensionError, UnknownResourceError, set_resource_file_existence_checking) from fanstatic.core import inclusion_renderers from fanstatic.core import thread_local_needed_data from fanstatic.core import ModeResourceDependencyError from fanstatic.codegen import sort_resources_topological def test_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(y1) assert needed.resources() == [x2, x1, y1] def test_resource_file_exists(tmpdir): tmpdir.join('a.js').write('/* hello world */') # by default this is set to False during the tests, but in normal # non-test circumstances this is set to True, and we want to # test things for real here set_resource_file_existence_checking(True) foo = Library('foo', tmpdir.strpath) # since a.js exists, this should work a = Resource(foo, 'a.js') # now we try to create a resource that refers to a file # that doesn't exist with pytest.raises(UnknownResourceError): b = Resource(foo, 'b.js') sub_c = tmpdir.mkdir('sub').join('c.css').write('c') c = Resource(foo, 'sub/c.css') def test_resource_register_with_library(): foo = Library('foo', '') x1 = Resource(foo, 'a.js', minified='a.min.js') assert len(foo.known_resources) == 2 assert x1 in foo.known_resources.values() # Can not use the same relpath for two Resource declarations. with pytest.raises(ConfigurationError): x2 = Resource(foo, 'a.js') def test_group_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') group = Group([x1, x2]) needed = NeededResources() needed.need(group) assert group.resources == set([x1, x2]) more_stuff = Resource(foo, 'more_stuff.js', depends=[group]) assert more_stuff.resources == set([x1, x2, more_stuff]) def test_convenience_need_not_initialized(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) dummy = get_needed() assert not isinstance(dummy, NeededResources) # We return a new dummy instance for every get_needed: dummy2 = get_needed() assert dummy != dummy2 # A dummy never has resources: assert not dummy.has_resources() dummy.need(y1) with pytest.raises(NotImplementedError): dummy.render() def test_convenience_clear_not_initialized(): # This test is put near the top of this module, or at least before # the very first time ``init_needed()`` is called. dummy = get_needed() with pytest.raises(NotImplementedError): dummy.clear() with pytest.raises(NotImplementedError): clear_needed() # Initialize a needed resources object. needed = init_needed() assert get_needed() == needed assert thread_local_needed_data.__dict__[NEEDED] == needed # Clear it. del_needed() # It is gone, really. with pytest.raises(KeyError): thread_local_needed_data.__dict__[NEEDED] # Clearing it again is OK. del_needed() # get_needed still work, dummy-style. dummy2 = get_needed() assert dummy2 != needed with pytest.raises(NotImplementedError): dummy.clear() with pytest.raises(NotImplementedError): clear_needed() def test_convenience_need(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = init_needed() assert get_needed() == needed assert get_needed().resources() == [] y1.need() assert get_needed().resources() == [x2, x1, y1] def test_convenience_group_resource_need(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js') group = Group([x1, x2, y1]) needed = init_needed() assert get_needed() == needed assert get_needed().resources() == [] group.need() assert get_needed().resources() == [x2, x1, y1] def test_depend_on_group(): foo = Library('foo', '') a = Resource(foo, 'a.js') b = Resource(foo, 'b.js') g = Group([a, b]) c = Resource(foo, 'c.js', depends=[g]) g2 = Group([g]) g3 = Group([g, g2]) assert c.depends == set([a, b]) assert g2.depends == set([a, b]) assert g3.depends == set([a, b]) needed = NeededResources() needed.need(c) assert needed.resources() == [a, b, c] def test_redundant_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(y1) needed.need(y1) assert needed.resources() == [x2, x1, y1] needed.need(x1) assert needed.resources() == [x2, x1, y1] needed.need(x2) assert needed.resources() == [x2, x1, y1] def test_redundant_resource_reorder(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(x1) needed.need(x2) needed.need(y1) assert needed.resources() == [x2, x1, y1] def test_redundant_more_complicated(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) needed = NeededResources() needed.need(a3) assert needed.resources() == [a1, a2, a3] needed.need(a4) # a4 is sorted before a3, because it is less deep # in the dependency tree assert needed.resources() == [a1, a2, a4, a3] def test_redundant_more_complicated_reversed(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) needed = NeededResources() needed.need(a4) needed.need(a3) # this will always be consistent, no matter # in what order we need the resources assert needed.resources() == [a1, a2, a4, a3] def test_redundant_more_complicated_depends_on_all(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) a5 = Resource(foo, 'a5.js', depends=[a4, a3]) needed = NeededResources() needed.need(a5) assert needed.resources() == [a1, a2, a4, a3, a5] def test_redundant_more_complicated_depends_on_all_reorder(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) a5 = Resource(foo, 'a5.js', depends=[a4, a3]) needed = NeededResources() needed.need(a3) needed.need(a5) assert needed.resources() == [a1, a2, a4, a3, a5] def test_mode_fully_specified(): foo = Library('foo', '') k_debug = Resource(foo, 'k-debug.js') k = Resource(foo, 'k.js', debug=k_debug) needed = NeededResources() needed.need(k) assert needed.resources() == [k] needed = NeededResources(debug=True) needed.need(k) assert needed.resources() == [k_debug] # If no minified can be found, the 'raw' resource is taken. needed = NeededResources(minified=True) needed.need(k) assert needed.resources() == [k] with pytest.raises(ConfigurationError): NeededResources(debug=True, minified=True) # If only a minified resource is defined, debug returns the raw version. x = Resource(foo, 'x.js', minified='x-min.js') needed = NeededResources(debug=True) needed.need(x) assert needed.resources() == [x] def test_mode_shortcut(): foo = Library('foo', '') k = Resource(foo, 'k.js', debug='k-debug.js') needed = NeededResources() needed.need(k) assert needed.resources() == [k] needed = NeededResources(debug=True) needed.need(k) assert len(needed.resources()) == 1 assert needed.resources()[0].relpath == 'k-debug.js' def test_mode_inherit_dependency_nr(): foo = Library('foo', '') k = Resource(foo, 'k.js') l_debug = Resource(foo, 'l-debug.js') assert l_debug.dependency_nr == 0 l = Resource(foo, 'l.js', debug=l_debug, depends=[k]) assert l_debug.dependency_nr == 1 def test_rollup(): foo = Library('foo', '') b1 = Resource(foo, 'b1.js') b2 = Resource(foo, 'b2.js') giant = Resource(foo, 'giant.js', supersedes=[b1, b2]) needed = NeededResources(rollup=True) needed.need(b1) needed.need(b2) assert needed.resources() == [giant] def test_rollup_cannot(): foo = Library('foo', '') b1 = Resource(foo, 'b1.js') b2 = Resource(foo, 'b2.js') giant = Resource(foo, 'giant.js', supersedes=[b1, b2]) needed = NeededResources(rollup=True) needed.need(b1) assert needed.resources() == [b1] assert giant not in needed.resources() def test_rollup_larger(): foo = Library('foo', '') c1 = Resource(foo, 'c1.css') c2 = Resource", https://github.com/MiCHiLU/fanstatic-tools,c075a2195c8138d103b72ef8445ef08f2a865984,fanstatic/test_core.py::test_convenience_need_not_initialized,fanstatic/test_core.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import with_statement import os import re import pytest import time import six from fanstatic import (Library, Resource, NeededResources, Group, init_needed, del_needed, get_needed, clear_needed, register_inclusion_renderer, ConfigurationError, bundle_resources, LibraryDependencyCycleError, NEEDED, UnknownResourceExtensionError, UnknownResourceError, set_resource_file_existence_checking) from fanstatic.core import inclusion_renderers from fanstatic.core import thread_local_needed_data from fanstatic.core import ModeResourceDependencyError from fanstatic.codegen import sort_resources_topological def test_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(y1) assert needed.resources() == [x2, x1, y1] def test_resource_file_exists(tmpdir): tmpdir.join('a.js').write('/* hello world */') # by default this is set to False during the tests, but in normal # non-test circumstances this is set to True, and we want to # test things for real here set_resource_file_existence_checking(True) foo = Library('foo', tmpdir.strpath) # since a.js exists, this should work a = Resource(foo, 'a.js') # now we try to create a resource that refers to a file # that doesn't exist with pytest.raises(UnknownResourceError): b = Resource(foo, 'b.js') sub_c = tmpdir.mkdir('sub').join('c.css').write('c') c = Resource(foo, 'sub/c.css') def test_resource_register_with_library(): foo = Library('foo', '') x1 = Resource(foo, 'a.js', minified='a.min.js') assert len(foo.known_resources) == 2 assert x1 in foo.known_resources.values() # Can not use the same relpath for two Resource declarations. with pytest.raises(ConfigurationError): x2 = Resource(foo, 'a.js') def test_group_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') group = Group([x1, x2]) needed = NeededResources() needed.need(group) assert group.resources == set([x1, x2]) more_stuff = Resource(foo, 'more_stuff.js', depends=[group]) assert more_stuff.resources == set([x1, x2, more_stuff]) def test_convenience_need_not_initialized(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) dummy = get_needed() assert not isinstance(dummy, NeededResources) # We return a new dummy instance for every get_needed: dummy2 = get_needed() assert dummy != dummy2 # A dummy never has resources: assert not dummy.has_resources() dummy.need(y1) with pytest.raises(NotImplementedError): dummy.render() def test_convenience_clear_not_initialized(): # This test is put near the top of this module, or at least before # the very first time ``init_needed()`` is called. dummy = get_needed() with pytest.raises(NotImplementedError): dummy.clear() with pytest.raises(NotImplementedError): clear_needed() # Initialize a needed resources object. needed = init_needed() assert get_needed() == needed assert thread_local_needed_data.__dict__[NEEDED] == needed # Clear it. del_needed() # It is gone, really. with pytest.raises(KeyError): thread_local_needed_data.__dict__[NEEDED] # Clearing it again is OK. del_needed() # get_needed still work, dummy-style. dummy2 = get_needed() assert dummy2 != needed with pytest.raises(NotImplementedError): dummy.clear() with pytest.raises(NotImplementedError): clear_needed() def test_convenience_need(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = init_needed() assert get_needed() == needed assert get_needed().resources() == [] y1.need() assert get_needed().resources() == [x2, x1, y1] def test_convenience_group_resource_need(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js') group = Group([x1, x2, y1]) needed = init_needed() assert get_needed() == needed assert get_needed().resources() == [] group.need() assert get_needed().resources() == [x2, x1, y1] def test_depend_on_group(): foo = Library('foo', '') a = Resource(foo, 'a.js') b = Resource(foo, 'b.js') g = Group([a, b]) c = Resource(foo, 'c.js', depends=[g]) g2 = Group([g]) g3 = Group([g, g2]) assert c.depends == set([a, b]) assert g2.depends == set([a, b]) assert g3.depends == set([a, b]) needed = NeededResources() needed.need(c) assert needed.resources() == [a, b, c] def test_redundant_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(y1) needed.need(y1) assert needed.resources() == [x2, x1, y1] needed.need(x1) assert needed.resources() == [x2, x1, y1] needed.need(x2) assert needed.resources() == [x2, x1, y1] def test_redundant_resource_reorder(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(x1) needed.need(x2) needed.need(y1) assert needed.resources() == [x2, x1, y1] def test_redundant_more_complicated(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) needed = NeededResources() needed.need(a3) assert needed.resources() == [a1, a2, a3] needed.need(a4) # a4 is sorted before a3, because it is less deep # in the dependency tree assert needed.resources() == [a1, a2, a4, a3] def test_redundant_more_complicated_reversed(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) needed = NeededResources() needed.need(a4) needed.need(a3) # this will always be consistent, no matter # in what order we need the resources assert needed.resources() == [a1, a2, a4, a3] def test_redundant_more_complicated_depends_on_all(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) a5 = Resource(foo, 'a5.js', depends=[a4, a3]) needed = NeededResources() needed.need(a5) assert needed.resources() == [a1, a2, a4, a3, a5] def test_redundant_more_complicated_depends_on_all_reorder(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) a5 = Resource(foo, 'a5.js', depends=[a4, a3]) needed = NeededResources() needed.need(a3) needed.need(a5) assert needed.resources() == [a1, a2, a4, a3, a5] def test_mode_fully_specified(): foo = Library('foo', '') k_debug = Resource(foo, 'k-debug.js') k = Resource(foo, 'k.js', debug=k_debug) needed = NeededResources() needed.need(k) assert needed.resources() == [k] needed = NeededResources(debug=True) needed.need(k) assert needed.resources() == [k_debug] # If no minified can be found, the 'raw' resource is taken. needed = NeededResources(minified=True) needed.need(k) assert needed.resources() == [k] with pytest.raises(ConfigurationError): NeededResources(debug=True, minified=True) # If only a minified resource is defined, debug returns the raw version. x = Resource(foo, 'x.js', minified='x-min.js') needed = NeededResources(debug=True) needed.need(x) assert needed.resources() == [x] def test_mode_shortcut(): foo = Library('foo', '') k = Resource(foo, 'k.js', debug='k-debug.js') needed = NeededResources() needed.need(k) assert needed.resources() == [k] needed = NeededResources(debug=True) needed.need(k) assert len(needed.resources()) == 1 assert needed.resources()[0].relpath == 'k-debug.js' def test_mode_inherit_dependency_nr(): foo = Library('foo', '') k = Resource(foo, 'k.js') l_debug = Resource(foo, 'l-debug.js') assert l_debug.dependency_nr == 0 l = Resource(foo, 'l.js', debug=l_debug, depends=[k]) assert l_debug.dependency_nr == 1 def test_rollup(): foo = Library('foo', '') b1 = Resource(foo, 'b1.js') b2 = Resource(foo, 'b2.js') giant = Resource(foo, 'giant.js', supersedes=[b1, b2]) needed = NeededResources(rollup=True) needed.need(b1) needed.need(b2) assert needed.resources() == [giant] def test_rollup_cannot(): foo = Library('foo', '') b1 = Resource(foo, 'b1.js') b2 = Resource(foo, 'b2.js') giant = Resource(foo, 'giant.js', supersedes=[b1, b2]) needed = NeededResources(rollup=True) needed.need(b1) assert needed.resources() == [b1] assert giant not in needed.resources() def test_rollup_larger(): foo = Library('foo', '') c1 = Resource(foo, 'c1.css') c2 = Resource", https://github.com/MiCHiLU/fanstatic-tools,c075a2195c8138d103b72ef8445ef08f2a865984,fanstatic/test_core.py::test_inclusion_renderers,fanstatic/test_core.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import with_statement import os import re import pytest import time import six from fanstatic import (Library, Resource, NeededResources, Group, init_needed, del_needed, get_needed, clear_needed, register_inclusion_renderer, ConfigurationError, bundle_resources, LibraryDependencyCycleError, NEEDED, UnknownResourceExtensionError, UnknownResourceError, set_resource_file_existence_checking) from fanstatic.core import inclusion_renderers from fanstatic.core import thread_local_needed_data from fanstatic.core import ModeResourceDependencyError from fanstatic.codegen import sort_resources_topological def test_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(y1) assert needed.resources() == [x2, x1, y1] def test_resource_file_exists(tmpdir): tmpdir.join('a.js').write('/* hello world */') # by default this is set to False during the tests, but in normal # non-test circumstances this is set to True, and we want to # test things for real here set_resource_file_existence_checking(True) foo = Library('foo', tmpdir.strpath) # since a.js exists, this should work a = Resource(foo, 'a.js') # now we try to create a resource that refers to a file # that doesn't exist with pytest.raises(UnknownResourceError): b = Resource(foo, 'b.js') sub_c = tmpdir.mkdir('sub').join('c.css').write('c') c = Resource(foo, 'sub/c.css') def test_resource_register_with_library(): foo = Library('foo', '') x1 = Resource(foo, 'a.js', minified='a.min.js') assert len(foo.known_resources) == 2 assert x1 in foo.known_resources.values() # Can not use the same relpath for two Resource declarations. with pytest.raises(ConfigurationError): x2 = Resource(foo, 'a.js') def test_group_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') group = Group([x1, x2]) needed = NeededResources() needed.need(group) assert group.resources == set([x1, x2]) more_stuff = Resource(foo, 'more_stuff.js', depends=[group]) assert more_stuff.resources == set([x1, x2, more_stuff]) def test_convenience_need_not_initialized(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) dummy = get_needed() assert not isinstance(dummy, NeededResources) # We return a new dummy instance for every get_needed: dummy2 = get_needed() assert dummy != dummy2 # A dummy never has resources: assert not dummy.has_resources() dummy.need(y1) with pytest.raises(NotImplementedError): dummy.render() def test_convenience_clear_not_initialized(): # This test is put near the top of this module, or at least before # the very first time ``init_needed()`` is called. dummy = get_needed() with pytest.raises(NotImplementedError): dummy.clear() with pytest.raises(NotImplementedError): clear_needed() # Initialize a needed resources object. needed = init_needed() assert get_needed() == needed assert thread_local_needed_data.__dict__[NEEDED] == needed # Clear it. del_needed() # It is gone, really. with pytest.raises(KeyError): thread_local_needed_data.__dict__[NEEDED] # Clearing it again is OK. del_needed() # get_needed still work, dummy-style. dummy2 = get_needed() assert dummy2 != needed with pytest.raises(NotImplementedError): dummy.clear() with pytest.raises(NotImplementedError): clear_needed() def test_convenience_need(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = init_needed() assert get_needed() == needed assert get_needed().resources() == [] y1.need() assert get_needed().resources() == [x2, x1, y1] def test_convenience_group_resource_need(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js') group = Group([x1, x2, y1]) needed = init_needed() assert get_needed() == needed assert get_needed().resources() == [] group.need() assert get_needed().resources() == [x2, x1, y1] def test_depend_on_group(): foo = Library('foo', '') a = Resource(foo, 'a.js') b = Resource(foo, 'b.js') g = Group([a, b]) c = Resource(foo, 'c.js', depends=[g]) g2 = Group([g]) g3 = Group([g, g2]) assert c.depends == set([a, b]) assert g2.depends == set([a, b]) assert g3.depends == set([a, b]) needed = NeededResources() needed.need(c) assert needed.resources() == [a, b, c] def test_redundant_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(y1) needed.need(y1) assert needed.resources() == [x2, x1, y1] needed.need(x1) assert needed.resources() == [x2, x1, y1] needed.need(x2) assert needed.resources() == [x2, x1, y1] def test_redundant_resource_reorder(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(x1) needed.need(x2) needed.need(y1) assert needed.resources() == [x2, x1, y1] def test_redundant_more_complicated(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) needed = NeededResources() needed.need(a3) assert needed.resources() == [a1, a2, a3] needed.need(a4) # a4 is sorted before a3, because it is less deep # in the dependency tree assert needed.resources() == [a1, a2, a4, a3] def test_redundant_more_complicated_reversed(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) needed = NeededResources() needed.need(a4) needed.need(a3) # this will always be consistent, no matter # in what order we need the resources assert needed.resources() == [a1, a2, a4, a3] def test_redundant_more_complicated_depends_on_all(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) a5 = Resource(foo, 'a5.js', depends=[a4, a3]) needed = NeededResources() needed.need(a5) assert needed.resources() == [a1, a2, a4, a3, a5] def test_redundant_more_complicated_depends_on_all_reorder(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) a5 = Resource(foo, 'a5.js', depends=[a4, a3]) needed = NeededResources() needed.need(a3) needed.need(a5) assert needed.resources() == [a1, a2, a4, a3, a5] def test_mode_fully_specified(): foo = Library('foo', '') k_debug = Resource(foo, 'k-debug.js') k = Resource(foo, 'k.js', debug=k_debug) needed = NeededResources() needed.need(k) assert needed.resources() == [k] needed = NeededResources(debug=True) needed.need(k) assert needed.resources() == [k_debug] # If no minified can be found, the 'raw' resource is taken. needed = NeededResources(minified=True) needed.need(k) assert needed.resources() == [k] with pytest.raises(ConfigurationError): NeededResources(debug=True, minified=True) # If only a minified resource is defined, debug returns the raw version. x = Resource(foo, 'x.js', minified='x-min.js') needed = NeededResources(debug=True) needed.need(x) assert needed.resources() == [x] def test_mode_shortcut(): foo = Library('foo', '') k = Resource(foo, 'k.js', debug='k-debug.js') needed = NeededResources() needed.need(k) assert needed.resources() == [k] needed = NeededResources(debug=True) needed.need(k) assert len(needed.resources()) == 1 assert needed.resources()[0].relpath == 'k-debug.js' def test_mode_inherit_dependency_nr(): foo = Library('foo', '') k = Resource(foo, 'k.js') l_debug = Resource(foo, 'l-debug.js') assert l_debug.dependency_nr == 0 l = Resource(foo, 'l.js', debug=l_debug, depends=[k]) assert l_debug.dependency_nr == 1 def test_rollup(): foo = Library('foo', '') b1 = Resource(foo, 'b1.js') b2 = Resource(foo, 'b2.js') giant = Resource(foo, 'giant.js', supersedes=[b1, b2]) needed = NeededResources(rollup=True) needed.need(b1) needed.need(b2) assert needed.resources() == [giant] def test_rollup_cannot(): foo = Library('foo', '') b1 = Resource(foo, 'b1.js') b2 = Resource(foo, 'b2.js') giant = Resource(foo, 'giant.js', supersedes=[b1, b2]) needed = NeededResources(rollup=True) needed.need(b1) assert needed.resources() == [b1] assert giant not in needed.resources() def test_rollup_larger(): foo = Library('foo', '') c1 = Resource(foo, 'c1.css') c2 = Resource", https://github.com/MiCHiLU/fanstatic-tools,c075a2195c8138d103b72ef8445ef08f2a865984,fanstatic/test_core.py::test_register_inclusion_renderer,fanstatic/test_core.py,NIO,flaky,Opened,https://github.com/michilu/fanstatic-tools/pull/1,classify;root_cause,"from __future__ import with_statement import os import re import pytest import time import six from fanstatic import (Library, Resource, NeededResources, Group, init_needed, del_needed, get_needed, clear_needed, register_inclusion_renderer, ConfigurationError, bundle_resources, LibraryDependencyCycleError, NEEDED, UnknownResourceExtensionError, UnknownResourceError, set_resource_file_existence_checking) from fanstatic.core import inclusion_renderers from fanstatic.core import thread_local_needed_data from fanstatic.core import ModeResourceDependencyError from fanstatic.codegen import sort_resources_topological def test_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(y1) assert needed.resources() == [x2, x1, y1] def test_resource_file_exists(tmpdir): tmpdir.join('a.js').write('/* hello world */') # by default this is set to False during the tests, but in normal # non-test circumstances this is set to True, and we want to # test things for real here set_resource_file_existence_checking(True) foo = Library('foo', tmpdir.strpath) # since a.js exists, this should work a = Resource(foo, 'a.js') # now we try to create a resource that refers to a file # that doesn't exist with pytest.raises(UnknownResourceError): b = Resource(foo, 'b.js') sub_c = tmpdir.mkdir('sub').join('c.css').write('c') c = Resource(foo, 'sub/c.css') def test_resource_register_with_library(): foo = Library('foo', '') x1 = Resource(foo, 'a.js', minified='a.min.js') assert len(foo.known_resources) == 2 assert x1 in foo.known_resources.values() # Can not use the same relpath for two Resource declarations. with pytest.raises(ConfigurationError): x2 = Resource(foo, 'a.js') def test_group_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') group = Group([x1, x2]) needed = NeededResources() needed.need(group) assert group.resources == set([x1, x2]) more_stuff = Resource(foo, 'more_stuff.js', depends=[group]) assert more_stuff.resources == set([x1, x2, more_stuff]) def test_convenience_need_not_initialized(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) dummy = get_needed() assert not isinstance(dummy, NeededResources) # We return a new dummy instance for every get_needed: dummy2 = get_needed() assert dummy != dummy2 # A dummy never has resources: assert not dummy.has_resources() dummy.need(y1) with pytest.raises(NotImplementedError): dummy.render() def test_convenience_clear_not_initialized(): # This test is put near the top of this module, or at least before # the very first time ``init_needed()`` is called. dummy = get_needed() with pytest.raises(NotImplementedError): dummy.clear() with pytest.raises(NotImplementedError): clear_needed() # Initialize a needed resources object. needed = init_needed() assert get_needed() == needed assert thread_local_needed_data.__dict__[NEEDED] == needed # Clear it. del_needed() # It is gone, really. with pytest.raises(KeyError): thread_local_needed_data.__dict__[NEEDED] # Clearing it again is OK. del_needed() # get_needed still work, dummy-style. dummy2 = get_needed() assert dummy2 != needed with pytest.raises(NotImplementedError): dummy.clear() with pytest.raises(NotImplementedError): clear_needed() def test_convenience_need(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = init_needed() assert get_needed() == needed assert get_needed().resources() == [] y1.need() assert get_needed().resources() == [x2, x1, y1] def test_convenience_group_resource_need(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js') group = Group([x1, x2, y1]) needed = init_needed() assert get_needed() == needed assert get_needed().resources() == [] group.need() assert get_needed().resources() == [x2, x1, y1] def test_depend_on_group(): foo = Library('foo', '') a = Resource(foo, 'a.js') b = Resource(foo, 'b.js') g = Group([a, b]) c = Resource(foo, 'c.js', depends=[g]) g2 = Group([g]) g3 = Group([g, g2]) assert c.depends == set([a, b]) assert g2.depends == set([a, b]) assert g3.depends == set([a, b]) needed = NeededResources() needed.need(c) assert needed.resources() == [a, b, c] def test_redundant_resource(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(y1) needed.need(y1) assert needed.resources() == [x2, x1, y1] needed.need(x1) assert needed.resources() == [x2, x1, y1] needed.need(x2) assert needed.resources() == [x2, x1, y1] def test_redundant_resource_reorder(): foo = Library('foo', '') x1 = Resource(foo, 'a.js') x2 = Resource(foo, 'b.css') y1 = Resource(foo, 'c.js', depends=[x1, x2]) needed = NeededResources() needed.need(x1) needed.need(x2) needed.need(y1) assert needed.resources() == [x2, x1, y1] def test_redundant_more_complicated(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) needed = NeededResources() needed.need(a3) assert needed.resources() == [a1, a2, a3] needed.need(a4) # a4 is sorted before a3, because it is less deep # in the dependency tree assert needed.resources() == [a1, a2, a4, a3] def test_redundant_more_complicated_reversed(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) needed = NeededResources() needed.need(a4) needed.need(a3) # this will always be consistent, no matter # in what order we need the resources assert needed.resources() == [a1, a2, a4, a3] def test_redundant_more_complicated_depends_on_all(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) a5 = Resource(foo, 'a5.js', depends=[a4, a3]) needed = NeededResources() needed.need(a5) assert needed.resources() == [a1, a2, a4, a3, a5] def test_redundant_more_complicated_depends_on_all_reorder(): foo = Library('foo', '') a1 = Resource(foo, 'a1.js') a2 = Resource(foo, 'a2.js', depends=[a1]) a3 = Resource(foo, 'a3.js', depends=[a2]) a4 = Resource(foo, 'a4.js', depends=[a1]) a5 = Resource(foo, 'a5.js', depends=[a4, a3]) needed = NeededResources() needed.need(a3) needed.need(a5) assert needed.resources() == [a1, a2, a4, a3, a5] def test_mode_fully_specified(): foo = Library('foo', '') k_debug = Resource(foo, 'k-debug.js') k = Resource(foo, 'k.js', debug=k_debug) needed = NeededResources() needed.need(k) assert needed.resources() == [k] needed = NeededResources(debug=True) needed.need(k) assert needed.resources() == [k_debug] # If no minified can be found, the 'raw' resource is taken. needed = NeededResources(minified=True) needed.need(k) assert needed.resources() == [k] with pytest.raises(ConfigurationError): NeededResources(debug=True, minified=True) # If only a minified resource is defined, debug returns the raw version. x = Resource(foo, 'x.js', minified='x-min.js') needed = NeededResources(debug=True) needed.need(x) assert needed.resources() == [x] def test_mode_shortcut(): foo = Library('foo', '') k = Resource(foo, 'k.js', debug='k-debug.js') needed = NeededResources() needed.need(k) assert needed.resources() == [k] needed = NeededResources(debug=True) needed.need(k) assert len(needed.resources()) == 1 assert needed.resources()[0].relpath == 'k-debug.js' def test_mode_inherit_dependency_nr(): foo = Library('foo', '') k = Resource(foo, 'k.js') l_debug = Resource(foo, 'l-debug.js') assert l_debug.dependency_nr == 0 l = Resource(foo, 'l.js', debug=l_debug, depends=[k]) assert l_debug.dependency_nr == 1 def test_rollup(): foo = Library('foo', '') b1 = Resource(foo, 'b1.js') b2 = Resource(foo, 'b2.js') giant = Resource(foo, 'giant.js', supersedes=[b1, b2]) needed = NeededResources(rollup=True) needed.need(b1) needed.need(b2) assert needed.resources() == [giant] def test_rollup_cannot(): foo = Library('foo', '') b1 = Resource(foo, 'b1.js') b2 = Resource(foo, 'b2.js') giant = Resource(foo, 'giant.js', supersedes=[b1, b2]) needed = NeededResources(rollup=True) needed.need(b1) assert needed.resources() == [b1] assert giant not in needed.resources() def test_rollup_larger(): foo = Library('foo', '') c1 = Resource(foo, 'c1.css') c2 = Resource", https://github.com/microsoft/knack,dc7b7d857ce3c11b2a3155b81f2470b3b579e7c9,tests/test_cli_scenarios.py::TestCLIScenarios::test_case_insensitive_command_path,tests/test_cli_scenarios.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- import os from collections import OrderedDict import unittest try: import mock except ImportError: from unittest import mock import mock from six import StringIO from knack import CLI from knack.commands import CLICommand, CLICommandsLoader from knack.invocation import CommandInvoker from tests.util import MockContext class TestCLIScenarios(unittest.TestCase): def setUp(self): self.mock_ctx = MockContext() def invoke_with_command_table(self, command, command_table): self.mock_ctx.invocation = CommandInvoker(cli_ctx=self.mock_ctx) self.mock_ctx.invocation.commands_loader.command_table = command_table self.mock_ctx.invocation.execute(command.split()) def test_list_value_parameter(self): handler_args = {} def handler(args): handler_args.update(args) command = CLICommand(self.mock_ctx, 'test command', handler) command.add_argument('hello', '--hello', nargs='+') command.add_argument('something', '--something') cmd_table = {'test command': command} self.invoke_with_command_table('test command --hello world sir --something else', cmd_table) self.assertEqual(handler_args['something'], 'else') self.assertEqual(handler_args['hello'][0], 'world') self.assertEqual(handler_args['hello'][1], 'sir') def test_case_insensitive_command_path(self): def handler(_): return 'PASSED' command = CLICommand(self.mock_ctx, 'test command', handler) command.add_argument('var', '--var', '-v') cmd_table = {'test command': command} def _test(cmd_line): ci = CommandInvoker(cli_ctx=self.mock_ctx) self.mock_ctx.invocation = ci self.mock_ctx.invocation.commands_loader.command_table = cmd_table return self.mock_ctx.invocation.execute(cmd_line.split()) # case insensitive command paths result = _test('TEST command --var blah') self.assertEqual(result.result, 'PASSED') result = _test('test COMMAND --var blah') self.assertEqual(result.result, 'PASSED') result = _test('test command -v blah') self.assertEqual(result.result, 'PASSED') # verify that long and short options remain case sensitive with mock.patch('sys.stderr', new_callable=StringIO): with self.assertRaises(SystemExit): _test('test command --vAR blah') with self.assertRaises(SystemExit): _test('test command -V blah') def test_cli_exapp1(self): def a_test_command_handler(_): return [{'a': 1, 'b': 1234}, {'a': 3, 'b': 4}] class MyCommandsLoader(CLICommandsLoader): def load_command_table(self, args): self.command_table['abc xyz'] = CLICommand(self.cli_ctx, 'abc xyz', a_test_command_handler) self.command_table['abc list'] = CLICommand(self.cli_ctx, 'abc list', a_test_command_handler) return OrderedDict(self.command_table) mycli = CLI(cli_name='exapp1', config_dir=os.path.expanduser(os.path.join('~', '.exapp1')), commands_loader_cls=MyCommandsLoader) expected_output = """"""[ { ""a"": 1, ""b"": 1234 }, { ""a"": 3, ""b"": 4 } ] """""" mock_stdout = StringIO() exit_code = mycli.invoke(['abc', 'xyz'], out_file=mock_stdout) self.assertEqual(expected_output, mock_stdout.getvalue()) self.assertEqual(0, exit_code) mock_stdout = StringIO() mycli.invoke(['abc', 'list'], out_file=mock_stdout) self.assertEqual(expected_output, mock_stdout.getvalue()) self.assertEqual(0, exit_code) expected_output = """"""{ ""a"": 1, ""b"": 1234 } """""" mock_stdout = StringIO() mycli.invoke(['abc', 'list', '--query', '[0]'], out_file=mock_stdout) self.assertEqual(expected_output, mock_stdout.getvalue()) self.assertEqual(0, exit_code) expected_output = ""1\n"" mock_stdout = StringIO() mycli.invoke(['abc', 'list', '--query', '[0].a'], out_file=mock_stdout) self.assertEqual(expected_output, mock_stdout.getvalue()) self.assertEqual(0, exit_code) if __name__ == '__main__': unittest.main() ", https://github.com/microsoft/knack,dc7b7d857ce3c11b2a3155b81f2470b3b579e7c9,tests/test_command_with_configured_defaults.py::TestCommandWithConfiguredDefaults::test_no_configured_default_on_required_arg,tests/test_command_with_configured_defaults.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- from __future__ import print_function import os import logging import sys import unittest try: import mock except ImportError: from unittest import mock from knack.arguments import ArgumentsContext from knack.commands import CLICommandsLoader, CommandGroup from tests.util import DummyCLI, redirect_io # a dummy callback for arg-parse def load_params(_): pass def list_foo(my_param): print(str(my_param), end='') class TestCommandWithConfiguredDefaults(unittest.TestCase): @classmethod def setUpClass(cls): # Ensure initialization has occurred correctly logging.basicConfig(level=logging.DEBUG) @classmethod def tearDownClass(cls): logging.shutdown() def _set_up_command_table(self, required): class TestCommandsLoader(CLICommandsLoader): def load_command_table(self, args): super(TestCommandsLoader, self).load_command_table(args) with CommandGroup(self, 'foo', '{}#{{}}'.format(__name__)) as g: g.command('list', 'list_foo') return self.command_table def load_arguments(self, command): with ArgumentsContext(self, 'foo') as c: c.argument('my_param', options_list='--my-param', configured_default='param', required=required) super(TestCommandsLoader, self).load_arguments(command) self.cli_ctx = DummyCLI(commands_loader_cls=TestCommandsLoader) @mock.patch.dict(os.environ, {'CLI_DEFAULTS_PARAM': 'myVal'}) @redirect_io def test_apply_configured_defaults_on_required_arg(self): self._set_up_command_table(required=True) self.cli_ctx.invoke('foo list'.split()) actual = self.io.getvalue() expected = 'myVal' self.assertEqual(expected, actual) @redirect_io def test_no_configured_default_on_required_arg(self): self._set_up_command_table(required=True) with self.assertRaises(SystemExit): self.cli_ctx.invoke('foo list'.split()) actual = self.io.getvalue() expected = 'required: --my-param' if sys.version_info[0] == 2: expected = 'argument --my-param is required' self.assertEqual(expected in actual, True) @mock.patch.dict(os.environ, {'CLI_DEFAULTS_PARAM': 'myVal'}) @redirect_io def test_apply_configured_defaults_on_optional_arg(self): self._set_up_command_table(required=False) self.cli_ctx.invoke('foo list'.split()) actual = self.io.getvalue() expected = 'myVal' self.assertEqual(expected, actual) if __name__ == '__main__': unittest.main() ", https://github.com/microsoft/knack,dc7b7d857ce3c11b2a3155b81f2470b3b579e7c9,tests/test_deprecation.py::TestArgumentDeprecation::test_deprecate_arguments_execute_expired,tests/test_deprecation.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- from __future__ import unicode_literals import unittest try: import mock except ImportError: from unittest import mock from threading import Lock from knack.arguments import ArgumentsContext from knack.commands import CLICommand, CLICommandsLoader, CommandGroup from tests.util import DummyCLI, redirect_io, disable_color def example_handler(arg1, arg2=None, arg3=None): """""" Short summary here. Long summary here. Still long summary. """""" pass def example_arg_handler(arg1, opt1, arg2=None, opt2=None, arg3=None, opt3=None, arg4=None, opt4=None, arg5=None, opt5=None): pass # pylint: disable=line-too-long class TestCommandDeprecation(unittest.TestCase): def setUp(self): from knack.help_files import helps class DeprecationTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): super(DeprecationTestCommandLoader, self).load_command_table(args) with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g: g.command('cmd1', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd1')) g.command('cmd2', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd2', hide='1.0.0')) g.command('cmd3', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd3', hide='0.1.0')) g.command('cmd4', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd4', expiration='1.0.0')) g.command('cmd5', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd5', expiration='0.1.0')) with CommandGroup(self, 'grp1', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-grp1')) as g: g.command('cmd1', 'example_handler') return self.command_table def load_arguments(self, command): with ArgumentsContext(self, '') as c: c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) super(DeprecationTestCommandLoader, self).load_arguments(command) helps['grp1'] = """""" type: group short-summary: A group. """""" self.cli_ctx = DummyCLI(commands_loader_cls=DeprecationTestCommandLoader) @redirect_io def test_deprecate_command_group_help(self): """""" Ensure deprecated commands appear (or don't appear) correctly in group help view. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('-h'.split()) actual = self.io.getvalue() expected = u"""""" Group {} Subgroups: grp1 [Deprecated] : A group. Commands: cmd1 [Deprecated] : Short summary here. cmd2 [Deprecated] : Short summary here. cmd4 [Deprecated] : Short summary here. """""".format(self.cli_ctx.name) self.assertEqual(expected, actual) @redirect_io def test_deprecate_command_help_hidden(self): """""" Ensure hidden deprecated command can be used. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('cmd3 -h'.split()) actual = self.io.getvalue() expected = """""" Command {} cmd3 : Short summary here. Long summary here. Still long summary. This command has been deprecated and will be removed in a future release. Use 'alt- cmd3' instead. Arguments -b [Required] : Allowed values: a, b, c. --arg -a : Allowed values: 1, 2, 3. --arg3 """""".format(self.cli_ctx.name) self.assertIn(expected, actual) @redirect_io def test_deprecate_command_plain_execute(self): """""" Ensure general warning displayed when running deprecated command. """""" self.cli_ctx.invoke('cmd1 -b b'.split()) actual = self.io.getvalue() expected = ""This command has been deprecated and will be removed in a future release. Use 'alt-cmd1' instead."" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_hidden_execute(self): """""" Ensure general warning displayed when running hidden deprecated command. """""" self.cli_ctx.invoke('cmd3 -b b'.split()) actual = self.io.getvalue() expected = ""This command has been deprecated and will be removed in a future release. Use 'alt-cmd3' instead."" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_expiring_execute(self): """""" Ensure specific warning displayed when running expiring deprecated command. """""" self.cli_ctx.invoke('cmd4 -b b'.split()) actual = self.io.getvalue() expected = ""This command has been deprecated and will be removed in version '1.0.0'. Use 'alt-cmd4' instead."" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_expiring_execute_no_color(self): """""" Ensure warning is displayed without color. """""" self.cli_ctx.enable_color = False self.cli_ctx.invoke('cmd4 -b b'.split()) actual = self.io.getvalue() expected = ""WARNING: This command has been deprecated and will be removed in version '1.0.0'"" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_expired_execute(self): """""" Ensure expired command cannot be reached. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('cmd5 -h'.split()) actual = self.io.getvalue() expected = """"""cli: 'cmd5' is not in the 'cli' command group."""""" self.assertIn(expected, actual) @redirect_io @disable_color def test_deprecate_command_expired_execute_no_color(self): """""" Ensure error is displayed without color. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('cmd5 -h'.split()) actual = self.io.getvalue() expected = """"""ERROR: cli: 'cmd5' is not in the 'cli' command group."""""" self.assertIn(expected, actual) class TestCommandGroupDeprecation(unittest.TestCase): def setUp(self): from knack.help_files import helps class DeprecationTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): super(DeprecationTestCommandLoader, self).load_command_table(args) with CommandGroup(self, 'group1', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group1')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group2', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group2', hide='1.0.0')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group3', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group3', hide='0.1.0')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group4', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group4', expiration='1.0.0')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group5', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group5', expiration='0.1.0')) as g: g.command('cmd1', 'example_handler') return self.command_table def load_arguments(self, command): with ArgumentsContext(self, '') as c: c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) super(DeprecationTestCommandLoader, self).load_arguments(command) helps['group1'] = """""" type: group short-summary: A group. """""" self.cli_ctx = DummyCLI(commands_loader_cls=DeprecationTestCommandLoader) @redirect_io def test_deprecate_command_group_help_plain(self): """""" Ensure help warnings appear for deprecated command group help. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('group1 -h'.split()) actual = self.io.getvalue() expected = """""" Group cli group1 : A group. This command group has been deprecated and will be removed in a future release. Use 'alt-group1' instead. Commands: cmd1 : Short summary here. """""".format(self.cli_ctx.name) self.assertEqual(expected, actual) @redirect_io def test_deprecate_command_group_help_hidden(self): """""" Ensure hidden deprecated command can be used. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('group3 -h'.split()) actual = self.io.getvalue() expected = """""" Group {} group3 This command group has been deprecated and will be removed in a future release. Use 'alt-group3' instead. Commands: cmd1 : Short summary here. """""".format(self.cli_ctx.name) self.assertIn(expected, actual) @redirect_io def test_deprecate_command_group_help_expiring(self): """""" Ensure specific warning displayed when running expiring deprecated command. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('group4 -h'.split()) actual = self.io.getvalue() expected = """""" Group {} group4 This command group has been deprecated and will be removed in version '1.0.0'. Use 'alt-group4' ", https://github.com/microsoft/knack,dc7b7d857ce3c11b2a3155b81f2470b3b579e7c9,tests/test_deprecation.py::TestArgumentDeprecation::test_deprecate_options_execute_expired,tests/test_deprecation.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- from __future__ import unicode_literals import unittest try: import mock except ImportError: from unittest import mock from threading import Lock from knack.arguments import ArgumentsContext from knack.commands import CLICommand, CLICommandsLoader, CommandGroup from tests.util import DummyCLI, redirect_io, disable_color def example_handler(arg1, arg2=None, arg3=None): """""" Short summary here. Long summary here. Still long summary. """""" pass def example_arg_handler(arg1, opt1, arg2=None, opt2=None, arg3=None, opt3=None, arg4=None, opt4=None, arg5=None, opt5=None): pass # pylint: disable=line-too-long class TestCommandDeprecation(unittest.TestCase): def setUp(self): from knack.help_files import helps class DeprecationTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): super(DeprecationTestCommandLoader, self).load_command_table(args) with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g: g.command('cmd1', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd1')) g.command('cmd2', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd2', hide='1.0.0')) g.command('cmd3', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd3', hide='0.1.0')) g.command('cmd4', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd4', expiration='1.0.0')) g.command('cmd5', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd5', expiration='0.1.0')) with CommandGroup(self, 'grp1', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-grp1')) as g: g.command('cmd1', 'example_handler') return self.command_table def load_arguments(self, command): with ArgumentsContext(self, '') as c: c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) super(DeprecationTestCommandLoader, self).load_arguments(command) helps['grp1'] = """""" type: group short-summary: A group. """""" self.cli_ctx = DummyCLI(commands_loader_cls=DeprecationTestCommandLoader) @redirect_io def test_deprecate_command_group_help(self): """""" Ensure deprecated commands appear (or don't appear) correctly in group help view. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('-h'.split()) actual = self.io.getvalue() expected = u"""""" Group {} Subgroups: grp1 [Deprecated] : A group. Commands: cmd1 [Deprecated] : Short summary here. cmd2 [Deprecated] : Short summary here. cmd4 [Deprecated] : Short summary here. """""".format(self.cli_ctx.name) self.assertEqual(expected, actual) @redirect_io def test_deprecate_command_help_hidden(self): """""" Ensure hidden deprecated command can be used. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('cmd3 -h'.split()) actual = self.io.getvalue() expected = """""" Command {} cmd3 : Short summary here. Long summary here. Still long summary. This command has been deprecated and will be removed in a future release. Use 'alt- cmd3' instead. Arguments -b [Required] : Allowed values: a, b, c. --arg -a : Allowed values: 1, 2, 3. --arg3 """""".format(self.cli_ctx.name) self.assertIn(expected, actual) @redirect_io def test_deprecate_command_plain_execute(self): """""" Ensure general warning displayed when running deprecated command. """""" self.cli_ctx.invoke('cmd1 -b b'.split()) actual = self.io.getvalue() expected = ""This command has been deprecated and will be removed in a future release. Use 'alt-cmd1' instead."" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_hidden_execute(self): """""" Ensure general warning displayed when running hidden deprecated command. """""" self.cli_ctx.invoke('cmd3 -b b'.split()) actual = self.io.getvalue() expected = ""This command has been deprecated and will be removed in a future release. Use 'alt-cmd3' instead."" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_expiring_execute(self): """""" Ensure specific warning displayed when running expiring deprecated command. """""" self.cli_ctx.invoke('cmd4 -b b'.split()) actual = self.io.getvalue() expected = ""This command has been deprecated and will be removed in version '1.0.0'. Use 'alt-cmd4' instead."" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_expiring_execute_no_color(self): """""" Ensure warning is displayed without color. """""" self.cli_ctx.enable_color = False self.cli_ctx.invoke('cmd4 -b b'.split()) actual = self.io.getvalue() expected = ""WARNING: This command has been deprecated and will be removed in version '1.0.0'"" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_expired_execute(self): """""" Ensure expired command cannot be reached. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('cmd5 -h'.split()) actual = self.io.getvalue() expected = """"""cli: 'cmd5' is not in the 'cli' command group."""""" self.assertIn(expected, actual) @redirect_io @disable_color def test_deprecate_command_expired_execute_no_color(self): """""" Ensure error is displayed without color. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('cmd5 -h'.split()) actual = self.io.getvalue() expected = """"""ERROR: cli: 'cmd5' is not in the 'cli' command group."""""" self.assertIn(expected, actual) class TestCommandGroupDeprecation(unittest.TestCase): def setUp(self): from knack.help_files import helps class DeprecationTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): super(DeprecationTestCommandLoader, self).load_command_table(args) with CommandGroup(self, 'group1', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group1')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group2', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group2', hide='1.0.0')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group3', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group3', hide='0.1.0')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group4', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group4', expiration='1.0.0')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group5', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group5', expiration='0.1.0')) as g: g.command('cmd1', 'example_handler') return self.command_table def load_arguments(self, command): with ArgumentsContext(self, '') as c: c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) super(DeprecationTestCommandLoader, self).load_arguments(command) helps['group1'] = """""" type: group short-summary: A group. """""" self.cli_ctx = DummyCLI(commands_loader_cls=DeprecationTestCommandLoader) @redirect_io def test_deprecate_command_group_help_plain(self): """""" Ensure help warnings appear for deprecated command group help. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('group1 -h'.split()) actual = self.io.getvalue() expected = """""" Group cli group1 : A group. This command group has been deprecated and will be removed in a future release. Use 'alt-group1' instead. Commands: cmd1 : Short summary here. """""".format(self.cli_ctx.name) self.assertEqual(expected, actual) @redirect_io def test_deprecate_command_group_help_hidden(self): """""" Ensure hidden deprecated command can be used. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('group3 -h'.split()) actual = self.io.getvalue() expected = """""" Group {} group3 This command group has been deprecated and will be removed in a future release. Use 'alt-group3' instead. Commands: cmd1 : Short summary here. """""".format(self.cli_ctx.name) self.assertIn(expected, actual) @redirect_io def test_deprecate_command_group_help_expiring(self): """""" Ensure specific warning displayed when running expiring deprecated command. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('group4 -h'.split()) actual = self.io.getvalue() expected = """""" Group {} group4 This command group has been deprecated and will be removed in version '1.0.0'. Use 'alt-group4' ", https://github.com/microsoft/knack,dc7b7d857ce3c11b2a3155b81f2470b3b579e7c9,tests/test_deprecation.py::TestCommandDeprecation::test_deprecate_command_expired_execute,tests/test_deprecation.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- from __future__ import unicode_literals import unittest try: import mock except ImportError: from unittest import mock from threading import Lock from knack.arguments import ArgumentsContext from knack.commands import CLICommand, CLICommandsLoader, CommandGroup from tests.util import DummyCLI, redirect_io, disable_color def example_handler(arg1, arg2=None, arg3=None): """""" Short summary here. Long summary here. Still long summary. """""" pass def example_arg_handler(arg1, opt1, arg2=None, opt2=None, arg3=None, opt3=None, arg4=None, opt4=None, arg5=None, opt5=None): pass # pylint: disable=line-too-long class TestCommandDeprecation(unittest.TestCase): def setUp(self): from knack.help_files import helps class DeprecationTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): super(DeprecationTestCommandLoader, self).load_command_table(args) with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g: g.command('cmd1', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd1')) g.command('cmd2', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd2', hide='1.0.0')) g.command('cmd3', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd3', hide='0.1.0')) g.command('cmd4', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd4', expiration='1.0.0')) g.command('cmd5', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd5', expiration='0.1.0')) with CommandGroup(self, 'grp1', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-grp1')) as g: g.command('cmd1', 'example_handler') return self.command_table def load_arguments(self, command): with ArgumentsContext(self, '') as c: c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) super(DeprecationTestCommandLoader, self).load_arguments(command) helps['grp1'] = """""" type: group short-summary: A group. """""" self.cli_ctx = DummyCLI(commands_loader_cls=DeprecationTestCommandLoader) @redirect_io def test_deprecate_command_group_help(self): """""" Ensure deprecated commands appear (or don't appear) correctly in group help view. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('-h'.split()) actual = self.io.getvalue() expected = u"""""" Group {} Subgroups: grp1 [Deprecated] : A group. Commands: cmd1 [Deprecated] : Short summary here. cmd2 [Deprecated] : Short summary here. cmd4 [Deprecated] : Short summary here. """""".format(self.cli_ctx.name) self.assertEqual(expected, actual) @redirect_io def test_deprecate_command_help_hidden(self): """""" Ensure hidden deprecated command can be used. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('cmd3 -h'.split()) actual = self.io.getvalue() expected = """""" Command {} cmd3 : Short summary here. Long summary here. Still long summary. This command has been deprecated and will be removed in a future release. Use 'alt- cmd3' instead. Arguments -b [Required] : Allowed values: a, b, c. --arg -a : Allowed values: 1, 2, 3. --arg3 """""".format(self.cli_ctx.name) self.assertIn(expected, actual) @redirect_io def test_deprecate_command_plain_execute(self): """""" Ensure general warning displayed when running deprecated command. """""" self.cli_ctx.invoke('cmd1 -b b'.split()) actual = self.io.getvalue() expected = ""This command has been deprecated and will be removed in a future release. Use 'alt-cmd1' instead."" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_hidden_execute(self): """""" Ensure general warning displayed when running hidden deprecated command. """""" self.cli_ctx.invoke('cmd3 -b b'.split()) actual = self.io.getvalue() expected = ""This command has been deprecated and will be removed in a future release. Use 'alt-cmd3' instead."" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_expiring_execute(self): """""" Ensure specific warning displayed when running expiring deprecated command. """""" self.cli_ctx.invoke('cmd4 -b b'.split()) actual = self.io.getvalue() expected = ""This command has been deprecated and will be removed in version '1.0.0'. Use 'alt-cmd4' instead."" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_expiring_execute_no_color(self): """""" Ensure warning is displayed without color. """""" self.cli_ctx.enable_color = False self.cli_ctx.invoke('cmd4 -b b'.split()) actual = self.io.getvalue() expected = ""WARNING: This command has been deprecated and will be removed in version '1.0.0'"" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_expired_execute(self): """""" Ensure expired command cannot be reached. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('cmd5 -h'.split()) actual = self.io.getvalue() expected = """"""cli: 'cmd5' is not in the 'cli' command group."""""" self.assertIn(expected, actual) @redirect_io @disable_color def test_deprecate_command_expired_execute_no_color(self): """""" Ensure error is displayed without color. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('cmd5 -h'.split()) actual = self.io.getvalue() expected = """"""ERROR: cli: 'cmd5' is not in the 'cli' command group."""""" self.assertIn(expected, actual) class TestCommandGroupDeprecation(unittest.TestCase): def setUp(self): from knack.help_files import helps class DeprecationTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): super(DeprecationTestCommandLoader, self).load_command_table(args) with CommandGroup(self, 'group1', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group1')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group2', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group2', hide='1.0.0')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group3', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group3', hide='0.1.0')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group4', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group4', expiration='1.0.0')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group5', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group5', expiration='0.1.0')) as g: g.command('cmd1', 'example_handler') return self.command_table def load_arguments(self, command): with ArgumentsContext(self, '') as c: c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) super(DeprecationTestCommandLoader, self).load_arguments(command) helps['group1'] = """""" type: group short-summary: A group. """""" self.cli_ctx = DummyCLI(commands_loader_cls=DeprecationTestCommandLoader) @redirect_io def test_deprecate_command_group_help_plain(self): """""" Ensure help warnings appear for deprecated command group help. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('group1 -h'.split()) actual = self.io.getvalue() expected = """""" Group cli group1 : A group. This command group has been deprecated and will be removed in a future release. Use 'alt-group1' instead. Commands: cmd1 : Short summary here. """""".format(self.cli_ctx.name) self.assertEqual(expected, actual) @redirect_io def test_deprecate_command_group_help_hidden(self): """""" Ensure hidden deprecated command can be used. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('group3 -h'.split()) actual = self.io.getvalue() expected = """""" Group {} group3 This command group has been deprecated and will be removed in a future release. Use 'alt-group3' instead. Commands: cmd1 : Short summary here. """""".format(self.cli_ctx.name) self.assertIn(expected, actual) @redirect_io def test_deprecate_command_group_help_expiring(self): """""" Ensure specific warning displayed when running expiring deprecated command. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('group4 -h'.split()) actual = self.io.getvalue() expected = """""" Group {} group4 This command group has been deprecated and will be removed in version '1.0.0'. Use 'alt-group4' ", https://github.com/microsoft/knack,dc7b7d857ce3c11b2a3155b81f2470b3b579e7c9,tests/test_deprecation.py::TestCommandDeprecation::test_deprecate_command_expired_execute_no_color,tests/test_deprecation.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- from __future__ import unicode_literals import unittest try: import mock except ImportError: from unittest import mock from threading import Lock from knack.arguments import ArgumentsContext from knack.commands import CLICommand, CLICommandsLoader, CommandGroup from tests.util import DummyCLI, redirect_io, disable_color def example_handler(arg1, arg2=None, arg3=None): """""" Short summary here. Long summary here. Still long summary. """""" pass def example_arg_handler(arg1, opt1, arg2=None, opt2=None, arg3=None, opt3=None, arg4=None, opt4=None, arg5=None, opt5=None): pass # pylint: disable=line-too-long class TestCommandDeprecation(unittest.TestCase): def setUp(self): from knack.help_files import helps class DeprecationTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): super(DeprecationTestCommandLoader, self).load_command_table(args) with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g: g.command('cmd1', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd1')) g.command('cmd2', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd2', hide='1.0.0')) g.command('cmd3', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd3', hide='0.1.0')) g.command('cmd4', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd4', expiration='1.0.0')) g.command('cmd5', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd5', expiration='0.1.0')) with CommandGroup(self, 'grp1', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-grp1')) as g: g.command('cmd1', 'example_handler') return self.command_table def load_arguments(self, command): with ArgumentsContext(self, '') as c: c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) super(DeprecationTestCommandLoader, self).load_arguments(command) helps['grp1'] = """""" type: group short-summary: A group. """""" self.cli_ctx = DummyCLI(commands_loader_cls=DeprecationTestCommandLoader) @redirect_io def test_deprecate_command_group_help(self): """""" Ensure deprecated commands appear (or don't appear) correctly in group help view. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('-h'.split()) actual = self.io.getvalue() expected = u"""""" Group {} Subgroups: grp1 [Deprecated] : A group. Commands: cmd1 [Deprecated] : Short summary here. cmd2 [Deprecated] : Short summary here. cmd4 [Deprecated] : Short summary here. """""".format(self.cli_ctx.name) self.assertEqual(expected, actual) @redirect_io def test_deprecate_command_help_hidden(self): """""" Ensure hidden deprecated command can be used. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('cmd3 -h'.split()) actual = self.io.getvalue() expected = """""" Command {} cmd3 : Short summary here. Long summary here. Still long summary. This command has been deprecated and will be removed in a future release. Use 'alt- cmd3' instead. Arguments -b [Required] : Allowed values: a, b, c. --arg -a : Allowed values: 1, 2, 3. --arg3 """""".format(self.cli_ctx.name) self.assertIn(expected, actual) @redirect_io def test_deprecate_command_plain_execute(self): """""" Ensure general warning displayed when running deprecated command. """""" self.cli_ctx.invoke('cmd1 -b b'.split()) actual = self.io.getvalue() expected = ""This command has been deprecated and will be removed in a future release. Use 'alt-cmd1' instead."" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_hidden_execute(self): """""" Ensure general warning displayed when running hidden deprecated command. """""" self.cli_ctx.invoke('cmd3 -b b'.split()) actual = self.io.getvalue() expected = ""This command has been deprecated and will be removed in a future release. Use 'alt-cmd3' instead."" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_expiring_execute(self): """""" Ensure specific warning displayed when running expiring deprecated command. """""" self.cli_ctx.invoke('cmd4 -b b'.split()) actual = self.io.getvalue() expected = ""This command has been deprecated and will be removed in version '1.0.0'. Use 'alt-cmd4' instead."" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_expiring_execute_no_color(self): """""" Ensure warning is displayed without color. """""" self.cli_ctx.enable_color = False self.cli_ctx.invoke('cmd4 -b b'.split()) actual = self.io.getvalue() expected = ""WARNING: This command has been deprecated and will be removed in version '1.0.0'"" self.assertIn(expected, actual) @redirect_io def test_deprecate_command_expired_execute(self): """""" Ensure expired command cannot be reached. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('cmd5 -h'.split()) actual = self.io.getvalue() expected = """"""cli: 'cmd5' is not in the 'cli' command group."""""" self.assertIn(expected, actual) @redirect_io @disable_color def test_deprecate_command_expired_execute_no_color(self): """""" Ensure error is displayed without color. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('cmd5 -h'.split()) actual = self.io.getvalue() expected = """"""ERROR: cli: 'cmd5' is not in the 'cli' command group."""""" self.assertIn(expected, actual) class TestCommandGroupDeprecation(unittest.TestCase): def setUp(self): from knack.help_files import helps class DeprecationTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): super(DeprecationTestCommandLoader, self).load_command_table(args) with CommandGroup(self, 'group1', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group1')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group2', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group2', hide='1.0.0')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group3', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group3', hide='0.1.0')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group4', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group4', expiration='1.0.0')) as g: g.command('cmd1', 'example_handler') with CommandGroup(self, 'group5', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group5', expiration='0.1.0')) as g: g.command('cmd1', 'example_handler') return self.command_table def load_arguments(self, command): with ArgumentsContext(self, '') as c: c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) super(DeprecationTestCommandLoader, self).load_arguments(command) helps['group1'] = """""" type: group short-summary: A group. """""" self.cli_ctx = DummyCLI(commands_loader_cls=DeprecationTestCommandLoader) @redirect_io def test_deprecate_command_group_help_plain(self): """""" Ensure help warnings appear for deprecated command group help. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('group1 -h'.split()) actual = self.io.getvalue() expected = """""" Group cli group1 : A group. This command group has been deprecated and will be removed in a future release. Use 'alt-group1' instead. Commands: cmd1 : Short summary here. """""".format(self.cli_ctx.name) self.assertEqual(expected, actual) @redirect_io def test_deprecate_command_group_help_hidden(self): """""" Ensure hidden deprecated command can be used. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('group3 -h'.split()) actual = self.io.getvalue() expected = """""" Group {} group3 This command group has been deprecated and will be removed in a future release. Use 'alt-group3' instead. Commands: cmd1 : Short summary here. """""".format(self.cli_ctx.name) self.assertIn(expected, actual) @redirect_io def test_deprecate_command_group_help_expiring(self): """""" Ensure specific warning displayed when running expiring deprecated command. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('group4 -h'.split()) actual = self.io.getvalue() expected = """""" Group {} group4 This command group has been deprecated and will be removed in version '1.0.0'. Use 'alt-group4' ", https://github.com/microsoft/knack,dc7b7d857ce3c11b2a3155b81f2470b3b579e7c9,tests/test_help.py::TestHelp::test_help_extra_params,tests/test_help.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- import sys import unittest import mock from six import StringIO from knack.help import ArgumentGroupRegistry, HelpObject from knack.arguments import ArgumentsContext from knack.commands import CLICommand, CLICommandsLoader, CommandGroup from knack.events import EVENT_PARSER_GLOBAL_CREATE from knack.invocation import CommandInvoker from tests.util import MockContext, DummyCLI io = {} def redirect_io(func): def wrapper(self): global io old_stdout = sys.stdout old_stderr = sys.stderr sys.stdout = sys.stderr = io = StringIO() func(self) io.close() sys.stdout = old_stdout sys.stderr = old_stderr return wrapper def example_handler(arg1, arg2=None, arg3=None): """""" Short summary here. Long summary here. Still long summary. """""" pass class TestHelpArgumentGroupRegistry(unittest.TestCase): def test_help_argument_group_registry(self): groups = [ 'Z Arguments', 'B Arguments', 'Global Arguments', 'A Arguments', ] group_registry = ArgumentGroupRegistry(groups) self.assertEqual(group_registry.get_group_priority('A Arguments'), '000002') self.assertEqual(group_registry.get_group_priority('B Arguments'), '000003') self.assertEqual(group_registry.get_group_priority('Z Arguments'), '000004') self.assertEqual(group_registry.get_group_priority('Global Arguments'), '001000') class TestHelpObject(unittest.TestCase): def test_short_summary_no_fullstop(self): obj = HelpObject() original_summary = 'This summary has no fullstop' obj.short_summary = original_summary self.assertEqual(obj.short_summary, '{}.'.format(original_summary)) def test_short_summary_fullstop(self): obj = HelpObject() original_summary = 'This summary has fullstop.' obj.short_summary = original_summary self.assertEqual(obj.short_summary, original_summary) def test_short_summary_exclamation_point(self): obj = HelpObject() original_summary = 'This summary has exclamation point!' obj.short_summary = original_summary self.assertEqual(obj.short_summary, original_summary) class TestHelp(unittest.TestCase): def setUp(self): from knack.help_files import helps class HelpTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): super(HelpTestCommandLoader, self).load_command_table(args) with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g: g.command('n1', 'example_handler') g.command('n2', 'example_handler') g.command('n3', 'example_handler') g.command('n4', 'example_handler') g.command('n5', 'example_handler') with CommandGroup(self, 'group alpha', '{}#{{}}'.format(__name__)) as g: g.command('n1', 'example_handler') with CommandGroup(self, 'group beta', '{}#{{}}'.format(__name__)) as g: g.command('n1', 'example_handler') return self.command_table def load_arguments(self, command): for scope in ['n1', 'group alpha', 'group beta']: with ArgumentsContext(self, scope) as c: c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) for scope in ['n4', 'n5']: with ArgumentsContext(self, scope) as c: c.argument('arg1', options_list=['--foobar']) c.argument('arg2', options_list=['--foobar2'], required=True) c.argument('arg3', options_list=['--foobar3'], help='the foobar3') super(HelpTestCommandLoader, self).load_arguments(command) helps['n2'] = """""" type: command short-summary: YAML short summary. long-summary: YAML long summary. More summary. """""" helps['n3'] = """""" type: command long-summary: | line1 line2 """""" helps['n4'] = """""" type: command parameters: - name: --foobar type: string required: false short-summary: one line partial sentence long-summary: text, markdown, etc. populator-commands: - mycli abc xyz - default - name: --foobar2 type: string required: true short-summary: one line partial sentence long-summary: paragraph(s) """""" helps['n5'] = """""" type: command short-summary: this module does xyz one-line or so long-summary: | this module.... kjsdflkj... klsfkj paragraph1 this module.... kjsdflkj... klsfkj paragraph2 parameters: - name: --foobar type: string required: false short-summary: one line partial sentence long-summary: text, markdown, etc. populator-commands: - mycli abc xyz - default - name: --foobar2 type: string required: true short-summary: one line partial sentence long-summary: paragraph(s) examples: - name: foo example text: example details """""" helps['group alpha'] = """""" type: group short-summary: this module does xyz one-line or so long-summary: | this module.... kjsdflkj... klsfkj paragraph1 this module.... kjsdflkj... klsfkj paragraph2 """""" helps['group alpha n1'] = """""" short-summary: this module does xyz one-line or so long-summary: | this module.... kjsdflkj... klsfkj paragraph1 this module.... kjsdflkj... klsfkj paragraph2 parameters: - name: --arg -a type: string required: false short-summary: one line partial sentence long-summary: text, markdown, etc. populator-commands: - mycli abc xyz - default - name: -b type: string required: true short-summary: one line partial sentence long-summary: paragraph(s) examples: - name: foo example text: example details """""" self.cli_ctx = DummyCLI(commands_loader_cls=HelpTestCommandLoader) @redirect_io def test_choice_list_with_ints(self): """""" Ensure choice_list works with integer lists. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n1 -h'.split()) actual = io.getvalue() expected = 'Allowed values: 1, 2, 3' self.assertIn(expected, actual) @redirect_io def test_help_param(self): """""" Ensure both --help and -h produce the same output. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n1 -h'.split()) with self.assertRaises(SystemExit): self.cli_ctx.invoke('n1 --help'.split()) @redirect_io def test_help_long_and_short_description_from_docstring(self): """""" Ensure the first sentence of a docstring is parsed as the short summary and subsequent text is interpretted as the long summary. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n1 -h'.split()) actual = io.getvalue() expected = '\nCommand\n {} n1 : Short summary here.\n Long summary here. Still long summary.'.format(self.cli_ctx.name) self.assertTrue(actual.startswith(expected)) @redirect_io def test_help_long_and_short_description_from_yaml(self): """""" Ensure the YAML version of short and long summary display correctly and override any values that may have been obtained through reflection. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n2 -h'.split()) actual = io.getvalue() expected = '\nCommand\n {} n2 : YAML short summary.\n YAML long summary. More summary.'.format(self.cli_ctx.name) self.assertTrue(actual.startswith(expected)) @redirect_io def test_help_long_description_multi_line(self): """""" Ensure that multi-line help in the YAML is displayed correctly. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n3 -h'.split()) actual = io.getvalue() expected = '\nCommand\n {} n3 : Short summary here.\n Line1\n line2.\n'.format(self.cli_ctx.name) self.assertTrue(actual.startswith(expected)) @redirect_io def test_help_params_documentations(self): """""" Ensure argument help is rendered according to the YAML spec. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n4 -h'.split()) expected = """""" Command {} n4 : Short summary here. Arguments --foobar [Required] : One line partial sentence. Values from: mycli abc xyz, default. Text, markdown, etc. --foobar2 [Required] : One line partial sentence. Paragraph(s). --foobar3 : The foobar3. """""" actual = io.getvalue() expected = expected.format(self.cli_ctx.name) self.assertTrue(actual.startswith(expected)) @redirect_io def test_help_full_documentations(self): """""" Test all features of YAML format. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n5 -h'.split()) expected = """""" Command ", https://github.com/microsoft/knack,dc7b7d857ce3c11b2a3155b81f2470b3b579e7c9,tests/test_help.py::TestHelp::test_help_missing_params,tests/test_help.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# -------------------------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- import sys import unittest import mock from six import StringIO from knack.help import ArgumentGroupRegistry, HelpObject from knack.arguments import ArgumentsContext from knack.commands import CLICommand, CLICommandsLoader, CommandGroup from knack.events import EVENT_PARSER_GLOBAL_CREATE from knack.invocation import CommandInvoker from tests.util import MockContext, DummyCLI io = {} def redirect_io(func): def wrapper(self): global io old_stdout = sys.stdout old_stderr = sys.stderr sys.stdout = sys.stderr = io = StringIO() func(self) io.close() sys.stdout = old_stdout sys.stderr = old_stderr return wrapper def example_handler(arg1, arg2=None, arg3=None): """""" Short summary here. Long summary here. Still long summary. """""" pass class TestHelpArgumentGroupRegistry(unittest.TestCase): def test_help_argument_group_registry(self): groups = [ 'Z Arguments', 'B Arguments', 'Global Arguments', 'A Arguments', ] group_registry = ArgumentGroupRegistry(groups) self.assertEqual(group_registry.get_group_priority('A Arguments'), '000002') self.assertEqual(group_registry.get_group_priority('B Arguments'), '000003') self.assertEqual(group_registry.get_group_priority('Z Arguments'), '000004') self.assertEqual(group_registry.get_group_priority('Global Arguments'), '001000') class TestHelpObject(unittest.TestCase): def test_short_summary_no_fullstop(self): obj = HelpObject() original_summary = 'This summary has no fullstop' obj.short_summary = original_summary self.assertEqual(obj.short_summary, '{}.'.format(original_summary)) def test_short_summary_fullstop(self): obj = HelpObject() original_summary = 'This summary has fullstop.' obj.short_summary = original_summary self.assertEqual(obj.short_summary, original_summary) def test_short_summary_exclamation_point(self): obj = HelpObject() original_summary = 'This summary has exclamation point!' obj.short_summary = original_summary self.assertEqual(obj.short_summary, original_summary) class TestHelp(unittest.TestCase): def setUp(self): from knack.help_files import helps class HelpTestCommandLoader(CLICommandsLoader): def load_command_table(self, args): super(HelpTestCommandLoader, self).load_command_table(args) with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g: g.command('n1', 'example_handler') g.command('n2', 'example_handler') g.command('n3', 'example_handler') g.command('n4', 'example_handler') g.command('n5', 'example_handler') with CommandGroup(self, 'group alpha', '{}#{{}}'.format(__name__)) as g: g.command('n1', 'example_handler') with CommandGroup(self, 'group beta', '{}#{{}}'.format(__name__)) as g: g.command('n1', 'example_handler') return self.command_table def load_arguments(self, command): for scope in ['n1', 'group alpha', 'group beta']: with ArgumentsContext(self, scope) as c: c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3]) c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c']) for scope in ['n4', 'n5']: with ArgumentsContext(self, scope) as c: c.argument('arg1', options_list=['--foobar']) c.argument('arg2', options_list=['--foobar2'], required=True) c.argument('arg3', options_list=['--foobar3'], help='the foobar3') super(HelpTestCommandLoader, self).load_arguments(command) helps['n2'] = """""" type: command short-summary: YAML short summary. long-summary: YAML long summary. More summary. """""" helps['n3'] = """""" type: command long-summary: | line1 line2 """""" helps['n4'] = """""" type: command parameters: - name: --foobar type: string required: false short-summary: one line partial sentence long-summary: text, markdown, etc. populator-commands: - mycli abc xyz - default - name: --foobar2 type: string required: true short-summary: one line partial sentence long-summary: paragraph(s) """""" helps['n5'] = """""" type: command short-summary: this module does xyz one-line or so long-summary: | this module.... kjsdflkj... klsfkj paragraph1 this module.... kjsdflkj... klsfkj paragraph2 parameters: - name: --foobar type: string required: false short-summary: one line partial sentence long-summary: text, markdown, etc. populator-commands: - mycli abc xyz - default - name: --foobar2 type: string required: true short-summary: one line partial sentence long-summary: paragraph(s) examples: - name: foo example text: example details """""" helps['group alpha'] = """""" type: group short-summary: this module does xyz one-line or so long-summary: | this module.... kjsdflkj... klsfkj paragraph1 this module.... kjsdflkj... klsfkj paragraph2 """""" helps['group alpha n1'] = """""" short-summary: this module does xyz one-line or so long-summary: | this module.... kjsdflkj... klsfkj paragraph1 this module.... kjsdflkj... klsfkj paragraph2 parameters: - name: --arg -a type: string required: false short-summary: one line partial sentence long-summary: text, markdown, etc. populator-commands: - mycli abc xyz - default - name: -b type: string required: true short-summary: one line partial sentence long-summary: paragraph(s) examples: - name: foo example text: example details """""" self.cli_ctx = DummyCLI(commands_loader_cls=HelpTestCommandLoader) @redirect_io def test_choice_list_with_ints(self): """""" Ensure choice_list works with integer lists. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n1 -h'.split()) actual = io.getvalue() expected = 'Allowed values: 1, 2, 3' self.assertIn(expected, actual) @redirect_io def test_help_param(self): """""" Ensure both --help and -h produce the same output. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n1 -h'.split()) with self.assertRaises(SystemExit): self.cli_ctx.invoke('n1 --help'.split()) @redirect_io def test_help_long_and_short_description_from_docstring(self): """""" Ensure the first sentence of a docstring is parsed as the short summary and subsequent text is interpretted as the long summary. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n1 -h'.split()) actual = io.getvalue() expected = '\nCommand\n {} n1 : Short summary here.\n Long summary here. Still long summary.'.format(self.cli_ctx.name) self.assertTrue(actual.startswith(expected)) @redirect_io def test_help_long_and_short_description_from_yaml(self): """""" Ensure the YAML version of short and long summary display correctly and override any values that may have been obtained through reflection. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n2 -h'.split()) actual = io.getvalue() expected = '\nCommand\n {} n2 : YAML short summary.\n YAML long summary. More summary.'.format(self.cli_ctx.name) self.assertTrue(actual.startswith(expected)) @redirect_io def test_help_long_description_multi_line(self): """""" Ensure that multi-line help in the YAML is displayed correctly. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n3 -h'.split()) actual = io.getvalue() expected = '\nCommand\n {} n3 : Short summary here.\n Line1\n line2.\n'.format(self.cli_ctx.name) self.assertTrue(actual.startswith(expected)) @redirect_io def test_help_params_documentations(self): """""" Ensure argument help is rendered according to the YAML spec. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n4 -h'.split()) expected = """""" Command {} n4 : Short summary here. Arguments --foobar [Required] : One line partial sentence. Values from: mycli abc xyz, default. Text, markdown, etc. --foobar2 [Required] : One line partial sentence. Paragraph(s). --foobar3 : The foobar3. """""" actual = io.getvalue() expected = expected.format(self.cli_ctx.name) self.assertTrue(actual.startswith(expected)) @redirect_io def test_help_full_documentations(self): """""" Test all features of YAML format. """""" with self.assertRaises(SystemExit): self.cli_ctx.invoke('n5 -h'.split()) expected = """""" Command ", https://github.com/miLibris/flask-rest-jsonapi,b44bc08b11213d49fadae873650d3555889052ec,tests/test_sqlalchemy_data_layer.py::test_api,tests/test_sqlalchemy_data_layer.py,NIO,flaky,Opened,https://github.com/miLibris/flask-rest-jsonapi/pull/207,classify;root_cause,"# -*- coding: utf-8 -*- from six.moves.urllib.parse import urlencode, parse_qs import pytest from sqlalchemy import create_engine, Column, Integer, DateTime, String, ForeignKey from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.ext.declarative import declarative_base from flask import Blueprint, make_response, json from marshmallow_jsonapi.flask import Schema, Relationship from marshmallow import Schema as MarshmallowSchema from marshmallow_jsonapi import fields from marshmallow import ValidationError from flask_rest_jsonapi import Api, ResourceList, ResourceDetail, ResourceRelationship, JsonApiException from flask_rest_jsonapi.pagination import add_pagination_links from flask_rest_jsonapi.exceptions import RelationNotFound, InvalidSort, InvalidFilters, InvalidInclude, BadRequest from flask_rest_jsonapi.querystring import QueryStringManager as QSManager from flask_rest_jsonapi.data_layers.alchemy import SqlalchemyDataLayer from flask_rest_jsonapi.data_layers.base import BaseDataLayer from flask_rest_jsonapi.data_layers.filtering.alchemy import Node import flask_rest_jsonapi.decorators import flask_rest_jsonapi.resource import flask_rest_jsonapi.schema @pytest.fixture(scope=""module"") def base(): yield declarative_base() @pytest.fixture(scope=""module"") def person_tag_model(base): class Person_Tag(base): __tablename__ = 'person_tag' id = Column(Integer, ForeignKey('person.person_id'), primary_key=True, index=True) key = Column(String, primary_key=True) value = Column(String, primary_key=True) yield Person_Tag @pytest.fixture(scope=""module"") def person_single_tag_model(base): class Person_Single_Tag(base): __tablename__ = 'person_single_tag' id = Column(Integer, ForeignKey('person.person_id'), primary_key=True, index=True) key = Column(String) value = Column(String) yield Person_Single_Tag @pytest.fixture(scope=""module"") def string_json_attribute_person_model(base): """""" This approach to faking JSON support for testing with sqlite is borrowed from: https://avacariu.me/articles/2016/compiling-json-as-text-for-sqlite-with-sqlalchemy """""" import sqlalchemy.types as types import json class StringyJSON(types.TypeDecorator): """"""Stores and retrieves JSON as TEXT."""""" impl = types.TEXT def process_bind_param(self, value, dialect): if value is not None: value = json.dumps(value) return value def process_result_value(self, value, dialect): if value is not None: value = json.loads(value) return value # TypeEngine.with_variant says ""use StringyJSON instead when # connecting to 'sqlite'"" MagicJSON = types.JSON().with_variant(StringyJSON, 'sqlite') class StringJsonAttributePerson(base): __tablename__ = 'string_json_attribute_person' person_id = Column(Integer, primary_key=True) name = Column(String, nullable=False) birth_date = Column(DateTime) # This model uses a String type for ""json_tags"" to avoid dependency on a nonstandard SQL type in testing, \ # while still demonstrating support address = Column(MagicJSON) yield StringJsonAttributePerson @pytest.fixture(scope=""module"") def person_model(base): class Person(base): __tablename__ = 'person' person_id = Column(Integer, primary_key=True) name = Column(String, nullable=False) birth_date = Column(DateTime) computers = relationship(""Computer"", backref=""person"") tags = relationship(""Person_Tag"", cascade=""save-update, merge, delete, delete-orphan"") single_tag = relationship(""Person_Single_Tag"", uselist=False, cascade=""save-update, merge, delete, delete-orphan"") computers_owned = relationship(""Computer"") yield Person @pytest.fixture(scope=""module"") def computer_model(base): class Computer(base): __tablename__ = 'computer' id = Column(Integer, primary_key=True) serial = Column(String, nullable=False) person_id = Column(Integer, ForeignKey('person.person_id')) yield Computer @pytest.fixture(scope=""module"") def engine(person_tag_model, person_single_tag_model, person_model, computer_model, string_json_attribute_person_model): engine = create_engine(""sqlite:///:memory:"") person_tag_model.metadata.create_all(engine) person_single_tag_model.metadata.create_all(engine) person_model.metadata.create_all(engine) computer_model.metadata.create_all(engine) string_json_attribute_person_model.metadata.create_all(engine) return engine @pytest.fixture(scope=""module"") def session(engine): Session = sessionmaker(bind=engine) return Session() @pytest.fixture() def person(session, person_model): person_ = person_model(name='test') session_ = session session_.add(person_) session_.commit() yield person_ session_.delete(person_) session_.commit() @pytest.fixture() def person_2(session, person_model): person_ = person_model(name='test2') session_ = session session_.add(person_) session_.commit() yield person_ session_.delete(person_) session_.commit() @pytest.fixture() def computer(session, computer_model): computer_ = computer_model(serial='1') session_ = session session_.add(computer_) session_.commit() yield computer_ session_.delete(computer_) session_.commit() @pytest.fixture(scope=""module"") def dummy_decorator(): def deco(f): def wrapper_f(*args, **kwargs): return f(*args, **kwargs) return wrapper_f yield deco @pytest.fixture(scope=""module"") def person_tag_schema(): class PersonTagSchema(MarshmallowSchema): class Meta: type_ = 'person_tag' id = fields.Str(dump_only=True, load_only=True) key = fields.Str() value = fields.Str() yield PersonTagSchema @pytest.fixture(scope=""module"") def person_single_tag_schema(): class PersonSingleTagSchema(MarshmallowSchema): class Meta: type_ = 'person_single_tag' id = fields.Str(dump_only=True, load_only=True) key = fields.Str() value = fields.Str() yield PersonSingleTagSchema @pytest.fixture(scope=""module"") def address_schema(): class AddressSchema(MarshmallowSchema): street = fields.String(required=True) city = fields.String(required=True) state = fields.String(missing='NC') zip = fields.String(required=True) yield AddressSchema @pytest.fixture(scope=""module"") def string_json_attribute_person_schema(address_schema): class StringJsonAttributePersonSchema(Schema): class Meta: type_ = 'string_json_attribute_person' self_view = 'api.string_json_attribute_person_detail' self_view_kwargs = {'person_id': ''} id = fields.Integer(as_string=True, dump_only=True, attribute='person_id') name = fields.Str(required=True) birth_date = fields.DateTime() address = fields.Nested(address_schema, many=False) yield StringJsonAttributePersonSchema @pytest.fixture(scope=""module"") def person_schema(person_tag_schema, person_single_tag_schema): class PersonSchema(Schema): class Meta: type_ = 'person' self_view = 'api.person_detail' self_view_kwargs = {'person_id': ''} id = fields.Integer(as_string=True, dump_only=True, attribute='person_id') name = fields.Str(required=True) birth_date = fields.DateTime() computers = Relationship(related_view='api.computer_list', related_view_kwargs={'person_id': ''}, schema='ComputerSchema', type_='computer', many=True) tags = fields.Nested(person_tag_schema, many=True) single_tag = fields.Nested(person_single_tag_schema) computers_owned = computers yield PersonSchema @pytest.fixture(scope=""module"") def computer_schema(): class ComputerSchema(Schema): class Meta: type_ = 'computer' self_view = 'api.computer_detail' self_view_kwargs = {'id': ''} id = fields.Integer(as_string=True, dump_only=True) serial = fields.Str(required=True) owner = Relationship(attribute='person', default=None, missing=None, related_view='api.person_detail', related_view_kwargs={'person_id': ''}, schema='PersonSchema', id_field='person_id', type_='person') yield ComputerSchema @pytest.fixture(scope=""module"") def before_create_object(): def before_create_object_(self, data, view_kwargs): pass yield before_create_object_ @pytest.fixture(scope=""module"") def before_update_object(): def before_update_object_(self, obj, data, view_kwargs): pass yield before_update_object_ @pytest.fixture(scope=""module"") def before_delete_object(): def before_delete_object_(self, obj, view_kwargs): pass yield before_delete_object_ @pytest.fixture(scope=""module"") def person_list(session, person_model, dummy_decorator, person_schema, before_create_object): class PersonList(ResourceList): schema = person_schema data_layer = {'model': person_model, 'session': session, 'mzthods': {'before_create_object': before_create_object}} get_decorators = [dummy_decorator] post_decorators = [dummy_decorator] get_schema_kwargs = dict() post_schema_kwargs = dict() yield", https://github.com/miLibris/flask-rest-jsonapi,b44bc08b11213d49fadae873650d3555889052ec,tests/test_sqlalchemy_data_layer.py::test_api_resources,tests/test_sqlalchemy_data_layer.py,NIO,flaky,Opened,https://github.com/miLibris/flask-rest-jsonapi/pull/207,classify;root_cause,"# -*- coding: utf-8 -*- from six.moves.urllib.parse import urlencode, parse_qs import pytest from sqlalchemy import create_engine, Column, Integer, DateTime, String, ForeignKey from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.ext.declarative import declarative_base from flask import Blueprint, make_response, json from marshmallow_jsonapi.flask import Schema, Relationship from marshmallow import Schema as MarshmallowSchema from marshmallow_jsonapi import fields from marshmallow import ValidationError from flask_rest_jsonapi import Api, ResourceList, ResourceDetail, ResourceRelationship, JsonApiException from flask_rest_jsonapi.pagination import add_pagination_links from flask_rest_jsonapi.exceptions import RelationNotFound, InvalidSort, InvalidFilters, InvalidInclude, BadRequest from flask_rest_jsonapi.querystring import QueryStringManager as QSManager from flask_rest_jsonapi.data_layers.alchemy import SqlalchemyDataLayer from flask_rest_jsonapi.data_layers.base import BaseDataLayer from flask_rest_jsonapi.data_layers.filtering.alchemy import Node import flask_rest_jsonapi.decorators import flask_rest_jsonapi.resource import flask_rest_jsonapi.schema @pytest.fixture(scope=""module"") def base(): yield declarative_base() @pytest.fixture(scope=""module"") def person_tag_model(base): class Person_Tag(base): __tablename__ = 'person_tag' id = Column(Integer, ForeignKey('person.person_id'), primary_key=True, index=True) key = Column(String, primary_key=True) value = Column(String, primary_key=True) yield Person_Tag @pytest.fixture(scope=""module"") def person_single_tag_model(base): class Person_Single_Tag(base): __tablename__ = 'person_single_tag' id = Column(Integer, ForeignKey('person.person_id'), primary_key=True, index=True) key = Column(String) value = Column(String) yield Person_Single_Tag @pytest.fixture(scope=""module"") def string_json_attribute_person_model(base): """""" This approach to faking JSON support for testing with sqlite is borrowed from: https://avacariu.me/articles/2016/compiling-json-as-text-for-sqlite-with-sqlalchemy """""" import sqlalchemy.types as types import json class StringyJSON(types.TypeDecorator): """"""Stores and retrieves JSON as TEXT."""""" impl = types.TEXT def process_bind_param(self, value, dialect): if value is not None: value = json.dumps(value) return value def process_result_value(self, value, dialect): if value is not None: value = json.loads(value) return value # TypeEngine.with_variant says ""use StringyJSON instead when # connecting to 'sqlite'"" MagicJSON = types.JSON().with_variant(StringyJSON, 'sqlite') class StringJsonAttributePerson(base): __tablename__ = 'string_json_attribute_person' person_id = Column(Integer, primary_key=True) name = Column(String, nullable=False) birth_date = Column(DateTime) # This model uses a String type for ""json_tags"" to avoid dependency on a nonstandard SQL type in testing, \ # while still demonstrating support address = Column(MagicJSON) yield StringJsonAttributePerson @pytest.fixture(scope=""module"") def person_model(base): class Person(base): __tablename__ = 'person' person_id = Column(Integer, primary_key=True) name = Column(String, nullable=False) birth_date = Column(DateTime) computers = relationship(""Computer"", backref=""person"") tags = relationship(""Person_Tag"", cascade=""save-update, merge, delete, delete-orphan"") single_tag = relationship(""Person_Single_Tag"", uselist=False, cascade=""save-update, merge, delete, delete-orphan"") computers_owned = relationship(""Computer"") yield Person @pytest.fixture(scope=""module"") def computer_model(base): class Computer(base): __tablename__ = 'computer' id = Column(Integer, primary_key=True) serial = Column(String, nullable=False) person_id = Column(Integer, ForeignKey('person.person_id')) yield Computer @pytest.fixture(scope=""module"") def engine(person_tag_model, person_single_tag_model, person_model, computer_model, string_json_attribute_person_model): engine = create_engine(""sqlite:///:memory:"") person_tag_model.metadata.create_all(engine) person_single_tag_model.metadata.create_all(engine) person_model.metadata.create_all(engine) computer_model.metadata.create_all(engine) string_json_attribute_person_model.metadata.create_all(engine) return engine @pytest.fixture(scope=""module"") def session(engine): Session = sessionmaker(bind=engine) return Session() @pytest.fixture() def person(session, person_model): person_ = person_model(name='test') session_ = session session_.add(person_) session_.commit() yield person_ session_.delete(person_) session_.commit() @pytest.fixture() def person_2(session, person_model): person_ = person_model(name='test2') session_ = session session_.add(person_) session_.commit() yield person_ session_.delete(person_) session_.commit() @pytest.fixture() def computer(session, computer_model): computer_ = computer_model(serial='1') session_ = session session_.add(computer_) session_.commit() yield computer_ session_.delete(computer_) session_.commit() @pytest.fixture(scope=""module"") def dummy_decorator(): def deco(f): def wrapper_f(*args, **kwargs): return f(*args, **kwargs) return wrapper_f yield deco @pytest.fixture(scope=""module"") def person_tag_schema(): class PersonTagSchema(MarshmallowSchema): class Meta: type_ = 'person_tag' id = fields.Str(dump_only=True, load_only=True) key = fields.Str() value = fields.Str() yield PersonTagSchema @pytest.fixture(scope=""module"") def person_single_tag_schema(): class PersonSingleTagSchema(MarshmallowSchema): class Meta: type_ = 'person_single_tag' id = fields.Str(dump_only=True, load_only=True) key = fields.Str() value = fields.Str() yield PersonSingleTagSchema @pytest.fixture(scope=""module"") def address_schema(): class AddressSchema(MarshmallowSchema): street = fields.String(required=True) city = fields.String(required=True) state = fields.String(missing='NC') zip = fields.String(required=True) yield AddressSchema @pytest.fixture(scope=""module"") def string_json_attribute_person_schema(address_schema): class StringJsonAttributePersonSchema(Schema): class Meta: type_ = 'string_json_attribute_person' self_view = 'api.string_json_attribute_person_detail' self_view_kwargs = {'person_id': ''} id = fields.Integer(as_string=True, dump_only=True, attribute='person_id') name = fields.Str(required=True) birth_date = fields.DateTime() address = fields.Nested(address_schema, many=False) yield StringJsonAttributePersonSchema @pytest.fixture(scope=""module"") def person_schema(person_tag_schema, person_single_tag_schema): class PersonSchema(Schema): class Meta: type_ = 'person' self_view = 'api.person_detail' self_view_kwargs = {'person_id': ''} id = fields.Integer(as_string=True, dump_only=True, attribute='person_id') name = fields.Str(required=True) birth_date = fields.DateTime() computers = Relationship(related_view='api.computer_list', related_view_kwargs={'person_id': ''}, schema='ComputerSchema', type_='computer', many=True) tags = fields.Nested(person_tag_schema, many=True) single_tag = fields.Nested(person_single_tag_schema) computers_owned = computers yield PersonSchema @pytest.fixture(scope=""module"") def computer_schema(): class ComputerSchema(Schema): class Meta: type_ = 'computer' self_view = 'api.computer_detail' self_view_kwargs = {'id': ''} id = fields.Integer(as_string=True, dump_only=True) serial = fields.Str(required=True) owner = Relationship(attribute='person', default=None, missing=None, related_view='api.person_detail', related_view_kwargs={'person_id': ''}, schema='PersonSchema', id_field='person_id', type_='person') yield ComputerSchema @pytest.fixture(scope=""module"") def before_create_object(): def before_create_object_(self, data, view_kwargs): pass yield before_create_object_ @pytest.fixture(scope=""module"") def before_update_object(): def before_update_object_(self, obj, data, view_kwargs): pass yield before_update_object_ @pytest.fixture(scope=""module"") def before_delete_object(): def before_delete_object_(self, obj, view_kwargs): pass yield before_delete_object_ @pytest.fixture(scope=""module"") def person_list(session, person_model, dummy_decorator, person_schema, before_create_object): class PersonList(ResourceList): schema = person_schema data_layer = {'model': person_model, 'session': session, 'mzthods': {'before_create_object': before_create_object}} get_decorators = [dummy_decorator] post_decorators = [dummy_decorator] get_schema_kwargs = dict() post_schema_kwargs = dict() yield", https://github.com/MirkoRossini/zanna,4c28b5ea3940fba69bee3fdf965a039c9b8f45c5,tests/test_decorators.py::TestDecorators::test_class_decorated,tests/test_decorators.py,OD-Vic,flaky,nan,nan,classify;root_cause," from unittest import TestCase from zanna import Injector from zanna import decorators import pytest class Thing: pass @decorators.provider_for(""value"") def provide_value(): return 3 @decorators.provider def provide_thing() -> Thing: return Thing() @decorators.inject class OtherThing: def __init__(self, value, thing:Thing): self.value = value self.thing = thing class TestDecorators(TestCase): def test_provider_decorated_needs_type_or_name(self): with pytest.raises(TypeError): @decorators.provider def provider_invalid(): pass inj = Injector(use_decorators=True) def test_provider_decorated(self): inj = Injector(use_decorators=True) assert inj.get_instance(""value"") == 3 assert isinstance(inj.get_instance(Thing), Thing) def test_class_decorated(self): inj = Injector(use_decorators=True) otherthing = inj.get_instance(OtherThing) assert otherthing.value == 3 assert isinstance(otherthing.thing, Thing) assert isinstance(otherthing, OtherThing) ", https://github.com/MirkoRossini/zanna,4c28b5ea3940fba69bee3fdf965a039c9b8f45c5,tests/test_decorators.py::TestDecorators::test_provider_decorated,tests/test_decorators.py,OD-Vic,flaky,nan,nan,classify;root_cause," from unittest import TestCase from zanna import Injector from zanna import decorators import pytest class Thing: pass @decorators.provider_for(""value"") def provide_value(): return 3 @decorators.provider def provide_thing() -> Thing: return Thing() @decorators.inject class OtherThing: def __init__(self, value, thing:Thing): self.value = value self.thing = thing class TestDecorators(TestCase): def test_provider_decorated_needs_type_or_name(self): with pytest.raises(TypeError): @decorators.provider def provider_invalid(): pass inj = Injector(use_decorators=True) def test_provider_decorated(self): inj = Injector(use_decorators=True) assert inj.get_instance(""value"") == 3 assert isinstance(inj.get_instance(Thing), Thing) def test_class_decorated(self): inj = Injector(use_decorators=True) otherthing = inj.get_instance(OtherThing) assert otherthing.value == 3 assert isinstance(otherthing.thing, Thing) assert isinstance(otherthing, OtherThing) ", https://github.com/mojochao/configaro,d7b432fb2f99c4cb3c3208082f0afff62454c83d,tests/test_configaro.py::test_get,tests/test_configaro.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import os import munch import pytest CONFIG_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'config')) SAMPLE_DATA = { 'name': 'defaults', 'log': { 'file': 'some-file.txt', 'level': 'ERROR' }, 'monitoring': { 'haproxy': { 'disabled': False }, 'nginx': { 'disabled': True } } } def test__module_path(): from configaro import _module_path assert _module_path(CONFIG_DIR, 'defaults') == os.path.join(CONFIG_DIR, 'defaults.py') def test__import_module(): from configaro import _import_module module = _import_module(CONFIG_DIR, 'defaults') assert module.config == SAMPLE_DATA with pytest.raises(ImportError): _import_module(CONFIG_DIR, 'default') def test__merge(): from configaro import _merge defaults = SAMPLE_DATA locals = { 'name': 'locals', 'log': { 'level': 'DEBUG' }, 'monitoring': { 'haproxy': { 'disabled': True } } } expected = { 'name': 'locals', 'log': { 'file': 'some-file.txt', 'level': 'DEBUG' }, 'monitoring': { 'haproxy': { 'disabled': True }, 'nginx': { 'disabled': True } } } merged = dict(_merge(defaults, locals)) assert merged == expected def test__load(): from configaro import _load, _module_path path = _module_path(CONFIG_DIR, 'defaults') config = _load(path) assert config['name'] == 'defaults' def test__cast(): from configaro import _cast assert _cast('None') is None assert isinstance(_cast('False'), bool) assert isinstance(_cast('1'), int) assert isinstance(_cast('1.234'), float) assert isinstance(_cast('Hello'), str) def test__get(): from configaro import ConfigPropertyNotFoundError, _get data = munch.munchify(SAMPLE_DATA) assert _get(data, 'name') == 'defaults' assert _get(data, 'log.level') == 'ERROR' assert _get(data, 'monitoring.haproxy.disabled') is False assert _get(data, 'monitoring.nginx.disabled') is True with pytest.raises(ConfigPropertyNotFoundError): assert _get(data, 'monitoring.nginx.disable') is True assert _get(data, 'monitoring.nginx.disable', default=None) is None def test__put(): from configaro import _get, _put data = munch.munchify(SAMPLE_DATA) _put(data, 'name', 'locals') assert _get(data, 'name') == 'locals' _put(data, 'log.level', 'DEBUG') assert _get(data, 'log.level') == 'DEBUG' def test__config_package_dir(): from configaro import _config_package_dir assert _config_package_dir('tests.config') == CONFIG_DIR def test___config_module_paths(): from configaro import _config_module_paths expected = [ os.path.join(CONFIG_DIR, 'defaults.py'), os.path.join(CONFIG_DIR, 'locals.py'), ] paths = _config_module_paths('tests.config') assert sorted(paths) == sorted(expected) def test_exports(): from configaro import __all__ as exports expected = [ 'ConfigError', 'ConfigModuleNotFoundError', 'ConfigModuleNotValidError', 'ConfigObjectNotInitializedError', 'ConfigPropertyNotFoundError', 'ConfigPropertyNotScalarError', 'ConfigUpdateNotValidError', 'get', 'init', 'put', ] assert sorted(exports) == sorted(expected) def test_init(): from configaro import init init('tests.config') def test_get(): from configaro import ConfigPropertyNotFoundError, get, init init('tests.config') expected = { 'name': 'locals', 'log': { 'file': 'some-file.txt', 'level': 'DEBUG' }, 'monitoring': { 'haproxy': { 'disabled': True }, 'nginx': { 'disabled': True } } } config = get() assert config.log.level == 'DEBUG' assert config == munch.munchify(expected) log = get('log') assert log.level == 'DEBUG' log = munch.unmunchify(log) assert log == expected['log'] assert get('name') == 'locals' assert get('log.level') == 'DEBUG' assert get('monitoring.haproxy.disabled') is True assert get('monitoring.nginx.disabled') is True with pytest.raises(ConfigPropertyNotFoundError): assert get('monitoring.nginx.disable') is True assert get('monitoring.nginx.disable', default=None) is None def test_put(): from configaro import ConfigPropertyNotScalarError, ConfigUpdateNotValidError, get, init, put init('tests.config') put('log.level=INFO') config = get() assert config.log.level == 'INFO' # ensure that we cannot put with a malformed arg with pytest.raises(ConfigUpdateNotValidError): put('log.level') # ensure that we cannot put a non-scalar with pytest.raises(ConfigPropertyNotScalarError): put('log=INFO') def test_ConfigaroError(): from configaro import ConfigError message = 'this is an error' error = ConfigError(message) assert error.message == message def test_NotInitializedError(): from configaro import ConfigError, ConfigObjectNotInitializedError error = ConfigObjectNotInitializedError() assert isinstance(error, ConfigError) assert error.message == 'config object not initialized' def test_ConfigNotFoundError(): from configaro import ConfigError, ConfigModuleNotFoundError path = '/some/path' error = ConfigModuleNotFoundError(path) assert isinstance(error, ConfigError) assert error.message == f'config module not found: {path}' assert error.path == path path = '/another/path' error = ConfigModuleNotFoundError(path=path) assert error.path == path def test_ConfigNotValidError(): from configaro import ConfigError, ConfigModuleNotValidError path = '/some/path' error = ConfigModuleNotValidError(path) assert isinstance(error, ConfigError) assert error.message == f'config module not valid: {path}' assert error.path == path error = ConfigModuleNotValidError(path=path) assert error.path == path def test_PropertyNotFoundError(): from configaro import ConfigError, ConfigPropertyNotFoundError data = None prop_name = 'prop.inner' error = ConfigPropertyNotFoundError(data, prop_name) assert isinstance(error, ConfigError) assert error.message == f'config property not found: {prop_name}' assert error.data == data assert error.prop_name == prop_name error = ConfigPropertyNotFoundError(prop_name=prop_name, data=data) assert error.data == data assert error.prop_name == prop_name def test_PropertyNotScalarError(): from configaro import ConfigError, ConfigPropertyNotScalarError data = None prop_name = 'prop.inner' error = ConfigPropertyNotScalarError(data, prop_name) assert isinstance(error, ConfigError) assert error.message == f'config property not scalar: {prop_name}' assert error.data == data assert error.prop_name == prop_name error = ConfigPropertyNotScalarError(prop_name=prop_name, data=data) assert error.data == data assert error.prop_name == prop_name def test_UpdateNotValidError(): from configaro import ConfigError, ConfigUpdateNotValidError update = 'prop=value' error = ConfigUpdateNotValidError(update) assert isinstance(error, ConfigError) assert error.message == f'config update not valid: {update}' assert error.update == update error = ConfigUpdateNotValidError(update=update) assert error.update == update ", https://github.com/mortele/OccamTools,26c47709f6b870c89403f03ade0950de9f301639,test/test_occam_data.py::test_occam_data_not_save_to_npy,test/test_occam_data.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest import numpy as np import warnings import shutil from occamtools.read_fort1 import Fort1 from occamtools.read_fort7 import Fort7 from occamtools.read_xyz import Xyz, _are_floats from occamtools.occam_data import (OccamData, _check_internal_consistency_all, _check_internal_consistency) file_name_fort_1 = os.path.join(os.path.dirname(__file__), os.pardir, 'data', 'fort.1') file_name_fort_7 = os.path.join(os.path.dirname(__file__), os.pardir, 'data', 'fort.7') file_name_fort_xyz = os.path.join(os.path.dirname(__file__), os.pardir, 'data', 'fort.8') class_dir = os.path.join(os.path.dirname(__file__), os.pardir, 'data', 'class_data') ignore = ['file_name', 'n_time_steps_', 'file_contents', 'comment_format_known', 'num_lines'] def _load_default_forts(silent=True): fort1 = Fort1(file_name_fort_1) fort1.read_file(silent=silent) fort7 = Fort7(file_name_fort_7) fort7.read_file(silent=silent) xyz = Xyz(file_name_fort_xyz) xyz.read_file(silent=silent) return fort1, fort7, xyz def _create_default_occam_data_object(load_from_npy=False, silent=True): fort1, fort7, xyz = _load_default_forts(silent=silent) occam_data = OccamData(fort1, fort7, xyz, load_from_npy=load_from_npy, silent=silent) return occam_data, fort1, fort7, xyz def _assert_all_attributes_present(occam_data, fort1, fort7, xyz): for f in (fort1, fort7, xyz): for key in f.__dict__: assert key in occam_data.__dict__ if key not in ignore else True def _check_equal(a, b): if _are_floats(a, b): return float(a) == pytest.approx(float(b), abs=1e-14) elif isinstance(a, str) and isinstance(b, str): return a == b elif hasattr(a, 'shape') and hasattr(b, 'shape'): return np.allclose(a, b) else: return a == b def _assert_all_attributes_equal(occam_data, fort1, fort7, xyz): for f in (fort1, fort7, xyz): for key in f.__dict__: if key not in ignore: assert _check_equal(f.__dict__[key], occam_data.__dict__[key]) def _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz): _assert_all_attributes_present(occam_data, fort1, fort7, xyz) _assert_all_attributes_equal(occam_data, fort1, fort7, xyz) def test_occam_data_check_internal_consistency_1_7(): fort1, fort7, xyz = _load_default_forts() c17 = _check_internal_consistency(fort1, fort7) assert c17 c1x = _check_internal_consistency(fort1, xyz) assert c1x c7x = _check_internal_consistency(fort7, xyz) assert c7x all_ = _check_internal_consistency_all(fort1, fort7, xyz) assert all_ assert (c17 and c1x and c7x) is all_ def test_occam_data_constructor_files(): fort1, fort7, xyz = _load_default_forts() occam_data = OccamData(fort1.file_name, fort7.file_name, xyz.file_name, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) occam_data = OccamData(fort1.file_name, None, None, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) occam_data = OccamData(None, fort7.file_name, None, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) occam_data = OccamData(None, None, xyz.file_name, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) def test_occam_data_attributes(): occam_data, fort1, fort7, xyz = _create_default_occam_data_object() _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) def test_occam_data_wrong_input(): caught = False try: _ = OccamData(None, None, None, silent=True) except ValueError: caught = True assert caught def test_occam_data_not_equal(): for i in range(2): fort1, fort7, xyz = _load_default_forts() if i == 0: fort1.n_particles += 1 else: fort1.title = 'changed' warnings.filterwarnings('ignore') fort1.n_particles += 1 occam_data = OccamData(fort1, fort7, xyz, silent=True) assert occam_data.consistent is False warnings.filterwarnings('error') caught = False try: _ = OccamData(fort1, fort7, xyz, silent=True) except Warning: caught = True assert caught warnings.filterwarnings('always') def test_occam_data_single_input(): fort1, fort7, xyz = _load_default_forts() occam_data = OccamData(fort1.file_name, load_from_npy=False, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) occam_data = OccamData(fort7.file_name, load_from_npy=False, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) occam_data = OccamData(xyz.file_name, load_from_npy=False, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) occam_data = OccamData(os.path.dirname(fort1.file_name), load_from_npy=False, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) inputs = ['this_is_not_a_file', None, 1, 8.29898, fort1, fort7, xyz] for inp in inputs: caught = False try: _ = OccamData(inp, load_from_npy=False, silent=True) except TypeError: caught = True except FileNotFoundError: caught = True assert caught def test_occam_data_save_load(): shutil.rmtree(class_dir, ignore_errors=True) occam_data, fort1, fort7, xyz = _create_default_occam_data_object() all_attributes = [key for key in occam_data.__dict__] attributes = [] for key in all_attributes: if isinstance(occam_data.__dict__[key], np.ndarray): attributes.append(key) occam_data.save() for array in attributes: expected_file_name = array + '.npy' assert os.path.exists(os.path.join(class_dir, expected_file_name)) for key in all_attributes: assert key in occam_data.__dict__ occam_data_npy_loaded = OccamData(os.path.join(class_dir, os.pardir), silent=True) for key in all_attributes: assert key in occam_data_npy_loaded.__dict__ assert not occam_data_npy_loaded.save(overwrite=False) occam_data_npy_loaded_file = OccamData(os.path.join(class_dir, os.pardir, 'fort.1'), silent=True) _assert_all_attributes_present_and_equal(occam_data_npy_loaded_file, fort1, fort7, xyz) caught = False try: _ = OccamData('this_is_not_a_file', silent=True) except FileNotFoundError: caught = True assert caught shutil.rmtree(class_dir) def test_occam_data_progress_bars(): fort1, fort7, fort8 = _load_default_forts() occam_data_silent = OccamData(fort1, fort7, fort8) fort1, fort7, fort8 = _load_default_forts(silent=False) occam_data_verbose = OccamData(fort1, fort7, fort8) for key in occam_data_silent.__dict__: assert _check_equal(occam_data_silent.__dict__[key], occam_data_verbose.__dict__[key]) occam_data_verbose.save() occam_data_verbose_npy = OccamData(file_name_fort_1, load_from_npy=False, silent=True) occam_data_verbose_npy = OccamData(file_name_fort_1, load_from_npy=True, silent=False) for key in occam_data_silent.__dict__: assert _check_equal(occam_data_silent.__dict__[key], occam_data_verbose_npy.__dict__[key]) shutil.rmtree(class_dir) def test_occam_data_not_save_to_npy(): assert not os.path.exists(class_dir) _ = OccamData(file_name_fort_1, save_to_npy=False, silent=True) assert not os.path.exists(class_dir) ", https://github.com/mortele/OccamTools,26c47709f6b870c89403f03ade0950de9f301639,test/test_occam_data.py::test_occam_data_progress_bars,test/test_occam_data.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import pytest import numpy as np import warnings import shutil from occamtools.read_fort1 import Fort1 from occamtools.read_fort7 import Fort7 from occamtools.read_xyz import Xyz, _are_floats from occamtools.occam_data import (OccamData, _check_internal_consistency_all, _check_internal_consistency) file_name_fort_1 = os.path.join(os.path.dirname(__file__), os.pardir, 'data', 'fort.1') file_name_fort_7 = os.path.join(os.path.dirname(__file__), os.pardir, 'data', 'fort.7') file_name_fort_xyz = os.path.join(os.path.dirname(__file__), os.pardir, 'data', 'fort.8') class_dir = os.path.join(os.path.dirname(__file__), os.pardir, 'data', 'class_data') ignore = ['file_name', 'n_time_steps_', 'file_contents', 'comment_format_known', 'num_lines'] def _load_default_forts(silent=True): fort1 = Fort1(file_name_fort_1) fort1.read_file(silent=silent) fort7 = Fort7(file_name_fort_7) fort7.read_file(silent=silent) xyz = Xyz(file_name_fort_xyz) xyz.read_file(silent=silent) return fort1, fort7, xyz def _create_default_occam_data_object(load_from_npy=False, silent=True): fort1, fort7, xyz = _load_default_forts(silent=silent) occam_data = OccamData(fort1, fort7, xyz, load_from_npy=load_from_npy, silent=silent) return occam_data, fort1, fort7, xyz def _assert_all_attributes_present(occam_data, fort1, fort7, xyz): for f in (fort1, fort7, xyz): for key in f.__dict__: assert key in occam_data.__dict__ if key not in ignore else True def _check_equal(a, b): if _are_floats(a, b): return float(a) == pytest.approx(float(b), abs=1e-14) elif isinstance(a, str) and isinstance(b, str): return a == b elif hasattr(a, 'shape') and hasattr(b, 'shape'): return np.allclose(a, b) else: return a == b def _assert_all_attributes_equal(occam_data, fort1, fort7, xyz): for f in (fort1, fort7, xyz): for key in f.__dict__: if key not in ignore: assert _check_equal(f.__dict__[key], occam_data.__dict__[key]) def _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz): _assert_all_attributes_present(occam_data, fort1, fort7, xyz) _assert_all_attributes_equal(occam_data, fort1, fort7, xyz) def test_occam_data_check_internal_consistency_1_7(): fort1, fort7, xyz = _load_default_forts() c17 = _check_internal_consistency(fort1, fort7) assert c17 c1x = _check_internal_consistency(fort1, xyz) assert c1x c7x = _check_internal_consistency(fort7, xyz) assert c7x all_ = _check_internal_consistency_all(fort1, fort7, xyz) assert all_ assert (c17 and c1x and c7x) is all_ def test_occam_data_constructor_files(): fort1, fort7, xyz = _load_default_forts() occam_data = OccamData(fort1.file_name, fort7.file_name, xyz.file_name, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) occam_data = OccamData(fort1.file_name, None, None, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) occam_data = OccamData(None, fort7.file_name, None, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) occam_data = OccamData(None, None, xyz.file_name, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) def test_occam_data_attributes(): occam_data, fort1, fort7, xyz = _create_default_occam_data_object() _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) def test_occam_data_wrong_input(): caught = False try: _ = OccamData(None, None, None, silent=True) except ValueError: caught = True assert caught def test_occam_data_not_equal(): for i in range(2): fort1, fort7, xyz = _load_default_forts() if i == 0: fort1.n_particles += 1 else: fort1.title = 'changed' warnings.filterwarnings('ignore') fort1.n_particles += 1 occam_data = OccamData(fort1, fort7, xyz, silent=True) assert occam_data.consistent is False warnings.filterwarnings('error') caught = False try: _ = OccamData(fort1, fort7, xyz, silent=True) except Warning: caught = True assert caught warnings.filterwarnings('always') def test_occam_data_single_input(): fort1, fort7, xyz = _load_default_forts() occam_data = OccamData(fort1.file_name, load_from_npy=False, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) occam_data = OccamData(fort7.file_name, load_from_npy=False, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) occam_data = OccamData(xyz.file_name, load_from_npy=False, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) occam_data = OccamData(os.path.dirname(fort1.file_name), load_from_npy=False, silent=True) _assert_all_attributes_present_and_equal(occam_data, fort1, fort7, xyz) inputs = ['this_is_not_a_file', None, 1, 8.29898, fort1, fort7, xyz] for inp in inputs: caught = False try: _ = OccamData(inp, load_from_npy=False, silent=True) except TypeError: caught = True except FileNotFoundError: caught = True assert caught def test_occam_data_save_load(): shutil.rmtree(class_dir, ignore_errors=True) occam_data, fort1, fort7, xyz = _create_default_occam_data_object() all_attributes = [key for key in occam_data.__dict__] attributes = [] for key in all_attributes: if isinstance(occam_data.__dict__[key], np.ndarray): attributes.append(key) occam_data.save() for array in attributes: expected_file_name = array + '.npy' assert os.path.exists(os.path.join(class_dir, expected_file_name)) for key in all_attributes: assert key in occam_data.__dict__ occam_data_npy_loaded = OccamData(os.path.join(class_dir, os.pardir), silent=True) for key in all_attributes: assert key in occam_data_npy_loaded.__dict__ assert not occam_data_npy_loaded.save(overwrite=False) occam_data_npy_loaded_file = OccamData(os.path.join(class_dir, os.pardir, 'fort.1'), silent=True) _assert_all_attributes_present_and_equal(occam_data_npy_loaded_file, fort1, fort7, xyz) caught = False try: _ = OccamData('this_is_not_a_file', silent=True) except FileNotFoundError: caught = True assert caught shutil.rmtree(class_dir) def test_occam_data_progress_bars(): fort1, fort7, fort8 = _load_default_forts() occam_data_silent = OccamData(fort1, fort7, fort8) fort1, fort7, fort8 = _load_default_forts(silent=False) occam_data_verbose = OccamData(fort1, fort7, fort8) for key in occam_data_silent.__dict__: assert _check_equal(occam_data_silent.__dict__[key], occam_data_verbose.__dict__[key]) occam_data_verbose.save() occam_data_verbose_npy = OccamData(file_name_fort_1, load_from_npy=False, silent=True) occam_data_verbose_npy = OccamData(file_name_fort_1, load_from_npy=True, silent=False) for key in occam_data_silent.__dict__: assert _check_equal(occam_data_silent.__dict__[key], occam_data_verbose_npy.__dict__[key]) shutil.rmtree(class_dir) def test_occam_data_not_save_to_npy(): assert not os.path.exists(class_dir) _ = OccamData(file_name_fort_1, save_to_npy=False, silent=True) assert not os.path.exists(class_dir) ", https://github.com/moyasar/moyasar-python,aa8716ae3f98f8782141abe6869dfbb90af98b62,tests/moyasar_test.py::test_request_should_return_json_object,tests/moyasar_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import moyasar import tests.fixtures.fixtures as f import tests.server_stubs as ss def test_that_it_has_a_version_number(): assert moyasar.api_version is not '' def test_should_accept_api_key(): moyasar.api_key = 'sk_test_BQokikJOvBiI2HlWgH4olfQ2' assert moyasar.api_key is not '' def test_request_should_return_json_object(): ss.stub_server_request(method='GET', url=moyasar.api_url + '/payments', resource=f.payments, status=200) response = moyasar.request('GET', moyasar.api_url + '/payments', None) assert isinstance(response.json(), dict) ", https://github.com/mozilla/PyPOM,9cb84df9d27b428b4e7423d1bbe6502e92990154,tests/test_driver.py::test_register_driver,tests/test_driver.py,NIO,flaky,RepoArchived,nan,classify;root_cause,"# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. def test_register_driver(): """""" We are testing the registerDriver hook"""""" import pytest from zope.interface import implementer from zope.interface import Interface from pypom.driver import registerDriver from pypom.interfaces import IDriver class IFakeDriver(Interface): """""" A fake marker interface"""""" @implementer(IFakeDriver) class FakeDriver: """""" A fake driver """""" def __init__(self, driver): self.driver = driver fake_driver = FakeDriver(None) # no register driver, look up error with pytest.raises(TypeError): IDriver(fake_driver) registerDriver(IFakeDriver, FakeDriver) # driver implementation available after registerDriver adapted_driver = IDriver(fake_driver) # same instance of adapted_driver assert isinstance(adapted_driver, FakeDriver) def test_multiple_register_driver(): """""" We are testing the registerDriver hook, multiple registrations"""""" from zope.interface import implementer from zope.interface import Interface from pypom.driver import registerDriver from pypom.interfaces import IDriver class IFakeDriver(Interface): """""" A fake marker interface"""""" @implementer(IFakeDriver) class FakeDriver: """""" A fake driver """""" def __init__(self, driver): self.driver = driver class IFakeDriver2(Interface): """""" Another fake marker interface"""""" @implementer(IFakeDriver2) class FakeDriver2: """""" Another fake driver """""" def __init__(self, driver): self.driver = driver fake_driver = FakeDriver(None) fake_driver2 = FakeDriver2(None) registerDriver(IFakeDriver, FakeDriver) registerDriver(IFakeDriver2, IFakeDriver2) # driver implementation available after registerDriver adapted_driver = IDriver(fake_driver) adapted_driver2 = IDriver(fake_driver2) # same instance of adapted_driver assert isinstance(adapted_driver, FakeDriver) assert isinstance(adapted_driver2, FakeDriver2) def test_register_driver_class_implements(): """""" We are testing the registerDriver hook with class implements"""""" from zope.interface import Interface from pypom.driver import registerDriver from pypom.interfaces import IDriver class IFakeDriver(Interface): """""" A fake marker interface"""""" class FakeDriver: """""" A fake driver """""" def __init__(self, driver): self.driver = driver fake_driver = FakeDriver(None) registerDriver(IFakeDriver, FakeDriver, [FakeDriver]) # driver implementation available after registerDriver adapted_driver = IDriver(fake_driver) # same instance of adapted_driver assert isinstance(adapted_driver, FakeDriver) ", https://github.com/mrmechko/neon,42ad78e3d79583a93abce6d06b81dafc29017514,tests/basic_tests.py::test_dont_memoize,tests/basic_tests.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from neon.normalize import auto_assign from neon.memoize import memoize, MEMO class Foo: @auto_assign(""test"") def __init__(self, test): pass def test_auto_assign(): """"""tests that a test exists"""""" foo = Foo(""Thing"") assert hasattr(foo, ""test"") assert foo.test == ""Thing"" @memoize def fib(i): if i < 2: return 1 return fib(i-1) + fib(i-2) def test_memoize(): assert fib(0) == 1 assert fib(1) == 1 assert fib(2) == 2 assert fib(28) == 514229 assert fib(29) == (2**3)*5*11*31*61 @memoize def foo(bar): return bar def test_dont_memoize(): assert foo({}) == {} assert foo not in MEMO assert any([m.__name__ == ""fib"" for m in MEMO]) ", https://github.com/mrob95/Breathe,4600818e24f4156cd7bb8cc0f43886b27323968e,tests/test_command_context.py::test_manual_context_noccr,tests/test_command_context.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from .testutils import DoNothing import pytest from dragonfly import get_engine, MimicFailure, AppContext from breathe import Breathe, CommandContext engine = get_engine(""text"") def test_manual_context(): Breathe.add_commands( CommandContext(""test""), {""pizza"": DoNothing(), ""curry"": DoNothing(), } ) # Fails because the rule isn't enabled yet with pytest.raises(MimicFailure): engine.mimic([""pizza"", ""pizza""]) engine.mimic([""enable"", ""test""]) engine.mimic([""pizza"", ""curry"", ""pizza""]) def test_manual_context_noccr(): Breathe.add_commands( CommandContext(""test"") | AppContext(""italy""), {""spaghetti"": DoNothing()}, ccr=False ) # Loaded rule should be referencing the original # ""test"" context loaded above, which should already be # active engine.mimic([""spaghetti""]) engine.mimic([""disable"", ""test""]) with pytest.raises(MimicFailure): engine.mimic([""spaghetti""]) engine.mimic([""pizza"", ""curry""]) engine.mimic([""spaghetti""], executable=""italy"") def test_negated_context(): Breathe.add_commands( ~(CommandContext(""america"") | AppContext(""england"")), {""steak"": DoNothing(), } ) engine.mimic([""steak""]) with pytest.raises(MimicFailure): engine.mimic([""steak""], executable=""england"") engine.mimic([""enable"", ""america""]) with pytest.raises(MimicFailure): engine.mimic([""steak""]) def test_clear(): Breathe.clear()", https://github.com/mrob95/Breathe,4600818e24f4156cd7bb8cc0f43886b27323968e,tests/test_loading.py::test_loading,tests/test_loading.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from .testutils import DoNothing import pytest, os from dragonfly import get_engine, MimicFailure, AppContext from breathe import Breathe, CommandContext from six import PY2 engine = get_engine(""text"") script_dir = os.path.dirname(__file__) file_path = os.path.join(script_dir, ""my_grammar/fruit.py"") def test_loading_failure(): with open(file_path, ""w"") as f: f.write("""""" from breathe import Breathe from ..testutils import DoNothing Breathe.add_commands(,,, None, { ""apple"": DoNothing(), } ) """""" ) modules = { ""tests"": { ""my_grammar"": [""fruit""], } } Breathe.load_modules(modules) assert len(Breathe.modules) == 1 assert len(Breathe.core_commands) == 0 def test_loading(): with open(file_path, ""w"") as f: f.write("""""" from breathe import Breathe from ..testutils import DoNothing Breathe.add_commands( None, { ""apple"": DoNothing(), } ) """""" ) engine.mimic(""rebuild everything test"") engine.mimic(""apple"") def test_reloading(): with open(file_path, ""w"") as f: f.write("""""" from breathe import Breathe from ..testutils import DoNothing Breathe.add_commands( None, { ""parsnip"": DoNothing(), } ) """""" ) # I have no idea why this is necessary, it's a total hack if PY2: os.remove(file_path + ""c"") engine.mimic(""rebuild everything test"") with pytest.raises(MimicFailure): engine.mimic(""apple"") engine.mimic(""parsnip"") assert len(Breathe.modules) == 1 def test_clear(): Breathe.clear() Breathe.modules = []", https://github.com/mrob95/Breathe,3ebcdfc9c011f1b64f00ae320f4704b78783f751,tests/test_loading.py::test_loading_failure,tests/test_loading.py,OD,flaky,Accepted,https://github.com/mrob95/Breathe/pull/10,classify;root_cause,"from .testutils import DoNothing import pytest, os from dragonfly import get_engine, MimicFailure, AppContext from breathe import Breathe, CommandContext from six import PY2 engine = get_engine(""text"") script_dir = os.path.dirname(__file__) file_path = os.path.join(script_dir, ""my_grammar/fruit.py"") def test_loading_failure(): with open(file_path, ""w"") as f: f.write("""""" from breathe import Breathe from ..testutils import DoNothing Breathe.add_commands(,,, None, { ""apple"": DoNothing(), } ) """""" ) modules = { ""tests"": { ""my_grammar"": [""fruit""], } } Breathe.load_modules(modules) assert len(Breathe.modules) == 1 assert len(Breathe.core_commands) == 0 def test_loading(): with open(file_path, ""w"") as f: f.write("""""" from breathe import Breathe from ..testutils import DoNothing Breathe.add_commands( None, { ""apple"": DoNothing(), } ) """""" ) engine.mimic(""rebuild everything test"") engine.mimic(""apple"") def test_reloading(): with open(file_path, ""w"") as f: f.write("""""" from breathe import Breathe from ..testutils import DoNothing Breathe.add_commands( None, { ""parsnip"": DoNothing(), } ) """""" ) # I have no idea why this is necessary, it's a total hack if PY2: os.remove(file_path + ""c"") engine.mimic(""rebuild everything test"") with pytest.raises(MimicFailure): engine.mimic(""apple"") engine.mimic(""parsnip"") assert len(Breathe.modules) == 1 def test_clear(): Breathe.clear() Breathe.modules = []", https://github.com/mrob95/Breathe,4600818e24f4156cd7bb8cc0f43886b27323968e,tests/test_loading.py::test_loading_failure,tests/test_loading.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from .testutils import DoNothing import pytest, os from dragonfly import get_engine, MimicFailure, AppContext from breathe import Breathe, CommandContext from six import PY2 engine = get_engine(""text"") script_dir = os.path.dirname(__file__) file_path = os.path.join(script_dir, ""my_grammar/fruit.py"") def test_loading_failure(): with open(file_path, ""w"") as f: f.write("""""" from breathe import Breathe from ..testutils import DoNothing Breathe.add_commands(,,, None, { ""apple"": DoNothing(), } ) """""" ) modules = { ""tests"": { ""my_grammar"": [""fruit""], } } Breathe.load_modules(modules) assert len(Breathe.modules) == 1 assert len(Breathe.core_commands) == 0 def test_loading(): with open(file_path, ""w"") as f: f.write("""""" from breathe import Breathe from ..testutils import DoNothing Breathe.add_commands( None, { ""apple"": DoNothing(), } ) """""" ) engine.mimic(""rebuild everything test"") engine.mimic(""apple"") def test_reloading(): with open(file_path, ""w"") as f: f.write("""""" from breathe import Breathe from ..testutils import DoNothing Breathe.add_commands( None, { ""parsnip"": DoNothing(), } ) """""" ) # I have no idea why this is necessary, it's a total hack if PY2: os.remove(file_path + ""c"") engine.mimic(""rebuild everything test"") with pytest.raises(MimicFailure): engine.mimic(""apple"") engine.mimic(""parsnip"") assert len(Breathe.modules) == 1 def test_clear(): Breathe.clear() Breathe.modules = []", https://github.com/mrob95/Breathe,4600818e24f4156cd7bb8cc0f43886b27323968e,tests/test_loading.py::test_reloading,tests/test_loading.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from .testutils import DoNothing import pytest, os from dragonfly import get_engine, MimicFailure, AppContext from breathe import Breathe, CommandContext from six import PY2 engine = get_engine(""text"") script_dir = os.path.dirname(__file__) file_path = os.path.join(script_dir, ""my_grammar/fruit.py"") def test_loading_failure(): with open(file_path, ""w"") as f: f.write("""""" from breathe import Breathe from ..testutils import DoNothing Breathe.add_commands(,,, None, { ""apple"": DoNothing(), } ) """""" ) modules = { ""tests"": { ""my_grammar"": [""fruit""], } } Breathe.load_modules(modules) assert len(Breathe.modules) == 1 assert len(Breathe.core_commands) == 0 def test_loading(): with open(file_path, ""w"") as f: f.write("""""" from breathe import Breathe from ..testutils import DoNothing Breathe.add_commands( None, { ""apple"": DoNothing(), } ) """""" ) engine.mimic(""rebuild everything test"") engine.mimic(""apple"") def test_reloading(): with open(file_path, ""w"") as f: f.write("""""" from breathe import Breathe from ..testutils import DoNothing Breathe.add_commands( None, { ""parsnip"": DoNothing(), } ) """""" ) # I have no idea why this is necessary, it's a total hack if PY2: os.remove(file_path + ""c"") engine.mimic(""rebuild everything test"") with pytest.raises(MimicFailure): engine.mimic(""apple"") engine.mimic(""parsnip"") assert len(Breathe.modules) == 1 def test_clear(): Breathe.clear() Breathe.modules = []", https://github.com/mrob95/Breathe,4600818e24f4156cd7bb8cc0f43886b27323968e,tests/test_merger.py::test_context_commands,tests/test_merger.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from dragonfly import ( get_engine, Dictation, IntegerRef, MimicFailure, AppContext, Choice, Repeat, ) from .testutils import DoNothing import pytest from breathe import Breathe, CommandContext engine = get_engine(""text"") def test_global_extras(): Breathe.add_global_extras(Dictation(""text"")) assert len(Breathe.global_extras) == 1 assert ""text"" in Breathe.global_extras Breathe.add_global_extras([Choice(""abc"", {""def"": ""ghi""})]) # Check that this is overridden Breathe.add_global_extras(IntegerRef(""n"", 1, 2, 1)) def test_core_commands(): Breathe.add_commands( None, { ""test one"": DoNothing(), ""test two"": DoNothing(), ""test three"": DoNothing(), ""banana []"": DoNothing() * Repeat(""n""), }, [IntegerRef(""n"", 1, 10, 1)], ) engine.mimic([""test"", ""three"", ""test"", ""two"", ""banana"", ""five""]) def test_context_commands(): Breathe.add_commands( AppContext(""notepad""), {""test []"": lambda num: DoNothing().execute()}, [Choice(""num"", {""four"": ""4"", ""five"": ""5"", ""six"": ""6""})], {""num"": """"}, ) with pytest.raises(MimicFailure): engine.mimic([""test"", ""three"", ""test"", ""four""]) engine.mimic([""test"", ""three"", ""test"", ""four""], executable=""notepad"") def test_noccr_commands(): Breathe.add_commands( AppContext(""firefox""), {""dictation "": DoNothing(), ""testing static"": DoNothing()}, ccr=False, ) engine.mimic([""testing"", ""static""], executable=""firefox"") with pytest.raises(MimicFailure): engine.mimic([""dictation"", ""TESTING""]) engine.mimic([""testing"", ""static"", ""testing"", ""static""], executable=""firefox"") engine.mimic([""dictation"", ""TESTING""], executable=""firefox"") def test_grammar_numbers(): engine.mimic([""test"", ""three""]) # Ensure that we are not adding more grammars than necessary assert len(engine.grammars) == 4 def test_nomapping_commands(): Breathe.add_commands(AppContext(""code.exe""), {}) def test_invalid(): Breathe.add_commands( AppContext(""code.exe""), { ""test that "": DoNothing(), 1: DoNothing(), }, ) assert len(Breathe.contexts) == 1 assert len(Breathe.context_commands) == 1 def test_kaldi_weight_passthrough(): Breathe.add_commands( None, { ""test weight"": DoNothing(), }, weight=10.0, ) assert Breathe.core_commands[-1].weight == 10.0 # This should probably be done as set up and tear down, eh def test_clear(): Breathe.clear() ", https://github.com/mrob95/Breathe,4600818e24f4156cd7bb8cc0f43886b27323968e,tests/test_merger.py::test_core_commands,tests/test_merger.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from dragonfly import ( get_engine, Dictation, IntegerRef, MimicFailure, AppContext, Choice, Repeat, ) from .testutils import DoNothing import pytest from breathe import Breathe, CommandContext engine = get_engine(""text"") def test_global_extras(): Breathe.add_global_extras(Dictation(""text"")) assert len(Breathe.global_extras) == 1 assert ""text"" in Breathe.global_extras Breathe.add_global_extras([Choice(""abc"", {""def"": ""ghi""})]) # Check that this is overridden Breathe.add_global_extras(IntegerRef(""n"", 1, 2, 1)) def test_core_commands(): Breathe.add_commands( None, { ""test one"": DoNothing(), ""test two"": DoNothing(), ""test three"": DoNothing(), ""banana []"": DoNothing() * Repeat(""n""), }, [IntegerRef(""n"", 1, 10, 1)], ) engine.mimic([""test"", ""three"", ""test"", ""two"", ""banana"", ""five""]) def test_context_commands(): Breathe.add_commands( AppContext(""notepad""), {""test []"": lambda num: DoNothing().execute()}, [Choice(""num"", {""four"": ""4"", ""five"": ""5"", ""six"": ""6""})], {""num"": """"}, ) with pytest.raises(MimicFailure): engine.mimic([""test"", ""three"", ""test"", ""four""]) engine.mimic([""test"", ""three"", ""test"", ""four""], executable=""notepad"") def test_noccr_commands(): Breathe.add_commands( AppContext(""firefox""), {""dictation "": DoNothing(), ""testing static"": DoNothing()}, ccr=False, ) engine.mimic([""testing"", ""static""], executable=""firefox"") with pytest.raises(MimicFailure): engine.mimic([""dictation"", ""TESTING""]) engine.mimic([""testing"", ""static"", ""testing"", ""static""], executable=""firefox"") engine.mimic([""dictation"", ""TESTING""], executable=""firefox"") def test_grammar_numbers(): engine.mimic([""test"", ""three""]) # Ensure that we are not adding more grammars than necessary assert len(engine.grammars) == 4 def test_nomapping_commands(): Breathe.add_commands(AppContext(""code.exe""), {}) def test_invalid(): Breathe.add_commands( AppContext(""code.exe""), { ""test that "": DoNothing(), 1: DoNothing(), }, ) assert len(Breathe.contexts) == 1 assert len(Breathe.context_commands) == 1 def test_kaldi_weight_passthrough(): Breathe.add_commands( None, { ""test weight"": DoNothing(), }, weight=10.0, ) assert Breathe.core_commands[-1].weight == 10.0 # This should probably be done as set up and tear down, eh def test_clear(): Breathe.clear() ", https://github.com/mrob95/Breathe,3ebcdfc9c011f1b64f00ae320f4704b78783f751,tests/test_merger.py::test_global_extras,tests/test_merger.py,OD,flaky,DevelopersDoNotWantFix,https://github.com/mrob95/Breathe/pull/11,classify;root_cause,"from dragonfly import ( get_engine, Dictation, IntegerRef, MimicFailure, AppContext, Choice, Repeat, ) from .testutils import DoNothing import pytest from breathe import Breathe, CommandContext engine = get_engine(""text"") def test_global_extras(): Breathe.add_global_extras(Dictation(""text"")) assert len(Breathe.global_extras) == 1 assert ""text"" in Breathe.global_extras Breathe.add_global_extras([Choice(""abc"", {""def"": ""ghi""})]) # Check that this is overridden Breathe.add_global_extras(IntegerRef(""n"", 1, 2, 1)) def test_core_commands(): Breathe.add_commands( None, { ""test one"": DoNothing(), ""test two"": DoNothing(), ""test three"": DoNothing(), ""banana []"": DoNothing() * Repeat(""n""), }, [IntegerRef(""n"", 1, 10, 1)], ) engine.mimic([""test"", ""three"", ""test"", ""two"", ""banana"", ""five""]) def test_context_commands(): Breathe.add_commands( AppContext(""notepad""), {""test []"": lambda num: DoNothing().execute()}, [Choice(""num"", {""four"": ""4"", ""five"": ""5"", ""six"": ""6""})], {""num"": """"}, ) with pytest.raises(MimicFailure): engine.mimic([""test"", ""three"", ""test"", ""four""]) engine.mimic([""test"", ""three"", ""test"", ""four""], executable=""notepad"") def test_noccr_commands(): Breathe.add_commands( AppContext(""firefox""), {""dictation "": DoNothing(), ""testing static"": DoNothing()}, ccr=False, ) engine.mimic([""testing"", ""static""], executable=""firefox"") with pytest.raises(MimicFailure): engine.mimic([""dictation"", ""TESTING""]) engine.mimic([""testing"", ""static"", ""testing"", ""static""], executable=""firefox"") engine.mimic([""dictation"", ""TESTING""], executable=""firefox"") def test_grammar_numbers(): engine.mimic([""test"", ""three""]) # Ensure that we are not adding more grammars than necessary assert len(engine.grammars) == 4 def test_nomapping_commands(): Breathe.add_commands(AppContext(""code.exe""), {}) def test_invalid(): Breathe.add_commands( AppContext(""code.exe""), { ""test that "": DoNothing(), 1: DoNothing(), }, ) assert len(Breathe.contexts) == 1 assert len(Breathe.context_commands) == 1 def test_kaldi_weight_passthrough(): Breathe.add_commands( None, { ""test weight"": DoNothing(), }, weight=10.0, ) assert Breathe.core_commands[-1].weight == 10.0 # This should probably be done as set up and tear down, eh def test_clear(): Breathe.clear() ", https://github.com/mrob95/Breathe,3ebcdfc9c011f1b64f00ae320f4704b78783f751,tests/test_merger.py::test_grammar_numbers,tests/test_merger.py,OD,flaky,DevelopersDoNotWantFix,https://github.com/mrob95/Breathe/pull/11,classify;root_cause,"from dragonfly import ( get_engine, Dictation, IntegerRef, MimicFailure, AppContext, Choice, Repeat, ) from .testutils import DoNothing import pytest from breathe import Breathe, CommandContext engine = get_engine(""text"") def test_global_extras(): Breathe.add_global_extras(Dictation(""text"")) assert len(Breathe.global_extras) == 1 assert ""text"" in Breathe.global_extras Breathe.add_global_extras([Choice(""abc"", {""def"": ""ghi""})]) # Check that this is overridden Breathe.add_global_extras(IntegerRef(""n"", 1, 2, 1)) def test_core_commands(): Breathe.add_commands( None, { ""test one"": DoNothing(), ""test two"": DoNothing(), ""test three"": DoNothing(), ""banana []"": DoNothing() * Repeat(""n""), }, [IntegerRef(""n"", 1, 10, 1)], ) engine.mimic([""test"", ""three"", ""test"", ""two"", ""banana"", ""five""]) def test_context_commands(): Breathe.add_commands( AppContext(""notepad""), {""test []"": lambda num: DoNothing().execute()}, [Choice(""num"", {""four"": ""4"", ""five"": ""5"", ""six"": ""6""})], {""num"": """"}, ) with pytest.raises(MimicFailure): engine.mimic([""test"", ""three"", ""test"", ""four""]) engine.mimic([""test"", ""three"", ""test"", ""four""], executable=""notepad"") def test_noccr_commands(): Breathe.add_commands( AppContext(""firefox""), {""dictation "": DoNothing(), ""testing static"": DoNothing()}, ccr=False, ) engine.mimic([""testing"", ""static""], executable=""firefox"") with pytest.raises(MimicFailure): engine.mimic([""dictation"", ""TESTING""]) engine.mimic([""testing"", ""static"", ""testing"", ""static""], executable=""firefox"") engine.mimic([""dictation"", ""TESTING""], executable=""firefox"") def test_grammar_numbers(): engine.mimic([""test"", ""three""]) # Ensure that we are not adding more grammars than necessary assert len(engine.grammars) == 4 def test_nomapping_commands(): Breathe.add_commands(AppContext(""code.exe""), {}) def test_invalid(): Breathe.add_commands( AppContext(""code.exe""), { ""test that "": DoNothing(), 1: DoNothing(), }, ) assert len(Breathe.contexts) == 1 assert len(Breathe.context_commands) == 1 def test_kaldi_weight_passthrough(): Breathe.add_commands( None, { ""test weight"": DoNothing(), }, weight=10.0, ) assert Breathe.core_commands[-1].weight == 10.0 # This should probably be done as set up and tear down, eh def test_clear(): Breathe.clear() ", https://github.com/mrob95/Breathe,4600818e24f4156cd7bb8cc0f43886b27323968e,tests/test_merger.py::test_grammar_numbers,tests/test_merger.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from dragonfly import ( get_engine, Dictation, IntegerRef, MimicFailure, AppContext, Choice, Repeat, ) from .testutils import DoNothing import pytest from breathe import Breathe, CommandContext engine = get_engine(""text"") def test_global_extras(): Breathe.add_global_extras(Dictation(""text"")) assert len(Breathe.global_extras) == 1 assert ""text"" in Breathe.global_extras Breathe.add_global_extras([Choice(""abc"", {""def"": ""ghi""})]) # Check that this is overridden Breathe.add_global_extras(IntegerRef(""n"", 1, 2, 1)) def test_core_commands(): Breathe.add_commands( None, { ""test one"": DoNothing(), ""test two"": DoNothing(), ""test three"": DoNothing(), ""banana []"": DoNothing() * Repeat(""n""), }, [IntegerRef(""n"", 1, 10, 1)], ) engine.mimic([""test"", ""three"", ""test"", ""two"", ""banana"", ""five""]) def test_context_commands(): Breathe.add_commands( AppContext(""notepad""), {""test []"": lambda num: DoNothing().execute()}, [Choice(""num"", {""four"": ""4"", ""five"": ""5"", ""six"": ""6""})], {""num"": """"}, ) with pytest.raises(MimicFailure): engine.mimic([""test"", ""three"", ""test"", ""four""]) engine.mimic([""test"", ""three"", ""test"", ""four""], executable=""notepad"") def test_noccr_commands(): Breathe.add_commands( AppContext(""firefox""), {""dictation "": DoNothing(), ""testing static"": DoNothing()}, ccr=False, ) engine.mimic([""testing"", ""static""], executable=""firefox"") with pytest.raises(MimicFailure): engine.mimic([""dictation"", ""TESTING""]) engine.mimic([""testing"", ""static"", ""testing"", ""static""], executable=""firefox"") engine.mimic([""dictation"", ""TESTING""], executable=""firefox"") def test_grammar_numbers(): engine.mimic([""test"", ""three""]) # Ensure that we are not adding more grammars than necessary assert len(engine.grammars) == 4 def test_nomapping_commands(): Breathe.add_commands(AppContext(""code.exe""), {}) def test_invalid(): Breathe.add_commands( AppContext(""code.exe""), { ""test that "": DoNothing(), 1: DoNothing(), }, ) assert len(Breathe.contexts) == 1 assert len(Breathe.context_commands) == 1 def test_kaldi_weight_passthrough(): Breathe.add_commands( None, { ""test weight"": DoNothing(), }, weight=10.0, ) assert Breathe.core_commands[-1].weight == 10.0 # This should probably be done as set up and tear down, eh def test_clear(): Breathe.clear() ", https://github.com/mrob95/Breathe,4600818e24f4156cd7bb8cc0f43886b27323968e,tests/test_merger.py::test_invalid,tests/test_merger.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from dragonfly import ( get_engine, Dictation, IntegerRef, MimicFailure, AppContext, Choice, Repeat, ) from .testutils import DoNothing import pytest from breathe import Breathe, CommandContext engine = get_engine(""text"") def test_global_extras(): Breathe.add_global_extras(Dictation(""text"")) assert len(Breathe.global_extras) == 1 assert ""text"" in Breathe.global_extras Breathe.add_global_extras([Choice(""abc"", {""def"": ""ghi""})]) # Check that this is overridden Breathe.add_global_extras(IntegerRef(""n"", 1, 2, 1)) def test_core_commands(): Breathe.add_commands( None, { ""test one"": DoNothing(), ""test two"": DoNothing(), ""test three"": DoNothing(), ""banana []"": DoNothing() * Repeat(""n""), }, [IntegerRef(""n"", 1, 10, 1)], ) engine.mimic([""test"", ""three"", ""test"", ""two"", ""banana"", ""five""]) def test_context_commands(): Breathe.add_commands( AppContext(""notepad""), {""test []"": lambda num: DoNothing().execute()}, [Choice(""num"", {""four"": ""4"", ""five"": ""5"", ""six"": ""6""})], {""num"": """"}, ) with pytest.raises(MimicFailure): engine.mimic([""test"", ""three"", ""test"", ""four""]) engine.mimic([""test"", ""three"", ""test"", ""four""], executable=""notepad"") def test_noccr_commands(): Breathe.add_commands( AppContext(""firefox""), {""dictation "": DoNothing(), ""testing static"": DoNothing()}, ccr=False, ) engine.mimic([""testing"", ""static""], executable=""firefox"") with pytest.raises(MimicFailure): engine.mimic([""dictation"", ""TESTING""]) engine.mimic([""testing"", ""static"", ""testing"", ""static""], executable=""firefox"") engine.mimic([""dictation"", ""TESTING""], executable=""firefox"") def test_grammar_numbers(): engine.mimic([""test"", ""three""]) # Ensure that we are not adding more grammars than necessary assert len(engine.grammars) == 4 def test_nomapping_commands(): Breathe.add_commands(AppContext(""code.exe""), {}) def test_invalid(): Breathe.add_commands( AppContext(""code.exe""), { ""test that "": DoNothing(), 1: DoNothing(), }, ) assert len(Breathe.contexts) == 1 assert len(Breathe.context_commands) == 1 def test_kaldi_weight_passthrough(): Breathe.add_commands( None, { ""test weight"": DoNothing(), }, weight=10.0, ) assert Breathe.core_commands[-1].weight == 10.0 # This should probably be done as set up and tear down, eh def test_clear(): Breathe.clear() ", https://github.com/mrob95/Breathe,4600818e24f4156cd7bb8cc0f43886b27323968e,tests/test_merger.py::test_noccr_commands,tests/test_merger.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from dragonfly import ( get_engine, Dictation, IntegerRef, MimicFailure, AppContext, Choice, Repeat, ) from .testutils import DoNothing import pytest from breathe import Breathe, CommandContext engine = get_engine(""text"") def test_global_extras(): Breathe.add_global_extras(Dictation(""text"")) assert len(Breathe.global_extras) == 1 assert ""text"" in Breathe.global_extras Breathe.add_global_extras([Choice(""abc"", {""def"": ""ghi""})]) # Check that this is overridden Breathe.add_global_extras(IntegerRef(""n"", 1, 2, 1)) def test_core_commands(): Breathe.add_commands( None, { ""test one"": DoNothing(), ""test two"": DoNothing(), ""test three"": DoNothing(), ""banana []"": DoNothing() * Repeat(""n""), }, [IntegerRef(""n"", 1, 10, 1)], ) engine.mimic([""test"", ""three"", ""test"", ""two"", ""banana"", ""five""]) def test_context_commands(): Breathe.add_commands( AppContext(""notepad""), {""test []"": lambda num: DoNothing().execute()}, [Choice(""num"", {""four"": ""4"", ""five"": ""5"", ""six"": ""6""})], {""num"": """"}, ) with pytest.raises(MimicFailure): engine.mimic([""test"", ""three"", ""test"", ""four""]) engine.mimic([""test"", ""three"", ""test"", ""four""], executable=""notepad"") def test_noccr_commands(): Breathe.add_commands( AppContext(""firefox""), {""dictation "": DoNothing(), ""testing static"": DoNothing()}, ccr=False, ) engine.mimic([""testing"", ""static""], executable=""firefox"") with pytest.raises(MimicFailure): engine.mimic([""dictation"", ""TESTING""]) engine.mimic([""testing"", ""static"", ""testing"", ""static""], executable=""firefox"") engine.mimic([""dictation"", ""TESTING""], executable=""firefox"") def test_grammar_numbers(): engine.mimic([""test"", ""three""]) # Ensure that we are not adding more grammars than necessary assert len(engine.grammars) == 4 def test_nomapping_commands(): Breathe.add_commands(AppContext(""code.exe""), {}) def test_invalid(): Breathe.add_commands( AppContext(""code.exe""), { ""test that "": DoNothing(), 1: DoNothing(), }, ) assert len(Breathe.contexts) == 1 assert len(Breathe.context_commands) == 1 def test_kaldi_weight_passthrough(): Breathe.add_commands( None, { ""test weight"": DoNothing(), }, weight=10.0, ) assert Breathe.core_commands[-1].weight == 10.0 # This should probably be done as set up and tear down, eh def test_clear(): Breathe.clear() ", https://github.com/mrob95/Breathe,3ebcdfc9c011f1b64f00ae320f4704b78783f751,tests/test_top_level.py::test_recognition,tests/test_top_level.py,OD,flaky,DevelopersDoNotWantFix,https://github.com/mrob95/Breathe/pull/11,classify;root_cause,"from dragonfly import ( get_engine, Dictation, IntegerRef, MimicFailure, AppContext, Choice, Repeat, ) from .testutils import DoNothing import pytest from breathe import Breathe, CommandContext from breathe.elements import CommandsRef, Exec import warnings engine = get_engine(""text"") def test_top_level_command(): Breathe.add_commands(None, {""orange"": DoNothing(), ""grapefruit"": DoNothing()}) Breathe.add_commands( AppContext(""notepad""), {""lemon"": DoNothing(), ""banana"": DoNothing()} ) Breathe.add_commands( AppContext(""notepad""), { ""fruit from and [] []"": DoNothing() + Exec(""sequence1"") + DoNothing() + Exec(""sequence2"")* Repeat(""n"") }, extras=[CommandsRef(""sequence1""), CommandsRef(""sequence2"", 2), IntegerRef(""n"", 1, 10, 1)], top_level=True, ) def test_top_level_command2(): Breathe.add_commands( AppContext(title=""chrome""), {""pear"": DoNothing(), ""grape"": DoNothing()} ) def test_global_top_level(): Breathe.add_commands( None, { "" are preferable to "": DoNothing() + Exec(""sequence1"") + DoNothing() + Exec(""sequence2"") }, extras=[CommandsRef(""sequence1""), CommandsRef(""sequence2"", 3)], top_level=True, ) def test_recognition(): engine.mimic(""lemon"", executable=""notepad"") engine.mimic(""fruit from lemon banana orange and five"", executable=""notepad"") engine.mimic( ""fruit from pear banana orange and grapefruit"", executable=""notepad"", title=""chrome"", ) with pytest.raises(MimicFailure): engine.mimic( ""fruit from pear banana orange and grapefruit"", executable=""notepad"" ) engine.mimic(""orange grapefruit are preferable to grapefruit"") engine.mimic(""orange grapefruit are preferable to lemon banana"", executable=""notepad"") assert len(Breathe.top_level_commands) == 2 def test_top_level_command_failure(): Breathe.add_commands( AppContext(""china""), { ""not marked top level and []"": DoNothing() + Exec(""sequence1"") + DoNothing() + Exec(""sequence2"")* Repeat(""n"") }, extras=[CommandsRef(""sequence1""), CommandsRef(""sequence2"", 2), IntegerRef(""n"", 1, 10, 1)], top_level=False, ) assert len(Breathe.top_level_commands) == 2 def test_clear(): Breathe.clear() ", https://github.com/mrob95/Breathe,4600818e24f4156cd7bb8cc0f43886b27323968e,tests/test_top_level.py::test_recognition,tests/test_top_level.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from dragonfly import ( get_engine, Dictation, IntegerRef, MimicFailure, AppContext, Choice, Repeat, ) from .testutils import DoNothing import pytest from breathe import Breathe, CommandContext from breathe.elements import CommandsRef, Exec import warnings engine = get_engine(""text"") def test_top_level_command(): Breathe.add_commands(None, {""orange"": DoNothing(), ""grapefruit"": DoNothing()}) Breathe.add_commands( AppContext(""notepad""), {""lemon"": DoNothing(), ""banana"": DoNothing()} ) Breathe.add_commands( AppContext(""notepad""), { ""fruit from and [] []"": DoNothing() + Exec(""sequence1"") + DoNothing() + Exec(""sequence2"")* Repeat(""n"") }, extras=[CommandsRef(""sequence1""), CommandsRef(""sequence2"", 2), IntegerRef(""n"", 1, 10, 1)], top_level=True, ) def test_top_level_command2(): Breathe.add_commands( AppContext(title=""chrome""), {""pear"": DoNothing(), ""grape"": DoNothing()} ) def test_global_top_level(): Breathe.add_commands( None, { "" are preferable to "": DoNothing() + Exec(""sequence1"") + DoNothing() + Exec(""sequence2"") }, extras=[CommandsRef(""sequence1""), CommandsRef(""sequence2"", 3)], top_level=True, ) def test_recognition(): engine.mimic(""lemon"", executable=""notepad"") engine.mimic(""fruit from lemon banana orange and five"", executable=""notepad"") engine.mimic( ""fruit from pear banana orange and grapefruit"", executable=""notepad"", title=""chrome"", ) with pytest.raises(MimicFailure): engine.mimic( ""fruit from pear banana orange and grapefruit"", executable=""notepad"" ) engine.mimic(""orange grapefruit are preferable to grapefruit"") engine.mimic(""orange grapefruit are preferable to lemon banana"", executable=""notepad"") assert len(Breathe.top_level_commands) == 2 def test_top_level_command_failure(): Breathe.add_commands( AppContext(""china""), { ""not marked top level and []"": DoNothing() + Exec(""sequence1"") + DoNothing() + Exec(""sequence2"")* Repeat(""n"") }, extras=[CommandsRef(""sequence1""), CommandsRef(""sequence2"", 2), IntegerRef(""n"", 1, 10, 1)], top_level=False, ) assert len(Breathe.top_level_commands) == 2 def test_clear(): Breathe.clear() ", https://github.com/mrob95/Breathe,3ebcdfc9c011f1b64f00ae320f4704b78783f751,tests/test_top_level.py::test_top_level_command_failure,tests/test_top_level.py,OD,flaky,DevelopersDoNotWantFix,https://github.com/mrob95/Breathe/pull/11,classify;root_cause,"from dragonfly import ( get_engine, Dictation, IntegerRef, MimicFailure, AppContext, Choice, Repeat, ) from .testutils import DoNothing import pytest from breathe import Breathe, CommandContext from breathe.elements import CommandsRef, Exec import warnings engine = get_engine(""text"") def test_top_level_command(): Breathe.add_commands(None, {""orange"": DoNothing(), ""grapefruit"": DoNothing()}) Breathe.add_commands( AppContext(""notepad""), {""lemon"": DoNothing(), ""banana"": DoNothing()} ) Breathe.add_commands( AppContext(""notepad""), { ""fruit from and [] []"": DoNothing() + Exec(""sequence1"") + DoNothing() + Exec(""sequence2"")* Repeat(""n"") }, extras=[CommandsRef(""sequence1""), CommandsRef(""sequence2"", 2), IntegerRef(""n"", 1, 10, 1)], top_level=True, ) def test_top_level_command2(): Breathe.add_commands( AppContext(title=""chrome""), {""pear"": DoNothing(), ""grape"": DoNothing()} ) def test_global_top_level(): Breathe.add_commands( None, { "" are preferable to "": DoNothing() + Exec(""sequence1"") + DoNothing() + Exec(""sequence2"") }, extras=[CommandsRef(""sequence1""), CommandsRef(""sequence2"", 3)], top_level=True, ) def test_recognition(): engine.mimic(""lemon"", executable=""notepad"") engine.mimic(""fruit from lemon banana orange and five"", executable=""notepad"") engine.mimic( ""fruit from pear banana orange and grapefruit"", executable=""notepad"", title=""chrome"", ) with pytest.raises(MimicFailure): engine.mimic( ""fruit from pear banana orange and grapefruit"", executable=""notepad"" ) engine.mimic(""orange grapefruit are preferable to grapefruit"") engine.mimic(""orange grapefruit are preferable to lemon banana"", executable=""notepad"") assert len(Breathe.top_level_commands) == 2 def test_top_level_command_failure(): Breathe.add_commands( AppContext(""china""), { ""not marked top level and []"": DoNothing() + Exec(""sequence1"") + DoNothing() + Exec(""sequence2"")* Repeat(""n"") }, extras=[CommandsRef(""sequence1""), CommandsRef(""sequence2"", 2), IntegerRef(""n"", 1, 10, 1)], top_level=False, ) assert len(Breathe.top_level_commands) == 2 def test_clear(): Breathe.clear() ", https://github.com/mrob95/Breathe,4600818e24f4156cd7bb8cc0f43886b27323968e,tests/test_top_level.py::test_top_level_command_failure,tests/test_top_level.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from dragonfly import ( get_engine, Dictation, IntegerRef, MimicFailure, AppContext, Choice, Repeat, ) from .testutils import DoNothing import pytest from breathe import Breathe, CommandContext from breathe.elements import CommandsRef, Exec import warnings engine = get_engine(""text"") def test_top_level_command(): Breathe.add_commands(None, {""orange"": DoNothing(), ""grapefruit"": DoNothing()}) Breathe.add_commands( AppContext(""notepad""), {""lemon"": DoNothing(), ""banana"": DoNothing()} ) Breathe.add_commands( AppContext(""notepad""), { ""fruit from and [] []"": DoNothing() + Exec(""sequence1"") + DoNothing() + Exec(""sequence2"")* Repeat(""n"") }, extras=[CommandsRef(""sequence1""), CommandsRef(""sequence2"", 2), IntegerRef(""n"", 1, 10, 1)], top_level=True, ) def test_top_level_command2(): Breathe.add_commands( AppContext(title=""chrome""), {""pear"": DoNothing(), ""grape"": DoNothing()} ) def test_global_top_level(): Breathe.add_commands( None, { "" are preferable to "": DoNothing() + Exec(""sequence1"") + DoNothing() + Exec(""sequence2"") }, extras=[CommandsRef(""sequence1""), CommandsRef(""sequence2"", 3)], top_level=True, ) def test_recognition(): engine.mimic(""lemon"", executable=""notepad"") engine.mimic(""fruit from lemon banana orange and five"", executable=""notepad"") engine.mimic( ""fruit from pear banana orange and grapefruit"", executable=""notepad"", title=""chrome"", ) with pytest.raises(MimicFailure): engine.mimic( ""fruit from pear banana orange and grapefruit"", executable=""notepad"" ) engine.mimic(""orange grapefruit are preferable to grapefruit"") engine.mimic(""orange grapefruit are preferable to lemon banana"", executable=""notepad"") assert len(Breathe.top_level_commands) == 2 def test_top_level_command_failure(): Breathe.add_commands( AppContext(""china""), { ""not marked top level and []"": DoNothing() + Exec(""sequence1"") + DoNothing() + Exec(""sequence2"")* Repeat(""n"") }, extras=[CommandsRef(""sequence1""), CommandsRef(""sequence2"", 2), IntegerRef(""n"", 1, 10, 1)], top_level=False, ) assert len(Breathe.top_level_commands) == 2 def test_clear(): Breathe.clear() ", https://github.com/mtik00/yamicache,1109d08b18be94bdee55e113309f7be29f25d840,tests/test_class.py::test_cached,tests/test_class.py,OD-Vic,flaky,nan,nan,classify;root_cause,"from __future__ import print_function import sys import time import pytest from yamicache import Cache, nocache, override_timeout if sys.version_info[0] == 2: range = xrange c = Cache(prefix='myapp', hashing=False, debug=False) class MyApp(object): @c.cached() def test1(self, argument, power): '''running test1''' return argument ** power @c.cached() def test2(self): '''running test2''' return 1 @c.cached(key='asdf') def test3(self, argument, power): '''running test3''' return argument ** power def test4(self): '''running test4''' return 4 @c.cached() def cant_cache(self): print(""here"") @pytest.fixture def cache_obj(): m = MyApp() return m def test_cached(cache_obj): for _ in range(10): cache_obj.test1(8, 0) assert len(c) == 1 assert cache_obj.test1(8, 0) == 1 for _ in range(10): cache_obj.test2() assert cache_obj.test2() == 1 assert len(c) == 2 c.clear() assert len(c) == 0 # Make sure the cached function is properly wrapped assert cache_obj.test2.__doc__ == 'running test2' def test_keyed_cached(cache_obj): for _ in range(10): cache_obj.test3(8, 0) cache_obj.test4() # Shouldn't be cached assert len(c) == 1 key = list(c.keys())[0] assert key == 'asdf' c.clear() assert len(c) == 0 # Make sure the cached function is properly wrapped assert cache_obj.test3.__doc__ == 'running test3' def test_utility(cache_obj): for _ in range(10): cache_obj.test1(8, 0) cache_obj.test1(8, 2) cache_obj.test1(8, 2) # Already cached cache_obj.test2() cache_obj.test3(8, 2) assert len(c) == 4 assert c.dump() != '{}' key = list(c.keys())[0] c.pop(key) assert len(c) == 3 assert key not in c assert len(c.keys()) == 3 assert len(c.values()) == 3 assert c.items() c.clear() assert not c.items() assert not c.keys() assert not c.values() assert not len(c) assert c.dump() == '{}' def test_counters(cache_obj): c.clear() c._debug = True for _ in range(10): cache_obj.test3(8, 2) assert len(c.counters) == 1 assert c.counters['asdf'] == 9 print(c.dump()) c.counters.clear() c.clear() def test_nocache(cache_obj): c.clear() c._debug = False assert len(c.counters) == 0 assert len(c) == 0 with nocache(c): for _ in range(10): cache_obj.test3(8, 2) cache_obj.test4() assert len(c.counters) == 0 assert len(c) == 0 def test_timeout(cache_obj): c.clear() c._debug = True cache_obj.test3(8, 2) cache_obj.test3(8, 2) time.sleep(1) c.collect() assert len(c) == 1 c.collect(since=time.time() - 20) assert len(c) == 0 with override_timeout(c, 1): cache_obj.test3(8, 2) cache_obj.test3(8, 2) assert len(c) == 1 time.sleep(1.5) c.collect() assert len(c) == 0 c.clear() # Test a call where the cache has timed out. # For this test, we want to load the cache with our specified timeout # value. Then wait longer than the timeout, and run the function again. # The hit counter should remain the same, since we didn't read the value # from cache. with override_timeout(c, 1): cache_obj.test3(8, 2) cache_obj.test3(8, 2) assert len(c.counters) == 1 before_count = list(c.counters.values())[0] assert len(c) == 1 time.sleep(1.5) cache_obj.test3(8, 2) # should be a new cache w/o the counter incrementing assert len(c) == 1 assert list(c.counters.values())[0] == before_count def test_prefix(cache_obj): c.clear() cache_obj.test1(8, 0) key = list(c.keys())[0] assert key.startswith('myapp|') def main(): # test_utility(MyApp()) # test_nocache(MyApp()) # test_cached(MyApp()) test_timeout(MyApp()) if __name__ == '__main__': main() ", https://github.com/mtik00/yamicache,1109d08b18be94bdee55e113309f7be29f25d840,tests/test_class.py::test_keyed_cached,tests/test_class.py,OD-Vic,flaky,nan,nan,classify;root_cause,"from __future__ import print_function import sys import time import pytest from yamicache import Cache, nocache, override_timeout if sys.version_info[0] == 2: range = xrange c = Cache(prefix='myapp', hashing=False, debug=False) class MyApp(object): @c.cached() def test1(self, argument, power): '''running test1''' return argument ** power @c.cached() def test2(self): '''running test2''' return 1 @c.cached(key='asdf') def test3(self, argument, power): '''running test3''' return argument ** power def test4(self): '''running test4''' return 4 @c.cached() def cant_cache(self): print(""here"") @pytest.fixture def cache_obj(): m = MyApp() return m def test_cached(cache_obj): for _ in range(10): cache_obj.test1(8, 0) assert len(c) == 1 assert cache_obj.test1(8, 0) == 1 for _ in range(10): cache_obj.test2() assert cache_obj.test2() == 1 assert len(c) == 2 c.clear() assert len(c) == 0 # Make sure the cached function is properly wrapped assert cache_obj.test2.__doc__ == 'running test2' def test_keyed_cached(cache_obj): for _ in range(10): cache_obj.test3(8, 0) cache_obj.test4() # Shouldn't be cached assert len(c) == 1 key = list(c.keys())[0] assert key == 'asdf' c.clear() assert len(c) == 0 # Make sure the cached function is properly wrapped assert cache_obj.test3.__doc__ == 'running test3' def test_utility(cache_obj): for _ in range(10): cache_obj.test1(8, 0) cache_obj.test1(8, 2) cache_obj.test1(8, 2) # Already cached cache_obj.test2() cache_obj.test3(8, 2) assert len(c) == 4 assert c.dump() != '{}' key = list(c.keys())[0] c.pop(key) assert len(c) == 3 assert key not in c assert len(c.keys()) == 3 assert len(c.values()) == 3 assert c.items() c.clear() assert not c.items() assert not c.keys() assert not c.values() assert not len(c) assert c.dump() == '{}' def test_counters(cache_obj): c.clear() c._debug = True for _ in range(10): cache_obj.test3(8, 2) assert len(c.counters) == 1 assert c.counters['asdf'] == 9 print(c.dump()) c.counters.clear() c.clear() def test_nocache(cache_obj): c.clear() c._debug = False assert len(c.counters) == 0 assert len(c) == 0 with nocache(c): for _ in range(10): cache_obj.test3(8, 2) cache_obj.test4() assert len(c.counters) == 0 assert len(c) == 0 def test_timeout(cache_obj): c.clear() c._debug = True cache_obj.test3(8, 2) cache_obj.test3(8, 2) time.sleep(1) c.collect() assert len(c) == 1 c.collect(since=time.time() - 20) assert len(c) == 0 with override_timeout(c, 1): cache_obj.test3(8, 2) cache_obj.test3(8, 2) assert len(c) == 1 time.sleep(1.5) c.collect() assert len(c) == 0 c.clear() # Test a call where the cache has timed out. # For this test, we want to load the cache with our specified timeout # value. Then wait longer than the timeout, and run the function again. # The hit counter should remain the same, since we didn't read the value # from cache. with override_timeout(c, 1): cache_obj.test3(8, 2) cache_obj.test3(8, 2) assert len(c.counters) == 1 before_count = list(c.counters.values())[0] assert len(c) == 1 time.sleep(1.5) cache_obj.test3(8, 2) # should be a new cache w/o the counter incrementing assert len(c) == 1 assert list(c.counters.values())[0] == before_count def test_prefix(cache_obj): c.clear() cache_obj.test1(8, 0) key = list(c.keys())[0] assert key.startswith('myapp|') def main(): # test_utility(MyApp()) # test_nocache(MyApp()) # test_cached(MyApp()) test_timeout(MyApp()) if __name__ == '__main__': main() ", https://github.com/mtik00/yamicache,1109d08b18be94bdee55e113309f7be29f25d840,tests/test_class.py::test_utility,tests/test_class.py,OD-Vic,flaky,nan,nan,classify;root_cause,"from __future__ import print_function import sys import time import pytest from yamicache import Cache, nocache, override_timeout if sys.version_info[0] == 2: range = xrange c = Cache(prefix='myapp', hashing=False, debug=False) class MyApp(object): @c.cached() def test1(self, argument, power): '''running test1''' return argument ** power @c.cached() def test2(self): '''running test2''' return 1 @c.cached(key='asdf') def test3(self, argument, power): '''running test3''' return argument ** power def test4(self): '''running test4''' return 4 @c.cached() def cant_cache(self): print(""here"") @pytest.fixture def cache_obj(): m = MyApp() return m def test_cached(cache_obj): for _ in range(10): cache_obj.test1(8, 0) assert len(c) == 1 assert cache_obj.test1(8, 0) == 1 for _ in range(10): cache_obj.test2() assert cache_obj.test2() == 1 assert len(c) == 2 c.clear() assert len(c) == 0 # Make sure the cached function is properly wrapped assert cache_obj.test2.__doc__ == 'running test2' def test_keyed_cached(cache_obj): for _ in range(10): cache_obj.test3(8, 0) cache_obj.test4() # Shouldn't be cached assert len(c) == 1 key = list(c.keys())[0] assert key == 'asdf' c.clear() assert len(c) == 0 # Make sure the cached function is properly wrapped assert cache_obj.test3.__doc__ == 'running test3' def test_utility(cache_obj): for _ in range(10): cache_obj.test1(8, 0) cache_obj.test1(8, 2) cache_obj.test1(8, 2) # Already cached cache_obj.test2() cache_obj.test3(8, 2) assert len(c) == 4 assert c.dump() != '{}' key = list(c.keys())[0] c.pop(key) assert len(c) == 3 assert key not in c assert len(c.keys()) == 3 assert len(c.values()) == 3 assert c.items() c.clear() assert not c.items() assert not c.keys() assert not c.values() assert not len(c) assert c.dump() == '{}' def test_counters(cache_obj): c.clear() c._debug = True for _ in range(10): cache_obj.test3(8, 2) assert len(c.counters) == 1 assert c.counters['asdf'] == 9 print(c.dump()) c.counters.clear() c.clear() def test_nocache(cache_obj): c.clear() c._debug = False assert len(c.counters) == 0 assert len(c) == 0 with nocache(c): for _ in range(10): cache_obj.test3(8, 2) cache_obj.test4() assert len(c.counters) == 0 assert len(c) == 0 def test_timeout(cache_obj): c.clear() c._debug = True cache_obj.test3(8, 2) cache_obj.test3(8, 2) time.sleep(1) c.collect() assert len(c) == 1 c.collect(since=time.time() - 20) assert len(c) == 0 with override_timeout(c, 1): cache_obj.test3(8, 2) cache_obj.test3(8, 2) assert len(c) == 1 time.sleep(1.5) c.collect() assert len(c) == 0 c.clear() # Test a call where the cache has timed out. # For this test, we want to load the cache with our specified timeout # value. Then wait longer than the timeout, and run the function again. # The hit counter should remain the same, since we didn't read the value # from cache. with override_timeout(c, 1): cache_obj.test3(8, 2) cache_obj.test3(8, 2) assert len(c.counters) == 1 before_count = list(c.counters.values())[0] assert len(c) == 1 time.sleep(1.5) cache_obj.test3(8, 2) # should be a new cache w/o the counter incrementing assert len(c) == 1 assert list(c.counters.values())[0] == before_count def test_prefix(cache_obj): c.clear() cache_obj.test1(8, 0) key = list(c.keys())[0] assert key.startswith('myapp|') def main(): # test_utility(MyApp()) # test_nocache(MyApp()) # test_cached(MyApp()) test_timeout(MyApp()) if __name__ == '__main__': main() ", https://github.com/mtik00/yamicache,1109d08b18be94bdee55e113309f7be29f25d840,tests/test_collide.py::test_avoid_collision,tests/test_collide.py,NIO,flaky,nan,nan,classify;root_cause,"from __future__ import print_function import pytest from yamicache import Cache c = Cache(prefix='myapp', hashing=False, debug=False) class App1(object): @c.cached() def test1(self, argument, power): '''running test1''' return argument ** power @c.cached(key='test') def test2(self): return 0 class App2(object): @c.cached() def test1(self, argument, power): '''running test1''' return argument ** power def test_avoid_collision(): '''Make sure cache keys don't collide''' a1 = App1() a2 = App2() assert len(c) == 0 a1.test1(1, 2) assert len(c) == 1 a2.test1(1, 2) assert len(c) == 2 print(c.dump()) def test_raises(): '''Ensure same key raises ValueError''' with pytest.raises(ValueError): @c.cached(key='test') def test2(self): return 0 def main(): test_raises() if __name__ == '__main__': main() ", https://github.com/mtik00/yamicache,1109d08b18be94bdee55e113309f7be29f25d840,tests/test_gc.py::test_deco_timeout,tests/test_gc.py,NOD,flaky,nan,nan,classify;root_cause,"from __future__ import print_function import sys import time import pytest from yamicache import Cache, override_timeout if sys.version_info[0] == 2: range = xrange c = Cache(prefix='myapp', hashing=False, debug=False, default_timeout=1, gc_thread_wait=0.5) @pytest.fixture def cache_obj(): m = MyApp() return m class MyApp(object): @c.cached() def test1(self, argument, power): '''running test1''' return argument ** power @c.cached(timeout=2) def test2(self): '''running test2''' return 3 def test_deco_timeout(cache_obj): '''Test custom decorator timeout''' c.clear() # Cache the result, which should use a 2s timeout, as opposed to the # default of 1s. tstart = time.time() cache_obj.test2() # Wait up to 5s for the GC thread to clear `test2()`. while len(c) and ((time.time() - tstart) < 5): time.sleep(0.1) # Give GC a chance to run tend = time.time() # The defined timeout is 2, and gc_thread_wait is 0.5, so the max we # should really be waiting is 2.5 (ish). The mininum is 2-ish. # NOTE: I've had a hell of time with duration variance on Travis-CI, which # is why the range is so big. time_diff = tend - tstart print('actual time: %s' % time_diff) assert 1.4 < time_diff < 3.5 def test_gc(cache_obj): for _ in range(10): cache_obj.test1(8, 0) assert len(c) == 1 # Let the GC thread collect the single cached item time.sleep(2) assert len(c) == 0 def test_gc2(cache_obj): '''Make sure the GC doesn't constantly run''' c.clear() with override_timeout(c, 3): cache_obj.test1(8, 0) item_added = c._from_timestamp(list(c.values())[0].time_added) # Should be cached... assert len(c) == 1 time.sleep(1) # Should still be cached... assert len(c) == 1 # Wait for the GC thread to do its thing t_start = time.time() t_end = t_start + 5 while time.time() < t_end: if not len(c): print(""took %s to clear"" % (time.time() - t_start)) break time.sleep(0.5) # Should be cleared assert len(c) == 0 # Make sure it's been at least 3 seconds since the item was added assert time.time() - item_added >= 3 def main(): test_deco_timeout(MyApp()) if __name__ == '__main__': main() ", https://github.com/mtik00/yamicache,1109d08b18be94bdee55e113309f7be29f25d840,tests/test_noclass.py::test_main,tests/test_noclass.py,NIO,flaky,Accepted,https://github.com/mtik00/yamicache/pull/10,classify;root_cause;fix_proposal,"''' Just a quick test to make sure there's no ``cls`` or ``self`` odness. ''' from __future__ import print_function import sys import time from yamicache import Cache if sys.version_info[0] == 2: range = xrange c = Cache(prefix='myapp', hashing=False, debug=False) @c.cached() def my_func(argument, power): '''running my_func''' return argument ** power @c.cached() def return_list(index): mylists = { 0: [1, 2, 3], 1: [4, 5, 6] } return mylists[index] def test_main(): assert len(c) == 0 for _ in [0, 1, 2, 3, 4, 5]: my_func(2, 3) assert len(c) == 1 def test_lists(): '''Make sure lists are returned''' assert return_list(0) == [1, 2, 3] assert return_list(0) == [1, 2, 3] assert return_list(1) == [4, 5, 6] assert return_list(1) == [4, 5, 6] class MyObject(object): def __init__(self, number): self.number = number @c.cached(timeout=1) def return_object_list(): return [MyObject(0), MyObject(1), MyObject(2)] def test_object_list(): '''Test a result with a timeout & objects are returned''' result = return_object_list() assert result[0].number == 0 assert result[1].number == 1 assert result[2].number == 2 time.sleep(2) result = return_object_list() assert result[0].number == 0 assert result[1].number == 1 assert result[2].number == 2 if __name__ == '__main__': test_main() ", https://github.com/mtik00/yamicache,1109d08b18be94bdee55e113309f7be29f25d840,tests/test_serialization.py::test_serialization,tests/test_serialization.py,NIO,flaky,Accepted,https://github.com/mtik00/yamicache/pull/14,classify;root_cause;fix_proposal,"from __future__ import print_function import os import sys import tempfile import pytest from yamicache import Cache try: import cPickle as pickle except ImportError: import pickle # noqa: F401 if sys.version_info[0] == 2: range = xrange c = Cache(prefix='myapp', hashing=False, debug=False) class MyApp(object): @c.cached() def test1(self, argument, power): '''running test1''' return argument ** power @c.cached() def test2(self): '''running test2''' return 1 @c.cached(key='asdf') def test3(self, argument, power): '''running test3''' return argument ** power def test4(self): '''running test4''' return 4 @c.cached() def cant_cache(self): print(""here"") @pytest.fixture def cache_obj(): m = MyApp() return m def test_serialization(cache_obj): for _ in range(10): cache_obj.test1(8, 0) assert len(c) == 1 assert cache_obj.test1(8, 0) == 1 for _ in range(10): cache_obj.test2() assert cache_obj.test2() == 1 assert len(c) == 2 # Dump the cache to a file temp_handle, filepath = tempfile.mkstemp() os.close(temp_handle) c.serialize(filepath) # Store the current obj for comparison orig = c._data_store.copy() c.clear() assert len(c) == 0 c.deserialize(filepath) assert len(c) assert c._data_store == orig os.unlink(filepath) def main(): test_serialization(MyApp()) if __name__ == '__main__': main() ", https://github.com/naorlivne/parse_it,886d7de0e74e67edc136411a5220e35c1149e743,test/test_prase_it.py::BaseTests::test_envvar_defined_false,test/test_prase_it.py,OD-Vic,flaky,nan,nan,classify;root_cause,"from unittest import TestCase, mock from parse_it import ParseIt from parse_it.command_line_args.command_line_args import * from parse_it.envvars.envvars import * from parse_it.file.toml import * from parse_it.file.hcl import * from parse_it.file.yaml import * from parse_it.file.json import * from parse_it.file.env import * from parse_it.file.ini import * from parse_it.file.xml import * from parse_it.file.file_reader import * from parse_it.type_estimate.type_estimate import * import os VALID_FILE_TYPE_EXTENSIONS = [ ""json"", ""yaml"", ""yml"", ""toml"", ""tml"", ""conf"", ""cfg"", ""ini"", ""xml"" ] test_files_location = os.getenv(""TEST_FILES_LOCATION"", ""test_files"") class BaseTests(TestCase): def test_command_line_args_empty(self): reply = read_command_line_arg(""empty_variable"") self.assertIsNone(reply) def test_command_line_arg_defined_false(self): reply = command_line_arg_defined(""empty_variable"") self.assertFalse(reply) def test_envvars_empty(self): reply = read_envvar(""empty_variable"") self.assertIsNone(reply) def test_envvars_string(self): os.environ[""TEST_ENVVAR""] = ""TEST_ENVVAR_VALUE"" reply = read_envvar(""TEST_ENVVAR"") self.assertEqual(reply, ""TEST_ENVVAR_VALUE"") def test_envvars_string_forced_uppercase(self): os.environ[""TEST_ENVVAR_UPPERCASE""] = ""TEST_ENVVAR_UPPERCASE_VALUE"" reply = read_envvar(""test_envvar_uppercase"", force_uppercase=True) self.assertEqual(reply, ""TEST_ENVVAR_UPPERCASE_VALUE"") def test_envvars_string_forced_lowercase(self): os.environ[""test_envvvar_lowercase""] = ""test_envvvar_lowercase_value"" reply = read_envvar(""test_envvvar_lowercase"", force_uppercase=False) self.assertEqual(reply, ""test_envvvar_lowercase_value"") def test_file_reader_read_file(self): reply = read_file(test_files_location + ""/test_read_file"") self.assertEqual(reply, ""it_reads!"") def test_file_reader_read_file_does_not_exist(self): reply = read_file(test_files_location + ""/totally_bogus_file"") self.assertIsNone(reply) def test_file_reader_file_or_folder_read_folder(self): reply = file_or_folder(test_files_location) self.assertEqual(reply, ""folder"") def test_file_reader_file_or_folder_read_file(self): reply = file_or_folder(test_files_location + ""/test.json"") self.assertEqual(reply, ""file"") def test_file_reader_file_or_folder_read_no_file(self): reply = file_or_folder(""/non_existing_path"") self.assertIsNone(reply) def test_file_reader_folder_exists(self): reply = folder_exists(test_files_location) self.assertTrue(reply) def test_file_reader_folder_does_not_exists(self): reply = folder_exists(""totally_bogus_directory"") self.assertFalse(reply) def test_file_reader_strip_trailing_slash(self): reply = strip_trailing_slash(test_files_location + ""/"") self.assertEqual(reply, test_files_location) def test_file_reader_strip_trailing_slash_root_folder(self): reply = strip_trailing_slash(""/"") self.assertEqual(reply, ""/"") def test_file_reader_strip_trailing_slash_no_strip_needed(self): reply = strip_trailing_slash(test_files_location) self.assertEqual(reply, test_files_location) def test_file_reader_file_types_in_folder_folder_does_not_exist_raise_warn(self): with self.assertWarns(Warning): file_types_in_folder(""totally_bogus_folder_location"", VALID_FILE_TYPE_EXTENSIONS) def test_read_envvar_folder_does_not_exist_raise_warn(self): with self.assertWarns(Warning): os.environ[""TEST_CONFIG_FOLDER_NON_EXISTING_ENVVAR""] = ""TEST_CONFIG_FOLDER_NON_EXISTING_ENVVAR"" parser = ParseIt(config_location=""totally_bogus_folder_location"", config_type_priority=[ ""yaml"", ""json"", ""env_vars"" ]) reply = parser.read_configuration_variable(""test_config_folder_non_existing_envvar"") self.assertEqual(reply, ""TEST_CONFIG_FOLDER_NON_EXISTING_ENVVAR"") def test_read_envvar_single_file_config(self): os.environ[""FILE_TYPE""] = ""envvar"" parser = ParseIt(config_location=test_files_location + ""/test.hcl"") reply = parser.read_configuration_variable(""file_type"") self.assertEqual(reply, ""envvar"") def test_read_cli_args_folder_does_not_exist_raise_warn(self): with self.assertWarns(Warning): ParseIt(config_location=""totally_bogus_folder_location"", config_type_priority=[ ""yaml"", ""json"", ""cli_args"" ]) test_args = [""parse_it_mock_script.py"", ""--test_cli_key_no_folder"", ""test_value""] with mock.patch('sys.argv', test_args): parser = ParseIt() reply = parser.read_configuration_variable(""test_cli_key_no_folder"") self.assertEqual(reply, ""test_value"") def test_file_reader_file_types_in_folder(self): reply = file_types_in_folder(test_files_location, VALID_FILE_TYPE_EXTENSIONS) expected_reply = { 'json': [ 'test.json', 'test_subfolder_1/test_sub_subfolder_2/test_subfolder_2.json', 'test_subfolder_1/test_sub_subfolder_3/test_subfolder_3.json', 'test_subfolder_1/test_subfolder_1.json' ], 'yaml': [ 'test.yaml' ], 'yml': [], 'toml': [ 'test.toml' ], 'tml': [], 'conf': [], 'cfg': [], 'ini': [ 'test.ini' ], 'xml': [ 'test.xml' ] } self.assertDictEqual(reply, expected_reply) def test_ini(self): reply = parse_ini_file(test_files_location + ""/test.ini"") expected_reply = { 'DEFAULT': { 'file_type': ""ini"", 'test_string': 'testing', 'test_bool_true': 'true', 'test_bool_false': 'false', 'test_int': '123.0', 'test_float': '123.123', 'test_list': '[""test1"", ""test2"", ""test3""]' }, 'test_ini': { 'test_ini_key': 'test_ini_value' } } self.assertDictEqual(reply, expected_reply) def test_json(self): reply = parse_json_file(test_files_location + ""/test.json"") expected_reply = { 'file_type': ""json"", 'test_string': 'testing', 'test_bool_true': True, 'test_bool_false': False, 'test_int': 123, 'test_float': 123.123, 'test_list': [ 'test1', 'test2', 'test3' ], 'test_json': { 'test_json_key': 'test_json_value' } } self.assertDictEqual(reply, expected_reply) def test_env_file(self): reply = parse_env_file(test_files_location + ""/test.env"") expected_reply = { 'file_type': 'env', 'test_string': 'testing', 'test_bool_true': 'true', 'test_bool_false': 'false', 'test_int': '123', 'test_float': '123.123', 'test_list': '[""test1"",""test2"",""test3""]', 'test_env': '{""test_env_key"": ""test_env_value""}'} self.assertDictEqual(reply, expected_reply) def test_hcl(self): reply = parse_hcl_file(test_files_location + ""/test.hcl"") expected_reply = { 'file_type': ""hcl"", 'test_string': 'testing', 'test_bool_true': True, 'test_bool_false': False, 'test_int': 123, 'test_float': 123.123, 'test_dict': { ""hcl_dict_key"": ""hcl_dict_value"" }, 'test_list': [ 'test1', 'test2', 'test3' ], 'test_hcl': { 'test_hcl_name': { 'test_hcl_key': 'test_hcl_value' } } } self.assertDictEqual(reply, expected_reply) def test_xml(self): reply = parse_xml_file(test_files_location + ""/test.xml"") expected_reply = { ""xml_root"": { 'file_type': 'xml', 'test_bool_false': 'false', 'test_bool_true': 'true', 'test_float': '123.123', 'test_int': '123', 'test_xml': { 'test_xml_key': 'test_xml_value' }, 'test_list': { 'element': [ 'test1', 'test2', 'test3' ] }, 'test_string': 'testing' } } self.assertDictEqual(reply, expected_reply) def test_toml(self): reply = parse_toml_file(test_files_location + ""/test.toml"") expected_reply = { 'file_type': ""toml"", 'test_string': 'testing', 'test_bool_true': True, 'test_bool_false': False, 'test_int': 123, 'test_float': 123.123, 'test_list': [ 'test1', 'test2', 'test3' ], 'test_toml': { 'test_toml_key': 'test_toml_value' } } self.assertDictEqual(reply, expected_reply) def test_yaml(self): reply = parse_yaml_file(test_files_location + ""/test.yaml"") expected_reply = { 'file_type': ""yaml"", 'test_string': 'testing', 'test_bool_true': True, ", https://github.com/naorlivne/parse_it,886d7de0e74e67edc136411a5220e35c1149e743,test/test_prase_it.py::BaseTests::test_parser_read_all_configuration_variables_raise_allowed_types_error,test/test_prase_it.py,OD-Brit,flaky,nan,nan,classify;root_cause,"from unittest import TestCase, mock from parse_it import ParseIt from parse_it.command_line_args.command_line_args import * from parse_it.envvars.envvars import * from parse_it.file.toml import * from parse_it.file.hcl import * from parse_it.file.yaml import * from parse_it.file.json import * from parse_it.file.env import * from parse_it.file.ini import * from parse_it.file.xml import * from parse_it.file.file_reader import * from parse_it.type_estimate.type_estimate import * import os VALID_FILE_TYPE_EXTENSIONS = [ ""json"", ""yaml"", ""yml"", ""toml"", ""tml"", ""conf"", ""cfg"", ""ini"", ""xml"" ] test_files_location = os.getenv(""TEST_FILES_LOCATION"", ""test_files"") class BaseTests(TestCase): def test_command_line_args_empty(self): reply = read_command_line_arg(""empty_variable"") self.assertIsNone(reply) def test_command_line_arg_defined_false(self): reply = command_line_arg_defined(""empty_variable"") self.assertFalse(reply) def test_envvars_empty(self): reply = read_envvar(""empty_variable"") self.assertIsNone(reply) def test_envvars_string(self): os.environ[""TEST_ENVVAR""] = ""TEST_ENVVAR_VALUE"" reply = read_envvar(""TEST_ENVVAR"") self.assertEqual(reply, ""TEST_ENVVAR_VALUE"") def test_envvars_string_forced_uppercase(self): os.environ[""TEST_ENVVAR_UPPERCASE""] = ""TEST_ENVVAR_UPPERCASE_VALUE"" reply = read_envvar(""test_envvar_uppercase"", force_uppercase=True) self.assertEqual(reply, ""TEST_ENVVAR_UPPERCASE_VALUE"") def test_envvars_string_forced_lowercase(self): os.environ[""test_envvvar_lowercase""] = ""test_envvvar_lowercase_value"" reply = read_envvar(""test_envvvar_lowercase"", force_uppercase=False) self.assertEqual(reply, ""test_envvvar_lowercase_value"") def test_file_reader_read_file(self): reply = read_file(test_files_location + ""/test_read_file"") self.assertEqual(reply, ""it_reads!"") def test_file_reader_read_file_does_not_exist(self): reply = read_file(test_files_location + ""/totally_bogus_file"") self.assertIsNone(reply) def test_file_reader_file_or_folder_read_folder(self): reply = file_or_folder(test_files_location) self.assertEqual(reply, ""folder"") def test_file_reader_file_or_folder_read_file(self): reply = file_or_folder(test_files_location + ""/test.json"") self.assertEqual(reply, ""file"") def test_file_reader_file_or_folder_read_no_file(self): reply = file_or_folder(""/non_existing_path"") self.assertIsNone(reply) def test_file_reader_folder_exists(self): reply = folder_exists(test_files_location) self.assertTrue(reply) def test_file_reader_folder_does_not_exists(self): reply = folder_exists(""totally_bogus_directory"") self.assertFalse(reply) def test_file_reader_strip_trailing_slash(self): reply = strip_trailing_slash(test_files_location + ""/"") self.assertEqual(reply, test_files_location) def test_file_reader_strip_trailing_slash_root_folder(self): reply = strip_trailing_slash(""/"") self.assertEqual(reply, ""/"") def test_file_reader_strip_trailing_slash_no_strip_needed(self): reply = strip_trailing_slash(test_files_location) self.assertEqual(reply, test_files_location) def test_file_reader_file_types_in_folder_folder_does_not_exist_raise_warn(self): with self.assertWarns(Warning): file_types_in_folder(""totally_bogus_folder_location"", VALID_FILE_TYPE_EXTENSIONS) def test_read_envvar_folder_does_not_exist_raise_warn(self): with self.assertWarns(Warning): os.environ[""TEST_CONFIG_FOLDER_NON_EXISTING_ENVVAR""] = ""TEST_CONFIG_FOLDER_NON_EXISTING_ENVVAR"" parser = ParseIt(config_location=""totally_bogus_folder_location"", config_type_priority=[ ""yaml"", ""json"", ""env_vars"" ]) reply = parser.read_configuration_variable(""test_config_folder_non_existing_envvar"") self.assertEqual(reply, ""TEST_CONFIG_FOLDER_NON_EXISTING_ENVVAR"") def test_read_envvar_single_file_config(self): os.environ[""FILE_TYPE""] = ""envvar"" parser = ParseIt(config_location=test_files_location + ""/test.hcl"") reply = parser.read_configuration_variable(""file_type"") self.assertEqual(reply, ""envvar"") def test_read_cli_args_folder_does_not_exist_raise_warn(self): with self.assertWarns(Warning): ParseIt(config_location=""totally_bogus_folder_location"", config_type_priority=[ ""yaml"", ""json"", ""cli_args"" ]) test_args = [""parse_it_mock_script.py"", ""--test_cli_key_no_folder"", ""test_value""] with mock.patch('sys.argv', test_args): parser = ParseIt() reply = parser.read_configuration_variable(""test_cli_key_no_folder"") self.assertEqual(reply, ""test_value"") def test_file_reader_file_types_in_folder(self): reply = file_types_in_folder(test_files_location, VALID_FILE_TYPE_EXTENSIONS) expected_reply = { 'json': [ 'test.json', 'test_subfolder_1/test_sub_subfolder_2/test_subfolder_2.json', 'test_subfolder_1/test_sub_subfolder_3/test_subfolder_3.json', 'test_subfolder_1/test_subfolder_1.json' ], 'yaml': [ 'test.yaml' ], 'yml': [], 'toml': [ 'test.toml' ], 'tml': [], 'conf': [], 'cfg': [], 'ini': [ 'test.ini' ], 'xml': [ 'test.xml' ] } self.assertDictEqual(reply, expected_reply) def test_ini(self): reply = parse_ini_file(test_files_location + ""/test.ini"") expected_reply = { 'DEFAULT': { 'file_type': ""ini"", 'test_string': 'testing', 'test_bool_true': 'true', 'test_bool_false': 'false', 'test_int': '123.0', 'test_float': '123.123', 'test_list': '[""test1"", ""test2"", ""test3""]' }, 'test_ini': { 'test_ini_key': 'test_ini_value' } } self.assertDictEqual(reply, expected_reply) def test_json(self): reply = parse_json_file(test_files_location + ""/test.json"") expected_reply = { 'file_type': ""json"", 'test_string': 'testing', 'test_bool_true': True, 'test_bool_false': False, 'test_int': 123, 'test_float': 123.123, 'test_list': [ 'test1', 'test2', 'test3' ], 'test_json': { 'test_json_key': 'test_json_value' } } self.assertDictEqual(reply, expected_reply) def test_env_file(self): reply = parse_env_file(test_files_location + ""/test.env"") expected_reply = { 'file_type': 'env', 'test_string': 'testing', 'test_bool_true': 'true', 'test_bool_false': 'false', 'test_int': '123', 'test_float': '123.123', 'test_list': '[""test1"",""test2"",""test3""]', 'test_env': '{""test_env_key"": ""test_env_value""}'} self.assertDictEqual(reply, expected_reply) def test_hcl(self): reply = parse_hcl_file(test_files_location + ""/test.hcl"") expected_reply = { 'file_type': ""hcl"", 'test_string': 'testing', 'test_bool_true': True, 'test_bool_false': False, 'test_int': 123, 'test_float': 123.123, 'test_dict': { ""hcl_dict_key"": ""hcl_dict_value"" }, 'test_list': [ 'test1', 'test2', 'test3' ], 'test_hcl': { 'test_hcl_name': { 'test_hcl_key': 'test_hcl_value' } } } self.assertDictEqual(reply, expected_reply) def test_xml(self): reply = parse_xml_file(test_files_location + ""/test.xml"") expected_reply = { ""xml_root"": { 'file_type': 'xml', 'test_bool_false': 'false', 'test_bool_true': 'true', 'test_float': '123.123', 'test_int': '123', 'test_xml': { 'test_xml_key': 'test_xml_value' }, 'test_list': { 'element': [ 'test1', 'test2', 'test3' ] }, 'test_string': 'testing' } } self.assertDictEqual(reply, expected_reply) def test_toml(self): reply = parse_toml_file(test_files_location + ""/test.toml"") expected_reply = { 'file_type': ""toml"", 'test_string': 'testing', 'test_bool_true': True, 'test_bool_false': False, 'test_int': 123, 'test_float': 123.123, 'test_list': [ 'test1', 'test2', 'test3' ], 'test_toml': { 'test_toml_key': 'test_toml_value' } } self.assertDictEqual(reply, expected_reply) def test_yaml(self): reply = parse_yaml_file(test_files_location + ""/test.yaml"") expected_reply = { 'file_type': ""yaml"", 'test_string': 'testing', 'test_bool_true': True, ", https://github.com/napalm-automation/napalm,0e23a55f52221e528be66c1f06a141f937e842f7,test/pyiosxr/test_iosxr.py::TestIOSXRDevice::test_lock_raises_LockError,test/pyiosxr/test_iosxr.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python # coding=utf-8 """"""Unit tests for pyiosxr, a module to interact with Cisco devices running IOS-XR."""""" import os import sys import time import unittest from lxml import etree as ET from six import binary_type # ~~~ import pyIOSXR modules ~~~ from napalm.pyIOSXR import IOSXR # exceptions from napalm.pyIOSXR.exceptions import LockError from napalm.pyIOSXR.exceptions import UnlockError from napalm.pyIOSXR.exceptions import XMLCLIError from napalm.pyIOSXR.exceptions import CommitError from napalm.pyIOSXR.exceptions import TimeoutError from napalm.pyIOSXR.exceptions import IteratorIDError from napalm.pyIOSXR.exceptions import InvalidInputError from napalm.pyIOSXR.exceptions import InvalidXMLResponse class _MockedNetMikoDevice(object): """""" Defines the minimum attributes necessary to mock a SSH connection using netmiko. """""" def __init__(self): class _MockedParamikoTransport(object): def close(self): pass self.remote_conn = _MockedParamikoTransport() @staticmethod def get_mock_file(command, format=""xml""): filename = ( command.replace( '', """", ) .replace("""", """") .replace(""<"", """") .replace("">"", ""_"") .replace(""/"", """") .replace(""\n"", """") .replace(""."", ""_"") .replace("" "", ""_"") .replace('""', ""_"") .replace(""="", ""_"") .replace(""$"", """") .replace("":"", """") .replace(""!"", """")[:150] ) curr_dir = os.path.dirname(os.path.abspath(__file__)) filename = ""{filename}.{fmt}"".format(filename=filename, fmt=format) fullpath = os.path.join(curr_dir, ""mock"", filename) with open(fullpath) as file_data: return file_data.read() def find_prompt(self): return self.get_mock_file(""\n"", format=""txt"") def send_command( self, command_string, delay_factor=0.1, max_loops=150, strip_prompt=True, strip_command=True, ): return self.get_mock_file(command_string) def send_command_timing(self, command_string, **kvargs): return self.get_mock_file(command_string) def receive_data_generator(self): return ["""", """"] # to have an iteration inside private method _netmiko_recv def send_command_expect( self, command_string, expect_string=None, delay_factor=0.2, max_loops=500, auto_find_prompt=True, strip_prompt=True, strip_command=True, ): # for the moment returns the output from send_command only # this may change in time return self.send_command(command_string) class _MockedIOSXRDevice(IOSXR): """""" Overrides only the very basic methods from the main device driver, that cannot be mocked. """""" def open(self): self.device = _MockedNetMikoDevice() self._cli_prompt = self.device.find_prompt() self._enter_xml_mode() def is_alive(self): return True class TestIOSXRDevice(unittest.TestCase): """""" Tests IOS-XR basic functions. """""" HOSTNAME = ""localhost"" USERNAME = ""vagrant"" PASSWORD = ""vagrant"" PORT = 12205 TIMEOUT = 0.1 # for tests, smaller values are prefferred LOCK = False LOG = sys.stdout MOCK = True def __repr__(self): return ( ""Connected as {user}@{host}:{port}, timeout is {tout}"".format( user=self.USERNAME, host=self.HOSTNAME, port=self.PORT, tout=self.TIMEOUT, ) if not self.MOCK else ""Simulates device behaviour using mocked data."" ) __str__ = __repr__ @classmethod def setUpClass(cls): """""" Opens the connection with the IOS-XR device. """""" if cls.MOCK: __cls = _MockedIOSXRDevice else: __cls = IOSXR cls.device = __cls( cls.HOSTNAME, cls.USERNAME, cls.PASSWORD, port=cls.PORT, lock=cls.LOCK, logfile=cls.LOG, timeout=cls.TIMEOUT, ) cls.device.open() @classmethod def tearDownClass(cls): """""" Closes the connection with the device. """""" cls.device.close() def test_mock_lock_connection_open(self): if self.MOCK: self.device.lock_on_connect = True # because there's one single mock file # and it is already used for the lock test # will tesst if raises LockError on connect self.assertRaises(LockError, self.device.lock) self.device.lock_on_connect = False # enough to see that will try to lock during connect def test_mock_close(self): """"""Testing if unlocking when connection is closed"""""" if self.MOCK: self.device.locked = True self.device.close() self.assertFalse(self.device.locked, msg=""Cannot unlock the DB."") def test_execute_rpc_method(self): """"""Testing private method _execute_rpc"""""" self.assertIsInstance( self.device._execute_rpc( """" ), ET._Element, msg=""Privat emethod _execute_rpc did not return a valid XML object."", ) def test__getttr__show_(self): """"""Testing special attribute __getattr___ against valid show command"""""" self.assertIsInstance( self.device.show_ntp_ass(), str, ""Special attribute __getattr___ did not return a valid string."", ) def test__getttr__show_args(self): """"""Testing special attribute __getattr___ against valid show command with arguments"""""" self.assertIsInstance(self.device.show_ntp(""ass""), str) def test_acquire_xml_agent(self): """"""Testing if able to acquire the XML agent."""""" self.device._lock_xml_agent(time.time()) self.assertTrue(self.device._xml_agent_locker.locked()) self.device._unlock_xml_agent() def test_acquire_locked_agent_raises_timeout_error(self): """"""Testing if trying to acquire the XML agent while locked raises TimeoutError."""""" self.device._lock_xml_agent(time.time()) # acquiring self.assertRaises( TimeoutError, self.device._lock_xml_agent, # trying to acquire again time.time(), ) self.device._unlock_xml_agent() # releasing back def test_release_xml_agent(self): """"""Testing releasing of XML agent."""""" self.device._lock_xml_agent(time.time()) self.assertTrue(self.device._xml_agent_locker.locked()) self.device._unlock_xml_agent() self.assertFalse(self.device._xml_agent_locker.locked()) def test_in_cli_mode(self): """"""Testing the private method _in_cli_mode."""""" self.assertTrue(self.device._in_cli_mode()) def test__getattr_show_config(self): """"""Testing special attribute __getattr___ against valid show config command"""""" self.assertIsInstance(self.device.show_run_ntp(config=True), str) def test__getattr__no_show(self): """"""""Test special attribute __getattr__ agains a no-show command"""""" raised = False try: self.device.configure_exclusive() except AttributeError: raised = True self.assertTrue(raised) def test_make_rpc_call_returns_XML(self): """"""Test if public method make_rpc_call returns str"""""" self.assertIsInstance( self.device.make_rpc_call( """" ), binary_type, ) def test_acquired_xml_agent(self): """""" Testing if raises TimeoutError if the XML agent is alredy acquired and released when exception thrown """""" self.device._lock_xml_agent(time.time()) # acquiring the XML agent self.assertRaises( TimeoutError, self.device.make_rpc_call, """", ) self.assertFalse( self.device._xml_agent_locker.locked() ) # Exception raised => xml agent released def test_try_to_read_till_timeout(self): """"""Testing if will try to read from the device till time out"""""" if self.MOCK: # hard to reproduce without mock data # as this event is not deterministic self.assertRaises( TimeoutError, self.device.make_rpc_call, """" ) def test_multiple_read_attempts_till_timeout(self): """"""Testing if will try to read non-empty replies from the device till time out"""""" if self.MOCK: # hard to reproduce without mock data # as this event is not deterministic self.assertRaises( TimeoutError, self.device.make_rpc_call, """" ) def test_iterator_id_raises_IteratorIDError(self): """"""Testing if reply containing the IteratorID attribute raises IteratorIDError"""""" self.device.load_candidate_config(config=""xml agent tty iteration on size 1"") # minimum iteration size self.device.commit_config(comment=""pyIOSXR-test_xml-agent-iteration-on"") # turning on iteration # and a very small value # requesting something that we know for sure will be a big output self.assertRaises( IteratorIDError, self.device.make_rpc_call, """, https://github.com/napalm-automation/napalm,0e23a55f52221e528be66c1f06a141f937e842f7,test/pyiosxr/test_iosxr.py::TestIOSXRDevice::test_mock_lock_connection_open,test/pyiosxr/test_iosxr.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python # coding=utf-8 """"""Unit tests for pyiosxr, a module to interact with Cisco devices running IOS-XR."""""" import os import sys import time import unittest from lxml import etree as ET from six import binary_type # ~~~ import pyIOSXR modules ~~~ from napalm.pyIOSXR import IOSXR # exceptions from napalm.pyIOSXR.exceptions import LockError from napalm.pyIOSXR.exceptions import UnlockError from napalm.pyIOSXR.exceptions import XMLCLIError from napalm.pyIOSXR.exceptions import CommitError from napalm.pyIOSXR.exceptions import TimeoutError from napalm.pyIOSXR.exceptions import IteratorIDError from napalm.pyIOSXR.exceptions import InvalidInputError from napalm.pyIOSXR.exceptions import InvalidXMLResponse class _MockedNetMikoDevice(object): """""" Defines the minimum attributes necessary to mock a SSH connection using netmiko. """""" def __init__(self): class _MockedParamikoTransport(object): def close(self): pass self.remote_conn = _MockedParamikoTransport() @staticmethod def get_mock_file(command, format=""xml""): filename = ( command.replace( '', """", ) .replace("""", """") .replace(""<"", """") .replace("">"", ""_"") .replace(""/"", """") .replace(""\n"", """") .replace(""."", ""_"") .replace("" "", ""_"") .replace('""', ""_"") .replace(""="", ""_"") .replace(""$"", """") .replace("":"", """") .replace(""!"", """")[:150] ) curr_dir = os.path.dirname(os.path.abspath(__file__)) filename = ""{filename}.{fmt}"".format(filename=filename, fmt=format) fullpath = os.path.join(curr_dir, ""mock"", filename) with open(fullpath) as file_data: return file_data.read() def find_prompt(self): return self.get_mock_file(""\n"", format=""txt"") def send_command( self, command_string, delay_factor=0.1, max_loops=150, strip_prompt=True, strip_command=True, ): return self.get_mock_file(command_string) def send_command_timing(self, command_string, **kvargs): return self.get_mock_file(command_string) def receive_data_generator(self): return ["""", """"] # to have an iteration inside private method _netmiko_recv def send_command_expect( self, command_string, expect_string=None, delay_factor=0.2, max_loops=500, auto_find_prompt=True, strip_prompt=True, strip_command=True, ): # for the moment returns the output from send_command only # this may change in time return self.send_command(command_string) class _MockedIOSXRDevice(IOSXR): """""" Overrides only the very basic methods from the main device driver, that cannot be mocked. """""" def open(self): self.device = _MockedNetMikoDevice() self._cli_prompt = self.device.find_prompt() self._enter_xml_mode() def is_alive(self): return True class TestIOSXRDevice(unittest.TestCase): """""" Tests IOS-XR basic functions. """""" HOSTNAME = ""localhost"" USERNAME = ""vagrant"" PASSWORD = ""vagrant"" PORT = 12205 TIMEOUT = 0.1 # for tests, smaller values are prefferred LOCK = False LOG = sys.stdout MOCK = True def __repr__(self): return ( ""Connected as {user}@{host}:{port}, timeout is {tout}"".format( user=self.USERNAME, host=self.HOSTNAME, port=self.PORT, tout=self.TIMEOUT, ) if not self.MOCK else ""Simulates device behaviour using mocked data."" ) __str__ = __repr__ @classmethod def setUpClass(cls): """""" Opens the connection with the IOS-XR device. """""" if cls.MOCK: __cls = _MockedIOSXRDevice else: __cls = IOSXR cls.device = __cls( cls.HOSTNAME, cls.USERNAME, cls.PASSWORD, port=cls.PORT, lock=cls.LOCK, logfile=cls.LOG, timeout=cls.TIMEOUT, ) cls.device.open() @classmethod def tearDownClass(cls): """""" Closes the connection with the device. """""" cls.device.close() def test_mock_lock_connection_open(self): if self.MOCK: self.device.lock_on_connect = True # because there's one single mock file # and it is already used for the lock test # will tesst if raises LockError on connect self.assertRaises(LockError, self.device.lock) self.device.lock_on_connect = False # enough to see that will try to lock during connect def test_mock_close(self): """"""Testing if unlocking when connection is closed"""""" if self.MOCK: self.device.locked = True self.device.close() self.assertFalse(self.device.locked, msg=""Cannot unlock the DB."") def test_execute_rpc_method(self): """"""Testing private method _execute_rpc"""""" self.assertIsInstance( self.device._execute_rpc( """" ), ET._Element, msg=""Privat emethod _execute_rpc did not return a valid XML object."", ) def test__getttr__show_(self): """"""Testing special attribute __getattr___ against valid show command"""""" self.assertIsInstance( self.device.show_ntp_ass(), str, ""Special attribute __getattr___ did not return a valid string."", ) def test__getttr__show_args(self): """"""Testing special attribute __getattr___ against valid show command with arguments"""""" self.assertIsInstance(self.device.show_ntp(""ass""), str) def test_acquire_xml_agent(self): """"""Testing if able to acquire the XML agent."""""" self.device._lock_xml_agent(time.time()) self.assertTrue(self.device._xml_agent_locker.locked()) self.device._unlock_xml_agent() def test_acquire_locked_agent_raises_timeout_error(self): """"""Testing if trying to acquire the XML agent while locked raises TimeoutError."""""" self.device._lock_xml_agent(time.time()) # acquiring self.assertRaises( TimeoutError, self.device._lock_xml_agent, # trying to acquire again time.time(), ) self.device._unlock_xml_agent() # releasing back def test_release_xml_agent(self): """"""Testing releasing of XML agent."""""" self.device._lock_xml_agent(time.time()) self.assertTrue(self.device._xml_agent_locker.locked()) self.device._unlock_xml_agent() self.assertFalse(self.device._xml_agent_locker.locked()) def test_in_cli_mode(self): """"""Testing the private method _in_cli_mode."""""" self.assertTrue(self.device._in_cli_mode()) def test__getattr_show_config(self): """"""Testing special attribute __getattr___ against valid show config command"""""" self.assertIsInstance(self.device.show_run_ntp(config=True), str) def test__getattr__no_show(self): """"""""Test special attribute __getattr__ agains a no-show command"""""" raised = False try: self.device.configure_exclusive() except AttributeError: raised = True self.assertTrue(raised) def test_make_rpc_call_returns_XML(self): """"""Test if public method make_rpc_call returns str"""""" self.assertIsInstance( self.device.make_rpc_call( """" ), binary_type, ) def test_acquired_xml_agent(self): """""" Testing if raises TimeoutError if the XML agent is alredy acquired and released when exception thrown """""" self.device._lock_xml_agent(time.time()) # acquiring the XML agent self.assertRaises( TimeoutError, self.device.make_rpc_call, """", ) self.assertFalse( self.device._xml_agent_locker.locked() ) # Exception raised => xml agent released def test_try_to_read_till_timeout(self): """"""Testing if will try to read from the device till time out"""""" if self.MOCK: # hard to reproduce without mock data # as this event is not deterministic self.assertRaises( TimeoutError, self.device.make_rpc_call, """" ) def test_multiple_read_attempts_till_timeout(self): """"""Testing if will try to read non-empty replies from the device till time out"""""" if self.MOCK: # hard to reproduce without mock data # as this event is not deterministic self.assertRaises( TimeoutError, self.device.make_rpc_call, """" ) def test_iterator_id_raises_IteratorIDError(self): """"""Testing if reply containing the IteratorID attribute raises IteratorIDError"""""" self.device.load_candidate_config(config=""xml agent tty iteration on size 1"") # minimum iteration size self.device.commit_config(comment=""pyIOSXR-test_xml-agent-iteration-on"") # turning on iteration # and a very small value # requesting something that we know for sure will be a big output self.assertRaises( IteratorIDError, self.device.make_rpc_call, """, https://github.com/ndbroadbent/icloud_photos_downloader,e5f304941e2a74b1fbda06c4270ec2fc100e6a49,tests/test_cli.py::CliTestCase::test_tqdm,tests/test_cli.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# coding=utf-8 from unittest import TestCase from vcr import VCR import os import click from click.testing import CliRunner from icloudpd.base import main vcr = VCR(decode_compressed_response=True) class CliTestCase(TestCase): def test_cli(self): runner = CliRunner() result = runner.invoke(main, [""--help""]) assert result.exit_code == 0 def test_log_levels(self): if not os.path.exists(""tests/fixtures/Photos""): os.makedirs(""tests/fixtures/Photos"") with vcr.use_cassette(""tests/vcr_cassettes/listing_photos.yml""): # Pass fixed client ID via environment variable os.environ[""CLIENT_ID""] = ""DE309E26-942E-11E8-92F5-14109FE0B321"" runner = CliRunner() result = runner.invoke( main, [ ""--username"", ""jdoe@gmail.com"", ""--password"", ""password1"", ""--recent"", ""0"", ""--log-level"", ""info"", ""-d"" ""tests/fixtures/Photos"", ], ) assert result.exit_code == 0 with vcr.use_cassette(""tests/vcr_cassettes/listing_photos.yml""): result = runner.invoke( main, [ ""--username"", ""jdoe@gmail.com"", ""--password"", ""password1"", ""--recent"", ""0"", ""--log-level"", ""error"", ""-d"", ""tests/fixtures/Photos"", ], ) assert result.exit_code == 0 def test_tqdm(self): if not os.path.exists(""tests/fixtures/Photos""): os.makedirs(""tests/fixtures/Photos"") with vcr.use_cassette(""tests/vcr_cassettes/listing_photos.yml""): # Force tqdm progress bar via ENV var os.environ[""FORCE_TQDM""] = ""yes"" runner = CliRunner() result = runner.invoke( main, [ ""--username"", ""jdoe@gmail.com"", ""--password"", ""password1"", ""--recent"", ""0"", ""-d"", ""tests/fixtures/Photos"", ], ) del os.environ[""FORCE_TQDM""] assert result.exit_code == 0 def test_unicode_directory(self): with vcr.use_cassette(""tests/vcr_cassettes/listing_photos.yml""): # Pass fixed client ID via environment variable os.environ[""CLIENT_ID""] = ""DE309E26-942E-11E8-92F5-14109FE0B321"" runner = CliRunner() result = runner.invoke( main, [ ""--username"", ""jdoe@gmail.com"", ""--password"", ""password1"", ""--recent"", ""0"", ""--log-level"", ""info"", ""-d"", ""tests/fixtures/相片"", ], ) assert result.exit_code == 0 ", https://github.com/Netflix-Skunkworks/policyuniverse,b633e572ecdba45d70398ac034fb44e0e4e7dbf8,policyuniverse/tests/test_expander_minimizer.py::TestMethods::test_expand_1,policyuniverse/tests/test_expander_minimizer.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright 2014 Netflix, Inc. # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """""" .. module: policyuniverse.tests.test_expander_minimizer :platform: Unix .. version:: $$VERSION$$ .. moduleauthor:: Mike Grima """""" import unittest import copy from policyuniverse.expander_minimizer import expand_policy from policyuniverse.expander_minimizer import minimize_policy from policyuniverse.expander_minimizer import expand_minimize_over_policies from policyuniverse.expander_minimizer import get_actions_from_statement from policyuniverse.expander_minimizer import all_permissions from policyuniverse.expander_minimizer import minimize_statement_actions from policyuniverse.expander_minimizer import _get_prefixes_for_action from policyuniverse.expander_minimizer import _expand_wildcard_action from policyuniverse.expander_minimizer import _get_desired_actions_from_statement WILDCARD_ACTION_1 = ""swf:res*"" WILDCARD_POLICY_1 = { ""Statement"": [{""Action"": [WILDCARD_ACTION_1], ""Resource"": ""*"", ""Effect"": ""Allow""}] } EXPANDED_ACTIONS_1 = [ ""swf:respondactivitytaskcanceled"", ""swf:respondactivitytaskcompleted"", ""swf:respondactivitytaskfailed"", ""swf:responddecisiontaskcompleted"", ] EXPANDED_POLICY_1 = { ""Statement"": [{""Action"": EXPANDED_ACTIONS_1, ""Resource"": ""*"", ""Effect"": ""Allow""}] } WILDCARD_POLICY_2 = { ""Statement"": [ {""Action"": [""swf:*activitytaskc*""], ""Resource"": ""*"", ""Effect"": ""Allow""} ] } EXPANDED_POLICY_2 = { ""Statement"": [ { ""Action"": [ ""swf:respondactivitytaskcanceled"", ""swf:respondactivitytaskcompleted"", ], ""Resource"": ""*"", ""Effect"": ""Allow"", } ] } POLICIES_1 = { ""policy"": {""policyname1"": WILDCARD_POLICY_1, ""policyname2"": WILDCARD_POLICY_2} } EXPANDED_POLICIES_1 = { ""policy"": {""policyname1"": EXPANDED_POLICY_1, ""policyname2"": EXPANDED_POLICY_2} } AUTOSCALING_PERMISSIONS = sorted( [ ""autoscaling:attachinstances"", ""autoscaling:attachloadbalancers"", ""autoscaling:attachloadbalancertargetgroups"", ""autoscaling:batchdeletescheduledaction"", ""autoscaling:batchputscheduledupdategroupaction"", ""autoscaling:cancelinstancerefresh"", ""autoscaling:completelifecycleaction"", ""autoscaling:createautoscalinggroup"", ""autoscaling:createlaunchconfiguration"", ""autoscaling:createorupdatetags"", ""autoscaling:deleteautoscalinggroup"", ""autoscaling:deletelaunchconfiguration"", ""autoscaling:deletelifecyclehook"", ""autoscaling:deletenotificationconfiguration"", ""autoscaling:deletepolicy"", ""autoscaling:deletescheduledaction"", ""autoscaling:deletetags"", ""autoscaling:describeaccountlimits"", ""autoscaling:describeadjustmenttypes"", ""autoscaling:describeautoscalinggroups"", ""autoscaling:describeautoscalinginstances"", ""autoscaling:describeautoscalingnotificationtypes"", ""autoscaling:describeinstancerefreshes"", ""autoscaling:describelaunchconfigurations"", ""autoscaling:describelifecyclehooks"", ""autoscaling:describelifecyclehooktypes"", ""autoscaling:describeloadbalancers"", ""autoscaling:describeloadbalancertargetgroups"", ""autoscaling:describemetriccollectiontypes"", ""autoscaling:describenotificationconfigurations"", ""autoscaling:describepolicies"", ""autoscaling:describescalingactivities"", ""autoscaling:describescalingprocesstypes"", ""autoscaling:describescheduledactions"", ""autoscaling:describetags"", ""autoscaling:describeterminationpolicytypes"", ""autoscaling:detachinstances"", ""autoscaling:detachloadbalancers"", ""autoscaling:detachloadbalancertargetgroups"", ""autoscaling:disablemetricscollection"", ""autoscaling:enablemetricscollection"", ""autoscaling:enterstandby"", ""autoscaling:executepolicy"", ""autoscaling:exitstandby"", ""autoscaling:putlifecyclehook"", ""autoscaling:putnotificationconfiguration"", ""autoscaling:putscalingpolicy"", ""autoscaling:putscheduledupdategroupaction"", ""autoscaling:recordlifecycleactionheartbeat"", ""autoscaling:resumeprocesses"", ""autoscaling:setdesiredcapacity"", ""autoscaling:setinstancehealth"", ""autoscaling:setinstanceprotection"", ""autoscaling:startinstancerefresh"", ""autoscaling:suspendprocesses"", ""autoscaling:terminateinstanceinautoscalinggroup"", ""autoscaling:updateautoscalinggroup"", ] ) def dc(o): """""" Some of the testing methods modify the datastructure you pass into them. We want to deepcopy each structure so one test doesn't break another. """""" return copy.deepcopy(o) class TestMethods(unittest.TestCase): def test_expand_1(self): expanded_policy = expand_policy(policy=dc(WILDCARD_POLICY_1)) self.assertEqual(expanded_policy, EXPANDED_POLICY_1) policy = { ""Statement"": { ""NotAction"": [""ec2:thispermissiondoesntexist""], ""Resource"": ""*"", ""Effect"": ""Deny"", } } expected_policy = { ""Statement"": [ { ""NotAction"": [""ec2:thispermissiondoesntexist""], ""Resource"": ""*"", ""Effect"": ""Deny"", } ] } expanded_policy = expand_policy(policy=dc(policy), expand_deny=False) self.assertEqual(expanded_policy, expected_policy) expanded_policy = expand_policy(policy=dc(policy), expand_deny=True) self.assertEqual(type(expanded_policy[""Statement""]), list) def test_expand_2(self): expanded_policy = expand_policy(policy=dc(WILDCARD_POLICY_2)) self.assertEqual(expanded_policy, EXPANDED_POLICY_2) def test_expand_minimize_over_policies(self): result = expand_minimize_over_policies(dc(POLICIES_1), expand_policy) self.assertEqual(result, EXPANDED_POLICIES_1) def test_expand_minimize_over_policies_1(self): result = expand_minimize_over_policies( EXPANDED_POLICY_1, minimize_policy, minchars=3 ) self.assertEqual(result, WILDCARD_POLICY_1) def test_get_prefixes_for_action(self): result = _get_prefixes_for_action(""iam:cat"") self.assertEqual(result, [""iam:"", ""iam:c"", ""iam:ca"", ""iam:cat""]) def test_expand_wildcard_action(self): result = _expand_wildcard_action([""autoscaling:*""]) self.assertEqual(sorted(result), AUTOSCALING_PERMISSIONS) def test_expand_wildcard_action_2(self): result = _expand_wildcard_action(""thistechdoesntexist:*"") self.assertEqual(result, [""thistechdoesntexist:*""]) def test_expand_wildcard_action_3(self): result = _expand_wildcard_action(""ec2:DescribeInstances"") self.assertEqual(result, [""ec2:describeinstances""]) def test_get_desired_actions_from_statement(self): result = _get_desired_actions_from_statement( dc(WILDCARD_POLICY_1[""Statement""][0]) ) self.assertEqual(result, set(EXPANDED_ACTIONS_1)) def test_get_desired_actions_from_statement_1(self): statement = { ""Action"": [""ec2:thispermissiondoesntexist""], ""Resource"": ""*"", ""Effect"": ""Allow"", } self.assertRaises(Exception, _get_desired_actions_from_statement, statement) def test_get_actions_from_statement(self): statement = { ""Action"": ""ec2:thispermissiondoesntexist"", ""NotAction"": list(all_permissions), ""Resource"": ""*"", ""Effect"": ""Allow"", } expected_result = {""ec2:thispermissiondoesntexist""} result = get_actions_from_statement(statement) self.assertEqual(result, expected_result) get_actions_from_statement(dict(NotAction=""abc"")) def test_minimize_statement_actions(self): statement = dict(Effect=""Deny"") self.assertRaises(Exception, minimize_statement_actions, statement) ", https://github.com/Netflix-Skunkworks/policyuniverse,b633e572ecdba45d70398ac034fb44e0e4e7dbf8,policyuniverse/tests/test_expander_minimizer.py::TestMethods::test_expand_minimize_over_policies,policyuniverse/tests/test_expander_minimizer.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright 2014 Netflix, Inc. # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """""" .. module: policyuniverse.tests.test_expander_minimizer :platform: Unix .. version:: $$VERSION$$ .. moduleauthor:: Mike Grima """""" import unittest import copy from policyuniverse.expander_minimizer import expand_policy from policyuniverse.expander_minimizer import minimize_policy from policyuniverse.expander_minimizer import expand_minimize_over_policies from policyuniverse.expander_minimizer import get_actions_from_statement from policyuniverse.expander_minimizer import all_permissions from policyuniverse.expander_minimizer import minimize_statement_actions from policyuniverse.expander_minimizer import _get_prefixes_for_action from policyuniverse.expander_minimizer import _expand_wildcard_action from policyuniverse.expander_minimizer import _get_desired_actions_from_statement WILDCARD_ACTION_1 = ""swf:res*"" WILDCARD_POLICY_1 = { ""Statement"": [{""Action"": [WILDCARD_ACTION_1], ""Resource"": ""*"", ""Effect"": ""Allow""}] } EXPANDED_ACTIONS_1 = [ ""swf:respondactivitytaskcanceled"", ""swf:respondactivitytaskcompleted"", ""swf:respondactivitytaskfailed"", ""swf:responddecisiontaskcompleted"", ] EXPANDED_POLICY_1 = { ""Statement"": [{""Action"": EXPANDED_ACTIONS_1, ""Resource"": ""*"", ""Effect"": ""Allow""}] } WILDCARD_POLICY_2 = { ""Statement"": [ {""Action"": [""swf:*activitytaskc*""], ""Resource"": ""*"", ""Effect"": ""Allow""} ] } EXPANDED_POLICY_2 = { ""Statement"": [ { ""Action"": [ ""swf:respondactivitytaskcanceled"", ""swf:respondactivitytaskcompleted"", ], ""Resource"": ""*"", ""Effect"": ""Allow"", } ] } POLICIES_1 = { ""policy"": {""policyname1"": WILDCARD_POLICY_1, ""policyname2"": WILDCARD_POLICY_2} } EXPANDED_POLICIES_1 = { ""policy"": {""policyname1"": EXPANDED_POLICY_1, ""policyname2"": EXPANDED_POLICY_2} } AUTOSCALING_PERMISSIONS = sorted( [ ""autoscaling:attachinstances"", ""autoscaling:attachloadbalancers"", ""autoscaling:attachloadbalancertargetgroups"", ""autoscaling:batchdeletescheduledaction"", ""autoscaling:batchputscheduledupdategroupaction"", ""autoscaling:cancelinstancerefresh"", ""autoscaling:completelifecycleaction"", ""autoscaling:createautoscalinggroup"", ""autoscaling:createlaunchconfiguration"", ""autoscaling:createorupdatetags"", ""autoscaling:deleteautoscalinggroup"", ""autoscaling:deletelaunchconfiguration"", ""autoscaling:deletelifecyclehook"", ""autoscaling:deletenotificationconfiguration"", ""autoscaling:deletepolicy"", ""autoscaling:deletescheduledaction"", ""autoscaling:deletetags"", ""autoscaling:describeaccountlimits"", ""autoscaling:describeadjustmenttypes"", ""autoscaling:describeautoscalinggroups"", ""autoscaling:describeautoscalinginstances"", ""autoscaling:describeautoscalingnotificationtypes"", ""autoscaling:describeinstancerefreshes"", ""autoscaling:describelaunchconfigurations"", ""autoscaling:describelifecyclehooks"", ""autoscaling:describelifecyclehooktypes"", ""autoscaling:describeloadbalancers"", ""autoscaling:describeloadbalancertargetgroups"", ""autoscaling:describemetriccollectiontypes"", ""autoscaling:describenotificationconfigurations"", ""autoscaling:describepolicies"", ""autoscaling:describescalingactivities"", ""autoscaling:describescalingprocesstypes"", ""autoscaling:describescheduledactions"", ""autoscaling:describetags"", ""autoscaling:describeterminationpolicytypes"", ""autoscaling:detachinstances"", ""autoscaling:detachloadbalancers"", ""autoscaling:detachloadbalancertargetgroups"", ""autoscaling:disablemetricscollection"", ""autoscaling:enablemetricscollection"", ""autoscaling:enterstandby"", ""autoscaling:executepolicy"", ""autoscaling:exitstandby"", ""autoscaling:putlifecyclehook"", ""autoscaling:putnotificationconfiguration"", ""autoscaling:putscalingpolicy"", ""autoscaling:putscheduledupdategroupaction"", ""autoscaling:recordlifecycleactionheartbeat"", ""autoscaling:resumeprocesses"", ""autoscaling:setdesiredcapacity"", ""autoscaling:setinstancehealth"", ""autoscaling:setinstanceprotection"", ""autoscaling:startinstancerefresh"", ""autoscaling:suspendprocesses"", ""autoscaling:terminateinstanceinautoscalinggroup"", ""autoscaling:updateautoscalinggroup"", ] ) def dc(o): """""" Some of the testing methods modify the datastructure you pass into them. We want to deepcopy each structure so one test doesn't break another. """""" return copy.deepcopy(o) class TestMethods(unittest.TestCase): def test_expand_1(self): expanded_policy = expand_policy(policy=dc(WILDCARD_POLICY_1)) self.assertEqual(expanded_policy, EXPANDED_POLICY_1) policy = { ""Statement"": { ""NotAction"": [""ec2:thispermissiondoesntexist""], ""Resource"": ""*"", ""Effect"": ""Deny"", } } expected_policy = { ""Statement"": [ { ""NotAction"": [""ec2:thispermissiondoesntexist""], ""Resource"": ""*"", ""Effect"": ""Deny"", } ] } expanded_policy = expand_policy(policy=dc(policy), expand_deny=False) self.assertEqual(expanded_policy, expected_policy) expanded_policy = expand_policy(policy=dc(policy), expand_deny=True) self.assertEqual(type(expanded_policy[""Statement""]), list) def test_expand_2(self): expanded_policy = expand_policy(policy=dc(WILDCARD_POLICY_2)) self.assertEqual(expanded_policy, EXPANDED_POLICY_2) def test_expand_minimize_over_policies(self): result = expand_minimize_over_policies(dc(POLICIES_1), expand_policy) self.assertEqual(result, EXPANDED_POLICIES_1) def test_expand_minimize_over_policies_1(self): result = expand_minimize_over_policies( EXPANDED_POLICY_1, minimize_policy, minchars=3 ) self.assertEqual(result, WILDCARD_POLICY_1) def test_get_prefixes_for_action(self): result = _get_prefixes_for_action(""iam:cat"") self.assertEqual(result, [""iam:"", ""iam:c"", ""iam:ca"", ""iam:cat""]) def test_expand_wildcard_action(self): result = _expand_wildcard_action([""autoscaling:*""]) self.assertEqual(sorted(result), AUTOSCALING_PERMISSIONS) def test_expand_wildcard_action_2(self): result = _expand_wildcard_action(""thistechdoesntexist:*"") self.assertEqual(result, [""thistechdoesntexist:*""]) def test_expand_wildcard_action_3(self): result = _expand_wildcard_action(""ec2:DescribeInstances"") self.assertEqual(result, [""ec2:describeinstances""]) def test_get_desired_actions_from_statement(self): result = _get_desired_actions_from_statement( dc(WILDCARD_POLICY_1[""Statement""][0]) ) self.assertEqual(result, set(EXPANDED_ACTIONS_1)) def test_get_desired_actions_from_statement_1(self): statement = { ""Action"": [""ec2:thispermissiondoesntexist""], ""Resource"": ""*"", ""Effect"": ""Allow"", } self.assertRaises(Exception, _get_desired_actions_from_statement, statement) def test_get_actions_from_statement(self): statement = { ""Action"": ""ec2:thispermissiondoesntexist"", ""NotAction"": list(all_permissions), ""Resource"": ""*"", ""Effect"": ""Allow"", } expected_result = {""ec2:thispermissiondoesntexist""} result = get_actions_from_statement(statement) self.assertEqual(result, expected_result) get_actions_from_statement(dict(NotAction=""abc"")) def test_minimize_statement_actions(self): statement = dict(Effect=""Deny"") self.assertRaises(Exception, minimize_statement_actions, statement) ", https://github.com/neulab/compare-mt,92502b30b1f228773179a01dd86fe1b8dfd400e5,tests/test_cache.py::TestScoreCache::test_score_cache_bootstrap,tests/test_cache.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import os.path import unittest import numpy as np import sys compare_mt_root = os.path.join(os.path.dirname(os.path.realpath(__file__)), "".."") sys.path.append(compare_mt_root) from compare_mt import scorers from compare_mt.corpus_utils import load_tokens, load_alignments from compare_mt import compare_mt_main from compare_mt import reporters def _get_example_data(): example_path = os.path.join(compare_mt_root, ""example"") ref_file = os.path.join(example_path, ""ted.ref.eng"") out1_file = os.path.join(example_path, ""ted.sys1.eng"") out2_file = os.path.join(example_path, ""ted.sys2.eng"") return [load_tokens(x) for x in (ref_file, out1_file, out2_file)] def _get_example_data_detokenized(): example_path = os.path.join(compare_mt_root, ""example"") ref_file = os.path.join(example_path, ""ted.ref.detok.eng"") out1_file = os.path.join(example_path, ""ted.sys1.detok.eng"") out2_file = os.path.join(example_path, ""ted.sys2.detok.eng"") return [load_tokens(x) for x in (ref_file, out1_file, out2_file)] class TestScoreCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_score_cache(self): cached_stats1 = compare_mt_main.generate_score_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_score_report(self.ref, [self.out2], to_cache=True) self.assertTrue('scores' in cached_stats1 and 'strs' in cached_stats1 and 'sign_stats' in cached_stats1) self.assertTrue('scores' in cached_stats2 and 'strs' in cached_stats2 and 'sign_stats' in cached_stats2) self.assertAlmostEqual(cached_stats1['scores'], 22.44, places=1) reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2], title='Aggregate Scores') ori_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], title='Aggregate Scores') self.assertTrue(cached_report.scores == ori_report.scores) self.assertTrue(cached_report.strs == ori_report.strs) self.assertTrue(cached_report.wins == ori_report.wins) def test_score_cache_bootstrap(self): cached_stats1 = compare_mt_main.generate_score_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_score_report(self.ref, [self.out2], to_cache=True) self.assertTrue('scores' in cached_stats1 and 'strs' in cached_stats1 and 'sign_stats' in cached_stats1) self.assertTrue('scores' in cached_stats2 and 'strs' in cached_stats2 and 'sign_stats' in cached_stats2) self.assertAlmostEqual(cached_stats1['scores'], 22.44, places=1) reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2], bootstrap=5, title='Aggregate Scores') ori_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], bootstrap=5, title='Aggregate Scores') self.assertTrue(cached_report.scores == ori_report.scores) self.assertTrue(cached_report.strs == ori_report.strs) self.assertTrue(cached_report.wins == ori_report.wins) class TestWordAccCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_wordacc_cache(self): cached_stats1 = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out2], to_cache=True) self.assertTrue('statistics' in cached_stats1 and 'my_ref_total_list' in cached_stats1 and 'my_out_matches_list' in cached_stats1) self.assertTrue('statistics' in cached_stats2 and 'my_ref_total_list' in cached_stats2 and 'my_out_matches_list' in cached_stats2) ori_report = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.statistics == ori_report.statistics) self.assertTrue(cached_report.examples == ori_report.examples) class TestSrcWordAccCache(unittest.TestCase): @classmethod def setUpClass(self): example_path = os.path.join(compare_mt_root, ""example"") self.ref, self.out1, self.out2 = _get_example_data() src_file = os.path.join(example_path, ""ted.orig.slk"") self.src = load_tokens(src_file) self.ref_align_file = os.path.join(example_path, ""ted.ref.align"") def test_src_wordacc_cache(self): cached_stats1 = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out1], self.src, ref_align_file=self.ref_align_file, to_cache=True) cached_stats2 = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out2], self.src, ref_align_file=self.ref_align_file, to_cache=True) self.assertTrue('statistics' in cached_stats1 and 'my_ref_total_list' in cached_stats1 and 'my_out_matches_list' in cached_stats1) self.assertTrue('statistics' in cached_stats2 and 'my_ref_total_list' in cached_stats2 and 'my_out_matches_list' in cached_stats2) ori_report = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out1, self.out2], self.src, ref_align_file=self.ref_align_file) cached_report = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out1, self.out2], self.src, ref_align_file=self.ref_align_file, cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.statistics == ori_report.statistics) self.assertTrue(cached_report.examples == ori_report.examples) class TestSentBucketCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_sentbucket_cache(self): cached_stats1 = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out2], to_cache=True) self.assertTrue('stats' in cached_stats1) self.assertTrue('stats' in cached_stats2) ori_report = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.sys_stats == ori_report.sys_stats) class TestNgramCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_ngram_cache(self): reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_stats1 = compare_mt_main.generate_ngram_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_ngram_report(self.ref, [self.out2], to_cache=True) self.assertTrue('totals' in cached_stats1 and 'matches' in cached_stats1 and 'overs' in cached_stats1 and 'unders' in cached_stats1) self.assertTrue('totals' in cached_stats2 and 'matches' in cached_stats2 and 'overs' in cached_stats2 and 'unders' in cached_stats2) ori_report = compare_mt_main.generate_ngram_report(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_ngram_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.scorelist == ori_report.scorelist) class TestSentExamCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_sentexam_cache(self): reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_stats1 = compare_mt_main.generate_sentence_examples(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_sentence_examples(self.ref, [self.out2], to_cache=True) self.assertTrue('scores' in cached_stats1 and 'strs' in cached_stats1) self.assertTrue('scores' in cached_stats2 and 'strs' in cached_stats2) ori_report = compare_mt_main.generate_sentence_examples(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_sentence_examples(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.scorediff_lists== ori_report.scorediff_lists) if __name__ == ""__main__"": unittest.main() ", https://github.com/neulab/compare-mt,92502b30b1f228773179a01dd86fe1b8dfd400e5,tests/test_cache.py::TestSentBucketCache::test_sentbucket_cache,tests/test_cache.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import os.path import unittest import numpy as np import sys compare_mt_root = os.path.join(os.path.dirname(os.path.realpath(__file__)), "".."") sys.path.append(compare_mt_root) from compare_mt import scorers from compare_mt.corpus_utils import load_tokens, load_alignments from compare_mt import compare_mt_main from compare_mt import reporters def _get_example_data(): example_path = os.path.join(compare_mt_root, ""example"") ref_file = os.path.join(example_path, ""ted.ref.eng"") out1_file = os.path.join(example_path, ""ted.sys1.eng"") out2_file = os.path.join(example_path, ""ted.sys2.eng"") return [load_tokens(x) for x in (ref_file, out1_file, out2_file)] def _get_example_data_detokenized(): example_path = os.path.join(compare_mt_root, ""example"") ref_file = os.path.join(example_path, ""ted.ref.detok.eng"") out1_file = os.path.join(example_path, ""ted.sys1.detok.eng"") out2_file = os.path.join(example_path, ""ted.sys2.detok.eng"") return [load_tokens(x) for x in (ref_file, out1_file, out2_file)] class TestScoreCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_score_cache(self): cached_stats1 = compare_mt_main.generate_score_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_score_report(self.ref, [self.out2], to_cache=True) self.assertTrue('scores' in cached_stats1 and 'strs' in cached_stats1 and 'sign_stats' in cached_stats1) self.assertTrue('scores' in cached_stats2 and 'strs' in cached_stats2 and 'sign_stats' in cached_stats2) self.assertAlmostEqual(cached_stats1['scores'], 22.44, places=1) reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2], title='Aggregate Scores') ori_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], title='Aggregate Scores') self.assertTrue(cached_report.scores == ori_report.scores) self.assertTrue(cached_report.strs == ori_report.strs) self.assertTrue(cached_report.wins == ori_report.wins) def test_score_cache_bootstrap(self): cached_stats1 = compare_mt_main.generate_score_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_score_report(self.ref, [self.out2], to_cache=True) self.assertTrue('scores' in cached_stats1 and 'strs' in cached_stats1 and 'sign_stats' in cached_stats1) self.assertTrue('scores' in cached_stats2 and 'strs' in cached_stats2 and 'sign_stats' in cached_stats2) self.assertAlmostEqual(cached_stats1['scores'], 22.44, places=1) reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2], bootstrap=5, title='Aggregate Scores') ori_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], bootstrap=5, title='Aggregate Scores') self.assertTrue(cached_report.scores == ori_report.scores) self.assertTrue(cached_report.strs == ori_report.strs) self.assertTrue(cached_report.wins == ori_report.wins) class TestWordAccCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_wordacc_cache(self): cached_stats1 = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out2], to_cache=True) self.assertTrue('statistics' in cached_stats1 and 'my_ref_total_list' in cached_stats1 and 'my_out_matches_list' in cached_stats1) self.assertTrue('statistics' in cached_stats2 and 'my_ref_total_list' in cached_stats2 and 'my_out_matches_list' in cached_stats2) ori_report = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.statistics == ori_report.statistics) self.assertTrue(cached_report.examples == ori_report.examples) class TestSrcWordAccCache(unittest.TestCase): @classmethod def setUpClass(self): example_path = os.path.join(compare_mt_root, ""example"") self.ref, self.out1, self.out2 = _get_example_data() src_file = os.path.join(example_path, ""ted.orig.slk"") self.src = load_tokens(src_file) self.ref_align_file = os.path.join(example_path, ""ted.ref.align"") def test_src_wordacc_cache(self): cached_stats1 = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out1], self.src, ref_align_file=self.ref_align_file, to_cache=True) cached_stats2 = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out2], self.src, ref_align_file=self.ref_align_file, to_cache=True) self.assertTrue('statistics' in cached_stats1 and 'my_ref_total_list' in cached_stats1 and 'my_out_matches_list' in cached_stats1) self.assertTrue('statistics' in cached_stats2 and 'my_ref_total_list' in cached_stats2 and 'my_out_matches_list' in cached_stats2) ori_report = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out1, self.out2], self.src, ref_align_file=self.ref_align_file) cached_report = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out1, self.out2], self.src, ref_align_file=self.ref_align_file, cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.statistics == ori_report.statistics) self.assertTrue(cached_report.examples == ori_report.examples) class TestSentBucketCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_sentbucket_cache(self): cached_stats1 = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out2], to_cache=True) self.assertTrue('stats' in cached_stats1) self.assertTrue('stats' in cached_stats2) ori_report = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.sys_stats == ori_report.sys_stats) class TestNgramCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_ngram_cache(self): reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_stats1 = compare_mt_main.generate_ngram_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_ngram_report(self.ref, [self.out2], to_cache=True) self.assertTrue('totals' in cached_stats1 and 'matches' in cached_stats1 and 'overs' in cached_stats1 and 'unders' in cached_stats1) self.assertTrue('totals' in cached_stats2 and 'matches' in cached_stats2 and 'overs' in cached_stats2 and 'unders' in cached_stats2) ori_report = compare_mt_main.generate_ngram_report(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_ngram_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.scorelist == ori_report.scorelist) class TestSentExamCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_sentexam_cache(self): reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_stats1 = compare_mt_main.generate_sentence_examples(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_sentence_examples(self.ref, [self.out2], to_cache=True) self.assertTrue('scores' in cached_stats1 and 'strs' in cached_stats1) self.assertTrue('scores' in cached_stats2 and 'strs' in cached_stats2) ori_report = compare_mt_main.generate_sentence_examples(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_sentence_examples(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.scorediff_lists== ori_report.scorediff_lists) if __name__ == ""__main__"": unittest.main() ", https://github.com/neulab/compare-mt,92502b30b1f228773179a01dd86fe1b8dfd400e5,tests/test_cache.py::TestSrcWordAccCache::test_src_wordacc_cache,tests/test_cache.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import os.path import unittest import numpy as np import sys compare_mt_root = os.path.join(os.path.dirname(os.path.realpath(__file__)), "".."") sys.path.append(compare_mt_root) from compare_mt import scorers from compare_mt.corpus_utils import load_tokens, load_alignments from compare_mt import compare_mt_main from compare_mt import reporters def _get_example_data(): example_path = os.path.join(compare_mt_root, ""example"") ref_file = os.path.join(example_path, ""ted.ref.eng"") out1_file = os.path.join(example_path, ""ted.sys1.eng"") out2_file = os.path.join(example_path, ""ted.sys2.eng"") return [load_tokens(x) for x in (ref_file, out1_file, out2_file)] def _get_example_data_detokenized(): example_path = os.path.join(compare_mt_root, ""example"") ref_file = os.path.join(example_path, ""ted.ref.detok.eng"") out1_file = os.path.join(example_path, ""ted.sys1.detok.eng"") out2_file = os.path.join(example_path, ""ted.sys2.detok.eng"") return [load_tokens(x) for x in (ref_file, out1_file, out2_file)] class TestScoreCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_score_cache(self): cached_stats1 = compare_mt_main.generate_score_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_score_report(self.ref, [self.out2], to_cache=True) self.assertTrue('scores' in cached_stats1 and 'strs' in cached_stats1 and 'sign_stats' in cached_stats1) self.assertTrue('scores' in cached_stats2 and 'strs' in cached_stats2 and 'sign_stats' in cached_stats2) self.assertAlmostEqual(cached_stats1['scores'], 22.44, places=1) reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2], title='Aggregate Scores') ori_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], title='Aggregate Scores') self.assertTrue(cached_report.scores == ori_report.scores) self.assertTrue(cached_report.strs == ori_report.strs) self.assertTrue(cached_report.wins == ori_report.wins) def test_score_cache_bootstrap(self): cached_stats1 = compare_mt_main.generate_score_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_score_report(self.ref, [self.out2], to_cache=True) self.assertTrue('scores' in cached_stats1 and 'strs' in cached_stats1 and 'sign_stats' in cached_stats1) self.assertTrue('scores' in cached_stats2 and 'strs' in cached_stats2 and 'sign_stats' in cached_stats2) self.assertAlmostEqual(cached_stats1['scores'], 22.44, places=1) reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2], bootstrap=5, title='Aggregate Scores') ori_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], bootstrap=5, title='Aggregate Scores') self.assertTrue(cached_report.scores == ori_report.scores) self.assertTrue(cached_report.strs == ori_report.strs) self.assertTrue(cached_report.wins == ori_report.wins) class TestWordAccCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_wordacc_cache(self): cached_stats1 = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out2], to_cache=True) self.assertTrue('statistics' in cached_stats1 and 'my_ref_total_list' in cached_stats1 and 'my_out_matches_list' in cached_stats1) self.assertTrue('statistics' in cached_stats2 and 'my_ref_total_list' in cached_stats2 and 'my_out_matches_list' in cached_stats2) ori_report = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.statistics == ori_report.statistics) self.assertTrue(cached_report.examples == ori_report.examples) class TestSrcWordAccCache(unittest.TestCase): @classmethod def setUpClass(self): example_path = os.path.join(compare_mt_root, ""example"") self.ref, self.out1, self.out2 = _get_example_data() src_file = os.path.join(example_path, ""ted.orig.slk"") self.src = load_tokens(src_file) self.ref_align_file = os.path.join(example_path, ""ted.ref.align"") def test_src_wordacc_cache(self): cached_stats1 = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out1], self.src, ref_align_file=self.ref_align_file, to_cache=True) cached_stats2 = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out2], self.src, ref_align_file=self.ref_align_file, to_cache=True) self.assertTrue('statistics' in cached_stats1 and 'my_ref_total_list' in cached_stats1 and 'my_out_matches_list' in cached_stats1) self.assertTrue('statistics' in cached_stats2 and 'my_ref_total_list' in cached_stats2 and 'my_out_matches_list' in cached_stats2) ori_report = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out1, self.out2], self.src, ref_align_file=self.ref_align_file) cached_report = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out1, self.out2], self.src, ref_align_file=self.ref_align_file, cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.statistics == ori_report.statistics) self.assertTrue(cached_report.examples == ori_report.examples) class TestSentBucketCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_sentbucket_cache(self): cached_stats1 = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out2], to_cache=True) self.assertTrue('stats' in cached_stats1) self.assertTrue('stats' in cached_stats2) ori_report = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.sys_stats == ori_report.sys_stats) class TestNgramCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_ngram_cache(self): reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_stats1 = compare_mt_main.generate_ngram_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_ngram_report(self.ref, [self.out2], to_cache=True) self.assertTrue('totals' in cached_stats1 and 'matches' in cached_stats1 and 'overs' in cached_stats1 and 'unders' in cached_stats1) self.assertTrue('totals' in cached_stats2 and 'matches' in cached_stats2 and 'overs' in cached_stats2 and 'unders' in cached_stats2) ori_report = compare_mt_main.generate_ngram_report(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_ngram_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.scorelist == ori_report.scorelist) class TestSentExamCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_sentexam_cache(self): reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_stats1 = compare_mt_main.generate_sentence_examples(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_sentence_examples(self.ref, [self.out2], to_cache=True) self.assertTrue('scores' in cached_stats1 and 'strs' in cached_stats1) self.assertTrue('scores' in cached_stats2 and 'strs' in cached_stats2) ori_report = compare_mt_main.generate_sentence_examples(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_sentence_examples(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.scorediff_lists== ori_report.scorediff_lists) if __name__ == ""__main__"": unittest.main() ", https://github.com/neulab/compare-mt,92502b30b1f228773179a01dd86fe1b8dfd400e5,tests/test_cache.py::TestWordAccCache::test_wordacc_cache,tests/test_cache.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import os.path import unittest import numpy as np import sys compare_mt_root = os.path.join(os.path.dirname(os.path.realpath(__file__)), "".."") sys.path.append(compare_mt_root) from compare_mt import scorers from compare_mt.corpus_utils import load_tokens, load_alignments from compare_mt import compare_mt_main from compare_mt import reporters def _get_example_data(): example_path = os.path.join(compare_mt_root, ""example"") ref_file = os.path.join(example_path, ""ted.ref.eng"") out1_file = os.path.join(example_path, ""ted.sys1.eng"") out2_file = os.path.join(example_path, ""ted.sys2.eng"") return [load_tokens(x) for x in (ref_file, out1_file, out2_file)] def _get_example_data_detokenized(): example_path = os.path.join(compare_mt_root, ""example"") ref_file = os.path.join(example_path, ""ted.ref.detok.eng"") out1_file = os.path.join(example_path, ""ted.sys1.detok.eng"") out2_file = os.path.join(example_path, ""ted.sys2.detok.eng"") return [load_tokens(x) for x in (ref_file, out1_file, out2_file)] class TestScoreCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_score_cache(self): cached_stats1 = compare_mt_main.generate_score_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_score_report(self.ref, [self.out2], to_cache=True) self.assertTrue('scores' in cached_stats1 and 'strs' in cached_stats1 and 'sign_stats' in cached_stats1) self.assertTrue('scores' in cached_stats2 and 'strs' in cached_stats2 and 'sign_stats' in cached_stats2) self.assertAlmostEqual(cached_stats1['scores'], 22.44, places=1) reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2], title='Aggregate Scores') ori_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], title='Aggregate Scores') self.assertTrue(cached_report.scores == ori_report.scores) self.assertTrue(cached_report.strs == ori_report.strs) self.assertTrue(cached_report.wins == ori_report.wins) def test_score_cache_bootstrap(self): cached_stats1 = compare_mt_main.generate_score_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_score_report(self.ref, [self.out2], to_cache=True) self.assertTrue('scores' in cached_stats1 and 'strs' in cached_stats1 and 'sign_stats' in cached_stats1) self.assertTrue('scores' in cached_stats2 and 'strs' in cached_stats2 and 'sign_stats' in cached_stats2) self.assertAlmostEqual(cached_stats1['scores'], 22.44, places=1) reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2], bootstrap=5, title='Aggregate Scores') ori_report = compare_mt_main.generate_score_report(self.ref, [self.out1, self.out2], bootstrap=5, title='Aggregate Scores') self.assertTrue(cached_report.scores == ori_report.scores) self.assertTrue(cached_report.strs == ori_report.strs) self.assertTrue(cached_report.wins == ori_report.wins) class TestWordAccCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_wordacc_cache(self): cached_stats1 = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out2], to_cache=True) self.assertTrue('statistics' in cached_stats1 and 'my_ref_total_list' in cached_stats1 and 'my_out_matches_list' in cached_stats1) self.assertTrue('statistics' in cached_stats2 and 'my_ref_total_list' in cached_stats2 and 'my_out_matches_list' in cached_stats2) ori_report = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_word_accuracy_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.statistics == ori_report.statistics) self.assertTrue(cached_report.examples == ori_report.examples) class TestSrcWordAccCache(unittest.TestCase): @classmethod def setUpClass(self): example_path = os.path.join(compare_mt_root, ""example"") self.ref, self.out1, self.out2 = _get_example_data() src_file = os.path.join(example_path, ""ted.orig.slk"") self.src = load_tokens(src_file) self.ref_align_file = os.path.join(example_path, ""ted.ref.align"") def test_src_wordacc_cache(self): cached_stats1 = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out1], self.src, ref_align_file=self.ref_align_file, to_cache=True) cached_stats2 = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out2], self.src, ref_align_file=self.ref_align_file, to_cache=True) self.assertTrue('statistics' in cached_stats1 and 'my_ref_total_list' in cached_stats1 and 'my_out_matches_list' in cached_stats1) self.assertTrue('statistics' in cached_stats2 and 'my_ref_total_list' in cached_stats2 and 'my_out_matches_list' in cached_stats2) ori_report = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out1, self.out2], self.src, ref_align_file=self.ref_align_file) cached_report = compare_mt_main.generate_src_word_accuracy_report(self.ref, [self.out1, self.out2], self.src, ref_align_file=self.ref_align_file, cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.statistics == ori_report.statistics) self.assertTrue(cached_report.examples == ori_report.examples) class TestSentBucketCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_sentbucket_cache(self): cached_stats1 = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out2], to_cache=True) self.assertTrue('stats' in cached_stats1) self.assertTrue('stats' in cached_stats2) ori_report = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_sentence_bucketed_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.sys_stats == ori_report.sys_stats) class TestNgramCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_ngram_cache(self): reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_stats1 = compare_mt_main.generate_ngram_report(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_ngram_report(self.ref, [self.out2], to_cache=True) self.assertTrue('totals' in cached_stats1 and 'matches' in cached_stats1 and 'overs' in cached_stats1 and 'unders' in cached_stats1) self.assertTrue('totals' in cached_stats2 and 'matches' in cached_stats2 and 'overs' in cached_stats2 and 'unders' in cached_stats2) ori_report = compare_mt_main.generate_ngram_report(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_ngram_report(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.scorelist == ori_report.scorelist) class TestSentExamCache(unittest.TestCase): @classmethod def setUpClass(self): self.ref, self.out1, self.out2 = _get_example_data() def test_sentexam_cache(self): reporters.sys_names = [f'sys{i+1}' for i in range(2)] cached_stats1 = compare_mt_main.generate_sentence_examples(self.ref, [self.out1], to_cache=True) cached_stats2 = compare_mt_main.generate_sentence_examples(self.ref, [self.out2], to_cache=True) self.assertTrue('scores' in cached_stats1 and 'strs' in cached_stats1) self.assertTrue('scores' in cached_stats2 and 'strs' in cached_stats2) ori_report = compare_mt_main.generate_sentence_examples(self.ref, [self.out1, self.out2]) cached_report = compare_mt_main.generate_sentence_examples(self.ref, [self.out1, self.out2], cache_dicts=[cached_stats1, cached_stats2]) self.assertTrue(cached_report.scorediff_lists== ori_report.scorediff_lists) if __name__ == ""__main__"": unittest.main() ", https://github.com/Neuraxio/Neuraxle,20c6e5713198345b43bf6899355f1b5cf65eb02c,testing/hyperparams/test_distributions.py::test_gaussian_distribution_mixture_log,testing/hyperparams/test_distributions.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Tests for Hyperparameters Distributions ======================================== .. Copyright 2019, Neuraxio Inc. Licensed under the Apache License, Version 2.0 (the ""License""); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an ""AS IS"" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """""" from collections import Counter import pytest from neuraxle.hyperparams.distributions import * NUM_TRIALS = 50000 def get_many_samples_for(hd): return [hd.rvs() for _ in range(NUM_TRIALS)] def test_boolean_distribution(): hd = Boolean() samples = get_many_samples_for(hd) falses = Counter(samples).get(False) trues = Counter(samples).get(True) # You'd need to win the lotto for this test to fail. Or a broken random sampler. Or a bug. assert trues > NUM_TRIALS * 0.4 assert falses > NUM_TRIALS * 0.4 assert hd.pdf(False) == 0.5 assert hd.pdf(0.) == 0.5 assert hd.pdf(True) == 0.5 assert hd.pdf(1.) == 0.5 assert hd.pdf(-0.1) == 0. assert hd.pdf(1.1) == 0. assert hd.cdf(False) == 0.5 assert hd.cdf(0.) == 0.5 assert hd.cdf(True) == 1. assert hd.cdf(1.) == 1. assert hd.cdf(-0.1) == 0. assert hd.cdf(1.1) == 1. assert hd.min() == 0 assert hd.max() == 1 assert abs(hd.mean() - 0.5) < 1e-6 assert abs(hd.std() - 0.5) < 1e-6 assert abs(hd.var() - 0.25) < 1e-6 # Verify that hd mean and variance also correspond to mean and variance of sampling. assert abs(hd.mean() - np.mean(samples)) < 1e-2 assert abs(hd.var() - np.var(samples)) < 1e-2 def test_boolean_distribution_with_proba(): proba_is_true = 0.7 hd = Boolean(proba_is_true=proba_is_true) samples = get_many_samples_for(hd) falses = Counter(samples).get(False) trues = Counter(samples).get(True) # You'd need to win the lotto for this test to fail. Or a broken random sampler. Or a bug. assert trues > NUM_TRIALS * (proba_is_true - 0.1) assert falses > NUM_TRIALS * (1 - proba_is_true - 0.1) assert abs(hd.pdf(False) - (1 - proba_is_true)) < 1e-6 assert abs(hd.pdf(0.) - (1 - proba_is_true)) < 1e-6 assert abs(hd.pdf(True) - proba_is_true) < 1e-6 assert abs(hd.pdf(1.) - proba_is_true) < 1e-6 assert abs(hd.pdf(-0.1) - 0.) < 1e-6 assert abs(hd.pdf(1.1) - 0.) < 1e-6 assert abs(hd.cdf(False) - (1-proba_is_true)) < 1e-6 assert abs(hd.cdf(0.) - (1-proba_is_true)) < 1e-6 assert abs(hd.cdf(True) - 1.) < 1e-6 assert abs(hd.cdf(1.) - 1.) < 1e-6 assert abs(hd.cdf(-0.1) - 0.) < 1e-6 assert abs(hd.cdf(1.1) - 1.) < 1e-6 assert hd.min() == 0 assert hd.max() == 1 assert abs(hd.mean() - proba_is_true) < 1e-6 assert abs(hd.std() - math.sqrt(proba_is_true * (1-proba_is_true))) < 1e-6 assert abs(hd.var() - proba_is_true * (1-proba_is_true)) < 1e-6 # Verify that hd mean and variance also correspond to mean and variance of sampling. assert abs(hd.mean() - np.mean(samples)) < 1e-2 assert abs(hd.var() - np.var(samples)) < 1e-2 @pytest.mark.parametrize(""ctor"", [Choice, PriorityChoice]) def test_choice_and_priority_choice(ctor): choice_list = [0, 1, False, ""Test""] hd = ctor(choice_list) samples = get_many_samples_for(hd) z0 = Counter(samples).get(0) z1 = Counter(samples).get(1) zNone = Counter(samples).get(False) zTest = Counter(samples).get(""Test"") # You'd need to win the lotto for this test to fail. Or a broken random sampler. Or a bug. assert z0 > NUM_TRIALS * 0.2 assert z1 > NUM_TRIALS * 0.2 assert zNone > NUM_TRIALS * 0.2 assert zTest > NUM_TRIALS * 0.2 assert abs(hd.pdf(0) - 1 / 4) < 1e-6 assert abs(hd.pdf(1) - 1 / 4) < 1e-6 assert abs(hd.pdf(False) - 1 / 4) < 1e-6 assert abs(hd.pdf(""Test"") - 1 / 4) < 1e-6 assert abs(hd.cdf(0) - 1 / 4) < 1e-6 assert abs(hd.cdf(1) - 2 / 4) < 1e-6 assert abs(hd.cdf(False) - 3 / 4) < 1e-6 assert hd.cdf(""Test"") == 1. with pytest.raises(ValueError): assert hd.pdf(3) == 0. assert hd.cdf(3) == 0. assert hd.min() == 0 assert hd.max() == len(choice_list) assert abs(hd.mean() - (len(choice_list) - 1) / 2) < 1e-6 assert abs(hd.var() - (len(choice_list) ** 2 - 1) / 12) < 1e-6 assert abs(hd.std() - math.sqrt((len(choice_list) ** 2 - 1) / 12)) < 1e-6 # Convert samples in sample index samples_index = [get_index_in_list_with_bool(choice_list, sample) for sample in samples] # Verify that hd mean and variance also correspond to mean and variance of sampling. assert abs((hd.mean() - np.mean(samples_index)) / hd.mean()) < 1e-1 assert abs((hd.var() - np.var(samples_index)) / hd.var()) < 1e-1 @pytest.mark.parametrize(""ctor"", [Choice, PriorityChoice]) def test_choice_and_priority_choice_with_probas(ctor): probas = [0.1, 0.4, 0.3, 0.2] probas_array = np.array(probas) choice_list = [0, 1, False, ""Test""] hd = ctor(choice_list, probas=probas) samples = get_many_samples_for(hd) z0 = Counter(samples).get(0) z1 = Counter(samples).get(1) zNone = Counter(samples).get(False) zTest = Counter(samples).get(""Test"") # You'd need to win the lotto for this test to fail. Or a broken random sampler. Or a bug. assert z0 > NUM_TRIALS * (probas[0] - 0.05) assert z1 > NUM_TRIALS * (probas[1] - 0.05) assert zNone > NUM_TRIALS * (probas[2] - 0.05) assert zTest > NUM_TRIALS * (probas[3] - 0.05) assert abs(hd.pdf(0) - probas[0]) < 1e-6 assert abs(hd.pdf(1) - probas[1]) < 1e-6 assert abs(hd.pdf(False) - probas[2]) < 1e-6 assert abs(hd.pdf(""Test"") - probas[3]) < 1e-6 assert abs(hd.cdf(0) - probas_array[0]) < 1e-6 assert abs(hd.cdf(1) - np.sum(probas_array[0:2])) < 1e-6 assert abs(hd.cdf(False) - np.sum(probas_array[0:3])) < 1e-6 assert abs(hd.cdf(""Test"") - 1.) < 1e-6 with pytest.raises(ValueError): assert hd.pdf(3) == 0. assert hd.cdf(3) == 0. assert hd.min() == 0 assert hd.max() == len(choice_list) assert abs(hd.mean() - 1.6) < 1e-6 assert abs(hd.var() - 0.84) < 1e-6 assert abs(hd.std() - 0.9165151389911679) < 1e-6 # Convert samples in sample index samples_index = [get_index_in_list_with_bool(choice_list, sample) for sample in samples] # Verify that hd mean and variance also correspond to mean and variance of sampling. assert abs((hd.mean() - np.mean(samples_index)) / hd.mean()) < 1e-1 assert abs((hd.var() - np.var(samples_index)) / hd.var()) < 1e-1 def test_quantized_uniform(): low = -10 high = 10 hd = Quantized(Uniform(low, high)) samples = get_many_samples_for(hd) for s in samples: assert type(s) == int samples_mean = np.abs(np.mean(samples)) assert samples_mean < 1.0 assert min(samples) >= -10.0 assert max(samples) <= 10.0 assert abs(hd.pdf(-10) - 1 / 40) < 1e-6 assert abs(hd.pdf(-9) - 1 / 20) < 1e-6 assert abs(hd.pdf(0) - 1 / 20) < 1e-6 assert abs(hd.pdf(9) - 1 / 20) < 1e-6 assert abs(hd.pdf(10) - 1 / 40) < 1e-6 assert abs(hd.cdf(-10) - 1 / 40) < 1e-6 assert abs(hd.cdf(-9) - 1.5 / 20) < 1e-6 assert abs(hd.cdf(0) - 10.5 / 20) < 1e-6 assert abs(hd.cdf(9) - 19.5 / 20) < 1e-6 assert abs(hd.cdf(9.2) - 19.5 / 20) < 1e-6 assert hd.cdf(10) == 1. assert hd.min() == low assert hd.max() == high assert abs(hd.mean() - 0.0) < 1e-6 assert abs(hd.var() - 33.50000000000001) < 1e-6 assert abs(hd.std() - 5.787918451395114) < 1e-6 # Verify that hd mean and variance also correspond to mean and variance of sampling. assert abs(hd.mean() - np.mean(samples)) < 1e-1 assert abs((hd.var() - np.var(samples)) / hd.var()) < 1e-1 def test_randint(): low = -10 high = 10 hd = RandInt(low, high) samples = get_many_samples_for(hd) for s in samples: assert type(s) == int samples_mean = np.abs(np.mean(samples)) assert samples_mean < 1.0 assert min(samples) >= -10.0 assert max(samples) <= 10.0 assert hd.pdf(-11) == 0. assert abs(hd.pdf(-10) - 1 / (10 + 10 + 1)) < 1e-6 assert abs(hd.pdf(0) - 1 / (10 + 10 + 1)) < 1e-6 assert hd.pdf(0.5) == 0. assert abs(hd.pdf(10) - 1 / (10 + 10 + 1)) < 1e-6 assert hd.pdf(11) == 0. assert hd.cdf(-10.1) == 0. assert abs(hd.cdf(-10) - 1 / (10 + 10 + 1)) < 1e-6 assert abs(hd.cdf(0) - (0 + 10 + 1) / (10 + 10 + 1)) < 1e-6 assert abs(hd.cdf(5) - (5 + 10 + 1) / (10 + 10 + 1)) < 1e-6 assert abs(hd.cdf(10) - 1.) < 1e-6 assert hd.cdf(10.1) == 1. assert hd.min() == low assert hd.max() == high assert abs(hd.mean() - (10 - 10) / 2) < 1e-6 assert abs(hd.var() - ((high - low + 1) ** 2 - 1) / 12) < 1e-6 assert abs(hd.std() - math.sqrt(((high - low + 1) ** 2 - 1) / 12)) < 1e-6 # Verify that hd mean and variance also correspond to mean and variance of sampling. assert abs(hd.mean() - np.mean(samples)) < 1e-1 assert abs((hd.var() - np.var(samples)) / hd.var()) < 1e-1 def test_uniform(): low = -10 high = 10 hd = Uniform(low, high) samples = get_many_samples_for(hd) samples_mean = np.abs(np.mean(samples)) assert samples_mean < 1.0 assert min(samples) >= -10.0 assert max(samples) <= 10.0 assert hd.pdf(-10.1) == 0. assert abs(hd.pdf(0) - 1 / (10 + 10)) < 1e-6 assert hd.pdf(10.1) == 0. assert hd.cdf(-10.1) == 0. assert abs(hd.cdf(0) - (0 + 10) / (10 + 10)) < 1e-6 assert hd.cdf(10.1) == 1. assert hd.min() == low assert hd.max() == high assert abs(hd.mean() - (10 - 10) / 2) < 1e-6 assert abs(hd.var() - 1 / 12 ", https://github.com/neurodsp-tools/neurodsp,19aa50c71deaa19e4ed85efa192afa8ab63d0bf1,neurodsp/tests/utils/test_download.py::test_check_data_file,neurodsp/tests/utils/test_download.py,OD-Brit,flaky,Rejected,https://github.com/neurodsp-tools/neurodsp/pull/308,classify;root_cause,"""""""Test functions for neurodsp.utils.download."""""" import os import shutil import numpy as np from neurodsp.utils.download import * ################################################################################################### ################################################################################################### TEST_FOLDER = 'test_data' def clean_up_downloads(): shutil.rmtree(TEST_FOLDER) ################################################################################################### ################################################################################################### def test_check_data_folder(): check_data_folder(TEST_FOLDER) assert os.path.isdir(TEST_FOLDER) def test_check_data_file(): filename = 'sample_data_1.npy' check_data_file(filename, TEST_FOLDER) assert os.path.isfile(os.path.join(TEST_FOLDER, filename)) def test_fetch_ndsp_data(): filename = 'sample_data_2.npy' fetch_ndsp_data(filename, folder=TEST_FOLDER) assert os.path.isfile(os.path.join(TEST_FOLDER, filename)) clean_up_downloads() def test_load_ndsp_data(): filename = 'sample_data_1.npy' data = load_ndsp_data(filename, folder=TEST_FOLDER) assert isinstance(data, np.ndarray) clean_up_downloads() ", https://github.com/nficano/pytube,b610f399722e001e99ee88d3673c148d903c5948,tests/test_streams.py::test_on_progress_hook,tests/test_streams.py,NIO,flaky,nan,nan,classify;root_cause,"# -*- coding: utf-8 -*- import os import random from datetime import datetime from unittest import mock from unittest.mock import MagicMock, Mock from pytube import request from pytube import Stream @mock.patch(""pytube.streams.request"") def test_stream_to_buffer(mock_request, cipher_signature): # Given stream_bytes = iter( [ bytes(os.urandom(8 * 1024)), bytes(os.urandom(8 * 1024)), bytes(os.urandom(8 * 1024)), ] ) mock_request.stream.return_value = stream_bytes buffer = MagicMock() # When cipher_signature.streams[0].stream_to_buffer(buffer) # Then assert buffer.write.call_count == 3 @mock.patch( ""pytube.streams.request.head"", MagicMock(return_value={""content-length"": ""6796391""}) ) def test_filesize(cipher_signature): assert cipher_signature.streams[0].filesize == 6796391 @mock.patch( ""pytube.streams.request.head"", MagicMock(return_value={""content-length"": ""6796391""}) ) def test_filesize_approx(cipher_signature): stream = cipher_signature.streams[0] assert stream.filesize_approx == 22350604 stream.bitrate = None assert stream.filesize_approx == 6796391 def test_default_filename(cipher_signature): expected = ""PSY - GANGNAM STYLE(강남스타일) MV.mp4"" stream = cipher_signature.streams[0] assert stream.default_filename == expected def test_title(cipher_signature): expected = ""title"" cipher_signature.player_config_args[""title""] = expected assert cipher_signature.title == expected expected = ""title2"" del cipher_signature.player_config_args[""title""] cipher_signature.player_response = {""videoDetails"": {""title"": expected}} assert cipher_signature.title == expected def test_expiration(cipher_signature): assert cipher_signature.streams[0].expiration == datetime(2020, 1, 16, 5, 12, 5) def test_caption_tracks(presigned_video): assert len(presigned_video.caption_tracks) == 13 def test_captions(presigned_video): assert len(presigned_video.captions) == 13 def test_description(cipher_signature): expected = ( ""PSY - ‘I LUV IT’ M/V @ https://youtu.be/Xvjnoagk6GU\n"" ""PSY - ‘New Face’ M/V @https://youtu.be/OwJPPaEyqhI\n\n"" ""PSY - 8TH ALBUM '4X2=8' on iTunes @\n"" ""https://smarturl.it/PSY_8thAlbum\n\n"" ""PSY - GANGNAM STYLE(강남스타일) on iTunes @ "" ""http://smarturl.it/PsyGangnam\n\n"" ""#PSY #싸이 #GANGNAMSTYLE #강남스타일\n\n"" ""More about PSY@\nhttp://www.youtube.com/officialpsy\n"" ""http://www.facebook.com/officialpsy\n"" ""http://twitter.com/psy_oppa\n"" ""https://www.instagram.com/42psy42\n"" ""http://iTunes.com/PSY\n"" ""http://sptfy.com/PSY\n"" ""http://weibo.com/psyoppa"" ) assert cipher_signature.description == expected cipher_signature.player_response = {} expected = ( ""PSY - ‘I LUV IT’ M/V @ https://youtu.be/Xvjnoagk6GU\n"" ""PSY - ‘New Face’ M/V @https://youtu.be/OwJPPaEyqhI\n"" ""PSY - 8TH ALBUM '4X2=8' on iTunes @\n"" ""https://smarturl.it/PSY_8thAlbum\n"" ""PSY - GANGNAM STYLE(강남스타일) on iTunes @ http://smarturl.it/PsyGangnam\n"" ""#PSY #싸이 #GANGNAMSTYLE #강남스타일\n"" ""More about PSY@\nhttp://www.youtube.com/officialpsy\n"" ""http://www.facebook.com/officialpsy\n"" ""http://twitter.com/psy_oppa\n"" ""https://www.instagram.com/42psy42\n"" ""http://iTunes.com/PSY\n"" ""http://sptfy.com/PSY\n"" ""http://weibo.com/psyoppa"" ) assert cipher_signature.description == expected def test_rating(cipher_signature): assert cipher_signature.rating == 4.522203 def test_length(cipher_signature): assert cipher_signature.length == 252 def test_views(cipher_signature): assert cipher_signature.views == 3494704859 @mock.patch( ""pytube.streams.request.head"", MagicMock(return_value={""content-length"": ""6796391""}) ) @mock.patch( ""pytube.streams.request.stream"", MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])), ) def test_download(cipher_signature): with mock.patch(""pytube.streams.open"", mock.mock_open(), create=True): stream = cipher_signature.streams[0] stream.download() @mock.patch( ""pytube.streams.request.head"", MagicMock(return_value={""content-length"": ""16384""}) ) @mock.patch( ""pytube.streams.request.stream"", MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])), ) @mock.patch(""pytube.streams.target_directory"", MagicMock(return_value=""/target"")) def test_download_with_prefix(cipher_signature): with mock.patch(""pytube.streams.open"", mock.mock_open(), create=True): stream = cipher_signature.streams[0] file_path = stream.download(filename_prefix=""prefix"") assert file_path == ""/target/prefixPSY - GANGNAM STYLE(강남스타일) MV.mp4"" @mock.patch( ""pytube.streams.request.head"", MagicMock(return_value={""content-length"": ""16384""}) ) @mock.patch( ""pytube.streams.request.stream"", MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])), ) @mock.patch(""pytube.streams.target_directory"", MagicMock(return_value=""/target"")) def test_download_with_filename(cipher_signature): with mock.patch(""pytube.streams.open"", mock.mock_open(), create=True): stream = cipher_signature.streams[0] file_path = stream.download(filename=""cool name bro"") assert file_path == ""/target/cool name bro.mp4"" @mock.patch( ""pytube.streams.request.head"", MagicMock(return_value={""content-length"": ""16384""}) ) @mock.patch( ""pytube.streams.request.stream"", MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])), ) @mock.patch(""pytube.streams.target_directory"", MagicMock(return_value=""/target"")) @mock.patch(""os.path.isfile"", MagicMock(return_value=True)) def test_download_with_existing(cipher_signature): with mock.patch(""pytube.streams.open"", mock.mock_open(), create=True): stream = cipher_signature.streams[0] os.path.getsize = Mock(return_value=stream.filesize) file_path = stream.download() assert file_path == ""/target/PSY - GANGNAM STYLE(강남스타일) MV.mp4"" assert not request.stream.called @mock.patch( ""pytube.streams.request.head"", MagicMock(return_value={""content-length"": ""16384""}) ) @mock.patch( ""pytube.streams.request.stream"", MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])), ) @mock.patch(""pytube.streams.target_directory"", MagicMock(return_value=""/target"")) @mock.patch(""os.path.isfile"", MagicMock(return_value=True)) def test_download_with_existing_no_skip(cipher_signature): with mock.patch(""pytube.streams.open"", mock.mock_open(), create=True): stream = cipher_signature.streams[0] os.path.getsize = Mock(return_value=stream.filesize) file_path = stream.download(skip_existing=False) assert file_path == ""/target/PSY - GANGNAM STYLE(강남스타일) MV.mp4"" assert request.stream.called def test_progressive_streams_return_includes_audio_track(cipher_signature): stream = cipher_signature.streams.filter(progressive=True)[0] assert stream.includes_audio_track def test_progressive_streams_return_includes_video_track(cipher_signature): stream = cipher_signature.streams.filter(progressive=True)[0] assert stream.includes_video_track @mock.patch( ""pytube.streams.request.head"", MagicMock(return_value={""content-length"": ""16384""}) ) @mock.patch( ""pytube.streams.request.stream"", MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])), ) def test_on_progress_hook(cipher_signature): callback_fn = mock.MagicMock() cipher_signature.register_on_progress_callback(callback_fn) with mock.patch(""pytube.streams.open"", mock.mock_open(), create=True): stream = cipher_signature.streams[0] stream.download() assert callback_fn.called args, _ = callback_fn.call_args assert len(args) == 3 stream, _, _ = args assert isinstance(stream, Stream) @mock.patch( ""pytube.streams.request.head"", MagicMock(return_value={""content-length"": ""16384""}) ) @mock.patch( ""pytube.streams.request.stream"", MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])), ) def test_on_complete_hook(cipher_signature): callback_fn = mock.MagicMock() cipher_signature.register_on_complete_callback(callback_fn) with mock.patch(""pytube.streams.open"", mock.mock_open(), create=True): stream = cipher_signature.streams[0] stream.download() assert callback_fn.called def test_author(cipher_signature): expected = ""Test author"" cipher_signature.player_response = {""videoDetails"": {""author"": expected}} assert cipher_signature.author == expected expected = ""unknown"" cipher_signature.player_response = {} assert cipher_signature.author == expected def test_thumbnail_when_in_details(cipher_signature): expected = ""some url"" cipher_signature.player_response = { ""videoDetails"": {""thumbnail"": {""thumbnails"": [{""url"": expected}]}} } assert cipher_signature.thumbnail_url == expected def test_thumbnail_when_not_in_details(cipher_signature): expected = ""https://img.youtube.com/vi/9bZkp7q19f0/maxresdefault.jpg"" cipher_signature.player_response = {} assert cipher_signature.thumbnail_url == expected def test_repr_for_audio_streams(cipher_signature): stream = str(cipher_signature.streams.filter(only_audio=True)[0]) expected = ( '' ) assert stream == expected def test_repr_for_video_streams(cipher_signature): stream = str(cipher_signature.streams.filter(only_video=True)[0]) expected = ( '' ) assert stream == expected ", https://github.com/nicolaszein/autodiscover,3009f81c51b7a4cb077427e429d740db98eb173b,tests/test_autodiscover.py::TestAutoDiscover::test_autodiscover_with_pattern,tests/test_autodiscover.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import sys import unittest import pathlib from autodiscover import AutoDiscover PATH = 'tests/module_to_import' class TestAutoDiscover(unittest.TestCase): def test_autodiscover(self): path = pathlib.Path(PATH) autodiscover = AutoDiscover(path=path) module = 'tests.module_to_import' self.assertNotIn(module, sys.modules) autodiscover() self.assertIn(module, sys.modules) def test_autodiscover_with_pattern(self): path = pathlib.Path(PATH) autodiscover = AutoDiscover(path=path, pattern='pattern.py') module = 'tests.module_to_import.pattern' sys.modules.pop(module) autodiscover() self.assertIn(module, sys.modules) ", https://github.com/nir0s/clu,77da967a4577ca4cf100cfe34e87b39ad88bf21c,tests/test_ghost.py::TestCLI::test_fail_init_two_stashes_passphrase_file_exists,tests/test_ghost.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# Copyright 2015,2016 Nir Cohen # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import time import json import shlex import base64 import shutil import tempfile import subprocess import mock import pytest import click.testing as clicktest import hvac # NOQA import botocore import elasticsearch # NOQA from moto import mock_s3 from sqlalchemy import sql from sqlalchemy import inspect from sqlalchemy import create_engine import ghost TEST_PASSPHRASE = 'a' def _invoke(command): cfy = clicktest.CliRunner() lexed_command = command if isinstance(command, list) \ else shlex.split(command) func = lexed_command[0] params = lexed_command[1:] return cfy.invoke(getattr(ghost, func), params) class TestGeneral: def test_get_current_time(self): assert len(ghost._get_current_time()) == 19 def test_generate_passphrase(self): passphrase = ghost.generate_passphrase() assert len(passphrase) == 12 assert isinstance(passphrase, str) longer_passphrase = ghost.generate_passphrase(13) assert len(longer_passphrase) == 13 def test_build_dict_from_key_value(self): key_values = ['a=b', 'c=d'] key_dict = ghost._build_dict_from_key_value(key_values) assert isinstance(key_dict, dict) assert 'a' in key_dict assert 'c' in key_dict assert key_dict.get('a') == 'b' assert key_dict.get('c') == 'd' def test_build_dict_no_key_equals_value(self): key_values = ['a=b', 'cd'] with pytest.raises(ghost.GhostError): ghost._build_dict_from_key_value(key_values) def test_prettify_dict(self): input = dict( description='a', uid='b', created_at='c', metadata={'x': 'y'}, modified='e', value={'key': 'value'}, name='g') prettified_input = ghost._prettify_dict(input).splitlines() assert 'Description: a' in prettified_input assert 'Uid: b' in prettified_input assert 'Created_At: c' in prettified_input assert 'Metadata: x=y;' in prettified_input assert 'Modified: e' in prettified_input assert 'Value: key=value;' in prettified_input assert 'Name: g' in prettified_input def test_prettify_dict_input_not_dict(self): with pytest.raises(AssertionError): ghost._prettify_dict('') def test_prettify_list(self): input = ['a', 'b', 'c'] prettified_input = ghost._prettify_list(input).splitlines() for line in prettified_input: assert ' - a' in prettified_input assert ' - b' in prettified_input assert ' - c' in prettified_input def test_prettify_list_input_not_list(self): with pytest.raises(AssertionError): ghost._prettify_list('') def test_get_passphrase(self): def _make_temp_passphrase_file(): fd, temp_file_path = tempfile.mkstemp() os.close(fd) os.remove(temp_file_path) return temp_file_path tempfile1 = _make_temp_passphrase_file() tempfile2 = _make_temp_passphrase_file() passphrase = '123' assert isinstance(ghost.POTENTIAL_PASSPHRASE_LOCATIONS, list) ghost.POTENTIAL_PASSPHRASE_LOCATIONS = [tempfile1, tempfile2] assert ghost.get_passphrase() is None for passphrase_file_path in ghost.POTENTIAL_PASSPHRASE_LOCATIONS: try: with open(passphrase_file_path, 'w') as passphrase_file: passphrase_file.write(passphrase) assert ghost.get_passphrase() == passphrase finally: os.remove(passphrase_file_path) assert passphrase is ghost.get_passphrase(passphrase) def _create_temp_file(): fd, temp_file = tempfile.mkstemp() os.close(fd) print('PATH: {0}'.format(temp_file)) try: os.remove(temp_file) except: pass return temp_file @pytest.fixture def stash_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass @pytest.fixture def temp_file_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass def get_tinydb(path): with open(path) as db: return json.loads(db.read())['ghost'] class TestStorage(object): @staticmethod def is_initialized(structure): assert isinstance(structure, bool) @staticmethod def put(structure): assert isinstance(structure, (str, int)) @staticmethod def get_nonexisting_key(structure): assert isinstance(structure, dict) assert not structure @staticmethod def get(structure): assert isinstance(structure, dict) assert isinstance(structure['value'], dict) assert isinstance(structure['metadata'], dict) str(structure['name']) str(structure['uid']) str(structure['description']) @staticmethod def delete(structure): assert isinstance(structure, bool) def list(self, structure): assert isinstance(structure, list) for key in structure: self.get(key) @staticmethod def empty_list(structure): assert isinstance(structure, list) assert len(structure) == 0 storage_tester = TestStorage() BASE_TEST_KEY = { 'name': 'my_key', 'value': {'a': 'b'}, 'metadata': {}, 'created_at': '', 'modified_at': '', 'uid': '', 'description': 'My Key' } class TestTinyDBStorage: def test_init(self): tmpdir = tempfile.mkdtemp() shutil.rmtree(tmpdir, ignore_errors=True) assert not os.path.isdir(tmpdir) stash_path = os.path.join(tmpdir, 'stash.json') storage = ghost.TinyDBStorage(stash_path) try: storage.init() assert os.path.isdir(tmpdir) finally: shutil.rmtree(tmpdir, ignore_errors=True) def test_init_stash_already_exists(self): fd, stash_path = tempfile.mkstemp() os.close(fd) try: storage = ghost.TinyDBStorage(stash_path) storage.init() finally: os.remove(stash_path) def test_init_stash_create_directory(self): stash_dir = tempfile.mkdtemp() shutil.rmtree(stash_dir, ignore_errors=True) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) assert os.path.isdir(stash_dir) is False storage.init() assert os.path.isdir(stash_dir) is True finally: shutil.rmtree(stash_dir, ignore_errors=True) def test_init_stash_in_current_dir(self): """"""Test this because it depends on the ability of the storage to understand whether it should or should not create a directory. """""" prev_dir = os.getcwd() stash_dir = tempfile.mkdtemp() os.chdir(stash_dir) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert os.path.isfile(stash_path) is False stash.init() assert os.path.isfile(stash_path) is True finally: os.chdir(prev_dir) shutil.rmtree(stash_dir, ignore_errors=True) def test_is_initialized(self, stash_path): storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert storage.is_initialized is False stash.init() assert storage.is_initialized is True storage_tester.is_initialized(storage.is_initialized) def test_put(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_id = storage.put(BASE_TEST_KEY) db = get_tinydb(stash_path) assert '1' in db assert db['1']['name'] == BASE_TEST_KEY['name'] assert len(db) == 1 storage_tester.put(key_id) def test_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) storage.put(BASE_TEST_KEY) key_list = storage.list() assert BASE_TEST_KEY in key_list assert len(key_list) == 1 storage_tester.list(key_list) def test_empty_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_list = storage.list() storage_tester.empty_list(key_list) def test_get_delete(self, stash_path): inserted_key = BASE_TEST_KEY storage = ghost.TinyDBStorage(stash_path) storage.put(inserted_key) retrieved_key = storage.get(BASE_TEST_KEY['name']) assert inserted_key == retrieved_key storage_tester.get(retrieved_key) result = storage.delete(BASE_TEST_KEY['name']) storage_tester.delete(result) key = storage.get(BASE_TEST_KEY['name']) storage_tester.get_nonexisting_key(key) class TestSQLAlchemyStorage: def test_missing_requirement(self): with mock.patch('ghost.SQLALCHEMY_EXISTS', False): with pytest.raises(ImportError): ghost.SQLAlchemyStorage() def test_init(self): tmpdir = os.pa", https://github.com/nir0s/clu,77da967a4577ca4cf100cfe34e87b39ad88bf21c,tests/test_ghost.py::TestCLI::test_init_already_initialized,tests/test_ghost.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"# Copyright 2015,2016 Nir Cohen # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import time import json import shlex import base64 import shutil import tempfile import subprocess import mock import pytest import click.testing as clicktest import hvac # NOQA import botocore import elasticsearch # NOQA from moto import mock_s3 from sqlalchemy import sql from sqlalchemy import inspect from sqlalchemy import create_engine import ghost TEST_PASSPHRASE = 'a' def _invoke(command): cfy = clicktest.CliRunner() lexed_command = command if isinstance(command, list) \ else shlex.split(command) func = lexed_command[0] params = lexed_command[1:] return cfy.invoke(getattr(ghost, func), params) class TestGeneral: def test_get_current_time(self): assert len(ghost._get_current_time()) == 19 def test_generate_passphrase(self): passphrase = ghost.generate_passphrase() assert len(passphrase) == 12 assert isinstance(passphrase, str) longer_passphrase = ghost.generate_passphrase(13) assert len(longer_passphrase) == 13 def test_build_dict_from_key_value(self): key_values = ['a=b', 'c=d'] key_dict = ghost._build_dict_from_key_value(key_values) assert isinstance(key_dict, dict) assert 'a' in key_dict assert 'c' in key_dict assert key_dict.get('a') == 'b' assert key_dict.get('c') == 'd' def test_build_dict_no_key_equals_value(self): key_values = ['a=b', 'cd'] with pytest.raises(ghost.GhostError): ghost._build_dict_from_key_value(key_values) def test_prettify_dict(self): input = dict( description='a', uid='b', created_at='c', metadata={'x': 'y'}, modified='e', value={'key': 'value'}, name='g') prettified_input = ghost._prettify_dict(input).splitlines() assert 'Description: a' in prettified_input assert 'Uid: b' in prettified_input assert 'Created_At: c' in prettified_input assert 'Metadata: x=y;' in prettified_input assert 'Modified: e' in prettified_input assert 'Value: key=value;' in prettified_input assert 'Name: g' in prettified_input def test_prettify_dict_input_not_dict(self): with pytest.raises(AssertionError): ghost._prettify_dict('') def test_prettify_list(self): input = ['a', 'b', 'c'] prettified_input = ghost._prettify_list(input).splitlines() for line in prettified_input: assert ' - a' in prettified_input assert ' - b' in prettified_input assert ' - c' in prettified_input def test_prettify_list_input_not_list(self): with pytest.raises(AssertionError): ghost._prettify_list('') def test_get_passphrase(self): def _make_temp_passphrase_file(): fd, temp_file_path = tempfile.mkstemp() os.close(fd) os.remove(temp_file_path) return temp_file_path tempfile1 = _make_temp_passphrase_file() tempfile2 = _make_temp_passphrase_file() passphrase = '123' assert isinstance(ghost.POTENTIAL_PASSPHRASE_LOCATIONS, list) ghost.POTENTIAL_PASSPHRASE_LOCATIONS = [tempfile1, tempfile2] assert ghost.get_passphrase() is None for passphrase_file_path in ghost.POTENTIAL_PASSPHRASE_LOCATIONS: try: with open(passphrase_file_path, 'w') as passphrase_file: passphrase_file.write(passphrase) assert ghost.get_passphrase() == passphrase finally: os.remove(passphrase_file_path) assert passphrase is ghost.get_passphrase(passphrase) def _create_temp_file(): fd, temp_file = tempfile.mkstemp() os.close(fd) print('PATH: {0}'.format(temp_file)) try: os.remove(temp_file) except: pass return temp_file @pytest.fixture def stash_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass @pytest.fixture def temp_file_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass def get_tinydb(path): with open(path) as db: return json.loads(db.read())['ghost'] class TestStorage(object): @staticmethod def is_initialized(structure): assert isinstance(structure, bool) @staticmethod def put(structure): assert isinstance(structure, (str, int)) @staticmethod def get_nonexisting_key(structure): assert isinstance(structure, dict) assert not structure @staticmethod def get(structure): assert isinstance(structure, dict) assert isinstance(structure['value'], dict) assert isinstance(structure['metadata'], dict) str(structure['name']) str(structure['uid']) str(structure['description']) @staticmethod def delete(structure): assert isinstance(structure, bool) def list(self, structure): assert isinstance(structure, list) for key in structure: self.get(key) @staticmethod def empty_list(structure): assert isinstance(structure, list) assert len(structure) == 0 storage_tester = TestStorage() BASE_TEST_KEY = { 'name': 'my_key', 'value': {'a': 'b'}, 'metadata': {}, 'created_at': '', 'modified_at': '', 'uid': '', 'description': 'My Key' } class TestTinyDBStorage: def test_init(self): tmpdir = tempfile.mkdtemp() shutil.rmtree(tmpdir, ignore_errors=True) assert not os.path.isdir(tmpdir) stash_path = os.path.join(tmpdir, 'stash.json') storage = ghost.TinyDBStorage(stash_path) try: storage.init() assert os.path.isdir(tmpdir) finally: shutil.rmtree(tmpdir, ignore_errors=True) def test_init_stash_already_exists(self): fd, stash_path = tempfile.mkstemp() os.close(fd) try: storage = ghost.TinyDBStorage(stash_path) storage.init() finally: os.remove(stash_path) def test_init_stash_create_directory(self): stash_dir = tempfile.mkdtemp() shutil.rmtree(stash_dir, ignore_errors=True) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) assert os.path.isdir(stash_dir) is False storage.init() assert os.path.isdir(stash_dir) is True finally: shutil.rmtree(stash_dir, ignore_errors=True) def test_init_stash_in_current_dir(self): """"""Test this because it depends on the ability of the storage to understand whether it should or should not create a directory. """""" prev_dir = os.getcwd() stash_dir = tempfile.mkdtemp() os.chdir(stash_dir) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert os.path.isfile(stash_path) is False stash.init() assert os.path.isfile(stash_path) is True finally: os.chdir(prev_dir) shutil.rmtree(stash_dir, ignore_errors=True) def test_is_initialized(self, stash_path): storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert storage.is_initialized is False stash.init() assert storage.is_initialized is True storage_tester.is_initialized(storage.is_initialized) def test_put(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_id = storage.put(BASE_TEST_KEY) db = get_tinydb(stash_path) assert '1' in db assert db['1']['name'] == BASE_TEST_KEY['name'] assert len(db) == 1 storage_tester.put(key_id) def test_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) storage.put(BASE_TEST_KEY) key_list = storage.list() assert BASE_TEST_KEY in key_list assert len(key_list) == 1 storage_tester.list(key_list) def test_empty_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_list = storage.list() storage_tester.empty_list(key_list) def test_get_delete(self, stash_path): inserted_key = BASE_TEST_KEY storage = ghost.TinyDBStorage(stash_path) storage.put(inserted_key) retrieved_key = storage.get(BASE_TEST_KEY['name']) assert inserted_key == retrieved_key storage_tester.get(retrieved_key) result = storage.delete(BASE_TEST_KEY['name']) storage_tester.delete(result) key = storage.get(BASE_TEST_KEY['name']) storage_tester.get_nonexisting_key(key) class TestSQLAlchemyStorage: def test_missing_requirement(self): with mock.patch('ghost.SQLALCHEMY_EXISTS', False): with pytest.raises(ImportError): ghost.SQLAlchemyStorage() def test_init(self): tmpdir = os.pa", https://github.com/nir0s/clu,77da967a4577ca4cf100cfe34e87b39ad88bf21c,tests/test_ghost.py::TestCLI::test_init_permission_denied_on_passphrase,tests/test_ghost.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"# Copyright 2015,2016 Nir Cohen # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import time import json import shlex import base64 import shutil import tempfile import subprocess import mock import pytest import click.testing as clicktest import hvac # NOQA import botocore import elasticsearch # NOQA from moto import mock_s3 from sqlalchemy import sql from sqlalchemy import inspect from sqlalchemy import create_engine import ghost TEST_PASSPHRASE = 'a' def _invoke(command): cfy = clicktest.CliRunner() lexed_command = command if isinstance(command, list) \ else shlex.split(command) func = lexed_command[0] params = lexed_command[1:] return cfy.invoke(getattr(ghost, func), params) class TestGeneral: def test_get_current_time(self): assert len(ghost._get_current_time()) == 19 def test_generate_passphrase(self): passphrase = ghost.generate_passphrase() assert len(passphrase) == 12 assert isinstance(passphrase, str) longer_passphrase = ghost.generate_passphrase(13) assert len(longer_passphrase) == 13 def test_build_dict_from_key_value(self): key_values = ['a=b', 'c=d'] key_dict = ghost._build_dict_from_key_value(key_values) assert isinstance(key_dict, dict) assert 'a' in key_dict assert 'c' in key_dict assert key_dict.get('a') == 'b' assert key_dict.get('c') == 'd' def test_build_dict_no_key_equals_value(self): key_values = ['a=b', 'cd'] with pytest.raises(ghost.GhostError): ghost._build_dict_from_key_value(key_values) def test_prettify_dict(self): input = dict( description='a', uid='b', created_at='c', metadata={'x': 'y'}, modified='e', value={'key': 'value'}, name='g') prettified_input = ghost._prettify_dict(input).splitlines() assert 'Description: a' in prettified_input assert 'Uid: b' in prettified_input assert 'Created_At: c' in prettified_input assert 'Metadata: x=y;' in prettified_input assert 'Modified: e' in prettified_input assert 'Value: key=value;' in prettified_input assert 'Name: g' in prettified_input def test_prettify_dict_input_not_dict(self): with pytest.raises(AssertionError): ghost._prettify_dict('') def test_prettify_list(self): input = ['a', 'b', 'c'] prettified_input = ghost._prettify_list(input).splitlines() for line in prettified_input: assert ' - a' in prettified_input assert ' - b' in prettified_input assert ' - c' in prettified_input def test_prettify_list_input_not_list(self): with pytest.raises(AssertionError): ghost._prettify_list('') def test_get_passphrase(self): def _make_temp_passphrase_file(): fd, temp_file_path = tempfile.mkstemp() os.close(fd) os.remove(temp_file_path) return temp_file_path tempfile1 = _make_temp_passphrase_file() tempfile2 = _make_temp_passphrase_file() passphrase = '123' assert isinstance(ghost.POTENTIAL_PASSPHRASE_LOCATIONS, list) ghost.POTENTIAL_PASSPHRASE_LOCATIONS = [tempfile1, tempfile2] assert ghost.get_passphrase() is None for passphrase_file_path in ghost.POTENTIAL_PASSPHRASE_LOCATIONS: try: with open(passphrase_file_path, 'w') as passphrase_file: passphrase_file.write(passphrase) assert ghost.get_passphrase() == passphrase finally: os.remove(passphrase_file_path) assert passphrase is ghost.get_passphrase(passphrase) def _create_temp_file(): fd, temp_file = tempfile.mkstemp() os.close(fd) print('PATH: {0}'.format(temp_file)) try: os.remove(temp_file) except: pass return temp_file @pytest.fixture def stash_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass @pytest.fixture def temp_file_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass def get_tinydb(path): with open(path) as db: return json.loads(db.read())['ghost'] class TestStorage(object): @staticmethod def is_initialized(structure): assert isinstance(structure, bool) @staticmethod def put(structure): assert isinstance(structure, (str, int)) @staticmethod def get_nonexisting_key(structure): assert isinstance(structure, dict) assert not structure @staticmethod def get(structure): assert isinstance(structure, dict) assert isinstance(structure['value'], dict) assert isinstance(structure['metadata'], dict) str(structure['name']) str(structure['uid']) str(structure['description']) @staticmethod def delete(structure): assert isinstance(structure, bool) def list(self, structure): assert isinstance(structure, list) for key in structure: self.get(key) @staticmethod def empty_list(structure): assert isinstance(structure, list) assert len(structure) == 0 storage_tester = TestStorage() BASE_TEST_KEY = { 'name': 'my_key', 'value': {'a': 'b'}, 'metadata': {}, 'created_at': '', 'modified_at': '', 'uid': '', 'description': 'My Key' } class TestTinyDBStorage: def test_init(self): tmpdir = tempfile.mkdtemp() shutil.rmtree(tmpdir, ignore_errors=True) assert not os.path.isdir(tmpdir) stash_path = os.path.join(tmpdir, 'stash.json') storage = ghost.TinyDBStorage(stash_path) try: storage.init() assert os.path.isdir(tmpdir) finally: shutil.rmtree(tmpdir, ignore_errors=True) def test_init_stash_already_exists(self): fd, stash_path = tempfile.mkstemp() os.close(fd) try: storage = ghost.TinyDBStorage(stash_path) storage.init() finally: os.remove(stash_path) def test_init_stash_create_directory(self): stash_dir = tempfile.mkdtemp() shutil.rmtree(stash_dir, ignore_errors=True) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) assert os.path.isdir(stash_dir) is False storage.init() assert os.path.isdir(stash_dir) is True finally: shutil.rmtree(stash_dir, ignore_errors=True) def test_init_stash_in_current_dir(self): """"""Test this because it depends on the ability of the storage to understand whether it should or should not create a directory. """""" prev_dir = os.getcwd() stash_dir = tempfile.mkdtemp() os.chdir(stash_dir) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert os.path.isfile(stash_path) is False stash.init() assert os.path.isfile(stash_path) is True finally: os.chdir(prev_dir) shutil.rmtree(stash_dir, ignore_errors=True) def test_is_initialized(self, stash_path): storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert storage.is_initialized is False stash.init() assert storage.is_initialized is True storage_tester.is_initialized(storage.is_initialized) def test_put(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_id = storage.put(BASE_TEST_KEY) db = get_tinydb(stash_path) assert '1' in db assert db['1']['name'] == BASE_TEST_KEY['name'] assert len(db) == 1 storage_tester.put(key_id) def test_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) storage.put(BASE_TEST_KEY) key_list = storage.list() assert BASE_TEST_KEY in key_list assert len(key_list) == 1 storage_tester.list(key_list) def test_empty_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_list = storage.list() storage_tester.empty_list(key_list) def test_get_delete(self, stash_path): inserted_key = BASE_TEST_KEY storage = ghost.TinyDBStorage(stash_path) storage.put(inserted_key) retrieved_key = storage.get(BASE_TEST_KEY['name']) assert inserted_key == retrieved_key storage_tester.get(retrieved_key) result = storage.delete(BASE_TEST_KEY['name']) storage_tester.delete(result) key = storage.get(BASE_TEST_KEY['name']) storage_tester.get_nonexisting_key(key) class TestSQLAlchemyStorage: def test_missing_requirement(self): with mock.patch('ghost.SQLALCHEMY_EXISTS', False): with pytest.raises(ImportError): ghost.SQLAlchemyStorage() def test_init(self): tmpdir = os.pa", https://github.com/nir0s/clu,77da967a4577ca4cf100cfe34e87b39ad88bf21c,tests/test_ghost.py::TestSQLAlchemyStorage::test_is_initialized,tests/test_ghost.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# Copyright 2015,2016 Nir Cohen # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import time import json import shlex import base64 import shutil import tempfile import subprocess import mock import pytest import click.testing as clicktest import hvac # NOQA import botocore import elasticsearch # NOQA from moto import mock_s3 from sqlalchemy import sql from sqlalchemy import inspect from sqlalchemy import create_engine import ghost TEST_PASSPHRASE = 'a' def _invoke(command): cfy = clicktest.CliRunner() lexed_command = command if isinstance(command, list) \ else shlex.split(command) func = lexed_command[0] params = lexed_command[1:] return cfy.invoke(getattr(ghost, func), params) class TestGeneral: def test_get_current_time(self): assert len(ghost._get_current_time()) == 19 def test_generate_passphrase(self): passphrase = ghost.generate_passphrase() assert len(passphrase) == 12 assert isinstance(passphrase, str) longer_passphrase = ghost.generate_passphrase(13) assert len(longer_passphrase) == 13 def test_build_dict_from_key_value(self): key_values = ['a=b', 'c=d'] key_dict = ghost._build_dict_from_key_value(key_values) assert isinstance(key_dict, dict) assert 'a' in key_dict assert 'c' in key_dict assert key_dict.get('a') == 'b' assert key_dict.get('c') == 'd' def test_build_dict_no_key_equals_value(self): key_values = ['a=b', 'cd'] with pytest.raises(ghost.GhostError): ghost._build_dict_from_key_value(key_values) def test_prettify_dict(self): input = dict( description='a', uid='b', created_at='c', metadata={'x': 'y'}, modified='e', value={'key': 'value'}, name='g') prettified_input = ghost._prettify_dict(input).splitlines() assert 'Description: a' in prettified_input assert 'Uid: b' in prettified_input assert 'Created_At: c' in prettified_input assert 'Metadata: x=y;' in prettified_input assert 'Modified: e' in prettified_input assert 'Value: key=value;' in prettified_input assert 'Name: g' in prettified_input def test_prettify_dict_input_not_dict(self): with pytest.raises(AssertionError): ghost._prettify_dict('') def test_prettify_list(self): input = ['a', 'b', 'c'] prettified_input = ghost._prettify_list(input).splitlines() for line in prettified_input: assert ' - a' in prettified_input assert ' - b' in prettified_input assert ' - c' in prettified_input def test_prettify_list_input_not_list(self): with pytest.raises(AssertionError): ghost._prettify_list('') def test_get_passphrase(self): def _make_temp_passphrase_file(): fd, temp_file_path = tempfile.mkstemp() os.close(fd) os.remove(temp_file_path) return temp_file_path tempfile1 = _make_temp_passphrase_file() tempfile2 = _make_temp_passphrase_file() passphrase = '123' assert isinstance(ghost.POTENTIAL_PASSPHRASE_LOCATIONS, list) ghost.POTENTIAL_PASSPHRASE_LOCATIONS = [tempfile1, tempfile2] assert ghost.get_passphrase() is None for passphrase_file_path in ghost.POTENTIAL_PASSPHRASE_LOCATIONS: try: with open(passphrase_file_path, 'w') as passphrase_file: passphrase_file.write(passphrase) assert ghost.get_passphrase() == passphrase finally: os.remove(passphrase_file_path) assert passphrase is ghost.get_passphrase(passphrase) def _create_temp_file(): fd, temp_file = tempfile.mkstemp() os.close(fd) print('PATH: {0}'.format(temp_file)) try: os.remove(temp_file) except: pass return temp_file @pytest.fixture def stash_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass @pytest.fixture def temp_file_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass def get_tinydb(path): with open(path) as db: return json.loads(db.read())['ghost'] class TestStorage(object): @staticmethod def is_initialized(structure): assert isinstance(structure, bool) @staticmethod def put(structure): assert isinstance(structure, (str, int)) @staticmethod def get_nonexisting_key(structure): assert isinstance(structure, dict) assert not structure @staticmethod def get(structure): assert isinstance(structure, dict) assert isinstance(structure['value'], dict) assert isinstance(structure['metadata'], dict) str(structure['name']) str(structure['uid']) str(structure['description']) @staticmethod def delete(structure): assert isinstance(structure, bool) def list(self, structure): assert isinstance(structure, list) for key in structure: self.get(key) @staticmethod def empty_list(structure): assert isinstance(structure, list) assert len(structure) == 0 storage_tester = TestStorage() BASE_TEST_KEY = { 'name': 'my_key', 'value': {'a': 'b'}, 'metadata': {}, 'created_at': '', 'modified_at': '', 'uid': '', 'description': 'My Key' } class TestTinyDBStorage: def test_init(self): tmpdir = tempfile.mkdtemp() shutil.rmtree(tmpdir, ignore_errors=True) assert not os.path.isdir(tmpdir) stash_path = os.path.join(tmpdir, 'stash.json') storage = ghost.TinyDBStorage(stash_path) try: storage.init() assert os.path.isdir(tmpdir) finally: shutil.rmtree(tmpdir, ignore_errors=True) def test_init_stash_already_exists(self): fd, stash_path = tempfile.mkstemp() os.close(fd) try: storage = ghost.TinyDBStorage(stash_path) storage.init() finally: os.remove(stash_path) def test_init_stash_create_directory(self): stash_dir = tempfile.mkdtemp() shutil.rmtree(stash_dir, ignore_errors=True) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) assert os.path.isdir(stash_dir) is False storage.init() assert os.path.isdir(stash_dir) is True finally: shutil.rmtree(stash_dir, ignore_errors=True) def test_init_stash_in_current_dir(self): """"""Test this because it depends on the ability of the storage to understand whether it should or should not create a directory. """""" prev_dir = os.getcwd() stash_dir = tempfile.mkdtemp() os.chdir(stash_dir) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert os.path.isfile(stash_path) is False stash.init() assert os.path.isfile(stash_path) is True finally: os.chdir(prev_dir) shutil.rmtree(stash_dir, ignore_errors=True) def test_is_initialized(self, stash_path): storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert storage.is_initialized is False stash.init() assert storage.is_initialized is True storage_tester.is_initialized(storage.is_initialized) def test_put(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_id = storage.put(BASE_TEST_KEY) db = get_tinydb(stash_path) assert '1' in db assert db['1']['name'] == BASE_TEST_KEY['name'] assert len(db) == 1 storage_tester.put(key_id) def test_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) storage.put(BASE_TEST_KEY) key_list = storage.list() assert BASE_TEST_KEY in key_list assert len(key_list) == 1 storage_tester.list(key_list) def test_empty_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_list = storage.list() storage_tester.empty_list(key_list) def test_get_delete(self, stash_path): inserted_key = BASE_TEST_KEY storage = ghost.TinyDBStorage(stash_path) storage.put(inserted_key) retrieved_key = storage.get(BASE_TEST_KEY['name']) assert inserted_key == retrieved_key storage_tester.get(retrieved_key) result = storage.delete(BASE_TEST_KEY['name']) storage_tester.delete(result) key = storage.get(BASE_TEST_KEY['name']) storage_tester.get_nonexisting_key(key) class TestSQLAlchemyStorage: def test_missing_requirement(self): with mock.patch('ghost.SQLALCHEMY_EXISTS', False): with pytest.raises(ImportError): ghost.SQLAlchemyStorage() def test_init(self): tmpdir = os.pa", https://github.com/nir0s/clu,77da967a4577ca4cf100cfe34e87b39ad88bf21c,tests/test_ghost.py::TestStash::test_broken_is_initialized,tests/test_ghost.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"# Copyright 2015,2016 Nir Cohen # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import time import json import shlex import base64 import shutil import tempfile import subprocess import mock import pytest import click.testing as clicktest import hvac # NOQA import botocore import elasticsearch # NOQA from moto import mock_s3 from sqlalchemy import sql from sqlalchemy import inspect from sqlalchemy import create_engine import ghost TEST_PASSPHRASE = 'a' def _invoke(command): cfy = clicktest.CliRunner() lexed_command = command if isinstance(command, list) \ else shlex.split(command) func = lexed_command[0] params = lexed_command[1:] return cfy.invoke(getattr(ghost, func), params) class TestGeneral: def test_get_current_time(self): assert len(ghost._get_current_time()) == 19 def test_generate_passphrase(self): passphrase = ghost.generate_passphrase() assert len(passphrase) == 12 assert isinstance(passphrase, str) longer_passphrase = ghost.generate_passphrase(13) assert len(longer_passphrase) == 13 def test_build_dict_from_key_value(self): key_values = ['a=b', 'c=d'] key_dict = ghost._build_dict_from_key_value(key_values) assert isinstance(key_dict, dict) assert 'a' in key_dict assert 'c' in key_dict assert key_dict.get('a') == 'b' assert key_dict.get('c') == 'd' def test_build_dict_no_key_equals_value(self): key_values = ['a=b', 'cd'] with pytest.raises(ghost.GhostError): ghost._build_dict_from_key_value(key_values) def test_prettify_dict(self): input = dict( description='a', uid='b', created_at='c', metadata={'x': 'y'}, modified='e', value={'key': 'value'}, name='g') prettified_input = ghost._prettify_dict(input).splitlines() assert 'Description: a' in prettified_input assert 'Uid: b' in prettified_input assert 'Created_At: c' in prettified_input assert 'Metadata: x=y;' in prettified_input assert 'Modified: e' in prettified_input assert 'Value: key=value;' in prettified_input assert 'Name: g' in prettified_input def test_prettify_dict_input_not_dict(self): with pytest.raises(AssertionError): ghost._prettify_dict('') def test_prettify_list(self): input = ['a', 'b', 'c'] prettified_input = ghost._prettify_list(input).splitlines() for line in prettified_input: assert ' - a' in prettified_input assert ' - b' in prettified_input assert ' - c' in prettified_input def test_prettify_list_input_not_list(self): with pytest.raises(AssertionError): ghost._prettify_list('') def test_get_passphrase(self): def _make_temp_passphrase_file(): fd, temp_file_path = tempfile.mkstemp() os.close(fd) os.remove(temp_file_path) return temp_file_path tempfile1 = _make_temp_passphrase_file() tempfile2 = _make_temp_passphrase_file() passphrase = '123' assert isinstance(ghost.POTENTIAL_PASSPHRASE_LOCATIONS, list) ghost.POTENTIAL_PASSPHRASE_LOCATIONS = [tempfile1, tempfile2] assert ghost.get_passphrase() is None for passphrase_file_path in ghost.POTENTIAL_PASSPHRASE_LOCATIONS: try: with open(passphrase_file_path, 'w') as passphrase_file: passphrase_file.write(passphrase) assert ghost.get_passphrase() == passphrase finally: os.remove(passphrase_file_path) assert passphrase is ghost.get_passphrase(passphrase) def _create_temp_file(): fd, temp_file = tempfile.mkstemp() os.close(fd) print('PATH: {0}'.format(temp_file)) try: os.remove(temp_file) except: pass return temp_file @pytest.fixture def stash_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass @pytest.fixture def temp_file_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass def get_tinydb(path): with open(path) as db: return json.loads(db.read())['ghost'] class TestStorage(object): @staticmethod def is_initialized(structure): assert isinstance(structure, bool) @staticmethod def put(structure): assert isinstance(structure, (str, int)) @staticmethod def get_nonexisting_key(structure): assert isinstance(structure, dict) assert not structure @staticmethod def get(structure): assert isinstance(structure, dict) assert isinstance(structure['value'], dict) assert isinstance(structure['metadata'], dict) str(structure['name']) str(structure['uid']) str(structure['description']) @staticmethod def delete(structure): assert isinstance(structure, bool) def list(self, structure): assert isinstance(structure, list) for key in structure: self.get(key) @staticmethod def empty_list(structure): assert isinstance(structure, list) assert len(structure) == 0 storage_tester = TestStorage() BASE_TEST_KEY = { 'name': 'my_key', 'value': {'a': 'b'}, 'metadata': {}, 'created_at': '', 'modified_at': '', 'uid': '', 'description': 'My Key' } class TestTinyDBStorage: def test_init(self): tmpdir = tempfile.mkdtemp() shutil.rmtree(tmpdir, ignore_errors=True) assert not os.path.isdir(tmpdir) stash_path = os.path.join(tmpdir, 'stash.json') storage = ghost.TinyDBStorage(stash_path) try: storage.init() assert os.path.isdir(tmpdir) finally: shutil.rmtree(tmpdir, ignore_errors=True) def test_init_stash_already_exists(self): fd, stash_path = tempfile.mkstemp() os.close(fd) try: storage = ghost.TinyDBStorage(stash_path) storage.init() finally: os.remove(stash_path) def test_init_stash_create_directory(self): stash_dir = tempfile.mkdtemp() shutil.rmtree(stash_dir, ignore_errors=True) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) assert os.path.isdir(stash_dir) is False storage.init() assert os.path.isdir(stash_dir) is True finally: shutil.rmtree(stash_dir, ignore_errors=True) def test_init_stash_in_current_dir(self): """"""Test this because it depends on the ability of the storage to understand whether it should or should not create a directory. """""" prev_dir = os.getcwd() stash_dir = tempfile.mkdtemp() os.chdir(stash_dir) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert os.path.isfile(stash_path) is False stash.init() assert os.path.isfile(stash_path) is True finally: os.chdir(prev_dir) shutil.rmtree(stash_dir, ignore_errors=True) def test_is_initialized(self, stash_path): storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert storage.is_initialized is False stash.init() assert storage.is_initialized is True storage_tester.is_initialized(storage.is_initialized) def test_put(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_id = storage.put(BASE_TEST_KEY) db = get_tinydb(stash_path) assert '1' in db assert db['1']['name'] == BASE_TEST_KEY['name'] assert len(db) == 1 storage_tester.put(key_id) def test_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) storage.put(BASE_TEST_KEY) key_list = storage.list() assert BASE_TEST_KEY in key_list assert len(key_list) == 1 storage_tester.list(key_list) def test_empty_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_list = storage.list() storage_tester.empty_list(key_list) def test_get_delete(self, stash_path): inserted_key = BASE_TEST_KEY storage = ghost.TinyDBStorage(stash_path) storage.put(inserted_key) retrieved_key = storage.get(BASE_TEST_KEY['name']) assert inserted_key == retrieved_key storage_tester.get(retrieved_key) result = storage.delete(BASE_TEST_KEY['name']) storage_tester.delete(result) key = storage.get(BASE_TEST_KEY['name']) storage_tester.get_nonexisting_key(key) class TestSQLAlchemyStorage: def test_missing_requirement(self): with mock.patch('ghost.SQLALCHEMY_EXISTS', False): with pytest.raises(ImportError): ghost.SQLAlchemyStorage() def test_init(self): tmpdir = os.pa", https://github.com/nir0s/clu,77da967a4577ca4cf100cfe34e87b39ad88bf21c,tests/test_ghost.py::TestTinyDBStorage::test_init_stash_in_current_dir,tests/test_ghost.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# Copyright 2015,2016 Nir Cohen # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import time import json import shlex import base64 import shutil import tempfile import subprocess import mock import pytest import click.testing as clicktest import hvac # NOQA import botocore import elasticsearch # NOQA from moto import mock_s3 from sqlalchemy import sql from sqlalchemy import inspect from sqlalchemy import create_engine import ghost TEST_PASSPHRASE = 'a' def _invoke(command): cfy = clicktest.CliRunner() lexed_command = command if isinstance(command, list) \ else shlex.split(command) func = lexed_command[0] params = lexed_command[1:] return cfy.invoke(getattr(ghost, func), params) class TestGeneral: def test_get_current_time(self): assert len(ghost._get_current_time()) == 19 def test_generate_passphrase(self): passphrase = ghost.generate_passphrase() assert len(passphrase) == 12 assert isinstance(passphrase, str) longer_passphrase = ghost.generate_passphrase(13) assert len(longer_passphrase) == 13 def test_build_dict_from_key_value(self): key_values = ['a=b', 'c=d'] key_dict = ghost._build_dict_from_key_value(key_values) assert isinstance(key_dict, dict) assert 'a' in key_dict assert 'c' in key_dict assert key_dict.get('a') == 'b' assert key_dict.get('c') == 'd' def test_build_dict_no_key_equals_value(self): key_values = ['a=b', 'cd'] with pytest.raises(ghost.GhostError): ghost._build_dict_from_key_value(key_values) def test_prettify_dict(self): input = dict( description='a', uid='b', created_at='c', metadata={'x': 'y'}, modified='e', value={'key': 'value'}, name='g') prettified_input = ghost._prettify_dict(input).splitlines() assert 'Description: a' in prettified_input assert 'Uid: b' in prettified_input assert 'Created_At: c' in prettified_input assert 'Metadata: x=y;' in prettified_input assert 'Modified: e' in prettified_input assert 'Value: key=value;' in prettified_input assert 'Name: g' in prettified_input def test_prettify_dict_input_not_dict(self): with pytest.raises(AssertionError): ghost._prettify_dict('') def test_prettify_list(self): input = ['a', 'b', 'c'] prettified_input = ghost._prettify_list(input).splitlines() for line in prettified_input: assert ' - a' in prettified_input assert ' - b' in prettified_input assert ' - c' in prettified_input def test_prettify_list_input_not_list(self): with pytest.raises(AssertionError): ghost._prettify_list('') def test_get_passphrase(self): def _make_temp_passphrase_file(): fd, temp_file_path = tempfile.mkstemp() os.close(fd) os.remove(temp_file_path) return temp_file_path tempfile1 = _make_temp_passphrase_file() tempfile2 = _make_temp_passphrase_file() passphrase = '123' assert isinstance(ghost.POTENTIAL_PASSPHRASE_LOCATIONS, list) ghost.POTENTIAL_PASSPHRASE_LOCATIONS = [tempfile1, tempfile2] assert ghost.get_passphrase() is None for passphrase_file_path in ghost.POTENTIAL_PASSPHRASE_LOCATIONS: try: with open(passphrase_file_path, 'w') as passphrase_file: passphrase_file.write(passphrase) assert ghost.get_passphrase() == passphrase finally: os.remove(passphrase_file_path) assert passphrase is ghost.get_passphrase(passphrase) def _create_temp_file(): fd, temp_file = tempfile.mkstemp() os.close(fd) print('PATH: {0}'.format(temp_file)) try: os.remove(temp_file) except: pass return temp_file @pytest.fixture def stash_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass @pytest.fixture def temp_file_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass def get_tinydb(path): with open(path) as db: return json.loads(db.read())['ghost'] class TestStorage(object): @staticmethod def is_initialized(structure): assert isinstance(structure, bool) @staticmethod def put(structure): assert isinstance(structure, (str, int)) @staticmethod def get_nonexisting_key(structure): assert isinstance(structure, dict) assert not structure @staticmethod def get(structure): assert isinstance(structure, dict) assert isinstance(structure['value'], dict) assert isinstance(structure['metadata'], dict) str(structure['name']) str(structure['uid']) str(structure['description']) @staticmethod def delete(structure): assert isinstance(structure, bool) def list(self, structure): assert isinstance(structure, list) for key in structure: self.get(key) @staticmethod def empty_list(structure): assert isinstance(structure, list) assert len(structure) == 0 storage_tester = TestStorage() BASE_TEST_KEY = { 'name': 'my_key', 'value': {'a': 'b'}, 'metadata': {}, 'created_at': '', 'modified_at': '', 'uid': '', 'description': 'My Key' } class TestTinyDBStorage: def test_init(self): tmpdir = tempfile.mkdtemp() shutil.rmtree(tmpdir, ignore_errors=True) assert not os.path.isdir(tmpdir) stash_path = os.path.join(tmpdir, 'stash.json') storage = ghost.TinyDBStorage(stash_path) try: storage.init() assert os.path.isdir(tmpdir) finally: shutil.rmtree(tmpdir, ignore_errors=True) def test_init_stash_already_exists(self): fd, stash_path = tempfile.mkstemp() os.close(fd) try: storage = ghost.TinyDBStorage(stash_path) storage.init() finally: os.remove(stash_path) def test_init_stash_create_directory(self): stash_dir = tempfile.mkdtemp() shutil.rmtree(stash_dir, ignore_errors=True) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) assert os.path.isdir(stash_dir) is False storage.init() assert os.path.isdir(stash_dir) is True finally: shutil.rmtree(stash_dir, ignore_errors=True) def test_init_stash_in_current_dir(self): """"""Test this because it depends on the ability of the storage to understand whether it should or should not create a directory. """""" prev_dir = os.getcwd() stash_dir = tempfile.mkdtemp() os.chdir(stash_dir) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert os.path.isfile(stash_path) is False stash.init() assert os.path.isfile(stash_path) is True finally: os.chdir(prev_dir) shutil.rmtree(stash_dir, ignore_errors=True) def test_is_initialized(self, stash_path): storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert storage.is_initialized is False stash.init() assert storage.is_initialized is True storage_tester.is_initialized(storage.is_initialized) def test_put(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_id = storage.put(BASE_TEST_KEY) db = get_tinydb(stash_path) assert '1' in db assert db['1']['name'] == BASE_TEST_KEY['name'] assert len(db) == 1 storage_tester.put(key_id) def test_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) storage.put(BASE_TEST_KEY) key_list = storage.list() assert BASE_TEST_KEY in key_list assert len(key_list) == 1 storage_tester.list(key_list) def test_empty_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_list = storage.list() storage_tester.empty_list(key_list) def test_get_delete(self, stash_path): inserted_key = BASE_TEST_KEY storage = ghost.TinyDBStorage(stash_path) storage.put(inserted_key) retrieved_key = storage.get(BASE_TEST_KEY['name']) assert inserted_key == retrieved_key storage_tester.get(retrieved_key) result = storage.delete(BASE_TEST_KEY['name']) storage_tester.delete(result) key = storage.get(BASE_TEST_KEY['name']) storage_tester.get_nonexisting_key(key) class TestSQLAlchemyStorage: def test_missing_requirement(self): with mock.patch('ghost.SQLALCHEMY_EXISTS', False): with pytest.raises(ImportError): ghost.SQLAlchemyStorage() def test_init(self): tmpdir = os.pa", https://github.com/nir0s/clu,77da967a4577ca4cf100cfe34e87b39ad88bf21c,tests/test_ghost.py::TestTinyDBStorage::test_is_initialized,tests/test_ghost.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# Copyright 2015,2016 Nir Cohen # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import time import json import shlex import base64 import shutil import tempfile import subprocess import mock import pytest import click.testing as clicktest import hvac # NOQA import botocore import elasticsearch # NOQA from moto import mock_s3 from sqlalchemy import sql from sqlalchemy import inspect from sqlalchemy import create_engine import ghost TEST_PASSPHRASE = 'a' def _invoke(command): cfy = clicktest.CliRunner() lexed_command = command if isinstance(command, list) \ else shlex.split(command) func = lexed_command[0] params = lexed_command[1:] return cfy.invoke(getattr(ghost, func), params) class TestGeneral: def test_get_current_time(self): assert len(ghost._get_current_time()) == 19 def test_generate_passphrase(self): passphrase = ghost.generate_passphrase() assert len(passphrase) == 12 assert isinstance(passphrase, str) longer_passphrase = ghost.generate_passphrase(13) assert len(longer_passphrase) == 13 def test_build_dict_from_key_value(self): key_values = ['a=b', 'c=d'] key_dict = ghost._build_dict_from_key_value(key_values) assert isinstance(key_dict, dict) assert 'a' in key_dict assert 'c' in key_dict assert key_dict.get('a') == 'b' assert key_dict.get('c') == 'd' def test_build_dict_no_key_equals_value(self): key_values = ['a=b', 'cd'] with pytest.raises(ghost.GhostError): ghost._build_dict_from_key_value(key_values) def test_prettify_dict(self): input = dict( description='a', uid='b', created_at='c', metadata={'x': 'y'}, modified='e', value={'key': 'value'}, name='g') prettified_input = ghost._prettify_dict(input).splitlines() assert 'Description: a' in prettified_input assert 'Uid: b' in prettified_input assert 'Created_At: c' in prettified_input assert 'Metadata: x=y;' in prettified_input assert 'Modified: e' in prettified_input assert 'Value: key=value;' in prettified_input assert 'Name: g' in prettified_input def test_prettify_dict_input_not_dict(self): with pytest.raises(AssertionError): ghost._prettify_dict('') def test_prettify_list(self): input = ['a', 'b', 'c'] prettified_input = ghost._prettify_list(input).splitlines() for line in prettified_input: assert ' - a' in prettified_input assert ' - b' in prettified_input assert ' - c' in prettified_input def test_prettify_list_input_not_list(self): with pytest.raises(AssertionError): ghost._prettify_list('') def test_get_passphrase(self): def _make_temp_passphrase_file(): fd, temp_file_path = tempfile.mkstemp() os.close(fd) os.remove(temp_file_path) return temp_file_path tempfile1 = _make_temp_passphrase_file() tempfile2 = _make_temp_passphrase_file() passphrase = '123' assert isinstance(ghost.POTENTIAL_PASSPHRASE_LOCATIONS, list) ghost.POTENTIAL_PASSPHRASE_LOCATIONS = [tempfile1, tempfile2] assert ghost.get_passphrase() is None for passphrase_file_path in ghost.POTENTIAL_PASSPHRASE_LOCATIONS: try: with open(passphrase_file_path, 'w') as passphrase_file: passphrase_file.write(passphrase) assert ghost.get_passphrase() == passphrase finally: os.remove(passphrase_file_path) assert passphrase is ghost.get_passphrase(passphrase) def _create_temp_file(): fd, temp_file = tempfile.mkstemp() os.close(fd) print('PATH: {0}'.format(temp_file)) try: os.remove(temp_file) except: pass return temp_file @pytest.fixture def stash_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass @pytest.fixture def temp_file_path(): temp_file = _create_temp_file() yield temp_file if os.path.isfile(temp_file): try: os.remove(temp_file) except: pass def get_tinydb(path): with open(path) as db: return json.loads(db.read())['ghost'] class TestStorage(object): @staticmethod def is_initialized(structure): assert isinstance(structure, bool) @staticmethod def put(structure): assert isinstance(structure, (str, int)) @staticmethod def get_nonexisting_key(structure): assert isinstance(structure, dict) assert not structure @staticmethod def get(structure): assert isinstance(structure, dict) assert isinstance(structure['value'], dict) assert isinstance(structure['metadata'], dict) str(structure['name']) str(structure['uid']) str(structure['description']) @staticmethod def delete(structure): assert isinstance(structure, bool) def list(self, structure): assert isinstance(structure, list) for key in structure: self.get(key) @staticmethod def empty_list(structure): assert isinstance(structure, list) assert len(structure) == 0 storage_tester = TestStorage() BASE_TEST_KEY = { 'name': 'my_key', 'value': {'a': 'b'}, 'metadata': {}, 'created_at': '', 'modified_at': '', 'uid': '', 'description': 'My Key' } class TestTinyDBStorage: def test_init(self): tmpdir = tempfile.mkdtemp() shutil.rmtree(tmpdir, ignore_errors=True) assert not os.path.isdir(tmpdir) stash_path = os.path.join(tmpdir, 'stash.json') storage = ghost.TinyDBStorage(stash_path) try: storage.init() assert os.path.isdir(tmpdir) finally: shutil.rmtree(tmpdir, ignore_errors=True) def test_init_stash_already_exists(self): fd, stash_path = tempfile.mkstemp() os.close(fd) try: storage = ghost.TinyDBStorage(stash_path) storage.init() finally: os.remove(stash_path) def test_init_stash_create_directory(self): stash_dir = tempfile.mkdtemp() shutil.rmtree(stash_dir, ignore_errors=True) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) assert os.path.isdir(stash_dir) is False storage.init() assert os.path.isdir(stash_dir) is True finally: shutil.rmtree(stash_dir, ignore_errors=True) def test_init_stash_in_current_dir(self): """"""Test this because it depends on the ability of the storage to understand whether it should or should not create a directory. """""" prev_dir = os.getcwd() stash_dir = tempfile.mkdtemp() os.chdir(stash_dir) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert os.path.isfile(stash_path) is False stash.init() assert os.path.isfile(stash_path) is True finally: os.chdir(prev_dir) shutil.rmtree(stash_dir, ignore_errors=True) def test_is_initialized(self, stash_path): storage = ghost.TinyDBStorage(stash_path) stash = ghost.Stash(storage) assert storage.is_initialized is False stash.init() assert storage.is_initialized is True storage_tester.is_initialized(storage.is_initialized) def test_put(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_id = storage.put(BASE_TEST_KEY) db = get_tinydb(stash_path) assert '1' in db assert db['1']['name'] == BASE_TEST_KEY['name'] assert len(db) == 1 storage_tester.put(key_id) def test_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) storage.put(BASE_TEST_KEY) key_list = storage.list() assert BASE_TEST_KEY in key_list assert len(key_list) == 1 storage_tester.list(key_list) def test_empty_list(self, stash_path): storage = ghost.TinyDBStorage(stash_path) key_list = storage.list() storage_tester.empty_list(key_list) def test_get_delete(self, stash_path): inserted_key = BASE_TEST_KEY storage = ghost.TinyDBStorage(stash_path) storage.put(inserted_key) retrieved_key = storage.get(BASE_TEST_KEY['name']) assert inserted_key == retrieved_key storage_tester.get(retrieved_key) result = storage.delete(BASE_TEST_KEY['name']) storage_tester.delete(result) key = storage.get(BASE_TEST_KEY['name']) storage_tester.get_nonexisting_key(key) class TestSQLAlchemyStorage: def test_missing_requirement(self): with mock.patch('ghost.SQLALCHEMY_EXISTS', False): with pytest.raises(ImportError): ghost.SQLAlchemyStorage() def test_init(self): tmpdir = os.pa", https://github.com/nithinmurali/pygsheets,b34fb501f9c5d85b31a66a6ee9cd8789c79fc7a6,tests/online_test.py::TestCell::test_link,tests/online_test.py,NIO,flaky,Accepted,https://github.com/nithinmurali/pygsheets/pull/516,classify;root_cause;fix_proposal,"from googleapiclient.errors import HttpError import sys import re import os import pytest sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import pygsheets from pygsheets.exceptions import CannotRemoveOwnerError from pygsheets.custom_types import ExportType from pygsheets import Cell import pygsheets.utils as utils from pygsheets.custom_types import HorizontalAlignment, VerticalAlignment try: # for python 2.x import ConfigParser except ImportError: # for python 3.6+ import configparser as ConfigParser CONFIG_FILENAME = os.path.join(os.path.dirname(__file__), 'data/tests.config') SERVICE_FILE_NAME = os.path.join(os.path.dirname(__file__), 'auth_test_data/pygsheettest_service_account.json') PYTHON_VERSION = str(sys.hexversion) def read_config(filename): config = ConfigParser.ConfigParser() config.readfp(open(filename)) return config test_config = None pygsheet_client = None def setup_module(module): global test_config, pygsheet_client try: test_config = read_config(CONFIG_FILENAME) except IOError as e: msg = ""Can't find %s for reading test configuration. "" raise Exception(msg % e.filename) try: pygsheet_client = pygsheets.authorize(service_account_file=SERVICE_FILE_NAME) except IOError as e: msg = ""Can't find %s for reading credentials. "" raise Exception(msg % e.filename) config_title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION sheets = pygsheet_client.open_all(query=""name = '{}'"".format(config_title)) for sheet in sheets: sheet.delete() def teardown_module(module): config_title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION sheets = pygsheet_client.open_all(query=""name = '{}'"".format(config_title)) for sheet in sheets: try: sheet.delete() except HttpError as err: # do not delete files which the test suite has no permission for. if err.resp['status'] == '403': pass else: raise # @pytest.mark.skip() class TestClient(object): def setup_class(self): title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION self.spreadsheet = pygsheet_client.create(title) def teardown_class(self): self.spreadsheet.delete() def test_open_title(self): title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION spreadsheet = pygsheet_client.open(title) assert(isinstance(spreadsheet, pygsheets.Spreadsheet)) assert spreadsheet.title == title def test_open_key(self): title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION spreadsheet = pygsheet_client.open_by_key(self.spreadsheet.id) assert(isinstance(spreadsheet, pygsheets.Spreadsheet)) assert spreadsheet.id == self.spreadsheet.id assert spreadsheet.title == title def test_open_url(self): url = test_config.get('Spreadsheet', 'url').format(self.spreadsheet.id) spreadsheet = pygsheet_client.open_by_url(url) assert(isinstance(spreadsheet, pygsheets.Spreadsheet)) assert spreadsheet.id == self.spreadsheet.id # TODO: Expand create tests. def test_create(self): title = 'test_create_file' + PYTHON_VERSION result = pygsheet_client.create(title) assert isinstance(result, pygsheets.Spreadsheet) assert title == result.title result.delete() # @pytest.mark.skip() class TestSpreadSheet(object): def setup_class(self): title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION self.spreadsheet = pygsheet_client.create(title) self.output_path = test_config.get('Output', 'path').format(PYTHON_VERSION) if not os.path.exists(self.output_path): os.mkdir(self.output_path) def teardown_class(self): self.spreadsheet.delete() for root, dirs, files in os.walk(self.output_path): for file in files: os.remove(root + file) os.rmdir(self.output_path) def test_properties(self): json_sheet = self.spreadsheet._jsonsheet assert self.spreadsheet.id == json_sheet['spreadsheetId'] assert self.spreadsheet.title == json_sheet['properties']['title'] assert self.spreadsheet.defaultformat == json_sheet['properties']['defaultFormat'] assert isinstance(self.spreadsheet.sheet1, pygsheets.Worksheet) def test_workssheet_add_del(self): self.spreadsheet.add_worksheet(""testSheetx"", 50, 60) try: wks = self.spreadsheet.worksheet_by_title(""testSheetx"") except pygsheets.WorksheetNotFound: pytest.fail() assert wks.rows == 50 assert wks.cols == 60 self.spreadsheet.del_worksheet(wks) with pytest.raises(pygsheets.WorksheetNotFound): self.spreadsheet.worksheet_by_title(""testSheetx"") def test_worksheet_opening(self): wkss = self.spreadsheet.worksheets() assert isinstance(wkss, list) assert isinstance(wkss[0], pygsheets.Worksheet) assert isinstance(self.spreadsheet.worksheet(), pygsheets.Worksheet) def add_worksheet(self): self.spreadsheet.add_worksheet(""dummy_temp_wks"", 100, 50) wks = self.spreadsheet.worksheet_by_title(""dummy_temp_wks"") assert isinstance(wks, pygsheets.Worksheet) assert wks.rows == 100 assert wks.cols == 50 def delete_worksheet(self): wks = self.spreadsheet.worksheet_by_title(""dummy_temp_wks"") self.spreadsheet.del_worksheet(wks) with pytest.raises(pygsheets.WorksheetNotFound): self.spreadsheet.worksheet_by_title(""dummy_temp_wks"") def test_updated(self): RFC_3339 = (r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?' r'(Z|[+-]\d{2}:\d{2})') has_match = re.match(RFC_3339, self.spreadsheet.updated) is not None assert has_match def test_find(self): self.spreadsheet.add_worksheet('testFind1', 10, 10) self.spreadsheet.add_worksheet('testFind2', 10, 10) self.spreadsheet.worksheet('title', 'testFind1').update_row(1, ['test', 'test']) self.spreadsheet.worksheet('title', 'testFind2').update_row(1, ['test', 'test']) cells = self.spreadsheet.find('test') assert isinstance(cells, list) assert len(cells) == 3 assert len(cells[0]) == 0 assert len(cells[1]) == 2 self.spreadsheet.del_worksheet(self.spreadsheet.worksheet('title', 'testFind1')) self.spreadsheet.del_worksheet(self.spreadsheet.worksheet('title', 'testFind2')) def test_export(self): wks_1 = self.spreadsheet.sheet1 wks_1.update_row(1, ['test', 'test', 'test', 'test']) self.spreadsheet.add_worksheet('Test2') wks_2 = self.spreadsheet.worksheet('title', 'Test2') wks_2.update_row(1, ['test', 'test', 'test', 'test']) self.spreadsheet.export(filename='test', path=self.output_path) self.spreadsheet.export(file_format=ExportType.XLS, filename='test', path=self.output_path) self.spreadsheet.export(file_format=ExportType.HTML, filename='test', path=self.output_path) self.spreadsheet.export(file_format=ExportType.ODT, filename='test', path=self.output_path) self.spreadsheet.export(file_format=ExportType.PDF, filename='test', path=self.output_path) assert os.path.exists('{}/test.pdf'.format(self.output_path)) assert os.path.exists('{}/test.xls'.format(self.output_path)) assert os.path.exists('{}/test.odt'.format(self.output_path)) assert os.path.exists('{}/test.zip'.format(self.output_path)) self.spreadsheet.export(filename='spreadsheet', path=self.output_path) assert os.path.exists('{}/spreadsheet0.csv'.format(self.output_path)) assert os.path.exists('{}/spreadsheet1.csv'.format(self.output_path)) self.spreadsheet.export(file_format=ExportType.TSV, filename='tsv_spreadsheet', path=self.output_path) assert os.path.exists('{}/tsv_spreadsheet0.tsv'.format(self.output_path)) assert os.path.exists('{}/tsv_spreadsheet1.tsv'.format(self.output_path)) wks_1.clear() self.spreadsheet.del_worksheet(wks_2) def test_permissions(self): old_per = self.spreadsheet.permissions assert isinstance(old_per, list) self.spreadsheet.share('pygsheettest2@gmail.com') assert len(self.spreadsheet.permissions) == (len(old_per) + 1) self.spreadsheet.remove_permission('pygsheettest2@gmail.com') assert len(old_per) == len(self.spreadsheet.permissions) with pytest.raises(CannotRemoveOwnerError): self.spreadsheet.remove_permission('', permission_id=self.spreadsheet.permissions[-1]['id']) def test_developer_metadata(self): old_meta = self.spreadsheet.get_developer_metadata() meta_val = self.spreadsheet.create_developer_metadata(""testkey"", ""testvalue"") assert meta_val.key == ""testkey"" assert meta_val.value == ""testvalue"" new_meta = self.spreadsheet.get_developer_metadata() assert len(new_meta) == len(old_meta) + 1 meta_val.value = ""newtestvalue"" meta_val.update() updated_meta_val = self.spreadsheet.get_developer_metadata(""testkey"")[0] assert updated_meta_val.key == ""testkey"" assert updated_meta_val.value == ""newtestvalue"" updated_meta_val.delete() final_meta = self.spreadsheet.get_developer_metadata() assert len(final_meta) == len(old_meta) # @pytest.mark.skip() class TestWorkSheet(object): def setup_class(self): title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION self.spreadsheet = pygsheet_client.create(title) self.worksheet = self.spreadsheet.worksheet() self.copy_sheet_tit", https://github.com/nithinmurali/pygsheets,48158e3a2c20d8b84a28b117ae1024bd4dedee08,tests/online_test.py::TestCell::test_wrap_strategy,tests/online_test.py,NIO,flaky,Accepted,https://github.com/nithinmurali/pygsheets/pull/517,classify;root_cause;fix_proposal,"from googleapiclient.errors import HttpError import sys import re import os import pytest sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import pygsheets from pygsheets.exceptions import CannotRemoveOwnerError from pygsheets.custom_types import ExportType from pygsheets import Cell import pygsheets.utils as utils from pygsheets.custom_types import HorizontalAlignment, VerticalAlignment try: # for python 2.x import ConfigParser except ImportError: # for python 3.6+ import configparser as ConfigParser CONFIG_FILENAME = os.path.join(os.path.dirname(__file__), 'data/tests.config') SERVICE_FILE_NAME = os.path.join(os.path.dirname(__file__), 'auth_test_data/pygsheettest_service_account.json') PYTHON_VERSION = str(sys.hexversion) def read_config(filename): config = ConfigParser.ConfigParser() config.readfp(open(filename)) return config test_config = None pygsheet_client = None def setup_module(module): global test_config, pygsheet_client try: test_config = read_config(CONFIG_FILENAME) except IOError as e: msg = ""Can't find %s for reading test configuration. "" raise Exception(msg % e.filename) try: pygsheet_client = pygsheets.authorize(service_account_file=SERVICE_FILE_NAME) except IOError as e: msg = ""Can't find %s for reading credentials. "" raise Exception(msg % e.filename) config_title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION sheets = pygsheet_client.open_all(query=""name = '{}'"".format(config_title)) for sheet in sheets: sheet.delete() def teardown_module(module): config_title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION sheets = pygsheet_client.open_all(query=""name = '{}'"".format(config_title)) for sheet in sheets: try: sheet.delete() except HttpError as err: # do not delete files which the test suite has no permission for. if err.resp['status'] == '403': pass else: raise # @pytest.mark.skip() class TestClient(object): def setup_class(self): title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION self.spreadsheet = pygsheet_client.create(title) def teardown_class(self): self.spreadsheet.delete() def test_open_title(self): title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION spreadsheet = pygsheet_client.open(title) assert(isinstance(spreadsheet, pygsheets.Spreadsheet)) assert spreadsheet.title == title def test_open_key(self): title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION spreadsheet = pygsheet_client.open_by_key(self.spreadsheet.id) assert(isinstance(spreadsheet, pygsheets.Spreadsheet)) assert spreadsheet.id == self.spreadsheet.id assert spreadsheet.title == title def test_open_url(self): url = test_config.get('Spreadsheet', 'url').format(self.spreadsheet.id) spreadsheet = pygsheet_client.open_by_url(url) assert(isinstance(spreadsheet, pygsheets.Spreadsheet)) assert spreadsheet.id == self.spreadsheet.id # TODO: Expand create tests. def test_create(self): title = 'test_create_file' + PYTHON_VERSION result = pygsheet_client.create(title) assert isinstance(result, pygsheets.Spreadsheet) assert title == result.title result.delete() # @pytest.mark.skip() class TestSpreadSheet(object): def setup_class(self): title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION self.spreadsheet = pygsheet_client.create(title) self.output_path = test_config.get('Output', 'path').format(PYTHON_VERSION) if not os.path.exists(self.output_path): os.mkdir(self.output_path) def teardown_class(self): self.spreadsheet.delete() for root, dirs, files in os.walk(self.output_path): for file in files: os.remove(root + file) os.rmdir(self.output_path) def test_properties(self): json_sheet = self.spreadsheet._jsonsheet assert self.spreadsheet.id == json_sheet['spreadsheetId'] assert self.spreadsheet.title == json_sheet['properties']['title'] assert self.spreadsheet.defaultformat == json_sheet['properties']['defaultFormat'] assert isinstance(self.spreadsheet.sheet1, pygsheets.Worksheet) def test_workssheet_add_del(self): self.spreadsheet.add_worksheet(""testSheetx"", 50, 60) try: wks = self.spreadsheet.worksheet_by_title(""testSheetx"") except pygsheets.WorksheetNotFound: pytest.fail() assert wks.rows == 50 assert wks.cols == 60 self.spreadsheet.del_worksheet(wks) with pytest.raises(pygsheets.WorksheetNotFound): self.spreadsheet.worksheet_by_title(""testSheetx"") def test_worksheet_opening(self): wkss = self.spreadsheet.worksheets() assert isinstance(wkss, list) assert isinstance(wkss[0], pygsheets.Worksheet) assert isinstance(self.spreadsheet.worksheet(), pygsheets.Worksheet) def add_worksheet(self): self.spreadsheet.add_worksheet(""dummy_temp_wks"", 100, 50) wks = self.spreadsheet.worksheet_by_title(""dummy_temp_wks"") assert isinstance(wks, pygsheets.Worksheet) assert wks.rows == 100 assert wks.cols == 50 def delete_worksheet(self): wks = self.spreadsheet.worksheet_by_title(""dummy_temp_wks"") self.spreadsheet.del_worksheet(wks) with pytest.raises(pygsheets.WorksheetNotFound): self.spreadsheet.worksheet_by_title(""dummy_temp_wks"") def test_updated(self): RFC_3339 = (r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?' r'(Z|[+-]\d{2}:\d{2})') has_match = re.match(RFC_3339, self.spreadsheet.updated) is not None assert has_match def test_find(self): self.spreadsheet.add_worksheet('testFind1', 10, 10) self.spreadsheet.add_worksheet('testFind2', 10, 10) self.spreadsheet.worksheet('title', 'testFind1').update_row(1, ['test', 'test']) self.spreadsheet.worksheet('title', 'testFind2').update_row(1, ['test', 'test']) cells = self.spreadsheet.find('test') assert isinstance(cells, list) assert len(cells) == 3 assert len(cells[0]) == 0 assert len(cells[1]) == 2 self.spreadsheet.del_worksheet(self.spreadsheet.worksheet('title', 'testFind1')) self.spreadsheet.del_worksheet(self.spreadsheet.worksheet('title', 'testFind2')) def test_export(self): wks_1 = self.spreadsheet.sheet1 wks_1.update_row(1, ['test', 'test', 'test', 'test']) self.spreadsheet.add_worksheet('Test2') wks_2 = self.spreadsheet.worksheet('title', 'Test2') wks_2.update_row(1, ['test', 'test', 'test', 'test']) self.spreadsheet.export(filename='test', path=self.output_path) self.spreadsheet.export(file_format=ExportType.XLS, filename='test', path=self.output_path) self.spreadsheet.export(file_format=ExportType.HTML, filename='test', path=self.output_path) self.spreadsheet.export(file_format=ExportType.ODT, filename='test', path=self.output_path) self.spreadsheet.export(file_format=ExportType.PDF, filename='test', path=self.output_path) assert os.path.exists('{}/test.pdf'.format(self.output_path)) assert os.path.exists('{}/test.xls'.format(self.output_path)) assert os.path.exists('{}/test.odt'.format(self.output_path)) assert os.path.exists('{}/test.zip'.format(self.output_path)) self.spreadsheet.export(filename='spreadsheet', path=self.output_path) assert os.path.exists('{}/spreadsheet0.csv'.format(self.output_path)) assert os.path.exists('{}/spreadsheet1.csv'.format(self.output_path)) self.spreadsheet.export(file_format=ExportType.TSV, filename='tsv_spreadsheet', path=self.output_path) assert os.path.exists('{}/tsv_spreadsheet0.tsv'.format(self.output_path)) assert os.path.exists('{}/tsv_spreadsheet1.tsv'.format(self.output_path)) wks_1.clear() self.spreadsheet.del_worksheet(wks_2) def test_permissions(self): old_per = self.spreadsheet.permissions assert isinstance(old_per, list) self.spreadsheet.share('pygsheettest2@gmail.com') assert len(self.spreadsheet.permissions) == (len(old_per) + 1) self.spreadsheet.remove_permission('pygsheettest2@gmail.com') assert len(old_per) == len(self.spreadsheet.permissions) with pytest.raises(CannotRemoveOwnerError): self.spreadsheet.remove_permission('', permission_id=self.spreadsheet.permissions[-1]['id']) def test_developer_metadata(self): old_meta = self.spreadsheet.get_developer_metadata() meta_val = self.spreadsheet.create_developer_metadata(""testkey"", ""testvalue"") assert meta_val.key == ""testkey"" assert meta_val.value == ""testvalue"" new_meta = self.spreadsheet.get_developer_metadata() assert len(new_meta) == len(old_meta) + 1 meta_val.value = ""newtestvalue"" meta_val.update() updated_meta_val = self.spreadsheet.get_developer_metadata(""testkey"")[0] assert updated_meta_val.key == ""testkey"" assert updated_meta_val.value == ""newtestvalue"" updated_meta_val.delete() final_meta = self.spreadsheet.get_developer_metadata() assert len(final_meta) == len(old_meta) # @pytest.mark.skip() class TestWorkSheet(object): def setup_class(self): title = test_config.get('Spreadsheet', 'title') + PYTHON_VERSION self.spreadsheet = pygsheet_client.create(title) self.worksheet = self.spreadsheet.worksheet() self.copy_sheet_tit", https://github.com/nMustaki/debinterface,abd30a55dcbadc30fd66143adc196034b663054f,test/test_hostapd.py::TestHostapd::test_read,test/test_hostapd.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"# -*- coding: utf-8 -*- import unittest import filecmp import tempfile from ..debinterface import Hostapd DEFAULT_CONTENT = ''' interface=wlan0 driver=nl80211 logger_syslog=-1 logger_syslog_level=2 logger_stdout=-1 logger_stdout_level=2 debug=4 #dump_file=/tmp/hostapd.dump #ctrl_interface=/var/run/hostapd #ctrl_interface_group=0 channel=4 hw_mode=g macaddr_acl=0 auth_algs=3 eapol_key_index_workaround=0 eap_server=0 wpa=3 ssid=cashpad-FTED wpa_passphrase=cashpad-GH67 wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP eapol_version=1 #wme_enabled=1 #ieee80211n=1 #ht_capab=[HT40-][HT40+][SHORT-GI-40][TX-STBC][RX-STBC1][DSSS_CCK-40] ''' DEFAULT_CONFIG = { 'interface': 'wlan0', 'driver': 'nl80211', # logs 'logger_syslog': ""-1"", 'logger_syslog_level': ""2"", 'logger_stdout': ""-1"", 'logger_stdout_level': ""2"", # debug 'debug': ""4"", # wifi 'hw_mode': 'g', # security goodies 'macaddr_acl': ""0"", 'eapol_key_index_workaround': ""0"", 'eap_server': ""0"", 'eapol_version': ""1"", # wifi auth 'channel': ""4"", 'ssid': 'cashpad-FTED', 'wpa_passphrase': 'cashpad-GH67', 'auth_algs': ""3"", 'wpa': ""3"", # WPA + WPA2. set to 2 to restrict to WPA2 'wpa_key_mgmt': 'WPA-PSK', 'wpa_pairwise': 'TKIP', 'rsn_pairwise': 'CCMP' # some windows clients may have issues with this one } class TestHostapd(unittest.TestCase): def test_read(self): self.maxDiff = None with tempfile.NamedTemporaryFile() as source: source.write(DEFAULT_CONTENT.encode(""ascii"")) source.flush() dns = Hostapd(source.name) dns.read() self.assertDictEqual(dns.config, DEFAULT_CONFIG) def test_write(self): self.maxDiff = None with tempfile.NamedTemporaryFile() as source: dns = Hostapd(source.name) dns._config = DEFAULT_CONFIG dns.write() source.flush() content = open(source.name).read().replace(""\n"", """") for line in DEFAULT_CONTENT.split(""\n""): if not line or line == ""\n"": continue if line.startswith(""#""): self.assertNotIn(line, content) else: self.assertIn(line, content) def test_validate_valid(self): """"""Test validate with valid data"""""" dns = Hostapd(""fdlkfdl"") dns._config = DEFAULT_CONFIG self.assertEqual(dns.validate(), True) def test_backup(self): with tempfile.NamedTemporaryFile() as source: source.write(DEFAULT_CONTENT.encode(""ascii"")) source.flush() dns = Hostapd(source.name) dns.backup() self.assertTrue(filecmp.cmp(source.name, dns.backup_path)) def test_restore(self): backup = tempfile.NamedTemporaryFile() conffile = tempfile.NamedTemporaryFile() backup.write(DEFAULT_CONTENT.encode(""ascii"")) backup.flush() dns = Hostapd(conffile.name, backup.name) dns.restore() try: self.assertTrue(filecmp.cmp(conffile.name, backup.name)) finally: backup.close() conffile.close() ", https://github.com/nordwind80/BT-Tracker,558c15b399871c1ca11d0c4ae1eb598e3060931e,Tracker/tests/test_event.py::TestEvent::test_object,Tracker/tests/test_event.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python3 # # Author: eaglewings # E-Mail: ZWFnbGV3aW5ncy55aUBnbWFpbC5jb20= # Created Time: 2019-04-14 20:13 # Last Modified: # Description: # - Project: BT Trackers Updater # - File Name: test_event.py # - Test singleton instance of event.py from ..event import status class TestEvent(object): def test_object(self): assert id(status.state) == id(status.state) def test_state_change(self): status.state = False assert status.state is False ", https://github.com/ohke/sqs-polling,34d1375cf73e05211c73cd43bdd120ce076181c8,sqs_polling/tests/test_sqs_polling.py::TestSqsPolling::test_execute_delete,sqs_polling/tests/test_sqs_polling.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from unittest.mock import MagicMock, patch from ..sqs_polling import _receive, _execute class TestSqsPolling(unittest.TestCase): def _get_queue_url(self): return ""https://sqs.ap-northeast-1.amazonaws.com/XXXXXXXXXXXX/your-sqs"" def _get_receipt_handle(self, i): return ""ReceiptHandle_{}"".format(i) def _get_body(self, i): return ""Body_{}"".format(i) def _get_responses(self, message_num): response = { ""ResponseMetadata"": { ""RetryAttempts"": 0, ""HTTPHeaders"": { ""content-type"": ""text/xml"", ""content-length"": ""860"", ""x-amzn-requestid"": ""66222d85-7321-7aa2-b21a-7ba1eb393a4a"", ""date"": ""Sat, 19 Oct 2019 04:49:28 GMT"" }, ""RequestId"": ""d7aa8408-0fa8-5a29-8538-d22c900749c1"", ""HTTPStatusCode"": 200 } } if message_num > 0: messages = [] for i in range(message_num): messages.append({ ""ReceiptHandle"": self._get_receipt_handle(i), ""MD5OfBody"": ""1db65a6a0a818fd39655b95e34ada11d"", ""MessageId"": ""58da63b0-426b-4fda-9b47-64017fd441dd"", ""Body"": self._get_body(i), }) response[""Messages""] = messages return response def test_receive_no_messages(self): sqs_mock = MagicMock() sqs_mock.receive_message.return_value = self._get_responses(0) ret = _receive(sqs_mock, """", 300, 1) assert ret == [] def test_receive_1_message(self): sqs_mock = MagicMock() sqs_mock.receive_message.return_value = self._get_responses(1) ret = _receive(sqs_mock, """", 300, 1) assert len(ret) == 1 assert ret[0][""Body""] == self._get_body(0) def test_receive_2_messages(self): sqs_mock = MagicMock() sqs_mock.receive_message.return_value = self._get_responses(2) ret = _receive(sqs_mock, """", 300, 2) assert len(ret) == 2 assert ret[1][""Body""] == self._get_body(1) def test_execute_delete(self): sqs_mock = MagicMock() callback_mock = MagicMock(return_value=True) with patch(""boto3.client"", return_value=sqs_mock) as _: _execute({}, self._get_queue_url(), callback_mock, None, self._get_responses(1)[""Messages""], True) callback_mock.assert_called_once_with(self._get_body(0)) sqs_mock.delete_message.assert_called_once_with( QueueUrl=self._get_queue_url(), ReceiptHandle=self._get_receipt_handle(0) ) def test_execute_not_delete(self): sqs_mock = MagicMock() callback_mock = MagicMock(return_value=False) with patch(""boto3.client"", return_value=sqs_mock) as _: _execute(sqs_mock, self._get_queue_url(), callback_mock, None, self._get_responses(1)[""Messages""], True) callback_mock.assert_called_once_with(self._get_body(0)) sqs_mock.delete_message.assert_not_called() def test_execute_with_args(self): sqs_mock = MagicMock() callback_mock = MagicMock(return_value=True) with patch(""boto3.client"", return_value=sqs_mock) as _: _execute(sqs_mock, self._get_queue_url(), callback_mock, (1, ""2""), self._get_responses(1)[""Messages""], True) callback_mock.assert_called_once_with(self._get_body(0), *(1, ""2"")) def test_execute_with_kwargs(self): sqs_mock = MagicMock() callback_mock = MagicMock(return_value=True) with patch(""boto3.client"", return_value=sqs_mock) as _: _execute(sqs_mock, self._get_queue_url(), callback_mock, {""key1"": 1, ""key2"": ""2""}, self._get_responses(1)[""Messages""], True) callback_mock.assert_called_once_with(self._get_body(0), **{""key1"": 1, ""key2"": ""2""}) def test_execute_with_whole(self): sqs_mock = MagicMock() callback_mock = MagicMock(return_value=True) with patch(""boto3.client"", return_value=sqs_mock) as _: _execute(sqs_mock, self._get_queue_url(), callback_mock, None, self._get_responses(1)[""Messages""], False) callback_mock.assert_called_once_with(self._get_responses(1)[""Messages""][0]) def test_execute_callback_returns_not_bool(self): sqs_mock = MagicMock() callback_mock = MagicMock(return_value=None) try: with patch(""boto3.client"", return_value=sqs_mock) as _: _execute(sqs_mock, self._get_queue_url(), callback_mock, None, self._get_responses(1)[""Messages""], True) except TypeError: assert True except: assert False else: assert False ", https://github.com/omersaraf/IOCynergy,1821423680f741d8ca06bcc6a02c8b21156f9ba0,cynergy/tests/test_life_cycle.py::test_multi,cynergy/tests/test_life_cycle.py,NIO,flaky,Opened,https://github.com/omersaraf/IOCynergy/pull/3,classify;root_cause,"from typing import Type from cynergy import container from cynergy.attributes import life_cycle, LifeCycle @life_cycle(LifeCycle.MULTI_INSTANCE) class MultiExample(object): count = 0 def __init__(self): MultiExample.count += 1 @life_cycle(LifeCycle.SINGLETON) class SingleExample(object): count = 0 def __init__(self): SingleExample.count += 1 class NoLifeCycleExample(object): count = 0 def __init__(self): NoLifeCycleExample.count += 1 def test_multi(): assert_instance_of(MultiExample, 2) def test_single(): assert_instance_of(SingleExample, 1) def test_no_lifecycle(): assert_instance_of(NoLifeCycleExample, 1) def assert_instance_of(cls: Type, expected_count): container.get(cls) container.get(cls) assert cls.count == expected_count ", https://github.com/omersaraf/IOCynergy,1821423680f741d8ca06bcc6a02c8b21156f9ba0,cynergy/tests/test_register_multiple.py::test_register_multiple,cynergy/tests/test_register_multiple.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from typing import List from cynergy import container class Example(object): pass class Example1(object): pass class Example2(object): pass class Example3(object): pass class Example4(object): pass class Main(object): def __init__(self, examples: List[Example], examples1: List[Example1]): self.examples1 = examples1 self.examples = examples class Main2(object): def __init__(self, examples: List[Example]): self.examples = examples def test_register_multiple(): container.register_many(Example, [Example1, Example2]) instance = container.get(List[Example]) assert type(instance) is list assert len(instance) == 2 assert type(instance[0]) is Example1 assert type(instance[1]) is Example2 def test_multiple_list_arguments(): container._clear_all() container.register_many(Example, [Example2, Example3]) container.register_many(Example1, [Example3, Example4]) instance = container.get(Main) assert type(instance) is Main assert len(instance.examples) == 2 assert len(instance.examples1) == 2 assert type(instance.examples[0]) is Example2 assert type(instance.examples[1]) is Example3 assert type(instance.examples1[0]) is Example3 assert type(instance.examples1[1]) is Example4 class MainWrapper(object): def __init__(self, main: Main): self.main = main def test_multiple_list_arguments_with_wrap(): container._clear_all() container.register_many(Example, [Example2, Example3]) container.register_many(Example1, [Example3, Example4]) instance = container.get(MainWrapper) assert type(instance) is MainWrapper assert len(instance.main.examples) == 2 assert len(instance.main.examples1) == 2 assert type(instance.main.examples[0]) is Example2 assert type(instance.main.examples[1]) is Example3 assert type(instance.main.examples1[0]) is Example3 assert type(instance.main.examples1[1]) is Example4 def test_register_multiple_when_onc_instance_is_already_registered(): container._clear_all() ex1 = Example2() container.register(Example1, ex1) container.register_many(Example, [Example1, Example3]) instance = container.get(Main2) assert type(instance) is Main2 assert len(instance.examples) == 2 assert instance.examples[0] == ex1 assert type(instance.examples[1]) is Example3 ", https://github.com/OpenDataServices/lib-cove,74ba874ec6882969894797ee0dbc5c70a16019e5,tests/lib/test_converters.py::test_convert_activity_xml_1,tests/lib/test_converters.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import csv import json import os import tempfile from libcove.config import LibCoveConfig from libcove.lib.converters import convert_json, convert_spreadsheet def test_convert_json_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-ocds-tests-"", dir=tempfile.gettempdir() ) json_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_json_1.json"", ) lib_cove_config = LibCoveConfig() output = convert_json( cove_temp_folder, """", json_filename, lib_cove_config, flatten=True ) assert output[""converted_url""] == ""/flattened"" assert len(output[""conversion_warning_messages""]) == 0 assert output[""conversion""] == ""flatten"" conversion_warning_messages_name = os.path.join( cove_temp_folder, ""conversion_warning_messages.json"" ) assert os.path.isfile(conversion_warning_messages_name) with open(conversion_warning_messages_name) as fp: conversion_warning_messages_data = json.load(fp) assert conversion_warning_messages_data == [] assert os.path.isfile(os.path.join(cove_temp_folder, ""flattened"", ""main.csv"")) with open(os.path.join(cove_temp_folder, ""flattened"", ""main.csv""), ""r"") as csvfile: csvreader = csv.reader(csvfile) header = next(csvreader) assert header[0] == ""id"" assert header[1] == ""title"" row1 = next(csvreader) assert row1[0] == ""1"" assert row1[1] == ""Cat"" row2 = next(csvreader) assert row2[0] == ""2"" assert row2[1] == ""Hat"" def test_convert_activity_xml_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-iati-tests-"", dir=tempfile.gettempdir() ) activity_xml_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_activity_1.xml"", ) lib_cove_config = LibCoveConfig() output = convert_json( cove_temp_folder, """", activity_xml_filename, lib_cove_config, flatten=True, xml=True, root_list_path=""iati-activity"", ) assert output[""converted_url""] == ""/flattened"" assert len(output[""conversion_warning_messages""]) == 0 assert output[""conversion""] == ""flatten"" conversion_warning_messages_name = os.path.join( cove_temp_folder, ""conversion_warning_messages.json"" ) assert os.path.isfile(conversion_warning_messages_name) with open(conversion_warning_messages_name) as fp: conversion_warning_messages_data = json.load(fp) assert conversion_warning_messages_data == [] assert os.path.isfile(os.path.join(cove_temp_folder, ""flattened.xlsx"")) assert os.path.isfile( os.path.join(cove_temp_folder, ""flattened"", ""iati-activity.csv"") ) with open( os.path.join(cove_temp_folder, ""flattened"", ""iati-activity.csv""), ""r"" ) as csvfile: csvreader = csv.reader(csvfile) header = next(csvreader) assert header[0] == ""@default-currency"" assert header[1] == ""iati-identifier"" row1 = next(csvreader) assert row1[0] == ""GBP"" assert row1[1] == ""GB-TEST-13-example_ODSC_2019"" def test_convert_org_xml_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-iati-tests-"", dir=tempfile.gettempdir() ) organisation_xml_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_org_1.xml"", ) lib_cove_config = LibCoveConfig() output = convert_json( cove_temp_folder, """", organisation_xml_filename, lib_cove_config, flatten=True, xml=True, root_list_path=""iati-organisation"", root_id=""organisation-identifier"", ) assert output[""converted_url""] == ""/flattened"" assert len(output[""conversion_warning_messages""]) == 0 assert output[""conversion""] == ""flatten"" conversion_warning_messages_name = os.path.join( cove_temp_folder, ""conversion_warning_messages.json"" ) assert os.path.isfile(conversion_warning_messages_name) with open(conversion_warning_messages_name) as fp: conversion_warning_messages_data = json.load(fp) assert conversion_warning_messages_data == [] assert os.path.isfile(os.path.join(cove_temp_folder, ""flattened.xlsx"")) assert os.path.isfile( os.path.join(cove_temp_folder, ""flattened"", ""iati-organisation.csv"") ) with open( os.path.join(cove_temp_folder, ""flattened"", ""iati-organisation.csv""), ""r"" ) as csvfile: csvreader = csv.reader(csvfile) header = next(csvreader) assert header[0] == ""organisation-identifier"" assert header[4] == ""name/narrative"" row1 = next(csvreader) assert row1[0] == ""GB-GOV-1"" assert row1[4] == ""UK Department for International Development"" def test_convert_json_root_is_list_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-ocds-tests-"", dir=tempfile.gettempdir() ) json_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_json_root_is_list_1.json"", ) lib_cove_config = LibCoveConfig() lib_cove_config.config[""root_is_list""] = True output = convert_json( cove_temp_folder, """", json_filename, lib_cove_config, flatten=True ) assert output[""converted_url""] == ""/flattened"" assert len(output[""conversion_warning_messages""]) == 0 assert output[""conversion""] == ""flatten"" conversion_warning_messages_name = os.path.join( cove_temp_folder, ""conversion_warning_messages.json"" ) assert os.path.isfile(conversion_warning_messages_name) with open(conversion_warning_messages_name) as fp: conversion_warning_messages_data = json.load(fp) assert conversion_warning_messages_data == [] assert os.path.isfile(os.path.join(cove_temp_folder, ""flattened"", ""main.csv"")) with open(os.path.join(cove_temp_folder, ""flattened"", ""main.csv""), ""r"") as csvfile: csvreader = csv.reader(csvfile) header = next(csvreader) assert header[0] == ""id"" assert header[1] == ""title"" row1 = next(csvreader) assert row1[0] == ""1"" assert row1[1] == ""Cat"" row2 = next(csvreader) assert row2[0] == ""2"" assert row2[1] == ""Hat"" def test_convert_csv_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-ocds-tests-"", dir=tempfile.gettempdir() ) csv_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_csv_1.csv"", ) csv_schema_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_csv_1_schema.json"", ) lib_cove_config = LibCoveConfig() lib_cove_config.config[""id_name""] = ""thing_id"" lib_cove_config.config[""root_is_list""] = True output = convert_spreadsheet( cove_temp_folder, """", csv_filename, ""csv"", lib_cove_config, schema_url=csv_schema_filename, ) assert output[""conversion""] == ""unflatten"" with open(output[""converted_path""]) as fp: json_data = json.load(fp) assert json_data == [ {""thing_id"": ""1"", ""title"": ""Cat""}, {""thing_id"": ""2"", ""title"": ""Hat""}, ] ", https://github.com/OpenDataServices/lib-cove,74ba874ec6882969894797ee0dbc5c70a16019e5,tests/lib/test_converters.py::test_convert_json_1,tests/lib/test_converters.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import csv import json import os import tempfile from libcove.config import LibCoveConfig from libcove.lib.converters import convert_json, convert_spreadsheet def test_convert_json_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-ocds-tests-"", dir=tempfile.gettempdir() ) json_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_json_1.json"", ) lib_cove_config = LibCoveConfig() output = convert_json( cove_temp_folder, """", json_filename, lib_cove_config, flatten=True ) assert output[""converted_url""] == ""/flattened"" assert len(output[""conversion_warning_messages""]) == 0 assert output[""conversion""] == ""flatten"" conversion_warning_messages_name = os.path.join( cove_temp_folder, ""conversion_warning_messages.json"" ) assert os.path.isfile(conversion_warning_messages_name) with open(conversion_warning_messages_name) as fp: conversion_warning_messages_data = json.load(fp) assert conversion_warning_messages_data == [] assert os.path.isfile(os.path.join(cove_temp_folder, ""flattened"", ""main.csv"")) with open(os.path.join(cove_temp_folder, ""flattened"", ""main.csv""), ""r"") as csvfile: csvreader = csv.reader(csvfile) header = next(csvreader) assert header[0] == ""id"" assert header[1] == ""title"" row1 = next(csvreader) assert row1[0] == ""1"" assert row1[1] == ""Cat"" row2 = next(csvreader) assert row2[0] == ""2"" assert row2[1] == ""Hat"" def test_convert_activity_xml_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-iati-tests-"", dir=tempfile.gettempdir() ) activity_xml_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_activity_1.xml"", ) lib_cove_config = LibCoveConfig() output = convert_json( cove_temp_folder, """", activity_xml_filename, lib_cove_config, flatten=True, xml=True, root_list_path=""iati-activity"", ) assert output[""converted_url""] == ""/flattened"" assert len(output[""conversion_warning_messages""]) == 0 assert output[""conversion""] == ""flatten"" conversion_warning_messages_name = os.path.join( cove_temp_folder, ""conversion_warning_messages.json"" ) assert os.path.isfile(conversion_warning_messages_name) with open(conversion_warning_messages_name) as fp: conversion_warning_messages_data = json.load(fp) assert conversion_warning_messages_data == [] assert os.path.isfile(os.path.join(cove_temp_folder, ""flattened.xlsx"")) assert os.path.isfile( os.path.join(cove_temp_folder, ""flattened"", ""iati-activity.csv"") ) with open( os.path.join(cove_temp_folder, ""flattened"", ""iati-activity.csv""), ""r"" ) as csvfile: csvreader = csv.reader(csvfile) header = next(csvreader) assert header[0] == ""@default-currency"" assert header[1] == ""iati-identifier"" row1 = next(csvreader) assert row1[0] == ""GBP"" assert row1[1] == ""GB-TEST-13-example_ODSC_2019"" def test_convert_org_xml_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-iati-tests-"", dir=tempfile.gettempdir() ) organisation_xml_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_org_1.xml"", ) lib_cove_config = LibCoveConfig() output = convert_json( cove_temp_folder, """", organisation_xml_filename, lib_cove_config, flatten=True, xml=True, root_list_path=""iati-organisation"", root_id=""organisation-identifier"", ) assert output[""converted_url""] == ""/flattened"" assert len(output[""conversion_warning_messages""]) == 0 assert output[""conversion""] == ""flatten"" conversion_warning_messages_name = os.path.join( cove_temp_folder, ""conversion_warning_messages.json"" ) assert os.path.isfile(conversion_warning_messages_name) with open(conversion_warning_messages_name) as fp: conversion_warning_messages_data = json.load(fp) assert conversion_warning_messages_data == [] assert os.path.isfile(os.path.join(cove_temp_folder, ""flattened.xlsx"")) assert os.path.isfile( os.path.join(cove_temp_folder, ""flattened"", ""iati-organisation.csv"") ) with open( os.path.join(cove_temp_folder, ""flattened"", ""iati-organisation.csv""), ""r"" ) as csvfile: csvreader = csv.reader(csvfile) header = next(csvreader) assert header[0] == ""organisation-identifier"" assert header[4] == ""name/narrative"" row1 = next(csvreader) assert row1[0] == ""GB-GOV-1"" assert row1[4] == ""UK Department for International Development"" def test_convert_json_root_is_list_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-ocds-tests-"", dir=tempfile.gettempdir() ) json_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_json_root_is_list_1.json"", ) lib_cove_config = LibCoveConfig() lib_cove_config.config[""root_is_list""] = True output = convert_json( cove_temp_folder, """", json_filename, lib_cove_config, flatten=True ) assert output[""converted_url""] == ""/flattened"" assert len(output[""conversion_warning_messages""]) == 0 assert output[""conversion""] == ""flatten"" conversion_warning_messages_name = os.path.join( cove_temp_folder, ""conversion_warning_messages.json"" ) assert os.path.isfile(conversion_warning_messages_name) with open(conversion_warning_messages_name) as fp: conversion_warning_messages_data = json.load(fp) assert conversion_warning_messages_data == [] assert os.path.isfile(os.path.join(cove_temp_folder, ""flattened"", ""main.csv"")) with open(os.path.join(cove_temp_folder, ""flattened"", ""main.csv""), ""r"") as csvfile: csvreader = csv.reader(csvfile) header = next(csvreader) assert header[0] == ""id"" assert header[1] == ""title"" row1 = next(csvreader) assert row1[0] == ""1"" assert row1[1] == ""Cat"" row2 = next(csvreader) assert row2[0] == ""2"" assert row2[1] == ""Hat"" def test_convert_csv_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-ocds-tests-"", dir=tempfile.gettempdir() ) csv_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_csv_1.csv"", ) csv_schema_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_csv_1_schema.json"", ) lib_cove_config = LibCoveConfig() lib_cove_config.config[""id_name""] = ""thing_id"" lib_cove_config.config[""root_is_list""] = True output = convert_spreadsheet( cove_temp_folder, """", csv_filename, ""csv"", lib_cove_config, schema_url=csv_schema_filename, ) assert output[""conversion""] == ""unflatten"" with open(output[""converted_path""]) as fp: json_data = json.load(fp) assert json_data == [ {""thing_id"": ""1"", ""title"": ""Cat""}, {""thing_id"": ""2"", ""title"": ""Hat""}, ] ", https://github.com/OpenDataServices/lib-cove,74ba874ec6882969894797ee0dbc5c70a16019e5,tests/lib/test_converters.py::test_convert_org_xml_1,tests/lib/test_converters.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import csv import json import os import tempfile from libcove.config import LibCoveConfig from libcove.lib.converters import convert_json, convert_spreadsheet def test_convert_json_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-ocds-tests-"", dir=tempfile.gettempdir() ) json_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_json_1.json"", ) lib_cove_config = LibCoveConfig() output = convert_json( cove_temp_folder, """", json_filename, lib_cove_config, flatten=True ) assert output[""converted_url""] == ""/flattened"" assert len(output[""conversion_warning_messages""]) == 0 assert output[""conversion""] == ""flatten"" conversion_warning_messages_name = os.path.join( cove_temp_folder, ""conversion_warning_messages.json"" ) assert os.path.isfile(conversion_warning_messages_name) with open(conversion_warning_messages_name) as fp: conversion_warning_messages_data = json.load(fp) assert conversion_warning_messages_data == [] assert os.path.isfile(os.path.join(cove_temp_folder, ""flattened"", ""main.csv"")) with open(os.path.join(cove_temp_folder, ""flattened"", ""main.csv""), ""r"") as csvfile: csvreader = csv.reader(csvfile) header = next(csvreader) assert header[0] == ""id"" assert header[1] == ""title"" row1 = next(csvreader) assert row1[0] == ""1"" assert row1[1] == ""Cat"" row2 = next(csvreader) assert row2[0] == ""2"" assert row2[1] == ""Hat"" def test_convert_activity_xml_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-iati-tests-"", dir=tempfile.gettempdir() ) activity_xml_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_activity_1.xml"", ) lib_cove_config = LibCoveConfig() output = convert_json( cove_temp_folder, """", activity_xml_filename, lib_cove_config, flatten=True, xml=True, root_list_path=""iati-activity"", ) assert output[""converted_url""] == ""/flattened"" assert len(output[""conversion_warning_messages""]) == 0 assert output[""conversion""] == ""flatten"" conversion_warning_messages_name = os.path.join( cove_temp_folder, ""conversion_warning_messages.json"" ) assert os.path.isfile(conversion_warning_messages_name) with open(conversion_warning_messages_name) as fp: conversion_warning_messages_data = json.load(fp) assert conversion_warning_messages_data == [] assert os.path.isfile(os.path.join(cove_temp_folder, ""flattened.xlsx"")) assert os.path.isfile( os.path.join(cove_temp_folder, ""flattened"", ""iati-activity.csv"") ) with open( os.path.join(cove_temp_folder, ""flattened"", ""iati-activity.csv""), ""r"" ) as csvfile: csvreader = csv.reader(csvfile) header = next(csvreader) assert header[0] == ""@default-currency"" assert header[1] == ""iati-identifier"" row1 = next(csvreader) assert row1[0] == ""GBP"" assert row1[1] == ""GB-TEST-13-example_ODSC_2019"" def test_convert_org_xml_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-iati-tests-"", dir=tempfile.gettempdir() ) organisation_xml_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_org_1.xml"", ) lib_cove_config = LibCoveConfig() output = convert_json( cove_temp_folder, """", organisation_xml_filename, lib_cove_config, flatten=True, xml=True, root_list_path=""iati-organisation"", root_id=""organisation-identifier"", ) assert output[""converted_url""] == ""/flattened"" assert len(output[""conversion_warning_messages""]) == 0 assert output[""conversion""] == ""flatten"" conversion_warning_messages_name = os.path.join( cove_temp_folder, ""conversion_warning_messages.json"" ) assert os.path.isfile(conversion_warning_messages_name) with open(conversion_warning_messages_name) as fp: conversion_warning_messages_data = json.load(fp) assert conversion_warning_messages_data == [] assert os.path.isfile(os.path.join(cove_temp_folder, ""flattened.xlsx"")) assert os.path.isfile( os.path.join(cove_temp_folder, ""flattened"", ""iati-organisation.csv"") ) with open( os.path.join(cove_temp_folder, ""flattened"", ""iati-organisation.csv""), ""r"" ) as csvfile: csvreader = csv.reader(csvfile) header = next(csvreader) assert header[0] == ""organisation-identifier"" assert header[4] == ""name/narrative"" row1 = next(csvreader) assert row1[0] == ""GB-GOV-1"" assert row1[4] == ""UK Department for International Development"" def test_convert_json_root_is_list_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-ocds-tests-"", dir=tempfile.gettempdir() ) json_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_json_root_is_list_1.json"", ) lib_cove_config = LibCoveConfig() lib_cove_config.config[""root_is_list""] = True output = convert_json( cove_temp_folder, """", json_filename, lib_cove_config, flatten=True ) assert output[""converted_url""] == ""/flattened"" assert len(output[""conversion_warning_messages""]) == 0 assert output[""conversion""] == ""flatten"" conversion_warning_messages_name = os.path.join( cove_temp_folder, ""conversion_warning_messages.json"" ) assert os.path.isfile(conversion_warning_messages_name) with open(conversion_warning_messages_name) as fp: conversion_warning_messages_data = json.load(fp) assert conversion_warning_messages_data == [] assert os.path.isfile(os.path.join(cove_temp_folder, ""flattened"", ""main.csv"")) with open(os.path.join(cove_temp_folder, ""flattened"", ""main.csv""), ""r"") as csvfile: csvreader = csv.reader(csvfile) header = next(csvreader) assert header[0] == ""id"" assert header[1] == ""title"" row1 = next(csvreader) assert row1[0] == ""1"" assert row1[1] == ""Cat"" row2 = next(csvreader) assert row2[0] == ""2"" assert row2[1] == ""Hat"" def test_convert_csv_1(): cove_temp_folder = tempfile.mkdtemp( prefix=""lib-cove-ocds-tests-"", dir=tempfile.gettempdir() ) csv_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_csv_1.csv"", ) csv_schema_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), ""fixtures"", ""converters"", ""convert_csv_1_schema.json"", ) lib_cove_config = LibCoveConfig() lib_cove_config.config[""id_name""] = ""thing_id"" lib_cove_config.config[""root_is_list""] = True output = convert_spreadsheet( cove_temp_folder, """", csv_filename, ""csv"", lib_cove_config, schema_url=csv_schema_filename, ) assert output[""conversion""] == ""unflatten"" with open(output[""converted_path""]) as fp: json_data = json.load(fp) assert json_data == [ {""thing_id"": ""1"", ""title"": ""Cat""}, {""thing_id"": ""2"", ""title"": ""Hat""}, ] ", https://github.com/openworm/YAROM,cd4fb6cb8fac72a198858225ce89ad8b9c9175fd,tests/test_mapper.py::MapperTest::test_children_are_added,tests/test_mapper.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import unittest from yarom.configure import Configureable from yarom.data import Data from yarom.mapper import Mapper from yarom.mappedClass import MappedClass from .base_test import _DataTestB import rdflib class MapperTest(_DataTestB): def setUp(self): _DataTestB.setUp(self) self.mapper = Mapper(('yarom.dataObject.DataObject', 'yarom.simpleProperty.SimpleProperty')) self.DataObject = self.mapper.load_class('yarom.dataObject.DataObject') Configureable.conf = self.TestConfig Configureable.conf = Data() Configureable.conf.openDatabase() def tearDown(self): Configureable.conf.closeDatabase() self.mapper.deregister_all() self.mapper = None _DataTestB.tearDown(self) @unittest.expectedFailure def test_add_to_graph(self): """"""Test that we can load a descendant of DataObject as a class"""""" # TODO: See related TODO in mapper.py dc = MappedClass(""TestDOM"", (self.DataObject,), dict()) self.assertIn( (dc.rdf_type, rdflib.RDFS['subClassOf'], self.DataObject.rdf_type), dc.du.rdf) def test_object_from_id_class(self): """""" Ensure we get an object from just the class name """""" dc = MappedClass(""TestDOM"", (self.DataObject,), dict()) self.mapper.add_class(dc) self.mapper.remap() g = self.mapper.oid(dc.rdf_type) self.assertIsInstance(g, dc) def test_children_are_added(self): """""" Ensure that, on registration, children are added """""" cls = MappedClass(""TestDOM"", (self.DataObject,), dict()) self.mapper.add_class(cls) self.assertIn( cls, self.DataObject.children, msg=""The test class is a child"") def test_children_are_deregistered(self): """""" Ensure that, on deregistration, DataObject types are cleared from the module namespace """""" self.mapper.deregister_all() self.assertEqual(len(self.mapper.MappedClasses), 0, msg=""No mapped classes"") ", https://github.com/openworm/YAROM,cd4fb6cb8fac72a198858225ce89ad8b9c9175fd,tests/test_mapper.py::MapperTest::test_children_are_deregistered,tests/test_mapper.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import unittest from yarom.configure import Configureable from yarom.data import Data from yarom.mapper import Mapper from yarom.mappedClass import MappedClass from .base_test import _DataTestB import rdflib class MapperTest(_DataTestB): def setUp(self): _DataTestB.setUp(self) self.mapper = Mapper(('yarom.dataObject.DataObject', 'yarom.simpleProperty.SimpleProperty')) self.DataObject = self.mapper.load_class('yarom.dataObject.DataObject') Configureable.conf = self.TestConfig Configureable.conf = Data() Configureable.conf.openDatabase() def tearDown(self): Configureable.conf.closeDatabase() self.mapper.deregister_all() self.mapper = None _DataTestB.tearDown(self) @unittest.expectedFailure def test_add_to_graph(self): """"""Test that we can load a descendant of DataObject as a class"""""" # TODO: See related TODO in mapper.py dc = MappedClass(""TestDOM"", (self.DataObject,), dict()) self.assertIn( (dc.rdf_type, rdflib.RDFS['subClassOf'], self.DataObject.rdf_type), dc.du.rdf) def test_object_from_id_class(self): """""" Ensure we get an object from just the class name """""" dc = MappedClass(""TestDOM"", (self.DataObject,), dict()) self.mapper.add_class(dc) self.mapper.remap() g = self.mapper.oid(dc.rdf_type) self.assertIsInstance(g, dc) def test_children_are_added(self): """""" Ensure that, on registration, children are added """""" cls = MappedClass(""TestDOM"", (self.DataObject,), dict()) self.mapper.add_class(cls) self.assertIn( cls, self.DataObject.children, msg=""The test class is a child"") def test_children_are_deregistered(self): """""" Ensure that, on deregistration, DataObject types are cleared from the module namespace """""" self.mapper.deregister_all() self.assertEqual(len(self.mapper.MappedClasses), 0, msg=""No mapped classes"") ", https://github.com/openworm/YAROM,cd4fb6cb8fac72a198858225ce89ad8b9c9175fd,tests/test_mapper.py::MapperTest::test_object_from_id_class,tests/test_mapper.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import unittest from yarom.configure import Configureable from yarom.data import Data from yarom.mapper import Mapper from yarom.mappedClass import MappedClass from .base_test import _DataTestB import rdflib class MapperTest(_DataTestB): def setUp(self): _DataTestB.setUp(self) self.mapper = Mapper(('yarom.dataObject.DataObject', 'yarom.simpleProperty.SimpleProperty')) self.DataObject = self.mapper.load_class('yarom.dataObject.DataObject') Configureable.conf = self.TestConfig Configureable.conf = Data() Configureable.conf.openDatabase() def tearDown(self): Configureable.conf.closeDatabase() self.mapper.deregister_all() self.mapper = None _DataTestB.tearDown(self) @unittest.expectedFailure def test_add_to_graph(self): """"""Test that we can load a descendant of DataObject as a class"""""" # TODO: See related TODO in mapper.py dc = MappedClass(""TestDOM"", (self.DataObject,), dict()) self.assertIn( (dc.rdf_type, rdflib.RDFS['subClassOf'], self.DataObject.rdf_type), dc.du.rdf) def test_object_from_id_class(self): """""" Ensure we get an object from just the class name """""" dc = MappedClass(""TestDOM"", (self.DataObject,), dict()) self.mapper.add_class(dc) self.mapper.remap() g = self.mapper.oid(dc.rdf_type) self.assertIsInstance(g, dc) def test_children_are_added(self): """""" Ensure that, on registration, children are added """""" cls = MappedClass(""TestDOM"", (self.DataObject,), dict()) self.mapper.add_class(cls) self.assertIn( cls, self.DataObject.children, msg=""The test class is a child"") def test_children_are_deregistered(self): """""" Ensure that, on deregistration, DataObject types are cleared from the module namespace """""" self.mapper.deregister_all() self.assertEqual(len(self.mapper.MappedClasses), 0, msg=""No mapped classes"") ", https://github.com/opper/pyserverpilot,b6b896fea1155fa95febde84f74e0d2230523ab0,tests/test_app.py::TestApp::test_add_ssl,tests/test_app.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from unittest.mock import patch import pytest from marshmallow import ValidationError from pyserverpilot import Serverpilot from pyserverpilot.models.app import App as AppModel from pyserverpilot.modules import Apps as AppsModule from .mocks.app_mock import AppMock @pytest.fixture def client(): return Serverpilot.client('apps', client_id='test_id', api_key='test_key') @pytest.fixture(scope='module') def shared(): return { 'app': None, } @patch('pyserverpilot.serverpilot.requests.request') class TestApp(object): def test_create_app(self, mock_sp, client: AppsModule, shared): mock_sp.return_value = AppMock('get_app') app_data = AppMock.create_app()['data'] response = client.create_app(**app_data) assert response.name == app_data['name'] shared['app'] = response def test_get_app(self, mock_sp, client: AppsModule, shared): mock_sp.return_value = AppMock('get_app') app = shared['app'] response = client.get_app(app.id) assert response.id == app.id def test_app_create_validation(self, mock_sp, client: AppsModule): with pytest.raises(ValidationError): client.create_app() # no parameters client.create_app(**AppMock.create_app(), unknown_param='should trigger error') # unknown parameters client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains='should trigger error') # invalid parameter type client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains=['website.com', 'www.website.com'], wordpress={}) # invalid worpdress fields client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains=['website.com', 'www.website.com'], wordpress={ 'site_title': 'Awesome site', 'admin_user': 'admin', 'admin_password': 'fail', # should fail because of invalid password 'admin_email': 'admin@website.com' }) mock_sp.return_value = AppMock('') client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains=['website.com', 'www.website.com'], wordpress={ 'site_title': 'Awesome site', 'admin_user': 'admin', 'admin_password': 'shouldnotfail', 'admin_email': 'admin@website.com' }) def test_update_app_validation(self, mock_sp, client: AppsModule, shared): app = shared['app'] # type: AppModel with pytest.raises(ValidationError): client.update_app(app.id, domains=""website.com"") # invalid parameter type mock_sp.return_value = AppMock('update_app') response = client.update_app(app.id, domains=['www.myshop.com', 'myshop.com'], runtime='php7.1') assert response.id == app.id assert response.runtime == 'php7.2' def test_add_ssl(self, mock_sp, client: AppsModule, shared): app = shared['app'] # type: AppModel with pytest.raises(ValidationError): client.add_ssl(app.id) # missing params client.add_ssl(app.id, key=123, cert='sslcert', cacerts='sslcacert') # invalid parameter type mock_sp.return_value = AppMock('add_ssl') response = client.add_ssl(app.id, key='sslkey', cert='sslcert', cacerts=None) assert response.key == 'sslkey' assert response.cert == 'sslcert' app.ssl = response def test_enable_force_ssl(self, mock_sp, client: AppsModule, shared): app = shared['app'] # type: AppModel with pytest.raises(ValidationError): client.set_force_ssl(app.id) client.set_force_ssl(app.id, force=""yes"") # invalid parameter type mock_sp.return_value = AppMock('set_force_ssl') response = client.set_force_ssl(app.id, force=True) assert response.key == app.ssl.key assert response.force is True ", https://github.com/opper/pyserverpilot,b6b896fea1155fa95febde84f74e0d2230523ab0,tests/test_app.py::TestApp::test_enable_force_ssl,tests/test_app.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from unittest.mock import patch import pytest from marshmallow import ValidationError from pyserverpilot import Serverpilot from pyserverpilot.models.app import App as AppModel from pyserverpilot.modules import Apps as AppsModule from .mocks.app_mock import AppMock @pytest.fixture def client(): return Serverpilot.client('apps', client_id='test_id', api_key='test_key') @pytest.fixture(scope='module') def shared(): return { 'app': None, } @patch('pyserverpilot.serverpilot.requests.request') class TestApp(object): def test_create_app(self, mock_sp, client: AppsModule, shared): mock_sp.return_value = AppMock('get_app') app_data = AppMock.create_app()['data'] response = client.create_app(**app_data) assert response.name == app_data['name'] shared['app'] = response def test_get_app(self, mock_sp, client: AppsModule, shared): mock_sp.return_value = AppMock('get_app') app = shared['app'] response = client.get_app(app.id) assert response.id == app.id def test_app_create_validation(self, mock_sp, client: AppsModule): with pytest.raises(ValidationError): client.create_app() # no parameters client.create_app(**AppMock.create_app(), unknown_param='should trigger error') # unknown parameters client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains='should trigger error') # invalid parameter type client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains=['website.com', 'www.website.com'], wordpress={}) # invalid worpdress fields client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains=['website.com', 'www.website.com'], wordpress={ 'site_title': 'Awesome site', 'admin_user': 'admin', 'admin_password': 'fail', # should fail because of invalid password 'admin_email': 'admin@website.com' }) mock_sp.return_value = AppMock('') client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains=['website.com', 'www.website.com'], wordpress={ 'site_title': 'Awesome site', 'admin_user': 'admin', 'admin_password': 'shouldnotfail', 'admin_email': 'admin@website.com' }) def test_update_app_validation(self, mock_sp, client: AppsModule, shared): app = shared['app'] # type: AppModel with pytest.raises(ValidationError): client.update_app(app.id, domains=""website.com"") # invalid parameter type mock_sp.return_value = AppMock('update_app') response = client.update_app(app.id, domains=['www.myshop.com', 'myshop.com'], runtime='php7.1') assert response.id == app.id assert response.runtime == 'php7.2' def test_add_ssl(self, mock_sp, client: AppsModule, shared): app = shared['app'] # type: AppModel with pytest.raises(ValidationError): client.add_ssl(app.id) # missing params client.add_ssl(app.id, key=123, cert='sslcert', cacerts='sslcacert') # invalid parameter type mock_sp.return_value = AppMock('add_ssl') response = client.add_ssl(app.id, key='sslkey', cert='sslcert', cacerts=None) assert response.key == 'sslkey' assert response.cert == 'sslcert' app.ssl = response def test_enable_force_ssl(self, mock_sp, client: AppsModule, shared): app = shared['app'] # type: AppModel with pytest.raises(ValidationError): client.set_force_ssl(app.id) client.set_force_ssl(app.id, force=""yes"") # invalid parameter type mock_sp.return_value = AppMock('set_force_ssl') response = client.set_force_ssl(app.id, force=True) assert response.key == app.ssl.key assert response.force is True ", https://github.com/opper/pyserverpilot,b6b896fea1155fa95febde84f74e0d2230523ab0,tests/test_app.py::TestApp::test_get_app,tests/test_app.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from unittest.mock import patch import pytest from marshmallow import ValidationError from pyserverpilot import Serverpilot from pyserverpilot.models.app import App as AppModel from pyserverpilot.modules import Apps as AppsModule from .mocks.app_mock import AppMock @pytest.fixture def client(): return Serverpilot.client('apps', client_id='test_id', api_key='test_key') @pytest.fixture(scope='module') def shared(): return { 'app': None, } @patch('pyserverpilot.serverpilot.requests.request') class TestApp(object): def test_create_app(self, mock_sp, client: AppsModule, shared): mock_sp.return_value = AppMock('get_app') app_data = AppMock.create_app()['data'] response = client.create_app(**app_data) assert response.name == app_data['name'] shared['app'] = response def test_get_app(self, mock_sp, client: AppsModule, shared): mock_sp.return_value = AppMock('get_app') app = shared['app'] response = client.get_app(app.id) assert response.id == app.id def test_app_create_validation(self, mock_sp, client: AppsModule): with pytest.raises(ValidationError): client.create_app() # no parameters client.create_app(**AppMock.create_app(), unknown_param='should trigger error') # unknown parameters client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains='should trigger error') # invalid parameter type client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains=['website.com', 'www.website.com'], wordpress={}) # invalid worpdress fields client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains=['website.com', 'www.website.com'], wordpress={ 'site_title': 'Awesome site', 'admin_user': 'admin', 'admin_password': 'fail', # should fail because of invalid password 'admin_email': 'admin@website.com' }) mock_sp.return_value = AppMock('') client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains=['website.com', 'www.website.com'], wordpress={ 'site_title': 'Awesome site', 'admin_user': 'admin', 'admin_password': 'shouldnotfail', 'admin_email': 'admin@website.com' }) def test_update_app_validation(self, mock_sp, client: AppsModule, shared): app = shared['app'] # type: AppModel with pytest.raises(ValidationError): client.update_app(app.id, domains=""website.com"") # invalid parameter type mock_sp.return_value = AppMock('update_app') response = client.update_app(app.id, domains=['www.myshop.com', 'myshop.com'], runtime='php7.1') assert response.id == app.id assert response.runtime == 'php7.2' def test_add_ssl(self, mock_sp, client: AppsModule, shared): app = shared['app'] # type: AppModel with pytest.raises(ValidationError): client.add_ssl(app.id) # missing params client.add_ssl(app.id, key=123, cert='sslcert', cacerts='sslcacert') # invalid parameter type mock_sp.return_value = AppMock('add_ssl') response = client.add_ssl(app.id, key='sslkey', cert='sslcert', cacerts=None) assert response.key == 'sslkey' assert response.cert == 'sslcert' app.ssl = response def test_enable_force_ssl(self, mock_sp, client: AppsModule, shared): app = shared['app'] # type: AppModel with pytest.raises(ValidationError): client.set_force_ssl(app.id) client.set_force_ssl(app.id, force=""yes"") # invalid parameter type mock_sp.return_value = AppMock('set_force_ssl') response = client.set_force_ssl(app.id, force=True) assert response.key == app.ssl.key assert response.force is True ", https://github.com/opper/pyserverpilot,b6b896fea1155fa95febde84f74e0d2230523ab0,tests/test_app.py::TestApp::test_update_app_validation,tests/test_app.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from unittest.mock import patch import pytest from marshmallow import ValidationError from pyserverpilot import Serverpilot from pyserverpilot.models.app import App as AppModel from pyserverpilot.modules import Apps as AppsModule from .mocks.app_mock import AppMock @pytest.fixture def client(): return Serverpilot.client('apps', client_id='test_id', api_key='test_key') @pytest.fixture(scope='module') def shared(): return { 'app': None, } @patch('pyserverpilot.serverpilot.requests.request') class TestApp(object): def test_create_app(self, mock_sp, client: AppsModule, shared): mock_sp.return_value = AppMock('get_app') app_data = AppMock.create_app()['data'] response = client.create_app(**app_data) assert response.name == app_data['name'] shared['app'] = response def test_get_app(self, mock_sp, client: AppsModule, shared): mock_sp.return_value = AppMock('get_app') app = shared['app'] response = client.get_app(app.id) assert response.id == app.id def test_app_create_validation(self, mock_sp, client: AppsModule): with pytest.raises(ValidationError): client.create_app() # no parameters client.create_app(**AppMock.create_app(), unknown_param='should trigger error') # unknown parameters client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains='should trigger error') # invalid parameter type client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains=['website.com', 'www.website.com'], wordpress={}) # invalid worpdress fields client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains=['website.com', 'www.website.com'], wordpress={ 'site_title': 'Awesome site', 'admin_user': 'admin', 'admin_password': 'fail', # should fail because of invalid password 'admin_email': 'admin@website.com' }) mock_sp.return_value = AppMock('') client.create_app(name='app', sysuserid='userid', runtime='php7.1', domains=['website.com', 'www.website.com'], wordpress={ 'site_title': 'Awesome site', 'admin_user': 'admin', 'admin_password': 'shouldnotfail', 'admin_email': 'admin@website.com' }) def test_update_app_validation(self, mock_sp, client: AppsModule, shared): app = shared['app'] # type: AppModel with pytest.raises(ValidationError): client.update_app(app.id, domains=""website.com"") # invalid parameter type mock_sp.return_value = AppMock('update_app') response = client.update_app(app.id, domains=['www.myshop.com', 'myshop.com'], runtime='php7.1') assert response.id == app.id assert response.runtime == 'php7.2' def test_add_ssl(self, mock_sp, client: AppsModule, shared): app = shared['app'] # type: AppModel with pytest.raises(ValidationError): client.add_ssl(app.id) # missing params client.add_ssl(app.id, key=123, cert='sslcert', cacerts='sslcacert') # invalid parameter type mock_sp.return_value = AppMock('add_ssl') response = client.add_ssl(app.id, key='sslkey', cert='sslcert', cacerts=None) assert response.key == 'sslkey' assert response.cert == 'sslcert' app.ssl = response def test_enable_force_ssl(self, mock_sp, client: AppsModule, shared): app = shared['app'] # type: AppModel with pytest.raises(ValidationError): client.set_force_ssl(app.id) client.set_force_ssl(app.id, force=""yes"") # invalid parameter type mock_sp.return_value = AppMock('set_force_ssl') response = client.set_force_ssl(app.id, force=True) assert response.key == app.ssl.key assert response.force is True ", https://github.com/opper/pyserverpilot,b6b896fea1155fa95febde84f74e0d2230523ab0,tests/test_db.py::TestDb::test_get_db,tests/test_db.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from unittest.mock import patch import pytest from marshmallow import ValidationError from pyserverpilot import Serverpilot from pyserverpilot.modules.dbs import Dbs as DbsModule from tests.mocks.db_mock import DbMock @pytest.fixture def client(): return Serverpilot.client('dbs', client_id='test_id', api_key='test_key') @pytest.fixture(scope='module') def shared(): return { 'db': None, } @patch('pyserverpilot.serverpilot.requests.request') class TestDb(object): def test_get_dbs(self, mock_sp, client: DbsModule): mock_sp.return_value = DbMock('get_dbs') response = client.get_dbs() assert len(response) == 2 def test_create_db(self, mock_sp, client: DbsModule, shared): mock_sp.return_value = DbMock('get_db') db_data = DbMock.create_db()['data'] response = client.create_db(**db_data) assert response.name == db_data['name'] shared['db'] = response def test_get_db(self, mock_sp, client: DbsModule, shared): db = shared['db'] mock_sp.return_value = DbMock('get_db') response = client.get_db(db.id) assert response.id == db.id assert response.appid == db.appid assert response.serverid == db.serverid def test_fail_create_db(self, mock_sp, client: DbsModule): with pytest.raises(ValidationError): client.create_db() # no params error client.create_db(appid='app', name='database!', # name contains invalid characters user={}) client.create_db(appid='app', name='database', user={ 'name': 'superlongnameshouldbeinvalid', # invalid username 'password': 'BaOWYl3IjMc4raBe' }) client.create_db(appid='app', name='database', user={ 'name': 'username', 'password': 'passwd', # invalid password }) client.create_db(appid='app', name='database', user={ 'name': 'username', 'password': 'BaOWYl3IjMc4raBe', 'hello': 'extraparam' # extra invalid param }) def test_update_db(self, mock_sp, client: DbsModule, shared): db = shared['db'] with pytest.raises(ValidationError): client.update_db(db.id) # no params error client.update_db(db.id, user={}) # invalid user schema client.update_db(db.id, user={ 'name': 'superlongnameshouldbeinvalid', # invalid username 'password': 'BaOWYl3IjMc4raBe' }) client.update_db(db.id, user={ 'name': 'username', 'password': 'passw', # invalid password }) client.update_db(db.id, user={ 'name': 'username', 'password': 'BaOWYl3IjMc4raBe', 'hello': 'extraparam' # extra invalid param }) new_user = { 'name': 'jerry', 'password': 'BaOWYl3IjMc4raBe' } mock_sp.return_value = DbMock('update_db') client.update_db(db.id, user=new_user) mock_sp.return_value = DbMock('update_db') response = client.get_db(shared['db'].id) assert response.user['name'] == new_user['name'] ", https://github.com/opper/pyserverpilot,b6b896fea1155fa95febde84f74e0d2230523ab0,tests/test_db.py::TestDb::test_update_db,tests/test_db.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from unittest.mock import patch import pytest from marshmallow import ValidationError from pyserverpilot import Serverpilot from pyserverpilot.modules.dbs import Dbs as DbsModule from tests.mocks.db_mock import DbMock @pytest.fixture def client(): return Serverpilot.client('dbs', client_id='test_id', api_key='test_key') @pytest.fixture(scope='module') def shared(): return { 'db': None, } @patch('pyserverpilot.serverpilot.requests.request') class TestDb(object): def test_get_dbs(self, mock_sp, client: DbsModule): mock_sp.return_value = DbMock('get_dbs') response = client.get_dbs() assert len(response) == 2 def test_create_db(self, mock_sp, client: DbsModule, shared): mock_sp.return_value = DbMock('get_db') db_data = DbMock.create_db()['data'] response = client.create_db(**db_data) assert response.name == db_data['name'] shared['db'] = response def test_get_db(self, mock_sp, client: DbsModule, shared): db = shared['db'] mock_sp.return_value = DbMock('get_db') response = client.get_db(db.id) assert response.id == db.id assert response.appid == db.appid assert response.serverid == db.serverid def test_fail_create_db(self, mock_sp, client: DbsModule): with pytest.raises(ValidationError): client.create_db() # no params error client.create_db(appid='app', name='database!', # name contains invalid characters user={}) client.create_db(appid='app', name='database', user={ 'name': 'superlongnameshouldbeinvalid', # invalid username 'password': 'BaOWYl3IjMc4raBe' }) client.create_db(appid='app', name='database', user={ 'name': 'username', 'password': 'passwd', # invalid password }) client.create_db(appid='app', name='database', user={ 'name': 'username', 'password': 'BaOWYl3IjMc4raBe', 'hello': 'extraparam' # extra invalid param }) def test_update_db(self, mock_sp, client: DbsModule, shared): db = shared['db'] with pytest.raises(ValidationError): client.update_db(db.id) # no params error client.update_db(db.id, user={}) # invalid user schema client.update_db(db.id, user={ 'name': 'superlongnameshouldbeinvalid', # invalid username 'password': 'BaOWYl3IjMc4raBe' }) client.update_db(db.id, user={ 'name': 'username', 'password': 'passw', # invalid password }) client.update_db(db.id, user={ 'name': 'username', 'password': 'BaOWYl3IjMc4raBe', 'hello': 'extraparam' # extra invalid param }) new_user = { 'name': 'jerry', 'password': 'BaOWYl3IjMc4raBe' } mock_sp.return_value = DbMock('update_db') client.update_db(db.id, user=new_user) mock_sp.return_value = DbMock('update_db') response = client.get_db(shared['db'].id) assert response.user['name'] == new_user['name'] ", https://github.com/Osirium/vcdriver,2f311df9a73c82b5837d01bac16a10eb4db955eb,test/unit/test_config.py::test_read,test/unit/test_config.py,NIO,flaky,Opened,https://github.com/Osirium/vcdriver/pull/23,classify;root_cause,"import os import mock import pytest from six.moves import configparser from vcdriver.config import load, configurable, read, reset def require_nothing(**kwargs): pass @configurable([('Vsphere Session', 'vcdriver_username')]) def require_username(vcdriver_username): pass @configurable([ ('Vsphere Session', 'vcdriver_username'), ('Vsphere Session', 'vcdriver_password') ]) def require_username_and_password( vcdriver_username, vcdriver_password ): pass @configurable([('Bad', 'Wrong')]) def require_bad_section(**kwargs): pass def config_file(path, prepopulated_data=None): config = configparser.RawConfigParser() config.add_section('Vsphere Session') config.set('Vsphere Session', 'vcdriver_host', '') config.set('Vsphere Session', 'vcdriver_port', '443') config.set('Vsphere Session', 'vcdriver_username', '') config.set('Vsphere Session', 'vcdriver_password', '') config.add_section('Virtual Machine Deployment') config.set( 'Virtual Machine Deployment', 'vcdriver_resource_pool', '' ) config.set('Virtual Machine Deployment', 'vcdriver_data_store', '') config.set( 'Virtual Machine Deployment', 'vcdriver_data_store_threshold', '0' ) config.set('Virtual Machine Deployment', 'vcdriver_folder', '') config.add_section('Virtual Machine Remote Management') config.set( 'Virtual Machine Remote Management', 'vcdriver_vm_ssh_username', '' ) config.set( 'Virtual Machine Remote Management', 'vcdriver_vm_ssh_password', '' ) config.set( 'Virtual Machine Remote Management', 'vcdriver_vm_winrm_username', '' ) config.set( 'Virtual Machine Remote Management', 'vcdriver_vm_winrm_password', '' ) if prepopulated_data: for section, key, value in prepopulated_data: config.set(section, key, value) with open(path, 'w') as cf: config.write(cf) @pytest.fixture(scope='module') def config_files(): config_file('config_file_1.cfg') config_file( 'config_file_2.cfg', [['Vsphere Session', 'vcdriver_username', 'Sinatra']] ) config_file( 'config_file_3.cfg', [['Vsphere Session', 'vcdriver_password', 'myway']] ) yield os.remove('config_file_1.cfg') os.remove('config_file_2.cfg') os.remove('config_file_3.cfg') def test_read(config_files): assert read() == { 'Vsphere Session': { 'vcdriver_host': '', 'vcdriver_port': '443', 'vcdriver_username': '', 'vcdriver_password': '' }, 'Virtual Machine Deployment': { 'vcdriver_resource_pool': '', 'vcdriver_data_store': '', 'vcdriver_data_store_threshold': '0', 'vcdriver_folder': '' }, 'Virtual Machine Remote Management': { 'vcdriver_vm_ssh_username': '', 'vcdriver_vm_ssh_password': '', 'vcdriver_vm_winrm_username': '', 'vcdriver_vm_winrm_password': '' } } load('config_file_3.cfg') assert read() == { 'Vsphere Session': { 'vcdriver_host': '', 'vcdriver_port': '443', 'vcdriver_username': '', 'vcdriver_password': 'myway' }, 'Virtual Machine Deployment': { 'vcdriver_resource_pool': '', 'vcdriver_data_store': '', 'vcdriver_data_store_threshold': '0', 'vcdriver_folder': '' }, 'Virtual Machine Remote Management': { 'vcdriver_vm_ssh_username': '', 'vcdriver_vm_ssh_password': '', 'vcdriver_vm_winrm_username': '', 'vcdriver_vm_winrm_password': '' } } def test_configurable_and_reset(config_files): load() require_nothing() with mock.patch('vcdriver.config.input') as input_mock: require_username() input_mock.assert_called_once() reset() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() input_mock.assert_called_once() getpass_mock.assert_called_once() load('config_file_1.cfg') require_nothing() with mock.patch('vcdriver.config.input') as input_mock: require_username() input_mock.assert_called_once() reset() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() input_mock.assert_called_once() getpass_mock.assert_called_once() load() require_nothing() with mock.patch('vcdriver.config.input') as input_mock: require_username() input_mock.assert_called_once() reset() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() input_mock.assert_called_once() getpass_mock.assert_called_once() os.environ['vcdriver_username'] = 'Sinatra' load() require_nothing() require_username() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() getpass_mock.assert_called_once() os.environ['vcdriver_username'] = '' load() require_nothing() with mock.patch('vcdriver.config.input') as input_mock: require_username() input_mock.assert_called_once() reset() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() input_mock.assert_called_once() getpass_mock.assert_called_once() load('config_file_2.cfg') require_nothing() require_username() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() getpass_mock.assert_called_once() reset() load() require_nothing() with mock.patch('vcdriver.config.input') as input_mock: require_username() input_mock.assert_called_once() reset() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() input_mock.assert_called_once() getpass_mock.assert_called_once() os.environ['vcdriver_username'] = 'Sinatra' load('config_file_3.cfg') require_nothing() require_username() require_username_and_password() os.environ['vcdriver_username'] = '' load() require_nothing() reset() with mock.patch('vcdriver.config.input') as input_mock: require_username() input_mock.assert_called_once() reset() with mock.patch('vcdriver.config.input') as input_mock: with mock.patch('vcdriver.config.getpass.getpass') as getpass_mock: require_username_and_password() input_mock.assert_called_once() getpass_mock.assert_called_once() require_username_and_password( vcdriver_username='Sinatra', vcdriver_password='myway' ) reset() with mock.patch('vcdriver.config.input') as input_mock: with pytest.raises(KeyError): require_bad_section() input_mock.assert_called_once() ", https://github.com/oskar456/dzonegit,8f952086aa41aee4ebb7432926ed8c6a9211ac9f,test_dzonegit.py::test_get_altered_files,test_dzonegit.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause," import pytest import subprocess import time import datetime import os from io import StringIO, BytesIO from pathlib import Path import dzonegit @pytest.fixture(scope=""session"") def git_dir(tmpdir_factory): d = tmpdir_factory.getbasetemp() d.chdir() subprocess.call([""git"", ""init""]) subprocess.call([""git"", ""config"", ""user.name"", ""dzonegit pytest""]) subprocess.call([""git"", ""config"", ""user.email"", ""nonexistent@example.com""]) return d def test_get_head(git_dir): git_dir.chdir() assert dzonegit.get_head() == ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" git_dir.join(""dummy"").write(""dummy\n"") subprocess.call([""git"", ""add"", ""dummy""]) subprocess.call([""git"", ""commit"", ""-m"", ""dummy""]) assert dzonegit.get_head() != ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" def test_check_whitespace_errors(git_dir): git_dir.chdir() git_dir.join(""whitespace"").write("" "") subprocess.call([""git"", ""add"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) subprocess.call([""git"", ""rm"", ""-f"", ""whitespace""]) subprocess.call([""git"", ""commit"", ""-m"", ""rm whitespace""]) dzonegit.check_whitespace_errors(dzonegit.get_head()) dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) def test_get_file_contents(git_dir): git_dir.chdir() assert dzonegit.get_file_contents(""dummy"") == b""dummy\n"" with pytest.raises(subprocess.CalledProcessError): dzonegit.get_file_contents('nonexistent') def test_compile_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( 1234567890 ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" r = dzonegit.compile_zone(""example.org"", testzone, missing_dot=True) assert not r.success assert r.zonehash is None assert r.stderr r = dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) assert r.success assert r.serial == ""1234567890"" assert r.zonehash r2 = dzonegit.compile_zone(""example.com"", testzone + b""\n\n; some comment"") assert r.zonehash == r2.zonehash testzone += b""1 60 IN PTR www\n"" dzonegit.compile_zone(""example.com"", testzone, missing_dot=False) with pytest.raises(ValueError): dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) def test_compile_unsmudged_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( $UNIXTIME ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" replaced = dzonegit.unixtime_directive(testzone) assert b""$UNIXTIME"" not in replaced r = dzonegit.compile_zone(""example.com"", testzone, 123456) assert r.success assert r.serial == str(123456) def test_smudge_serial(): bstdin = BytesIO(b""something $UNIXTIME something"") bstdout = BytesIO() dzonegit.smudge_serial(bstdin, bstdout, 123456) assert b""something 123456 something"" == bstdout.getvalue() def test_is_serial_increased(): assert dzonegit.is_serial_increased(1234567890, ""2018010100"") assert dzonegit.is_serial_increased(""2018010100"", ""4018010100"") assert dzonegit.is_serial_increased(""4018010100"", ""1234567890"") assert not dzonegit.is_serial_increased(2018010100, ""1234567890"") assert not dzonegit.is_serial_increased(1, 1) def test_get_altered_files(git_dir): git_dir.chdir() git_dir.join(""dummy"").write(""dummy2\n"") git_dir.join(""new"").write(""newfile\n"") subprocess.call([""git"", ""add"", ""dummy"", ""new""]) files = set(dzonegit.get_altered_files(""HEAD"", ""AM"")) assert files == {Path(""dummy""), Path(""new"")} # Refers to test_check_whitespace_errors files = set(dzonegit.get_altered_files(""HEAD~"", ""D"", ""HEAD"")) assert files == {Path(""whitespace"")} subprocess.call([""git"", ""checkout"", ""-f"", ""HEAD""]) assert set(dzonegit.get_altered_files(""HEAD"", ""AM"")) == set() def test_get_zone_origin(): testzone = b"""""" $ORIGIN examPle.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 $ORIGIN sub $ORIGIN subsub.example.com. $ORIGIN example.com. """""" assert ""example.com"" == dzonegit.get_zone_origin(testzone) testzone = b"""""" @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns $ORIGIN example.com. ns.example.com. 60 IN A 192.0.2.1 """""" assert dzonegit.get_zone_origin(testzone) is None def test_get_zone_name(): testzone = b"""""" $ORIGIN eXample.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", """", ) assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", testzone, ) with pytest.raises(ValueError): dzonegit.get_zone_name(""zones/example.org.zone"", testzone) subprocess.call([""git"", ""config"", ""dzonegit.allowfancynames"", ""TRUE""]) dzonegit.get_zone_name(""zones/example.org.zone"", testzone) testzone = b"""""" $ORIGIN 240/28.2.0.192.in-addr.arpa. @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns 60 IN A 192.0.2.1 """""" assert ""240/28.2.0.192.in-addr.arpa"" == dzonegit.get_zone_name( ""zones/240-28.2.0.192.in-addr.arpa.zone"", testzone, ) def test_replace_serial(git_dir): git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""1"", ""60"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 60 61 60 60 60 60 NS ns.example.org. """""" assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""61"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 61 61 60 60 60 60 NS ns.example.org. """""" git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm ( 60 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""6000000"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm ( 6000000 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""" assert not dzonegit.replace_serial(Path(""dummy.zone""), ""0"", ""60"") def test_check_updated_zones(git_dir): git_dir.chdir() git_dir.join(""dummy.zone"").write("""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""empty dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.com. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""updated dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" $ORIGIN other. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD"", autoupdate_serial=True) subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone with $UNIXTIME""]) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""final dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") def test_get_increased_serial(): ", https://github.com/oskar456/dzonegit,8f952086aa41aee4ebb7432926ed8c6a9211ac9f,test_dzonegit.py::test_get_file_contents,test_dzonegit.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause," import pytest import subprocess import time import datetime import os from io import StringIO, BytesIO from pathlib import Path import dzonegit @pytest.fixture(scope=""session"") def git_dir(tmpdir_factory): d = tmpdir_factory.getbasetemp() d.chdir() subprocess.call([""git"", ""init""]) subprocess.call([""git"", ""config"", ""user.name"", ""dzonegit pytest""]) subprocess.call([""git"", ""config"", ""user.email"", ""nonexistent@example.com""]) return d def test_get_head(git_dir): git_dir.chdir() assert dzonegit.get_head() == ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" git_dir.join(""dummy"").write(""dummy\n"") subprocess.call([""git"", ""add"", ""dummy""]) subprocess.call([""git"", ""commit"", ""-m"", ""dummy""]) assert dzonegit.get_head() != ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" def test_check_whitespace_errors(git_dir): git_dir.chdir() git_dir.join(""whitespace"").write("" "") subprocess.call([""git"", ""add"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) subprocess.call([""git"", ""rm"", ""-f"", ""whitespace""]) subprocess.call([""git"", ""commit"", ""-m"", ""rm whitespace""]) dzonegit.check_whitespace_errors(dzonegit.get_head()) dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) def test_get_file_contents(git_dir): git_dir.chdir() assert dzonegit.get_file_contents(""dummy"") == b""dummy\n"" with pytest.raises(subprocess.CalledProcessError): dzonegit.get_file_contents('nonexistent') def test_compile_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( 1234567890 ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" r = dzonegit.compile_zone(""example.org"", testzone, missing_dot=True) assert not r.success assert r.zonehash is None assert r.stderr r = dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) assert r.success assert r.serial == ""1234567890"" assert r.zonehash r2 = dzonegit.compile_zone(""example.com"", testzone + b""\n\n; some comment"") assert r.zonehash == r2.zonehash testzone += b""1 60 IN PTR www\n"" dzonegit.compile_zone(""example.com"", testzone, missing_dot=False) with pytest.raises(ValueError): dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) def test_compile_unsmudged_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( $UNIXTIME ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" replaced = dzonegit.unixtime_directive(testzone) assert b""$UNIXTIME"" not in replaced r = dzonegit.compile_zone(""example.com"", testzone, 123456) assert r.success assert r.serial == str(123456) def test_smudge_serial(): bstdin = BytesIO(b""something $UNIXTIME something"") bstdout = BytesIO() dzonegit.smudge_serial(bstdin, bstdout, 123456) assert b""something 123456 something"" == bstdout.getvalue() def test_is_serial_increased(): assert dzonegit.is_serial_increased(1234567890, ""2018010100"") assert dzonegit.is_serial_increased(""2018010100"", ""4018010100"") assert dzonegit.is_serial_increased(""4018010100"", ""1234567890"") assert not dzonegit.is_serial_increased(2018010100, ""1234567890"") assert not dzonegit.is_serial_increased(1, 1) def test_get_altered_files(git_dir): git_dir.chdir() git_dir.join(""dummy"").write(""dummy2\n"") git_dir.join(""new"").write(""newfile\n"") subprocess.call([""git"", ""add"", ""dummy"", ""new""]) files = set(dzonegit.get_altered_files(""HEAD"", ""AM"")) assert files == {Path(""dummy""), Path(""new"")} # Refers to test_check_whitespace_errors files = set(dzonegit.get_altered_files(""HEAD~"", ""D"", ""HEAD"")) assert files == {Path(""whitespace"")} subprocess.call([""git"", ""checkout"", ""-f"", ""HEAD""]) assert set(dzonegit.get_altered_files(""HEAD"", ""AM"")) == set() def test_get_zone_origin(): testzone = b"""""" $ORIGIN examPle.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 $ORIGIN sub $ORIGIN subsub.example.com. $ORIGIN example.com. """""" assert ""example.com"" == dzonegit.get_zone_origin(testzone) testzone = b"""""" @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns $ORIGIN example.com. ns.example.com. 60 IN A 192.0.2.1 """""" assert dzonegit.get_zone_origin(testzone) is None def test_get_zone_name(): testzone = b"""""" $ORIGIN eXample.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", """", ) assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", testzone, ) with pytest.raises(ValueError): dzonegit.get_zone_name(""zones/example.org.zone"", testzone) subprocess.call([""git"", ""config"", ""dzonegit.allowfancynames"", ""TRUE""]) dzonegit.get_zone_name(""zones/example.org.zone"", testzone) testzone = b"""""" $ORIGIN 240/28.2.0.192.in-addr.arpa. @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns 60 IN A 192.0.2.1 """""" assert ""240/28.2.0.192.in-addr.arpa"" == dzonegit.get_zone_name( ""zones/240-28.2.0.192.in-addr.arpa.zone"", testzone, ) def test_replace_serial(git_dir): git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""1"", ""60"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 60 61 60 60 60 60 NS ns.example.org. """""" assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""61"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 61 61 60 60 60 60 NS ns.example.org. """""" git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm ( 60 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""6000000"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm ( 6000000 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""" assert not dzonegit.replace_serial(Path(""dummy.zone""), ""0"", ""60"") def test_check_updated_zones(git_dir): git_dir.chdir() git_dir.join(""dummy.zone"").write("""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""empty dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.com. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""updated dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" $ORIGIN other. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD"", autoupdate_serial=True) subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone with $UNIXTIME""]) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""final dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") def test_get_increased_serial(): ", https://github.com/oskar456/dzonegit,8f952086aa41aee4ebb7432926ed8c6a9211ac9f,test_dzonegit.py::test_get_head,test_dzonegit.py,NIO,flaky,Accepted,https://github.com/oskar456/dzonegit/pull/19,classify;root_cause;fix_proposal," import pytest import subprocess import time import datetime import os from io import StringIO, BytesIO from pathlib import Path import dzonegit @pytest.fixture(scope=""session"") def git_dir(tmpdir_factory): d = tmpdir_factory.getbasetemp() d.chdir() subprocess.call([""git"", ""init""]) subprocess.call([""git"", ""config"", ""user.name"", ""dzonegit pytest""]) subprocess.call([""git"", ""config"", ""user.email"", ""nonexistent@example.com""]) return d def test_get_head(git_dir): git_dir.chdir() assert dzonegit.get_head() == ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" git_dir.join(""dummy"").write(""dummy\n"") subprocess.call([""git"", ""add"", ""dummy""]) subprocess.call([""git"", ""commit"", ""-m"", ""dummy""]) assert dzonegit.get_head() != ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" def test_check_whitespace_errors(git_dir): git_dir.chdir() git_dir.join(""whitespace"").write("" "") subprocess.call([""git"", ""add"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) subprocess.call([""git"", ""rm"", ""-f"", ""whitespace""]) subprocess.call([""git"", ""commit"", ""-m"", ""rm whitespace""]) dzonegit.check_whitespace_errors(dzonegit.get_head()) dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) def test_get_file_contents(git_dir): git_dir.chdir() assert dzonegit.get_file_contents(""dummy"") == b""dummy\n"" with pytest.raises(subprocess.CalledProcessError): dzonegit.get_file_contents('nonexistent') def test_compile_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( 1234567890 ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" r = dzonegit.compile_zone(""example.org"", testzone, missing_dot=True) assert not r.success assert r.zonehash is None assert r.stderr r = dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) assert r.success assert r.serial == ""1234567890"" assert r.zonehash r2 = dzonegit.compile_zone(""example.com"", testzone + b""\n\n; some comment"") assert r.zonehash == r2.zonehash testzone += b""1 60 IN PTR www\n"" dzonegit.compile_zone(""example.com"", testzone, missing_dot=False) with pytest.raises(ValueError): dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) def test_compile_unsmudged_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( $UNIXTIME ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" replaced = dzonegit.unixtime_directive(testzone) assert b""$UNIXTIME"" not in replaced r = dzonegit.compile_zone(""example.com"", testzone, 123456) assert r.success assert r.serial == str(123456) def test_smudge_serial(): bstdin = BytesIO(b""something $UNIXTIME something"") bstdout = BytesIO() dzonegit.smudge_serial(bstdin, bstdout, 123456) assert b""something 123456 something"" == bstdout.getvalue() def test_is_serial_increased(): assert dzonegit.is_serial_increased(1234567890, ""2018010100"") assert dzonegit.is_serial_increased(""2018010100"", ""4018010100"") assert dzonegit.is_serial_increased(""4018010100"", ""1234567890"") assert not dzonegit.is_serial_increased(2018010100, ""1234567890"") assert not dzonegit.is_serial_increased(1, 1) def test_get_altered_files(git_dir): git_dir.chdir() git_dir.join(""dummy"").write(""dummy2\n"") git_dir.join(""new"").write(""newfile\n"") subprocess.call([""git"", ""add"", ""dummy"", ""new""]) files = set(dzonegit.get_altered_files(""HEAD"", ""AM"")) assert files == {Path(""dummy""), Path(""new"")} # Refers to test_check_whitespace_errors files = set(dzonegit.get_altered_files(""HEAD~"", ""D"", ""HEAD"")) assert files == {Path(""whitespace"")} subprocess.call([""git"", ""checkout"", ""-f"", ""HEAD""]) assert set(dzonegit.get_altered_files(""HEAD"", ""AM"")) == set() def test_get_zone_origin(): testzone = b"""""" $ORIGIN examPle.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 $ORIGIN sub $ORIGIN subsub.example.com. $ORIGIN example.com. """""" assert ""example.com"" == dzonegit.get_zone_origin(testzone) testzone = b"""""" @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns $ORIGIN example.com. ns.example.com. 60 IN A 192.0.2.1 """""" assert dzonegit.get_zone_origin(testzone) is None def test_get_zone_name(): testzone = b"""""" $ORIGIN eXample.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", """", ) assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", testzone, ) with pytest.raises(ValueError): dzonegit.get_zone_name(""zones/example.org.zone"", testzone) subprocess.call([""git"", ""config"", ""dzonegit.allowfancynames"", ""TRUE""]) dzonegit.get_zone_name(""zones/example.org.zone"", testzone) testzone = b"""""" $ORIGIN 240/28.2.0.192.in-addr.arpa. @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns 60 IN A 192.0.2.1 """""" assert ""240/28.2.0.192.in-addr.arpa"" == dzonegit.get_zone_name( ""zones/240-28.2.0.192.in-addr.arpa.zone"", testzone, ) def test_replace_serial(git_dir): git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""1"", ""60"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 60 61 60 60 60 60 NS ns.example.org. """""" assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""61"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 61 61 60 60 60 60 NS ns.example.org. """""" git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm ( 60 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""6000000"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm ( 6000000 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""" assert not dzonegit.replace_serial(Path(""dummy.zone""), ""0"", ""60"") def test_check_updated_zones(git_dir): git_dir.chdir() git_dir.join(""dummy.zone"").write("""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""empty dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.com. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""updated dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" $ORIGIN other. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD"", autoupdate_serial=True) subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone with $UNIXTIME""]) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""final dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") def test_get_increased_serial(): ", https://github.com/oskar456/dzonegit,8f952086aa41aee4ebb7432926ed8c6a9211ac9f,test_dzonegit.py::test_post_receive,test_dzonegit.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause," import pytest import subprocess import time import datetime import os from io import StringIO, BytesIO from pathlib import Path import dzonegit @pytest.fixture(scope=""session"") def git_dir(tmpdir_factory): d = tmpdir_factory.getbasetemp() d.chdir() subprocess.call([""git"", ""init""]) subprocess.call([""git"", ""config"", ""user.name"", ""dzonegit pytest""]) subprocess.call([""git"", ""config"", ""user.email"", ""nonexistent@example.com""]) return d def test_get_head(git_dir): git_dir.chdir() assert dzonegit.get_head() == ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" git_dir.join(""dummy"").write(""dummy\n"") subprocess.call([""git"", ""add"", ""dummy""]) subprocess.call([""git"", ""commit"", ""-m"", ""dummy""]) assert dzonegit.get_head() != ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" def test_check_whitespace_errors(git_dir): git_dir.chdir() git_dir.join(""whitespace"").write("" "") subprocess.call([""git"", ""add"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) subprocess.call([""git"", ""rm"", ""-f"", ""whitespace""]) subprocess.call([""git"", ""commit"", ""-m"", ""rm whitespace""]) dzonegit.check_whitespace_errors(dzonegit.get_head()) dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) def test_get_file_contents(git_dir): git_dir.chdir() assert dzonegit.get_file_contents(""dummy"") == b""dummy\n"" with pytest.raises(subprocess.CalledProcessError): dzonegit.get_file_contents('nonexistent') def test_compile_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( 1234567890 ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" r = dzonegit.compile_zone(""example.org"", testzone, missing_dot=True) assert not r.success assert r.zonehash is None assert r.stderr r = dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) assert r.success assert r.serial == ""1234567890"" assert r.zonehash r2 = dzonegit.compile_zone(""example.com"", testzone + b""\n\n; some comment"") assert r.zonehash == r2.zonehash testzone += b""1 60 IN PTR www\n"" dzonegit.compile_zone(""example.com"", testzone, missing_dot=False) with pytest.raises(ValueError): dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) def test_compile_unsmudged_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( $UNIXTIME ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" replaced = dzonegit.unixtime_directive(testzone) assert b""$UNIXTIME"" not in replaced r = dzonegit.compile_zone(""example.com"", testzone, 123456) assert r.success assert r.serial == str(123456) def test_smudge_serial(): bstdin = BytesIO(b""something $UNIXTIME something"") bstdout = BytesIO() dzonegit.smudge_serial(bstdin, bstdout, 123456) assert b""something 123456 something"" == bstdout.getvalue() def test_is_serial_increased(): assert dzonegit.is_serial_increased(1234567890, ""2018010100"") assert dzonegit.is_serial_increased(""2018010100"", ""4018010100"") assert dzonegit.is_serial_increased(""4018010100"", ""1234567890"") assert not dzonegit.is_serial_increased(2018010100, ""1234567890"") assert not dzonegit.is_serial_increased(1, 1) def test_get_altered_files(git_dir): git_dir.chdir() git_dir.join(""dummy"").write(""dummy2\n"") git_dir.join(""new"").write(""newfile\n"") subprocess.call([""git"", ""add"", ""dummy"", ""new""]) files = set(dzonegit.get_altered_files(""HEAD"", ""AM"")) assert files == {Path(""dummy""), Path(""new"")} # Refers to test_check_whitespace_errors files = set(dzonegit.get_altered_files(""HEAD~"", ""D"", ""HEAD"")) assert files == {Path(""whitespace"")} subprocess.call([""git"", ""checkout"", ""-f"", ""HEAD""]) assert set(dzonegit.get_altered_files(""HEAD"", ""AM"")) == set() def test_get_zone_origin(): testzone = b"""""" $ORIGIN examPle.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 $ORIGIN sub $ORIGIN subsub.example.com. $ORIGIN example.com. """""" assert ""example.com"" == dzonegit.get_zone_origin(testzone) testzone = b"""""" @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns $ORIGIN example.com. ns.example.com. 60 IN A 192.0.2.1 """""" assert dzonegit.get_zone_origin(testzone) is None def test_get_zone_name(): testzone = b"""""" $ORIGIN eXample.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", """", ) assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", testzone, ) with pytest.raises(ValueError): dzonegit.get_zone_name(""zones/example.org.zone"", testzone) subprocess.call([""git"", ""config"", ""dzonegit.allowfancynames"", ""TRUE""]) dzonegit.get_zone_name(""zones/example.org.zone"", testzone) testzone = b"""""" $ORIGIN 240/28.2.0.192.in-addr.arpa. @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns 60 IN A 192.0.2.1 """""" assert ""240/28.2.0.192.in-addr.arpa"" == dzonegit.get_zone_name( ""zones/240-28.2.0.192.in-addr.arpa.zone"", testzone, ) def test_replace_serial(git_dir): git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""1"", ""60"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 60 61 60 60 60 60 NS ns.example.org. """""" assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""61"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 61 61 60 60 60 60 NS ns.example.org. """""" git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm ( 60 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""6000000"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm ( 6000000 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""" assert not dzonegit.replace_serial(Path(""dummy.zone""), ""0"", ""60"") def test_check_updated_zones(git_dir): git_dir.chdir() git_dir.join(""dummy.zone"").write("""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""empty dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.com. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""updated dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" $ORIGIN other. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD"", autoupdate_serial=True) subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone with $UNIXTIME""]) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""final dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") def test_get_increased_serial(): ", https://github.com/oskar456/dzonegit,8f952086aa41aee4ebb7432926ed8c6a9211ac9f,test_dzonegit.py::test_pre_receive,test_dzonegit.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause," import pytest import subprocess import time import datetime import os from io import StringIO, BytesIO from pathlib import Path import dzonegit @pytest.fixture(scope=""session"") def git_dir(tmpdir_factory): d = tmpdir_factory.getbasetemp() d.chdir() subprocess.call([""git"", ""init""]) subprocess.call([""git"", ""config"", ""user.name"", ""dzonegit pytest""]) subprocess.call([""git"", ""config"", ""user.email"", ""nonexistent@example.com""]) return d def test_get_head(git_dir): git_dir.chdir() assert dzonegit.get_head() == ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" git_dir.join(""dummy"").write(""dummy\n"") subprocess.call([""git"", ""add"", ""dummy""]) subprocess.call([""git"", ""commit"", ""-m"", ""dummy""]) assert dzonegit.get_head() != ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" def test_check_whitespace_errors(git_dir): git_dir.chdir() git_dir.join(""whitespace"").write("" "") subprocess.call([""git"", ""add"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) subprocess.call([""git"", ""rm"", ""-f"", ""whitespace""]) subprocess.call([""git"", ""commit"", ""-m"", ""rm whitespace""]) dzonegit.check_whitespace_errors(dzonegit.get_head()) dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) def test_get_file_contents(git_dir): git_dir.chdir() assert dzonegit.get_file_contents(""dummy"") == b""dummy\n"" with pytest.raises(subprocess.CalledProcessError): dzonegit.get_file_contents('nonexistent') def test_compile_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( 1234567890 ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" r = dzonegit.compile_zone(""example.org"", testzone, missing_dot=True) assert not r.success assert r.zonehash is None assert r.stderr r = dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) assert r.success assert r.serial == ""1234567890"" assert r.zonehash r2 = dzonegit.compile_zone(""example.com"", testzone + b""\n\n; some comment"") assert r.zonehash == r2.zonehash testzone += b""1 60 IN PTR www\n"" dzonegit.compile_zone(""example.com"", testzone, missing_dot=False) with pytest.raises(ValueError): dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) def test_compile_unsmudged_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( $UNIXTIME ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" replaced = dzonegit.unixtime_directive(testzone) assert b""$UNIXTIME"" not in replaced r = dzonegit.compile_zone(""example.com"", testzone, 123456) assert r.success assert r.serial == str(123456) def test_smudge_serial(): bstdin = BytesIO(b""something $UNIXTIME something"") bstdout = BytesIO() dzonegit.smudge_serial(bstdin, bstdout, 123456) assert b""something 123456 something"" == bstdout.getvalue() def test_is_serial_increased(): assert dzonegit.is_serial_increased(1234567890, ""2018010100"") assert dzonegit.is_serial_increased(""2018010100"", ""4018010100"") assert dzonegit.is_serial_increased(""4018010100"", ""1234567890"") assert not dzonegit.is_serial_increased(2018010100, ""1234567890"") assert not dzonegit.is_serial_increased(1, 1) def test_get_altered_files(git_dir): git_dir.chdir() git_dir.join(""dummy"").write(""dummy2\n"") git_dir.join(""new"").write(""newfile\n"") subprocess.call([""git"", ""add"", ""dummy"", ""new""]) files = set(dzonegit.get_altered_files(""HEAD"", ""AM"")) assert files == {Path(""dummy""), Path(""new"")} # Refers to test_check_whitespace_errors files = set(dzonegit.get_altered_files(""HEAD~"", ""D"", ""HEAD"")) assert files == {Path(""whitespace"")} subprocess.call([""git"", ""checkout"", ""-f"", ""HEAD""]) assert set(dzonegit.get_altered_files(""HEAD"", ""AM"")) == set() def test_get_zone_origin(): testzone = b"""""" $ORIGIN examPle.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 $ORIGIN sub $ORIGIN subsub.example.com. $ORIGIN example.com. """""" assert ""example.com"" == dzonegit.get_zone_origin(testzone) testzone = b"""""" @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns $ORIGIN example.com. ns.example.com. 60 IN A 192.0.2.1 """""" assert dzonegit.get_zone_origin(testzone) is None def test_get_zone_name(): testzone = b"""""" $ORIGIN eXample.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", """", ) assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", testzone, ) with pytest.raises(ValueError): dzonegit.get_zone_name(""zones/example.org.zone"", testzone) subprocess.call([""git"", ""config"", ""dzonegit.allowfancynames"", ""TRUE""]) dzonegit.get_zone_name(""zones/example.org.zone"", testzone) testzone = b"""""" $ORIGIN 240/28.2.0.192.in-addr.arpa. @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns 60 IN A 192.0.2.1 """""" assert ""240/28.2.0.192.in-addr.arpa"" == dzonegit.get_zone_name( ""zones/240-28.2.0.192.in-addr.arpa.zone"", testzone, ) def test_replace_serial(git_dir): git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""1"", ""60"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 60 61 60 60 60 60 NS ns.example.org. """""" assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""61"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 61 61 60 60 60 60 NS ns.example.org. """""" git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm ( 60 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""6000000"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm ( 6000000 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""" assert not dzonegit.replace_serial(Path(""dummy.zone""), ""0"", ""60"") def test_check_updated_zones(git_dir): git_dir.chdir() git_dir.join(""dummy.zone"").write("""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""empty dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.com. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""updated dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" $ORIGIN other. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD"", autoupdate_serial=True) subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone with $UNIXTIME""]) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""final dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") def test_get_increased_serial(): ", https://github.com/oskar456/dzonegit,8f952086aa41aee4ebb7432926ed8c6a9211ac9f,test_dzonegit.py::test_template_config,test_dzonegit.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause," import pytest import subprocess import time import datetime import os from io import StringIO, BytesIO from pathlib import Path import dzonegit @pytest.fixture(scope=""session"") def git_dir(tmpdir_factory): d = tmpdir_factory.getbasetemp() d.chdir() subprocess.call([""git"", ""init""]) subprocess.call([""git"", ""config"", ""user.name"", ""dzonegit pytest""]) subprocess.call([""git"", ""config"", ""user.email"", ""nonexistent@example.com""]) return d def test_get_head(git_dir): git_dir.chdir() assert dzonegit.get_head() == ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" git_dir.join(""dummy"").write(""dummy\n"") subprocess.call([""git"", ""add"", ""dummy""]) subprocess.call([""git"", ""commit"", ""-m"", ""dummy""]) assert dzonegit.get_head() != ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" def test_check_whitespace_errors(git_dir): git_dir.chdir() git_dir.join(""whitespace"").write("" "") subprocess.call([""git"", ""add"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) subprocess.call([""git"", ""rm"", ""-f"", ""whitespace""]) subprocess.call([""git"", ""commit"", ""-m"", ""rm whitespace""]) dzonegit.check_whitespace_errors(dzonegit.get_head()) dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) def test_get_file_contents(git_dir): git_dir.chdir() assert dzonegit.get_file_contents(""dummy"") == b""dummy\n"" with pytest.raises(subprocess.CalledProcessError): dzonegit.get_file_contents('nonexistent') def test_compile_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( 1234567890 ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" r = dzonegit.compile_zone(""example.org"", testzone, missing_dot=True) assert not r.success assert r.zonehash is None assert r.stderr r = dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) assert r.success assert r.serial == ""1234567890"" assert r.zonehash r2 = dzonegit.compile_zone(""example.com"", testzone + b""\n\n; some comment"") assert r.zonehash == r2.zonehash testzone += b""1 60 IN PTR www\n"" dzonegit.compile_zone(""example.com"", testzone, missing_dot=False) with pytest.raises(ValueError): dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) def test_compile_unsmudged_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( $UNIXTIME ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" replaced = dzonegit.unixtime_directive(testzone) assert b""$UNIXTIME"" not in replaced r = dzonegit.compile_zone(""example.com"", testzone, 123456) assert r.success assert r.serial == str(123456) def test_smudge_serial(): bstdin = BytesIO(b""something $UNIXTIME something"") bstdout = BytesIO() dzonegit.smudge_serial(bstdin, bstdout, 123456) assert b""something 123456 something"" == bstdout.getvalue() def test_is_serial_increased(): assert dzonegit.is_serial_increased(1234567890, ""2018010100"") assert dzonegit.is_serial_increased(""2018010100"", ""4018010100"") assert dzonegit.is_serial_increased(""4018010100"", ""1234567890"") assert not dzonegit.is_serial_increased(2018010100, ""1234567890"") assert not dzonegit.is_serial_increased(1, 1) def test_get_altered_files(git_dir): git_dir.chdir() git_dir.join(""dummy"").write(""dummy2\n"") git_dir.join(""new"").write(""newfile\n"") subprocess.call([""git"", ""add"", ""dummy"", ""new""]) files = set(dzonegit.get_altered_files(""HEAD"", ""AM"")) assert files == {Path(""dummy""), Path(""new"")} # Refers to test_check_whitespace_errors files = set(dzonegit.get_altered_files(""HEAD~"", ""D"", ""HEAD"")) assert files == {Path(""whitespace"")} subprocess.call([""git"", ""checkout"", ""-f"", ""HEAD""]) assert set(dzonegit.get_altered_files(""HEAD"", ""AM"")) == set() def test_get_zone_origin(): testzone = b"""""" $ORIGIN examPle.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 $ORIGIN sub $ORIGIN subsub.example.com. $ORIGIN example.com. """""" assert ""example.com"" == dzonegit.get_zone_origin(testzone) testzone = b"""""" @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns $ORIGIN example.com. ns.example.com. 60 IN A 192.0.2.1 """""" assert dzonegit.get_zone_origin(testzone) is None def test_get_zone_name(): testzone = b"""""" $ORIGIN eXample.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", """", ) assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", testzone, ) with pytest.raises(ValueError): dzonegit.get_zone_name(""zones/example.org.zone"", testzone) subprocess.call([""git"", ""config"", ""dzonegit.allowfancynames"", ""TRUE""]) dzonegit.get_zone_name(""zones/example.org.zone"", testzone) testzone = b"""""" $ORIGIN 240/28.2.0.192.in-addr.arpa. @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns 60 IN A 192.0.2.1 """""" assert ""240/28.2.0.192.in-addr.arpa"" == dzonegit.get_zone_name( ""zones/240-28.2.0.192.in-addr.arpa.zone"", testzone, ) def test_replace_serial(git_dir): git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""1"", ""60"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 60 61 60 60 60 60 NS ns.example.org. """""" assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""61"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 61 61 60 60 60 60 NS ns.example.org. """""" git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm ( 60 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""6000000"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm ( 6000000 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""" assert not dzonegit.replace_serial(Path(""dummy.zone""), ""0"", ""60"") def test_check_updated_zones(git_dir): git_dir.chdir() git_dir.join(""dummy.zone"").write("""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""empty dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.com. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""updated dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" $ORIGIN other. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD"", autoupdate_serial=True) subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone with $UNIXTIME""]) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""final dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") def test_get_increased_serial(): ", https://github.com/oskar456/dzonegit,8f952086aa41aee4ebb7432926ed8c6a9211ac9f,test_dzonegit.py::test_update,test_dzonegit.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause," import pytest import subprocess import time import datetime import os from io import StringIO, BytesIO from pathlib import Path import dzonegit @pytest.fixture(scope=""session"") def git_dir(tmpdir_factory): d = tmpdir_factory.getbasetemp() d.chdir() subprocess.call([""git"", ""init""]) subprocess.call([""git"", ""config"", ""user.name"", ""dzonegit pytest""]) subprocess.call([""git"", ""config"", ""user.email"", ""nonexistent@example.com""]) return d def test_get_head(git_dir): git_dir.chdir() assert dzonegit.get_head() == ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" git_dir.join(""dummy"").write(""dummy\n"") subprocess.call([""git"", ""add"", ""dummy""]) subprocess.call([""git"", ""commit"", ""-m"", ""dummy""]) assert dzonegit.get_head() != ""4b825dc642cb6eb9a060e54bf8d69288fbee4904"" def test_check_whitespace_errors(git_dir): git_dir.chdir() git_dir.join(""whitespace"").write("" "") subprocess.call([""git"", ""add"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""whitespace""]) with pytest.raises(ValueError): dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) subprocess.call([""git"", ""rm"", ""-f"", ""whitespace""]) subprocess.call([""git"", ""commit"", ""-m"", ""rm whitespace""]) dzonegit.check_whitespace_errors(dzonegit.get_head()) dzonegit.check_whitespace_errors(""HEAD~"", dzonegit.get_head()) def test_get_file_contents(git_dir): git_dir.chdir() assert dzonegit.get_file_contents(""dummy"") == b""dummy\n"" with pytest.raises(subprocess.CalledProcessError): dzonegit.get_file_contents('nonexistent') def test_compile_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( 1234567890 ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" r = dzonegit.compile_zone(""example.org"", testzone, missing_dot=True) assert not r.success assert r.zonehash is None assert r.stderr r = dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) assert r.success assert r.serial == ""1234567890"" assert r.zonehash r2 = dzonegit.compile_zone(""example.com"", testzone + b""\n\n; some comment"") assert r.zonehash == r2.zonehash testzone += b""1 60 IN PTR www\n"" dzonegit.compile_zone(""example.com"", testzone, missing_dot=False) with pytest.raises(ValueError): dzonegit.compile_zone(""example.com"", testzone, missing_dot=True) def test_compile_unsmudged_zone(): testzone = b"""""" $ORIGIN example.com. @ 60 IN SOA ns hostmaster ( $UNIXTIME ; serial 3600 ; refresh (1 hour) 900 ; retry (15 minutes) 1814400 ; expire (3 weeks) 60 ; minimum (1 minute) ) 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" replaced = dzonegit.unixtime_directive(testzone) assert b""$UNIXTIME"" not in replaced r = dzonegit.compile_zone(""example.com"", testzone, 123456) assert r.success assert r.serial == str(123456) def test_smudge_serial(): bstdin = BytesIO(b""something $UNIXTIME something"") bstdout = BytesIO() dzonegit.smudge_serial(bstdin, bstdout, 123456) assert b""something 123456 something"" == bstdout.getvalue() def test_is_serial_increased(): assert dzonegit.is_serial_increased(1234567890, ""2018010100"") assert dzonegit.is_serial_increased(""2018010100"", ""4018010100"") assert dzonegit.is_serial_increased(""4018010100"", ""1234567890"") assert not dzonegit.is_serial_increased(2018010100, ""1234567890"") assert not dzonegit.is_serial_increased(1, 1) def test_get_altered_files(git_dir): git_dir.chdir() git_dir.join(""dummy"").write(""dummy2\n"") git_dir.join(""new"").write(""newfile\n"") subprocess.call([""git"", ""add"", ""dummy"", ""new""]) files = set(dzonegit.get_altered_files(""HEAD"", ""AM"")) assert files == {Path(""dummy""), Path(""new"")} # Refers to test_check_whitespace_errors files = set(dzonegit.get_altered_files(""HEAD~"", ""D"", ""HEAD"")) assert files == {Path(""whitespace"")} subprocess.call([""git"", ""checkout"", ""-f"", ""HEAD""]) assert set(dzonegit.get_altered_files(""HEAD"", ""AM"")) == set() def test_get_zone_origin(): testzone = b"""""" $ORIGIN examPle.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 $ORIGIN sub $ORIGIN subsub.example.com. $ORIGIN example.com. """""" assert ""example.com"" == dzonegit.get_zone_origin(testzone) testzone = b"""""" @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns $ORIGIN example.com. ns.example.com. 60 IN A 192.0.2.1 """""" assert dzonegit.get_zone_origin(testzone) is None def test_get_zone_name(): testzone = b"""""" $ORIGIN eXample.com. ;coment @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns.example.com. 60 IN A 192.0.2.1 """""" assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", """", ) assert ""example.com"" == dzonegit.get_zone_name( ""zones/example.com.zone"", testzone, ) with pytest.raises(ValueError): dzonegit.get_zone_name(""zones/example.org.zone"", testzone) subprocess.call([""git"", ""config"", ""dzonegit.allowfancynames"", ""TRUE""]) dzonegit.get_zone_name(""zones/example.org.zone"", testzone) testzone = b"""""" $ORIGIN 240/28.2.0.192.in-addr.arpa. @ 60 IN SOA ns hostmaster 1 60 60 60 60 60 IN NS ns ns 60 IN A 192.0.2.1 """""" assert ""240/28.2.0.192.in-addr.arpa"" == dzonegit.get_zone_name( ""zones/240-28.2.0.192.in-addr.arpa.zone"", testzone, ) def test_replace_serial(git_dir): git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""1"", ""60"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 60 61 60 60 60 60 NS ns.example.org. """""" assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""61"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm 61 61 60 60 60 60 NS ns.example.org. """""" git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm ( 60 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""") assert dzonegit.replace_serial(Path(""dummy.zone""), ""60"", ""6000000"") assert git_dir.join(""dummy.zone"").read() == """""" @ 60 IN SOA ns hm ( 6000000 ; serial 60 ; refresh 60 ; retry 60 ; expire 60 ; minimum ) 60 NS ns.example.org. """""" assert not dzonegit.replace_serial(Path(""dummy.zone""), ""0"", ""60"") def test_check_updated_zones(git_dir): git_dir.chdir() git_dir.join(""dummy.zone"").write("""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""empty dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.com. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""updated dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") git_dir.join(""dummy.zone"").write("""""" $ORIGIN other. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(""HEAD"", autoupdate_serial=True) subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 61 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""dummy.zone with $UNIXTIME""]) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm 1 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) with pytest.raises(ValueError): dzonegit.check_updated_zones(dzonegit.get_head()) git_dir.join(""dummy.zone"").write("""""" $ORIGIN dummy. @ 60 IN SOA ns hm $UNIXTIME 60 60 60 60 60 NS ns.example.org. """""") subprocess.call([""git"", ""add"", ""dummy.zone""]) dzonegit.check_updated_zones(dzonegit.get_head()) subprocess.call([""git"", ""commit"", ""-m"", ""final dummy.zone""]) dzonegit.check_updated_zones(""HEAD~"", ""HEAD"") def test_get_increased_serial(): ", https://github.com/ozzywalsh/metcli,4658176034cea68955849bf488494c8ce3ffcbcd,metcli/test_metcli.py::test_met_request_should_handle_connection_problem,metcli/test_metcli.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from unittest.mock import MagicMock import pytest import requests import requests_mock from . import metcli def test_met_request_should_return_dict(): session = requests.Session() adapter = requests_mock.Adapter() session.mount('mock', adapter) json_data = '{""name"": ""Bob""}' with requests_mock.mock() as m: m.get('https://www.met.ie/api/weather/national', text=json_data) result = metcli.met_request('weather/national') assert isinstance(result, dict) def test_met_request_should_handle_connection_problem(capsys): with requests_mock.Mocker() as m: m.get('https://www.met.ie/api/weather/national', exc=requests.exceptions.ConnectionError) with pytest.raises(SystemExit) as excinfo: metcli.met_request('weather/national') assert str(excinfo.value.code) == 'There was a problem connecting to Met Éireann.' def test_get_national(): metcli.met_request = MagicMock() metcli.get_national() url = metcli.met_request.call_args assert 'weather/national' in str(url) ", https://github.com/ozzywalsh/metcli,4658176034cea68955849bf488494c8ce3ffcbcd,metcli/test_metcli.py::test_met_request_should_return_dict,metcli/test_metcli.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from unittest.mock import MagicMock import pytest import requests import requests_mock from . import metcli def test_met_request_should_return_dict(): session = requests.Session() adapter = requests_mock.Adapter() session.mount('mock', adapter) json_data = '{""name"": ""Bob""}' with requests_mock.mock() as m: m.get('https://www.met.ie/api/weather/national', text=json_data) result = metcli.met_request('weather/national') assert isinstance(result, dict) def test_met_request_should_handle_connection_problem(capsys): with requests_mock.Mocker() as m: m.get('https://www.met.ie/api/weather/national', exc=requests.exceptions.ConnectionError) with pytest.raises(SystemExit) as excinfo: metcli.met_request('weather/national') assert str(excinfo.value.code) == 'There was a problem connecting to Met Éireann.' def test_get_national(): metcli.met_request = MagicMock() metcli.get_national() url = metcli.met_request.call_args assert 'weather/national' in str(url) ", https://github.com/Parsely/serpextract,aec8f2bd79c66953938569a24b05c693ab70b4ce,tests/test_serps.py::TestSERPs::test_custom_parser_explicit,tests/test_serps.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import unittest from six.moves.urllib.parse import urlparse from serpextract import (SearchEngineParser, extract, is_serp, get_all_query_params, add_custom_parser, get_all_query_params_by_domain) class TestSERPs(unittest.TestCase): """"""Most popular search engines to really worry about until we have full on test coverage are: - Google - Baidu - Yahoo - Yandex - Microsoft (Bing) We test these engines, and for each we ensure we try to cover the multiple country case and the keywords with crazy characters case. """""" def setUp(self): self.custom_serp_url = 'http://search.piccshare.com/search.php?cat=web&channel=main&hl=en&q=test' self.custom_parser = SearchEngineParser(u'PiccShare', u'q', u'/search.php?q={k}', u'utf-8') def assertInvalidSERP(self, url, **kwargs): self.assertIsNone(extract(url, **kwargs)) self.assertFalse(is_serp(url, **kwargs)) def assertValidSERP(self, url, expected_engine_name, expected_keyword, **kwargs): # Test both the URL and a parsed URL version for url in (url, urlparse(url)): res = extract(url, **kwargs) self.assertEqual(res.keyword, expected_keyword) self.assertEqual(res.engine_name, expected_engine_name) self.assertTrue(is_serp(url, **kwargs)) def assertValidSERPs(self, expected_serps, **kwargs): for url, engine_name, keyword in expected_serps: self.assertValidSERP(url, engine_name, keyword, **kwargs) def test_google(self): serps = ( ('http://www.google.com/url?sa=t&rct=j&q=hello&source=web&cd=1&ved=0CCoQFjAA&url=http%3A%2F%2Fwww.hellomagazine.ca%2F&ei=MDfSUe-JMob9ygGn24CoCw&usg=AFQjCNF6TQIo1aZe7WI8knqcdZax-lpg-A&bvm=bv.48572450,d.aWc', 'Google', u'hello'), ('http://www.google.co.uk/url?sa=t&rct=j&q=hello&source=web&cd=1&ved=0CDMQFjAA&url=http%3A%2F%2Fwww.hellomagazine.com%2F&ei=4TfSUdCLEY2_ywHXp4GADg&usg=AFQjCNE2TScP1sOG-TytWVe-kB0UUbWncg&bvm=bv.48572450,d.aWc', 'Google', u'hello'), ('http://www.google.co.in/url?sa=t&rct=j&q=%E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87&source=web&cd=1&ved=0CCwQFjAA&url=http%3A%2F%2Fhi.wikipedia.org%2Fwiki%2F%25E0%25A4%25A8%25E0%25A4%25AE%25E0%25A4%25B8%25E0%25A5%258D%25E0%25A4%25A4%25E0%25A5%2587&ei=eTfSUceOJ6i4yAGk6oGoDw&usg=AFQjCNGpYEO70ix-UtllM5iLl8Ywenwbpw&bvm=bv.48572450,d.aWc', 'Google', u'\u0928\u092e\u0938\u094d\u0924\u0947'), ('http://www.google.com.ua/url?sa=t&rct=j&q=%D0%BF%D1%80%D0%B8%D0%B2%D1%96%D1%82&source=web&cd=1&ved=0CC0QFjAA&url=http%3A%2F%2Fuk.wiktionary.org%2Fwiki%2F%25D0%25BF%25D1%2580%25D0%25B8%25D0%25B2%25D1%2596%25D1%2582&ei=dTjSUYnYJuSOyAG-rYDoCQ&usg=AFQjCNHloehQAu7UJ6upCqMVCDpNh3sNTg&bvm=bv.48572450,d.aWc', 'Google', u'\u043f\u0440\u0438\u0432\u0456\u0442'), ('http://www.google.ca/url?sa=i&rct=j&q=%22justin+timberlake%22&source=images&cd=&docid=mPgs-1UiH9v_-M&tbnid=Zkq86Kfj5IWYxM:&ved=0CAEQjxw&url=http%3A%2F%2Fwww.salon.com%2F2013%2F01%2F10%2Fis_justin_timberlake_releasing_a_new_album%2F&ei=ZuDmUaOlEaTk4AOE_4GgCQ&bvm=bv.49405654,d.dmg&psig=AFQjCNEX5ylDUbWb68msE2VIYZw-ObWtNg&ust=1374171617895913', 'Google', u'""justin timberlake""'), ('https://www.google.com/', 'Google', u''), ('https://www.google.co.uk', 'Google', u''), ('http://www.google.ca/search?hl=en&site=imghp&tbm=isch&source=hp&biw=1436&bih=508&q=lenovo&oq=lenovo&gs_l=img.3..0l10.2042.2539.0.2755.6.5.0.1.1.0.99.382.5.5.0....0.0..1ac.1.20.img.zuc4SkaG3pk#q=lenovo&hl=en&site=imghp&tbs=isz:l,qdr:d,itp:photo&tbm=isch&source=lnt&sa=X&ei=chbnUcwB88fgA9GdgdgD&ved=0CD0QpwUoAg&bav=on.2,or.r_qf.&bvm=bv.49405654%2Cd.dmg%2Cpv.xjs.s.en_US.QXiTEk6XjhM.O&fp=74e28ccdf351cc74&biw=1436&bih=508&facrc=_&imgdii=_&imgrc=PWcY9IoUsS8fqM%3A%3BLXHKDtPubm1b_M%3Bhttp%253A%252F%252Fmy.kyozou.com%252Fpictures%252F_15%252F14595%252F14594417.jpg%3Bhttp%253A%252F%252Fwww.ebay.com%252Fitm%252FLENOVO-X200-LAPTOP-CORE-2-DUO-1-86GHz-4GB-160GB-WIRELESS-%252F221252458890%253Fpt%253DLaptops_Nov05%2526hash%253Ditem3383ac998a%3B1600%3B1200', 'Google Images', u'lenovo'), ('https://www.google.it', 'Google', u''), ('https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&sqi=2&ved=0ahUKEwipjbfT-aPSAhUnCcAKHTbHALEQFgg7MAQ&url=https%3A%2F%2Fwww.thesun.co.uk%2Ftvandshowbiz%2F2735307%2Fantonio-banderas-rushed-to-surrey-hospital-after-suffering-agonising-chest-pains-during-workout%2F&usg=AFQjCNG4c9vrYPeffmVxckMAfRj51PMlpA&bvm=bv.147448319,d.ZGg', 'Google', u'') # TODO: More google edge cases ) self.assertValidSERPs(serps) def test_baidu(self): # TODO: More tests for Baidu serps = ( ('http://www.baidu.com/s?wd=%E4%BD%A0%E5%A5%BD&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_n=2&rsv_sug3=1&rsv_sug=0&rsv_sug1=1&rsv_sug4=352&inputT=1295', 'Baidu', u'\u4f60\u597d'), ) self.assertValidSERPs(serps) def test_yahoo(self): serps = ( ('http://ca.search.yahoo.com/search;_ylt=At9vKXZDJTDsQ6o7bDQPLBUt17V_;_ylc=X1MDMjE0MjYyMzUzMwRfcgMyBGZyA3lmcC10LTcxNQRuX2dwcwMxMARvcmlnaW4DY2EueWFob28uY29tBHF1ZXJ5A2hlbGxvBHNhbwMx?p=hello&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-715', 'Yahoo!', u'hello'), ('http://search.yahoo.com/search;_ylt=AnQcoCW29caK.8RLkGgSiqGbvZx4?p=united+states&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-900', 'Yahoo!', u'united states'), ('http://r.search.yahoo.com/_ylt=A0LEVy5UeJBUUzgAR2FXNyoA;_ylu=X3oDMTEzaGpsYTZuBHNlYwNzcgRwb3MDMgRjb2xvA2JmMQR2dGlkA1ZJUDU1OF8x/RV=2/RE=1418782933/RO=10/RU=http%3a%2f%2fen.wikipedia.org%2fwiki%2fToronto/RK=0/RS=cUOWJ12k59iqbScMA1r6sQedikc-', 'Yahoo!', u''), ('http://search.yahoo.co.jp/r/FOR=37DL17lV3iiMNf3sheF_mW3_Wj.3fsuxSYrKIfnPpLU9WfFZNrTrfvC9QoqcRuxcbIEmM8.KUZMstk6uS4bFjWiMtyznwPKVLc5D_jdUJEZfaGWVckvvflhOs2_IJ92LNv.BC70V4k5MjELmVo9hCIH.8oEkxxu2ItwfOqmPrTxpzKFGXYCaoRqASxIKkddp31_emIbJyjwM0Cxikt2KOCIa3BRhNPt3uM5q1I8sG5dEyh.2SUUIwrxSNaFjtr6fO6M6zDkqso.DcLnt6s1c9s6eBpzDiOSLFdLB2CPpUxyy6b6ZLemCC_6fbLnc7hsmYTE03FEs1zBy66gmxQv5U76_DkDzA2XaLqbD_U7JdCQ4e7XsyfXHXCcCQgSolYEj8U6cm_v7TJWFihGxbTC8o_crbF4Vuh36ueZOxueYss_Ng4kBlsTNaK4OEfNhjf.xjL.FZQQxRO.5b.mo05R78PP98pHrCJUy51uQg5X865ItMIodV262ZVR1aWdNzw0uE3_QTRk-/_ylt=A2Ri6BZGTQlZOEAAkUPPJ4pQ;_ylu=X3oDMTBuMjdjOXM3BHBvcwMxNwRzZWMDc3IEc2xrA3RpdGxl/SIG=18cti0pve/EXP=1493882630/**http%3A/www.swissinfo.ch/jpn/%25E7%25B1%25B3%25E8%25BB%258D-%25E5%25A4%25AA%25E5%25B9%25B3%25E6%25B4%258B%25E3%2581%25A7%25EF%25BD%2589%25EF%25BD%2583%25EF%25BD%2582%25EF%25BD%258D%25E5%2586%258D%25E5%25AE%259F%25E9%25A8%2593%25E3%2581%25B8/43149636', 'Yahoo! Japan', u''), ) self.assertValidSERPs(serps) def test_yandex(self): serps = ( ('http://www.yandex.com/yandsearch?text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82&lr=87', 'Yandex', u'\u043f\u0440\u0438\u0432\u0435\u0442'), ('http://yandex.ru/yandsearch?lr=10115&text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82', 'Yandex', u'\u043f\u0440\u0438\u0432\u0435\u0442'), ) self.assertValidSERPs(serps) def test_bing(self): serps = ( ('http://www.bing.com/search?q=united+states&go=&qs=n&form=QBLH&filt=all&pq=united+states&sc=8-13&sp=-1&sk=', 'Bing', u'united states'), ) self.assertValidSERPs(serps) def test_path_engines(self): """"""Tests for search engines that contain keywords within their paths and require regex extraction."""""" serps = ( ('http://www.123people.ca/s/michael+sukmanowsky', '123people', u'michael sukmanowsky'), ('http://www.1.cz/s/ars-technica/', '1.cz', u'ars-technica'), # These guys do not properly URL encode their keywords ) def test_custom_parser_explicit(self): self.assertInvalidSERP(self.custom_serp_url) self.assertValidSERP(self.custom_serp_url, self.custom_parser.engine_name, u'test', parser=self.custom_parser) def test_custom_parser_implicit(self): from serpextract.serpextract import _get_search_engines, _engines self.assertInvalidSERP(self.custom_serp_url) add_custom_parser(u'search.piccshare.com', self.custom_parser) self.assertValidSERP(self.custom_serp_url, self.custom_parser.engine_name, u'test') del _engines[u'search.piccshare.com'] def test_naive_detection(self): self.assertInvalidSERP(self.custom_serp_url) self.assertValidSERP(self.custom_serp_url, u'piccshare', u'test', use_naive_method=True) url = 'http://www.yahoo.com/#/%C2%BF??;%C2%AB99555$&&&4&' urlp = urlparse(url) self.assertInvalidSERP(urlparse(url), use_naive_method=True) self.assertInvalidSERP(url, use_naive_method=True) def test_get_all_query_params(self): """"""Ensure that get_all_query_params is a non-empty list."""""" params = get_all_query_params() self.assertIsInstance(params, list) self.assertGreater(len(params), 0) def test_get_query_params_by_domain(self): """""" make sure that individual subdomains are enumerated properly """""" params_by_domain = get_all_query_params_by_domain() google_params = [u'q', u'query'] bing_params = [u'Q', u'q'] baidu_params = [u'kw', u'wd', u'word'] yahoo_params = [u'p', u'q', u'va'] so_net_params = [u'kw', u'query'] goo_ne_jp_params = [u'MT'] t_online_params = [u'q'] self.assertEqual(params_by_domain['google.com'], google_params) self.assertEqual(params_by_domain['google.de'], google_params) self.assertEqual(params_by_domain['google.co.uk'], google_params) self.a", https://github.com/Parsely/serpextract,aec8f2bd79c66953938569a24b05c693ab70b4ce,tests/test_serps.py::TestSERPs::test_custom_parser_implicit,tests/test_serps.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import unittest from six.moves.urllib.parse import urlparse from serpextract import (SearchEngineParser, extract, is_serp, get_all_query_params, add_custom_parser, get_all_query_params_by_domain) class TestSERPs(unittest.TestCase): """"""Most popular search engines to really worry about until we have full on test coverage are: - Google - Baidu - Yahoo - Yandex - Microsoft (Bing) We test these engines, and for each we ensure we try to cover the multiple country case and the keywords with crazy characters case. """""" def setUp(self): self.custom_serp_url = 'http://search.piccshare.com/search.php?cat=web&channel=main&hl=en&q=test' self.custom_parser = SearchEngineParser(u'PiccShare', u'q', u'/search.php?q={k}', u'utf-8') def assertInvalidSERP(self, url, **kwargs): self.assertIsNone(extract(url, **kwargs)) self.assertFalse(is_serp(url, **kwargs)) def assertValidSERP(self, url, expected_engine_name, expected_keyword, **kwargs): # Test both the URL and a parsed URL version for url in (url, urlparse(url)): res = extract(url, **kwargs) self.assertEqual(res.keyword, expected_keyword) self.assertEqual(res.engine_name, expected_engine_name) self.assertTrue(is_serp(url, **kwargs)) def assertValidSERPs(self, expected_serps, **kwargs): for url, engine_name, keyword in expected_serps: self.assertValidSERP(url, engine_name, keyword, **kwargs) def test_google(self): serps = ( ('http://www.google.com/url?sa=t&rct=j&q=hello&source=web&cd=1&ved=0CCoQFjAA&url=http%3A%2F%2Fwww.hellomagazine.ca%2F&ei=MDfSUe-JMob9ygGn24CoCw&usg=AFQjCNF6TQIo1aZe7WI8knqcdZax-lpg-A&bvm=bv.48572450,d.aWc', 'Google', u'hello'), ('http://www.google.co.uk/url?sa=t&rct=j&q=hello&source=web&cd=1&ved=0CDMQFjAA&url=http%3A%2F%2Fwww.hellomagazine.com%2F&ei=4TfSUdCLEY2_ywHXp4GADg&usg=AFQjCNE2TScP1sOG-TytWVe-kB0UUbWncg&bvm=bv.48572450,d.aWc', 'Google', u'hello'), ('http://www.google.co.in/url?sa=t&rct=j&q=%E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87&source=web&cd=1&ved=0CCwQFjAA&url=http%3A%2F%2Fhi.wikipedia.org%2Fwiki%2F%25E0%25A4%25A8%25E0%25A4%25AE%25E0%25A4%25B8%25E0%25A5%258D%25E0%25A4%25A4%25E0%25A5%2587&ei=eTfSUceOJ6i4yAGk6oGoDw&usg=AFQjCNGpYEO70ix-UtllM5iLl8Ywenwbpw&bvm=bv.48572450,d.aWc', 'Google', u'\u0928\u092e\u0938\u094d\u0924\u0947'), ('http://www.google.com.ua/url?sa=t&rct=j&q=%D0%BF%D1%80%D0%B8%D0%B2%D1%96%D1%82&source=web&cd=1&ved=0CC0QFjAA&url=http%3A%2F%2Fuk.wiktionary.org%2Fwiki%2F%25D0%25BF%25D1%2580%25D0%25B8%25D0%25B2%25D1%2596%25D1%2582&ei=dTjSUYnYJuSOyAG-rYDoCQ&usg=AFQjCNHloehQAu7UJ6upCqMVCDpNh3sNTg&bvm=bv.48572450,d.aWc', 'Google', u'\u043f\u0440\u0438\u0432\u0456\u0442'), ('http://www.google.ca/url?sa=i&rct=j&q=%22justin+timberlake%22&source=images&cd=&docid=mPgs-1UiH9v_-M&tbnid=Zkq86Kfj5IWYxM:&ved=0CAEQjxw&url=http%3A%2F%2Fwww.salon.com%2F2013%2F01%2F10%2Fis_justin_timberlake_releasing_a_new_album%2F&ei=ZuDmUaOlEaTk4AOE_4GgCQ&bvm=bv.49405654,d.dmg&psig=AFQjCNEX5ylDUbWb68msE2VIYZw-ObWtNg&ust=1374171617895913', 'Google', u'""justin timberlake""'), ('https://www.google.com/', 'Google', u''), ('https://www.google.co.uk', 'Google', u''), ('http://www.google.ca/search?hl=en&site=imghp&tbm=isch&source=hp&biw=1436&bih=508&q=lenovo&oq=lenovo&gs_l=img.3..0l10.2042.2539.0.2755.6.5.0.1.1.0.99.382.5.5.0....0.0..1ac.1.20.img.zuc4SkaG3pk#q=lenovo&hl=en&site=imghp&tbs=isz:l,qdr:d,itp:photo&tbm=isch&source=lnt&sa=X&ei=chbnUcwB88fgA9GdgdgD&ved=0CD0QpwUoAg&bav=on.2,or.r_qf.&bvm=bv.49405654%2Cd.dmg%2Cpv.xjs.s.en_US.QXiTEk6XjhM.O&fp=74e28ccdf351cc74&biw=1436&bih=508&facrc=_&imgdii=_&imgrc=PWcY9IoUsS8fqM%3A%3BLXHKDtPubm1b_M%3Bhttp%253A%252F%252Fmy.kyozou.com%252Fpictures%252F_15%252F14595%252F14594417.jpg%3Bhttp%253A%252F%252Fwww.ebay.com%252Fitm%252FLENOVO-X200-LAPTOP-CORE-2-DUO-1-86GHz-4GB-160GB-WIRELESS-%252F221252458890%253Fpt%253DLaptops_Nov05%2526hash%253Ditem3383ac998a%3B1600%3B1200', 'Google Images', u'lenovo'), ('https://www.google.it', 'Google', u''), ('https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&sqi=2&ved=0ahUKEwipjbfT-aPSAhUnCcAKHTbHALEQFgg7MAQ&url=https%3A%2F%2Fwww.thesun.co.uk%2Ftvandshowbiz%2F2735307%2Fantonio-banderas-rushed-to-surrey-hospital-after-suffering-agonising-chest-pains-during-workout%2F&usg=AFQjCNG4c9vrYPeffmVxckMAfRj51PMlpA&bvm=bv.147448319,d.ZGg', 'Google', u'') # TODO: More google edge cases ) self.assertValidSERPs(serps) def test_baidu(self): # TODO: More tests for Baidu serps = ( ('http://www.baidu.com/s?wd=%E4%BD%A0%E5%A5%BD&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_n=2&rsv_sug3=1&rsv_sug=0&rsv_sug1=1&rsv_sug4=352&inputT=1295', 'Baidu', u'\u4f60\u597d'), ) self.assertValidSERPs(serps) def test_yahoo(self): serps = ( ('http://ca.search.yahoo.com/search;_ylt=At9vKXZDJTDsQ6o7bDQPLBUt17V_;_ylc=X1MDMjE0MjYyMzUzMwRfcgMyBGZyA3lmcC10LTcxNQRuX2dwcwMxMARvcmlnaW4DY2EueWFob28uY29tBHF1ZXJ5A2hlbGxvBHNhbwMx?p=hello&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-715', 'Yahoo!', u'hello'), ('http://search.yahoo.com/search;_ylt=AnQcoCW29caK.8RLkGgSiqGbvZx4?p=united+states&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-900', 'Yahoo!', u'united states'), ('http://r.search.yahoo.com/_ylt=A0LEVy5UeJBUUzgAR2FXNyoA;_ylu=X3oDMTEzaGpsYTZuBHNlYwNzcgRwb3MDMgRjb2xvA2JmMQR2dGlkA1ZJUDU1OF8x/RV=2/RE=1418782933/RO=10/RU=http%3a%2f%2fen.wikipedia.org%2fwiki%2fToronto/RK=0/RS=cUOWJ12k59iqbScMA1r6sQedikc-', 'Yahoo!', u''), ('http://search.yahoo.co.jp/r/FOR=37DL17lV3iiMNf3sheF_mW3_Wj.3fsuxSYrKIfnPpLU9WfFZNrTrfvC9QoqcRuxcbIEmM8.KUZMstk6uS4bFjWiMtyznwPKVLc5D_jdUJEZfaGWVckvvflhOs2_IJ92LNv.BC70V4k5MjELmVo9hCIH.8oEkxxu2ItwfOqmPrTxpzKFGXYCaoRqASxIKkddp31_emIbJyjwM0Cxikt2KOCIa3BRhNPt3uM5q1I8sG5dEyh.2SUUIwrxSNaFjtr6fO6M6zDkqso.DcLnt6s1c9s6eBpzDiOSLFdLB2CPpUxyy6b6ZLemCC_6fbLnc7hsmYTE03FEs1zBy66gmxQv5U76_DkDzA2XaLqbD_U7JdCQ4e7XsyfXHXCcCQgSolYEj8U6cm_v7TJWFihGxbTC8o_crbF4Vuh36ueZOxueYss_Ng4kBlsTNaK4OEfNhjf.xjL.FZQQxRO.5b.mo05R78PP98pHrCJUy51uQg5X865ItMIodV262ZVR1aWdNzw0uE3_QTRk-/_ylt=A2Ri6BZGTQlZOEAAkUPPJ4pQ;_ylu=X3oDMTBuMjdjOXM3BHBvcwMxNwRzZWMDc3IEc2xrA3RpdGxl/SIG=18cti0pve/EXP=1493882630/**http%3A/www.swissinfo.ch/jpn/%25E7%25B1%25B3%25E8%25BB%258D-%25E5%25A4%25AA%25E5%25B9%25B3%25E6%25B4%258B%25E3%2581%25A7%25EF%25BD%2589%25EF%25BD%2583%25EF%25BD%2582%25EF%25BD%258D%25E5%2586%258D%25E5%25AE%259F%25E9%25A8%2593%25E3%2581%25B8/43149636', 'Yahoo! Japan', u''), ) self.assertValidSERPs(serps) def test_yandex(self): serps = ( ('http://www.yandex.com/yandsearch?text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82&lr=87', 'Yandex', u'\u043f\u0440\u0438\u0432\u0435\u0442'), ('http://yandex.ru/yandsearch?lr=10115&text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82', 'Yandex', u'\u043f\u0440\u0438\u0432\u0435\u0442'), ) self.assertValidSERPs(serps) def test_bing(self): serps = ( ('http://www.bing.com/search?q=united+states&go=&qs=n&form=QBLH&filt=all&pq=united+states&sc=8-13&sp=-1&sk=', 'Bing', u'united states'), ) self.assertValidSERPs(serps) def test_path_engines(self): """"""Tests for search engines that contain keywords within their paths and require regex extraction."""""" serps = ( ('http://www.123people.ca/s/michael+sukmanowsky', '123people', u'michael sukmanowsky'), ('http://www.1.cz/s/ars-technica/', '1.cz', u'ars-technica'), # These guys do not properly URL encode their keywords ) def test_custom_parser_explicit(self): self.assertInvalidSERP(self.custom_serp_url) self.assertValidSERP(self.custom_serp_url, self.custom_parser.engine_name, u'test', parser=self.custom_parser) def test_custom_parser_implicit(self): from serpextract.serpextract import _get_search_engines, _engines self.assertInvalidSERP(self.custom_serp_url) add_custom_parser(u'search.piccshare.com', self.custom_parser) self.assertValidSERP(self.custom_serp_url, self.custom_parser.engine_name, u'test') del _engines[u'search.piccshare.com'] def test_naive_detection(self): self.assertInvalidSERP(self.custom_serp_url) self.assertValidSERP(self.custom_serp_url, u'piccshare', u'test', use_naive_method=True) url = 'http://www.yahoo.com/#/%C2%BF??;%C2%AB99555$&&&4&' urlp = urlparse(url) self.assertInvalidSERP(urlparse(url), use_naive_method=True) self.assertInvalidSERP(url, use_naive_method=True) def test_get_all_query_params(self): """"""Ensure that get_all_query_params is a non-empty list."""""" params = get_all_query_params() self.assertIsInstance(params, list) self.assertGreater(len(params), 0) def test_get_query_params_by_domain(self): """""" make sure that individual subdomains are enumerated properly """""" params_by_domain = get_all_query_params_by_domain() google_params = [u'q', u'query'] bing_params = [u'Q', u'q'] baidu_params = [u'kw', u'wd', u'word'] yahoo_params = [u'p', u'q', u'va'] so_net_params = [u'kw', u'query'] goo_ne_jp_params = [u'MT'] t_online_params = [u'q'] self.assertEqual(params_by_domain['google.com'], google_params) self.assertEqual(params_by_domain['google.de'], google_params) self.assertEqual(params_by_domain['google.co.uk'], google_params) self.a", https://github.com/Parsely/serpextract,aec8f2bd79c66953938569a24b05c693ab70b4ce,tests/test_serps.py::TestSERPs::test_naive_detection,tests/test_serps.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import unittest from six.moves.urllib.parse import urlparse from serpextract import (SearchEngineParser, extract, is_serp, get_all_query_params, add_custom_parser, get_all_query_params_by_domain) class TestSERPs(unittest.TestCase): """"""Most popular search engines to really worry about until we have full on test coverage are: - Google - Baidu - Yahoo - Yandex - Microsoft (Bing) We test these engines, and for each we ensure we try to cover the multiple country case and the keywords with crazy characters case. """""" def setUp(self): self.custom_serp_url = 'http://search.piccshare.com/search.php?cat=web&channel=main&hl=en&q=test' self.custom_parser = SearchEngineParser(u'PiccShare', u'q', u'/search.php?q={k}', u'utf-8') def assertInvalidSERP(self, url, **kwargs): self.assertIsNone(extract(url, **kwargs)) self.assertFalse(is_serp(url, **kwargs)) def assertValidSERP(self, url, expected_engine_name, expected_keyword, **kwargs): # Test both the URL and a parsed URL version for url in (url, urlparse(url)): res = extract(url, **kwargs) self.assertEqual(res.keyword, expected_keyword) self.assertEqual(res.engine_name, expected_engine_name) self.assertTrue(is_serp(url, **kwargs)) def assertValidSERPs(self, expected_serps, **kwargs): for url, engine_name, keyword in expected_serps: self.assertValidSERP(url, engine_name, keyword, **kwargs) def test_google(self): serps = ( ('http://www.google.com/url?sa=t&rct=j&q=hello&source=web&cd=1&ved=0CCoQFjAA&url=http%3A%2F%2Fwww.hellomagazine.ca%2F&ei=MDfSUe-JMob9ygGn24CoCw&usg=AFQjCNF6TQIo1aZe7WI8knqcdZax-lpg-A&bvm=bv.48572450,d.aWc', 'Google', u'hello'), ('http://www.google.co.uk/url?sa=t&rct=j&q=hello&source=web&cd=1&ved=0CDMQFjAA&url=http%3A%2F%2Fwww.hellomagazine.com%2F&ei=4TfSUdCLEY2_ywHXp4GADg&usg=AFQjCNE2TScP1sOG-TytWVe-kB0UUbWncg&bvm=bv.48572450,d.aWc', 'Google', u'hello'), ('http://www.google.co.in/url?sa=t&rct=j&q=%E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87&source=web&cd=1&ved=0CCwQFjAA&url=http%3A%2F%2Fhi.wikipedia.org%2Fwiki%2F%25E0%25A4%25A8%25E0%25A4%25AE%25E0%25A4%25B8%25E0%25A5%258D%25E0%25A4%25A4%25E0%25A5%2587&ei=eTfSUceOJ6i4yAGk6oGoDw&usg=AFQjCNGpYEO70ix-UtllM5iLl8Ywenwbpw&bvm=bv.48572450,d.aWc', 'Google', u'\u0928\u092e\u0938\u094d\u0924\u0947'), ('http://www.google.com.ua/url?sa=t&rct=j&q=%D0%BF%D1%80%D0%B8%D0%B2%D1%96%D1%82&source=web&cd=1&ved=0CC0QFjAA&url=http%3A%2F%2Fuk.wiktionary.org%2Fwiki%2F%25D0%25BF%25D1%2580%25D0%25B8%25D0%25B2%25D1%2596%25D1%2582&ei=dTjSUYnYJuSOyAG-rYDoCQ&usg=AFQjCNHloehQAu7UJ6upCqMVCDpNh3sNTg&bvm=bv.48572450,d.aWc', 'Google', u'\u043f\u0440\u0438\u0432\u0456\u0442'), ('http://www.google.ca/url?sa=i&rct=j&q=%22justin+timberlake%22&source=images&cd=&docid=mPgs-1UiH9v_-M&tbnid=Zkq86Kfj5IWYxM:&ved=0CAEQjxw&url=http%3A%2F%2Fwww.salon.com%2F2013%2F01%2F10%2Fis_justin_timberlake_releasing_a_new_album%2F&ei=ZuDmUaOlEaTk4AOE_4GgCQ&bvm=bv.49405654,d.dmg&psig=AFQjCNEX5ylDUbWb68msE2VIYZw-ObWtNg&ust=1374171617895913', 'Google', u'""justin timberlake""'), ('https://www.google.com/', 'Google', u''), ('https://www.google.co.uk', 'Google', u''), ('http://www.google.ca/search?hl=en&site=imghp&tbm=isch&source=hp&biw=1436&bih=508&q=lenovo&oq=lenovo&gs_l=img.3..0l10.2042.2539.0.2755.6.5.0.1.1.0.99.382.5.5.0....0.0..1ac.1.20.img.zuc4SkaG3pk#q=lenovo&hl=en&site=imghp&tbs=isz:l,qdr:d,itp:photo&tbm=isch&source=lnt&sa=X&ei=chbnUcwB88fgA9GdgdgD&ved=0CD0QpwUoAg&bav=on.2,or.r_qf.&bvm=bv.49405654%2Cd.dmg%2Cpv.xjs.s.en_US.QXiTEk6XjhM.O&fp=74e28ccdf351cc74&biw=1436&bih=508&facrc=_&imgdii=_&imgrc=PWcY9IoUsS8fqM%3A%3BLXHKDtPubm1b_M%3Bhttp%253A%252F%252Fmy.kyozou.com%252Fpictures%252F_15%252F14595%252F14594417.jpg%3Bhttp%253A%252F%252Fwww.ebay.com%252Fitm%252FLENOVO-X200-LAPTOP-CORE-2-DUO-1-86GHz-4GB-160GB-WIRELESS-%252F221252458890%253Fpt%253DLaptops_Nov05%2526hash%253Ditem3383ac998a%3B1600%3B1200', 'Google Images', u'lenovo'), ('https://www.google.it', 'Google', u''), ('https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&sqi=2&ved=0ahUKEwipjbfT-aPSAhUnCcAKHTbHALEQFgg7MAQ&url=https%3A%2F%2Fwww.thesun.co.uk%2Ftvandshowbiz%2F2735307%2Fantonio-banderas-rushed-to-surrey-hospital-after-suffering-agonising-chest-pains-during-workout%2F&usg=AFQjCNG4c9vrYPeffmVxckMAfRj51PMlpA&bvm=bv.147448319,d.ZGg', 'Google', u'') # TODO: More google edge cases ) self.assertValidSERPs(serps) def test_baidu(self): # TODO: More tests for Baidu serps = ( ('http://www.baidu.com/s?wd=%E4%BD%A0%E5%A5%BD&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_n=2&rsv_sug3=1&rsv_sug=0&rsv_sug1=1&rsv_sug4=352&inputT=1295', 'Baidu', u'\u4f60\u597d'), ) self.assertValidSERPs(serps) def test_yahoo(self): serps = ( ('http://ca.search.yahoo.com/search;_ylt=At9vKXZDJTDsQ6o7bDQPLBUt17V_;_ylc=X1MDMjE0MjYyMzUzMwRfcgMyBGZyA3lmcC10LTcxNQRuX2dwcwMxMARvcmlnaW4DY2EueWFob28uY29tBHF1ZXJ5A2hlbGxvBHNhbwMx?p=hello&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-715', 'Yahoo!', u'hello'), ('http://search.yahoo.com/search;_ylt=AnQcoCW29caK.8RLkGgSiqGbvZx4?p=united+states&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-900', 'Yahoo!', u'united states'), ('http://r.search.yahoo.com/_ylt=A0LEVy5UeJBUUzgAR2FXNyoA;_ylu=X3oDMTEzaGpsYTZuBHNlYwNzcgRwb3MDMgRjb2xvA2JmMQR2dGlkA1ZJUDU1OF8x/RV=2/RE=1418782933/RO=10/RU=http%3a%2f%2fen.wikipedia.org%2fwiki%2fToronto/RK=0/RS=cUOWJ12k59iqbScMA1r6sQedikc-', 'Yahoo!', u''), ('http://search.yahoo.co.jp/r/FOR=37DL17lV3iiMNf3sheF_mW3_Wj.3fsuxSYrKIfnPpLU9WfFZNrTrfvC9QoqcRuxcbIEmM8.KUZMstk6uS4bFjWiMtyznwPKVLc5D_jdUJEZfaGWVckvvflhOs2_IJ92LNv.BC70V4k5MjELmVo9hCIH.8oEkxxu2ItwfOqmPrTxpzKFGXYCaoRqASxIKkddp31_emIbJyjwM0Cxikt2KOCIa3BRhNPt3uM5q1I8sG5dEyh.2SUUIwrxSNaFjtr6fO6M6zDkqso.DcLnt6s1c9s6eBpzDiOSLFdLB2CPpUxyy6b6ZLemCC_6fbLnc7hsmYTE03FEs1zBy66gmxQv5U76_DkDzA2XaLqbD_U7JdCQ4e7XsyfXHXCcCQgSolYEj8U6cm_v7TJWFihGxbTC8o_crbF4Vuh36ueZOxueYss_Ng4kBlsTNaK4OEfNhjf.xjL.FZQQxRO.5b.mo05R78PP98pHrCJUy51uQg5X865ItMIodV262ZVR1aWdNzw0uE3_QTRk-/_ylt=A2Ri6BZGTQlZOEAAkUPPJ4pQ;_ylu=X3oDMTBuMjdjOXM3BHBvcwMxNwRzZWMDc3IEc2xrA3RpdGxl/SIG=18cti0pve/EXP=1493882630/**http%3A/www.swissinfo.ch/jpn/%25E7%25B1%25B3%25E8%25BB%258D-%25E5%25A4%25AA%25E5%25B9%25B3%25E6%25B4%258B%25E3%2581%25A7%25EF%25BD%2589%25EF%25BD%2583%25EF%25BD%2582%25EF%25BD%258D%25E5%2586%258D%25E5%25AE%259F%25E9%25A8%2593%25E3%2581%25B8/43149636', 'Yahoo! Japan', u''), ) self.assertValidSERPs(serps) def test_yandex(self): serps = ( ('http://www.yandex.com/yandsearch?text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82&lr=87', 'Yandex', u'\u043f\u0440\u0438\u0432\u0435\u0442'), ('http://yandex.ru/yandsearch?lr=10115&text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82', 'Yandex', u'\u043f\u0440\u0438\u0432\u0435\u0442'), ) self.assertValidSERPs(serps) def test_bing(self): serps = ( ('http://www.bing.com/search?q=united+states&go=&qs=n&form=QBLH&filt=all&pq=united+states&sc=8-13&sp=-1&sk=', 'Bing', u'united states'), ) self.assertValidSERPs(serps) def test_path_engines(self): """"""Tests for search engines that contain keywords within their paths and require regex extraction."""""" serps = ( ('http://www.123people.ca/s/michael+sukmanowsky', '123people', u'michael sukmanowsky'), ('http://www.1.cz/s/ars-technica/', '1.cz', u'ars-technica'), # These guys do not properly URL encode their keywords ) def test_custom_parser_explicit(self): self.assertInvalidSERP(self.custom_serp_url) self.assertValidSERP(self.custom_serp_url, self.custom_parser.engine_name, u'test', parser=self.custom_parser) def test_custom_parser_implicit(self): from serpextract.serpextract import _get_search_engines, _engines self.assertInvalidSERP(self.custom_serp_url) add_custom_parser(u'search.piccshare.com', self.custom_parser) self.assertValidSERP(self.custom_serp_url, self.custom_parser.engine_name, u'test') del _engines[u'search.piccshare.com'] def test_naive_detection(self): self.assertInvalidSERP(self.custom_serp_url) self.assertValidSERP(self.custom_serp_url, u'piccshare', u'test', use_naive_method=True) url = 'http://www.yahoo.com/#/%C2%BF??;%C2%AB99555$&&&4&' urlp = urlparse(url) self.assertInvalidSERP(urlparse(url), use_naive_method=True) self.assertInvalidSERP(url, use_naive_method=True) def test_get_all_query_params(self): """"""Ensure that get_all_query_params is a non-empty list."""""" params = get_all_query_params() self.assertIsInstance(params, list) self.assertGreater(len(params), 0) def test_get_query_params_by_domain(self): """""" make sure that individual subdomains are enumerated properly """""" params_by_domain = get_all_query_params_by_domain() google_params = [u'q', u'query'] bing_params = [u'Q', u'q'] baidu_params = [u'kw', u'wd', u'word'] yahoo_params = [u'p', u'q', u'va'] so_net_params = [u'kw', u'query'] goo_ne_jp_params = [u'MT'] t_online_params = [u'q'] self.assertEqual(params_by_domain['google.com'], google_params) self.assertEqual(params_by_domain['google.de'], google_params) self.assertEqual(params_by_domain['google.co.uk'], google_params) self.a", https://github.com/Parsely/serpextract/,aec8f2bd79c66953938569a24b05c693ab70b4ce,tests/test_serps.py::TestSERPs::test_custom_parser_explicit,tests/test_serps.py,OD-Vic,flaky,nan,nan,classify;root_cause,"from __future__ import absolute_import import unittest from six.moves.urllib.parse import urlparse from serpextract import (SearchEngineParser, extract, is_serp, get_all_query_params, add_custom_parser, get_all_query_params_by_domain) class TestSERPs(unittest.TestCase): """"""Most popular search engines to really worry about until we have full on test coverage are: - Google - Baidu - Yahoo - Yandex - Microsoft (Bing) We test these engines, and for each we ensure we try to cover the multiple country case and the keywords with crazy characters case. """""" def setUp(self): self.custom_serp_url = 'http://search.piccshare.com/search.php?cat=web&channel=main&hl=en&q=test' self.custom_parser = SearchEngineParser(u'PiccShare', u'q', u'/search.php?q={k}', u'utf-8') def assertInvalidSERP(self, url, **kwargs): self.assertIsNone(extract(url, **kwargs)) self.assertFalse(is_serp(url, **kwargs)) def assertValidSERP(self, url, expected_engine_name, expected_keyword, **kwargs): # Test both the URL and a parsed URL version for url in (url, urlparse(url)): res = extract(url, **kwargs) self.assertEqual(res.keyword, expected_keyword) self.assertEqual(res.engine_name, expected_engine_name) self.assertTrue(is_serp(url, **kwargs)) def assertValidSERPs(self, expected_serps, **kwargs): for url, engine_name, keyword in expected_serps: self.assertValidSERP(url, engine_name, keyword, **kwargs) def test_google(self): serps = ( ('http://www.google.com/url?sa=t&rct=j&q=hello&source=web&cd=1&ved=0CCoQFjAA&url=http%3A%2F%2Fwww.hellomagazine.ca%2F&ei=MDfSUe-JMob9ygGn24CoCw&usg=AFQjCNF6TQIo1aZe7WI8knqcdZax-lpg-A&bvm=bv.48572450,d.aWc', 'Google', u'hello'), ('http://www.google.co.uk/url?sa=t&rct=j&q=hello&source=web&cd=1&ved=0CDMQFjAA&url=http%3A%2F%2Fwww.hellomagazine.com%2F&ei=4TfSUdCLEY2_ywHXp4GADg&usg=AFQjCNE2TScP1sOG-TytWVe-kB0UUbWncg&bvm=bv.48572450,d.aWc', 'Google', u'hello'), ('http://www.google.co.in/url?sa=t&rct=j&q=%E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87&source=web&cd=1&ved=0CCwQFjAA&url=http%3A%2F%2Fhi.wikipedia.org%2Fwiki%2F%25E0%25A4%25A8%25E0%25A4%25AE%25E0%25A4%25B8%25E0%25A5%258D%25E0%25A4%25A4%25E0%25A5%2587&ei=eTfSUceOJ6i4yAGk6oGoDw&usg=AFQjCNGpYEO70ix-UtllM5iLl8Ywenwbpw&bvm=bv.48572450,d.aWc', 'Google', u'\u0928\u092e\u0938\u094d\u0924\u0947'), ('http://www.google.com.ua/url?sa=t&rct=j&q=%D0%BF%D1%80%D0%B8%D0%B2%D1%96%D1%82&source=web&cd=1&ved=0CC0QFjAA&url=http%3A%2F%2Fuk.wiktionary.org%2Fwiki%2F%25D0%25BF%25D1%2580%25D0%25B8%25D0%25B2%25D1%2596%25D1%2582&ei=dTjSUYnYJuSOyAG-rYDoCQ&usg=AFQjCNHloehQAu7UJ6upCqMVCDpNh3sNTg&bvm=bv.48572450,d.aWc', 'Google', u'\u043f\u0440\u0438\u0432\u0456\u0442'), ('http://www.google.ca/url?sa=i&rct=j&q=%22justin+timberlake%22&source=images&cd=&docid=mPgs-1UiH9v_-M&tbnid=Zkq86Kfj5IWYxM:&ved=0CAEQjxw&url=http%3A%2F%2Fwww.salon.com%2F2013%2F01%2F10%2Fis_justin_timberlake_releasing_a_new_album%2F&ei=ZuDmUaOlEaTk4AOE_4GgCQ&bvm=bv.49405654,d.dmg&psig=AFQjCNEX5ylDUbWb68msE2VIYZw-ObWtNg&ust=1374171617895913', 'Google', u'""justin timberlake""'), ('https://www.google.com/', 'Google', u''), ('https://www.google.co.uk', 'Google', u''), ('http://www.google.ca/search?hl=en&site=imghp&tbm=isch&source=hp&biw=1436&bih=508&q=lenovo&oq=lenovo&gs_l=img.3..0l10.2042.2539.0.2755.6.5.0.1.1.0.99.382.5.5.0....0.0..1ac.1.20.img.zuc4SkaG3pk#q=lenovo&hl=en&site=imghp&tbs=isz:l,qdr:d,itp:photo&tbm=isch&source=lnt&sa=X&ei=chbnUcwB88fgA9GdgdgD&ved=0CD0QpwUoAg&bav=on.2,or.r_qf.&bvm=bv.49405654%2Cd.dmg%2Cpv.xjs.s.en_US.QXiTEk6XjhM.O&fp=74e28ccdf351cc74&biw=1436&bih=508&facrc=_&imgdii=_&imgrc=PWcY9IoUsS8fqM%3A%3BLXHKDtPubm1b_M%3Bhttp%253A%252F%252Fmy.kyozou.com%252Fpictures%252F_15%252F14595%252F14594417.jpg%3Bhttp%253A%252F%252Fwww.ebay.com%252Fitm%252FLENOVO-X200-LAPTOP-CORE-2-DUO-1-86GHz-4GB-160GB-WIRELESS-%252F221252458890%253Fpt%253DLaptops_Nov05%2526hash%253Ditem3383ac998a%3B1600%3B1200', 'Google Images', u'lenovo'), ('https://www.google.it', 'Google', u''), ('https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&sqi=2&ved=0ahUKEwipjbfT-aPSAhUnCcAKHTbHALEQFgg7MAQ&url=https%3A%2F%2Fwww.thesun.co.uk%2Ftvandshowbiz%2F2735307%2Fantonio-banderas-rushed-to-surrey-hospital-after-suffering-agonising-chest-pains-during-workout%2F&usg=AFQjCNG4c9vrYPeffmVxckMAfRj51PMlpA&bvm=bv.147448319,d.ZGg', 'Google', u'') # TODO: More google edge cases ) self.assertValidSERPs(serps) def test_baidu(self): # TODO: More tests for Baidu serps = ( ('http://www.baidu.com/s?wd=%E4%BD%A0%E5%A5%BD&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_n=2&rsv_sug3=1&rsv_sug=0&rsv_sug1=1&rsv_sug4=352&inputT=1295', 'Baidu', u'\u4f60\u597d'), ) self.assertValidSERPs(serps) def test_yahoo(self): serps = ( ('http://ca.search.yahoo.com/search;_ylt=At9vKXZDJTDsQ6o7bDQPLBUt17V_;_ylc=X1MDMjE0MjYyMzUzMwRfcgMyBGZyA3lmcC10LTcxNQRuX2dwcwMxMARvcmlnaW4DY2EueWFob28uY29tBHF1ZXJ5A2hlbGxvBHNhbwMx?p=hello&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-715', 'Yahoo!', u'hello'), ('http://search.yahoo.com/search;_ylt=AnQcoCW29caK.8RLkGgSiqGbvZx4?p=united+states&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-900', 'Yahoo!', u'united states'), ('http://r.search.yahoo.com/_ylt=A0LEVy5UeJBUUzgAR2FXNyoA;_ylu=X3oDMTEzaGpsYTZuBHNlYwNzcgRwb3MDMgRjb2xvA2JmMQR2dGlkA1ZJUDU1OF8x/RV=2/RE=1418782933/RO=10/RU=http%3a%2f%2fen.wikipedia.org%2fwiki%2fToronto/RK=0/RS=cUOWJ12k59iqbScMA1r6sQedikc-', 'Yahoo!', u''), ('http://search.yahoo.co.jp/r/FOR=37DL17lV3iiMNf3sheF_mW3_Wj.3fsuxSYrKIfnPpLU9WfFZNrTrfvC9QoqcRuxcbIEmM8.KUZMstk6uS4bFjWiMtyznwPKVLc5D_jdUJEZfaGWVckvvflhOs2_IJ92LNv.BC70V4k5MjELmVo9hCIH.8oEkxxu2ItwfOqmPrTxpzKFGXYCaoRqASxIKkddp31_emIbJyjwM0Cxikt2KOCIa3BRhNPt3uM5q1I8sG5dEyh.2SUUIwrxSNaFjtr6fO6M6zDkqso.DcLnt6s1c9s6eBpzDiOSLFdLB2CPpUxyy6b6ZLemCC_6fbLnc7hsmYTE03FEs1zBy66gmxQv5U76_DkDzA2XaLqbD_U7JdCQ4e7XsyfXHXCcCQgSolYEj8U6cm_v7TJWFihGxbTC8o_crbF4Vuh36ueZOxueYss_Ng4kBlsTNaK4OEfNhjf.xjL.FZQQxRO.5b.mo05R78PP98pHrCJUy51uQg5X865ItMIodV262ZVR1aWdNzw0uE3_QTRk-/_ylt=A2Ri6BZGTQlZOEAAkUPPJ4pQ;_ylu=X3oDMTBuMjdjOXM3BHBvcwMxNwRzZWMDc3IEc2xrA3RpdGxl/SIG=18cti0pve/EXP=1493882630/**http%3A/www.swissinfo.ch/jpn/%25E7%25B1%25B3%25E8%25BB%258D-%25E5%25A4%25AA%25E5%25B9%25B3%25E6%25B4%258B%25E3%2581%25A7%25EF%25BD%2589%25EF%25BD%2583%25EF%25BD%2582%25EF%25BD%258D%25E5%2586%258D%25E5%25AE%259F%25E9%25A8%2593%25E3%2581%25B8/43149636', 'Yahoo! Japan', u''), ) self.assertValidSERPs(serps) def test_yandex(self): serps = ( ('http://www.yandex.com/yandsearch?text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82&lr=87', 'Yandex', u'\u043f\u0440\u0438\u0432\u0435\u0442'), ('http://yandex.ru/yandsearch?lr=10115&text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82', 'Yandex', u'\u043f\u0440\u0438\u0432\u0435\u0442'), ) self.assertValidSERPs(serps) def test_bing(self): serps = ( ('http://www.bing.com/search?q=united+states&go=&qs=n&form=QBLH&filt=all&pq=united+states&sc=8-13&sp=-1&sk=', 'Bing', u'united states'), ) self.assertValidSERPs(serps) def test_path_engines(self): """"""Tests for search engines that contain keywords within their paths and require regex extraction."""""" serps = ( ('http://www.123people.ca/s/michael+sukmanowsky', '123people', u'michael sukmanowsky'), ('http://www.1.cz/s/ars-technica/', '1.cz', u'ars-technica'), # These guys do not properly URL encode their keywords ) def test_custom_parser_explicit(self): self.assertInvalidSERP(self.custom_serp_url) self.assertValidSERP(self.custom_serp_url, self.custom_parser.engine_name, u'test', parser=self.custom_parser) def test_custom_parser_implicit(self): from serpextract.serpextract import _get_search_engines, _engines self.assertInvalidSERP(self.custom_serp_url) add_custom_parser(u'search.piccshare.com', self.custom_parser) self.assertValidSERP(self.custom_serp_url, self.custom_parser.engine_name, u'test') del _engines[u'search.piccshare.com'] def test_naive_detection(self): self.assertInvalidSERP(self.custom_serp_url) self.assertValidSERP(self.custom_serp_url, u'piccshare', u'test', use_naive_method=True) url = 'http://www.yahoo.com/#/%C2%BF??;%C2%AB99555$&&&4&' urlp = urlparse(url) self.assertInvalidSERP(urlparse(url), use_naive_method=True) self.assertInvalidSERP(url, use_naive_method=True) def test_get_all_query_params(self): """"""Ensure that get_all_query_params is a non-empty list."""""" params = get_all_query_params() self.assertIsInstance(params, list) self.assertGreater(len(params), 0) def test_get_query_params_by_domain(self): """""" make sure that individual subdomains are enumerated properly """""" params_by_domain = get_all_query_params_by_domain() google_params = [u'q', u'query'] bing_params = [u'Q', u'q'] baidu_params = [u'kw', u'wd', u'word'] yahoo_params = [u'p', u'q', u'va'] so_net_params = [u'kw', u'query'] goo_ne_jp_params = [u'MT'] t_online_params = [u'q'] self.assertEqual(params_by_domain['google.com'], google_params) self.assertEqual(params_by_domain['google.de'], google_params) self.assertEqual(params_by_domain['google.co.uk'], google_params) self.a", https://github.com/Parsely/serpextract/,aec8f2bd79c66953938569a24b05c693ab70b4ce,tests/test_serps.py::TestSERPs::test_custom_parser_implicit,tests/test_serps.py,OD-Brit,flaky,nan,nan,classify;root_cause,"from __future__ import absolute_import import unittest from six.moves.urllib.parse import urlparse from serpextract import (SearchEngineParser, extract, is_serp, get_all_query_params, add_custom_parser, get_all_query_params_by_domain) class TestSERPs(unittest.TestCase): """"""Most popular search engines to really worry about until we have full on test coverage are: - Google - Baidu - Yahoo - Yandex - Microsoft (Bing) We test these engines, and for each we ensure we try to cover the multiple country case and the keywords with crazy characters case. """""" def setUp(self): self.custom_serp_url = 'http://search.piccshare.com/search.php?cat=web&channel=main&hl=en&q=test' self.custom_parser = SearchEngineParser(u'PiccShare', u'q', u'/search.php?q={k}', u'utf-8') def assertInvalidSERP(self, url, **kwargs): self.assertIsNone(extract(url, **kwargs)) self.assertFalse(is_serp(url, **kwargs)) def assertValidSERP(self, url, expected_engine_name, expected_keyword, **kwargs): # Test both the URL and a parsed URL version for url in (url, urlparse(url)): res = extract(url, **kwargs) self.assertEqual(res.keyword, expected_keyword) self.assertEqual(res.engine_name, expected_engine_name) self.assertTrue(is_serp(url, **kwargs)) def assertValidSERPs(self, expected_serps, **kwargs): for url, engine_name, keyword in expected_serps: self.assertValidSERP(url, engine_name, keyword, **kwargs) def test_google(self): serps = ( ('http://www.google.com/url?sa=t&rct=j&q=hello&source=web&cd=1&ved=0CCoQFjAA&url=http%3A%2F%2Fwww.hellomagazine.ca%2F&ei=MDfSUe-JMob9ygGn24CoCw&usg=AFQjCNF6TQIo1aZe7WI8knqcdZax-lpg-A&bvm=bv.48572450,d.aWc', 'Google', u'hello'), ('http://www.google.co.uk/url?sa=t&rct=j&q=hello&source=web&cd=1&ved=0CDMQFjAA&url=http%3A%2F%2Fwww.hellomagazine.com%2F&ei=4TfSUdCLEY2_ywHXp4GADg&usg=AFQjCNE2TScP1sOG-TytWVe-kB0UUbWncg&bvm=bv.48572450,d.aWc', 'Google', u'hello'), ('http://www.google.co.in/url?sa=t&rct=j&q=%E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87&source=web&cd=1&ved=0CCwQFjAA&url=http%3A%2F%2Fhi.wikipedia.org%2Fwiki%2F%25E0%25A4%25A8%25E0%25A4%25AE%25E0%25A4%25B8%25E0%25A5%258D%25E0%25A4%25A4%25E0%25A5%2587&ei=eTfSUceOJ6i4yAGk6oGoDw&usg=AFQjCNGpYEO70ix-UtllM5iLl8Ywenwbpw&bvm=bv.48572450,d.aWc', 'Google', u'\u0928\u092e\u0938\u094d\u0924\u0947'), ('http://www.google.com.ua/url?sa=t&rct=j&q=%D0%BF%D1%80%D0%B8%D0%B2%D1%96%D1%82&source=web&cd=1&ved=0CC0QFjAA&url=http%3A%2F%2Fuk.wiktionary.org%2Fwiki%2F%25D0%25BF%25D1%2580%25D0%25B8%25D0%25B2%25D1%2596%25D1%2582&ei=dTjSUYnYJuSOyAG-rYDoCQ&usg=AFQjCNHloehQAu7UJ6upCqMVCDpNh3sNTg&bvm=bv.48572450,d.aWc', 'Google', u'\u043f\u0440\u0438\u0432\u0456\u0442'), ('http://www.google.ca/url?sa=i&rct=j&q=%22justin+timberlake%22&source=images&cd=&docid=mPgs-1UiH9v_-M&tbnid=Zkq86Kfj5IWYxM:&ved=0CAEQjxw&url=http%3A%2F%2Fwww.salon.com%2F2013%2F01%2F10%2Fis_justin_timberlake_releasing_a_new_album%2F&ei=ZuDmUaOlEaTk4AOE_4GgCQ&bvm=bv.49405654,d.dmg&psig=AFQjCNEX5ylDUbWb68msE2VIYZw-ObWtNg&ust=1374171617895913', 'Google', u'""justin timberlake""'), ('https://www.google.com/', 'Google', u''), ('https://www.google.co.uk', 'Google', u''), ('http://www.google.ca/search?hl=en&site=imghp&tbm=isch&source=hp&biw=1436&bih=508&q=lenovo&oq=lenovo&gs_l=img.3..0l10.2042.2539.0.2755.6.5.0.1.1.0.99.382.5.5.0....0.0..1ac.1.20.img.zuc4SkaG3pk#q=lenovo&hl=en&site=imghp&tbs=isz:l,qdr:d,itp:photo&tbm=isch&source=lnt&sa=X&ei=chbnUcwB88fgA9GdgdgD&ved=0CD0QpwUoAg&bav=on.2,or.r_qf.&bvm=bv.49405654%2Cd.dmg%2Cpv.xjs.s.en_US.QXiTEk6XjhM.O&fp=74e28ccdf351cc74&biw=1436&bih=508&facrc=_&imgdii=_&imgrc=PWcY9IoUsS8fqM%3A%3BLXHKDtPubm1b_M%3Bhttp%253A%252F%252Fmy.kyozou.com%252Fpictures%252F_15%252F14595%252F14594417.jpg%3Bhttp%253A%252F%252Fwww.ebay.com%252Fitm%252FLENOVO-X200-LAPTOP-CORE-2-DUO-1-86GHz-4GB-160GB-WIRELESS-%252F221252458890%253Fpt%253DLaptops_Nov05%2526hash%253Ditem3383ac998a%3B1600%3B1200', 'Google Images', u'lenovo'), ('https://www.google.it', 'Google', u''), ('https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&sqi=2&ved=0ahUKEwipjbfT-aPSAhUnCcAKHTbHALEQFgg7MAQ&url=https%3A%2F%2Fwww.thesun.co.uk%2Ftvandshowbiz%2F2735307%2Fantonio-banderas-rushed-to-surrey-hospital-after-suffering-agonising-chest-pains-during-workout%2F&usg=AFQjCNG4c9vrYPeffmVxckMAfRj51PMlpA&bvm=bv.147448319,d.ZGg', 'Google', u'') # TODO: More google edge cases ) self.assertValidSERPs(serps) def test_baidu(self): # TODO: More tests for Baidu serps = ( ('http://www.baidu.com/s?wd=%E4%BD%A0%E5%A5%BD&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_n=2&rsv_sug3=1&rsv_sug=0&rsv_sug1=1&rsv_sug4=352&inputT=1295', 'Baidu', u'\u4f60\u597d'), ) self.assertValidSERPs(serps) def test_yahoo(self): serps = ( ('http://ca.search.yahoo.com/search;_ylt=At9vKXZDJTDsQ6o7bDQPLBUt17V_;_ylc=X1MDMjE0MjYyMzUzMwRfcgMyBGZyA3lmcC10LTcxNQRuX2dwcwMxMARvcmlnaW4DY2EueWFob28uY29tBHF1ZXJ5A2hlbGxvBHNhbwMx?p=hello&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-715', 'Yahoo!', u'hello'), ('http://search.yahoo.com/search;_ylt=AnQcoCW29caK.8RLkGgSiqGbvZx4?p=united+states&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-900', 'Yahoo!', u'united states'), ('http://r.search.yahoo.com/_ylt=A0LEVy5UeJBUUzgAR2FXNyoA;_ylu=X3oDMTEzaGpsYTZuBHNlYwNzcgRwb3MDMgRjb2xvA2JmMQR2dGlkA1ZJUDU1OF8x/RV=2/RE=1418782933/RO=10/RU=http%3a%2f%2fen.wikipedia.org%2fwiki%2fToronto/RK=0/RS=cUOWJ12k59iqbScMA1r6sQedikc-', 'Yahoo!', u''), ('http://search.yahoo.co.jp/r/FOR=37DL17lV3iiMNf3sheF_mW3_Wj.3fsuxSYrKIfnPpLU9WfFZNrTrfvC9QoqcRuxcbIEmM8.KUZMstk6uS4bFjWiMtyznwPKVLc5D_jdUJEZfaGWVckvvflhOs2_IJ92LNv.BC70V4k5MjELmVo9hCIH.8oEkxxu2ItwfOqmPrTxpzKFGXYCaoRqASxIKkddp31_emIbJyjwM0Cxikt2KOCIa3BRhNPt3uM5q1I8sG5dEyh.2SUUIwrxSNaFjtr6fO6M6zDkqso.DcLnt6s1c9s6eBpzDiOSLFdLB2CPpUxyy6b6ZLemCC_6fbLnc7hsmYTE03FEs1zBy66gmxQv5U76_DkDzA2XaLqbD_U7JdCQ4e7XsyfXHXCcCQgSolYEj8U6cm_v7TJWFihGxbTC8o_crbF4Vuh36ueZOxueYss_Ng4kBlsTNaK4OEfNhjf.xjL.FZQQxRO.5b.mo05R78PP98pHrCJUy51uQg5X865ItMIodV262ZVR1aWdNzw0uE3_QTRk-/_ylt=A2Ri6BZGTQlZOEAAkUPPJ4pQ;_ylu=X3oDMTBuMjdjOXM3BHBvcwMxNwRzZWMDc3IEc2xrA3RpdGxl/SIG=18cti0pve/EXP=1493882630/**http%3A/www.swissinfo.ch/jpn/%25E7%25B1%25B3%25E8%25BB%258D-%25E5%25A4%25AA%25E5%25B9%25B3%25E6%25B4%258B%25E3%2581%25A7%25EF%25BD%2589%25EF%25BD%2583%25EF%25BD%2582%25EF%25BD%258D%25E5%2586%258D%25E5%25AE%259F%25E9%25A8%2593%25E3%2581%25B8/43149636', 'Yahoo! Japan', u''), ) self.assertValidSERPs(serps) def test_yandex(self): serps = ( ('http://www.yandex.com/yandsearch?text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82&lr=87', 'Yandex', u'\u043f\u0440\u0438\u0432\u0435\u0442'), ('http://yandex.ru/yandsearch?lr=10115&text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82', 'Yandex', u'\u043f\u0440\u0438\u0432\u0435\u0442'), ) self.assertValidSERPs(serps) def test_bing(self): serps = ( ('http://www.bing.com/search?q=united+states&go=&qs=n&form=QBLH&filt=all&pq=united+states&sc=8-13&sp=-1&sk=', 'Bing', u'united states'), ) self.assertValidSERPs(serps) def test_path_engines(self): """"""Tests for search engines that contain keywords within their paths and require regex extraction."""""" serps = ( ('http://www.123people.ca/s/michael+sukmanowsky', '123people', u'michael sukmanowsky'), ('http://www.1.cz/s/ars-technica/', '1.cz', u'ars-technica'), # These guys do not properly URL encode their keywords ) def test_custom_parser_explicit(self): self.assertInvalidSERP(self.custom_serp_url) self.assertValidSERP(self.custom_serp_url, self.custom_parser.engine_name, u'test', parser=self.custom_parser) def test_custom_parser_implicit(self): from serpextract.serpextract import _get_search_engines, _engines self.assertInvalidSERP(self.custom_serp_url) add_custom_parser(u'search.piccshare.com', self.custom_parser) self.assertValidSERP(self.custom_serp_url, self.custom_parser.engine_name, u'test') del _engines[u'search.piccshare.com'] def test_naive_detection(self): self.assertInvalidSERP(self.custom_serp_url) self.assertValidSERP(self.custom_serp_url, u'piccshare', u'test', use_naive_method=True) url = 'http://www.yahoo.com/#/%C2%BF??;%C2%AB99555$&&&4&' urlp = urlparse(url) self.assertInvalidSERP(urlparse(url), use_naive_method=True) self.assertInvalidSERP(url, use_naive_method=True) def test_get_all_query_params(self): """"""Ensure that get_all_query_params is a non-empty list."""""" params = get_all_query_params() self.assertIsInstance(params, list) self.assertGreater(len(params), 0) def test_get_query_params_by_domain(self): """""" make sure that individual subdomains are enumerated properly """""" params_by_domain = get_all_query_params_by_domain() google_params = [u'q', u'query'] bing_params = [u'Q', u'q'] baidu_params = [u'kw', u'wd', u'word'] yahoo_params = [u'p', u'q', u'va'] so_net_params = [u'kw', u'query'] goo_ne_jp_params = [u'MT'] t_online_params = [u'q'] self.assertEqual(params_by_domain['google.com'], google_params) self.assertEqual(params_by_domain['google.de'], google_params) self.assertEqual(params_by_domain['google.co.uk'], google_params) self.a", https://github.com/Parsely/serpextract/,aec8f2bd79c66953938569a24b05c693ab70b4ce,tests/test_serps.py::TestSERPs::test_naive_detection,tests/test_serps.py,OD-Vic,flaky,nan,nan,classify;root_cause,"from __future__ import absolute_import import unittest from six.moves.urllib.parse import urlparse from serpextract import (SearchEngineParser, extract, is_serp, get_all_query_params, add_custom_parser, get_all_query_params_by_domain) class TestSERPs(unittest.TestCase): """"""Most popular search engines to really worry about until we have full on test coverage are: - Google - Baidu - Yahoo - Yandex - Microsoft (Bing) We test these engines, and for each we ensure we try to cover the multiple country case and the keywords with crazy characters case. """""" def setUp(self): self.custom_serp_url = 'http://search.piccshare.com/search.php?cat=web&channel=main&hl=en&q=test' self.custom_parser = SearchEngineParser(u'PiccShare', u'q', u'/search.php?q={k}', u'utf-8') def assertInvalidSERP(self, url, **kwargs): self.assertIsNone(extract(url, **kwargs)) self.assertFalse(is_serp(url, **kwargs)) def assertValidSERP(self, url, expected_engine_name, expected_keyword, **kwargs): # Test both the URL and a parsed URL version for url in (url, urlparse(url)): res = extract(url, **kwargs) self.assertEqual(res.keyword, expected_keyword) self.assertEqual(res.engine_name, expected_engine_name) self.assertTrue(is_serp(url, **kwargs)) def assertValidSERPs(self, expected_serps, **kwargs): for url, engine_name, keyword in expected_serps: self.assertValidSERP(url, engine_name, keyword, **kwargs) def test_google(self): serps = ( ('http://www.google.com/url?sa=t&rct=j&q=hello&source=web&cd=1&ved=0CCoQFjAA&url=http%3A%2F%2Fwww.hellomagazine.ca%2F&ei=MDfSUe-JMob9ygGn24CoCw&usg=AFQjCNF6TQIo1aZe7WI8knqcdZax-lpg-A&bvm=bv.48572450,d.aWc', 'Google', u'hello'), ('http://www.google.co.uk/url?sa=t&rct=j&q=hello&source=web&cd=1&ved=0CDMQFjAA&url=http%3A%2F%2Fwww.hellomagazine.com%2F&ei=4TfSUdCLEY2_ywHXp4GADg&usg=AFQjCNE2TScP1sOG-TytWVe-kB0UUbWncg&bvm=bv.48572450,d.aWc', 'Google', u'hello'), ('http://www.google.co.in/url?sa=t&rct=j&q=%E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87&source=web&cd=1&ved=0CCwQFjAA&url=http%3A%2F%2Fhi.wikipedia.org%2Fwiki%2F%25E0%25A4%25A8%25E0%25A4%25AE%25E0%25A4%25B8%25E0%25A5%258D%25E0%25A4%25A4%25E0%25A5%2587&ei=eTfSUceOJ6i4yAGk6oGoDw&usg=AFQjCNGpYEO70ix-UtllM5iLl8Ywenwbpw&bvm=bv.48572450,d.aWc', 'Google', u'\u0928\u092e\u0938\u094d\u0924\u0947'), ('http://www.google.com.ua/url?sa=t&rct=j&q=%D0%BF%D1%80%D0%B8%D0%B2%D1%96%D1%82&source=web&cd=1&ved=0CC0QFjAA&url=http%3A%2F%2Fuk.wiktionary.org%2Fwiki%2F%25D0%25BF%25D1%2580%25D0%25B8%25D0%25B2%25D1%2596%25D1%2582&ei=dTjSUYnYJuSOyAG-rYDoCQ&usg=AFQjCNHloehQAu7UJ6upCqMVCDpNh3sNTg&bvm=bv.48572450,d.aWc', 'Google', u'\u043f\u0440\u0438\u0432\u0456\u0442'), ('http://www.google.ca/url?sa=i&rct=j&q=%22justin+timberlake%22&source=images&cd=&docid=mPgs-1UiH9v_-M&tbnid=Zkq86Kfj5IWYxM:&ved=0CAEQjxw&url=http%3A%2F%2Fwww.salon.com%2F2013%2F01%2F10%2Fis_justin_timberlake_releasing_a_new_album%2F&ei=ZuDmUaOlEaTk4AOE_4GgCQ&bvm=bv.49405654,d.dmg&psig=AFQjCNEX5ylDUbWb68msE2VIYZw-ObWtNg&ust=1374171617895913', 'Google', u'""justin timberlake""'), ('https://www.google.com/', 'Google', u''), ('https://www.google.co.uk', 'Google', u''), ('http://www.google.ca/search?hl=en&site=imghp&tbm=isch&source=hp&biw=1436&bih=508&q=lenovo&oq=lenovo&gs_l=img.3..0l10.2042.2539.0.2755.6.5.0.1.1.0.99.382.5.5.0....0.0..1ac.1.20.img.zuc4SkaG3pk#q=lenovo&hl=en&site=imghp&tbs=isz:l,qdr:d,itp:photo&tbm=isch&source=lnt&sa=X&ei=chbnUcwB88fgA9GdgdgD&ved=0CD0QpwUoAg&bav=on.2,or.r_qf.&bvm=bv.49405654%2Cd.dmg%2Cpv.xjs.s.en_US.QXiTEk6XjhM.O&fp=74e28ccdf351cc74&biw=1436&bih=508&facrc=_&imgdii=_&imgrc=PWcY9IoUsS8fqM%3A%3BLXHKDtPubm1b_M%3Bhttp%253A%252F%252Fmy.kyozou.com%252Fpictures%252F_15%252F14595%252F14594417.jpg%3Bhttp%253A%252F%252Fwww.ebay.com%252Fitm%252FLENOVO-X200-LAPTOP-CORE-2-DUO-1-86GHz-4GB-160GB-WIRELESS-%252F221252458890%253Fpt%253DLaptops_Nov05%2526hash%253Ditem3383ac998a%3B1600%3B1200', 'Google Images', u'lenovo'), ('https://www.google.it', 'Google', u''), ('https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&sqi=2&ved=0ahUKEwipjbfT-aPSAhUnCcAKHTbHALEQFgg7MAQ&url=https%3A%2F%2Fwww.thesun.co.uk%2Ftvandshowbiz%2F2735307%2Fantonio-banderas-rushed-to-surrey-hospital-after-suffering-agonising-chest-pains-during-workout%2F&usg=AFQjCNG4c9vrYPeffmVxckMAfRj51PMlpA&bvm=bv.147448319,d.ZGg', 'Google', u'') # TODO: More google edge cases ) self.assertValidSERPs(serps) def test_baidu(self): # TODO: More tests for Baidu serps = ( ('http://www.baidu.com/s?wd=%E4%BD%A0%E5%A5%BD&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_n=2&rsv_sug3=1&rsv_sug=0&rsv_sug1=1&rsv_sug4=352&inputT=1295', 'Baidu', u'\u4f60\u597d'), ) self.assertValidSERPs(serps) def test_yahoo(self): serps = ( ('http://ca.search.yahoo.com/search;_ylt=At9vKXZDJTDsQ6o7bDQPLBUt17V_;_ylc=X1MDMjE0MjYyMzUzMwRfcgMyBGZyA3lmcC10LTcxNQRuX2dwcwMxMARvcmlnaW4DY2EueWFob28uY29tBHF1ZXJ5A2hlbGxvBHNhbwMx?p=hello&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-715', 'Yahoo!', u'hello'), ('http://search.yahoo.com/search;_ylt=AnQcoCW29caK.8RLkGgSiqGbvZx4?p=united+states&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-900', 'Yahoo!', u'united states'), ('http://r.search.yahoo.com/_ylt=A0LEVy5UeJBUUzgAR2FXNyoA;_ylu=X3oDMTEzaGpsYTZuBHNlYwNzcgRwb3MDMgRjb2xvA2JmMQR2dGlkA1ZJUDU1OF8x/RV=2/RE=1418782933/RO=10/RU=http%3a%2f%2fen.wikipedia.org%2fwiki%2fToronto/RK=0/RS=cUOWJ12k59iqbScMA1r6sQedikc-', 'Yahoo!', u''), ('http://search.yahoo.co.jp/r/FOR=37DL17lV3iiMNf3sheF_mW3_Wj.3fsuxSYrKIfnPpLU9WfFZNrTrfvC9QoqcRuxcbIEmM8.KUZMstk6uS4bFjWiMtyznwPKVLc5D_jdUJEZfaGWVckvvflhOs2_IJ92LNv.BC70V4k5MjELmVo9hCIH.8oEkxxu2ItwfOqmPrTxpzKFGXYCaoRqASxIKkddp31_emIbJyjwM0Cxikt2KOCIa3BRhNPt3uM5q1I8sG5dEyh.2SUUIwrxSNaFjtr6fO6M6zDkqso.DcLnt6s1c9s6eBpzDiOSLFdLB2CPpUxyy6b6ZLemCC_6fbLnc7hsmYTE03FEs1zBy66gmxQv5U76_DkDzA2XaLqbD_U7JdCQ4e7XsyfXHXCcCQgSolYEj8U6cm_v7TJWFihGxbTC8o_crbF4Vuh36ueZOxueYss_Ng4kBlsTNaK4OEfNhjf.xjL.FZQQxRO.5b.mo05R78PP98pHrCJUy51uQg5X865ItMIodV262ZVR1aWdNzw0uE3_QTRk-/_ylt=A2Ri6BZGTQlZOEAAkUPPJ4pQ;_ylu=X3oDMTBuMjdjOXM3BHBvcwMxNwRzZWMDc3IEc2xrA3RpdGxl/SIG=18cti0pve/EXP=1493882630/**http%3A/www.swissinfo.ch/jpn/%25E7%25B1%25B3%25E8%25BB%258D-%25E5%25A4%25AA%25E5%25B9%25B3%25E6%25B4%258B%25E3%2581%25A7%25EF%25BD%2589%25EF%25BD%2583%25EF%25BD%2582%25EF%25BD%258D%25E5%2586%258D%25E5%25AE%259F%25E9%25A8%2593%25E3%2581%25B8/43149636', 'Yahoo! Japan', u''), ) self.assertValidSERPs(serps) def test_yandex(self): serps = ( ('http://www.yandex.com/yandsearch?text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82&lr=87', 'Yandex', u'\u043f\u0440\u0438\u0432\u0435\u0442'), ('http://yandex.ru/yandsearch?lr=10115&text=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82', 'Yandex', u'\u043f\u0440\u0438\u0432\u0435\u0442'), ) self.assertValidSERPs(serps) def test_bing(self): serps = ( ('http://www.bing.com/search?q=united+states&go=&qs=n&form=QBLH&filt=all&pq=united+states&sc=8-13&sp=-1&sk=', 'Bing', u'united states'), ) self.assertValidSERPs(serps) def test_path_engines(self): """"""Tests for search engines that contain keywords within their paths and require regex extraction."""""" serps = ( ('http://www.123people.ca/s/michael+sukmanowsky', '123people', u'michael sukmanowsky'), ('http://www.1.cz/s/ars-technica/', '1.cz', u'ars-technica'), # These guys do not properly URL encode their keywords ) def test_custom_parser_explicit(self): self.assertInvalidSERP(self.custom_serp_url) self.assertValidSERP(self.custom_serp_url, self.custom_parser.engine_name, u'test', parser=self.custom_parser) def test_custom_parser_implicit(self): from serpextract.serpextract import _get_search_engines, _engines self.assertInvalidSERP(self.custom_serp_url) add_custom_parser(u'search.piccshare.com', self.custom_parser) self.assertValidSERP(self.custom_serp_url, self.custom_parser.engine_name, u'test') del _engines[u'search.piccshare.com'] def test_naive_detection(self): self.assertInvalidSERP(self.custom_serp_url) self.assertValidSERP(self.custom_serp_url, u'piccshare', u'test', use_naive_method=True) url = 'http://www.yahoo.com/#/%C2%BF??;%C2%AB99555$&&&4&' urlp = urlparse(url) self.assertInvalidSERP(urlparse(url), use_naive_method=True) self.assertInvalidSERP(url, use_naive_method=True) def test_get_all_query_params(self): """"""Ensure that get_all_query_params is a non-empty list."""""" params = get_all_query_params() self.assertIsInstance(params, list) self.assertGreater(len(params), 0) def test_get_query_params_by_domain(self): """""" make sure that individual subdomains are enumerated properly """""" params_by_domain = get_all_query_params_by_domain() google_params = [u'q', u'query'] bing_params = [u'Q', u'q'] baidu_params = [u'kw', u'wd', u'word'] yahoo_params = [u'p', u'q', u'va'] so_net_params = [u'kw', u'query'] goo_ne_jp_params = [u'MT'] t_online_params = [u'q'] self.assertEqual(params_by_domain['google.com'], google_params) self.assertEqual(params_by_domain['google.de'], google_params) self.assertEqual(params_by_domain['google.co.uk'], google_params) self.a", https://github.com/pawelzny/dotty_dict,ae87e3fb76cbec5bfb9c885ccfb942afc3f0c9b5,tests/test_dotty_cache.py::test_getitem_cache,tests/test_dotty_cache.py,OD,flaky,Opened,https://github.com/pawelzny/dotty_dict/pull/85,classify;root_cause,"import unittest from unittest.mock import MagicMock from dotty_dict import dotty class TestDottyCache(unittest.TestCase): def test_getitem_cache(self): dot = dotty() dot._data = MagicMock() for _ in range(10): dot.get('x.y.z') self.assertEqual(dot.__getitem__.cache_info().hits, 9) ", https://github.com/pedrovanzella/do-the-webp,09b372de1c4f1d73d9284517c57c0d869f55144b,tests/test_compressor.py::test_compress_dir,tests/test_compressor.py,NIO,flaky,RepoArchived,nan,classify;root_cause,"from do_the_webp.compressor import compress import shutil import os import zipfile COMPRESS_TO_DIR = 'tmp' SOURCE_DIR = 'samples/to_be_compressed' def setup_module(): os.mkdir(COMPRESS_TO_DIR) def teardown_module(): shutil.rmtree(COMPRESS_TO_DIR) def test_compress_dir(): zipped = compress(SOURCE_DIR, COMPRESS_TO_DIR) assert zipped == 'to_be_compressed.cbz', ""file is properly named"" compressed_files = [] with zipfile.ZipFile(os.path.join(COMPRESS_TO_DIR, zipped), 'r') as z: compressed_files = [os.path.basename(x) for x in z.namelist()] assert os.listdir(SOURCE_DIR) == compressed_files, ""all files in the source dir were zipped"" ", https://github.com/pierrepo/autoclasswrapper,af43dd833e5586386098d1793a59332e968069da,tests/test_output.py::TestOutputClass::test_write_dendrogram,tests/test_output.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import sys import os import shutil import filecmp import pytest sys.path.insert(0,os.getcwd()) import autoclasswrapper as wrapper here = os.path.abspath(os.path.dirname(__file__)) data_dir = os.path.join(here, ""test_data"") target_root_name = ""sample-3-classes-real-location"" target_root_path = os.path.join(data_dir, target_root_name) @pytest.fixture(scope='session') def tmp_dir(tmpdir_factory): """"""Create temp dir, add files and cd in it Temp dir is in /tmp/pytest-of-$USER/pytest-XXX/ Doc: https://docs.pytest.org/en/latest/tmpdir.html """""" tmpd = tmpdir_factory.mktemp(""output"") os.chdir(str(tmpd)) for ext in ("".tsv"", "".case-data-1""): shutil.copy2(os.path.join(data_dir, target_root_name + ext), str(tmpd)) print(""Tests are in: {}"".format(str(tmpd))) class TestOutputClass(object): """"""Test for the Output class """""" def test_init(self): res = wrapper.Output() assert res.root_in_name == ""autoclass"" assert res.root_out_name == ""autoclass_out"" assert res.tolerate_error == False assert res.case_number == 0 assert res.class_number == 0 assert res.df == None assert res.stats == None assert res.experiment_names == [] def test_extract_results(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() assert ""Found 600 cases classified in 3 classes"" in caplog.text assert res.stats[""main-class""].nunique() == 3 assert res.stats.shape == (600, 5) def test_aggregate_input_data(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() res.aggregate_input_data() assert ""Aggregating input data"" in caplog.text assert os.path.isfile(res.root_out_name+"".tsv"") ref_file = target_root_path + ""_out.tsv"" assert filecmp.cmp(ref_file, res.root_out_name + "".tsv"", shallow=False) def test_write_cdt(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() res.aggregate_input_data() res.write_cdt() assert os.path.isfile(res.root_out_name+"".cdt"") ref_file = target_root_path + ""_out.cdt"" assert filecmp.cmp(ref_file, res.root_out_name + "".cdt"", shallow=False) def test_write_cdt_with_proba(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() res.aggregate_input_data() res.write_cdt(with_proba=True) assert os.path.isfile(res.root_out_name + ""_withproba.cdt"") ref_file = target_root_path + ""_out_withproba.cdt"" assert filecmp.cmp(ref_file, res.root_out_name + ""_withproba.cdt"", shallow=False) def test_write_dendrogram_no_stats(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() res.aggregate_input_data() res.write_dendrogram() statfile = res.root_out_name + ""_stats.tsv"" assert ""Cannot find {}"".format(statfile) in caplog.text def test_write_cluster_stats(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() res.aggregate_input_data() res.write_class_stats() assert os.path.isfile(res.root_out_name + ""_stats.tsv"") ref_file = target_root_path + ""_out_stats.tsv"" assert filecmp.cmp(ref_file, res.root_out_name + ""_stats.tsv"", shallow=False) def test_write_dendrogram(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() res.aggregate_input_data() res.write_dendrogram() assert os.path.isfile(res.root_out_name + ""_dendrogram.png"") ", https://github.com/pierrepo/autoclasswrapper,af43dd833e5586386098d1793a59332e968069da,tests/test_output.py::TestOutputClass::test_write_dendrogram_no_stats,tests/test_output.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import sys import os import shutil import filecmp import pytest sys.path.insert(0,os.getcwd()) import autoclasswrapper as wrapper here = os.path.abspath(os.path.dirname(__file__)) data_dir = os.path.join(here, ""test_data"") target_root_name = ""sample-3-classes-real-location"" target_root_path = os.path.join(data_dir, target_root_name) @pytest.fixture(scope='session') def tmp_dir(tmpdir_factory): """"""Create temp dir, add files and cd in it Temp dir is in /tmp/pytest-of-$USER/pytest-XXX/ Doc: https://docs.pytest.org/en/latest/tmpdir.html """""" tmpd = tmpdir_factory.mktemp(""output"") os.chdir(str(tmpd)) for ext in ("".tsv"", "".case-data-1""): shutil.copy2(os.path.join(data_dir, target_root_name + ext), str(tmpd)) print(""Tests are in: {}"".format(str(tmpd))) class TestOutputClass(object): """"""Test for the Output class """""" def test_init(self): res = wrapper.Output() assert res.root_in_name == ""autoclass"" assert res.root_out_name == ""autoclass_out"" assert res.tolerate_error == False assert res.case_number == 0 assert res.class_number == 0 assert res.df == None assert res.stats == None assert res.experiment_names == [] def test_extract_results(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() assert ""Found 600 cases classified in 3 classes"" in caplog.text assert res.stats[""main-class""].nunique() == 3 assert res.stats.shape == (600, 5) def test_aggregate_input_data(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() res.aggregate_input_data() assert ""Aggregating input data"" in caplog.text assert os.path.isfile(res.root_out_name+"".tsv"") ref_file = target_root_path + ""_out.tsv"" assert filecmp.cmp(ref_file, res.root_out_name + "".tsv"", shallow=False) def test_write_cdt(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() res.aggregate_input_data() res.write_cdt() assert os.path.isfile(res.root_out_name+"".cdt"") ref_file = target_root_path + ""_out.cdt"" assert filecmp.cmp(ref_file, res.root_out_name + "".cdt"", shallow=False) def test_write_cdt_with_proba(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() res.aggregate_input_data() res.write_cdt(with_proba=True) assert os.path.isfile(res.root_out_name + ""_withproba.cdt"") ref_file = target_root_path + ""_out_withproba.cdt"" assert filecmp.cmp(ref_file, res.root_out_name + ""_withproba.cdt"", shallow=False) def test_write_dendrogram_no_stats(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() res.aggregate_input_data() res.write_dendrogram() statfile = res.root_out_name + ""_stats.tsv"" assert ""Cannot find {}"".format(statfile) in caplog.text def test_write_cluster_stats(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() res.aggregate_input_data() res.write_class_stats() assert os.path.isfile(res.root_out_name + ""_stats.tsv"") ref_file = target_root_path + ""_out_stats.tsv"" assert filecmp.cmp(ref_file, res.root_out_name + ""_stats.tsv"", shallow=False) def test_write_dendrogram(self, caplog, tmp_dir): res = wrapper.Output(target_root_name) res.extract_results() res.aggregate_input_data() res.write_dendrogram() assert os.path.isfile(res.root_out_name + ""_dendrogram.png"") ", https://github.com/PiotrDabkowski/torchpwl,73b09cfa9f4c173988d8f075485b887c694fc5a8,torchpwl/pwl_test.py::test_pwl_default_init_response[7-3-MonoPointPWL],torchpwl/pwl_test.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest import torch import numpy as np from .pwl import PointPWL, MonoPointPWL, SlopedPWL, MonoSlopedPWL TOLERANCE = 1e-4 torch.manual_seed(11) def get_x(num_channels, batch_size=37, std=3.): return torch.Tensor(batch_size, num_channels).normal_(mean=0., std=std) @pytest.mark.parametrize(""pwl_module"", [PointPWL, MonoPointPWL, SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3, 5]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7, 11]) def test_pwl_init(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) x = get_x(num_channels) y = module(x) @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL, MonoPointPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) def test_pwl_default_init_response(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) x = get_x(num_channels) y = module(x) # Should initialize to y = x by default. expected_y = x assert torch.max(torch.abs(y - expected_y)) < TOLERANCE @pytest.mark.parametrize(""pwl_module"", [MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) @pytest.mark.parametrize(""monotonicity"", [-1, 0, 1]) def test_pwl_default_init_mono_response(pwl_module, num_channels, num_breakpoints, monotonicity): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints, monotonicity=monotonicity) x = get_x(num_channels) y = module(x) # Should initialize to y = x if monotonicity is 1 or 0, otherwise y = -x expected_y = x if monotonicity in (1, 0) else -x assert torch.max(torch.abs(y - expected_y)) < TOLERANCE @pytest.mark.parametrize(""pwl_module"", [MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) def test_pwl_default_init_multi_mono_response(pwl_module, num_channels, num_breakpoints): monotonicity = torch.Tensor(num_channels).normal_(std=100).long() % 3 - 1 module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints, monotonicity=monotonicity) x = get_x(num_channels) y = module(x) # Should initialize to y = x if monotonicity is 1 or 0, otherwise y = -x expected_y = torch.where(torch.eq(monotonicity, -1).unsqueeze(0), -x, x) assert torch.max(torch.abs(y - expected_y)) < TOLERANCE @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) def test_pwl_gradient_flows(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) x = get_x(num_channels) x.requires_grad = True y = module(x) torch.sum(y).backward() expected_grad = 1. assert torch.max(torch.abs(x.grad - expected_grad)) < TOLERANCE @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) def test_pwl_sloped_correct_num_breakpoints(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) assert list(module.get_sorted_x_positions().shape) == [ num_channels, num_breakpoints ] @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL, PointPWL, MonoPointPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 2, 3, 4]) def test_pwl_is_continous(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) with torch.no_grad(): for parameter in module.parameters(): parameter.normal_() x = torch.linspace( -4., 4., steps=10000).unsqueeze(1).expand(-1, num_channels) y = module(x) dy = torch.roll(y, shifts=-1, dims=0) - y dx = torch.roll(x, shifts=-1, dims=0) - x grad = dy / dx if isinstance(module, (PointPWL, MonoPointPWL)): allowed_grad = torch.max(4 / module.get_spreads()) else: allowed_grad = 4 assert torch.max(abs(grad)) < allowed_grad @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 2, 3, 4]) @pytest.mark.parametrize( ""optimizer_fn"", [ #lambda params: torch.optim.SGD(params=params, lr=0.1, momentum=0.5), lambda params: torch.optim.Adam(params=params, lr=0.2), ]) def test_pwl_fits(pwl_module, num_channels, num_breakpoints, optimizer_fn): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) bs = 128 opt = optimizer_fn(module.parameters()) steps = 4000 loss_ = 0 desired_loss = 0.02 for step in range(steps): x = torch.Tensor(np.random.normal(0, scale=2, size=(bs, num_channels))) expected_y = torch.Tensor( np.random.normal(0, scale=0.1, size=(bs, num_channels)) + np.where(x > 0.2, x, 0.2)) y = module(x) loss = torch.mean((expected_y - y)**2) opt.zero_grad() loss.backward() opt.step() if step % 10 == 0: print(loss.item()) loss_ = 0.8 * loss_ + loss.item() * 0.2 if loss_ < desired_loss: break assert loss_ < desired_loss @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""input_shape"", [ (11, 5, 7, 3), (11, 6), (11, 1), (11, 1, 1, 1), (5, 1, 2, 1), (5, 2, 2, 1), (5, 2, 2), ]) def test_input_packing(pwl_module, input_shape): num_channels = input_shape[1] b = pwl_module(num_channels=num_channels, num_breakpoints=2) inp = torch.Tensor(*input_shape).normal_() unpacked_inp = b.unpack_input(inp) assert unpacked_inp.shape[1] == num_channels assert len(unpacked_inp.shape) == 2 inp_restored = b.repack_input(unpacked_inp, inp.shape) assert list(inp_restored.shape) == list(inp.shape) assert torch.max(torch.abs(inp_restored - inp)).item() < TOLERANCE ", https://github.com/PiotrDabkowski/torchpwl,73b09cfa9f4c173988d8f075485b887c694fc5a8,torchpwl/pwl_test.py::test_pwl_is_continous[2-3-PointPWL],torchpwl/pwl_test.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest import torch import numpy as np from .pwl import PointPWL, MonoPointPWL, SlopedPWL, MonoSlopedPWL TOLERANCE = 1e-4 torch.manual_seed(11) def get_x(num_channels, batch_size=37, std=3.): return torch.Tensor(batch_size, num_channels).normal_(mean=0., std=std) @pytest.mark.parametrize(""pwl_module"", [PointPWL, MonoPointPWL, SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3, 5]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7, 11]) def test_pwl_init(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) x = get_x(num_channels) y = module(x) @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL, MonoPointPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) def test_pwl_default_init_response(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) x = get_x(num_channels) y = module(x) # Should initialize to y = x by default. expected_y = x assert torch.max(torch.abs(y - expected_y)) < TOLERANCE @pytest.mark.parametrize(""pwl_module"", [MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) @pytest.mark.parametrize(""monotonicity"", [-1, 0, 1]) def test_pwl_default_init_mono_response(pwl_module, num_channels, num_breakpoints, monotonicity): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints, monotonicity=monotonicity) x = get_x(num_channels) y = module(x) # Should initialize to y = x if monotonicity is 1 or 0, otherwise y = -x expected_y = x if monotonicity in (1, 0) else -x assert torch.max(torch.abs(y - expected_y)) < TOLERANCE @pytest.mark.parametrize(""pwl_module"", [MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) def test_pwl_default_init_multi_mono_response(pwl_module, num_channels, num_breakpoints): monotonicity = torch.Tensor(num_channels).normal_(std=100).long() % 3 - 1 module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints, monotonicity=monotonicity) x = get_x(num_channels) y = module(x) # Should initialize to y = x if monotonicity is 1 or 0, otherwise y = -x expected_y = torch.where(torch.eq(monotonicity, -1).unsqueeze(0), -x, x) assert torch.max(torch.abs(y - expected_y)) < TOLERANCE @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) def test_pwl_gradient_flows(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) x = get_x(num_channels) x.requires_grad = True y = module(x) torch.sum(y).backward() expected_grad = 1. assert torch.max(torch.abs(x.grad - expected_grad)) < TOLERANCE @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) def test_pwl_sloped_correct_num_breakpoints(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) assert list(module.get_sorted_x_positions().shape) == [ num_channels, num_breakpoints ] @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL, PointPWL, MonoPointPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 2, 3, 4]) def test_pwl_is_continous(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) with torch.no_grad(): for parameter in module.parameters(): parameter.normal_() x = torch.linspace( -4., 4., steps=10000).unsqueeze(1).expand(-1, num_channels) y = module(x) dy = torch.roll(y, shifts=-1, dims=0) - y dx = torch.roll(x, shifts=-1, dims=0) - x grad = dy / dx if isinstance(module, (PointPWL, MonoPointPWL)): allowed_grad = torch.max(4 / module.get_spreads()) else: allowed_grad = 4 assert torch.max(abs(grad)) < allowed_grad @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 2, 3, 4]) @pytest.mark.parametrize( ""optimizer_fn"", [ #lambda params: torch.optim.SGD(params=params, lr=0.1, momentum=0.5), lambda params: torch.optim.Adam(params=params, lr=0.2), ]) def test_pwl_fits(pwl_module, num_channels, num_breakpoints, optimizer_fn): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) bs = 128 opt = optimizer_fn(module.parameters()) steps = 4000 loss_ = 0 desired_loss = 0.02 for step in range(steps): x = torch.Tensor(np.random.normal(0, scale=2, size=(bs, num_channels))) expected_y = torch.Tensor( np.random.normal(0, scale=0.1, size=(bs, num_channels)) + np.where(x > 0.2, x, 0.2)) y = module(x) loss = torch.mean((expected_y - y)**2) opt.zero_grad() loss.backward() opt.step() if step % 10 == 0: print(loss.item()) loss_ = 0.8 * loss_ + loss.item() * 0.2 if loss_ < desired_loss: break assert loss_ < desired_loss @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""input_shape"", [ (11, 5, 7, 3), (11, 6), (11, 1), (11, 1, 1, 1), (5, 1, 2, 1), (5, 2, 2, 1), (5, 2, 2), ]) def test_input_packing(pwl_module, input_shape): num_channels = input_shape[1] b = pwl_module(num_channels=num_channels, num_breakpoints=2) inp = torch.Tensor(*input_shape).normal_() unpacked_inp = b.unpack_input(inp) assert unpacked_inp.shape[1] == num_channels assert len(unpacked_inp.shape) == 2 inp_restored = b.repack_input(unpacked_inp, inp.shape) assert list(inp_restored.shape) == list(inp.shape) assert torch.max(torch.abs(inp_restored - inp)).item() < TOLERANCE ", https://github.com/PiotrDabkowski/torchpwl,73b09cfa9f4c173988d8f075485b887c694fc5a8,torchpwl/pwl_test.py::test_pwl_is_continous[4-1-PointPWL],torchpwl/pwl_test.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest import torch import numpy as np from .pwl import PointPWL, MonoPointPWL, SlopedPWL, MonoSlopedPWL TOLERANCE = 1e-4 torch.manual_seed(11) def get_x(num_channels, batch_size=37, std=3.): return torch.Tensor(batch_size, num_channels).normal_(mean=0., std=std) @pytest.mark.parametrize(""pwl_module"", [PointPWL, MonoPointPWL, SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3, 5]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7, 11]) def test_pwl_init(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) x = get_x(num_channels) y = module(x) @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL, MonoPointPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) def test_pwl_default_init_response(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) x = get_x(num_channels) y = module(x) # Should initialize to y = x by default. expected_y = x assert torch.max(torch.abs(y - expected_y)) < TOLERANCE @pytest.mark.parametrize(""pwl_module"", [MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) @pytest.mark.parametrize(""monotonicity"", [-1, 0, 1]) def test_pwl_default_init_mono_response(pwl_module, num_channels, num_breakpoints, monotonicity): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints, monotonicity=monotonicity) x = get_x(num_channels) y = module(x) # Should initialize to y = x if monotonicity is 1 or 0, otherwise y = -x expected_y = x if monotonicity in (1, 0) else -x assert torch.max(torch.abs(y - expected_y)) < TOLERANCE @pytest.mark.parametrize(""pwl_module"", [MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) def test_pwl_default_init_multi_mono_response(pwl_module, num_channels, num_breakpoints): monotonicity = torch.Tensor(num_channels).normal_(std=100).long() % 3 - 1 module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints, monotonicity=monotonicity) x = get_x(num_channels) y = module(x) # Should initialize to y = x if monotonicity is 1 or 0, otherwise y = -x expected_y = torch.where(torch.eq(monotonicity, -1).unsqueeze(0), -x, x) assert torch.max(torch.abs(y - expected_y)) < TOLERANCE @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) def test_pwl_gradient_flows(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) x = get_x(num_channels) x.requires_grad = True y = module(x) torch.sum(y).backward() expected_grad = 1. assert torch.max(torch.abs(x.grad - expected_grad)) < TOLERANCE @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 7]) def test_pwl_sloped_correct_num_breakpoints(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) assert list(module.get_sorted_x_positions().shape) == [ num_channels, num_breakpoints ] @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL, PointPWL, MonoPointPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 2, 3, 4]) def test_pwl_is_continous(pwl_module, num_channels, num_breakpoints): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) with torch.no_grad(): for parameter in module.parameters(): parameter.normal_() x = torch.linspace( -4., 4., steps=10000).unsqueeze(1).expand(-1, num_channels) y = module(x) dy = torch.roll(y, shifts=-1, dims=0) - y dx = torch.roll(x, shifts=-1, dims=0) - x grad = dy / dx if isinstance(module, (PointPWL, MonoPointPWL)): allowed_grad = torch.max(4 / module.get_spreads()) else: allowed_grad = 4 assert torch.max(abs(grad)) < allowed_grad @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""num_channels"", [1, 3]) @pytest.mark.parametrize(""num_breakpoints"", [1, 2, 3, 4]) @pytest.mark.parametrize( ""optimizer_fn"", [ #lambda params: torch.optim.SGD(params=params, lr=0.1, momentum=0.5), lambda params: torch.optim.Adam(params=params, lr=0.2), ]) def test_pwl_fits(pwl_module, num_channels, num_breakpoints, optimizer_fn): module = pwl_module( num_channels=num_channels, num_breakpoints=num_breakpoints) bs = 128 opt = optimizer_fn(module.parameters()) steps = 4000 loss_ = 0 desired_loss = 0.02 for step in range(steps): x = torch.Tensor(np.random.normal(0, scale=2, size=(bs, num_channels))) expected_y = torch.Tensor( np.random.normal(0, scale=0.1, size=(bs, num_channels)) + np.where(x > 0.2, x, 0.2)) y = module(x) loss = torch.mean((expected_y - y)**2) opt.zero_grad() loss.backward() opt.step() if step % 10 == 0: print(loss.item()) loss_ = 0.8 * loss_ + loss.item() * 0.2 if loss_ < desired_loss: break assert loss_ < desired_loss @pytest.mark.parametrize(""pwl_module"", [SlopedPWL, MonoSlopedPWL]) @pytest.mark.parametrize(""input_shape"", [ (11, 5, 7, 3), (11, 6), (11, 1), (11, 1, 1, 1), (5, 1, 2, 1), (5, 2, 2, 1), (5, 2, 2), ]) def test_input_packing(pwl_module, input_shape): num_channels = input_shape[1] b = pwl_module(num_channels=num_channels, num_breakpoints=2) inp = torch.Tensor(*input_shape).normal_() unpacked_inp = b.unpack_input(inp) assert unpacked_inp.shape[1] == num_channels assert len(unpacked_inp.shape) == 2 inp_restored = b.repack_input(unpacked_inp, inp.shape) assert list(inp_restored.shape) == list(inp.shape) assert torch.max(torch.abs(inp_restored - inp)).item() < TOLERANCE ", https://github.com/politeauthority/docker-pretty-ps,2fcc9db4e391df2b96ccc2e967b41b6263fdcc2d,tests/test_dockerprettyps.py::TestDockerPrettyPs::test_container_display_name,tests/test_dockerprettyps.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"""""""Unit Tests for docker-pretty-ps """""" from datetime import datetime, timedelta import os import dockerprettyps from .data import docker_ps_data as test_ps_data from .data.cli_args import CliArgs class TestDockerPrettyPs(object): def test_version(self): """""" Tests dockerprettyps.version() method, basically making sure it doesnt crash. """""" assert dockerprettyps.__version__ assert dockerprettyps.version() def test_clean_output(self): """""" Tests the dockerprettyps.clean_output() method to make sure it takes the standard out from a 'docker ps' command and properly translated that into a usable set of docker container data. """""" dir_path = os.path.dirname(os.path.realpath(__file__)) data = open(os.path.join(dir_path, ""data"", ""raw_docker_ps.txt""), ""r"").read() containers = dockerprettyps.clean_output(data) assert isinstance(containers, list) assert len(containers) == 16 for container in containers: assert container.get(""container_id"") assert container.get(""image"") assert container.get(""command"") assert container.get(""created"") assert container.get(""created_date"") assert container.get(""status"") assert container.get(""status_date"") assert container.get(""running"") def test__parse_ports(self): """""" Tests the dockerprettyps._parse_ports() method ensure that we break apart ports properly as a trimmed list. """""" port_str = ' 0.0.0.0:5000->5000/tcp, 0.0.0.0:5001->80/tcp' ports = dockerprettyps._parse_ports(port_str) assert isinstance(ports, list) assert ports == ['0.0.0.0:5000->5000/tcp', '0.0.0.0:5001->80/tcp'] port_str = ' ' ports = dockerprettyps._parse_ports(port_str) assert isinstance(ports, list) assert ports == [] # assert type(test_ps_data.ps_data_1) == str def test__parse_ps_date(self): """""" Tests the dockerprettyps._clean_ps_date() method to make sure it takes docker ps date differences and makes a real datetime. United Example input: Up 20 hours """""" status_date_str = 'Up 12 minutes' the_date_minutes = dockerprettyps._parse_ps_date(status_date_str) assert isinstance(the_date_minutes, datetime) assert the_date_minutes < datetime.now() assert the_date_minutes > datetime.now() - timedelta(minutes=13) create_date_str = '5 days ago' the_date_days = dockerprettyps._parse_ps_date(create_date_str) assert isinstance(the_date_days, datetime) assert the_date_days < datetime.now() assert the_date_days > datetime.now() - timedelta(days=6) def test__clean_status(self): """""" Tests the dockerprettyps._clean_status() method to see if a the output from a container signifies if the container is running currently. """""" assert not dockerprettyps._clean_status(""Exited (1) 22 minutes ago"") assert dockerprettyps._clean_status(""Up 12 minutes"") def test_get_container_colors(self): """""" Tests the dockerprettyps.get_container_colors() method which runs all containers throuh the get_color method(), to try and assign a semi unique color to an instance based on it's container name. """""" containers = test_ps_data.ps_containers colorless_containers = [] for c in containers: c.pop('color') colorless_containers.append(c) color_containers = dockerprettyps.get_container_colors(colorless_containers) for c in color_containers: assert 'color' in c assert isinstance(c['color'], str) def test_get_color(self): """""" Tests the dockerprettyps.get_color() method to make sure any int passed will return an ANSII color code. """""" assert dockerprettyps.get_color(1) == ""\033[94m"" assert dockerprettyps.get_color(200) == ""\033[92m"" def test__get_num_running_containers(self): """""" Tests the dockerprettyps._get_num_running_containers() method to make sure it can actually count. """""" assert dockerprettyps._get_num_running_containers(test_ps_data.ps_containers) == 5 def test_filter_containers(self): """""" Tests the dockerprettyps.filter_containers() method to make sure it removes containers based on 'search' as well as running or not running containers. """""" assert len(test_ps_data.ps_containers) == 6 filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, CliArgs()) # assert len(filtered) == 5 args = CliArgs() args.search = [""postgres""] filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, args) assert len(filtered) == 1 args = CliArgs() args.search = [""postgres"", ""bad""] filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, args) assert len(filtered) == 3 args = CliArgs() args.all = True filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, args) assert len(filtered) == 6 def test_order_containers(self): """""" Tests the dockerprettyps.order_containers() method to make sure we order containers as you would expect. """""" self.order_containers_standard() self.order_containers_reverse() self.order_containers_by_name() self.order_containers_by_create_date() def order_containers_standard(self): """""" Tests that dockerprettyps.order_containers() will order containers by status_date by default. The default will list containers by oldest status_date to newest. """""" ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, CliArgs()) position_first_status_date = ordered[0][""status_date""] position_last_status_date = ordered[len(ordered) - 1][""status_date""] assert isinstance(ordered, list) assert position_first_status_date < position_last_status_date def order_containers_reverse(self): """""" Tests that dockerprettyps.order_containers() will order containers by status_date by default. This will list containers by newest status_date to oldest. """""" args = CliArgs() args.reverse = True ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_date = ordered[0][""status_date""] position_last_date = ordered[len(ordered) - 1][""status_date""] assert position_first_date > position_last_date def order_containers_by_create_date(self): """""" Tests that dockerprettyps.order_containers() will order containers by created date. """""" args = CliArgs() phrases = [""created"", ""created""] for phrase in phrases: args.order = phrase args.reverse = False ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_date = ordered[0][""created_date""] position_last_date = ordered[len(ordered) - 1][""created_date""] assert position_first_date < position_last_date args.reverse = True reverse_order = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_date = reverse_order[0][""created_date""] position_last_date = reverse_order[len(ordered) - 1][""created_date""] assert position_first_date > position_last_date def order_containers_by_name(self): """""" Tests that dockerprettyps.order_containers() will order containers by name. """""" args = CliArgs() phrases = [""name"", ""container""] for phrase in phrases: args.order = phrase ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_name = ordered[0][""name""] position_last_name = ordered[len(ordered) - 1][""name""] assert position_first_name == ""alpine-sshd"" assert position_last_name == ""some-postgres"" def order_containers_by_image(self): """""" Tests that dockerprettyps.order_containers() will order containers by name. """""" args = CliArgs() phrases = [""name"", ""container""] for phrase in phrases: args.order = phrase ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_name = ordered[0][""name""] position_last_name = ordered[len(ordered) - 1][""name""] assert position_first_name == ""alpine-sshd"" assert position_last_name == ""some-postgres"" def test_print_format(self): """""" Tests the dockerprettyps.print_format() method, primarily checking that the method doesnt fail, since it mostly just prints to the console. """""" assert dockerprettyps.print_format(test_ps_data.ps_containers, 6, 5, CliArgs()) def test_container_display_name(self): """""" Tests the dockerprettyps.container_display_name() method to see if we create the right console formatting for a container, with potential bolding to highlight search items. """""" containers = test_ps_data.ps_containers container = containers[0] container_display = dockerprettyps.container_display_name(container, CliArgs()) assert container_display == container[""color""] + container[""name""] + dockerprettyps.ENDC # Test that we bold the portion of a container name that matches a search if we have one. args = CliArgs() args.search = [""post""] for container in containers: ", https://github.com/politeauthority/docker-pretty-ps,2fcc9db4e391df2b96ccc2e967b41b6263fdcc2d,tests/test_dockerprettyps.py::TestDockerPrettyPs::test_get_container_colors,tests/test_dockerprettyps.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"""""""Unit Tests for docker-pretty-ps """""" from datetime import datetime, timedelta import os import dockerprettyps from .data import docker_ps_data as test_ps_data from .data.cli_args import CliArgs class TestDockerPrettyPs(object): def test_version(self): """""" Tests dockerprettyps.version() method, basically making sure it doesnt crash. """""" assert dockerprettyps.__version__ assert dockerprettyps.version() def test_clean_output(self): """""" Tests the dockerprettyps.clean_output() method to make sure it takes the standard out from a 'docker ps' command and properly translated that into a usable set of docker container data. """""" dir_path = os.path.dirname(os.path.realpath(__file__)) data = open(os.path.join(dir_path, ""data"", ""raw_docker_ps.txt""), ""r"").read() containers = dockerprettyps.clean_output(data) assert isinstance(containers, list) assert len(containers) == 16 for container in containers: assert container.get(""container_id"") assert container.get(""image"") assert container.get(""command"") assert container.get(""created"") assert container.get(""created_date"") assert container.get(""status"") assert container.get(""status_date"") assert container.get(""running"") def test__parse_ports(self): """""" Tests the dockerprettyps._parse_ports() method ensure that we break apart ports properly as a trimmed list. """""" port_str = ' 0.0.0.0:5000->5000/tcp, 0.0.0.0:5001->80/tcp' ports = dockerprettyps._parse_ports(port_str) assert isinstance(ports, list) assert ports == ['0.0.0.0:5000->5000/tcp', '0.0.0.0:5001->80/tcp'] port_str = ' ' ports = dockerprettyps._parse_ports(port_str) assert isinstance(ports, list) assert ports == [] # assert type(test_ps_data.ps_data_1) == str def test__parse_ps_date(self): """""" Tests the dockerprettyps._clean_ps_date() method to make sure it takes docker ps date differences and makes a real datetime. United Example input: Up 20 hours """""" status_date_str = 'Up 12 minutes' the_date_minutes = dockerprettyps._parse_ps_date(status_date_str) assert isinstance(the_date_minutes, datetime) assert the_date_minutes < datetime.now() assert the_date_minutes > datetime.now() - timedelta(minutes=13) create_date_str = '5 days ago' the_date_days = dockerprettyps._parse_ps_date(create_date_str) assert isinstance(the_date_days, datetime) assert the_date_days < datetime.now() assert the_date_days > datetime.now() - timedelta(days=6) def test__clean_status(self): """""" Tests the dockerprettyps._clean_status() method to see if a the output from a container signifies if the container is running currently. """""" assert not dockerprettyps._clean_status(""Exited (1) 22 minutes ago"") assert dockerprettyps._clean_status(""Up 12 minutes"") def test_get_container_colors(self): """""" Tests the dockerprettyps.get_container_colors() method which runs all containers throuh the get_color method(), to try and assign a semi unique color to an instance based on it's container name. """""" containers = test_ps_data.ps_containers colorless_containers = [] for c in containers: c.pop('color') colorless_containers.append(c) color_containers = dockerprettyps.get_container_colors(colorless_containers) for c in color_containers: assert 'color' in c assert isinstance(c['color'], str) def test_get_color(self): """""" Tests the dockerprettyps.get_color() method to make sure any int passed will return an ANSII color code. """""" assert dockerprettyps.get_color(1) == ""\033[94m"" assert dockerprettyps.get_color(200) == ""\033[92m"" def test__get_num_running_containers(self): """""" Tests the dockerprettyps._get_num_running_containers() method to make sure it can actually count. """""" assert dockerprettyps._get_num_running_containers(test_ps_data.ps_containers) == 5 def test_filter_containers(self): """""" Tests the dockerprettyps.filter_containers() method to make sure it removes containers based on 'search' as well as running or not running containers. """""" assert len(test_ps_data.ps_containers) == 6 filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, CliArgs()) # assert len(filtered) == 5 args = CliArgs() args.search = [""postgres""] filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, args) assert len(filtered) == 1 args = CliArgs() args.search = [""postgres"", ""bad""] filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, args) assert len(filtered) == 3 args = CliArgs() args.all = True filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, args) assert len(filtered) == 6 def test_order_containers(self): """""" Tests the dockerprettyps.order_containers() method to make sure we order containers as you would expect. """""" self.order_containers_standard() self.order_containers_reverse() self.order_containers_by_name() self.order_containers_by_create_date() def order_containers_standard(self): """""" Tests that dockerprettyps.order_containers() will order containers by status_date by default. The default will list containers by oldest status_date to newest. """""" ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, CliArgs()) position_first_status_date = ordered[0][""status_date""] position_last_status_date = ordered[len(ordered) - 1][""status_date""] assert isinstance(ordered, list) assert position_first_status_date < position_last_status_date def order_containers_reverse(self): """""" Tests that dockerprettyps.order_containers() will order containers by status_date by default. This will list containers by newest status_date to oldest. """""" args = CliArgs() args.reverse = True ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_date = ordered[0][""status_date""] position_last_date = ordered[len(ordered) - 1][""status_date""] assert position_first_date > position_last_date def order_containers_by_create_date(self): """""" Tests that dockerprettyps.order_containers() will order containers by created date. """""" args = CliArgs() phrases = [""created"", ""created""] for phrase in phrases: args.order = phrase args.reverse = False ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_date = ordered[0][""created_date""] position_last_date = ordered[len(ordered) - 1][""created_date""] assert position_first_date < position_last_date args.reverse = True reverse_order = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_date = reverse_order[0][""created_date""] position_last_date = reverse_order[len(ordered) - 1][""created_date""] assert position_first_date > position_last_date def order_containers_by_name(self): """""" Tests that dockerprettyps.order_containers() will order containers by name. """""" args = CliArgs() phrases = [""name"", ""container""] for phrase in phrases: args.order = phrase ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_name = ordered[0][""name""] position_last_name = ordered[len(ordered) - 1][""name""] assert position_first_name == ""alpine-sshd"" assert position_last_name == ""some-postgres"" def order_containers_by_image(self): """""" Tests that dockerprettyps.order_containers() will order containers by name. """""" args = CliArgs() phrases = [""name"", ""container""] for phrase in phrases: args.order = phrase ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_name = ordered[0][""name""] position_last_name = ordered[len(ordered) - 1][""name""] assert position_first_name == ""alpine-sshd"" assert position_last_name == ""some-postgres"" def test_print_format(self): """""" Tests the dockerprettyps.print_format() method, primarily checking that the method doesnt fail, since it mostly just prints to the console. """""" assert dockerprettyps.print_format(test_ps_data.ps_containers, 6, 5, CliArgs()) def test_container_display_name(self): """""" Tests the dockerprettyps.container_display_name() method to see if we create the right console formatting for a container, with potential bolding to highlight search items. """""" containers = test_ps_data.ps_containers container = containers[0] container_display = dockerprettyps.container_display_name(container, CliArgs()) assert container_display == container[""color""] + container[""name""] + dockerprettyps.ENDC # Test that we bold the portion of a container name that matches a search if we have one. args = CliArgs() args.search = [""post""] for container in containers: ", https://github.com/politeauthority/docker-pretty-ps,2fcc9db4e391df2b96ccc2e967b41b6263fdcc2d,tests/test_dockerprettyps.py::TestDockerPrettyPs::test_give_json,tests/test_dockerprettyps.py,NIO,flaky,Opened,https://github.com/politeauthority/docker-pretty-ps/pull/35,classify;root_cause,"""""""Unit Tests for docker-pretty-ps """""" from datetime import datetime, timedelta import os import dockerprettyps from .data import docker_ps_data as test_ps_data from .data.cli_args import CliArgs class TestDockerPrettyPs(object): def test_version(self): """""" Tests dockerprettyps.version() method, basically making sure it doesnt crash. """""" assert dockerprettyps.__version__ assert dockerprettyps.version() def test_clean_output(self): """""" Tests the dockerprettyps.clean_output() method to make sure it takes the standard out from a 'docker ps' command and properly translated that into a usable set of docker container data. """""" dir_path = os.path.dirname(os.path.realpath(__file__)) data = open(os.path.join(dir_path, ""data"", ""raw_docker_ps.txt""), ""r"").read() containers = dockerprettyps.clean_output(data) assert isinstance(containers, list) assert len(containers) == 16 for container in containers: assert container.get(""container_id"") assert container.get(""image"") assert container.get(""command"") assert container.get(""created"") assert container.get(""created_date"") assert container.get(""status"") assert container.get(""status_date"") assert container.get(""running"") def test__parse_ports(self): """""" Tests the dockerprettyps._parse_ports() method ensure that we break apart ports properly as a trimmed list. """""" port_str = ' 0.0.0.0:5000->5000/tcp, 0.0.0.0:5001->80/tcp' ports = dockerprettyps._parse_ports(port_str) assert isinstance(ports, list) assert ports == ['0.0.0.0:5000->5000/tcp', '0.0.0.0:5001->80/tcp'] port_str = ' ' ports = dockerprettyps._parse_ports(port_str) assert isinstance(ports, list) assert ports == [] # assert type(test_ps_data.ps_data_1) == str def test__parse_ps_date(self): """""" Tests the dockerprettyps._clean_ps_date() method to make sure it takes docker ps date differences and makes a real datetime. United Example input: Up 20 hours """""" status_date_str = 'Up 12 minutes' the_date_minutes = dockerprettyps._parse_ps_date(status_date_str) assert isinstance(the_date_minutes, datetime) assert the_date_minutes < datetime.now() assert the_date_minutes > datetime.now() - timedelta(minutes=13) create_date_str = '5 days ago' the_date_days = dockerprettyps._parse_ps_date(create_date_str) assert isinstance(the_date_days, datetime) assert the_date_days < datetime.now() assert the_date_days > datetime.now() - timedelta(days=6) def test__clean_status(self): """""" Tests the dockerprettyps._clean_status() method to see if a the output from a container signifies if the container is running currently. """""" assert not dockerprettyps._clean_status(""Exited (1) 22 minutes ago"") assert dockerprettyps._clean_status(""Up 12 minutes"") def test_get_container_colors(self): """""" Tests the dockerprettyps.get_container_colors() method which runs all containers throuh the get_color method(), to try and assign a semi unique color to an instance based on it's container name. """""" containers = test_ps_data.ps_containers colorless_containers = [] for c in containers: c.pop('color') colorless_containers.append(c) color_containers = dockerprettyps.get_container_colors(colorless_containers) for c in color_containers: assert 'color' in c assert isinstance(c['color'], str) def test_get_color(self): """""" Tests the dockerprettyps.get_color() method to make sure any int passed will return an ANSII color code. """""" assert dockerprettyps.get_color(1) == ""\033[94m"" assert dockerprettyps.get_color(200) == ""\033[92m"" def test__get_num_running_containers(self): """""" Tests the dockerprettyps._get_num_running_containers() method to make sure it can actually count. """""" assert dockerprettyps._get_num_running_containers(test_ps_data.ps_containers) == 5 def test_filter_containers(self): """""" Tests the dockerprettyps.filter_containers() method to make sure it removes containers based on 'search' as well as running or not running containers. """""" assert len(test_ps_data.ps_containers) == 6 filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, CliArgs()) # assert len(filtered) == 5 args = CliArgs() args.search = [""postgres""] filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, args) assert len(filtered) == 1 args = CliArgs() args.search = [""postgres"", ""bad""] filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, args) assert len(filtered) == 3 args = CliArgs() args.all = True filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, args) assert len(filtered) == 6 def test_order_containers(self): """""" Tests the dockerprettyps.order_containers() method to make sure we order containers as you would expect. """""" self.order_containers_standard() self.order_containers_reverse() self.order_containers_by_name() self.order_containers_by_create_date() def order_containers_standard(self): """""" Tests that dockerprettyps.order_containers() will order containers by status_date by default. The default will list containers by oldest status_date to newest. """""" ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, CliArgs()) position_first_status_date = ordered[0][""status_date""] position_last_status_date = ordered[len(ordered) - 1][""status_date""] assert isinstance(ordered, list) assert position_first_status_date < position_last_status_date def order_containers_reverse(self): """""" Tests that dockerprettyps.order_containers() will order containers by status_date by default. This will list containers by newest status_date to oldest. """""" args = CliArgs() args.reverse = True ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_date = ordered[0][""status_date""] position_last_date = ordered[len(ordered) - 1][""status_date""] assert position_first_date > position_last_date def order_containers_by_create_date(self): """""" Tests that dockerprettyps.order_containers() will order containers by created date. """""" args = CliArgs() phrases = [""created"", ""created""] for phrase in phrases: args.order = phrase args.reverse = False ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_date = ordered[0][""created_date""] position_last_date = ordered[len(ordered) - 1][""created_date""] assert position_first_date < position_last_date args.reverse = True reverse_order = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_date = reverse_order[0][""created_date""] position_last_date = reverse_order[len(ordered) - 1][""created_date""] assert position_first_date > position_last_date def order_containers_by_name(self): """""" Tests that dockerprettyps.order_containers() will order containers by name. """""" args = CliArgs() phrases = [""name"", ""container""] for phrase in phrases: args.order = phrase ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_name = ordered[0][""name""] position_last_name = ordered[len(ordered) - 1][""name""] assert position_first_name == ""alpine-sshd"" assert position_last_name == ""some-postgres"" def order_containers_by_image(self): """""" Tests that dockerprettyps.order_containers() will order containers by name. """""" args = CliArgs() phrases = [""name"", ""container""] for phrase in phrases: args.order = phrase ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_name = ordered[0][""name""] position_last_name = ordered[len(ordered) - 1][""name""] assert position_first_name == ""alpine-sshd"" assert position_last_name == ""some-postgres"" def test_print_format(self): """""" Tests the dockerprettyps.print_format() method, primarily checking that the method doesnt fail, since it mostly just prints to the console. """""" assert dockerprettyps.print_format(test_ps_data.ps_containers, 6, 5, CliArgs()) def test_container_display_name(self): """""" Tests the dockerprettyps.container_display_name() method to see if we create the right console formatting for a container, with potential bolding to highlight search items. """""" containers = test_ps_data.ps_containers container = containers[0] container_display = dockerprettyps.container_display_name(container, CliArgs()) assert container_display == container[""color""] + container[""name""] + dockerprettyps.ENDC # Test that we bold the portion of a container name that matches a search if we have one. args = CliArgs() args.search = [""post""] for container in containers: ", https://github.com/politeauthority/docker-pretty-ps,2fcc9db4e391df2b96ccc2e967b41b6263fdcc2d,tests/test_dockerprettyps.py::TestDockerPrettyPs::test_print_format,tests/test_dockerprettyps.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"""""""Unit Tests for docker-pretty-ps """""" from datetime import datetime, timedelta import os import dockerprettyps from .data import docker_ps_data as test_ps_data from .data.cli_args import CliArgs class TestDockerPrettyPs(object): def test_version(self): """""" Tests dockerprettyps.version() method, basically making sure it doesnt crash. """""" assert dockerprettyps.__version__ assert dockerprettyps.version() def test_clean_output(self): """""" Tests the dockerprettyps.clean_output() method to make sure it takes the standard out from a 'docker ps' command and properly translated that into a usable set of docker container data. """""" dir_path = os.path.dirname(os.path.realpath(__file__)) data = open(os.path.join(dir_path, ""data"", ""raw_docker_ps.txt""), ""r"").read() containers = dockerprettyps.clean_output(data) assert isinstance(containers, list) assert len(containers) == 16 for container in containers: assert container.get(""container_id"") assert container.get(""image"") assert container.get(""command"") assert container.get(""created"") assert container.get(""created_date"") assert container.get(""status"") assert container.get(""status_date"") assert container.get(""running"") def test__parse_ports(self): """""" Tests the dockerprettyps._parse_ports() method ensure that we break apart ports properly as a trimmed list. """""" port_str = ' 0.0.0.0:5000->5000/tcp, 0.0.0.0:5001->80/tcp' ports = dockerprettyps._parse_ports(port_str) assert isinstance(ports, list) assert ports == ['0.0.0.0:5000->5000/tcp', '0.0.0.0:5001->80/tcp'] port_str = ' ' ports = dockerprettyps._parse_ports(port_str) assert isinstance(ports, list) assert ports == [] # assert type(test_ps_data.ps_data_1) == str def test__parse_ps_date(self): """""" Tests the dockerprettyps._clean_ps_date() method to make sure it takes docker ps date differences and makes a real datetime. United Example input: Up 20 hours """""" status_date_str = 'Up 12 minutes' the_date_minutes = dockerprettyps._parse_ps_date(status_date_str) assert isinstance(the_date_minutes, datetime) assert the_date_minutes < datetime.now() assert the_date_minutes > datetime.now() - timedelta(minutes=13) create_date_str = '5 days ago' the_date_days = dockerprettyps._parse_ps_date(create_date_str) assert isinstance(the_date_days, datetime) assert the_date_days < datetime.now() assert the_date_days > datetime.now() - timedelta(days=6) def test__clean_status(self): """""" Tests the dockerprettyps._clean_status() method to see if a the output from a container signifies if the container is running currently. """""" assert not dockerprettyps._clean_status(""Exited (1) 22 minutes ago"") assert dockerprettyps._clean_status(""Up 12 minutes"") def test_get_container_colors(self): """""" Tests the dockerprettyps.get_container_colors() method which runs all containers throuh the get_color method(), to try and assign a semi unique color to an instance based on it's container name. """""" containers = test_ps_data.ps_containers colorless_containers = [] for c in containers: c.pop('color') colorless_containers.append(c) color_containers = dockerprettyps.get_container_colors(colorless_containers) for c in color_containers: assert 'color' in c assert isinstance(c['color'], str) def test_get_color(self): """""" Tests the dockerprettyps.get_color() method to make sure any int passed will return an ANSII color code. """""" assert dockerprettyps.get_color(1) == ""\033[94m"" assert dockerprettyps.get_color(200) == ""\033[92m"" def test__get_num_running_containers(self): """""" Tests the dockerprettyps._get_num_running_containers() method to make sure it can actually count. """""" assert dockerprettyps._get_num_running_containers(test_ps_data.ps_containers) == 5 def test_filter_containers(self): """""" Tests the dockerprettyps.filter_containers() method to make sure it removes containers based on 'search' as well as running or not running containers. """""" assert len(test_ps_data.ps_containers) == 6 filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, CliArgs()) # assert len(filtered) == 5 args = CliArgs() args.search = [""postgres""] filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, args) assert len(filtered) == 1 args = CliArgs() args.search = [""postgres"", ""bad""] filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, args) assert len(filtered) == 3 args = CliArgs() args.all = True filtered = dockerprettyps.filter_containers(test_ps_data.ps_containers, args) assert len(filtered) == 6 def test_order_containers(self): """""" Tests the dockerprettyps.order_containers() method to make sure we order containers as you would expect. """""" self.order_containers_standard() self.order_containers_reverse() self.order_containers_by_name() self.order_containers_by_create_date() def order_containers_standard(self): """""" Tests that dockerprettyps.order_containers() will order containers by status_date by default. The default will list containers by oldest status_date to newest. """""" ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, CliArgs()) position_first_status_date = ordered[0][""status_date""] position_last_status_date = ordered[len(ordered) - 1][""status_date""] assert isinstance(ordered, list) assert position_first_status_date < position_last_status_date def order_containers_reverse(self): """""" Tests that dockerprettyps.order_containers() will order containers by status_date by default. This will list containers by newest status_date to oldest. """""" args = CliArgs() args.reverse = True ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_date = ordered[0][""status_date""] position_last_date = ordered[len(ordered) - 1][""status_date""] assert position_first_date > position_last_date def order_containers_by_create_date(self): """""" Tests that dockerprettyps.order_containers() will order containers by created date. """""" args = CliArgs() phrases = [""created"", ""created""] for phrase in phrases: args.order = phrase args.reverse = False ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_date = ordered[0][""created_date""] position_last_date = ordered[len(ordered) - 1][""created_date""] assert position_first_date < position_last_date args.reverse = True reverse_order = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_date = reverse_order[0][""created_date""] position_last_date = reverse_order[len(ordered) - 1][""created_date""] assert position_first_date > position_last_date def order_containers_by_name(self): """""" Tests that dockerprettyps.order_containers() will order containers by name. """""" args = CliArgs() phrases = [""name"", ""container""] for phrase in phrases: args.order = phrase ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_name = ordered[0][""name""] position_last_name = ordered[len(ordered) - 1][""name""] assert position_first_name == ""alpine-sshd"" assert position_last_name == ""some-postgres"" def order_containers_by_image(self): """""" Tests that dockerprettyps.order_containers() will order containers by name. """""" args = CliArgs() phrases = [""name"", ""container""] for phrase in phrases: args.order = phrase ordered = dockerprettyps.order_containers(test_ps_data.ps_containers, args) position_first_name = ordered[0][""name""] position_last_name = ordered[len(ordered) - 1][""name""] assert position_first_name == ""alpine-sshd"" assert position_last_name == ""some-postgres"" def test_print_format(self): """""" Tests the dockerprettyps.print_format() method, primarily checking that the method doesnt fail, since it mostly just prints to the console. """""" assert dockerprettyps.print_format(test_ps_data.ps_containers, 6, 5, CliArgs()) def test_container_display_name(self): """""" Tests the dockerprettyps.container_display_name() method to see if we create the right console formatting for a container, with potential bolding to highlight search items. """""" containers = test_ps_data.ps_containers container = containers[0] container_display = dockerprettyps.container_display_name(container, CliArgs()) assert container_display == container[""color""] + container[""name""] + dockerprettyps.ENDC # Test that we bold the portion of a container name that matches a search if we have one. args = CliArgs() args.search = [""post""] for container in containers: ", https://github.com/PolyJIT/benchbuild,2ab959e84fc9119813c46de1034ffaefc2ee391b,tests/integration/test_cli_slurm.py::test_slurm_command,tests/integration/test_cli_slurm.py,NIO,flaky,Accepted,https://github.com/PolyJIT/benchbuild/pull/425,classify;root_cause;fix_proposal,"import typing as tp import pytest from plumbum import local from benchbuild.cli.slurm import Slurm from benchbuild.utils import cmd @pytest.fixture def cmd_mock() -> tp.Callable[[str], None]: def _cmd_mock(name: str): cmd.__overrides__[name] = ['/bin/true'] yield _cmd_mock cmd.__overrides__ = [] def test_slurm_command(tmp_path, cmd_mock): cmd_mock('srun') with pytest.raises(SystemExit) as pytest_wrapped_e: with local.cwd(tmp_path): Slurm.run(argv=['slurm', '-E', 'empty', 'test']) assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 0 ", https://github.com/Poogles/limitediterables,3ce3bc28d5c4092db3d5499ad60339c007f4c0e0,tests/test_iterables.py::test_actual_rate_slower_than_rate_limit,tests/test_iterables.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import time from unittest.mock import MagicMock import limitediterables.iterables as iterables def test_rate_limit(): """"""Test the rate limiting returns the same values as the parent."""""" # Create dummy range of numbers. target = range(100) slow_iter = iterables.LimitedIterable(target, limit=50) start_time = time.perf_counter() consumed_target = [i for i in slow_iter] end_time = time.perf_counter() expected = [i for i in target] assert consumed_target == expected, ""Check to see results are correct."" time_taken = end_time - start_time assert time_taken > 1.9, ""Check to see rate limit works low."" assert time_taken < 2.1, ""Check to see rate limit works high."" def test_rate_limit_negative(): """"""Test the rate limiting returns the same values as the parent."""""" # Create dummy range of numbers. target = range(100) slow_iter = iterables.LimitedIterable(target, limit=-50) start_time = time.perf_counter() consumed_target = [i for i in slow_iter] end_time = time.perf_counter() expected = [i for i in target] assert consumed_target == expected, ""Check to see results are correct."" time_taken = end_time - start_time assert time_taken > 1.9, ""Check to see rate limit works low."" assert time_taken < 2.1, ""Check to see rate limit works high."" def test_actual_rate_slower_than_rate_limit(): """"""Test to check we don't limit a iterator if it's slower already."""""" # Create dummy range of numbers. target = range(10) # Limit is set to be 100 second, with a range of 10 # that would result in the list being consumed in 0.1s slow_iter = iterables.LimitedIterable(target, limit=100) start_time = time.perf_counter() # For each iteration sleep 0.3, for 10 iterations thats # 3 seconds total sleep time. consumed_target = [] for i in slow_iter: time.sleep(0.3) consumed_target.append(i) end_time = time.perf_counter() expected = [i for i in target] time_taken = end_time - start_time assert consumed_target == expected, ""Check to see results are correct."" assert time_taken > 2.9, ""Check to see rate limit works low."" assert time_taken < 3.1, ""Check to see rate limit works high."" def test_no_limit(): """"""Test passing in no rate limit works."""""" target = range(10000) slow_iter = iterables.LimitedIterable(target) expected = [i for i in target] iterables.time.sleep = MagicMock() consumed_target = [i for i in slow_iter] assert consumed_target == expected, ""Check to see results are correct."" assert iterables.time.sleep.call_count == 0, ""Sleep not called."" ", https://github.com/Poogles/limitediterables,3ce3bc28d5c4092db3d5499ad60339c007f4c0e0,tests/test_iterables.py::test_rate_limit,tests/test_iterables.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import time from unittest.mock import MagicMock import limitediterables.iterables as iterables def test_rate_limit(): """"""Test the rate limiting returns the same values as the parent."""""" # Create dummy range of numbers. target = range(100) slow_iter = iterables.LimitedIterable(target, limit=50) start_time = time.perf_counter() consumed_target = [i for i in slow_iter] end_time = time.perf_counter() expected = [i for i in target] assert consumed_target == expected, ""Check to see results are correct."" time_taken = end_time - start_time assert time_taken > 1.9, ""Check to see rate limit works low."" assert time_taken < 2.1, ""Check to see rate limit works high."" def test_rate_limit_negative(): """"""Test the rate limiting returns the same values as the parent."""""" # Create dummy range of numbers. target = range(100) slow_iter = iterables.LimitedIterable(target, limit=-50) start_time = time.perf_counter() consumed_target = [i for i in slow_iter] end_time = time.perf_counter() expected = [i for i in target] assert consumed_target == expected, ""Check to see results are correct."" time_taken = end_time - start_time assert time_taken > 1.9, ""Check to see rate limit works low."" assert time_taken < 2.1, ""Check to see rate limit works high."" def test_actual_rate_slower_than_rate_limit(): """"""Test to check we don't limit a iterator if it's slower already."""""" # Create dummy range of numbers. target = range(10) # Limit is set to be 100 second, with a range of 10 # that would result in the list being consumed in 0.1s slow_iter = iterables.LimitedIterable(target, limit=100) start_time = time.perf_counter() # For each iteration sleep 0.3, for 10 iterations thats # 3 seconds total sleep time. consumed_target = [] for i in slow_iter: time.sleep(0.3) consumed_target.append(i) end_time = time.perf_counter() expected = [i for i in target] time_taken = end_time - start_time assert consumed_target == expected, ""Check to see results are correct."" assert time_taken > 2.9, ""Check to see rate limit works low."" assert time_taken < 3.1, ""Check to see rate limit works high."" def test_no_limit(): """"""Test passing in no rate limit works."""""" target = range(10000) slow_iter = iterables.LimitedIterable(target) expected = [i for i in target] iterables.time.sleep = MagicMock() consumed_target = [i for i in slow_iter] assert consumed_target == expected, ""Check to see results are correct."" assert iterables.time.sleep.call_count == 0, ""Sleep not called."" ", https://github.com/Poogles/limitediterables,3ce3bc28d5c4092db3d5499ad60339c007f4c0e0,tests/test_iterables.py::test_rate_limit_negative,tests/test_iterables.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import time from unittest.mock import MagicMock import limitediterables.iterables as iterables def test_rate_limit(): """"""Test the rate limiting returns the same values as the parent."""""" # Create dummy range of numbers. target = range(100) slow_iter = iterables.LimitedIterable(target, limit=50) start_time = time.perf_counter() consumed_target = [i for i in slow_iter] end_time = time.perf_counter() expected = [i for i in target] assert consumed_target == expected, ""Check to see results are correct."" time_taken = end_time - start_time assert time_taken > 1.9, ""Check to see rate limit works low."" assert time_taken < 2.1, ""Check to see rate limit works high."" def test_rate_limit_negative(): """"""Test the rate limiting returns the same values as the parent."""""" # Create dummy range of numbers. target = range(100) slow_iter = iterables.LimitedIterable(target, limit=-50) start_time = time.perf_counter() consumed_target = [i for i in slow_iter] end_time = time.perf_counter() expected = [i for i in target] assert consumed_target == expected, ""Check to see results are correct."" time_taken = end_time - start_time assert time_taken > 1.9, ""Check to see rate limit works low."" assert time_taken < 2.1, ""Check to see rate limit works high."" def test_actual_rate_slower_than_rate_limit(): """"""Test to check we don't limit a iterator if it's slower already."""""" # Create dummy range of numbers. target = range(10) # Limit is set to be 100 second, with a range of 10 # that would result in the list being consumed in 0.1s slow_iter = iterables.LimitedIterable(target, limit=100) start_time = time.perf_counter() # For each iteration sleep 0.3, for 10 iterations thats # 3 seconds total sleep time. consumed_target = [] for i in slow_iter: time.sleep(0.3) consumed_target.append(i) end_time = time.perf_counter() expected = [i for i in target] time_taken = end_time - start_time assert consumed_target == expected, ""Check to see results are correct."" assert time_taken > 2.9, ""Check to see rate limit works low."" assert time_taken < 3.1, ""Check to see rate limit works high."" def test_no_limit(): """"""Test passing in no rate limit works."""""" target = range(10000) slow_iter = iterables.LimitedIterable(target) expected = [i for i in target] iterables.time.sleep = MagicMock() consumed_target = [i for i in slow_iter] assert consumed_target == expected, ""Check to see results are correct."" assert iterables.time.sleep.call_count == 0, ""Sleep not called."" ", https://github.com/prancer-io/cloud-validation-framework,94522590d9881a5125d81da701a65f7f63383c0d,tests/processor/connector/test_snapshot_azure.py::test_get_node_happy,tests/processor/connector/test_snapshot_azure.py,NOD,flaky,nan,nan,classify;root_cause,""""""" Tests for snapshot azure"""""" import pytest snapshot = { ""source"": ""azureStructure.json"", ""testUser"": ""ajeybk1@kbajeygmail.onmicrosoft.com"", ""subscriptionId"": ""37f11aaf-0b72-44ef-a173-308e990279da"", ""nodes"": [ { ""snapshotId"": ""1"", ""type"": ""Microsoft.Compute/availabilitySets"", ""collection"": ""Microsoft.Compute"", ""path"":""/resourceGroups/mno-nonprod-shared-cet-eastus2-networkWatcher/providers/Microsoft.Compute/availabilitySets/mno-nonprod-shared-cet-eastus2-tab-as03"" } ] } snapshot_crawler = { ""source"": ""azureStructure1"", ""type"": ""azure"", ""testUser"": ""ajey.khanapuri@liquware.com"", ""subscriptionId"": [ ""d34d6141-7a19-4458-b0dd-f038bb7760c1"" ], ""nodes"": [ { ""masterSnapshotId"": ""31"", ""type"": ""Microsoft.Compute/availabilitySets"", ""collection"": ""Microsoft.Compute"" } ] } def mock_insert_one_document(doc, collection, dbname, check_keys=True): pass def mock_get_access_token(): return 'clientsecret' def mock_empty_get_access_token(): return None def mock_get_vault_data(client_id=None): return None def mock_get_web_client_data(snapshot_type, snapshot_source, snapshot_user): return 'client_id', None, 'sub_name', 'sub_id', 'tenant_id' def mock1_get_web_client_data(snapshot_type, snapshot_source, snapshot_user): if snapshot_user =='abcd': return 'client_id', None, 'sub_name', 'sub_id', 'tenant_id' return 'client_id', 'client_secret', 'sub_name', 'sub_id', 'tenant_id' def mock_http_get_request_happy(url, headers=None): data = {'a': 'b'} return 200, data def mock_http_get_crawler_request(url, headers=None): data = {'value': [ { 'id': 'test_id', 'type': 'Microsoft.Compute/availabilitySets' } ] } return 200, data def mock_get_from_currentdata(name): return None def mock_http_get_request_error(url, headers=None): data = {'a': 'b'} return 400, data def mock_get_client_secret(): return None def mock_db_json_source(): return True def mock_fs_json_source(): return False def mock_get_collection_size(collection_name): return 100 def mock_config_value(section, key, default=None): if key == 'structure': return 'structure' elif key == 'dbname': return 'dbname' return 'pytestdb' def mock_api_version_get_document(collection, dbname=None, sort=None, query=None, limit=1): return [{ ""json"": { ""fileType"": ""structure"", ""type"": ""others"", ""Microsoft.RecoveryServices/locations/backupStatus"": { ""version"": ""2017-07-01"" } } }] def test_get_version_for_type(monkeypatch): from processor.connector.snapshot_azure import get_version_for_type assert None == get_version_for_type({}) assert '2019-09-01' == get_version_for_type({'type': 'Microsoft.Network/virtualNetworks'}) monkeypatch.setattr('processor.connector.snapshot_azure.json_source', mock_db_json_source) monkeypatch.setattr('processor.connector.snapshot_azure.config_value', mock_config_value) monkeypatch.setattr('processor.connector.snapshot_azure.get_documents', mock_api_version_get_document) assert '2017-07-01' == get_version_for_type({'type': 'Microsoft.RecoveryServices/locations/backupStatus'}) def test_get_node_happy(monkeypatch): monkeypatch.setattr('processor.connector.snapshot_azure.http_get_request', mock_http_get_request_happy) from processor.connector.snapshot_azure import get_node data = { 'type': 'Microsoft.Network/virtualNetworks', 'snapshotId': '1', 'path': ""/resourceGroups/mno-nonprod-shared-cet-eastus2-networkWatcher/providers/"" ""Microsoft.Compute/availabilitySets/mno-nonprod-shared-cet-eastus2-tab-as03"" } ret = get_node(None, None, None, data, 'abc', 'azureStructure') assert True == isinstance(ret, dict) ret = get_node('abcd', 'devtest', 'xyz', data, 'abc', 'azureStructure') assert True == isinstance(ret, dict) assert {'a': 'b'} == ret['json'] def test_get_node_error(monkeypatch): monkeypatch.setattr('processor.connector.snapshot_azure.http_get_request', mock_http_get_request_error) from processor.connector.snapshot_azure import get_node data = { 'type': 'Microsoft.Network/virtualNetworks', 'snapshotId': '1', 'path': ""/resourceGroups/mno-nonprod-shared-cet-eastus2-networkWatcher/providers/"" ""Microsoft.Compute/availabilitySets/mno-nonprod-shared-cet-eastus2-tab-as03"" } ret = get_node(None, None, None, data, 'abc', 'azureStructure') assert True == isinstance(ret, dict) ret = get_node('abcd', 'sub', 'xyz', data, 'abc', 'azureStructure') assert True == isinstance(ret, dict) assert {} == ret['json'] def test_populate_azure_snapshot(monkeypatch): monkeypatch.setattr('processor.connector.snapshot_azure.http_get_request', mock_http_get_request_happy) monkeypatch.setattr('processor.connector.snapshot_azure.get_access_token', mock_get_access_token) monkeypatch.setattr('processor.connector.snapshot_azure.insert_one_document', mock_insert_one_document) monkeypatch.setattr('processor.connector.snapshot_azure.get_web_client_data', mock1_get_web_client_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_vault_data', mock_get_vault_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_client_secret', mock_get_client_secret) monkeypatch.setattr('processor.connector.snapshot_azure.get_collection_size', mock_get_collection_size) from processor.connector.snapshot_azure import populate_azure_snapshot val = populate_azure_snapshot(snapshot, 'azure') assert val == {'1': True} snapshot['testUser'] = 'abcd' with pytest.raises(Exception): populate_azure_snapshot(snapshot, 'azure') # val = populate_azure_snapshot(snapshot, 'azure') # assert val == {'1': False} def test_populate_azure_snapshot_invalid_token(monkeypatch): monkeypatch.setattr('processor.connector.snapshot_azure.http_get_request', mock_http_get_request_happy) monkeypatch.setattr('processor.connector.snapshot_azure.get_access_token', mock_empty_get_access_token) monkeypatch.setattr('processor.connector.snapshot_azure.insert_one_document', mock_insert_one_document) monkeypatch.setattr('processor.connector.snapshot_azure.get_vault_data', mock_get_vault_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_client_secret', mock_get_client_secret) monkeypatch.setattr('processor.connector.snapshot_azure.get_collection_size', mock_get_collection_size) from processor.connector.snapshot_azure import populate_azure_snapshot snapshot[""testUser""] = ""ajeybk1@kbajeygmail.onmicrosoft.com"" with pytest.raises(Exception): populate_azure_snapshot(snapshot, 'azure') # val = populate_azure_snapshot(snapshot, 'azure') # assert val == {'1': False} def test_populate_azure_snapshot_invalid_secret(monkeypatch): monkeypatch.setattr('processor.connector.snapshot_azure.http_get_request', mock_http_get_request_happy) monkeypatch.setattr('processor.connector.snapshot_azure.get_access_token', mock_get_access_token) monkeypatch.setattr('processor.connector.snapshot_azure.insert_one_document', mock_insert_one_document) monkeypatch.setattr('processor.connector.snapshot_azure.get_web_client_data', mock_get_web_client_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_vault_data', mock_get_vault_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_client_secret', mock_get_client_secret) from processor.connector.snapshot_azure import populate_azure_snapshot snapshot[""testUser""] = ""ajeybk1@kbajeygmail.onmicrosoft.com"" with pytest.raises(Exception): populate_azure_snapshot(snapshot, 'azure') # val = populate_azure_snapshot(snapshot, 'azure') # assert val == {'1': False} def test_populate_azure_snapshot_crawler(monkeypatch): monkeypatch.setattr('processor.connector.snapshot_azure.http_get_request', mock_http_get_crawler_request) monkeypatch.setattr('processor.connector.snapshot_azure.get_access_token', mock_get_access_token) monkeypatch.setattr('processor.connector.snapshot_azure.insert_one_document', mock_insert_one_document) monkeypatch.setattr('processor.connector.snapshot_azure.get_web_client_data', mock1_get_web_client_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_vault_data', mock_get_vault_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_client_secret', mock_get_client_secret) monkeypatch.setattr('processor.connector.snapshot_azure.get_from_currentdata', mock_get_from_currentdata) from processor.connector.snapshot_azure import populate_azure_snapshot val = populate_azure_snapshot(snapshot_crawler, 'azure') assert val == {'31': [{'masterSnapshotId': ['31'], 'path': 'test_id', 'snapshotId': '310', 'status': 'active', 'validate': True}]} ", https://github.com/prancer-io/cloud-validation-framework,94522590d9881a5125d81da701a65f7f63383c0d,tests/processor/connector/test_snapshot_azure.py::test_get_version_for_type,tests/processor/connector/test_snapshot_azure.py,NOD,flaky,nan,nan,classify;root_cause,""""""" Tests for snapshot azure"""""" import pytest snapshot = { ""source"": ""azureStructure.json"", ""testUser"": ""ajeybk1@kbajeygmail.onmicrosoft.com"", ""subscriptionId"": ""37f11aaf-0b72-44ef-a173-308e990279da"", ""nodes"": [ { ""snapshotId"": ""1"", ""type"": ""Microsoft.Compute/availabilitySets"", ""collection"": ""Microsoft.Compute"", ""path"":""/resourceGroups/mno-nonprod-shared-cet-eastus2-networkWatcher/providers/Microsoft.Compute/availabilitySets/mno-nonprod-shared-cet-eastus2-tab-as03"" } ] } snapshot_crawler = { ""source"": ""azureStructure1"", ""type"": ""azure"", ""testUser"": ""ajey.khanapuri@liquware.com"", ""subscriptionId"": [ ""d34d6141-7a19-4458-b0dd-f038bb7760c1"" ], ""nodes"": [ { ""masterSnapshotId"": ""31"", ""type"": ""Microsoft.Compute/availabilitySets"", ""collection"": ""Microsoft.Compute"" } ] } def mock_insert_one_document(doc, collection, dbname, check_keys=True): pass def mock_get_access_token(): return 'clientsecret' def mock_empty_get_access_token(): return None def mock_get_vault_data(client_id=None): return None def mock_get_web_client_data(snapshot_type, snapshot_source, snapshot_user): return 'client_id', None, 'sub_name', 'sub_id', 'tenant_id' def mock1_get_web_client_data(snapshot_type, snapshot_source, snapshot_user): if snapshot_user =='abcd': return 'client_id', None, 'sub_name', 'sub_id', 'tenant_id' return 'client_id', 'client_secret', 'sub_name', 'sub_id', 'tenant_id' def mock_http_get_request_happy(url, headers=None): data = {'a': 'b'} return 200, data def mock_http_get_crawler_request(url, headers=None): data = {'value': [ { 'id': 'test_id', 'type': 'Microsoft.Compute/availabilitySets' } ] } return 200, data def mock_get_from_currentdata(name): return None def mock_http_get_request_error(url, headers=None): data = {'a': 'b'} return 400, data def mock_get_client_secret(): return None def mock_db_json_source(): return True def mock_fs_json_source(): return False def mock_get_collection_size(collection_name): return 100 def mock_config_value(section, key, default=None): if key == 'structure': return 'structure' elif key == 'dbname': return 'dbname' return 'pytestdb' def mock_api_version_get_document(collection, dbname=None, sort=None, query=None, limit=1): return [{ ""json"": { ""fileType"": ""structure"", ""type"": ""others"", ""Microsoft.RecoveryServices/locations/backupStatus"": { ""version"": ""2017-07-01"" } } }] def test_get_version_for_type(monkeypatch): from processor.connector.snapshot_azure import get_version_for_type assert None == get_version_for_type({}) assert '2019-09-01' == get_version_for_type({'type': 'Microsoft.Network/virtualNetworks'}) monkeypatch.setattr('processor.connector.snapshot_azure.json_source', mock_db_json_source) monkeypatch.setattr('processor.connector.snapshot_azure.config_value', mock_config_value) monkeypatch.setattr('processor.connector.snapshot_azure.get_documents', mock_api_version_get_document) assert '2017-07-01' == get_version_for_type({'type': 'Microsoft.RecoveryServices/locations/backupStatus'}) def test_get_node_happy(monkeypatch): monkeypatch.setattr('processor.connector.snapshot_azure.http_get_request', mock_http_get_request_happy) from processor.connector.snapshot_azure import get_node data = { 'type': 'Microsoft.Network/virtualNetworks', 'snapshotId': '1', 'path': ""/resourceGroups/mno-nonprod-shared-cet-eastus2-networkWatcher/providers/"" ""Microsoft.Compute/availabilitySets/mno-nonprod-shared-cet-eastus2-tab-as03"" } ret = get_node(None, None, None, data, 'abc', 'azureStructure') assert True == isinstance(ret, dict) ret = get_node('abcd', 'devtest', 'xyz', data, 'abc', 'azureStructure') assert True == isinstance(ret, dict) assert {'a': 'b'} == ret['json'] def test_get_node_error(monkeypatch): monkeypatch.setattr('processor.connector.snapshot_azure.http_get_request', mock_http_get_request_error) from processor.connector.snapshot_azure import get_node data = { 'type': 'Microsoft.Network/virtualNetworks', 'snapshotId': '1', 'path': ""/resourceGroups/mno-nonprod-shared-cet-eastus2-networkWatcher/providers/"" ""Microsoft.Compute/availabilitySets/mno-nonprod-shared-cet-eastus2-tab-as03"" } ret = get_node(None, None, None, data, 'abc', 'azureStructure') assert True == isinstance(ret, dict) ret = get_node('abcd', 'sub', 'xyz', data, 'abc', 'azureStructure') assert True == isinstance(ret, dict) assert {} == ret['json'] def test_populate_azure_snapshot(monkeypatch): monkeypatch.setattr('processor.connector.snapshot_azure.http_get_request', mock_http_get_request_happy) monkeypatch.setattr('processor.connector.snapshot_azure.get_access_token', mock_get_access_token) monkeypatch.setattr('processor.connector.snapshot_azure.insert_one_document', mock_insert_one_document) monkeypatch.setattr('processor.connector.snapshot_azure.get_web_client_data', mock1_get_web_client_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_vault_data', mock_get_vault_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_client_secret', mock_get_client_secret) monkeypatch.setattr('processor.connector.snapshot_azure.get_collection_size', mock_get_collection_size) from processor.connector.snapshot_azure import populate_azure_snapshot val = populate_azure_snapshot(snapshot, 'azure') assert val == {'1': True} snapshot['testUser'] = 'abcd' with pytest.raises(Exception): populate_azure_snapshot(snapshot, 'azure') # val = populate_azure_snapshot(snapshot, 'azure') # assert val == {'1': False} def test_populate_azure_snapshot_invalid_token(monkeypatch): monkeypatch.setattr('processor.connector.snapshot_azure.http_get_request', mock_http_get_request_happy) monkeypatch.setattr('processor.connector.snapshot_azure.get_access_token', mock_empty_get_access_token) monkeypatch.setattr('processor.connector.snapshot_azure.insert_one_document', mock_insert_one_document) monkeypatch.setattr('processor.connector.snapshot_azure.get_vault_data', mock_get_vault_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_client_secret', mock_get_client_secret) monkeypatch.setattr('processor.connector.snapshot_azure.get_collection_size', mock_get_collection_size) from processor.connector.snapshot_azure import populate_azure_snapshot snapshot[""testUser""] = ""ajeybk1@kbajeygmail.onmicrosoft.com"" with pytest.raises(Exception): populate_azure_snapshot(snapshot, 'azure') # val = populate_azure_snapshot(snapshot, 'azure') # assert val == {'1': False} def test_populate_azure_snapshot_invalid_secret(monkeypatch): monkeypatch.setattr('processor.connector.snapshot_azure.http_get_request', mock_http_get_request_happy) monkeypatch.setattr('processor.connector.snapshot_azure.get_access_token', mock_get_access_token) monkeypatch.setattr('processor.connector.snapshot_azure.insert_one_document', mock_insert_one_document) monkeypatch.setattr('processor.connector.snapshot_azure.get_web_client_data', mock_get_web_client_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_vault_data', mock_get_vault_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_client_secret', mock_get_client_secret) from processor.connector.snapshot_azure import populate_azure_snapshot snapshot[""testUser""] = ""ajeybk1@kbajeygmail.onmicrosoft.com"" with pytest.raises(Exception): populate_azure_snapshot(snapshot, 'azure') # val = populate_azure_snapshot(snapshot, 'azure') # assert val == {'1': False} def test_populate_azure_snapshot_crawler(monkeypatch): monkeypatch.setattr('processor.connector.snapshot_azure.http_get_request', mock_http_get_crawler_request) monkeypatch.setattr('processor.connector.snapshot_azure.get_access_token', mock_get_access_token) monkeypatch.setattr('processor.connector.snapshot_azure.insert_one_document', mock_insert_one_document) monkeypatch.setattr('processor.connector.snapshot_azure.get_web_client_data', mock1_get_web_client_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_vault_data', mock_get_vault_data) monkeypatch.setattr('processor.connector.snapshot_azure.get_client_secret', mock_get_client_secret) monkeypatch.setattr('processor.connector.snapshot_azure.get_from_currentdata', mock_get_from_currentdata) from processor.connector.snapshot_azure import populate_azure_snapshot val = populate_azure_snapshot(snapshot_crawler, 'azure') assert val == {'31': [{'masterSnapshotId': ['31'], 'path': 'test_id', 'snapshotId': '310', 'status': 'active', 'validate': True}]} ", https://github.com/prancer-io/cloud-validation-framework,94522590d9881a5125d81da701a65f7f63383c0d,tests/processor/helper/config/test_config_utils.py::test_framework_config,tests/processor/helper/config/test_config_utils.py,NOD,flaky,nan,nan,classify;root_cause,"import os import tempfile import configparser from processor.helper.config.config_utils import framework_dir, framework_config from processor.helper.config.config_utils import get_config_data,\ config_value, get_test_json_dir, framework_currentdata TESTSDIR = None def set_tests_dir(): global TESTSDIR if TESTSDIR: return TESTSDIR MYDIR = os.path.abspath(os.path.dirname(__file__)) TESTSDIR = os.getenv('FRAMEWORKDIR', os.path.join(MYDIR, '../../../../')) return TESTSDIR set_tests_dir() def test_framework_dir(): os.chdir(TESTSDIR) tests_curdir = os.getcwd() fw_dir = framework_dir() os.chdir(fw_dir) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir def test_framework_config(): configini = '%s/config.ini' % TESTSDIR with open(configini) as f: tests_configini = f.read() configfile = framework_config() with open(configfile) as f: prod_configini = f.read() assert tests_configini == prod_configini def test_rundata_dir(): tests_rundata = '%s/rundata' % TESTSDIR rundata_exists = os.path.exists(tests_rundata) and os.path.isdir(tests_rundata) # assert rundata_exists == False def test_get_solution_dir(): os.chdir(TESTSDIR) tests_curdir = os.getcwd() os.chdir(framework_dir()) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir def test_get_config_data(): configdata = get_config_data(None) assert configdata is None configdata = get_config_data('/tmp/asdxz.ini') assert configdata is None tests_configini = '%s/config.ini' % TESTSDIR tests_configdata = get_config_data(tests_configini) assert tests_configdata is not None prod_configdata = get_config_data(framework_config()) assert prod_configdata is not None assert tests_configdata == prod_configdata def test_config_value(): config = configparser.ConfigParser() config['DEFAULT'] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} config['bitbucket.org'] = {} config['bitbucket.org']['User'] = 'hg' config['topsecret.server.com'] = {} topsecret = config['topsecret.server.com'] topsecret['Port'] = '50022' # mutates the parser topsecret['ForwardX11'] = 'no' # same here config['DEFAULT']['ForwardX11'] = 'yes' newpath = tempfile.mkdtemp() configini = '%s/example.ini' % newpath with open(configini, 'w') as configfile: config.write(configfile) assert configini is not None value = config_value('DEFAULT', 'Expansion', configfile='/tmp/axdfe.ini') assert value is None value = config_value('DEFAULT1', 'Expansion', configfile=configini) assert value is None value = config_value('DEFAULT', 'Compression', configfile=configini) assert value == 'yes' value = config_value('DEFAULT', 'Expansion', configfile=configini) assert value is None value = config_value('DEFAULT', 'Expansion', configfile=configini, default='no') assert value == 'no' value = config_value('DEFAULT', 'Expansion', configfile='/tmp/axdfe.ini', default='no') assert value == 'no' value = config_value('bitbucket.org', 'User', configfile=configini, default='no') test_value = 'hg' assert test_value == value def test_get_test_json_dir(): test_dir = os.getenv('TESTDIR', '/realm/validation/') val_dir = '%s/%s' % (TESTSDIR, test_dir) os.chdir(val_dir) tests_curdir = os.getcwd() os.chdir(get_test_json_dir()) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir ", https://github.com/prancer-io/cloud-validation-framework,94522590d9881a5125d81da701a65f7f63383c0d,tests/processor/helper/config/test_config_utils.py::test_framework_dir,tests/processor/helper/config/test_config_utils.py,NOD,flaky,nan,nan,classify;root_cause,"import os import tempfile import configparser from processor.helper.config.config_utils import framework_dir, framework_config from processor.helper.config.config_utils import get_config_data,\ config_value, get_test_json_dir, framework_currentdata TESTSDIR = None def set_tests_dir(): global TESTSDIR if TESTSDIR: return TESTSDIR MYDIR = os.path.abspath(os.path.dirname(__file__)) TESTSDIR = os.getenv('FRAMEWORKDIR', os.path.join(MYDIR, '../../../../')) return TESTSDIR set_tests_dir() def test_framework_dir(): os.chdir(TESTSDIR) tests_curdir = os.getcwd() fw_dir = framework_dir() os.chdir(fw_dir) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir def test_framework_config(): configini = '%s/config.ini' % TESTSDIR with open(configini) as f: tests_configini = f.read() configfile = framework_config() with open(configfile) as f: prod_configini = f.read() assert tests_configini == prod_configini def test_rundata_dir(): tests_rundata = '%s/rundata' % TESTSDIR rundata_exists = os.path.exists(tests_rundata) and os.path.isdir(tests_rundata) # assert rundata_exists == False def test_get_solution_dir(): os.chdir(TESTSDIR) tests_curdir = os.getcwd() os.chdir(framework_dir()) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir def test_get_config_data(): configdata = get_config_data(None) assert configdata is None configdata = get_config_data('/tmp/asdxz.ini') assert configdata is None tests_configini = '%s/config.ini' % TESTSDIR tests_configdata = get_config_data(tests_configini) assert tests_configdata is not None prod_configdata = get_config_data(framework_config()) assert prod_configdata is not None assert tests_configdata == prod_configdata def test_config_value(): config = configparser.ConfigParser() config['DEFAULT'] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} config['bitbucket.org'] = {} config['bitbucket.org']['User'] = 'hg' config['topsecret.server.com'] = {} topsecret = config['topsecret.server.com'] topsecret['Port'] = '50022' # mutates the parser topsecret['ForwardX11'] = 'no' # same here config['DEFAULT']['ForwardX11'] = 'yes' newpath = tempfile.mkdtemp() configini = '%s/example.ini' % newpath with open(configini, 'w') as configfile: config.write(configfile) assert configini is not None value = config_value('DEFAULT', 'Expansion', configfile='/tmp/axdfe.ini') assert value is None value = config_value('DEFAULT1', 'Expansion', configfile=configini) assert value is None value = config_value('DEFAULT', 'Compression', configfile=configini) assert value == 'yes' value = config_value('DEFAULT', 'Expansion', configfile=configini) assert value is None value = config_value('DEFAULT', 'Expansion', configfile=configini, default='no') assert value == 'no' value = config_value('DEFAULT', 'Expansion', configfile='/tmp/axdfe.ini', default='no') assert value == 'no' value = config_value('bitbucket.org', 'User', configfile=configini, default='no') test_value = 'hg' assert test_value == value def test_get_test_json_dir(): test_dir = os.getenv('TESTDIR', '/realm/validation/') val_dir = '%s/%s' % (TESTSDIR, test_dir) os.chdir(val_dir) tests_curdir = os.getcwd() os.chdir(get_test_json_dir()) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir ", https://github.com/prancer-io/cloud-validation-framework,94522590d9881a5125d81da701a65f7f63383c0d,tests/processor/helper/config/test_config_utils.py::test_get_config_data,tests/processor/helper/config/test_config_utils.py,NOD,flaky,nan,nan,classify;root_cause,"import os import tempfile import configparser from processor.helper.config.config_utils import framework_dir, framework_config from processor.helper.config.config_utils import get_config_data,\ config_value, get_test_json_dir, framework_currentdata TESTSDIR = None def set_tests_dir(): global TESTSDIR if TESTSDIR: return TESTSDIR MYDIR = os.path.abspath(os.path.dirname(__file__)) TESTSDIR = os.getenv('FRAMEWORKDIR', os.path.join(MYDIR, '../../../../')) return TESTSDIR set_tests_dir() def test_framework_dir(): os.chdir(TESTSDIR) tests_curdir = os.getcwd() fw_dir = framework_dir() os.chdir(fw_dir) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir def test_framework_config(): configini = '%s/config.ini' % TESTSDIR with open(configini) as f: tests_configini = f.read() configfile = framework_config() with open(configfile) as f: prod_configini = f.read() assert tests_configini == prod_configini def test_rundata_dir(): tests_rundata = '%s/rundata' % TESTSDIR rundata_exists = os.path.exists(tests_rundata) and os.path.isdir(tests_rundata) # assert rundata_exists == False def test_get_solution_dir(): os.chdir(TESTSDIR) tests_curdir = os.getcwd() os.chdir(framework_dir()) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir def test_get_config_data(): configdata = get_config_data(None) assert configdata is None configdata = get_config_data('/tmp/asdxz.ini') assert configdata is None tests_configini = '%s/config.ini' % TESTSDIR tests_configdata = get_config_data(tests_configini) assert tests_configdata is not None prod_configdata = get_config_data(framework_config()) assert prod_configdata is not None assert tests_configdata == prod_configdata def test_config_value(): config = configparser.ConfigParser() config['DEFAULT'] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} config['bitbucket.org'] = {} config['bitbucket.org']['User'] = 'hg' config['topsecret.server.com'] = {} topsecret = config['topsecret.server.com'] topsecret['Port'] = '50022' # mutates the parser topsecret['ForwardX11'] = 'no' # same here config['DEFAULT']['ForwardX11'] = 'yes' newpath = tempfile.mkdtemp() configini = '%s/example.ini' % newpath with open(configini, 'w') as configfile: config.write(configfile) assert configini is not None value = config_value('DEFAULT', 'Expansion', configfile='/tmp/axdfe.ini') assert value is None value = config_value('DEFAULT1', 'Expansion', configfile=configini) assert value is None value = config_value('DEFAULT', 'Compression', configfile=configini) assert value == 'yes' value = config_value('DEFAULT', 'Expansion', configfile=configini) assert value is None value = config_value('DEFAULT', 'Expansion', configfile=configini, default='no') assert value == 'no' value = config_value('DEFAULT', 'Expansion', configfile='/tmp/axdfe.ini', default='no') assert value == 'no' value = config_value('bitbucket.org', 'User', configfile=configini, default='no') test_value = 'hg' assert test_value == value def test_get_test_json_dir(): test_dir = os.getenv('TESTDIR', '/realm/validation/') val_dir = '%s/%s' % (TESTSDIR, test_dir) os.chdir(val_dir) tests_curdir = os.getcwd() os.chdir(get_test_json_dir()) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir ", https://github.com/prancer-io/cloud-validation-framework,94522590d9881a5125d81da701a65f7f63383c0d,tests/processor/helper/config/test_config_utils.py::test_get_solution_dir,tests/processor/helper/config/test_config_utils.py,NOD,flaky,nan,nan,classify;root_cause,"import os import tempfile import configparser from processor.helper.config.config_utils import framework_dir, framework_config from processor.helper.config.config_utils import get_config_data,\ config_value, get_test_json_dir, framework_currentdata TESTSDIR = None def set_tests_dir(): global TESTSDIR if TESTSDIR: return TESTSDIR MYDIR = os.path.abspath(os.path.dirname(__file__)) TESTSDIR = os.getenv('FRAMEWORKDIR', os.path.join(MYDIR, '../../../../')) return TESTSDIR set_tests_dir() def test_framework_dir(): os.chdir(TESTSDIR) tests_curdir = os.getcwd() fw_dir = framework_dir() os.chdir(fw_dir) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir def test_framework_config(): configini = '%s/config.ini' % TESTSDIR with open(configini) as f: tests_configini = f.read() configfile = framework_config() with open(configfile) as f: prod_configini = f.read() assert tests_configini == prod_configini def test_rundata_dir(): tests_rundata = '%s/rundata' % TESTSDIR rundata_exists = os.path.exists(tests_rundata) and os.path.isdir(tests_rundata) # assert rundata_exists == False def test_get_solution_dir(): os.chdir(TESTSDIR) tests_curdir = os.getcwd() os.chdir(framework_dir()) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir def test_get_config_data(): configdata = get_config_data(None) assert configdata is None configdata = get_config_data('/tmp/asdxz.ini') assert configdata is None tests_configini = '%s/config.ini' % TESTSDIR tests_configdata = get_config_data(tests_configini) assert tests_configdata is not None prod_configdata = get_config_data(framework_config()) assert prod_configdata is not None assert tests_configdata == prod_configdata def test_config_value(): config = configparser.ConfigParser() config['DEFAULT'] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} config['bitbucket.org'] = {} config['bitbucket.org']['User'] = 'hg' config['topsecret.server.com'] = {} topsecret = config['topsecret.server.com'] topsecret['Port'] = '50022' # mutates the parser topsecret['ForwardX11'] = 'no' # same here config['DEFAULT']['ForwardX11'] = 'yes' newpath = tempfile.mkdtemp() configini = '%s/example.ini' % newpath with open(configini, 'w') as configfile: config.write(configfile) assert configini is not None value = config_value('DEFAULT', 'Expansion', configfile='/tmp/axdfe.ini') assert value is None value = config_value('DEFAULT1', 'Expansion', configfile=configini) assert value is None value = config_value('DEFAULT', 'Compression', configfile=configini) assert value == 'yes' value = config_value('DEFAULT', 'Expansion', configfile=configini) assert value is None value = config_value('DEFAULT', 'Expansion', configfile=configini, default='no') assert value == 'no' value = config_value('DEFAULT', 'Expansion', configfile='/tmp/axdfe.ini', default='no') assert value == 'no' value = config_value('bitbucket.org', 'User', configfile=configini, default='no') test_value = 'hg' assert test_value == value def test_get_test_json_dir(): test_dir = os.getenv('TESTDIR', '/realm/validation/') val_dir = '%s/%s' % (TESTSDIR, test_dir) os.chdir(val_dir) tests_curdir = os.getcwd() os.chdir(get_test_json_dir()) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir ", https://github.com/prancer-io/cloud-validation-framework,94522590d9881a5125d81da701a65f7f63383c0d,tests/processor/helper/config/test_config_utils.py::test_get_test_json_dir,tests/processor/helper/config/test_config_utils.py,NOD,flaky,nan,nan,classify;root_cause,"import os import tempfile import configparser from processor.helper.config.config_utils import framework_dir, framework_config from processor.helper.config.config_utils import get_config_data,\ config_value, get_test_json_dir, framework_currentdata TESTSDIR = None def set_tests_dir(): global TESTSDIR if TESTSDIR: return TESTSDIR MYDIR = os.path.abspath(os.path.dirname(__file__)) TESTSDIR = os.getenv('FRAMEWORKDIR', os.path.join(MYDIR, '../../../../')) return TESTSDIR set_tests_dir() def test_framework_dir(): os.chdir(TESTSDIR) tests_curdir = os.getcwd() fw_dir = framework_dir() os.chdir(fw_dir) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir def test_framework_config(): configini = '%s/config.ini' % TESTSDIR with open(configini) as f: tests_configini = f.read() configfile = framework_config() with open(configfile) as f: prod_configini = f.read() assert tests_configini == prod_configini def test_rundata_dir(): tests_rundata = '%s/rundata' % TESTSDIR rundata_exists = os.path.exists(tests_rundata) and os.path.isdir(tests_rundata) # assert rundata_exists == False def test_get_solution_dir(): os.chdir(TESTSDIR) tests_curdir = os.getcwd() os.chdir(framework_dir()) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir def test_get_config_data(): configdata = get_config_data(None) assert configdata is None configdata = get_config_data('/tmp/asdxz.ini') assert configdata is None tests_configini = '%s/config.ini' % TESTSDIR tests_configdata = get_config_data(tests_configini) assert tests_configdata is not None prod_configdata = get_config_data(framework_config()) assert prod_configdata is not None assert tests_configdata == prod_configdata def test_config_value(): config = configparser.ConfigParser() config['DEFAULT'] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} config['bitbucket.org'] = {} config['bitbucket.org']['User'] = 'hg' config['topsecret.server.com'] = {} topsecret = config['topsecret.server.com'] topsecret['Port'] = '50022' # mutates the parser topsecret['ForwardX11'] = 'no' # same here config['DEFAULT']['ForwardX11'] = 'yes' newpath = tempfile.mkdtemp() configini = '%s/example.ini' % newpath with open(configini, 'w') as configfile: config.write(configfile) assert configini is not None value = config_value('DEFAULT', 'Expansion', configfile='/tmp/axdfe.ini') assert value is None value = config_value('DEFAULT1', 'Expansion', configfile=configini) assert value is None value = config_value('DEFAULT', 'Compression', configfile=configini) assert value == 'yes' value = config_value('DEFAULT', 'Expansion', configfile=configini) assert value is None value = config_value('DEFAULT', 'Expansion', configfile=configini, default='no') assert value == 'no' value = config_value('DEFAULT', 'Expansion', configfile='/tmp/axdfe.ini', default='no') assert value == 'no' value = config_value('bitbucket.org', 'User', configfile=configini, default='no') test_value = 'hg' assert test_value == value def test_get_test_json_dir(): test_dir = os.getenv('TESTDIR', '/realm/validation/') val_dir = '%s/%s' % (TESTSDIR, test_dir) os.chdir(val_dir) tests_curdir = os.getcwd() os.chdir(get_test_json_dir()) prod_curdir = os.getcwd() assert tests_curdir == prod_curdir ", https://github.com/prancer-io/cloud-validation-framework,94522590d9881a5125d81da701a65f7f63383c0d,tests/processor/helper/config/test_rundata_utils.py::test_init_config,tests/processor/helper/config/test_rundata_utils.py,NOD,flaky,nan,nan,classify;root_cause,"import os import shutil from processor.helper.config.rundata_utils import init_currentdata,\ put_in_currentdata, delete_from_currentdata, delete_currentdata from processor.helper.config.config_utils import framework_currentdata TESTSDIR = None def set_tests_dir(): global TESTSDIR if TESTSDIR: return TESTSDIR MYDIR = os.path.abspath(os.path.dirname(__file__)) TESTSDIR = os.getenv('FRAMEWORKDIR', os.path.join(MYDIR, '../../../../')) return TESTSDIR set_tests_dir() def test_init_config(): runcfg = framework_currentdata() rundir = os.path.dirname(runcfg) # if os.path.exists(rundir): # shutil.rmtree(rundir) # assert False == os.path.exists(rundir) assert True == os.path.exists(runcfg) init_currentdata() assert True == os.path.exists(rundir) assert True == os.path.exists(runcfg) os.remove(runcfg) assert True == os.path.exists(rundir) assert False == os.path.exists(runcfg) init_currentdata() assert True == os.path.exists(rundir) assert True == os.path.exists(runcfg) def test_add_to_run_config(load_json_file): runcfg = framework_currentdata() init_currentdata() assert True == os.path.exists(runcfg) put_in_currentdata('a', 'val1') runconfig = load_json_file(runcfg) result = True if runconfig and 'a' in runconfig and runconfig['a'] == 'val1' else False assert result == True put_in_currentdata('b', ['val1']) runconfig = load_json_file(runcfg) result = True if runconfig and 'b' in runconfig and runconfig['b'] == ['val1'] else False assert result == True put_in_currentdata('b', 'val2') runconfig = load_json_file(runcfg) result = True if runconfig and 'b' in runconfig and runconfig['b'] == ['val1', 'val2'] else False assert result == True def test_delete_from_run_config(load_json_file): runcfg = framework_currentdata() init_currentdata() assert True == os.path.exists(runcfg) put_in_currentdata('a', 'val1') runconfig = load_json_file(runcfg) result = True if runconfig and 'a' in runconfig and runconfig['a'] == 'val1' else False assert result == True delete_from_currentdata('a') runconfig = load_json_file(runcfg) result = False if runconfig and 'a' in runconfig else True assert result == True def test_delete_run_config(): runcfg = framework_currentdata() init_currentdata() assert True == os.path.exists(runcfg) put_in_currentdata('token', 'abcd') delete_currentdata() assert False == os.path.exists(runcfg) ", https://github.com/prancer-io/cloud-validation-framework,94522590d9881a5125d81da701a65f7f63383c0d,tests/processor/helper/httpapi/test_restapi_azure.py::test_web_client_data,tests/processor/helper/httpapi/test_restapi_azure.py,NOD,flaky,nan,nan,classify;root_cause,"import os def mock_config_value(key, default=None): return 'pytestdb' def mock_getenv(key, default=None): return 'clientSecret' def mock_empty_getenv(key, default=None): return default def mock_input(text): return 'clientSecret' def mock_valid_http_post_request(url, data, headers={}, json_type=False): return 200, {'access_token': 'abcd', 'expires_on': '1234'} def mock_invalid_http_post_request(url, data, headers={}, json_type=False): return 401, {'access_token': None} def mock_valid_http_get_request(url, headers=None, name='GET'): return 200, {'access_token': 'abcd'} def mock_invalid_http_get_request(url, headers=None, name='GET'): return 401, {'access_token': None} def mock_valid_http_delete_request(url, headers=None, name='GET'): return 200, {'access_token': 'abcd'} def mock_invalid_http_delete_request(url, headers=None, name='GET'): return 401, {'access_token': None} def mock_valid_http_put_request(url, request_data, headers=None, json_type=True, name='PUT'): return 200, {'hello': 'world'} def mock_invalid_http_put_request(url, request_data, headers=None, json_type=True, name='PUT'): return 401, {'hello': 'world'} def mock_all_keys_http_request(url, headers=None, name='GET'): data = { ""value"" : [{ ""id"": ""https://%s.vault.azure.net/secrets/hello"" },{ ""id"": ""https://%s.vault.azure.net/secrets/hello2"" }] } return 200, data def mock_exception_all_keys_http_request(url, headers=None, name='GET'): return 401, {'access_token': None} def mock_get_from_currentdata(key): if key == 'subscriptionId': return 'subscriptionId' elif key == 'tenant_id': return 'tenant_id' elif key == 'clientId': return 'clientId' elif key == 'rg': return 'rg' elif key == 'vaultClientSecret': return 'vaultClientSecret' elif key == 'jsonsource': return False else: return None def mock_empty_get_from_currentdata(key): if key == 'subscriptionId': return 'subscriptionId' elif key == 'tenant_id': return 'tenant_id' else: return None def mock_get_documents(collection, query=None, dbname=None, sort=None, limit=10): return [{ ""_id"": ""5c24af787456217c485ad1e6"", ""checksum"": ""7d814f2f82a32ea91ef37de9e11d0486"", ""collection"": ""microsoftcompute"", ""json"":{ ""id"": 124, ""location"": ""eastus2"", ""name"": ""mno-nonprod-shared-cet-eastus2-tab-as03"" }, ""queryuser"": ""ajeybk1@kbajeygmail.onmicrosoft.com"", ""snapshotId"": 1, ""timestamp"": 1545908086831 }] def mock_json_source(): return True def test_get_azure_functions(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.get_from_currentdata', mock_get_from_currentdata) monkeypatch.setattr(os, 'getenv', mock_getenv) monkeypatch.setattr('processor.helper.httpapi.restapi_azure.input', mock_input) from processor.helper.httpapi.restapi_azure import get_subscription_id,\ get_tenant_id, get_client_id, get_resource_group, get_client_secret assert 'subscriptionId' == get_subscription_id() assert 'tenant_id' == get_tenant_id() assert 'clientId' == get_client_id() assert 'rg' == get_resource_group() assert 'clientSecret' == get_client_secret() def test_web_client_data(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.get_from_currentdata', mock_get_from_currentdata) monkeypatch.setattr(os, 'getenv', mock_empty_getenv) monkeypatch.setattr('processor.helper.httpapi.restapi_azure.input', mock_input) from processor.helper.httpapi.restapi_azure import get_client_secret, get_web_client_data assert 'clientSecret' == get_client_secret() client_id, client_secret, sub_name, sub_id, tenant_id = \ get_web_client_data('azure', 'azureConnector.json', '') assert client_id is not None assert client_secret == '' assert sub_id is not None assert sub_name is not None assert tenant_id is not None def test_get_access_token(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.get_from_currentdata', mock_get_from_currentdata) monkeypatch.setattr(os, 'getenv', mock_getenv) monkeypatch.setattr('processor.helper.httpapi.restapi_azure.http_post_request', mock_valid_http_post_request) from processor.helper.httpapi.restapi_azure import get_access_token val = get_access_token() assert val == 'abcd' def test_none_get_access_token(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.get_from_currentdata', mock_empty_get_from_currentdata) monkeypatch.setattr(os, 'getenv', mock_getenv) monkeypatch.setattr('processor.helper.httpapi.restapi_azure.http_post_request', mock_valid_http_post_request) from processor.helper.httpapi.restapi_azure import get_access_token val = get_access_token() assert val is None def test_invalid_http_get_access_token(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.get_from_currentdata', mock_get_from_currentdata) monkeypatch.setattr(os, 'getenv', mock_getenv) monkeypatch.setattr('processor.helper.httpapi.restapi_azure.http_post_request', mock_invalid_http_post_request) from processor.helper.httpapi.restapi_azure import get_access_token val = get_access_token() assert val is None def test_get_azure_data(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.get_documents', mock_get_documents) monkeypatch.setattr('processor.helper.httpapi.restapi_azure.json_source', mock_json_source) monkeypatch.setattr('processor.helper.httpapi.restapi_azure.config_value', mock_config_value) from processor.helper.httpapi.restapi_azure import get_azure_data data = get_azure_data('snapshot') assert data is not None def test_get_vault_client_secret(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.get_from_currentdata', mock_get_from_currentdata) monkeypatch.setattr(os, 'getenv', mock_getenv) from processor.helper.httpapi.restapi_azure import get_vault_client_secret val = get_vault_client_secret() assert val is not None def test_empty_get_vault_client_secret(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.get_from_currentdata', mock_empty_get_from_currentdata) monkeypatch.setattr(os, 'getenv', mock_getenv) from processor.helper.httpapi.restapi_azure import get_vault_client_secret val = get_vault_client_secret() assert val is not None def test_empty_env_get_vault_client_secret(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.get_from_currentdata', mock_empty_get_from_currentdata) monkeypatch.setattr(os, 'getenv', mock_empty_getenv) monkeypatch.setattr('processor.helper.httpapi.restapi_azure.input', mock_input) from processor.helper.httpapi.restapi_azure import get_vault_client_secret val = get_vault_client_secret() assert val is not None def test_get_keyvault_secret(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.http_get_request', mock_valid_http_get_request) from processor.helper.httpapi.restapi_azure import get_keyvault_secret val = get_keyvault_secret('abcdvault', 'abcdsecret', 'vaulttoken') assert val == {'access_token': 'abcd'} def test_invalid_get_keyvault_secret(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.http_get_request', mock_invalid_http_get_request) from processor.helper.httpapi.restapi_azure import get_keyvault_secret val = get_keyvault_secret('abcdvault', 'abcdsecret', 'vaulttoken') assert val == {'access_token': None} def test_get_vault_access_token(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.get_from_currentdata', mock_get_from_currentdata) monkeypatch.setattr(os, 'getenv', mock_getenv) monkeypatch.setattr('processor.helper.httpapi.restapi_azure.http_post_request', mock_valid_http_post_request) from processor.helper.httpapi.restapi_azure import get_vault_access_token val = get_vault_access_token('tenant_id', 'vault_client_id', 'client_secret') assert val == 'abcd' def test_invalid_get_vault_access_token(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.get_from_currentdata', mock_get_from_currentdata) monkeypatch.setattr(os, 'getenv', mock_getenv) monkeypatch.setattr('processor.helper.httpapi.restapi_azure.http_post_request', mock_invalid_http_post_request) from processor.helper.httpapi.restapi_azure import get_vault_access_token val = get_vault_access_token('tenant_id', 'vault_client_id', 'client_secret') assert val is None def test_set_keyvault_secret(monkeypatch): monkeypatch.setattr('processor.helper.httpapi.restapi_azure.http_put_request', mock_valid_http_put_request) from processor.helper.httpapi.restapi_azure import set_keyvault_secret val = set_keyvault_secret('abcdvault', 'vaulttoken', 'key','value') assert val == True monkeypatch.setattr('processor.helper.httpapi.restapi_azure.http_put_request', mock_invalid_http_put_request) val = set_keyvault_secret('abcdvault', 'vaulttoken', 'key','value') assert val == False def test_get_all_secrets(monkeypatch): monkeypatch.setattr('processor.h", https://github.com/prancer-io/cloud-validation-framework,94522590d9881a5125d81da701a65f7f63383c0d,tests/processor/template_processor/base/test_base_template_processor.py::test_populate_template_snapshot_false,tests/processor/template_processor/base/test_base_template_processor.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest node_data = { 'snapshotId' : 'SNAPSHOT_1', 'type': 'deploymentmanager', 'collection': 'deploymentmanager', 'paths': [ 'google_template/template_files/jinja/cloudbuild.yaml' ] } master_node_data = { 'snapshotId' : 'MASTER_SNAPSHOT_1', 'type': 'deploymentmanager', 'collection': 'deploymentmanager', 'paths': [ 'jinja/cloudbuild.yaml' ] } template_processor_kwargs = { 'container': 'google_template', 'snapshot_source': 'fsGoogleConnector', 'repopath': '/tmp/tmp2u5r6vxn', 'connector_data': { 'fileType' : 'structure', 'type' : 'filesystem', 'companyName' : 'prancer-test', 'gitProvider' : 'https://github.com/GoogleCloudPlatform/deploymentmanager-samples.git', 'username' : 'test', 'httpsUser' : 'test', 'branchName' : 'master', 'private' : False }, 'dbname': 'validator', 'snapshot': { 'source': 'base-template-connector', 'nodes' : [ { 'snapshotId': 'SNAPSHOT_1', 'type': 'deploymentmanager', 'collection': 'deploymentmanager', 'paths' : [ 'google_template/template_files/python/cloudbuild.yaml' ], 'status': 'active' } ] }, 'snapshot_data': { 'SNAPSHOT_1': False, } } def mock_get_collection_size(collection_name): return 0 def mock_create_indexes(collection, database, indexes): return True def mock_insert_one_document(doc, collection, dbname, check_keys=False): pass def mock_config_value(section, key, default=None): if key == 'dbname': return 'dbname' return '' def mock_process_template(self, paths): return { ""resources"" : [] } def test_populate_template_snapshot_false(monkeypatch): monkeypatch.setattr('processor.template_processor.base.base_template_processor.config_value', mock_config_value) monkeypatch.setattr('processor.template_processor.base.base_template_processor.get_collection_size', mock_get_collection_size) monkeypatch.setattr('processor.template_processor.base.base_template_processor.create_indexes', mock_create_indexes) monkeypatch.setattr('processor.template_processor.base.base_template_processor.insert_one_document', mock_insert_one_document) from processor.template_processor.base.base_template_processor import TemplateProcessor template_processor = TemplateProcessor(node_data, **template_processor_kwargs) snapshot_data = template_processor.populate_template_snapshot() assert snapshot_data == { 'SNAPSHOT_1': False } def test_populate_template_snapshot_true(monkeypatch): monkeypatch.setattr('processor.template_processor.base.base_template_processor.config_value', mock_config_value) monkeypatch.setattr('processor.template_processor.base.base_template_processor.get_collection_size', mock_get_collection_size) monkeypatch.setattr('processor.template_processor.base.base_template_processor.create_indexes', mock_create_indexes) monkeypatch.setattr('processor.template_processor.base.base_template_processor.insert_one_document', mock_insert_one_document) monkeypatch.setattr('processor.template_processor.base.base_template_processor.TemplateProcessor.process_template', mock_process_template) from processor.template_processor.base.base_template_processor import TemplateProcessor template_processor = TemplateProcessor(node_data, **template_processor_kwargs) snapshot_data = template_processor.populate_template_snapshot() assert snapshot_data == { 'SNAPSHOT_1': True } ", https://github.com/Prodesire/pydu,e6e4055f81dbbece55dccfe29b7fb82b9bf40610,tests/test_iter.py::test_first_last,tests/test_iter.py,OD-Brit,flaky,Opened,https://github.com/Prodesire/pydu/pull/21,classify;root_cause,"import pytest from pydu.iter import first, last, all, any, join @pytest.mark.parametrize( 'iterable', ( [1, 2], (1, 2), {1, 2}, {1: 1, 2: 2}, iter([1, 2]) )) def test_first_last(iterable): assert first(iterable) == 1 assert last(iterable) == 2 def test_all(): assert all([0, 1, 2], lambda x: x+1) assert not all([0, 1, 2], lambda x: x) def test_any(): assert any([-1, -1, 0], lambda x: x+1) assert not any([-1, -1, -1], lambda x: x + 1) def test_join(): assert join(iter([1, '2', 3])) == '123' assert join(iter([1, '2', 3]), separator=',') == '1,2,3' ", https://github.com/Prodesire/pydu,4a1c2f15f7274d5079fb08b02ab1dbe08c0cefef,tests/test_network.py::test_get_free_port,tests/test_network.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import pytest from pydu.network import (dotted_netmask, is_ipv4, is_ipv6, get_free_port, ip2int, int2ip) @pytest.mark.parametrize( 'mask, expected', ( (8, '255.0.0.0'), (24, '255.255.255.0'), (25, '255.255.255.128'), )) def test_dotted_netmask(mask, expected): assert dotted_netmask(mask) == expected class TestIsIPv4Address: def test_valid(self): assert is_ipv4('8.8.8.8') @pytest.mark.parametrize('value', ('8.8.8.8.8', 'localhost.localdomain')) def test_invalid(self, value): assert not is_ipv4(value) class TestIsIPv6Address: def test_valid(self): assert is_ipv6('fe80::9e5b:b149:e187:1a18') @pytest.mark.parametrize('value', ('fe80::9e5b:b149:e187::', 'localhost.localdomain')) def test_invalid(self, value): assert not is_ipv6(value) def test_get_free_port(): port = get_free_port() assert isinstance(port, int) assert 65536 > port > 0 def test_ip2int(): assert ip2int('10.1.1.1') == 167837953 with pytest.raises(ValueError): ip2int('255.255.255.256') assert ip2int('fe80::9e5b:b149:e187:1a18') == 338288524927261089665429805853095434776 with pytest.raises(ValueError): ip2int('fe80::9e5b:b149:e187::') def test_int2ip(): assert int2ip(167837953) == '10.1.1.1' assert int2ip(338288524927261089665429805853095434776) == 'fe80::9e5b:b149:e187:1a18' with pytest.raises(ValueError): int2ip(10**50) ", https://github.com/Prodesire/pydu,4a1c2f15f7274d5079fb08b02ab1dbe08c0cefef,tests/test_request.py::test_check_connect,tests/test_request.py,NIO,flaky,Opened,https://github.com/Prodesire/pydu/pull/19,classify;root_cause,"import socket from .testing import mockserver import pydu.request from pydu.network import get_free_port from pydu.request import FileName, check_connect, update_query_params, cookies_str_to_dict def test_filename_from_url(): url = 'http://www.example.com/test.txt' assert FileName.from_url(url) == 'test.txt' url = 'http://www.example.com/' assert FileName.from_url(url) is None def test_filename_from_headers(): headers = {'Content-Disposition': 'attachment; filename=test.txt'} assert FileName.from_headers(headers) == 'test.txt' headers = [('Content-Disposition', 'attachment; filename=test.txt')] assert FileName.from_headers(headers) == 'test.txt' headers = 'Content-Disposition: attachment; filename=test.txt' assert FileName.from_headers(headers) == 'test.txt' headers = 'Content-Disposition: attachment; filename=abc/test.txt' assert FileName.from_headers(headers) == 'test.txt' headers = '' assert FileName.from_headers(headers) is None headers = 'Content-Disposition: abc' assert FileName.from_headers(headers) is None headers = 'Content-Disposition: abc;' assert FileName.from_headers(headers) is None headers = 'Content-Disposition: attachment; filename=test.txt; filename=test2.txt' assert FileName.from_headers(headers) is None headers = 'Content-Disposition: attachment; filename=' assert FileName.from_headers(headers) is None @mockserver def test_check_connect(port=None): assert check_connect('127.0.0.1', port=port, timeout=0.01) assert not check_connect('127.0.0.1', port=get_free_port(), timeout=0.01) def mock_socket(*args): raise socket.error pydu.request.socket.socket = mock_socket assert not check_connect('127.0.0.1', port=port, timeout=0.01) def test_update_query_params(): base = 'http://example.com/' assert update_query_params(base, {'foo': 1}) == base + '?foo=1' assert update_query_params(base + '?foo=1', {'foo': 2}) == base + '?foo=2' assert update_query_params(base + '?foo=1', {'foo': 2, 'bar': 3}) in \ (base + '?foo=2&bar=3', base + '?bar=3&foo=2') def test_cookies_str_to_dict(): cookies = cookies_str_to_dict('a=a; \tb=b;\nc=c;d;e=') assert cookies['a'] == 'a' assert cookies['b'] == 'b' assert cookies['c'] == 'c' assert cookies['e'] == '' ", https://github.com/Prodesire/pydu,4a1c2f15f7274d5079fb08b02ab1dbe08c0cefef,tests/test_system.py::TestWhich::test_cmd_path,tests/test_system.py,NIO,flaky,Opened,https://github.com/Prodesire/pydu/pull/22,classify;root_cause,"import os import stat import time import pytest from pydu.platform import WINDOWS from pydu.system import (FileTracker, makedirs, remove, removes, open_file, copy, touch, chmod, which) if not WINDOWS: from pydu.system import link, symlink class TestFileTracker: def test_track_open(self, tmpdir): FileTracker.track() path = tmpdir.join('test').strpath f = open(path, 'w') assert f in FileTracker.get_openfiles() f.close() assert f not in FileTracker.get_openfiles() def test_track_context_open(self, tmpdir): FileTracker.track() path = tmpdir.join('test').strpath with open(path, 'w') as f: assert f in FileTracker.get_openfiles() assert f not in FileTracker.get_openfiles() def test_untrack(self, tmpdir): FileTracker.track() FileTracker.untrack() path = tmpdir.join('test').strpath f = open(path, 'w') assert f not in FileTracker.get_openfiles() class TestMakeDirs: def test_makedirs(self, tmpdir): path = str(tmpdir.join('test')) makedirs(path) assert os.path.exists(path) def test_makedirs_with_exists_path(self, tmpdir): path = str(tmpdir.join('test')) makedirs(path) makedirs(path, exist_ok=True) with pytest.raises(Exception): makedirs(path, exist_ok=False) def test_makedirs_with_ignore_error(self, tmpdir): path = str(tmpdir.join('test')) makedirs(path) makedirs(path, ignore_errors=True) def test_makedirs_without_ignore_error(self, tmpdir): path = str(tmpdir.join('test')) makedirs(path) with pytest.raises(Exception): makedirs(path, ignore_errors=False, exist_ok=False) def test_makedirs_with_mutl_dirs(self, tmpdir): path = str(tmpdir.join('test/test')) makedirs(path) assert os.path.exists(path) def test_touch(tmpdir): path = str(tmpdir.join('test')) touch(path) assert os.path.isfile(path) class TestRemove: def test_remove_dir(self, tmpdir): path = str(tmpdir.join('test')) makedirs(path) remove(path) assert not os.path.exists(path) def test_remove_file(self, tmpdir): f = str(tmpdir.join('test.txt')) touch(f) remove(f) assert not os.path.exists(f) def test_remove_mutil_dirs(self, tmpdir): path = str(tmpdir.join('test/test')) makedirs(path) path = str(tmpdir.join('test')) remove(path) assert not os.path.exists(path) def test_remove_with_ignore_error(self, tmpdir): path = str(tmpdir.join('test')) remove(path, ignore_errors=True) def test_remove_without_ignore_error(self, tmpdir): path = str(tmpdir.join('test')) with pytest.raises(Exception): remove(path, ignore_errors=False) def test_remove_without_ignore_error_with_onerror(self): pass class TestRemoves: def test_removes_paths(self, tmpdir): path1 = str(tmpdir.join('test1')) path2 = str(tmpdir.join('test2')) path3 = str(tmpdir.join('test3')) for path in [path1, path2, path3]: makedirs(path) removes([path1, path2, path3]) assert not os.path.exists(path1) assert not os.path.exists(path2) assert not os.path.exists(path3) def test_removes_files(self, tmpdir): f1 = str(tmpdir.join('test1.txt')) f2 = str(tmpdir.join('test2.txt')) f3 = str(tmpdir.join('test3.txt')) for f in [f1, f2, f3]: touch(f) removes([f1, f2, f3]) for f in [f1, f2, f3]: assert not os.path.exists(f) def test_removes_files_and_path(self, tmpdir): f1 = str(tmpdir.join('test1.txt')) f2 = str(tmpdir.join('test2.txt')) p1 = str(tmpdir.join('test1')) p2 = str(tmpdir.join('test2')) for f in [f1, f2]: touch(f) for p in [p1, p2]: makedirs(p) removes([f1, f2, p1, p2]) for f in [f1, f2, p1, p2]: assert not os.path.exists(f) class TestOpenFile: def test_open_file_without_parent_dir(self, tmpdir): f = str(tmpdir.join('test/test1.txt')) open_file(f) assert os.path.exists(f) def test_open_file_in_exist_path(self, tmpdir): f = str(tmpdir.join('test2.txt')) open_file(f) assert os.path.exists(f) def test_open_exist_file(self, tmpdir): f = str(tmpdir.join('test3.txt')) open_file(f) with open(f, 'r') as f: with pytest.raises(Exception): os.remove(f) def test_open_file_with_ignore_error(self, tmpdir): f = str(tmpdir.join('test1.txt')) open_file(f, mode='r', ignore_errors=True) def test_open_file_without_ignore_error(self, tmpdir): f = str(tmpdir.join('test1.txt')) with pytest.raises(Exception): open_file(f, mode='r') @pytest.mark.skipif(WINDOWS, reason='Not support on windows') class TestLink: def test_link_a_file(self, tmpdir): f = str(tmpdir.join('test.txt')) link_f = str(tmpdir.join('test.link')) touch(f) link(f, link_f) assert os.path.exists(link_f) def test_link_with_ignore_error(self, tmpdir): dirname = str(tmpdir.join('test')) link_dirname = str(tmpdir.join('test.link')) makedirs(dirname) link(dirname, link_dirname, ignore_errors=True) def test_link_without_ignore_error(self, tmpdir): d = str(tmpdir.join('test')) link_d = str(tmpdir.join('test.link')) makedirs(d) with pytest.raises(Exception): link(d, link_d) def test_link_with_overwrite(self, tmpdir): f = str(tmpdir.join('test.txt')) link_f = str(tmpdir.join('test.link')) touch(f) link(f, link_f) t1 = os.path.getctime(link_f) time.sleep(1) link(f, link_f, overwrite=True) t2 = os.path.getctime(link_f) assert t1 != t2 @pytest.mark.skipif(WINDOWS, reason='Not support on windows') class TestSymLink: def test_symlink_a_file(self, tmpdir): f = str(tmpdir.join('test.txt')) link_f = str(tmpdir.join('test.link')) touch(f) symlink(f, link_f) assert os.path.exists(link_f) assert os.path.islink(link_f) def test_symlink_with_ignore_error(self, tmpdir): d = str(tmpdir.join('test')) link_d = str(tmpdir.join('test.link')) makedirs(d) link(d, link_d, ignore_errors=True) def test_symlink_with_overwrite(self, tmpdir): f = str(tmpdir.join('test.txt')) link_f = str(tmpdir.join('test.link')) touch(f) symlink(f, link_f) t1 = os.lstat(link_f).st_ctime time.sleep(1) symlink(f, link_f, overwrite=True) t2 = os.lstat(link_f).st_ctime assert t1 != t2 def test_symlink_without_ignore_error(self, tmpdir): d = str(tmpdir.join('test')) link_d = str(tmpdir.join('test.link')) makedirs(d) with pytest.raises(Exception): link(d, link_d) class TestCopy: def test_copy_file(self, tmpdir): f = str(tmpdir.join('test.txt')) f_copy = str(tmpdir.join('test_copy.txt')) touch(f) copy(f, f_copy) assert os.path.exists(f_copy) def test_copy_non_empty_dir(self, tmpdir): f = str(tmpdir.join('test/test.txt')) d = str(tmpdir.join('test')) d_copy = str(tmpdir.join('test_copy')) os.makedirs(d) touch(f) copy(d, d_copy) assert os.path.exists(d_copy) def test_copy_empty_dir(self, tmpdir): d = str(tmpdir.join('test')) d_copy = str(tmpdir.join('test_copy')) makedirs(d) copy(d, d_copy) assert os.path.exists(d_copy) @pytest.mark.skipif(WINDOWS, reason='Not support on windows') def test_copy_dir_follow_symlink(self, tmpdir): f = str(tmpdir.join('test/test.txt')) d = str(tmpdir.join('test')) link_f = str(tmpdir.join('test/test_link.txt')) d_copy = str(tmpdir.join('test_copy')) new_link_f = str(tmpdir.join('test_copy/test_link.txt')) makedirs(d) touch(f) os.symlink(f, link_f) copy(d, d_copy, follow_symlinks=True) assert os.path.exists(d_copy) assert os.path.islink(new_link_f) @pytest.mark.skipif(WINDOWS, reason='Not support on windows') def test_copy_dir_not_follow_symlink(self, tmpdir): f = str(tmpdir.join('test/test.txt')) d = str(tmpdir.join('test')) link_f = str(tmpdir.join('test/test_link.txt')) d_copy = str(tmpdir.join('test_copy')) new_link_f = str(tmpdir.join('test_copy/test_link.txt')) makedirs(d) touch(f) os.symlink(f, link_f) copy(d, d_copy, follow_symlinks=False) assert os.path.exists(d_copy) assert not os.path.islink(new_link_f) @pytest.mark.skipif(WINDOWS, reason='Not support on windows') def test_copy_file_follow_symlink(self, tmpdir): f = str(tmpdir.join('test.txt')) link_f = str(tmpdir.join('test.link')) copy_link_f = str(tmpdir.join('test_copy.link')) touch(f) link(f, link_f) copy(link_f, copy_link_f, follow_symlinks=True) assert os.path.exists(copy_link_f) assert not os.path.islink(copy_link_f) @pytest.mark.skipif(WINDOWS, reason='Not support on windows') def test_copy_file_not_follow_symlink(self, tmpdir): f = str(tmpdir.join('test.txt')) link_f = str(tmpdir.join('test.link')) copy_link_f = str(tmpdir.join('test_copy.link')) touch(f) os.symlink(f, link_f) copy(link_f, copy_link_f, follow_symlinks=False) assert os.path.exists(copy_li", https://github.com/prymitive/upaas-common,7a7d9ffb385151092c637c3ef500a79623c005ae,tests/test_storage_local.py::test_dir_exists,tests/test_storage_local.py,NIO,flaky,RepoArchived,nan,classify;root_cause,"# -*- coding: utf-8 -*- """""" :copyright: Copyright 2013-2014 by Łukasz Mierzwa :contact: l.mierzwa@gmail.com """""" from __future__ import unicode_literals import os import shutil import tempfile import datetime import pytest from upaas.storage.local import LocalStorage from upaas.storage.exceptions import FileNotFound from upaas.config.base import ConfigurationError from upaas.utils import load_handler @pytest.fixture(scope=""module"") def storage(request): directory = tempfile.mkdtemp(prefix=""upaas_teststorage_"") storage = LocalStorage({'dir': directory}) def cleanup(): shutil.rmtree(storage.settings.dir) request.addfinalizer(cleanup) return storage def test_find_storage(): assert load_handler( 'upaas.storage.local.LocalStorage', settings={'dir': '/'}) is not None def test_find_storage_invalid(): with pytest.raises(ConfigurationError): load_handler('upaas.storage.local.LocalStorage') def test_valid_settings(storage): assert storage.settings.dir is not None def test_invalid_settings(): with pytest.raises(ConfigurationError): LocalStorage({}) def test_config_dir_exists(storage): assert os.path.isdir(storage.settings.dir) def test_config_dir_missing(): with pytest.raises(ConfigurationError): LocalStorage({""dir"": ""/non-existing-dir""}) def test_file_exists(storage): file_name = ""file_exists"" open(os.path.join(storage.settings.dir, file_name), ""w"").close() assert storage.exists(file_name) is True def test_dir_exists(storage): dir_name = ""dir_exists"" os.mkdir(os.path.join(storage.settings.dir, dir_name)) assert storage.exists(dir_name) is True def test_not_exists(storage): assert storage.exists(""missing_file"") is False def test_get_not_exists(storage): with pytest.raises(FileNotFound): storage.get(""missing file"", ""output"") def test_put_and_get(storage, empty_dir, empty_file): storage.put(empty_file, ""abc"") assert os.path.isfile(os.path.join(storage.settings.dir, ""abc"")) local_path = os.path.join(empty_dir, ""xyz"") storage.get(""abc"", local_path) assert os.path.exists(local_path) def test_delete_not_exists(storage): with pytest.raises(FileNotFound): storage.delete(""missing file"") def test_put_and_delete(storage, empty_file): storage.put(empty_file, ""delete.me"") assert os.path.isfile(os.path.join(storage.settings.dir, ""delete.me"")) storage.delete(""delete.me"") assert os.path.isfile( os.path.join(storage.settings.dir, ""delete.me"")) is False def test_size_empty(storage, empty_file): storage.put(empty_file, ""empty.file"") assert storage.size(""empty.file"") == 0 def test_size_not_empty(storage, empty_file): with open(empty_file, 'w') as f: f.write('123456789') storage.put(empty_file, ""not-empty.file"") assert storage.size(""not-empty.file"") == 9 def test_mtime(storage, empty_file): storage.put(empty_file, ""mtime.check"") assert storage.mtime(""mtime.check"") == datetime.datetime.fromtimestamp( os.path.getmtime(os.path.join(storage.settings.dir, ""mtime.check""))) ", https://github.com/pustotnik/zenmake,6cff05114cea34fc204d9e95422f11dc25cd1228,tests/test_waf_assist.py::testUsedWafTaskKeys,tests/test_waf_assist.py,NIO,flaky,Accepted,https://github.com/pustotnik/zenmake/pull/29,classify;root_cause;fix_proposal,"# coding=utf-8 # # pylint: disable = wildcard-import, unused-wildcard-import # pylint: disable = missing-docstring, invalid-name, bad-continuation # pylint: disable = too-many-statements """""" Copyright (c) 2019, Alexander Magola. All rights reserved. license: BSD 3-Clause License, see LICENSE for more details. """""" import os from copy import deepcopy from waflib.ConfigSet import ConfigSet from waflib import Context from zm import utils, db from zm.waf import assist from zm.autodict import AutoDict from zm.constants import * from zm.pathutils import PathsParam from zm.features import ToolchainVars import tests.common as cmn joinpath = os.path.join abspath = os.path.abspath normpath = os.path.normpath relpath = os.path.relpath isfile = os.path.isfile def testWriteZenMakeMetaFile(tmpdir): buildconffile = tmpdir.join(""buildconf.py"") buildconffile.write(""buildconf"") dir1 = tmpdir.mkdir(""dir1"") buildconffile2 = dir1.join(""buildconf.yml"") buildconffile2.write(""buildconf"") zmmetafile = tmpdir.join(""zmmetafile"") fakeConfPaths = AutoDict() fakeConfPaths.buildconffile = str(buildconffile) fakeConfPaths.zmmetafile = str(zmmetafile) fakeConfManager = AutoDict() fakeConfManager.root.confPaths = fakeConfPaths fakeConfManager.configs = [ AutoDict(path = str(buildconffile)), AutoDict(path = str(buildconffile2)), ] monitFiles = [x.path for x in fakeConfManager.configs] assert not isfile(fakeConfPaths.zmmetafile) attrs = {'var': 1, 'd': 'zxc'} assist.writeZenMakeMetaFile(fakeConfPaths.zmmetafile, monitFiles, attrs) assert isfile(fakeConfPaths.zmmetafile) dbfile = db.PyDBFile(fakeConfPaths.zmmetafile, extension = '') cfgenv = dbfile.load(asConfigSet = True) assert 'rundir' in cfgenv assert 'topdir' in cfgenv assert 'outdir' in cfgenv assert 'monitfiles' in cfgenv assert cfgenv.monitfiles == [ str(buildconffile), str(buildconffile2) ] assert 'monithash' in cfgenv assert 'attrs' in cfgenv assert cfgenv.attrs == attrs _hash = utils.hashFiles(cfgenv.monitfiles) assert cfgenv.monithash == _hash assert 'toolenvs' in cfgenv tvars = ToolchainVars envVarNames = tvars.allSysFlagVars() + tvars.allSysVarsToSetToolchain() for name in envVarNames: assert name in cfgenv.toolenvs def testMakeTasksCachePath(): buildtype, zmcachedir = ('somename', 'somedir') path = assist.makeTasksCachePath(zmcachedir, buildtype) assert path == joinpath(zmcachedir, ""%s.tasks"" % buildtype) def testMakeTaskVariantName(): buildtype = 'ddd' taskName = 'bbb' name = assist.makeTaskVariantName(buildtype, taskName) assert name == '%s.%s' % (buildtype, taskName) taskName = ' bbb ' name = assist.makeTaskVariantName(buildtype, taskName) assert name == '%s.%s' % (buildtype, taskName.strip()) taskName = ' bbb ccc ' name = assist.makeTaskVariantName(buildtype, taskName) assert name == '%s.%s' % (buildtype, 'bbb_ccc') taskName = ' bbb ^$ccc ' name = assist.makeTaskVariantName(buildtype, taskName) assert name == '%s.%s' % (buildtype, 'bbb_..ccc') def testSetTaskEnvVars(): cfgEnvVars = ToolchainVars.allCfgFlagVars() toolchainSettings = AutoDict() toolchainSettings.gcc.vars = {} taskParamsFixture = [] baseTaskParams = { 'features' : ['cshlib'], 'toolchain': ['gcc'], } for var in cfgEnvVars: taskParamsFixture.extend([ dict(baseTaskParams, **{ var.lower() : 'var1' }), dict(baseTaskParams, **{ var.lower() : 'var1 var2' }), dict(baseTaskParams, **{ var.lower() : ['var1', 'var2'] }), ]) for taskParams in taskParamsFixture: env = ConfigSet() assist.setTaskEnvVars(env, taskParams, toolchainSettings) for key, val in taskParams.items(): if key in ('toolchain', 'features'): continue envkey = key.upper() assert envkey in env assert env[envkey] == utils.toList(val) def testDetectTaskFeatures(): taskParams = {} ctx = Context.Context(run_dir = os.getcwd()) assert assist.detectTaskFeatures(ctx, taskParams) == [] taskParams = { 'features' : '' } assert assist.detectTaskFeatures(ctx, taskParams) == [] for ftype in ('stlib', 'shlib', 'program'): for lang in ('c', 'cxx'): fulltype = '%s%s' % (lang, ftype) taskParams = { 'features' : fulltype } assert sorted(assist.detectTaskFeatures(ctx, taskParams)) == sorted([ lang, fulltype ]) taskParams = { 'features' : [lang, fulltype] } assert sorted(assist.detectTaskFeatures(ctx, taskParams)) == sorted([ lang, fulltype ]) def testHandleTaskIncludesParam(): rootdir = abspath(joinpath(os.getcwd(), 'testroot')) # just any abs path startdir = abspath(joinpath(rootdir, 'subdir')) taskParams = {} _taskParams = deepcopy(taskParams) assist.handleTaskIncludesParam(_taskParams, startdir) assert _taskParams['includes'] == ['.'] startDirs = [ abspath(joinpath(rootdir, '.')), abspath(joinpath(rootdir, '..')), abspath(joinpath(rootdir, 'subdir')), ] pathsIncludes = [ 'inc1 inc2', [ joinpath(os.getcwd(), '123', 'inc3'), joinpath(os.getcwd(), '456', 'inc4'), ], ] pathsExportIncludes = [ False, True, 'inc1 inc2', [ joinpath(os.getcwd(), '123', 'inc3'), joinpath(os.getcwd(), '456', 'inc4'), ], ] def calcExpectedPaths(newStartdir, paramStartDir, paths): paths = utils.toList(paths) paths = [ joinpath(paramStartDir, x) for x in paths ] paths = [ normpath(relpath(x, newStartdir)) for x in paths ] return paths _paths = [(x, y) for x in pathsIncludes for y in pathsExportIncludes] for paramStartDir in startDirs: for incPaths, expPaths in _paths: taskParams = { 'includes': PathsParam(incPaths, paramStartDir, kind = 'paths'), } if isinstance(expPaths, bool): taskParams['export-includes'] = expPaths else: taskParams['export-includes'] = \ PathsParam(expPaths, paramStartDir, kind = 'paths') _taskParams = deepcopy(taskParams) assist.handleTaskIncludesParam(_taskParams, startdir) includePaths = calcExpectedPaths(startdir, paramStartDir, incPaths) expected = ['.'] expected.extend(includePaths) assert _taskParams['includes'] == expected if isinstance(expPaths, bool): if expPaths: assert _taskParams['export-includes'] == expected else: assert 'export-includes' not in _taskParams else: exportPaths = calcExpectedPaths(startdir, paramStartDir, expPaths) assert _taskParams['export-includes'] == exportPaths def testHandleTaskSourceParam(tmpdir, mocker): """""" ./src/.. ./zm/buildconf.py cwd = ./zm bconf.rootdir = . bconf.path = ./zm """""" rootdir = tmpdir cwd = tmpdir.mkdir(""zm"") srcroot = tmpdir.mkdir(""src"") srcroot.join('some', '2', ""trash.txt"").write(""content"", ensure = True) srcroot.join('some', '2', ""main.c"").write(""content"", ensure = True) srcroot.join('some', '2', ""test.c"").write(""content"", ensure = True) srcroot.join('some', '2', ""test.h"").write(""content"", ensure = True) srcroot.join('some', '2', ""main.cpp"").write(""content"", ensure = True) srcroot.join('some', '2', ""test.cpp"").write(""content"", ensure = True) srcroot.join('some', '1', ""Test.cpp"").write(""content"", ensure = True) ctx = Context.Context(run_dir = str(cwd)) # ctx.path = Node(run_dir) bconf = AutoDict( rootdir = str(rootdir), path = str(cwd), confPaths = AutoDict(buildroot = ""build""), ) def getbconf(pathNode): return bconf ctx.getbconf = mocker.MagicMock(side_effect = getbconf) def getStartDirNode(path): return None ctx.getStartDirNode = mocker.MagicMock(side_effect = getStartDirNode) taskParams = { '$startdir' : '.' } # empty taskParams['source'] = {} src = assist.handleTaskSourceParam(ctx, taskParams) assert src == [] # find with wildcard srcParams = dict( startdir = 'src', ) taskParams['source'] = srcParams realStartDir = abspath(joinpath(bconf.rootdir, srcParams['startdir'])) # case 1 srcParams['include'] = ['some/**/*.cpp'] for _ in range(2): # to check with cache rv = assist.handleTaskSourceParam(ctx, taskParams) assert sorted([x.abspath() for x in rv]) == sorted([ joinpath(realStartDir, 'some', '2', 'main.cpp'), joinpath(realStartDir, 'some', '2', 'test.cpp'), joinpath(realStartDir, 'some', '1', 'Test.cpp'), ]) # case 2 srcParams['include'] = ['**/*.cpp'] srcParams['exclude'] = ['**/*test*'] for _ in range(2): # to check with cache rv = assist.handleTaskSourceParam(ctx, taskParams) assert sorted([x.abspath() for x in rv]) == sorted([ joinpath(realStartDir, 'some', '2', 'main.cpp'), joinpath(realStartDir, 'some', '1', 'Test.cpp'), ]) # case 3 srcParams['include'] = ['**/*.cpp', '**/*.c'] srcParams['exclude'] = ['**/*test*'] srcParams['ignorecase'] = True for _ in range(2): # to check with cache rv = assist.handleTaskSourceParam(ctx, taskParams) assert sorted([x.abspath() for x in rv]) == sorted([ joinpath(realStartDir, 'some', '2', 'main.c'), joinpath(realStartDir, 'some', '2', 'ma", https://github.com/pycqa/mccabe,e92e9e79799c5796f76f3da821dbb5aa56e41028,test_mccabe.py::RegressionTests::test_max_complexity_is_always_an_int,test_mccabe.py,OD-Brit,flaky,nan,nan,classify;root_cause,"import unittest import sys try: from StringIO import StringIO except ImportError: from io import StringIO import pytest import mccabe from mccabe import get_code_complexity # Snippets are put outside of testcases because of spacing issue that would # otherwise occur with triple quoted strings. trivial = 'def f(): pass' expr_as_statement = '''\ def f(): 0xF00D ''' sequential = """"""\ def f(n): k = n + 4 s = k + n return s """""" sequential_unencapsulated = """"""\ k = 2 + 4 s = k + 3 """""" if_elif_else_dead_path = """"""\ def f(n): if n > 3: return ""bigger than three"" elif n > 4: return ""is never executed"" else: return ""smaller than or equal to three"" """""" for_loop = """"""\ def f(): for i in range(10): print(i) """""" for_else = """"""\ def f(mylist): for i in mylist: print(i) else: print(None) """""" recursive = """"""\ def f(n): if n > 4: return f(n - 1) else: return n """""" nested_functions = """"""\ def a(): def b(): def c(): pass c() b() """""" try_else = """"""\ try: print(1) except TypeA: print(2) except TypeB: print(3) else: print(4) """""" async_keywords = """"""\ async def foobar(a, b, c): await whatever(a, b, c) if await b: pass async with c: pass async for x in a: pass """""" annotated_assign = """"""\ def f(): x: Any = None """""" def get_complexity_number(snippet, strio, max=0): """"""Get the complexity number from the printed string."""""" # Report from the lowest complexity number. get_code_complexity(snippet, max) strio_val = strio.getvalue() if strio_val: return int(strio_val.split()[-1].strip(""()"")) else: return None class McCabeTestCase(unittest.TestCase): def setUp(self): # If not assigned to sys.stdout then getvalue() won't capture anything. self._orig_stdout = sys.stdout sys.stdout = self.strio = StringIO() def tearDown(self): # https://mail.python.org/pipermail/tutor/2012-January/088031.html self.strio.close() sys.stdout = self._orig_stdout def assert_complexity(self, snippet, max): complexity = get_complexity_number(snippet, self.strio) self.assertEqual(complexity, max) # should have the same complexity when inside a function as well. infunc = 'def f():\n ' + snippet.replace('\n', '\n ') complexity = get_complexity_number(infunc, self.strio) self.assertEqual(complexity, max) def test_print_message(self): get_code_complexity(sequential, 0) printed_message = self.strio.getvalue() self.assertEqual(printed_message, ""stdin:1:1: C901 'f' is too complex (1)\n"") def test_sequential_snippet(self): complexity = get_complexity_number(sequential, self.strio) self.assertEqual(complexity, 1) def test_sequential_unencapsulated_snippet(self): complexity = get_complexity_number(sequential_unencapsulated, self.strio) self.assertEqual(complexity, None) def test_if_elif_else_dead_path_snippet(self): complexity = get_complexity_number(if_elif_else_dead_path, self.strio) # Paths that will never be executed are counted! self.assertEqual(complexity, 3) def test_for_loop_snippet(self): complexity = get_complexity_number(for_loop, self.strio) # The for loop adds an execution path; sometimes it won't be run. self.assertEqual(complexity, 2) def test_for_else_snippet(self): complexity = get_complexity_number(for_else, self.strio) # The for loop doesn't add an execution path, but its `else` does self.assertEqual(complexity, 2) def test_recursive_snippet(self): complexity = get_complexity_number(recursive, self.strio) self.assertEqual(complexity, 2) def test_nested_functions_snippet(self): complexity = get_complexity_number(nested_functions, self.strio) self.assertEqual(complexity, 3) def test_trivial(self): """"""The most-trivial program should pass a max-complexity=1 test"""""" complexity = get_complexity_number(trivial, self.strio, max=1) self.assertEqual(complexity, None) printed_message = self.strio.getvalue() self.assertEqual(printed_message, """") def test_expr_as_statement(self): complexity = get_complexity_number(expr_as_statement, self.strio) self.assertEqual(complexity, 1) def test_try_else(self): self.assert_complexity(try_else, 4) @pytest.mark.skipif(sys.version_info < (3, 5), reason=""Async keywords are only valid on Python 3.5+"") def test_async_keywords(self): """"""Validate that we properly process async keyword usage."""""" complexity = get_complexity_number(async_keywords, self.strio) self.assertEqual(complexity, 3) @pytest.mark.skipif( sys.version_info < (3, 6), reason=""Annotated assignments are only valid on Python 3.6+"", ) def test_annotated_assignment(self): complexity = get_complexity_number(annotated_assign, self.strio) self.assertEqual(complexity, 1) class RegressionTests(unittest.TestCase): def setUp(self): self.original_complexity = mccabe.McCabeChecker.max_complexity def tearDown(self): mccabe.McCabeChecker.max_complexity = self.original_complexity def test_max_complexity_is_always_an_int(self): """"""Ensure bug #32 does not regress."""""" class _options(object): max_complexity = None options = _options() options.max_complexity = '16' self.assertEqual(0, mccabe.McCabeChecker.max_complexity) mccabe.McCabeChecker.parse_options(options) self.assertEqual(16, mccabe.McCabeChecker.max_complexity) if __name__ == ""__main__"": unittest.main() ", https://github.com/PyFunceble/domain2idna,39a1c4e1ebb877ed511e53b618fbe437a685c970,tests/test_cli.py::TestCLI::test_domain,tests/test_cli.py,OD-Vic,flaky,Opened,https://github.com/PyFunceble/domain2idna/pull/4,classify;root_cause,"#!/usr/bin/env python3 """""" domain2idna - The tool to convert a domain or a file with a list of domain to the famous IDNA format. Tests the API endpoints. Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom Contributors: Let's contribute to domains2idna!! Project link: https://github.com/PyFunceble/domain2idna Project documentation: http://domain2idna.readthedocs.io License: MIT License Copyright (c) 2018-2019 Nissar Chababy Copyright (c) 2019 PyFunceble Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """""" # pylint: disable=duplicate-code import sys from io import StringIO from os import path from unittest import TestCase from unittest import main as launch_tests from domain2idna.cli import file, subjects from domain2idna.helpers import File class BaseStdout(TestCase): """""" This class is the one we use when we want to catch stdout. """""" def setUp(self): """""" Setup stdout. """""" sys.stdout = StringIO() def tearDown(self): sys.stdout.close() sys.stdout = sys.__stdout__ class TestCLI(BaseStdout): """""" This class will test domain2idna.__init__. """""" def setUp(self): """""" Setup all cross tests variables. """""" self.domain_to_test = [""ṁỵetherwallet.com"", ""xn--etherwallet-tv8eq7f.com\n""] self.domains_to_test = [ ""bịllogram.com"", ""0.0.0.0 ṁỵetherwallet.com"", ""coinbȧse.com"", ""cryptopiạ.com"", ""bittréẋ.com"", ""cṙyptopia.com"", ] self.converted = [ ""xn--bllogram-g80d.com"", ""0.0.0.0 xn--etherwallet-tv8eq7f.com"", ""xn--coinbse-30c.com"", ""xn--cryptopi-ux0d.com"", ""xn--bittr-fsa6124c.com"", ""xn--cyptopia-4e0d.com"", ] self.empty_inputs = ["""", "" "", "" "", None, False] def test_domain(self): """""" This method will test domain2idna.domain() """""" BaseStdout.setUp(self) expected = self.domain_to_test[-1] subjects(self.domain_to_test[0], None) actual = sys.stdout.getvalue() self.assertEqual(expected, actual) def test_domain_output(self): """""" This method will test domain2idna.domain() for the case that we want the output into a file. """""" output_file = ""this_file_is_a_ghost"" expected = False actual = path.isfile(output_file) self.assertEqual(expected, actual) expected = self.domain_to_test[-1][:-1] subjects(self.domain_to_test[0], output_file) actual = File(output_file).read() self.assertEqual(expected, actual) File(output_file).delete() expected = False actual = path.isfile(output_file) self.assertEqual(expected, actual) def test_empty_domain(self): """""" This method will test domain2idna.domain() for the case that an empty string is given. """""" for empty_domain in self.empty_inputs: self.assertRaises( ValueError, lambda: subjects(empty_domain), # pylint: disable=cell-var-from-loop ) def test_file(self): """""" This method will test domain2idna.file(). """""" file_to_pass = ""this_file_is_a_ghost"" BaseStdout.setUp(self) expected = False actual = path.isfile(file_to_pass) self.assertEqual(expected, actual) File(file_to_pass).write(""\n"".join(self.domains_to_test)) expected = True actual = path.isfile(file_to_pass) self.assertEqual(expected, actual) expected = ""\n"".join(self.domains_to_test) actual = File(file_to_pass).read() self.assertEqual(expected, actual) expected = ""\n"".join(self.converted) + ""\n"" file(file_to_pass, None) actual = sys.stdout.getvalue() self.assertEqual(expected, actual) File(file_to_pass).delete() expected = False actual = path.isfile(file_to_pass) self.assertEqual(expected, actual) def test_file_output(self): """""" This method will test domain2idna.file() for the case we want the results in a file. """""" file_to_pass = ""this_file_is_a_ghost"" output_file = ""this_file_is_a_converted_ghost"" expected = False actual = path.isfile(file_to_pass) self.assertEqual(expected, actual) File(file_to_pass).write(""\n"".join(self.domains_to_test)) expected = True actual = path.isfile(file_to_pass) self.assertEqual(expected, actual) expected = ""\n"".join(self.domains_to_test) actual = File(file_to_pass).read() self.assertEqual(expected, actual) expected = ""\n"".join(self.converted) file(file_to_pass, output_file) actual = File(output_file).read() self.assertEqual(expected, actual) File(file_to_pass).delete() File(output_file).delete() expected = False actual = path.isfile(file_to_pass) self.assertEqual(expected, actual) expected = False actual = path.isfile(output_file) self.assertEqual(expected, actual) if __name__ == ""__main__"": launch_tests() ", https://github.com/PyFunceble/domain2idna,39a1c4e1ebb877ed511e53b618fbe437a685c970,tests/test_cli.py::TestCLI::test_file,tests/test_cli.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python3 """""" domain2idna - The tool to convert a domain or a file with a list of domain to the famous IDNA format. Tests the API endpoints. Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom Contributors: Let's contribute to domains2idna!! Project link: https://github.com/PyFunceble/domain2idna Project documentation: http://domain2idna.readthedocs.io License: MIT License Copyright (c) 2018-2019 Nissar Chababy Copyright (c) 2019 PyFunceble Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """""" # pylint: disable=duplicate-code import sys from io import StringIO from os import path from unittest import TestCase from unittest import main as launch_tests from domain2idna.cli import file, subjects from domain2idna.helpers import File class BaseStdout(TestCase): """""" This class is the one we use when we want to catch stdout. """""" def setUp(self): """""" Setup stdout. """""" sys.stdout = StringIO() def tearDown(self): sys.stdout.close() sys.stdout = sys.__stdout__ class TestCLI(BaseStdout): """""" This class will test domain2idna.__init__. """""" def setUp(self): """""" Setup all cross tests variables. """""" self.domain_to_test = [""ṁỵetherwallet.com"", ""xn--etherwallet-tv8eq7f.com\n""] self.domains_to_test = [ ""bịllogram.com"", ""0.0.0.0 ṁỵetherwallet.com"", ""coinbȧse.com"", ""cryptopiạ.com"", ""bittréẋ.com"", ""cṙyptopia.com"", ] self.converted = [ ""xn--bllogram-g80d.com"", ""0.0.0.0 xn--etherwallet-tv8eq7f.com"", ""xn--coinbse-30c.com"", ""xn--cryptopi-ux0d.com"", ""xn--bittr-fsa6124c.com"", ""xn--cyptopia-4e0d.com"", ] self.empty_inputs = ["""", "" "", "" "", None, False] def test_domain(self): """""" This method will test domain2idna.domain() """""" BaseStdout.setUp(self) expected = self.domain_to_test[-1] subjects(self.domain_to_test[0], None) actual = sys.stdout.getvalue() self.assertEqual(expected, actual) def test_domain_output(self): """""" This method will test domain2idna.domain() for the case that we want the output into a file. """""" output_file = ""this_file_is_a_ghost"" expected = False actual = path.isfile(output_file) self.assertEqual(expected, actual) expected = self.domain_to_test[-1][:-1] subjects(self.domain_to_test[0], output_file) actual = File(output_file).read() self.assertEqual(expected, actual) File(output_file).delete() expected = False actual = path.isfile(output_file) self.assertEqual(expected, actual) def test_empty_domain(self): """""" This method will test domain2idna.domain() for the case that an empty string is given. """""" for empty_domain in self.empty_inputs: self.assertRaises( ValueError, lambda: subjects(empty_domain), # pylint: disable=cell-var-from-loop ) def test_file(self): """""" This method will test domain2idna.file(). """""" file_to_pass = ""this_file_is_a_ghost"" BaseStdout.setUp(self) expected = False actual = path.isfile(file_to_pass) self.assertEqual(expected, actual) File(file_to_pass).write(""\n"".join(self.domains_to_test)) expected = True actual = path.isfile(file_to_pass) self.assertEqual(expected, actual) expected = ""\n"".join(self.domains_to_test) actual = File(file_to_pass).read() self.assertEqual(expected, actual) expected = ""\n"".join(self.converted) + ""\n"" file(file_to_pass, None) actual = sys.stdout.getvalue() self.assertEqual(expected, actual) File(file_to_pass).delete() expected = False actual = path.isfile(file_to_pass) self.assertEqual(expected, actual) def test_file_output(self): """""" This method will test domain2idna.file() for the case we want the results in a file. """""" file_to_pass = ""this_file_is_a_ghost"" output_file = ""this_file_is_a_converted_ghost"" expected = False actual = path.isfile(file_to_pass) self.assertEqual(expected, actual) File(file_to_pass).write(""\n"".join(self.domains_to_test)) expected = True actual = path.isfile(file_to_pass) self.assertEqual(expected, actual) expected = ""\n"".join(self.domains_to_test) actual = File(file_to_pass).read() self.assertEqual(expected, actual) expected = ""\n"".join(self.converted) file(file_to_pass, output_file) actual = File(output_file).read() self.assertEqual(expected, actual) File(file_to_pass).delete() File(output_file).delete() expected = False actual = path.isfile(file_to_pass) self.assertEqual(expected, actual) expected = False actual = path.isfile(output_file) self.assertEqual(expected, actual) if __name__ == ""__main__"": launch_tests() ", https://github.com/PyFunceble/domain2idna,39a1c4e1ebb877ed511e53b618fbe437a685c970,tests/test_converter.py::TestConverter::test_commented_line,tests/test_converter.py,OD-Vic,flaky,Opened,https://github.com/PyFunceble/domain2idna/pull/4,classify;root_cause,"#!/usr/bin/env python3 """""" domain2idna - The tool to convert a domain or a file with a list of domain to the famous IDNA format. Tests the converter itself. Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom Contributors: Let's contribute to domains2idna!! Project link: https://github.com/PyFunceble/domain2idna Project documentation: http://domain2idna.readthedocs.io License: MIT License Copyright (c) 2018-2019 Nissar Chababy Copyright (c) 2019 PyFunceble Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """""" from unittest import TestCase from unittest import main as launch_tests from domain2idna.converter import Converter class TestConverter(TestCase): """""" Tests Converter. """""" def test_to_idna_single(self): """""" Runs and tests Converter """""" domain_to_test = ""ṁỵetherwallet.com"" expected = ""xn--etherwallet-tv8eq7f.com"" actual = Converter(domain_to_test).get_converted() self.assertEqual(expected, actual) def test_to_idna_multiple(self): """""" Runs and tests Converter. """""" domains_to_test = [ ""bịllogram.com"", ""bittréẋ.com"", ""cryptopiạ.com"", ""coinbȧse.com"", ""cṙyptopia.com"", ""0.0.0.0 ṁỵetherwallet.com"", ""0.0.0.0/8"", ] expected = [ ""xn--bllogram-g80d.com"", ""xn--bittr-fsa6124c.com"", ""xn--cryptopi-ux0d.com"", ""xn--coinbse-30c.com"", ""xn--cyptopia-4e0d.com"", ""0.0.0.0 xn--etherwallet-tv8eq7f.com"", ""0.0.0.0/8"", ] actual = Converter(domains_to_test).get_converted() self.assertEqual(expected, actual) def test_to_idna_multiple_urls(self): """""" Runs and tests Converter. """""" domains_to_test = [ ""http://bịllogram.com"", ""https://bittréẋ.com/path;parameters?query#fragment"", ""ftp://cryptopiạ.com"", ""git://coinbȧse.com"", ""://coinbȧse.com/hello_world"", ] expected = [ ""http://xn--bllogram-g80d.com"", ""https://xn--bittr-fsa6124c.com/path;parameters?query#fragment"", ""ftp://xn--cryptopi-ux0d.com"", ""git://xn--coinbse-30c.com"", ""://xn--coinbse-30c.com/hello_world"", ] actual = Converter(domains_to_test).get_converted() self.assertEqual(expected, actual) def test_commented_line(self): """""" Tests that the comments are returned normally. """""" comments = [ ""# Hello, World!"", ""# This is another commented line"", ""cryptopiạ.com # This is a comment at the end of a line."", ""# This is a commented line with bittréẋ.com"", ""# cryptopiạ.com"", ] expected = [ ""# Hello, World!"", ""# This is another commented line"", ""xn--cryptopi-ux0d.com # This is a comment at the end of a line."", ""# This is a commented line with bittréẋ.com"", ""# cryptopiạ.com"", ] actual = Converter(comments).get_converted() self.assertEqual(expected, actual) def test_hosts_file_format(self): """""" Tests that the hosts file format is always respected. """""" given = [ ""0.0.0.0 bịllogram.com"", ""127.0.0.1 bittréẋ.com"", ""0.0.0.0 cryptopiạ.com"", ""127.0.0.1 coinbȧse.com"", ""0.0.0.0 cṙyptopia.com"", ] expected = [ ""0.0.0.0 xn--bllogram-g80d.com"", ""127.0.0.1 xn--bittr-fsa6124c.com"", ""0.0.0.0 xn--cryptopi-ux0d.com"", ""127.0.0.1 xn--coinbse-30c.com"", ""0.0.0.0 xn--cyptopia-4e0d.com"", ] actual = Converter(given).get_converted() self.assertEqual(expected, actual) given = ""0.0.0.0 bịllogram.com"" expected = ""0.0.0.0 xn--bllogram-g80d.com"" actual = Converter(given).get_converted() self.assertEqual(expected, actual) if __name__ == ""__main__"": launch_tests() ", https://github.com/PyFunceble/domain2idna,39a1c4e1ebb877ed511e53b618fbe437a685c970,tests/test_converter.py::TestConverter::test_hosts_file_format,tests/test_converter.py,OD-Vic,flaky,Opened,https://github.com/PyFunceble/domain2idna/pull/4,classify;root_cause,"#!/usr/bin/env python3 """""" domain2idna - The tool to convert a domain or a file with a list of domain to the famous IDNA format. Tests the converter itself. Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom Contributors: Let's contribute to domains2idna!! Project link: https://github.com/PyFunceble/domain2idna Project documentation: http://domain2idna.readthedocs.io License: MIT License Copyright (c) 2018-2019 Nissar Chababy Copyright (c) 2019 PyFunceble Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """""" from unittest import TestCase from unittest import main as launch_tests from domain2idna.converter import Converter class TestConverter(TestCase): """""" Tests Converter. """""" def test_to_idna_single(self): """""" Runs and tests Converter """""" domain_to_test = ""ṁỵetherwallet.com"" expected = ""xn--etherwallet-tv8eq7f.com"" actual = Converter(domain_to_test).get_converted() self.assertEqual(expected, actual) def test_to_idna_multiple(self): """""" Runs and tests Converter. """""" domains_to_test = [ ""bịllogram.com"", ""bittréẋ.com"", ""cryptopiạ.com"", ""coinbȧse.com"", ""cṙyptopia.com"", ""0.0.0.0 ṁỵetherwallet.com"", ""0.0.0.0/8"", ] expected = [ ""xn--bllogram-g80d.com"", ""xn--bittr-fsa6124c.com"", ""xn--cryptopi-ux0d.com"", ""xn--coinbse-30c.com"", ""xn--cyptopia-4e0d.com"", ""0.0.0.0 xn--etherwallet-tv8eq7f.com"", ""0.0.0.0/8"", ] actual = Converter(domains_to_test).get_converted() self.assertEqual(expected, actual) def test_to_idna_multiple_urls(self): """""" Runs and tests Converter. """""" domains_to_test = [ ""http://bịllogram.com"", ""https://bittréẋ.com/path;parameters?query#fragment"", ""ftp://cryptopiạ.com"", ""git://coinbȧse.com"", ""://coinbȧse.com/hello_world"", ] expected = [ ""http://xn--bllogram-g80d.com"", ""https://xn--bittr-fsa6124c.com/path;parameters?query#fragment"", ""ftp://xn--cryptopi-ux0d.com"", ""git://xn--coinbse-30c.com"", ""://xn--coinbse-30c.com/hello_world"", ] actual = Converter(domains_to_test).get_converted() self.assertEqual(expected, actual) def test_commented_line(self): """""" Tests that the comments are returned normally. """""" comments = [ ""# Hello, World!"", ""# This is another commented line"", ""cryptopiạ.com # This is a comment at the end of a line."", ""# This is a commented line with bittréẋ.com"", ""# cryptopiạ.com"", ] expected = [ ""# Hello, World!"", ""# This is another commented line"", ""xn--cryptopi-ux0d.com # This is a comment at the end of a line."", ""# This is a commented line with bittréẋ.com"", ""# cryptopiạ.com"", ] actual = Converter(comments).get_converted() self.assertEqual(expected, actual) def test_hosts_file_format(self): """""" Tests that the hosts file format is always respected. """""" given = [ ""0.0.0.0 bịllogram.com"", ""127.0.0.1 bittréẋ.com"", ""0.0.0.0 cryptopiạ.com"", ""127.0.0.1 coinbȧse.com"", ""0.0.0.0 cṙyptopia.com"", ] expected = [ ""0.0.0.0 xn--bllogram-g80d.com"", ""127.0.0.1 xn--bittr-fsa6124c.com"", ""0.0.0.0 xn--cryptopi-ux0d.com"", ""127.0.0.1 xn--coinbse-30c.com"", ""0.0.0.0 xn--cyptopia-4e0d.com"", ] actual = Converter(given).get_converted() self.assertEqual(expected, actual) given = ""0.0.0.0 bịllogram.com"" expected = ""0.0.0.0 xn--bllogram-g80d.com"" actual = Converter(given).get_converted() self.assertEqual(expected, actual) if __name__ == ""__main__"": launch_tests() ", https://github.com/PyFunceble/domain2idna,39a1c4e1ebb877ed511e53b618fbe437a685c970,tests/test_converter.py::TestConverter::test_to_idna_multiple,tests/test_converter.py,OD-Vic,flaky,Opened,https://github.com/PyFunceble/domain2idna/pull/4,classify;root_cause,"#!/usr/bin/env python3 """""" domain2idna - The tool to convert a domain or a file with a list of domain to the famous IDNA format. Tests the converter itself. Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom Contributors: Let's contribute to domains2idna!! Project link: https://github.com/PyFunceble/domain2idna Project documentation: http://domain2idna.readthedocs.io License: MIT License Copyright (c) 2018-2019 Nissar Chababy Copyright (c) 2019 PyFunceble Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """""" from unittest import TestCase from unittest import main as launch_tests from domain2idna.converter import Converter class TestConverter(TestCase): """""" Tests Converter. """""" def test_to_idna_single(self): """""" Runs and tests Converter """""" domain_to_test = ""ṁỵetherwallet.com"" expected = ""xn--etherwallet-tv8eq7f.com"" actual = Converter(domain_to_test).get_converted() self.assertEqual(expected, actual) def test_to_idna_multiple(self): """""" Runs and tests Converter. """""" domains_to_test = [ ""bịllogram.com"", ""bittréẋ.com"", ""cryptopiạ.com"", ""coinbȧse.com"", ""cṙyptopia.com"", ""0.0.0.0 ṁỵetherwallet.com"", ""0.0.0.0/8"", ] expected = [ ""xn--bllogram-g80d.com"", ""xn--bittr-fsa6124c.com"", ""xn--cryptopi-ux0d.com"", ""xn--coinbse-30c.com"", ""xn--cyptopia-4e0d.com"", ""0.0.0.0 xn--etherwallet-tv8eq7f.com"", ""0.0.0.0/8"", ] actual = Converter(domains_to_test).get_converted() self.assertEqual(expected, actual) def test_to_idna_multiple_urls(self): """""" Runs and tests Converter. """""" domains_to_test = [ ""http://bịllogram.com"", ""https://bittréẋ.com/path;parameters?query#fragment"", ""ftp://cryptopiạ.com"", ""git://coinbȧse.com"", ""://coinbȧse.com/hello_world"", ] expected = [ ""http://xn--bllogram-g80d.com"", ""https://xn--bittr-fsa6124c.com/path;parameters?query#fragment"", ""ftp://xn--cryptopi-ux0d.com"", ""git://xn--coinbse-30c.com"", ""://xn--coinbse-30c.com/hello_world"", ] actual = Converter(domains_to_test).get_converted() self.assertEqual(expected, actual) def test_commented_line(self): """""" Tests that the comments are returned normally. """""" comments = [ ""# Hello, World!"", ""# This is another commented line"", ""cryptopiạ.com # This is a comment at the end of a line."", ""# This is a commented line with bittréẋ.com"", ""# cryptopiạ.com"", ] expected = [ ""# Hello, World!"", ""# This is another commented line"", ""xn--cryptopi-ux0d.com # This is a comment at the end of a line."", ""# This is a commented line with bittréẋ.com"", ""# cryptopiạ.com"", ] actual = Converter(comments).get_converted() self.assertEqual(expected, actual) def test_hosts_file_format(self): """""" Tests that the hosts file format is always respected. """""" given = [ ""0.0.0.0 bịllogram.com"", ""127.0.0.1 bittréẋ.com"", ""0.0.0.0 cryptopiạ.com"", ""127.0.0.1 coinbȧse.com"", ""0.0.0.0 cṙyptopia.com"", ] expected = [ ""0.0.0.0 xn--bllogram-g80d.com"", ""127.0.0.1 xn--bittr-fsa6124c.com"", ""0.0.0.0 xn--cryptopi-ux0d.com"", ""127.0.0.1 xn--coinbse-30c.com"", ""0.0.0.0 xn--cyptopia-4e0d.com"", ] actual = Converter(given).get_converted() self.assertEqual(expected, actual) given = ""0.0.0.0 bịllogram.com"" expected = ""0.0.0.0 xn--bllogram-g80d.com"" actual = Converter(given).get_converted() self.assertEqual(expected, actual) if __name__ == ""__main__"": launch_tests() ", https://github.com/PyFunceble/domain2idna,39a1c4e1ebb877ed511e53b618fbe437a685c970,tests/test_converter.py::TestConverter::test_to_idna_multiple_urls,tests/test_converter.py,OD-Vic,flaky,Opened,https://github.com/PyFunceble/domain2idna/pull/4,classify;root_cause,"#!/usr/bin/env python3 """""" domain2idna - The tool to convert a domain or a file with a list of domain to the famous IDNA format. Tests the converter itself. Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom Contributors: Let's contribute to domains2idna!! Project link: https://github.com/PyFunceble/domain2idna Project documentation: http://domain2idna.readthedocs.io License: MIT License Copyright (c) 2018-2019 Nissar Chababy Copyright (c) 2019 PyFunceble Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """""" from unittest import TestCase from unittest import main as launch_tests from domain2idna.converter import Converter class TestConverter(TestCase): """""" Tests Converter. """""" def test_to_idna_single(self): """""" Runs and tests Converter """""" domain_to_test = ""ṁỵetherwallet.com"" expected = ""xn--etherwallet-tv8eq7f.com"" actual = Converter(domain_to_test).get_converted() self.assertEqual(expected, actual) def test_to_idna_multiple(self): """""" Runs and tests Converter. """""" domains_to_test = [ ""bịllogram.com"", ""bittréẋ.com"", ""cryptopiạ.com"", ""coinbȧse.com"", ""cṙyptopia.com"", ""0.0.0.0 ṁỵetherwallet.com"", ""0.0.0.0/8"", ] expected = [ ""xn--bllogram-g80d.com"", ""xn--bittr-fsa6124c.com"", ""xn--cryptopi-ux0d.com"", ""xn--coinbse-30c.com"", ""xn--cyptopia-4e0d.com"", ""0.0.0.0 xn--etherwallet-tv8eq7f.com"", ""0.0.0.0/8"", ] actual = Converter(domains_to_test).get_converted() self.assertEqual(expected, actual) def test_to_idna_multiple_urls(self): """""" Runs and tests Converter. """""" domains_to_test = [ ""http://bịllogram.com"", ""https://bittréẋ.com/path;parameters?query#fragment"", ""ftp://cryptopiạ.com"", ""git://coinbȧse.com"", ""://coinbȧse.com/hello_world"", ] expected = [ ""http://xn--bllogram-g80d.com"", ""https://xn--bittr-fsa6124c.com/path;parameters?query#fragment"", ""ftp://xn--cryptopi-ux0d.com"", ""git://xn--coinbse-30c.com"", ""://xn--coinbse-30c.com/hello_world"", ] actual = Converter(domains_to_test).get_converted() self.assertEqual(expected, actual) def test_commented_line(self): """""" Tests that the comments are returned normally. """""" comments = [ ""# Hello, World!"", ""# This is another commented line"", ""cryptopiạ.com # This is a comment at the end of a line."", ""# This is a commented line with bittréẋ.com"", ""# cryptopiạ.com"", ] expected = [ ""# Hello, World!"", ""# This is another commented line"", ""xn--cryptopi-ux0d.com # This is a comment at the end of a line."", ""# This is a commented line with bittréẋ.com"", ""# cryptopiạ.com"", ] actual = Converter(comments).get_converted() self.assertEqual(expected, actual) def test_hosts_file_format(self): """""" Tests that the hosts file format is always respected. """""" given = [ ""0.0.0.0 bịllogram.com"", ""127.0.0.1 bittréẋ.com"", ""0.0.0.0 cryptopiạ.com"", ""127.0.0.1 coinbȧse.com"", ""0.0.0.0 cṙyptopia.com"", ] expected = [ ""0.0.0.0 xn--bllogram-g80d.com"", ""127.0.0.1 xn--bittr-fsa6124c.com"", ""0.0.0.0 xn--cryptopi-ux0d.com"", ""127.0.0.1 xn--coinbse-30c.com"", ""0.0.0.0 xn--cyptopia-4e0d.com"", ] actual = Converter(given).get_converted() self.assertEqual(expected, actual) given = ""0.0.0.0 bịllogram.com"" expected = ""0.0.0.0 xn--bllogram-g80d.com"" actual = Converter(given).get_converted() self.assertEqual(expected, actual) if __name__ == ""__main__"": launch_tests() ", https://github.com/PyFunceble/domain2idna,39a1c4e1ebb877ed511e53b618fbe437a685c970,tests/test_converter.py::TestConverter::test_to_idna_single,tests/test_converter.py,OD-Vic,flaky,Opened,https://github.com/PyFunceble/domain2idna/pull/4,classify;root_cause,"#!/usr/bin/env python3 """""" domain2idna - The tool to convert a domain or a file with a list of domain to the famous IDNA format. Tests the converter itself. Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom Contributors: Let's contribute to domains2idna!! Project link: https://github.com/PyFunceble/domain2idna Project documentation: http://domain2idna.readthedocs.io License: MIT License Copyright (c) 2018-2019 Nissar Chababy Copyright (c) 2019 PyFunceble Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """""" from unittest import TestCase from unittest import main as launch_tests from domain2idna.converter import Converter class TestConverter(TestCase): """""" Tests Converter. """""" def test_to_idna_single(self): """""" Runs and tests Converter """""" domain_to_test = ""ṁỵetherwallet.com"" expected = ""xn--etherwallet-tv8eq7f.com"" actual = Converter(domain_to_test).get_converted() self.assertEqual(expected, actual) def test_to_idna_multiple(self): """""" Runs and tests Converter. """""" domains_to_test = [ ""bịllogram.com"", ""bittréẋ.com"", ""cryptopiạ.com"", ""coinbȧse.com"", ""cṙyptopia.com"", ""0.0.0.0 ṁỵetherwallet.com"", ""0.0.0.0/8"", ] expected = [ ""xn--bllogram-g80d.com"", ""xn--bittr-fsa6124c.com"", ""xn--cryptopi-ux0d.com"", ""xn--coinbse-30c.com"", ""xn--cyptopia-4e0d.com"", ""0.0.0.0 xn--etherwallet-tv8eq7f.com"", ""0.0.0.0/8"", ] actual = Converter(domains_to_test).get_converted() self.assertEqual(expected, actual) def test_to_idna_multiple_urls(self): """""" Runs and tests Converter. """""" domains_to_test = [ ""http://bịllogram.com"", ""https://bittréẋ.com/path;parameters?query#fragment"", ""ftp://cryptopiạ.com"", ""git://coinbȧse.com"", ""://coinbȧse.com/hello_world"", ] expected = [ ""http://xn--bllogram-g80d.com"", ""https://xn--bittr-fsa6124c.com/path;parameters?query#fragment"", ""ftp://xn--cryptopi-ux0d.com"", ""git://xn--coinbse-30c.com"", ""://xn--coinbse-30c.com/hello_world"", ] actual = Converter(domains_to_test).get_converted() self.assertEqual(expected, actual) def test_commented_line(self): """""" Tests that the comments are returned normally. """""" comments = [ ""# Hello, World!"", ""# This is another commented line"", ""cryptopiạ.com # This is a comment at the end of a line."", ""# This is a commented line with bittréẋ.com"", ""# cryptopiạ.com"", ] expected = [ ""# Hello, World!"", ""# This is another commented line"", ""xn--cryptopi-ux0d.com # This is a comment at the end of a line."", ""# This is a commented line with bittréẋ.com"", ""# cryptopiạ.com"", ] actual = Converter(comments).get_converted() self.assertEqual(expected, actual) def test_hosts_file_format(self): """""" Tests that the hosts file format is always respected. """""" given = [ ""0.0.0.0 bịllogram.com"", ""127.0.0.1 bittréẋ.com"", ""0.0.0.0 cryptopiạ.com"", ""127.0.0.1 coinbȧse.com"", ""0.0.0.0 cṙyptopia.com"", ] expected = [ ""0.0.0.0 xn--bllogram-g80d.com"", ""127.0.0.1 xn--bittr-fsa6124c.com"", ""0.0.0.0 xn--cryptopi-ux0d.com"", ""127.0.0.1 xn--coinbse-30c.com"", ""0.0.0.0 xn--cyptopia-4e0d.com"", ] actual = Converter(given).get_converted() self.assertEqual(expected, actual) given = ""0.0.0.0 bịllogram.com"" expected = ""0.0.0.0 xn--bllogram-g80d.com"" actual = Converter(given).get_converted() self.assertEqual(expected, actual) if __name__ == ""__main__"": launch_tests() ", https://github.com/PyFunceble/domain2idna,39a1c4e1ebb877ed511e53b618fbe437a685c970,tests/test_helpers.py::TestFile::test_delete_not_exist,tests/test_helpers.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python3 """""" domain2idna - The tool to convert a domain or a file with a list of domain to the famous IDNA format. Tests the helpers. Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom Contributors: Let's contribute to domains2idna!! Project link: https://github.com/PyFunceble/domain2idna Project documentation: http://domain2idna.readthedocs.io License: MIT License Copyright (c) 2018-2019 Nissar Chababy Copyright (c) 2019 PyFunceble Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """""" from os import path from unittest import TestCase from unittest import main as launch_tests from domain2idna.helpers import File class TestFile(TestCase): """""" Tests helpers.File. """""" def test_write_delete(self): """""" Tests helpers.File.write along with helpers.File.delete. """""" expected = ""Hello, World! I'm PyFunceble"" File(""hi"").write(expected) with open(""hi"") as file: actual = file.read() self.assertEqual(expected, actual) expected = False File(""hi"").delete() actual = path.isfile(""hi"") self.assertEqual(expected, actual) def test_write_overwrite_delete(self): """""" Tests helpers.File.write along with helpers.File.delete. """""" expected = ""Hello, World! I'm PyFunceble"" File(""hi"").write(expected) with open(""hi"") as file: actual = file.read() self.assertEqual(expected, actual) expected = ""Hello, World! Python is great, you should consider learning it!"" File(""hi"").write(expected) with open(""hi"") as file: actual = file.read() self.assertEqual(expected, actual) expected = False File(""hi"").delete() actual = path.isfile(""hi"") self.assertEqual(expected, actual) def test_read_delete(self): """""" Tests helpers.File.read along with helpers.File.delete. """""" expected = ""Hello, World! This has been written by Fun Ilrys."" File(""hi"").write(expected) actual = File(""hi"").read() self.assertEqual(expected, actual) expected = False File(""hi"").delete() actual = path.isfile(""hi"") self.assertEqual(expected, actual) def test_delete_not_exist(self): """""" Tests helpers.File.delete for the case that the file does not exists. """""" filename = ""this_file_is_a_ghost"" expected = False actual = path.isfile(filename) self.assertEqual(expected, actual) File(filename).delete() expected = False actual = path.isfile(filename) self.assertEqual(expected, actual) if __name__ == ""__main__"": launch_tests() ", https://github.com/PyFunceble/domain2idna,39a1c4e1ebb877ed511e53b618fbe437a685c970,tests/test_helpers.py::TestFile::test_read_delete,tests/test_helpers.py,OD-Vic,flaky,Opened,https://github.com/PyFunceble/domain2idna/pull/4,classify;root_cause,"#!/usr/bin/env python3 """""" domain2idna - The tool to convert a domain or a file with a list of domain to the famous IDNA format. Tests the helpers. Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom Contributors: Let's contribute to domains2idna!! Project link: https://github.com/PyFunceble/domain2idna Project documentation: http://domain2idna.readthedocs.io License: MIT License Copyright (c) 2018-2019 Nissar Chababy Copyright (c) 2019 PyFunceble Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """""" from os import path from unittest import TestCase from unittest import main as launch_tests from domain2idna.helpers import File class TestFile(TestCase): """""" Tests helpers.File. """""" def test_write_delete(self): """""" Tests helpers.File.write along with helpers.File.delete. """""" expected = ""Hello, World! I'm PyFunceble"" File(""hi"").write(expected) with open(""hi"") as file: actual = file.read() self.assertEqual(expected, actual) expected = False File(""hi"").delete() actual = path.isfile(""hi"") self.assertEqual(expected, actual) def test_write_overwrite_delete(self): """""" Tests helpers.File.write along with helpers.File.delete. """""" expected = ""Hello, World! I'm PyFunceble"" File(""hi"").write(expected) with open(""hi"") as file: actual = file.read() self.assertEqual(expected, actual) expected = ""Hello, World! Python is great, you should consider learning it!"" File(""hi"").write(expected) with open(""hi"") as file: actual = file.read() self.assertEqual(expected, actual) expected = False File(""hi"").delete() actual = path.isfile(""hi"") self.assertEqual(expected, actual) def test_read_delete(self): """""" Tests helpers.File.read along with helpers.File.delete. """""" expected = ""Hello, World! This has been written by Fun Ilrys."" File(""hi"").write(expected) actual = File(""hi"").read() self.assertEqual(expected, actual) expected = False File(""hi"").delete() actual = path.isfile(""hi"") self.assertEqual(expected, actual) def test_delete_not_exist(self): """""" Tests helpers.File.delete for the case that the file does not exists. """""" filename = ""this_file_is_a_ghost"" expected = False actual = path.isfile(filename) self.assertEqual(expected, actual) File(filename).delete() expected = False actual = path.isfile(filename) self.assertEqual(expected, actual) if __name__ == ""__main__"": launch_tests() ", https://github.com/PyFunceble/domain2idna,39a1c4e1ebb877ed511e53b618fbe437a685c970,tests/test_helpers.py::TestFile::test_write_delete,tests/test_helpers.py,OD-Vic,flaky,Opened,https://github.com/PyFunceble/domain2idna/pull/4,classify;root_cause,"#!/usr/bin/env python3 """""" domain2idna - The tool to convert a domain or a file with a list of domain to the famous IDNA format. Tests the helpers. Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom Contributors: Let's contribute to domains2idna!! Project link: https://github.com/PyFunceble/domain2idna Project documentation: http://domain2idna.readthedocs.io License: MIT License Copyright (c) 2018-2019 Nissar Chababy Copyright (c) 2019 PyFunceble Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """""" from os import path from unittest import TestCase from unittest import main as launch_tests from domain2idna.helpers import File class TestFile(TestCase): """""" Tests helpers.File. """""" def test_write_delete(self): """""" Tests helpers.File.write along with helpers.File.delete. """""" expected = ""Hello, World! I'm PyFunceble"" File(""hi"").write(expected) with open(""hi"") as file: actual = file.read() self.assertEqual(expected, actual) expected = False File(""hi"").delete() actual = path.isfile(""hi"") self.assertEqual(expected, actual) def test_write_overwrite_delete(self): """""" Tests helpers.File.write along with helpers.File.delete. """""" expected = ""Hello, World! I'm PyFunceble"" File(""hi"").write(expected) with open(""hi"") as file: actual = file.read() self.assertEqual(expected, actual) expected = ""Hello, World! Python is great, you should consider learning it!"" File(""hi"").write(expected) with open(""hi"") as file: actual = file.read() self.assertEqual(expected, actual) expected = False File(""hi"").delete() actual = path.isfile(""hi"") self.assertEqual(expected, actual) def test_read_delete(self): """""" Tests helpers.File.read along with helpers.File.delete. """""" expected = ""Hello, World! This has been written by Fun Ilrys."" File(""hi"").write(expected) actual = File(""hi"").read() self.assertEqual(expected, actual) expected = False File(""hi"").delete() actual = path.isfile(""hi"") self.assertEqual(expected, actual) def test_delete_not_exist(self): """""" Tests helpers.File.delete for the case that the file does not exists. """""" filename = ""this_file_is_a_ghost"" expected = False actual = path.isfile(filename) self.assertEqual(expected, actual) File(filename).delete() expected = False actual = path.isfile(filename) self.assertEqual(expected, actual) if __name__ == ""__main__"": launch_tests() ", https://github.com/PyFunceble/domain2idna,39a1c4e1ebb877ed511e53b618fbe437a685c970,tests/test_helpers.py::TestFile::test_write_overwrite_delete,tests/test_helpers.py,OD-Vic,flaky,Opened,https://github.com/PyFunceble/domain2idna/pull/4,classify;root_cause,"#!/usr/bin/env python3 """""" domain2idna - The tool to convert a domain or a file with a list of domain to the famous IDNA format. Tests the helpers. Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom Contributors: Let's contribute to domains2idna!! Project link: https://github.com/PyFunceble/domain2idna Project documentation: http://domain2idna.readthedocs.io License: MIT License Copyright (c) 2018-2019 Nissar Chababy Copyright (c) 2019 PyFunceble Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """""" from os import path from unittest import TestCase from unittest import main as launch_tests from domain2idna.helpers import File class TestFile(TestCase): """""" Tests helpers.File. """""" def test_write_delete(self): """""" Tests helpers.File.write along with helpers.File.delete. """""" expected = ""Hello, World! I'm PyFunceble"" File(""hi"").write(expected) with open(""hi"") as file: actual = file.read() self.assertEqual(expected, actual) expected = False File(""hi"").delete() actual = path.isfile(""hi"") self.assertEqual(expected, actual) def test_write_overwrite_delete(self): """""" Tests helpers.File.write along with helpers.File.delete. """""" expected = ""Hello, World! I'm PyFunceble"" File(""hi"").write(expected) with open(""hi"") as file: actual = file.read() self.assertEqual(expected, actual) expected = ""Hello, World! Python is great, you should consider learning it!"" File(""hi"").write(expected) with open(""hi"") as file: actual = file.read() self.assertEqual(expected, actual) expected = False File(""hi"").delete() actual = path.isfile(""hi"") self.assertEqual(expected, actual) def test_read_delete(self): """""" Tests helpers.File.read along with helpers.File.delete. """""" expected = ""Hello, World! This has been written by Fun Ilrys."" File(""hi"").write(expected) actual = File(""hi"").read() self.assertEqual(expected, actual) expected = False File(""hi"").delete() actual = path.isfile(""hi"") self.assertEqual(expected, actual) def test_delete_not_exist(self): """""" Tests helpers.File.delete for the case that the file does not exists. """""" filename = ""this_file_is_a_ghost"" expected = False actual = path.isfile(filename) self.assertEqual(expected, actual) File(filename).delete() expected = False actual = path.isfile(filename) self.assertEqual(expected, actual) if __name__ == ""__main__"": launch_tests() ", https://github.com/PyFunceble/domain2idna,39a1c4e1ebb877ed511e53b618fbe437a685c970,tests/test_init.py::TestInit::test_get,tests/test_init.py,OD-Vic,flaky,Opened,https://github.com/PyFunceble/domain2idna/pull/4,classify;root_cause,"#!/usr/bin/env python3 """""" domain2idna - The tool to convert a domain or a file with a list of domain to the famous IDNA format. Tests the API endpoints. Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom Contributors: Let's contribute to domains2idna!! Project link: https://github.com/PyFunceble/domain2idna Project documentation: http://domain2idna.readthedocs.io License: MIT License Copyright (c) 2018-2019 Nissar Chababy Copyright (c) 2019 PyFunceble Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """""" # pylint: disable=duplicate-code from unittest import TestCase from unittest import main as launch_tests from domain2idna import domain2idna class TestInit(TestCase): """""" This class will test domain2idna.__init__. """""" def setUp(self): """""" Setup all cross tests variables. """""" self.domain_to_test = [""ṁỵetherwallet.com"", ""xn--etherwallet-tv8eq7f.com\n""] self.domains_to_test = [ ""bittréẋ.com"", ""bịllogram.com"", ""coinbȧse.com"", ""cryptopiạ.com"", ""cṙyptopia.com"", ""0.0.0.0 ṁỵetherwallet.com"", ] self.converted = [ ""xn--bittr-fsa6124c.com"", ""xn--bllogram-g80d.com"", ""xn--coinbse-30c.com"", ""xn--cryptopi-ux0d.com"", ""xn--cyptopia-4e0d.com"", ""0.0.0.0 xn--etherwallet-tv8eq7f.com"", ] self.empty_inputs = ["""", "" "", "" "", None, False] def test_get(self): """""" This method will test domain2idna.get """""" expected = self.converted actual = domain2idna(self.domains_to_test) self.assertEqual(expected, actual) expected = self.empty_inputs actual = domain2idna(self.empty_inputs) self.assertEqual(expected, actual) expected = None actual = domain2idna(None) self.assertEqual(expected, actual) if __name__ == ""__main__"": launch_tests() ", https://github.com/pyglet/pyglet,9c2b638f7474045d7db6578924e4f96324ea7158,tests/unit/test_clock_fps.py::test_compute_fps,tests/unit/test_clock_fps.py,NIO,flaky,InspiredAFix,https://github.com/pyglet/pyglet/pull/514,classify;root_cause,"""""""Tests clock timing between frames and estimations of frames per second. """""" import time import pytest from pyglet import clock from ..annotations import skip_if_continuous_integration def sleep(seconds): """"""Busy sleep on the CPU which is very precise"""""" pyclock = clock.get_default() start = pyclock.time() while pyclock.time() - start < seconds: pass # since clock is global, we initialize a new clock on every test clock.set_default(clock.Clock()) def test_first_tick_is_delta_zero(): """""" Tests that the first tick is dt = 0. """""" dt = clock.tick() assert dt == 0 def test_start_at_zero_fps(): """""" Tests that the default clock starts with zero fps. """""" assert clock.get_fps() == 0 @skip_if_continuous_integration() def test_elapsed_time_between_tick(): """""" Test that the tick function returns the correct elapsed time between frames, in seconds. Because we are measuring time differences, we expect a small error (1%) from the expected value. """""" sleep_time = 0.2 # initialize internal counter clock.tick() # test between initialization and first tick sleep(sleep_time) delta_time_1 = clock.tick() # test between non-initialization tick and next tick sleep(sleep_time) delta_time_2 = clock.tick() assert delta_time_1 == pytest.approx(sleep_time, rel=0.01*sleep_time) assert delta_time_2 == pytest.approx(sleep_time, rel=0.01*sleep_time) @skip_if_continuous_integration() def test_compute_fps(): """""" Test that the clock computes a reasonable value of frames per second when simulated for 120 ticks at 60 frames per second. Because sleep is not very precise and fps are unbounded, we expect a moderate error (10%) from the expected value. """""" ticks = 120 # for averaging expected_fps = 60 seconds_per_tick = 1./expected_fps for i in range(ticks): time.sleep(seconds_per_tick) clock.tick() computed_fps = clock.get_fps() assert computed_fps == pytest.approx(expected_fps, rel=0.1*expected_fps) ", https://github.com/pyglet/pyglet,9c2b638f7474045d7db6578924e4f96324ea7158,tests/unit/test_clock_fps.py::test_first_tick_is_delta_zero,tests/unit/test_clock_fps.py,NIO,flaky,InspiredAFix,https://github.com/pyglet/pyglet/pull/514,classify;root_cause,"""""""Tests clock timing between frames and estimations of frames per second. """""" import time import pytest from pyglet import clock from ..annotations import skip_if_continuous_integration def sleep(seconds): """"""Busy sleep on the CPU which is very precise"""""" pyclock = clock.get_default() start = pyclock.time() while pyclock.time() - start < seconds: pass # since clock is global, we initialize a new clock on every test clock.set_default(clock.Clock()) def test_first_tick_is_delta_zero(): """""" Tests that the first tick is dt = 0. """""" dt = clock.tick() assert dt == 0 def test_start_at_zero_fps(): """""" Tests that the default clock starts with zero fps. """""" assert clock.get_fps() == 0 @skip_if_continuous_integration() def test_elapsed_time_between_tick(): """""" Test that the tick function returns the correct elapsed time between frames, in seconds. Because we are measuring time differences, we expect a small error (1%) from the expected value. """""" sleep_time = 0.2 # initialize internal counter clock.tick() # test between initialization and first tick sleep(sleep_time) delta_time_1 = clock.tick() # test between non-initialization tick and next tick sleep(sleep_time) delta_time_2 = clock.tick() assert delta_time_1 == pytest.approx(sleep_time, rel=0.01*sleep_time) assert delta_time_2 == pytest.approx(sleep_time, rel=0.01*sleep_time) @skip_if_continuous_integration() def test_compute_fps(): """""" Test that the clock computes a reasonable value of frames per second when simulated for 120 ticks at 60 frames per second. Because sleep is not very precise and fps are unbounded, we expect a moderate error (10%) from the expected value. """""" ticks = 120 # for averaging expected_fps = 60 seconds_per_tick = 1./expected_fps for i in range(ticks): time.sleep(seconds_per_tick) clock.tick() computed_fps = clock.get_fps() assert computed_fps == pytest.approx(expected_fps, rel=0.1*expected_fps) ", https://github.com/pyglet/pyglet,9c2b638f7474045d7db6578924e4f96324ea7158,tests/unit/test_clock_fps.py::test_start_at_zero_fps,tests/unit/test_clock_fps.py,NIO,flaky,InspiredAFix,https://github.com/pyglet/pyglet/pull/514,classify;root_cause,"""""""Tests clock timing between frames and estimations of frames per second. """""" import time import pytest from pyglet import clock from ..annotations import skip_if_continuous_integration def sleep(seconds): """"""Busy sleep on the CPU which is very precise"""""" pyclock = clock.get_default() start = pyclock.time() while pyclock.time() - start < seconds: pass # since clock is global, we initialize a new clock on every test clock.set_default(clock.Clock()) def test_first_tick_is_delta_zero(): """""" Tests that the first tick is dt = 0. """""" dt = clock.tick() assert dt == 0 def test_start_at_zero_fps(): """""" Tests that the default clock starts with zero fps. """""" assert clock.get_fps() == 0 @skip_if_continuous_integration() def test_elapsed_time_between_tick(): """""" Test that the tick function returns the correct elapsed time between frames, in seconds. Because we are measuring time differences, we expect a small error (1%) from the expected value. """""" sleep_time = 0.2 # initialize internal counter clock.tick() # test between initialization and first tick sleep(sleep_time) delta_time_1 = clock.tick() # test between non-initialization tick and next tick sleep(sleep_time) delta_time_2 = clock.tick() assert delta_time_1 == pytest.approx(sleep_time, rel=0.01*sleep_time) assert delta_time_2 == pytest.approx(sleep_time, rel=0.01*sleep_time) @skip_if_continuous_integration() def test_compute_fps(): """""" Test that the clock computes a reasonable value of frames per second when simulated for 120 ticks at 60 frames per second. Because sleep is not very precise and fps are unbounded, we expect a moderate error (10%) from the expected value. """""" ticks = 120 # for averaging expected_fps = 60 seconds_per_tick = 1./expected_fps for i in range(ticks): time.sleep(seconds_per_tick) clock.tick() computed_fps = clock.get_fps() assert computed_fps == pytest.approx(expected_fps, rel=0.1*expected_fps) ", https://github.com/pythological/kanren,bacc7eb5895c348e348c1b5291ddf188f88c5c88,tests/test_assoccomm.py::test_eq_assoccomm,tests/test_assoccomm.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest from collections.abc import Sequence from etuples.core import etuple from cons import cons from unification import reify, var, isvar, unify from kanren.core import run from kanren.facts import fact from kanren.assoccomm import ( associative, commutative, eq_assoc_args, eq_comm, eq_assoc, eq_assoccomm, assoc_args, flatten_assoc_args, assoc_flatten, ) from kanren.term import operator, arguments, term class Node(object): def __init__(self, op, args): self.op = op self.args = args def __eq__(self, other): return ( type(self) == type(other) and self.op == other.op and self.args == other.args ) def __hash__(self): return hash((type(self), self.op, self.args)) def __str__(self): return ""%s(%s)"" % (self.op.name, "", "".join(map(str, self.args))) __repr__ = __str__ class Operator(object): def __init__(self, name): self.name = name Add = Operator(""add"") Mul = Operator(""mul"") def add(*args): return Node(Add, args) def mul(*args): return Node(Mul, args) @term.register(Operator, Sequence) def term_Operator(op, args): return Node(op, args) @arguments.register(Node) def arguments_Node(n): return n.args @operator.register(Node) def operator_Node(n): return n.op def results(g, s=None): if s is None: s = dict() return tuple(g(s)) def test_eq_comm(): x, y, z = var(), var(), var() commutative.facts.clear() commutative.index.clear() comm_op = ""comm_op"" fact(commutative, comm_op) assert run(0, True, eq_comm(1, 1)) == (True,) assert run(0, True, eq_comm((comm_op, 1, 2, 3), (comm_op, 1, 2, 3))) == (True,) assert run(0, True, eq_comm((comm_op, 3, 2, 1), (comm_op, 1, 2, 3))) == (True,) assert run(0, y, eq_comm((comm_op, 3, y, 1), (comm_op, 1, 2, 3))) == (2,) assert run(0, (x, y), eq_comm((comm_op, x, y, 1), (comm_op, 1, 2, 3))) == ( (2, 3), (3, 2), ) assert run(0, (x, y), eq_comm((comm_op, 2, 3, 1), (comm_op, 1, x, y))) == ( (2, 3), (3, 2), ) assert not run( 0, True, eq_comm((""op"", 3, 2, 1), (""op"", 1, 2, 3)) ) # not commutative assert not run(0, True, eq_comm((3, comm_op, 2, 1), (comm_op, 1, 2, 3))) assert not run(0, True, eq_comm((comm_op, 1, 2, 1), (comm_op, 1, 2, 3))) assert not run(0, True, eq_comm((""op"", 1, 2, 3), (comm_op, 1, 2, 3))) # Test for variable args res = run(4, (x, y), eq_comm(x, y)) exp_res_form = ( (etuple(comm_op, x, y), etuple(comm_op, y, x)), (x, y), (etuple(etuple(comm_op, x, y)), etuple(etuple(comm_op, y, x))), (etuple(comm_op, x, y, z), etuple(comm_op, x, z, y)), ) for a, b in zip(res, exp_res_form): s = unify(a, b) assert s is not False assert all(isvar(i) for i in reify((x, y, z), s)) # Make sure it can unify single elements assert (3,) == run(0, x, eq_comm((comm_op, 1, 2, 3), (comm_op, 2, x, 1))) # `eq_comm` should propagate through assert (3,) == run( 0, x, eq_comm((""div"", 1, (comm_op, 1, 2, 3)), (""div"", 1, (comm_op, 2, x, 1))) ) # Now it should not assert () == run( 0, x, eq_comm((""div"", 1, (""div"", 1, 2, 3)), (""div"", 1, (""div"", 2, x, 1))) ) expected_res = {(1, 2, 3), (2, 1, 3), (3, 1, 2), (1, 3, 2), (2, 3, 1), (3, 2, 1)} assert expected_res == set( run(0, (x, y, z), eq_comm((comm_op, 1, 2, 3), (comm_op, x, y, z))) ) assert expected_res == set( run(0, (x, y, z), eq_comm((comm_op, x, y, z), (comm_op, 1, 2, 3))) ) assert expected_res == set( run( 0, (x, y, z), eq_comm((""div"", 1, (comm_op, 1, 2, 3)), (""div"", 1, (comm_op, x, y, z))), ) ) e1 = (comm_op, (comm_op, 1, x), y) e2 = (comm_op, 2, (comm_op, 3, 1)) assert run(0, (x, y), eq_comm(e1, e2)) == ((3, 2),) e1 = ((comm_op, 3, 1),) e2 = ((comm_op, 1, x),) assert run(0, x, eq_comm(e1, e2)) == (3,) e1 = (2, (comm_op, 3, 1)) e2 = (y, (comm_op, 1, x)) assert run(0, (x, y), eq_comm(e1, e2)) == ((3, 2),) e1 = (comm_op, (comm_op, 1, x), y) e2 = (comm_op, 2, (comm_op, 3, 1)) assert run(0, (x, y), eq_comm(e1, e2)) == ((3, 2),) @pytest.mark.xfail(reason=""`applyo`/`buildo` needs to be a constraint."", strict=True) def test_eq_comm_object(): x = var(""x"") fact(commutative, Add) fact(associative, Add) assert run(0, x, eq_comm(add(1, 2, 3), add(3, 1, x))) == (2,) assert set(run(0, x, eq_comm(add(1, 2), x))) == set((add(1, 2), add(2, 1))) assert set(run(0, x, eq_assoccomm(add(1, 2, 3), add(1, x)))) == set( (add(2, 3), add(3, 2)) ) def test_flatten_assoc_args(): op = ""add"" def op_pred(x): return x == op assert list(flatten_assoc_args(op_pred, [op, 1, 2, 3, 4])) == [op, 1, 2, 3, 4] assert list(flatten_assoc_args(op_pred, [op, 1, 2, [op]])) == [op, 1, 2, [op]] assert list(flatten_assoc_args(op_pred, [[op, 1, 2, [op]]])) == [1, 2, [op]] res = list( flatten_assoc_args( op_pred, [[1, 2, op], 3, [op, 4, [op, [op]]], [op, 5], 6, op, 7] ) ) exp_res = [[1, 2, op], 3, 4, [op], 5, 6, op, 7] assert res == exp_res def test_assoc_args(): op = ""add"" def op_pred(x): return x == op assert tuple(assoc_args(op, (1, 2, 3), 2)) == (((op, 1, 2), 3), (1, (op, 2, 3)),) assert tuple(assoc_args(op, [1, 2, 3], 2)) == ([[op, 1, 2], 3], [1, [op, 2, 3]],) assert tuple(assoc_args(op, (1, 2, 3), 1)) == ( ((op, 1), 2, 3), (1, (op, 2), 3), (1, 2, (op, 3)), ) assert tuple(assoc_args(op, (1, 2, 3), 3)) == ((1, 2, 3),) f_rands = flatten_assoc_args(op_pred, (1, (op, 2, 3))) assert tuple(assoc_args(op, f_rands, 2, ctor=tuple)) == ( ((op, 1, 2), 3), (1, (op, 2, 3)), ) def test_eq_assoc_args(): assoc_op = ""assoc_op"" fact(associative, assoc_op) assert not run(0, True, eq_assoc_args(assoc_op, (1,), [1], n=None)) assert run(0, True, eq_assoc_args(assoc_op, (1,), (1,), n=None)) == (True,) assert run(0, True, eq_assoc_args(assoc_op, (1, 1), (1, 1))) == (True,) assert run(0, True, eq_assoc_args(assoc_op, (1, 2, 3), (1, (assoc_op, 2, 3)))) == ( True, ) assert run(0, True, eq_assoc_args(assoc_op, (1, (assoc_op, 2, 3)), (1, 2, 3))) == ( True, ) assert run( 0, True, eq_assoc_args(assoc_op, (1, (assoc_op, 2, 3), 4), (1, 2, 3, 4)) ) == (True,) assert not run( 0, True, eq_assoc_args(assoc_op, (1, 2, 3), (1, (assoc_op, 2, 3), 4)) ) x, y = var(), var() assert run(0, True, eq_assoc_args(assoc_op, (x,), (x,), n=None)) == (True,) assert run(0, x, eq_assoc_args(assoc_op, x, (y,), n=None)) == ((y,),) assert run(0, x, eq_assoc_args(assoc_op, (y,), x, n=None)) == ((y,),) assert run(0, x, eq_assoc_args(assoc_op, (1, x, 4), (1, 2, 3, 4))) == ( (assoc_op, 2, 3), ) assert run(0, x, eq_assoc_args(assoc_op, (1, 2, 3, 4), (1, x, 4))) == ( (assoc_op, 2, 3), ) assert run(0, x, eq_assoc_args(assoc_op, [1, x, 4], [1, 2, 3, 4])) == ( [assoc_op, 2, 3], ) assert run(0, True, eq_assoc_args(assoc_op, (1, 1), (""other_op"", 1, 1))) == () assert run(0, x, eq_assoc_args(assoc_op, (1, 2, 3), x, n=2)) == ( ((assoc_op, 1, 2), 3), (1, (assoc_op, 2, 3)), ) assert run(0, x, eq_assoc_args(assoc_op, x, (1, 2, 3), n=2)) == ( ((assoc_op, 1, 2), 3), (1, (assoc_op, 2, 3)), ) assert run(0, x, eq_assoc_args(assoc_op, (1, 2, 3), x)) == ( ((assoc_op, 1, 2), 3), (1, (assoc_op, 2, 3)), (1, 2, 3), ) assert () not in run(0, x, eq_assoc_args(assoc_op, (), x, no_ident=True)) assert (1,) not in run(0, x, eq_assoc_args(assoc_op, (1,), x, no_ident=True)) assert (1, 2, 3) not in run( 0, x, eq_assoc_args(assoc_op, (1, 2, 3), x, no_ident=True) ) assert ( run( 0, True, eq_assoc_args( assoc_op, (1, (assoc_op, 2, 3)), (1, (assoc_op, 2, 3)), no_ident=True, ), ) == () ) assert run( 0, True, eq_assoc_args( assoc_op, (1, (assoc_op, 2, 3)), ((assoc_op, 1, 2), 3), no_ident=True, ), ) == (True,) def test_eq_assoc(): assoc_op = ""assoc_op"" associative.index.clear() associative.facts.clear() fact(associative, assoc_op) assert run(0, True, eq_assoc(1, 1)) == (True,) assert run(0, True, eq_assoc((assoc_op, 1, 2, 3), (assoc_op, 1, 2, 3))) == (True,) assert not run(0, True, eq_assoc((assoc_op, 3, 2, 1), (assoc_op, 1, 2, 3))) assert run( 0, True, eq_assoc((assoc_op, (assoc_op, 1, 2), 3), (assoc_op, 1, 2, 3)) ) == (True,) assert run( 0, True, eq_assoc((assoc_op, 1, 2, 3), (assoc_op, (assoc_op, 1, 2), 3)) ) == (True,) o = ""op"" assert not run(0, True, eq_assoc((o, 1, 2, 3), (o, (o, 1, 2), 3))) x = var() res = run(0, x, eq_assoc((assoc_op, 1, 2, 3), x, n=2)) assert res == ( (assoc_op, (assoc_op, 1, 2), 3), (assoc_op, 1, 2, 3), (assoc_op, 1, (assoc_op, 2, 3)), ) res = run(0, x, eq_assoc(x, (assoc_op, 1, 2, 3), n=2)) assert res == ( (assoc_op, (assoc_op, 1, 2), 3), (assoc_op, 1, 2, 3), (assoc_op, 1, (assoc_op, 2, 3)), ) y, z = var(), var() # Check results when both arguments are variables res = run(3, (x, y), eq_assoc(x, y)) exp_res_form = ( (etuple(assoc_op, x, y, z), etuple(assoc_op, etuple(assoc_op, x, y), z)), (x, y), ( etuple(etuple(assoc_op, x, y, z)), etuple(etuple(assoc_op, etuple(assoc_op, x, y), z)), ), ) for a, b in zip(res, exp_res_form): ", https://github.com/python-dugong/python-dugong,00dd4d5aab6eddb116cace2829a9f93bda153664,test/test_aio.py::test_aio_future,test/test_aio.py,NIO,flaky,Accepted,https://github.com/python-dugong/python-dugong/pull/9,classify;root_cause;fix_proposal,"''' test_aio.py - Unit tests for Dugong Copyright © 2014 Nikolaus Rath This module may be distributed under the terms of the Python Software Foundation License Version 2. The complete license text may be retrieved from http://hg.python.org/cpython/file/65f2c92ed079/LICENSE. ''' if __name__ == '__main__': import sys import pytest sys.exit(pytest.main([__file__] + sys.argv[1:])) import socket import logging from dugong import PollNeeded, POLLIN try: import asyncio except ImportError: import pytest pytestmark = pytest.mark.skipif(True, reason='asyncio module not available') else: from dugong import AioFuture log = logging.getLogger(__name__) def read(sock): for i in range(3): log.debug('yielding') yield PollNeeded(sock.fileno(), POLLIN) log.debug('trying to read') buf = sock.recv(100) assert buf.decode() == 'text-%03d' % i log.debug('got: %s', buf) @asyncio.coroutine def write(sock): for i in range(3): log.debug('sleeping') yield from asyncio.sleep(1) buf = ('text-%03d' % i).encode() log.debug('writing %s', buf) sock.send(buf) def test_aio_future(): loop = asyncio.get_event_loop() try: (sock1, sock2) = socket.socketpair() asyncio.Task(write(sock2)) read_fut = AioFuture(read(sock1)) read_fut.add_done_callback(lambda fut: loop.stop()) loop.call_later(6, loop.stop) loop.run_forever() assert read_fut.done() sock1.close() sock2.close() finally: loop.close() ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__encode_error_with_debug_enabled,tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__error_handled_by_middleware,tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__error_handled_by_middleware_raises_exception,tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__error_with_debug_enabled,tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__exceptions[error0-HTTPStatus.NOT_MODIFIED],tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__exceptions[error1-400],tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__exceptions[error2-400],tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__exceptions[error5-500],tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__exceptions[NotImplementedError-501],tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__exceptions[ValueError-500],tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__http_response,tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__invalid_headers[r1-406-URI not available in preferred format],tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__invalid_headers[r2-405-Specified method is invalid for this resource],tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/python-odin/odinweb,198424133584acc18cb41c8d18d91f803abc810f,tests/test_containers.py::TestApiInterfaceBase::test_dispatch__with_middleware,tests/test_containers.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import pytest from odinweb.resources import Error from odin.exceptions import ValidationError from odinweb import api from odinweb import containers from odinweb.constants import Method, HTTPStatus from odinweb.data_structures import NoPath, UrlPath, HttpResponse from odinweb.decorators import Operation from odinweb.helpers import create_response from odinweb.testing import MockRequest from .resources import User ################################################# # Mocks def mock_callback(self, request, **kwargs): return 'returned' class UserApi(containers.ResourceApi): resource = User def __init__(self): super(UserApi, self).__init__() self.calls = [] @api.collection def list_items(self, request): self.calls.append('list_items') return [User(1, 'Dave'), User(2, 'Bob')] @api.detail def get_item(self, request, resource_id): self.calls.append('get_item') return User(resource_id, 'Dave') @api.action(path='start', methods=Method.POST) def start_item(self, request): self.calls.append('start_item') return create_response(request, status=202) def mock_callback(self, request, **path_args): self.calls.append('mock_callback') return path_args class MockResourceApi(object): def op_paths(self, path_base): yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.GET) yield path_base + UrlPath.parse('a/b'), Operation(mock_callback, UrlPath.parse('a/b'), Method.POST) yield path_base + UrlPath.parse('d/e'), Operation(mock_callback, UrlPath.parse('d/e'), (Method.POST, Method.PATCH)) class MockApiInterface(containers.ApiInterfaceBase): pass ################################################ # Tests class TestResourceApiMeta(object): def test_empty_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): pass assert ExampleApi._operations == [] def test_normal_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class ExampleApi(api.ResourceApi): @api.collection def list_items(self, request): pass @api.detail def get_item(self, request, resource_id): pass @api.Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert ExampleApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] def test_sub_classed_api(self, mocker): mocker.patch('odinweb.decorators.Operation._operation_count', 0) class SuperApi(api.ResourceApi): @api.collection def list_items(self, request): pass class SubApi(SuperApi): @api.detail def get_item(self, request, resource_id): pass @Operation(methods=(Method.POST, Method.PUT)) def create_item(self, request): pass assert SubApi._operations == [ Operation(mock_callback, NoPath, Method.GET), Operation(mock_callback, '{resource_id}', Method.GET), Operation(mock_callback, NoPath, (Method.POST, Method.PUT)), ] class TestResourceApi(object): def test_api_name__default(self): target = UserApi() assert target.api_name == 'user' def test_api_name__custom(self): class Example(api.ResourceApi): resource = User api_name = 'users' target = Example() assert target.api_name == 'users' def test_op_paths(self): target = UserApi() actual = dict(target.op_paths(NoPath)) assert actual == { UrlPath.parse('user'): Operation(mock_callback, NoPath, methods=Method.GET), UrlPath.parse('user/{resource_id}'): Operation(mock_callback, '{resource_id}', methods=Method.GET), UrlPath.parse('user/start'): Operation(mock_callback, 'start', methods=Method.POST), } class TestApiContainer(object): @pytest.mark.parametrize('options,attr,value', ( ({}, 'name', None), ({'name': 'foo'}, 'name', 'foo'), ({}, 'path_prefix', UrlPath()), ({'name': 'foo'}, 'path_prefix', UrlPath('foo')), ({'path_prefix': ['bar']}, 'path_prefix', UrlPath('bar')), )) def test_options(self, options, attr, value): target = containers.ApiContainer(**options) assert hasattr(target, attr) assert getattr(target, attr) == value def test_extra_option(self): with pytest.raises(TypeError, message=""Got an unexpected keyword argument 'foo'""): containers.ApiContainer(foo=1, name='test') with pytest.raises(TypeError): containers.ApiContainer(foo=1, bar=2) def test_op_paths(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths('c')) assert actual == { UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('c/a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('c/d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } def test_op_paths__no_sub_path(self): target = containers.ApiContainer(MockResourceApi()) actual = dict(target.op_paths()) assert actual == { UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.GET), UrlPath.parse('a/b'): Operation(mock_callback, 'a/b', Method.POST), UrlPath.parse('d/e'): Operation(mock_callback, 'd/e', (Method.POST, Method.PATCH)), } class TestApiCollection(object): """""" Actually test with ApiVersion is this is a thin layer over a collection. """""" @pytest.mark.parametrize('options,version,name,path_prefix', ( ({}, 1, 'v1', UrlPath('v1')), ({'version': 2}, 2, 'v2', UrlPath('v2')), ({'version': 3, 'name': 'version-3'}, 3, 'version-3', UrlPath('version-3')), )) def test_version_options(self, options, version, name, path_prefix): target = containers.ApiVersion(**options) assert target.version == version assert target.name == name assert target.path_prefix == path_prefix def test_register_operation(self): target = containers.ApiCollection() @target.operation(""a/b"") def my_operation(request): pass actual = dict(target.op_paths()) assert len(actual) == 1 assert actual == { UrlPath.parse(""a/b""): Operation(mock_callback, 'a/b') } class TestApiInterfaceBase(object): @pytest.mark.parametrize('options,name,debug_enabled,path_prefix', ( ({}, 'api', False, UrlPath.parse('/api')), ({'name': '!api'}, '!api', False, UrlPath.parse('/!api')), ({'path_prefix': '/my-app/'}, 'api', False, UrlPath.parse('/my-app')), ({'debug_enabled': True}, 'api', True, UrlPath.parse('/api')), )) def test_options(self, options, name, debug_enabled, path_prefix): target = containers.ApiInterfaceBase(**options) assert target.name == name assert target.debug_enabled == debug_enabled assert target.path_prefix == path_prefix def test_init_non_absolute(self): with pytest.raises(ValueError): containers.ApiInterfaceBase(path_prefix='ab/c') def test_dispatch(self): pass @pytest.mark.parametrize('r, status, message', ( (MockRequest(headers={'content-type': 'application/xml', 'accepts': 'application/json'}), 422, 'Unprocessable Entity'), (MockRequest(headers={'content-type': 'application/json', 'accepts': 'application/xml'}), 406, 'URI not available in preferred format'), (MockRequest(method=Method.POST), 405, 'Specified method is invalid for this resource'), )) def test_dispatch__invalid_headers(self, r, status, message): target = containers.ApiInterfaceBase() operation = Operation(mock_callback) actual = target.dispatch(operation, r) assert actual.status == status assert actual.body == message @pytest.mark.parametrize('error,status', ( (api.ImmediateHttpResponse(None, HTTPStatus.NOT_MODIFIED, {}), HTTPStatus.NOT_MODIFIED), (ValidationError(""Error""), 400), (ValidationError({}), 400), (NotImplementedError, 501), (ValueError, 500), (api.ImmediateHttpResponse(ValueError, HTTPStatus.NOT_MODIFIED, {}), 500), )) def test_dispatch__exceptions(self, error, status): def callback(request): raise error target = containers.ApiInterfaceBase() operation = Operation(callback) actual = target.dispatch(operation, MockRequest()) assert actual.status == status def test_dispatch__with_middleware(self): calls = [] class Middleware(object): def pre_request(self, request, path_args): calls.append('pre_request') def pre_dispatch(self, request, path_args): calls.append('pre_dispatch') path_args['foo'] = 'bar' def post_dispatch(self, request, response): calls.append('post_dispatch') return 'eek' + response def post_request(self, request, response): calls.append('post_request') response['test'] = 'header' return response def callback(request, **args): assert args['foo'] == 'bar' ", https://github.com/qiyangduan/schemaindex,83bfb96c78d194370b068e11f89a9e4d6af48a46,test/test_plugin_mysql_hdfs.py::test_plugin_hdfs_add,test/test_plugin_mysql_hdfs.py,NIO,flaky,Opened,https://github.com/qiyangduan/schemaindex/pull/1,classify;root_cause,"from __future__ import unicode_literals from __future__ import print_function # import pytest from schemaindex.app.config import cfg from schemaindex.app.schemaindexapp import si_app from schemaindex.app.pluginmanager import si_pm import os @pytest.fixture(scope='module') def resource_a_setup(request): print('\nDoing setup by putting init file') db_file_path = cfg['database']['sqlite_file'] # open(config['main']['init_indicator_file'], 'a').close() if os.path.exists(db_file_path): print('DB file is ready, going to delete it .') os.remove(db_file_path) si_app.schemaindex_init(db_file_path = db_file_path) def fin(): print (""\nDoing teardown"") request.addfinalizer(fin) ds_dict = { 'ds_name': 'emp1', 'ds_type': 'sqlalchemy', 'ds_desc': 'created by unittest', 'ds_param': {'connect_string': 'sqlite:////home/duan/schemaindex.sqlite3.goodbackup', 'schema_name': 'na', } } def test_plugin_loading(resource_a_setup): list1 = si_app.get_plugin_name_list() print(list1) assert len(list1) == 0 si_pm.scan_reflect_plugins() list1 = si_app.get_plugin_name_list() assert 'simplehdfsindex' in list1 def test_plugin_mysql_add(resource_a_setup): dict1 = si_app.get_data_source_dict(ds_name=ds_dict['ds_name']) assert dict1 is None si_app.add_data_soruce(ds_dict) dict1 = si_app.get_data_source_dict(ds_name=ds_dict['ds_name']) assert dict1['ds_param']['connect_string'] == ds_dict['ds_param']['connect_string'] def test_plugin_mysql_reflect(after='test_plugin_mysql_add'): si_pm.reflect_db(data_source_name=ds_dict['ds_name']) suggest1=si_app.get_whoosh_search_suggestion(q='mta') print(suggest1) assert 'mtable' in suggest1 hdfs_ds_dict = { 'ds_name': 'hdfs1', 'ds_type': 'hdfsindex', 'ds_desc': 'created by unittest of hdfsindex', 'ds_url': 'http://localhost:50070', 'ds_param': {'hdfs_web_url': 'http://localhost:50070', 'hdfs_url': 'hdfs://localhost:9000', 'start_inotify': 'off', 'root_path':'/', 'inotify_trx_id':'-1', } } def test_plugin_hdfs_add(): dict1 = si_app.get_data_source_dict(ds_name=hdfs_ds_dict['ds_name']) assert dict1 is None si_app.add_data_soruce(hdfs_ds_dict) dict1 = si_app.get_data_source_dict(ds_name=hdfs_ds_dict['ds_name']) assert dict1['ds_param']['hdfs_web_url'] == 'http://localhost:50070' def test_plugin_hdfs_reflect(after='test_plugin_hdfs_add_reflect'): si_pm.reflect_db(data_source_name=hdfs_ds_dict['ds_name']) suggest1 = si_app.get_whoosh_search_suggestion(q='citi') print(suggest1) assert 'cities' in suggest1 #assert 'dept_name' in suggest1 ", https://github.com/qiyangduan/schemaindex,83bfb96c78d194370b068e11f89a9e4d6af48a46,test/test_plugin_mysql_hdfs.py::test_plugin_loading,test/test_plugin_mysql_hdfs.py,NIO,flaky,Opened,https://github.com/qiyangduan/schemaindex/pull/2,classify;root_cause,"from __future__ import unicode_literals from __future__ import print_function # import pytest from schemaindex.app.config import cfg from schemaindex.app.schemaindexapp import si_app from schemaindex.app.pluginmanager import si_pm import os @pytest.fixture(scope='module') def resource_a_setup(request): print('\nDoing setup by putting init file') db_file_path = cfg['database']['sqlite_file'] # open(config['main']['init_indicator_file'], 'a').close() if os.path.exists(db_file_path): print('DB file is ready, going to delete it .') os.remove(db_file_path) si_app.schemaindex_init(db_file_path = db_file_path) def fin(): print (""\nDoing teardown"") request.addfinalizer(fin) ds_dict = { 'ds_name': 'emp1', 'ds_type': 'sqlalchemy', 'ds_desc': 'created by unittest', 'ds_param': {'connect_string': 'sqlite:////home/duan/schemaindex.sqlite3.goodbackup', 'schema_name': 'na', } } def test_plugin_loading(resource_a_setup): list1 = si_app.get_plugin_name_list() print(list1) assert len(list1) == 0 si_pm.scan_reflect_plugins() list1 = si_app.get_plugin_name_list() assert 'simplehdfsindex' in list1 def test_plugin_mysql_add(resource_a_setup): dict1 = si_app.get_data_source_dict(ds_name=ds_dict['ds_name']) assert dict1 is None si_app.add_data_soruce(ds_dict) dict1 = si_app.get_data_source_dict(ds_name=ds_dict['ds_name']) assert dict1['ds_param']['connect_string'] == ds_dict['ds_param']['connect_string'] def test_plugin_mysql_reflect(after='test_plugin_mysql_add'): si_pm.reflect_db(data_source_name=ds_dict['ds_name']) suggest1=si_app.get_whoosh_search_suggestion(q='mta') print(suggest1) assert 'mtable' in suggest1 hdfs_ds_dict = { 'ds_name': 'hdfs1', 'ds_type': 'hdfsindex', 'ds_desc': 'created by unittest of hdfsindex', 'ds_url': 'http://localhost:50070', 'ds_param': {'hdfs_web_url': 'http://localhost:50070', 'hdfs_url': 'hdfs://localhost:9000', 'start_inotify': 'off', 'root_path':'/', 'inotify_trx_id':'-1', } } def test_plugin_hdfs_add(): dict1 = si_app.get_data_source_dict(ds_name=hdfs_ds_dict['ds_name']) assert dict1 is None si_app.add_data_soruce(hdfs_ds_dict) dict1 = si_app.get_data_source_dict(ds_name=hdfs_ds_dict['ds_name']) assert dict1['ds_param']['hdfs_web_url'] == 'http://localhost:50070' def test_plugin_hdfs_reflect(after='test_plugin_hdfs_add_reflect'): si_pm.reflect_db(data_source_name=hdfs_ds_dict['ds_name']) suggest1 = si_app.get_whoosh_search_suggestion(q='citi') print(suggest1) assert 'cities' in suggest1 #assert 'dept_name' in suggest1 ", https://github.com/qiyangduan/schemaindex,83bfb96c78d194370b068e11f89a9e4d6af48a46,test/test_plugin_mysql_hdfs.py::test_plugin_mysql_add,test/test_plugin_mysql_hdfs.py,NIO,flaky,Opened,https://github.com/qiyangduan/schemaindex/pull/1,classify;root_cause,"from __future__ import unicode_literals from __future__ import print_function # import pytest from schemaindex.app.config import cfg from schemaindex.app.schemaindexapp import si_app from schemaindex.app.pluginmanager import si_pm import os @pytest.fixture(scope='module') def resource_a_setup(request): print('\nDoing setup by putting init file') db_file_path = cfg['database']['sqlite_file'] # open(config['main']['init_indicator_file'], 'a').close() if os.path.exists(db_file_path): print('DB file is ready, going to delete it .') os.remove(db_file_path) si_app.schemaindex_init(db_file_path = db_file_path) def fin(): print (""\nDoing teardown"") request.addfinalizer(fin) ds_dict = { 'ds_name': 'emp1', 'ds_type': 'sqlalchemy', 'ds_desc': 'created by unittest', 'ds_param': {'connect_string': 'sqlite:////home/duan/schemaindex.sqlite3.goodbackup', 'schema_name': 'na', } } def test_plugin_loading(resource_a_setup): list1 = si_app.get_plugin_name_list() print(list1) assert len(list1) == 0 si_pm.scan_reflect_plugins() list1 = si_app.get_plugin_name_list() assert 'simplehdfsindex' in list1 def test_plugin_mysql_add(resource_a_setup): dict1 = si_app.get_data_source_dict(ds_name=ds_dict['ds_name']) assert dict1 is None si_app.add_data_soruce(ds_dict) dict1 = si_app.get_data_source_dict(ds_name=ds_dict['ds_name']) assert dict1['ds_param']['connect_string'] == ds_dict['ds_param']['connect_string'] def test_plugin_mysql_reflect(after='test_plugin_mysql_add'): si_pm.reflect_db(data_source_name=ds_dict['ds_name']) suggest1=si_app.get_whoosh_search_suggestion(q='mta') print(suggest1) assert 'mtable' in suggest1 hdfs_ds_dict = { 'ds_name': 'hdfs1', 'ds_type': 'hdfsindex', 'ds_desc': 'created by unittest of hdfsindex', 'ds_url': 'http://localhost:50070', 'ds_param': {'hdfs_web_url': 'http://localhost:50070', 'hdfs_url': 'hdfs://localhost:9000', 'start_inotify': 'off', 'root_path':'/', 'inotify_trx_id':'-1', } } def test_plugin_hdfs_add(): dict1 = si_app.get_data_source_dict(ds_name=hdfs_ds_dict['ds_name']) assert dict1 is None si_app.add_data_soruce(hdfs_ds_dict) dict1 = si_app.get_data_source_dict(ds_name=hdfs_ds_dict['ds_name']) assert dict1['ds_param']['hdfs_web_url'] == 'http://localhost:50070' def test_plugin_hdfs_reflect(after='test_plugin_hdfs_add_reflect'): si_pm.reflect_db(data_source_name=hdfs_ds_dict['ds_name']) suggest1 = si_app.get_whoosh_search_suggestion(q='citi') print(suggest1) assert 'cities' in suggest1 #assert 'dept_name' in suggest1 ", https://github.com/Qordobacode/fitbert,4b0f2f3f47e5a3fea0ee59d65f71c44b830a93d9,fitbert/tests.py::test_delemmatizes_lemmas,fitbert/tests.py,NIO,flaky,Opened,https://github.com/Qordobacode/fitbert/pull/23,classify;root_cause,"import pytest from fitbert import FitBert from fitbert.delemmatize import Delemmatizer dl = Delemmatizer() def test_delemmatizer_instantiates(): assert Delemmatizer() is not None, ""It instantiates"" def test_delemmatizer_callable(): assert callable(dl), ""Delemmatizer instance should be callable"" def test_delemmatizes_lemmas(): assert dl(""look"") == [ ""looked"", ""looking"", ""looks"", ""look"", ], ""should delemmatize lemmas"" def test_delemmatizes_non_lemmas(): assert dl(""ran"") == [ ""ran"", ""running"", ""runs"", ""run"", ], ""should delemmatize non-lemmas"" """""" def test_masker_works_without_instantiating(): masked_string, masked = FitBert.mask( ""This might be justified to signalling the connection between drunken driving and fatal accidents."", (27, 37), ) assert FitBert.mask_token in masked_string, ""It should mask using the mask token"" assert masked == ""signalling"", ""It should mask the write substring"" """""" @pytest.mark.slow def test_ranking(): fb = FitBert(model_name=""distilbert-base-uncased"") assert callable(fb.fitb) sentences = [ ""When she started talking about her ex-boyfriends, he looked like a ***mask*** out of water"", ""The boy was warned that if he misbehaved in the class, he would have to pay ***mask***."", ""I am surprised that you have ***mask*** patience."", ] options = [ [""frog"", ""fish""], [""the drummer"", ""the flutist"", ""the piper""], [""such a"", ""so"", ""such""], ] answers = [""fish"", ""the piper"", ""such""] for sentence, option, answer in zip(sentences, options, answers): ranked_options = fb.rank(sentence, option) assert ranked_options[0] == answer, ""It should rank options"" sentence = ""Psychology includes the study of conscious and unconscious phenomena, as well as ***mask*** and thought."" options = [""feelings""] answer = ""feeling"" ranked_options = fb.rank(sentence, options, True) assert ranked_options[0] == answer, ""It should find and rank related options"" ", https://github.com/Qordobacode/fitbert,4b0f2f3f47e5a3fea0ee59d65f71c44b830a93d9,fitbert/tests.py::test_delemmatizes_non_lemmas,fitbert/tests.py,NIO,flaky,Opened,https://github.com/Qordobacode/fitbert/pull/23,classify;root_cause,"import pytest from fitbert import FitBert from fitbert.delemmatize import Delemmatizer dl = Delemmatizer() def test_delemmatizer_instantiates(): assert Delemmatizer() is not None, ""It instantiates"" def test_delemmatizer_callable(): assert callable(dl), ""Delemmatizer instance should be callable"" def test_delemmatizes_lemmas(): assert dl(""look"") == [ ""looked"", ""looking"", ""looks"", ""look"", ], ""should delemmatize lemmas"" def test_delemmatizes_non_lemmas(): assert dl(""ran"") == [ ""ran"", ""running"", ""runs"", ""run"", ], ""should delemmatize non-lemmas"" """""" def test_masker_works_without_instantiating(): masked_string, masked = FitBert.mask( ""This might be justified to signalling the connection between drunken driving and fatal accidents."", (27, 37), ) assert FitBert.mask_token in masked_string, ""It should mask using the mask token"" assert masked == ""signalling"", ""It should mask the write substring"" """""" @pytest.mark.slow def test_ranking(): fb = FitBert(model_name=""distilbert-base-uncased"") assert callable(fb.fitb) sentences = [ ""When she started talking about her ex-boyfriends, he looked like a ***mask*** out of water"", ""The boy was warned that if he misbehaved in the class, he would have to pay ***mask***."", ""I am surprised that you have ***mask*** patience."", ] options = [ [""frog"", ""fish""], [""the drummer"", ""the flutist"", ""the piper""], [""such a"", ""so"", ""such""], ] answers = [""fish"", ""the piper"", ""such""] for sentence, option, answer in zip(sentences, options, answers): ranked_options = fb.rank(sentence, option) assert ranked_options[0] == answer, ""It should rank options"" sentence = ""Psychology includes the study of conscious and unconscious phenomena, as well as ***mask*** and thought."" options = [""feelings""] answer = ""feeling"" ranked_options = fb.rank(sentence, options, True) assert ranked_options[0] == answer, ""It should find and rank related options"" ", https://github.com/QsonLabs/qlmetrics,3a4cc70bb68ef9b476aaee562b82a4d54acfd826,tests/test_bench.py::TestBenchmarkMemory::test_bench_as_decorator_trackmem_does_print_timing_info_to_stdout_in_non_default,tests/test_bench.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import pytest import sys import time import re import uuid # Python 2.x 3.x compatibility from six.moves import xrange from qlmetrics import bench @bench.timethis_returnstats def foo(x): time.sleep(.001) return x @bench.timethis() def foo2(x): time.sleep(.001) return x @bench.timethis(verbose=False) def foo3(x): time.sleep(.001) return x class TestBenchmarking(object): def test_bench_as_decorator_returnstats_returns_a_tuple_on_single_assignment(self): _ = foo(x=10) assert type(_) is tuple def test_bench_as_decorator_timethis_returnstatus_prints_timing_info_to_stdout(self, capsys): """""" capsys variable is a pytest function to capture system error and output """""" _, __ = foo(x=10) captured_stdouterr = capsys.readouterr() timefoo_stdout_regex = re.compile(r""@timecall: foo took (\d+\.\d+) seconds"") assert timefoo_stdout_regex.match(captured_stdouterr.out) is not None def test_bench_as_decorator_timethis_returnstats_can_return_the_metrics_as_dict(self): _, timing = foo(x=10) # this will throw warning in pylint but not an issue assert type(timing) is dict @pytest.mark.parametrize(""prop,prop_type"", [ (""duration"", float), (""func_name"", str), ], scope=""class"") def test_bench_as_decorator_timethis_returnstats_dict_has_properties(self, prop, prop_type): _, timing = foo(x=10) # this will throw warning in pylint for each paramaterized test assert type(timing[prop]) is prop_type def test_bench_as_decorator_timethis_does_not_return_a_tuple_on_single_assignment(self): _ = foo2(x=10) assert type(_) is not tuple def test_bench_as_decorator_timethis_prints_timing_info_to_stdout(self, capsys): """""" capsys variable is a pytest function to capture system error and output """""" _ = foo2(x=10) captured_stdouterr = capsys.readouterr() timefoo_stdout_regex = re.compile(r""@timecall: foo2 took (\d+\.\d+) seconds"") assert timefoo_stdout_regex.match(captured_stdouterr.out) is not None def test_bench_as_decorator_timethis_does_not_print_timing_info_to_stdout_when_verbose_false(self, capsys): """""" capsys variable is a pytest function to capture system error and output """""" _ = foo3(x=10) captured_stdouterr = capsys.readouterr() timefoo_stdout_regex = re.compile(r""@timecall: foo3 took (\d+\.\d+) seconds"") assert timefoo_stdout_regex.match(captured_stdouterr.out) is None def test_bench_as_decorator_timethis_does_print_timing_info_to_stdout_when_verbose_true(self, capsys): """""" capsys variable is a pytest function to capture system error and output """""" @bench.timethis(verbose=True) def foo4(x): time.sleep(.001) return x _ = foo4(x=10) captured_stdouterr = capsys.readouterr() timefoo_stdout_regex = re.compile(r""@timecall: foo4 took (\d+\.\d+) seconds"") assert timefoo_stdout_regex.match(captured_stdouterr.out) is not None def test_bench_as_decorator_timethis_does_print_timing_info_to_stdout_with_lambda(self, capsys): """""" capsys variable is a pytest function to capture system error and output """""" @bench.timethis(fmt_func=lambda f, d: ""Took {duration} seconds"".format(**{""duration"": d}), verbose=True) def foo4(x): time.sleep(.001) return x _ = foo4(x=10) captured_stdouterr = capsys.readouterr() timefoo_stdout_regex = re.compile(r""Took (\d+\.\d+) seconds"") assert timefoo_stdout_regex.match(captured_stdouterr.out) is not None def test_bench_as_decorator_timethis_does_print_timing_info_to_stdout_with_def_func(self, capsys): """""" capsys variable is a pytest function to capture system error and output """""" def format_timing(func_name, duration): return ""Function %s took %s"" % (func_name, duration) @bench.timethis(fmt_func=format_timing, verbose=True) def foo4(x): time.sleep(.001) return x _ = foo4(x=10) captured_stdouterr = capsys.readouterr() timefoo_stdout_regex = re.compile(r""Function foo4 took (\d+\.\d+)"") assert timefoo_stdout_regex.match(captured_stdouterr.out) is not None class TestBenchmarkMemory(object): def test_bench_as_decorator_trackmem_does_print_timing_info_to_stdout_in_non_default(self, capsys): """""" capsys variable is a pytest function to capture system error and output """""" @bench.trackmem(verbose=True) def foo_mem(numrows): rows = [] for _ in xrange(numrows): rows.append({'a': str(uuid.uuid4()), 'b': str(uuid.uuid4()), 'c': str(uuid.uuid4())}) return rows _ = foo_mem(10**3) captured_stdouterr = capsys.readouterr() trackmemfoo_stdout_regex = re.compile(r""@trackmem: foo_mem start: (\d+\.\d+) end: (\d+\.\d+) used: (\d+\.\d+) (B|KB|MB|GB)"") assert trackmemfoo_stdout_regex.match(captured_stdouterr.out) is not None def test_bench_as_decorator_trackmem_does_print_timing_info_to_stdout_when_passed_func(self, capsys): """""" capsys variable is a pytest function to capture system error and output """""" def fmt_mem(func_name, start_mem, end_mem, usage, unit): return ""track mem {} {}, start: {}, end: {}, usage: {}"".format(unit, func_name, start_mem, end_mem, usage) @bench.trackmem(fmt_func=fmt_mem, verbose=True) def foo_mem(numrows): rows = [] for _ in xrange(numrows): rows.append({'a': str(uuid.uuid4()), 'b': str(uuid.uuid4()), 'c': str(uuid.uuid4())}) return rows _ = foo_mem(10**3) captured_stdouterr = capsys.readouterr() trackmemfoo_stdout_regex = re.compile(r""track mem MB foo_mem, start: (\d+\.\d+), end: (\d+\.\d+), usage: (\d+\.\d+)"") assert trackmemfoo_stdout_regex.match(captured_stdouterr.out) is not None @pytest.mark.parametrize(""size_value"", [ 0, 0.1 ]) def test_size_raises_type_error_when_passed_non_iterable_value(size_value): with pytest.raises(TypeError): _ = bench.size(size_value, ""GB"") @pytest.mark.parametrize(""size_value1"", [ ""1"", # single string is iterable, so will try and be converted and raise typeerror [""2048"", ""4096""], [2048, ""4096""], ]) def test_size_raises_type_error_when_passed_interable_nonconverteable_objects(size_value1): with pytest.raises(TypeError): _ = bench.size(size_value1, ""GB"") def test_size_raises_value_error_when_not_provided_valid_unit(): with pytest.raises(ValueError): _ = bench.size([2048], ""k"") @pytest.mark.parametrize(""values,unit,converted"", [ ([2048, 4096], ""B"", [2048, 4096]), ([2048, 4096], ""b"", [2048, 4096]), ([2048, 4096], ""Kb"", [2, 4]), # test case on unit mixed ([2048, 4096], ""KB"", [2, 4]), # test case on unit caps ([2048, 4096], ""kb"", [2, 4]), # test case on unit lower ([1048576], ""MB"", [1]), ([-10485760], ""mb"", [-10]), # test negative returns negative ([524288], ""Mb"", [0.5]), # test fractional ([107374182400], ""GB"", [100]), # test large (for memory) ]) def test_conversions_are_accurate(values, unit, converted): cv = bench.size(values, unit) assert cv == converted ", https://github.com/quantopian/qgrid,877b420d3bd83297bbcc97202b914001a85afff2,qgrid/tests/test_grid.py::test_edit_multi_index_df,qgrid/tests/test_grid.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from qgrid import QgridWidget, set_defaults, show_grid, on as qgrid_on from traitlets import All import numpy as np import pandas as pd import json def create_df(): return pd.DataFrame( { ""A"": 1.0, ""Date"": pd.Timestamp(""20130102""), ""C"": pd.Series(1, index=list(range(4)), dtype=""float32""), ""D"": np.array([3] * 4, dtype=""int32""), ""E"": pd.Categorical([""test"", ""train"", ""foo"", ""bar""]), ""F"": [""foo"", ""bar"", ""buzz"", ""fox""], } ) def create_large_df(size=10000): large_df = pd.DataFrame(np.random.randn(size, 4), columns=list(""ABCD"")) large_df[""B (as str)""] = large_df[""B""].map(lambda x: str(x)) return large_df def create_multi_index_df(): arrays = [ [""bar"", ""bar"", ""baz"", ""baz"", ""foo"", ""foo"", ""qux"", ""qux""], [""one"", ""two"", ""one"", ""two"", ""one"", ""two"", ""one"", ""two""], ] return pd.DataFrame(np.random.randn(8, 4), index=arrays) def create_interval_index_df(): td = np.cumsum(np.random.randint(1, 15 * 60, 1000)) start = pd.Timestamp(""2017-04-17"") df = pd.DataFrame( [(start + pd.Timedelta(seconds=d)) for d in td], columns=[""time""], dtype=""M8[ns]"", ) df[""time_bin""] = np.cumsum(np.random.randint(1, 15 * 60, 1000)) return df def init_event_history(event_names, widget=None): event_history = [] def on_change(event, qgrid_widget): event_history.append(event) if widget is not None: widget.on(event_names, on_change) else: qgrid_on(event_names, on_change) return event_history def test_edit_date(): view = QgridWidget(df=create_df()) check_edit_success( view, ""Date"", 3, pd.Timestamp(""2013-01-02 00:00:00""), ""2013-01-02T00:00:00.000Z"", pd.Timestamp(""2013-01-16 00:00:00""), ""2013-01-16T00:00:00.000Z"", ) def test_edit_multi_index_df(): df_multi = create_multi_index_df() df_multi.index.set_names(""first"", level=0, inplace=True) view = QgridWidget(df=df_multi) old_val = df_multi.loc[(""bar"", ""two""), 1] check_edit_success( view, 1, 1, old_val, round(old_val, pd.get_option(""display.precision"") - 1), 3.45678, 3.45678, ) def check_edit_success( widget, col_name, row_index, old_val_obj, old_val_json, new_val_obj, new_val_json, ): event_history = init_event_history(""cell_edited"", widget) grid_data = json.loads(widget._df_json)[""data""] assert grid_data[row_index][str(col_name)] == old_val_json widget._handle_qgrid_msg_helper( { ""column"": col_name, ""row_index"": row_index, ""type"": ""edit_cell"", ""unfiltered_index"": row_index, ""value"": new_val_json, } ) expected_index_val = widget._df.index[row_index] assert event_history == [ { ""name"": ""cell_edited"", ""index"": expected_index_val, ""column"": col_name, ""old"": old_val_obj, ""new"": new_val_obj, ""source"": ""gui"", } ] assert widget._df[col_name][row_index] == new_val_obj # call _update_table so the widget updates _df_json widget._update_table(fire_data_change_event=False) grid_data = json.loads(widget._df_json)[""data""] assert grid_data[row_index][str(col_name)] == new_val_json def test_edit_number(): old_val = 3 view = QgridWidget(df=create_df()) for idx in range(-10, 10, 1): check_edit_success(view, ""D"", 2, old_val, old_val, idx, idx) old_val = idx def test_add_row_button(): widget = QgridWidget(df=create_df()) event_history = init_event_history(""row_added"", widget=widget) widget._handle_qgrid_msg_helper({""type"": ""add_row""}) assert event_history == [ {""name"": ""row_added"", ""index"": 4, ""source"": ""gui""} ] # make sure the added row in the internal dataframe contains the # expected values added_index = event_history[0][""index""] expected_values = pd.Series({ ""qgrid_unfiltered_index"": 4, ""A"": 1, ""C"": 1, ""D"": 3, ""Date"": pd.Timestamp(""2013-01-02 00:00:00""), ""E"": ""bar"", ""F"": ""fox"" }) sort_idx = widget._df.loc[added_index].index assert (widget._df.loc[added_index] == expected_values[sort_idx]).all() def test_remove_row_button(): widget = QgridWidget(df=create_df()) event_history = init_event_history( [""row_removed"", ""selection_changed""], widget=widget ) selected_rows = [1, 2] widget._handle_qgrid_msg_helper( {""rows"": selected_rows, ""type"": ""change_selection""} ) widget._handle_qgrid_msg_helper({""type"": ""remove_row""}) assert event_history == [ { ""name"": ""selection_changed"", ""old"": [], ""new"": selected_rows, ""source"": ""gui"", }, {""name"": ""row_removed"", ""indices"": selected_rows, ""source"": ""gui""}, ] def test_mixed_type_column(): df = pd.DataFrame({""A"": [1.2, ""xy"", 4], ""B"": [3, 4, 5]}) df = df.set_index(pd.Index([""yz"", 7, 3.2])) view = QgridWidget(df=df) view._handle_qgrid_msg_helper( {""type"": ""change_sort"", ""sort_field"": ""A"", ""sort_ascending"": True} ) view._handle_qgrid_msg_helper( {""type"": ""show_filter_dropdown"", ""field"": ""A"", ""search_val"": None} ) def test_nans(): df = pd.DataFrame( [(pd.Timestamp(""2017-02-02""), np.nan), (4, 2), (""foo"", ""bar"")] ) view = QgridWidget(df=df) view._handle_qgrid_msg_helper( {""type"": ""change_sort"", ""sort_field"": 1, ""sort_ascending"": True} ) view._handle_qgrid_msg_helper( {""type"": ""show_filter_dropdown"", ""field"": 1, ""search_val"": None} ) def test_row_edit_callback(): sample_df = create_df() def can_edit_row(row): return row[""E""] == ""train"" and row[""F""] == ""bar"" view = QgridWidget(df=sample_df, row_edit_callback=can_edit_row) view._handle_qgrid_msg_helper( {""type"": ""change_sort"", ""sort_field"": ""index"", ""sort_ascending"": True} ) expected_dict = {0: False, 1: True, 2: False, 3: False} assert expected_dict == view._editable_rows def test_period_object_column(): range_index = pd.period_range(start=""2000"", periods=10, freq=""B"") df = pd.DataFrame({""a"": 5, ""b"": range_index}, index=range_index) view = QgridWidget(df=df) view._handle_qgrid_msg_helper( {""type"": ""change_sort"", ""sort_field"": ""index"", ""sort_ascending"": True} ) view._handle_qgrid_msg_helper( {""type"": ""show_filter_dropdown"", ""field"": ""index"", ""search_val"": None} ) view._handle_qgrid_msg_helper( {""type"": ""change_sort"", ""sort_field"": ""b"", ""sort_ascending"": True} ) view._handle_qgrid_msg_helper( {""type"": ""show_filter_dropdown"", ""field"": ""b"", ""search_val"": None} ) def test_get_selected_df(): sample_df = create_df() selected_rows = [1, 3] view = QgridWidget(df=sample_df) view._handle_qgrid_msg_helper( {""rows"": selected_rows, ""type"": ""change_selection""} ) selected_df = view.get_selected_df() assert len(selected_df) == 2 assert sample_df.iloc[selected_rows].equals(selected_df) def test_integer_index_filter(): view = QgridWidget(df=create_df()) view._handle_qgrid_msg_helper( { ""field"": ""index"", ""filter_info"": { ""field"": ""index"", ""max"": None, ""min"": 2, ""type"": ""slider"", }, ""type"": ""change_filter"", } ) filtered_df = view.get_changed_df() assert len(filtered_df) == 2 def test_series_of_text_filters(): view = QgridWidget(df=create_df()) view._handle_qgrid_msg_helper( {""type"": ""show_filter_dropdown"", ""field"": ""E"", ""search_val"": None} ) view._handle_qgrid_msg_helper( { ""field"": ""E"", ""filter_info"": { ""field"": ""E"", ""selected"": [0, 1], ""type"": ""text"", ""excluded"": [], }, ""type"": ""change_filter"", } ) filtered_df = view.get_changed_df() assert len(filtered_df) == 2 # reset the filter... view._handle_qgrid_msg_helper( { ""field"": ""E"", ""filter_info"": { ""field"": ""E"", ""selected"": None, ""type"": ""text"", ""excluded"": [], }, ""type"": ""change_filter"", } ) # ...and apply a text filter on a different column view._handle_qgrid_msg_helper( {""type"": ""show_filter_dropdown"", ""field"": ""F"", ""search_val"": None} ) view._handle_qgrid_msg_helper( { ""field"": ""F"", ""filter_info"": { ""field"": ""F"", ""selected"": [0, 1], ""type"": ""text"", ""excluded"": [], }, ""type"": ""change_filter"", } ) filtered_df = view.get_changed_df() assert len(filtered_df) == 2 def test_date_index(): df = create_df() df.set_index(""Date"", inplace=True) view = QgridWidget(df=df) view._handle_qgrid_msg_helper( { ""type"": ""change_filter"", ""field"": ""A"", ""filter_info"": { ""field"": ""A"", ""type"": ""slider"", ""min"": 2, ""max"": 3, }, } ) def test_multi_index(): widget = QgridWidget(df=create_multi_index_df()) event_history = init_event_history( [""filter_dropdown_shown"", ""filter_changed"", ""sort_changed""], widget=widget, ) widget._handle_qgrid_msg_helper( { ""type"": ""show_filter_dropdown"", ""field"": ""level_0"", ""search_val"": None, } ) widget", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_BaseDataset_common_behaviours.py::test_sanity_checks,pyradigm/tests/test_BaseDataset_common_behaviours.py,NIO,flaky,Accepted,https://github.com/raamana/pyradigm/pull/48,classify;root_cause;fix_proposal,""""""" Tests to ensure certain behaviours among all child classes of BaseDataset For example, dataset_path= arg must be accepted during init copy construction must be supported: Dataset(dataset_instance) returns a copy """""" from inspect import signature from collections.abc import Iterable from pyradigm import (ClassificationDataset as ClfDataset, RegressionDataset as RegrDataset) from pyradigm.utils import make_random_ClfDataset from pyradigm.base import is_iterable_but_not_str, PyradigmException, \ ConstantValuesException, InfiniteOrNaNValuesException, EmptyFeatureSetException from pytest import raises, warns import numpy as np import random import os from os.path import join as pjoin, realpath, dirname from warnings import warn class_list = (ClfDataset, RegrDataset) constructor_must_offer_param_list = ('dataset_path', 'data', 'targets', 'dtype', 'description', 'feature_names') cur_dir = dirname(realpath(__file__)) out_dir = pjoin(cur_dir, 'tmp') os.makedirs(out_dir, exist_ok=True) out_file = pjoin(out_dir, 'random_example_dataset.pkl') ds = make_random_ClfDataset() def _not_equal(array_one, array_two): if isinstance(array_one, dict): not_equal = False for key, val in array_one.items(): if _not_equal(val, array_two[key]): not_equal = True break elif is_iterable_but_not_str(array_one): not_equal = False try: for v1, v2 in zip(array_one, array_two): if v1 != v2: not_equal = True break except: print('not_equal type of array_one {},' 'array_two {}'.format(type(array_one), type(array_two))) raise else: not_equal = array_one != array_two if not isinstance(not_equal, bool): not_equal = any(not_equal) return not_equal def test_constructor_must_offer_params(): for cls in class_list: cls_sign = signature(cls) for param in constructor_must_offer_param_list: if not param in cls_sign.parameters: raise SyntaxError('Class {} does not offer {} as an argument ' 'during init!'.format(cls, param)) def test_attributes(): """"""Creation, access and properties"""""" id_list = np.array(ds.samplet_ids) # ensuring strings can't be added to float attributes ds.add_attr('age', id_list[0], 43) for mismatched_type in ['43', 2 + 3j]: with raises(TypeError): ds.add_attr('age', id_list[2], mismatched_type) # ensuring floats can't be added to string attributes ds.add_attr('gender', id_list[3], 'female') for mismatched_type in [43, 2 + 3j]: with raises(TypeError): ds.add_attr('gender', id_list[4], mismatched_type) # adding to multiple samplets at a time # this should work ds.add_attr('gender', id_list[:3], ('female', 'male', 'male')) # but not this: with raises(ValueError): ds.add_attr('gender', id_list[:3], ('female', 'male',)) # dataset attributes try: ds.add_dataset_attr('version', 2.0) # ds.add_dataset_attr('params', ['foo', 'bar', 20, 12, '/work/path']) # arbitrary values are causing problems with np.not_equal checks # using simple values for now ds.add_dataset_attr('params', ['foo', 'bar', '/work/path']) except: raise AttributeError('Unable to add dataset attributes') # retrieval random_ids = id_list[random.sample(range(50), 5)] values_set = np.random.rand(5) ds.add_attr('random_float', random_ids, values_set) retrieved = ds.get_attr('random_float', random_ids) if not all(np.isclose(retrieved, values_set)): raise ValueError('Retrieved attribute values do not match the originals!') # ensuring ds.get_subset() returns subset for each attribute for index, id_ in enumerate(id_list): ds.add_attr('sid', id_, id_) ds.add_attr('index', id_, index) id_subset = id_list[:3] sub = ds.get_subset(id_subset) if set(sub.attr['sid']) != set(id_subset) or \ set(sub.attr['index']) != set(id_subset): raise ValueError('attrs are not being propagated during .get_subset()') with raises(KeyError): ds.get_attr('non_existing_attr') with raises(KeyError): # existing but not all of them are set ds.get_attr('random_float', ds.samplet_ids) with warns(UserWarning): ds.del_attr('non_existing_attr') try: ds.del_attr('random_float') except: raise AttributeError('Attr deletion failed!') def test_save_load(): ds.save(file_path=out_file) reloaded_ds = ds.__class__(dataset_path=out_file) if ds != reloaded_ds: raise IOError('Error in save/load implementation!') must_have_attr = ('_data', '_targets', '_dtype', '_target_type', '_description', '_num_features', '_feature_names', '_attr', '_attr_dtype', '_dataset_attr') for attr in must_have_attr: if not hasattr(reloaded_ds, attr): raise AttributeError('Attribute {} missing after reload from disk' ''.format(attr)) orig_val = getattr(ds, attr) reloaded = getattr(reloaded_ds, attr) not_equal = False try: if isinstance(orig_val, dict): for key, val in orig_val.items(): if _not_equal(val, reloaded[key]): warn('Values differ for attr {} in samplet {}' ' when reloaded from disk'.format(attr, key)) not_equal = True break elif is_iterable_but_not_str(orig_val): for aa, bb in zip(orig_val, reloaded): if aa != bb: not_equal= True break # not_equal = any(np.not_equal(orig_val, reloaded)) elif np.issubdtype(type(orig_val), np.generic): not_equal = reloaded != orig_val else: raise TypeError('Unrecognized type {} for attr {}' ''.format(type(orig_val), attr)) except: raise if not isinstance(not_equal, bool): not_equal = any(not_equal) if not_equal: raise AttributeError('Attribute {} differs between the reloaded' ' and the original datasets'.format(attr)) def test_nan_inf_values(): target_val = 3 for cls_type in (RegrDataset, ClfDataset): cds_clean = cls_type(allow_nan_inf=False) for invalid_value in [np.NaN, np.Inf]: with raises(InfiniteOrNaNValuesException): cds_clean.add_samplet('a', [1, invalid_value, 3], target_val) cds_dirty = cls_type(allow_nan_inf=True) for sid, valid_value in zip(('a', 'b'), [np.NaN, np.Inf]): try: cds_dirty.add_samplet(sid, [1, valid_value, 3], target_val) except: raise def test_sanity_checks(): """"""Ensure that sanity checks are performed, and as expected."""""" ### -------------- as you add them to dataset -------------- with raises(EmptyFeatureSetException): ds.add_samplet('empty_features', [], 'target') ### -------------- as you save them to disk -------------- ds.add_samplet('all_zeros', np.zeros((ds.num_features,1)), 'target') with raises(ConstantValuesException): ds.save(out_file) ds.del_samplet('all_zeros') # checking for random constant value! const_value = np.random.randint(10, 100) const_feat_set = np.full((ds.num_features, 1), const_value) ds.add_samplet('all_constant', const_feat_set, 'target') with raises(ConstantValuesException): ds.save(out_file) # now checking for constants across samplets # this is easily achieved by adding different samplets with same features # such a bug is possible, when user made a mistake querying # the right files for the right samplet ID const_ds = ClfDataset() rand_feat_same_across_samplets = np.random.randn(10) for index in range(np.random.randint(10, 100)): const_ds.add_samplet(str(index), rand_feat_same_across_samplets, index) with raises(ConstantValuesException): const_ds.save(out_file) test_attributes() # test_save_load() # test_sanity_checks() test_nan_inf_values()", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_BaseDataset_common_behaviours.py::test_save_load,pyradigm/tests/test_BaseDataset_common_behaviours.py,OD-Vic,flaky,nan,nan,classify;root_cause,""""""" Tests to ensure certain behaviours among all child classes of BaseDataset For example, dataset_path= arg must be accepted during init copy construction must be supported: Dataset(dataset_instance) returns a copy """""" from inspect import signature from collections.abc import Iterable from pyradigm import (ClassificationDataset as ClfDataset, RegressionDataset as RegrDataset) from pyradigm.utils import make_random_ClfDataset from pyradigm.base import is_iterable_but_not_str, PyradigmException, \ ConstantValuesException, InfiniteOrNaNValuesException, EmptyFeatureSetException from pytest import raises, warns import numpy as np import random import os from os.path import join as pjoin, realpath, dirname from warnings import warn class_list = (ClfDataset, RegrDataset) constructor_must_offer_param_list = ('dataset_path', 'data', 'targets', 'dtype', 'description', 'feature_names') cur_dir = dirname(realpath(__file__)) out_dir = pjoin(cur_dir, 'tmp') os.makedirs(out_dir, exist_ok=True) out_file = pjoin(out_dir, 'random_example_dataset.pkl') ds = make_random_ClfDataset() def _not_equal(array_one, array_two): if isinstance(array_one, dict): not_equal = False for key, val in array_one.items(): if _not_equal(val, array_two[key]): not_equal = True break elif is_iterable_but_not_str(array_one): not_equal = False try: for v1, v2 in zip(array_one, array_two): if v1 != v2: not_equal = True break except: print('not_equal type of array_one {},' 'array_two {}'.format(type(array_one), type(array_two))) raise else: not_equal = array_one != array_two if not isinstance(not_equal, bool): not_equal = any(not_equal) return not_equal def test_constructor_must_offer_params(): for cls in class_list: cls_sign = signature(cls) for param in constructor_must_offer_param_list: if not param in cls_sign.parameters: raise SyntaxError('Class {} does not offer {} as an argument ' 'during init!'.format(cls, param)) def test_attributes(): """"""Creation, access and properties"""""" id_list = np.array(ds.samplet_ids) # ensuring strings can't be added to float attributes ds.add_attr('age', id_list[0], 43) for mismatched_type in ['43', 2 + 3j]: with raises(TypeError): ds.add_attr('age', id_list[2], mismatched_type) # ensuring floats can't be added to string attributes ds.add_attr('gender', id_list[3], 'female') for mismatched_type in [43, 2 + 3j]: with raises(TypeError): ds.add_attr('gender', id_list[4], mismatched_type) # adding to multiple samplets at a time # this should work ds.add_attr('gender', id_list[:3], ('female', 'male', 'male')) # but not this: with raises(ValueError): ds.add_attr('gender', id_list[:3], ('female', 'male',)) # dataset attributes try: ds.add_dataset_attr('version', 2.0) # ds.add_dataset_attr('params', ['foo', 'bar', 20, 12, '/work/path']) # arbitrary values are causing problems with np.not_equal checks # using simple values for now ds.add_dataset_attr('params', ['foo', 'bar', '/work/path']) except: raise AttributeError('Unable to add dataset attributes') # retrieval random_ids = id_list[random.sample(range(50), 5)] values_set = np.random.rand(5) ds.add_attr('random_float', random_ids, values_set) retrieved = ds.get_attr('random_float', random_ids) if not all(np.isclose(retrieved, values_set)): raise ValueError('Retrieved attribute values do not match the originals!') # ensuring ds.get_subset() returns subset for each attribute for index, id_ in enumerate(id_list): ds.add_attr('sid', id_, id_) ds.add_attr('index', id_, index) id_subset = id_list[:3] sub = ds.get_subset(id_subset) if set(sub.attr['sid']) != set(id_subset) or \ set(sub.attr['index']) != set(id_subset): raise ValueError('attrs are not being propagated during .get_subset()') with raises(KeyError): ds.get_attr('non_existing_attr') with raises(KeyError): # existing but not all of them are set ds.get_attr('random_float', ds.samplet_ids) with warns(UserWarning): ds.del_attr('non_existing_attr') try: ds.del_attr('random_float') except: raise AttributeError('Attr deletion failed!') def test_save_load(): ds.save(file_path=out_file) reloaded_ds = ds.__class__(dataset_path=out_file) if ds != reloaded_ds: raise IOError('Error in save/load implementation!') must_have_attr = ('_data', '_targets', '_dtype', '_target_type', '_description', '_num_features', '_feature_names', '_attr', '_attr_dtype', '_dataset_attr') for attr in must_have_attr: if not hasattr(reloaded_ds, attr): raise AttributeError('Attribute {} missing after reload from disk' ''.format(attr)) orig_val = getattr(ds, attr) reloaded = getattr(reloaded_ds, attr) not_equal = False try: if isinstance(orig_val, dict): for key, val in orig_val.items(): if _not_equal(val, reloaded[key]): warn('Values differ for attr {} in samplet {}' ' when reloaded from disk'.format(attr, key)) not_equal = True break elif is_iterable_but_not_str(orig_val): for aa, bb in zip(orig_val, reloaded): if aa != bb: not_equal= True break # not_equal = any(np.not_equal(orig_val, reloaded)) elif np.issubdtype(type(orig_val), np.generic): not_equal = reloaded != orig_val else: raise TypeError('Unrecognized type {} for attr {}' ''.format(type(orig_val), attr)) except: raise if not isinstance(not_equal, bool): not_equal = any(not_equal) if not_equal: raise AttributeError('Attribute {} differs between the reloaded' ' and the original datasets'.format(attr)) def test_nan_inf_values(): target_val = 3 for cls_type in (RegrDataset, ClfDataset): cds_clean = cls_type(allow_nan_inf=False) for invalid_value in [np.NaN, np.Inf]: with raises(InfiniteOrNaNValuesException): cds_clean.add_samplet('a', [1, invalid_value, 3], target_val) cds_dirty = cls_type(allow_nan_inf=True) for sid, valid_value in zip(('a', 'b'), [np.NaN, np.Inf]): try: cds_dirty.add_samplet(sid, [1, valid_value, 3], target_val) except: raise def test_sanity_checks(): """"""Ensure that sanity checks are performed, and as expected."""""" ### -------------- as you add them to dataset -------------- with raises(EmptyFeatureSetException): ds.add_samplet('empty_features', [], 'target') ### -------------- as you save them to disk -------------- ds.add_samplet('all_zeros', np.zeros((ds.num_features,1)), 'target') with raises(ConstantValuesException): ds.save(out_file) ds.del_samplet('all_zeros') # checking for random constant value! const_value = np.random.randint(10, 100) const_feat_set = np.full((ds.num_features, 1), const_value) ds.add_samplet('all_constant', const_feat_set, 'target') with raises(ConstantValuesException): ds.save(out_file) # now checking for constants across samplets # this is easily achieved by adding different samplets with same features # such a bug is possible, when user made a mistake querying # the right files for the right samplet ID const_ds = ClfDataset() rand_feat_same_across_samplets = np.random.randn(10) for index in range(np.random.randint(10, 100)): const_ds.add_samplet(str(index), rand_feat_same_across_samplets, index) with raises(ConstantValuesException): const_ds.save(out_file) test_attributes() # test_save_load() # test_sanity_checks() test_nan_inf_values()", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_classify_dataset.py::test_add,pyradigm/tests/test_classify_dataset.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import sys from os.path import dirname, join as pjoin, realpath import numpy as np sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm.classify import ClassificationDataset as ClfDataset out_dir = '.' num_targets = np.random.randint(2, 50) target_sizes = np.random.randint(10, 100, num_targets) num_features = np.random.randint(10, 100) num_samples = sum(target_sizes) target_set = np.array(['C{:05d}'.format(x) for x in range(num_targets)]) feat_names = np.array([str(x) for x in range(num_features)]) test_dataset = ClfDataset() for target_index, target_id in enumerate(target_set): for sub_ix in range(target_sizes[target_index]): subj_id = '{}_S{:05d}'.format(target_set[target_index], sub_ix) feat = np.random.random(num_features) test_dataset.add_samplet(subj_id, feat, target_id, feature_names=feat_names) out_file = os.path.join(out_dir, 'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = ClfDataset() for sub_id in test_dataset.samplet_ids: feat = np.random.random(num_features) same_ids_new_feat.add_samplet(sub_id, feat, test_dataset.targets[sub_id]) same_ids_new_feat.feature_names = np.array(['new_f{}'.format(x) for x in range( num_features)]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) target_set, target_sizes = test_dataset.summarize() reloaded_dataset = ClfDataset(dataset_path=out_file, description='reloaded test_dataset') copy_dataset = ClfDataset(in_dataset=test_dataset) rand_index = np.random.randint(0, len(target_set), 1)[0] random_target_name = target_set[rand_index] random_target_ds = test_dataset.get_class(random_target_name) other_targets_ds = test_dataset - random_target_ds other_target_set = set(target_set) - set([random_target_name]) other_targets_get_with_list = test_dataset.get_class(other_target_set) recombined = other_targets_ds + random_target_ds empty_dataset = ClfDataset() test2 = ClfDataset() test3 = ClfDataset() def test_empty(): assert not empty_dataset def test_target_type(): rand_id = test_dataset.samplet_ids[np.random.randint(2, num_samples)] if not isinstance(test_dataset.targets[rand_id], test_dataset._target_type): raise TypeError('invalid target type for samplet id {}'.format(rand_id)) def test_num_targets(): assert test_dataset.num_targets == num_targets def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samples, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samplets == sum(target_sizes) def test_subtract(): assert other_targets_ds.num_samplets == sum(target_sizes) - target_sizes[rand_index] def test_get_target_list(): assert other_targets_ds == other_targets_get_with_list def test_add(): a = other_targets_ds + random_target_ds n = a.num_samplets n1 = other_targets_ds.num_samplets n2 = random_target_ds.num_samplets assert n1 + n2 == n assert set(a.samplet_ids) == set( other_targets_ds.samplet_ids + random_target_ds.samplet_ids) assert a.num_features == other_targets_ds.num_features == \ random_target_ds.num_features assert all(a.feature_names == other_targets_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.samplet_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid + 'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid] == new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_data_type(): for in_dtype in [np.float_, np.int, np.bool_]: cds = ClfDataset(dtype=in_dtype) cds.add_samplet('a', [1, 2.0, -434], 'class') if cds.dtype != in_dtype or cds['a'].dtype != in_dtype: raise TypeError('Dataset not maintaining the features in the requested' 'dtype {}. They are in {}'.format(in_dtype, cds.dtype)) def test_cant_read_nonexisting_file(): with raises(IOError): a = ClfDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(TypeError): a = ClfDataset( in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = ClfDataset(dataset_path=None, in_dataset=None, data=list()) with raises(ValueError): c = ClfDataset(dataset_path=None, in_dataset=None, data=None, targets='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_labels() assert len(vec_labels) == len(sub_ids) assert len(vec_labels) == matrix.shape[0] def test_init_with_dict(): new_ds = ClfDataset(data=test_dataset.data, targets=test_dataset.targets) assert new_ds == test_dataset # def test_labels_setter(): # fewer_labels = test_dataset.labels # label_keys = list(fewer_labels.samplet_ids()) # fewer_labels.pop(label_keys[0]) # # with raises(ValueError): # test_dataset.labels = fewer_labels # # same_len_diff_key = fewer_labels # same_len_diff_key[u'sldiursvdkvjs'] = 1 # with raises(ValueError): # test_dataset.labels = same_len_diff_key # # # must be dict # with raises(ValueError): # test_dataset.labels = None def test_targets_setter(): fewer_targets = test_dataset.targets targets_keys = list(fewer_targets.keys()) fewer_targets.pop(targets_keys[0]) with raises(ValueError): test_dataset.targets = fewer_targets same_len_diff_key = fewer_targets same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.targets = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features - 2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.samplet_ids[0] with raises(ValueError): test_dataset.add_samplet(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.samplet_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features + 1, 1) with raises(ValueError): test_dataset.add_samplet(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_samplet(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_class(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset = np.random.randint(1, nf, size=subset_len) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = ClfDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = ClfDataset(dataset_path=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_target_ds.num_samplets == target_sizes[rand_index] def test_get_subset(): assert random_target_ds == reloaded_dataset.get_class(random_target_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samplets) member = test_dataset.samplet_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.randint(1, n, size=min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samplets - 1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc_in_class=perc) # separating the calculation by class to mimic the implementation in the # class expected_size = s", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_classify_dataset.py::test_cant_write_to_nonexisting_dir,pyradigm/tests/test_classify_dataset.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import sys from os.path import dirname, join as pjoin, realpath import numpy as np sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm.classify import ClassificationDataset as ClfDataset out_dir = '.' num_targets = np.random.randint(2, 50) target_sizes = np.random.randint(10, 100, num_targets) num_features = np.random.randint(10, 100) num_samples = sum(target_sizes) target_set = np.array(['C{:05d}'.format(x) for x in range(num_targets)]) feat_names = np.array([str(x) for x in range(num_features)]) test_dataset = ClfDataset() for target_index, target_id in enumerate(target_set): for sub_ix in range(target_sizes[target_index]): subj_id = '{}_S{:05d}'.format(target_set[target_index], sub_ix) feat = np.random.random(num_features) test_dataset.add_samplet(subj_id, feat, target_id, feature_names=feat_names) out_file = os.path.join(out_dir, 'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = ClfDataset() for sub_id in test_dataset.samplet_ids: feat = np.random.random(num_features) same_ids_new_feat.add_samplet(sub_id, feat, test_dataset.targets[sub_id]) same_ids_new_feat.feature_names = np.array(['new_f{}'.format(x) for x in range( num_features)]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) target_set, target_sizes = test_dataset.summarize() reloaded_dataset = ClfDataset(dataset_path=out_file, description='reloaded test_dataset') copy_dataset = ClfDataset(in_dataset=test_dataset) rand_index = np.random.randint(0, len(target_set), 1)[0] random_target_name = target_set[rand_index] random_target_ds = test_dataset.get_class(random_target_name) other_targets_ds = test_dataset - random_target_ds other_target_set = set(target_set) - set([random_target_name]) other_targets_get_with_list = test_dataset.get_class(other_target_set) recombined = other_targets_ds + random_target_ds empty_dataset = ClfDataset() test2 = ClfDataset() test3 = ClfDataset() def test_empty(): assert not empty_dataset def test_target_type(): rand_id = test_dataset.samplet_ids[np.random.randint(2, num_samples)] if not isinstance(test_dataset.targets[rand_id], test_dataset._target_type): raise TypeError('invalid target type for samplet id {}'.format(rand_id)) def test_num_targets(): assert test_dataset.num_targets == num_targets def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samples, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samplets == sum(target_sizes) def test_subtract(): assert other_targets_ds.num_samplets == sum(target_sizes) - target_sizes[rand_index] def test_get_target_list(): assert other_targets_ds == other_targets_get_with_list def test_add(): a = other_targets_ds + random_target_ds n = a.num_samplets n1 = other_targets_ds.num_samplets n2 = random_target_ds.num_samplets assert n1 + n2 == n assert set(a.samplet_ids) == set( other_targets_ds.samplet_ids + random_target_ds.samplet_ids) assert a.num_features == other_targets_ds.num_features == \ random_target_ds.num_features assert all(a.feature_names == other_targets_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.samplet_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid + 'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid] == new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_data_type(): for in_dtype in [np.float_, np.int, np.bool_]: cds = ClfDataset(dtype=in_dtype) cds.add_samplet('a', [1, 2.0, -434], 'class') if cds.dtype != in_dtype or cds['a'].dtype != in_dtype: raise TypeError('Dataset not maintaining the features in the requested' 'dtype {}. They are in {}'.format(in_dtype, cds.dtype)) def test_cant_read_nonexisting_file(): with raises(IOError): a = ClfDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(TypeError): a = ClfDataset( in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = ClfDataset(dataset_path=None, in_dataset=None, data=list()) with raises(ValueError): c = ClfDataset(dataset_path=None, in_dataset=None, data=None, targets='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_labels() assert len(vec_labels) == len(sub_ids) assert len(vec_labels) == matrix.shape[0] def test_init_with_dict(): new_ds = ClfDataset(data=test_dataset.data, targets=test_dataset.targets) assert new_ds == test_dataset # def test_labels_setter(): # fewer_labels = test_dataset.labels # label_keys = list(fewer_labels.samplet_ids()) # fewer_labels.pop(label_keys[0]) # # with raises(ValueError): # test_dataset.labels = fewer_labels # # same_len_diff_key = fewer_labels # same_len_diff_key[u'sldiursvdkvjs'] = 1 # with raises(ValueError): # test_dataset.labels = same_len_diff_key # # # must be dict # with raises(ValueError): # test_dataset.labels = None def test_targets_setter(): fewer_targets = test_dataset.targets targets_keys = list(fewer_targets.keys()) fewer_targets.pop(targets_keys[0]) with raises(ValueError): test_dataset.targets = fewer_targets same_len_diff_key = fewer_targets same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.targets = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features - 2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.samplet_ids[0] with raises(ValueError): test_dataset.add_samplet(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.samplet_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features + 1, 1) with raises(ValueError): test_dataset.add_samplet(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_samplet(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_class(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset = np.random.randint(1, nf, size=subset_len) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = ClfDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = ClfDataset(dataset_path=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_target_ds.num_samplets == target_sizes[rand_index] def test_get_subset(): assert random_target_ds == reloaded_dataset.get_class(random_target_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samplets) member = test_dataset.samplet_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.randint(1, n, size=min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samplets - 1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc_in_class=perc) # separating the calculation by class to mimic the implementation in the # class expected_size = s", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_classify_dataset.py::test_init_with_dict,pyradigm/tests/test_classify_dataset.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import sys from os.path import dirname, join as pjoin, realpath import numpy as np sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm.classify import ClassificationDataset as ClfDataset out_dir = '.' num_targets = np.random.randint(2, 50) target_sizes = np.random.randint(10, 100, num_targets) num_features = np.random.randint(10, 100) num_samples = sum(target_sizes) target_set = np.array(['C{:05d}'.format(x) for x in range(num_targets)]) feat_names = np.array([str(x) for x in range(num_features)]) test_dataset = ClfDataset() for target_index, target_id in enumerate(target_set): for sub_ix in range(target_sizes[target_index]): subj_id = '{}_S{:05d}'.format(target_set[target_index], sub_ix) feat = np.random.random(num_features) test_dataset.add_samplet(subj_id, feat, target_id, feature_names=feat_names) out_file = os.path.join(out_dir, 'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = ClfDataset() for sub_id in test_dataset.samplet_ids: feat = np.random.random(num_features) same_ids_new_feat.add_samplet(sub_id, feat, test_dataset.targets[sub_id]) same_ids_new_feat.feature_names = np.array(['new_f{}'.format(x) for x in range( num_features)]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) target_set, target_sizes = test_dataset.summarize() reloaded_dataset = ClfDataset(dataset_path=out_file, description='reloaded test_dataset') copy_dataset = ClfDataset(in_dataset=test_dataset) rand_index = np.random.randint(0, len(target_set), 1)[0] random_target_name = target_set[rand_index] random_target_ds = test_dataset.get_class(random_target_name) other_targets_ds = test_dataset - random_target_ds other_target_set = set(target_set) - set([random_target_name]) other_targets_get_with_list = test_dataset.get_class(other_target_set) recombined = other_targets_ds + random_target_ds empty_dataset = ClfDataset() test2 = ClfDataset() test3 = ClfDataset() def test_empty(): assert not empty_dataset def test_target_type(): rand_id = test_dataset.samplet_ids[np.random.randint(2, num_samples)] if not isinstance(test_dataset.targets[rand_id], test_dataset._target_type): raise TypeError('invalid target type for samplet id {}'.format(rand_id)) def test_num_targets(): assert test_dataset.num_targets == num_targets def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samples, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samplets == sum(target_sizes) def test_subtract(): assert other_targets_ds.num_samplets == sum(target_sizes) - target_sizes[rand_index] def test_get_target_list(): assert other_targets_ds == other_targets_get_with_list def test_add(): a = other_targets_ds + random_target_ds n = a.num_samplets n1 = other_targets_ds.num_samplets n2 = random_target_ds.num_samplets assert n1 + n2 == n assert set(a.samplet_ids) == set( other_targets_ds.samplet_ids + random_target_ds.samplet_ids) assert a.num_features == other_targets_ds.num_features == \ random_target_ds.num_features assert all(a.feature_names == other_targets_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.samplet_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid + 'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid] == new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_data_type(): for in_dtype in [np.float_, np.int, np.bool_]: cds = ClfDataset(dtype=in_dtype) cds.add_samplet('a', [1, 2.0, -434], 'class') if cds.dtype != in_dtype or cds['a'].dtype != in_dtype: raise TypeError('Dataset not maintaining the features in the requested' 'dtype {}. They are in {}'.format(in_dtype, cds.dtype)) def test_cant_read_nonexisting_file(): with raises(IOError): a = ClfDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(TypeError): a = ClfDataset( in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = ClfDataset(dataset_path=None, in_dataset=None, data=list()) with raises(ValueError): c = ClfDataset(dataset_path=None, in_dataset=None, data=None, targets='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_labels() assert len(vec_labels) == len(sub_ids) assert len(vec_labels) == matrix.shape[0] def test_init_with_dict(): new_ds = ClfDataset(data=test_dataset.data, targets=test_dataset.targets) assert new_ds == test_dataset # def test_labels_setter(): # fewer_labels = test_dataset.labels # label_keys = list(fewer_labels.samplet_ids()) # fewer_labels.pop(label_keys[0]) # # with raises(ValueError): # test_dataset.labels = fewer_labels # # same_len_diff_key = fewer_labels # same_len_diff_key[u'sldiursvdkvjs'] = 1 # with raises(ValueError): # test_dataset.labels = same_len_diff_key # # # must be dict # with raises(ValueError): # test_dataset.labels = None def test_targets_setter(): fewer_targets = test_dataset.targets targets_keys = list(fewer_targets.keys()) fewer_targets.pop(targets_keys[0]) with raises(ValueError): test_dataset.targets = fewer_targets same_len_diff_key = fewer_targets same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.targets = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features - 2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.samplet_ids[0] with raises(ValueError): test_dataset.add_samplet(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.samplet_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features + 1, 1) with raises(ValueError): test_dataset.add_samplet(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_samplet(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_class(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset = np.random.randint(1, nf, size=subset_len) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = ClfDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = ClfDataset(dataset_path=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_target_ds.num_samplets == target_sizes[rand_index] def test_get_subset(): assert random_target_ds == reloaded_dataset.get_class(random_target_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samplets) member = test_dataset.samplet_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.randint(1, n, size=min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samplets - 1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc_in_class=perc) # separating the calculation by class to mimic the implementation in the # class expected_size = s", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_classify_dataset.py::test_num_targets,pyradigm/tests/test_classify_dataset.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import sys from os.path import dirname, join as pjoin, realpath import numpy as np sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm.classify import ClassificationDataset as ClfDataset out_dir = '.' num_targets = np.random.randint(2, 50) target_sizes = np.random.randint(10, 100, num_targets) num_features = np.random.randint(10, 100) num_samples = sum(target_sizes) target_set = np.array(['C{:05d}'.format(x) for x in range(num_targets)]) feat_names = np.array([str(x) for x in range(num_features)]) test_dataset = ClfDataset() for target_index, target_id in enumerate(target_set): for sub_ix in range(target_sizes[target_index]): subj_id = '{}_S{:05d}'.format(target_set[target_index], sub_ix) feat = np.random.random(num_features) test_dataset.add_samplet(subj_id, feat, target_id, feature_names=feat_names) out_file = os.path.join(out_dir, 'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = ClfDataset() for sub_id in test_dataset.samplet_ids: feat = np.random.random(num_features) same_ids_new_feat.add_samplet(sub_id, feat, test_dataset.targets[sub_id]) same_ids_new_feat.feature_names = np.array(['new_f{}'.format(x) for x in range( num_features)]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) target_set, target_sizes = test_dataset.summarize() reloaded_dataset = ClfDataset(dataset_path=out_file, description='reloaded test_dataset') copy_dataset = ClfDataset(in_dataset=test_dataset) rand_index = np.random.randint(0, len(target_set), 1)[0] random_target_name = target_set[rand_index] random_target_ds = test_dataset.get_class(random_target_name) other_targets_ds = test_dataset - random_target_ds other_target_set = set(target_set) - set([random_target_name]) other_targets_get_with_list = test_dataset.get_class(other_target_set) recombined = other_targets_ds + random_target_ds empty_dataset = ClfDataset() test2 = ClfDataset() test3 = ClfDataset() def test_empty(): assert not empty_dataset def test_target_type(): rand_id = test_dataset.samplet_ids[np.random.randint(2, num_samples)] if not isinstance(test_dataset.targets[rand_id], test_dataset._target_type): raise TypeError('invalid target type for samplet id {}'.format(rand_id)) def test_num_targets(): assert test_dataset.num_targets == num_targets def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samples, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samplets == sum(target_sizes) def test_subtract(): assert other_targets_ds.num_samplets == sum(target_sizes) - target_sizes[rand_index] def test_get_target_list(): assert other_targets_ds == other_targets_get_with_list def test_add(): a = other_targets_ds + random_target_ds n = a.num_samplets n1 = other_targets_ds.num_samplets n2 = random_target_ds.num_samplets assert n1 + n2 == n assert set(a.samplet_ids) == set( other_targets_ds.samplet_ids + random_target_ds.samplet_ids) assert a.num_features == other_targets_ds.num_features == \ random_target_ds.num_features assert all(a.feature_names == other_targets_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.samplet_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid + 'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid] == new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_data_type(): for in_dtype in [np.float_, np.int, np.bool_]: cds = ClfDataset(dtype=in_dtype) cds.add_samplet('a', [1, 2.0, -434], 'class') if cds.dtype != in_dtype or cds['a'].dtype != in_dtype: raise TypeError('Dataset not maintaining the features in the requested' 'dtype {}. They are in {}'.format(in_dtype, cds.dtype)) def test_cant_read_nonexisting_file(): with raises(IOError): a = ClfDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(TypeError): a = ClfDataset( in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = ClfDataset(dataset_path=None, in_dataset=None, data=list()) with raises(ValueError): c = ClfDataset(dataset_path=None, in_dataset=None, data=None, targets='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_labels() assert len(vec_labels) == len(sub_ids) assert len(vec_labels) == matrix.shape[0] def test_init_with_dict(): new_ds = ClfDataset(data=test_dataset.data, targets=test_dataset.targets) assert new_ds == test_dataset # def test_labels_setter(): # fewer_labels = test_dataset.labels # label_keys = list(fewer_labels.samplet_ids()) # fewer_labels.pop(label_keys[0]) # # with raises(ValueError): # test_dataset.labels = fewer_labels # # same_len_diff_key = fewer_labels # same_len_diff_key[u'sldiursvdkvjs'] = 1 # with raises(ValueError): # test_dataset.labels = same_len_diff_key # # # must be dict # with raises(ValueError): # test_dataset.labels = None def test_targets_setter(): fewer_targets = test_dataset.targets targets_keys = list(fewer_targets.keys()) fewer_targets.pop(targets_keys[0]) with raises(ValueError): test_dataset.targets = fewer_targets same_len_diff_key = fewer_targets same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.targets = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features - 2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.samplet_ids[0] with raises(ValueError): test_dataset.add_samplet(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.samplet_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features + 1, 1) with raises(ValueError): test_dataset.add_samplet(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_samplet(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_class(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset = np.random.randint(1, nf, size=subset_len) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = ClfDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = ClfDataset(dataset_path=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_target_ds.num_samplets == target_sizes[rand_index] def test_get_subset(): assert random_target_ds == reloaded_dataset.get_class(random_target_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samplets) member = test_dataset.samplet_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.randint(1, n, size=min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samplets - 1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc_in_class=perc) # separating the calculation by class to mimic the implementation in the # class expected_size = s", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_classify_dataset.py::test_return_data_labels,pyradigm/tests/test_classify_dataset.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import sys from os.path import dirname, join as pjoin, realpath import numpy as np sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm.classify import ClassificationDataset as ClfDataset out_dir = '.' num_targets = np.random.randint(2, 50) target_sizes = np.random.randint(10, 100, num_targets) num_features = np.random.randint(10, 100) num_samples = sum(target_sizes) target_set = np.array(['C{:05d}'.format(x) for x in range(num_targets)]) feat_names = np.array([str(x) for x in range(num_features)]) test_dataset = ClfDataset() for target_index, target_id in enumerate(target_set): for sub_ix in range(target_sizes[target_index]): subj_id = '{}_S{:05d}'.format(target_set[target_index], sub_ix) feat = np.random.random(num_features) test_dataset.add_samplet(subj_id, feat, target_id, feature_names=feat_names) out_file = os.path.join(out_dir, 'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = ClfDataset() for sub_id in test_dataset.samplet_ids: feat = np.random.random(num_features) same_ids_new_feat.add_samplet(sub_id, feat, test_dataset.targets[sub_id]) same_ids_new_feat.feature_names = np.array(['new_f{}'.format(x) for x in range( num_features)]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) target_set, target_sizes = test_dataset.summarize() reloaded_dataset = ClfDataset(dataset_path=out_file, description='reloaded test_dataset') copy_dataset = ClfDataset(in_dataset=test_dataset) rand_index = np.random.randint(0, len(target_set), 1)[0] random_target_name = target_set[rand_index] random_target_ds = test_dataset.get_class(random_target_name) other_targets_ds = test_dataset - random_target_ds other_target_set = set(target_set) - set([random_target_name]) other_targets_get_with_list = test_dataset.get_class(other_target_set) recombined = other_targets_ds + random_target_ds empty_dataset = ClfDataset() test2 = ClfDataset() test3 = ClfDataset() def test_empty(): assert not empty_dataset def test_target_type(): rand_id = test_dataset.samplet_ids[np.random.randint(2, num_samples)] if not isinstance(test_dataset.targets[rand_id], test_dataset._target_type): raise TypeError('invalid target type for samplet id {}'.format(rand_id)) def test_num_targets(): assert test_dataset.num_targets == num_targets def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samples, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samplets == sum(target_sizes) def test_subtract(): assert other_targets_ds.num_samplets == sum(target_sizes) - target_sizes[rand_index] def test_get_target_list(): assert other_targets_ds == other_targets_get_with_list def test_add(): a = other_targets_ds + random_target_ds n = a.num_samplets n1 = other_targets_ds.num_samplets n2 = random_target_ds.num_samplets assert n1 + n2 == n assert set(a.samplet_ids) == set( other_targets_ds.samplet_ids + random_target_ds.samplet_ids) assert a.num_features == other_targets_ds.num_features == \ random_target_ds.num_features assert all(a.feature_names == other_targets_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.samplet_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid + 'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid] == new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_data_type(): for in_dtype in [np.float_, np.int, np.bool_]: cds = ClfDataset(dtype=in_dtype) cds.add_samplet('a', [1, 2.0, -434], 'class') if cds.dtype != in_dtype or cds['a'].dtype != in_dtype: raise TypeError('Dataset not maintaining the features in the requested' 'dtype {}. They are in {}'.format(in_dtype, cds.dtype)) def test_cant_read_nonexisting_file(): with raises(IOError): a = ClfDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(TypeError): a = ClfDataset( in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = ClfDataset(dataset_path=None, in_dataset=None, data=list()) with raises(ValueError): c = ClfDataset(dataset_path=None, in_dataset=None, data=None, targets='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_labels() assert len(vec_labels) == len(sub_ids) assert len(vec_labels) == matrix.shape[0] def test_init_with_dict(): new_ds = ClfDataset(data=test_dataset.data, targets=test_dataset.targets) assert new_ds == test_dataset # def test_labels_setter(): # fewer_labels = test_dataset.labels # label_keys = list(fewer_labels.samplet_ids()) # fewer_labels.pop(label_keys[0]) # # with raises(ValueError): # test_dataset.labels = fewer_labels # # same_len_diff_key = fewer_labels # same_len_diff_key[u'sldiursvdkvjs'] = 1 # with raises(ValueError): # test_dataset.labels = same_len_diff_key # # # must be dict # with raises(ValueError): # test_dataset.labels = None def test_targets_setter(): fewer_targets = test_dataset.targets targets_keys = list(fewer_targets.keys()) fewer_targets.pop(targets_keys[0]) with raises(ValueError): test_dataset.targets = fewer_targets same_len_diff_key = fewer_targets same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.targets = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features - 2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.samplet_ids[0] with raises(ValueError): test_dataset.add_samplet(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.samplet_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features + 1, 1) with raises(ValueError): test_dataset.add_samplet(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_samplet(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_class(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset = np.random.randint(1, nf, size=subset_len) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = ClfDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = ClfDataset(dataset_path=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_target_ds.num_samplets == target_sizes[rand_index] def test_get_subset(): assert random_target_ds == reloaded_dataset.get_class(random_target_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samplets) member = test_dataset.samplet_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.randint(1, n, size=min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samplets - 1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc_in_class=perc) # separating the calculation by class to mimic the implementation in the # class expected_size = s", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_old_pyradigm.py::test_add,pyradigm/tests/test_old_pyradigm.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os, sys import numpy as np from os.path import join as pjoin, exists as pexists, realpath, basename, dirname, isfile sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm import MLDataset out_dir = '.' num_classes = np.random.randint( 2, 50) class_sizes = np.random.randint(10, 100, num_classes) num_features = np.random.randint(10, 100) num_samples = sum(class_sizes) class_set = np.array([ 'C{:05d}'.format(x) for x in range(num_classes)]) feat_names = np.array([ str(x) for x in range(num_features) ]) test_dataset = MLDataset() for class_index, class_id in enumerate(class_set): for sub_ix in range(class_sizes[class_index]): subj_id = '{}_S{:05d}'.format(class_set[class_index],sub_ix) feat = np.random.random(num_features) test_dataset.add_sample(subj_id, feat, class_index, class_id, feat_names) out_file = os.path.join(out_dir,'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = MLDataset() for sub_id in test_dataset.keys: feat = np.random.random(num_features) same_ids_new_feat.add_sample(sub_id, feat, test_dataset.labels[sub_id], test_dataset.classes[sub_id]) same_ids_new_feat.feature_names = np.array([ 'new_f{}'.format(x) for x in range( num_features) ]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) class_set, label_set, class_sizes = test_dataset.summarize_classes() reloaded_dataset = MLDataset(filepath=out_file, description='reloaded test_dataset') copy_dataset = MLDataset(in_dataset=test_dataset) rand_index = np.random.randint(0,len(class_set),1)[0] random_class_name = class_set[rand_index] random_class_ds = test_dataset.get_class(random_class_name) other_classes_ds = test_dataset - random_class_ds other_class_set = set(class_set)-set([random_class_name]) other_classes_get_with_list = test_dataset.get_class(other_class_set) recombined = other_classes_ds + random_class_ds empty_dataset = MLDataset() test2 = MLDataset() test3 = MLDataset() def test_empty(): assert not empty_dataset def test_num_classes(): assert test_dataset.num_classes == num_classes def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samples, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samples == sum(class_sizes) def test_subtract(): assert other_classes_ds.num_samples == sum(class_sizes) - class_sizes[rand_index] def test_get_class_list(): assert other_classes_ds == other_classes_get_with_list def test_add(): a = other_classes_ds + random_class_ds n = a.num_samples n1 = other_classes_ds.num_samples n2 = random_class_ds.num_samples assert n1 + n2 == n assert set(a.sample_ids) == set(other_classes_ds.sample_ids+random_class_ds.sample_ids) assert a.num_features == other_classes_ds.num_features == random_class_ds.num_features assert all(a.feature_names == other_classes_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([ test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.sample_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid+'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid]==new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_cant_read_nonexisting_file(): with raises(IOError): a = MLDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(ValueError): a = MLDataset(in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = MLDataset(filepath=None, in_dataset=None, data=list()) with raises(ValueError): c = MLDataset(filepath=None, in_dataset=None, data=None, labels=None, classes='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_labels() assert len(vec_labels)==len(sub_ids) assert len(vec_labels)==matrix.shape[0] def test_init_with_dict(): new_ds = MLDataset(data=test_dataset.data, labels=test_dataset.labels, classes=test_dataset.classes) assert new_ds == test_dataset def test_labels_setter(): fewer_labels = test_dataset.labels label_keys = list(fewer_labels.keys()) fewer_labels.pop(label_keys[0]) with raises(ValueError): test_dataset.labels = fewer_labels same_len_diff_key = fewer_labels same_len_diff_key[u'sldiursvdkvjs'] = 1 with raises(ValueError): test_dataset.labels = same_len_diff_key # must be dict with raises(ValueError): test_dataset.labels = None def test_classes_setter(): fewer_classes = test_dataset.classes classes_keys = list(fewer_classes.keys()) fewer_classes.pop(classes_keys[0]) with raises(ValueError): test_dataset.classes = fewer_classes same_len_diff_key = fewer_classes same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.classes = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features-2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.sample_ids[0] with raises(ValueError): test_dataset.add_sample(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.sample_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features+1,1) with raises(ValueError): test_dataset.add_sample(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_sample(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_class(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset= np.random.randint(1, nf, size=subset_len ) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = MLDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = MLDataset(filepath=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_class_ds.num_samples == class_sizes[rand_index] def test_get_subset(): assert random_class_ds == reloaded_dataset.get_class(random_class_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samples) member = test_dataset.sample_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.random_integers(1, n, min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samples-1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc_in_class=perc) # separating the calculation by class to mimic the implementation in the class expected_size = sum([np.int64(np.floor(n_in_class*perc)) for n_in_class in class_sizes]) assert subset.num_samples == expected_size def test_random_subset_by_count(): smallest_size = min(class_sizes) for count in np.random.randint(1, smallest_size, 7): subset = copy_dataset.random_subset_ids_by_count(count_per_class=count) assert len(subset) == num_classes*count def test_train_test_split_ids_count(): smallest_size = min(class_sizes) for count in np.random.randint(1, smallest_size, 7): subset_train, subset_test = copy_dataset.train_test_split_ids(count_per_class=count) assert len(subset_train) == num_classes*count assert len(subset_test ) == copy_dataset.num_samples-num_classes*count assert len(set(subset_train).intersection(subset_test))==0 with raises(ValueError): copy_dataset.train_test_split_ids(count_per_class=-1) with raises(ValueError): ", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_old_pyradigm.py::test_init_with_dict,pyradigm/tests/test_old_pyradigm.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os, sys import numpy as np from os.path import join as pjoin, exists as pexists, realpath, basename, dirname, isfile sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm import MLDataset out_dir = '.' num_classes = np.random.randint( 2, 50) class_sizes = np.random.randint(10, 100, num_classes) num_features = np.random.randint(10, 100) num_samples = sum(class_sizes) class_set = np.array([ 'C{:05d}'.format(x) for x in range(num_classes)]) feat_names = np.array([ str(x) for x in range(num_features) ]) test_dataset = MLDataset() for class_index, class_id in enumerate(class_set): for sub_ix in range(class_sizes[class_index]): subj_id = '{}_S{:05d}'.format(class_set[class_index],sub_ix) feat = np.random.random(num_features) test_dataset.add_sample(subj_id, feat, class_index, class_id, feat_names) out_file = os.path.join(out_dir,'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = MLDataset() for sub_id in test_dataset.keys: feat = np.random.random(num_features) same_ids_new_feat.add_sample(sub_id, feat, test_dataset.labels[sub_id], test_dataset.classes[sub_id]) same_ids_new_feat.feature_names = np.array([ 'new_f{}'.format(x) for x in range( num_features) ]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) class_set, label_set, class_sizes = test_dataset.summarize_classes() reloaded_dataset = MLDataset(filepath=out_file, description='reloaded test_dataset') copy_dataset = MLDataset(in_dataset=test_dataset) rand_index = np.random.randint(0,len(class_set),1)[0] random_class_name = class_set[rand_index] random_class_ds = test_dataset.get_class(random_class_name) other_classes_ds = test_dataset - random_class_ds other_class_set = set(class_set)-set([random_class_name]) other_classes_get_with_list = test_dataset.get_class(other_class_set) recombined = other_classes_ds + random_class_ds empty_dataset = MLDataset() test2 = MLDataset() test3 = MLDataset() def test_empty(): assert not empty_dataset def test_num_classes(): assert test_dataset.num_classes == num_classes def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samples, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samples == sum(class_sizes) def test_subtract(): assert other_classes_ds.num_samples == sum(class_sizes) - class_sizes[rand_index] def test_get_class_list(): assert other_classes_ds == other_classes_get_with_list def test_add(): a = other_classes_ds + random_class_ds n = a.num_samples n1 = other_classes_ds.num_samples n2 = random_class_ds.num_samples assert n1 + n2 == n assert set(a.sample_ids) == set(other_classes_ds.sample_ids+random_class_ds.sample_ids) assert a.num_features == other_classes_ds.num_features == random_class_ds.num_features assert all(a.feature_names == other_classes_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([ test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.sample_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid+'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid]==new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_cant_read_nonexisting_file(): with raises(IOError): a = MLDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(ValueError): a = MLDataset(in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = MLDataset(filepath=None, in_dataset=None, data=list()) with raises(ValueError): c = MLDataset(filepath=None, in_dataset=None, data=None, labels=None, classes='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_labels() assert len(vec_labels)==len(sub_ids) assert len(vec_labels)==matrix.shape[0] def test_init_with_dict(): new_ds = MLDataset(data=test_dataset.data, labels=test_dataset.labels, classes=test_dataset.classes) assert new_ds == test_dataset def test_labels_setter(): fewer_labels = test_dataset.labels label_keys = list(fewer_labels.keys()) fewer_labels.pop(label_keys[0]) with raises(ValueError): test_dataset.labels = fewer_labels same_len_diff_key = fewer_labels same_len_diff_key[u'sldiursvdkvjs'] = 1 with raises(ValueError): test_dataset.labels = same_len_diff_key # must be dict with raises(ValueError): test_dataset.labels = None def test_classes_setter(): fewer_classes = test_dataset.classes classes_keys = list(fewer_classes.keys()) fewer_classes.pop(classes_keys[0]) with raises(ValueError): test_dataset.classes = fewer_classes same_len_diff_key = fewer_classes same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.classes = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features-2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.sample_ids[0] with raises(ValueError): test_dataset.add_sample(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.sample_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features+1,1) with raises(ValueError): test_dataset.add_sample(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_sample(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_class(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset= np.random.randint(1, nf, size=subset_len ) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = MLDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = MLDataset(filepath=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_class_ds.num_samples == class_sizes[rand_index] def test_get_subset(): assert random_class_ds == reloaded_dataset.get_class(random_class_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samples) member = test_dataset.sample_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.random_integers(1, n, min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samples-1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc_in_class=perc) # separating the calculation by class to mimic the implementation in the class expected_size = sum([np.int64(np.floor(n_in_class*perc)) for n_in_class in class_sizes]) assert subset.num_samples == expected_size def test_random_subset_by_count(): smallest_size = min(class_sizes) for count in np.random.randint(1, smallest_size, 7): subset = copy_dataset.random_subset_ids_by_count(count_per_class=count) assert len(subset) == num_classes*count def test_train_test_split_ids_count(): smallest_size = min(class_sizes) for count in np.random.randint(1, smallest_size, 7): subset_train, subset_test = copy_dataset.train_test_split_ids(count_per_class=count) assert len(subset_train) == num_classes*count assert len(subset_test ) == copy_dataset.num_samples-num_classes*count assert len(set(subset_train).intersection(subset_test))==0 with raises(ValueError): copy_dataset.train_test_split_ids(count_per_class=-1) with raises(ValueError): ", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_old_pyradigm.py::test_num_classes,pyradigm/tests/test_old_pyradigm.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os, sys import numpy as np from os.path import join as pjoin, exists as pexists, realpath, basename, dirname, isfile sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm import MLDataset out_dir = '.' num_classes = np.random.randint( 2, 50) class_sizes = np.random.randint(10, 100, num_classes) num_features = np.random.randint(10, 100) num_samples = sum(class_sizes) class_set = np.array([ 'C{:05d}'.format(x) for x in range(num_classes)]) feat_names = np.array([ str(x) for x in range(num_features) ]) test_dataset = MLDataset() for class_index, class_id in enumerate(class_set): for sub_ix in range(class_sizes[class_index]): subj_id = '{}_S{:05d}'.format(class_set[class_index],sub_ix) feat = np.random.random(num_features) test_dataset.add_sample(subj_id, feat, class_index, class_id, feat_names) out_file = os.path.join(out_dir,'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = MLDataset() for sub_id in test_dataset.keys: feat = np.random.random(num_features) same_ids_new_feat.add_sample(sub_id, feat, test_dataset.labels[sub_id], test_dataset.classes[sub_id]) same_ids_new_feat.feature_names = np.array([ 'new_f{}'.format(x) for x in range( num_features) ]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) class_set, label_set, class_sizes = test_dataset.summarize_classes() reloaded_dataset = MLDataset(filepath=out_file, description='reloaded test_dataset') copy_dataset = MLDataset(in_dataset=test_dataset) rand_index = np.random.randint(0,len(class_set),1)[0] random_class_name = class_set[rand_index] random_class_ds = test_dataset.get_class(random_class_name) other_classes_ds = test_dataset - random_class_ds other_class_set = set(class_set)-set([random_class_name]) other_classes_get_with_list = test_dataset.get_class(other_class_set) recombined = other_classes_ds + random_class_ds empty_dataset = MLDataset() test2 = MLDataset() test3 = MLDataset() def test_empty(): assert not empty_dataset def test_num_classes(): assert test_dataset.num_classes == num_classes def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samples, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samples == sum(class_sizes) def test_subtract(): assert other_classes_ds.num_samples == sum(class_sizes) - class_sizes[rand_index] def test_get_class_list(): assert other_classes_ds == other_classes_get_with_list def test_add(): a = other_classes_ds + random_class_ds n = a.num_samples n1 = other_classes_ds.num_samples n2 = random_class_ds.num_samples assert n1 + n2 == n assert set(a.sample_ids) == set(other_classes_ds.sample_ids+random_class_ds.sample_ids) assert a.num_features == other_classes_ds.num_features == random_class_ds.num_features assert all(a.feature_names == other_classes_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([ test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.sample_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid+'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid]==new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_cant_read_nonexisting_file(): with raises(IOError): a = MLDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(ValueError): a = MLDataset(in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = MLDataset(filepath=None, in_dataset=None, data=list()) with raises(ValueError): c = MLDataset(filepath=None, in_dataset=None, data=None, labels=None, classes='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_labels() assert len(vec_labels)==len(sub_ids) assert len(vec_labels)==matrix.shape[0] def test_init_with_dict(): new_ds = MLDataset(data=test_dataset.data, labels=test_dataset.labels, classes=test_dataset.classes) assert new_ds == test_dataset def test_labels_setter(): fewer_labels = test_dataset.labels label_keys = list(fewer_labels.keys()) fewer_labels.pop(label_keys[0]) with raises(ValueError): test_dataset.labels = fewer_labels same_len_diff_key = fewer_labels same_len_diff_key[u'sldiursvdkvjs'] = 1 with raises(ValueError): test_dataset.labels = same_len_diff_key # must be dict with raises(ValueError): test_dataset.labels = None def test_classes_setter(): fewer_classes = test_dataset.classes classes_keys = list(fewer_classes.keys()) fewer_classes.pop(classes_keys[0]) with raises(ValueError): test_dataset.classes = fewer_classes same_len_diff_key = fewer_classes same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.classes = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features-2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.sample_ids[0] with raises(ValueError): test_dataset.add_sample(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.sample_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features+1,1) with raises(ValueError): test_dataset.add_sample(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_sample(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_class(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset= np.random.randint(1, nf, size=subset_len ) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = MLDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = MLDataset(filepath=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_class_ds.num_samples == class_sizes[rand_index] def test_get_subset(): assert random_class_ds == reloaded_dataset.get_class(random_class_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samples) member = test_dataset.sample_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.random_integers(1, n, min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samples-1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc_in_class=perc) # separating the calculation by class to mimic the implementation in the class expected_size = sum([np.int64(np.floor(n_in_class*perc)) for n_in_class in class_sizes]) assert subset.num_samples == expected_size def test_random_subset_by_count(): smallest_size = min(class_sizes) for count in np.random.randint(1, smallest_size, 7): subset = copy_dataset.random_subset_ids_by_count(count_per_class=count) assert len(subset) == num_classes*count def test_train_test_split_ids_count(): smallest_size = min(class_sizes) for count in np.random.randint(1, smallest_size, 7): subset_train, subset_test = copy_dataset.train_test_split_ids(count_per_class=count) assert len(subset_train) == num_classes*count assert len(subset_test ) == copy_dataset.num_samples-num_classes*count assert len(set(subset_train).intersection(subset_test))==0 with raises(ValueError): copy_dataset.train_test_split_ids(count_per_class=-1) with raises(ValueError): ", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_old_pyradigm.py::test_return_data_labels,pyradigm/tests/test_old_pyradigm.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os, sys import numpy as np from os.path import join as pjoin, exists as pexists, realpath, basename, dirname, isfile sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm import MLDataset out_dir = '.' num_classes = np.random.randint( 2, 50) class_sizes = np.random.randint(10, 100, num_classes) num_features = np.random.randint(10, 100) num_samples = sum(class_sizes) class_set = np.array([ 'C{:05d}'.format(x) for x in range(num_classes)]) feat_names = np.array([ str(x) for x in range(num_features) ]) test_dataset = MLDataset() for class_index, class_id in enumerate(class_set): for sub_ix in range(class_sizes[class_index]): subj_id = '{}_S{:05d}'.format(class_set[class_index],sub_ix) feat = np.random.random(num_features) test_dataset.add_sample(subj_id, feat, class_index, class_id, feat_names) out_file = os.path.join(out_dir,'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = MLDataset() for sub_id in test_dataset.keys: feat = np.random.random(num_features) same_ids_new_feat.add_sample(sub_id, feat, test_dataset.labels[sub_id], test_dataset.classes[sub_id]) same_ids_new_feat.feature_names = np.array([ 'new_f{}'.format(x) for x in range( num_features) ]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) class_set, label_set, class_sizes = test_dataset.summarize_classes() reloaded_dataset = MLDataset(filepath=out_file, description='reloaded test_dataset') copy_dataset = MLDataset(in_dataset=test_dataset) rand_index = np.random.randint(0,len(class_set),1)[0] random_class_name = class_set[rand_index] random_class_ds = test_dataset.get_class(random_class_name) other_classes_ds = test_dataset - random_class_ds other_class_set = set(class_set)-set([random_class_name]) other_classes_get_with_list = test_dataset.get_class(other_class_set) recombined = other_classes_ds + random_class_ds empty_dataset = MLDataset() test2 = MLDataset() test3 = MLDataset() def test_empty(): assert not empty_dataset def test_num_classes(): assert test_dataset.num_classes == num_classes def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samples, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samples == sum(class_sizes) def test_subtract(): assert other_classes_ds.num_samples == sum(class_sizes) - class_sizes[rand_index] def test_get_class_list(): assert other_classes_ds == other_classes_get_with_list def test_add(): a = other_classes_ds + random_class_ds n = a.num_samples n1 = other_classes_ds.num_samples n2 = random_class_ds.num_samples assert n1 + n2 == n assert set(a.sample_ids) == set(other_classes_ds.sample_ids+random_class_ds.sample_ids) assert a.num_features == other_classes_ds.num_features == random_class_ds.num_features assert all(a.feature_names == other_classes_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([ test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.sample_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid+'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid]==new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_cant_read_nonexisting_file(): with raises(IOError): a = MLDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(ValueError): a = MLDataset(in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = MLDataset(filepath=None, in_dataset=None, data=list()) with raises(ValueError): c = MLDataset(filepath=None, in_dataset=None, data=None, labels=None, classes='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_labels() assert len(vec_labels)==len(sub_ids) assert len(vec_labels)==matrix.shape[0] def test_init_with_dict(): new_ds = MLDataset(data=test_dataset.data, labels=test_dataset.labels, classes=test_dataset.classes) assert new_ds == test_dataset def test_labels_setter(): fewer_labels = test_dataset.labels label_keys = list(fewer_labels.keys()) fewer_labels.pop(label_keys[0]) with raises(ValueError): test_dataset.labels = fewer_labels same_len_diff_key = fewer_labels same_len_diff_key[u'sldiursvdkvjs'] = 1 with raises(ValueError): test_dataset.labels = same_len_diff_key # must be dict with raises(ValueError): test_dataset.labels = None def test_classes_setter(): fewer_classes = test_dataset.classes classes_keys = list(fewer_classes.keys()) fewer_classes.pop(classes_keys[0]) with raises(ValueError): test_dataset.classes = fewer_classes same_len_diff_key = fewer_classes same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.classes = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features-2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.sample_ids[0] with raises(ValueError): test_dataset.add_sample(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.sample_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features+1,1) with raises(ValueError): test_dataset.add_sample(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_sample(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_class(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset= np.random.randint(1, nf, size=subset_len ) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = MLDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = MLDataset(filepath=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_class_ds.num_samples == class_sizes[rand_index] def test_get_subset(): assert random_class_ds == reloaded_dataset.get_class(random_class_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samples) member = test_dataset.sample_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.random_integers(1, n, min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samples-1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc_in_class=perc) # separating the calculation by class to mimic the implementation in the class expected_size = sum([np.int64(np.floor(n_in_class*perc)) for n_in_class in class_sizes]) assert subset.num_samples == expected_size def test_random_subset_by_count(): smallest_size = min(class_sizes) for count in np.random.randint(1, smallest_size, 7): subset = copy_dataset.random_subset_ids_by_count(count_per_class=count) assert len(subset) == num_classes*count def test_train_test_split_ids_count(): smallest_size = min(class_sizes) for count in np.random.randint(1, smallest_size, 7): subset_train, subset_test = copy_dataset.train_test_split_ids(count_per_class=count) assert len(subset_train) == num_classes*count assert len(subset_test ) == copy_dataset.num_samples-num_classes*count assert len(set(subset_train).intersection(subset_test))==0 with raises(ValueError): copy_dataset.train_test_split_ids(count_per_class=-1) with raises(ValueError): ", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_regress_dataset.py::test_add,pyradigm/tests/test_regress_dataset.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import random import sys import numpy as np sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm.regress import RegressionDataset as RegrDataset out_dir = '.' num_targets = np.random.randint(2, 50) target_sizes = np.random.randint(10, 100, num_targets) num_features = np.random.randint(10, 100) num_samplets = sum(target_sizes) # randomizing target values also to avoid subtle bugs related to their value # using random.sample, as np.random.randint does not return all unique numbers target_set = random.sample(range(100), num_targets) feat_names = np.array([str(x) for x in range(num_features)]) test_dataset = RegrDataset() for target_index, target_id in enumerate(target_set): for sub_ix in range(target_sizes[target_index]): subj_id = '{}_S{:05d}'.format(target_id, sub_ix) feat = np.random.random(num_features) test_dataset.add_samplet(subj_id, feat, target_id, feature_names=feat_names) out_file = os.path.join(out_dir, 'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = RegrDataset() for sub_id in test_dataset.samplet_ids: feat = np.random.random(num_features) same_ids_new_feat.add_samplet(sub_id, feat, test_dataset.targets[sub_id]) same_ids_new_feat.feature_names = np.array(['new_f{}'.format(x) for x in range( num_features)]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) target_set, target_sizes = test_dataset.summarize() reloaded_dataset = RegrDataset(dataset_path=out_file, description='reloaded test_dataset') copy_dataset = RegrDataset(in_dataset=test_dataset) rand_index = np.random.randint(0, len(target_set), 1)[0] random_target_name = target_set[rand_index] random_target_ds = test_dataset.get_target(random_target_name) other_targets_ds = test_dataset - random_target_ds other_target_set = set(target_set) - set([random_target_name]) other_targets_get_with_list = test_dataset.get_target(other_target_set) recombined = other_targets_ds + random_target_ds empty_dataset = RegrDataset() test2 = RegrDataset() test3 = RegrDataset() def test_empty(): assert not empty_dataset def test_target_type(): rand_id = test_dataset.samplet_ids[np.random.randint(2, num_samplets)] if not isinstance(test_dataset.targets[rand_id], test_dataset._target_type): raise TypeError('invalid target type for samplet id {}'.format(rand_id)) def test_num_targets(): assert test_dataset.num_targets == num_targets def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samplets, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samplets == sum(target_sizes) def test_subtract(): assert other_targets_ds.num_samplets == sum(target_sizes) - target_sizes[ rand_index] def test_get_target_list(): assert other_targets_ds == other_targets_get_with_list def test_add(): a = other_targets_ds + random_target_ds n = a.num_samplets n1 = other_targets_ds.num_samplets n2 = random_target_ds.num_samplets assert n1 + n2 == n assert set(a.samplet_ids) == set( other_targets_ds.samplet_ids + random_target_ds.samplet_ids) assert a.num_features == other_targets_ds.num_features == \ random_target_ds.num_features assert all(a.feature_names == other_targets_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.samplet_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid + 'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid] == new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_data_type(): for in_dtype in [np.float_, np.int, np.bool_]: cds = RegrDataset(dtype=in_dtype) cds.add_samplet('a', [1, 2.0, -434], 2) if cds.dtype != in_dtype or cds['a'].dtype != in_dtype: raise TypeError('Dataset not maintaining the features in the requested' 'dtype {}. They are in {}'.format(in_dtype, cds.dtype)) def test_cant_read_nonexisting_file(): with raises(IOError): a = RegrDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(TypeError): a = RegrDataset( in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = RegrDataset(dataset_path=None, in_dataset=None, data=list()) with raises(ValueError): c = RegrDataset(dataset_path=None, in_dataset=None, data=None, targets='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_targets() assert len(vec_labels) == len(sub_ids) assert len(vec_labels) == matrix.shape[0] def test_init_with_dict(): new_ds = RegrDataset(data=test_dataset.data, targets=test_dataset.targets) assert new_ds == test_dataset # def test_labels_setter(): # fewer_labels = test_dataset.labels # label_keys = list(fewer_labels.samplet_ids()) # fewer_labels.pop(label_keys[0]) # # with raises(ValueError): # test_dataset.labels = fewer_labels # # same_len_diff_key = fewer_labels # same_len_diff_key[u'sldiursvdkvjs'] = 1 # with raises(ValueError): # test_dataset.labels = same_len_diff_key # # # must be dict # with raises(ValueError): # test_dataset.labels = None def test_targets_setter(): fewer_targets = test_dataset.targets targets_keys = list(fewer_targets.keys()) fewer_targets.pop(targets_keys[0]) with raises(ValueError): test_dataset.targets = fewer_targets same_len_diff_key = fewer_targets same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.targets = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features - 2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.samplet_ids[0] with raises(ValueError): test_dataset.add_samplet(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.samplet_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features + 1, 1) with raises(ValueError): test_dataset.add_samplet(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_samplet(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_target(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset = np.random.randint(1, nf, size=subset_len) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = RegrDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = RegrDataset(dataset_path=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_target_ds.num_samplets == target_sizes[rand_index] def test_get_subset(): assert random_target_ds == reloaded_dataset.get_target(random_target_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samplets) member = test_dataset.samplet_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.random_integers(1, n, min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samplets - 1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc=perc) # NOT separa", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_regress_dataset.py::test_cant_write_to_nonexisting_dir,pyradigm/tests/test_regress_dataset.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import random import sys import numpy as np sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm.regress import RegressionDataset as RegrDataset out_dir = '.' num_targets = np.random.randint(2, 50) target_sizes = np.random.randint(10, 100, num_targets) num_features = np.random.randint(10, 100) num_samplets = sum(target_sizes) # randomizing target values also to avoid subtle bugs related to their value # using random.sample, as np.random.randint does not return all unique numbers target_set = random.sample(range(100), num_targets) feat_names = np.array([str(x) for x in range(num_features)]) test_dataset = RegrDataset() for target_index, target_id in enumerate(target_set): for sub_ix in range(target_sizes[target_index]): subj_id = '{}_S{:05d}'.format(target_id, sub_ix) feat = np.random.random(num_features) test_dataset.add_samplet(subj_id, feat, target_id, feature_names=feat_names) out_file = os.path.join(out_dir, 'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = RegrDataset() for sub_id in test_dataset.samplet_ids: feat = np.random.random(num_features) same_ids_new_feat.add_samplet(sub_id, feat, test_dataset.targets[sub_id]) same_ids_new_feat.feature_names = np.array(['new_f{}'.format(x) for x in range( num_features)]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) target_set, target_sizes = test_dataset.summarize() reloaded_dataset = RegrDataset(dataset_path=out_file, description='reloaded test_dataset') copy_dataset = RegrDataset(in_dataset=test_dataset) rand_index = np.random.randint(0, len(target_set), 1)[0] random_target_name = target_set[rand_index] random_target_ds = test_dataset.get_target(random_target_name) other_targets_ds = test_dataset - random_target_ds other_target_set = set(target_set) - set([random_target_name]) other_targets_get_with_list = test_dataset.get_target(other_target_set) recombined = other_targets_ds + random_target_ds empty_dataset = RegrDataset() test2 = RegrDataset() test3 = RegrDataset() def test_empty(): assert not empty_dataset def test_target_type(): rand_id = test_dataset.samplet_ids[np.random.randint(2, num_samplets)] if not isinstance(test_dataset.targets[rand_id], test_dataset._target_type): raise TypeError('invalid target type for samplet id {}'.format(rand_id)) def test_num_targets(): assert test_dataset.num_targets == num_targets def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samplets, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samplets == sum(target_sizes) def test_subtract(): assert other_targets_ds.num_samplets == sum(target_sizes) - target_sizes[ rand_index] def test_get_target_list(): assert other_targets_ds == other_targets_get_with_list def test_add(): a = other_targets_ds + random_target_ds n = a.num_samplets n1 = other_targets_ds.num_samplets n2 = random_target_ds.num_samplets assert n1 + n2 == n assert set(a.samplet_ids) == set( other_targets_ds.samplet_ids + random_target_ds.samplet_ids) assert a.num_features == other_targets_ds.num_features == \ random_target_ds.num_features assert all(a.feature_names == other_targets_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.samplet_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid + 'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid] == new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_data_type(): for in_dtype in [np.float_, np.int, np.bool_]: cds = RegrDataset(dtype=in_dtype) cds.add_samplet('a', [1, 2.0, -434], 2) if cds.dtype != in_dtype or cds['a'].dtype != in_dtype: raise TypeError('Dataset not maintaining the features in the requested' 'dtype {}. They are in {}'.format(in_dtype, cds.dtype)) def test_cant_read_nonexisting_file(): with raises(IOError): a = RegrDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(TypeError): a = RegrDataset( in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = RegrDataset(dataset_path=None, in_dataset=None, data=list()) with raises(ValueError): c = RegrDataset(dataset_path=None, in_dataset=None, data=None, targets='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_targets() assert len(vec_labels) == len(sub_ids) assert len(vec_labels) == matrix.shape[0] def test_init_with_dict(): new_ds = RegrDataset(data=test_dataset.data, targets=test_dataset.targets) assert new_ds == test_dataset # def test_labels_setter(): # fewer_labels = test_dataset.labels # label_keys = list(fewer_labels.samplet_ids()) # fewer_labels.pop(label_keys[0]) # # with raises(ValueError): # test_dataset.labels = fewer_labels # # same_len_diff_key = fewer_labels # same_len_diff_key[u'sldiursvdkvjs'] = 1 # with raises(ValueError): # test_dataset.labels = same_len_diff_key # # # must be dict # with raises(ValueError): # test_dataset.labels = None def test_targets_setter(): fewer_targets = test_dataset.targets targets_keys = list(fewer_targets.keys()) fewer_targets.pop(targets_keys[0]) with raises(ValueError): test_dataset.targets = fewer_targets same_len_diff_key = fewer_targets same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.targets = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features - 2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.samplet_ids[0] with raises(ValueError): test_dataset.add_samplet(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.samplet_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features + 1, 1) with raises(ValueError): test_dataset.add_samplet(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_samplet(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_target(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset = np.random.randint(1, nf, size=subset_len) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = RegrDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = RegrDataset(dataset_path=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_target_ds.num_samplets == target_sizes[rand_index] def test_get_subset(): assert random_target_ds == reloaded_dataset.get_target(random_target_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samplets) member = test_dataset.samplet_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.random_integers(1, n, min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samplets - 1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc=perc) # NOT separa", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_regress_dataset.py::test_init_with_dict,pyradigm/tests/test_regress_dataset.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import random import sys import numpy as np sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm.regress import RegressionDataset as RegrDataset out_dir = '.' num_targets = np.random.randint(2, 50) target_sizes = np.random.randint(10, 100, num_targets) num_features = np.random.randint(10, 100) num_samplets = sum(target_sizes) # randomizing target values also to avoid subtle bugs related to their value # using random.sample, as np.random.randint does not return all unique numbers target_set = random.sample(range(100), num_targets) feat_names = np.array([str(x) for x in range(num_features)]) test_dataset = RegrDataset() for target_index, target_id in enumerate(target_set): for sub_ix in range(target_sizes[target_index]): subj_id = '{}_S{:05d}'.format(target_id, sub_ix) feat = np.random.random(num_features) test_dataset.add_samplet(subj_id, feat, target_id, feature_names=feat_names) out_file = os.path.join(out_dir, 'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = RegrDataset() for sub_id in test_dataset.samplet_ids: feat = np.random.random(num_features) same_ids_new_feat.add_samplet(sub_id, feat, test_dataset.targets[sub_id]) same_ids_new_feat.feature_names = np.array(['new_f{}'.format(x) for x in range( num_features)]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) target_set, target_sizes = test_dataset.summarize() reloaded_dataset = RegrDataset(dataset_path=out_file, description='reloaded test_dataset') copy_dataset = RegrDataset(in_dataset=test_dataset) rand_index = np.random.randint(0, len(target_set), 1)[0] random_target_name = target_set[rand_index] random_target_ds = test_dataset.get_target(random_target_name) other_targets_ds = test_dataset - random_target_ds other_target_set = set(target_set) - set([random_target_name]) other_targets_get_with_list = test_dataset.get_target(other_target_set) recombined = other_targets_ds + random_target_ds empty_dataset = RegrDataset() test2 = RegrDataset() test3 = RegrDataset() def test_empty(): assert not empty_dataset def test_target_type(): rand_id = test_dataset.samplet_ids[np.random.randint(2, num_samplets)] if not isinstance(test_dataset.targets[rand_id], test_dataset._target_type): raise TypeError('invalid target type for samplet id {}'.format(rand_id)) def test_num_targets(): assert test_dataset.num_targets == num_targets def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samplets, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samplets == sum(target_sizes) def test_subtract(): assert other_targets_ds.num_samplets == sum(target_sizes) - target_sizes[ rand_index] def test_get_target_list(): assert other_targets_ds == other_targets_get_with_list def test_add(): a = other_targets_ds + random_target_ds n = a.num_samplets n1 = other_targets_ds.num_samplets n2 = random_target_ds.num_samplets assert n1 + n2 == n assert set(a.samplet_ids) == set( other_targets_ds.samplet_ids + random_target_ds.samplet_ids) assert a.num_features == other_targets_ds.num_features == \ random_target_ds.num_features assert all(a.feature_names == other_targets_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.samplet_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid + 'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid] == new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_data_type(): for in_dtype in [np.float_, np.int, np.bool_]: cds = RegrDataset(dtype=in_dtype) cds.add_samplet('a', [1, 2.0, -434], 2) if cds.dtype != in_dtype or cds['a'].dtype != in_dtype: raise TypeError('Dataset not maintaining the features in the requested' 'dtype {}. They are in {}'.format(in_dtype, cds.dtype)) def test_cant_read_nonexisting_file(): with raises(IOError): a = RegrDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(TypeError): a = RegrDataset( in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = RegrDataset(dataset_path=None, in_dataset=None, data=list()) with raises(ValueError): c = RegrDataset(dataset_path=None, in_dataset=None, data=None, targets='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_targets() assert len(vec_labels) == len(sub_ids) assert len(vec_labels) == matrix.shape[0] def test_init_with_dict(): new_ds = RegrDataset(data=test_dataset.data, targets=test_dataset.targets) assert new_ds == test_dataset # def test_labels_setter(): # fewer_labels = test_dataset.labels # label_keys = list(fewer_labels.samplet_ids()) # fewer_labels.pop(label_keys[0]) # # with raises(ValueError): # test_dataset.labels = fewer_labels # # same_len_diff_key = fewer_labels # same_len_diff_key[u'sldiursvdkvjs'] = 1 # with raises(ValueError): # test_dataset.labels = same_len_diff_key # # # must be dict # with raises(ValueError): # test_dataset.labels = None def test_targets_setter(): fewer_targets = test_dataset.targets targets_keys = list(fewer_targets.keys()) fewer_targets.pop(targets_keys[0]) with raises(ValueError): test_dataset.targets = fewer_targets same_len_diff_key = fewer_targets same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.targets = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features - 2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.samplet_ids[0] with raises(ValueError): test_dataset.add_samplet(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.samplet_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features + 1, 1) with raises(ValueError): test_dataset.add_samplet(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_samplet(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_target(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset = np.random.randint(1, nf, size=subset_len) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = RegrDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = RegrDataset(dataset_path=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_target_ds.num_samplets == target_sizes[rand_index] def test_get_subset(): assert random_target_ds == reloaded_dataset.get_target(random_target_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samplets) member = test_dataset.samplet_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.random_integers(1, n, min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samplets - 1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc=perc) # NOT separa", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_regress_dataset.py::test_num_targets,pyradigm/tests/test_regress_dataset.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import random import sys import numpy as np sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm.regress import RegressionDataset as RegrDataset out_dir = '.' num_targets = np.random.randint(2, 50) target_sizes = np.random.randint(10, 100, num_targets) num_features = np.random.randint(10, 100) num_samplets = sum(target_sizes) # randomizing target values also to avoid subtle bugs related to their value # using random.sample, as np.random.randint does not return all unique numbers target_set = random.sample(range(100), num_targets) feat_names = np.array([str(x) for x in range(num_features)]) test_dataset = RegrDataset() for target_index, target_id in enumerate(target_set): for sub_ix in range(target_sizes[target_index]): subj_id = '{}_S{:05d}'.format(target_id, sub_ix) feat = np.random.random(num_features) test_dataset.add_samplet(subj_id, feat, target_id, feature_names=feat_names) out_file = os.path.join(out_dir, 'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = RegrDataset() for sub_id in test_dataset.samplet_ids: feat = np.random.random(num_features) same_ids_new_feat.add_samplet(sub_id, feat, test_dataset.targets[sub_id]) same_ids_new_feat.feature_names = np.array(['new_f{}'.format(x) for x in range( num_features)]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) target_set, target_sizes = test_dataset.summarize() reloaded_dataset = RegrDataset(dataset_path=out_file, description='reloaded test_dataset') copy_dataset = RegrDataset(in_dataset=test_dataset) rand_index = np.random.randint(0, len(target_set), 1)[0] random_target_name = target_set[rand_index] random_target_ds = test_dataset.get_target(random_target_name) other_targets_ds = test_dataset - random_target_ds other_target_set = set(target_set) - set([random_target_name]) other_targets_get_with_list = test_dataset.get_target(other_target_set) recombined = other_targets_ds + random_target_ds empty_dataset = RegrDataset() test2 = RegrDataset() test3 = RegrDataset() def test_empty(): assert not empty_dataset def test_target_type(): rand_id = test_dataset.samplet_ids[np.random.randint(2, num_samplets)] if not isinstance(test_dataset.targets[rand_id], test_dataset._target_type): raise TypeError('invalid target type for samplet id {}'.format(rand_id)) def test_num_targets(): assert test_dataset.num_targets == num_targets def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samplets, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samplets == sum(target_sizes) def test_subtract(): assert other_targets_ds.num_samplets == sum(target_sizes) - target_sizes[ rand_index] def test_get_target_list(): assert other_targets_ds == other_targets_get_with_list def test_add(): a = other_targets_ds + random_target_ds n = a.num_samplets n1 = other_targets_ds.num_samplets n2 = random_target_ds.num_samplets assert n1 + n2 == n assert set(a.samplet_ids) == set( other_targets_ds.samplet_ids + random_target_ds.samplet_ids) assert a.num_features == other_targets_ds.num_features == \ random_target_ds.num_features assert all(a.feature_names == other_targets_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.samplet_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid + 'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid] == new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_data_type(): for in_dtype in [np.float_, np.int, np.bool_]: cds = RegrDataset(dtype=in_dtype) cds.add_samplet('a', [1, 2.0, -434], 2) if cds.dtype != in_dtype or cds['a'].dtype != in_dtype: raise TypeError('Dataset not maintaining the features in the requested' 'dtype {}. They are in {}'.format(in_dtype, cds.dtype)) def test_cant_read_nonexisting_file(): with raises(IOError): a = RegrDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(TypeError): a = RegrDataset( in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = RegrDataset(dataset_path=None, in_dataset=None, data=list()) with raises(ValueError): c = RegrDataset(dataset_path=None, in_dataset=None, data=None, targets='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_targets() assert len(vec_labels) == len(sub_ids) assert len(vec_labels) == matrix.shape[0] def test_init_with_dict(): new_ds = RegrDataset(data=test_dataset.data, targets=test_dataset.targets) assert new_ds == test_dataset # def test_labels_setter(): # fewer_labels = test_dataset.labels # label_keys = list(fewer_labels.samplet_ids()) # fewer_labels.pop(label_keys[0]) # # with raises(ValueError): # test_dataset.labels = fewer_labels # # same_len_diff_key = fewer_labels # same_len_diff_key[u'sldiursvdkvjs'] = 1 # with raises(ValueError): # test_dataset.labels = same_len_diff_key # # # must be dict # with raises(ValueError): # test_dataset.labels = None def test_targets_setter(): fewer_targets = test_dataset.targets targets_keys = list(fewer_targets.keys()) fewer_targets.pop(targets_keys[0]) with raises(ValueError): test_dataset.targets = fewer_targets same_len_diff_key = fewer_targets same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.targets = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features - 2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.samplet_ids[0] with raises(ValueError): test_dataset.add_samplet(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.samplet_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features + 1, 1) with raises(ValueError): test_dataset.add_samplet(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_samplet(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_target(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset = np.random.randint(1, nf, size=subset_len) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = RegrDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = RegrDataset(dataset_path=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_target_ds.num_samplets == target_sizes[rand_index] def test_get_subset(): assert random_target_ds == reloaded_dataset.get_target(random_target_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samplets) member = test_dataset.samplet_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.random_integers(1, n, min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samplets - 1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc=perc) # NOT separa", https://github.com/raamana/pyradigm,01d47cf34a9af4bb104e2891559059a522221b4d,pyradigm/tests/test_regress_dataset.py::test_return_data_labels,pyradigm/tests/test_regress_dataset.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import random import sys import numpy as np sys.dont_write_bytecode = True from pytest import raises, warns from pyradigm.regress import RegressionDataset as RegrDataset out_dir = '.' num_targets = np.random.randint(2, 50) target_sizes = np.random.randint(10, 100, num_targets) num_features = np.random.randint(10, 100) num_samplets = sum(target_sizes) # randomizing target values also to avoid subtle bugs related to their value # using random.sample, as np.random.randint does not return all unique numbers target_set = random.sample(range(100), num_targets) feat_names = np.array([str(x) for x in range(num_features)]) test_dataset = RegrDataset() for target_index, target_id in enumerate(target_set): for sub_ix in range(target_sizes[target_index]): subj_id = '{}_S{:05d}'.format(target_id, sub_ix) feat = np.random.random(num_features) test_dataset.add_samplet(subj_id, feat, target_id, feature_names=feat_names) out_file = os.path.join(out_dir, 'random_example_dataset.pkl') test_dataset.save(out_file) # same IDs, new features same_ids_new_feat = RegrDataset() for sub_id in test_dataset.samplet_ids: feat = np.random.random(num_features) same_ids_new_feat.add_samplet(sub_id, feat, test_dataset.targets[sub_id]) same_ids_new_feat.feature_names = np.array(['new_f{}'.format(x) for x in range( num_features)]) test_dataset.description = 'test dataset' print(test_dataset) print('default format:\n {}'.format(test_dataset)) print('full repr :\n {:full}'.format(test_dataset)) print('string/short :\n {:s}'.format(test_dataset)) target_set, target_sizes = test_dataset.summarize() reloaded_dataset = RegrDataset(dataset_path=out_file, description='reloaded test_dataset') copy_dataset = RegrDataset(in_dataset=test_dataset) rand_index = np.random.randint(0, len(target_set), 1)[0] random_target_name = target_set[rand_index] random_target_ds = test_dataset.get_target(random_target_name) other_targets_ds = test_dataset - random_target_ds other_target_set = set(target_set) - set([random_target_name]) other_targets_get_with_list = test_dataset.get_target(other_target_set) recombined = other_targets_ds + random_target_ds empty_dataset = RegrDataset() test2 = RegrDataset() test3 = RegrDataset() def test_empty(): assert not empty_dataset def test_target_type(): rand_id = test_dataset.samplet_ids[np.random.randint(2, num_samplets)] if not isinstance(test_dataset.targets[rand_id], test_dataset._target_type): raise TypeError('invalid target type for samplet id {}'.format(rand_id)) def test_num_targets(): assert test_dataset.num_targets == num_targets def test_num_features(): assert test_dataset.num_features == num_features def test_shape(): assert test_dataset.shape == (num_samplets, num_features) def test_num_features_setter(): with raises(AttributeError): test_dataset.num_features = 0 def test_num_samples(): assert test_dataset.num_samplets == sum(target_sizes) def test_subtract(): assert other_targets_ds.num_samplets == sum(target_sizes) - target_sizes[ rand_index] def test_get_target_list(): assert other_targets_ds == other_targets_get_with_list def test_add(): a = other_targets_ds + random_target_ds n = a.num_samplets n1 = other_targets_ds.num_samplets n2 = random_target_ds.num_samplets assert n1 + n2 == n assert set(a.samplet_ids) == set( other_targets_ds.samplet_ids + random_target_ds.samplet_ids) assert a.num_features == other_targets_ds.num_features == \ random_target_ds.num_features assert all(a.feature_names == other_targets_ds.feature_names) comb_ds = test_dataset + same_ids_new_feat comb_names = np.concatenate([test_dataset.feature_names, same_ids_new_feat.feature_names]) if not all(comb_ds.feature_names == comb_names): raise ValueError('feature names were not carried forward in combining two ' 'datasets with same IDs and different feature names!') def test_set_existing_sample(): sid = test_dataset.samplet_ids[0] new_feat = np.random.random(num_features) with raises(KeyError): test_dataset[sid + 'nonexisting'] = new_feat with raises(ValueError): test_dataset[sid] = new_feat[:-2] # diff dimensionality test_dataset[sid] = new_feat if not np.all(test_dataset[sid] == new_feat): raise ValueError('Bug in replacing features for an existing sample!' 'Retrieved features do not match previously set features.') def test_data_type(): for in_dtype in [np.float_, np.int, np.bool_]: cds = RegrDataset(dtype=in_dtype) cds.add_samplet('a', [1, 2.0, -434], 2) if cds.dtype != in_dtype or cds['a'].dtype != in_dtype: raise TypeError('Dataset not maintaining the features in the requested' 'dtype {}. They are in {}'.format(in_dtype, cds.dtype)) def test_cant_read_nonexisting_file(): with raises(IOError): a = RegrDataset('/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') def test_cant_write_to_nonexisting_dir(): with raises(IOError): test_dataset.save('/nonexistentrandomdir/jdknvoindvi93/arbitrary.noname.pkl') def test_invalid_constructor(): with raises(TypeError): a = RegrDataset( in_dataset='/nonexistentrandomdir/disofddlsfj/arbitrary.noname.pkl') with raises(ValueError): # data simply should not be a dict b = RegrDataset(dataset_path=None, in_dataset=None, data=list()) with raises(ValueError): c = RegrDataset(dataset_path=None, in_dataset=None, data=None, targets='invalid_value') def test_return_data_labels(): matrix, vec_labels, sub_ids = test_dataset.data_and_targets() assert len(vec_labels) == len(sub_ids) assert len(vec_labels) == matrix.shape[0] def test_init_with_dict(): new_ds = RegrDataset(data=test_dataset.data, targets=test_dataset.targets) assert new_ds == test_dataset # def test_labels_setter(): # fewer_labels = test_dataset.labels # label_keys = list(fewer_labels.samplet_ids()) # fewer_labels.pop(label_keys[0]) # # with raises(ValueError): # test_dataset.labels = fewer_labels # # same_len_diff_key = fewer_labels # same_len_diff_key[u'sldiursvdkvjs'] = 1 # with raises(ValueError): # test_dataset.labels = same_len_diff_key # # # must be dict # with raises(ValueError): # test_dataset.labels = None def test_targets_setter(): fewer_targets = test_dataset.targets targets_keys = list(fewer_targets.keys()) fewer_targets.pop(targets_keys[0]) with raises(ValueError): test_dataset.targets = fewer_targets same_len_diff_key = fewer_targets same_len_diff_key['sldiursvdkvjs'] = 'lfjd' with raises(ValueError): test_dataset.targets = same_len_diff_key def test_feat_names_setter(): # fewer with raises(ValueError): test_dataset.feature_names = feat_names[0:test_dataset.num_features - 2] # too many with raises(ValueError): test_dataset.feature_names = np.append(feat_names, 'blahblah') def test_add_existing_id(): sid = test_dataset.samplet_ids[0] with raises(ValueError): test_dataset.add_samplet(sid, None, None) def test_add_new_id_diff_dim(): new_id = 'dsfdkfslj38748937439kdshfkjhf38' sid = test_dataset.samplet_ids[0] data_diff_dim = np.random.rand(test_dataset.num_features + 1, 1) with raises(ValueError): test_dataset.add_samplet(new_id, data_diff_dim, None, None) def test_del_nonexisting_id(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.del_samplet(nonexisting_id) def test_get_nonexisting_class(): nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with raises(ValueError): test_dataset.get_target(nonexisting_id) def test_rand_feat_subset(): nf = copy_dataset.num_features subset_len = np.random.randint(1, nf) subset = np.random.randint(1, nf, size=subset_len) subds = copy_dataset.get_feature_subset(subset) assert subds.num_features == subset_len def test_eq_self(): assert test_dataset == test_dataset def test_eq_copy(): new_copy = RegrDataset(in_dataset=copy_dataset) assert new_copy == copy_dataset def test_unpickling(): out_file = os.path.join(out_dir, 'random_pickled_dataset.pkl') copy_dataset.save(out_file) reloaded_dataset = RegrDataset(dataset_path=out_file, description='reloaded test_dataset') assert copy_dataset == reloaded_dataset def test_subset_class(): assert random_target_ds.num_samplets == target_sizes[rand_index] def test_get_subset(): assert random_target_ds == reloaded_dataset.get_target(random_target_name) nonexisting_id = u'dsfdkfslj38748937439kdshfkjhf38' with warns(UserWarning): test_dataset.get_subset(nonexisting_id) def test_membership(): rand_idx = np.random.randint(0, test_dataset.num_samplets) member = test_dataset.samplet_ids[rand_idx] not_member = u'sdfdkshfdsk34823058wdkfhd83hifnalwe8fh8t' assert member in test_dataset assert not_member not in test_dataset def rand_ints_range(n, k): return np.random.random_integers(1, n, min(n, k)) def test_glance(): for k in np.random.randint(1, test_dataset.num_samplets - 1, 10): glanced_subset = test_dataset.glance(k) assert len(glanced_subset) == k def test_random_subset(): for perc in np.arange(0.1, 1, 0.2): subset = copy_dataset.random_subset(perc=perc) # NOT separa", https://github.com/raphaelvallat/pingouin,08560bb978949e97653b203f7374e90b022b88fb,pingouin/tests/test_regression.py::TestRegression::test_logistic_regression,pingouin/tests/test_regression.py,OD-Vic,flaky,Accepted,https://github.com/raphaelvallat/pingouin/pull/303,classify;root_cause,"import pytest import numpy as np import pandas as pd from unittest import TestCase from scipy.stats import linregress, zscore from sklearn.linear_model import LinearRegression from pandas.testing import assert_frame_equal from numpy.testing import assert_almost_equal, assert_equal from pingouin import read_dataset from pingouin.regression import (linear_regression, logistic_regression, mediation_analysis, _pval_from_bootci) # 1st dataset: mediation df = read_dataset('mediation') df['Zero'] = 0 df['One'] = 1 df['Two'] = 2 df_nan = df.copy() df_nan.loc[1, 'M'] = np.nan df_nan.loc[10, 'X'] = np.nan df_nan.loc[12, ['Y', 'Ybin']] = np.nan # 2nd dataset: penguins data = read_dataset('penguins').dropna() data['male'] = (data['sex'] == 'male').astype(int) data['body_mass_kg'] = data['body_mass_g'] / 1000 class TestRegression(TestCase): """"""Test regression.py."""""" def test_linear_regression(self): """"""Test function linear_regression. Compare against JASP and R lm() function. """""" # Simple regression (compare to R lm()) lm = linear_regression(df['X'], df['Y']) # Pingouin sc = linregress(df['X'], df['Y']) # SciPy # When using assert_equal, we need to use .to_numpy() assert_equal(lm['names'].to_numpy(), ['Intercept', 'X']) assert_almost_equal(lm['coef'][1], sc.slope) assert_almost_equal(lm['coef'][0], sc.intercept) assert_almost_equal(lm['se'][1], sc.stderr) assert_almost_equal(lm['pval'][1], sc.pvalue) assert_almost_equal(np.sqrt(lm['r2'][0]), sc.rvalue) assert lm.residuals_.size == df['Y'].size assert_equal(lm['CI[2.5%]'].round(5).to_numpy(), [1.48155, 0.17553]) assert_equal(lm['CI[97.5%]'].round(5).to_numpy(), [4.23286, 0.61672]) assert round(lm['r2'].iloc[0], 4) == 0.1147 assert round(lm['adj_r2'].iloc[0], 4) == 0.1057 assert lm.df_model_ == 1 assert lm.df_resid_ == 98 # Multiple regression with intercept (compare to JASP) X = df[['X', 'M']].to_numpy() y = df['Y'].to_numpy() lm = linear_regression(X, y, as_dataframe=False) # Pingouin sk = LinearRegression(fit_intercept=True).fit(X, y) # SkLearn assert_equal(lm['names'], ['Intercept', 'x1', 'x2']) assert_almost_equal(lm['coef'][1:], sk.coef_) assert_almost_equal(lm['coef'][0], sk.intercept_) assert_almost_equal(sk.score(X, y), lm['r2']) assert lm['residuals'].size == y.size # No need for .to_numpy here because we're using a dict and not pandas assert_equal([.605, .110, .101], np.round(lm['se'], 3)) assert_equal([3.145, 0.361, 6.321], np.round(lm['T'], 3)) assert_equal([0.002, 0.719, 0.000], np.round(lm['pval'], 3)) assert_equal([.703, -.178, .436], np.round(lm['CI[2.5%]'], 3)) assert_equal([3.106, .257, .835], np.round(lm['CI[97.5%]'], 3)) # No intercept lm = linear_regression(X, y, add_intercept=False, as_dataframe=False) sk = LinearRegression(fit_intercept=False).fit(X, y) assert_almost_equal(lm['coef'], sk.coef_) # Scikit-learn gives wrong R^2 score when no intercept present because # sklearn.metrics.r2_score always assumes that an intercept is present # https://stackoverflow.com/questions/54614157/scikit-learn-statsmodels-which-r-squared-is-correct # assert_almost_equal(sk.score(X, y), lm['r2']) # Instead, we compare to R lm() function: assert round(lm['r2'], 4) == 0.9096 assert round(lm['adj_r2'], 4) == 0.9078 assert lm['df_model'] == 2 assert lm['df_resid'] == 98 # Test other arguments linear_regression(df[['X', 'M']], df['Y'], coef_only=True) linear_regression(df[['X', 'M']], df['Y'], alpha=0.01) linear_regression(df[['X', 'M']], df['Y'], alpha=0.10) # With missing values linear_regression(df_nan[['X', 'M']], df_nan['Y'], remove_na=True) # With columns with only one unique value lm1 = linear_regression(df[['X', 'M', 'One']], df['Y']) lm2 = linear_regression(df[['X', 'M', 'One']], df['Y'], add_intercept=False) assert lm1.shape[0] == 3 assert lm2.shape[0] == 3 assert np.isclose(lm1.at[0, 'r2'], lm2.at[0, 'r2']) # With zero-only column lm1 = linear_regression(df[['X', 'M', 'Zero', 'One']], df['Y']) lm2 = linear_regression(df[['X', 'M', 'Zero', 'One']], df['Y'].to_numpy(), add_intercept=False) lm3 = linear_regression(df[['X', 'Zero', 'M', 'Zero']].to_numpy(), df['Y'], add_intercept=False) assert_equal(lm1.loc[:, 'names'].to_numpy(), ['Intercept', 'X', 'M']) assert_equal(lm2.loc[:, 'names'].to_numpy(), ['X', 'M', 'One']) assert_equal(lm3.loc[:, 'names'].to_numpy(), ['x1', 'x3']) # With duplicate columns lm1 = linear_regression(df[['X', 'One', 'Zero', 'M', 'M', 'X']], df['Y']) lm2 = linear_regression( df[['X', 'One', 'Zero', 'M', 'M', 'X']].to_numpy(), df['Y'], add_intercept=False ) assert_equal(lm1.loc[:, 'names'].to_numpy(), ['Intercept', 'X', 'M']) assert_equal(lm2.loc[:, 'names'].to_numpy(), ['x1', 'x2', 'x4']) # Relative importance # Compare to R package relaimpo # >>> data <- read.csv('mediation.csv') # >>> lm1 <- lm(Y ~ X + M, data = data) # >>> calc.relimp(lm1, type=c(""lmg"")) lm = linear_regression(df[['X', 'M']], df['Y'], relimp=True) assert_almost_equal(lm.loc[[1, 2], 'relimp'], [0.05778011, 0.31521913]) assert_almost_equal(lm.loc[[1, 2], 'relimp_perc'], [15.49068, 84.50932], decimal=4) # Now we make sure that relimp_perc sums to 100% and relimp sums to r2 assert np.isclose(lm['relimp_perc'].sum(), 100.) assert np.isclose(lm['relimp'].sum(), lm.at[0, 'r2']) # 2 predictors, no intercept # Careful here, the sum of relimp is always the R^2 of the model # INCLUDING the intercept. Therefore, if the data are not normalized # and we set add_intercept to false, the sum of relimp will be # higher than the linear regression model. # A workaround is to standardize our data before: df_z = df[['X', 'M', 'Y']].apply(zscore) lm = linear_regression(df_z[['X', 'M']], df_z['Y'], add_intercept=False, as_dataframe=False, relimp=True) assert_almost_equal(lm['relimp'], [0.05778011, 0.31521913], decimal=4) assert_almost_equal(lm['relimp_perc'], [15.49068, 84.50932], decimal=4) assert np.isclose(np.sum(lm['relimp']), lm['r2']) # 3 predictors + intercept lm = linear_regression(df[['X', 'M', 'Ybin']], df['Y'], relimp=True) assert_almost_equal(lm.loc[[1, 2, 3], 'relimp'], [0.06010737, 0.31724368, 0.01217479]) assert_almost_equal(lm.loc[[1, 2, 3], 'relimp_perc'], [15.43091, 81.44355, 3.12554], decimal=4) assert np.isclose(lm['relimp'].sum(), lm.at[0, 'r2']) ###################################################################### # WEIGHTED REGRESSION - compare against R lm() function # Note that the summary function of R sometimes round to 4 decimals, # sometimes to 5, etc.. lm = linear_regression(df[['X', 'M']], df['Y'], weights=df['W2']) assert_equal(lm['coef'].round(5).to_numpy(), [1.89530, 0.03905, 0.63912]) assert_equal(lm['se'].round(5).to_numpy(), [0.60498, 0.10984, 0.10096]) assert_equal(lm['T'].round(3).to_numpy(), [3.133, 0.356, 6.331]) # R round to 3 assert_equal(lm['pval'].round(5).to_numpy(), [0.00229, 0.72296, 0.00000]) assert_equal(lm['CI[2.5%]'].round(5).to_numpy(), [0.69459, -0.17896, 0.43874]) assert_equal(lm['CI[97.5%]'].round(5).to_numpy(), [3.09602, 0.25706, 0.83949]) assert round(lm['r2'].iloc[0], 4) == 0.3742 assert round(lm['adj_r2'].iloc[0], 4) == 0.3613 assert lm.df_model_ == 2 assert lm.df_resid_ == 97 # No intercept lm = linear_regression(df[['X', 'M']], df['Y'], add_intercept=False, weights=df['W2']) assert_equal(lm['coef'].round(5).to_numpy(), [0.26924, 0.71733]) assert_equal(lm['se'].round(5).to_numpy(), [0.08525, 0.10213]) assert_equal(lm['T'].round(3).to_numpy(), [3.158, 7.024]) assert_equal(lm['pval'].round(5).to_numpy(), [0.00211, 0.00000]) assert_equal(lm['CI[2.5%]'].round(5).to_numpy(), [0.10007, 0.51466]) assert_equal(lm['CI[97.5%]'].round(4).to_numpy(), [0.4384, 0.9200]) assert round(lm['r2'].iloc[0], 4) == 0.9090 assert round(lm['adj_r2'].iloc[0], 4) == 0.9072 assert lm.df_model_ == 2 assert lm.df_resid_ == 98 # With some weights set to zero # Here, R gives slightl different results than statsmodels because # zero weights are not taken into account when calculating the degrees # of freedom. Pingouin is similar to R. lm = linear_regression(df[['X']], df['Y'], weights=df['W1']) assert_equal(lm['coef'].round(4).to_numpy(), [3.5597, 0.2820]) assert_equal(lm['se'].round(4).to_numpy(), [0.7355, 0.1222]) assert_equal(lm['pval'].round(4).to_numpy(), [0.0000, 0.0232]) assert_equal(lm['CI[2.5%]'].round(5).to_numpy(), [2.09935, 0.03943]) assert_equal(lm['CI[97.5%]'].round(5).to_numpy(), [5.02015, 0.52453]) assert round(lm['r2'].iloc[0], 5) == 0.05364 a", https://github.com/rduldulao/tornado-swirl,b8df27efc3d7fd0dce0f0290767c94ac2e747311,tests/test_swirl.py::TestSampleEndpoints::test_security_scheme1,tests/test_swirl.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"#pylint: disable=all import tornado_swirl.swagger as swirl import tornado_swirl.settings as settings import tornado_swirl.openapi.security as security from tornado_swirl import api_routes from tornado.testing import AsyncHTTPTestCase, gen_test from tornado.web import RequestHandler import json class TestSampleEndpoints(AsyncHTTPTestCase): def setUp(self): super(TestSampleEndpoints, self).setUp() settings._API_HANDLERS = [] settings._SCHEMAS = {} settings._ROUTES = [] def get_app(self): return swirl.Application() def reset_settings(self): settings.default_settings = { 'static_path': settings.STATIC_PATH, 'swagger_prefix': '/swagger', 'api_version': 'v1.0', 'title': 'Sample API', 'description': 'Sample description', 'servers': [], 'api_key': '', 'enabled_methods': ['get', 'post', 'put', 'patch', 'delete'], 'exclude_namespaces': [], 'json_mime_type': 'application/json' } @gen_test def test_simple_1(self): self.reset_settings() @swirl.restapi('/test') class HandlerTest(RequestHandler): def get(self): """"""This is a simple test get. This is a simple description. Query Parameters: foo (string) -- Optional. Simple query string. Response: out (string) -- An output. """""" self.finish() self._app.add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test'] assert obj['paths']['/test']['get'] obj = obj['paths']['/test']['get'] assert obj['responses'] assert obj['responses']['200'] assert obj['responses']['200']['description'] == 'An output.' assert obj['responses']['200']['content']['text/plain']['schema'] assert obj['responses']['200']['content']['text/plain']['schema']['type'] == 'string' @gen_test def test_simple_2(self): self.reset_settings() @swirl.restapi(r'/test/(?P\w+)/(?P\d+)') class HandlerTest(RequestHandler): def post(self, a, b): """"""This is a simple test get. This is a simple description. Path Parameters: a (string) -- The a. b (integer) -- The b Response: out (string) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """""" self.finish() self.get_app().add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test/{a}/{b}'] assert obj['paths']['/test/{a}/{b}']['post'] obj = obj['paths']['/test/{a}/{b}']['post'] assert obj['responses']['400'] assert obj['responses']['404'] @gen_test def test_simple_3(self): self.reset_settings() @swirl.restapi(r'/test/(?P\w+)/(?P\d+)') class HandlerTest(RequestHandler): def post(self, a, b): """"""This is a simple test get. This is a simple description. Path Parameters: a (string) -- The a. b (integer) -- The b Response: out (Model) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """""" self.finish() @swirl.schema class Model(object): """"""This is a sample model. Foo Bar description. Properties: name (string): Foo name type (enum[foo, bar]) : Foo type """""" pass self.get_app().add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test/{a}/{b}'] assert obj['paths']['/test/{a}/{b}']['post'] assert obj['components'] assert obj['components']['schemas'] assert obj['components']['schemas']['Model'] obj = obj['paths']['/test/{a}/{b}']['post'] assert obj['responses']['400'] assert obj['responses']['404'] @gen_test def test_request_body_form_data(self): self.reset_settings() @swirl.restapi(r'/test/form') class HandlerTest(RequestHandler): def post(self, a, b): """"""This is a simple test post with form data. This is a simple description. Request Body: a (string) -- The a. b (integer) -- The b Response: out (string) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """""" self.finish() self.get_app().add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test/form'] assert obj['paths']['/test/form']['post'] assert obj['paths']['/test/form']['post']['requestBody'] assert obj['paths']['/test/form']['post']['requestBody']['content'] assert obj['paths']['/test/form']['post']['requestBody']['content']['application/x-www-form-urlencoded'] @gen_test def test_request_body_file_data(self): self.reset_settings() @swirl.restapi(r'/test/form') class HandlerTest(RequestHandler): def post(self, a, b): """"""This is a simple test post with form data. This is a simple description. Request Body: file (file:text/csv) -- The file. Returns: out (string) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """""" self.finish() self.get_app().add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) print(obj) assert obj['paths'] assert obj['paths']['/test/form'] assert obj['paths']['/test/form']['post'] assert obj['paths']['/test/form']['post']['requestBody'] assert obj['paths']['/test/form']['post']['requestBody']['content'] assert obj['paths']['/test/form']['post']['requestBody']['content']['text/csv'] @gen_test def test_request_body_model(self): self.reset_settings() @swirl.restapi(r'/test/form') class HandlerTest(RequestHandler): def post(self, a, b): """"""This is a simple test post with form data. This is a simple description. Request Body: user (Model) -- Model model. Response: out (string) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """""" self.finish() @swirl.schema class Model(object): """"""This is a sample model. Foo Bar description. Properties: name (string): Foo name type (enum[foo, bar]) : Foo type """""" pass self.get_app().add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test/form'] assert obj['paths']['/test/form']['post'] assert obj['paths']['/test/form']['post']['requestBody'] assert obj['paths']['/test/form']['post']['requestBody']['content'] assert obj['paths']['/test/form']['post']['requestBody']['content']['application/json'] @gen_test def test_simple_descriptions(self): self.reset_settings() @swirl.restapi('/test') class HandlerTest(RequestHandler): def get(self): """"""This is a simple test get. This is a simple description. Query Parameters: foo (string) -- Optional. Simple query string. example: bar Response: out (string) -- An output. example: foo """""" self.finish() self.get_app().add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test'] assert obj['paths']['/test']['get'] obj = obj['paths']['/test']['get'] assert obj['responses'] assert obj['responses']['200'] assert obj['responses']['200']['description'] == 'An output.' assert obj['responses']['200']['content']['text/plain']['schema'] assert obj['responses']['200']['content']['text/plain']['schema']['type'] == 'string' assert obj['responses']['200']['content']['text/plain']['schema']['example'] == 'foo' @gen_test def test_simp", https://github.com/rduldulao/tornado-swirl,b8df27efc3d7fd0dce0f0290767c94ac2e747311,tests/test_swirl.py::TestSampleEndpoints::test_security_scheme2,tests/test_swirl.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"#pylint: disable=all import tornado_swirl.swagger as swirl import tornado_swirl.settings as settings import tornado_swirl.openapi.security as security from tornado_swirl import api_routes from tornado.testing import AsyncHTTPTestCase, gen_test from tornado.web import RequestHandler import json class TestSampleEndpoints(AsyncHTTPTestCase): def setUp(self): super(TestSampleEndpoints, self).setUp() settings._API_HANDLERS = [] settings._SCHEMAS = {} settings._ROUTES = [] def get_app(self): return swirl.Application() def reset_settings(self): settings.default_settings = { 'static_path': settings.STATIC_PATH, 'swagger_prefix': '/swagger', 'api_version': 'v1.0', 'title': 'Sample API', 'description': 'Sample description', 'servers': [], 'api_key': '', 'enabled_methods': ['get', 'post', 'put', 'patch', 'delete'], 'exclude_namespaces': [], 'json_mime_type': 'application/json' } @gen_test def test_simple_1(self): self.reset_settings() @swirl.restapi('/test') class HandlerTest(RequestHandler): def get(self): """"""This is a simple test get. This is a simple description. Query Parameters: foo (string) -- Optional. Simple query string. Response: out (string) -- An output. """""" self.finish() self._app.add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test'] assert obj['paths']['/test']['get'] obj = obj['paths']['/test']['get'] assert obj['responses'] assert obj['responses']['200'] assert obj['responses']['200']['description'] == 'An output.' assert obj['responses']['200']['content']['text/plain']['schema'] assert obj['responses']['200']['content']['text/plain']['schema']['type'] == 'string' @gen_test def test_simple_2(self): self.reset_settings() @swirl.restapi(r'/test/(?P\w+)/(?P\d+)') class HandlerTest(RequestHandler): def post(self, a, b): """"""This is a simple test get. This is a simple description. Path Parameters: a (string) -- The a. b (integer) -- The b Response: out (string) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """""" self.finish() self.get_app().add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test/{a}/{b}'] assert obj['paths']['/test/{a}/{b}']['post'] obj = obj['paths']['/test/{a}/{b}']['post'] assert obj['responses']['400'] assert obj['responses']['404'] @gen_test def test_simple_3(self): self.reset_settings() @swirl.restapi(r'/test/(?P\w+)/(?P\d+)') class HandlerTest(RequestHandler): def post(self, a, b): """"""This is a simple test get. This is a simple description. Path Parameters: a (string) -- The a. b (integer) -- The b Response: out (Model) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """""" self.finish() @swirl.schema class Model(object): """"""This is a sample model. Foo Bar description. Properties: name (string): Foo name type (enum[foo, bar]) : Foo type """""" pass self.get_app().add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test/{a}/{b}'] assert obj['paths']['/test/{a}/{b}']['post'] assert obj['components'] assert obj['components']['schemas'] assert obj['components']['schemas']['Model'] obj = obj['paths']['/test/{a}/{b}']['post'] assert obj['responses']['400'] assert obj['responses']['404'] @gen_test def test_request_body_form_data(self): self.reset_settings() @swirl.restapi(r'/test/form') class HandlerTest(RequestHandler): def post(self, a, b): """"""This is a simple test post with form data. This is a simple description. Request Body: a (string) -- The a. b (integer) -- The b Response: out (string) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """""" self.finish() self.get_app().add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test/form'] assert obj['paths']['/test/form']['post'] assert obj['paths']['/test/form']['post']['requestBody'] assert obj['paths']['/test/form']['post']['requestBody']['content'] assert obj['paths']['/test/form']['post']['requestBody']['content']['application/x-www-form-urlencoded'] @gen_test def test_request_body_file_data(self): self.reset_settings() @swirl.restapi(r'/test/form') class HandlerTest(RequestHandler): def post(self, a, b): """"""This is a simple test post with form data. This is a simple description. Request Body: file (file:text/csv) -- The file. Returns: out (string) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """""" self.finish() self.get_app().add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) print(obj) assert obj['paths'] assert obj['paths']['/test/form'] assert obj['paths']['/test/form']['post'] assert obj['paths']['/test/form']['post']['requestBody'] assert obj['paths']['/test/form']['post']['requestBody']['content'] assert obj['paths']['/test/form']['post']['requestBody']['content']['text/csv'] @gen_test def test_request_body_model(self): self.reset_settings() @swirl.restapi(r'/test/form') class HandlerTest(RequestHandler): def post(self, a, b): """"""This is a simple test post with form data. This is a simple description. Request Body: user (Model) -- Model model. Response: out (string) -- An output. Errors: 400 -- Bad Request 404 -- Not Found """""" self.finish() @swirl.schema class Model(object): """"""This is a sample model. Foo Bar description. Properties: name (string): Foo name type (enum[foo, bar]) : Foo type """""" pass self.get_app().add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test/form'] assert obj['paths']['/test/form']['post'] assert obj['paths']['/test/form']['post']['requestBody'] assert obj['paths']['/test/form']['post']['requestBody']['content'] assert obj['paths']['/test/form']['post']['requestBody']['content']['application/json'] @gen_test def test_simple_descriptions(self): self.reset_settings() @swirl.restapi('/test') class HandlerTest(RequestHandler): def get(self): """"""This is a simple test get. This is a simple description. Query Parameters: foo (string) -- Optional. Simple query string. example: bar Response: out (string) -- An output. example: foo """""" self.finish() self.get_app().add_handlers(r"".*"", api_routes()) response = yield self.http_client.fetch(self.get_url('/swagger/spec')) obj = json.loads(response.body.decode('utf-8')) assert obj['paths'] assert obj['paths']['/test'] assert obj['paths']['/test']['get'] obj = obj['paths']['/test']['get'] assert obj['responses'] assert obj['responses']['200'] assert obj['responses']['200']['description'] == 'An output.' assert obj['responses']['200']['content']['text/plain']['schema'] assert obj['responses']['200']['content']['text/plain']['schema']['type'] == 'string' assert obj['responses']['200']['content']['text/plain']['schema']['example'] == 'foo' @gen_test def test_simp", https://github.com/richecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_add_vertex,__tests__/test_simple_graph.py,OD,flaky,Accepted,https://github.com/richecr/PyGraph/pull/96,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/richecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_delete_vertex,__tests__/test_simple_graph.py,OD,flaky,Accepted,https://github.com/richecr/PyGraph/pull/96,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/richecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_duplicated_edge,__tests__/test_simple_graph.py,OD,flaky,Accepted,https://github.com/richecr/PyGraph/pull/96,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/richecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_false_cycle_graph,__tests__/test_simple_graph.py,OD,flaky,Accepted,https://github.com/richecr/PyGraph/pull/96,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/richecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_false_regular_graph,__tests__/test_simple_graph.py,OD,flaky,Accepted,https://github.com/richecr/PyGraph/pull/96,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/richecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_get_all_vertex,__tests__/test_simple_graph.py,OD,flaky,Accepted,https://github.com/richecr/PyGraph/pull/96,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/richecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_is_terminal,__tests__/test_simple_graph.py,OD,flaky,Accepted,https://github.com/richecr/PyGraph/pull/96,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/richecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_num_edges,__tests__/test_simple_graph.py,OD,flaky,Accepted,https://github.com/richecr/PyGraph/pull/96,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/richecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_num_vertex,__tests__/test_simple_graph.py,OD,flaky,Accepted,https://github.com/richecr/PyGraph/pull/96,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/richecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_true_regular_graph,__tests__/test_simple_graph.py,OD,flaky,Accepted,https://github.com/richecr/PyGraph/pull/96,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/richecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_vertex_neighbors,__tests__/test_simple_graph.py,OD,flaky,Accepted,https://github.com/richecr/PyGraph/pull/96,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_add_vertex,__tests__/test_simple_graph.py,NIO,flaky,Opened,https://github.com/richecr/PyGraph/pull/94,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_delete_vertex,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_duplicated_edge,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_edge_exists,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_false_cycle_graph,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_false_regular_graph,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_is_terminal,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_list_graph_edges,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_list_graph_vertices,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_num_edges,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_num_vertex,__tests__/test_simple_graph.py,NIO,flaky,Opened,https://github.com/richecr/PyGraph/pull/94,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_show_edge,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_true_cycle_graph,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_vertex_degree,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_vertex_exists,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_vertex_neighbors,__tests__/test_simple_graph.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/Rickecr/PyGraph,e81c3a0f543f5bfcda1a603c6dcecde13d582c57,__tests__/test_simple_graph.py::TestSimpleGraph::test_vertices_adjacency,__tests__/test_simple_graph.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..py_graph_t.SimpleGraph import SimpleGraph from ..py_graph_t.exceptions.SimpleGraphException import ( EdgeDuplicatedException, LoopDetectedException ) class TestSimpleGraph: graph = SimpleGraph() def test_num_vertex(self): self.graph.add_vertex(""a"") assert self.graph.num_vertex() == 1 def test_add_vertex(self): value = ""b"" vertex = self.graph.add_vertex(value) assert str(vertex) == 'Vértice b' def test_delete_vertex(self): vertex_delete = self.graph.delete_vertex(""a"") assert self.graph.num_vertex() == 1 assert str(vertex_delete) == ""Vértice a"" def test_is_terminal(self): self.graph.add_vertex(""a"") edge = self.graph.add_edge(""a"", ""b"", ""ab"") assert self.graph.is_terminal(edge, 'a') and \ self.graph.is_terminal(edge, 'b') def test_vertex_exists(self): assert self.graph.vertex_exists(""a"") def test_edge_exists(self): assert self.graph.edge_exists(""a"", ""b"") def test_num_edges(self): assert self.graph.num_edges() == 1 def test_vertex_neighbors(self): neighbors_true = [] for x in range(10): self.graph.add_vertex(str(x)) for x in range(1, 5): neighbors_true.append(self.graph.vertices[str(x)]) self.graph.add_edge(""0"", str(x), ""x"") assert self.graph.vertex_neighbors(""0"") == neighbors_true def test_vertex_degree(self): assert len(self.graph.vertex_neighbors(""b"")) == 1 def test_vertices_adjacency(self): neighbors_vertices = self.graph.vertex_neighbors(""a"") vertex_b = self.graph.vertices.get(""b"") assert vertex_b in neighbors_vertices def test_get_all_vertex(self): assert str(self.graph.get_all_vertex()) == ""{'b': Vértice b, "" + \ ""'a': Vértice a, '0': Vértice 0, '1': Vértice 1, "" + \ ""'2': Vértice 2, '3': Vértice 3, '4': Vértice 4, "" + \ ""'5': Vértice 5, '6': Vértice 6, '7': Vértice 7, "" + \ ""'8': Vértice 8, '9': Vértice 9}"" def test_list_graph_vertices(self): vertices = [] for vertex in self.graph.vertices: vertices.append(vertex) assert str(self.graph.list_graph_vertices()) == \ ""['b', 'a', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']"" def test_list_graph_edges(self): edges = [] for edge in self.graph.edges: edges.append(edge.name) assert str(self.graph.list_graph_edges()) == \ ""['ab', 'x', 'x', 'x', 'x']"" def test_show_edge(self): assert str(self.graph.show_edge('a', 'b')) == \ str('ab: Vértice a -> Vértice b') def test_true_cycle_graph(self): self.graph.add_vertex(""c"") self.graph.add_edge(""b"", ""c"", 2) self.graph.add_edge(""c"", ""a"", 3) assert self.graph.has_cycle() is True assert self.graph.has_loop() is False def test_false_cycle_graph(self): self.graph.delete_edge(""c"", ""a"") self.graph.add_vertex(""d"") self.graph.add_edge(""c"", ""d"", 3) assert self.graph.has_cycle() is False assert self.graph.has_loop() is False def test_loop(self): with pytest.raises(LoopDetectedException): assert self.graph.add_edge(""d"", ""d"") assert self.graph.has_loop() is False def test_true_regular_graph(self): self.graph.delete_vertex(""d"") for i in range(0, 10): self.graph.delete_vertex(str(i)) self.graph.add_vertex(""d"") self.graph.delete_edge(""b"", ""c"") self.graph.add_edge(""c"", ""d"", 3) print(self.graph.__str__()) assert self.graph.check_regular_graph() is True def test_false_regular_graph(self): self.graph.add_edge(""b"", ""c"", 3) assert self.graph.check_regular_graph() is False def test_duplicated_edge(self): with pytest.raises(EdgeDuplicatedException): assert self.graph.add_edge(""b"", ""c"") ", https://github.com/rochefort-lab/fissa,2077a8c8a029948dcf1d4c352f785700f42aff9f,fissa/tests/test_core.py::TestExperimentA::test_imagelistloaded_roizip,fissa/tests/test_core.py,OD-Vic,flaky,nan,nan,classify;root_cause,"'''Unit tests for core.py.''' from __future__ import division from datetime import datetime import random import os, os.path import shutil import unittest import numpy as np from .base_test import BaseTestCase from .. import core from .. import datahandler class TestExperimentA(BaseTestCase): '''Test Experiment class and its methods.''' def __init__(self, *args, **kw): super(TestExperimentA, self).__init__(*args, **kw) self.resources_dir = os.path.join(self.test_directory, 'resources', 'a') self.output_dir = os.path.join( self.resources_dir, 'out-{}-{:06d}'.format( datetime.now().strftime('%H%M%S%f'), random.randrange(999999) ) ) self.images_dir = 'images' self.image_names = ['AVG_A01_R1_small.tif'] self.roi_zip_path = 'rois.zip' self.roi_paths = [os.path.join('rois', r) for r in ['01.roi']] self.expected_00 = np.array([ [11.25423074, 0. , 0. , 7.55432252, 19.11182766, 0. , 6.37473238, 0. , 0. , 0. , 0. , 1.58567319, 2.28185467, 0. , 16.70204514, 17.55112746, 17.23642459, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 14.75392227], [89.75326173, 81.33290066, 88.77502093, 80.71108594, 85.5315738 , 78.42423771, 80.3659251 , 84.46124736, 78.04229961, 81.48360449, 82.12879963, 83.11862592, 83.09085808, 91.22418523, 86.42399606, 81.05860567, 86.15497276, 81.53903092, 80.53875696, 83.41061814, 80.59332446, 81.64495893, 86.26057223, 82.47622273, 83.28735277, 84.00697623, 83.68517083, 83.19829805, 82.06518458], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], ]) def setUp(self): if os.path.isdir(self.output_dir): self.tearDown() os.makedirs(self.output_dir) def tearDown(self): if os.path.isdir(self.output_dir): shutil.rmtree(self.output_dir) def test_imagedir_roizip(self): image_path = os.path.join(self.resources_dir, self.images_dir) roi_path = os.path.join(self.resources_dir, self.roi_zip_path) exp = core.Experiment(image_path, roi_path, self.output_dir) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) self.assert_equal(len(actual[0]), 1) self.assert_allclose(actual[0][0], self.expected_00) def test_imagelist_roizip(self): image_paths = [ os.path.join(self.resources_dir, self.images_dir, img) for img in self.image_names ] roi_path = os.path.join(self.resources_dir, self.roi_zip_path) exp = core.Experiment(image_paths, roi_path, self.output_dir) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) self.assert_equal(len(actual[0]), 1) self.assert_allclose(actual[0][0], self.expected_00) def test_imagelistloaded_roizip(self): image_paths = [ os.path.join(self.resources_dir, self.images_dir, img) for img in self.image_names ] images = [datahandler.image2array(pth) for pth in image_paths] roi_path = os.path.join(self.resources_dir, self.roi_zip_path) exp = core.Experiment(images, roi_path, self.output_dir) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) self.assert_equal(len(actual[0]), 1) self.assert_allclose(actual[0][0], self.expected_00) @unittest.expectedFailure def test_imagedir_roilistpath(self): image_path = os.path.join(self.resources_dir, self.images_dir) roi_paths = [ os.path.join(self.resources_dir, r) for r in self.roi_paths ] print(roi_paths) exp = core.Experiment(image_path, roi_paths, self.output_dir) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) self.assert_equal(len(actual[0]), 1) self.assert_allclose(actual[0][0], self.expected_00) @unittest.expectedFailure def test_imagelist_roilistpath(self): image_paths = [ os.path.join(self.resources_dir, self.images_dir, img) for img in self.image_names ] roi_paths = [ os.path.join(self.resources_dir, r) for r in self.roi_paths ] exp = core.Experiment(image_paths, roi_paths, self.output_dir) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) self.assert_equal(len(actual[0]), 1) self.assert_allclose(actual[0][0], self.expected_00) def test_nocache(self): image_path = os.path.join(self.resources_dir, self.images_dir) roi_path = os.path.join(self.resources_dir, self.roi_zip_path) exp = core.Experiment(image_path, roi_path) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) self.assert_equal(len(actual[0]), 1) self.assert_allclose(actual[0][0], self.expected_00) def test_ncores_preparation_1(self): image_path = os.path.join(self.resources_dir, self.images_dir) roi_path = os.path.join(self.resources_dir, self.roi_zip_path) exp = core.Experiment(image_path, roi_path, self.output_dir, ncores_preparation=1) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) self.assert_equal(len(actual[0]), 1) self.assert_allclose(actual[0][0], self.expected_00) def test_ncores_preparation_2(self): image_path = os.path.join(self.resources_dir, self.images_dir) roi_path = os.path.join(self.resources_dir, self.roi_zip_path) exp = core.Experiment(image_path, roi_path, self.output_dir, ncores_preparation=2) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) self.assert_equal(len(actual[0]), 1) self.assert_allclose(actual[0][0], self.expected_00) def test_ncores_separate_1(self): image_path = os.path.join(self.resources_dir, self.images_dir) roi_path = os.path.join(self.resources_dir, self.roi_zip_path) exp = core.Experiment(image_path, roi_path, self.output_dir, ncores_separation=1) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) self.assert_equal(len(actual[0]), 1) self.assert_allclose(actual[0][0], self.expected_00) def test_ncores_separate_2(self): image_path = os.path.join(self.resources_dir, self.images_dir) roi_path = os.path.join(self.resources_dir, self.roi_zip_path) exp = core.Experiment(image_path, roi_path, self.output_dir, ncores_separation=2) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) self.assert_equal(len(actual[0]), 1) self.assert_allclose(actual[0][0], self.expected_00) def test_lowmemorymode(self): image_path = os.path.join(self.resources_dir, self.images_dir) roi_path = os.path.join(self.resources_dir, self.roi_zip_path) exp = core.Experiment(image_path, roi_path, self.output_dir, lowmemory_mode=True) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) self.assert_equal(len(actual[0]), 1) self.assert_allclose(actual[0][0], self.expected_00) def test_manualhandler(self): image_path = os.path.join(self.resources_dir, self.images_dir) roi_path = os.path.join(self.resources_dir, self.roi_zip_path) exp = core.Experiment(image_path, roi_path, self.output_dir, datahandler_custom=datahandler) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) self.assert_equal(len(actual[0]), 1) self.assert_allclose(actual[0][0], self.expected_00) def test_nofolder(self): self.tearDown() image_path = os.path.join(self.resources_dir, self.images_dir) roi_path = os.path.join(self.resources_dir, self.roi_zip_path) exp = core.Experiment(image_path, roi_path, self.output_dir) exp.separate() actual = exp.result self.assert_equal(len(actual), 1) ", https://github.com/rorymurdock/basic_auth,622f2133c13988cec549f4ea55bed14e50f90876,tests/test_basic.py::test_read_config,tests/test_basic.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"""""""Automated testing for Basic"""""" import os import string import random import pytest from basic_auth import Auth AUTH = Auth() # Create some random values def random_string(string_length=15): """"""Generate a random string of fixed length """""" letters = string.ascii_lowercase return ''.join(random.choice(letters) for i in range(string_length)) KEY = random_string() VALUE = random_string() def test_encode(): """"""Test encoding credentials"""""" assert AUTH.encode(""username"", ""password"") == 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=' assert AUTH.encode( ""YuEA33Ss*o55uk0!vKc7"", ""93H20VlU&vyuSr$aS&kJ"" ) == 'Basic WXVFQTMzU3MqbzU1dWswIXZLYzc6OTNIMjBWbFUmdnl1U3IkYVMma0o=' def test_decode(): """"""Test decoding credentials"""""" assert AUTH.decode('Basic dXNlcm5hbWU6cGFzc3dvcmQ=') == ""username:password"" assert AUTH.decode( 'Basic WXVFQTMzU3MqbzU1dWswIXZLYzc6OTNIMjBWbFUmdnl1U3IkYVMma0o=' ) == ""YuEA33Ss*o55uk0!vKc7:93H20VlU&vyuSr$aS&kJ"" def test_check_config_dir(): """"""Test directory related operations"""""" # Check no folder exists already assert AUTH.check_config_dir() is False # Create directory assert AUTH.create_config_directory() is True # Verify directory exists assert AUTH.check_config_dir() is True # Create read only directory Auth(config_dir=""config/readonly"").create_config_directory() os.chmod(""config/readonly"", 444) # Test writing to read only assert Auth( config_dir=""config/readonly/test"").create_config_directory() is False def test_check_file_exists(): """"""Test file related operations"""""" # Check file doesn't exist assert AUTH.check_file_exists('nofile') is False # Test a directory assert AUTH.check_file_exists(""readonly"") is False # Create file and verify AUTH.write_config("""", ""test.json"") assert AUTH.check_file_exists('test.json') is True def test_write_config(): """"""Test writing config to files"""""" # Create some dummy data data = {} data[KEY] = VALUE # Overwrite file from previous test with random data assert AUTH.write_config(data, ""test.json"") is True # Try to write in a readonly folder assert Auth(""config/readonly"").write_config(data, ""test.json"") is False # Try to create a new folder assert Auth(""config/newfolder"").write_config(data, ""test.json"") is True # Create new folder in read only will sys.exit with pytest.raises(SystemExit) as pytest_wrapped_e: Auth(""config/readonly/newfolder"").write_config(data, ""test.json"") assert pytest_wrapped_e.type == SystemExit def test_read_config(): """"""Test opening files"""""" # Try to open a non existant config assert AUTH.read_config('nofile.json') is False # Try a directory assert AUTH.read_config('readonly') is False # Try a good file assert isinstance(AUTH.read_config(""test.json""), dict) is True # Try a bad file with open(""config/bad_file.json"", 'w') as outfile: outfile.write(""Thisisbaddata"") assert AUTH.read_config(""bad_file.json"") is False def test_verify_config(): """"""Verify written config"""""" # Verify config written earlier assert AUTH.verify_config(""test.json"", KEY, VALUE) is True # Verify config mismatch assert AUTH.verify_config(""test.json"", KEY, ""Not value"") is False # Verify non json data assert AUTH.verify_config(""bad_file.json"", KEY, VALUE) is False ", https://github.com/rorymurdock/basic_auth,622f2133c13988cec549f4ea55bed14e50f90876,tests/test_basic.py::test_verify_config,tests/test_basic.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"""""""Automated testing for Basic"""""" import os import string import random import pytest from basic_auth import Auth AUTH = Auth() # Create some random values def random_string(string_length=15): """"""Generate a random string of fixed length """""" letters = string.ascii_lowercase return ''.join(random.choice(letters) for i in range(string_length)) KEY = random_string() VALUE = random_string() def test_encode(): """"""Test encoding credentials"""""" assert AUTH.encode(""username"", ""password"") == 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=' assert AUTH.encode( ""YuEA33Ss*o55uk0!vKc7"", ""93H20VlU&vyuSr$aS&kJ"" ) == 'Basic WXVFQTMzU3MqbzU1dWswIXZLYzc6OTNIMjBWbFUmdnl1U3IkYVMma0o=' def test_decode(): """"""Test decoding credentials"""""" assert AUTH.decode('Basic dXNlcm5hbWU6cGFzc3dvcmQ=') == ""username:password"" assert AUTH.decode( 'Basic WXVFQTMzU3MqbzU1dWswIXZLYzc6OTNIMjBWbFUmdnl1U3IkYVMma0o=' ) == ""YuEA33Ss*o55uk0!vKc7:93H20VlU&vyuSr$aS&kJ"" def test_check_config_dir(): """"""Test directory related operations"""""" # Check no folder exists already assert AUTH.check_config_dir() is False # Create directory assert AUTH.create_config_directory() is True # Verify directory exists assert AUTH.check_config_dir() is True # Create read only directory Auth(config_dir=""config/readonly"").create_config_directory() os.chmod(""config/readonly"", 444) # Test writing to read only assert Auth( config_dir=""config/readonly/test"").create_config_directory() is False def test_check_file_exists(): """"""Test file related operations"""""" # Check file doesn't exist assert AUTH.check_file_exists('nofile') is False # Test a directory assert AUTH.check_file_exists(""readonly"") is False # Create file and verify AUTH.write_config("""", ""test.json"") assert AUTH.check_file_exists('test.json') is True def test_write_config(): """"""Test writing config to files"""""" # Create some dummy data data = {} data[KEY] = VALUE # Overwrite file from previous test with random data assert AUTH.write_config(data, ""test.json"") is True # Try to write in a readonly folder assert Auth(""config/readonly"").write_config(data, ""test.json"") is False # Try to create a new folder assert Auth(""config/newfolder"").write_config(data, ""test.json"") is True # Create new folder in read only will sys.exit with pytest.raises(SystemExit) as pytest_wrapped_e: Auth(""config/readonly/newfolder"").write_config(data, ""test.json"") assert pytest_wrapped_e.type == SystemExit def test_read_config(): """"""Test opening files"""""" # Try to open a non existant config assert AUTH.read_config('nofile.json') is False # Try a directory assert AUTH.read_config('readonly') is False # Try a good file assert isinstance(AUTH.read_config(""test.json""), dict) is True # Try a bad file with open(""config/bad_file.json"", 'w') as outfile: outfile.write(""Thisisbaddata"") assert AUTH.read_config(""bad_file.json"") is False def test_verify_config(): """"""Verify written config"""""" # Verify config written earlier assert AUTH.verify_config(""test.json"", KEY, VALUE) is True # Verify config mismatch assert AUTH.verify_config(""test.json"", KEY, ""Not value"") is False # Verify non json data assert AUTH.verify_config(""bad_file.json"", KEY, VALUE) is False ", https://github.com/rorymurdock/basic_auth,622f2133c13988cec549f4ea55bed14e50f90876,tests/test_general.py::test_file_data_arguments,tests/test_general.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"""""""Automated testing for General BAC"""""" import os import string import random import argparse from io import StringIO from basic_auth import Auth from Examples.general import Config # Init package AUTH = Auth() CONFIG = Config() # Random int / str generators def random_string(string_length=15): """"""Generate a random string of fixed length """""" letters = string.ascii_lowercase return ''.join(random.choice(letters) for i in range(string_length)) # Create some random values to use for testing RANDOM_URL = ""%s.com"" % random_string(8) RANDOM_USERNAME = random_string(8) RANDOM_PASSWORD = random_string() # Build the data structure expected EXPECTED_RESULT = {} EXPECTED_RESULT['authorization'] = AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD) EXPECTED_RESULT['url'] = RANDOM_URL # Print data used print(""Using generated data:"") print(""\tURL: %s"" % RANDOM_URL) print(""\tUsername: %s"" % RANDOM_USERNAME) print(""\tPassword: %s"" % RANDOM_PASSWORD) # Test STD in def test_interactive_data_encode(monkeypatch): """"""Test entering details via std in"""""" # Stage the std in data url = StringIO('%s\n%s\n%s' % (RANDOM_URL, RANDOM_USERNAME, RANDOM_PASSWORD)) # Send std in and run the main function monkeypatch.setattr('sys.stdin', url) data = CONFIG.interactive() # Compare the data structure generated against the static build assert data == EXPECTED_RESULT def test_arguments_data_encode(): """"""Test mocking the arguments and writing the config"""""" args = argparse.Namespace(url=RANDOM_URL, username=RANDOM_USERNAME, password=RANDOM_PASSWORD) result = CONFIG.arguments(args) assert result == EXPECTED_RESULT def test_main_arguments(): """"""Test mocking the arguments and running them through main()"""""" args = argparse.Namespace(url=RANDOM_URL, username=RANDOM_USERNAME, password=RANDOM_PASSWORD) result = Config(""general_args.json"").main(args) assert result is True def test_main_interactive(monkeypatch): """"""Test mocking the std in and writing the config"""""" url = StringIO('%s\n%s\n%s' % (RANDOM_URL, RANDOM_USERNAME, RANDOM_PASSWORD)) # Send std in and run the main function monkeypatch.setattr('sys.stdin', url) # Stage the empty args args = argparse.Namespace(url=None, username=None, password=None) result = Config(""general_interactive.json"").main(args) assert result is True def test_file_data_arguments(): """"""Verify the data written via mock arguments"""""" filename = 'general_args.json' assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD)) is True assert AUTH.verify_config(filename, 'url', RANDOM_URL) is True def test_file_data_interactive(): """"""Verify the data written via mock std in"""""" filename = 'general_interactive.json' assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD)) is True assert AUTH.verify_config(filename, 'url', RANDOM_URL) is True def test_main_results(): """"""Test the results of the real arguments"""""" # Due to complexities testing with arguments to get full coverage # run the script externally with full arguments os.popen('python3 -m pip install -e .').read() os.popen( 'python3 Examples/general.py -url example.com -username citests -password hunter2' ).read() filename = ""general.json"" assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(""citests"", ""hunter2"")) is True assert AUTH.verify_config(filename, 'url', ""example.com"") is True ", https://github.com/rorymurdock/basic_auth,622f2133c13988cec549f4ea55bed14e50f90876,tests/test_general.py::test_file_data_interactive,tests/test_general.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"""""""Automated testing for General BAC"""""" import os import string import random import argparse from io import StringIO from basic_auth import Auth from Examples.general import Config # Init package AUTH = Auth() CONFIG = Config() # Random int / str generators def random_string(string_length=15): """"""Generate a random string of fixed length """""" letters = string.ascii_lowercase return ''.join(random.choice(letters) for i in range(string_length)) # Create some random values to use for testing RANDOM_URL = ""%s.com"" % random_string(8) RANDOM_USERNAME = random_string(8) RANDOM_PASSWORD = random_string() # Build the data structure expected EXPECTED_RESULT = {} EXPECTED_RESULT['authorization'] = AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD) EXPECTED_RESULT['url'] = RANDOM_URL # Print data used print(""Using generated data:"") print(""\tURL: %s"" % RANDOM_URL) print(""\tUsername: %s"" % RANDOM_USERNAME) print(""\tPassword: %s"" % RANDOM_PASSWORD) # Test STD in def test_interactive_data_encode(monkeypatch): """"""Test entering details via std in"""""" # Stage the std in data url = StringIO('%s\n%s\n%s' % (RANDOM_URL, RANDOM_USERNAME, RANDOM_PASSWORD)) # Send std in and run the main function monkeypatch.setattr('sys.stdin', url) data = CONFIG.interactive() # Compare the data structure generated against the static build assert data == EXPECTED_RESULT def test_arguments_data_encode(): """"""Test mocking the arguments and writing the config"""""" args = argparse.Namespace(url=RANDOM_URL, username=RANDOM_USERNAME, password=RANDOM_PASSWORD) result = CONFIG.arguments(args) assert result == EXPECTED_RESULT def test_main_arguments(): """"""Test mocking the arguments and running them through main()"""""" args = argparse.Namespace(url=RANDOM_URL, username=RANDOM_USERNAME, password=RANDOM_PASSWORD) result = Config(""general_args.json"").main(args) assert result is True def test_main_interactive(monkeypatch): """"""Test mocking the std in and writing the config"""""" url = StringIO('%s\n%s\n%s' % (RANDOM_URL, RANDOM_USERNAME, RANDOM_PASSWORD)) # Send std in and run the main function monkeypatch.setattr('sys.stdin', url) # Stage the empty args args = argparse.Namespace(url=None, username=None, password=None) result = Config(""general_interactive.json"").main(args) assert result is True def test_file_data_arguments(): """"""Verify the data written via mock arguments"""""" filename = 'general_args.json' assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD)) is True assert AUTH.verify_config(filename, 'url', RANDOM_URL) is True def test_file_data_interactive(): """"""Verify the data written via mock std in"""""" filename = 'general_interactive.json' assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD)) is True assert AUTH.verify_config(filename, 'url', RANDOM_URL) is True def test_main_results(): """"""Test the results of the real arguments"""""" # Due to complexities testing with arguments to get full coverage # run the script externally with full arguments os.popen('python3 -m pip install -e .').read() os.popen( 'python3 Examples/general.py -url example.com -username citests -password hunter2' ).read() filename = ""general.json"" assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(""citests"", ""hunter2"")) is True assert AUTH.verify_config(filename, 'url', ""example.com"") is True ", https://github.com/rorymurdock/basic_auth,622f2133c13988cec549f4ea55bed14e50f90876,tests/test_wso.py::test_file_data_arguments,tests/test_wso.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"""""""Automated testing for WSO BAC"""""" import os import string import random import argparse from io import StringIO from basic_auth import Auth from Examples.WSO import Config # Init package AUTH = Auth() WSO_CONFIG = Config() # Random int / str generators def random_string(string_length=15): """"""Generate a random string of fixed length """""" letters = string.ascii_lowercase return ''.join(random.choice(letters) for i in range(string_length)) def random_number(): """"""Generate a random string of fixed length """""" return random.randint(0, 9999) # Create some random values to use for testing RANDOM_URL = ""cn%i.awmdmtest.com"" % random_number() RANDOM_USERNAME = random_string(8) RANDOM_PASSWORD = random_string() RANDOM_TENANTCODE = random_string() # Build the data structure expected EXPECTED_RESULT = {} EXPECTED_RESULT['authorization'] = AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD) EXPECTED_RESULT['url'] = RANDOM_URL EXPECTED_RESULT['aw-tenant-code'] = RANDOM_TENANTCODE # Print data used print(""Using generated data:"") print(""\tURL: %s"" % RANDOM_URL) print(""\tUsername: %s"" % RANDOM_USERNAME) print(""\tPassword: %s"" % RANDOM_PASSWORD) print(""\tAW Tenant Code: %s"" % RANDOM_TENANTCODE) # Test STD in def test_interactive_data_encode(monkeypatch): """"""Test entering details via std in"""""" # Stage the std in data url = StringIO( '%s\n%s\n%s\n%s' % (RANDOM_URL, RANDOM_USERNAME, RANDOM_PASSWORD, RANDOM_TENANTCODE)) # Send std in and run the main function monkeypatch.setattr('sys.stdin', url) data = WSO_CONFIG.interactive() # Compare the data structure generated against the static build assert data == EXPECTED_RESULT def test_arguments_data_encode(): """"""Test mocking the arguments and writing the config"""""" args = argparse.Namespace(url=RANDOM_URL, username=RANDOM_USERNAME, password=RANDOM_PASSWORD, tenantcode=RANDOM_TENANTCODE) result = WSO_CONFIG.arguments(args) assert result == EXPECTED_RESULT def test_main_arguments(): """"""Test mocking the arguments and running them through main()"""""" args = argparse.Namespace(url=RANDOM_URL, username=RANDOM_USERNAME, password=RANDOM_PASSWORD, tenantcode=RANDOM_TENANTCODE) result = Config(""wso_args.json"").main(args) assert result is True def test_main_interactive(monkeypatch): """"""Test mocking the std in and writing the config"""""" url = StringIO( '%s\n%s\n%s\n%s' % (RANDOM_URL, RANDOM_USERNAME, RANDOM_PASSWORD, RANDOM_TENANTCODE)) # Send std in and run the main function monkeypatch.setattr('sys.stdin', url) # Stage the empty args args = argparse.Namespace(url=None, username=None, password=None, tenantcode=None) result = Config(""wso_interactive.json"").main(args) assert result is True def test_file_data_arguments(): """"""Verify the data written via mock arguments"""""" filename = 'wso_args.json' assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD)) is True assert AUTH.verify_config(filename, 'url', RANDOM_URL) is True assert AUTH.verify_config(filename, 'aw-tenant-code', RANDOM_TENANTCODE) is True def test_file_data_interactive(): """"""Verify the data written via mock std in"""""" filename = 'wso_interactive.json' assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD)) is True assert AUTH.verify_config(filename, 'url', RANDOM_URL) is True assert AUTH.verify_config(filename, 'aw-tenant-code', RANDOM_TENANTCODE) is True def test_main_results(): """"""Test the results of the real arguments"""""" # Due to complexities testing with arguments to get full coverage # run the script externally with full arguments os.popen('python3 -m pip install -e .') os.popen( 'python3 Examples/WSO.py -url cn1234.awtest.com -username citests -password hunter2 -tenantcode shibboleet' ).read() filename = ""uem.json"" assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(""citests"", ""hunter2"")) is True assert AUTH.verify_config(filename, 'url', ""cn1234.awtest.com"") is True assert AUTH.verify_config(filename, 'aw-tenant-code', ""shibboleet"") is True ", https://github.com/rorymurdock/basic_auth,622f2133c13988cec549f4ea55bed14e50f90876,tests/test_wso.py::test_file_data_interactive,tests/test_wso.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"""""""Automated testing for WSO BAC"""""" import os import string import random import argparse from io import StringIO from basic_auth import Auth from Examples.WSO import Config # Init package AUTH = Auth() WSO_CONFIG = Config() # Random int / str generators def random_string(string_length=15): """"""Generate a random string of fixed length """""" letters = string.ascii_lowercase return ''.join(random.choice(letters) for i in range(string_length)) def random_number(): """"""Generate a random string of fixed length """""" return random.randint(0, 9999) # Create some random values to use for testing RANDOM_URL = ""cn%i.awmdmtest.com"" % random_number() RANDOM_USERNAME = random_string(8) RANDOM_PASSWORD = random_string() RANDOM_TENANTCODE = random_string() # Build the data structure expected EXPECTED_RESULT = {} EXPECTED_RESULT['authorization'] = AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD) EXPECTED_RESULT['url'] = RANDOM_URL EXPECTED_RESULT['aw-tenant-code'] = RANDOM_TENANTCODE # Print data used print(""Using generated data:"") print(""\tURL: %s"" % RANDOM_URL) print(""\tUsername: %s"" % RANDOM_USERNAME) print(""\tPassword: %s"" % RANDOM_PASSWORD) print(""\tAW Tenant Code: %s"" % RANDOM_TENANTCODE) # Test STD in def test_interactive_data_encode(monkeypatch): """"""Test entering details via std in"""""" # Stage the std in data url = StringIO( '%s\n%s\n%s\n%s' % (RANDOM_URL, RANDOM_USERNAME, RANDOM_PASSWORD, RANDOM_TENANTCODE)) # Send std in and run the main function monkeypatch.setattr('sys.stdin', url) data = WSO_CONFIG.interactive() # Compare the data structure generated against the static build assert data == EXPECTED_RESULT def test_arguments_data_encode(): """"""Test mocking the arguments and writing the config"""""" args = argparse.Namespace(url=RANDOM_URL, username=RANDOM_USERNAME, password=RANDOM_PASSWORD, tenantcode=RANDOM_TENANTCODE) result = WSO_CONFIG.arguments(args) assert result == EXPECTED_RESULT def test_main_arguments(): """"""Test mocking the arguments and running them through main()"""""" args = argparse.Namespace(url=RANDOM_URL, username=RANDOM_USERNAME, password=RANDOM_PASSWORD, tenantcode=RANDOM_TENANTCODE) result = Config(""wso_args.json"").main(args) assert result is True def test_main_interactive(monkeypatch): """"""Test mocking the std in and writing the config"""""" url = StringIO( '%s\n%s\n%s\n%s' % (RANDOM_URL, RANDOM_USERNAME, RANDOM_PASSWORD, RANDOM_TENANTCODE)) # Send std in and run the main function monkeypatch.setattr('sys.stdin', url) # Stage the empty args args = argparse.Namespace(url=None, username=None, password=None, tenantcode=None) result = Config(""wso_interactive.json"").main(args) assert result is True def test_file_data_arguments(): """"""Verify the data written via mock arguments"""""" filename = 'wso_args.json' assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD)) is True assert AUTH.verify_config(filename, 'url', RANDOM_URL) is True assert AUTH.verify_config(filename, 'aw-tenant-code', RANDOM_TENANTCODE) is True def test_file_data_interactive(): """"""Verify the data written via mock std in"""""" filename = 'wso_interactive.json' assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD)) is True assert AUTH.verify_config(filename, 'url', RANDOM_URL) is True assert AUTH.verify_config(filename, 'aw-tenant-code', RANDOM_TENANTCODE) is True def test_main_results(): """"""Test the results of the real arguments"""""" # Due to complexities testing with arguments to get full coverage # run the script externally with full arguments os.popen('python3 -m pip install -e .') os.popen( 'python3 Examples/WSO.py -url cn1234.awtest.com -username citests -password hunter2 -tenantcode shibboleet' ).read() filename = ""uem.json"" assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(""citests"", ""hunter2"")) is True assert AUTH.verify_config(filename, 'url', ""cn1234.awtest.com"") is True assert AUTH.verify_config(filename, 'aw-tenant-code', ""shibboleet"") is True ", https://github.com/runwayml/model-sdk,3132b2ab424f1020e8d30e4897057103ab3e6623,tests/test_model.py::test_model_setup_and_command,tests/test_model.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"# -*- coding: utf-8 -*- # Ensure that the local version of the runway module is used, not a pip # installed version import sys sys.path.insert(0, '..') sys.path.insert(0, '.') import os import json import pytest import time import gzip from time import sleep from runway.model import RunwayModel from runway.__version__ import __version__ as model_sdk_version from runway.data_types import category, text, number, array, image, vector, file, any as any_type from runway.exceptions import * from runway.utils import gzip_decompress, gzip_compress from utils import * from deepdiff import DeepDiff from flask import abort from multiprocessing import Process from io import BytesIO as IO from pytest_cov.embed import cleanup_on_sigterm cleanup_on_sigterm() os.environ['RW_NO_SERVE'] = '1' # Testing Flask Applications: http://flask.pocoo.org/docs/1.0/testing/ def test_model_setup_and_command(): # use a dict to share state across function scopes. This makes up for the # fact that Python 2.x doesn't have support for the 'nonlocal' keyword. closure = dict(setup_ran = False, command_ran = False) expected_manifest = { 'modelSDKVersion': model_sdk_version, 'millisRunning': None, 'millisSinceLastCommand': None, 'GPU': os.environ.get('GPU', False), 'options': [{ 'type': 'category', 'name': 'size', 'oneOf': ['big', 'small'], 'default': 'big', 'description': 'The size of the model. Bigger is better but also slower.', }], 'commands': [{ 'name': 'test_command', 'description': None, 'inputs': [{ 'type': 'text', 'name': 'input', 'description': 'Some input text.', 'default': '', 'minLength': 0 }], 'outputs': [{ 'type': 'number', 'name': 'output', 'description': 'An output number.', 'default': 0 }] }] } rw = RunwayModel() description = 'The size of the model. Bigger is better but also slower.' @rw.setup(options={ 'size': category(choices=['big', 'small'], description=description) }) def setup(opts): closure['setup_ran'] = True return {} inputs = { 'input': text(description='Some input text.') } outputs = { 'output': number(description='An output number.') } description = 'A test command whose description contains emoji 🕳' expected_manifest['commands'][0]['description'] = description @rw.command('test_command', inputs=inputs, outputs=outputs, description=description) def test_command(model, opts): closure['command_ran'] = True return 100 rw.run(debug=True) client = get_test_client(rw) response = client.get('/meta') assert response.is_json manifest = json.loads(response.data) # unset millisRunning as we can't reliably predict this value. # testing that it is an int should be good enough. assert type(manifest['millisRunning']) == int manifest['millisRunning'] = None assert manifest == expected_manifest # TEMPORARILY CHECK / PATH IN ADDITION TO /meta ---------------------------- # ... sorry for the gross dupe code ;) response = client.get('/') assert response.is_json manifest = json.loads(response.data) # unset millisRunning as we can't reliably predict this value. # testing that it is an int should be good enough. assert type(manifest['millisRunning']) == int manifest['millisRunning'] = None assert manifest == expected_manifest # -------------------------------------------------------------------------- # check the input/output manifest for GET /test_command response = client.get('/test_command') assert response.is_json command_manifest = json.loads(response.data) assert command_manifest == expected_manifest['commands'][0] post_data = { 'input': 'test input' } response = client.post('/test_command', json=post_data) assert response.is_json assert json.loads(response.data) == { 'output' : 100 } # now that we've run a command lets make sure millis since last command is # a number manifest_after_command = get_manifest(client) assert type(manifest_after_command['millisSinceLastCommand']) == int assert closure['command_ran'] == True assert closure['setup_ran'] == True def test_model_status(): rw = RunwayModel() assert rw.running_status == 'STARTING' rw.run(debug=True) assert rw.running_status == 'RUNNING' def test_model_healthcheck(): rw = RunwayModel() rw.run(debug=True) client = get_test_client(rw) response = client.get('/healthcheck') assert response.is_json assert response.json == { 'status': 'RUNNING' } def test_model_setup_no_arguments(): # use a dict to share state across function scopes. This makes up for the # fact that Python 2.x doesn't have support for the 'nonlocal' keyword. closure = dict(setup_ran = False) rw = RunwayModel() # Any reason @rw.setup called with no arguments requires the decorated # function NOT to have arguments? This seems a bit like an idiosyncracy to # me. Why not keep the function signature of the wrapped function the # same regardless and simply pass an empty dict in the case of no options? @rw.setup def setup(): closure['setup_ran'] = True rw.run(debug=True) assert closure['setup_ran'] == True def test_model_setup_empty_options(): # use a dict to share state across function scopes. This makes up for the # fact that Python 2.x doesn't have support for the 'nonlocal' keyword. closure = dict(setup_ran = False) rw = RunwayModel() # Any reason @rw.setup called with no arguments requires the decorated # function NOT to have arguments? This seems a bit like an idiosyncracy to # me. Why not keep the function signature of the wrapped function the # same regardless and simply pass an empty dict in the case of no options? @rw.setup(options={}) def setup(opts): closure['setup_ran'] = True rw.run(debug=True) assert closure['setup_ran'] == True def test_model_options_passed_as_arguments_to_run(): # use a dict to share state across function scopes. This makes up for the # fact that Python 2.x doesn't have support for the 'nonlocal' keyword. closure = dict(setup_ran = False) rw = RunwayModel() @rw.setup(options={'initialization_array': array(item_type=text)}) def setup(opts): assert opts['initialization_array'] == ['one', 'two', 'three'] closure['setup_ran'] = True rw.run(debug=True, model_options={ 'initialization_array': ['one', 'two', 'three'] }) assert closure['setup_ran'] == True def test_model_options_missing(): rw = RunwayModel() @rw.setup(options={'initialization_array': array(item_type=text)}) def setup(opts): pass # this will print to stderr still, but the test should pass with pytest.raises(SystemExit): with pytest.raises(MissingOptionError): rw.run(debug=True) def test_setup_invalid_category(): rw = RunwayModel() @rw.setup(options={'category': category(choices=['Starks', 'Lannisters'])}) def setup(opts): pass rw.run(debug=True) client = get_test_client(rw) response = client.post('/setup', json={ 'category': 'Tyrells' }) assert response.status_code == 400 json_response = json.loads(response.data) assert 'error' in json_response # ensure the user is displayed an error that indicates the category option # is problematic assert 'Invalid argument: category' in json_response['error'] # ensure the user is displayed an error that indicates the problematic value assert 'Tyrells' in json_response['error'] def test_command_invalid_category(): rw = RunwayModel() inputs = {'category': category(choices=['Starks', 'Lannisters'])} outputs = {'reflect': text } @rw.command('test_command', inputs=inputs, outputs=outputs) def test_command(opts): return opts['category'] rw.run(debug=True) client = get_test_client(rw) response = client.post('/test_command', json={ 'category': 'Targaryen' }) assert response.status_code == 400 json_response = json.loads(response.data) assert 'error' in json_response # ensure the user is displayed an error that indicates the category option # is problematic assert 'Invalid argument: category' in json_response['error'] # ensure the user is displayed an error that indicates the problematic value assert 'Targaryen' in json_response['error'] def test_meta(capsys): rw = RunwayModel() @rw.setup(options={'initialization_array': array(item_type=text)}) def setup(opts): pass kwargs_1 = { 'inputs': { 'image': image, 'vector': vector(length=5) }, 'outputs': { 'label': text } } @rw.command('command_1', **kwargs_1) def command_1(opts): pass kwargs_2 = { 'description': 'This command is used for testing.', 'inputs': { 'any': any_type, 'file': file }, 'outputs': { 'number': number(min=10, max=100) } } @rw.command('command_2', **kwargs_2) def command_2(opts): pass expected_manifest = { 'options': [ { 'minLength': 0, 'type': 'array', 'name': 'initialization_array', 'description': None, 'itemType': { 'default': '', 'minLength': 0, 'type': 'text', 'name': 'text_array_item', 'description': None ", https://github.com/saabeilin/kafkian,79eb88b874876632590fea48afcab09592c717dd,tests/unit/test_consumer_errors.py::test_consumer_generator_raises_and_closed_on_error,tests/unit/test_consumer_errors.py,NIO,flaky,nan,nan,classify;root_cause,"import uuid from unittest.mock import patch, Mock import pytest from confluent_kafka.cimpl import KafkaError from tests.unit.conftest import consumer_close_mock from kafkian.consumer import Consumer, KafkianException KAFKA_BOOTSTRAP_SERVERS = 'localhost:29092' TEST_TOPIC = 'test.test.' + str(uuid.uuid4()) CONSUMER_CONFIG = { 'bootstrap.servers': KAFKA_BOOTSTRAP_SERVERS, 'auto.offset.reset': 'earliest', 'group.id': str(uuid.uuid4()) } @pytest.fixture def consumer(): return Consumer(CONSUMER_CONFIG, [TEST_TOPIC]) class MockMessage: def __init__(self, _key, _value, _error): self._key = _key self._value = _value self._error = _error def key(self): return self._key def value(self): return self._value def set_key(self, new_key): self._key = new_key def set_value(self, new_value): self._value = new_value def error(self): return self._error class MockError: def __init__(self, _code): self._code = _code def code(self): return self._code def test_consumer_ignores_partition_eof(consumer): key = bytes(str(uuid.uuid4()), encoding='utf8') value = bytes(str(uuid.uuid4()), encoding='utf8') # The first message with _PARTITION_EOF error, should be skipped m1 = MockMessage(None, None, _error=MockError(_code=KafkaError._PARTITION_EOF)) # The next one should be delivered m2 = MockMessage(key, value, _error=None) messages = iter([m1, m2]) def next_message(ignored): return next(messages) with patch('kafkian.consumer.Consumer._poll', next_message): m = next(consumer) assert m.key == key assert m.value == value def test_consumer_generator_raises_and_closed_on_error(consumer): m = MockMessage(None, None, _error=MockError(_code=-1)) with patch('kafkian.consumer.Consumer._poll', Mock(return_value=m)): try: next(consumer) except Exception as e: assert isinstance(e, KafkianException) consumer_close_mock.assert_called_once_with() ", https://github.com/saabeilin/kafkian,79eb88b874876632590fea48afcab09592c717dd,tests/unit/test_producer_avro.py::test_avro_producer_produce,tests/unit/test_producer_avro.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import uuid from unittest.mock import MagicMock, patch import pytest from confluent_kafka import avro from kafkian import producer from kafkian.serde.avroserdebase import AvroRecord from kafkian.serde.serialization import AvroSerializer, Serializer from tests.unit.conftest import producer_produce_mock KAFKA_BOOTSTRAP_SERVERS = 'localhost:29092' SCHEMA_REGISTRY_URL = 'https://localhost:28081' TEST_TOPIC = 'test.test.' + str(uuid.uuid4()) PRODUCER_CONFIG = { 'bootstrap.servers': KAFKA_BOOTSTRAP_SERVERS, 'schema.registry.url': SCHEMA_REGISTRY_URL, } value_schema_str = """""" { ""namespace"": ""my.test"", ""name"": ""value"", ""type"": ""record"", ""fields"" : [ { ""name"" : ""name"", ""type"" : ""string"" } ] } """""" class Message(AvroRecord): _schema = avro.loads(value_schema_str) message = Message({ 'name': 'some name' }) def teardown_function(function): producer_produce_mock.reset_mock() @pytest.fixture(scope='module') def avro_producer(): return producer.Producer( PRODUCER_CONFIG, value_serializer=AvroSerializer(schema_registry_url=SCHEMA_REGISTRY_URL) ) def test_producer_init(avro_producer): assert isinstance( avro_producer.key_serializer, Serializer ) assert isinstance( avro_producer.value_serializer, AvroSerializer ) @patch( 'confluent_kafka.avro.CachedSchemaRegistryClient.register', MagicMock(return_value=1) ) @patch( 'confluent_kafka.avro.CachedSchemaRegistryClient.get_latest_schema', MagicMock(return_value=(1, message._schema, 1)) ) @patch( 'confluent_kafka.avro.CachedSchemaRegistryClient.get_by_id', MagicMock(return_value=message._schema) ) def test_avro_producer_produce(avro_producer): key = 'a' value = message topic = 'z' avro_producer.produce(key=key, value=value, topic=topic) producer_produce_mock.assert_called_once_with( topic, key, avro_producer.value_serializer.serialize(value, topic) ) # # def test_get_subject_names(avro_producer): # topic_name = 'test_topic' # key_subject_name, value_subject_name = ( # avro_producer._get_subject_names(topic_name) # ) # assert key_subject_name == (topic_name + '-key') # assert value_subject_name == (topic_name + '-value') # # # def test_get_topic_schemas(avro_producer): # mock_avro_schema_registry.return_value.\ # get_latest_schema.side_effect = ['1', '2'] # topic_list = ['a'] # topic_schemas = avro_producer._get_topic_schemas(topic_list) # topic, key_schema, value_schema = topic_schemas['a'] # assert topic == 'a' # assert key_schema == '1' # assert value_schema == '2' ", https://github.com/saabeilin/kafkian,79eb88b874876632590fea48afcab09592c717dd,tests/unit/test_producer_avro_key.py::test_avro_producer_produce,tests/unit/test_producer_avro_key.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import uuid from unittest.mock import MagicMock, patch import pytest from kafkian import producer from kafkian.serde.serialization import AvroStringKeySerializer, Serializer from tests.unit.conftest import producer_produce_mock KAFKA_BOOTSTRAP_SERVERS = 'localhost:29092' SCHEMA_REGISTRY_URL = 'https://localhost:28081' TEST_TOPIC = 'test.test.' + str(uuid.uuid4()) PRODUCER_CONFIG = { 'bootstrap.servers': KAFKA_BOOTSTRAP_SERVERS, 'schema.registry.url': SCHEMA_REGISTRY_URL, } def teardown_function(function): producer_produce_mock.reset_mock() @pytest.fixture(scope='module') def avro_producer(): return producer.Producer( PRODUCER_CONFIG, key_serializer=AvroStringKeySerializer(schema_registry_url=SCHEMA_REGISTRY_URL) ) def test_producer_init(avro_producer): assert isinstance( avro_producer.key_serializer, AvroStringKeySerializer ) assert isinstance( avro_producer.value_serializer, Serializer ) @patch( 'confluent_kafka.avro.CachedSchemaRegistryClient.register', MagicMock(return_value=1) ) @patch( 'confluent_kafka.avro.CachedSchemaRegistryClient.get_latest_schema', MagicMock(return_value=(1, AvroStringKeySerializer.KEY_SCHEMA, 1)) ) @patch( 'confluent_kafka.avro.CachedSchemaRegistryClient.get_by_id', MagicMock(return_value=AvroStringKeySerializer.KEY_SCHEMA) ) def test_avro_producer_produce(avro_producer): key = 'a' value = 'a' topic = 'c' avro_producer.produce(topic, key=key, value=value) producer_produce_mock.assert_called_once_with( topic, b'\x00\x00\x00\x00\x01\x02a', value ) ", https://github.com/saga-vcs/saga,7beaae8831696ce813aa85620387e416719198db,tests/test_excel.py::test_merge[tests/merges/xlsx/change_rows],tests/test_excel.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import os import pytest from tests.test_utils import do_test_merge def get_test_merges(): merge_tests = [] for path in os.listdir(""tests/merges/xlsx""): path = os.path.join(""tests/merges/xlsx"", path) if os.path.isdir(path): merge_tests.append(path) return merge_tests @pytest.mark.parametrize(""merge_test_name"", get_test_merges()) def test_merge(merge_test_name): assert do_test_merge(merge_test_name, ""xlsx"")", https://github.com/saga-vcs/saga,7beaae8831696ce813aa85620387e416719198db,tests/test_excel.py::test_merge[tests/merges/xlsx/existing_file],tests/test_excel.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import os import pytest from tests.test_utils import do_test_merge def get_test_merges(): merge_tests = [] for path in os.listdir(""tests/merges/xlsx""): path = os.path.join(""tests/merges/xlsx"", path) if os.path.isdir(path): merge_tests.append(path) return merge_tests @pytest.mark.parametrize(""merge_test_name"", get_test_merges()) def test_merge(merge_test_name): assert do_test_merge(merge_test_name, ""xlsx"")", https://github.com/saga-vcs/saga,7beaae8831696ce813aa85620387e416719198db,tests/test_excel.py::test_merge[tests/merges/xlsx/new_row_init_empty],tests/test_excel.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import os import pytest from tests.test_utils import do_test_merge def get_test_merges(): merge_tests = [] for path in os.listdir(""tests/merges/xlsx""): path = os.path.join(""tests/merges/xlsx"", path) if os.path.isdir(path): merge_tests.append(path) return merge_tests @pytest.mark.parametrize(""merge_test_name"", get_test_merges()) def test_merge(merge_test_name): assert do_test_merge(merge_test_name, ""xlsx"")", https://github.com/saga-vcs/saga,7beaae8831696ce813aa85620387e416719198db,tests/test_excel.py::test_merge[tests/merges/xlsx/new_string],tests/test_excel.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import os import pytest from tests.test_utils import do_test_merge def get_test_merges(): merge_tests = [] for path in os.listdir(""tests/merges/xlsx""): path = os.path.join(""tests/merges/xlsx"", path) if os.path.isdir(path): merge_tests.append(path) return merge_tests @pytest.mark.parametrize(""merge_test_name"", get_test_merges()) def test_merge(merge_test_name): assert do_test_merge(merge_test_name, ""xlsx"")", https://github.com/saga-vcs/saga,7beaae8831696ce813aa85620387e416719198db,tests/test_excel.py::test_merge[tests/merges/xlsx/shift],tests/test_excel.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import os import pytest from tests.test_utils import do_test_merge def get_test_merges(): merge_tests = [] for path in os.listdir(""tests/merges/xlsx""): path = os.path.join(""tests/merges/xlsx"", path) if os.path.isdir(path): merge_tests.append(path) return merge_tests @pytest.mark.parametrize(""merge_test_name"", get_test_merges()) def test_merge(merge_test_name): assert do_test_merge(merge_test_name, ""xlsx"")", https://github.com/saga-vcs/saga,7beaae8831696ce813aa85620387e416719198db,tests/test_excel.py::test_merge[tests/merges/xlsx/single_element],tests/test_excel.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import os import pytest from tests.test_utils import do_test_merge def get_test_merges(): merge_tests = [] for path in os.listdir(""tests/merges/xlsx""): path = os.path.join(""tests/merges/xlsx"", path) if os.path.isdir(path): merge_tests.append(path) return merge_tests @pytest.mark.parametrize(""merge_test_name"", get_test_merges()) def test_merge(merge_test_name): assert do_test_merge(merge_test_name, ""xlsx"")", https://github.com/saga-vcs/saga,7beaae8831696ce813aa85620387e416719198db,tests/test_excel.py::test_merge[tests/merges/xlsx/string_and_number],tests/test_excel.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import os import pytest from tests.test_utils import do_test_merge def get_test_merges(): merge_tests = [] for path in os.listdir(""tests/merges/xlsx""): path = os.path.join(""tests/merges/xlsx"", path) if os.path.isdir(path): merge_tests.append(path) return merge_tests @pytest.mark.parametrize(""merge_test_name"", get_test_merges()) def test_merge(merge_test_name): assert do_test_merge(merge_test_name, ""xlsx"")", https://github.com/saga-vcs/saga,7beaae8831696ce813aa85620387e416719198db,tests/test_text.py::test_merge[tests/merges/txt/add_in_both],tests/test_text.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest import os from tests.test_utils import do_test_merge from saga.base_file.File import File from saga.file_types.text_file import parse_text_file, write_text_file def create_text_file(path, contents): if os.path.isfile(path): os.remove(path) print(""path"") f = open(path, 'w+') f.write(contents) f.close() @pytest.fixture() def setup_text_files(): if not os.path.isdir(""temp""): os.mkdir('temp') create_text_file(""temp/text0"", ""line1\nline2"") def test_create(setup_text_files): text_file = parse_text_file(""id"", ""name"", ""temp/text0"") assert text_file.file_name == ""name"" assert text_file.file_contents.mixed_data_type == [""line1"", ""line2""] def test_create_then_write(setup_text_files): text_file = parse_text_file(""id"", ""name"", ""temp/text0"") write_text_file(text_file) f = open('temp/text0', 'r') assert f.read() == ""line1\nline2"" def test_get_operations_no_change(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) for key in ops: assert len(ops[key]) == 0 def test_get_operations_insert_end(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline2\nline3"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[2]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_insert_middle(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline3\nline2"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[1]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_insert_multiple_middle(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline3\nline4\nline2"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 2 assert ops[""inserted""] == [[1], [2]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_remove(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[1]] def test_get_operations_remove_all(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", """") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 2 assert ops[""removed""] == [[0], [1]] def test_get_operations_remove_mulitple(setup_text_files): create_text_file(""temp/text0"", ""1\n2\n3\n4"") text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""1\n4\n"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 2 assert ops[""removed""] == [[1], [2]] def test_get_operations_insert_remove_change(setup_text_files): create_text_file(""temp/text0"", ""line1\nline2111\nline3\nline4"") text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline211\nline4\nxyz"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[3]] assert len(ops[""changed""]) == 1 assert ops[""changed""] == [[1]] assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[2]] def test_complex_operations(setup_text_files): old_code_list = [ ""for (int i = 0; i < 100; i++ ) {"", ""System.out.println(i * 2);"", ""int temp = i * 1000;"", ""if (temp < i) {"", ""System.out.println(temp, i);"", ""}"", ""}"", ] old_code = ""\n"".join(old_code_list) new_code_list = [ ""for (int j = 0; j < 100; j++ ) {"", ""int temp1 = j * 1000;"", ""if (temp1 < j) {"", ""System.out.println(temp1, j);"", ""}"", ""}"", ] new_code = ""\n"".join(new_code_list) create_text_file(""temp/text0"", old_code) text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", new_code) text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 4 assert ops[""changed""] == [[0], [2], [3], [4]] assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[1]] def get_test_merges(): merge_tests = [] for path in os.listdir(""tests/merges/txt""): path = os.path.join(""tests/merges/txt"", path) if os.path.isdir(path): merge_tests.append(path) return merge_tests @pytest.mark.parametrize(""merge_test_name"", get_test_merges()) def test_merge(merge_test_name): assert do_test_merge(merge_test_name, ""txt"")", https://github.com/saga-vcs/saga,7beaae8831696ce813aa85620387e416719198db,tests/test_text.py::test_merge[tests/merges/txt/add_in_middle],tests/test_text.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest import os from tests.test_utils import do_test_merge from saga.base_file.File import File from saga.file_types.text_file import parse_text_file, write_text_file def create_text_file(path, contents): if os.path.isfile(path): os.remove(path) print(""path"") f = open(path, 'w+') f.write(contents) f.close() @pytest.fixture() def setup_text_files(): if not os.path.isdir(""temp""): os.mkdir('temp') create_text_file(""temp/text0"", ""line1\nline2"") def test_create(setup_text_files): text_file = parse_text_file(""id"", ""name"", ""temp/text0"") assert text_file.file_name == ""name"" assert text_file.file_contents.mixed_data_type == [""line1"", ""line2""] def test_create_then_write(setup_text_files): text_file = parse_text_file(""id"", ""name"", ""temp/text0"") write_text_file(text_file) f = open('temp/text0', 'r') assert f.read() == ""line1\nline2"" def test_get_operations_no_change(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) for key in ops: assert len(ops[key]) == 0 def test_get_operations_insert_end(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline2\nline3"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[2]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_insert_middle(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline3\nline2"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[1]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_insert_multiple_middle(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline3\nline4\nline2"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 2 assert ops[""inserted""] == [[1], [2]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_remove(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[1]] def test_get_operations_remove_all(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", """") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 2 assert ops[""removed""] == [[0], [1]] def test_get_operations_remove_mulitple(setup_text_files): create_text_file(""temp/text0"", ""1\n2\n3\n4"") text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""1\n4\n"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 2 assert ops[""removed""] == [[1], [2]] def test_get_operations_insert_remove_change(setup_text_files): create_text_file(""temp/text0"", ""line1\nline2111\nline3\nline4"") text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline211\nline4\nxyz"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[3]] assert len(ops[""changed""]) == 1 assert ops[""changed""] == [[1]] assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[2]] def test_complex_operations(setup_text_files): old_code_list = [ ""for (int i = 0; i < 100; i++ ) {"", ""System.out.println(i * 2);"", ""int temp = i * 1000;"", ""if (temp < i) {"", ""System.out.println(temp, i);"", ""}"", ""}"", ] old_code = ""\n"".join(old_code_list) new_code_list = [ ""for (int j = 0; j < 100; j++ ) {"", ""int temp1 = j * 1000;"", ""if (temp1 < j) {"", ""System.out.println(temp1, j);"", ""}"", ""}"", ] new_code = ""\n"".join(new_code_list) create_text_file(""temp/text0"", old_code) text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", new_code) text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 4 assert ops[""changed""] == [[0], [2], [3], [4]] assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[1]] def get_test_merges(): merge_tests = [] for path in os.listdir(""tests/merges/txt""): path = os.path.join(""tests/merges/txt"", path) if os.path.isdir(path): merge_tests.append(path) return merge_tests @pytest.mark.parametrize(""merge_test_name"", get_test_merges()) def test_merge(merge_test_name): assert do_test_merge(merge_test_name, ""txt"")", https://github.com/saga-vcs/saga,7beaae8831696ce813aa85620387e416719198db,tests/test_text.py::test_merge[tests/merges/txt/add_line],tests/test_text.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest import os from tests.test_utils import do_test_merge from saga.base_file.File import File from saga.file_types.text_file import parse_text_file, write_text_file def create_text_file(path, contents): if os.path.isfile(path): os.remove(path) print(""path"") f = open(path, 'w+') f.write(contents) f.close() @pytest.fixture() def setup_text_files(): if not os.path.isdir(""temp""): os.mkdir('temp') create_text_file(""temp/text0"", ""line1\nline2"") def test_create(setup_text_files): text_file = parse_text_file(""id"", ""name"", ""temp/text0"") assert text_file.file_name == ""name"" assert text_file.file_contents.mixed_data_type == [""line1"", ""line2""] def test_create_then_write(setup_text_files): text_file = parse_text_file(""id"", ""name"", ""temp/text0"") write_text_file(text_file) f = open('temp/text0', 'r') assert f.read() == ""line1\nline2"" def test_get_operations_no_change(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) for key in ops: assert len(ops[key]) == 0 def test_get_operations_insert_end(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline2\nline3"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[2]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_insert_middle(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline3\nline2"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[1]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_insert_multiple_middle(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline3\nline4\nline2"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 2 assert ops[""inserted""] == [[1], [2]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_remove(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[1]] def test_get_operations_remove_all(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", """") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 2 assert ops[""removed""] == [[0], [1]] def test_get_operations_remove_mulitple(setup_text_files): create_text_file(""temp/text0"", ""1\n2\n3\n4"") text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""1\n4\n"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 2 assert ops[""removed""] == [[1], [2]] def test_get_operations_insert_remove_change(setup_text_files): create_text_file(""temp/text0"", ""line1\nline2111\nline3\nline4"") text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline211\nline4\nxyz"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[3]] assert len(ops[""changed""]) == 1 assert ops[""changed""] == [[1]] assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[2]] def test_complex_operations(setup_text_files): old_code_list = [ ""for (int i = 0; i < 100; i++ ) {"", ""System.out.println(i * 2);"", ""int temp = i * 1000;"", ""if (temp < i) {"", ""System.out.println(temp, i);"", ""}"", ""}"", ] old_code = ""\n"".join(old_code_list) new_code_list = [ ""for (int j = 0; j < 100; j++ ) {"", ""int temp1 = j * 1000;"", ""if (temp1 < j) {"", ""System.out.println(temp1, j);"", ""}"", ""}"", ] new_code = ""\n"".join(new_code_list) create_text_file(""temp/text0"", old_code) text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", new_code) text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 4 assert ops[""changed""] == [[0], [2], [3], [4]] assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[1]] def get_test_merges(): merge_tests = [] for path in os.listdir(""tests/merges/txt""): path = os.path.join(""tests/merges/txt"", path) if os.path.isdir(path): merge_tests.append(path) return merge_tests @pytest.mark.parametrize(""merge_test_name"", get_test_merges()) def test_merge(merge_test_name): assert do_test_merge(merge_test_name, ""txt"")", https://github.com/saga-vcs/saga,7beaae8831696ce813aa85620387e416719198db,tests/test_text.py::test_merge[tests/merges/txt/remove_in_both],tests/test_text.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest import os from tests.test_utils import do_test_merge from saga.base_file.File import File from saga.file_types.text_file import parse_text_file, write_text_file def create_text_file(path, contents): if os.path.isfile(path): os.remove(path) print(""path"") f = open(path, 'w+') f.write(contents) f.close() @pytest.fixture() def setup_text_files(): if not os.path.isdir(""temp""): os.mkdir('temp') create_text_file(""temp/text0"", ""line1\nline2"") def test_create(setup_text_files): text_file = parse_text_file(""id"", ""name"", ""temp/text0"") assert text_file.file_name == ""name"" assert text_file.file_contents.mixed_data_type == [""line1"", ""line2""] def test_create_then_write(setup_text_files): text_file = parse_text_file(""id"", ""name"", ""temp/text0"") write_text_file(text_file) f = open('temp/text0', 'r') assert f.read() == ""line1\nline2"" def test_get_operations_no_change(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) for key in ops: assert len(ops[key]) == 0 def test_get_operations_insert_end(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline2\nline3"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[2]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_insert_middle(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline3\nline2"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[1]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_insert_multiple_middle(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline3\nline4\nline2"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 2 assert ops[""inserted""] == [[1], [2]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_remove(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[1]] def test_get_operations_remove_all(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", """") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 2 assert ops[""removed""] == [[0], [1]] def test_get_operations_remove_mulitple(setup_text_files): create_text_file(""temp/text0"", ""1\n2\n3\n4"") text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""1\n4\n"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 2 assert ops[""removed""] == [[1], [2]] def test_get_operations_insert_remove_change(setup_text_files): create_text_file(""temp/text0"", ""line1\nline2111\nline3\nline4"") text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline211\nline4\nxyz"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[3]] assert len(ops[""changed""]) == 1 assert ops[""changed""] == [[1]] assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[2]] def test_complex_operations(setup_text_files): old_code_list = [ ""for (int i = 0; i < 100; i++ ) {"", ""System.out.println(i * 2);"", ""int temp = i * 1000;"", ""if (temp < i) {"", ""System.out.println(temp, i);"", ""}"", ""}"", ] old_code = ""\n"".join(old_code_list) new_code_list = [ ""for (int j = 0; j < 100; j++ ) {"", ""int temp1 = j * 1000;"", ""if (temp1 < j) {"", ""System.out.println(temp1, j);"", ""}"", ""}"", ] new_code = ""\n"".join(new_code_list) create_text_file(""temp/text0"", old_code) text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", new_code) text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 4 assert ops[""changed""] == [[0], [2], [3], [4]] assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[1]] def get_test_merges(): merge_tests = [] for path in os.listdir(""tests/merges/txt""): path = os.path.join(""tests/merges/txt"", path) if os.path.isdir(path): merge_tests.append(path) return merge_tests @pytest.mark.parametrize(""merge_test_name"", get_test_merges()) def test_merge(merge_test_name): assert do_test_merge(merge_test_name, ""txt"")", https://github.com/saga-vcs/saga,7beaae8831696ce813aa85620387e416719198db,tests/test_text.py::test_merge[tests/merges/txt/remove_line],tests/test_text.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest import os from tests.test_utils import do_test_merge from saga.base_file.File import File from saga.file_types.text_file import parse_text_file, write_text_file def create_text_file(path, contents): if os.path.isfile(path): os.remove(path) print(""path"") f = open(path, 'w+') f.write(contents) f.close() @pytest.fixture() def setup_text_files(): if not os.path.isdir(""temp""): os.mkdir('temp') create_text_file(""temp/text0"", ""line1\nline2"") def test_create(setup_text_files): text_file = parse_text_file(""id"", ""name"", ""temp/text0"") assert text_file.file_name == ""name"" assert text_file.file_contents.mixed_data_type == [""line1"", ""line2""] def test_create_then_write(setup_text_files): text_file = parse_text_file(""id"", ""name"", ""temp/text0"") write_text_file(text_file) f = open('temp/text0', 'r') assert f.read() == ""line1\nline2"" def test_get_operations_no_change(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) for key in ops: assert len(ops[key]) == 0 def test_get_operations_insert_end(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline2\nline3"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[2]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_insert_middle(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline3\nline2"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[1]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_insert_multiple_middle(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline3\nline4\nline2"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 2 assert ops[""inserted""] == [[1], [2]] assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 0 def test_get_operations_remove(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[1]] def test_get_operations_remove_all(setup_text_files): text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", """") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 2 assert ops[""removed""] == [[0], [1]] def test_get_operations_remove_mulitple(setup_text_files): create_text_file(""temp/text0"", ""1\n2\n3\n4"") text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""1\n4\n"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 0 assert len(ops[""removed""]) == 2 assert ops[""removed""] == [[1], [2]] def test_get_operations_insert_remove_change(setup_text_files): create_text_file(""temp/text0"", ""line1\nline2111\nline3\nline4"") text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", ""line1\nline211\nline4\nxyz"") text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 1 assert ops[""inserted""] == [[3]] assert len(ops[""changed""]) == 1 assert ops[""changed""] == [[1]] assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[2]] def test_complex_operations(setup_text_files): old_code_list = [ ""for (int i = 0; i < 100; i++ ) {"", ""System.out.println(i * 2);"", ""int temp = i * 1000;"", ""if (temp < i) {"", ""System.out.println(temp, i);"", ""}"", ""}"", ] old_code = ""\n"".join(old_code_list) new_code_list = [ ""for (int j = 0; j < 100; j++ ) {"", ""int temp1 = j * 1000;"", ""if (temp1 < j) {"", ""System.out.println(temp1, j);"", ""}"", ""}"", ] new_code = ""\n"".join(new_code_list) create_text_file(""temp/text0"", old_code) text_file0 = parse_text_file(""id"", ""name"", ""temp/text0"") create_text_file(""temp/text0"", new_code) text_file1 = parse_text_file(""id"", ""name"", ""temp/text0"") ops = text_file0.get_operations(text_file1) assert len(ops[""file""]) == 0 assert len(ops[""inserted""]) == 0 assert len(ops[""changed""]) == 4 assert ops[""changed""] == [[0], [2], [3], [4]] assert len(ops[""removed""]) == 1 assert ops[""removed""] == [[1]] def get_test_merges(): merge_tests = [] for path in os.listdir(""tests/merges/txt""): path = os.path.join(""tests/merges/txt"", path) if os.path.isdir(path): merge_tests.append(path) return merge_tests @pytest.mark.parametrize(""merge_test_name"", get_test_merges()) def test_merge(merge_test_name): assert do_test_merge(merge_test_name, ""txt"")", https://github.com/Samreay/ChainConsumer,5606696525d91f11d8093085934fa352b98ce97c,tests/test_analysis.py::test_summary_power,tests/test_analysis.py,NOD,flaky,Accepted,https://github.com/Samreay/ChainConsumer/pull/86,classify;root_cause;fix_proposal,"import os import tempfile import numpy as np from scipy.interpolate import interp1d from scipy.stats import skewnorm, norm import pytest from chainconsumer import ChainConsumer class TestChain(object): np.random.seed(1) n = 2000000 data = np.random.normal(loc=5.0, scale=1.5, size=n) data2 = np.random.normal(loc=3, scale=1.0, size=n) data_combined = np.vstack((data, data2)).T data_skew = skewnorm.rvs(5, loc=1, scale=1.5, size=n) def test_summary(self): tolerance = 4e-2 consumer = ChainConsumer() consumer.add_chain(self.data[::10]) consumer.configure(kde=True) summary = consumer.analysis.get_summary() actual = np.array(list(summary.values())[0]) expected = np.array([3.5, 5.0, 6.5]) diff = np.abs(expected - actual) assert np.all(diff < tolerance) def test_summary_no_smooth(self): tolerance = 5e-2 consumer = ChainConsumer() consumer.add_chain(self.data) consumer.configure(smooth=0, bins=2.4) summary = consumer.analysis.get_summary() actual = np.array(list(summary.values())[0]) expected = np.array([3.5, 5.0, 6.5]) diff = np.abs(expected - actual) assert np.all(diff < tolerance) def test_summary2(self): tolerance = 5e-2 consumer = ChainConsumer() consumer.add_chain(self.data_combined, parameters=[""a"", ""b""], name=""chain1"") consumer.add_chain(self.data_combined, name=""chain2"") summary = consumer.analysis.get_summary() k1 = list(summary[0].keys()) k2 = list(summary[1].keys()) assert len(k1) == 2 assert ""a"" in k1 assert ""b"" in k1 assert len(k2) == 2 assert ""a"" in k2 assert ""b"" in k2 expected1 = np.array([3.5, 5.0, 6.5]) expected2 = np.array([2.0, 3.0, 4.0]) diff1 = np.abs(expected1 - np.array(list(summary[0][""a""]))) diff2 = np.abs(expected2 - np.array(list(summary[0][""b""]))) assert np.all(diff1 < tolerance) assert np.all(diff2 < tolerance) def test_summary_some_params(self): consumer = ChainConsumer() consumer.add_chain(self.data_combined, parameters=[""a"", ""b""], name=""chain1"") summary = consumer.analysis.get_summary(parameters=[""a""], squeeze=False) k1 = list(summary[0].keys()) assert len(k1) == 1 assert ""a"" in k1 assert ""b"" not in k1 def test_summary1(self): tolerance = 5e-2 consumer = ChainConsumer() consumer.add_chain(self.data) consumer.configure(bins=0.8) summary = consumer.analysis.get_summary() actual = np.array(list(summary.values())[0]) expected = np.array([3.5, 5.0, 6.5]) diff = np.abs(expected - actual) assert np.all(diff < tolerance) def test_summary_specific(self): tolerance = 5e-2 consumer = ChainConsumer() consumer.add_chain(self.data, name=""A"") consumer.configure(bins=0.8) summary = consumer.analysis.get_summary(chains=""A"") actual = np.array(list(summary.values())[0]) expected = np.array([3.5, 5.0, 6.5]) diff = np.abs(expected - actual) assert np.all(diff < tolerance) def test_summary_disjoint(self): tolerance = 5e-2 consumer = ChainConsumer() consumer.add_chain(self.data, parameters=""A"") consumer.add_chain(self.data, parameters=""B"") consumer.configure(bins=0.8) summary = consumer.analysis.get_summary(parameters=""A"") assert len(summary) == 2 # Two chains assert summary[1] == {} # Second chain doesnt have param A actual = summary[0][""A""] expected = np.array([3.5, 5.0, 6.5]) diff = np.abs(expected - actual) assert np.all(diff < tolerance) def test_summary_power(self): tolerance = 4e-2 consumer = ChainConsumer() data = np.random.normal(loc=0, scale=np.sqrt(2), size=1000000) consumer.add_chain(data, power=2.0) summary = consumer.analysis.get_summary() actual = np.array(list(summary.values())[0]) expected = np.array([-1.0, 0.0, 1.0]) diff = np.abs(expected - actual) assert np.all(diff < tolerance) def test_output_text(self): consumer = ChainConsumer() consumer.add_chain(self.data, parameters=[""a""]) consumer.configure(bins=0.8) vals = consumer.analysis.get_summary()[""a""] text = consumer.analysis.get_parameter_text(*vals) assert text == r""5.0\pm 1.5"" def test_output_text_asymmetric(self): p1 = [1.0, 2.0, 3.5] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""2.0^{+1.5}_{-1.0}"" def test_output_format1(self): p1 = [1.0e-1, 2.0e-1, 3.5e-1] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""0.20^{+0.15}_{-0.10}"" def test_output_format2(self): p1 = [1.0e-2, 2.0e-2, 3.5e-2] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""0.020^{+0.015}_{-0.010}"" def test_output_format3(self): p1 = [1.0e-3, 2.0e-3, 3.5e-3] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""\left( 2.0^{+1.5}_{-1.0} \right) \times 10^{-3}"" def test_output_format4(self): p1 = [1.0e3, 2.0e3, 3.5e3] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""\left( 2.0^{+1.5}_{-1.0} \right) \times 10^{3}"" def test_output_format5(self): p1 = [1.1e6, 2.2e6, 3.3e6] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""\left( 2.2\pm 1.1 \right) \times 10^{6}"" def test_output_format6(self): p1 = [1.0e-2, 2.0e-2, 3.5e-2] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1, wrap=True) assert text == r""$0.020^{+0.015}_{-0.010}$"" def test_output_format7(self): p1 = [None, 2.0e-2, 3.5e-2] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == """" def test_output_format8(self): p1 = [-1, -0.0, 1] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""0.0\pm 1.0"" def test_output_format9(self): x = 123456.789 d = 123.321 p1 = [x - d, x, x + d] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""123460\pm 120"" def test_output_format10(self): x = 123456.789 d = 1234.321 p1 = [x - d, x, x + d] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""\left( 123.5\pm 1.2 \right) \times 10^{3}"" def test_output_format11(self): x = 222.222 d = 111.111 p1 = [x - d, x, x + d] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""220\pm 110"" def test_output_format12(self): x = 222.222 d = 11.111 p1 = [x - d, x, x + d] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""222\pm 11"" def test_output_format13(self): x = 2222.222 d = 11.111 p1 = [x - d, x, x + d] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""2222\pm 11"" def test_output_format14(self): x = 222.222 d = 1.111 p1 = [x - d, x, x + d] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""222.2\pm 1.1"" def test_output_format15(self): x = 222.222 d = 0.111 p1 = [x - d, x, x + d] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""222.22\pm 0.11"" def test_output_format16(self): x = 222.2222222 d = 0.0111 p1 = [x - d, x, x + d] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""222.222\pm 0.011"" def test_output_format17(self): p1 = [1.0, 1.0, 2.0] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""1.0^{+1.0}_{-0.0}"" def test_output_format18(self): p1 = [10000.0, 10000.0, 10000.0] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""\left( 1.0\pm 0.0 \right) \times 10^{4}"" def test_output_format19(self): p1 = [1.0, 2.0, 2.0] consumer = ChainConsumer() text = consumer.analysis.get_parameter_text(*p1) assert text == r""2.0^{+0.0}_{-1.0}"" def test_file_loading1(self): data = self.data[:1000] directory = tempfile._get_default_tempdir() filename = next(tempfile._get_candidate_names()) filename = directory + os.sep + filename + "".txt"" np.savetxt(filename, data) consumer = ChainConsumer() consumer.add_chain(filename) summary = consumer.analysis.get_summary() actual = np.array(list(summary.values())[0]) assert np.abs(actual[1] - 5.0) < 0.5 def test_file_loading2(self): data = self.data[:1000] directory = tempfile._get_default_tempdir() filename = next(tempfile._get_candidate_names()) filename = directory + os.sep + filename + "".npy"" np.save(filename, data) consumer = ChainConsumer() consumer.add_chain(filename) summary = consumer.a", https://github.com/samuelduchesne/pyTrnsysType,6cf552e4ccd66f7b728e84654e580befe2061114,tests/test_xml.py::TestTrnsysModel::test_connect_eqcollection_to_type,tests/test_xml.py,NIO,flaky,Deleted,nan,classify;root_cause,"import os from tempfile import NamedTemporaryFile import pytest from mock import patch from path import Path from shapely.geometry import Point, LineString @pytest.fixture(scope=""class"") def fan_type(): """"""Fixture to create a TrnsysModel from xml"""""" from pyTrnsysType.trnsymodel import TrnsysModel fan1 = TrnsysModel.from_xml(Path(""tests/input_files/Type146.xml"")) yield fan1 @pytest.fixture(scope=""class"") def pipe_type(): """"""Fixture to create a TrnsysModel from xml. Also tests using a Path"""""" from pyTrnsysType.trnsymodel import TrnsysModel pipe = TrnsysModel.from_xml(""tests/input_files/Type951.xml"") yield pipe @pytest.fixture(scope=""class"") def tank_type(): from pyTrnsysType.trnsymodel import TrnsysModel with patch(""builtins.input"", return_value=""y""): tank = TrnsysModel.from_xml(""tests/input_files/Type4a.xml"") yield tank @pytest.fixture(scope=""class"") def weather_type(): from pyTrnsysType.trnsymodel import TrnsysModel with patch(""builtins.input"", return_value=""y""): weather = TrnsysModel.from_xml(""tests/input_files/Type15-3.xml"") yield weather class TestTrnsysModel: @patch(""builtins.input"", return_value=""y"") def test_chiller_type(self, input): """"""Fixture to create a TrnsysModel from xml from an xml that contains unknown tags. Should primpt user. Passes when input == 'y'"""""" from pyTrnsysType.trnsymodel import TrnsysModel fan1 = TrnsysModel.from_xml(""tests/input_files/Type107-simplified.xml"") return fan1 def test_unit_name(self, pipe_type): assert pipe_type.unit_name == ""Type951"" def test_cycles(self, pipe_type): n_nodes = 20 pipe_type.parameters[""Number_of_Radial_Soil_Nodes""] = n_nodes mylist = list(pipe_type.parameters.keys()) sub = ""Radial_Distance_of_Node_"" actual = len([s for s in mylist if sub.lower() in s.lower()]) assert pipe_type.parameters[""Radial_Distance_of_Node_1""].idx == 27 assert pipe_type.parameters[""Radial_Distance_of_Node_1""].one_based_idx == 28 assert actual == n_nodes def test_cycles_2(self, pipe_type): """"""changing number of fluid nodes from 100 to 20 should create 20 outputs for pipe 2 and 20 outputs for pipe 1"""""" outputs = pipe_type.outputs mylist = list(outputs.keys()) sub_1 = ""Average_Fluid_Temperature_Pipe_1_"" sub_2 = ""Average_Fluid_Temperature_Pipe_2_"" expected_1 = len([s for s in mylist if sub_1.lower() in s.lower()]) expected_2 = len([s for s in mylist if sub_2.lower() in s.lower()]) assert 100 == expected_1 assert 100 == expected_2 n_nodes = 20 pipe_type.parameters[""Number_of_Fluid_Nodes""] = n_nodes outputs = pipe_type.outputs mylist = list(outputs.keys()) sub_1 = ""Average_Fluid_Temperature_Pipe_1_"" sub_2 = ""Average_Fluid_Temperature_Pipe_2_"" expected_1 = len([s for s in mylist if sub_1.lower() in s.lower()]) expected_2 = len([s for s in mylist if sub_2.lower() in s.lower()]) assert n_nodes == expected_1 assert n_nodes == expected_2 def test_cycles_issue14(self): """"""https://github.com/samuelduchesne/pyTrnsysType/issues/14"""""" from pyTrnsysType.trnsymodel import TrnsysModel valve = TrnsysModel.from_xml(""tests/input_files/Type647.xml"") assert valve.parameters[""Number_of_Outlet_Ports""].value == 2 assert valve.outputs[""Outlet_Flowrate_1""] assert valve.outputs[""Outlet_Flowrate_2""] with pytest.raises(KeyError): # Make sure the cyclebase is not returned assert valve.outputs[""Outlet_Flowrate""] def test_cycles_order(self): from pyTrnsysType.trnsymodel import TrnsysModel weather_type = TrnsysModel.from_xml(""tests/input_files/Type15-3.xml"") assert weather_type.outputs[24].name == ""Beam radiation for surface-1"" weather_type.parameters[""Number_of_surfaces""] = 2 assert weather_type.outputs[26].name == ""Beam radiation for surface-2"" def test_cancel_missing_tag(self, tank_type): from pyTrnsysType.trnsymodel import TrnsysModel from bs4 import BeautifulSoup with pytest.raises(NotImplementedError): with patch(""builtins.input"", return_value=""N""): with open(""tests/input_files/Type4a.xml"") as xml: soup = BeautifulSoup(xml, ""xml"") new_tag = soup.new_tag(""obscureTag"") new_tag.string = ""this is a test"" soup.find(""TrnsysModel"").append(new_tag) with NamedTemporaryFile(""w"", delete=False) as tmp: tmp.write(str(soup)) tmp.close() tank = TrnsysModel.from_xml(tmp.name) os.unlink(tmp.name) def test_out_of_bounds(self, pipe_type): """"""should trigger ValueError because out of bounds"""""" with pytest.raises(ValueError): pipe_type.parameters[""Number_of_Radial_Soil_Nodes""] = 21 def test_get_attr(self, fan_type): """"""Test getter for class TypeVariable"""""" in_air_temp = fan_type.inputs[""Inlet_Air_Temperature""] assert in_air_temp def test_set_attr(self, fan_type): """"""Test setter for class TypeVariable"""""" new_value = 12 attr_name = ""Inlet_Air_Temperature"" fan_type.inputs[attr_name] = new_value Q_ = fan_type.inputs[attr_name].value assert fan_type.inputs[attr_name].value == Q_.__class__(new_value, Q_.units) def test_set_attr_quantity(self, fan_type): """"""Test setter for class TypeVariable with type _Quantity. This tests setting a value with different but equivalent units"""""" attr_name = ""Rated_Volumetric_Flow_Rate"" new_value = fan_type.parameters[attr_name].value.to(""m^3/s"") * 10 fan_type.parameters[attr_name] = new_value assert fan_type.parameters[attr_name].value == new_value.to(""l/s"") def test_get_initial_input_values(self, tank_type): print(tank_type.initial_input_values) def test_set_initial_input_values(self, fan_type): new_value = -20 attr_name = ""Inlet_Air_Temperature"" fan_type.initial_input_values[attr_name] = new_value assert fan_type.initial_input_values[attr_name] def test_get_attr_derivative(self, tank_type): """"""Test setter for class Derivative"""""" attr_name = ""Initial_temperature_of_node_1"" assert tank_type.derivatives[attr_name].value.m == 50.0 def test_set_attr_derivative(self, tank_type): """"""Test setter for class Derivative"""""" attr_name = ""Initial_temperature_of_node_1"" tank_type.derivatives[attr_name] = 60 assert tank_type.derivatives[attr_name].value.m == 60.0 def test_set_attr_cycle_parameters(self, pipe_type): """"""Test setter for class TypeVariable"""""" attr_name = ""Radial_Distance_of_Node_1"" new_value = 0.05 pipe_type.parameters[attr_name] = new_value Q_ = pipe_type.parameters[attr_name].value assert pipe_type.parameters[attr_name].value == Q_.__class__( new_value, Q_.units ) def test_to_deck(self, fan_type): """"""test to Input File representation of a TrnsysModel"""""" print(fan_type._to_deck()) def test_initial_input_values_to_deck(self, fan_type): """"""test to Input File representation of a TrnsysModel"""""" print(fan_type.initial_input_values._to_deck()) def test_set_attr_cycle_question(self, tank_type): attr_name = ""Besides_the_top_and_bottom_nodes_how_many_other_nodes_are_there_"" new_value = 10 tank_type.outputs[attr_name] = new_value Q_ = tank_type.outputs[attr_name].value assert tank_type.outputs[attr_name].value == Q_.__class__(new_value, Q_.units) def test_set_attr_cycle_question_2(self, tank_type): attr_name = ""How_many_temperature_levels_nodes_should_be_used_in_the_tank_"" new_value = 10 tank_type.parameters[attr_name] = new_value Q_ = tank_type.parameters[attr_name].value assert tank_type.parameters[attr_name].value == Q_.__class__( new_value, Q_.units ) def test_trnsysmodel_repr(self, tank_type): """"""test the __repr__ for :class:`TrnsysModel`"""""" assert str(tank_type) == ""[5]Type4: Storage Tank; Fixed Inlets, Uniform Losses"" def test_typecycle_repr(self, tank_type): assert repr(tank_type._meta.cycles[0]) == ""output 1 to 13"" def test_collections_repr(self, tank_type): assert repr(tank_type.inputs) == str(tank_type.inputs) assert repr(tank_type.outputs) == str(tank_type.outputs) assert repr(tank_type.parameters) == str(tank_type.parameters) def test_TypeVariable_repr(self, tank_type): for _, a in tank_type.inputs.items(): assert float(a) == 45.0 assert ( repr(a) == ""Hot-side temperature; units=C; value=45.0 °C\nThe"" "" temperature of the fluid flowing into the tank from "" ""the heat source. The inlet location for this hot-side "" ""fluid is one element below the upper auxiliary heating"" "" element."" ) break for _, a in tank_type.outputs.items(): assert float(a) == 0.0 assert ( repr(a) == ""Temperature to heat source; units=C; value=0.0 "" ""°C\nThe temperature of the fluid flowing from the"" "" bottom of the storage tank and returning to the heat "" ""source (the temperature of the bottom node)."" ) break for _, a in tank_type.parameters.items(): assert int(a) == 1 assert ( repr(a) == ""Fixed inlet positions; un", https://github.com/samuelduchesne/pyTrnsysType,6cf552e4ccd66f7b728e84654e580befe2061114,tests/test_xml.py::TestTrnsysModel::test_copy_trnsys_model,tests/test_xml.py,NIO,flaky,Deleted,nan,classify;root_cause,"import os from tempfile import NamedTemporaryFile import pytest from mock import patch from path import Path from shapely.geometry import Point, LineString @pytest.fixture(scope=""class"") def fan_type(): """"""Fixture to create a TrnsysModel from xml"""""" from pyTrnsysType.trnsymodel import TrnsysModel fan1 = TrnsysModel.from_xml(Path(""tests/input_files/Type146.xml"")) yield fan1 @pytest.fixture(scope=""class"") def pipe_type(): """"""Fixture to create a TrnsysModel from xml. Also tests using a Path"""""" from pyTrnsysType.trnsymodel import TrnsysModel pipe = TrnsysModel.from_xml(""tests/input_files/Type951.xml"") yield pipe @pytest.fixture(scope=""class"") def tank_type(): from pyTrnsysType.trnsymodel import TrnsysModel with patch(""builtins.input"", return_value=""y""): tank = TrnsysModel.from_xml(""tests/input_files/Type4a.xml"") yield tank @pytest.fixture(scope=""class"") def weather_type(): from pyTrnsysType.trnsymodel import TrnsysModel with patch(""builtins.input"", return_value=""y""): weather = TrnsysModel.from_xml(""tests/input_files/Type15-3.xml"") yield weather class TestTrnsysModel: @patch(""builtins.input"", return_value=""y"") def test_chiller_type(self, input): """"""Fixture to create a TrnsysModel from xml from an xml that contains unknown tags. Should primpt user. Passes when input == 'y'"""""" from pyTrnsysType.trnsymodel import TrnsysModel fan1 = TrnsysModel.from_xml(""tests/input_files/Type107-simplified.xml"") return fan1 def test_unit_name(self, pipe_type): assert pipe_type.unit_name == ""Type951"" def test_cycles(self, pipe_type): n_nodes = 20 pipe_type.parameters[""Number_of_Radial_Soil_Nodes""] = n_nodes mylist = list(pipe_type.parameters.keys()) sub = ""Radial_Distance_of_Node_"" actual = len([s for s in mylist if sub.lower() in s.lower()]) assert pipe_type.parameters[""Radial_Distance_of_Node_1""].idx == 27 assert pipe_type.parameters[""Radial_Distance_of_Node_1""].one_based_idx == 28 assert actual == n_nodes def test_cycles_2(self, pipe_type): """"""changing number of fluid nodes from 100 to 20 should create 20 outputs for pipe 2 and 20 outputs for pipe 1"""""" outputs = pipe_type.outputs mylist = list(outputs.keys()) sub_1 = ""Average_Fluid_Temperature_Pipe_1_"" sub_2 = ""Average_Fluid_Temperature_Pipe_2_"" expected_1 = len([s for s in mylist if sub_1.lower() in s.lower()]) expected_2 = len([s for s in mylist if sub_2.lower() in s.lower()]) assert 100 == expected_1 assert 100 == expected_2 n_nodes = 20 pipe_type.parameters[""Number_of_Fluid_Nodes""] = n_nodes outputs = pipe_type.outputs mylist = list(outputs.keys()) sub_1 = ""Average_Fluid_Temperature_Pipe_1_"" sub_2 = ""Average_Fluid_Temperature_Pipe_2_"" expected_1 = len([s for s in mylist if sub_1.lower() in s.lower()]) expected_2 = len([s for s in mylist if sub_2.lower() in s.lower()]) assert n_nodes == expected_1 assert n_nodes == expected_2 def test_cycles_issue14(self): """"""https://github.com/samuelduchesne/pyTrnsysType/issues/14"""""" from pyTrnsysType.trnsymodel import TrnsysModel valve = TrnsysModel.from_xml(""tests/input_files/Type647.xml"") assert valve.parameters[""Number_of_Outlet_Ports""].value == 2 assert valve.outputs[""Outlet_Flowrate_1""] assert valve.outputs[""Outlet_Flowrate_2""] with pytest.raises(KeyError): # Make sure the cyclebase is not returned assert valve.outputs[""Outlet_Flowrate""] def test_cycles_order(self): from pyTrnsysType.trnsymodel import TrnsysModel weather_type = TrnsysModel.from_xml(""tests/input_files/Type15-3.xml"") assert weather_type.outputs[24].name == ""Beam radiation for surface-1"" weather_type.parameters[""Number_of_surfaces""] = 2 assert weather_type.outputs[26].name == ""Beam radiation for surface-2"" def test_cancel_missing_tag(self, tank_type): from pyTrnsysType.trnsymodel import TrnsysModel from bs4 import BeautifulSoup with pytest.raises(NotImplementedError): with patch(""builtins.input"", return_value=""N""): with open(""tests/input_files/Type4a.xml"") as xml: soup = BeautifulSoup(xml, ""xml"") new_tag = soup.new_tag(""obscureTag"") new_tag.string = ""this is a test"" soup.find(""TrnsysModel"").append(new_tag) with NamedTemporaryFile(""w"", delete=False) as tmp: tmp.write(str(soup)) tmp.close() tank = TrnsysModel.from_xml(tmp.name) os.unlink(tmp.name) def test_out_of_bounds(self, pipe_type): """"""should trigger ValueError because out of bounds"""""" with pytest.raises(ValueError): pipe_type.parameters[""Number_of_Radial_Soil_Nodes""] = 21 def test_get_attr(self, fan_type): """"""Test getter for class TypeVariable"""""" in_air_temp = fan_type.inputs[""Inlet_Air_Temperature""] assert in_air_temp def test_set_attr(self, fan_type): """"""Test setter for class TypeVariable"""""" new_value = 12 attr_name = ""Inlet_Air_Temperature"" fan_type.inputs[attr_name] = new_value Q_ = fan_type.inputs[attr_name].value assert fan_type.inputs[attr_name].value == Q_.__class__(new_value, Q_.units) def test_set_attr_quantity(self, fan_type): """"""Test setter for class TypeVariable with type _Quantity. This tests setting a value with different but equivalent units"""""" attr_name = ""Rated_Volumetric_Flow_Rate"" new_value = fan_type.parameters[attr_name].value.to(""m^3/s"") * 10 fan_type.parameters[attr_name] = new_value assert fan_type.parameters[attr_name].value == new_value.to(""l/s"") def test_get_initial_input_values(self, tank_type): print(tank_type.initial_input_values) def test_set_initial_input_values(self, fan_type): new_value = -20 attr_name = ""Inlet_Air_Temperature"" fan_type.initial_input_values[attr_name] = new_value assert fan_type.initial_input_values[attr_name] def test_get_attr_derivative(self, tank_type): """"""Test setter for class Derivative"""""" attr_name = ""Initial_temperature_of_node_1"" assert tank_type.derivatives[attr_name].value.m == 50.0 def test_set_attr_derivative(self, tank_type): """"""Test setter for class Derivative"""""" attr_name = ""Initial_temperature_of_node_1"" tank_type.derivatives[attr_name] = 60 assert tank_type.derivatives[attr_name].value.m == 60.0 def test_set_attr_cycle_parameters(self, pipe_type): """"""Test setter for class TypeVariable"""""" attr_name = ""Radial_Distance_of_Node_1"" new_value = 0.05 pipe_type.parameters[attr_name] = new_value Q_ = pipe_type.parameters[attr_name].value assert pipe_type.parameters[attr_name].value == Q_.__class__( new_value, Q_.units ) def test_to_deck(self, fan_type): """"""test to Input File representation of a TrnsysModel"""""" print(fan_type._to_deck()) def test_initial_input_values_to_deck(self, fan_type): """"""test to Input File representation of a TrnsysModel"""""" print(fan_type.initial_input_values._to_deck()) def test_set_attr_cycle_question(self, tank_type): attr_name = ""Besides_the_top_and_bottom_nodes_how_many_other_nodes_are_there_"" new_value = 10 tank_type.outputs[attr_name] = new_value Q_ = tank_type.outputs[attr_name].value assert tank_type.outputs[attr_name].value == Q_.__class__(new_value, Q_.units) def test_set_attr_cycle_question_2(self, tank_type): attr_name = ""How_many_temperature_levels_nodes_should_be_used_in_the_tank_"" new_value = 10 tank_type.parameters[attr_name] = new_value Q_ = tank_type.parameters[attr_name].value assert tank_type.parameters[attr_name].value == Q_.__class__( new_value, Q_.units ) def test_trnsysmodel_repr(self, tank_type): """"""test the __repr__ for :class:`TrnsysModel`"""""" assert str(tank_type) == ""[5]Type4: Storage Tank; Fixed Inlets, Uniform Losses"" def test_typecycle_repr(self, tank_type): assert repr(tank_type._meta.cycles[0]) == ""output 1 to 13"" def test_collections_repr(self, tank_type): assert repr(tank_type.inputs) == str(tank_type.inputs) assert repr(tank_type.outputs) == str(tank_type.outputs) assert repr(tank_type.parameters) == str(tank_type.parameters) def test_TypeVariable_repr(self, tank_type): for _, a in tank_type.inputs.items(): assert float(a) == 45.0 assert ( repr(a) == ""Hot-side temperature; units=C; value=45.0 °C\nThe"" "" temperature of the fluid flowing into the tank from "" ""the heat source. The inlet location for this hot-side "" ""fluid is one element below the upper auxiliary heating"" "" element."" ) break for _, a in tank_type.outputs.items(): assert float(a) == 0.0 assert ( repr(a) == ""Temperature to heat source; units=C; value=0.0 "" ""°C\nThe temperature of the fluid flowing from the"" "" bottom of the storage tank and returning to the heat "" ""source (the temperature of the bottom node)."" ) break for _, a in tank_type.parameters.items(): assert int(a) == 1 assert ( repr(a) == ""Fixed inlet positions; un", https://github.com/samuelduchesne/pyTrnsysType,6cf552e4ccd66f7b728e84654e580befe2061114,tests/test_xml.py::TestTrnsysModel::test_cycles_2,tests/test_xml.py,NIO,flaky,Deleted,nan,classify;root_cause,"import os from tempfile import NamedTemporaryFile import pytest from mock import patch from path import Path from shapely.geometry import Point, LineString @pytest.fixture(scope=""class"") def fan_type(): """"""Fixture to create a TrnsysModel from xml"""""" from pyTrnsysType.trnsymodel import TrnsysModel fan1 = TrnsysModel.from_xml(Path(""tests/input_files/Type146.xml"")) yield fan1 @pytest.fixture(scope=""class"") def pipe_type(): """"""Fixture to create a TrnsysModel from xml. Also tests using a Path"""""" from pyTrnsysType.trnsymodel import TrnsysModel pipe = TrnsysModel.from_xml(""tests/input_files/Type951.xml"") yield pipe @pytest.fixture(scope=""class"") def tank_type(): from pyTrnsysType.trnsymodel import TrnsysModel with patch(""builtins.input"", return_value=""y""): tank = TrnsysModel.from_xml(""tests/input_files/Type4a.xml"") yield tank @pytest.fixture(scope=""class"") def weather_type(): from pyTrnsysType.trnsymodel import TrnsysModel with patch(""builtins.input"", return_value=""y""): weather = TrnsysModel.from_xml(""tests/input_files/Type15-3.xml"") yield weather class TestTrnsysModel: @patch(""builtins.input"", return_value=""y"") def test_chiller_type(self, input): """"""Fixture to create a TrnsysModel from xml from an xml that contains unknown tags. Should primpt user. Passes when input == 'y'"""""" from pyTrnsysType.trnsymodel import TrnsysModel fan1 = TrnsysModel.from_xml(""tests/input_files/Type107-simplified.xml"") return fan1 def test_unit_name(self, pipe_type): assert pipe_type.unit_name == ""Type951"" def test_cycles(self, pipe_type): n_nodes = 20 pipe_type.parameters[""Number_of_Radial_Soil_Nodes""] = n_nodes mylist = list(pipe_type.parameters.keys()) sub = ""Radial_Distance_of_Node_"" actual = len([s for s in mylist if sub.lower() in s.lower()]) assert pipe_type.parameters[""Radial_Distance_of_Node_1""].idx == 27 assert pipe_type.parameters[""Radial_Distance_of_Node_1""].one_based_idx == 28 assert actual == n_nodes def test_cycles_2(self, pipe_type): """"""changing number of fluid nodes from 100 to 20 should create 20 outputs for pipe 2 and 20 outputs for pipe 1"""""" outputs = pipe_type.outputs mylist = list(outputs.keys()) sub_1 = ""Average_Fluid_Temperature_Pipe_1_"" sub_2 = ""Average_Fluid_Temperature_Pipe_2_"" expected_1 = len([s for s in mylist if sub_1.lower() in s.lower()]) expected_2 = len([s for s in mylist if sub_2.lower() in s.lower()]) assert 100 == expected_1 assert 100 == expected_2 n_nodes = 20 pipe_type.parameters[""Number_of_Fluid_Nodes""] = n_nodes outputs = pipe_type.outputs mylist = list(outputs.keys()) sub_1 = ""Average_Fluid_Temperature_Pipe_1_"" sub_2 = ""Average_Fluid_Temperature_Pipe_2_"" expected_1 = len([s for s in mylist if sub_1.lower() in s.lower()]) expected_2 = len([s for s in mylist if sub_2.lower() in s.lower()]) assert n_nodes == expected_1 assert n_nodes == expected_2 def test_cycles_issue14(self): """"""https://github.com/samuelduchesne/pyTrnsysType/issues/14"""""" from pyTrnsysType.trnsymodel import TrnsysModel valve = TrnsysModel.from_xml(""tests/input_files/Type647.xml"") assert valve.parameters[""Number_of_Outlet_Ports""].value == 2 assert valve.outputs[""Outlet_Flowrate_1""] assert valve.outputs[""Outlet_Flowrate_2""] with pytest.raises(KeyError): # Make sure the cyclebase is not returned assert valve.outputs[""Outlet_Flowrate""] def test_cycles_order(self): from pyTrnsysType.trnsymodel import TrnsysModel weather_type = TrnsysModel.from_xml(""tests/input_files/Type15-3.xml"") assert weather_type.outputs[24].name == ""Beam radiation for surface-1"" weather_type.parameters[""Number_of_surfaces""] = 2 assert weather_type.outputs[26].name == ""Beam radiation for surface-2"" def test_cancel_missing_tag(self, tank_type): from pyTrnsysType.trnsymodel import TrnsysModel from bs4 import BeautifulSoup with pytest.raises(NotImplementedError): with patch(""builtins.input"", return_value=""N""): with open(""tests/input_files/Type4a.xml"") as xml: soup = BeautifulSoup(xml, ""xml"") new_tag = soup.new_tag(""obscureTag"") new_tag.string = ""this is a test"" soup.find(""TrnsysModel"").append(new_tag) with NamedTemporaryFile(""w"", delete=False) as tmp: tmp.write(str(soup)) tmp.close() tank = TrnsysModel.from_xml(tmp.name) os.unlink(tmp.name) def test_out_of_bounds(self, pipe_type): """"""should trigger ValueError because out of bounds"""""" with pytest.raises(ValueError): pipe_type.parameters[""Number_of_Radial_Soil_Nodes""] = 21 def test_get_attr(self, fan_type): """"""Test getter for class TypeVariable"""""" in_air_temp = fan_type.inputs[""Inlet_Air_Temperature""] assert in_air_temp def test_set_attr(self, fan_type): """"""Test setter for class TypeVariable"""""" new_value = 12 attr_name = ""Inlet_Air_Temperature"" fan_type.inputs[attr_name] = new_value Q_ = fan_type.inputs[attr_name].value assert fan_type.inputs[attr_name].value == Q_.__class__(new_value, Q_.units) def test_set_attr_quantity(self, fan_type): """"""Test setter for class TypeVariable with type _Quantity. This tests setting a value with different but equivalent units"""""" attr_name = ""Rated_Volumetric_Flow_Rate"" new_value = fan_type.parameters[attr_name].value.to(""m^3/s"") * 10 fan_type.parameters[attr_name] = new_value assert fan_type.parameters[attr_name].value == new_value.to(""l/s"") def test_get_initial_input_values(self, tank_type): print(tank_type.initial_input_values) def test_set_initial_input_values(self, fan_type): new_value = -20 attr_name = ""Inlet_Air_Temperature"" fan_type.initial_input_values[attr_name] = new_value assert fan_type.initial_input_values[attr_name] def test_get_attr_derivative(self, tank_type): """"""Test setter for class Derivative"""""" attr_name = ""Initial_temperature_of_node_1"" assert tank_type.derivatives[attr_name].value.m == 50.0 def test_set_attr_derivative(self, tank_type): """"""Test setter for class Derivative"""""" attr_name = ""Initial_temperature_of_node_1"" tank_type.derivatives[attr_name] = 60 assert tank_type.derivatives[attr_name].value.m == 60.0 def test_set_attr_cycle_parameters(self, pipe_type): """"""Test setter for class TypeVariable"""""" attr_name = ""Radial_Distance_of_Node_1"" new_value = 0.05 pipe_type.parameters[attr_name] = new_value Q_ = pipe_type.parameters[attr_name].value assert pipe_type.parameters[attr_name].value == Q_.__class__( new_value, Q_.units ) def test_to_deck(self, fan_type): """"""test to Input File representation of a TrnsysModel"""""" print(fan_type._to_deck()) def test_initial_input_values_to_deck(self, fan_type): """"""test to Input File representation of a TrnsysModel"""""" print(fan_type.initial_input_values._to_deck()) def test_set_attr_cycle_question(self, tank_type): attr_name = ""Besides_the_top_and_bottom_nodes_how_many_other_nodes_are_there_"" new_value = 10 tank_type.outputs[attr_name] = new_value Q_ = tank_type.outputs[attr_name].value assert tank_type.outputs[attr_name].value == Q_.__class__(new_value, Q_.units) def test_set_attr_cycle_question_2(self, tank_type): attr_name = ""How_many_temperature_levels_nodes_should_be_used_in_the_tank_"" new_value = 10 tank_type.parameters[attr_name] = new_value Q_ = tank_type.parameters[attr_name].value assert tank_type.parameters[attr_name].value == Q_.__class__( new_value, Q_.units ) def test_trnsysmodel_repr(self, tank_type): """"""test the __repr__ for :class:`TrnsysModel`"""""" assert str(tank_type) == ""[5]Type4: Storage Tank; Fixed Inlets, Uniform Losses"" def test_typecycle_repr(self, tank_type): assert repr(tank_type._meta.cycles[0]) == ""output 1 to 13"" def test_collections_repr(self, tank_type): assert repr(tank_type.inputs) == str(tank_type.inputs) assert repr(tank_type.outputs) == str(tank_type.outputs) assert repr(tank_type.parameters) == str(tank_type.parameters) def test_TypeVariable_repr(self, tank_type): for _, a in tank_type.inputs.items(): assert float(a) == 45.0 assert ( repr(a) == ""Hot-side temperature; units=C; value=45.0 °C\nThe"" "" temperature of the fluid flowing into the tank from "" ""the heat source. The inlet location for this hot-side "" ""fluid is one element below the upper auxiliary heating"" "" element."" ) break for _, a in tank_type.outputs.items(): assert float(a) == 0.0 assert ( repr(a) == ""Temperature to heat source; units=C; value=0.0 "" ""°C\nThe temperature of the fluid flowing from the"" "" bottom of the storage tank and returning to the heat "" ""source (the temperature of the bottom node)."" ) break for _, a in tank_type.parameters.items(): assert int(a) == 1 assert ( repr(a) == ""Fixed inlet positions; un", https://github.com/samuelduchesne/pyTrnsysType,6cf552e4ccd66f7b728e84654e580befe2061114,tests/test_xml.py::TestTrnsysModel::test_get_attr_derivative,tests/test_xml.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os from tempfile import NamedTemporaryFile import pytest from mock import patch from path import Path from shapely.geometry import Point, LineString @pytest.fixture(scope=""class"") def fan_type(): """"""Fixture to create a TrnsysModel from xml"""""" from pyTrnsysType.trnsymodel import TrnsysModel fan1 = TrnsysModel.from_xml(Path(""tests/input_files/Type146.xml"")) yield fan1 @pytest.fixture(scope=""class"") def pipe_type(): """"""Fixture to create a TrnsysModel from xml. Also tests using a Path"""""" from pyTrnsysType.trnsymodel import TrnsysModel pipe = TrnsysModel.from_xml(""tests/input_files/Type951.xml"") yield pipe @pytest.fixture(scope=""class"") def tank_type(): from pyTrnsysType.trnsymodel import TrnsysModel with patch(""builtins.input"", return_value=""y""): tank = TrnsysModel.from_xml(""tests/input_files/Type4a.xml"") yield tank @pytest.fixture(scope=""class"") def weather_type(): from pyTrnsysType.trnsymodel import TrnsysModel with patch(""builtins.input"", return_value=""y""): weather = TrnsysModel.from_xml(""tests/input_files/Type15-3.xml"") yield weather class TestTrnsysModel: @patch(""builtins.input"", return_value=""y"") def test_chiller_type(self, input): """"""Fixture to create a TrnsysModel from xml from an xml that contains unknown tags. Should primpt user. Passes when input == 'y'"""""" from pyTrnsysType.trnsymodel import TrnsysModel fan1 = TrnsysModel.from_xml(""tests/input_files/Type107-simplified.xml"") return fan1 def test_unit_name(self, pipe_type): assert pipe_type.unit_name == ""Type951"" def test_cycles(self, pipe_type): n_nodes = 20 pipe_type.parameters[""Number_of_Radial_Soil_Nodes""] = n_nodes mylist = list(pipe_type.parameters.keys()) sub = ""Radial_Distance_of_Node_"" actual = len([s for s in mylist if sub.lower() in s.lower()]) assert pipe_type.parameters[""Radial_Distance_of_Node_1""].idx == 27 assert pipe_type.parameters[""Radial_Distance_of_Node_1""].one_based_idx == 28 assert actual == n_nodes def test_cycles_2(self, pipe_type): """"""changing number of fluid nodes from 100 to 20 should create 20 outputs for pipe 2 and 20 outputs for pipe 1"""""" outputs = pipe_type.outputs mylist = list(outputs.keys()) sub_1 = ""Average_Fluid_Temperature_Pipe_1_"" sub_2 = ""Average_Fluid_Temperature_Pipe_2_"" expected_1 = len([s for s in mylist if sub_1.lower() in s.lower()]) expected_2 = len([s for s in mylist if sub_2.lower() in s.lower()]) assert 100 == expected_1 assert 100 == expected_2 n_nodes = 20 pipe_type.parameters[""Number_of_Fluid_Nodes""] = n_nodes outputs = pipe_type.outputs mylist = list(outputs.keys()) sub_1 = ""Average_Fluid_Temperature_Pipe_1_"" sub_2 = ""Average_Fluid_Temperature_Pipe_2_"" expected_1 = len([s for s in mylist if sub_1.lower() in s.lower()]) expected_2 = len([s for s in mylist if sub_2.lower() in s.lower()]) assert n_nodes == expected_1 assert n_nodes == expected_2 def test_cycles_issue14(self): """"""https://github.com/samuelduchesne/pyTrnsysType/issues/14"""""" from pyTrnsysType.trnsymodel import TrnsysModel valve = TrnsysModel.from_xml(""tests/input_files/Type647.xml"") assert valve.parameters[""Number_of_Outlet_Ports""].value == 2 assert valve.outputs[""Outlet_Flowrate_1""] assert valve.outputs[""Outlet_Flowrate_2""] with pytest.raises(KeyError): # Make sure the cyclebase is not returned assert valve.outputs[""Outlet_Flowrate""] def test_cycles_order(self): from pyTrnsysType.trnsymodel import TrnsysModel weather_type = TrnsysModel.from_xml(""tests/input_files/Type15-3.xml"") assert weather_type.outputs[24].name == ""Beam radiation for surface-1"" weather_type.parameters[""Number_of_surfaces""] = 2 assert weather_type.outputs[26].name == ""Beam radiation for surface-2"" def test_cancel_missing_tag(self, tank_type): from pyTrnsysType.trnsymodel import TrnsysModel from bs4 import BeautifulSoup with pytest.raises(NotImplementedError): with patch(""builtins.input"", return_value=""N""): with open(""tests/input_files/Type4a.xml"") as xml: soup = BeautifulSoup(xml, ""xml"") new_tag = soup.new_tag(""obscureTag"") new_tag.string = ""this is a test"" soup.find(""TrnsysModel"").append(new_tag) with NamedTemporaryFile(""w"", delete=False) as tmp: tmp.write(str(soup)) tmp.close() tank = TrnsysModel.from_xml(tmp.name) os.unlink(tmp.name) def test_out_of_bounds(self, pipe_type): """"""should trigger ValueError because out of bounds"""""" with pytest.raises(ValueError): pipe_type.parameters[""Number_of_Radial_Soil_Nodes""] = 21 def test_get_attr(self, fan_type): """"""Test getter for class TypeVariable"""""" in_air_temp = fan_type.inputs[""Inlet_Air_Temperature""] assert in_air_temp def test_set_attr(self, fan_type): """"""Test setter for class TypeVariable"""""" new_value = 12 attr_name = ""Inlet_Air_Temperature"" fan_type.inputs[attr_name] = new_value Q_ = fan_type.inputs[attr_name].value assert fan_type.inputs[attr_name].value == Q_.__class__(new_value, Q_.units) def test_set_attr_quantity(self, fan_type): """"""Test setter for class TypeVariable with type _Quantity. This tests setting a value with different but equivalent units"""""" attr_name = ""Rated_Volumetric_Flow_Rate"" new_value = fan_type.parameters[attr_name].value.to(""m^3/s"") * 10 fan_type.parameters[attr_name] = new_value assert fan_type.parameters[attr_name].value == new_value.to(""l/s"") def test_get_initial_input_values(self, tank_type): print(tank_type.initial_input_values) def test_set_initial_input_values(self, fan_type): new_value = -20 attr_name = ""Inlet_Air_Temperature"" fan_type.initial_input_values[attr_name] = new_value assert fan_type.initial_input_values[attr_name] def test_get_attr_derivative(self, tank_type): """"""Test setter for class Derivative"""""" attr_name = ""Initial_temperature_of_node_1"" assert tank_type.derivatives[attr_name].value.m == 50.0 def test_set_attr_derivative(self, tank_type): """"""Test setter for class Derivative"""""" attr_name = ""Initial_temperature_of_node_1"" tank_type.derivatives[attr_name] = 60 assert tank_type.derivatives[attr_name].value.m == 60.0 def test_set_attr_cycle_parameters(self, pipe_type): """"""Test setter for class TypeVariable"""""" attr_name = ""Radial_Distance_of_Node_1"" new_value = 0.05 pipe_type.parameters[attr_name] = new_value Q_ = pipe_type.parameters[attr_name].value assert pipe_type.parameters[attr_name].value == Q_.__class__( new_value, Q_.units ) def test_to_deck(self, fan_type): """"""test to Input File representation of a TrnsysModel"""""" print(fan_type._to_deck()) def test_initial_input_values_to_deck(self, fan_type): """"""test to Input File representation of a TrnsysModel"""""" print(fan_type.initial_input_values._to_deck()) def test_set_attr_cycle_question(self, tank_type): attr_name = ""Besides_the_top_and_bottom_nodes_how_many_other_nodes_are_there_"" new_value = 10 tank_type.outputs[attr_name] = new_value Q_ = tank_type.outputs[attr_name].value assert tank_type.outputs[attr_name].value == Q_.__class__(new_value, Q_.units) def test_set_attr_cycle_question_2(self, tank_type): attr_name = ""How_many_temperature_levels_nodes_should_be_used_in_the_tank_"" new_value = 10 tank_type.parameters[attr_name] = new_value Q_ = tank_type.parameters[attr_name].value assert tank_type.parameters[attr_name].value == Q_.__class__( new_value, Q_.units ) def test_trnsysmodel_repr(self, tank_type): """"""test the __repr__ for :class:`TrnsysModel`"""""" assert str(tank_type) == ""[5]Type4: Storage Tank; Fixed Inlets, Uniform Losses"" def test_typecycle_repr(self, tank_type): assert repr(tank_type._meta.cycles[0]) == ""output 1 to 13"" def test_collections_repr(self, tank_type): assert repr(tank_type.inputs) == str(tank_type.inputs) assert repr(tank_type.outputs) == str(tank_type.outputs) assert repr(tank_type.parameters) == str(tank_type.parameters) def test_TypeVariable_repr(self, tank_type): for _, a in tank_type.inputs.items(): assert float(a) == 45.0 assert ( repr(a) == ""Hot-side temperature; units=C; value=45.0 °C\nThe"" "" temperature of the fluid flowing into the tank from "" ""the heat source. The inlet location for this hot-side "" ""fluid is one element below the upper auxiliary heating"" "" element."" ) break for _, a in tank_type.outputs.items(): assert float(a) == 0.0 assert ( repr(a) == ""Temperature to heat source; units=C; value=0.0 "" ""°C\nThe temperature of the fluid flowing from the"" "" bottom of the storage tank and returning to the heat "" ""source (the temperature of the bottom node)."" ) break for _, a in tank_type.parameters.items(): assert int(a) == 1 assert ( repr(a) == ""Fixed inlet positions; un", https://github.com/sanjaz/lazypr,226895868769f613d9e905f265a4fd7c421d2fd4,tests/test_config.py::TestConfig::test_get_default_config_file_path,tests/test_config.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"""""""Loading Configuration tests."""""" import os from lazypr.config import get_default_config_file_path class TestConfig: """"""Test loading configuration."""""" def test_get_default_config_file_path(self): """"""Test getting default config file path."""""" real_home = os.environ.get(""HOME"") os.environ[""HOME""] = ""/some/test/value"" expected_path = ""/some/test/value/.config/lazy_pr.ini"" assert get_default_config_file_path() == expected_path if real_home is not None: os.environ[""HOME""] = real_home def test_get_default_config_file_path_xdg(self): """"""Test getting config file path when `XDG_CONFIG_HOME` is set."""""" real_config_home = os.environ.get(""XDG_CONFIG_HOME"") os.environ[""XDG_CONFIG_HOME""] = ""/some/test/value"" expected_path = ""/some/test/value/lazy_pr.ini"" assert get_default_config_file_path() == expected_path if real_config_home is not None: os.environ[""XDG_CONFIG_HOME""] = real_config_home ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/EPlusInterfaceFunctions_tests/test_iddgroups.py::test_group2commlst,eppy/tests/EPlusInterfaceFunctions_tests/test_iddgroups.py,NIO,flaky,Opened,https://github.com/santoshphilip/eppy/pull/358,classify;root_cause,"# Copyright (c) 2016 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""pytest for iddgroups"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.EPlusInterfaceFunctions.iddgroups as iddgroups iddtxt = """"""! W/m2, W or deg C ! W/s ! W/W ! years ! ************************************************************************** Lead Input; Simulation Data; \\group G1 Version, \\unique-object \\format singleLine A1 ; \\field Version Identifier \\required-field \\default 7.0 Version1, \\unique-object \\format singleLine A1 ; \\field Version Identifier \\required-field \\default 7.0 Version2, \\unique-object \\format singleLine A1 ; \\field Version Identifier \\required-field \\default 7.0 \\group G2 VersionG, \\unique-object \\format singleLine A1 ; \\field Version Identifier \\required-field \\default 7.0 VersionG1, \\unique-object \\format singleLine A1 ; \\field Version Identifier \\required-field \\default 7.0 VersionG2, \\unique-object \\format singleLine A1 ; \\field Version Identifier \\required-field \\default 7.0 """""" idd_commlst = [ [[]], [[]], [ [""unique-object"", ""format singleLine""], [""field Version Identifier"", ""required-field"", ""default 7.0""], ], [ [""unique-object"", ""format singleLine""], [""field Version Identifier"", ""required-field"", ""default 7.0""], ], [ [""unique-object"", ""format singleLine""], [""field Version Identifier"", ""required-field"", ""default 7.0""], ], [ [""unique-object"", ""format singleLine""], [""field Version Identifier"", ""required-field"", ""default 7.0""], ], [ [""unique-object"", ""format singleLine""], [""field Version Identifier"", ""required-field"", ""default 7.0""], ], [ [""unique-object"", ""format singleLine""], [""field Version Identifier"", ""required-field"", ""default 7.0""], ], ] idf_commdct = [ [{}], [{}], [ {""format"": [""singleLine""], ""unique-object"": [""""]}, {""default"": [""7.0""], ""field"": [""Version Identifier""], ""required-field"": [""""]}, ], [ {""format"": [""singleLine""], ""unique-object"": [""""]}, {""default"": [""7.0""], ""field"": [""Version Identifier""], ""required-field"": [""""]}, ], [ {""format"": [""singleLine""], ""unique-object"": [""""]}, {""default"": [""7.0""], ""field"": [""Version Identifier""], ""required-field"": [""""]}, ], [ {""format"": [""singleLine""], ""unique-object"": [""""]}, {""default"": [""7.0""], ""field"": [""Version Identifier""], ""required-field"": [""""]}, ], [ {""format"": [""singleLine""], ""unique-object"": [""""]}, {""default"": [""7.0""], ""field"": [""Version Identifier""], ""required-field"": [""""]}, ], [ {""format"": [""singleLine""], ""unique-object"": [""""]}, {""default"": [""7.0""], ""field"": [""Version Identifier""], ""required-field"": [""""]}, ], ] def test_idd2groups(): """"""py.test for idd2groups"""""" data = ( ( { ""G2"": [""VersionG"", ""VersionG1"", ""VersionG2""], ""G1"": [""Version"", ""Version1"", ""Version2""], None: [""Lead Input"", ""Simulation Data""], }, ), # gdict ) for (gdict,) in data: result = iddgroups.iddtxt2groups(iddtxt) assert result == gdict def test_idd2group(): """"""py.test for idd2group"""""" data = ( ( { ""G2"": [""VersionG"", ""VersionG1"", ""VersionG2""], ""G1"": [""Version"", ""Version1"", ""Version2""], None: [""Lead Input"", ""Simulation Data""], }, ), # gdict ) for (gdict,) in data: fhandle = StringIO(iddtxt) result = iddgroups.idd2group(fhandle) assert result == gdict def test_iddtxt2grouplist(): """"""py.test for iddtxt2grouplist"""""" data = ( ( [ (None, ""Lead Input""), (None, ""Simulation Data""), (""G1"", ""Version""), (""G1"", ""Version1""), (""G1"", ""Version2""), (""G2"", ""VersionG""), (""G2"", ""VersionG1""), (""G2"", ""VersionG2""), ], ), # glist ) for (glist,) in data: result = iddgroups.iddtxt2grouplist(iddtxt) assert result == glist def test_idd2grouplist(): """"""py.test idd2grouplist"""""" data = ( ( [ (None, ""Lead Input""), (None, ""Simulation Data""), (""G1"", ""Version""), (""G1"", ""Version1""), (""G1"", ""Version2""), (""G2"", ""VersionG""), (""G2"", ""VersionG1""), (""G2"", ""VersionG2""), ], ), # glist ) for (glist,) in data: fhandle = StringIO(iddtxt) result = iddgroups.idd2grouplist(fhandle) assert result == glist def test_group2commlst(): """"""py.test for group2commlst"""""" data = ( ( [ [[""group None"", ""idfobj Lead Input""]], [[""group None"", ""idfobj Simulation Data""]], [ [ ""group G1"", ""idfobj Version"", ""unique-object"", ""format singleLine"", ], [""field Version Identifier"", ""required-field"", ""default 7.0""], ], [ [ ""group G1"", ""idfobj Version1"", ""unique-object"", ""format singleLine"", ], [""field Version Identifier"", ""required-field"", ""default 7.0""], ], [ [ ""group G1"", ""idfobj Version2"", ""unique-object"", ""format singleLine"", ], [""field Version Identifier"", ""required-field"", ""default 7.0""], ], [ [ ""group G2"", ""idfobj VersionG"", ""unique-object"", ""format singleLine"", ], [""field Version Identifier"", ""required-field"", ""default 7.0""], ], [ [ ""group G2"", ""idfobj VersionG1"", ""unique-object"", ""format singleLine"", ], [""field Version Identifier"", ""required-field"", ""default 7.0""], ], [ [ ""group G2"", ""idfobj VersionG2"", ""unique-object"", ""format singleLine"", ], [""field Version Identifier"", ""required-field"", ""default 7.0""], ], ], ), # groupcommlst ) for (groupcommlst,) in data: glist = iddgroups.iddtxt2grouplist(iddtxt) result = iddgroups.group2commlst(idd_commlst, glist) assert result == groupcommlst ( ( [ (None, ""Lead Input""), (None, ""Simulation Data""), (""G1"", ""Version""), (""G1"", ""Version1""), (""G1"", ""Version2""), (""G2"", ""VersionG""), (""G2"", ""VersionG1""), (""G2"", ""VersionG2""), ], ), # glist ) def test_group2commdct(): """"""py.test for group2commdct"""""" data = ( ( [ [{""group"": None, ""idfobj"": ""Lead Input""}], [{""group"": None, ""idfobj"": ""Simulation Data""}], [ { ""group"": ""G1"", ""idfobj"": ""Version"", ""format"": [""singleLine""], ""unique-object"": [""""], }, { ""default"": [""7.0""], ""field"": [""Version Identifier""], ""required-field"": [""""], }, ], [ { ""group"": ""G1"", ""idfobj"": ""Version1"", ""format"": [""singleLine""], ""unique-object"": [""""], }, { ""default"": [""7.0""], ""field"": [""Version Identifier""], ""required-field"": [""""], }, ], [ { ""group"": ""G1"", ""idfobj"": ""Version2"", ""format"": [""singleLine""], ""unique-object"": [""""], }, { ""default"": [""7.0""], ""field"": [""Version Identifier""], ""required-field"": [""""], }, ], [ { ""group"": ""G2"", ""idfobj"": ""VersionG"", ""format"": [""singleLine""], ""unique-object"": [""""], ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_bunch_subclass.py::TestEpBunch::test_getreferingobjs,eppy/tests/test_bunch_subclass.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for bunch_subclass"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import pytest from six import StringIO from eppy.EPlusInterfaceFunctions import readidf import eppy.bunch_subclass as bunch_subclass import eppy.bunchhelpers as bunchhelpers from eppy.iddcurrent import iddcurrent import eppy.idfreader as idfreader from eppy.modeleditor import IDF # This test is ugly because I have to send file names and not able to send file handles EpBunch = bunch_subclass.EpBunch iddtxt = iddcurrent.iddtxt # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) # This test is ugly because I have to send file names and not able to send file handles idftxt = """"""Version, 6.0; BuildingSurface:Detailed, Zn001:Wall001, !- Name Wall, !- Surface Type EXTWALL80, !- Construction Name West Zone, !- Zone Name Outdoors, !- Outside Boundary Condition , !- Outside Boundary Condition Object SunExposed, !- Sun Exposure WindExposed, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 0,0,3.048000, !- X,Y,Z ==> Vertex 1 {m} 0,0,0, !- X,Y,Z ==> Vertex 2 {m} 6.096000,0,0, !- X,Y,Z ==> Vertex 3 {m} 6.096000,0,3.048000; !- X,Y,Z ==> Vertex 4 {m} FenestrationSurface:Detailed, Zn001:Wall001:Win001, !- Name Window, !- Surface Type WIN-CON-LIGHT, !- Construction Name Zn001:Wall001, !- Building Surface Name , !- Outside Boundary Condition Object 0.5000000, !- View Factor to Ground , !- Shading Control Name , !- Frame and Divider Name 1.0, !- Multiplier 4, !- Number of Vertices 0.548000,0,2.5000, !- X,Y,Z ==> Vertex 1 {m} 0.548000,0,0.5000, !- X,Y,Z ==> Vertex 2 {m} 5.548000,0,0.5000, !- X,Y,Z ==> Vertex 3 {m} 5.548000,0,2.5000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn001:Wall002, !- Name Wall, !- Surface Type EXTWALL80, !- Construction Name West Zone, !- Zone Name Outdoors, !- Outside Boundary Condition , !- Outside Boundary Condition Object SunExposed, !- Sun Exposure WindExposed, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 0,6.096000,3.048000, !- X,Y,Z ==> Vertex 1 {m} 0,6.096000,0, !- X,Y,Z ==> Vertex 2 {m} 0,0,0, !- X,Y,Z ==> Vertex 3 {m} 0,0,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn001:Wall003, !- Name Wall, !- Surface Type PARTITION06, !- Construction Name West Zone, !- Zone Name Surface, !- Outside Boundary Condition Zn003:Wall004, !- Outside Boundary Condition Object NoSun, !- Sun Exposure NoWind, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 6.096000,6.096000,3.048000, !- X,Y,Z ==> Vertex 1 {m} 6.096000,6.096000,0, !- X,Y,Z ==> Vertex 2 {m} 0,6.096000,0, !- X,Y,Z ==> Vertex 3 {m} 0,6.096000,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn001:Wall004, !- Name Wall, !- Surface Type PARTITION06, !- Construction Name West Zone, !- Zone Name Surface, !- Outside Boundary Condition Zn002:Wall004, !- Outside Boundary Condition Object NoSun, !- Sun Exposure NoWind, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 6.096000,0,3.048000, !- X,Y,Z ==> Vertex 1 {m} 6.096000,0,0, !- X,Y,Z ==> Vertex 2 {m} 6.096000,6.096000,0, !- X,Y,Z ==> Vertex 3 {m} 6.096000,6.096000,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn001:Flr001, !- Name Floor, !- Surface Type FLOOR SLAB 8 IN, !- Construction Name West Zone, !- Zone Name Surface, !- Outside Boundary Condition Zn001:Flr001, !- Outside Boundary Condition Object NoSun, !- Sun Exposure NoWind, !- Wind Exposure 1.000000, !- View Factor to Ground 4, !- Number of Vertices 0,0,0, !- X,Y,Z ==> Vertex 1 {m} 0,6.096000,0, !- X,Y,Z ==> Vertex 2 {m} 6.096000,6.096000,0, !- X,Y,Z ==> Vertex 3 {m} 6.096000,0,0; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn001:Roof001, !- Name Roof, !- Surface Type ROOF34, !- Construction Name West Zone, !- Zone Name Outdoors, !- Outside Boundary Condition , !- Outside Boundary Condition Object SunExposed, !- Sun Exposure WindExposed, !- Wind Exposure 0, !- View Factor to Ground 4, !- Number of Vertices 0,6.096000,3.048000, !- X,Y,Z ==> Vertex 1 {m} 0,0,3.048000, !- X,Y,Z ==> Vertex 2 {m} 6.096000,0,3.048000, !- X,Y,Z ==> Vertex 3 {m} 6.096000,6.096000,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn002:Wall001, !- Name Wall, !- Surface Type EXTWALL80, !- Construction Name EAST ZONE, !- Zone Name Outdoors, !- Outside Boundary Condition , !- Outside Boundary Condition Object SunExposed, !- Sun Exposure WindExposed, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 12.19200,6.096000,3.048000, !- X,Y,Z ==> Vertex 1 {m} 12.19200,6.096000,0, !- X,Y,Z ==> Vertex 2 {m} 9.144000,6.096000,0, !- X,Y,Z ==> Vertex 3 {m} 9.144000,6.096000,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn002:Wall002, !- Name Wall, !- Surface Type EXTWALL80, !- Construction Name EAST ZONE, !- Zone Name Outdoors, !- Outside Boundary Condition , !- Outside Boundary Condition Object SunExposed, !- Sun Exposure WindExposed, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 6.096000,0,3.048000, !- X,Y,Z ==> Vertex 1 {m} 6.096000,0,0, !- X,Y,Z ==> Vertex 2 {m} 12.19200,0,0, !- X,Y,Z ==> Vertex 3 {m} 12.19200,0,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn002:Wall003, !- Name Wall, !- Surface Type EXTWALL80, !- Construction Name EAST ZONE, !- Zone Name Outdoors, !- Outside Boundary Condition , !- Outside Boundary Condition Object SunExposed, !- Sun Exposure WindExposed, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 12.19200,0,3.048000, !- X,Y,Z ==> Vertex 1 {m} 12.19200,0,0, !- X,Y,Z ==> Vertex 2 {m} 12.19200,6.096000,0, !- X,Y,Z ==> Vertex 3 {m} 12.19200,6.096000,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn002:Wall004, !- Name Wall, !- Surface Type PARTITION06, !- Construction Name EAST ZONE, !- Zone Name Surface, !- Outside Boundary Condition Zn001:Wall004, !- Outside Boundary Condition Object NoSun, !- Sun Exposure NoWind, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 6.096000,6.096000,3.048000, !- X,Y,Z ==> Vertex 1 {m} 6.096000,6.096000,0, !- X,Y,Z ==> Vertex 2 {m} 6.096000,0,0, !- X,Y,Z ==> Vertex 3 {m} 6.096000,0,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn002:Wall005, !- Name Wall, !- Surface Type PARTITION06, !- Construction Name EAST ZONE, !- Zone Name Surface, !- Outside Boundary Condition Zn003:Wall005, !- Outside Boundary Condition Object NoSun, !- Sun Exposure NoWind, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 9.144000,6.096000,3.048000, !- X,Y,Z ==> Vertex 1 {m} 9.144000,6.096000,0, !- X,Y,Z ==> Vertex 2 {m} 6.096000,6.096000,0, !- X,Y,Z ==> Vertex 3 {m} 6.096000,6.096000,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn002:Flr001, !- Name Floor, !- Surface Type FLOOR SLAB 8 IN, !- Construction Name EAST ZONE, !- Zone Name Surface, !- Outside Boundary Condition Zn002:Flr001, !- Outside Boundary Condition Object NoSun, !- Sun Exposure NoWin", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_bunch_subclass.py::TestEpBunch::test_get_referenced_object,eppy/tests/test_bunch_subclass.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for bunch_subclass"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import pytest from six import StringIO from eppy.EPlusInterfaceFunctions import readidf import eppy.bunch_subclass as bunch_subclass import eppy.bunchhelpers as bunchhelpers from eppy.iddcurrent import iddcurrent import eppy.idfreader as idfreader from eppy.modeleditor import IDF # This test is ugly because I have to send file names and not able to send file handles EpBunch = bunch_subclass.EpBunch iddtxt = iddcurrent.iddtxt # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) # This test is ugly because I have to send file names and not able to send file handles idftxt = """"""Version, 6.0; BuildingSurface:Detailed, Zn001:Wall001, !- Name Wall, !- Surface Type EXTWALL80, !- Construction Name West Zone, !- Zone Name Outdoors, !- Outside Boundary Condition , !- Outside Boundary Condition Object SunExposed, !- Sun Exposure WindExposed, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 0,0,3.048000, !- X,Y,Z ==> Vertex 1 {m} 0,0,0, !- X,Y,Z ==> Vertex 2 {m} 6.096000,0,0, !- X,Y,Z ==> Vertex 3 {m} 6.096000,0,3.048000; !- X,Y,Z ==> Vertex 4 {m} FenestrationSurface:Detailed, Zn001:Wall001:Win001, !- Name Window, !- Surface Type WIN-CON-LIGHT, !- Construction Name Zn001:Wall001, !- Building Surface Name , !- Outside Boundary Condition Object 0.5000000, !- View Factor to Ground , !- Shading Control Name , !- Frame and Divider Name 1.0, !- Multiplier 4, !- Number of Vertices 0.548000,0,2.5000, !- X,Y,Z ==> Vertex 1 {m} 0.548000,0,0.5000, !- X,Y,Z ==> Vertex 2 {m} 5.548000,0,0.5000, !- X,Y,Z ==> Vertex 3 {m} 5.548000,0,2.5000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn001:Wall002, !- Name Wall, !- Surface Type EXTWALL80, !- Construction Name West Zone, !- Zone Name Outdoors, !- Outside Boundary Condition , !- Outside Boundary Condition Object SunExposed, !- Sun Exposure WindExposed, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 0,6.096000,3.048000, !- X,Y,Z ==> Vertex 1 {m} 0,6.096000,0, !- X,Y,Z ==> Vertex 2 {m} 0,0,0, !- X,Y,Z ==> Vertex 3 {m} 0,0,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn001:Wall003, !- Name Wall, !- Surface Type PARTITION06, !- Construction Name West Zone, !- Zone Name Surface, !- Outside Boundary Condition Zn003:Wall004, !- Outside Boundary Condition Object NoSun, !- Sun Exposure NoWind, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 6.096000,6.096000,3.048000, !- X,Y,Z ==> Vertex 1 {m} 6.096000,6.096000,0, !- X,Y,Z ==> Vertex 2 {m} 0,6.096000,0, !- X,Y,Z ==> Vertex 3 {m} 0,6.096000,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn001:Wall004, !- Name Wall, !- Surface Type PARTITION06, !- Construction Name West Zone, !- Zone Name Surface, !- Outside Boundary Condition Zn002:Wall004, !- Outside Boundary Condition Object NoSun, !- Sun Exposure NoWind, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 6.096000,0,3.048000, !- X,Y,Z ==> Vertex 1 {m} 6.096000,0,0, !- X,Y,Z ==> Vertex 2 {m} 6.096000,6.096000,0, !- X,Y,Z ==> Vertex 3 {m} 6.096000,6.096000,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn001:Flr001, !- Name Floor, !- Surface Type FLOOR SLAB 8 IN, !- Construction Name West Zone, !- Zone Name Surface, !- Outside Boundary Condition Zn001:Flr001, !- Outside Boundary Condition Object NoSun, !- Sun Exposure NoWind, !- Wind Exposure 1.000000, !- View Factor to Ground 4, !- Number of Vertices 0,0,0, !- X,Y,Z ==> Vertex 1 {m} 0,6.096000,0, !- X,Y,Z ==> Vertex 2 {m} 6.096000,6.096000,0, !- X,Y,Z ==> Vertex 3 {m} 6.096000,0,0; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn001:Roof001, !- Name Roof, !- Surface Type ROOF34, !- Construction Name West Zone, !- Zone Name Outdoors, !- Outside Boundary Condition , !- Outside Boundary Condition Object SunExposed, !- Sun Exposure WindExposed, !- Wind Exposure 0, !- View Factor to Ground 4, !- Number of Vertices 0,6.096000,3.048000, !- X,Y,Z ==> Vertex 1 {m} 0,0,3.048000, !- X,Y,Z ==> Vertex 2 {m} 6.096000,0,3.048000, !- X,Y,Z ==> Vertex 3 {m} 6.096000,6.096000,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn002:Wall001, !- Name Wall, !- Surface Type EXTWALL80, !- Construction Name EAST ZONE, !- Zone Name Outdoors, !- Outside Boundary Condition , !- Outside Boundary Condition Object SunExposed, !- Sun Exposure WindExposed, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 12.19200,6.096000,3.048000, !- X,Y,Z ==> Vertex 1 {m} 12.19200,6.096000,0, !- X,Y,Z ==> Vertex 2 {m} 9.144000,6.096000,0, !- X,Y,Z ==> Vertex 3 {m} 9.144000,6.096000,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn002:Wall002, !- Name Wall, !- Surface Type EXTWALL80, !- Construction Name EAST ZONE, !- Zone Name Outdoors, !- Outside Boundary Condition , !- Outside Boundary Condition Object SunExposed, !- Sun Exposure WindExposed, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 6.096000,0,3.048000, !- X,Y,Z ==> Vertex 1 {m} 6.096000,0,0, !- X,Y,Z ==> Vertex 2 {m} 12.19200,0,0, !- X,Y,Z ==> Vertex 3 {m} 12.19200,0,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn002:Wall003, !- Name Wall, !- Surface Type EXTWALL80, !- Construction Name EAST ZONE, !- Zone Name Outdoors, !- Outside Boundary Condition , !- Outside Boundary Condition Object SunExposed, !- Sun Exposure WindExposed, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 12.19200,0,3.048000, !- X,Y,Z ==> Vertex 1 {m} 12.19200,0,0, !- X,Y,Z ==> Vertex 2 {m} 12.19200,6.096000,0, !- X,Y,Z ==> Vertex 3 {m} 12.19200,6.096000,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn002:Wall004, !- Name Wall, !- Surface Type PARTITION06, !- Construction Name EAST ZONE, !- Zone Name Surface, !- Outside Boundary Condition Zn001:Wall004, !- Outside Boundary Condition Object NoSun, !- Sun Exposure NoWind, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 6.096000,6.096000,3.048000, !- X,Y,Z ==> Vertex 1 {m} 6.096000,6.096000,0, !- X,Y,Z ==> Vertex 2 {m} 6.096000,0,0, !- X,Y,Z ==> Vertex 3 {m} 6.096000,0,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn002:Wall005, !- Name Wall, !- Surface Type PARTITION06, !- Construction Name EAST ZONE, !- Zone Name Surface, !- Outside Boundary Condition Zn003:Wall005, !- Outside Boundary Condition Object NoSun, !- Sun Exposure NoWind, !- Wind Exposure 0.5000000, !- View Factor to Ground 4, !- Number of Vertices 9.144000,6.096000,3.048000, !- X,Y,Z ==> Vertex 1 {m} 9.144000,6.096000,0, !- X,Y,Z ==> Vertex 2 {m} 6.096000,6.096000,0, !- X,Y,Z ==> Vertex 3 {m} 6.096000,6.096000,3.048000; !- X,Y,Z ==> Vertex 4 {m} BuildingSurface:Detailed, Zn002:Flr001, !- Name Floor, !- Surface Type FLOOR SLAB 8 IN, !- Construction Name EAST ZONE, !- Zone Name Surface, !- Outside Boundary Condition Zn002:Flr001, !- Outside Boundary Condition Object NoSun, !- Sun Exposure NoWin", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_case_insensitive.py::test_contains[BUILDING],eppy/tests/test_case_insensitive.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_get_and_set(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") buildings = idf.idfobjects[""building""] assert len(buildings) == 1 @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_contains(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_del(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects del idf.idfobjects[""building""] assert key not in idf.idfobjects ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_case_insensitive.py::test_contains[BuIlDiNg],eppy/tests/test_case_insensitive.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_get_and_set(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") buildings = idf.idfobjects[""building""] assert len(buildings) == 1 @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_contains(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_del(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects del idf.idfobjects[""building""] assert key not in idf.idfobjects ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_case_insensitive.py::test_contains[Building],eppy/tests/test_case_insensitive.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_get_and_set(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") buildings = idf.idfobjects[""building""] assert len(buildings) == 1 @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_contains(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_del(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects del idf.idfobjects[""building""] assert key not in idf.idfobjects ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_case_insensitive.py::test_contains[building],eppy/tests/test_case_insensitive.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_get_and_set(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") buildings = idf.idfobjects[""building""] assert len(buildings) == 1 @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_contains(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_del(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects del idf.idfobjects[""building""] assert key not in idf.idfobjects ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_case_insensitive.py::test_del[BUILDING],eppy/tests/test_case_insensitive.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_get_and_set(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") buildings = idf.idfobjects[""building""] assert len(buildings) == 1 @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_contains(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_del(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects del idf.idfobjects[""building""] assert key not in idf.idfobjects ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_case_insensitive.py::test_del[BuIlDiNg],eppy/tests/test_case_insensitive.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_get_and_set(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") buildings = idf.idfobjects[""building""] assert len(buildings) == 1 @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_contains(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_del(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects del idf.idfobjects[""building""] assert key not in idf.idfobjects ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_case_insensitive.py::test_del[Building],eppy/tests/test_case_insensitive.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_get_and_set(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") buildings = idf.idfobjects[""building""] assert len(buildings) == 1 @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_contains(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_del(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects del idf.idfobjects[""building""] assert key not in idf.idfobjects ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_case_insensitive.py::test_del[building],eppy/tests/test_case_insensitive.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_get_and_set(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") buildings = idf.idfobjects[""building""] assert len(buildings) == 1 @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_contains(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_del(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects del idf.idfobjects[""building""] assert key not in idf.idfobjects ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_case_insensitive.py::test_get_and_set[BUILDING],eppy/tests/test_case_insensitive.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_get_and_set(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") buildings = idf.idfobjects[""building""] assert len(buildings) == 1 @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_contains(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_del(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects del idf.idfobjects[""building""] assert key not in idf.idfobjects ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_case_insensitive.py::test_get_and_set[BuIlDiNg],eppy/tests/test_case_insensitive.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_get_and_set(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") buildings = idf.idfobjects[""building""] assert len(buildings) == 1 @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_contains(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_del(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects del idf.idfobjects[""building""] assert key not in idf.idfobjects ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_case_insensitive.py::test_get_and_set[Building],eppy/tests/test_case_insensitive.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_get_and_set(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") buildings = idf.idfobjects[""building""] assert len(buildings) == 1 @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_contains(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_del(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects del idf.idfobjects[""building""] assert key not in idf.idfobjects ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_case_insensitive.py::test_get_and_set[building],eppy/tests/test_case_insensitive.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_get_and_set(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") buildings = idf.idfobjects[""building""] assert len(buildings) == 1 @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_contains(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects @pytest.mark.parametrize(""key"", [""BUILDING"", ""Building"", ""building"", ""BuIlDiNg""]) def test_del(base_idf, key): idf = base_idf idf.newidfobject(key, Name=""Building"") assert key in idf.idfobjects del idf.idfobjects[""building""] assert key not in idf.idfobjects ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_examples.py::test_addobject,eppy/tests/test_examples.py,NIO,flaky,Opened,https://github.com/santoshphilip/eppy/pull/355,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for examples. (ex_*.py files)"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from eppy.idfreader import idfreader import eppy.snippet as snippet import os import eppy.pytest_helpers as pytest_helpers import eppy.modeleditor as modeleditor # iddfile = ""../iddfiles/Energy+V7_0_0_036.idd"" # fname = ""../idffiles/V_7_0/5ZoneSupRetPlenRAB.idf"" # iddsnippet = snippet.iddsnippet from eppy.iddcurrent import iddcurrent iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet from six import StringIO idffhandle = StringIO(idfsnippet) iddfhandle = StringIO(iddsnippet) bunchdt, data, commdct, idd_index = idfreader(idffhandle, iddfhandle) def test_readwrite(): """"""py.test for ex_readwrite"""""" txt = str(data) head = ""Zone,\n PLENUM-1,\n 0.0,\n 0.0,\n 0.0,\n 0.0,\n 1,\n 1,\n 0.609600067,\n 283.2;\n\n"" tail = ""\n\nBuildingSurface:Detailed,\n WALL-1PF,\n WALL,\n WALL-1,\n PLENUM-1,\n Outdoors,\n ,\n SunExposed,\n WindExposed,\n 0.5,\n 4,\n 0.0,\n 0.0,\n 3.0,\n 0.0,\n 0.0,\n 2.4,\n 30.5,\n 0.0,\n 2.4,\n 30.5,\n 0.0,\n 3.0;\n\n"" # assert head == txt[:108] # assert tail == txt[-280:] def test_pythonic(): """"""py.test for ex_pythonic.py"""""" zones = bunchdt[""zone""] # all the zones zone0 = zones[0] # - printout = ""PLENUM-1"" assert zone0.Name == printout # - printout = [ ""PLENUM-1"", ""SPACE1-1"", ""SPACE2-1"", ""SPACE3-1"", ""SPACE4-1"", ""SPACE5-1"", ""Sup-PLENUM-1"", ] zonenames = [zone.Name for zone in zones] assert printout == zonenames # - printout = [ ""283.2"", ""239.247360229"", ""103.311355591"", ""239.247360229"", ""103.311355591"", ""447.682556152"", ""208.6"", ] zonevolumes = [zone.Volume for zone in zones] for item1, item2 in zip(printout, zonevolumes): item1, item2 = float(item1), float(item2) assert pytest_helpers.almostequal(item1, item2) # - printout = [(""SPACE2-1"", ""103.311355591""), (""SPACE4-1"", ""103.311355591"")] smallzones = [zn for zn in zones if float(zn.Volume) < 150] namevolume = [(zn.Name, zn.Volume) for zn in smallzones] for (n1, v1), (n2, v2) in zip(printout, namevolume): (n1, v1) = (n1, float(v1)) (n2, v2) = (n2, float(v2)) assert n1 == n2 assert pytest_helpers.almostequal(v1, v2) # - printout = 2 assert printout == len(smallzones) # - printout = [ ""PLENUM-1"", ""SPACE1-1"", ""FIRST-SMALL-ZONE"", ""SPACE3-1"", ""SECOND-SMALL-ZONE"", ""SPACE5-1"", ""Sup-PLENUM-1"", ] smallzones[0].Name = ""FIRST-SMALL-ZONE"" smallzones[1].Name = ""SECOND-SMALL-ZONE"" # now the zone names are: zonenames = [zone.Name for zone in zones] assert printout == zonenames def test_addobject(): """"""py.test for ex_addobject.py"""""" zones = bunchdt[""zone""] # all the zones assert len(zones) == 7 modeleditor.addobject(bunchdt, data, commdct, ""Zone"".upper(), None, aname=""NewZone"") assert len(zones) == 8 assert zones[-1].obj == [ ""ZONE"", ""NewZone"", 0.0, 0.0, 0.0, 0.0, 1, 1, ""autocalculate"", ""autocalculate"", ""autocalculate"", """", """", ""Yes"", ] def test_functions(): """"""py.test for ex_functions.py"""""" surfaces = bunchdt[""BuildingSurface:Detailed""] # all the surfaces assert len(surfaces) == 1 surface = surfaces[0] assert surface.Name == ""WALL-1PF"" assert surface.azimuth == 180.0 assert surface.tilt == 90.0 assert pytest_helpers.almostequal(surface.area, 18.3) ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_examples.py::test_pythonic,eppy/tests/test_examples.py,NIO,flaky,Opened,https://github.com/santoshphilip/eppy/pull/356,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for examples. (ex_*.py files)"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from eppy.idfreader import idfreader import eppy.snippet as snippet import os import eppy.pytest_helpers as pytest_helpers import eppy.modeleditor as modeleditor # iddfile = ""../iddfiles/Energy+V7_0_0_036.idd"" # fname = ""../idffiles/V_7_0/5ZoneSupRetPlenRAB.idf"" # iddsnippet = snippet.iddsnippet from eppy.iddcurrent import iddcurrent iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet from six import StringIO idffhandle = StringIO(idfsnippet) iddfhandle = StringIO(iddsnippet) bunchdt, data, commdct, idd_index = idfreader(idffhandle, iddfhandle) def test_readwrite(): """"""py.test for ex_readwrite"""""" txt = str(data) head = ""Zone,\n PLENUM-1,\n 0.0,\n 0.0,\n 0.0,\n 0.0,\n 1,\n 1,\n 0.609600067,\n 283.2;\n\n"" tail = ""\n\nBuildingSurface:Detailed,\n WALL-1PF,\n WALL,\n WALL-1,\n PLENUM-1,\n Outdoors,\n ,\n SunExposed,\n WindExposed,\n 0.5,\n 4,\n 0.0,\n 0.0,\n 3.0,\n 0.0,\n 0.0,\n 2.4,\n 30.5,\n 0.0,\n 2.4,\n 30.5,\n 0.0,\n 3.0;\n\n"" # assert head == txt[:108] # assert tail == txt[-280:] def test_pythonic(): """"""py.test for ex_pythonic.py"""""" zones = bunchdt[""zone""] # all the zones zone0 = zones[0] # - printout = ""PLENUM-1"" assert zone0.Name == printout # - printout = [ ""PLENUM-1"", ""SPACE1-1"", ""SPACE2-1"", ""SPACE3-1"", ""SPACE4-1"", ""SPACE5-1"", ""Sup-PLENUM-1"", ] zonenames = [zone.Name for zone in zones] assert printout == zonenames # - printout = [ ""283.2"", ""239.247360229"", ""103.311355591"", ""239.247360229"", ""103.311355591"", ""447.682556152"", ""208.6"", ] zonevolumes = [zone.Volume for zone in zones] for item1, item2 in zip(printout, zonevolumes): item1, item2 = float(item1), float(item2) assert pytest_helpers.almostequal(item1, item2) # - printout = [(""SPACE2-1"", ""103.311355591""), (""SPACE4-1"", ""103.311355591"")] smallzones = [zn for zn in zones if float(zn.Volume) < 150] namevolume = [(zn.Name, zn.Volume) for zn in smallzones] for (n1, v1), (n2, v2) in zip(printout, namevolume): (n1, v1) = (n1, float(v1)) (n2, v2) = (n2, float(v2)) assert n1 == n2 assert pytest_helpers.almostequal(v1, v2) # - printout = 2 assert printout == len(smallzones) # - printout = [ ""PLENUM-1"", ""SPACE1-1"", ""FIRST-SMALL-ZONE"", ""SPACE3-1"", ""SECOND-SMALL-ZONE"", ""SPACE5-1"", ""Sup-PLENUM-1"", ] smallzones[0].Name = ""FIRST-SMALL-ZONE"" smallzones[1].Name = ""SECOND-SMALL-ZONE"" # now the zone names are: zonenames = [zone.Name for zone in zones] assert printout == zonenames def test_addobject(): """"""py.test for ex_addobject.py"""""" zones = bunchdt[""zone""] # all the zones assert len(zones) == 7 modeleditor.addobject(bunchdt, data, commdct, ""Zone"".upper(), None, aname=""NewZone"") assert len(zones) == 8 assert zones[-1].obj == [ ""ZONE"", ""NewZone"", 0.0, 0.0, 0.0, 0.0, 1, 1, ""autocalculate"", ""autocalculate"", ""autocalculate"", """", """", ""Yes"", ] def test_functions(): """"""py.test for ex_functions.py"""""" surfaces = bunchdt[""BuildingSurface:Detailed""] # all the surfaces assert len(surfaces) == 1 surface = surfaces[0] assert surface.Name == ""WALL-1PF"" assert surface.azimuth == 180.0 assert surface.tilt == 90.0 assert pytest_helpers.almostequal(surface.area, 18.3) ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_fanpower.py::testfanpower_bhp,eppy/tests/test_fanpower.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2017 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for fanpower.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.fanpower as fanpower from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_pascal2inh2o(): """"""py.test for pascal2inh2o and inh2o2pascal"""""" data = ( (1, 0.00401865), # pascal, expected (186.816675, 0.7507501808391), # pascal, expected ) for pascal, expected in data: result = fanpower.pascal2inh2o(pascal) assert almostequal(result, expected, places=5) data = ( (1, 0.00401865), # expected, inh20 (186.816675, 0.7507501808391), # expected, inh20 ) for expected, inh20 in data: result = fanpower.inh2o2pascal(inh20) assert almostequal(result, expected, places=3) def test_m3s2cfm(): """"""py.test for m3s2cfm and cfm2m3s"""""" data = ( (1, 2118.880003), # m3s, expected (1.28442384, 2721.539989952472), # m3s, expected ) for m3s, expected in data: result = fanpower.m3s2cfm(m3s) assert result == expected data = ( (1, 2118.880003), # expected, cfm (1.28442384, 2721.539989952472), # expected, cfm ) for expected, cfm in data: result = fanpower.cfm2m3s(cfm) assert result == expected def test_fan_bhp(): """"""py.test for fan_bhp"""""" data = ( ( 0.342, 186.816675, 1.28442384, 0.939940898974, ), # fan_tot_eff, pascal, m3s, expected ( 0.537, 871.81115, 2.44326719, 5.31400249068, ), # fan_tot_eff, pascal, m3s, expected ) for fan_tot_eff, pascal, m3s, expected in data: result = fanpower.fan_bhp(fan_tot_eff, pascal, m3s) assert almostequal(result, expected) def test_bhp2watts(): """"""py.test for bhp2watts and watts2bhp"""""" data = ( (0.939940898974, 700.9139283649118), # bhp, expected (5.31400249068, 3962.6516573000763), # bhp, expected ) for bhp, expected in data: result = fanpower.bhp2watts(bhp) assert result == expected data = ( (0.939940898974, 700.9139283649118), # expected, watts (5.31400249068, 3962.6516573000763), # expected, watts ) for expected, watts in data: result = fanpower.watts2bhp(watts) assert result == expected def test_fan_watts(): """"""py.test for fan_watts"""""" data = ( ( 0.342, 186.816675, 1.28442384, 700.9139283649118, ), # fan_tot_eff, pascal, m3s, expected ( 0.537, 871.81115, 2.44326719, 3962.6516573000763, ), # fan_tot_eff, pascal, m3s, expected ) for fan_tot_eff, pascal, m3s, expected in data: result = fanpower.fan_watts(fan_tot_eff, pascal, m3s) assert almostequal(result, expected) vavfan = """"""Fan:VariableVolume, Fan 2, !- Name OfficeHVACAvail, !- Availability Schedule Name 0.519, !- Fan Total Efficiency 164.3824832215, !- Pressure Rise {Pa} 5.6633693184, !- Maximum Flow Rate {m3/s} FixedFlowRate, !- Fan Power Minimum Flow Rate Input Method 0, !- Fan Power Minimum Flow Fraction 1.4158423296, !- Fan Power Minimum Air Flow Rate {m3/s} 0.865, !- Motor Efficiency 1, !- Motor In Airstream Fraction 0.04076, !- Fan Power Coefficient 1 0.088045, !- Fan Power Coefficient 2 -0.072926, !- Fan Power Coefficient 3 0.94374, !- Fan Power Coefficient 4 0, !- Fan Power Coefficient 5 AC(1)(2) Supply Side (Return Air) Inlet Node, !- Air Inlet Node Name Fan 2 Outlet Node; !- Air Outlet Node Name """""" def testfanpower_bhp(): """"""py.test for fanpower_bhp in idf"""""" idf = IDF(StringIO(vavfan)) thefans = idf.idfobjects[""Fan:VariableVolume"".upper()] thefan = thefans[0] bhp = thefan.f_fanpower_bhp assert almostequal(bhp, 2.40306611606) # test autosize thefan.Maximum_Flow_Rate = ""autosize"" bhp = thefan.f_fanpower_bhp assert bhp == ""autosize"" def testfanpower_watts(): """"""py.test for fanpower_watts in idf"""""" idf = IDF(StringIO(vavfan)) thefans = idf.idfobjects[""Fan:VariableVolume"".upper()] thefan = thefans[0] watts = thefan.f_fanpower_watts assert almostequal(watts, 1791.9664027495671) # test autosize thefan.Maximum_Flow_Rate = ""autosize"" watts = thefan.f_fanpower_watts assert watts == ""autosize"" def test_fan_maxcfm(): """"""py.test for fan_maxcfm in idf"""""" idf = IDF(StringIO(vavfan)) thefans = idf.idfobjects[""Fan:VariableVolume"".upper()] thefan = thefans[0] cfm = thefan.f_fan_maxcfm assert almostequal(cfm, 12000, places=5) # test autosize thefan.Maximum_Flow_Rate = ""autosize"" watts = thefan.f_fanpower_watts assert watts == ""autosize"" def test_bhp2pascal(): """"""py.test for bhp2pascal"""""" bhp = 10.182489271962908 cfm = 74999.99998975938 fan_tot_eff = 0.58 exp_pascal, exp_m3s = (124.544455, 35.39605824) result_pascal, result_m3s = fanpower.bhp2pascal(bhp, cfm, fan_tot_eff) assert almostequal(result_pascal, exp_pascal) assert almostequal(result_m3s, exp_m3s) def test_watts2pascal(): """"""py.test for watts2pascal"""""" watts = 7593.0822501027405 cfm = 74999.99998975938 fan_tot_eff = 0.58 exp_pascal, exp_m3s = (124.544455, 35.39605824) result_pascal, result_m3s = fanpower.watts2pascal(watts, cfm, fan_tot_eff) assert almostequal(result_pascal, exp_pascal) assert almostequal(result_m3s, exp_m3s) ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_fanpower.py::testfanpower_watts,eppy/tests/test_fanpower.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2017 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for fanpower.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.fanpower as fanpower from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_pascal2inh2o(): """"""py.test for pascal2inh2o and inh2o2pascal"""""" data = ( (1, 0.00401865), # pascal, expected (186.816675, 0.7507501808391), # pascal, expected ) for pascal, expected in data: result = fanpower.pascal2inh2o(pascal) assert almostequal(result, expected, places=5) data = ( (1, 0.00401865), # expected, inh20 (186.816675, 0.7507501808391), # expected, inh20 ) for expected, inh20 in data: result = fanpower.inh2o2pascal(inh20) assert almostequal(result, expected, places=3) def test_m3s2cfm(): """"""py.test for m3s2cfm and cfm2m3s"""""" data = ( (1, 2118.880003), # m3s, expected (1.28442384, 2721.539989952472), # m3s, expected ) for m3s, expected in data: result = fanpower.m3s2cfm(m3s) assert result == expected data = ( (1, 2118.880003), # expected, cfm (1.28442384, 2721.539989952472), # expected, cfm ) for expected, cfm in data: result = fanpower.cfm2m3s(cfm) assert result == expected def test_fan_bhp(): """"""py.test for fan_bhp"""""" data = ( ( 0.342, 186.816675, 1.28442384, 0.939940898974, ), # fan_tot_eff, pascal, m3s, expected ( 0.537, 871.81115, 2.44326719, 5.31400249068, ), # fan_tot_eff, pascal, m3s, expected ) for fan_tot_eff, pascal, m3s, expected in data: result = fanpower.fan_bhp(fan_tot_eff, pascal, m3s) assert almostequal(result, expected) def test_bhp2watts(): """"""py.test for bhp2watts and watts2bhp"""""" data = ( (0.939940898974, 700.9139283649118), # bhp, expected (5.31400249068, 3962.6516573000763), # bhp, expected ) for bhp, expected in data: result = fanpower.bhp2watts(bhp) assert result == expected data = ( (0.939940898974, 700.9139283649118), # expected, watts (5.31400249068, 3962.6516573000763), # expected, watts ) for expected, watts in data: result = fanpower.watts2bhp(watts) assert result == expected def test_fan_watts(): """"""py.test for fan_watts"""""" data = ( ( 0.342, 186.816675, 1.28442384, 700.9139283649118, ), # fan_tot_eff, pascal, m3s, expected ( 0.537, 871.81115, 2.44326719, 3962.6516573000763, ), # fan_tot_eff, pascal, m3s, expected ) for fan_tot_eff, pascal, m3s, expected in data: result = fanpower.fan_watts(fan_tot_eff, pascal, m3s) assert almostequal(result, expected) vavfan = """"""Fan:VariableVolume, Fan 2, !- Name OfficeHVACAvail, !- Availability Schedule Name 0.519, !- Fan Total Efficiency 164.3824832215, !- Pressure Rise {Pa} 5.6633693184, !- Maximum Flow Rate {m3/s} FixedFlowRate, !- Fan Power Minimum Flow Rate Input Method 0, !- Fan Power Minimum Flow Fraction 1.4158423296, !- Fan Power Minimum Air Flow Rate {m3/s} 0.865, !- Motor Efficiency 1, !- Motor In Airstream Fraction 0.04076, !- Fan Power Coefficient 1 0.088045, !- Fan Power Coefficient 2 -0.072926, !- Fan Power Coefficient 3 0.94374, !- Fan Power Coefficient 4 0, !- Fan Power Coefficient 5 AC(1)(2) Supply Side (Return Air) Inlet Node, !- Air Inlet Node Name Fan 2 Outlet Node; !- Air Outlet Node Name """""" def testfanpower_bhp(): """"""py.test for fanpower_bhp in idf"""""" idf = IDF(StringIO(vavfan)) thefans = idf.idfobjects[""Fan:VariableVolume"".upper()] thefan = thefans[0] bhp = thefan.f_fanpower_bhp assert almostequal(bhp, 2.40306611606) # test autosize thefan.Maximum_Flow_Rate = ""autosize"" bhp = thefan.f_fanpower_bhp assert bhp == ""autosize"" def testfanpower_watts(): """"""py.test for fanpower_watts in idf"""""" idf = IDF(StringIO(vavfan)) thefans = idf.idfobjects[""Fan:VariableVolume"".upper()] thefan = thefans[0] watts = thefan.f_fanpower_watts assert almostequal(watts, 1791.9664027495671) # test autosize thefan.Maximum_Flow_Rate = ""autosize"" watts = thefan.f_fanpower_watts assert watts == ""autosize"" def test_fan_maxcfm(): """"""py.test for fan_maxcfm in idf"""""" idf = IDF(StringIO(vavfan)) thefans = idf.idfobjects[""Fan:VariableVolume"".upper()] thefan = thefans[0] cfm = thefan.f_fan_maxcfm assert almostequal(cfm, 12000, places=5) # test autosize thefan.Maximum_Flow_Rate = ""autosize"" watts = thefan.f_fanpower_watts assert watts == ""autosize"" def test_bhp2pascal(): """"""py.test for bhp2pascal"""""" bhp = 10.182489271962908 cfm = 74999.99998975938 fan_tot_eff = 0.58 exp_pascal, exp_m3s = (124.544455, 35.39605824) result_pascal, result_m3s = fanpower.bhp2pascal(bhp, cfm, fan_tot_eff) assert almostequal(result_pascal, exp_pascal) assert almostequal(result_m3s, exp_m3s) def test_watts2pascal(): """"""py.test for watts2pascal"""""" watts = 7593.0822501027405 cfm = 74999.99998975938 fan_tot_eff = 0.58 exp_pascal, exp_m3s = (124.544455, 35.39605824) result_pascal, result_m3s = fanpower.watts2pascal(watts, cfm, fan_tot_eff) assert almostequal(result_pascal, exp_pascal) assert almostequal(result_m3s, exp_m3s) ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_fanpower.py::test_fan_maxcfm,eppy/tests/test_fanpower.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2017 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for fanpower.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.fanpower as fanpower from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_pascal2inh2o(): """"""py.test for pascal2inh2o and inh2o2pascal"""""" data = ( (1, 0.00401865), # pascal, expected (186.816675, 0.7507501808391), # pascal, expected ) for pascal, expected in data: result = fanpower.pascal2inh2o(pascal) assert almostequal(result, expected, places=5) data = ( (1, 0.00401865), # expected, inh20 (186.816675, 0.7507501808391), # expected, inh20 ) for expected, inh20 in data: result = fanpower.inh2o2pascal(inh20) assert almostequal(result, expected, places=3) def test_m3s2cfm(): """"""py.test for m3s2cfm and cfm2m3s"""""" data = ( (1, 2118.880003), # m3s, expected (1.28442384, 2721.539989952472), # m3s, expected ) for m3s, expected in data: result = fanpower.m3s2cfm(m3s) assert result == expected data = ( (1, 2118.880003), # expected, cfm (1.28442384, 2721.539989952472), # expected, cfm ) for expected, cfm in data: result = fanpower.cfm2m3s(cfm) assert result == expected def test_fan_bhp(): """"""py.test for fan_bhp"""""" data = ( ( 0.342, 186.816675, 1.28442384, 0.939940898974, ), # fan_tot_eff, pascal, m3s, expected ( 0.537, 871.81115, 2.44326719, 5.31400249068, ), # fan_tot_eff, pascal, m3s, expected ) for fan_tot_eff, pascal, m3s, expected in data: result = fanpower.fan_bhp(fan_tot_eff, pascal, m3s) assert almostequal(result, expected) def test_bhp2watts(): """"""py.test for bhp2watts and watts2bhp"""""" data = ( (0.939940898974, 700.9139283649118), # bhp, expected (5.31400249068, 3962.6516573000763), # bhp, expected ) for bhp, expected in data: result = fanpower.bhp2watts(bhp) assert result == expected data = ( (0.939940898974, 700.9139283649118), # expected, watts (5.31400249068, 3962.6516573000763), # expected, watts ) for expected, watts in data: result = fanpower.watts2bhp(watts) assert result == expected def test_fan_watts(): """"""py.test for fan_watts"""""" data = ( ( 0.342, 186.816675, 1.28442384, 700.9139283649118, ), # fan_tot_eff, pascal, m3s, expected ( 0.537, 871.81115, 2.44326719, 3962.6516573000763, ), # fan_tot_eff, pascal, m3s, expected ) for fan_tot_eff, pascal, m3s, expected in data: result = fanpower.fan_watts(fan_tot_eff, pascal, m3s) assert almostequal(result, expected) vavfan = """"""Fan:VariableVolume, Fan 2, !- Name OfficeHVACAvail, !- Availability Schedule Name 0.519, !- Fan Total Efficiency 164.3824832215, !- Pressure Rise {Pa} 5.6633693184, !- Maximum Flow Rate {m3/s} FixedFlowRate, !- Fan Power Minimum Flow Rate Input Method 0, !- Fan Power Minimum Flow Fraction 1.4158423296, !- Fan Power Minimum Air Flow Rate {m3/s} 0.865, !- Motor Efficiency 1, !- Motor In Airstream Fraction 0.04076, !- Fan Power Coefficient 1 0.088045, !- Fan Power Coefficient 2 -0.072926, !- Fan Power Coefficient 3 0.94374, !- Fan Power Coefficient 4 0, !- Fan Power Coefficient 5 AC(1)(2) Supply Side (Return Air) Inlet Node, !- Air Inlet Node Name Fan 2 Outlet Node; !- Air Outlet Node Name """""" def testfanpower_bhp(): """"""py.test for fanpower_bhp in idf"""""" idf = IDF(StringIO(vavfan)) thefans = idf.idfobjects[""Fan:VariableVolume"".upper()] thefan = thefans[0] bhp = thefan.f_fanpower_bhp assert almostequal(bhp, 2.40306611606) # test autosize thefan.Maximum_Flow_Rate = ""autosize"" bhp = thefan.f_fanpower_bhp assert bhp == ""autosize"" def testfanpower_watts(): """"""py.test for fanpower_watts in idf"""""" idf = IDF(StringIO(vavfan)) thefans = idf.idfobjects[""Fan:VariableVolume"".upper()] thefan = thefans[0] watts = thefan.f_fanpower_watts assert almostequal(watts, 1791.9664027495671) # test autosize thefan.Maximum_Flow_Rate = ""autosize"" watts = thefan.f_fanpower_watts assert watts == ""autosize"" def test_fan_maxcfm(): """"""py.test for fan_maxcfm in idf"""""" idf = IDF(StringIO(vavfan)) thefans = idf.idfobjects[""Fan:VariableVolume"".upper()] thefan = thefans[0] cfm = thefan.f_fan_maxcfm assert almostequal(cfm, 12000, places=5) # test autosize thefan.Maximum_Flow_Rate = ""autosize"" watts = thefan.f_fanpower_watts assert watts == ""autosize"" def test_bhp2pascal(): """"""py.test for bhp2pascal"""""" bhp = 10.182489271962908 cfm = 74999.99998975938 fan_tot_eff = 0.58 exp_pascal, exp_m3s = (124.544455, 35.39605824) result_pascal, result_m3s = fanpower.bhp2pascal(bhp, cfm, fan_tot_eff) assert almostequal(result_pascal, exp_pascal) assert almostequal(result_m3s, exp_m3s) def test_watts2pascal(): """"""py.test for watts2pascal"""""" watts = 7593.0822501027405 cfm = 74999.99998975938 fan_tot_eff = 0.58 exp_pascal, exp_m3s = (124.544455, 35.39605824) result_pascal, result_m3s = fanpower.watts2pascal(watts, cfm, fan_tot_eff) assert almostequal(result_pascal, exp_pascal) assert almostequal(result_m3s, exp_m3s) ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_componentsintobranch,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_connectcomponents,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_getbranchcomponents,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_getfieldnamesendswith,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_getnodefieldname,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_initinletoutlet,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_makecondenserloop,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_makeductbranch,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_makeductcomponent,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_makepipebranch,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_makepipecomponent,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_makeplantloop,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_renamenodes,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_hvacbuilder.py::test_replacebranch,eppy/tests/test_hvacbuilder.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for hvacbuilder"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.hvacbuilder as hvacbuilder from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF # idd is read only once in this test # if it has already been read from some other test, it will continue with the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_flattencopy(): """"""py.test for flattencopy"""""" tdata = ( ([1, 2], [1, 2]), # lst , nlst ([1, 2, [3, 4]], [1, 2, 3, 4]), # lst , nlst ([1, 2, [3, [4, 5, 6], 7, 8]], [1, 2, 3, 4, 5, 6, 7, 8]), # lst , nlst ([1, 2, [3, [4, 5, [6, 7], 8], 9]], [1, 2, 3, 4, 5, 6, 7, 8, 9]), # lst , nlst ) for lst, nlst in tdata: result = hvacbuilder.flattencopy(lst) assert result == nlst def test_makeplantloop(): """"""pytest for makeplantloop"""""" tdata = ( ( """", ""p_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet, Bypass;BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass;BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass;BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass;BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet, Bypass;BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet, Bypass;BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass;BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass;BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass;BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet, Bypass;BRANCHLIST, p_loop Supply Branchs, sb0, sb1, sb2, sb3, sb4;BRANCHLIST, p_loop Demand Branchs, db0, db1, db2, db3, db4;CONNECTOR:SPLITTER, p_loop_supply_splitter, sb0, sb1, sb2, sb3;CONNECTOR:SPLITTER, p_loop_demand_splitter, db0, db1, db2, db3;CONNECTOR:MIXER, p_loop_supply_mixer, sb4, sb1, sb2, sb3;CONNECTOR:MIXER, p_loop_demand_mixer, db4, db1, db2, db3;CONNECTORLIST, p_loop Supply Connectors, Connector:Splitter, p_loop_supply_splitter, Connector:Mixer, p_loop_supply_mixer;CONNECTORLIST, p_loop Demand Connectors, Connector:Splitter, p_loop_demand_splitter, Connector:Mixer, p_loop_demand_mixer;PIPE:ADIABATIC, sb0_pipe, p_loop Supply Inlet, sb0_pipe_outlet;PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet;PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet;PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet;PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, p_loop Supply Outlet;PIPE:ADIABATIC, db0_pipe, p_loop Demand Inlet, db0_pipe_outlet;PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet;PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet;PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet;PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, p_loop Demand Outlet;PLANTLOOP, p_loop, Water, , , , , , , 0.0, Autocalculate, p_loop Supply Inlet, p_loop Supply Outlet, p_loop Supply Branchs, p_loop Supply Connectors, p_loop Demand Inlet, p_loop Demand Outlet, p_loop Demand Branchs, p_loop Demand Connectors, Sequential, , SingleSetpoint, None, None;"""""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""p_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makeplantloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) # print('=' * 15) # print(idf1.model) # print('-' * 15) # print(idf2.model) # print('=' * 15) assert str(idf1.model) == str(idf2.model) def test_makecondenserloop(): """"""pytest for makecondenserloop"""""" tdata = ( ( """", ""c_loop"", [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""], [""db0"", [""db1"", ""db2"", ""db3""], ""db4""], """"""BRANCH, sb0, 0.0, , Pipe:Adiabatic, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet, Bypass; BRANCH, sb1, 0.0, , Pipe:Adiabatic, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet, Bypass; BRANCH, sb2, 0.0, , Pipe:Adiabatic, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet, Bypass; BRANCH, sb3, 0.0, , Pipe:Adiabatic, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet, Bypass; BRANCH, sb4, 0.0, , Pipe:Adiabatic, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet, Bypass; BRANCH, db0, 0.0, , Pipe:Adiabatic, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet, Bypass; BRANCH, db1, 0.0, , Pipe:Adiabatic, db1_pipe, db1_pipe_inlet, db1_pipe_outlet, Bypass; BRANCH, db2, 0.0, , Pipe:Adiabatic, db2_pipe, db2_pipe_inlet, db2_pipe_outlet, Bypass; BRANCH, db3, 0.0, , Pipe:Adiabatic, db3_pipe, db3_pipe_inlet, db3_pipe_outlet, Bypass; BRANCH, db4, 0.0, , Pipe:Adiabatic, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet, Bypass; BRANCHLIST, c_loop Cond_Supply Branchs, sb0, sb1, sb2, sb3, sb4; BRANCHLIST, c_loop Condenser Demand Branchs, db0, db1, db2, db3, db4; CONNECTOR:SPLITTER, c_loop_supply_splitter, sb0, sb1, sb2, sb3; CONNECTOR:SPLITTER, c_loop_demand_splitter, db0, db1, db2, db3; CONNECTOR:MIXER, c_loop_supply_mixer, sb4, sb1, sb2, sb3; CONNECTOR:MIXER, c_loop_demand_mixer, db4, db1, db2, db3; CONNECTORLIST, c_loop Cond_Supply Connectors, Connector:Splitter, c_loop_supply_splitter, Connector:Mixer, c_loop_supply_mixer; CONNECTORLIST, c_loop Condenser Demand Connectors, Connector:Splitter, c_loop_demand_splitter, Connector:Mixer, c_loop_demand_mixer; PIPE:ADIABATIC, sb0_pipe, c_loop Cond_Supply Inlet, sb0_pipe_outlet; PIPE:ADIABATIC, sb1_pipe, sb1_pipe_inlet, sb1_pipe_outlet; PIPE:ADIABATIC, sb2_pipe, sb2_pipe_inlet, sb2_pipe_outlet; PIPE:ADIABATIC, sb3_pipe, sb3_pipe_inlet, sb3_pipe_outlet; PIPE:ADIABATIC, sb4_pipe, sb4_pipe_inlet, c_loop Cond_Supply Outlet; PIPE:ADIABATIC, db0_pipe, c_loop Demand Inlet, db0_pipe_outlet; PIPE:ADIABATIC, db1_pipe, db1_pipe_inlet, db1_pipe_outlet; PIPE:ADIABATIC, db2_pipe, db2_pipe_inlet, db2_pipe_outlet; PIPE:ADIABATIC, db3_pipe, db3_pipe_inlet, db3_pipe_outlet; PIPE:ADIABATIC, db4_pipe, db4_pipe_inlet, c_loop Demand Outlet; CONDENSERLOOP, c_loop, Water, , , , , , , 0.0, Autocalculate, c_loop Cond_Supply Inlet, c_loop Cond_Supply Outlet, c_loop Cond_Supply Branchs, c_loop Cond_Supply Connectors, c_loop Demand Inlet, c_loop Demand Outlet, c_loop Condenser Demand Branchs, c_loop Condenser Demand Connectors, Sequential, None; """""", ), # blankidf, loopname, sloop, dloop, nidf ) for blankidf, loopname, sloop, dloop, nidf in tdata: fhandle = StringIO("""") idf1 = IDF(fhandle) loopname = ""c_loop"" sloop = [""sb0"", [""sb1"", ""sb2"", ""sb3""], ""sb4""] dloop = [""db0"", [""db1"", ""db2"", ""db3""], ""db4""] hvacbuilder.makecondenserloop(idf1, loopname, sloop, dloop) idf2 = IDF(StringIO(nidf)) assert str(idf1.model) == str(idf2.model) def test_getbranchcomponents(): """"""py.test for getbranchcomponents"""""" tdata = ( ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; """""", True, [(""PIPE:ADIABATIC"", ""np1""), (""PIPE:ADIABATIC"", ""np2"")], ), # idftxt, utest, componentlist ( """"""BRANCH, sb1, 0.0, , PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node, , PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet, ; PIPE:ADIABATIC, np1, np1_inlet, np1_np2_node; PIPE:ADIABATIC, np2, np1_np2_node, np2_outlet; """""", False, [ [""PIPE:ADIABATIC"", ""np1"", ""np1_inlet"", ""np1_np2_node""], [""PIPE:ADIABATIC"", ""np2"", ""np1_np2_node"", ""np2_outlet""], ], ), # idftxt, utest, componentlist ) for idftxt, utest, componentlist in tdata: fhandle = StringIO(idftxt) idf = IDF(fhandle) branch = idf.idfobjects[""BRANCH""][0] result = hvacbuilder.getbranchcomponents(idf, branch, utest=utest) if utest: assert result == componentlist else: lresult = [item.obj for item in result] ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_IDF.py::TestIDF::test_popidfobject,eppy/tests/test_IDF.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for class IDF"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF def test_IDF(): """"""py.test for class IDF"""""" stored_idd = IDF.iddname IDF.iddname = None assert IDF.iddname == None IDF.setiddname(""gumby"", testing=True) assert IDF.iddname == ""gumby"" IDF.setiddname(""karamba"", testing=True) assert IDF.iddname != ""karamba"" assert IDF.iddname == ""gumby"" IDF.iddname = stored_idd iddsnippet = iddcurrent.iddtxt iddfhandle = StringIO(iddsnippet) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) class TestIDF(object): """"""py.test for IDF function"""""" def test_removeidfobject(self): """"""py.test for IDF.removeidfobject """""" idftxt = """" idfhandle = StringIO(idftxt) idf = IDF(idfhandle) key = ""BUILDING"" idf.newidfobject(key, Name=""Building_remove"") idf.newidfobject(key, Name=""Building1"") idf.newidfobject(key, Name=""Building_remove"") idf.newidfobject(key, Name=""Building2"") buildings = idf.idfobjects[""building""] removethis = buildings[-2] idf.removeidfobject(removethis) assert buildings[2].Name == ""Building2"" assert idf.model.dt[key][2][1] == ""Building2"" def test_popidfobject(self): idftxt = """" idfhandle = StringIO(idftxt) idf = IDF(idfhandle) key = ""BUILDING"" idf.newidfobject(key, Name=""Building_remove"") idf.newidfobject(key, Name=""Building1"") idf.newidfobject(key, Name=""Building_remove"") idf.newidfobject(key, Name=""Building2"") buildings = idf.idfobjects[""building""] removethis = buildings[-2] idf.popidfobject(key, 2) assert buildings[2].Name == ""Building2"" assert idf.model.dt[key][2][1] == ""Building2"" ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_IDF.py::TestIDF::test_removeidfobject,eppy/tests/test_IDF.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for class IDF"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF def test_IDF(): """"""py.test for class IDF"""""" stored_idd = IDF.iddname IDF.iddname = None assert IDF.iddname == None IDF.setiddname(""gumby"", testing=True) assert IDF.iddname == ""gumby"" IDF.setiddname(""karamba"", testing=True) assert IDF.iddname != ""karamba"" assert IDF.iddname == ""gumby"" IDF.iddname = stored_idd iddsnippet = iddcurrent.iddtxt iddfhandle = StringIO(iddsnippet) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) class TestIDF(object): """"""py.test for IDF function"""""" def test_removeidfobject(self): """"""py.test for IDF.removeidfobject """""" idftxt = """" idfhandle = StringIO(idftxt) idf = IDF(idfhandle) key = ""BUILDING"" idf.newidfobject(key, Name=""Building_remove"") idf.newidfobject(key, Name=""Building1"") idf.newidfobject(key, Name=""Building_remove"") idf.newidfobject(key, Name=""Building2"") buildings = idf.idfobjects[""building""] removethis = buildings[-2] idf.removeidfobject(removethis) assert buildings[2].Name == ""Building2"" assert idf.model.dt[key][2][1] == ""Building2"" def test_popidfobject(self): idftxt = """" idfhandle = StringIO(idftxt) idf = IDF(idfhandle) key = ""BUILDING"" idf.newidfobject(key, Name=""Building_remove"") idf.newidfobject(key, Name=""Building1"") idf.newidfobject(key, Name=""Building_remove"") idf.newidfobject(key, Name=""Building2"") buildings = idf.idfobjects[""building""] removethis = buildings[-2] idf.popidfobject(key, 2) assert buildings[2].Name == ""Building2"" assert idf.model.dt[key][2][1] == ""Building2"" ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_idfreader.py::test_convertallfields,eppy/tests/test_idfreader.py,NIO,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""pytest for idfreader. very few tests"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.idfreader as idfreader from eppy.EPlusInterfaceFunctions import readidf from eppy.iddcurrent import iddcurrent iddfhandle = StringIO(iddcurrent.iddtxt) def test_iddversiontuple(): """"""py.test for iddversiontuple"""""" iddtxt = """"""stuff 9.8.4 other stuff"""""" fhandle = StringIO(iddtxt) result = idfreader.iddversiontuple(fhandle) assert result == (9, 8, 4) def test_convertafield(): """"""py.test for convertafield"""""" data = ( ({""type"": ""integer""}, ""1"", ""N1"", 1), # field_comm, field_val, field_iddname, expected ({}, ""1"", ""N1"", 1.0), # field_comm, field_val, field_iddname, expected ({""type"": ""real""}, ""1"", ""N1"", 1.0), # field_comm, field_val, field_iddname, expected ({}, ""autosize"", ""N1"", ""autosize""), ({}, ""4"", ""A1"", ""4""), # field_comm, field_val, field_iddname, expected ) for field_comm, field_val, field_iddname, expected in data: result = idfreader.convertafield(field_comm, field_val, field_iddname) assert result == expected def test_convertallfields(): """"""py.test convertallfields"""""" data = ( (""version, 8.1;"", ""VERSION"", [""version"", ""8.1""]), # idfstr, objkey, expected ( ""WINDOWMATERIAL:SIMPLEGLAZINGSYSTEM, simple, 0.45;"", ""WINDOWMATERIAL:SIMPLEGLAZINGSYSTEM"", [""WINDOWMATERIAL:SIMPLEGLAZINGSYSTEM"", ""simple"", 0.45], ), # idfstr, objkey, expected ( ""HVACTEMPLATE:ZONE:FANCOIL, gumby1, gumby2, 0.45;"", ""HVACTEMPLATE:ZONE:FANCOIL"", [""HVACTEMPLATE:ZONE:FANCOIL"", ""gumby1"", ""gumby2"", 0.45], ), # idfstr, objkey, expected ( ""HVACTEMPLATE:ZONE:FANCOIL, gumby1, gumby2, autosize;"", ""HVACTEMPLATE:ZONE:FANCOIL"", [""HVACTEMPLATE:ZONE:FANCOIL"", ""gumby1"", ""gumby2"", ""autosize""], ), # idfstr, objkey, expected ) commdct = None block = None for idfstr, objkey, expected in data: idfhandle = StringIO(idfstr) block, data, commdct, idd_index = readidf.readdatacommdct1( idfhandle, iddfile=iddfhandle, commdct=commdct, block=block ) idfreader.convertallfields(data, commdct, block) result = data.dt[objkey][0] assert result == expected def test_getextensible(): """"""py.test for getextensible"""""" data = ( ( [ { ""format"": [""singleLine""], ""group"": ""Simulation Parameters"", ""idfobj"": ""Version"", ""memo"": [""Specifies the EnergyPlus version of the IDF file.""], ""unique-object"": [""""], }, {}, {}, {}, ], None, ), # objidd, expected ( [ { ""extensible:2"": [ '- repeat last two fields, remembering to remove ; from ""inner"" fields.' ], ""group"": ""Schedules"", ""idfobj"": ""Schedule:Day:Interval"", ""memo"": [ ""A Schedule:Day:Interval contains a full day of values with specified end times for each value"", ""Currently, is set up to allow for 10 minute intervals for an entire day."", ], ""min-fields"": [""5""], }, {}, {}, {}, ], 2, ), # objidd, expected ) for objidd, expected in data: result = idfreader.getextensible(objidd) assert result == expected def test_endof_extensible(): """"""py.test for endof_extensible"""""" data = ( (1, [""gumby"", ""A1"", ""A2""], [""A2""]), # extensible, thisblock, expected (1, [""gumby"", ""A1"", ""A2"", ""A3""], [""A3""]), # extensible, thisblock, expected ( 2, [""gumby"", ""A1"", ""A2"", ""A3""], [""A2"", ""A3""], ), # extensible, thisblock, expected ) for extensible, thisblock, expected in data: result = idfreader.endof_extensible(extensible, thisblock) assert result == expected def test_extension_of_extensible(): """"""py.test for extension_of_extensible"""""" data = ( ( [ { ""extensible:1"": None, ""group"": ""Schedules"", ""idfobj"": ""Schedule:Day:Interval"", }, {}, {}, {}, ], [""Gumby"", ""A1"", ""A2""], 1, [""A3""], ), # objidd, objblock, n, expected ( [ { ""extensible:1"": None, ""group"": ""Schedules"", ""idfobj"": ""Schedule:Day:Interval"", }, {}, {}, {}, ], [""Gumby"", ""A1"", ""A2""], 2, [""A3"", ""A4""], ), # objidd, objblock, n, expected ( [ { ""extensible:1"": None, ""group"": ""Schedules"", ""idfobj"": ""Schedule:Day:Interval"", }, {}, {}, {}, ], [""Gumby"", ""A1"", ""A2""], 3, [""A3"", ""A4"", ""A5""], ), # objidd, objblock, n, expected ( [ { ""extensible:2"": None, ""group"": ""Schedules"", ""idfobj"": ""Schedule:Day:Interval"", }, {}, {}, {}, ], [""Gumby"", ""A1"", ""A2""], 2, [""A3"", ""A4""], ), # objidd, objblock, n, expected ( [ { ""extensible:4"": None, ""group"": ""Schedules"", ""idfobj"": ""Schedule:Day:Interval"", }, {}, {}, {}, ], [""Gumby"", ""N3"", ""A4"", ""M8"", ""A5""], 4, [""N4"", ""A6"", ""M9"", ""A7""], ), # objidd, objblock, n, expected ( [ { ""extensible:4"": None, ""group"": ""Schedules"", ""idfobj"": ""Schedule:Day:Interval"", }, {}, {}, {}, ], [""Gumby"", ""N3"", ""A4"", ""M8"", ""A5""], 12, [ ""N4"", ""A6"", ""M9"", ""A7"", ""N5"", ""A8"", ""M10"", ""A9"", ""N6"", ""A10"", ""M11"", ""A11"", ], ), # objidd, objblock, n, expected ) for objidd, objblock, n, expected in data: result = idfreader.extension_of_extensible(objidd, objblock, n) assert result == expected ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_idf_helpers.py::test_copyidfintoidf,eppy/tests/test_idf_helpers.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for idf"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import iteritems from six import StringIO from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.idf_helpers as idf_helpers iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_idfobjectkeys(): """"""py.test for idfobjectkeys"""""" expected = [ ""LEAD INPUT"", ""SIMULATION DATA"", ""VERSION"", ""SIMULATIONCONTROL"", ""BUILDING"", ] idf = IDF(StringIO("""")) result = idf_helpers.idfobjectkeys(idf) assert result[:5] == expected def test_getanymentions(): """"""py.test for getanymentions"""""" idf = IDF(StringIO("""")) mat = idf.newidfobject(""MATERIAL"", Name=""mat"") aconst = idf.newidfobject(""CONSTRUCTION"", Name=""const"") foundobjs = idf_helpers.getanymentions(idf, mat) assert len(foundobjs) == 1 assert foundobjs[0] == mat def test_getobject_use_prevfield(): """"""py.test for getobject_use_prevfield"""""" idf = IDF(StringIO("""")) branch = idf.newidfobject( ""BRANCH"", Name=""CW Pump Branch"", Component_1_Object_Type=""Pump:VariableSpeed"", Component_1_Name=""CW Circ Pump"", ) pump = idf.newidfobject(""PUMP:VARIABLESPEED"", Name=""CW Circ Pump"") foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_1_Name"") assert foundobject == pump # test for all times it should return None foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Name"") foundobject = None # prev field not end with Object_Type foundobject = idf_helpers.getobject_use_prevfield( idf, branch, ""Component_11_Object_Type"" ) foundobject = None # field does not end with ""Name"" foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_3_Name"") foundobject = None # bad idfobject key def test_getidfkeyswithnodes(): """"""py.test for getidfkeyswithnodes"""""" nodekeys = idf_helpers.getidfkeyswithnodes() # print(len(nodekeys)) assert ""PLANTLOOP"" in nodekeys assert ""ZONE"" not in nodekeys # def test_a(): # assert 1== 2 def test_getobjectswithnode(): """"""py.test for getobjectswithnode"""""" idf = IDF(StringIO("""")) nodekeys = idf_helpers.getidfkeyswithnodes() plantloop = idf.newidfobject( ""PlantLoop"", Name=""Chilled Water Loop"", Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=""CW Pump Branch"", Component_1_Inlet_Node_Name=""CW Supply Inlet Node"", ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=""CW Circ Pump"", Inlet_Node_Name=""CW Supply Inlet Node"", ) zone = idf.newidfobject(""zone"") foundobjs = idf_helpers.getobjectswithnode(idf, nodekeys, ""CW Supply Inlet Node"") expected = [plantloop, branch, pump] expectedset = set([item.key for item in expected]) resultset = set([item.key for item in foundobjs]) assert resultset == expectedset expectedset = set([item.Name for item in expected]) resultset = set([item.Name for item in foundobjs]) assert resultset == expectedset def test_name2idfobject(): """"""py.test for name2idfobject"""""" idf = IDF(StringIO("""")) plantloopname = ""plantloopname"" branchname = ""branchname"" pumpname = ""pumpname"" zonename = ""zonename"" plantloop = idf.newidfobject( ""PlantLoop"", Name=plantloopname, Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=branchname, Component_1_Inlet_Node_Name=""CW Supply Inlet Node"" ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=pumpname, Inlet_Node_Name=""CW Supply Inlet Node"" ) zone = idf.newidfobject(""zone"", Name=zonename) simulation = idf.newidfobject(""SimulationControl"") # - test names = [plantloopname, branchname, pumpname, zonename] idfobjs = [plantloop, branch, pump, zone] for name, idfobj in zip(names, idfobjs): result = idf_helpers.name2idfobject(idf, Name=name) assert result == idfobj # test when objkeys!=None objkey = ""ZoneHVAC:EquipmentConnections"" equipconnections = idf.newidfobject(objkey, Zone_Name=zonename) result = idf_helpers.name2idfobject(idf, Zone_Name=zonename, objkeys=[objkey]) assert result == equipconnections def test_getidfobjectlist(): """"""py.test for getidfobjectlist"""""" names = [""a"", ""b"", ""c"", ""d"", ""e""] idf = IDF(StringIO("""")) idf.newidfobject(""building"", Name=""a"") idf.newidfobject(""building"", Name=""b"") idf.newidfobject(""Site:Location"", Name=""c"") idf.newidfobject(""ScheduleTypeLimits"", Name=""d"") idf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(idf) assert [res.Name for res in result] == names def test_copyidfintoidf(): """"""py.test for copyidfintoidf"""""" tonames = [""a"", ""b"", ""c""] fromnames = [""d"", ""e""] allnames = [""a"", ""b"", ""c"", ""d"", ""e""] toidf = IDF(StringIO("""")) toidf.newidfobject(""building"", Name=""a"") toidf.newidfobject(""building"", Name=""b"") toidf.newidfobject(""Site:Location"", Name=""c"") result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == tonames fromidf = IDF(StringIO("""")) fromidf.newidfobject(""ScheduleTypeLimits"", Name=""d"") fromidf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(fromidf) assert [res.Name for res in result] == fromnames idf_helpers.copyidfintoidf(toidf, fromidf) result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == allnames ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_idf_helpers.py::test_getanymentions,eppy/tests/test_idf_helpers.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for idf"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import iteritems from six import StringIO from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.idf_helpers as idf_helpers iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_idfobjectkeys(): """"""py.test for idfobjectkeys"""""" expected = [ ""LEAD INPUT"", ""SIMULATION DATA"", ""VERSION"", ""SIMULATIONCONTROL"", ""BUILDING"", ] idf = IDF(StringIO("""")) result = idf_helpers.idfobjectkeys(idf) assert result[:5] == expected def test_getanymentions(): """"""py.test for getanymentions"""""" idf = IDF(StringIO("""")) mat = idf.newidfobject(""MATERIAL"", Name=""mat"") aconst = idf.newidfobject(""CONSTRUCTION"", Name=""const"") foundobjs = idf_helpers.getanymentions(idf, mat) assert len(foundobjs) == 1 assert foundobjs[0] == mat def test_getobject_use_prevfield(): """"""py.test for getobject_use_prevfield"""""" idf = IDF(StringIO("""")) branch = idf.newidfobject( ""BRANCH"", Name=""CW Pump Branch"", Component_1_Object_Type=""Pump:VariableSpeed"", Component_1_Name=""CW Circ Pump"", ) pump = idf.newidfobject(""PUMP:VARIABLESPEED"", Name=""CW Circ Pump"") foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_1_Name"") assert foundobject == pump # test for all times it should return None foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Name"") foundobject = None # prev field not end with Object_Type foundobject = idf_helpers.getobject_use_prevfield( idf, branch, ""Component_11_Object_Type"" ) foundobject = None # field does not end with ""Name"" foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_3_Name"") foundobject = None # bad idfobject key def test_getidfkeyswithnodes(): """"""py.test for getidfkeyswithnodes"""""" nodekeys = idf_helpers.getidfkeyswithnodes() # print(len(nodekeys)) assert ""PLANTLOOP"" in nodekeys assert ""ZONE"" not in nodekeys # def test_a(): # assert 1== 2 def test_getobjectswithnode(): """"""py.test for getobjectswithnode"""""" idf = IDF(StringIO("""")) nodekeys = idf_helpers.getidfkeyswithnodes() plantloop = idf.newidfobject( ""PlantLoop"", Name=""Chilled Water Loop"", Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=""CW Pump Branch"", Component_1_Inlet_Node_Name=""CW Supply Inlet Node"", ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=""CW Circ Pump"", Inlet_Node_Name=""CW Supply Inlet Node"", ) zone = idf.newidfobject(""zone"") foundobjs = idf_helpers.getobjectswithnode(idf, nodekeys, ""CW Supply Inlet Node"") expected = [plantloop, branch, pump] expectedset = set([item.key for item in expected]) resultset = set([item.key for item in foundobjs]) assert resultset == expectedset expectedset = set([item.Name for item in expected]) resultset = set([item.Name for item in foundobjs]) assert resultset == expectedset def test_name2idfobject(): """"""py.test for name2idfobject"""""" idf = IDF(StringIO("""")) plantloopname = ""plantloopname"" branchname = ""branchname"" pumpname = ""pumpname"" zonename = ""zonename"" plantloop = idf.newidfobject( ""PlantLoop"", Name=plantloopname, Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=branchname, Component_1_Inlet_Node_Name=""CW Supply Inlet Node"" ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=pumpname, Inlet_Node_Name=""CW Supply Inlet Node"" ) zone = idf.newidfobject(""zone"", Name=zonename) simulation = idf.newidfobject(""SimulationControl"") # - test names = [plantloopname, branchname, pumpname, zonename] idfobjs = [plantloop, branch, pump, zone] for name, idfobj in zip(names, idfobjs): result = idf_helpers.name2idfobject(idf, Name=name) assert result == idfobj # test when objkeys!=None objkey = ""ZoneHVAC:EquipmentConnections"" equipconnections = idf.newidfobject(objkey, Zone_Name=zonename) result = idf_helpers.name2idfobject(idf, Zone_Name=zonename, objkeys=[objkey]) assert result == equipconnections def test_getidfobjectlist(): """"""py.test for getidfobjectlist"""""" names = [""a"", ""b"", ""c"", ""d"", ""e""] idf = IDF(StringIO("""")) idf.newidfobject(""building"", Name=""a"") idf.newidfobject(""building"", Name=""b"") idf.newidfobject(""Site:Location"", Name=""c"") idf.newidfobject(""ScheduleTypeLimits"", Name=""d"") idf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(idf) assert [res.Name for res in result] == names def test_copyidfintoidf(): """"""py.test for copyidfintoidf"""""" tonames = [""a"", ""b"", ""c""] fromnames = [""d"", ""e""] allnames = [""a"", ""b"", ""c"", ""d"", ""e""] toidf = IDF(StringIO("""")) toidf.newidfobject(""building"", Name=""a"") toidf.newidfobject(""building"", Name=""b"") toidf.newidfobject(""Site:Location"", Name=""c"") result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == tonames fromidf = IDF(StringIO("""")) fromidf.newidfobject(""ScheduleTypeLimits"", Name=""d"") fromidf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(fromidf) assert [res.Name for res in result] == fromnames idf_helpers.copyidfintoidf(toidf, fromidf) result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == allnames ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_idf_helpers.py::test_getidfkeyswithnodes,eppy/tests/test_idf_helpers.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for idf"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import iteritems from six import StringIO from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.idf_helpers as idf_helpers iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_idfobjectkeys(): """"""py.test for idfobjectkeys"""""" expected = [ ""LEAD INPUT"", ""SIMULATION DATA"", ""VERSION"", ""SIMULATIONCONTROL"", ""BUILDING"", ] idf = IDF(StringIO("""")) result = idf_helpers.idfobjectkeys(idf) assert result[:5] == expected def test_getanymentions(): """"""py.test for getanymentions"""""" idf = IDF(StringIO("""")) mat = idf.newidfobject(""MATERIAL"", Name=""mat"") aconst = idf.newidfobject(""CONSTRUCTION"", Name=""const"") foundobjs = idf_helpers.getanymentions(idf, mat) assert len(foundobjs) == 1 assert foundobjs[0] == mat def test_getobject_use_prevfield(): """"""py.test for getobject_use_prevfield"""""" idf = IDF(StringIO("""")) branch = idf.newidfobject( ""BRANCH"", Name=""CW Pump Branch"", Component_1_Object_Type=""Pump:VariableSpeed"", Component_1_Name=""CW Circ Pump"", ) pump = idf.newidfobject(""PUMP:VARIABLESPEED"", Name=""CW Circ Pump"") foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_1_Name"") assert foundobject == pump # test for all times it should return None foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Name"") foundobject = None # prev field not end with Object_Type foundobject = idf_helpers.getobject_use_prevfield( idf, branch, ""Component_11_Object_Type"" ) foundobject = None # field does not end with ""Name"" foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_3_Name"") foundobject = None # bad idfobject key def test_getidfkeyswithnodes(): """"""py.test for getidfkeyswithnodes"""""" nodekeys = idf_helpers.getidfkeyswithnodes() # print(len(nodekeys)) assert ""PLANTLOOP"" in nodekeys assert ""ZONE"" not in nodekeys # def test_a(): # assert 1== 2 def test_getobjectswithnode(): """"""py.test for getobjectswithnode"""""" idf = IDF(StringIO("""")) nodekeys = idf_helpers.getidfkeyswithnodes() plantloop = idf.newidfobject( ""PlantLoop"", Name=""Chilled Water Loop"", Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=""CW Pump Branch"", Component_1_Inlet_Node_Name=""CW Supply Inlet Node"", ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=""CW Circ Pump"", Inlet_Node_Name=""CW Supply Inlet Node"", ) zone = idf.newidfobject(""zone"") foundobjs = idf_helpers.getobjectswithnode(idf, nodekeys, ""CW Supply Inlet Node"") expected = [plantloop, branch, pump] expectedset = set([item.key for item in expected]) resultset = set([item.key for item in foundobjs]) assert resultset == expectedset expectedset = set([item.Name for item in expected]) resultset = set([item.Name for item in foundobjs]) assert resultset == expectedset def test_name2idfobject(): """"""py.test for name2idfobject"""""" idf = IDF(StringIO("""")) plantloopname = ""plantloopname"" branchname = ""branchname"" pumpname = ""pumpname"" zonename = ""zonename"" plantloop = idf.newidfobject( ""PlantLoop"", Name=plantloopname, Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=branchname, Component_1_Inlet_Node_Name=""CW Supply Inlet Node"" ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=pumpname, Inlet_Node_Name=""CW Supply Inlet Node"" ) zone = idf.newidfobject(""zone"", Name=zonename) simulation = idf.newidfobject(""SimulationControl"") # - test names = [plantloopname, branchname, pumpname, zonename] idfobjs = [plantloop, branch, pump, zone] for name, idfobj in zip(names, idfobjs): result = idf_helpers.name2idfobject(idf, Name=name) assert result == idfobj # test when objkeys!=None objkey = ""ZoneHVAC:EquipmentConnections"" equipconnections = idf.newidfobject(objkey, Zone_Name=zonename) result = idf_helpers.name2idfobject(idf, Zone_Name=zonename, objkeys=[objkey]) assert result == equipconnections def test_getidfobjectlist(): """"""py.test for getidfobjectlist"""""" names = [""a"", ""b"", ""c"", ""d"", ""e""] idf = IDF(StringIO("""")) idf.newidfobject(""building"", Name=""a"") idf.newidfobject(""building"", Name=""b"") idf.newidfobject(""Site:Location"", Name=""c"") idf.newidfobject(""ScheduleTypeLimits"", Name=""d"") idf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(idf) assert [res.Name for res in result] == names def test_copyidfintoidf(): """"""py.test for copyidfintoidf"""""" tonames = [""a"", ""b"", ""c""] fromnames = [""d"", ""e""] allnames = [""a"", ""b"", ""c"", ""d"", ""e""] toidf = IDF(StringIO("""")) toidf.newidfobject(""building"", Name=""a"") toidf.newidfobject(""building"", Name=""b"") toidf.newidfobject(""Site:Location"", Name=""c"") result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == tonames fromidf = IDF(StringIO("""")) fromidf.newidfobject(""ScheduleTypeLimits"", Name=""d"") fromidf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(fromidf) assert [res.Name for res in result] == fromnames idf_helpers.copyidfintoidf(toidf, fromidf) result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == allnames ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_idf_helpers.py::test_getidfobjectlist,eppy/tests/test_idf_helpers.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for idf"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import iteritems from six import StringIO from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.idf_helpers as idf_helpers iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_idfobjectkeys(): """"""py.test for idfobjectkeys"""""" expected = [ ""LEAD INPUT"", ""SIMULATION DATA"", ""VERSION"", ""SIMULATIONCONTROL"", ""BUILDING"", ] idf = IDF(StringIO("""")) result = idf_helpers.idfobjectkeys(idf) assert result[:5] == expected def test_getanymentions(): """"""py.test for getanymentions"""""" idf = IDF(StringIO("""")) mat = idf.newidfobject(""MATERIAL"", Name=""mat"") aconst = idf.newidfobject(""CONSTRUCTION"", Name=""const"") foundobjs = idf_helpers.getanymentions(idf, mat) assert len(foundobjs) == 1 assert foundobjs[0] == mat def test_getobject_use_prevfield(): """"""py.test for getobject_use_prevfield"""""" idf = IDF(StringIO("""")) branch = idf.newidfobject( ""BRANCH"", Name=""CW Pump Branch"", Component_1_Object_Type=""Pump:VariableSpeed"", Component_1_Name=""CW Circ Pump"", ) pump = idf.newidfobject(""PUMP:VARIABLESPEED"", Name=""CW Circ Pump"") foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_1_Name"") assert foundobject == pump # test for all times it should return None foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Name"") foundobject = None # prev field not end with Object_Type foundobject = idf_helpers.getobject_use_prevfield( idf, branch, ""Component_11_Object_Type"" ) foundobject = None # field does not end with ""Name"" foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_3_Name"") foundobject = None # bad idfobject key def test_getidfkeyswithnodes(): """"""py.test for getidfkeyswithnodes"""""" nodekeys = idf_helpers.getidfkeyswithnodes() # print(len(nodekeys)) assert ""PLANTLOOP"" in nodekeys assert ""ZONE"" not in nodekeys # def test_a(): # assert 1== 2 def test_getobjectswithnode(): """"""py.test for getobjectswithnode"""""" idf = IDF(StringIO("""")) nodekeys = idf_helpers.getidfkeyswithnodes() plantloop = idf.newidfobject( ""PlantLoop"", Name=""Chilled Water Loop"", Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=""CW Pump Branch"", Component_1_Inlet_Node_Name=""CW Supply Inlet Node"", ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=""CW Circ Pump"", Inlet_Node_Name=""CW Supply Inlet Node"", ) zone = idf.newidfobject(""zone"") foundobjs = idf_helpers.getobjectswithnode(idf, nodekeys, ""CW Supply Inlet Node"") expected = [plantloop, branch, pump] expectedset = set([item.key for item in expected]) resultset = set([item.key for item in foundobjs]) assert resultset == expectedset expectedset = set([item.Name for item in expected]) resultset = set([item.Name for item in foundobjs]) assert resultset == expectedset def test_name2idfobject(): """"""py.test for name2idfobject"""""" idf = IDF(StringIO("""")) plantloopname = ""plantloopname"" branchname = ""branchname"" pumpname = ""pumpname"" zonename = ""zonename"" plantloop = idf.newidfobject( ""PlantLoop"", Name=plantloopname, Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=branchname, Component_1_Inlet_Node_Name=""CW Supply Inlet Node"" ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=pumpname, Inlet_Node_Name=""CW Supply Inlet Node"" ) zone = idf.newidfobject(""zone"", Name=zonename) simulation = idf.newidfobject(""SimulationControl"") # - test names = [plantloopname, branchname, pumpname, zonename] idfobjs = [plantloop, branch, pump, zone] for name, idfobj in zip(names, idfobjs): result = idf_helpers.name2idfobject(idf, Name=name) assert result == idfobj # test when objkeys!=None objkey = ""ZoneHVAC:EquipmentConnections"" equipconnections = idf.newidfobject(objkey, Zone_Name=zonename) result = idf_helpers.name2idfobject(idf, Zone_Name=zonename, objkeys=[objkey]) assert result == equipconnections def test_getidfobjectlist(): """"""py.test for getidfobjectlist"""""" names = [""a"", ""b"", ""c"", ""d"", ""e""] idf = IDF(StringIO("""")) idf.newidfobject(""building"", Name=""a"") idf.newidfobject(""building"", Name=""b"") idf.newidfobject(""Site:Location"", Name=""c"") idf.newidfobject(""ScheduleTypeLimits"", Name=""d"") idf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(idf) assert [res.Name for res in result] == names def test_copyidfintoidf(): """"""py.test for copyidfintoidf"""""" tonames = [""a"", ""b"", ""c""] fromnames = [""d"", ""e""] allnames = [""a"", ""b"", ""c"", ""d"", ""e""] toidf = IDF(StringIO("""")) toidf.newidfobject(""building"", Name=""a"") toidf.newidfobject(""building"", Name=""b"") toidf.newidfobject(""Site:Location"", Name=""c"") result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == tonames fromidf = IDF(StringIO("""")) fromidf.newidfobject(""ScheduleTypeLimits"", Name=""d"") fromidf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(fromidf) assert [res.Name for res in result] == fromnames idf_helpers.copyidfintoidf(toidf, fromidf) result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == allnames ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_idf_helpers.py::test_getobjectswithnode,eppy/tests/test_idf_helpers.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for idf"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import iteritems from six import StringIO from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.idf_helpers as idf_helpers iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_idfobjectkeys(): """"""py.test for idfobjectkeys"""""" expected = [ ""LEAD INPUT"", ""SIMULATION DATA"", ""VERSION"", ""SIMULATIONCONTROL"", ""BUILDING"", ] idf = IDF(StringIO("""")) result = idf_helpers.idfobjectkeys(idf) assert result[:5] == expected def test_getanymentions(): """"""py.test for getanymentions"""""" idf = IDF(StringIO("""")) mat = idf.newidfobject(""MATERIAL"", Name=""mat"") aconst = idf.newidfobject(""CONSTRUCTION"", Name=""const"") foundobjs = idf_helpers.getanymentions(idf, mat) assert len(foundobjs) == 1 assert foundobjs[0] == mat def test_getobject_use_prevfield(): """"""py.test for getobject_use_prevfield"""""" idf = IDF(StringIO("""")) branch = idf.newidfobject( ""BRANCH"", Name=""CW Pump Branch"", Component_1_Object_Type=""Pump:VariableSpeed"", Component_1_Name=""CW Circ Pump"", ) pump = idf.newidfobject(""PUMP:VARIABLESPEED"", Name=""CW Circ Pump"") foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_1_Name"") assert foundobject == pump # test for all times it should return None foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Name"") foundobject = None # prev field not end with Object_Type foundobject = idf_helpers.getobject_use_prevfield( idf, branch, ""Component_11_Object_Type"" ) foundobject = None # field does not end with ""Name"" foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_3_Name"") foundobject = None # bad idfobject key def test_getidfkeyswithnodes(): """"""py.test for getidfkeyswithnodes"""""" nodekeys = idf_helpers.getidfkeyswithnodes() # print(len(nodekeys)) assert ""PLANTLOOP"" in nodekeys assert ""ZONE"" not in nodekeys # def test_a(): # assert 1== 2 def test_getobjectswithnode(): """"""py.test for getobjectswithnode"""""" idf = IDF(StringIO("""")) nodekeys = idf_helpers.getidfkeyswithnodes() plantloop = idf.newidfobject( ""PlantLoop"", Name=""Chilled Water Loop"", Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=""CW Pump Branch"", Component_1_Inlet_Node_Name=""CW Supply Inlet Node"", ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=""CW Circ Pump"", Inlet_Node_Name=""CW Supply Inlet Node"", ) zone = idf.newidfobject(""zone"") foundobjs = idf_helpers.getobjectswithnode(idf, nodekeys, ""CW Supply Inlet Node"") expected = [plantloop, branch, pump] expectedset = set([item.key for item in expected]) resultset = set([item.key for item in foundobjs]) assert resultset == expectedset expectedset = set([item.Name for item in expected]) resultset = set([item.Name for item in foundobjs]) assert resultset == expectedset def test_name2idfobject(): """"""py.test for name2idfobject"""""" idf = IDF(StringIO("""")) plantloopname = ""plantloopname"" branchname = ""branchname"" pumpname = ""pumpname"" zonename = ""zonename"" plantloop = idf.newidfobject( ""PlantLoop"", Name=plantloopname, Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=branchname, Component_1_Inlet_Node_Name=""CW Supply Inlet Node"" ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=pumpname, Inlet_Node_Name=""CW Supply Inlet Node"" ) zone = idf.newidfobject(""zone"", Name=zonename) simulation = idf.newidfobject(""SimulationControl"") # - test names = [plantloopname, branchname, pumpname, zonename] idfobjs = [plantloop, branch, pump, zone] for name, idfobj in zip(names, idfobjs): result = idf_helpers.name2idfobject(idf, Name=name) assert result == idfobj # test when objkeys!=None objkey = ""ZoneHVAC:EquipmentConnections"" equipconnections = idf.newidfobject(objkey, Zone_Name=zonename) result = idf_helpers.name2idfobject(idf, Zone_Name=zonename, objkeys=[objkey]) assert result == equipconnections def test_getidfobjectlist(): """"""py.test for getidfobjectlist"""""" names = [""a"", ""b"", ""c"", ""d"", ""e""] idf = IDF(StringIO("""")) idf.newidfobject(""building"", Name=""a"") idf.newidfobject(""building"", Name=""b"") idf.newidfobject(""Site:Location"", Name=""c"") idf.newidfobject(""ScheduleTypeLimits"", Name=""d"") idf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(idf) assert [res.Name for res in result] == names def test_copyidfintoidf(): """"""py.test for copyidfintoidf"""""" tonames = [""a"", ""b"", ""c""] fromnames = [""d"", ""e""] allnames = [""a"", ""b"", ""c"", ""d"", ""e""] toidf = IDF(StringIO("""")) toidf.newidfobject(""building"", Name=""a"") toidf.newidfobject(""building"", Name=""b"") toidf.newidfobject(""Site:Location"", Name=""c"") result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == tonames fromidf = IDF(StringIO("""")) fromidf.newidfobject(""ScheduleTypeLimits"", Name=""d"") fromidf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(fromidf) assert [res.Name for res in result] == fromnames idf_helpers.copyidfintoidf(toidf, fromidf) result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == allnames ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_idf_helpers.py::test_getobject_use_prevfield,eppy/tests/test_idf_helpers.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for idf"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import iteritems from six import StringIO from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.idf_helpers as idf_helpers iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_idfobjectkeys(): """"""py.test for idfobjectkeys"""""" expected = [ ""LEAD INPUT"", ""SIMULATION DATA"", ""VERSION"", ""SIMULATIONCONTROL"", ""BUILDING"", ] idf = IDF(StringIO("""")) result = idf_helpers.idfobjectkeys(idf) assert result[:5] == expected def test_getanymentions(): """"""py.test for getanymentions"""""" idf = IDF(StringIO("""")) mat = idf.newidfobject(""MATERIAL"", Name=""mat"") aconst = idf.newidfobject(""CONSTRUCTION"", Name=""const"") foundobjs = idf_helpers.getanymentions(idf, mat) assert len(foundobjs) == 1 assert foundobjs[0] == mat def test_getobject_use_prevfield(): """"""py.test for getobject_use_prevfield"""""" idf = IDF(StringIO("""")) branch = idf.newidfobject( ""BRANCH"", Name=""CW Pump Branch"", Component_1_Object_Type=""Pump:VariableSpeed"", Component_1_Name=""CW Circ Pump"", ) pump = idf.newidfobject(""PUMP:VARIABLESPEED"", Name=""CW Circ Pump"") foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_1_Name"") assert foundobject == pump # test for all times it should return None foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Name"") foundobject = None # prev field not end with Object_Type foundobject = idf_helpers.getobject_use_prevfield( idf, branch, ""Component_11_Object_Type"" ) foundobject = None # field does not end with ""Name"" foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_3_Name"") foundobject = None # bad idfobject key def test_getidfkeyswithnodes(): """"""py.test for getidfkeyswithnodes"""""" nodekeys = idf_helpers.getidfkeyswithnodes() # print(len(nodekeys)) assert ""PLANTLOOP"" in nodekeys assert ""ZONE"" not in nodekeys # def test_a(): # assert 1== 2 def test_getobjectswithnode(): """"""py.test for getobjectswithnode"""""" idf = IDF(StringIO("""")) nodekeys = idf_helpers.getidfkeyswithnodes() plantloop = idf.newidfobject( ""PlantLoop"", Name=""Chilled Water Loop"", Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=""CW Pump Branch"", Component_1_Inlet_Node_Name=""CW Supply Inlet Node"", ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=""CW Circ Pump"", Inlet_Node_Name=""CW Supply Inlet Node"", ) zone = idf.newidfobject(""zone"") foundobjs = idf_helpers.getobjectswithnode(idf, nodekeys, ""CW Supply Inlet Node"") expected = [plantloop, branch, pump] expectedset = set([item.key for item in expected]) resultset = set([item.key for item in foundobjs]) assert resultset == expectedset expectedset = set([item.Name for item in expected]) resultset = set([item.Name for item in foundobjs]) assert resultset == expectedset def test_name2idfobject(): """"""py.test for name2idfobject"""""" idf = IDF(StringIO("""")) plantloopname = ""plantloopname"" branchname = ""branchname"" pumpname = ""pumpname"" zonename = ""zonename"" plantloop = idf.newidfobject( ""PlantLoop"", Name=plantloopname, Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=branchname, Component_1_Inlet_Node_Name=""CW Supply Inlet Node"" ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=pumpname, Inlet_Node_Name=""CW Supply Inlet Node"" ) zone = idf.newidfobject(""zone"", Name=zonename) simulation = idf.newidfobject(""SimulationControl"") # - test names = [plantloopname, branchname, pumpname, zonename] idfobjs = [plantloop, branch, pump, zone] for name, idfobj in zip(names, idfobjs): result = idf_helpers.name2idfobject(idf, Name=name) assert result == idfobj # test when objkeys!=None objkey = ""ZoneHVAC:EquipmentConnections"" equipconnections = idf.newidfobject(objkey, Zone_Name=zonename) result = idf_helpers.name2idfobject(idf, Zone_Name=zonename, objkeys=[objkey]) assert result == equipconnections def test_getidfobjectlist(): """"""py.test for getidfobjectlist"""""" names = [""a"", ""b"", ""c"", ""d"", ""e""] idf = IDF(StringIO("""")) idf.newidfobject(""building"", Name=""a"") idf.newidfobject(""building"", Name=""b"") idf.newidfobject(""Site:Location"", Name=""c"") idf.newidfobject(""ScheduleTypeLimits"", Name=""d"") idf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(idf) assert [res.Name for res in result] == names def test_copyidfintoidf(): """"""py.test for copyidfintoidf"""""" tonames = [""a"", ""b"", ""c""] fromnames = [""d"", ""e""] allnames = [""a"", ""b"", ""c"", ""d"", ""e""] toidf = IDF(StringIO("""")) toidf.newidfobject(""building"", Name=""a"") toidf.newidfobject(""building"", Name=""b"") toidf.newidfobject(""Site:Location"", Name=""c"") result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == tonames fromidf = IDF(StringIO("""")) fromidf.newidfobject(""ScheduleTypeLimits"", Name=""d"") fromidf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(fromidf) assert [res.Name for res in result] == fromnames idf_helpers.copyidfintoidf(toidf, fromidf) result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == allnames ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_idf_helpers.py::test_idfobjectkeys,eppy/tests/test_idf_helpers.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for idf"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import iteritems from six import StringIO from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.idf_helpers as idf_helpers iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_idfobjectkeys(): """"""py.test for idfobjectkeys"""""" expected = [ ""LEAD INPUT"", ""SIMULATION DATA"", ""VERSION"", ""SIMULATIONCONTROL"", ""BUILDING"", ] idf = IDF(StringIO("""")) result = idf_helpers.idfobjectkeys(idf) assert result[:5] == expected def test_getanymentions(): """"""py.test for getanymentions"""""" idf = IDF(StringIO("""")) mat = idf.newidfobject(""MATERIAL"", Name=""mat"") aconst = idf.newidfobject(""CONSTRUCTION"", Name=""const"") foundobjs = idf_helpers.getanymentions(idf, mat) assert len(foundobjs) == 1 assert foundobjs[0] == mat def test_getobject_use_prevfield(): """"""py.test for getobject_use_prevfield"""""" idf = IDF(StringIO("""")) branch = idf.newidfobject( ""BRANCH"", Name=""CW Pump Branch"", Component_1_Object_Type=""Pump:VariableSpeed"", Component_1_Name=""CW Circ Pump"", ) pump = idf.newidfobject(""PUMP:VARIABLESPEED"", Name=""CW Circ Pump"") foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_1_Name"") assert foundobject == pump # test for all times it should return None foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Name"") foundobject = None # prev field not end with Object_Type foundobject = idf_helpers.getobject_use_prevfield( idf, branch, ""Component_11_Object_Type"" ) foundobject = None # field does not end with ""Name"" foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_3_Name"") foundobject = None # bad idfobject key def test_getidfkeyswithnodes(): """"""py.test for getidfkeyswithnodes"""""" nodekeys = idf_helpers.getidfkeyswithnodes() # print(len(nodekeys)) assert ""PLANTLOOP"" in nodekeys assert ""ZONE"" not in nodekeys # def test_a(): # assert 1== 2 def test_getobjectswithnode(): """"""py.test for getobjectswithnode"""""" idf = IDF(StringIO("""")) nodekeys = idf_helpers.getidfkeyswithnodes() plantloop = idf.newidfobject( ""PlantLoop"", Name=""Chilled Water Loop"", Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=""CW Pump Branch"", Component_1_Inlet_Node_Name=""CW Supply Inlet Node"", ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=""CW Circ Pump"", Inlet_Node_Name=""CW Supply Inlet Node"", ) zone = idf.newidfobject(""zone"") foundobjs = idf_helpers.getobjectswithnode(idf, nodekeys, ""CW Supply Inlet Node"") expected = [plantloop, branch, pump] expectedset = set([item.key for item in expected]) resultset = set([item.key for item in foundobjs]) assert resultset == expectedset expectedset = set([item.Name for item in expected]) resultset = set([item.Name for item in foundobjs]) assert resultset == expectedset def test_name2idfobject(): """"""py.test for name2idfobject"""""" idf = IDF(StringIO("""")) plantloopname = ""plantloopname"" branchname = ""branchname"" pumpname = ""pumpname"" zonename = ""zonename"" plantloop = idf.newidfobject( ""PlantLoop"", Name=plantloopname, Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=branchname, Component_1_Inlet_Node_Name=""CW Supply Inlet Node"" ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=pumpname, Inlet_Node_Name=""CW Supply Inlet Node"" ) zone = idf.newidfobject(""zone"", Name=zonename) simulation = idf.newidfobject(""SimulationControl"") # - test names = [plantloopname, branchname, pumpname, zonename] idfobjs = [plantloop, branch, pump, zone] for name, idfobj in zip(names, idfobjs): result = idf_helpers.name2idfobject(idf, Name=name) assert result == idfobj # test when objkeys!=None objkey = ""ZoneHVAC:EquipmentConnections"" equipconnections = idf.newidfobject(objkey, Zone_Name=zonename) result = idf_helpers.name2idfobject(idf, Zone_Name=zonename, objkeys=[objkey]) assert result == equipconnections def test_getidfobjectlist(): """"""py.test for getidfobjectlist"""""" names = [""a"", ""b"", ""c"", ""d"", ""e""] idf = IDF(StringIO("""")) idf.newidfobject(""building"", Name=""a"") idf.newidfobject(""building"", Name=""b"") idf.newidfobject(""Site:Location"", Name=""c"") idf.newidfobject(""ScheduleTypeLimits"", Name=""d"") idf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(idf) assert [res.Name for res in result] == names def test_copyidfintoidf(): """"""py.test for copyidfintoidf"""""" tonames = [""a"", ""b"", ""c""] fromnames = [""d"", ""e""] allnames = [""a"", ""b"", ""c"", ""d"", ""e""] toidf = IDF(StringIO("""")) toidf.newidfobject(""building"", Name=""a"") toidf.newidfobject(""building"", Name=""b"") toidf.newidfobject(""Site:Location"", Name=""c"") result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == tonames fromidf = IDF(StringIO("""")) fromidf.newidfobject(""ScheduleTypeLimits"", Name=""d"") fromidf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(fromidf) assert [res.Name for res in result] == fromnames idf_helpers.copyidfintoidf(toidf, fromidf) result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == allnames ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_idf_helpers.py::test_name2idfobject,eppy/tests/test_idf_helpers.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for idf"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import iteritems from six import StringIO from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.idf_helpers as idf_helpers iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_idfobjectkeys(): """"""py.test for idfobjectkeys"""""" expected = [ ""LEAD INPUT"", ""SIMULATION DATA"", ""VERSION"", ""SIMULATIONCONTROL"", ""BUILDING"", ] idf = IDF(StringIO("""")) result = idf_helpers.idfobjectkeys(idf) assert result[:5] == expected def test_getanymentions(): """"""py.test for getanymentions"""""" idf = IDF(StringIO("""")) mat = idf.newidfobject(""MATERIAL"", Name=""mat"") aconst = idf.newidfobject(""CONSTRUCTION"", Name=""const"") foundobjs = idf_helpers.getanymentions(idf, mat) assert len(foundobjs) == 1 assert foundobjs[0] == mat def test_getobject_use_prevfield(): """"""py.test for getobject_use_prevfield"""""" idf = IDF(StringIO("""")) branch = idf.newidfobject( ""BRANCH"", Name=""CW Pump Branch"", Component_1_Object_Type=""Pump:VariableSpeed"", Component_1_Name=""CW Circ Pump"", ) pump = idf.newidfobject(""PUMP:VARIABLESPEED"", Name=""CW Circ Pump"") foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_1_Name"") assert foundobject == pump # test for all times it should return None foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Name"") foundobject = None # prev field not end with Object_Type foundobject = idf_helpers.getobject_use_prevfield( idf, branch, ""Component_11_Object_Type"" ) foundobject = None # field does not end with ""Name"" foundobject = idf_helpers.getobject_use_prevfield(idf, branch, ""Component_3_Name"") foundobject = None # bad idfobject key def test_getidfkeyswithnodes(): """"""py.test for getidfkeyswithnodes"""""" nodekeys = idf_helpers.getidfkeyswithnodes() # print(len(nodekeys)) assert ""PLANTLOOP"" in nodekeys assert ""ZONE"" not in nodekeys # def test_a(): # assert 1== 2 def test_getobjectswithnode(): """"""py.test for getobjectswithnode"""""" idf = IDF(StringIO("""")) nodekeys = idf_helpers.getidfkeyswithnodes() plantloop = idf.newidfobject( ""PlantLoop"", Name=""Chilled Water Loop"", Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=""CW Pump Branch"", Component_1_Inlet_Node_Name=""CW Supply Inlet Node"", ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=""CW Circ Pump"", Inlet_Node_Name=""CW Supply Inlet Node"", ) zone = idf.newidfobject(""zone"") foundobjs = idf_helpers.getobjectswithnode(idf, nodekeys, ""CW Supply Inlet Node"") expected = [plantloop, branch, pump] expectedset = set([item.key for item in expected]) resultset = set([item.key for item in foundobjs]) assert resultset == expectedset expectedset = set([item.Name for item in expected]) resultset = set([item.Name for item in foundobjs]) assert resultset == expectedset def test_name2idfobject(): """"""py.test for name2idfobject"""""" idf = IDF(StringIO("""")) plantloopname = ""plantloopname"" branchname = ""branchname"" pumpname = ""pumpname"" zonename = ""zonename"" plantloop = idf.newidfobject( ""PlantLoop"", Name=plantloopname, Plant_Side_Inlet_Node_Name=""CW Supply Inlet Node"", ) branch = idf.newidfobject( ""Branch"", Name=branchname, Component_1_Inlet_Node_Name=""CW Supply Inlet Node"" ) pump = idf.newidfobject( ""Pump:VariableSpeed"", Name=pumpname, Inlet_Node_Name=""CW Supply Inlet Node"" ) zone = idf.newidfobject(""zone"", Name=zonename) simulation = idf.newidfobject(""SimulationControl"") # - test names = [plantloopname, branchname, pumpname, zonename] idfobjs = [plantloop, branch, pump, zone] for name, idfobj in zip(names, idfobjs): result = idf_helpers.name2idfobject(idf, Name=name) assert result == idfobj # test when objkeys!=None objkey = ""ZoneHVAC:EquipmentConnections"" equipconnections = idf.newidfobject(objkey, Zone_Name=zonename) result = idf_helpers.name2idfobject(idf, Zone_Name=zonename, objkeys=[objkey]) assert result == equipconnections def test_getidfobjectlist(): """"""py.test for getidfobjectlist"""""" names = [""a"", ""b"", ""c"", ""d"", ""e""] idf = IDF(StringIO("""")) idf.newidfobject(""building"", Name=""a"") idf.newidfobject(""building"", Name=""b"") idf.newidfobject(""Site:Location"", Name=""c"") idf.newidfobject(""ScheduleTypeLimits"", Name=""d"") idf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(idf) assert [res.Name for res in result] == names def test_copyidfintoidf(): """"""py.test for copyidfintoidf"""""" tonames = [""a"", ""b"", ""c""] fromnames = [""d"", ""e""] allnames = [""a"", ""b"", ""c"", ""d"", ""e""] toidf = IDF(StringIO("""")) toidf.newidfobject(""building"", Name=""a"") toidf.newidfobject(""building"", Name=""b"") toidf.newidfobject(""Site:Location"", Name=""c"") result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == tonames fromidf = IDF(StringIO("""")) fromidf.newidfobject(""ScheduleTypeLimits"", Name=""d"") fromidf.newidfobject(""ScheduleTypeLimits"", Name=""e"") result = idf_helpers.getidfobjectlist(fromidf) assert [res.Name for res in result] == fromnames idf_helpers.copyidfintoidf(toidf, fromidf) result = idf_helpers.getidfobjectlist(toidf) assert [res.Name for res in result] == allnames ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_json_functions.py::test_updateidf,eppy/tests/test_json_functions.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for json_functions"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from eppy import modeleditor from eppy.modeleditor import IDF from six import StringIO from eppy.iddcurrent import iddcurrent from eppy import json_functions # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_key2elements(): """"""py.test for key2elements"""""" data = ( (""a.b.c.d"", [""a"", ""b"", ""c"", ""d""]), # key, elements (""idf.a.Name.name.c"", [""idf"", ""a"", ""Name.name"", ""c""]), # key, elements (""idf.a.'Name.name'.c"", [""idf"", ""a"", ""Name.name"", ""c""]), # key, elements (""idf.a.'Name.name.n'.c"", [""idf"", ""a"", ""Name.name.n"", ""c""]), # key, elements ) for key, elements in data: result = json_functions.key2elements(key) # print(result) # print(elements) assert result == elements def test_updateidf(): """"""py.test for updateidf"""""" iddtxt = """"""!IDD_Version 8.4.0"""""" data = ( ( """"""Version, 8.3; !- Version Identifier """""", {""idf.version..Version_Identifier"": ""0.1""}, ""version"", ""Version_Identifier"", ""0.1"", ), # idftxt, dct, key, field, fieldval ( """"""SimulationControl, No, !- Do Zone Sizing Calculation No, !- Do System Sizing Calculation No, !- Do Plant Sizing Calculation No, !- Run Simulation for Sizing Periods Yes; !- Run Simulation for Weather File Run Periods """""", {""idf.SimulationControl..Do_Zone_Sizing_Calculation"": ""Yes""}, ""SimulationControl"", ""Do_Zone_Sizing_Calculation"", ""Yes"", ), # idftxt, dct, key, field, fieldval ( """"""Building, Untitled, !- Name 0.0, !- North Axis {deg} City, !- Terrain 0.04, !- Loads Convergence Tolerance Value 0.4, !- Temperature Convergence Tolerance Value {deltaC} FullInteriorAndExterior, !- Solar Distribution 25, !- Maximum Number of Warmup Days ; !- Minimum Number of Warmup Days """""", {""idf.BUilding.Untitled.Terrain"": ""Rural""}, ""Building"", ""Terrain"", ""Rural"", ), # idftxt, dct, key, field, fieldval # make a new object ( """""" """""", {""idf.BUilding.Taj.Terrain"": ""Rural""}, ""Building"", ""Terrain"", ""Rural"", ), # idftxt, dct, key, field, fieldval # make a new object with no Name field ( """""" """""", {""idf.GlobalGeometryRules..Starting_Vertex_Position"": ""UpperLeftCorner""}, ""GlobalGeometryRules"", ""Starting_Vertex_Position"", ""UpperLeftCorner"", ), # idftxt, dct, key, field, fieldval ( """"""Building, Name.name, !- Name 0.0, !- North Axis {deg} City, !- Terrain 0.04, !- Loads Convergence Tolerance Value 0.4, !- Temperature Convergence Tolerance Value {deltaC} FullInteriorAndExterior, !- Solar Distribution 25, !- Maximum Number of Warmup Days ; !- Minimum Number of Warmup Days """""", {""idf.BUilding.Name.name.Terrain"": ""Rural""}, ""Building"", ""Terrain"", ""Rural"", ), # idftxt, dct, key, field, fieldval ( """"""Building, Name.name, !- Name 0.0, !- North Axis {deg} City, !- Terrain 0.04, !- Loads Convergence Tolerance Value 0.4, !- Temperature Convergence Tolerance Value {deltaC} FullInteriorAndExterior, !- Solar Distribution 25, !- Maximum Number of Warmup Days ; !- Minimum Number of Warmup Days """""", {""idf.BUilding.'Name.name'.Terrain"": ""Rural""}, ""Building"", ""Terrain"", ""Rural"", ), # idftxt, dct, key, field, fieldval ) for idftxt, dct, key, field, fieldval in data: idfhandle = StringIO(idftxt) idf = IDF(idfhandle) json_functions.updateidf(idf, dct) assert idf.idfobjects[key][0][field] == fieldval ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_getallobjlists,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_getiddgroupdict,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_getrefnames,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_idd_index,eppy/tests/test_modeleditor.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_idfinmsequence,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_idfstr,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_initread,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_initreadtxt,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_new,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_newidfobject,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_newidfobject_warning,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_refname2key,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_rename,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_save,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_saveas,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_savecopy,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_save_with_lineendings_and_encodings,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_modeleditor.py::test_zonearea_zonevolume,eppy/tests/test_modeleditor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2012 Santosh Philip # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""py.test for modeleditor"""""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from itertools import product import os import warnings import pytest from six import StringIO from six import string_types from eppy import modeleditor from eppy.bunch_subclass import Bunch from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal import eppy.snippet as snippet iddsnippet = iddcurrent.iddtxt idfsnippet = snippet.idfsnippet # idffhandle = StringIO(idfsnippet) # iddfhandle = StringIO(iddsnippet) # bunchdt, data, commdct, gdict = idfreader.idfreader(idffhandle, iddfhandle, None) # idd is read only once in this test # if it has already been read from some other test, it will continue with # the old reading iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) def test_poptrailing(): """"""py.test for poptrailing"""""" tdata = ( ([1, 2, 3, """", 56, """", """", """", """"], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, """", 56], [1, 2, 3, """", 56]), # lst, popped ([1, 2, 3, 56], [1, 2, 3, 56]), # lst, popped ) for before, after in iter(tdata): assert modeleditor.poptrailing(before) == after def test_extendlist(): """"""py.test for extendlist"""""" tdata = ( ([1, 2, 3], 2, 0, [1, 2, 3]), # lst, i, value, nlst ([1, 2, 3], 3, 0, [1, 2, 3, 0]), # lst, i, value, nlst ([1, 2, 3], 5, 0, [1, 2, 3, 0, 0, 0]), # lst, i, value, nlst ([1, 2, 3], 7, 0, [1, 2, 3, 0, 0, 0, 0, 0]), # lst, i, value, nlst ) for lst, i, value, nlst in tdata: modeleditor.extendlist(lst, i, value=value) assert lst == nlst def test_namebunch(): """"""py.test for namebunch"""""" thedata = ( (Bunch(dict(Name="""", a=5)), ""yay"", ""yay""), # abunch, aname, thename (Bunch(dict(Name=None, a=5)), ""yay"", None), # abunch, aname, thename ) for abunch, aname, thename in thedata: result = modeleditor.namebunch(abunch, aname) assert result.Name == thename def test_getnamedargs(): """"""py.test for getnamedargs"""""" result = dict(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(a=1, b=2, c=3) assert result == modeleditor.getnamedargs(dict(a=1, b=2, c=3)) assert result == modeleditor.getnamedargs(dict(a=1, b=2), c=3) assert result == modeleditor.getnamedargs(dict(a=1), c=3, b=2) def test_getrefnames(): """"""py.test for getrefnames"""""" tdata = ( ( ""ZONE"", [ ""ZoneNames"", ""OutFaceEnvNames"", ""ZoneAndZoneListNames"", ""AirflowNetworkNodeAndZoneNames"", ], ), # objkey, therefs ( ""FluidProperties:Name"".upper(), [""FluidNames"", ""FluidAndGlycolNames""], ), # objkey, therefs (""Building"".upper(), []), # objkey, therefs ) for objkey, therefs in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getrefnames(idf, objkey) assert result == therefs def test_getallobjlists(): """"""py.test for getallobjlists"""""" tdata = ( ( ""TransformerNames"", [(""ElectricLoadCenter:Distribution"".upper(), ""TransformerNames"", [10])], ), # refname, objlists ) for refname, objlists in tdata: fhandle = StringIO("""") idf = IDF(fhandle) result = modeleditor.getallobjlists(idf, refname) assert result == objlists def test_rename(): """"""py.test for rename"""""" idftxt = """"""Material, G01a 19mm gypsum board, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name G01a 19mm gypsum board, !- Outside Layer F04 Wall air space resistance, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 Construction, Other Wall, !- Name G01a 19mm gypsum board, !- Outside Layer G01a 19mm gypsum board, !- Layer 2 G01a 19mm gypsum board; !- Layer 3 """""" ridftxt = """"""Material, peanut butter, !- Name MediumSmooth, !- Roughness 0.019, !- Thickness {m} 0.16, !- Conductivity {W/m-K} 800, !- Density {kg/m3} 1090; !- Specific Heat {J/kg-K} Construction, Interior Wall, !- Name peanut butter, !- Outside Layer F04 Wall air space resistance, !- Layer 2 peanut butter; !- Layer 3 Construction, Other Wall, !- Name peanut butter, !- Outside Layer peanut butter, !- Layer 2 peanut butter; !- Layer 3 """""" fhandle = StringIO(idftxt) idf = IDF(fhandle) result = modeleditor.rename( idf, ""Material"".upper(), ""G01a 19mm gypsum board"", ""peanut butter"" ) assert result.Name == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Outside_Layer == ""peanut butter"" assert idf.idfobjects[""CONSTRUCTION""][0].Layer_3 == ""peanut butter"" def test_zonearea_zonevolume(): """"""py.test for zonearea and zonevolume"""""" idftxt = """"""Zone, 473222, 0.0, 0.0, 0.0, 0.0, , 1; BuildingSurface:Detailed, F7289B, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 2.23, 2.56, 0.0, 2.23, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.56, 0.0; BuildingSurface:Detailed, F3659B, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.56, 1.49, 2.23, 2.56, 0.0, 0.0, 2.56, 0.0, 0.0, 2.56, 1.49; BuildingSurface:Detailed, 46C6C9, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 0.0, 1.49, 2.23, 0.0, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 1.02548139464, 1.49; BuildingSurface:Detailed, 4287DD, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 2.56, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.49; BuildingSurface:Detailed, 570C2E, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 0.0, 1.49, 0.0, 0.0, 0.0, 2.23, 0.0, 0.0, 2.23, 0.0, 1.49; BuildingSurface:Detailed, BAEA99, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 0.0, 2.56, 1.49, 0.0, 0.0, 1.49, 2.23, 0.0, 1.49, 2.23, 2.56, 1.49; BuildingSurface:Detailed, C879FE, Floor, Exterior Floor, 473222, Ground, , NoSun, NoWind, , 4, 3.22, 2.52548139464, 0.0, 3.22, 1.02548139464, 0.0, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0; BuildingSurface:Detailed, 25B601, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, F5EADC, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 1.02548139464, 1.49, 2.23, 1.02548139464, 0.0, 3.22, 1.02548139464, 0.0, 3.22, 1.02548139464, 1.49; BuildingSurface:Detailed, D0AABE, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 1.02548139464, 1.49, 3.22, 1.02548139464, 0.0, 3.22, 2.52548139464, 0.0, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, B0EA02, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 3.22, 2.52548139464, 1.49, 3.22, 2.52548139464, 0.0, 2.23, 2.52548139464, 0.0, 2.23, 2.52548139464, 1.49; BuildingSurface:Detailed, E6DF3B, Roof, Exterior Roof, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 1.02548139464, 1.49, 3.22, 1.02548139464, 1.49, 3.22, 2.52548139464, 1.49; BuildingSurface:Detailed, 4F8681, Wall, Exterior Wall, 473222, Outdoors, , SunExposed, WindExposed, , 4, 2.23, 2.52548139464, 1.49, 2.23, 2.52548139464, 0.0, 2.23, 2.56, 0.0, 2.23, 2.56, 1.49; """""" idf = IDF(StringIO(idftxt)) result = modeleditor.zonearea(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zoneheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zone_floor2roofheight(idf, ""473222"") assert almostequal(result, 1.49) result = modeleditor.zonevolume(idf, ""473222"") assert almostequal(result, 10.718762) # remove floor zone = idf.getobject(""ZONE"", ""473222"") surfs = idf.idfobjects[""BuildingSurface:Detailed"".upper()] zone_surfs = [s for s in surfs if s.Zone_Name == zone.Name] floors = [s for s in zone_surfs if s.Surface_Type.upper() == ""FLOOR""] for floor in floors: idf.removeidfobject(floor) result = modeleditor.zonearea_floor(idf, ""473222"") assert almostequal(result, 0) result = modeleditor.zonearea_roofceiling(idf, ""473222"") assert almostequal(result, 7.1938) result = modeleditor.zonearea(idf, ""473222", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_simpleread.py::test_idfreadtest,eppy/tests/test_simpleread.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""py.test for simpleread.py"""""" # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from six import StringIO import eppy.simpleread as simpleread def test_idf2txt(): """"""py.test for idf2txt"""""" data = ( ( """""" VERSION, 7.3; !- Version Identifier SIMULATIONCONTROL, Yes, !- Do Zone Sizing Calculation Yes, !- Do System Sizing Calculation Yes, !- Do Plant Sizing Calculation No, !- Run Simulation for Sizing Periods Yes; !- Run Simulation for Weather File Run Periods BUILDING, Empire State Building, !- Name 30.0, !- North Axis City, !- Terrain 0.04, !- Loads Convergence Tolerance Value 0.4, !- Temperature Convergence Tolerance Value FullExterior, !- Solar Distribution 25, !- Maximum Number of Warmup Days 6; !- Minimum Number of Warmup Days SITE:LOCATION, CHICAGO_IL_USA TMY2-94846, !- Name 41.78, !- Latitude -87.75, !- Longitude -6.0, !- Time Zone 190.0; !- Elevation """""", """"""; BUILDING, Empire State Building, 30.0, City, 0.04, 0.4, FullExterior, 25.0, 6.0; SIMULATIONCONTROL, Yes, Yes, Yes, No, Yes; SITE:LOCATION, CHICAGO_IL_USA TMY2-94846, 41.78, -87.75, -6.0, 190.0; VERSION, 7.3; """""", ), # intxt, outtxt ) for intxt, outtxt in data: result = simpleread.idf2txt(intxt) assert result == outtxt def test_idfreadtest(): """"""py.test for idfreadtest"""""" data = ( ( """"""!IDD_Version 7.2.0.006 Version, \\unique-object \\format singleLine A1 ; \\field Version Identifier SimulationControl, \\unique-object A1, \\field Do Zone Sizing Calculation A2, \\field Do System Sizing Calculation A3, \\field Do Plant Sizing Calculation A4, \\field Run Simulation for Sizing Periods A5; \\field Run Simulation for Weather File Run Periods Building, \\unique-object A1 , \\field Name N1 , \\field North Axis A2 , \\field Terrain N2 , \\field Loads Convergence Tolerance Value N3 , \\field Temperature Convergence Tolerance Value A3 , \\field Solar Distribution N4 , \\field Maximum Number of Warmup Days N5 ; \\field Minimum Number of Warmup Days Site:Location, \\unique-object A1 , \\field Name N1 , \\field Latitude N2 , \\field Longitude N3 , \\field Time Zone N4 ; \\field Elevation """""", """""" VERSION, 7.3; !- Version Identifier SIMULATIONCONTROL, Yes, !- Do Zone Sizing Calculation Yes, !- Do System Sizing Calculation Yes, !- Do Plant Sizing Calculation No, !- Run Simulation for Sizing Periods Yes; !- Run Simulation for Weather File Run Periods BUILDING, Empire State Building, !- Name 30.0, !- North Axis City, !- Terrain 0.04, !- Loads Convergence Tolerance Value 0.4, !- Temperature Convergence Tolerance Value FullExterior, !- Solar Distribution 25, !- Maximum Number of Warmup Days 6; !- Minimum Number of Warmup Days SITE:LOCATION, CHICAGO_IL_USA TMY2-94846, !- Name 41.78, !- Latitude -87.75, !- Longitude -6.0, !- Time Zone 190.0; !- Elevation """""", ), # iddtxt, idftxt ) for iddtxt, idftxt in data: iddhandle = StringIO(iddtxt) idfhandle1 = StringIO(idftxt) idfhandle2 = StringIO(idftxt) result = simpleread.idfreadtest(iddhandle, idfhandle1, idfhandle2) assert result == True ", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_heatcapacity_1_layer_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_heatcapacity_2_layer_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_heatcapacity_airgap_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_heatcapacity_infraredtransparent_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_heatcapacity_material,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_heatcapacity_nomass_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_heatcapacity_roofvegetation_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_rvalue_1_layer_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_rvalue_2_layer_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_rvalue_airgap_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_rvalue_fails,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_rvalue_infraredtransparent_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_rvalue_material,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_rvalue_nomass_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_rvalue_roofvegetation_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_ufactor_1_layer_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_ufactor_2_layer_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_ufactor_airgap_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_ufactor_infraredtransparent_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_ufactor_material,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_ufactor_nomass_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/santoshphilip/eppy,98e58583dce6c0fcec9c7b1ff1142bae0a67ddc7,eppy/tests/test_thermal_properties.py::Test_ThermalProperties::test_ufactor_roofvegetation_construction,eppy/tests/test_thermal_properties.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# Copyright (c) 2016 Jamie Bull # ======================================================================= # Distributed under the MIT License. # (See accompanying file LICENSE or copy at # http://opensource.org/licenses/MIT) # ======================================================================= """"""Tests for thermal_properties.py """""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import warnings from six import StringIO from eppy.constructions.thermal_properties import INSIDE_FILM_R from eppy.constructions.thermal_properties import OUTSIDE_FILM_R from eppy.iddcurrent import iddcurrent from eppy.modeleditor import IDF from eppy.pytest_helpers import almostequal iddfhandle = StringIO(iddcurrent.iddtxt) if IDF.getiddname() == None: IDF.setiddname(iddfhandle) single_layer = """""" Construction, TestConstruction, !- Name TestMaterial; !- Inside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" expected_failure = """""" Construction, TestConstruction, !- Name Skyhooks; !- Inside Layer Boiler:HotWater, Skyhooks, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" double_layer = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance """""" air_gap = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer AirGap, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:AirGap, AirGap, 0.1; !- Thermal Resistance """""" infrared_transparent = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer InfraredTransparent, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:InfraredTransparent, InfraredTransparent; !- Name """""" no_mass = """""" Construction, TestConstruction, !- Name TestMaterial, !- Inside Layer NoMass, !- Layer 2 TestMaterial; !- Outside Layer Material, TestMaterial, Rough, !- Roughness 0.10, !- Thickness {m} 0.5, !- Conductivity {W/m-K} 1000.0, !- Density {kg/m3} 1200, !- Specific Heat {J/kg-K} 0.9, !- Thermal Absorptance 0.6, !- Solar Absorptance 0.6; !- Visible Absorptance Material:NoMass, NoMass, ! Material Name , ! Roughness 0.1, ! Resistance {M**2K/W} , ! Thermal Absorptance , ! Solar Absorptance ; ! Visible Absorptance """""" roof_vegetation = """""" Construction, TestConstruction, !- Name RoofVegetation; !- Inside Layer Material:RoofVegetation, RoofVegetation, !- Name , !- Height of Plants {m} , !- Leaf Area Index {dimensionless} , !- Leaf Reflectivity {dimensionless} , !- Leaf Emissivity , !- Minimum Stomatal Resistance {s/m} , !- Soil Layer Name , !- Roughness 0.1, !- Thickness {m} 0.5, !- Conductivity of Dry Soil {W/m-K} 1000, !- Density of Dry Soil {kg/m3} 1200, !- Specific Heat of Dry Soil {J/kg-K} , !- Thermal Absorptance , !- Solar Absorptance , !- Visible Absorptance , !- Saturation Volumetric Moisture Content of the Soil Layer , !- Residual Volumetric Moisture Content of the Soil Layer , !- Initial Volumetric Moisture Content of the Soil Layer ; !- Moisture Diffusion Calculation Method """""" class Test_ThermalProperties(object): def setup_method(self, test_method): self.idf = IDF() def test_rvalue_1_layer_construction(self): self.idf.initreadtxt(single_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = INSIDE_FILM_R + m.Thickness / m.Conductivity + OUTSIDE_FILM_R assert c.rvalue == expected assert c.rvalue == 0.35 def test_rvalue_fails(self): self.idf.initreadtxt(expected_failure) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") try: c.rvalue assert False except AttributeError as e: assert str(e) == ""Skyhooks material not found in IDF"" def test_rvalue_2_layer_construction(self): self.idf.initreadtxt(double_layer) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert c.rvalue == expected assert c.rvalue == 0.55 def test_rvalue_airgap_construction(self): self.idf.initreadtxt(air_gap) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") a = self.idf.getobject(""MATERIAL:AIRGAP"", ""AirGap"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + a.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_infraredtransparent_construction(self): self.idf.initreadtxt(infrared_transparent) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.55, places=2) def test_rvalue_nomass_construction(self): self.idf.initreadtxt(no_mass) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL"", ""TestMaterial"") n = self.idf.getobject(""MATERIAL:NOMASS"", ""NoMass"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity + n.Thermal_Resistance + m.Thickness / m.Conductivity + OUTSIDE_FILM_R ) assert almostequal(c.rvalue, expected, places=2) assert almostequal(c.rvalue, 0.65, places=2) def test_rvalue_roofvegetation_construction(self): self.idf.initreadtxt(roof_vegetation) c = self.idf.getobject(""CONSTRUCTION"", ""TestConstruction"") m = self.idf.getobject(""MATERIAL:ROOFVEGETATION"", ""RoofVegetation"") expected = ( INSIDE_FILM_R + m.Thickness / m.Conductivity_of_Dry_Soil + OUTSIDE_FILM_R ) with warnings.catch_warnings(record=True) as w: warnings.simplefilter(""always"") assert c.rvalue == expected assert c.rvalue == 0.35 # check that a UserWarning is raised assert issubclass(w[-1].category, UserWarning) def test_rvalue_material(self): s", https://github.com/sashgorokhov/python-telegram-handler,4ac0e5cbb6ac3c3f81a57be6b3cea49dfd8e8ec4,tests/test_handlers.py::test_handler_init_without_chat,tests/test_handlers.py,OD-Brit,flaky,RepoArchived,nan,classify;root_cause,"import json import logging import mock import pytest import requests import telegram_handler.handlers # From http://stackoverflow.com/questions/899067/how-should-i-verify-a-log-message-when-testing-python-code-under-nose class MockLoggingHandler(logging.Handler): """"""Mock logging handler to check for expected logs. Messages are available from an instance's ``messages`` dict, in order, indexed by a lowercase log level string (e.g., 'debug', 'info', etc.). """""" def __init__(self, *args, **kwargs): self.messages = {'debug': [], 'info': [], 'warning': [], 'error': [], 'critical': []} super(MockLoggingHandler, self).__init__(*args, **kwargs) def emit(self, record): ""Store a message from ``record`` in the instance's ``messages`` dict."" self.acquire() try: name = record.levelname.lower() self.messages.setdefault(name, []) self.messages[name].append(record.getMessage()) finally: self.release() def reset(self): self.acquire() try: for message_list in self.messages.values(): message_list.clear() finally: self.release() @pytest.fixture def handler(): handler = telegram_handler.handlers.TelegramHandler('foo', 'bar', level=logging.DEBUG) telegram_handler.handlers.logger.handlers = [] telegram_handler.handlers.logger.addHandler(MockLoggingHandler()) telegram_handler.handlers.logger.level = logging.DEBUG return handler def test_emit(handler): record = logging.makeLogRecord({'msg': 'hello'}) with mock.patch('requests.post') as patch: handler.emit(record) assert patch.called assert patch.call_count == 1 assert patch.call_args[1]['json']['chat_id'] == 'bar' assert 'hello' in patch.call_args[1]['json']['text'] assert patch.call_args[1]['json']['parse_mode'] == 'HTML' def test_emit_big_message(handler): message = '*' * telegram_handler.handlers.MAX_MESSAGE_LEN record = logging.makeLogRecord({'msg': message}) with mock.patch('requests.post') as patch: handler.emit(record) assert patch.called assert patch.call_count == 1 def test_emit_http_exception(handler): record = logging.makeLogRecord({'msg': 'hello'}) with mock.patch('requests.post') as patch: response = requests.Response() response.status_code = 500 response._content = 'Server error'.encode() patch.return_value = response handler.emit(record) assert telegram_handler.handlers.logger.handlers[0].messages['error'] assert telegram_handler.handlers.logger.handlers[0].messages['debug'] def test_emit_telegram_error(handler): record = logging.makeLogRecord({'msg': 'hello'}) with mock.patch('requests.post') as patch: response = requests.Response() response.status_code = 200 response._content = json.dumps({'ok': False}).encode() patch.return_value = response handler.emit(record) assert telegram_handler.handlers.logger.handlers[0].messages['warning'] def test_get_chat_id_success(handler): with mock.patch('requests.post') as patch: response = requests.Response() response.status_code = 200 response._content = json.dumps({'ok': True, 'result': [{'message': {'chat': {'id': 'foo'}}}]}).encode() patch.return_value = response assert handler.get_chat_id() == 'foo' def test_get_chat_id_telegram_error(handler): with mock.patch('requests.post') as patch: response = requests.Response() response.status_code = 200 response._content = json.dumps({'ok': False}).encode() patch.return_value = response assert handler.get_chat_id() is None assert telegram_handler.handlers.logger.handlers[0].messages['error'] def test_get_chat_id_no_response(handler): with mock.patch.object(handler, 'request') as patch: patch.return_value = None value = handler.get_chat_id() assert value is None patch.assert_called_once() def test_get_chat_id_response_invalid_format(handler): with mock.patch('requests.post') as patch: response = requests.Response() response.status_code = 200 response._content = json.dumps({'ok': True, 'result': []}).encode() patch.return_value = response assert handler.get_chat_id() is None assert telegram_handler.handlers.logger.handlers[0].messages['error'] assert telegram_handler.handlers.logger.handlers[0].messages['debug'] def test_handler_init_without_chat(): with mock.patch('requests.post') as patch: response = requests.Response() response.status_code = 200 response._content = json.dumps({'ok': False}).encode() patch.return_value = response handler = telegram_handler.handlers.TelegramHandler('foo', level=logging.INFO) assert patch.called assert telegram_handler.handlers.logger.handlers[0].messages['error'] assert handler.level == logging.NOTSET def test_handler_respects_proxy(): proxies = { 'http': 'http_proxy_sample', 'https': 'https_proxy_sample', } handler = telegram_handler.handlers.TelegramHandler('foo', 'bar', level=logging.INFO, proxies=proxies) record = logging.makeLogRecord({'msg': 'hello'}) with mock.patch('requests.post') as patch: handler.emit(record) assert patch.call_args[1]['proxies'] == proxies def test_custom_formatter(handler): handler.setFormatter(logging.Formatter()) record = logging.makeLogRecord({'msg': 'hello'}) with mock.patch('requests.post') as patch: handler.emit(record) assert 'parse_mode' not in patch.call_args[1]['json'] ", https://github.com/scikit-hep/scikit-hep/,e78234da9e754e380a7d871afb56b20baa73b99b,tests/dataset/test_numpydataset.py::test_methods,tests/dataset/test_numpydataset.py,OD-Vic,flaky,Deleted,nan,classify;root_cause,"#!/usr/bin/env python # Licensed under a 3-clause BSD style license, see LICENSE. """""" Tests for the skhep.dataset.numpydataset module. """""" # ----------------------------------------------------------------------------- # Import statements # ----------------------------------------------------------------------------- import pytest from pytest import approx import os import numpy as np from skhep.utils.provenance import ObjectOrigin # these are not imported automatically from skhep.dataset.numpydataset import * from skhep.dataset.selection import Selection # ----------------------------------------------------------------------------- # Actual tests # ----------------------------------------------------------------------------- ar = np.array([(1,1),(2,2),(3,3)],dtype=[('x',int), ('y',int)]) def test_constructors(): ds = NumpyDataset(ar) a = SkhepNumpyArray([1,2,3]) def test_properties(): ds = NumpyDataset(ar, ObjectOrigin('simple_array')) assert type(ds.data) == np.ndarray assert ds.provenance[0].__repr__() == '' assert ds.provenance.__repr__() == '' assert ds.mutable == True assert ds.immutable == False assert ds.transient == True assert ds.persistent == False with pytest.raises(NotImplementedError): ds.datashape def test_methods(): ds1 = NumpyDataset(ar) ds1.__repr__() assert ds1.__str__() == ""[(1, 1) (2, 2) (3, 3)]"" assert ds1.nentries == 3 assert ds1.nevents == 3 assert ds1.variables == [""x"", ""y""] assert ds1.keys() == [""x"", ""y""] ds1.to_file('npds.npz') ds2 = NumpyDataset.from_file('npds.npz') assert ds2.provenance[0].__repr__() == '' os.remove('npds.npz') with pytest.raises(IOError): ds = NumpyDataset.from_file('non_existent_file') ds3 = ds1.copy() assert ds1['x'].tolist() == [1,2,3] assert ds1.x.tolist() == [1,2,3] assert ds1.x.name == ""x"" assert ds1.x.provenance[0].detail == repr(ds1) ds1.z = np.ones((3,)) assert ds1['z'].tolist() == [1,1,1] assert ds1.z.tolist() == [1,1,1] assert ds1.provenance[-1].__repr__() == """" ds1['w'] = np.zeros((3,)) assert ds1.provenance[-1].__repr__() == """" assert ds1['w'].tolist() == [0,0,0] assert ds1.w.tolist() == [0,0,0] with pytest.raises(ValueError): ds1.__setitem__('h',np.ones((4,))) with pytest.raises(ValueError): ds1.__setitem__('k',""array"") ds1.z = ds1.w assert ds1.provenance[-1].__repr__() =="""" ds1.w = np.array([6,7,8]) assert ds1.provenance[-1].__repr__() =="""" def test_transformations(): ds1 = NumpyDataset(ar) ds1.x += 1 assert repr(ds1.provenance[-1]) == """" ds1.x *= 2 assert repr(ds1.provenance[-1]) == """" ds1.x -= 3 assert repr(ds1.provenance[-1]) == """" ds1.x /= 4 assert repr(ds1.provenance[-1]) == """" ds1.r = (ds1.x**2 + ds1.y**2)**0.5 ds1.expx = np.exp(ds1.x) def test_operations(): a = SkhepNumpyArray([1,2,3,4], name = ""a"") b = SkhepNumpyArray([0,1,2,3], name = ""b"") np.add.at(a, b, 1) assert a.all() == SkhepNumpyArray([2, 3, 4, 5]).all() c = np.exp(a) print(c[0]) assert c[0] == approx(np.exp(2)) assert c.provenance[-1].detail == ""exp( a )"" def test_concatenate(): ar1 = np.array([(1,1),(2,2),(3,3)],dtype=[('x',int), ('y',int)]) ar2 = np.array([(4,-2),(0,7),(4,3)],dtype=[('x',int), ('y',int)]) ar3 = np.array([(3,-1),(2,5),(1,1)],dtype=[('x',int), ('y',int)]) ds1 = NumpyDataset(ar1) ds2 = NumpyDataset(ar2) ds3 = NumpyDataset(ar3) with pytest.raises(TypeError): ds = concatenate(ds1) with pytest.raises(TypeError): ds = concatenate([ds1,""a""]) ds = concatenate([ds1, ds2, ds3]) assert ds.nentries == ds1.nentries + ds2.nentries + ds3.nentries assert ds[-1][0] == 1 assert ds[-1][1] == 1 assert repr(ds.provenance) == """" # details = ""0: {0}\n"".format(ds1.provenance.detail) # details += ""1: {0}\n"".format(ds2.provenance.detail) # details += ""2: {0}"".format(ds3.provenance.detail) # assert ds.provenance.detail == details ds1_ = ds1.select(""x > 1"") ds2_ = ds2.select(""x > 1"") ds3_ = ds3.select(""x > 1"") ds_ = concatenate([ds1_, ds2_, ds3_]) assert ds_.nentries == ds.select(""x > 1"").nentries assert repr(ds_.provenance) == """" # details = ""0: 0: {0}\n"".format(ds1_.provenance[0].detail) # details += "" 1: {0}\n"".format(ds1_.provenance[1].detail) # details += ""1: 0: {0}\n"".format(ds2_.provenance[0].detail) # details += "" 1: {0}\n"".format(ds2_.provenance[1].detail) # details += ""2: 0: {0}\n"".format(ds3_.provenance[0].detail) # details += "" 1: {0}"".format(ds3_.provenance[1].detail) # assert ds_.provenance.detail == details def test_selections(): ar = np.array([(1,1),(2,2),(3,3)],dtype=[('x',int), ('y',int)]) ds1 = NumpyDataset(ar) ds1.z = (ds1.x**2 + ds1.y**2)**0.5 sel = Selection(""x > 1"") ds2 = ds1.select(sel) ds2.provenance[-1] == "" 1), applied)>"" assert ds2.__str__() == ds1.select(""x > 1"").__str__() assert ds2.provenance[-1].detail == (""Selection, x > 1, applied (#events before = 3, "" + ""#events after = 2, Efficiency = 66.6667 +/- 27.2166 %)"") ds3 = ds1[ds1.x > 1] a = ds1.x > 1 print(ds3.provenance[-1]) assert repr(ds3.provenance[-1]) == "" 1)>"" ds4 = ds1.select(ds1.x > 1) assert repr(ds4.provenance[-1]) == "" 1)>"" ds5 = ds1.select(""(x > 1) & (y > 1)"") ds6 = ds1.select(""min(x,y) > 1"") ds7 = ds1.select(""max(x,y) > 1"") ds8 = ds1.select(""( x * y ) > 1"") ds9 = ds1.select(""max(x,y,z) > 1"") with pytest.raises(ValueError): ds10 = ds1.copy() ds10.select(ds8.x) ", https://github.com/sdrobert/pydrobert-gpyopt,ce2699ca89687f848c2a86db44eed125d65886de,test_gpyopt.py::test_strings_are_provided,test_gpyopt.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import from __future__ import division from __future__ import print_function import os from tempfile import mkdtemp from shutil import rmtree import pytest import pydrobert.gpyopt as gpyopt import numpy as np __author__ = ""Sean Robertson"" __email__ = ""sdrobert@cs.toronto.edu"" __license__ = ""Apache 2.0"" __copyright__ = ""Copyright 2018 Sean Robertson"" @pytest.fixture def temp_dir(): dir_name = mkdtemp() yield dir_name rmtree(dir_name) def _polynomial(x, d, shift, noise=None): if noise is None: noise = 0. elif noise == 'random': noise = np.random.random() return x ** d + shift + noise def _squared(x, shift): return _polynomial(x, 2, shift) def _day_cannot_be_tuesday(day, some_other_value=1): return day != 'tuesday' def _int_not_odd(int_): return bool(1 - int_ % 2) def _enforcer_objective(int_, day): assert _day_cannot_be_tuesday(day) assert _int_not_odd(int_) return np.random.randn() def test_simple_objective(): wrapper = gpyopt.GPyOptObjectiveWrapper(_squared) wrapper.set_fixed_parameter('shift', 1.) wrapper.set_variable_parameter('x', 'continuous', (-1., 1.)) params = gpyopt.BayesianOptimizationParams( model_type='gp', kernel_type='rbf', seed=1, max_samples=10, noise_var=0, ) best_1 = gpyopt.bayesopt(wrapper, params) best_2 = gpyopt.bayesopt(wrapper, params) assert best_1['x'] == best_2['x'] params.log10_min_diff = -3 params.max_samples = None best_3 = gpyopt.bayesopt(wrapper, params) # note that log10_min_diff is not the same as the absolute # value between best results assert abs(_squared(best_3['x'], 1.) - 1.) < 1e-2 @pytest.mark.xfail def test_constraints(): wrapper = gpyopt.GPyOptObjectiveWrapper(_enforcer_objective) wrapper.set_variable_parameter('int_', 'discrete', tuple(range(100))) wrapper.set_variable_parameter('day', 'categorical', ('monday', 'tuesday')) params = gpyopt.BayesianOptimizationParams( initial_design_samples=4, max_iterations=5, seed=2, ) best = gpyopt.bayesopt(wrapper, params, constraints=[ _day_cannot_be_tuesday, _int_not_odd, ]) assert best['int_'] in range(0, 100, 2) assert best['day'] == 'monday' def test_multiple_variable_types(): wrapper = gpyopt.GPyOptObjectiveWrapper(_polynomial) wrapper.set_variable_parameter('shift', 'categorical', (10, 0, 1)) wrapper.set_variable_parameter('x', 'continuous', (.1, .9)) wrapper.set_variable_parameter('d', 'discrete', tuple(range(-1, 5))) params = gpyopt.BayesianOptimizationParams( model_type='gp', seed=1, log10_min_diff=-2, initial_design_samples=10, noise_var=0, ) best = gpyopt.bayesopt(wrapper, params) assert not best['shift'] assert best['d'] == 5 @pytest.mark.parametrize('inter_max_samples', [4, 5, 6]) def test_restart_with_seed_gives_same_predictions(temp_dir, inter_max_samples): wrapper = gpyopt.GPyOptObjectiveWrapper(_polynomial) wrapper.set_variable_parameter('x', 'continuous', (-1., 1.)) wrapper.set_fixed_parameter('d', 2) wrapper.set_fixed_parameter('shift', 1) hist_path = os.path.join(temp_dir, 'hist.csv') params = gpyopt.BayesianOptimizationParams( max_samples=inter_max_samples, model_type='gp', seed=1, ) gpyopt.bayesopt(wrapper, params, hist_path) params.max_samples = 8 best_1 = gpyopt.bayesopt(wrapper, params, hist_path) best_2 = gpyopt.bayesopt(wrapper, params) assert abs(best_1['x'] - best_2['x']) < 1e-5 def test_can_serialize_lots_of_stuff(temp_dir): def _obj(**kwargs): return 0. wrapper = gpyopt.GPyOptObjectiveWrapper(_obj) wrapper.set_fixed_parameter('a', object()) wrapper.set_variable_parameter('b', 'continuous', (0., 1.)) wrapper.set_variable_parameter('c', 'discrete', (0, 1)) d_tup = (1, None, 'None', 0., object()) wrapper.set_variable_parameter('d', 'categorical', d_tup) hist_path = os.path.join(temp_dir, 'hist.csv') params = gpyopt.BayesianOptimizationParams( initial_design_samples=4, max_samples=5, seed=5, ) best_1 = gpyopt.bayesopt(wrapper, params, hist_path) assert best_1['d'] in d_tup best_2 = gpyopt.bayesopt(wrapper, params, hist_path) assert best_1['c'] == best_2['c'] assert abs(best_1['b'] - best_2['b']) < 1e-5 assert best_1['d'] == best_2['d'] def test_grid(): X = np.zeros(10) def _obj(x): X[int(x)] = 1. return 0. wrapper = gpyopt.GPyOptObjectiveWrapper(_obj) wrapper.set_variable_parameter('x', 'discrete', tuple(range(10))) def test_strings_are_provided(): def _obj(mystring): assert isinstance(mystring, str) return int(mystring[0]) wrapper = gpyopt.GPyOptObjectiveWrapper(_obj) wrapper.set_variable_parameter('mystring', 'categorical', '1234') params = gpyopt.BayesianOptimizationParams( initial_design_numdata=2, max_samples=10, ) best = gpyopt.bayesopt(wrapper, params) assert best['mystring'] == '1' ", https://github.com/seanbreckenridge/keepassxc-pwned,aead83c1749b62006a28526905d8fee5cd2ae902,tests/test_user_input.py::test_user_input_password_not_found,tests/test_user_input.py,NIO,flaky,Accepted,https://github.com/seanbreckenridge/keepassxc-pwned/pull/18,classify;root_cause;fix_proposal,"import vcr from unittest.mock import patch from .common import * from getpass import getpass @vcr.use_cassette(""tests/vcr_cassettes/test_user_password_fails.yaml"") @patch(""getpass.getpass"") def test_user_input_password_found(getpass, capsys): getpass.return_value = ""test"" with pytest.raises(SystemExit): import keepassxc_pwned.__main__ captured = capsys.readouterr() captured_lines = captured.out.splitlines() assert len(captured_lines) == 1 assert captured_lines[0] == ""Found password 76479 times!"" @vcr.use_cassette(""tests/vcr_cassettes/test_user_password_succeeds.yaml"") @patch(""getpass.getpass"") def test_user_input_password_not_found(getpass, capsys): getpass.return_value = ""^lL2fY@B82C[UHbpd`B[0QO#Ky5Im-D+63F%nwe40iQ*Pt`Sqyn6Q0u254.2I?Vf4!+U83[lYl[=E6iU6v=hZ-9a5y3=o.^VO36="" import keepassxc_pwned.__main__ captured = capsys.readouterr() captured_lines = captured.out.splitlines() assert len(captured_lines) == 1 assert captured_lines[0] == ""Could not find that password in the dataset."" ", https://github.com/seanpar203/event-bus,5353d6472926a299ab4f3a8c33114a725d5b7639,tests/test_event_emit.py::test_event_emit,tests/test_event_emit.py,NIO,flaky,Opened,https://github.com/seanpar203/event-bus/pull/12,classify;root_cause,""""""" Tests event being emitted and proper functions being called. """""" from event_bus.bus import EventBus # Constants bus = EventBus() GLOBAL_VAR = 'Init' EVENT_NAME = 'completed' @bus.on(event=EVENT_NAME) def subscription(): """""" Subscribed event to run after event `completed` """""" global GLOBAL_VAR GLOBAL_VAR = 'Finished' def test_event_emit(): """""" Tests that a function subscribed to an event executes on emit. """""" # Before Emit assert GLOBAL_VAR == 'Init' bus.emit(EVENT_NAME) # After Emit assert GLOBAL_VAR == 'Finished' ", https://github.com/seanpar203/event-bus,5353d6472926a299ab4f3a8c33114a725d5b7639,tests/test_subscribed_removal.py::test_suscribed_event_was_removed,tests/test_subscribed_removal.py,NIO,flaky,Opened,https://github.com/seanpar203/event-bus/pull/13,classify;root_cause,""""""" Tests removing subscribed events. """""" from event_bus import EventBus from event_bus.exceptions import EventDoesntExist bus = EventBus() # Constants(Well kinda, we manipulate them in subscribed events) EVENT_NAME = 'event' @bus.on(event=EVENT_NAME) def event_one(): """""" Mock event. """""" pass def test_suscribed_event_was_removed(): """""" Checks to make sure that the removal of subscribed event works. """""" before_count = bus.event_count bus.remove_event(event=EVENT_NAME, func_name=event_one.__name__) after_count = bus.event_count assert before_count == 1 assert after_count == 0 def test_removing_event_that_doest_exist(): """""" Checks to make sure that the removal of subscribed event works. """""" before_count = bus.event_count try: bus.remove_event(event=EVENT_NAME, func_name='hello') except EventDoesntExist: assert before_count == bus.event_count ", https://github.com/securenative/securenative-python,0eae99733b7565341487ad32f044be8dc51032cd,tests/configuration_manager_test.py::ConfigurationManagerTest::test_default_values_for_invalid_enum_config_props,tests/configuration_manager_test.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import configparser import os import platform import unittest from securenative.config.configuration_manager import ConfigurationManager from securenative.enums.failover_strategy import FailOverStrategy class ConfigurationManagerTest(unittest.TestCase): def setUp(self): self.config_file_path = ""/tmp/securenative.ini"" def create_ini_file(self, conf): os.environ[""SECURENATIVE_COMFIG_FILE""] = self.config_file_path try: os.remove(self.config_file_path) except FileNotFoundError: pass config = configparser.ConfigParser() for key, value in conf.items(): config.set(""DEFAULT"", key.upper(), value) with open(self.config_file_path, ""w"") as configfile: config.write(configfile) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_parse_config_file_correctly(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_API_KEY"": ""SOME_API_KEY"", ""SECURENATIVE_APP_NAME"": ""SOME_APP_NAME"", ""SECURENATIVE_API_URL"": ""SOME_API_URL"", ""SECURENATIVE_INTERVAL"": ""1000"", ""SECURENATIVE_HEARTBEAT_INTERVAL"": ""5000"", ""SECURENATIVE_MAX_EVENTS"": ""100"", ""SECURENATIVE_TIMEOUT"": ""1500"", ""SECURENATIVE_AUTO_SEND"": ""True"", ""SECURENATIVE_DISABLE"": ""False"", ""SECURENATIVE_LOG_LEVEL"": ""Critical"", ""SECURENATIVE_FAILOVER_STRATEGY"": ""fail-closed"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.api_key, ""SOME_API_KEY"") self.assertEqual(options.api_url, ""SOME_API_URL"") self.assertEqual(options.auto_send, ""True"") self.assertEqual(options.disable, ""False"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_CLOSED.value) self.assertEqual(options.interval, ""1000"") self.assertEqual(options.log_level, ""Critical"") self.assertEqual(options.max_events, ""100"") self.assertEqual(options.timeout, ""1500"") @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_ignore_unknown_config_in_properties_file(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_TIMEOUT"": ""1500"", ""SECURENATIVE_UNKNOWN_KEY"": ""SOME_UNKNOWN_KEY"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.timeout, ""1500"") @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_handle_invalid_config_file(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = {""bla"": ""bla""} self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def ignore_invalid_config_file_entries(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_API_KEY"": 1, ""SECURENATIVE_API_URL"": 3, ""SECURENATIVE_TIMEOUT"": ""bad timeout"", ""SECURENATIVE_FAILOVER_STRATEGY"": ""fail-what"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_OPEN) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_load_default_config(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertIsNone(options.api_key) self.assertEqual(options.api_url, ""https://api.securenative.com/collector/api/v1"") self.assertEqual(str(options.interval), ""1000"") self.assertEqual(options.timeout, ""1500"") self.assertEqual(str(options.max_events), ""1000"") self.assertEqual(str(options.auto_send), ""True"") self.assertEqual(str(options.disable), ""False"") self.assertEqual(options.log_level, ""CRITICAL"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_OPEN.value) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_get_config_from_env_variables(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass os.environ[""SECURENATIVE_API_KEY""] = ""SOME_ENV_API_KEY"" os.environ[""SECURENATIVE_API_URL""] = ""SOME_API_URL"" os.environ[""SECURENATIVE_INTERVAL""] = ""6000"" os.environ[""SECURENATIVE_MAX_EVENTS""] = ""700"" os.environ[""SECURENATIVE_TIMEOUT""] = ""1700"" os.environ[""SECURENATIVE_AUTO_SEND""] = ""False"" os.environ[""SECURENATIVE_DISABLE""] = ""True"" os.environ[""SECURENATIVE_LOG_LEVEL""] = ""Debug"" os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] = ""fail-closed"" options = ConfigurationManager.load_config(None) self.assertEqual(options.api_key, ""SOME_ENV_API_KEY"") self.assertEqual(options.api_url, ""SOME_API_URL"") self.assertEqual(options.interval, ""6000"") self.assertEqual(options.timeout, ""1700"") self.assertEqual(options.max_events, ""700"") self.assertEqual(options.auto_send, ""False"") self.assertEqual(options.disable, ""True"") self.assertEqual(options.log_level, ""Debug"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_CLOSED.value) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_default_values_for_invalid_enum_config_props(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.envir", https://github.com/securenative/securenative-python,0eae99733b7565341487ad32f044be8dc51032cd,tests/configuration_manager_test.py::ConfigurationManagerTest::test_get_config_from_env_variables,tests/configuration_manager_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import configparser import os import platform import unittest from securenative.config.configuration_manager import ConfigurationManager from securenative.enums.failover_strategy import FailOverStrategy class ConfigurationManagerTest(unittest.TestCase): def setUp(self): self.config_file_path = ""/tmp/securenative.ini"" def create_ini_file(self, conf): os.environ[""SECURENATIVE_COMFIG_FILE""] = self.config_file_path try: os.remove(self.config_file_path) except FileNotFoundError: pass config = configparser.ConfigParser() for key, value in conf.items(): config.set(""DEFAULT"", key.upper(), value) with open(self.config_file_path, ""w"") as configfile: config.write(configfile) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_parse_config_file_correctly(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_API_KEY"": ""SOME_API_KEY"", ""SECURENATIVE_APP_NAME"": ""SOME_APP_NAME"", ""SECURENATIVE_API_URL"": ""SOME_API_URL"", ""SECURENATIVE_INTERVAL"": ""1000"", ""SECURENATIVE_HEARTBEAT_INTERVAL"": ""5000"", ""SECURENATIVE_MAX_EVENTS"": ""100"", ""SECURENATIVE_TIMEOUT"": ""1500"", ""SECURENATIVE_AUTO_SEND"": ""True"", ""SECURENATIVE_DISABLE"": ""False"", ""SECURENATIVE_LOG_LEVEL"": ""Critical"", ""SECURENATIVE_FAILOVER_STRATEGY"": ""fail-closed"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.api_key, ""SOME_API_KEY"") self.assertEqual(options.api_url, ""SOME_API_URL"") self.assertEqual(options.auto_send, ""True"") self.assertEqual(options.disable, ""False"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_CLOSED.value) self.assertEqual(options.interval, ""1000"") self.assertEqual(options.log_level, ""Critical"") self.assertEqual(options.max_events, ""100"") self.assertEqual(options.timeout, ""1500"") @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_ignore_unknown_config_in_properties_file(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_TIMEOUT"": ""1500"", ""SECURENATIVE_UNKNOWN_KEY"": ""SOME_UNKNOWN_KEY"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.timeout, ""1500"") @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_handle_invalid_config_file(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = {""bla"": ""bla""} self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def ignore_invalid_config_file_entries(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_API_KEY"": 1, ""SECURENATIVE_API_URL"": 3, ""SECURENATIVE_TIMEOUT"": ""bad timeout"", ""SECURENATIVE_FAILOVER_STRATEGY"": ""fail-what"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_OPEN) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_load_default_config(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertIsNone(options.api_key) self.assertEqual(options.api_url, ""https://api.securenative.com/collector/api/v1"") self.assertEqual(str(options.interval), ""1000"") self.assertEqual(options.timeout, ""1500"") self.assertEqual(str(options.max_events), ""1000"") self.assertEqual(str(options.auto_send), ""True"") self.assertEqual(str(options.disable), ""False"") self.assertEqual(options.log_level, ""CRITICAL"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_OPEN.value) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_get_config_from_env_variables(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass os.environ[""SECURENATIVE_API_KEY""] = ""SOME_ENV_API_KEY"" os.environ[""SECURENATIVE_API_URL""] = ""SOME_API_URL"" os.environ[""SECURENATIVE_INTERVAL""] = ""6000"" os.environ[""SECURENATIVE_MAX_EVENTS""] = ""700"" os.environ[""SECURENATIVE_TIMEOUT""] = ""1700"" os.environ[""SECURENATIVE_AUTO_SEND""] = ""False"" os.environ[""SECURENATIVE_DISABLE""] = ""True"" os.environ[""SECURENATIVE_LOG_LEVEL""] = ""Debug"" os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] = ""fail-closed"" options = ConfigurationManager.load_config(None) self.assertEqual(options.api_key, ""SOME_ENV_API_KEY"") self.assertEqual(options.api_url, ""SOME_API_URL"") self.assertEqual(options.interval, ""6000"") self.assertEqual(options.timeout, ""1700"") self.assertEqual(options.max_events, ""700"") self.assertEqual(options.auto_send, ""False"") self.assertEqual(options.disable, ""True"") self.assertEqual(options.log_level, ""Debug"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_CLOSED.value) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_default_values_for_invalid_enum_config_props(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.envir", https://github.com/securenative/securenative-python,0eae99733b7565341487ad32f044be8dc51032cd,tests/configuration_manager_test.py::ConfigurationManagerTest::test_ignore_unknown_config_in_properties_file,tests/configuration_manager_test.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import configparser import os import platform import unittest from securenative.config.configuration_manager import ConfigurationManager from securenative.enums.failover_strategy import FailOverStrategy class ConfigurationManagerTest(unittest.TestCase): def setUp(self): self.config_file_path = ""/tmp/securenative.ini"" def create_ini_file(self, conf): os.environ[""SECURENATIVE_COMFIG_FILE""] = self.config_file_path try: os.remove(self.config_file_path) except FileNotFoundError: pass config = configparser.ConfigParser() for key, value in conf.items(): config.set(""DEFAULT"", key.upper(), value) with open(self.config_file_path, ""w"") as configfile: config.write(configfile) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_parse_config_file_correctly(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_API_KEY"": ""SOME_API_KEY"", ""SECURENATIVE_APP_NAME"": ""SOME_APP_NAME"", ""SECURENATIVE_API_URL"": ""SOME_API_URL"", ""SECURENATIVE_INTERVAL"": ""1000"", ""SECURENATIVE_HEARTBEAT_INTERVAL"": ""5000"", ""SECURENATIVE_MAX_EVENTS"": ""100"", ""SECURENATIVE_TIMEOUT"": ""1500"", ""SECURENATIVE_AUTO_SEND"": ""True"", ""SECURENATIVE_DISABLE"": ""False"", ""SECURENATIVE_LOG_LEVEL"": ""Critical"", ""SECURENATIVE_FAILOVER_STRATEGY"": ""fail-closed"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.api_key, ""SOME_API_KEY"") self.assertEqual(options.api_url, ""SOME_API_URL"") self.assertEqual(options.auto_send, ""True"") self.assertEqual(options.disable, ""False"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_CLOSED.value) self.assertEqual(options.interval, ""1000"") self.assertEqual(options.log_level, ""Critical"") self.assertEqual(options.max_events, ""100"") self.assertEqual(options.timeout, ""1500"") @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_ignore_unknown_config_in_properties_file(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_TIMEOUT"": ""1500"", ""SECURENATIVE_UNKNOWN_KEY"": ""SOME_UNKNOWN_KEY"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.timeout, ""1500"") @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_handle_invalid_config_file(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = {""bla"": ""bla""} self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def ignore_invalid_config_file_entries(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_API_KEY"": 1, ""SECURENATIVE_API_URL"": 3, ""SECURENATIVE_TIMEOUT"": ""bad timeout"", ""SECURENATIVE_FAILOVER_STRATEGY"": ""fail-what"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_OPEN) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_load_default_config(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertIsNone(options.api_key) self.assertEqual(options.api_url, ""https://api.securenative.com/collector/api/v1"") self.assertEqual(str(options.interval), ""1000"") self.assertEqual(options.timeout, ""1500"") self.assertEqual(str(options.max_events), ""1000"") self.assertEqual(str(options.auto_send), ""True"") self.assertEqual(str(options.disable), ""False"") self.assertEqual(options.log_level, ""CRITICAL"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_OPEN.value) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_get_config_from_env_variables(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass os.environ[""SECURENATIVE_API_KEY""] = ""SOME_ENV_API_KEY"" os.environ[""SECURENATIVE_API_URL""] = ""SOME_API_URL"" os.environ[""SECURENATIVE_INTERVAL""] = ""6000"" os.environ[""SECURENATIVE_MAX_EVENTS""] = ""700"" os.environ[""SECURENATIVE_TIMEOUT""] = ""1700"" os.environ[""SECURENATIVE_AUTO_SEND""] = ""False"" os.environ[""SECURENATIVE_DISABLE""] = ""True"" os.environ[""SECURENATIVE_LOG_LEVEL""] = ""Debug"" os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] = ""fail-closed"" options = ConfigurationManager.load_config(None) self.assertEqual(options.api_key, ""SOME_ENV_API_KEY"") self.assertEqual(options.api_url, ""SOME_API_URL"") self.assertEqual(options.interval, ""6000"") self.assertEqual(options.timeout, ""1700"") self.assertEqual(options.max_events, ""700"") self.assertEqual(options.auto_send, ""False"") self.assertEqual(options.disable, ""True"") self.assertEqual(options.log_level, ""Debug"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_CLOSED.value) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_default_values_for_invalid_enum_config_props(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.envir", https://github.com/securenative/securenative-python,0eae99733b7565341487ad32f044be8dc51032cd,tests/configuration_manager_test.py::ConfigurationManagerTest::test_load_default_config,tests/configuration_manager_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import configparser import os import platform import unittest from securenative.config.configuration_manager import ConfigurationManager from securenative.enums.failover_strategy import FailOverStrategy class ConfigurationManagerTest(unittest.TestCase): def setUp(self): self.config_file_path = ""/tmp/securenative.ini"" def create_ini_file(self, conf): os.environ[""SECURENATIVE_COMFIG_FILE""] = self.config_file_path try: os.remove(self.config_file_path) except FileNotFoundError: pass config = configparser.ConfigParser() for key, value in conf.items(): config.set(""DEFAULT"", key.upper(), value) with open(self.config_file_path, ""w"") as configfile: config.write(configfile) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_parse_config_file_correctly(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_API_KEY"": ""SOME_API_KEY"", ""SECURENATIVE_APP_NAME"": ""SOME_APP_NAME"", ""SECURENATIVE_API_URL"": ""SOME_API_URL"", ""SECURENATIVE_INTERVAL"": ""1000"", ""SECURENATIVE_HEARTBEAT_INTERVAL"": ""5000"", ""SECURENATIVE_MAX_EVENTS"": ""100"", ""SECURENATIVE_TIMEOUT"": ""1500"", ""SECURENATIVE_AUTO_SEND"": ""True"", ""SECURENATIVE_DISABLE"": ""False"", ""SECURENATIVE_LOG_LEVEL"": ""Critical"", ""SECURENATIVE_FAILOVER_STRATEGY"": ""fail-closed"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.api_key, ""SOME_API_KEY"") self.assertEqual(options.api_url, ""SOME_API_URL"") self.assertEqual(options.auto_send, ""True"") self.assertEqual(options.disable, ""False"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_CLOSED.value) self.assertEqual(options.interval, ""1000"") self.assertEqual(options.log_level, ""Critical"") self.assertEqual(options.max_events, ""100"") self.assertEqual(options.timeout, ""1500"") @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_ignore_unknown_config_in_properties_file(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_TIMEOUT"": ""1500"", ""SECURENATIVE_UNKNOWN_KEY"": ""SOME_UNKNOWN_KEY"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.timeout, ""1500"") @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_handle_invalid_config_file(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = {""bla"": ""bla""} self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def ignore_invalid_config_file_entries(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_API_KEY"": 1, ""SECURENATIVE_API_URL"": 3, ""SECURENATIVE_TIMEOUT"": ""bad timeout"", ""SECURENATIVE_FAILOVER_STRATEGY"": ""fail-what"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_OPEN) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_load_default_config(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertIsNone(options.api_key) self.assertEqual(options.api_url, ""https://api.securenative.com/collector/api/v1"") self.assertEqual(str(options.interval), ""1000"") self.assertEqual(options.timeout, ""1500"") self.assertEqual(str(options.max_events), ""1000"") self.assertEqual(str(options.auto_send), ""True"") self.assertEqual(str(options.disable), ""False"") self.assertEqual(options.log_level, ""CRITICAL"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_OPEN.value) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_get_config_from_env_variables(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass os.environ[""SECURENATIVE_API_KEY""] = ""SOME_ENV_API_KEY"" os.environ[""SECURENATIVE_API_URL""] = ""SOME_API_URL"" os.environ[""SECURENATIVE_INTERVAL""] = ""6000"" os.environ[""SECURENATIVE_MAX_EVENTS""] = ""700"" os.environ[""SECURENATIVE_TIMEOUT""] = ""1700"" os.environ[""SECURENATIVE_AUTO_SEND""] = ""False"" os.environ[""SECURENATIVE_DISABLE""] = ""True"" os.environ[""SECURENATIVE_LOG_LEVEL""] = ""Debug"" os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] = ""fail-closed"" options = ConfigurationManager.load_config(None) self.assertEqual(options.api_key, ""SOME_ENV_API_KEY"") self.assertEqual(options.api_url, ""SOME_API_URL"") self.assertEqual(options.interval, ""6000"") self.assertEqual(options.timeout, ""1700"") self.assertEqual(options.max_events, ""700"") self.assertEqual(options.auto_send, ""False"") self.assertEqual(options.disable, ""True"") self.assertEqual(options.log_level, ""Debug"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_CLOSED.value) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_default_values_for_invalid_enum_config_props(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.envir", https://github.com/securenative/securenative-python,0eae99733b7565341487ad32f044be8dc51032cd,tests/configuration_manager_test.py::ConfigurationManagerTest::test_parse_config_file_correctly,tests/configuration_manager_test.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import configparser import os import platform import unittest from securenative.config.configuration_manager import ConfigurationManager from securenative.enums.failover_strategy import FailOverStrategy class ConfigurationManagerTest(unittest.TestCase): def setUp(self): self.config_file_path = ""/tmp/securenative.ini"" def create_ini_file(self, conf): os.environ[""SECURENATIVE_COMFIG_FILE""] = self.config_file_path try: os.remove(self.config_file_path) except FileNotFoundError: pass config = configparser.ConfigParser() for key, value in conf.items(): config.set(""DEFAULT"", key.upper(), value) with open(self.config_file_path, ""w"") as configfile: config.write(configfile) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_parse_config_file_correctly(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_API_KEY"": ""SOME_API_KEY"", ""SECURENATIVE_APP_NAME"": ""SOME_APP_NAME"", ""SECURENATIVE_API_URL"": ""SOME_API_URL"", ""SECURENATIVE_INTERVAL"": ""1000"", ""SECURENATIVE_HEARTBEAT_INTERVAL"": ""5000"", ""SECURENATIVE_MAX_EVENTS"": ""100"", ""SECURENATIVE_TIMEOUT"": ""1500"", ""SECURENATIVE_AUTO_SEND"": ""True"", ""SECURENATIVE_DISABLE"": ""False"", ""SECURENATIVE_LOG_LEVEL"": ""Critical"", ""SECURENATIVE_FAILOVER_STRATEGY"": ""fail-closed"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.api_key, ""SOME_API_KEY"") self.assertEqual(options.api_url, ""SOME_API_URL"") self.assertEqual(options.auto_send, ""True"") self.assertEqual(options.disable, ""False"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_CLOSED.value) self.assertEqual(options.interval, ""1000"") self.assertEqual(options.log_level, ""Critical"") self.assertEqual(options.max_events, ""100"") self.assertEqual(options.timeout, ""1500"") @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_ignore_unknown_config_in_properties_file(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_TIMEOUT"": ""1500"", ""SECURENATIVE_UNKNOWN_KEY"": ""SOME_UNKNOWN_KEY"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.timeout, ""1500"") @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_handle_invalid_config_file(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = {""bla"": ""bla""} self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def ignore_invalid_config_file_entries(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass config = { ""SECURENATIVE_API_KEY"": 1, ""SECURENATIVE_API_URL"": 3, ""SECURENATIVE_TIMEOUT"": ""bad timeout"", ""SECURENATIVE_FAILOVER_STRATEGY"": ""fail-what"" } self.create_ini_file(config) options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_OPEN) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_load_default_config(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass options = ConfigurationManager.load_config(None) self.assertIsNotNone(options) self.assertIsNone(options.api_key) self.assertEqual(options.api_url, ""https://api.securenative.com/collector/api/v1"") self.assertEqual(str(options.interval), ""1000"") self.assertEqual(options.timeout, ""1500"") self.assertEqual(str(options.max_events), ""1000"") self.assertEqual(str(options.auto_send), ""True"") self.assertEqual(str(options.disable), ""False"") self.assertEqual(options.log_level, ""CRITICAL"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_OPEN.value) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_get_config_from_env_variables(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.environ[""SECURENATIVE_AUTO_SEND""] del os.environ[""SECURENATIVE_DISABLE""] del os.environ[""SECURENATIVE_LOG_LEVEL""] del os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] except FileNotFoundError: pass except KeyError: pass os.environ[""SECURENATIVE_API_KEY""] = ""SOME_ENV_API_KEY"" os.environ[""SECURENATIVE_API_URL""] = ""SOME_API_URL"" os.environ[""SECURENATIVE_INTERVAL""] = ""6000"" os.environ[""SECURENATIVE_MAX_EVENTS""] = ""700"" os.environ[""SECURENATIVE_TIMEOUT""] = ""1700"" os.environ[""SECURENATIVE_AUTO_SEND""] = ""False"" os.environ[""SECURENATIVE_DISABLE""] = ""True"" os.environ[""SECURENATIVE_LOG_LEVEL""] = ""Debug"" os.environ[""SECURENATIVE_FAILOVER_STRATEGY""] = ""fail-closed"" options = ConfigurationManager.load_config(None) self.assertEqual(options.api_key, ""SOME_ENV_API_KEY"") self.assertEqual(options.api_url, ""SOME_API_URL"") self.assertEqual(options.interval, ""6000"") self.assertEqual(options.timeout, ""1700"") self.assertEqual(options.max_events, ""700"") self.assertEqual(options.auto_send, ""False"") self.assertEqual(options.disable, ""True"") self.assertEqual(options.log_level, ""Debug"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_CLOSED.value) @unittest.skipIf(platform.system() == ""Windows"" or platform.system() == ""windows"", ""test not supported on windows"") def test_default_values_for_invalid_enum_config_props(self): try: os.remove(self.config_file_path) del os.environ[""SECURENATIVE_API_KEY""] del os.environ[""SECURENATIVE_API_URL""] del os.environ[""SECURENATIVE_INTERVAL""] del os.environ[""SECURENATIVE_MAX_EVENTS""] del os.environ[""SECURENATIVE_TIMEOUT""] del os.envir", https://github.com/securenative/securenative-python,0eae99733b7565341487ad32f044be8dc51032cd,tests/securenative_test.py::SecureNativeTest::test_get_sdk_instance_without_init_throws,tests/securenative_test.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest from securenative.config.configuration_manager import ConfigurationManager from securenative.enums.failover_strategy import FailOverStrategy from securenative.exceptions.securenative_config_exception import SecureNativeConfigException from securenative.exceptions.securenative_sdk_Illegal_state_exception import SecureNativeSDKIllegalStateException from securenative.exceptions.securenative_sdk_exception import SecureNativeSDKException from securenative.securenative import SecureNative class SecureNativeTest(unittest.TestCase): def test_get_sdk_instance_without_init_throws(self): with self.assertRaises(SecureNativeSDKIllegalStateException): SecureNative.get_instance() def test_init_sdk_without_api_key_should_throw(self): with self.assertRaises(SecureNativeSDKException): SecureNative.init_with_options(ConfigurationManager.config_builder()) def test_init_sdk_with_empty_api_key_should_throw(self): with self.assertRaises(SecureNativeConfigException): SecureNativeSDKException, SecureNative.init_with_api_key("""") def test_init_sdk_with_api_key_and_defaults(self): SecureNative._flush() api_key = ""API_KEY"" securenative = SecureNative.init_with_api_key(api_key) options = securenative.get_options() self.assertEqual(options.api_key, api_key) self.assertEqual(options.api_url, ""https://api.securenative.com/collector/api/v1"") self.assertEqual(options.interval, 1000) self.assertEqual(options.timeout, 1500) self.assertEqual(options.max_events, 1000) self.assertEqual(options.auto_send, True) self.assertEqual(options.disable, False) self.assertEqual(options.log_level, ""CRITICAL"") self.assertEqual(options.fail_over_strategy, FailOverStrategy.FAIL_OPEN.value) def test_init_sdk_twice_will_throw(self): with self.assertRaises(SecureNativeSDKException): SecureNative.init_with_api_key(""API_KEY"") SecureNative.init_with_api_key(""API_KEY"") def test_init_sdk_with_api_key_and_get_instance(self): SecureNative._flush() api_key = ""API_KEY"" securenative = SecureNative.init_with_api_key(api_key) self.assertEqual(securenative, SecureNative.get_instance()) def test_init_sdk_with_builder(self): SecureNative._flush() securenative = SecureNative.init_with_options(SecureNative.config_builder(). with_api_key(""API_KEY""). with_max_events(10). with_log_level(""ERROR"")) options = securenative.get_options() self.assertEqual(options.api_key, ""API_KEY"") self.assertEqual(options.max_events, 10) self.assertEqual(options.log_level, ""ERROR"") ", https://github.com/SergeyShk/ruTS,af02f627d9022f0f4825d9bed4ffc97d3d329f2d,tests/test_utils.py::test_extract_archive,tests/test_utils.py,NOD,flaky,nan,nan,classify;root_cause,"import pytest import shutil from pathlib import Path from ruts.utils import download_file, extract_archive, safe_divide, to_path def test_to_path_is_str(): path = ""/usr/local/"" assert to_path(path) == Path(path) def test_to_path_is_path(): path = Path(""/usr/local/"") assert to_path(path) == path def test_to_path_type_error(): paths = [666, ['a', 'b'], {'a': 'b'}] for path in paths: with pytest.raises(TypeError): to_path(path) def test_download_file(): url_zip = ""https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/packages/corpora/stopwords.zip"" url_tar = ""https://bit.ly/30JYkQF"" filename = ""razdel.tar.gz"" dirpath = ""/tmp/ruts_download"" assert download_file(url_zip, dirpath=dirpath) == ""/tmp/ruts_download/stopwords.zip"" assert download_file(url_tar, filename=filename, dirpath=dirpath) == ""/tmp/ruts_download/razdel.tar.gz"" assert download_file(url_zip, dirpath=dirpath) == None def test_download_file_runtime_error(): url = ""https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/packages/corpora/stopword.zip"" dirpath = ""/tmp/ruts_download"" with pytest.raises(RuntimeError): download_file(url, dirpath=dirpath, force=True) def test_extract_archive(): archive_file_zip = ""/tmp/ruts_download/stopwords.zip"" archive_file_tar = ""/tmp/ruts_download/razdel.tar.gz"" extract_dir = ""/tmp/ruts_extract"" assert extract_archive(archive_file_zip, extract_dir=extract_dir) == ""/tmp/ruts_extract/stopwords"" assert extract_archive(archive_file_tar) == ""/tmp/ruts_download/razdel"" assert extract_archive(""/tmp/ruts_extract/stopwords/russian"") == ""/tmp/ruts_extract/stopwords"" shutil.rmtree(""/tmp/ruts_extract"", ignore_errors=True) shutil.rmtree(""/tmp/ruts_download"", ignore_errors=True) def test_safe_divide(): assert safe_divide(1, 5) == 0.2 assert safe_divide(1, 0) == 0 assert safe_divide(1, '', -1) == -1", https://github.com/sgaynetdinov/py-vkontakte,b03246b8d4b6f5ee5ab7e5c67e09e1f14b85fd9c,tests/test_message.py::test_message,tests/test_message.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest from vk.messages import Message message_json = { ""id"": 18000, ""date"": 621734400, ""out"": 1, ""user_id"": -151808000, ""read_state"": 1, ""title"": """", ""body"": ""Hello world"", ""random_id"": 597248445, } def test_message(factory): message = Message.from_json(None, message_json) assert isinstance(message, Message) assert message.date == 621734400 assert message.id == 18000 assert message.out == 1 assert message.body == ""Hello world"" assert message.random_id == 597248445 assert message.update_time == None @pytest.mark.parametrize('update_time, expected', [ (None, None), (100500, 100500), ]) def test_is_updated(update_time, expected): message_json['update_time'] = update_time message = Message.from_json(None, message_json) assert message.update_time == expected ", https://github.com/sjoerdk/anonapi,5e70dfd9fcb7dcc4e2345a6a153d9530f4886e73,tests/test_cli_create.py::test_job_parameter_set,tests/test_cli_create.py,NIO,flaky,Opened,https://github.com/sjoerdk/anonapi/pull/545,classify;root_cause,"from pathlib import Path, PureWindowsPath import pytest from anonapi.batch import BatchFolder from anonapi.cli.create_commands import ( main, JobParameterSet, ParameterMappingException, JobSetValidationError, ) from anonapi.mapper import MappingFolder from anonapi.parameters import ( SourceIdentifierParameter, FolderIdentifier, Description, SourceIdentifier, Parameter, PatientID, PIMSKey, RootSourcePath, ParameterException, ParameterSet, ) from anonapi.settings import JobDefaultParameters from tests.mock_responses import RequestsMockResponseExamples @pytest.fixture() def mock_requests_for_job_creation(mock_requests): """"""Mock requests library so that every call to it will return a standard anonapi Job created response """""" mock_requests.set_response(RequestsMockResponseExamples.JOB_CREATED_RESPONSE) return mock_requests def test_create_from_mapping_no_mapping(mock_main_runner): """"""Running from-mapping in a folder without mapping should not work"""""" result = mock_main_runner.invoke(main, ""from-mapping"") assert result.exit_code == 1 assert ""No mapping"" in result.output def test_create_from_mapping(mock_from_mapping_runner, mock_requests_for_job_creation): """"""Try from-mapping, creating jobs based on a mapping"""""" # Run and answer are you sure 'N' result = mock_from_mapping_runner.invoke( main, ""from-mapping"", input=""N"", catch_exceptions=False ) assert result.exit_code == 0 assert ""Cancelled"" in result.output assert mock_requests_for_job_creation.requests.post.call_count == 0 # Now answer Y result = mock_from_mapping_runner.invoke( main, ""from-mapping"", input=""Y"", catch_exceptions=False ) assert result.exit_code == 0 assert mock_requests_for_job_creation.requests.post.call_count == 20 batch_folder = BatchFolder(mock_from_mapping_runner.mock_context.current_dir) assert batch_folder.has_batch() assert batch_folder.load().job_ids == [ 1234 ] # only 1 id as mock requests returns the same job id each call def test_create_from_mapping_server_error(mock_from_mapping_runner, mock_requests): """"""Try from-mapping, when job creation will yield an error from server"""""" mock_requests.set_responses( [RequestsMockResponseExamples.ERROR_USER_NOT_CONNECTED_TO_PROJECT] ) result = mock_from_mapping_runner.invoke( main, ""from-mapping"", input=""Y"", catch_exceptions=False ) assert result.exit_code == 0 assert mock_requests.requests.post.call_count == 1 batch_folder = BatchFolder(mock_from_mapping_runner.mock_context.current_dir) assert ( not batch_folder.has_batch() ) # No batch should be there as all creation failed! def test_create_from_mapping_server_error_halfway( mock_from_mapping_runner, mock_requests ): """"""What if an error occurs halfway through? """""" mock_requests.set_responses( [ RequestsMockResponseExamples.JOB_CREATED_RESPONSE, RequestsMockResponseExamples.JOB_CREATED_RESPONSE, RequestsMockResponseExamples.ERROR_USER_NOT_CONNECTED_TO_PROJECT, ] ) result = mock_from_mapping_runner.invoke( main, ""from-mapping"", input=""Y"", catch_exceptions=False ) assert result.exit_code == 0 assert mock_requests.requests.post.call_count == 3 batch_folder = BatchFolder(mock_from_mapping_runner.mock_context.current_dir) assert batch_folder.has_batch() assert batch_folder.load().job_ids == [1234] def test_show_set_default_parameters(mock_main_runner): # Try to run from-mapping parameters: JobDefaultParameters = mock_main_runner.get_context().settings.job_default_parameters parameters.project_name = ""test_project"" parameters.destination_path = ""test_destination"" result = mock_main_runner.invoke(main, ""show-defaults"") assert result.exit_code == 0 assert all(x in result.output for x in [""test_project"", ""test_destination""]) def test_create_from_mapping_relative_path( mock_from_mapping_runner, mock_requests_for_job_creation ): """"""Source identifiers in mappings are usually given as relative paths. However, jobs should be created with absolute, unambiguous paths. Check that this conversion works """""" result = mock_from_mapping_runner.invoke( main, ""from-mapping"", input=""Y"", catch_exceptions=False ) assert result.exit_code == 0 assert mock_requests_for_job_creation.requests.post.call_count == 20 current_dir = str(mock_from_mapping_runner.mock_context.current_dir) mapping = MappingFolder(current_dir).load_mapping() def all_paths(mapping_in): """"""List[str] of all paths in mapping"""""" return [ str(x) for y in mapping_in.rows() for x in y if isinstance(x, SourceIdentifierParameter) ] # in mapping there should be no mention of the current dir assert not any([current_dir in x for x in all_paths(mapping)]) expected_root = ( ParameterSet(mapping.options).get_param_by_type(RootSourcePath).value ) # But in the created jobs the current dir should have been added assert str(expected_root) in str( mock_requests_for_job_creation.requests.post.call_args[1][""data""][""source_path""] ) def test_create_from_mapping_dry_run( mock_from_mapping_runner, mock_requests_for_job_creation ): """"""Test dry run mode. Should not hit anything important """""" result = mock_from_mapping_runner.invoke( main, ""from-mapping --dry-run"", input=""Y"", catch_exceptions=False ) assert result.exit_code == 0 assert ""Dry run"" in result.output assert ""patient4"" in result.output assert mock_requests_for_job_creation.requests.post.call_count == 0 def test_create_from_mapping_folder_and_pacs( mock_from_mapping_runner, a_folder_with_mapping_diverse, mock_requests_for_job_creation, ): """"""PACS identifiers should generate slightly different jobs then Folder identifiers."""""" mock_requests = mock_requests_for_job_creation mock_from_mapping_runner.set_mock_current_dir(a_folder_with_mapping_diverse) result = mock_from_mapping_runner.invoke( main, ""from-mapping"", input=""Y"", catch_exceptions=False ) assert result.exit_code == 0 assert ""Error:"" not in result.output assert mock_requests.requests.post.call_count == 4 batch_folder = BatchFolder(mock_from_mapping_runner.mock_context.current_dir) assert batch_folder.has_batch() assert batch_folder.load().job_ids == [ 1234 ] # only 1 id as mock requests returns the same job id each call def test_job_parameter_set(all_parameters): """"""Map Parameter objects to their parameter names in job-creation functions"""""" mapped = JobParameterSet(all_parameters).as_kwargs() assert mapped[""anon_name""] == ""patientName0"" # sending an unknown parameter will raise an exception class UnknownIdentifier(SourceIdentifier): pass with pytest.raises(ParameterMappingException): JobParameterSet( all_parameters + [SourceIdentifierParameter(str(UnknownIdentifier(None)))] ).as_kwargs() class UnknownParameter(Parameter): pass with pytest.raises(ParameterMappingException): JobParameterSet(all_parameters + [UnknownParameter()]).as_kwargs() def test_job_parameter_set_validate(all_parameters): """"""Test specific problems in job parameter sets"""""" param_set = JobParameterSet(all_parameters) # this should not raise anything param_set.validate() # now without a root source root_path, it is not possible to know what the # relative root_path parameters are referring to. Exception. param_set.parameters.remove(param_set.get_param_by_type(RootSourcePath)) with pytest.raises(JobSetValidationError) as e: param_set.validate() def test_job_parameter_set_validate_non_unc_paths(all_parameters): """"""Windows maps drive letters, makes then unaccessible to normal python code and then forbids using anything BUT drive letters in windows cmd A very annoying combination which makes it hard to determine what a path is in windows. Just make sure no jobs can be created with drive letters any paths """""" param_set = JobParameterSet(all_parameters) root_source = param_set.get_param_by_type(RootSourcePath) root_source.value = PureWindowsPath(r""Z:\folder1"") with pytest.raises(JobSetValidationError) as e: param_set.validate() def test_job_parameter_set_defaults(): """"""You can set defaults for a parameter set. These are only kept if not overwritten """""" param_set = JobParameterSet( [PatientID(""1234""), PIMSKey(""0000"")], default_parameters=[Description(""Default"")], ) assert param_set.get_param_by_type(Description).value == ""Default"" param_set2 = JobParameterSet( [PatientID(""1234""), Description(""Overwrite!"")], default_parameters=[Description(""Default"")], ) assert param_set2.get_param_by_type(Description).value == ""Overwrite!"" ", https://github.com/sloria/TextBlob,6396e24e85af7462cbed648fee21db5082a1f3fb,tests/test_formats.py::TestRegistry::test_register,tests/test_formats.py,NIO,flaky,Opened,https://github.com/sloria/TextBlob/pull/404,classify;root_cause,"# -*- coding: utf-8 -*- import os import unittest from nose.tools import * # noqa (PEP8 asserts) from textblob import formats from textblob.compat import unicode HERE = os.path.abspath(os.path.dirname(__file__)) CSV_FILE = os.path.join(HERE, 'data.csv') JSON_FILE = os.path.join(HERE, ""data.json"") TSV_FILE = os.path.join(HERE, ""data.tsv"") class TestFormats(unittest.TestCase): def setUp(self): pass def test_detect_csv(self): with open(CSV_FILE) as fp: format = formats.detect(fp) assert_equal(format, formats.CSV) def test_detect_json(self): with open(JSON_FILE) as fp: format = formats.detect(fp) assert_equal(format, formats.JSON) def test_available(self): registry = formats.get_registry() assert_true('csv' in registry.keys()) assert_true('json' in registry.keys()) assert_true('tsv' in registry.keys()) class TestDelimitedFormat(unittest.TestCase): def test_delimiter_defaults_to_comma(self): assert_equal(formats.DelimitedFormat.delimiter, "","") def test_detect(self): with open(CSV_FILE, 'r') as fp: stream = fp.read() assert_true(formats.DelimitedFormat.detect(stream)) with open(JSON_FILE, 'r') as fp: stream = fp.read() assert_false(formats.DelimitedFormat.detect(stream)) class TestCSV(unittest.TestCase): def test_read_from_filename(self): with open(CSV_FILE) as fp: data = formats.CSV(fp) def test_detect(self): with open(CSV_FILE, 'r') as fp: stream = fp.read() assert_true(formats.CSV.detect(stream)) with open(JSON_FILE, 'r') as fp: stream = fp.read() assert_false(formats.CSV.detect(stream)) class TestTSV(unittest.TestCase): def test_read_from_file_object(self): with open(TSV_FILE) as fp: data = formats.TSV(fp) def test_detect(self): with open(TSV_FILE, 'r') as fp: stream = fp.read() assert_true(formats.TSV.detect(stream)) with open(CSV_FILE, 'r') as fp: stream = fp.read() assert_false(formats.TSV.detect(stream)) class TestJSON(unittest.TestCase): def test_read_from_file_object(self): with open(JSON_FILE) as fp: formats.JSON(fp) def test_detect(self): with open(JSON_FILE, 'r') as fp: stream = fp.read() assert_true(formats.JSON.detect(stream)) with open(CSV_FILE, 'r') as fp: stream = fp.read() assert_false(formats.JSON.detect(stream)) def test_to_iterable(self): with open(JSON_FILE) as fp: d = formats.JSON(fp) data = d.to_iterable() first = data[0] text, label = first[0], first[1] assert_true(isinstance(text, unicode)) class CustomFormat(formats.BaseFormat): def to_iterable(): return [ ('I like turtles', 'pos'), ('I hate turtles', 'neg') ] @classmethod def detect(cls, stream): return True class TestRegistry(unittest.TestCase): def setUp(self): pass def test_register(self): registry = formats.get_registry() assert_false(CustomFormat in registry.values()) formats.register('trt', CustomFormat) assert_true(CustomFormat in registry.values()) assert_true('trt' in registry.keys()) if __name__ == '__main__': unittest.main() ", https://github.com/solegalli/feature_engine,ba4d7050b3923d620c1773b66cd4c51ed24498dd,tests/test_missing_data_imputer.py::test_ArbitraryNumberImputer,tests/test_missing_data_imputer.py,OD-Vic,flaky,Deleted,nan,classify;root_cause,"# Authors: Soledad Galli # License: BSD 3 clause import pytest import numpy as np import pandas as pd from sklearn.exceptions import NotFittedError from feature_engine.missing_data_imputers import _define_seed from feature_engine.missing_data_imputers import MeanMedianImputer from feature_engine.missing_data_imputers import EndTailImputer from feature_engine.missing_data_imputers import ArbitraryNumberImputer from feature_engine.missing_data_imputers import CategoricalVariableImputer from feature_engine.missing_data_imputers import RandomSampleImputer from feature_engine.missing_data_imputers import AddMissingIndicator def test_define_seed(dataframe_vartypes): assert _define_seed(dataframe_vartypes, 0, ['Age', 'Marks'], how='add') == 21 assert _define_seed(dataframe_vartypes, 0, ['Age', 'Marks'], how='multiply') == 18 assert _define_seed(dataframe_vartypes, 2, ['Age', 'Marks'], how='add') == 20 assert _define_seed(dataframe_vartypes, 2, ['Age', 'Marks'], how='multiply') == 13 assert _define_seed(dataframe_vartypes, 1, ['Age'], how='add') == 21 assert _define_seed(dataframe_vartypes, 3, ['Marks'], how='multiply') == 1 def test_MeanMedianImputer(dataframe_na): # test case 1: automatically finds numerical variables imputer = MeanMedianImputer(imputation_method='mean', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(28.714285714285715) ref_df['Marks'] = ref_df['Marks'].fillna(0.6833333333333332) # check init params assert imputer.imputation_method == 'mean' assert imputer.variables == ['Age', 'Marks'] # check fit attributes assert imputer.imputer_dict_ == {'Age': 28.714285714285715, 'Marks': 0.6833333333333332} assert imputer.input_shape_ == (8, 6) # check transform output: indicated variables no NA # Not indicated variables still have NA assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: single user determined variable imputer = MeanMedianImputer(imputation_method='median', variables=['Age']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(23.0) # init params assert imputer.imputation_method == 'median' assert imputer.variables == ['Age'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 23.0} # transform params assert X_transformed['Age'].isnull().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): MeanMedianImputer(imputation_method='arbitrary') with pytest.raises(NotFittedError): imputer = MeanMedianImputer() imputer.transform(dataframe_na) def test_EndTailImputer(dataframe_na): # test case 1: automatically find variables + gaussian limits + right tail imputer = EndTailImputer(distribution='gaussian', tail='right', fold=3, variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(58.94908118478389) ref_df['Marks'] = ref_df['Marks'].fillna(1.3244261503263175) # init params assert imputer.distribution == 'gaussian' assert imputer.tail == 'right' assert imputer.fold == 3 assert imputer.variables == ['Age', 'Marks'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 58.94908118478389, 'Marks': 1.3244261503263175} # transform params: indicated vars ==> no NA, not indicated vars with NA assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['City', 'Name']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: selected variables + IQR rule + right tail imputer = EndTailImputer(distribution='skewed', tail='right', fold=1.5, variables=['Age', 'Marks']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(65.5) ref_df['Marks'] = ref_df['Marks'].fillna(1.0625) # fit and transform params assert imputer.imputer_dict_ == {'Age': 65.5, 'Marks': 1.0625} assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 3: selected variables + maximum value imputer = EndTailImputer(distribution='max', tail='right', fold=2, variables=['Age', 'Marks']) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': 82.0, 'Marks': 1.8} # test case 4: automatically select variables + gaussian limits + left tail imputer = EndTailImputer(distribution='gaussian', tail='left', fold=3) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': -1.520509756212462, 'Marks': 0.04224051634034898} # test case 5: IQR + left tail imputer = EndTailImputer(distribution='skewed', tail='left', fold=1.5, variables=['Age', 'Marks']) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': -6.5, 'Marks': 0.36249999999999993} with pytest.raises(ValueError): EndTailImputer(distribution='arbitrary') with pytest.raises(ValueError): EndTailImputer(tail='arbitrary') with pytest.raises(ValueError): EndTailImputer(fold=-1) with pytest.raises(NotFittedError): imputer = EndTailImputer() imputer.transform(dataframe_na) def test_ArbitraryNumberImputer(dataframe_na): # test case 1: automatically select variables imputer = ArbitraryNumberImputer(arbitrary_number=99, variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(99) ref_df['Marks'] = ref_df['Marks'].fillna(99) # init params assert imputer.arbitrary_number == 99 assert imputer.variables == ['Age', 'Marks'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 99, 'Marks': 99} # transform params assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: user indicates variables imputer = ArbitraryNumberImputer(arbitrary_number=-1, variables=['Age']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(-1) # init params assert imputer.arbitrary_number == -1 assert imputer.variables == ['Age'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': -1} # transform output assert X_transformed['Age'].isnull().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): ArbitraryNumberImputer(arbitrary_number='arbitrary') with pytest.raises(NotFittedError): imputer = ArbitraryNumberImputer() imputer.transform(dataframe_na) # test case 3: arbitrary numbers passed as dict imputer = ArbitraryNumberImputer(imputer_dict={'Age': -42, 'Marks': -999}) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(-42) ref_df['Marks'] = ref_df['Marks'].fillna(-999) # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': -42, 'Marks': -999} # transform params assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): ArbitraryNumberImputer(imputer_dict={'Age': 'arbitrary_number'}) def test_CategoricalVariableImputer(dataframe_na): # test case 1: imputation with missing + automatically select variables imputer = CategoricalVariableImputer(imputation_method='missing', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Name'] = ref_df['Name'].fillna('Missing') ref_df['City'] = ref_df['City'].fillna('Missing') ref_df['Studies'] = ref_df['Studies'].fillna('Missing') # init params assert imputer.imputation_method == 'missing' assert imputer.variables == ['Name', 'City', 'Studies'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Name': 'Missing', 'City': 'Missing', 'Studies': 'Missing'} # transform output assert X_transformed[['Name', 'City', 'Studies']].isnull().sum().sum() == 0 assert X_transformed[['Age', 'Marks']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: imputing custom user-defined string + automatically select variables imputer = CategoricalVariableImputer(imputation_method='missing', fill_value='Unknown', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Name'] = ref_df['Name'].fillna('Unknown') ref_df['City'] = ref_df['City'].fillna('Unknown') ref_df['Studies'] = ref_df['Studies'].fillna('Unknown') # init params assert imputer.imputation_method == 'missing' assert imputer.fill_value == 'Unknown' assert imputer.variables == ['Name', 'City', 'Studies'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Name': 'Unknown', 'City': 'Unknown', 'Studies': 'Unknown'} # transform output assert X_transformed[['Name', 'City', 'Studies']].isnull().sum().sum() == 0 ", https://github.com/solegalli/feature_engine,ba4d7050b3923d620c1773b66cd4c51ed24498dd,tests/test_missing_data_imputer.py::test_CategoricalVariableImputer,tests/test_missing_data_imputer.py,NIO,flaky,DeveloperFixed,nan,classify;root_cause,"# Authors: Soledad Galli # License: BSD 3 clause import pytest import numpy as np import pandas as pd from sklearn.exceptions import NotFittedError from feature_engine.missing_data_imputers import _define_seed from feature_engine.missing_data_imputers import MeanMedianImputer from feature_engine.missing_data_imputers import EndTailImputer from feature_engine.missing_data_imputers import ArbitraryNumberImputer from feature_engine.missing_data_imputers import CategoricalVariableImputer from feature_engine.missing_data_imputers import RandomSampleImputer from feature_engine.missing_data_imputers import AddMissingIndicator def test_define_seed(dataframe_vartypes): assert _define_seed(dataframe_vartypes, 0, ['Age', 'Marks'], how='add') == 21 assert _define_seed(dataframe_vartypes, 0, ['Age', 'Marks'], how='multiply') == 18 assert _define_seed(dataframe_vartypes, 2, ['Age', 'Marks'], how='add') == 20 assert _define_seed(dataframe_vartypes, 2, ['Age', 'Marks'], how='multiply') == 13 assert _define_seed(dataframe_vartypes, 1, ['Age'], how='add') == 21 assert _define_seed(dataframe_vartypes, 3, ['Marks'], how='multiply') == 1 def test_MeanMedianImputer(dataframe_na): # test case 1: automatically finds numerical variables imputer = MeanMedianImputer(imputation_method='mean', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(28.714285714285715) ref_df['Marks'] = ref_df['Marks'].fillna(0.6833333333333332) # check init params assert imputer.imputation_method == 'mean' assert imputer.variables == ['Age', 'Marks'] # check fit attributes assert imputer.imputer_dict_ == {'Age': 28.714285714285715, 'Marks': 0.6833333333333332} assert imputer.input_shape_ == (8, 6) # check transform output: indicated variables no NA # Not indicated variables still have NA assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: single user determined variable imputer = MeanMedianImputer(imputation_method='median', variables=['Age']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(23.0) # init params assert imputer.imputation_method == 'median' assert imputer.variables == ['Age'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 23.0} # transform params assert X_transformed['Age'].isnull().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): MeanMedianImputer(imputation_method='arbitrary') with pytest.raises(NotFittedError): imputer = MeanMedianImputer() imputer.transform(dataframe_na) def test_EndTailImputer(dataframe_na): # test case 1: automatically find variables + gaussian limits + right tail imputer = EndTailImputer(distribution='gaussian', tail='right', fold=3, variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(58.94908118478389) ref_df['Marks'] = ref_df['Marks'].fillna(1.3244261503263175) # init params assert imputer.distribution == 'gaussian' assert imputer.tail == 'right' assert imputer.fold == 3 assert imputer.variables == ['Age', 'Marks'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 58.94908118478389, 'Marks': 1.3244261503263175} # transform params: indicated vars ==> no NA, not indicated vars with NA assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['City', 'Name']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: selected variables + IQR rule + right tail imputer = EndTailImputer(distribution='skewed', tail='right', fold=1.5, variables=['Age', 'Marks']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(65.5) ref_df['Marks'] = ref_df['Marks'].fillna(1.0625) # fit and transform params assert imputer.imputer_dict_ == {'Age': 65.5, 'Marks': 1.0625} assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 3: selected variables + maximum value imputer = EndTailImputer(distribution='max', tail='right', fold=2, variables=['Age', 'Marks']) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': 82.0, 'Marks': 1.8} # test case 4: automatically select variables + gaussian limits + left tail imputer = EndTailImputer(distribution='gaussian', tail='left', fold=3) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': -1.520509756212462, 'Marks': 0.04224051634034898} # test case 5: IQR + left tail imputer = EndTailImputer(distribution='skewed', tail='left', fold=1.5, variables=['Age', 'Marks']) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': -6.5, 'Marks': 0.36249999999999993} with pytest.raises(ValueError): EndTailImputer(distribution='arbitrary') with pytest.raises(ValueError): EndTailImputer(tail='arbitrary') with pytest.raises(ValueError): EndTailImputer(fold=-1) with pytest.raises(NotFittedError): imputer = EndTailImputer() imputer.transform(dataframe_na) def test_ArbitraryNumberImputer(dataframe_na): # test case 1: automatically select variables imputer = ArbitraryNumberImputer(arbitrary_number=99, variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(99) ref_df['Marks'] = ref_df['Marks'].fillna(99) # init params assert imputer.arbitrary_number == 99 assert imputer.variables == ['Age', 'Marks'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 99, 'Marks': 99} # transform params assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: user indicates variables imputer = ArbitraryNumberImputer(arbitrary_number=-1, variables=['Age']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(-1) # init params assert imputer.arbitrary_number == -1 assert imputer.variables == ['Age'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': -1} # transform output assert X_transformed['Age'].isnull().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): ArbitraryNumberImputer(arbitrary_number='arbitrary') with pytest.raises(NotFittedError): imputer = ArbitraryNumberImputer() imputer.transform(dataframe_na) # test case 3: arbitrary numbers passed as dict imputer = ArbitraryNumberImputer(imputer_dict={'Age': -42, 'Marks': -999}) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(-42) ref_df['Marks'] = ref_df['Marks'].fillna(-999) # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': -42, 'Marks': -999} # transform params assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): ArbitraryNumberImputer(imputer_dict={'Age': 'arbitrary_number'}) def test_CategoricalVariableImputer(dataframe_na): # test case 1: imputation with missing + automatically select variables imputer = CategoricalVariableImputer(imputation_method='missing', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Name'] = ref_df['Name'].fillna('Missing') ref_df['City'] = ref_df['City'].fillna('Missing') ref_df['Studies'] = ref_df['Studies'].fillna('Missing') # init params assert imputer.imputation_method == 'missing' assert imputer.variables == ['Name', 'City', 'Studies'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Name': 'Missing', 'City': 'Missing', 'Studies': 'Missing'} # transform output assert X_transformed[['Name', 'City', 'Studies']].isnull().sum().sum() == 0 assert X_transformed[['Age', 'Marks']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: imputing custom user-defined string + automatically select variables imputer = CategoricalVariableImputer(imputation_method='missing', fill_value='Unknown', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Name'] = ref_df['Name'].fillna('Unknown') ref_df['City'] = ref_df['City'].fillna('Unknown') ref_df['Studies'] = ref_df['Studies'].fillna('Unknown') # init params assert imputer.imputation_method == 'missing' assert imputer.fill_value == 'Unknown' assert imputer.variables == ['Name', 'City', 'Studies'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Name': 'Unknown', 'City': 'Unknown', 'Studies': 'Unknown'} # transform output assert X_transformed[['Name', 'City', 'Studies']].isnull().sum().sum() == 0 ", https://github.com/solegalli/feature_engine,ba4d7050b3923d620c1773b66cd4c51ed24498dd,tests/test_missing_data_imputer.py::test_EndTailImputer,tests/test_missing_data_imputer.py,OD-Vic,flaky,Deleted,nan,classify;root_cause,"# Authors: Soledad Galli # License: BSD 3 clause import pytest import numpy as np import pandas as pd from sklearn.exceptions import NotFittedError from feature_engine.missing_data_imputers import _define_seed from feature_engine.missing_data_imputers import MeanMedianImputer from feature_engine.missing_data_imputers import EndTailImputer from feature_engine.missing_data_imputers import ArbitraryNumberImputer from feature_engine.missing_data_imputers import CategoricalVariableImputer from feature_engine.missing_data_imputers import RandomSampleImputer from feature_engine.missing_data_imputers import AddMissingIndicator def test_define_seed(dataframe_vartypes): assert _define_seed(dataframe_vartypes, 0, ['Age', 'Marks'], how='add') == 21 assert _define_seed(dataframe_vartypes, 0, ['Age', 'Marks'], how='multiply') == 18 assert _define_seed(dataframe_vartypes, 2, ['Age', 'Marks'], how='add') == 20 assert _define_seed(dataframe_vartypes, 2, ['Age', 'Marks'], how='multiply') == 13 assert _define_seed(dataframe_vartypes, 1, ['Age'], how='add') == 21 assert _define_seed(dataframe_vartypes, 3, ['Marks'], how='multiply') == 1 def test_MeanMedianImputer(dataframe_na): # test case 1: automatically finds numerical variables imputer = MeanMedianImputer(imputation_method='mean', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(28.714285714285715) ref_df['Marks'] = ref_df['Marks'].fillna(0.6833333333333332) # check init params assert imputer.imputation_method == 'mean' assert imputer.variables == ['Age', 'Marks'] # check fit attributes assert imputer.imputer_dict_ == {'Age': 28.714285714285715, 'Marks': 0.6833333333333332} assert imputer.input_shape_ == (8, 6) # check transform output: indicated variables no NA # Not indicated variables still have NA assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: single user determined variable imputer = MeanMedianImputer(imputation_method='median', variables=['Age']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(23.0) # init params assert imputer.imputation_method == 'median' assert imputer.variables == ['Age'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 23.0} # transform params assert X_transformed['Age'].isnull().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): MeanMedianImputer(imputation_method='arbitrary') with pytest.raises(NotFittedError): imputer = MeanMedianImputer() imputer.transform(dataframe_na) def test_EndTailImputer(dataframe_na): # test case 1: automatically find variables + gaussian limits + right tail imputer = EndTailImputer(distribution='gaussian', tail='right', fold=3, variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(58.94908118478389) ref_df['Marks'] = ref_df['Marks'].fillna(1.3244261503263175) # init params assert imputer.distribution == 'gaussian' assert imputer.tail == 'right' assert imputer.fold == 3 assert imputer.variables == ['Age', 'Marks'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 58.94908118478389, 'Marks': 1.3244261503263175} # transform params: indicated vars ==> no NA, not indicated vars with NA assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['City', 'Name']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: selected variables + IQR rule + right tail imputer = EndTailImputer(distribution='skewed', tail='right', fold=1.5, variables=['Age', 'Marks']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(65.5) ref_df['Marks'] = ref_df['Marks'].fillna(1.0625) # fit and transform params assert imputer.imputer_dict_ == {'Age': 65.5, 'Marks': 1.0625} assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 3: selected variables + maximum value imputer = EndTailImputer(distribution='max', tail='right', fold=2, variables=['Age', 'Marks']) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': 82.0, 'Marks': 1.8} # test case 4: automatically select variables + gaussian limits + left tail imputer = EndTailImputer(distribution='gaussian', tail='left', fold=3) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': -1.520509756212462, 'Marks': 0.04224051634034898} # test case 5: IQR + left tail imputer = EndTailImputer(distribution='skewed', tail='left', fold=1.5, variables=['Age', 'Marks']) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': -6.5, 'Marks': 0.36249999999999993} with pytest.raises(ValueError): EndTailImputer(distribution='arbitrary') with pytest.raises(ValueError): EndTailImputer(tail='arbitrary') with pytest.raises(ValueError): EndTailImputer(fold=-1) with pytest.raises(NotFittedError): imputer = EndTailImputer() imputer.transform(dataframe_na) def test_ArbitraryNumberImputer(dataframe_na): # test case 1: automatically select variables imputer = ArbitraryNumberImputer(arbitrary_number=99, variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(99) ref_df['Marks'] = ref_df['Marks'].fillna(99) # init params assert imputer.arbitrary_number == 99 assert imputer.variables == ['Age', 'Marks'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 99, 'Marks': 99} # transform params assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: user indicates variables imputer = ArbitraryNumberImputer(arbitrary_number=-1, variables=['Age']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(-1) # init params assert imputer.arbitrary_number == -1 assert imputer.variables == ['Age'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': -1} # transform output assert X_transformed['Age'].isnull().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): ArbitraryNumberImputer(arbitrary_number='arbitrary') with pytest.raises(NotFittedError): imputer = ArbitraryNumberImputer() imputer.transform(dataframe_na) # test case 3: arbitrary numbers passed as dict imputer = ArbitraryNumberImputer(imputer_dict={'Age': -42, 'Marks': -999}) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(-42) ref_df['Marks'] = ref_df['Marks'].fillna(-999) # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': -42, 'Marks': -999} # transform params assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): ArbitraryNumberImputer(imputer_dict={'Age': 'arbitrary_number'}) def test_CategoricalVariableImputer(dataframe_na): # test case 1: imputation with missing + automatically select variables imputer = CategoricalVariableImputer(imputation_method='missing', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Name'] = ref_df['Name'].fillna('Missing') ref_df['City'] = ref_df['City'].fillna('Missing') ref_df['Studies'] = ref_df['Studies'].fillna('Missing') # init params assert imputer.imputation_method == 'missing' assert imputer.variables == ['Name', 'City', 'Studies'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Name': 'Missing', 'City': 'Missing', 'Studies': 'Missing'} # transform output assert X_transformed[['Name', 'City', 'Studies']].isnull().sum().sum() == 0 assert X_transformed[['Age', 'Marks']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: imputing custom user-defined string + automatically select variables imputer = CategoricalVariableImputer(imputation_method='missing', fill_value='Unknown', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Name'] = ref_df['Name'].fillna('Unknown') ref_df['City'] = ref_df['City'].fillna('Unknown') ref_df['Studies'] = ref_df['Studies'].fillna('Unknown') # init params assert imputer.imputation_method == 'missing' assert imputer.fill_value == 'Unknown' assert imputer.variables == ['Name', 'City', 'Studies'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Name': 'Unknown', 'City': 'Unknown', 'Studies': 'Unknown'} # transform output assert X_transformed[['Name', 'City', 'Studies']].isnull().sum().sum() == 0 ", https://github.com/solegalli/feature_engine,ba4d7050b3923d620c1773b66cd4c51ed24498dd,tests/test_missing_data_imputer.py::test_MeanMedianImputer,tests/test_missing_data_imputer.py,OD-Vic,flaky,Deleted,nan,classify;root_cause,"# Authors: Soledad Galli # License: BSD 3 clause import pytest import numpy as np import pandas as pd from sklearn.exceptions import NotFittedError from feature_engine.missing_data_imputers import _define_seed from feature_engine.missing_data_imputers import MeanMedianImputer from feature_engine.missing_data_imputers import EndTailImputer from feature_engine.missing_data_imputers import ArbitraryNumberImputer from feature_engine.missing_data_imputers import CategoricalVariableImputer from feature_engine.missing_data_imputers import RandomSampleImputer from feature_engine.missing_data_imputers import AddMissingIndicator def test_define_seed(dataframe_vartypes): assert _define_seed(dataframe_vartypes, 0, ['Age', 'Marks'], how='add') == 21 assert _define_seed(dataframe_vartypes, 0, ['Age', 'Marks'], how='multiply') == 18 assert _define_seed(dataframe_vartypes, 2, ['Age', 'Marks'], how='add') == 20 assert _define_seed(dataframe_vartypes, 2, ['Age', 'Marks'], how='multiply') == 13 assert _define_seed(dataframe_vartypes, 1, ['Age'], how='add') == 21 assert _define_seed(dataframe_vartypes, 3, ['Marks'], how='multiply') == 1 def test_MeanMedianImputer(dataframe_na): # test case 1: automatically finds numerical variables imputer = MeanMedianImputer(imputation_method='mean', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(28.714285714285715) ref_df['Marks'] = ref_df['Marks'].fillna(0.6833333333333332) # check init params assert imputer.imputation_method == 'mean' assert imputer.variables == ['Age', 'Marks'] # check fit attributes assert imputer.imputer_dict_ == {'Age': 28.714285714285715, 'Marks': 0.6833333333333332} assert imputer.input_shape_ == (8, 6) # check transform output: indicated variables no NA # Not indicated variables still have NA assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: single user determined variable imputer = MeanMedianImputer(imputation_method='median', variables=['Age']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(23.0) # init params assert imputer.imputation_method == 'median' assert imputer.variables == ['Age'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 23.0} # transform params assert X_transformed['Age'].isnull().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): MeanMedianImputer(imputation_method='arbitrary') with pytest.raises(NotFittedError): imputer = MeanMedianImputer() imputer.transform(dataframe_na) def test_EndTailImputer(dataframe_na): # test case 1: automatically find variables + gaussian limits + right tail imputer = EndTailImputer(distribution='gaussian', tail='right', fold=3, variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(58.94908118478389) ref_df['Marks'] = ref_df['Marks'].fillna(1.3244261503263175) # init params assert imputer.distribution == 'gaussian' assert imputer.tail == 'right' assert imputer.fold == 3 assert imputer.variables == ['Age', 'Marks'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 58.94908118478389, 'Marks': 1.3244261503263175} # transform params: indicated vars ==> no NA, not indicated vars with NA assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['City', 'Name']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: selected variables + IQR rule + right tail imputer = EndTailImputer(distribution='skewed', tail='right', fold=1.5, variables=['Age', 'Marks']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(65.5) ref_df['Marks'] = ref_df['Marks'].fillna(1.0625) # fit and transform params assert imputer.imputer_dict_ == {'Age': 65.5, 'Marks': 1.0625} assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 3: selected variables + maximum value imputer = EndTailImputer(distribution='max', tail='right', fold=2, variables=['Age', 'Marks']) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': 82.0, 'Marks': 1.8} # test case 4: automatically select variables + gaussian limits + left tail imputer = EndTailImputer(distribution='gaussian', tail='left', fold=3) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': -1.520509756212462, 'Marks': 0.04224051634034898} # test case 5: IQR + left tail imputer = EndTailImputer(distribution='skewed', tail='left', fold=1.5, variables=['Age', 'Marks']) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': -6.5, 'Marks': 0.36249999999999993} with pytest.raises(ValueError): EndTailImputer(distribution='arbitrary') with pytest.raises(ValueError): EndTailImputer(tail='arbitrary') with pytest.raises(ValueError): EndTailImputer(fold=-1) with pytest.raises(NotFittedError): imputer = EndTailImputer() imputer.transform(dataframe_na) def test_ArbitraryNumberImputer(dataframe_na): # test case 1: automatically select variables imputer = ArbitraryNumberImputer(arbitrary_number=99, variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(99) ref_df['Marks'] = ref_df['Marks'].fillna(99) # init params assert imputer.arbitrary_number == 99 assert imputer.variables == ['Age', 'Marks'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 99, 'Marks': 99} # transform params assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: user indicates variables imputer = ArbitraryNumberImputer(arbitrary_number=-1, variables=['Age']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(-1) # init params assert imputer.arbitrary_number == -1 assert imputer.variables == ['Age'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': -1} # transform output assert X_transformed['Age'].isnull().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): ArbitraryNumberImputer(arbitrary_number='arbitrary') with pytest.raises(NotFittedError): imputer = ArbitraryNumberImputer() imputer.transform(dataframe_na) # test case 3: arbitrary numbers passed as dict imputer = ArbitraryNumberImputer(imputer_dict={'Age': -42, 'Marks': -999}) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(-42) ref_df['Marks'] = ref_df['Marks'].fillna(-999) # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': -42, 'Marks': -999} # transform params assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): ArbitraryNumberImputer(imputer_dict={'Age': 'arbitrary_number'}) def test_CategoricalVariableImputer(dataframe_na): # test case 1: imputation with missing + automatically select variables imputer = CategoricalVariableImputer(imputation_method='missing', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Name'] = ref_df['Name'].fillna('Missing') ref_df['City'] = ref_df['City'].fillna('Missing') ref_df['Studies'] = ref_df['Studies'].fillna('Missing') # init params assert imputer.imputation_method == 'missing' assert imputer.variables == ['Name', 'City', 'Studies'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Name': 'Missing', 'City': 'Missing', 'Studies': 'Missing'} # transform output assert X_transformed[['Name', 'City', 'Studies']].isnull().sum().sum() == 0 assert X_transformed[['Age', 'Marks']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: imputing custom user-defined string + automatically select variables imputer = CategoricalVariableImputer(imputation_method='missing', fill_value='Unknown', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Name'] = ref_df['Name'].fillna('Unknown') ref_df['City'] = ref_df['City'].fillna('Unknown') ref_df['Studies'] = ref_df['Studies'].fillna('Unknown') # init params assert imputer.imputation_method == 'missing' assert imputer.fill_value == 'Unknown' assert imputer.variables == ['Name', 'City', 'Studies'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Name': 'Unknown', 'City': 'Unknown', 'Studies': 'Unknown'} # transform output assert X_transformed[['Name', 'City', 'Studies']].isnull().sum().sum() == 0 ", https://github.com/solegalli/feature_engine,ba4d7050b3923d620c1773b66cd4c51ed24498dd,tests/test_missing_data_imputer.py::test_RandomSampleImputer,tests/test_missing_data_imputer.py,NIO,flaky,DeveloperFixed,nan,classify;root_cause,"# Authors: Soledad Galli # License: BSD 3 clause import pytest import numpy as np import pandas as pd from sklearn.exceptions import NotFittedError from feature_engine.missing_data_imputers import _define_seed from feature_engine.missing_data_imputers import MeanMedianImputer from feature_engine.missing_data_imputers import EndTailImputer from feature_engine.missing_data_imputers import ArbitraryNumberImputer from feature_engine.missing_data_imputers import CategoricalVariableImputer from feature_engine.missing_data_imputers import RandomSampleImputer from feature_engine.missing_data_imputers import AddMissingIndicator def test_define_seed(dataframe_vartypes): assert _define_seed(dataframe_vartypes, 0, ['Age', 'Marks'], how='add') == 21 assert _define_seed(dataframe_vartypes, 0, ['Age', 'Marks'], how='multiply') == 18 assert _define_seed(dataframe_vartypes, 2, ['Age', 'Marks'], how='add') == 20 assert _define_seed(dataframe_vartypes, 2, ['Age', 'Marks'], how='multiply') == 13 assert _define_seed(dataframe_vartypes, 1, ['Age'], how='add') == 21 assert _define_seed(dataframe_vartypes, 3, ['Marks'], how='multiply') == 1 def test_MeanMedianImputer(dataframe_na): # test case 1: automatically finds numerical variables imputer = MeanMedianImputer(imputation_method='mean', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(28.714285714285715) ref_df['Marks'] = ref_df['Marks'].fillna(0.6833333333333332) # check init params assert imputer.imputation_method == 'mean' assert imputer.variables == ['Age', 'Marks'] # check fit attributes assert imputer.imputer_dict_ == {'Age': 28.714285714285715, 'Marks': 0.6833333333333332} assert imputer.input_shape_ == (8, 6) # check transform output: indicated variables no NA # Not indicated variables still have NA assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: single user determined variable imputer = MeanMedianImputer(imputation_method='median', variables=['Age']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(23.0) # init params assert imputer.imputation_method == 'median' assert imputer.variables == ['Age'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 23.0} # transform params assert X_transformed['Age'].isnull().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): MeanMedianImputer(imputation_method='arbitrary') with pytest.raises(NotFittedError): imputer = MeanMedianImputer() imputer.transform(dataframe_na) def test_EndTailImputer(dataframe_na): # test case 1: automatically find variables + gaussian limits + right tail imputer = EndTailImputer(distribution='gaussian', tail='right', fold=3, variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(58.94908118478389) ref_df['Marks'] = ref_df['Marks'].fillna(1.3244261503263175) # init params assert imputer.distribution == 'gaussian' assert imputer.tail == 'right' assert imputer.fold == 3 assert imputer.variables == ['Age', 'Marks'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 58.94908118478389, 'Marks': 1.3244261503263175} # transform params: indicated vars ==> no NA, not indicated vars with NA assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['City', 'Name']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: selected variables + IQR rule + right tail imputer = EndTailImputer(distribution='skewed', tail='right', fold=1.5, variables=['Age', 'Marks']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(65.5) ref_df['Marks'] = ref_df['Marks'].fillna(1.0625) # fit and transform params assert imputer.imputer_dict_ == {'Age': 65.5, 'Marks': 1.0625} assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 3: selected variables + maximum value imputer = EndTailImputer(distribution='max', tail='right', fold=2, variables=['Age', 'Marks']) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': 82.0, 'Marks': 1.8} # test case 4: automatically select variables + gaussian limits + left tail imputer = EndTailImputer(distribution='gaussian', tail='left', fold=3) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': -1.520509756212462, 'Marks': 0.04224051634034898} # test case 5: IQR + left tail imputer = EndTailImputer(distribution='skewed', tail='left', fold=1.5, variables=['Age', 'Marks']) imputer.fit(dataframe_na) assert imputer.imputer_dict_ == {'Age': -6.5, 'Marks': 0.36249999999999993} with pytest.raises(ValueError): EndTailImputer(distribution='arbitrary') with pytest.raises(ValueError): EndTailImputer(tail='arbitrary') with pytest.raises(ValueError): EndTailImputer(fold=-1) with pytest.raises(NotFittedError): imputer = EndTailImputer() imputer.transform(dataframe_na) def test_ArbitraryNumberImputer(dataframe_na): # test case 1: automatically select variables imputer = ArbitraryNumberImputer(arbitrary_number=99, variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(99) ref_df['Marks'] = ref_df['Marks'].fillna(99) # init params assert imputer.arbitrary_number == 99 assert imputer.variables == ['Age', 'Marks'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': 99, 'Marks': 99} # transform params assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: user indicates variables imputer = ArbitraryNumberImputer(arbitrary_number=-1, variables=['Age']) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(-1) # init params assert imputer.arbitrary_number == -1 assert imputer.variables == ['Age'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': -1} # transform output assert X_transformed['Age'].isnull().sum() == 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): ArbitraryNumberImputer(arbitrary_number='arbitrary') with pytest.raises(NotFittedError): imputer = ArbitraryNumberImputer() imputer.transform(dataframe_na) # test case 3: arbitrary numbers passed as dict imputer = ArbitraryNumberImputer(imputer_dict={'Age': -42, 'Marks': -999}) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Age'] = ref_df['Age'].fillna(-42) ref_df['Marks'] = ref_df['Marks'].fillna(-999) # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Age': -42, 'Marks': -999} # transform params assert X_transformed[['Age', 'Marks']].isnull().sum().sum() == 0 assert X_transformed[['Name', 'City']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) with pytest.raises(ValueError): ArbitraryNumberImputer(imputer_dict={'Age': 'arbitrary_number'}) def test_CategoricalVariableImputer(dataframe_na): # test case 1: imputation with missing + automatically select variables imputer = CategoricalVariableImputer(imputation_method='missing', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Name'] = ref_df['Name'].fillna('Missing') ref_df['City'] = ref_df['City'].fillna('Missing') ref_df['Studies'] = ref_df['Studies'].fillna('Missing') # init params assert imputer.imputation_method == 'missing' assert imputer.variables == ['Name', 'City', 'Studies'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Name': 'Missing', 'City': 'Missing', 'Studies': 'Missing'} # transform output assert X_transformed[['Name', 'City', 'Studies']].isnull().sum().sum() == 0 assert X_transformed[['Age', 'Marks']].isnull().sum().sum() > 0 pd.testing.assert_frame_equal(X_transformed, ref_df) # test case 2: imputing custom user-defined string + automatically select variables imputer = CategoricalVariableImputer(imputation_method='missing', fill_value='Unknown', variables=None) X_transformed = imputer.fit_transform(dataframe_na) ref_df = dataframe_na.copy() ref_df['Name'] = ref_df['Name'].fillna('Unknown') ref_df['City'] = ref_df['City'].fillna('Unknown') ref_df['Studies'] = ref_df['Studies'].fillna('Unknown') # init params assert imputer.imputation_method == 'missing' assert imputer.fill_value == 'Unknown' assert imputer.variables == ['Name', 'City', 'Studies'] # fit params assert imputer.input_shape_ == (8, 6) assert imputer.imputer_dict_ == {'Name': 'Unknown', 'City': 'Unknown', 'Studies': 'Unknown'} # transform output assert X_transformed[['Name', 'City', 'Studies']].isnull().sum().sum() == 0 ", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_class_import.py::test_can_ignore_email_module,tests/test_class_import.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import time import sys from .fake_module import ( fake_date_function, fake_datetime_function, fake_gmtime_function, fake_localtime_function, fake_strftime_function, fake_time_function, ) from . import fake_module from freezegun import freeze_time from freezegun.api import ( FakeDatetime, FakeDate, fake_time, fake_localtime, fake_gmtime, fake_strftime, ) import datetime @freeze_time(""2012-01-14"") def test_import_datetime_works(): assert fake_datetime_function().day == 14 @freeze_time(""2012-01-14"") def test_import_date_works(): assert fake_date_function().day == 14 @freeze_time(""2012-01-14"") def test_import_time(): local_time = datetime.datetime(2012, 1, 14) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) assert fake_time_function() == expected_timestamp def test_start_and_stop_works(): freezer = freeze_time(""2012-01-14"") result = fake_datetime_function() assert result.__class__ == datetime.datetime assert result.__class__ != FakeDatetime freezer.start() assert fake_datetime_function().day == 14 assert isinstance(fake_datetime_function(), datetime.datetime) assert isinstance(fake_datetime_function(), FakeDatetime) freezer.stop() result = fake_datetime_function() assert result.__class__ == datetime.datetime assert result.__class__ != FakeDatetime def test_isinstance_works(): date = datetime.date.today() now = datetime.datetime.now() freezer = freeze_time('2011-01-01') freezer.start() assert isinstance(date, datetime.date) assert not isinstance(date, datetime.datetime) assert isinstance(now, datetime.datetime) assert isinstance(now, datetime.date) freezer.stop() def test_issubclass_works(): real_date = datetime.date real_datetime = datetime.datetime freezer = freeze_time('2011-01-01') freezer.start() assert issubclass(real_date, datetime.date) assert issubclass(real_datetime, datetime.datetime) freezer.stop() def test_fake_uses_real_when_ignored(): real_time_before = time.time() with freeze_time('2012-01-14', ignore=['tests.fake_module']): real_time = fake_time_function() real_time_after = time.time() assert real_time_before <= real_time <= real_time_after def test_can_ignore_email_module(): from email.utils import formatdate with freeze_time('2012-01-14'): faked_date_str = formatdate() before_date_str = formatdate() with freeze_time('2012-01-14', ignore=['email']): date_str = formatdate() after_date_str = formatdate() assert date_str != faked_date_str assert before_date_str <= date_str <= after_date_str @freeze_time('2011-01-01') def test_avoid_replacing_equal_to_anything(): assert fake_module.equal_to_anything.description == 'This is the equal_to_anything object' @freeze_time(""2012-01-14 12:00:00"") def test_import_localtime(): struct = fake_localtime_function() assert struct.tm_year == 2012 assert struct.tm_mon == 1 assert struct.tm_mday >= 13 # eg. GMT+14 assert struct.tm_mday <= 15 # eg. GMT-14 @freeze_time(""2012-01-14 12:00:00"") def test_fake_gmtime_function(): struct = fake_gmtime_function() assert struct.tm_year == 2012 assert struct.tm_mon == 1 assert struct.tm_mday == 14 @freeze_time(""2012-01-14"") def test_fake_strftime_function(): assert fake_strftime_function() == '2012' def test_import_after_start(): with freeze_time('2012-01-14'): assert 'tests.another_module' not in sys.modules.keys() from tests import another_module # Reals assert another_module.get_datetime() is datetime.datetime assert another_module.get_datetime() is FakeDatetime assert another_module.get_date() is datetime.date assert another_module.get_date() is FakeDate assert another_module.get_time() is time.time assert another_module.get_time() is fake_time assert another_module.get_localtime() is time.localtime assert another_module.get_localtime() is fake_localtime assert another_module.get_gmtime() is time.gmtime assert another_module.get_gmtime() is fake_gmtime assert another_module.get_strftime() is time.strftime assert another_module.get_strftime() is fake_strftime # Fakes assert another_module.get_fake_datetime() is FakeDatetime assert another_module.get_fake_date() is FakeDate assert another_module.get_fake_time() is fake_time assert another_module.get_fake_localtime() is fake_localtime assert another_module.get_fake_gmtime() is fake_gmtime assert another_module.get_fake_strftime() is fake_strftime # Reals assert another_module.get_datetime() is datetime.datetime assert not another_module.get_datetime() is FakeDatetime assert another_module.get_date() is datetime.date assert not another_module.get_date() is FakeDate assert another_module.get_time() is time.time assert not another_module.get_time() is fake_time assert another_module.get_localtime() is time.localtime assert not another_module.get_localtime() is fake_localtime assert another_module.get_gmtime() is time.gmtime assert not another_module.get_gmtime() is fake_gmtime assert another_module.get_strftime() is time.strftime assert not another_module.get_strftime() is fake_strftime # Fakes assert another_module.get_fake_datetime() is FakeDatetime assert another_module.get_fake_date() is FakeDate assert another_module.get_fake_time() is fake_time assert another_module.get_fake_localtime() is fake_localtime assert another_module.get_fake_gmtime() is fake_gmtime assert another_module.get_fake_strftime() is fake_strftime def test_none_as_initial(): with freeze_time() as ft: ft.move_to('2012-01-14') assert fake_strftime_function() == '2012' ", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_class_import.py::test_fake_uses_real_when_ignored,tests/test_class_import.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import time import sys from .fake_module import ( fake_date_function, fake_datetime_function, fake_gmtime_function, fake_localtime_function, fake_strftime_function, fake_time_function, ) from . import fake_module from freezegun import freeze_time from freezegun.api import ( FakeDatetime, FakeDate, fake_time, fake_localtime, fake_gmtime, fake_strftime, ) import datetime @freeze_time(""2012-01-14"") def test_import_datetime_works(): assert fake_datetime_function().day == 14 @freeze_time(""2012-01-14"") def test_import_date_works(): assert fake_date_function().day == 14 @freeze_time(""2012-01-14"") def test_import_time(): local_time = datetime.datetime(2012, 1, 14) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) assert fake_time_function() == expected_timestamp def test_start_and_stop_works(): freezer = freeze_time(""2012-01-14"") result = fake_datetime_function() assert result.__class__ == datetime.datetime assert result.__class__ != FakeDatetime freezer.start() assert fake_datetime_function().day == 14 assert isinstance(fake_datetime_function(), datetime.datetime) assert isinstance(fake_datetime_function(), FakeDatetime) freezer.stop() result = fake_datetime_function() assert result.__class__ == datetime.datetime assert result.__class__ != FakeDatetime def test_isinstance_works(): date = datetime.date.today() now = datetime.datetime.now() freezer = freeze_time('2011-01-01') freezer.start() assert isinstance(date, datetime.date) assert not isinstance(date, datetime.datetime) assert isinstance(now, datetime.datetime) assert isinstance(now, datetime.date) freezer.stop() def test_issubclass_works(): real_date = datetime.date real_datetime = datetime.datetime freezer = freeze_time('2011-01-01') freezer.start() assert issubclass(real_date, datetime.date) assert issubclass(real_datetime, datetime.datetime) freezer.stop() def test_fake_uses_real_when_ignored(): real_time_before = time.time() with freeze_time('2012-01-14', ignore=['tests.fake_module']): real_time = fake_time_function() real_time_after = time.time() assert real_time_before <= real_time <= real_time_after def test_can_ignore_email_module(): from email.utils import formatdate with freeze_time('2012-01-14'): faked_date_str = formatdate() before_date_str = formatdate() with freeze_time('2012-01-14', ignore=['email']): date_str = formatdate() after_date_str = formatdate() assert date_str != faked_date_str assert before_date_str <= date_str <= after_date_str @freeze_time('2011-01-01') def test_avoid_replacing_equal_to_anything(): assert fake_module.equal_to_anything.description == 'This is the equal_to_anything object' @freeze_time(""2012-01-14 12:00:00"") def test_import_localtime(): struct = fake_localtime_function() assert struct.tm_year == 2012 assert struct.tm_mon == 1 assert struct.tm_mday >= 13 # eg. GMT+14 assert struct.tm_mday <= 15 # eg. GMT-14 @freeze_time(""2012-01-14 12:00:00"") def test_fake_gmtime_function(): struct = fake_gmtime_function() assert struct.tm_year == 2012 assert struct.tm_mon == 1 assert struct.tm_mday == 14 @freeze_time(""2012-01-14"") def test_fake_strftime_function(): assert fake_strftime_function() == '2012' def test_import_after_start(): with freeze_time('2012-01-14'): assert 'tests.another_module' not in sys.modules.keys() from tests import another_module # Reals assert another_module.get_datetime() is datetime.datetime assert another_module.get_datetime() is FakeDatetime assert another_module.get_date() is datetime.date assert another_module.get_date() is FakeDate assert another_module.get_time() is time.time assert another_module.get_time() is fake_time assert another_module.get_localtime() is time.localtime assert another_module.get_localtime() is fake_localtime assert another_module.get_gmtime() is time.gmtime assert another_module.get_gmtime() is fake_gmtime assert another_module.get_strftime() is time.strftime assert another_module.get_strftime() is fake_strftime # Fakes assert another_module.get_fake_datetime() is FakeDatetime assert another_module.get_fake_date() is FakeDate assert another_module.get_fake_time() is fake_time assert another_module.get_fake_localtime() is fake_localtime assert another_module.get_fake_gmtime() is fake_gmtime assert another_module.get_fake_strftime() is fake_strftime # Reals assert another_module.get_datetime() is datetime.datetime assert not another_module.get_datetime() is FakeDatetime assert another_module.get_date() is datetime.date assert not another_module.get_date() is FakeDate assert another_module.get_time() is time.time assert not another_module.get_time() is fake_time assert another_module.get_localtime() is time.localtime assert not another_module.get_localtime() is fake_localtime assert another_module.get_gmtime() is time.gmtime assert not another_module.get_gmtime() is fake_gmtime assert another_module.get_strftime() is time.strftime assert not another_module.get_strftime() is fake_strftime # Fakes assert another_module.get_fake_datetime() is FakeDatetime assert another_module.get_fake_date() is FakeDate assert another_module.get_fake_time() is fake_time assert another_module.get_fake_localtime() is fake_localtime assert another_module.get_fake_gmtime() is fake_gmtime assert another_module.get_fake_strftime() is fake_strftime def test_none_as_initial(): with freeze_time() as ft: ft.move_to('2012-01-14') assert fake_strftime_function() == '2012' ", https://github.com/spulec/freezegun,8994558274161b527ab83a3b13ea5969e9ebd144,tests/test_class_import.py::test_import_after_start,tests/test_class_import.py,NOD,flaky,nan,nan,classify;root_cause,"import time import sys from .fake_module import ( fake_date_function, fake_datetime_function, fake_gmtime_function, fake_localtime_function, fake_strftime_function, fake_time_function, ) from . import fake_module from freezegun import freeze_time from freezegun.api import ( FakeDatetime, FakeDate, fake_time, fake_localtime, fake_gmtime, fake_strftime, ) import datetime @freeze_time(""2012-01-14"") def test_import_datetime_works(): assert fake_datetime_function().day == 14 @freeze_time(""2012-01-14"") def test_import_date_works(): assert fake_date_function().day == 14 @freeze_time(""2012-01-14"") def test_import_time(): local_time = datetime.datetime(2012, 1, 14) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) assert fake_time_function() == expected_timestamp def test_start_and_stop_works(): freezer = freeze_time(""2012-01-14"") result = fake_datetime_function() assert result.__class__ == datetime.datetime assert result.__class__ != FakeDatetime freezer.start() assert fake_datetime_function().day == 14 assert isinstance(fake_datetime_function(), datetime.datetime) assert isinstance(fake_datetime_function(), FakeDatetime) freezer.stop() result = fake_datetime_function() assert result.__class__ == datetime.datetime assert result.__class__ != FakeDatetime def test_isinstance_works(): date = datetime.date.today() now = datetime.datetime.now() freezer = freeze_time('2011-01-01') freezer.start() assert isinstance(date, datetime.date) assert not isinstance(date, datetime.datetime) assert isinstance(now, datetime.datetime) assert isinstance(now, datetime.date) freezer.stop() def test_issubclass_works(): real_date = datetime.date real_datetime = datetime.datetime freezer = freeze_time('2011-01-01') freezer.start() assert issubclass(real_date, datetime.date) assert issubclass(real_datetime, datetime.datetime) freezer.stop() def test_fake_uses_real_when_ignored(): real_time_before = time.time() with freeze_time('2012-01-14', ignore=['tests.fake_module']): real_time = fake_time_function() real_time_after = time.time() assert real_time_before <= real_time <= real_time_after def test_can_ignore_email_module(): from email.utils import formatdate with freeze_time('2012-01-14'): faked_date_str = formatdate() before_date_str = formatdate() with freeze_time('2012-01-14', ignore=['email']): date_str = formatdate() after_date_str = formatdate() assert date_str != faked_date_str assert before_date_str <= date_str <= after_date_str @freeze_time('2011-01-01') def test_avoid_replacing_equal_to_anything(): assert fake_module.equal_to_anything.description == 'This is the equal_to_anything object' @freeze_time(""2012-01-14 12:00:00"") def test_import_localtime(): struct = fake_localtime_function() assert struct.tm_year == 2012 assert struct.tm_mon == 1 assert struct.tm_mday >= 13 # eg. GMT+14 assert struct.tm_mday <= 15 # eg. GMT-14 @freeze_time(""2012-01-14 12:00:00"") def test_fake_gmtime_function(): struct = fake_gmtime_function() assert struct.tm_year == 2012 assert struct.tm_mon == 1 assert struct.tm_mday == 14 @freeze_time(""2012-01-14"") def test_fake_strftime_function(): assert fake_strftime_function() == '2012' def test_import_after_start(): with freeze_time('2012-01-14'): assert 'tests.another_module' not in sys.modules.keys() from tests import another_module # Reals assert another_module.get_datetime() is datetime.datetime assert another_module.get_datetime() is FakeDatetime assert another_module.get_date() is datetime.date assert another_module.get_date() is FakeDate assert another_module.get_time() is time.time assert another_module.get_time() is fake_time assert another_module.get_localtime() is time.localtime assert another_module.get_localtime() is fake_localtime assert another_module.get_gmtime() is time.gmtime assert another_module.get_gmtime() is fake_gmtime assert another_module.get_strftime() is time.strftime assert another_module.get_strftime() is fake_strftime # Fakes assert another_module.get_fake_datetime() is FakeDatetime assert another_module.get_fake_date() is FakeDate assert another_module.get_fake_time() is fake_time assert another_module.get_fake_localtime() is fake_localtime assert another_module.get_fake_gmtime() is fake_gmtime assert another_module.get_fake_strftime() is fake_strftime # Reals assert another_module.get_datetime() is datetime.datetime assert not another_module.get_datetime() is FakeDatetime assert another_module.get_date() is datetime.date assert not another_module.get_date() is FakeDate assert another_module.get_time() is time.time assert not another_module.get_time() is fake_time assert another_module.get_localtime() is time.localtime assert not another_module.get_localtime() is fake_localtime assert another_module.get_gmtime() is time.gmtime assert not another_module.get_gmtime() is fake_gmtime assert another_module.get_strftime() is time.strftime assert not another_module.get_strftime() is fake_strftime # Fakes assert another_module.get_fake_datetime() is FakeDatetime assert another_module.get_fake_date() is FakeDate assert another_module.get_fake_time() is fake_time assert another_module.get_fake_localtime() is fake_localtime assert another_module.get_fake_gmtime() is fake_gmtime assert another_module.get_fake_strftime() is fake_strftime del sys.modules['tests.another_module'] def test_none_as_initial(): with freeze_time() as ft: ft.move_to('2012-01-14') assert fake_strftime_function() == '2012' ", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_class_import.py::test_import_after_start,tests/test_class_import.py,NIO,flaky,Accepted,https://github.com/spulec/freezegun/pull/407,classify;root_cause;fix_proposal,"import time import sys from .fake_module import ( fake_date_function, fake_datetime_function, fake_gmtime_function, fake_localtime_function, fake_strftime_function, fake_time_function, ) from . import fake_module from freezegun import freeze_time from freezegun.api import ( FakeDatetime, FakeDate, fake_time, fake_localtime, fake_gmtime, fake_strftime, ) import datetime @freeze_time(""2012-01-14"") def test_import_datetime_works(): assert fake_datetime_function().day == 14 @freeze_time(""2012-01-14"") def test_import_date_works(): assert fake_date_function().day == 14 @freeze_time(""2012-01-14"") def test_import_time(): local_time = datetime.datetime(2012, 1, 14) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) assert fake_time_function() == expected_timestamp def test_start_and_stop_works(): freezer = freeze_time(""2012-01-14"") result = fake_datetime_function() assert result.__class__ == datetime.datetime assert result.__class__ != FakeDatetime freezer.start() assert fake_datetime_function().day == 14 assert isinstance(fake_datetime_function(), datetime.datetime) assert isinstance(fake_datetime_function(), FakeDatetime) freezer.stop() result = fake_datetime_function() assert result.__class__ == datetime.datetime assert result.__class__ != FakeDatetime def test_isinstance_works(): date = datetime.date.today() now = datetime.datetime.now() freezer = freeze_time('2011-01-01') freezer.start() assert isinstance(date, datetime.date) assert not isinstance(date, datetime.datetime) assert isinstance(now, datetime.datetime) assert isinstance(now, datetime.date) freezer.stop() def test_issubclass_works(): real_date = datetime.date real_datetime = datetime.datetime freezer = freeze_time('2011-01-01') freezer.start() assert issubclass(real_date, datetime.date) assert issubclass(real_datetime, datetime.datetime) freezer.stop() def test_fake_uses_real_when_ignored(): real_time_before = time.time() with freeze_time('2012-01-14', ignore=['tests.fake_module']): real_time = fake_time_function() real_time_after = time.time() assert real_time_before <= real_time <= real_time_after def test_can_ignore_email_module(): from email.utils import formatdate with freeze_time('2012-01-14'): faked_date_str = formatdate() before_date_str = formatdate() with freeze_time('2012-01-14', ignore=['email']): date_str = formatdate() after_date_str = formatdate() assert date_str != faked_date_str assert before_date_str <= date_str <= after_date_str @freeze_time('2011-01-01') def test_avoid_replacing_equal_to_anything(): assert fake_module.equal_to_anything.description == 'This is the equal_to_anything object' @freeze_time(""2012-01-14 12:00:00"") def test_import_localtime(): struct = fake_localtime_function() assert struct.tm_year == 2012 assert struct.tm_mon == 1 assert struct.tm_mday >= 13 # eg. GMT+14 assert struct.tm_mday <= 15 # eg. GMT-14 @freeze_time(""2012-01-14 12:00:00"") def test_fake_gmtime_function(): struct = fake_gmtime_function() assert struct.tm_year == 2012 assert struct.tm_mon == 1 assert struct.tm_mday == 14 @freeze_time(""2012-01-14"") def test_fake_strftime_function(): assert fake_strftime_function() == '2012' def test_import_after_start(): with freeze_time('2012-01-14'): assert 'tests.another_module' not in sys.modules.keys() from tests import another_module # Reals assert another_module.get_datetime() is datetime.datetime assert another_module.get_datetime() is FakeDatetime assert another_module.get_date() is datetime.date assert another_module.get_date() is FakeDate assert another_module.get_time() is time.time assert another_module.get_time() is fake_time assert another_module.get_localtime() is time.localtime assert another_module.get_localtime() is fake_localtime assert another_module.get_gmtime() is time.gmtime assert another_module.get_gmtime() is fake_gmtime assert another_module.get_strftime() is time.strftime assert another_module.get_strftime() is fake_strftime # Fakes assert another_module.get_fake_datetime() is FakeDatetime assert another_module.get_fake_date() is FakeDate assert another_module.get_fake_time() is fake_time assert another_module.get_fake_localtime() is fake_localtime assert another_module.get_fake_gmtime() is fake_gmtime assert another_module.get_fake_strftime() is fake_strftime # Reals assert another_module.get_datetime() is datetime.datetime assert not another_module.get_datetime() is FakeDatetime assert another_module.get_date() is datetime.date assert not another_module.get_date() is FakeDate assert another_module.get_time() is time.time assert not another_module.get_time() is fake_time assert another_module.get_localtime() is time.localtime assert not another_module.get_localtime() is fake_localtime assert another_module.get_gmtime() is time.gmtime assert not another_module.get_gmtime() is fake_gmtime assert another_module.get_strftime() is time.strftime assert not another_module.get_strftime() is fake_strftime # Fakes assert another_module.get_fake_datetime() is FakeDatetime assert another_module.get_fake_date() is FakeDate assert another_module.get_fake_time() is fake_time assert another_module.get_fake_localtime() is fake_localtime assert another_module.get_fake_gmtime() is fake_gmtime assert another_module.get_fake_strftime() is fake_strftime def test_none_as_initial(): with freeze_time() as ft: ft.move_to('2012-01-14') assert fake_strftime_function() == '2012' ", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_datetimes.py::test_context_manager,tests/test_datetimes.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import time import calendar import datetime import unittest import locale import sys from unittest import SkipTest from dateutil.tz import UTC import pytest from tests import utils from freezegun import freeze_time from freezegun.api import FakeDatetime, FakeDate try: import maya except ImportError: maya = None # time.clock was removed in Python 3.8 HAS_CLOCK = hasattr(time, 'clock') HAS_TIME_NS = hasattr(time, 'time_ns') class temp_locale(object): """"""Temporarily change the locale."""""" def __init__(self, *targets): self.targets = targets def __enter__(self): self.old = locale.setlocale(locale.LC_ALL) for target in self.targets: try: locale.setlocale(locale.LC_ALL, target) return except locale.Error: pass msg = 'could not set locale to any of: %s' % ', '.join(self.targets) raise SkipTest(msg) def __exit__(self, *args): locale.setlocale(locale.LC_ALL, self.old) # Small sample of locales where '%x' expands to a dd/mm/yyyy string, # which can cause trouble when parsed with dateutil. _dd_mm_yyyy_locales = ['da_DK.UTF-8', 'de_DE.UTF-8', 'fr_FR.UTF-8'] def test_simple_api(): # time to freeze is always provided in UTC freezer = freeze_time(""2012-01-14"") # expected timestamp must be a timestamp, corresponding to 2012-01-14 UTC local_time = datetime.datetime(2012, 1, 14) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert time.time() == expected_timestamp assert datetime.datetime.now() == datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14) assert datetime.date.today() == datetime.date(2012, 1, 14) assert datetime.datetime.now().today() == datetime.datetime(2012, 1, 14) freezer.stop() assert time.time() != expected_timestamp assert datetime.datetime.now() != datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() != datetime.datetime(2012, 1, 14) freezer = freeze_time(""2012-01-10 13:52:01"") freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 10, 13, 52, 1) freezer.stop() def test_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-4) # expected timestamp must be a timestamp, # corresponding to 2012-01-14 03:21:34 UTC # and it doesn't depend on tz_offset local_time = datetime.datetime(2012, 1, 14, 3, 21, 34) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 21, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) assert time.time() == expected_timestamp freezer.stop() def test_timedelta_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-datetime.timedelta(hours=3, minutes=30)) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 51, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) freezer.stop() def test_tz_offset_with_today(): freezer = freeze_time(""2012-01-14"", tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(2012, 1, 13) freezer.stop() assert datetime.date.today() != datetime.date(2012, 1, 13) def test_zero_tz_offset_with_time(): # we expect the system to behave like a system with UTC timezone # at the beginning of the Epoch freezer = freeze_time('1970-01-01') freezer.start() assert datetime.date.today() == datetime.date(1970, 1, 1) assert datetime.datetime.now() == datetime.datetime(1970, 1, 1) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_tz_offset_with_time(): # we expect the system to behave like a system with UTC-4 timezone # at the beginning of the Epoch (wall clock should be 4 hrs late) freezer = freeze_time('1970-01-01', tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(1969, 12, 31) assert datetime.datetime.now() == datetime.datetime(1969, 12, 31, 20) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_time_with_microseconds(): freezer = freeze_time(datetime.datetime(1970, 1, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 1.123456 freezer.stop() def test_time_with_dst(): freezer = freeze_time(datetime.datetime(1970, 6, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 13046401.123456 freezer.stop() def test_manual_increment(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(delta=datetime.timedelta(seconds=10)) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_manual_increment_seconds(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(10) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_move_to(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) other_datetime = datetime.datetime(year=2, month=8, day=13, hour=14, minute=5, second=0) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.move_to(other_datetime) assert frozen_datetime() == other_datetime frozen_datetime.move_to(initial_datetime) assert frozen_datetime() == initial_datetime def test_bad_time_argument(): try: freeze_time(""2012-13-14"", tz_offset=-4) except ValueError: pass else: assert False, ""Bad values should raise a ValueError"" def test_time_gmtime(): with freeze_time('2012-01-14 03:21:34'): time_struct = time.gmtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 3 assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 @pytest.mark.skipif(not HAS_CLOCK, reason=""time.clock was removed in Python 3.8"") def test_time_clock(): with freeze_time('2012-01-14 03:21:34'): assert time.clock() == 0 with freeze_time('2012-01-14 03:21:35'): assert time.clock() == 1 with freeze_time('2012-01-14 03:21:36'): assert time.clock() == 2 class modify_timezone(object): def __init__(self, new_timezone): self.new_timezone = new_timezone self.original_timezone = time.timezone def __enter__(self): time.timezone = self.new_timezone def __exit__(self, *args): time.timezone = self.original_timezone def test_time_localtime(): with modify_timezone(-3600): # Set this for UTC-1 with freeze_time('2012-01-14 03:21:34'): time_struct = time.localtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 4 # offset of 1 hour due to time zone assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 assert time.localtime().tm_year != 2012 def test_strftime(): with modify_timezone(0): with freeze_time('1970-01-01'): assert time.strftime(""%Y"") == ""1970"" def test_real_strftime_fall_through(): this_real_year = datetime.datetime.now().year with freeze_time(): assert time.strftime('%Y') == str(this_real_year) assert time.strftime('%Y', (2001, 1, 1, 1, 1, 1, 1, 1, 1)) == '2001' def test_date_object(): frozen_date = datetime.date(year=2012, month=11, day=10) date_freezer = freeze_time(frozen_date) regular_freezer = freeze_time('2012-11-10') assert date_freezer.time_to_freeze == regular_freezer.time_to_freeze def test_old_date_object(): frozen_date = datetime.date(year=1, month=1, day=1) with freeze_time(frozen_date): assert datetime.date.today() == frozen_date def test_date_with_locale(): with temp_locale(*_dd_mm_yyyy_locales): frozen_date = datetime.date(year=2012, month=1, day=2) date_freezer = freeze_time(frozen_date) assert date_freezer.time_to_freeze.date() == frozen_date def test_invalid_type(): try: freeze_time(int(4)) except TypeError: pass else: assert False, ""Bad types should raise a TypeError"" def test_datetime_object(): frozen_datetime = datetime.", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_datetimes.py::test_min_and_max,tests/test_datetimes.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import time import calendar import datetime import unittest import locale import sys from unittest import SkipTest from dateutil.tz import UTC import pytest from tests import utils from freezegun import freeze_time from freezegun.api import FakeDatetime, FakeDate try: import maya except ImportError: maya = None # time.clock was removed in Python 3.8 HAS_CLOCK = hasattr(time, 'clock') HAS_TIME_NS = hasattr(time, 'time_ns') class temp_locale(object): """"""Temporarily change the locale."""""" def __init__(self, *targets): self.targets = targets def __enter__(self): self.old = locale.setlocale(locale.LC_ALL) for target in self.targets: try: locale.setlocale(locale.LC_ALL, target) return except locale.Error: pass msg = 'could not set locale to any of: %s' % ', '.join(self.targets) raise SkipTest(msg) def __exit__(self, *args): locale.setlocale(locale.LC_ALL, self.old) # Small sample of locales where '%x' expands to a dd/mm/yyyy string, # which can cause trouble when parsed with dateutil. _dd_mm_yyyy_locales = ['da_DK.UTF-8', 'de_DE.UTF-8', 'fr_FR.UTF-8'] def test_simple_api(): # time to freeze is always provided in UTC freezer = freeze_time(""2012-01-14"") # expected timestamp must be a timestamp, corresponding to 2012-01-14 UTC local_time = datetime.datetime(2012, 1, 14) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert time.time() == expected_timestamp assert datetime.datetime.now() == datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14) assert datetime.date.today() == datetime.date(2012, 1, 14) assert datetime.datetime.now().today() == datetime.datetime(2012, 1, 14) freezer.stop() assert time.time() != expected_timestamp assert datetime.datetime.now() != datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() != datetime.datetime(2012, 1, 14) freezer = freeze_time(""2012-01-10 13:52:01"") freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 10, 13, 52, 1) freezer.stop() def test_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-4) # expected timestamp must be a timestamp, # corresponding to 2012-01-14 03:21:34 UTC # and it doesn't depend on tz_offset local_time = datetime.datetime(2012, 1, 14, 3, 21, 34) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 21, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) assert time.time() == expected_timestamp freezer.stop() def test_timedelta_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-datetime.timedelta(hours=3, minutes=30)) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 51, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) freezer.stop() def test_tz_offset_with_today(): freezer = freeze_time(""2012-01-14"", tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(2012, 1, 13) freezer.stop() assert datetime.date.today() != datetime.date(2012, 1, 13) def test_zero_tz_offset_with_time(): # we expect the system to behave like a system with UTC timezone # at the beginning of the Epoch freezer = freeze_time('1970-01-01') freezer.start() assert datetime.date.today() == datetime.date(1970, 1, 1) assert datetime.datetime.now() == datetime.datetime(1970, 1, 1) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_tz_offset_with_time(): # we expect the system to behave like a system with UTC-4 timezone # at the beginning of the Epoch (wall clock should be 4 hrs late) freezer = freeze_time('1970-01-01', tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(1969, 12, 31) assert datetime.datetime.now() == datetime.datetime(1969, 12, 31, 20) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_time_with_microseconds(): freezer = freeze_time(datetime.datetime(1970, 1, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 1.123456 freezer.stop() def test_time_with_dst(): freezer = freeze_time(datetime.datetime(1970, 6, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 13046401.123456 freezer.stop() def test_manual_increment(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(delta=datetime.timedelta(seconds=10)) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_manual_increment_seconds(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(10) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_move_to(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) other_datetime = datetime.datetime(year=2, month=8, day=13, hour=14, minute=5, second=0) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.move_to(other_datetime) assert frozen_datetime() == other_datetime frozen_datetime.move_to(initial_datetime) assert frozen_datetime() == initial_datetime def test_bad_time_argument(): try: freeze_time(""2012-13-14"", tz_offset=-4) except ValueError: pass else: assert False, ""Bad values should raise a ValueError"" def test_time_gmtime(): with freeze_time('2012-01-14 03:21:34'): time_struct = time.gmtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 3 assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 @pytest.mark.skipif(not HAS_CLOCK, reason=""time.clock was removed in Python 3.8"") def test_time_clock(): with freeze_time('2012-01-14 03:21:34'): assert time.clock() == 0 with freeze_time('2012-01-14 03:21:35'): assert time.clock() == 1 with freeze_time('2012-01-14 03:21:36'): assert time.clock() == 2 class modify_timezone(object): def __init__(self, new_timezone): self.new_timezone = new_timezone self.original_timezone = time.timezone def __enter__(self): time.timezone = self.new_timezone def __exit__(self, *args): time.timezone = self.original_timezone def test_time_localtime(): with modify_timezone(-3600): # Set this for UTC-1 with freeze_time('2012-01-14 03:21:34'): time_struct = time.localtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 4 # offset of 1 hour due to time zone assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 assert time.localtime().tm_year != 2012 def test_strftime(): with modify_timezone(0): with freeze_time('1970-01-01'): assert time.strftime(""%Y"") == ""1970"" def test_real_strftime_fall_through(): this_real_year = datetime.datetime.now().year with freeze_time(): assert time.strftime('%Y') == str(this_real_year) assert time.strftime('%Y', (2001, 1, 1, 1, 1, 1, 1, 1, 1)) == '2001' def test_date_object(): frozen_date = datetime.date(year=2012, month=11, day=10) date_freezer = freeze_time(frozen_date) regular_freezer = freeze_time('2012-11-10') assert date_freezer.time_to_freeze == regular_freezer.time_to_freeze def test_old_date_object(): frozen_date = datetime.date(year=1, month=1, day=1) with freeze_time(frozen_date): assert datetime.date.today() == frozen_date def test_date_with_locale(): with temp_locale(*_dd_mm_yyyy_locales): frozen_date = datetime.date(year=2012, month=1, day=2) date_freezer = freeze_time(frozen_date) assert date_freezer.time_to_freeze.date() == frozen_date def test_invalid_type(): try: freeze_time(int(4)) except TypeError: pass else: assert False, ""Bad types should raise a TypeError"" def test_datetime_object(): frozen_datetime = datetime.", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_datetimes.py::test_nested_context_manager,tests/test_datetimes.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import time import calendar import datetime import unittest import locale import sys from unittest import SkipTest from dateutil.tz import UTC import pytest from tests import utils from freezegun import freeze_time from freezegun.api import FakeDatetime, FakeDate try: import maya except ImportError: maya = None # time.clock was removed in Python 3.8 HAS_CLOCK = hasattr(time, 'clock') HAS_TIME_NS = hasattr(time, 'time_ns') class temp_locale(object): """"""Temporarily change the locale."""""" def __init__(self, *targets): self.targets = targets def __enter__(self): self.old = locale.setlocale(locale.LC_ALL) for target in self.targets: try: locale.setlocale(locale.LC_ALL, target) return except locale.Error: pass msg = 'could not set locale to any of: %s' % ', '.join(self.targets) raise SkipTest(msg) def __exit__(self, *args): locale.setlocale(locale.LC_ALL, self.old) # Small sample of locales where '%x' expands to a dd/mm/yyyy string, # which can cause trouble when parsed with dateutil. _dd_mm_yyyy_locales = ['da_DK.UTF-8', 'de_DE.UTF-8', 'fr_FR.UTF-8'] def test_simple_api(): # time to freeze is always provided in UTC freezer = freeze_time(""2012-01-14"") # expected timestamp must be a timestamp, corresponding to 2012-01-14 UTC local_time = datetime.datetime(2012, 1, 14) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert time.time() == expected_timestamp assert datetime.datetime.now() == datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14) assert datetime.date.today() == datetime.date(2012, 1, 14) assert datetime.datetime.now().today() == datetime.datetime(2012, 1, 14) freezer.stop() assert time.time() != expected_timestamp assert datetime.datetime.now() != datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() != datetime.datetime(2012, 1, 14) freezer = freeze_time(""2012-01-10 13:52:01"") freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 10, 13, 52, 1) freezer.stop() def test_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-4) # expected timestamp must be a timestamp, # corresponding to 2012-01-14 03:21:34 UTC # and it doesn't depend on tz_offset local_time = datetime.datetime(2012, 1, 14, 3, 21, 34) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 21, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) assert time.time() == expected_timestamp freezer.stop() def test_timedelta_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-datetime.timedelta(hours=3, minutes=30)) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 51, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) freezer.stop() def test_tz_offset_with_today(): freezer = freeze_time(""2012-01-14"", tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(2012, 1, 13) freezer.stop() assert datetime.date.today() != datetime.date(2012, 1, 13) def test_zero_tz_offset_with_time(): # we expect the system to behave like a system with UTC timezone # at the beginning of the Epoch freezer = freeze_time('1970-01-01') freezer.start() assert datetime.date.today() == datetime.date(1970, 1, 1) assert datetime.datetime.now() == datetime.datetime(1970, 1, 1) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_tz_offset_with_time(): # we expect the system to behave like a system with UTC-4 timezone # at the beginning of the Epoch (wall clock should be 4 hrs late) freezer = freeze_time('1970-01-01', tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(1969, 12, 31) assert datetime.datetime.now() == datetime.datetime(1969, 12, 31, 20) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_time_with_microseconds(): freezer = freeze_time(datetime.datetime(1970, 1, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 1.123456 freezer.stop() def test_time_with_dst(): freezer = freeze_time(datetime.datetime(1970, 6, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 13046401.123456 freezer.stop() def test_manual_increment(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(delta=datetime.timedelta(seconds=10)) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_manual_increment_seconds(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(10) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_move_to(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) other_datetime = datetime.datetime(year=2, month=8, day=13, hour=14, minute=5, second=0) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.move_to(other_datetime) assert frozen_datetime() == other_datetime frozen_datetime.move_to(initial_datetime) assert frozen_datetime() == initial_datetime def test_bad_time_argument(): try: freeze_time(""2012-13-14"", tz_offset=-4) except ValueError: pass else: assert False, ""Bad values should raise a ValueError"" def test_time_gmtime(): with freeze_time('2012-01-14 03:21:34'): time_struct = time.gmtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 3 assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 @pytest.mark.skipif(not HAS_CLOCK, reason=""time.clock was removed in Python 3.8"") def test_time_clock(): with freeze_time('2012-01-14 03:21:34'): assert time.clock() == 0 with freeze_time('2012-01-14 03:21:35'): assert time.clock() == 1 with freeze_time('2012-01-14 03:21:36'): assert time.clock() == 2 class modify_timezone(object): def __init__(self, new_timezone): self.new_timezone = new_timezone self.original_timezone = time.timezone def __enter__(self): time.timezone = self.new_timezone def __exit__(self, *args): time.timezone = self.original_timezone def test_time_localtime(): with modify_timezone(-3600): # Set this for UTC-1 with freeze_time('2012-01-14 03:21:34'): time_struct = time.localtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 4 # offset of 1 hour due to time zone assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 assert time.localtime().tm_year != 2012 def test_strftime(): with modify_timezone(0): with freeze_time('1970-01-01'): assert time.strftime(""%Y"") == ""1970"" def test_real_strftime_fall_through(): this_real_year = datetime.datetime.now().year with freeze_time(): assert time.strftime('%Y') == str(this_real_year) assert time.strftime('%Y', (2001, 1, 1, 1, 1, 1, 1, 1, 1)) == '2001' def test_date_object(): frozen_date = datetime.date(year=2012, month=11, day=10) date_freezer = freeze_time(frozen_date) regular_freezer = freeze_time('2012-11-10') assert date_freezer.time_to_freeze == regular_freezer.time_to_freeze def test_old_date_object(): frozen_date = datetime.date(year=1, month=1, day=1) with freeze_time(frozen_date): assert datetime.date.today() == frozen_date def test_date_with_locale(): with temp_locale(*_dd_mm_yyyy_locales): frozen_date = datetime.date(year=2012, month=1, day=2) date_freezer = freeze_time(frozen_date) assert date_freezer.time_to_freeze.date() == frozen_date def test_invalid_type(): try: freeze_time(int(4)) except TypeError: pass else: assert False, ""Bad types should raise a TypeError"" def test_datetime_object(): frozen_datetime = datetime.", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_datetimes.py::test_nested_context_manager_with_tz_offsets,tests/test_datetimes.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import time import calendar import datetime import unittest import locale import sys from unittest import SkipTest from dateutil.tz import UTC import pytest from tests import utils from freezegun import freeze_time from freezegun.api import FakeDatetime, FakeDate try: import maya except ImportError: maya = None # time.clock was removed in Python 3.8 HAS_CLOCK = hasattr(time, 'clock') HAS_TIME_NS = hasattr(time, 'time_ns') class temp_locale(object): """"""Temporarily change the locale."""""" def __init__(self, *targets): self.targets = targets def __enter__(self): self.old = locale.setlocale(locale.LC_ALL) for target in self.targets: try: locale.setlocale(locale.LC_ALL, target) return except locale.Error: pass msg = 'could not set locale to any of: %s' % ', '.join(self.targets) raise SkipTest(msg) def __exit__(self, *args): locale.setlocale(locale.LC_ALL, self.old) # Small sample of locales where '%x' expands to a dd/mm/yyyy string, # which can cause trouble when parsed with dateutil. _dd_mm_yyyy_locales = ['da_DK.UTF-8', 'de_DE.UTF-8', 'fr_FR.UTF-8'] def test_simple_api(): # time to freeze is always provided in UTC freezer = freeze_time(""2012-01-14"") # expected timestamp must be a timestamp, corresponding to 2012-01-14 UTC local_time = datetime.datetime(2012, 1, 14) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert time.time() == expected_timestamp assert datetime.datetime.now() == datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14) assert datetime.date.today() == datetime.date(2012, 1, 14) assert datetime.datetime.now().today() == datetime.datetime(2012, 1, 14) freezer.stop() assert time.time() != expected_timestamp assert datetime.datetime.now() != datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() != datetime.datetime(2012, 1, 14) freezer = freeze_time(""2012-01-10 13:52:01"") freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 10, 13, 52, 1) freezer.stop() def test_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-4) # expected timestamp must be a timestamp, # corresponding to 2012-01-14 03:21:34 UTC # and it doesn't depend on tz_offset local_time = datetime.datetime(2012, 1, 14, 3, 21, 34) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 21, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) assert time.time() == expected_timestamp freezer.stop() def test_timedelta_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-datetime.timedelta(hours=3, minutes=30)) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 51, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) freezer.stop() def test_tz_offset_with_today(): freezer = freeze_time(""2012-01-14"", tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(2012, 1, 13) freezer.stop() assert datetime.date.today() != datetime.date(2012, 1, 13) def test_zero_tz_offset_with_time(): # we expect the system to behave like a system with UTC timezone # at the beginning of the Epoch freezer = freeze_time('1970-01-01') freezer.start() assert datetime.date.today() == datetime.date(1970, 1, 1) assert datetime.datetime.now() == datetime.datetime(1970, 1, 1) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_tz_offset_with_time(): # we expect the system to behave like a system with UTC-4 timezone # at the beginning of the Epoch (wall clock should be 4 hrs late) freezer = freeze_time('1970-01-01', tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(1969, 12, 31) assert datetime.datetime.now() == datetime.datetime(1969, 12, 31, 20) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_time_with_microseconds(): freezer = freeze_time(datetime.datetime(1970, 1, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 1.123456 freezer.stop() def test_time_with_dst(): freezer = freeze_time(datetime.datetime(1970, 6, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 13046401.123456 freezer.stop() def test_manual_increment(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(delta=datetime.timedelta(seconds=10)) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_manual_increment_seconds(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(10) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_move_to(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) other_datetime = datetime.datetime(year=2, month=8, day=13, hour=14, minute=5, second=0) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.move_to(other_datetime) assert frozen_datetime() == other_datetime frozen_datetime.move_to(initial_datetime) assert frozen_datetime() == initial_datetime def test_bad_time_argument(): try: freeze_time(""2012-13-14"", tz_offset=-4) except ValueError: pass else: assert False, ""Bad values should raise a ValueError"" def test_time_gmtime(): with freeze_time('2012-01-14 03:21:34'): time_struct = time.gmtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 3 assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 @pytest.mark.skipif(not HAS_CLOCK, reason=""time.clock was removed in Python 3.8"") def test_time_clock(): with freeze_time('2012-01-14 03:21:34'): assert time.clock() == 0 with freeze_time('2012-01-14 03:21:35'): assert time.clock() == 1 with freeze_time('2012-01-14 03:21:36'): assert time.clock() == 2 class modify_timezone(object): def __init__(self, new_timezone): self.new_timezone = new_timezone self.original_timezone = time.timezone def __enter__(self): time.timezone = self.new_timezone def __exit__(self, *args): time.timezone = self.original_timezone def test_time_localtime(): with modify_timezone(-3600): # Set this for UTC-1 with freeze_time('2012-01-14 03:21:34'): time_struct = time.localtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 4 # offset of 1 hour due to time zone assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 assert time.localtime().tm_year != 2012 def test_strftime(): with modify_timezone(0): with freeze_time('1970-01-01'): assert time.strftime(""%Y"") == ""1970"" def test_real_strftime_fall_through(): this_real_year = datetime.datetime.now().year with freeze_time(): assert time.strftime('%Y') == str(this_real_year) assert time.strftime('%Y', (2001, 1, 1, 1, 1, 1, 1, 1, 1)) == '2001' def test_date_object(): frozen_date = datetime.date(year=2012, month=11, day=10) date_freezer = freeze_time(frozen_date) regular_freezer = freeze_time('2012-11-10') assert date_freezer.time_to_freeze == regular_freezer.time_to_freeze def test_old_date_object(): frozen_date = datetime.date(year=1, month=1, day=1) with freeze_time(frozen_date): assert datetime.date.today() == frozen_date def test_date_with_locale(): with temp_locale(*_dd_mm_yyyy_locales): frozen_date = datetime.date(year=2012, month=1, day=2) date_freezer = freeze_time(frozen_date) assert date_freezer.time_to_freeze.date() == frozen_date def test_invalid_type(): try: freeze_time(int(4)) except TypeError: pass else: assert False, ""Bad types should raise a TypeError"" def test_datetime_object(): frozen_datetime = datetime.", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_datetimes.py::test_simple_api,tests/test_datetimes.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import time import calendar import datetime import unittest import locale import sys from unittest import SkipTest from dateutil.tz import UTC import pytest from tests import utils from freezegun import freeze_time from freezegun.api import FakeDatetime, FakeDate try: import maya except ImportError: maya = None # time.clock was removed in Python 3.8 HAS_CLOCK = hasattr(time, 'clock') HAS_TIME_NS = hasattr(time, 'time_ns') class temp_locale(object): """"""Temporarily change the locale."""""" def __init__(self, *targets): self.targets = targets def __enter__(self): self.old = locale.setlocale(locale.LC_ALL) for target in self.targets: try: locale.setlocale(locale.LC_ALL, target) return except locale.Error: pass msg = 'could not set locale to any of: %s' % ', '.join(self.targets) raise SkipTest(msg) def __exit__(self, *args): locale.setlocale(locale.LC_ALL, self.old) # Small sample of locales where '%x' expands to a dd/mm/yyyy string, # which can cause trouble when parsed with dateutil. _dd_mm_yyyy_locales = ['da_DK.UTF-8', 'de_DE.UTF-8', 'fr_FR.UTF-8'] def test_simple_api(): # time to freeze is always provided in UTC freezer = freeze_time(""2012-01-14"") # expected timestamp must be a timestamp, corresponding to 2012-01-14 UTC local_time = datetime.datetime(2012, 1, 14) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert time.time() == expected_timestamp assert datetime.datetime.now() == datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14) assert datetime.date.today() == datetime.date(2012, 1, 14) assert datetime.datetime.now().today() == datetime.datetime(2012, 1, 14) freezer.stop() assert time.time() != expected_timestamp assert datetime.datetime.now() != datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() != datetime.datetime(2012, 1, 14) freezer = freeze_time(""2012-01-10 13:52:01"") freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 10, 13, 52, 1) freezer.stop() def test_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-4) # expected timestamp must be a timestamp, # corresponding to 2012-01-14 03:21:34 UTC # and it doesn't depend on tz_offset local_time = datetime.datetime(2012, 1, 14, 3, 21, 34) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 21, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) assert time.time() == expected_timestamp freezer.stop() def test_timedelta_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-datetime.timedelta(hours=3, minutes=30)) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 51, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) freezer.stop() def test_tz_offset_with_today(): freezer = freeze_time(""2012-01-14"", tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(2012, 1, 13) freezer.stop() assert datetime.date.today() != datetime.date(2012, 1, 13) def test_zero_tz_offset_with_time(): # we expect the system to behave like a system with UTC timezone # at the beginning of the Epoch freezer = freeze_time('1970-01-01') freezer.start() assert datetime.date.today() == datetime.date(1970, 1, 1) assert datetime.datetime.now() == datetime.datetime(1970, 1, 1) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_tz_offset_with_time(): # we expect the system to behave like a system with UTC-4 timezone # at the beginning of the Epoch (wall clock should be 4 hrs late) freezer = freeze_time('1970-01-01', tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(1969, 12, 31) assert datetime.datetime.now() == datetime.datetime(1969, 12, 31, 20) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_time_with_microseconds(): freezer = freeze_time(datetime.datetime(1970, 1, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 1.123456 freezer.stop() def test_time_with_dst(): freezer = freeze_time(datetime.datetime(1970, 6, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 13046401.123456 freezer.stop() def test_manual_increment(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(delta=datetime.timedelta(seconds=10)) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_manual_increment_seconds(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(10) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_move_to(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) other_datetime = datetime.datetime(year=2, month=8, day=13, hour=14, minute=5, second=0) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.move_to(other_datetime) assert frozen_datetime() == other_datetime frozen_datetime.move_to(initial_datetime) assert frozen_datetime() == initial_datetime def test_bad_time_argument(): try: freeze_time(""2012-13-14"", tz_offset=-4) except ValueError: pass else: assert False, ""Bad values should raise a ValueError"" def test_time_gmtime(): with freeze_time('2012-01-14 03:21:34'): time_struct = time.gmtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 3 assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 @pytest.mark.skipif(not HAS_CLOCK, reason=""time.clock was removed in Python 3.8"") def test_time_clock(): with freeze_time('2012-01-14 03:21:34'): assert time.clock() == 0 with freeze_time('2012-01-14 03:21:35'): assert time.clock() == 1 with freeze_time('2012-01-14 03:21:36'): assert time.clock() == 2 class modify_timezone(object): def __init__(self, new_timezone): self.new_timezone = new_timezone self.original_timezone = time.timezone def __enter__(self): time.timezone = self.new_timezone def __exit__(self, *args): time.timezone = self.original_timezone def test_time_localtime(): with modify_timezone(-3600): # Set this for UTC-1 with freeze_time('2012-01-14 03:21:34'): time_struct = time.localtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 4 # offset of 1 hour due to time zone assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 assert time.localtime().tm_year != 2012 def test_strftime(): with modify_timezone(0): with freeze_time('1970-01-01'): assert time.strftime(""%Y"") == ""1970"" def test_real_strftime_fall_through(): this_real_year = datetime.datetime.now().year with freeze_time(): assert time.strftime('%Y') == str(this_real_year) assert time.strftime('%Y', (2001, 1, 1, 1, 1, 1, 1, 1, 1)) == '2001' def test_date_object(): frozen_date = datetime.date(year=2012, month=11, day=10) date_freezer = freeze_time(frozen_date) regular_freezer = freeze_time('2012-11-10') assert date_freezer.time_to_freeze == regular_freezer.time_to_freeze def test_old_date_object(): frozen_date = datetime.date(year=1, month=1, day=1) with freeze_time(frozen_date): assert datetime.date.today() == frozen_date def test_date_with_locale(): with temp_locale(*_dd_mm_yyyy_locales): frozen_date = datetime.date(year=2012, month=1, day=2) date_freezer = freeze_time(frozen_date) assert date_freezer.time_to_freeze.date() == frozen_date def test_invalid_type(): try: freeze_time(int(4)) except TypeError: pass else: assert False, ""Bad types should raise a TypeError"" def test_datetime_object(): frozen_datetime = datetime.", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_datetimes.py::test_time_localtime,tests/test_datetimes.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import time import calendar import datetime import unittest import locale import sys from unittest import SkipTest from dateutil.tz import UTC import pytest from tests import utils from freezegun import freeze_time from freezegun.api import FakeDatetime, FakeDate try: import maya except ImportError: maya = None # time.clock was removed in Python 3.8 HAS_CLOCK = hasattr(time, 'clock') HAS_TIME_NS = hasattr(time, 'time_ns') class temp_locale(object): """"""Temporarily change the locale."""""" def __init__(self, *targets): self.targets = targets def __enter__(self): self.old = locale.setlocale(locale.LC_ALL) for target in self.targets: try: locale.setlocale(locale.LC_ALL, target) return except locale.Error: pass msg = 'could not set locale to any of: %s' % ', '.join(self.targets) raise SkipTest(msg) def __exit__(self, *args): locale.setlocale(locale.LC_ALL, self.old) # Small sample of locales where '%x' expands to a dd/mm/yyyy string, # which can cause trouble when parsed with dateutil. _dd_mm_yyyy_locales = ['da_DK.UTF-8', 'de_DE.UTF-8', 'fr_FR.UTF-8'] def test_simple_api(): # time to freeze is always provided in UTC freezer = freeze_time(""2012-01-14"") # expected timestamp must be a timestamp, corresponding to 2012-01-14 UTC local_time = datetime.datetime(2012, 1, 14) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert time.time() == expected_timestamp assert datetime.datetime.now() == datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14) assert datetime.date.today() == datetime.date(2012, 1, 14) assert datetime.datetime.now().today() == datetime.datetime(2012, 1, 14) freezer.stop() assert time.time() != expected_timestamp assert datetime.datetime.now() != datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() != datetime.datetime(2012, 1, 14) freezer = freeze_time(""2012-01-10 13:52:01"") freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 10, 13, 52, 1) freezer.stop() def test_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-4) # expected timestamp must be a timestamp, # corresponding to 2012-01-14 03:21:34 UTC # and it doesn't depend on tz_offset local_time = datetime.datetime(2012, 1, 14, 3, 21, 34) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 21, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) assert time.time() == expected_timestamp freezer.stop() def test_timedelta_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-datetime.timedelta(hours=3, minutes=30)) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 51, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) freezer.stop() def test_tz_offset_with_today(): freezer = freeze_time(""2012-01-14"", tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(2012, 1, 13) freezer.stop() assert datetime.date.today() != datetime.date(2012, 1, 13) def test_zero_tz_offset_with_time(): # we expect the system to behave like a system with UTC timezone # at the beginning of the Epoch freezer = freeze_time('1970-01-01') freezer.start() assert datetime.date.today() == datetime.date(1970, 1, 1) assert datetime.datetime.now() == datetime.datetime(1970, 1, 1) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_tz_offset_with_time(): # we expect the system to behave like a system with UTC-4 timezone # at the beginning of the Epoch (wall clock should be 4 hrs late) freezer = freeze_time('1970-01-01', tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(1969, 12, 31) assert datetime.datetime.now() == datetime.datetime(1969, 12, 31, 20) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_time_with_microseconds(): freezer = freeze_time(datetime.datetime(1970, 1, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 1.123456 freezer.stop() def test_time_with_dst(): freezer = freeze_time(datetime.datetime(1970, 6, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 13046401.123456 freezer.stop() def test_manual_increment(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(delta=datetime.timedelta(seconds=10)) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_manual_increment_seconds(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(10) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_move_to(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) other_datetime = datetime.datetime(year=2, month=8, day=13, hour=14, minute=5, second=0) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.move_to(other_datetime) assert frozen_datetime() == other_datetime frozen_datetime.move_to(initial_datetime) assert frozen_datetime() == initial_datetime def test_bad_time_argument(): try: freeze_time(""2012-13-14"", tz_offset=-4) except ValueError: pass else: assert False, ""Bad values should raise a ValueError"" def test_time_gmtime(): with freeze_time('2012-01-14 03:21:34'): time_struct = time.gmtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 3 assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 @pytest.mark.skipif(not HAS_CLOCK, reason=""time.clock was removed in Python 3.8"") def test_time_clock(): with freeze_time('2012-01-14 03:21:34'): assert time.clock() == 0 with freeze_time('2012-01-14 03:21:35'): assert time.clock() == 1 with freeze_time('2012-01-14 03:21:36'): assert time.clock() == 2 class modify_timezone(object): def __init__(self, new_timezone): self.new_timezone = new_timezone self.original_timezone = time.timezone def __enter__(self): time.timezone = self.new_timezone def __exit__(self, *args): time.timezone = self.original_timezone def test_time_localtime(): with modify_timezone(-3600): # Set this for UTC-1 with freeze_time('2012-01-14 03:21:34'): time_struct = time.localtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 4 # offset of 1 hour due to time zone assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 assert time.localtime().tm_year != 2012 def test_strftime(): with modify_timezone(0): with freeze_time('1970-01-01'): assert time.strftime(""%Y"") == ""1970"" def test_real_strftime_fall_through(): this_real_year = datetime.datetime.now().year with freeze_time(): assert time.strftime('%Y') == str(this_real_year) assert time.strftime('%Y', (2001, 1, 1, 1, 1, 1, 1, 1, 1)) == '2001' def test_date_object(): frozen_date = datetime.date(year=2012, month=11, day=10) date_freezer = freeze_time(frozen_date) regular_freezer = freeze_time('2012-11-10') assert date_freezer.time_to_freeze == regular_freezer.time_to_freeze def test_old_date_object(): frozen_date = datetime.date(year=1, month=1, day=1) with freeze_time(frozen_date): assert datetime.date.today() == frozen_date def test_date_with_locale(): with temp_locale(*_dd_mm_yyyy_locales): frozen_date = datetime.date(year=2012, month=1, day=2) date_freezer = freeze_time(frozen_date) assert date_freezer.time_to_freeze.date() == frozen_date def test_invalid_type(): try: freeze_time(int(4)) except TypeError: pass else: assert False, ""Bad types should raise a TypeError"" def test_datetime_object(): frozen_datetime = datetime.", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_datetimes.py::test_time_ns,tests/test_datetimes.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import time import calendar import datetime import unittest import locale import sys from unittest import SkipTest from dateutil.tz import UTC import pytest from tests import utils from freezegun import freeze_time from freezegun.api import FakeDatetime, FakeDate try: import maya except ImportError: maya = None # time.clock was removed in Python 3.8 HAS_CLOCK = hasattr(time, 'clock') HAS_TIME_NS = hasattr(time, 'time_ns') class temp_locale(object): """"""Temporarily change the locale."""""" def __init__(self, *targets): self.targets = targets def __enter__(self): self.old = locale.setlocale(locale.LC_ALL) for target in self.targets: try: locale.setlocale(locale.LC_ALL, target) return except locale.Error: pass msg = 'could not set locale to any of: %s' % ', '.join(self.targets) raise SkipTest(msg) def __exit__(self, *args): locale.setlocale(locale.LC_ALL, self.old) # Small sample of locales where '%x' expands to a dd/mm/yyyy string, # which can cause trouble when parsed with dateutil. _dd_mm_yyyy_locales = ['da_DK.UTF-8', 'de_DE.UTF-8', 'fr_FR.UTF-8'] def test_simple_api(): # time to freeze is always provided in UTC freezer = freeze_time(""2012-01-14"") # expected timestamp must be a timestamp, corresponding to 2012-01-14 UTC local_time = datetime.datetime(2012, 1, 14) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert time.time() == expected_timestamp assert datetime.datetime.now() == datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14) assert datetime.date.today() == datetime.date(2012, 1, 14) assert datetime.datetime.now().today() == datetime.datetime(2012, 1, 14) freezer.stop() assert time.time() != expected_timestamp assert datetime.datetime.now() != datetime.datetime(2012, 1, 14) assert datetime.datetime.utcnow() != datetime.datetime(2012, 1, 14) freezer = freeze_time(""2012-01-10 13:52:01"") freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 10, 13, 52, 1) freezer.stop() def test_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-4) # expected timestamp must be a timestamp, # corresponding to 2012-01-14 03:21:34 UTC # and it doesn't depend on tz_offset local_time = datetime.datetime(2012, 1, 14, 3, 21, 34) utc_time = local_time - datetime.timedelta(seconds=time.timezone) expected_timestamp = time.mktime(utc_time.timetuple()) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 21, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) assert time.time() == expected_timestamp freezer.stop() def test_timedelta_tz_offset(): freezer = freeze_time(""2012-01-14 03:21:34"", tz_offset=-datetime.timedelta(hours=3, minutes=30)) freezer.start() assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 51, 34) assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34) freezer.stop() def test_tz_offset_with_today(): freezer = freeze_time(""2012-01-14"", tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(2012, 1, 13) freezer.stop() assert datetime.date.today() != datetime.date(2012, 1, 13) def test_zero_tz_offset_with_time(): # we expect the system to behave like a system with UTC timezone # at the beginning of the Epoch freezer = freeze_time('1970-01-01') freezer.start() assert datetime.date.today() == datetime.date(1970, 1, 1) assert datetime.datetime.now() == datetime.datetime(1970, 1, 1) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_tz_offset_with_time(): # we expect the system to behave like a system with UTC-4 timezone # at the beginning of the Epoch (wall clock should be 4 hrs late) freezer = freeze_time('1970-01-01', tz_offset=-4) freezer.start() assert datetime.date.today() == datetime.date(1969, 12, 31) assert datetime.datetime.now() == datetime.datetime(1969, 12, 31, 20) assert datetime.datetime.utcnow() == datetime.datetime(1970, 1, 1) assert time.time() == 0.0 freezer.stop() def test_time_with_microseconds(): freezer = freeze_time(datetime.datetime(1970, 1, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 1.123456 freezer.stop() def test_time_with_dst(): freezer = freeze_time(datetime.datetime(1970, 6, 1, 0, 0, 1, 123456)) freezer.start() assert time.time() == 13046401.123456 freezer.stop() def test_manual_increment(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(delta=datetime.timedelta(seconds=10)) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_manual_increment_seconds(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.tick() initial_datetime += datetime.timedelta(seconds=1) assert frozen_datetime() == initial_datetime frozen_datetime.tick(10) initial_datetime += datetime.timedelta(seconds=10) assert frozen_datetime() == initial_datetime def test_move_to(): initial_datetime = datetime.datetime(year=1, month=7, day=12, hour=15, minute=6, second=3) other_datetime = datetime.datetime(year=2, month=8, day=13, hour=14, minute=5, second=0) with freeze_time(initial_datetime) as frozen_datetime: assert frozen_datetime() == initial_datetime frozen_datetime.move_to(other_datetime) assert frozen_datetime() == other_datetime frozen_datetime.move_to(initial_datetime) assert frozen_datetime() == initial_datetime def test_bad_time_argument(): try: freeze_time(""2012-13-14"", tz_offset=-4) except ValueError: pass else: assert False, ""Bad values should raise a ValueError"" def test_time_gmtime(): with freeze_time('2012-01-14 03:21:34'): time_struct = time.gmtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 3 assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 @pytest.mark.skipif(not HAS_CLOCK, reason=""time.clock was removed in Python 3.8"") def test_time_clock(): with freeze_time('2012-01-14 03:21:34'): assert time.clock() == 0 with freeze_time('2012-01-14 03:21:35'): assert time.clock() == 1 with freeze_time('2012-01-14 03:21:36'): assert time.clock() == 2 class modify_timezone(object): def __init__(self, new_timezone): self.new_timezone = new_timezone self.original_timezone = time.timezone def __enter__(self): time.timezone = self.new_timezone def __exit__(self, *args): time.timezone = self.original_timezone def test_time_localtime(): with modify_timezone(-3600): # Set this for UTC-1 with freeze_time('2012-01-14 03:21:34'): time_struct = time.localtime() assert time_struct.tm_year == 2012 assert time_struct.tm_mon == 1 assert time_struct.tm_mday == 14 assert time_struct.tm_hour == 4 # offset of 1 hour due to time zone assert time_struct.tm_min == 21 assert time_struct.tm_sec == 34 assert time_struct.tm_wday == 5 assert time_struct.tm_yday == 14 assert time_struct.tm_isdst == -1 assert time.localtime().tm_year != 2012 def test_strftime(): with modify_timezone(0): with freeze_time('1970-01-01'): assert time.strftime(""%Y"") == ""1970"" def test_real_strftime_fall_through(): this_real_year = datetime.datetime.now().year with freeze_time(): assert time.strftime('%Y') == str(this_real_year) assert time.strftime('%Y', (2001, 1, 1, 1, 1, 1, 1, 1, 1)) == '2001' def test_date_object(): frozen_date = datetime.date(year=2012, month=11, day=10) date_freezer = freeze_time(frozen_date) regular_freezer = freeze_time('2012-11-10') assert date_freezer.time_to_freeze == regular_freezer.time_to_freeze def test_old_date_object(): frozen_date = datetime.date(year=1, month=1, day=1) with freeze_time(frozen_date): assert datetime.date.today() == frozen_date def test_date_with_locale(): with temp_locale(*_dd_mm_yyyy_locales): frozen_date = datetime.date(year=2012, month=1, day=2) date_freezer = freeze_time(frozen_date) assert date_freezer.time_to_freeze.date() == frozen_date def test_invalid_type(): try: freeze_time(int(4)) except TypeError: pass else: assert False, ""Bad types should raise a TypeError"" def test_datetime_object(): frozen_datetime = datetime.", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_errors.py::test_ignore_errors_in_start[ImportError],tests/test_errors.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import contextlib import datetime import sys import pytest from freezegun import freeze_time class ModuleWithError(object): """""" A module that triggers an error on __dir__ access. This could happen with modules that overrides its __dir__ method and performing non standard operations. One such example is IPython which adds shim modules when certain packages are imported. (Eg. `import IPython.html`) This leads to errors upon activating freezegun if the modules being shimmed are not installed. See: https://github.com/ipython/ipython/blob/5.8.0/IPython/utils/shimmodule.py#L75 """""" __name__ = 'module_with_error' __dict__ = {} def __init__(self, error_type): self.error_triggered = False self.error_type = error_type def __dir__(self): try: raise self.error_type() finally: self.error_triggered = True @contextlib.contextmanager def assert_module_with_raised_error(error_type): """"""Install a module into sys.modules that raises an error upon invoking __dir__."""""" module = sys.modules['module_with_error'] = ModuleWithError(error_type) try: yield finally: del sys.modules['module_with_error'] assert module.error_triggered @pytest.mark.parametrize('error_type', [ImportError, TypeError]) def test_ignore_errors_in_start(error_type): with assert_module_with_raised_error(error_type): freezer = freeze_time(datetime.datetime(2019, 1, 11, 9, 34)) try: freezer.start() finally: freezer.stop() ", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_errors.py::test_ignore_errors_in_start[TypeError],tests/test_errors.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import contextlib import datetime import sys import pytest from freezegun import freeze_time class ModuleWithError(object): """""" A module that triggers an error on __dir__ access. This could happen with modules that overrides its __dir__ method and performing non standard operations. One such example is IPython which adds shim modules when certain packages are imported. (Eg. `import IPython.html`) This leads to errors upon activating freezegun if the modules being shimmed are not installed. See: https://github.com/ipython/ipython/blob/5.8.0/IPython/utils/shimmodule.py#L75 """""" __name__ = 'module_with_error' __dict__ = {} def __init__(self, error_type): self.error_triggered = False self.error_type = error_type def __dir__(self): try: raise self.error_type() finally: self.error_triggered = True @contextlib.contextmanager def assert_module_with_raised_error(error_type): """"""Install a module into sys.modules that raises an error upon invoking __dir__."""""" module = sys.modules['module_with_error'] = ModuleWithError(error_type) try: yield finally: del sys.modules['module_with_error'] assert module.error_triggered @pytest.mark.parametrize('error_type', [ImportError, TypeError]) def test_ignore_errors_in_start(error_type): with assert_module_with_raised_error(error_type): freezer = freeze_time(datetime.datetime(2019, 1, 11, 9, 34)) try: freezer.start() finally: freezer.stop() ", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_uuid.py::test_uuid1_future,tests/test_uuid.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import datetime import uuid from freezegun import freeze_time def time_from_uuid(value): """""" Converts an UUID(1) to it's datetime value """""" uvalue = value if isinstance(value, uuid.UUID) else uuid.UUID(value) assert uvalue.version == 1 return (datetime.datetime(1582, 10, 15) + datetime.timedelta(microseconds=uvalue.time // 10)) def test_uuid1_future(): """""" Test that we can go back in time after setting a future date. Normally UUID1 would disallow this, since it keeps track of the _last_timestamp, but we override that now. """""" future_target = datetime.datetime(2056, 2, 6, 14, 3, 21) with freeze_time(future_target): assert time_from_uuid(uuid.uuid1()) == future_target past_target = datetime.datetime(1978, 7, 6, 23, 6, 31) with freeze_time(past_target): assert time_from_uuid(uuid.uuid1()) == past_target def test_uuid1_past(): """""" Test that we can go forward in time after setting some time in the past. This is simply the opposite of test_uuid1_future() """""" past_target = datetime.datetime(1978, 7, 6, 23, 6, 31) with freeze_time(past_target): assert time_from_uuid(uuid.uuid1()) == past_target future_target = datetime.datetime(2056, 2, 6, 14, 3, 21) with freeze_time(future_target): assert time_from_uuid(uuid.uuid1()) == future_target ", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_warnings.py::test_ignore_warnings_in_start,tests/test_warnings.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import contextlib import datetime import sys import types import warnings from freezegun import freeze_time class ModuleWithWarning(object): """""" A module that triggers warnings on attribute access. This does not happen with regular modules, there has to be a bit of lazy module magic going on in order for this to happen. Examples of modules that uses this pattern in real projects can be found at: py.code - the compiler package import causes a warning to be emitted: https://github.com/pytest-dev/py/blob/67987e26aadddbbe7d1ec76c16ea9be346ae9811/py/__init__.py https://github.com/pytest-dev/py/blob/67987e26aadddbbe7d1ec76c16ea9be346ae9811/py/_code/_assertionold.py#L3 celery.task - the sets module is listed in __all__ in celery.task and freeze_time accesses it: https://github.com/celery/celery/blob/46c92025cdec07a4a30ad44901cf66cb27346638/celery/task/__init__.py https://github.com/celery/celery/blob/46c92025cdec07a4a30ad44901cf66cb27346638/celery/task/sets.py """""" __name__ = 'module_with_warning' __dict__ = {} warning_triggered = False counter = 0 @property def attribute_that_emits_a_warning(self): # Use unique warning messages to avoid messages being only reported once self.__class__.counter += 1 warnings.warn('this is test warning #{counter}'.format(counter=self.__class__.counter)) self.warning_triggered = True @contextlib.contextmanager def assert_module_with_emitted_warning(): """"""Install a module that triggers warnings into sys.modules and ensure the warning was triggered in the with-block. """""" module = sys.modules['module_with_warning'] = ModuleWithWarning() try: yield finally: del sys.modules['module_with_warning'] assert module.warning_triggered @contextlib.contextmanager def assert_no_warnings(): """"""A context manager that makes sure no warnings was emitted."""""" with warnings.catch_warnings(record=True) as caught_warnings: warnings.filterwarnings('always') yield assert not caught_warnings def test_ignore_warnings_in_start(): """"""Make sure that modules being introspected in start() does not emit warnings."""""" with assert_module_with_emitted_warning(): freezer = freeze_time(datetime.datetime(2016, 10, 27, 9, 56)) try: with assert_no_warnings(): freezer.start() finally: freezer.stop() def test_ignore_warnings_in_stop(): """"""Make sure that modules that was loaded after start() does not trigger warnings in stop()"""""" freezer = freeze_time(datetime.datetime(2016, 10, 27, 9, 56)) freezer.start() with assert_module_with_emitted_warning(): with assert_no_warnings(): freezer.stop() ", https://github.com/spulec/freezegun,b46da782a7a051081fd51577749cfc0074db0cc6,tests/test_warnings.py::test_ignore_warnings_in_stop,tests/test_warnings.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import contextlib import datetime import sys import types import warnings from freezegun import freeze_time class ModuleWithWarning(object): """""" A module that triggers warnings on attribute access. This does not happen with regular modules, there has to be a bit of lazy module magic going on in order for this to happen. Examples of modules that uses this pattern in real projects can be found at: py.code - the compiler package import causes a warning to be emitted: https://github.com/pytest-dev/py/blob/67987e26aadddbbe7d1ec76c16ea9be346ae9811/py/__init__.py https://github.com/pytest-dev/py/blob/67987e26aadddbbe7d1ec76c16ea9be346ae9811/py/_code/_assertionold.py#L3 celery.task - the sets module is listed in __all__ in celery.task and freeze_time accesses it: https://github.com/celery/celery/blob/46c92025cdec07a4a30ad44901cf66cb27346638/celery/task/__init__.py https://github.com/celery/celery/blob/46c92025cdec07a4a30ad44901cf66cb27346638/celery/task/sets.py """""" __name__ = 'module_with_warning' __dict__ = {} warning_triggered = False counter = 0 @property def attribute_that_emits_a_warning(self): # Use unique warning messages to avoid messages being only reported once self.__class__.counter += 1 warnings.warn('this is test warning #{counter}'.format(counter=self.__class__.counter)) self.warning_triggered = True @contextlib.contextmanager def assert_module_with_emitted_warning(): """"""Install a module that triggers warnings into sys.modules and ensure the warning was triggered in the with-block. """""" module = sys.modules['module_with_warning'] = ModuleWithWarning() try: yield finally: del sys.modules['module_with_warning'] assert module.warning_triggered @contextlib.contextmanager def assert_no_warnings(): """"""A context manager that makes sure no warnings was emitted."""""" with warnings.catch_warnings(record=True) as caught_warnings: warnings.filterwarnings('always') yield assert not caught_warnings def test_ignore_warnings_in_start(): """"""Make sure that modules being introspected in start() does not emit warnings."""""" with assert_module_with_emitted_warning(): freezer = freeze_time(datetime.datetime(2016, 10, 27, 9, 56)) try: with assert_no_warnings(): freezer.start() finally: freezer.stop() def test_ignore_warnings_in_stop(): """"""Make sure that modules that was loaded after start() does not trigger warnings in stop()"""""" freezer = freeze_time(datetime.datetime(2016, 10, 27, 9, 56)) freezer.start() with assert_module_with_emitted_warning(): with assert_no_warnings(): freezer.stop() ", https://github.com/sputt/qer,c77e6777967ee660447beb9522c867a7233ef78d,tests/repos/test_pypi.py::test_no_candidates,tests/repos/test_pypi.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import pkg_resources import requests import responses import pytest from req_compile.repos.repository import Candidate, WheelVersionTags, DistributionType import req_compile.repos.pypi from req_compile.repos.pypi import PyPIRepository, check_python_compatibility INDEX_URL = ""https://pypi.org"" @pytest.fixture def mocked_responses(): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: yield rsps @pytest.fixture def read_contents(): def _do_read(resource): with open(os.path.join(os.path.dirname(__file__), resource)) as handle: return handle.read() return _do_read def test_successful_numpy(mocked_responses, tmpdir, read_contents): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) # The total is the total number of links - exe links, which we do not support assert len(candidates) == 1127 - 34 assert len(mocked_responses.calls) == 1 def test_no_candidates(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/garbage/"", status=404) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""garbage"")) assert candidates == [] assert len(mocked_responses.calls) == 1 def test_pypi_500(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/numpy/"", status=500) repo = PyPIRepository(INDEX_URL, wheeldir) with pytest.raises(requests.HTTPError): repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) def test_resolve_new_numpy(mocked_responses, tmpdir, read_contents, mocker): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) for candidate in candidates: if ""1.16.3"" in candidate.link[1]: mocked_responses.add( responses.GET, candidate.link[1], body=read_contents(""numpy.whl-contents""), status=200, ) mock_extract = mocker.MagicMock() mock_extract.return_value.name = ""numpy"" mocker.patch(""req_compile.repos.pypi.extract_metadata"", mock_extract) candidate, cached = repo.get_candidate(pkg_resources.Requirement.parse(""numpy"")) assert candidate is not None assert not cached listing = tmpdir.listdir() assert len(listing) == 1 assert ""1.16.3"" in str(listing[0]) assert "".whl"" in str(listing[0]) # Query the index, and download assert len(mocked_responses.calls) == 2 @pytest.mark.parametrize( ""url_to_check"", [ ""https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3.zip#sha256=HASH"", ], ) def test_python_requires_wheel_tags( mocked_responses, tmpdir, mock_py_version, read_contents, url_to_check ): mock_py_version(""3.7.12"") wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidate = [ candidate for candidate in repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) if candidate.link[1] == url_to_check ][0] if candidate.py_version: assert candidate.py_version.check_compatibility() @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.7.15"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.6.4"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.5.0"", ""==3.5""), (""3.2.17"", "">=2.7, ==3.*""), (""3.5.4"", ""~=3.5""), (""3.7.4"", ""~=3""), (""2.7.12"", ""2.7""), ], ) def test_requires_python_compatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert check_python_compatibility(py_requires) @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.6.2"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.2.17"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.2.17"", "">=2.7, !=3.*""), (""4.1.0"", ""~=3.5""), (""2.7.2"", ""~=3.3""), (""2.7.2"", "">=3.3,<4""), (""3.7.4"", ""2.7""), ], ) def test_requires_python_incompatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert not check_python_compatibility(py_requires) def test_links_parser_wheel(): filename = ""pytest-4.3.0-py2.py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""pytest"", filename, pkg_resources.parse_version(""4.3.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_py3_wheel(): filename = ""PyVISA-1.9.1-py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA"", filename, pkg_resources.parse_version(""1.9.1""), WheelVersionTags((""py3"",)), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_parser_tar_gz_hyph(): filename = ""PyVISA-py-0.3.1.tar.gz"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA-py"", filename, pkg_resources.parse_version(""0.3.1""), None, None, ""any"", (url, filename), DistributionType.SDIST, ) ] def test_tar_gz_dot(): filename = ""backports.html-1.1.0.tar.gz"" candidate = req_compile.repos.repository._tar_gz_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), None, None, ""any"", ""test"", DistributionType.SDIST, ) def test_wheel_dot(): filename = ""backports.html-1.1.0-py2.py3-none-any.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", ""test"", DistributionType.WHEEL, ) def test_wheel_platform_specific_tags(): filename = ""pywin32-224-cp27-cp27m-win_amd64.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""pywin32"", filename, pkg_resources.parse_version(""224""), WheelVersionTags((""cp27"",)), ""cp27m"", ""win_amd64"", ""test"", DistributionType.WHEEL, ) ", https://github.com/sputt/qer,c77e6777967ee660447beb9522c867a7233ef78d,tests/repos/test_pypi.py::test_pypi_500,tests/repos/test_pypi.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import pkg_resources import requests import responses import pytest from req_compile.repos.repository import Candidate, WheelVersionTags, DistributionType import req_compile.repos.pypi from req_compile.repos.pypi import PyPIRepository, check_python_compatibility INDEX_URL = ""https://pypi.org"" @pytest.fixture def mocked_responses(): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: yield rsps @pytest.fixture def read_contents(): def _do_read(resource): with open(os.path.join(os.path.dirname(__file__), resource)) as handle: return handle.read() return _do_read def test_successful_numpy(mocked_responses, tmpdir, read_contents): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) # The total is the total number of links - exe links, which we do not support assert len(candidates) == 1127 - 34 assert len(mocked_responses.calls) == 1 def test_no_candidates(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/garbage/"", status=404) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""garbage"")) assert candidates == [] assert len(mocked_responses.calls) == 1 def test_pypi_500(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/numpy/"", status=500) repo = PyPIRepository(INDEX_URL, wheeldir) with pytest.raises(requests.HTTPError): repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) def test_resolve_new_numpy(mocked_responses, tmpdir, read_contents, mocker): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) for candidate in candidates: if ""1.16.3"" in candidate.link[1]: mocked_responses.add( responses.GET, candidate.link[1], body=read_contents(""numpy.whl-contents""), status=200, ) mock_extract = mocker.MagicMock() mock_extract.return_value.name = ""numpy"" mocker.patch(""req_compile.repos.pypi.extract_metadata"", mock_extract) candidate, cached = repo.get_candidate(pkg_resources.Requirement.parse(""numpy"")) assert candidate is not None assert not cached listing = tmpdir.listdir() assert len(listing) == 1 assert ""1.16.3"" in str(listing[0]) assert "".whl"" in str(listing[0]) # Query the index, and download assert len(mocked_responses.calls) == 2 @pytest.mark.parametrize( ""url_to_check"", [ ""https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3.zip#sha256=HASH"", ], ) def test_python_requires_wheel_tags( mocked_responses, tmpdir, mock_py_version, read_contents, url_to_check ): mock_py_version(""3.7.12"") wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidate = [ candidate for candidate in repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) if candidate.link[1] == url_to_check ][0] if candidate.py_version: assert candidate.py_version.check_compatibility() @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.7.15"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.6.4"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.5.0"", ""==3.5""), (""3.2.17"", "">=2.7, ==3.*""), (""3.5.4"", ""~=3.5""), (""3.7.4"", ""~=3""), (""2.7.12"", ""2.7""), ], ) def test_requires_python_compatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert check_python_compatibility(py_requires) @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.6.2"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.2.17"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.2.17"", "">=2.7, !=3.*""), (""4.1.0"", ""~=3.5""), (""2.7.2"", ""~=3.3""), (""2.7.2"", "">=3.3,<4""), (""3.7.4"", ""2.7""), ], ) def test_requires_python_incompatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert not check_python_compatibility(py_requires) def test_links_parser_wheel(): filename = ""pytest-4.3.0-py2.py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""pytest"", filename, pkg_resources.parse_version(""4.3.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_py3_wheel(): filename = ""PyVISA-1.9.1-py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA"", filename, pkg_resources.parse_version(""1.9.1""), WheelVersionTags((""py3"",)), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_parser_tar_gz_hyph(): filename = ""PyVISA-py-0.3.1.tar.gz"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA-py"", filename, pkg_resources.parse_version(""0.3.1""), None, None, ""any"", (url, filename), DistributionType.SDIST, ) ] def test_tar_gz_dot(): filename = ""backports.html-1.1.0.tar.gz"" candidate = req_compile.repos.repository._tar_gz_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), None, None, ""any"", ""test"", DistributionType.SDIST, ) def test_wheel_dot(): filename = ""backports.html-1.1.0-py2.py3-none-any.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", ""test"", DistributionType.WHEEL, ) def test_wheel_platform_specific_tags(): filename = ""pywin32-224-cp27-cp27m-win_amd64.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""pywin32"", filename, pkg_resources.parse_version(""224""), WheelVersionTags((""cp27"",)), ""cp27m"", ""win_amd64"", ""test"", DistributionType.WHEEL, ) ", https://github.com/sputt/qer,c77e6777967ee660447beb9522c867a7233ef78d,tests/repos/test_pypi.py::test_python_requires_wheel_tags[https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH],tests/repos/test_pypi.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import pkg_resources import requests import responses import pytest from req_compile.repos.repository import Candidate, WheelVersionTags, DistributionType import req_compile.repos.pypi from req_compile.repos.pypi import PyPIRepository, check_python_compatibility INDEX_URL = ""https://pypi.org"" @pytest.fixture def mocked_responses(): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: yield rsps @pytest.fixture def read_contents(): def _do_read(resource): with open(os.path.join(os.path.dirname(__file__), resource)) as handle: return handle.read() return _do_read def test_successful_numpy(mocked_responses, tmpdir, read_contents): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) # The total is the total number of links - exe links, which we do not support assert len(candidates) == 1127 - 34 assert len(mocked_responses.calls) == 1 def test_no_candidates(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/garbage/"", status=404) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""garbage"")) assert candidates == [] assert len(mocked_responses.calls) == 1 def test_pypi_500(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/numpy/"", status=500) repo = PyPIRepository(INDEX_URL, wheeldir) with pytest.raises(requests.HTTPError): repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) def test_resolve_new_numpy(mocked_responses, tmpdir, read_contents, mocker): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) for candidate in candidates: if ""1.16.3"" in candidate.link[1]: mocked_responses.add( responses.GET, candidate.link[1], body=read_contents(""numpy.whl-contents""), status=200, ) mock_extract = mocker.MagicMock() mock_extract.return_value.name = ""numpy"" mocker.patch(""req_compile.repos.pypi.extract_metadata"", mock_extract) candidate, cached = repo.get_candidate(pkg_resources.Requirement.parse(""numpy"")) assert candidate is not None assert not cached listing = tmpdir.listdir() assert len(listing) == 1 assert ""1.16.3"" in str(listing[0]) assert "".whl"" in str(listing[0]) # Query the index, and download assert len(mocked_responses.calls) == 2 @pytest.mark.parametrize( ""url_to_check"", [ ""https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3.zip#sha256=HASH"", ], ) def test_python_requires_wheel_tags( mocked_responses, tmpdir, mock_py_version, read_contents, url_to_check ): mock_py_version(""3.7.12"") wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidate = [ candidate for candidate in repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) if candidate.link[1] == url_to_check ][0] if candidate.py_version: assert candidate.py_version.check_compatibility() @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.7.15"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.6.4"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.5.0"", ""==3.5""), (""3.2.17"", "">=2.7, ==3.*""), (""3.5.4"", ""~=3.5""), (""3.7.4"", ""~=3""), (""2.7.12"", ""2.7""), ], ) def test_requires_python_compatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert check_python_compatibility(py_requires) @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.6.2"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.2.17"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.2.17"", "">=2.7, !=3.*""), (""4.1.0"", ""~=3.5""), (""2.7.2"", ""~=3.3""), (""2.7.2"", "">=3.3,<4""), (""3.7.4"", ""2.7""), ], ) def test_requires_python_incompatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert not check_python_compatibility(py_requires) def test_links_parser_wheel(): filename = ""pytest-4.3.0-py2.py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""pytest"", filename, pkg_resources.parse_version(""4.3.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_py3_wheel(): filename = ""PyVISA-1.9.1-py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA"", filename, pkg_resources.parse_version(""1.9.1""), WheelVersionTags((""py3"",)), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_parser_tar_gz_hyph(): filename = ""PyVISA-py-0.3.1.tar.gz"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA-py"", filename, pkg_resources.parse_version(""0.3.1""), None, None, ""any"", (url, filename), DistributionType.SDIST, ) ] def test_tar_gz_dot(): filename = ""backports.html-1.1.0.tar.gz"" candidate = req_compile.repos.repository._tar_gz_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), None, None, ""any"", ""test"", DistributionType.SDIST, ) def test_wheel_dot(): filename = ""backports.html-1.1.0-py2.py3-none-any.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", ""test"", DistributionType.WHEEL, ) def test_wheel_platform_specific_tags(): filename = ""pywin32-224-cp27-cp27m-win_amd64.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""pywin32"", filename, pkg_resources.parse_version(""224""), WheelVersionTags((""cp27"",)), ""cp27m"", ""win_amd64"", ""test"", DistributionType.WHEEL, ) ", https://github.com/sputt/qer,c77e6777967ee660447beb9522c867a7233ef78d,tests/repos/test_pypi.py::test_python_requires_wheel_tags[https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH],tests/repos/test_pypi.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import pkg_resources import requests import responses import pytest from req_compile.repos.repository import Candidate, WheelVersionTags, DistributionType import req_compile.repos.pypi from req_compile.repos.pypi import PyPIRepository, check_python_compatibility INDEX_URL = ""https://pypi.org"" @pytest.fixture def mocked_responses(): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: yield rsps @pytest.fixture def read_contents(): def _do_read(resource): with open(os.path.join(os.path.dirname(__file__), resource)) as handle: return handle.read() return _do_read def test_successful_numpy(mocked_responses, tmpdir, read_contents): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) # The total is the total number of links - exe links, which we do not support assert len(candidates) == 1127 - 34 assert len(mocked_responses.calls) == 1 def test_no_candidates(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/garbage/"", status=404) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""garbage"")) assert candidates == [] assert len(mocked_responses.calls) == 1 def test_pypi_500(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/numpy/"", status=500) repo = PyPIRepository(INDEX_URL, wheeldir) with pytest.raises(requests.HTTPError): repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) def test_resolve_new_numpy(mocked_responses, tmpdir, read_contents, mocker): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) for candidate in candidates: if ""1.16.3"" in candidate.link[1]: mocked_responses.add( responses.GET, candidate.link[1], body=read_contents(""numpy.whl-contents""), status=200, ) mock_extract = mocker.MagicMock() mock_extract.return_value.name = ""numpy"" mocker.patch(""req_compile.repos.pypi.extract_metadata"", mock_extract) candidate, cached = repo.get_candidate(pkg_resources.Requirement.parse(""numpy"")) assert candidate is not None assert not cached listing = tmpdir.listdir() assert len(listing) == 1 assert ""1.16.3"" in str(listing[0]) assert "".whl"" in str(listing[0]) # Query the index, and download assert len(mocked_responses.calls) == 2 @pytest.mark.parametrize( ""url_to_check"", [ ""https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3.zip#sha256=HASH"", ], ) def test_python_requires_wheel_tags( mocked_responses, tmpdir, mock_py_version, read_contents, url_to_check ): mock_py_version(""3.7.12"") wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidate = [ candidate for candidate in repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) if candidate.link[1] == url_to_check ][0] if candidate.py_version: assert candidate.py_version.check_compatibility() @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.7.15"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.6.4"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.5.0"", ""==3.5""), (""3.2.17"", "">=2.7, ==3.*""), (""3.5.4"", ""~=3.5""), (""3.7.4"", ""~=3""), (""2.7.12"", ""2.7""), ], ) def test_requires_python_compatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert check_python_compatibility(py_requires) @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.6.2"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.2.17"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.2.17"", "">=2.7, !=3.*""), (""4.1.0"", ""~=3.5""), (""2.7.2"", ""~=3.3""), (""2.7.2"", "">=3.3,<4""), (""3.7.4"", ""2.7""), ], ) def test_requires_python_incompatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert not check_python_compatibility(py_requires) def test_links_parser_wheel(): filename = ""pytest-4.3.0-py2.py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""pytest"", filename, pkg_resources.parse_version(""4.3.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_py3_wheel(): filename = ""PyVISA-1.9.1-py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA"", filename, pkg_resources.parse_version(""1.9.1""), WheelVersionTags((""py3"",)), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_parser_tar_gz_hyph(): filename = ""PyVISA-py-0.3.1.tar.gz"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA-py"", filename, pkg_resources.parse_version(""0.3.1""), None, None, ""any"", (url, filename), DistributionType.SDIST, ) ] def test_tar_gz_dot(): filename = ""backports.html-1.1.0.tar.gz"" candidate = req_compile.repos.repository._tar_gz_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), None, None, ""any"", ""test"", DistributionType.SDIST, ) def test_wheel_dot(): filename = ""backports.html-1.1.0-py2.py3-none-any.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", ""test"", DistributionType.WHEEL, ) def test_wheel_platform_specific_tags(): filename = ""pywin32-224-cp27-cp27m-win_amd64.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""pywin32"", filename, pkg_resources.parse_version(""224""), WheelVersionTags((""cp27"",)), ""cp27m"", ""win_amd64"", ""test"", DistributionType.WHEEL, ) ", https://github.com/sputt/qer,c77e6777967ee660447beb9522c867a7233ef78d,tests/repos/test_pypi.py::test_python_requires_wheel_tags[https://pypi.org/numpy-1.16.3.zip#sha256=HASH],tests/repos/test_pypi.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import pkg_resources import requests import responses import pytest from req_compile.repos.repository import Candidate, WheelVersionTags, DistributionType import req_compile.repos.pypi from req_compile.repos.pypi import PyPIRepository, check_python_compatibility INDEX_URL = ""https://pypi.org"" @pytest.fixture def mocked_responses(): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: yield rsps @pytest.fixture def read_contents(): def _do_read(resource): with open(os.path.join(os.path.dirname(__file__), resource)) as handle: return handle.read() return _do_read def test_successful_numpy(mocked_responses, tmpdir, read_contents): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) # The total is the total number of links - exe links, which we do not support assert len(candidates) == 1127 - 34 assert len(mocked_responses.calls) == 1 def test_no_candidates(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/garbage/"", status=404) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""garbage"")) assert candidates == [] assert len(mocked_responses.calls) == 1 def test_pypi_500(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/numpy/"", status=500) repo = PyPIRepository(INDEX_URL, wheeldir) with pytest.raises(requests.HTTPError): repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) def test_resolve_new_numpy(mocked_responses, tmpdir, read_contents, mocker): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) for candidate in candidates: if ""1.16.3"" in candidate.link[1]: mocked_responses.add( responses.GET, candidate.link[1], body=read_contents(""numpy.whl-contents""), status=200, ) mock_extract = mocker.MagicMock() mock_extract.return_value.name = ""numpy"" mocker.patch(""req_compile.repos.pypi.extract_metadata"", mock_extract) candidate, cached = repo.get_candidate(pkg_resources.Requirement.parse(""numpy"")) assert candidate is not None assert not cached listing = tmpdir.listdir() assert len(listing) == 1 assert ""1.16.3"" in str(listing[0]) assert "".whl"" in str(listing[0]) # Query the index, and download assert len(mocked_responses.calls) == 2 @pytest.mark.parametrize( ""url_to_check"", [ ""https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3.zip#sha256=HASH"", ], ) def test_python_requires_wheel_tags( mocked_responses, tmpdir, mock_py_version, read_contents, url_to_check ): mock_py_version(""3.7.12"") wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidate = [ candidate for candidate in repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) if candidate.link[1] == url_to_check ][0] if candidate.py_version: assert candidate.py_version.check_compatibility() @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.7.15"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.6.4"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.5.0"", ""==3.5""), (""3.2.17"", "">=2.7, ==3.*""), (""3.5.4"", ""~=3.5""), (""3.7.4"", ""~=3""), (""2.7.12"", ""2.7""), ], ) def test_requires_python_compatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert check_python_compatibility(py_requires) @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.6.2"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.2.17"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.2.17"", "">=2.7, !=3.*""), (""4.1.0"", ""~=3.5""), (""2.7.2"", ""~=3.3""), (""2.7.2"", "">=3.3,<4""), (""3.7.4"", ""2.7""), ], ) def test_requires_python_incompatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert not check_python_compatibility(py_requires) def test_links_parser_wheel(): filename = ""pytest-4.3.0-py2.py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""pytest"", filename, pkg_resources.parse_version(""4.3.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_py3_wheel(): filename = ""PyVISA-1.9.1-py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA"", filename, pkg_resources.parse_version(""1.9.1""), WheelVersionTags((""py3"",)), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_parser_tar_gz_hyph(): filename = ""PyVISA-py-0.3.1.tar.gz"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA-py"", filename, pkg_resources.parse_version(""0.3.1""), None, None, ""any"", (url, filename), DistributionType.SDIST, ) ] def test_tar_gz_dot(): filename = ""backports.html-1.1.0.tar.gz"" candidate = req_compile.repos.repository._tar_gz_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), None, None, ""any"", ""test"", DistributionType.SDIST, ) def test_wheel_dot(): filename = ""backports.html-1.1.0-py2.py3-none-any.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", ""test"", DistributionType.WHEEL, ) def test_wheel_platform_specific_tags(): filename = ""pywin32-224-cp27-cp27m-win_amd64.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""pywin32"", filename, pkg_resources.parse_version(""224""), WheelVersionTags((""cp27"",)), ""cp27m"", ""win_amd64"", ""test"", DistributionType.WHEEL, ) ", https://github.com/sputt/qer,c77e6777967ee660447beb9522c867a7233ef78d,tests/repos/test_pypi.py::test_successful_numpy,tests/repos/test_pypi.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import pkg_resources import requests import responses import pytest from req_compile.repos.repository import Candidate, WheelVersionTags, DistributionType import req_compile.repos.pypi from req_compile.repos.pypi import PyPIRepository, check_python_compatibility INDEX_URL = ""https://pypi.org"" @pytest.fixture def mocked_responses(): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: yield rsps @pytest.fixture def read_contents(): def _do_read(resource): with open(os.path.join(os.path.dirname(__file__), resource)) as handle: return handle.read() return _do_read def test_successful_numpy(mocked_responses, tmpdir, read_contents): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) # The total is the total number of links - exe links, which we do not support assert len(candidates) == 1127 - 34 assert len(mocked_responses.calls) == 1 def test_no_candidates(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/garbage/"", status=404) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""garbage"")) assert candidates == [] assert len(mocked_responses.calls) == 1 def test_pypi_500(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/numpy/"", status=500) repo = PyPIRepository(INDEX_URL, wheeldir) with pytest.raises(requests.HTTPError): repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) def test_resolve_new_numpy(mocked_responses, tmpdir, read_contents, mocker): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) for candidate in candidates: if ""1.16.3"" in candidate.link[1]: mocked_responses.add( responses.GET, candidate.link[1], body=read_contents(""numpy.whl-contents""), status=200, ) mock_extract = mocker.MagicMock() mock_extract.return_value.name = ""numpy"" mocker.patch(""req_compile.repos.pypi.extract_metadata"", mock_extract) candidate, cached = repo.get_candidate(pkg_resources.Requirement.parse(""numpy"")) assert candidate is not None assert not cached listing = tmpdir.listdir() assert len(listing) == 1 assert ""1.16.3"" in str(listing[0]) assert "".whl"" in str(listing[0]) # Query the index, and download assert len(mocked_responses.calls) == 2 @pytest.mark.parametrize( ""url_to_check"", [ ""https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3.zip#sha256=HASH"", ], ) def test_python_requires_wheel_tags( mocked_responses, tmpdir, mock_py_version, read_contents, url_to_check ): mock_py_version(""3.7.12"") wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidate = [ candidate for candidate in repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) if candidate.link[1] == url_to_check ][0] if candidate.py_version: assert candidate.py_version.check_compatibility() @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.7.15"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.6.4"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.5.0"", ""==3.5""), (""3.2.17"", "">=2.7, ==3.*""), (""3.5.4"", ""~=3.5""), (""3.7.4"", ""~=3""), (""2.7.12"", ""2.7""), ], ) def test_requires_python_compatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert check_python_compatibility(py_requires) @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.6.2"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.2.17"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.2.17"", "">=2.7, !=3.*""), (""4.1.0"", ""~=3.5""), (""2.7.2"", ""~=3.3""), (""2.7.2"", "">=3.3,<4""), (""3.7.4"", ""2.7""), ], ) def test_requires_python_incompatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert not check_python_compatibility(py_requires) def test_links_parser_wheel(): filename = ""pytest-4.3.0-py2.py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""pytest"", filename, pkg_resources.parse_version(""4.3.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_py3_wheel(): filename = ""PyVISA-1.9.1-py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA"", filename, pkg_resources.parse_version(""1.9.1""), WheelVersionTags((""py3"",)), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_parser_tar_gz_hyph(): filename = ""PyVISA-py-0.3.1.tar.gz"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA-py"", filename, pkg_resources.parse_version(""0.3.1""), None, None, ""any"", (url, filename), DistributionType.SDIST, ) ] def test_tar_gz_dot(): filename = ""backports.html-1.1.0.tar.gz"" candidate = req_compile.repos.repository._tar_gz_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), None, None, ""any"", ""test"", DistributionType.SDIST, ) def test_wheel_dot(): filename = ""backports.html-1.1.0-py2.py3-none-any.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", ""test"", DistributionType.WHEEL, ) def test_wheel_platform_specific_tags(): filename = ""pywin32-224-cp27-cp27m-win_amd64.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""pywin32"", filename, pkg_resources.parse_version(""224""), WheelVersionTags((""cp27"",)), ""cp27m"", ""win_amd64"", ""test"", DistributionType.WHEEL, ) ", https://github.com/sputt/qer,c77e6777967ee660447beb9522c867a7233ef78d,tests/test_extractor.py::test_extractor[mock_targz],tests/test_extractor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import contextlib import logging import os import pytest import req_compile.metadata.extractor @contextlib.contextmanager def temp_cwd(new_cwd): old_cwd = os.getcwd() try: os.chdir(new_cwd) yield new_cwd finally: os.chdir(old_cwd) @pytest.mark.parametrize(""archive_fixture"", [""mock_targz"", ""mock_zip"", ""mock_zip""]) def test_extractor(archive_fixture, tmpdir, mock_targz, mock_zip): directory = ""comtypes-1.1.7"" if archive_fixture == ""mock_targz"": archive = mock_targz(directory) elif archive_fixture == ""mock_zip"": archive = mock_zip(directory) else: archive = os.path.abspath(os.path.join(""source-packages"", directory)) if archive_fixture == ""mock_targz"": extractor = req_compile.metadata.extractor.TarExtractor(""gz"", archive) prefix = directory + ""/"" elif archive_fixture == ""mock_zip"": extractor = req_compile.metadata.extractor.ZipExtractor(archive) prefix = directory + ""/"" else: extractor = req_compile.metadata.extractor.NonExtractor(archive) prefix = """" with contextlib.closing(extractor): all_names = set(extractor.names()) assert all_names == { prefix + ""README"", prefix + ""setup.py"", prefix + ""comtypes/__init__.py"", prefix + ""test/setup.py"", } prefix = extractor.fake_root + os.sep + prefix assert extractor.contents(prefix + ""README"") == ""README CONTENTS"" with extractor.open(prefix + ""README"") as handle: assert handle.read(2) == ""RE"" assert extractor.contents(prefix + ""README"") == ""README CONTENTS"" assert extractor.exists(prefix + ""test"") assert extractor.exists(prefix + ""test/setup.py"") assert not extractor.exists(prefix + ""test/setup2.py"") ", https://github.com/sputt/qer,c77e6777967ee660447beb9522c867a7233ef78d,tests/test_extractor.py::test_extractor[mock_zip0],tests/test_extractor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import contextlib import logging import os import pytest import req_compile.metadata.extractor @contextlib.contextmanager def temp_cwd(new_cwd): old_cwd = os.getcwd() try: os.chdir(new_cwd) yield new_cwd finally: os.chdir(old_cwd) @pytest.mark.parametrize(""archive_fixture"", [""mock_targz"", ""mock_zip"", ""mock_zip""]) def test_extractor(archive_fixture, tmpdir, mock_targz, mock_zip): directory = ""comtypes-1.1.7"" if archive_fixture == ""mock_targz"": archive = mock_targz(directory) elif archive_fixture == ""mock_zip"": archive = mock_zip(directory) else: archive = os.path.abspath(os.path.join(""source-packages"", directory)) if archive_fixture == ""mock_targz"": extractor = req_compile.metadata.extractor.TarExtractor(""gz"", archive) prefix = directory + ""/"" elif archive_fixture == ""mock_zip"": extractor = req_compile.metadata.extractor.ZipExtractor(archive) prefix = directory + ""/"" else: extractor = req_compile.metadata.extractor.NonExtractor(archive) prefix = """" with contextlib.closing(extractor): all_names = set(extractor.names()) assert all_names == { prefix + ""README"", prefix + ""setup.py"", prefix + ""comtypes/__init__.py"", prefix + ""test/setup.py"", } prefix = extractor.fake_root + os.sep + prefix assert extractor.contents(prefix + ""README"") == ""README CONTENTS"" with extractor.open(prefix + ""README"") as handle: assert handle.read(2) == ""RE"" assert extractor.contents(prefix + ""README"") == ""README CONTENTS"" assert extractor.exists(prefix + ""test"") assert extractor.exists(prefix + ""test/setup.py"") assert not extractor.exists(prefix + ""test/setup2.py"") ", https://github.com/sputt/qer,c77e6777967ee660447beb9522c867a7233ef78d,tests/test_extractor.py::test_extractor[mock_zip1],tests/test_extractor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import contextlib import logging import os import pytest import req_compile.metadata.extractor @contextlib.contextmanager def temp_cwd(new_cwd): old_cwd = os.getcwd() try: os.chdir(new_cwd) yield new_cwd finally: os.chdir(old_cwd) @pytest.mark.parametrize(""archive_fixture"", [""mock_targz"", ""mock_zip"", ""mock_zip""]) def test_extractor(archive_fixture, tmpdir, mock_targz, mock_zip): directory = ""comtypes-1.1.7"" if archive_fixture == ""mock_targz"": archive = mock_targz(directory) elif archive_fixture == ""mock_zip"": archive = mock_zip(directory) else: archive = os.path.abspath(os.path.join(""source-packages"", directory)) if archive_fixture == ""mock_targz"": extractor = req_compile.metadata.extractor.TarExtractor(""gz"", archive) prefix = directory + ""/"" elif archive_fixture == ""mock_zip"": extractor = req_compile.metadata.extractor.ZipExtractor(archive) prefix = directory + ""/"" else: extractor = req_compile.metadata.extractor.NonExtractor(archive) prefix = """" with contextlib.closing(extractor): all_names = set(extractor.names()) assert all_names == { prefix + ""README"", prefix + ""setup.py"", prefix + ""comtypes/__init__.py"", prefix + ""test/setup.py"", } prefix = extractor.fake_root + os.sep + prefix assert extractor.contents(prefix + ""README"") == ""README CONTENTS"" with extractor.open(prefix + ""README"") as handle: assert handle.read(2) == ""RE"" assert extractor.contents(prefix + ""README"") == ""README CONTENTS"" assert extractor.exists(prefix + ""test"") assert extractor.exists(prefix + ""test/setup.py"") assert not extractor.exists(prefix + ""test/setup2.py"") ", https://github.com/sputt/req-compile,c77e6777967ee660447beb9522c867a7233ef78d,tests/repos/test_pypi.py::test_no_candidates,tests/repos/test_pypi.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import pkg_resources import requests import responses import pytest from req_compile.repos.repository import Candidate, WheelVersionTags, DistributionType import req_compile.repos.pypi from req_compile.repos.pypi import PyPIRepository, check_python_compatibility INDEX_URL = ""https://pypi.org"" @pytest.fixture def mocked_responses(): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: yield rsps @pytest.fixture def read_contents(): def _do_read(resource): with open(os.path.join(os.path.dirname(__file__), resource)) as handle: return handle.read() return _do_read def test_successful_numpy(mocked_responses, tmpdir, read_contents): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) # The total is the total number of links - exe links, which we do not support assert len(candidates) == 1127 - 34 assert len(mocked_responses.calls) == 1 def test_no_candidates(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/garbage/"", status=404) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""garbage"")) assert candidates == [] assert len(mocked_responses.calls) == 1 def test_pypi_500(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/numpy/"", status=500) repo = PyPIRepository(INDEX_URL, wheeldir) with pytest.raises(requests.HTTPError): repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) def test_resolve_new_numpy(mocked_responses, tmpdir, read_contents, mocker): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) for candidate in candidates: if ""1.16.3"" in candidate.link[1]: mocked_responses.add( responses.GET, candidate.link[1], body=read_contents(""numpy.whl-contents""), status=200, ) mock_extract = mocker.MagicMock() mock_extract.return_value.name = ""numpy"" mocker.patch(""req_compile.repos.pypi.extract_metadata"", mock_extract) candidate, cached = repo.get_candidate(pkg_resources.Requirement.parse(""numpy"")) assert candidate is not None assert not cached listing = tmpdir.listdir() assert len(listing) == 1 assert ""1.16.3"" in str(listing[0]) assert "".whl"" in str(listing[0]) # Query the index, and download assert len(mocked_responses.calls) == 2 @pytest.mark.parametrize( ""url_to_check"", [ ""https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3.zip#sha256=HASH"", ], ) def test_python_requires_wheel_tags( mocked_responses, tmpdir, mock_py_version, read_contents, url_to_check ): mock_py_version(""3.7.12"") wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidate = [ candidate for candidate in repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) if candidate.link[1] == url_to_check ][0] if candidate.py_version: assert candidate.py_version.check_compatibility() @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.7.15"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.6.4"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.5.0"", ""==3.5""), (""3.2.17"", "">=2.7, ==3.*""), (""3.5.4"", ""~=3.5""), (""3.7.4"", ""~=3""), (""2.7.12"", ""2.7""), ], ) def test_requires_python_compatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert check_python_compatibility(py_requires) @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.6.2"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.2.17"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.2.17"", "">=2.7, !=3.*""), (""4.1.0"", ""~=3.5""), (""2.7.2"", ""~=3.3""), (""2.7.2"", "">=3.3,<4""), (""3.7.4"", ""2.7""), ], ) def test_requires_python_incompatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert not check_python_compatibility(py_requires) def test_links_parser_wheel(): filename = ""pytest-4.3.0-py2.py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""pytest"", filename, pkg_resources.parse_version(""4.3.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_py3_wheel(): filename = ""PyVISA-1.9.1-py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA"", filename, pkg_resources.parse_version(""1.9.1""), WheelVersionTags((""py3"",)), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_parser_tar_gz_hyph(): filename = ""PyVISA-py-0.3.1.tar.gz"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA-py"", filename, pkg_resources.parse_version(""0.3.1""), None, None, ""any"", (url, filename), DistributionType.SDIST, ) ] def test_tar_gz_dot(): filename = ""backports.html-1.1.0.tar.gz"" candidate = req_compile.repos.repository._tar_gz_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), None, None, ""any"", ""test"", DistributionType.SDIST, ) def test_wheel_dot(): filename = ""backports.html-1.1.0-py2.py3-none-any.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", ""test"", DistributionType.WHEEL, ) def test_wheel_platform_specific_tags(): filename = ""pywin32-224-cp27-cp27m-win_amd64.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""pywin32"", filename, pkg_resources.parse_version(""224""), WheelVersionTags((""cp27"",)), ""cp27m"", ""win_amd64"", ""test"", DistributionType.WHEEL, ) ", https://github.com/sputt/req-compile,c77e6777967ee660447beb9522c867a7233ef78d,tests/repos/test_pypi.py::test_pypi_500,tests/repos/test_pypi.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import pkg_resources import requests import responses import pytest from req_compile.repos.repository import Candidate, WheelVersionTags, DistributionType import req_compile.repos.pypi from req_compile.repos.pypi import PyPIRepository, check_python_compatibility INDEX_URL = ""https://pypi.org"" @pytest.fixture def mocked_responses(): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: yield rsps @pytest.fixture def read_contents(): def _do_read(resource): with open(os.path.join(os.path.dirname(__file__), resource)) as handle: return handle.read() return _do_read def test_successful_numpy(mocked_responses, tmpdir, read_contents): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) # The total is the total number of links - exe links, which we do not support assert len(candidates) == 1127 - 34 assert len(mocked_responses.calls) == 1 def test_no_candidates(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/garbage/"", status=404) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""garbage"")) assert candidates == [] assert len(mocked_responses.calls) == 1 def test_pypi_500(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/numpy/"", status=500) repo = PyPIRepository(INDEX_URL, wheeldir) with pytest.raises(requests.HTTPError): repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) def test_resolve_new_numpy(mocked_responses, tmpdir, read_contents, mocker): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) for candidate in candidates: if ""1.16.3"" in candidate.link[1]: mocked_responses.add( responses.GET, candidate.link[1], body=read_contents(""numpy.whl-contents""), status=200, ) mock_extract = mocker.MagicMock() mock_extract.return_value.name = ""numpy"" mocker.patch(""req_compile.repos.pypi.extract_metadata"", mock_extract) candidate, cached = repo.get_candidate(pkg_resources.Requirement.parse(""numpy"")) assert candidate is not None assert not cached listing = tmpdir.listdir() assert len(listing) == 1 assert ""1.16.3"" in str(listing[0]) assert "".whl"" in str(listing[0]) # Query the index, and download assert len(mocked_responses.calls) == 2 @pytest.mark.parametrize( ""url_to_check"", [ ""https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3.zip#sha256=HASH"", ], ) def test_python_requires_wheel_tags( mocked_responses, tmpdir, mock_py_version, read_contents, url_to_check ): mock_py_version(""3.7.12"") wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidate = [ candidate for candidate in repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) if candidate.link[1] == url_to_check ][0] if candidate.py_version: assert candidate.py_version.check_compatibility() @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.7.15"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.6.4"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.5.0"", ""==3.5""), (""3.2.17"", "">=2.7, ==3.*""), (""3.5.4"", ""~=3.5""), (""3.7.4"", ""~=3""), (""2.7.12"", ""2.7""), ], ) def test_requires_python_compatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert check_python_compatibility(py_requires) @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.6.2"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.2.17"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.2.17"", "">=2.7, !=3.*""), (""4.1.0"", ""~=3.5""), (""2.7.2"", ""~=3.3""), (""2.7.2"", "">=3.3,<4""), (""3.7.4"", ""2.7""), ], ) def test_requires_python_incompatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert not check_python_compatibility(py_requires) def test_links_parser_wheel(): filename = ""pytest-4.3.0-py2.py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""pytest"", filename, pkg_resources.parse_version(""4.3.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_py3_wheel(): filename = ""PyVISA-1.9.1-py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA"", filename, pkg_resources.parse_version(""1.9.1""), WheelVersionTags((""py3"",)), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_parser_tar_gz_hyph(): filename = ""PyVISA-py-0.3.1.tar.gz"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA-py"", filename, pkg_resources.parse_version(""0.3.1""), None, None, ""any"", (url, filename), DistributionType.SDIST, ) ] def test_tar_gz_dot(): filename = ""backports.html-1.1.0.tar.gz"" candidate = req_compile.repos.repository._tar_gz_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), None, None, ""any"", ""test"", DistributionType.SDIST, ) def test_wheel_dot(): filename = ""backports.html-1.1.0-py2.py3-none-any.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", ""test"", DistributionType.WHEEL, ) def test_wheel_platform_specific_tags(): filename = ""pywin32-224-cp27-cp27m-win_amd64.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""pywin32"", filename, pkg_resources.parse_version(""224""), WheelVersionTags((""cp27"",)), ""cp27m"", ""win_amd64"", ""test"", DistributionType.WHEEL, ) ", https://github.com/sputt/req-compile,c77e6777967ee660447beb9522c867a7233ef78d,tests/repos/test_pypi.py::test_python_requires_wheel_tags[https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH],tests/repos/test_pypi.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import pkg_resources import requests import responses import pytest from req_compile.repos.repository import Candidate, WheelVersionTags, DistributionType import req_compile.repos.pypi from req_compile.repos.pypi import PyPIRepository, check_python_compatibility INDEX_URL = ""https://pypi.org"" @pytest.fixture def mocked_responses(): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: yield rsps @pytest.fixture def read_contents(): def _do_read(resource): with open(os.path.join(os.path.dirname(__file__), resource)) as handle: return handle.read() return _do_read def test_successful_numpy(mocked_responses, tmpdir, read_contents): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) # The total is the total number of links - exe links, which we do not support assert len(candidates) == 1127 - 34 assert len(mocked_responses.calls) == 1 def test_no_candidates(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/garbage/"", status=404) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""garbage"")) assert candidates == [] assert len(mocked_responses.calls) == 1 def test_pypi_500(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/numpy/"", status=500) repo = PyPIRepository(INDEX_URL, wheeldir) with pytest.raises(requests.HTTPError): repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) def test_resolve_new_numpy(mocked_responses, tmpdir, read_contents, mocker): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) for candidate in candidates: if ""1.16.3"" in candidate.link[1]: mocked_responses.add( responses.GET, candidate.link[1], body=read_contents(""numpy.whl-contents""), status=200, ) mock_extract = mocker.MagicMock() mock_extract.return_value.name = ""numpy"" mocker.patch(""req_compile.repos.pypi.extract_metadata"", mock_extract) candidate, cached = repo.get_candidate(pkg_resources.Requirement.parse(""numpy"")) assert candidate is not None assert not cached listing = tmpdir.listdir() assert len(listing) == 1 assert ""1.16.3"" in str(listing[0]) assert "".whl"" in str(listing[0]) # Query the index, and download assert len(mocked_responses.calls) == 2 @pytest.mark.parametrize( ""url_to_check"", [ ""https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3.zip#sha256=HASH"", ], ) def test_python_requires_wheel_tags( mocked_responses, tmpdir, mock_py_version, read_contents, url_to_check ): mock_py_version(""3.7.12"") wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidate = [ candidate for candidate in repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) if candidate.link[1] == url_to_check ][0] if candidate.py_version: assert candidate.py_version.check_compatibility() @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.7.15"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.6.4"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.5.0"", ""==3.5""), (""3.2.17"", "">=2.7, ==3.*""), (""3.5.4"", ""~=3.5""), (""3.7.4"", ""~=3""), (""2.7.12"", ""2.7""), ], ) def test_requires_python_compatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert check_python_compatibility(py_requires) @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.6.2"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.2.17"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.2.17"", "">=2.7, !=3.*""), (""4.1.0"", ""~=3.5""), (""2.7.2"", ""~=3.3""), (""2.7.2"", "">=3.3,<4""), (""3.7.4"", ""2.7""), ], ) def test_requires_python_incompatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert not check_python_compatibility(py_requires) def test_links_parser_wheel(): filename = ""pytest-4.3.0-py2.py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""pytest"", filename, pkg_resources.parse_version(""4.3.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_py3_wheel(): filename = ""PyVISA-1.9.1-py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA"", filename, pkg_resources.parse_version(""1.9.1""), WheelVersionTags((""py3"",)), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_parser_tar_gz_hyph(): filename = ""PyVISA-py-0.3.1.tar.gz"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA-py"", filename, pkg_resources.parse_version(""0.3.1""), None, None, ""any"", (url, filename), DistributionType.SDIST, ) ] def test_tar_gz_dot(): filename = ""backports.html-1.1.0.tar.gz"" candidate = req_compile.repos.repository._tar_gz_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), None, None, ""any"", ""test"", DistributionType.SDIST, ) def test_wheel_dot(): filename = ""backports.html-1.1.0-py2.py3-none-any.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", ""test"", DistributionType.WHEEL, ) def test_wheel_platform_specific_tags(): filename = ""pywin32-224-cp27-cp27m-win_amd64.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""pywin32"", filename, pkg_resources.parse_version(""224""), WheelVersionTags((""cp27"",)), ""cp27m"", ""win_amd64"", ""test"", DistributionType.WHEEL, ) ", https://github.com/sputt/req-compile,c77e6777967ee660447beb9522c867a7233ef78d,tests/repos/test_pypi.py::test_python_requires_wheel_tags[https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH],tests/repos/test_pypi.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import pkg_resources import requests import responses import pytest from req_compile.repos.repository import Candidate, WheelVersionTags, DistributionType import req_compile.repos.pypi from req_compile.repos.pypi import PyPIRepository, check_python_compatibility INDEX_URL = ""https://pypi.org"" @pytest.fixture def mocked_responses(): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: yield rsps @pytest.fixture def read_contents(): def _do_read(resource): with open(os.path.join(os.path.dirname(__file__), resource)) as handle: return handle.read() return _do_read def test_successful_numpy(mocked_responses, tmpdir, read_contents): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) # The total is the total number of links - exe links, which we do not support assert len(candidates) == 1127 - 34 assert len(mocked_responses.calls) == 1 def test_no_candidates(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/garbage/"", status=404) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""garbage"")) assert candidates == [] assert len(mocked_responses.calls) == 1 def test_pypi_500(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/numpy/"", status=500) repo = PyPIRepository(INDEX_URL, wheeldir) with pytest.raises(requests.HTTPError): repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) def test_resolve_new_numpy(mocked_responses, tmpdir, read_contents, mocker): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) for candidate in candidates: if ""1.16.3"" in candidate.link[1]: mocked_responses.add( responses.GET, candidate.link[1], body=read_contents(""numpy.whl-contents""), status=200, ) mock_extract = mocker.MagicMock() mock_extract.return_value.name = ""numpy"" mocker.patch(""req_compile.repos.pypi.extract_metadata"", mock_extract) candidate, cached = repo.get_candidate(pkg_resources.Requirement.parse(""numpy"")) assert candidate is not None assert not cached listing = tmpdir.listdir() assert len(listing) == 1 assert ""1.16.3"" in str(listing[0]) assert "".whl"" in str(listing[0]) # Query the index, and download assert len(mocked_responses.calls) == 2 @pytest.mark.parametrize( ""url_to_check"", [ ""https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3.zip#sha256=HASH"", ], ) def test_python_requires_wheel_tags( mocked_responses, tmpdir, mock_py_version, read_contents, url_to_check ): mock_py_version(""3.7.12"") wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidate = [ candidate for candidate in repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) if candidate.link[1] == url_to_check ][0] if candidate.py_version: assert candidate.py_version.check_compatibility() @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.7.15"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.6.4"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.5.0"", ""==3.5""), (""3.2.17"", "">=2.7, ==3.*""), (""3.5.4"", ""~=3.5""), (""3.7.4"", ""~=3""), (""2.7.12"", ""2.7""), ], ) def test_requires_python_compatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert check_python_compatibility(py_requires) @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.6.2"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.2.17"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.2.17"", "">=2.7, !=3.*""), (""4.1.0"", ""~=3.5""), (""2.7.2"", ""~=3.3""), (""2.7.2"", "">=3.3,<4""), (""3.7.4"", ""2.7""), ], ) def test_requires_python_incompatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert not check_python_compatibility(py_requires) def test_links_parser_wheel(): filename = ""pytest-4.3.0-py2.py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""pytest"", filename, pkg_resources.parse_version(""4.3.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_py3_wheel(): filename = ""PyVISA-1.9.1-py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA"", filename, pkg_resources.parse_version(""1.9.1""), WheelVersionTags((""py3"",)), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_parser_tar_gz_hyph(): filename = ""PyVISA-py-0.3.1.tar.gz"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA-py"", filename, pkg_resources.parse_version(""0.3.1""), None, None, ""any"", (url, filename), DistributionType.SDIST, ) ] def test_tar_gz_dot(): filename = ""backports.html-1.1.0.tar.gz"" candidate = req_compile.repos.repository._tar_gz_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), None, None, ""any"", ""test"", DistributionType.SDIST, ) def test_wheel_dot(): filename = ""backports.html-1.1.0-py2.py3-none-any.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", ""test"", DistributionType.WHEEL, ) def test_wheel_platform_specific_tags(): filename = ""pywin32-224-cp27-cp27m-win_amd64.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""pywin32"", filename, pkg_resources.parse_version(""224""), WheelVersionTags((""cp27"",)), ""cp27m"", ""win_amd64"", ""test"", DistributionType.WHEEL, ) ", https://github.com/sputt/req-compile,c77e6777967ee660447beb9522c867a7233ef78d,tests/repos/test_pypi.py::test_python_requires_wheel_tags[https://pypi.org/numpy-1.16.3.zip#sha256=HASH],tests/repos/test_pypi.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import pkg_resources import requests import responses import pytest from req_compile.repos.repository import Candidate, WheelVersionTags, DistributionType import req_compile.repos.pypi from req_compile.repos.pypi import PyPIRepository, check_python_compatibility INDEX_URL = ""https://pypi.org"" @pytest.fixture def mocked_responses(): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: yield rsps @pytest.fixture def read_contents(): def _do_read(resource): with open(os.path.join(os.path.dirname(__file__), resource)) as handle: return handle.read() return _do_read def test_successful_numpy(mocked_responses, tmpdir, read_contents): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) # The total is the total number of links - exe links, which we do not support assert len(candidates) == 1127 - 34 assert len(mocked_responses.calls) == 1 def test_no_candidates(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/garbage/"", status=404) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""garbage"")) assert candidates == [] assert len(mocked_responses.calls) == 1 def test_pypi_500(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/numpy/"", status=500) repo = PyPIRepository(INDEX_URL, wheeldir) with pytest.raises(requests.HTTPError): repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) def test_resolve_new_numpy(mocked_responses, tmpdir, read_contents, mocker): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) for candidate in candidates: if ""1.16.3"" in candidate.link[1]: mocked_responses.add( responses.GET, candidate.link[1], body=read_contents(""numpy.whl-contents""), status=200, ) mock_extract = mocker.MagicMock() mock_extract.return_value.name = ""numpy"" mocker.patch(""req_compile.repos.pypi.extract_metadata"", mock_extract) candidate, cached = repo.get_candidate(pkg_resources.Requirement.parse(""numpy"")) assert candidate is not None assert not cached listing = tmpdir.listdir() assert len(listing) == 1 assert ""1.16.3"" in str(listing[0]) assert "".whl"" in str(listing[0]) # Query the index, and download assert len(mocked_responses.calls) == 2 @pytest.mark.parametrize( ""url_to_check"", [ ""https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3.zip#sha256=HASH"", ], ) def test_python_requires_wheel_tags( mocked_responses, tmpdir, mock_py_version, read_contents, url_to_check ): mock_py_version(""3.7.12"") wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidate = [ candidate for candidate in repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) if candidate.link[1] == url_to_check ][0] if candidate.py_version: assert candidate.py_version.check_compatibility() @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.7.15"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.6.4"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.5.0"", ""==3.5""), (""3.2.17"", "">=2.7, ==3.*""), (""3.5.4"", ""~=3.5""), (""3.7.4"", ""~=3""), (""2.7.12"", ""2.7""), ], ) def test_requires_python_compatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert check_python_compatibility(py_requires) @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.6.2"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.2.17"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.2.17"", "">=2.7, !=3.*""), (""4.1.0"", ""~=3.5""), (""2.7.2"", ""~=3.3""), (""2.7.2"", "">=3.3,<4""), (""3.7.4"", ""2.7""), ], ) def test_requires_python_incompatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert not check_python_compatibility(py_requires) def test_links_parser_wheel(): filename = ""pytest-4.3.0-py2.py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""pytest"", filename, pkg_resources.parse_version(""4.3.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_py3_wheel(): filename = ""PyVISA-1.9.1-py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA"", filename, pkg_resources.parse_version(""1.9.1""), WheelVersionTags((""py3"",)), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_parser_tar_gz_hyph(): filename = ""PyVISA-py-0.3.1.tar.gz"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA-py"", filename, pkg_resources.parse_version(""0.3.1""), None, None, ""any"", (url, filename), DistributionType.SDIST, ) ] def test_tar_gz_dot(): filename = ""backports.html-1.1.0.tar.gz"" candidate = req_compile.repos.repository._tar_gz_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), None, None, ""any"", ""test"", DistributionType.SDIST, ) def test_wheel_dot(): filename = ""backports.html-1.1.0-py2.py3-none-any.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", ""test"", DistributionType.WHEEL, ) def test_wheel_platform_specific_tags(): filename = ""pywin32-224-cp27-cp27m-win_amd64.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""pywin32"", filename, pkg_resources.parse_version(""224""), WheelVersionTags((""cp27"",)), ""cp27m"", ""win_amd64"", ""test"", DistributionType.WHEEL, ) ", https://github.com/sputt/req-compile,c77e6777967ee660447beb9522c867a7233ef78d,tests/repos/test_pypi.py::test_successful_numpy,tests/repos/test_pypi.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import os import pkg_resources import requests import responses import pytest from req_compile.repos.repository import Candidate, WheelVersionTags, DistributionType import req_compile.repos.pypi from req_compile.repos.pypi import PyPIRepository, check_python_compatibility INDEX_URL = ""https://pypi.org"" @pytest.fixture def mocked_responses(): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: yield rsps @pytest.fixture def read_contents(): def _do_read(resource): with open(os.path.join(os.path.dirname(__file__), resource)) as handle: return handle.read() return _do_read def test_successful_numpy(mocked_responses, tmpdir, read_contents): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) # The total is the total number of links - exe links, which we do not support assert len(candidates) == 1127 - 34 assert len(mocked_responses.calls) == 1 def test_no_candidates(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/garbage/"", status=404) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""garbage"")) assert candidates == [] assert len(mocked_responses.calls) == 1 def test_pypi_500(mocked_responses, tmpdir): wheeldir = str(tmpdir) mocked_responses.add(responses.GET, INDEX_URL + ""/numpy/"", status=500) repo = PyPIRepository(INDEX_URL, wheeldir) with pytest.raises(requests.HTTPError): repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) def test_resolve_new_numpy(mocked_responses, tmpdir, read_contents, mocker): wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidates = repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) for candidate in candidates: if ""1.16.3"" in candidate.link[1]: mocked_responses.add( responses.GET, candidate.link[1], body=read_contents(""numpy.whl-contents""), status=200, ) mock_extract = mocker.MagicMock() mock_extract.return_value.name = ""numpy"" mocker.patch(""req_compile.repos.pypi.extract_metadata"", mock_extract) candidate, cached = repo.get_candidate(pkg_resources.Requirement.parse(""numpy"")) assert candidate is not None assert not cached listing = tmpdir.listdir() assert len(listing) == 1 assert ""1.16.3"" in str(listing[0]) assert "".whl"" in str(listing[0]) # Query the index, and download assert len(mocked_responses.calls) == 2 @pytest.mark.parametrize( ""url_to_check"", [ ""https://pypi.org/numpy-1.16.3-cp37-cp37m-win_amd64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3-cp37-cp37m-manylinux1_x86_64.whl#sha256=HASH"", ""https://pypi.org/numpy-1.16.3.zip#sha256=HASH"", ], ) def test_python_requires_wheel_tags( mocked_responses, tmpdir, mock_py_version, read_contents, url_to_check ): mock_py_version(""3.7.12"") wheeldir = str(tmpdir) mocked_responses.add( responses.GET, INDEX_URL + ""/numpy/"", body=read_contents(""numpy.html""), status=200, ) repo = PyPIRepository(INDEX_URL, wheeldir) candidate = [ candidate for candidate in repo.get_candidates(pkg_resources.Requirement.parse(""numpy"")) if candidate.link[1] == url_to_check ][0] if candidate.py_version: assert candidate.py_version.check_compatibility() @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.7.15"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.6.4"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.5.0"", ""==3.5""), (""3.2.17"", "">=2.7, ==3.*""), (""3.5.4"", ""~=3.5""), (""3.7.4"", ""~=3""), (""2.7.12"", ""2.7""), ], ) def test_requires_python_compatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert check_python_compatibility(py_requires) @pytest.mark.parametrize( ""sys_py_version, py_requires"", [ (""2.6.2"", "">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*""), (""3.2.17"", "">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*""), (""3.2.17"", "">=2.7, !=3.*""), (""4.1.0"", ""~=3.5""), (""2.7.2"", ""~=3.3""), (""2.7.2"", "">=3.3,<4""), (""3.7.4"", ""2.7""), ], ) def test_requires_python_incompatible(mock_py_version, sys_py_version, py_requires): mock_py_version(sys_py_version) assert not check_python_compatibility(py_requires) def test_links_parser_wheel(): filename = ""pytest-4.3.0-py2.py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""pytest"", filename, pkg_resources.parse_version(""4.3.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_py3_wheel(): filename = ""PyVISA-1.9.1-py3-none-any.whl"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA"", filename, pkg_resources.parse_version(""1.9.1""), WheelVersionTags((""py3"",)), None, ""any"", (url, filename), DistributionType.WHEEL, ) ] def test_links_parser_tar_gz_hyph(): filename = ""PyVISA-py-0.3.1.tar.gz"" url = ""https://url"" lp = req_compile.repos.pypi.LinksHTMLParser(url) lp.active_link = url, filename lp.handle_data(filename) assert lp.dists == [ Candidate( ""PyVISA-py"", filename, pkg_resources.parse_version(""0.3.1""), None, None, ""any"", (url, filename), DistributionType.SDIST, ) ] def test_tar_gz_dot(): filename = ""backports.html-1.1.0.tar.gz"" candidate = req_compile.repos.repository._tar_gz_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), None, None, ""any"", ""test"", DistributionType.SDIST, ) def test_wheel_dot(): filename = ""backports.html-1.1.0-py2.py3-none-any.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""backports.html"", filename, pkg_resources.parse_version(""1.1.0""), WheelVersionTags((""py2"", ""py3"")), None, ""any"", ""test"", DistributionType.WHEEL, ) def test_wheel_platform_specific_tags(): filename = ""pywin32-224-cp27-cp27m-win_amd64.whl"" candidate = req_compile.repos.repository._wheel_candidate(""test"", filename) assert candidate == Candidate( ""pywin32"", filename, pkg_resources.parse_version(""224""), WheelVersionTags((""cp27"",)), ""cp27m"", ""win_amd64"", ""test"", DistributionType.WHEEL, ) ", https://github.com/sputt/req-compile,c77e6777967ee660447beb9522c867a7233ef78d,tests/test_extractor.py::test_extractor[mock_targz],tests/test_extractor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import contextlib import logging import os import pytest import req_compile.metadata.extractor @contextlib.contextmanager def temp_cwd(new_cwd): old_cwd = os.getcwd() try: os.chdir(new_cwd) yield new_cwd finally: os.chdir(old_cwd) @pytest.mark.parametrize(""archive_fixture"", [""mock_targz"", ""mock_zip"", ""mock_zip""]) def test_extractor(archive_fixture, tmpdir, mock_targz, mock_zip): directory = ""comtypes-1.1.7"" if archive_fixture == ""mock_targz"": archive = mock_targz(directory) elif archive_fixture == ""mock_zip"": archive = mock_zip(directory) else: archive = os.path.abspath(os.path.join(""source-packages"", directory)) if archive_fixture == ""mock_targz"": extractor = req_compile.metadata.extractor.TarExtractor(""gz"", archive) prefix = directory + ""/"" elif archive_fixture == ""mock_zip"": extractor = req_compile.metadata.extractor.ZipExtractor(archive) prefix = directory + ""/"" else: extractor = req_compile.metadata.extractor.NonExtractor(archive) prefix = """" with contextlib.closing(extractor): all_names = set(extractor.names()) assert all_names == { prefix + ""README"", prefix + ""setup.py"", prefix + ""comtypes/__init__.py"", prefix + ""test/setup.py"", } prefix = extractor.fake_root + os.sep + prefix assert extractor.contents(prefix + ""README"") == ""README CONTENTS"" with extractor.open(prefix + ""README"") as handle: assert handle.read(2) == ""RE"" assert extractor.contents(prefix + ""README"") == ""README CONTENTS"" assert extractor.exists(prefix + ""test"") assert extractor.exists(prefix + ""test/setup.py"") assert not extractor.exists(prefix + ""test/setup2.py"") ", https://github.com/sputt/req-compile,c77e6777967ee660447beb9522c867a7233ef78d,tests/test_extractor.py::test_extractor[mock_zip0],tests/test_extractor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import contextlib import logging import os import pytest import req_compile.metadata.extractor @contextlib.contextmanager def temp_cwd(new_cwd): old_cwd = os.getcwd() try: os.chdir(new_cwd) yield new_cwd finally: os.chdir(old_cwd) @pytest.mark.parametrize(""archive_fixture"", [""mock_targz"", ""mock_zip"", ""mock_zip""]) def test_extractor(archive_fixture, tmpdir, mock_targz, mock_zip): directory = ""comtypes-1.1.7"" if archive_fixture == ""mock_targz"": archive = mock_targz(directory) elif archive_fixture == ""mock_zip"": archive = mock_zip(directory) else: archive = os.path.abspath(os.path.join(""source-packages"", directory)) if archive_fixture == ""mock_targz"": extractor = req_compile.metadata.extractor.TarExtractor(""gz"", archive) prefix = directory + ""/"" elif archive_fixture == ""mock_zip"": extractor = req_compile.metadata.extractor.ZipExtractor(archive) prefix = directory + ""/"" else: extractor = req_compile.metadata.extractor.NonExtractor(archive) prefix = """" with contextlib.closing(extractor): all_names = set(extractor.names()) assert all_names == { prefix + ""README"", prefix + ""setup.py"", prefix + ""comtypes/__init__.py"", prefix + ""test/setup.py"", } prefix = extractor.fake_root + os.sep + prefix assert extractor.contents(prefix + ""README"") == ""README CONTENTS"" with extractor.open(prefix + ""README"") as handle: assert handle.read(2) == ""RE"" assert extractor.contents(prefix + ""README"") == ""README CONTENTS"" assert extractor.exists(prefix + ""test"") assert extractor.exists(prefix + ""test/setup.py"") assert not extractor.exists(prefix + ""test/setup2.py"") ", https://github.com/sputt/req-compile,c77e6777967ee660447beb9522c867a7233ef78d,tests/test_extractor.py::test_extractor[mock_zip1],tests/test_extractor.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import contextlib import logging import os import pytest import req_compile.metadata.extractor @contextlib.contextmanager def temp_cwd(new_cwd): old_cwd = os.getcwd() try: os.chdir(new_cwd) yield new_cwd finally: os.chdir(old_cwd) @pytest.mark.parametrize(""archive_fixture"", [""mock_targz"", ""mock_zip"", ""mock_zip""]) def test_extractor(archive_fixture, tmpdir, mock_targz, mock_zip): directory = ""comtypes-1.1.7"" if archive_fixture == ""mock_targz"": archive = mock_targz(directory) elif archive_fixture == ""mock_zip"": archive = mock_zip(directory) else: archive = os.path.abspath(os.path.join(""source-packages"", directory)) if archive_fixture == ""mock_targz"": extractor = req_compile.metadata.extractor.TarExtractor(""gz"", archive) prefix = directory + ""/"" elif archive_fixture == ""mock_zip"": extractor = req_compile.metadata.extractor.ZipExtractor(archive) prefix = directory + ""/"" else: extractor = req_compile.metadata.extractor.NonExtractor(archive) prefix = """" with contextlib.closing(extractor): all_names = set(extractor.names()) assert all_names == { prefix + ""README"", prefix + ""setup.py"", prefix + ""comtypes/__init__.py"", prefix + ""test/setup.py"", } prefix = extractor.fake_root + os.sep + prefix assert extractor.contents(prefix + ""README"") == ""README CONTENTS"" with extractor.open(prefix + ""README"") as handle: assert handle.read(2) == ""RE"" assert extractor.contents(prefix + ""README"") == ""README CONTENTS"" assert extractor.exists(prefix + ""test"") assert extractor.exists(prefix + ""test/setup.py"") assert not extractor.exists(prefix + ""test/setup2.py"") ", https://github.com/stackify/stackify-api-python,c2625f558c41dc6c79474226b1ca7766b192dd66,tests/test_handler.py::TestListener::test_not_identified,tests/test_handler.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Test the stackify.handler module """""" import unittest from mock import patch, Mock try: import Queue as queue except ImportError: import queue from stackify.handler import StackifyHandler, StackifyListener from stackify.transport.application import ApiConfiguration import logging class TestHandler(unittest.TestCase): ''' Test the StackifyHandler class ''' def test_queue_full(self): '''The queue should evict when full''' q = queue.Queue(1) handler = StackifyHandler(queue_=q, listener=Mock()) # don't print warnings on overflow, so mute stackify logger logging.getLogger('stackify').propagate = False handler.enqueue('test1') handler.enqueue('test2') handler.enqueue('test3') self.assertEqual(q.qsize(), 1) self.assertEqual(q.get(), 'test3') class TestListener(unittest.TestCase): ''' Test the StackifyListener class ''' def setUp(self): self.config = ApiConfiguration( application='test_appname', environment='test_environment', api_key='test_apikey', api_url='test_apiurl', ) # don't print warnings on http crashes, so mute stackify logger logging.getLogger('stackify').propagate = False @patch('stackify.transport.default.DefaultTransport.create_message') @patch('stackify.transport.default.http.HTTPClient.POST') def test_not_identified(self, post, logmsg): '''The HTTPClient identifies automatically if needed''' listener = StackifyListener(queue_=Mock(), config=self.config) listener.handle(Mock()) listener.send_group() self.assertTrue(listener.transport._transport.identified) @patch('stackify.transport.default.DefaultTransport.create_message') @patch('stackify.transport.default.DefaultTransport.create_group_message') @patch('stackify.transport.default.http.HTTPClient.POST') def test_send_group_if_needed(self, post, logmsggroup, logmsg): '''The listener sends groups of messages''' listener = StackifyListener(queue_=Mock(), max_batch=3, config=self.config) listener.transport._transport.identified = True listener.handle(1) self.assertFalse(post.called) listener.handle(2) self.assertFalse(post.called) self.assertEqual(len(listener.messages), 2) listener.handle(3) self.assertTrue(post.called) self.assertEqual(len(listener.messages), 0) listener.handle(4) self.assertEqual(post.call_count, 1) self.assertEqual(len(listener.messages), 1) @patch('stackify.transport.default.DefaultTransport.create_message') @patch('stackify.handler.StackifyListener.send_group') def test_clear_queue_shutdown(self, send_group, logmsg): '''The listener sends the leftover messages on the queue when shutting down''' listener = StackifyListener(queue_=Mock(), max_batch=3, config=self.config) listener.transport._transport.identified = True listener._thread = Mock() listener.handle(1) listener.handle(2) self.assertFalse(send_group.called) listener.stop() self.assertTrue(send_group.called) @patch('stackify.transport.default.DefaultTransport.create_message') @patch('stackify.transport.default.DefaultTransport.create_group_message') @patch('stackify.transport.default.http.HTTPClient.send_log_group') def test_send_group_crash(self, send_log_group, logmsggroup, logmsg): '''The listener drops messages after retrying''' listener = StackifyListener(queue_=Mock(), max_batch=3, config=self.config) listener.transport._transport.identified = True send_log_group.side_effect = Exception listener.handle(1) listener.handle(2) listener.handle(3) self.assertEqual(len(listener.messages), 0) listener.handle(4) self.assertEqual(len(listener.messages), 1) self.assertEqual(send_log_group.call_count, 1) if __name__ == '__main__': unittest.main() ", https://github.com/stackify/stackify-api-python,c2625f558c41dc6c79474226b1ca7766b192dd66,tests/test_handler.py::TestListener::test_send_group_crash,tests/test_handler.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Test the stackify.handler module """""" import unittest from mock import patch, Mock try: import Queue as queue except ImportError: import queue from stackify.handler import StackifyHandler, StackifyListener from stackify.transport.application import ApiConfiguration import logging class TestHandler(unittest.TestCase): ''' Test the StackifyHandler class ''' def test_queue_full(self): '''The queue should evict when full''' q = queue.Queue(1) handler = StackifyHandler(queue_=q, listener=Mock()) # don't print warnings on overflow, so mute stackify logger logging.getLogger('stackify').propagate = False handler.enqueue('test1') handler.enqueue('test2') handler.enqueue('test3') self.assertEqual(q.qsize(), 1) self.assertEqual(q.get(), 'test3') class TestListener(unittest.TestCase): ''' Test the StackifyListener class ''' def setUp(self): self.config = ApiConfiguration( application='test_appname', environment='test_environment', api_key='test_apikey', api_url='test_apiurl', ) # don't print warnings on http crashes, so mute stackify logger logging.getLogger('stackify').propagate = False @patch('stackify.transport.default.DefaultTransport.create_message') @patch('stackify.transport.default.http.HTTPClient.POST') def test_not_identified(self, post, logmsg): '''The HTTPClient identifies automatically if needed''' listener = StackifyListener(queue_=Mock(), config=self.config) listener.handle(Mock()) listener.send_group() self.assertTrue(listener.transport._transport.identified) @patch('stackify.transport.default.DefaultTransport.create_message') @patch('stackify.transport.default.DefaultTransport.create_group_message') @patch('stackify.transport.default.http.HTTPClient.POST') def test_send_group_if_needed(self, post, logmsggroup, logmsg): '''The listener sends groups of messages''' listener = StackifyListener(queue_=Mock(), max_batch=3, config=self.config) listener.transport._transport.identified = True listener.handle(1) self.assertFalse(post.called) listener.handle(2) self.assertFalse(post.called) self.assertEqual(len(listener.messages), 2) listener.handle(3) self.assertTrue(post.called) self.assertEqual(len(listener.messages), 0) listener.handle(4) self.assertEqual(post.call_count, 1) self.assertEqual(len(listener.messages), 1) @patch('stackify.transport.default.DefaultTransport.create_message') @patch('stackify.handler.StackifyListener.send_group') def test_clear_queue_shutdown(self, send_group, logmsg): '''The listener sends the leftover messages on the queue when shutting down''' listener = StackifyListener(queue_=Mock(), max_batch=3, config=self.config) listener.transport._transport.identified = True listener._thread = Mock() listener.handle(1) listener.handle(2) self.assertFalse(send_group.called) listener.stop() self.assertTrue(send_group.called) @patch('stackify.transport.default.DefaultTransport.create_message') @patch('stackify.transport.default.DefaultTransport.create_group_message') @patch('stackify.transport.default.http.HTTPClient.send_log_group') def test_send_group_crash(self, send_log_group, logmsggroup, logmsg): '''The listener drops messages after retrying''' listener = StackifyListener(queue_=Mock(), max_batch=3, config=self.config) listener.transport._transport.identified = True send_log_group.side_effect = Exception listener.handle(1) listener.handle(2) listener.handle(3) self.assertEqual(len(listener.messages), 0) listener.handle(4) self.assertEqual(len(listener.messages), 1) self.assertEqual(send_log_group.call_count, 1) if __name__ == '__main__': unittest.main() ", https://github.com/stackify/stackify-api-python,c2625f558c41dc6c79474226b1ca7766b192dd66,tests/test_handler.py::TestListener::test_send_group_if_needed,tests/test_handler.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,""""""" Test the stackify.handler module """""" import unittest from mock import patch, Mock try: import Queue as queue except ImportError: import queue from stackify.handler import StackifyHandler, StackifyListener from stackify.transport.application import ApiConfiguration import logging class TestHandler(unittest.TestCase): ''' Test the StackifyHandler class ''' def test_queue_full(self): '''The queue should evict when full''' q = queue.Queue(1) handler = StackifyHandler(queue_=q, listener=Mock()) # don't print warnings on overflow, so mute stackify logger logging.getLogger('stackify').propagate = False handler.enqueue('test1') handler.enqueue('test2') handler.enqueue('test3') self.assertEqual(q.qsize(), 1) self.assertEqual(q.get(), 'test3') class TestListener(unittest.TestCase): ''' Test the StackifyListener class ''' def setUp(self): self.config = ApiConfiguration( application='test_appname', environment='test_environment', api_key='test_apikey', api_url='test_apiurl', ) # don't print warnings on http crashes, so mute stackify logger logging.getLogger('stackify').propagate = False @patch('stackify.transport.default.DefaultTransport.create_message') @patch('stackify.transport.default.http.HTTPClient.POST') def test_not_identified(self, post, logmsg): '''The HTTPClient identifies automatically if needed''' listener = StackifyListener(queue_=Mock(), config=self.config) listener.handle(Mock()) listener.send_group() self.assertTrue(listener.transport._transport.identified) @patch('stackify.transport.default.DefaultTransport.create_message') @patch('stackify.transport.default.DefaultTransport.create_group_message') @patch('stackify.transport.default.http.HTTPClient.POST') def test_send_group_if_needed(self, post, logmsggroup, logmsg): '''The listener sends groups of messages''' listener = StackifyListener(queue_=Mock(), max_batch=3, config=self.config) listener.transport._transport.identified = True listener.handle(1) self.assertFalse(post.called) listener.handle(2) self.assertFalse(post.called) self.assertEqual(len(listener.messages), 2) listener.handle(3) self.assertTrue(post.called) self.assertEqual(len(listener.messages), 0) listener.handle(4) self.assertEqual(post.call_count, 1) self.assertEqual(len(listener.messages), 1) @patch('stackify.transport.default.DefaultTransport.create_message') @patch('stackify.handler.StackifyListener.send_group') def test_clear_queue_shutdown(self, send_group, logmsg): '''The listener sends the leftover messages on the queue when shutting down''' listener = StackifyListener(queue_=Mock(), max_batch=3, config=self.config) listener.transport._transport.identified = True listener._thread = Mock() listener.handle(1) listener.handle(2) self.assertFalse(send_group.called) listener.stop() self.assertTrue(send_group.called) @patch('stackify.transport.default.DefaultTransport.create_message') @patch('stackify.transport.default.DefaultTransport.create_group_message') @patch('stackify.transport.default.http.HTTPClient.send_log_group') def test_send_group_crash(self, send_log_group, logmsggroup, logmsg): '''The listener drops messages after retrying''' listener = StackifyListener(queue_=Mock(), max_batch=3, config=self.config) listener.transport._transport.identified = True send_log_group.side_effect = Exception listener.handle(1) listener.handle(2) listener.handle(3) self.assertEqual(len(listener.messages), 0) listener.handle(4) self.assertEqual(len(listener.messages), 1) self.assertEqual(send_log_group.call_count, 1) if __name__ == '__main__': unittest.main() ", https://github.com/stackify/stackify-api-python,c2625f558c41dc6c79474226b1ca7766b192dd66,tests/transport/test_init.py::TestTransport::test_default_send_url,tests/transport/test_init.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import logging from mock import patch from tests.bases import ClearEnvTest from stackify.constants import AGENT_LOG_URL from stackify.constants import DEFAULT_HTTP_ENDPOINT from stackify.protos import stackify_agent_pb2 from stackify.transport import configure_transport from stackify.transport.agent import AgentHTTPTransport from stackify.transport.agent import AgentSocketTransport from stackify.transport.default import DefaultTransport from stackify.transport.default.log import LogMsg from stackify.transport.default.log import LogMsgGroup class TestTransport(ClearEnvTest): def test_invalid_transport(self): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', 'transport': 'invalid', } transport = configure_transport(**config) assert isinstance(transport, DefaultTransport) def test_default_transport(self): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', } transport = configure_transport(**config) assert isinstance(transport, DefaultTransport) def test_default_create_message(self): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', } transport = configure_transport(**config) message = transport.create_message(logging.makeLogRecord({'mgs': 'message'})) assert isinstance(message, LogMsg) def test_default_create_group_message(self): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', } transport = configure_transport(**config) message = transport.create_message(logging.makeLogRecord({'mgs': 'message'})) group_message = transport.create_group_message([message]) assert isinstance(group_message, LogMsgGroup) @patch('stackify.transport.default.http.HTTPClient.send') def test_default_send_url(self, mock_send): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', } transport = configure_transport(**config) message = transport.create_message(logging.makeLogRecord({'mgs': 'message'})) group_message = transport.create_group_message([message]) transport.send(group_message) assert mock_send.called assert mock_send.call_args_list[0][0][0] == '/Log/Save' def test_agent_socket_transport(self): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', 'socket_url': 'test_socketurl', 'transport': 'agent_socket', } transport = configure_transport(**config) assert isinstance(transport, AgentSocketTransport) def test_agent_socket_create_message(self): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', 'socket_url': 'test_socketurl', 'transport': 'agent_socket', } transport = configure_transport(**config) message = transport.create_message(logging.makeLogRecord({'mgs': 'message', 'funcName': 'foo'})) assert isinstance(message, stackify_agent_pb2.LogGroup.Log) def test_agent_socket_create_group_message(self): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', 'socket_url': 'test_socketurl', 'transport': 'agent_socket', } transport = configure_transport(**config) message = transport.create_message(logging.makeLogRecord({'mgs': 'message', 'funcName': 'foo'})) group_message = transport.create_group_message([message]) assert isinstance(group_message, stackify_agent_pb2.LogGroup) @patch('stackify.transport.agent.agent_socket.AgentSocket.send') def test_agent_socket_send_url(self, mock_send): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', 'socket_url': 'test_socketurl', 'transport': 'agent_socket', } transport = configure_transport(**config) message = transport.create_message(logging.makeLogRecord({'mgs': 'message', 'funcName': 'foo'})) group_message = transport.create_group_message([message]) transport.send(group_message) assert mock_send.called assert mock_send.call_args_list[0][0][0] == 'test_socketurl/log' @patch('stackify.transport.agent.agent_socket.AgentSocket.send') def test_agent_socket_send_url_default(self, mock_send): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', 'transport': 'agent_socket', } transport = configure_transport(**config) message = transport.create_message(logging.makeLogRecord({'mgs': 'message', 'funcName': 'foo'})) group_message = transport.create_group_message([message]) transport.send(group_message) assert mock_send.called assert mock_send.call_args_list[0][0][0] == 'http+unix://%2Fusr%2Flocal%2Fstackify%2Fstackify.sock/log' def test_agent_http_transport(self): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', 'http_endpoint': 'test.url', 'transport': 'agent_http', } transport = configure_transport(**config) assert isinstance(transport, AgentHTTPTransport) def test_agent_http_create_message(self): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', 'http_endpoint': 'test.url', 'transport': 'agent_http', } transport = configure_transport(**config) message = transport.create_message(logging.makeLogRecord({'mgs': 'message', 'funcName': 'foo'})) assert isinstance(message, stackify_agent_pb2.LogGroup.Log) def test_agent_http_create_group_message(self): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', 'http_endpoint': 'test.url', 'transport': 'agent_http', } transport = configure_transport(**config) message = transport.create_message(logging.makeLogRecord({'mgs': 'message', 'funcName': 'foo'})) group_message = transport.create_group_message([message]) assert isinstance(group_message, stackify_agent_pb2.LogGroup) @patch('stackify.transport.agent.agent_http.AgentHTTP.send') def test_agent_http_send_url(self, mock_send): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', 'http_endpoint': 'test.url', 'transport': 'agent_http', } transport = configure_transport(**config) assert isinstance(transport, AgentHTTPTransport) message = transport.create_message(logging.makeLogRecord({'mgs': 'message', 'funcName': 'foo'})) group_message = transport.create_group_message([message]) transport.send(group_message) assert mock_send.called assert mock_send.call_args_list[0][0][0] == 'test.url/log' @patch('stackify.transport.agent.agent_http.AgentHTTP.send') def test_agent_http_send_url_default(self, mock_send): config = { 'application': 'test_appname', 'environment': 'test_environment', 'api_key': 'test_apikey', 'api_url': 'test_apiurl', 'transport': 'agent_http', } transport = configure_transport(**config) assert isinstance(transport, AgentHTTPTransport) message = transport.create_message(logging.makeLogRecord({'mgs': 'message', 'funcName': 'foo'})) group_message = transport.create_group_message([message]) transport.send(group_message) assert mock_send.called assert mock_send.call_args_list[0][0][0] == DEFAULT_HTTP_ENDPOINT + AGENT_LOG_URL ", https://github.com/staffanm/layeredconfig,c0eae96da7848ea52b43ac947f284eb2324d9ce1,tests/test_layeredconfig.py::TestLayeredWithSingleSource::test_commandline,tests/test_layeredconfig.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import unicode_literals """""" test_layeredconfig ---------------------------------- Tests for `layeredconfig` module. """""" import os import logging import sys import codecs from six import text_type as str from datetime import date, datetime import argparse import json from operator import itemgetter from copy import deepcopy try: from collections import OrderedDict except ImportError: # pragma: no cover # if on python 2.6 from ordereddict import OrderedDict if sys.version_info < (2, 7, 0): # pragma: no cover import unittest2 as unittest else: import unittest import requests # The system under test from layeredconfig import (LayeredConfig, Defaults, INIFile, JSONFile, YAMLFile, PListFile, PyFile, Environment, Commandline, EtcdStore, UNIT_SEP) class LayeredConfigHelperTests(object): # Testcases for less-capable sources may override this supported_types = (str, int, bool, list, date, datetime) supports_nesting = True def _test_config_singlesection(self, cfg): self.assertIs(type(cfg.home), str) self.assertEqual(cfg.home, 'mydata') int_type = int if int in self.supported_types else str self.assertIs(type(cfg.processes), int_type) self.assertEqual(cfg.processes, int_type(4)) if bool in self.supported_types: self.assertEqual(cfg.force, True) else: self.assertEqual(cfg.force, ""True"") if list in self.supported_types: list_type = list list_want = ['foo', 'bar'] self.assertIs(type(cfg.extra[0]), str) else: list_type = str list_want = ""foo, bar"" # recommended list serialization self.assertIs(type(cfg.extra), list_type) self.assertEqual(cfg.extra, list_want) if date in self.supported_types: date_type = date date_want = date(2014, 10, 15) else: date_type = str date_want = ""2014-10-15"" # recommended date serialization self.assertIs(type(cfg.expires), date_type) self.assertEqual(cfg.expires, date_want) if datetime in self.supported_types: datetime_type = datetime datetime_want = datetime(2014, 10, 15, 14, 32, 7) else: datetime_type = str datetime_want = ""2014-10-15 14:32:07"" self.assertIs(type(cfg.lastrun), datetime_type) self.assertEqual(cfg.lastrun, datetime_want) def _test_config_subsections(self, cfg): self.assertEqual(cfg.home, 'mydata') with self.assertRaises(AttributeError): cfg.mymodule.home int_type = int if int in self.supported_types else str self.assertEqual(cfg.processes, int_type(4)) with self.assertRaises(AttributeError): cfg.mymodule.processes if bool in self.supported_types: self.assertEqual(cfg.mymodule.force, False) else: self.assertEqual(cfg.mymodule.force, ""False"") if list in self.supported_types: list_type = list list_want = ['foo', 'bar'] list_want_sub = ['foo', 'baz'] else: list_type = str list_want = ""foo, bar"" list_want_sub = ""foo, baz"" self.assertEqual(cfg.extra, list_want) self.assertEqual(cfg.mymodule.extra, list_want_sub) if self.supports_nesting: self.assertEqual(cfg.mymodule.arbitrary.nesting.depth, 'works') with self.assertRaises(AttributeError): cfg.expires if date in self.supported_types: date_type = date date_want = date(2014, 10, 15) else: date_type = str date_want = ""2014-10-15"" # recommended date serialization self.assertEqual(cfg.mymodule.expires, date_want) def _test_layered_configs(self, cfg): self.assertEqual(cfg.home, 'otherdata') bool_type = bool if bool in self.supported_types else str self.assertEqual(cfg.mymodule.force, bool_type(False)) def _test_layered_subsection_configs(self, cfg): if date in self.supported_types: date_type = date date_want = date(2014, 10, 15) else: date_type = str date_want = ""2014-10-15"" # recommended date serialization self.assertIs(type(cfg.mymodule.expires), date_type) self.assertEqual(cfg.mymodule.expires, date_want) class ConfigSourceHelperTests(LayeredConfigHelperTests): # First, a number of straightforward tests for any # ConfigSource-derived object. Concrete test classes should set up # self.simple and self.complex instances to match these. def test_keys(self): self.assertEqual(set(self.simple.keys()), set(('home', 'processes', 'force', 'extra', 'expires', 'lastrun'))) self.assertEqual(set(self.complex.keys()), set(('home', 'processes', 'force', 'extra'))) def test_subsection_keys(self): self.assertEqual(set(self.complex.subsection('mymodule').keys()), set(('force', 'extra', 'expires'))) def test_subsections(self): self.assertEqual(set(self.simple.subsections()), set()) self.assertEqual(set(self.complex.subsections()), set(('mymodule', 'extramodule'))) def test_subsection_nested(self): subsec = self.complex.subsection('mymodule') self.assertEqual(set(subsec.subsections()), set(('arbitrary',))) def test_has(self): for key in self.simple.keys(): self.assertTrue(self.simple.has(key)) def test_typed(self): for key in self.simple.keys(): self.assertTrue(self.simple.typed(key)) def test_get(self): # FIXME: This test should be able to look at supported_types # like test_singlesection and test_subsections do, so derived # testcase classes don't need to override it. self.assertEqual(self.simple.get(""home""), ""mydata"") self.assertEqual(self.simple.get(""processes""), 4) self.assertEqual(self.simple.get(""force""), True) self.assertEqual(self.simple.get(""extra""), ['foo', 'bar']) self.assertEqual(self.simple.get(""expires""), date(2014, 10, 15)) self.assertEqual(self.simple.get(""lastrun""), datetime(2014, 10, 15, 14, 32, 7)) # Then, two validation helpers for checking a complete # LayeredConfig object, where validation can be performed # different depending on the abilities of the source (eg. typing) def test_config_singlesection(self): # each subclass is responsible for creating a self.simple # object of the type being tested cfg = LayeredConfig(self.simple) self._test_config_singlesection(cfg) def test_config_subsections(self): cfg = LayeredConfig(self.complex) self._test_config_subsections(cfg) def test_layered_subsections(self): # this testcases excercies a bug (or rather a deficency in the # file-based sources) -- if the highest-priority source has # subsections, and a lower-priority file-based source lacks # those subsections, bad things would happen. # # see https://github.com/staffanm/layeredconfig/issues/2 cfg = LayeredConfig(self.complex, self.extra) self._test_layered_configs(cfg) def test_overwriting_with_missing_subsections(self): # this testcase tests the inverse of test_layered_subsections cfg = LayeredConfig(self.complex, self.extra_layered) self._test_layered_subsection_configs(cfg) DUMP_DEFAULTS = {'processes': int, 'force': bool, 'extra': list, 'mymodule': { 'force': bool, 'extra': list, 'expires': date, }, 'extramodule': { 'unique': bool } } DUMP_WANT = {'home': 'mydata', 'processes': 4, 'force': True, 'extra': ['foo', 'bar'], 'mymodule': { 'force': False, 'extra': ['foo', 'baz'], 'expires': date(2014, 10, 15), 'arbitrary': { 'nesting': { 'depth': 'works' } } }, 'extramodule': { 'unique': True } } def test_dump(self): want = deepcopy(self.DUMP_WANT) if not self.supports_nesting: # INIFile does not support nested sections del want['mymodule']['arbitrary'] cfg = LayeredConfig(Defaults(self.DUMP_DEFAULTS), self.complex) got = LayeredConfig.dump(cfg) self.maxDiff = None self.assertEquals(want, got) def test_dump_layered(self): want = deepcopy(self.DUMP_WANT) if not self.supports_nesting: # INIFile does not support nested sections del want['mymodule']['arbitrary'] want['home'] = 'otherdata' cfg = LayeredConfig(Defaults(self.DUMP_DEFAULTS), self.complex, self.extra) got = LayeredConfig.dump(cfg) self.maxDiff = None self.assertEquals(want, got) # common helper class TestINIFileHelper(object): def setUp(self): super(TestINIFileHelper, self).setUp() with open(""simple.ini"", ""w"") as fp: fp.write("""""" [__root__] home = mydata processes = 4 force = True extra = foo, bar expires = 2014-10-15 lastrun = 2014-10-1", https://github.com/starofrainnight/licenraptor,ed543a2b43b75d733579509a78161928ac29234c,tests/test_db.py::TestRegisterFind::test_exisitng[MIT],tests/test_db.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import pytest import licenraptor from licenraptor import base from licenraptor.licenses import MITLicense KEYS = ('rpm', 'name', 'python', 'url') class TestRegisterFind(object): ''' Test register() and find() ''' @classmethod def teardown_class(cls): del licenraptor.core._db['FOO'] def test_register_no_id(self): ''' Test that License classes cannot be registered without id ''' class FooLicense(base.License): pass with pytest.raises(AttributeError): licenraptor.register(FooLicense) def test_register_and_find(self): ''' Test that License classes can be registered with id ''' class FooLicense(base.License): id = 'FOO' licenraptor.register(FooLicense) assert licenraptor.find('FOO') == FooLicense def test_nonexisting(self): ''' Test that non-existing license cannot be found ''' with pytest.raises(KeyError): licenraptor.find('This is not an existing SPDX identifier') @pytest.mark.parametrize('id', ('MIT',)) def test_exisitng(self, id): ''' Test that an exisitng license can be found ''' assert licenraptor.find(id).id == id class TestFindByFunction(object): ''' Tests for find_by_function() ''' @pytest.mark.parametrize('multiple', (True, False)) def test_find_by_true(self, multiple): result = licenraptor.find_by_function(lambda x: True, multiple) assert result def test_find_by_false_multiple(self): results = licenraptor.find_by_function(lambda x: False, multiple=True) assert results == [] def test_find_by_false_single(self): with pytest.raises(KeyError): result = licenraptor.find_by_function( lambda x: False, multiple=False) def test_find_by_function_is_equal(self): result = licenraptor.find_by_function( lambda x: x == MITLicense, multiple=False) assert result == MITLicense @pytest.mark.parametrize(('text', 'res'), (('license', True), ('foobar', False))) def test_find_by_function_lower_endswith(self, text, res): results = licenraptor.find_by_function( lambda x: x.name.lower().endswith(text), multiple=True) assert (MITLicense in results) == res class TestFindByKeyWithoutIndex(object): ''' Tests for find_by_key() when no index has been built ''' @pytest.mark.parametrize('key', KEYS) def test_find_by_key_multiple(self, key): ''' Test that it is possible to find the license by various keys ''' value = getattr(MITLicense, key) results = licenraptor.find_by_key(key, value) assert results == [MITLicense] @pytest.mark.parametrize('key', KEYS) def test_find_by_key_single(self, key): ''' Test that it is possible to find the license by various keys ''' value = getattr(MITLicense, key) result = licenraptor.find_by_key(key, value, multiple=False) assert result == MITLicense @pytest.mark.parametrize('key', KEYS) def test_find_by_key_multiple_wrong(self, key): ''' Test that when finding multiple results, empty list is returned when nothing found ''' value = 'nonexistent value' results = licenraptor.find_by_key(key, value) assert results == [] @pytest.mark.parametrize('key', KEYS) def test_find_by_key_single_wrong(self, key): ''' Test that when finding single result, KeyError is risen when nothing found ''' value = 'nonexistent value' with pytest.raises(KeyError): result = licenraptor.find_by_key(key, value, multiple=False) def test_bsd_finds_multiple(self): ''' Test that searching for BSD in rpm key return at least two results ''' results = licenraptor.find_by_key('rpm', 'BSD') assert len(results) >= 2 class TestFindByKeyWithIndex(TestFindByKeyWithoutIndex): ''' Tests for find_by_key() when the index has been built ''' @classmethod def setup_class(cls): for key in KEYS: licenraptor.build_index(key) @classmethod def teardown_class(cls): for key in KEYS: licenraptor.delete_index(key) class TestIter(object): ''' Tests for the iter() function ''' def test_iter(self): for cls in licenraptor.iter(): pass ", https://github.com/starofrainnight/licenraptor,ed543a2b43b75d733579509a78161928ac29234c,tests/test_db.py::TestRegisterFind::test_nonexisting,tests/test_db.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import pytest import licenraptor from licenraptor import base from licenraptor.licenses import MITLicense KEYS = ('rpm', 'name', 'python', 'url') class TestRegisterFind(object): ''' Test register() and find() ''' @classmethod def teardown_class(cls): del licenraptor.core._db['FOO'] def test_register_no_id(self): ''' Test that License classes cannot be registered without id ''' class FooLicense(base.License): pass with pytest.raises(AttributeError): licenraptor.register(FooLicense) def test_register_and_find(self): ''' Test that License classes can be registered with id ''' class FooLicense(base.License): id = 'FOO' licenraptor.register(FooLicense) assert licenraptor.find('FOO') == FooLicense def test_nonexisting(self): ''' Test that non-existing license cannot be found ''' with pytest.raises(KeyError): licenraptor.find('This is not an existing SPDX identifier') @pytest.mark.parametrize('id', ('MIT',)) def test_exisitng(self, id): ''' Test that an exisitng license can be found ''' assert licenraptor.find(id).id == id class TestFindByFunction(object): ''' Tests for find_by_function() ''' @pytest.mark.parametrize('multiple', (True, False)) def test_find_by_true(self, multiple): result = licenraptor.find_by_function(lambda x: True, multiple) assert result def test_find_by_false_multiple(self): results = licenraptor.find_by_function(lambda x: False, multiple=True) assert results == [] def test_find_by_false_single(self): with pytest.raises(KeyError): result = licenraptor.find_by_function( lambda x: False, multiple=False) def test_find_by_function_is_equal(self): result = licenraptor.find_by_function( lambda x: x == MITLicense, multiple=False) assert result == MITLicense @pytest.mark.parametrize(('text', 'res'), (('license', True), ('foobar', False))) def test_find_by_function_lower_endswith(self, text, res): results = licenraptor.find_by_function( lambda x: x.name.lower().endswith(text), multiple=True) assert (MITLicense in results) == res class TestFindByKeyWithoutIndex(object): ''' Tests for find_by_key() when no index has been built ''' @pytest.mark.parametrize('key', KEYS) def test_find_by_key_multiple(self, key): ''' Test that it is possible to find the license by various keys ''' value = getattr(MITLicense, key) results = licenraptor.find_by_key(key, value) assert results == [MITLicense] @pytest.mark.parametrize('key', KEYS) def test_find_by_key_single(self, key): ''' Test that it is possible to find the license by various keys ''' value = getattr(MITLicense, key) result = licenraptor.find_by_key(key, value, multiple=False) assert result == MITLicense @pytest.mark.parametrize('key', KEYS) def test_find_by_key_multiple_wrong(self, key): ''' Test that when finding multiple results, empty list is returned when nothing found ''' value = 'nonexistent value' results = licenraptor.find_by_key(key, value) assert results == [] @pytest.mark.parametrize('key', KEYS) def test_find_by_key_single_wrong(self, key): ''' Test that when finding single result, KeyError is risen when nothing found ''' value = 'nonexistent value' with pytest.raises(KeyError): result = licenraptor.find_by_key(key, value, multiple=False) def test_bsd_finds_multiple(self): ''' Test that searching for BSD in rpm key return at least two results ''' results = licenraptor.find_by_key('rpm', 'BSD') assert len(results) >= 2 class TestFindByKeyWithIndex(TestFindByKeyWithoutIndex): ''' Tests for find_by_key() when the index has been built ''' @classmethod def setup_class(cls): for key in KEYS: licenraptor.build_index(key) @classmethod def teardown_class(cls): for key in KEYS: licenraptor.delete_index(key) class TestIter(object): ''' Tests for the iter() function ''' def test_iter(self): for cls in licenraptor.iter(): pass ", https://github.com/starofrainnight/licenraptor,ed543a2b43b75d733579509a78161928ac29234c,tests/test_db.py::TestRegisterFind::test_register_no_id,tests/test_db.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import pytest import licenraptor from licenraptor import base from licenraptor.licenses import MITLicense KEYS = ('rpm', 'name', 'python', 'url') class TestRegisterFind(object): ''' Test register() and find() ''' @classmethod def teardown_class(cls): del licenraptor.core._db['FOO'] def test_register_no_id(self): ''' Test that License classes cannot be registered without id ''' class FooLicense(base.License): pass with pytest.raises(AttributeError): licenraptor.register(FooLicense) def test_register_and_find(self): ''' Test that License classes can be registered with id ''' class FooLicense(base.License): id = 'FOO' licenraptor.register(FooLicense) assert licenraptor.find('FOO') == FooLicense def test_nonexisting(self): ''' Test that non-existing license cannot be found ''' with pytest.raises(KeyError): licenraptor.find('This is not an existing SPDX identifier') @pytest.mark.parametrize('id', ('MIT',)) def test_exisitng(self, id): ''' Test that an exisitng license can be found ''' assert licenraptor.find(id).id == id class TestFindByFunction(object): ''' Tests for find_by_function() ''' @pytest.mark.parametrize('multiple', (True, False)) def test_find_by_true(self, multiple): result = licenraptor.find_by_function(lambda x: True, multiple) assert result def test_find_by_false_multiple(self): results = licenraptor.find_by_function(lambda x: False, multiple=True) assert results == [] def test_find_by_false_single(self): with pytest.raises(KeyError): result = licenraptor.find_by_function( lambda x: False, multiple=False) def test_find_by_function_is_equal(self): result = licenraptor.find_by_function( lambda x: x == MITLicense, multiple=False) assert result == MITLicense @pytest.mark.parametrize(('text', 'res'), (('license', True), ('foobar', False))) def test_find_by_function_lower_endswith(self, text, res): results = licenraptor.find_by_function( lambda x: x.name.lower().endswith(text), multiple=True) assert (MITLicense in results) == res class TestFindByKeyWithoutIndex(object): ''' Tests for find_by_key() when no index has been built ''' @pytest.mark.parametrize('key', KEYS) def test_find_by_key_multiple(self, key): ''' Test that it is possible to find the license by various keys ''' value = getattr(MITLicense, key) results = licenraptor.find_by_key(key, value) assert results == [MITLicense] @pytest.mark.parametrize('key', KEYS) def test_find_by_key_single(self, key): ''' Test that it is possible to find the license by various keys ''' value = getattr(MITLicense, key) result = licenraptor.find_by_key(key, value, multiple=False) assert result == MITLicense @pytest.mark.parametrize('key', KEYS) def test_find_by_key_multiple_wrong(self, key): ''' Test that when finding multiple results, empty list is returned when nothing found ''' value = 'nonexistent value' results = licenraptor.find_by_key(key, value) assert results == [] @pytest.mark.parametrize('key', KEYS) def test_find_by_key_single_wrong(self, key): ''' Test that when finding single result, KeyError is risen when nothing found ''' value = 'nonexistent value' with pytest.raises(KeyError): result = licenraptor.find_by_key(key, value, multiple=False) def test_bsd_finds_multiple(self): ''' Test that searching for BSD in rpm key return at least two results ''' results = licenraptor.find_by_key('rpm', 'BSD') assert len(results) >= 2 class TestFindByKeyWithIndex(TestFindByKeyWithoutIndex): ''' Tests for find_by_key() when the index has been built ''' @classmethod def setup_class(cls): for key in KEYS: licenraptor.build_index(key) @classmethod def teardown_class(cls): for key in KEYS: licenraptor.delete_index(key) class TestIter(object): ''' Tests for the iter() function ''' def test_iter(self): for cls in licenraptor.iter(): pass ", https://github.com/stateya/m3u-dump,1ec3eeedb4c650af76c6c66859375ca2d2a6e1bc,tests/test_m3u_dump.py::test_command_line_already_playlist,tests/test_m3u_dump.py,NIO,flaky,Opened,https://github.com/stateya/m3u-dump/pull/2,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """""" test_m3u_dump ---------------------------------- Tests for `m3u_dump` module. """""" import os import pytest from click.testing import CliRunner from m3u_dump import cli from m3u_dump.m3u_dump import M3uDump @pytest.fixture(scope='session') def music_dir(tmpdir_factory): return tmpdir_factory.mktemp('music') @pytest.fixture(scope='session') def music_files(music_dir): d = music_dir d.join('dummy001.mp3').write('dummy') d.mkdir('sub').join('dummy002.mp3').write('dummy') d.mkdir('sub4').join('dummy002.mp3').write('dummy') d.mkdir('sub2').mkdir('sub3').join('あいう えお.mp3').write('dummy') d.mkdir('sub3').mkdir('かきく けこ').join('あいう えお.mp3').write('dummy') return d # noinspection PyShadowingNames @pytest.fixture(scope='session') def multi_playlist_music_files(music_dir): d = music_dir d.join('aaaa.m3u').write('dummy') d.mkdir('sub7').join('multi-dummy001.mp3').write('dummy') d.mkdir('sub8').mkdir('sub2').join('multi-dummy002.mp3').write('dummy') d.mkdir('sub9').join('multi-あいう えお.mp3').write('dummy') d.mkdir('sub10').join('multi-あいう えお.mp3').write('dummy') d.mkdir('sub11').join('multi-dummy004.mp3').write('dummy') d.mkdir('sub12').join('hello hello.mp3').write('dummy') return d @pytest.fixture def playlist_dir(tmpdir_factory): return tmpdir_factory.mktemp('playlist') # noinspection PyShadowingNames @pytest.fixture def playlist_current(playlist_dir): f = playlist_dir.join('playlist.m3u') f.write(""""""#EXTM3U #EXTINF:409,artist - music_name /full/path/dummy001.mp3 #EXTINF:281,artist - music_name /full/path/dummy002.mp3 #EXTINF:275,artist - music_name music/あいう えお.mp3 #EXTINF:263,artist - music_name /full/path/music/あいう えお.mp3 #EXTINF:288,artist - music_name /full/path/aaa/dummy002.mp3 #EXTINF:222,artist = music_name ../../hello.mp3"""""") return f @pytest.fixture(scope='session') def already_exists_playlist(tmpdir_factory): d = tmpdir_factory.mktemp('already-dir') music_path = str(d.mkdir('music').join('already_path.mp3').write('dummy')) playlist_content = """"""#EXTM3U #EXTINF:409,artist - music_name {}"""""".format(os.path.join(str(d), 'music', 'already_path.mp3')) playlist_path = str(d.join('playlist.m3u').write(playlist_content)) return d # noinspection PyShadowingNames @pytest.fixture def playlist_current2(playlist_dir): f = playlist_dir.join('playlist2.m3u8') f.write(""""""#EXTM3U #EXTINF:409,artist - music_name /full/path/multi-dummy001.mp3 #EXTINF:282,artist - music_name /full/path/multi-dummy001.mp3 #EXTINF:281,artist - music_name /full/path/multi-dummy002.mp3 #EXTINF:275,artist - music_name music/multi-あいう えお.mp3 #EXTINF:263,artist - music_name /full/path/music/multi-あいう えお.mp3 #EXTINF:288,artist - music_name /full/path/aaa/multi-dummy004.mp3 #EXTINF:222,artist = music_name ../../multi-hello.mp3"""""") return f @pytest.fixture(scope='session') def dump_music_path(tmpdir_factory): d = tmpdir_factory.mktemp('dst') return str(d) def test_command_line_interface(): runner = CliRunner() result = runner.invoke(cli.main) assert result.exit_code == 2 # must arguments assert 'Error: Missing argument' in result.output help_result = runner.invoke(cli.main, ['--help']) assert help_result.exit_code == 0 assert '--help' in help_result.output assert 'Show this message and exit.' in help_result.output # noinspection PyShadowingNames def test_command_line_dryrun(playlist_current, tmpdir_factory, music_files): dst_dir = str(tmpdir_factory.mktemp('no-dump-music')) runner = CliRunner() result = runner.invoke(cli.main, ['--dry-run', str(playlist_current), dst_dir, '--fix-search-path', str(music_files)]) assert 'Welcome m3u-dump' in result.output assert 'copy was completed(successful' in result.output assert result.exit_code == 0 # must arguments # copy できていないこと assert os.path.exists(os.path.join(dst_dir, 'dummy001.mp3')) is False assert os.path.exists(os.path.join(dst_dir, 'dummy002.mp3')) is False assert os.path.exists(os.path.join(dst_dir, 'あいう えお.mp3')) is False assert os.path.exists(os.path.join(dst_dir, 'あいう えお.mp3')) is False assert os.path.exists(os.path.join(dst_dir, 'hello.mp3')) is False playlist_name = os.path.basename(str(playlist_current)) playlist_path = os.path.join(dst_dir, playlist_name) assert os.path.exists(playlist_path) is False # noinspection PyShadowingNames def test_command_line_start(playlist_current, tmpdir_factory, music_files): dst_dir = str(tmpdir_factory.mktemp('dump-music')) runner = CliRunner() result = runner.invoke(cli.main, [str(playlist_current), dst_dir, '--fix-search-path', str(music_files)]) for line in result.output.split('\n'): print(line) assert 'Welcome m3u-dump' in result.output assert 'copy was completed(successful' in result.output assert result.exit_code == 0 # must arguments # copy できているか確認する assert os.path.exists(os.path.join(dst_dir, 'dummy001.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'dummy002.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'あいう えお.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'あいう えお.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'hello.mp3')) is False playlist_name = os.path.basename(str(playlist_current)) playlist_path = os.path.join(dst_dir, playlist_name) assert os.path.exists(playlist_path) is True with open(playlist_path, 'r') as f: assert '#EXTM3U' == f.readline().rstrip('\n') assert '#EXTINF:409,artist - music_name' == f.readline().rstrip('\n') assert 'dummy001.mp3' == f.readline().rstrip('\n') assert '#EXTINF:281,artist - music_name' == f.readline().rstrip('\n') assert 'dummy002.mp3' == f.readline().rstrip('\n') assert '#EXTINF:275,artist - music_name' == f.readline().rstrip('\n') assert 'あいう えお.mp3' == f.readline().rstrip('\n') assert '#EXTINF:263,artist - music_name' == f.readline().rstrip('\n') assert 'あいう えお.mp3' == f.readline().rstrip('\n') assert '#EXTINF:288,artist - music_name' == f.readline().rstrip('\n') assert 'dummy002.mp3' == f.readline().rstrip('\n') assert '' == f.readline().rstrip('\n') # noinspection PyShadowingNames def test_command_line_no_fix_start(playlist_current, tmpdir_factory, music_files): dst_dir = str(tmpdir_factory.mktemp('dump-music')) runner = CliRunner() result = runner.invoke(cli.main, [str(playlist_current), dst_dir]) for line in result.output.split('\n'): print(line) assert 'Welcome m3u-dump' in result.output assert 'copy was completed(successful' in result.output assert result.exit_code == 0 # must arguments # noinspection PyShadowingNames def test_command_line_already_playlist(already_exists_playlist): music_path = os.path.join(str(already_exists_playlist), 'music') dst_dir = os.path.join(str(already_exists_playlist), 'dst') os.mkdir(dst_dir) playlist_path = os.path.join(str(already_exists_playlist), 'playlist.m3u') runner = CliRunner() result = runner.invoke(cli.main, [playlist_path, dst_dir, '--fix-search-path', str(music_path)]) for line in result.output.split('\n'): print(line) assert 'Welcome m3u-dump' in result.output assert 'copy was completed(successful' in result.output assert result.exit_code == 0 # must arguments # copy できているか確認する assert os.path.exists(os.path.join(dst_dir, 'already_path.mp3')) is True playlist_path = os.path.join(dst_dir, 'playlist.m3u') assert os.path.exists(playlist_path) is True with open(playlist_path, 'r') as f: assert '#EXTM3U' == f.readline().rstrip('\n') assert '#EXTINF:409,artist - music_name' == f.readline().rstrip('\n') assert 'already_path.mp3' == f.readline().rstrip('\n') # noinspection PyShadowingNames def test_command_line_multi_playlist(playlist_current, playlist_current2, tmpdir_factory, music_files, multi_playlist_music_files): playlist_dir = os.path.dirname(str(playlist_current)) dst_dir = str(tmpdir_factory.mktemp('dump-music')) runner = CliRunner() result = runner.invoke(cli.main, [playlist_dir, dst_dir, '--fix-search-path', str(music_files)]) for line in result.output.split('\n'): print(line) assert 'Welcome m3u-dump' in result.output assert 'copy was completed(successful' in result.output assert result.exit_code == 0 # must arguments # copy できているか確認する assert os.path.exists(os.path.join(dst_dir, 'dummy001.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'dummy002.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'あいう えお.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'あいう えお.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'hello.mp3')) is False assert os.path.exists(os.path.join(dst_dir, 'multi-dummy001.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'multi-dummy002.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'multi-あいう えお.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'multi-あいう えお.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'multi-dummy004.mp3')) is True playlist_name = os.path.basename(str(playlist_current)) playlist_path = os.path.join(dst_dir, playlist_name) assert os.path.exists(playlist_path) is True with open(playlist_path, 'r') as f: assert '#EXTM3U' == f.readline().rstrip('\n') assert '#EXTINF:409,artist - music_name' == f.readline().rstrip( ", https://github.com/stateya/m3u-dump,1ec3eeedb4c650af76c6c66859375ca2d2a6e1bc,tests/test_m3u_dump.py::test_copy_music_dryrun,tests/test_m3u_dump.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """""" test_m3u_dump ---------------------------------- Tests for `m3u_dump` module. """""" import os import pytest from click.testing import CliRunner from m3u_dump import cli from m3u_dump.m3u_dump import M3uDump @pytest.fixture(scope='session') def music_dir(tmpdir_factory): return tmpdir_factory.mktemp('music') @pytest.fixture(scope='session') def music_files(music_dir): d = music_dir d.join('dummy001.mp3').write('dummy') d.mkdir('sub').join('dummy002.mp3').write('dummy') d.mkdir('sub4').join('dummy002.mp3').write('dummy') d.mkdir('sub2').mkdir('sub3').join('あいう えお.mp3').write('dummy') d.mkdir('sub3').mkdir('かきく けこ').join('あいう えお.mp3').write('dummy') return d # noinspection PyShadowingNames @pytest.fixture(scope='session') def multi_playlist_music_files(music_dir): d = music_dir d.join('aaaa.m3u').write('dummy') d.mkdir('sub7').join('multi-dummy001.mp3').write('dummy') d.mkdir('sub8').mkdir('sub2').join('multi-dummy002.mp3').write('dummy') d.mkdir('sub9').join('multi-あいう えお.mp3').write('dummy') d.mkdir('sub10').join('multi-あいう えお.mp3').write('dummy') d.mkdir('sub11').join('multi-dummy004.mp3').write('dummy') d.mkdir('sub12').join('hello hello.mp3').write('dummy') return d @pytest.fixture def playlist_dir(tmpdir_factory): return tmpdir_factory.mktemp('playlist') # noinspection PyShadowingNames @pytest.fixture def playlist_current(playlist_dir): f = playlist_dir.join('playlist.m3u') f.write(""""""#EXTM3U #EXTINF:409,artist - music_name /full/path/dummy001.mp3 #EXTINF:281,artist - music_name /full/path/dummy002.mp3 #EXTINF:275,artist - music_name music/あいう えお.mp3 #EXTINF:263,artist - music_name /full/path/music/あいう えお.mp3 #EXTINF:288,artist - music_name /full/path/aaa/dummy002.mp3 #EXTINF:222,artist = music_name ../../hello.mp3"""""") return f @pytest.fixture(scope='session') def already_exists_playlist(tmpdir_factory): d = tmpdir_factory.mktemp('already-dir') music_path = str(d.mkdir('music').join('already_path.mp3').write('dummy')) playlist_content = """"""#EXTM3U #EXTINF:409,artist - music_name {}"""""".format(os.path.join(str(d), 'music', 'already_path.mp3')) playlist_path = str(d.join('playlist.m3u').write(playlist_content)) return d # noinspection PyShadowingNames @pytest.fixture def playlist_current2(playlist_dir): f = playlist_dir.join('playlist2.m3u8') f.write(""""""#EXTM3U #EXTINF:409,artist - music_name /full/path/multi-dummy001.mp3 #EXTINF:282,artist - music_name /full/path/multi-dummy001.mp3 #EXTINF:281,artist - music_name /full/path/multi-dummy002.mp3 #EXTINF:275,artist - music_name music/multi-あいう えお.mp3 #EXTINF:263,artist - music_name /full/path/music/multi-あいう えお.mp3 #EXTINF:288,artist - music_name /full/path/aaa/multi-dummy004.mp3 #EXTINF:222,artist = music_name ../../multi-hello.mp3"""""") return f @pytest.fixture(scope='session') def dump_music_path(tmpdir_factory): d = tmpdir_factory.mktemp('dst') return str(d) def test_command_line_interface(): runner = CliRunner() result = runner.invoke(cli.main) assert result.exit_code == 2 # must arguments assert 'Error: Missing argument' in result.output help_result = runner.invoke(cli.main, ['--help']) assert help_result.exit_code == 0 assert '--help' in help_result.output assert 'Show this message and exit.' in help_result.output # noinspection PyShadowingNames def test_command_line_dryrun(playlist_current, tmpdir_factory, music_files): dst_dir = str(tmpdir_factory.mktemp('no-dump-music')) runner = CliRunner() result = runner.invoke(cli.main, ['--dry-run', str(playlist_current), dst_dir, '--fix-search-path', str(music_files)]) assert 'Welcome m3u-dump' in result.output assert 'copy was completed(successful' in result.output assert result.exit_code == 0 # must arguments # copy できていないこと assert os.path.exists(os.path.join(dst_dir, 'dummy001.mp3')) is False assert os.path.exists(os.path.join(dst_dir, 'dummy002.mp3')) is False assert os.path.exists(os.path.join(dst_dir, 'あいう えお.mp3')) is False assert os.path.exists(os.path.join(dst_dir, 'あいう えお.mp3')) is False assert os.path.exists(os.path.join(dst_dir, 'hello.mp3')) is False playlist_name = os.path.basename(str(playlist_current)) playlist_path = os.path.join(dst_dir, playlist_name) assert os.path.exists(playlist_path) is False # noinspection PyShadowingNames def test_command_line_start(playlist_current, tmpdir_factory, music_files): dst_dir = str(tmpdir_factory.mktemp('dump-music')) runner = CliRunner() result = runner.invoke(cli.main, [str(playlist_current), dst_dir, '--fix-search-path', str(music_files)]) for line in result.output.split('\n'): print(line) assert 'Welcome m3u-dump' in result.output assert 'copy was completed(successful' in result.output assert result.exit_code == 0 # must arguments # copy できているか確認する assert os.path.exists(os.path.join(dst_dir, 'dummy001.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'dummy002.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'あいう えお.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'あいう えお.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'hello.mp3')) is False playlist_name = os.path.basename(str(playlist_current)) playlist_path = os.path.join(dst_dir, playlist_name) assert os.path.exists(playlist_path) is True with open(playlist_path, 'r') as f: assert '#EXTM3U' == f.readline().rstrip('\n') assert '#EXTINF:409,artist - music_name' == f.readline().rstrip('\n') assert 'dummy001.mp3' == f.readline().rstrip('\n') assert '#EXTINF:281,artist - music_name' == f.readline().rstrip('\n') assert 'dummy002.mp3' == f.readline().rstrip('\n') assert '#EXTINF:275,artist - music_name' == f.readline().rstrip('\n') assert 'あいう えお.mp3' == f.readline().rstrip('\n') assert '#EXTINF:263,artist - music_name' == f.readline().rstrip('\n') assert 'あいう えお.mp3' == f.readline().rstrip('\n') assert '#EXTINF:288,artist - music_name' == f.readline().rstrip('\n') assert 'dummy002.mp3' == f.readline().rstrip('\n') assert '' == f.readline().rstrip('\n') # noinspection PyShadowingNames def test_command_line_no_fix_start(playlist_current, tmpdir_factory, music_files): dst_dir = str(tmpdir_factory.mktemp('dump-music')) runner = CliRunner() result = runner.invoke(cli.main, [str(playlist_current), dst_dir]) for line in result.output.split('\n'): print(line) assert 'Welcome m3u-dump' in result.output assert 'copy was completed(successful' in result.output assert result.exit_code == 0 # must arguments # noinspection PyShadowingNames def test_command_line_already_playlist(already_exists_playlist): music_path = os.path.join(str(already_exists_playlist), 'music') dst_dir = os.path.join(str(already_exists_playlist), 'dst') os.mkdir(dst_dir) playlist_path = os.path.join(str(already_exists_playlist), 'playlist.m3u') runner = CliRunner() result = runner.invoke(cli.main, [playlist_path, dst_dir, '--fix-search-path', str(music_path)]) for line in result.output.split('\n'): print(line) assert 'Welcome m3u-dump' in result.output assert 'copy was completed(successful' in result.output assert result.exit_code == 0 # must arguments # copy できているか確認する assert os.path.exists(os.path.join(dst_dir, 'already_path.mp3')) is True playlist_path = os.path.join(dst_dir, 'playlist.m3u') assert os.path.exists(playlist_path) is True with open(playlist_path, 'r') as f: assert '#EXTM3U' == f.readline().rstrip('\n') assert '#EXTINF:409,artist - music_name' == f.readline().rstrip('\n') assert 'already_path.mp3' == f.readline().rstrip('\n') # noinspection PyShadowingNames def test_command_line_multi_playlist(playlist_current, playlist_current2, tmpdir_factory, music_files, multi_playlist_music_files): playlist_dir = os.path.dirname(str(playlist_current)) dst_dir = str(tmpdir_factory.mktemp('dump-music')) runner = CliRunner() result = runner.invoke(cli.main, [playlist_dir, dst_dir, '--fix-search-path', str(music_files)]) for line in result.output.split('\n'): print(line) assert 'Welcome m3u-dump' in result.output assert 'copy was completed(successful' in result.output assert result.exit_code == 0 # must arguments # copy できているか確認する assert os.path.exists(os.path.join(dst_dir, 'dummy001.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'dummy002.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'あいう えお.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'あいう えお.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'hello.mp3')) is False assert os.path.exists(os.path.join(dst_dir, 'multi-dummy001.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'multi-dummy002.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'multi-あいう えお.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'multi-あいう えお.mp3')) is True assert os.path.exists(os.path.join(dst_dir, 'multi-dummy004.mp3')) is True playlist_name = os.path.basename(str(playlist_current)) playlist_path = os.path.join(dst_dir, playlist_name) assert os.path.exists(playlist_path) is True with open(playlist_path, 'r') as f: assert '#EXTM3U' == f.readline().rstrip('\n') assert '#EXTINF:409,artist - music_name' == f.readline().rstrip( ", https://github.com/stfc/fparser,4be91d2e1078fa5588e84a3299131f297afba675,src/fparser/one/tests/test_block_stmts.py::test_implicit_topyf,src/fparser/one/tests/test_block_stmts.py,NOD,flaky,nan,nan,classify;root_cause,"# -*- coding: utf-8 -*- # Copyright (c) 2018 Science and Technology Facilities Council. # # All rights reserved. # # Modifications made as part of the fparser project are distributed # under the following license: # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # ""AS IS"" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ''' File containing tests for the one.block_statements module. ''' from __future__ import absolute_import, print_function import pytest from fparser.common.utils import AnalyzeError from fparser.common.sourceinfo import FortranFormat from fparser.one.parsefortran import FortranParser from fparser.common.readfortran import FortranStringReader def test_get_type_by_name(monkeypatch): ''' Tests for HasImplicitStmt.get_type_by_name(). ''' from fparser.one.typedecl_statements import Real, Integer # We can't just create a HasImplicitStmt object so we get the parser # to create a module object as that sub-classes HasImplicitStmt (amongst # other things). string = '''\ module some_block end module some_block ''' reader = FortranStringReader(string) reader.set_format(FortranFormat(True, False)) parser = FortranParser(reader) parser.parse() mod = parser.block.content[0] # Now we have a Module object, we can call get_type_by_name()... rtype = mod.get_type_by_name(""a_real"") assert isinstance(rtype, Real) itype = mod.get_type_by_name(""i_int"") assert isinstance(itype, Integer) # Check that we raise the correct error if we don't have any implicit # rules set monkeypatch.setattr(mod.a, ""implicit_rules"", None) with pytest.raises(AnalyzeError) as err: _ = mod.get_type_by_name(""i_int"") assert ""Implicit rules mapping is null"" in str(err.value) def test_get_type_by_name_implicit(): ''' Tests for HasImplicitStmt.get_type_by_name() when the source code contains IMPLICIT statements. ''' from fparser.one.typedecl_statements import Real, Integer # We can't just create a HasImplicitStmt object so we get the parser # to create a module object as that sub-classes HasImplicitStmt (amongst # other things). string = '''\ module some_block implicit real (a-e) implicit integer (f-z) end module some_block ''' reader = FortranStringReader(string) reader.set_format(FortranFormat(True, False)) parser = FortranParser(reader) parser.parse() # Get the module object mod = parser.block.content[0] # We have to run the analyze method on the Implicit objects # produced by the parser in order to populate the implicit_rules # of the module. mod.content[0].analyze() mod.content[1].analyze() # Now we can call get_type_by_name()... rtype = mod.get_type_by_name(""a_real"") assert isinstance(rtype, Real) itype = mod.get_type_by_name(""f_int"") assert isinstance(itype, Integer) def test_implicit_topyf(monkeypatch): ''' Tests for the topyf() method of HasImplicitStmt. ''' # We can't just create a HasImplicitStmt object so we get the parser # to create a module object as that sub-classes HasImplicitStmt (amongst # other things). string = '''\ module some_block implicit real (a-e) implicit integer (f-z) end module some_block ''' reader = FortranStringReader(string) reader.set_format(FortranFormat(True, False)) parser = FortranParser(reader) parser.parse() # Get the module object mod = parser.block.content[0] code = mod.topyf() assert ""! default IMPLICIT rules apply"" not in code mod.content[0].analyze() mod.content[1].analyze() code = mod.topyf() assert ""REAL (a, b, c, d, e)"" in code assert ""INTEGER (f, g, h"" in code monkeypatch.setattr(mod.a, ""implicit_rules"", None) code = mod.topyf() assert ""IMPLICIT NONE"" in code ", https://github.com/stfc/fparser,4be91d2e1078fa5588e84a3299131f297afba675,src/fparser/one/tests/test_parsefortran.py::test_log_cache,src/fparser/one/tests/test_parsefortran.py,NIO,flaky,Accepted,https://github.com/stfc/fparser/pull/299,classify;root_cause;fix_proposal,"#!/usr/bin/env python # -*- coding: utf-8 -*- ############################################################################# # Modified work Copyright (c) 2017-2018 Science and Technology # Facilities Council # # All rights reserved. # # Modifications made as part of the fparser project are distributed # under the following license: # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # ""AS IS"" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ############################################################################## ''' Tests the fparser.one.parsefortran module. ''' import pytest import fparser.one.parsefortran import fparser.common.readfortran def test_log_empty(log): ''' Tests that a reader without next() method causes an event to be logged. ''' class EmptyReader(object): ''' A faux reader with no next() method. ''' id = 'thingumy' unit_under_test = fparser.one.parsefortran.FortranParser(EmptyReader()) unit_under_test.analyze() assert log.messages == {'debug': [], 'info': ['Nothing to analyze.'], 'warning': [], 'error': [], 'critical': []} def test_log_cache(log): ''' Tests that using a cached reader object logs an event. ''' class Readerlike(object): ''' Dummy reader class, the only purpose of which is to have an id and not cause the parser to fail. ''' id = 'thisun' def next(self): ''' Simple non-failure-causing method. ''' yield 'NOT A THING' raise StopIteration # Expect everything to go okay, no log messages. log.reset() _ = fparser.one.parsefortran.FortranParser(Readerlike()) assert log.messages == {'debug': [], 'info': [], 'warning': [], 'error': [], 'critical': []} # This time we should use a cached log. _ = fparser.one.parsefortran.FortranParser(Readerlike()) assert log.messages == {'debug': [], 'info': ['using cached thisun'], 'warning': [], 'error': [], 'critical': []} def test_log_failure(log, monkeypatch): ''' Tests that an unexpected read failure causes an event to be logged. ''' def faulty_next(self, ignore_comments=False): ''' Raies any old exception. ''' raise Exception('That''s all folks!') monkeypatch.setattr('fparser.common.readfortran.FortranStringReader.next', faulty_next) reader = fparser.common.readfortran.FortranStringReader('') unit_under_test = fparser.one.parsefortran.FortranParser(reader) with pytest.raises(Exception): unit_under_test.parse() assert log.messages['debug'][0] \ .startswith('An error occurred during parsing.') assert log.messages['info'] == [] assert log.messages['warning'] == [] assert log.messages['error'] == [] assert log.messages['critical'][0].startswith('While processing') assert log.messages['critical'][1] == 'STOPPED PARSING' def test_pyf(): ''' Tests inherited from implementation code. ''' string = """""" python module foo interface tere subroutine bar real r end subroutine bar end interface tere end python module foo """""" expected = [' PYTHONMODULE foo', ' INTERFACE tere', ' SUBROUTINE bar()', ' REAL r', ' END SUBROUTINE bar', ' END INTERFACE tere', ' END PYTHONMODULE foo'] reader = fparser.common.readfortran.FortranStringReader(string) reader.set_format(fparser.common.sourceinfo.FortranFormat.from_mode('pyf')) parser = fparser.one.parsefortran.FortranParser(reader) parser.parse() caught = parser.block.tofortran().splitlines() assert caught[0][:13] == '!BEGINSOURCE ' assert caught[1:] == expected def test_free90(): ''' Tests inherited from implementation code. ''' string = """""" module foo subroutine bar real r if ( pc_get_lun() .ne. 6) & write ( pc_get_lun(), '( & & /, a, /, "" p="", i4, "" stopping c_flag="", a, & & /, "" print unit="", i8)') & trim(title), pcpsx_i_pel(), trim(c_flag), pc_get_lun() if (.true.) then call smth end if aaa : if (.false.) then else if (a) then else end if aaa hey = 1 end subroutine bar abstract interface end interface end module foo """""" expected = [' MODULE foo', ' SUBROUTINE bar()', ' REAL r', ' IF (pc_get_lun() .ne. 6)' + ' WRITE (pc_get_lun(), \'( /, A, /, "" P = "", i4,' + ' "" stopping c_flag="", a, /, "" print unit="", i8)\')' + ' trim(title), pcpsx_i_pel(), trim(c_flag), pc_get_lun()', ' IF (.true.) THEN', ' CALL smth', ' END IF', ' aaa: IF (.false.) THEN', ' ELSE IF (a) THEN', ' ELSE', ' END IF aaa', ' hey = 1', ' END SUBROUTINE bar', ' ABSTRACT INTERFACE', ' END INTERFACE', ' END MODULE foo'] reader = fparser.common.readfortran.FortranStringReader(string) mode = fparser.common.sourceinfo.FortranFormat.from_mode('free') reader.set_format(mode) parser = fparser.one.parsefortran.FortranParser(reader) parser.parse() caught = parser.block.tofortran().splitlines() assert caught[0][:13] == '!BEGINSOURCE ' assert caught[1:] == expected def test_module_procedure(): ''' Tests a type that contains a procedure, and makes sure it has a module_procedures attribute ''' string = """""" module foo type, public :: grid_type contains procedure :: get_tmask end type grid_type end module foo """""" reader = fparser.common.readfortran.FortranStringReader(string) mode = fparser.common.sourceinfo.FortranFormat.from_mode('free') reader.set_format(mode) parser = fparser.one.parsefortran.FortranParser(reader) parser.parse() # Fails if the class Type does not have a ""module_procedures"" attribute parser.analyze() def test_f77(): ''' Tests inherited from implementation code. ''' string = """"""\ program foo a = 3 end subroutine bar end pure function foo(a) end pure real*4 recursive function bar() end """""" expected = [' PROGRAM foo', ' a = 3', ' END PROGRAM foo', ' SUBROUTINE bar()', ' END SUBROUTINE bar', ' pure FUNCTION foo(a)', ' END FUNCTION foo', ' pure recursive REAL*4 FUNCTION bar()', ' END FUNCTION bar'] reader = fparser.common.readfortran.FortranStringReader(string) parser = fparser.one.parsefortran.FortranParser(reader) parser.parse() assert isinstance(parser.block, fparser.one.block_statements.BeginSource) caught = parser.block.tofortran().splitlines() assert caught[0][:25] == ' ! BEGINSOURCE ' assert caught[1:] == expected ", https://github.com/stfc/fparser,4be91d2e1078fa5588e84a3299131f297afba675,src/fparser/one/tests/test_parsefortran.py::test_log_empty,src/fparser/one/tests/test_parsefortran.py,NIO,flaky,Accepted,https://github.com/stfc/fparser/pull/300,classify;root_cause;fix_proposal,"#!/usr/bin/env python # -*- coding: utf-8 -*- ############################################################################# # Modified work Copyright (c) 2017-2018 Science and Technology # Facilities Council # # All rights reserved. # # Modifications made as part of the fparser project are distributed # under the following license: # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # ""AS IS"" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ############################################################################## ''' Tests the fparser.one.parsefortran module. ''' import pytest import fparser.one.parsefortran import fparser.common.readfortran def test_log_empty(log): ''' Tests that a reader without next() method causes an event to be logged. ''' class EmptyReader(object): ''' A faux reader with no next() method. ''' id = 'thingumy' unit_under_test = fparser.one.parsefortran.FortranParser(EmptyReader()) unit_under_test.analyze() assert log.messages == {'debug': [], 'info': ['Nothing to analyze.'], 'warning': [], 'error': [], 'critical': []} def test_log_cache(log): ''' Tests that using a cached reader object logs an event. ''' class Readerlike(object): ''' Dummy reader class, the only purpose of which is to have an id and not cause the parser to fail. ''' id = 'thisun' def next(self): ''' Simple non-failure-causing method. ''' yield 'NOT A THING' raise StopIteration # Expect everything to go okay, no log messages. log.reset() _ = fparser.one.parsefortran.FortranParser(Readerlike()) assert log.messages == {'debug': [], 'info': [], 'warning': [], 'error': [], 'critical': []} # This time we should use a cached log. _ = fparser.one.parsefortran.FortranParser(Readerlike()) assert log.messages == {'debug': [], 'info': ['using cached thisun'], 'warning': [], 'error': [], 'critical': []} def test_log_failure(log, monkeypatch): ''' Tests that an unexpected read failure causes an event to be logged. ''' def faulty_next(self, ignore_comments=False): ''' Raies any old exception. ''' raise Exception('That''s all folks!') monkeypatch.setattr('fparser.common.readfortran.FortranStringReader.next', faulty_next) reader = fparser.common.readfortran.FortranStringReader('') unit_under_test = fparser.one.parsefortran.FortranParser(reader) with pytest.raises(Exception): unit_under_test.parse() assert log.messages['debug'][0] \ .startswith('An error occurred during parsing.') assert log.messages['info'] == [] assert log.messages['warning'] == [] assert log.messages['error'] == [] assert log.messages['critical'][0].startswith('While processing') assert log.messages['critical'][1] == 'STOPPED PARSING' def test_pyf(): ''' Tests inherited from implementation code. ''' string = """""" python module foo interface tere subroutine bar real r end subroutine bar end interface tere end python module foo """""" expected = [' PYTHONMODULE foo', ' INTERFACE tere', ' SUBROUTINE bar()', ' REAL r', ' END SUBROUTINE bar', ' END INTERFACE tere', ' END PYTHONMODULE foo'] reader = fparser.common.readfortran.FortranStringReader(string) reader.set_format(fparser.common.sourceinfo.FortranFormat.from_mode('pyf')) parser = fparser.one.parsefortran.FortranParser(reader) parser.parse() caught = parser.block.tofortran().splitlines() assert caught[0][:13] == '!BEGINSOURCE ' assert caught[1:] == expected def test_free90(): ''' Tests inherited from implementation code. ''' string = """""" module foo subroutine bar real r if ( pc_get_lun() .ne. 6) & write ( pc_get_lun(), '( & & /, a, /, "" p="", i4, "" stopping c_flag="", a, & & /, "" print unit="", i8)') & trim(title), pcpsx_i_pel(), trim(c_flag), pc_get_lun() if (.true.) then call smth end if aaa : if (.false.) then else if (a) then else end if aaa hey = 1 end subroutine bar abstract interface end interface end module foo """""" expected = [' MODULE foo', ' SUBROUTINE bar()', ' REAL r', ' IF (pc_get_lun() .ne. 6)' + ' WRITE (pc_get_lun(), \'( /, A, /, "" P = "", i4,' + ' "" stopping c_flag="", a, /, "" print unit="", i8)\')' + ' trim(title), pcpsx_i_pel(), trim(c_flag), pc_get_lun()', ' IF (.true.) THEN', ' CALL smth', ' END IF', ' aaa: IF (.false.) THEN', ' ELSE IF (a) THEN', ' ELSE', ' END IF aaa', ' hey = 1', ' END SUBROUTINE bar', ' ABSTRACT INTERFACE', ' END INTERFACE', ' END MODULE foo'] reader = fparser.common.readfortran.FortranStringReader(string) mode = fparser.common.sourceinfo.FortranFormat.from_mode('free') reader.set_format(mode) parser = fparser.one.parsefortran.FortranParser(reader) parser.parse() caught = parser.block.tofortran().splitlines() assert caught[0][:13] == '!BEGINSOURCE ' assert caught[1:] == expected def test_module_procedure(): ''' Tests a type that contains a procedure, and makes sure it has a module_procedures attribute ''' string = """""" module foo type, public :: grid_type contains procedure :: get_tmask end type grid_type end module foo """""" reader = fparser.common.readfortran.FortranStringReader(string) mode = fparser.common.sourceinfo.FortranFormat.from_mode('free') reader.set_format(mode) parser = fparser.one.parsefortran.FortranParser(reader) parser.parse() # Fails if the class Type does not have a ""module_procedures"" attribute parser.analyze() def test_f77(): ''' Tests inherited from implementation code. ''' string = """"""\ program foo a = 3 end subroutine bar end pure function foo(a) end pure real*4 recursive function bar() end """""" expected = [' PROGRAM foo', ' a = 3', ' END PROGRAM foo', ' SUBROUTINE bar()', ' END SUBROUTINE bar', ' pure FUNCTION foo(a)', ' END FUNCTION foo', ' pure recursive REAL*4 FUNCTION bar()', ' END FUNCTION bar'] reader = fparser.common.readfortran.FortranStringReader(string) parser = fparser.one.parsefortran.FortranParser(reader) parser.parse() assert isinstance(parser.block, fparser.one.block_statements.BeginSource) caught = parser.block.tofortran().splitlines() assert caught[0][:25] == ' ! BEGINSOURCE ' assert caught[1:] == expected ", https://github.com/stfc/fparser,4be91d2e1078fa5588e84a3299131f297afba675,src/fparser/one/tests/test_parsefortran.py::test_log_failure,src/fparser/one/tests/test_parsefortran.py,NIO,flaky,Accepted,https://github.com/stfc/fparser/pull/300,classify;root_cause;fix_proposal,"#!/usr/bin/env python # -*- coding: utf-8 -*- ############################################################################# # Modified work Copyright (c) 2017-2018 Science and Technology # Facilities Council # # All rights reserved. # # Modifications made as part of the fparser project are distributed # under the following license: # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # ""AS IS"" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ############################################################################## ''' Tests the fparser.one.parsefortran module. ''' import pytest import fparser.one.parsefortran import fparser.common.readfortran def test_log_empty(log): ''' Tests that a reader without next() method causes an event to be logged. ''' class EmptyReader(object): ''' A faux reader with no next() method. ''' id = 'thingumy' unit_under_test = fparser.one.parsefortran.FortranParser(EmptyReader()) unit_under_test.analyze() assert log.messages == {'debug': [], 'info': ['Nothing to analyze.'], 'warning': [], 'error': [], 'critical': []} def test_log_cache(log): ''' Tests that using a cached reader object logs an event. ''' class Readerlike(object): ''' Dummy reader class, the only purpose of which is to have an id and not cause the parser to fail. ''' id = 'thisun' def next(self): ''' Simple non-failure-causing method. ''' yield 'NOT A THING' raise StopIteration # Expect everything to go okay, no log messages. log.reset() _ = fparser.one.parsefortran.FortranParser(Readerlike()) assert log.messages == {'debug': [], 'info': [], 'warning': [], 'error': [], 'critical': []} # This time we should use a cached log. _ = fparser.one.parsefortran.FortranParser(Readerlike()) assert log.messages == {'debug': [], 'info': ['using cached thisun'], 'warning': [], 'error': [], 'critical': []} def test_log_failure(log, monkeypatch): ''' Tests that an unexpected read failure causes an event to be logged. ''' def faulty_next(self, ignore_comments=False): ''' Raies any old exception. ''' raise Exception('That''s all folks!') monkeypatch.setattr('fparser.common.readfortran.FortranStringReader.next', faulty_next) reader = fparser.common.readfortran.FortranStringReader('') unit_under_test = fparser.one.parsefortran.FortranParser(reader) with pytest.raises(Exception): unit_under_test.parse() assert log.messages['debug'][0] \ .startswith('An error occurred during parsing.') assert log.messages['info'] == [] assert log.messages['warning'] == [] assert log.messages['error'] == [] assert log.messages['critical'][0].startswith('While processing') assert log.messages['critical'][1] == 'STOPPED PARSING' def test_pyf(): ''' Tests inherited from implementation code. ''' string = """""" python module foo interface tere subroutine bar real r end subroutine bar end interface tere end python module foo """""" expected = [' PYTHONMODULE foo', ' INTERFACE tere', ' SUBROUTINE bar()', ' REAL r', ' END SUBROUTINE bar', ' END INTERFACE tere', ' END PYTHONMODULE foo'] reader = fparser.common.readfortran.FortranStringReader(string) reader.set_format(fparser.common.sourceinfo.FortranFormat.from_mode('pyf')) parser = fparser.one.parsefortran.FortranParser(reader) parser.parse() caught = parser.block.tofortran().splitlines() assert caught[0][:13] == '!BEGINSOURCE ' assert caught[1:] == expected def test_free90(): ''' Tests inherited from implementation code. ''' string = """""" module foo subroutine bar real r if ( pc_get_lun() .ne. 6) & write ( pc_get_lun(), '( & & /, a, /, "" p="", i4, "" stopping c_flag="", a, & & /, "" print unit="", i8)') & trim(title), pcpsx_i_pel(), trim(c_flag), pc_get_lun() if (.true.) then call smth end if aaa : if (.false.) then else if (a) then else end if aaa hey = 1 end subroutine bar abstract interface end interface end module foo """""" expected = [' MODULE foo', ' SUBROUTINE bar()', ' REAL r', ' IF (pc_get_lun() .ne. 6)' + ' WRITE (pc_get_lun(), \'( /, A, /, "" P = "", i4,' + ' "" stopping c_flag="", a, /, "" print unit="", i8)\')' + ' trim(title), pcpsx_i_pel(), trim(c_flag), pc_get_lun()', ' IF (.true.) THEN', ' CALL smth', ' END IF', ' aaa: IF (.false.) THEN', ' ELSE IF (a) THEN', ' ELSE', ' END IF aaa', ' hey = 1', ' END SUBROUTINE bar', ' ABSTRACT INTERFACE', ' END INTERFACE', ' END MODULE foo'] reader = fparser.common.readfortran.FortranStringReader(string) mode = fparser.common.sourceinfo.FortranFormat.from_mode('free') reader.set_format(mode) parser = fparser.one.parsefortran.FortranParser(reader) parser.parse() caught = parser.block.tofortran().splitlines() assert caught[0][:13] == '!BEGINSOURCE ' assert caught[1:] == expected def test_module_procedure(): ''' Tests a type that contains a procedure, and makes sure it has a module_procedures attribute ''' string = """""" module foo type, public :: grid_type contains procedure :: get_tmask end type grid_type end module foo """""" reader = fparser.common.readfortran.FortranStringReader(string) mode = fparser.common.sourceinfo.FortranFormat.from_mode('free') reader.set_format(mode) parser = fparser.one.parsefortran.FortranParser(reader) parser.parse() # Fails if the class Type does not have a ""module_procedures"" attribute parser.analyze() def test_f77(): ''' Tests inherited from implementation code. ''' string = """"""\ program foo a = 3 end subroutine bar end pure function foo(a) end pure real*4 recursive function bar() end """""" expected = [' PROGRAM foo', ' a = 3', ' END PROGRAM foo', ' SUBROUTINE bar()', ' END SUBROUTINE bar', ' pure FUNCTION foo(a)', ' END FUNCTION foo', ' pure recursive REAL*4 FUNCTION bar()', ' END FUNCTION bar'] reader = fparser.common.readfortran.FortranStringReader(string) parser = fparser.one.parsefortran.FortranParser(reader) parser.parse() assert isinstance(parser.block, fparser.one.block_statements.BeginSource) caught = parser.block.tofortran().splitlines() assert caught[0][:25] == ' ! BEGINSOURCE ' assert caught[1:] == expected ", https://github.com/Stratoscale/skipper,dde3194bf6cf227b30c6e54b18c37317d2e0647e,tests/test_cli.py::TestCLI::test_make_without_build_container_tag,tests/test_cli.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import mock import os from six.moves import http_client import unittest import click import six from click import testing from skipper import cli from skipper import config, utils REGISTRY = 'registry.io:5000' IMAGE = 'image' TAG = '1234567' FQDN_IMAGE = REGISTRY + '/' + IMAGE + ':' + TAG BUILD_CONTAINER_IMAGE = 'build-container-image' BUILD_CONTAINER_TAG = 'build-container-tag' BUILD_CONTAINER_FQDN_IMAGE = REGISTRY + '/' + BUILD_CONTAINER_IMAGE + ':' + BUILD_CONTAINER_TAG ENV = [""KEY1=VAL1"", ""KEY2=VAL2""] SKIPPER_CONF_CONTAINER_CONTEXT = '/some/context' SKIPPER_CONF_BUILD_CONTAINER_IMAGE = 'skipper-conf-build-container-image' SKIPPER_CONF_BUILD_CONTAINER_TAG = 'skipper-conf-build-container-tag' SKIPPER_CONF_BUILD_CONTAINER_FQDN_IMAGE = REGISTRY + '/' + SKIPPER_CONF_BUILD_CONTAINER_IMAGE + ':' + SKIPPER_CONF_BUILD_CONTAINER_TAG SKIPPER_CONF_MAKEFILE = 'Makefile.skipper' SKIPPER_CONF = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, } } CONFIG_ENV = { ""KEY2"": ""NOT_VAL2"", ""KEY3"": ""VAL3"", ""KEY4"": ""$VAL4"", ""KEY5"": ""$$VAL5"" } CONFIG_ENV_EVALUATION = { ""KEY2"": ""NOT_VAL2"", ""KEY3"": ""VAL3"", ""KEY4"": ""val4-evaluation"", ""KEY5"": ""$VAL5"" } SKIPPER_CONF_WITH_ENV = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'env': CONFIG_ENV } SKIPPER_CONF_WITH_ENV_LIST = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'env': ['key1=value1', 'key2'] } SKIPPER_CONF_WITH_ENV_WRONG_TYPE = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'env': 'wrong-env-type', } SKIPPER_CONF_WITH_CONTAINERS = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'containers': { 'image1': 'app1/Dockerfile', 'image2': 'app2/Dockerfile', } } SKIPPER_CONF_WITH_VOLUMES = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'volumes': [ 'volume1', 'volume2', ] } SKIPPER_CONF_WITH_WORKDIR = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'workdir': 'test-workdir' } SKIPPER_CONF_WITH_WORKSPACE = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'workspace': '/test/workspace' } SKIPPER_CONF_WITH_GIT_REV = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': 'git:revision', 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, } SKIPPER_CONF_WITH_CONTEXT = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'container-context': SKIPPER_CONF_CONTAINER_CONTEXT } SKIPPER_CONF_WITH_CONTEXT_NO_TAG = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'container-context': SKIPPER_CONF_CONTAINER_CONTEXT } class TestCLI(unittest.TestCase): def setUp(self): self.runtime = ""docker"" utils.CONTAINER_RUNTIME_COMMAND = self.runtime self._runner = testing.CliRunner() self.global_params = [ '--registry', REGISTRY, '--build-container-image', BUILD_CONTAINER_IMAGE, '--build-container-tag', BUILD_CONTAINER_TAG ] def test_cli_without_params(self): result = self._invoke_cli() self.assertEqual(result.exit_code, 0) def test_cli_help(self): result = self._invoke_cli(global_params=['--help']) self.assertEqual(result.exit_code, 0) def test_subcommand_help(self): for subcmd in ('build', 'push', 'make', 'run'): result = self._invoke_cli( global_params=None, subcmd=subcmd, subcmd_params=['--help'] ) self.assertEqual(result.exit_code, 0) def test_subcommand_without_global_params(self): subcmd_params_map = { 'push': [IMAGE], 'run': ['ls' '-l'], 'make': ['-f', 'Makefile', 'all'], } for subcmd, subcmd_params in six.iteritems(subcmd_params_map): result = self._invoke_cli( global_params=None, subcmd=subcmd, subcmd_params=subcmd_params, ) self.assertIsInstance(result.exception, click.BadParameter) self.assertEqual(result.exit_code, -1) @mock.patch('skipper.runner.run', autospec=True) def test_subcommand_without_subcommand_params(self, skipper_runner_run_mock): for subcmd in ('build', 'push', 'run', 'make'): result = self._invoke_cli(self.global_params, subcmd) self.assertNotEqual(result.exit_code, 0) self.assertFalse(skipper_runner_run_mock.called) @mock.patch('skipper.utils.get_images_from_dockerfiles', mock.MagicMock(autospec=True, return_value={ 'image1': '/home/user/work/project/Dockerfile.image1', 'image2': '/home/user/work/project/Dockerfile.image2'})) @mock.patch('skipper.git.get_hash', mock.MagicMock(autospec=True, return_value='1234567')) @mock.patch('os.path.exists', mock.MagicMock(autospec=True, return_value=True)) @mock.patch('skipper.runner.run', autospec=True, return_value=0) def test_build_existing_image(self, skipper_runner_run_mock): build_params = ['image1'] self._invoke_cli( global_params=self.global_params, subcmd='build', subcmd_params=build_params ) expected_command = [ 'build', '--network=host', '--build-arg', 'TAG=1234567', '-f', '/home/user/work/project/Dockerfile.image1', '-t', 'image1:1234567', '/home/user/work/project' ] skipper_runner_run_mock.assert_called_once_with(expected_command) @mock.patch('skipper.utils.get_images_from_dockerfiles', mock.MagicMock(autospec=True, return_value={ 'image1': '/home/user/work/project/Dockerfile.image1'})) @mock.patch('skipper.git.get_hash', mock.MagicMock(autospec=True, return_value='1234567')) @mock.patch('os.path.exists', mock.MagicMock(autospec=True, return_value=True)) @mock.patch('skipper.runner.run', autospec=True, return_value=0) def test_build_existing_image_with_context(self, skipper_runner_run_mock): build_params = ['image1', '--container-context', '/home/user/work/project'] self._invoke_cli( global_params=self.global_params, subcmd='build', subcmd_params=build_params ) expected_command = [ 'build', '--network=host', '--build-arg', 'TAG=1234567', '-f', '/home/user/work/project/Dockerfile.image1', '-t', 'image1:1234567', '/home/user/work/project' ] skipper_runner_run_mock.assert_called_once_with(expected_command) @mock.patch('skipper.utils.get_images_from_dockerfiles', mock.MagicMock(autospec=True, return_value={ 'image1': '/home/user/work/project/Dockerfile.image1', 'image2': '/home/user/work/project/Dockerfile.image2'})) @mock.patch('__builtin__.open', mock.MagicMock(create=True)) @mock.patch('os.path.exists', mock.MagicMock(autospec=True, return_value=True)) @mock.patch('yaml.safe_load', mock.MagicMock(autospec=True, return_value=SKIPPER_CONF_WITH_CONTEXT)) @mock.patch('skipper.git.get_hash', mock.MagicMock(autospec=True, return_value='1234567')) @mock.patch('skipper.runner.run', autospec=True, return_value=0) def test_build_with_context_from_config_file(self, skipper_runner_run_mock): build_params = ['image1'] self._invoke_cli( defaults=config.load_defaults(), subcmd='build', subcmd_params=build_params ) expected_command = [ 'build', '--network=host', '--build-arg', 'TAG=1234567', '-f', '/home/user/work/project/Dockerfile.image1', '-t', 'image1:1234567', ", https://github.com/Stratoscale/skipper,dde3194bf6cf227b30c6e54b18c37317d2e0647e,tests/test_cli.py::TestCLI::test_run_without_build_container_tag,tests/test_cli.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import mock import os from six.moves import http_client import unittest import click import six from click import testing from skipper import cli from skipper import config, utils REGISTRY = 'registry.io:5000' IMAGE = 'image' TAG = '1234567' FQDN_IMAGE = REGISTRY + '/' + IMAGE + ':' + TAG BUILD_CONTAINER_IMAGE = 'build-container-image' BUILD_CONTAINER_TAG = 'build-container-tag' BUILD_CONTAINER_FQDN_IMAGE = REGISTRY + '/' + BUILD_CONTAINER_IMAGE + ':' + BUILD_CONTAINER_TAG ENV = [""KEY1=VAL1"", ""KEY2=VAL2""] SKIPPER_CONF_CONTAINER_CONTEXT = '/some/context' SKIPPER_CONF_BUILD_CONTAINER_IMAGE = 'skipper-conf-build-container-image' SKIPPER_CONF_BUILD_CONTAINER_TAG = 'skipper-conf-build-container-tag' SKIPPER_CONF_BUILD_CONTAINER_FQDN_IMAGE = REGISTRY + '/' + SKIPPER_CONF_BUILD_CONTAINER_IMAGE + ':' + SKIPPER_CONF_BUILD_CONTAINER_TAG SKIPPER_CONF_MAKEFILE = 'Makefile.skipper' SKIPPER_CONF = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, } } CONFIG_ENV = { ""KEY2"": ""NOT_VAL2"", ""KEY3"": ""VAL3"", ""KEY4"": ""$VAL4"", ""KEY5"": ""$$VAL5"" } CONFIG_ENV_EVALUATION = { ""KEY2"": ""NOT_VAL2"", ""KEY3"": ""VAL3"", ""KEY4"": ""val4-evaluation"", ""KEY5"": ""$VAL5"" } SKIPPER_CONF_WITH_ENV = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'env': CONFIG_ENV } SKIPPER_CONF_WITH_ENV_LIST = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'env': ['key1=value1', 'key2'] } SKIPPER_CONF_WITH_ENV_WRONG_TYPE = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'env': 'wrong-env-type', } SKIPPER_CONF_WITH_CONTAINERS = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'containers': { 'image1': 'app1/Dockerfile', 'image2': 'app2/Dockerfile', } } SKIPPER_CONF_WITH_VOLUMES = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'volumes': [ 'volume1', 'volume2', ] } SKIPPER_CONF_WITH_WORKDIR = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'workdir': 'test-workdir' } SKIPPER_CONF_WITH_WORKSPACE = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'workspace': '/test/workspace' } SKIPPER_CONF_WITH_GIT_REV = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': 'git:revision', 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, } SKIPPER_CONF_WITH_CONTEXT = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'container-context': SKIPPER_CONF_CONTAINER_CONTEXT } SKIPPER_CONF_WITH_CONTEXT_NO_TAG = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'container-context': SKIPPER_CONF_CONTAINER_CONTEXT } class TestCLI(unittest.TestCase): def setUp(self): self.runtime = ""docker"" utils.CONTAINER_RUNTIME_COMMAND = self.runtime self._runner = testing.CliRunner() self.global_params = [ '--registry', REGISTRY, '--build-container-image', BUILD_CONTAINER_IMAGE, '--build-container-tag', BUILD_CONTAINER_TAG ] def test_cli_without_params(self): result = self._invoke_cli() self.assertEqual(result.exit_code, 0) def test_cli_help(self): result = self._invoke_cli(global_params=['--help']) self.assertEqual(result.exit_code, 0) def test_subcommand_help(self): for subcmd in ('build', 'push', 'make', 'run'): result = self._invoke_cli( global_params=None, subcmd=subcmd, subcmd_params=['--help'] ) self.assertEqual(result.exit_code, 0) def test_subcommand_without_global_params(self): subcmd_params_map = { 'push': [IMAGE], 'run': ['ls' '-l'], 'make': ['-f', 'Makefile', 'all'], } for subcmd, subcmd_params in six.iteritems(subcmd_params_map): result = self._invoke_cli( global_params=None, subcmd=subcmd, subcmd_params=subcmd_params, ) self.assertIsInstance(result.exception, click.BadParameter) self.assertEqual(result.exit_code, -1) @mock.patch('skipper.runner.run', autospec=True) def test_subcommand_without_subcommand_params(self, skipper_runner_run_mock): for subcmd in ('build', 'push', 'run', 'make'): result = self._invoke_cli(self.global_params, subcmd) self.assertNotEqual(result.exit_code, 0) self.assertFalse(skipper_runner_run_mock.called) @mock.patch('skipper.utils.get_images_from_dockerfiles', mock.MagicMock(autospec=True, return_value={ 'image1': '/home/user/work/project/Dockerfile.image1', 'image2': '/home/user/work/project/Dockerfile.image2'})) @mock.patch('skipper.git.get_hash', mock.MagicMock(autospec=True, return_value='1234567')) @mock.patch('os.path.exists', mock.MagicMock(autospec=True, return_value=True)) @mock.patch('skipper.runner.run', autospec=True, return_value=0) def test_build_existing_image(self, skipper_runner_run_mock): build_params = ['image1'] self._invoke_cli( global_params=self.global_params, subcmd='build', subcmd_params=build_params ) expected_command = [ 'build', '--network=host', '--build-arg', 'TAG=1234567', '-f', '/home/user/work/project/Dockerfile.image1', '-t', 'image1:1234567', '/home/user/work/project' ] skipper_runner_run_mock.assert_called_once_with(expected_command) @mock.patch('skipper.utils.get_images_from_dockerfiles', mock.MagicMock(autospec=True, return_value={ 'image1': '/home/user/work/project/Dockerfile.image1'})) @mock.patch('skipper.git.get_hash', mock.MagicMock(autospec=True, return_value='1234567')) @mock.patch('os.path.exists', mock.MagicMock(autospec=True, return_value=True)) @mock.patch('skipper.runner.run', autospec=True, return_value=0) def test_build_existing_image_with_context(self, skipper_runner_run_mock): build_params = ['image1', '--container-context', '/home/user/work/project'] self._invoke_cli( global_params=self.global_params, subcmd='build', subcmd_params=build_params ) expected_command = [ 'build', '--network=host', '--build-arg', 'TAG=1234567', '-f', '/home/user/work/project/Dockerfile.image1', '-t', 'image1:1234567', '/home/user/work/project' ] skipper_runner_run_mock.assert_called_once_with(expected_command) @mock.patch('skipper.utils.get_images_from_dockerfiles', mock.MagicMock(autospec=True, return_value={ 'image1': '/home/user/work/project/Dockerfile.image1', 'image2': '/home/user/work/project/Dockerfile.image2'})) @mock.patch('__builtin__.open', mock.MagicMock(create=True)) @mock.patch('os.path.exists', mock.MagicMock(autospec=True, return_value=True)) @mock.patch('yaml.safe_load', mock.MagicMock(autospec=True, return_value=SKIPPER_CONF_WITH_CONTEXT)) @mock.patch('skipper.git.get_hash', mock.MagicMock(autospec=True, return_value='1234567')) @mock.patch('skipper.runner.run', autospec=True, return_value=0) def test_build_with_context_from_config_file(self, skipper_runner_run_mock): build_params = ['image1'] self._invoke_cli( defaults=config.load_defaults(), subcmd='build', subcmd_params=build_params ) expected_command = [ 'build', '--network=host', '--build-arg', 'TAG=1234567', '-f', '/home/user/work/project/Dockerfile.image1', '-t', 'image1:1234567', ", https://github.com/Stratoscale/skipper,dde3194bf6cf227b30c6e54b18c37317d2e0647e,tests/test_cli.py::TestCLI::test_run_without_build_container_tag_cached,tests/test_cli.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import mock import os from six.moves import http_client import unittest import click import six from click import testing from skipper import cli from skipper import config, utils REGISTRY = 'registry.io:5000' IMAGE = 'image' TAG = '1234567' FQDN_IMAGE = REGISTRY + '/' + IMAGE + ':' + TAG BUILD_CONTAINER_IMAGE = 'build-container-image' BUILD_CONTAINER_TAG = 'build-container-tag' BUILD_CONTAINER_FQDN_IMAGE = REGISTRY + '/' + BUILD_CONTAINER_IMAGE + ':' + BUILD_CONTAINER_TAG ENV = [""KEY1=VAL1"", ""KEY2=VAL2""] SKIPPER_CONF_CONTAINER_CONTEXT = '/some/context' SKIPPER_CONF_BUILD_CONTAINER_IMAGE = 'skipper-conf-build-container-image' SKIPPER_CONF_BUILD_CONTAINER_TAG = 'skipper-conf-build-container-tag' SKIPPER_CONF_BUILD_CONTAINER_FQDN_IMAGE = REGISTRY + '/' + SKIPPER_CONF_BUILD_CONTAINER_IMAGE + ':' + SKIPPER_CONF_BUILD_CONTAINER_TAG SKIPPER_CONF_MAKEFILE = 'Makefile.skipper' SKIPPER_CONF = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, } } CONFIG_ENV = { ""KEY2"": ""NOT_VAL2"", ""KEY3"": ""VAL3"", ""KEY4"": ""$VAL4"", ""KEY5"": ""$$VAL5"" } CONFIG_ENV_EVALUATION = { ""KEY2"": ""NOT_VAL2"", ""KEY3"": ""VAL3"", ""KEY4"": ""val4-evaluation"", ""KEY5"": ""$VAL5"" } SKIPPER_CONF_WITH_ENV = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'env': CONFIG_ENV } SKIPPER_CONF_WITH_ENV_LIST = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'env': ['key1=value1', 'key2'] } SKIPPER_CONF_WITH_ENV_WRONG_TYPE = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'env': 'wrong-env-type', } SKIPPER_CONF_WITH_CONTAINERS = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'containers': { 'image1': 'app1/Dockerfile', 'image2': 'app2/Dockerfile', } } SKIPPER_CONF_WITH_VOLUMES = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'volumes': [ 'volume1', 'volume2', ] } SKIPPER_CONF_WITH_WORKDIR = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'workdir': 'test-workdir' } SKIPPER_CONF_WITH_WORKSPACE = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'workspace': '/test/workspace' } SKIPPER_CONF_WITH_GIT_REV = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': 'git:revision', 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, } SKIPPER_CONF_WITH_CONTEXT = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'build-container-tag': SKIPPER_CONF_BUILD_CONTAINER_TAG, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'container-context': SKIPPER_CONF_CONTAINER_CONTEXT } SKIPPER_CONF_WITH_CONTEXT_NO_TAG = { 'registry': REGISTRY, 'build-container-image': SKIPPER_CONF_BUILD_CONTAINER_IMAGE, 'make': { 'makefile': SKIPPER_CONF_MAKEFILE, }, 'container-context': SKIPPER_CONF_CONTAINER_CONTEXT } class TestCLI(unittest.TestCase): def setUp(self): self.runtime = ""docker"" utils.CONTAINER_RUNTIME_COMMAND = self.runtime self._runner = testing.CliRunner() self.global_params = [ '--registry', REGISTRY, '--build-container-image', BUILD_CONTAINER_IMAGE, '--build-container-tag', BUILD_CONTAINER_TAG ] def test_cli_without_params(self): result = self._invoke_cli() self.assertEqual(result.exit_code, 0) def test_cli_help(self): result = self._invoke_cli(global_params=['--help']) self.assertEqual(result.exit_code, 0) def test_subcommand_help(self): for subcmd in ('build', 'push', 'make', 'run'): result = self._invoke_cli( global_params=None, subcmd=subcmd, subcmd_params=['--help'] ) self.assertEqual(result.exit_code, 0) def test_subcommand_without_global_params(self): subcmd_params_map = { 'push': [IMAGE], 'run': ['ls' '-l'], 'make': ['-f', 'Makefile', 'all'], } for subcmd, subcmd_params in six.iteritems(subcmd_params_map): result = self._invoke_cli( global_params=None, subcmd=subcmd, subcmd_params=subcmd_params, ) self.assertIsInstance(result.exception, click.BadParameter) self.assertEqual(result.exit_code, -1) @mock.patch('skipper.runner.run', autospec=True) def test_subcommand_without_subcommand_params(self, skipper_runner_run_mock): for subcmd in ('build', 'push', 'run', 'make'): result = self._invoke_cli(self.global_params, subcmd) self.assertNotEqual(result.exit_code, 0) self.assertFalse(skipper_runner_run_mock.called) @mock.patch('skipper.utils.get_images_from_dockerfiles', mock.MagicMock(autospec=True, return_value={ 'image1': '/home/user/work/project/Dockerfile.image1', 'image2': '/home/user/work/project/Dockerfile.image2'})) @mock.patch('skipper.git.get_hash', mock.MagicMock(autospec=True, return_value='1234567')) @mock.patch('os.path.exists', mock.MagicMock(autospec=True, return_value=True)) @mock.patch('skipper.runner.run', autospec=True, return_value=0) def test_build_existing_image(self, skipper_runner_run_mock): build_params = ['image1'] self._invoke_cli( global_params=self.global_params, subcmd='build', subcmd_params=build_params ) expected_command = [ 'build', '--network=host', '--build-arg', 'TAG=1234567', '-f', '/home/user/work/project/Dockerfile.image1', '-t', 'image1:1234567', '/home/user/work/project' ] skipper_runner_run_mock.assert_called_once_with(expected_command) @mock.patch('skipper.utils.get_images_from_dockerfiles', mock.MagicMock(autospec=True, return_value={ 'image1': '/home/user/work/project/Dockerfile.image1'})) @mock.patch('skipper.git.get_hash', mock.MagicMock(autospec=True, return_value='1234567')) @mock.patch('os.path.exists', mock.MagicMock(autospec=True, return_value=True)) @mock.patch('skipper.runner.run', autospec=True, return_value=0) def test_build_existing_image_with_context(self, skipper_runner_run_mock): build_params = ['image1', '--container-context', '/home/user/work/project'] self._invoke_cli( global_params=self.global_params, subcmd='build', subcmd_params=build_params ) expected_command = [ 'build', '--network=host', '--build-arg', 'TAG=1234567', '-f', '/home/user/work/project/Dockerfile.image1', '-t', 'image1:1234567', '/home/user/work/project' ] skipper_runner_run_mock.assert_called_once_with(expected_command) @mock.patch('skipper.utils.get_images_from_dockerfiles', mock.MagicMock(autospec=True, return_value={ 'image1': '/home/user/work/project/Dockerfile.image1', 'image2': '/home/user/work/project/Dockerfile.image2'})) @mock.patch('__builtin__.open', mock.MagicMock(create=True)) @mock.patch('os.path.exists', mock.MagicMock(autospec=True, return_value=True)) @mock.patch('yaml.safe_load', mock.MagicMock(autospec=True, return_value=SKIPPER_CONF_WITH_CONTEXT)) @mock.patch('skipper.git.get_hash', mock.MagicMock(autospec=True, return_value='1234567')) @mock.patch('skipper.runner.run', autospec=True, return_value=0) def test_build_with_context_from_config_file(self, skipper_runner_run_mock): build_params = ['image1'] self._invoke_cli( defaults=config.load_defaults(), subcmd='build', subcmd_params=build_params ) expected_command = [ 'build', '--network=host', '--build-arg', 'TAG=1234567', '-f', '/home/user/work/project/Dockerfile.image1', '-t', 'image1:1234567', ", https://github.com/strizhechenko/netutils-linux,dfcb0900ffffa3020656338bbab123f6da00a168,netutils_linux_tuning/test_rx_buffers.py::RxBuffersTuneTest::test_all,netutils_linux_tuning/test_rx_buffers.py,OD-Brit,flaky,nan,nan,classify;root_cause,"# coding=utf-8 import unittest from netutils_linux_tuning.rx_buffers import RxBuffersTune class RxBuffersTuneTest(unittest.TestCase): """""" Just in-memory test of evaluation rx-buffer's size. No device's settings changed. """""" dataset = { 4096: [(256, 2048), (512, 2048), (2048, 2048), (3072, 3072), (4096, 4096)], 511: ((200, 511), (511, 511), (400, 511)), 8096: ((200, 2048), (2048, 2048), (3000, 3000), (8096, 8096)) } def setUp(self): self.tune = RxBuffersTune(['test']) self.default_upper_bound = 2048 def test_all(self): for max_buffer in self.dataset: for current, expected in self.dataset[max_buffer]: self.assertEqual(self.tune.eval_prefered_size(current, max_buffer, self.default_upper_bound), expected) if __name__ == '__main__': unittest.main() ", https://github.com/summa-tx/riemann,0fda32e783487572c869aaabc6af7adba01cbd00,riemann/tests/encoding/test_addresses.py::TestAddresses::test_make_p2pkh_address,riemann/tests/encoding/test_addresses.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import unittest import riemann from riemann import utils from .. import helpers from ...encoding import addresses as addr # NB: # How to make more of these. # 1. Install python-bitcoinlib # 2. Compile a script to an array of ints (same as input to bytes class) # 3. Follow procedure below # # from bitcoin.core.script import CScript # from bitcoin.wallet import CBitcoinAddress # a = CScript(bytes.fromhex('HEX SCRIPT')) # CBitcoinAddress.from_scriptPubKey(a.to_p2sh_scriptPubKey() # # For more P2PKH addresses: # P2PKHBitcoinAddress.from_pubkey(PUBKEY_BYTES, accept_invalid=True) class TestAddresses(unittest.TestCase): def tearDown(self): riemann.select_network('bitcoin_main') def test_make_p2sh_address(self): a = addr.make_p2sh_address('OP_IF') self.assertEqual(a, helpers.OP_IF['p2sh']) def test_make_p2sh_address_msig(self): a = addr.make_p2sh_address(helpers.MSIG_2_2['redeem_script']) self.assertEqual(a, helpers.MSIG_2_2['p2sh']) def test_make_p2wsh_address(self): a = addr.make_p2wsh_address( helpers.P2WSH['human']['witnesses'][0]['wit_script']) self.assertEqual(a, helpers.P2WSH['human']['ins'][0]['addr']) def test_make_p2pkh_address(self): a = addr.make_p2pkh_address(b'\x00' * 65) self.assertEqual(a, helpers.ADDR[0]['p2pkh']) b = addr.make_p2pkh_address(b'\x11' * 65) self.assertEqual(b, helpers.ADDR[1]['p2pkh']) def test_make_p2wpkh_address(self): a = addr.make_p2wpkh_address(helpers.P2WPKH_ADDR['pubkey']) self.assertEqual(a, helpers.P2WPKH_ADDR['address']) def test_parse(self): self.assertEqual(addr.parse(helpers.OP_IF['p2sh']), b'\x05' + helpers.OP_IF['script_hash']) self.assertEqual(addr.parse(helpers.MSIG_2_2['p2sh']), b'\x05' + helpers.MSIG_2_2['script_hash']) self.assertEqual(addr.parse( helpers.P2WSH['human']['ins'][0]['addr']), b'\x00\x20' + helpers.P2WSH['ser']['ins'][0]['pk_script'][2:]) self.assertEqual(addr.parse(helpers.P2WPKH_ADDR['address']), b'\x00\x14' + helpers.P2WPKH_ADDR['pkh']) self.assertEqual(addr.parse(helpers.ADDR[0]['p2pkh']), b'\x00' + helpers.PK['ser'][0]['pkh']) with self.assertRaises(ValueError) as context: addr.parse('This is not a valid address.') self.assertIn('Unsupported address format. Got: ', str(context.exception)) def test_parse_hash(self): self.assertEqual(addr.parse_hash(helpers.OP_IF['p2sh']), helpers.OP_IF['script_hash']) self.assertEqual(addr.parse_hash(helpers.MSIG_2_2['p2sh']), helpers.MSIG_2_2['script_hash']) self.assertEqual( addr.parse_hash( helpers.P2WSH['human']['ins'][0]['addr']), helpers.P2WSH['ser']['ins'][0]['pk_script'][2:]) self.assertEqual(addr.parse_hash(helpers.P2WPKH_ADDR['address']), helpers.P2WPKH_ADDR['pkh']) self.assertEqual(addr.parse_hash(helpers.ADDR[0]['p2pkh']), helpers.PK['ser'][0]['pkh']) with self.assertRaises(ValueError) as context: addr.parse('bc1blahblahblah') self.assertIn('Unsupported address format. Got: ', str(context.exception)) # Test cash addr code riemann.select_network('bitcoin_cash_main') self.assertEqual( addr.parse_hash(helpers.OP_IF['p2sh']), helpers.OP_IF['script_hash']) self.assertEqual( addr.parse_hash(helpers.OP_IF['cashaddr']), helpers.OP_IF['script_hash']) self.assertEqual( addr.parse_hash(helpers.CASHADDR['p2pkh']), utils.hash160(helpers.CASHADDR['pubkey'])) def test_cashaddrs(self): riemann.select_network('bitcoin_cash_main') self.assertEqual( addr.make_legacy_p2sh_address('OP_IF'), helpers.OP_IF['p2sh']) self.assertEqual( addr.make_sh_address('OP_IF'), helpers.OP_IF['cashaddr']) self.assertEqual( addr.make_legacy_p2pkh_address(helpers.CASHADDR['pubkey']), helpers.CASHADDR['legacy_p2pkh']) self.assertEqual( addr.make_pkh_address(helpers.CASHADDR['pubkey']), helpers.CASHADDR['p2pkh']) def test_from_output_script(self): self.assertEqual( addr.from_output_script(helpers.OP_IF['output_script']), helpers.OP_IF['p2sh']) self.assertEqual( addr.from_output_script( helpers.P2WSH['ser']['ins'][0]['pk_script']), helpers.P2WSH['human']['ins'][0]['addr']) self.assertEqual( addr.from_output_script(helpers.PK['ser'][0]['pkh_output']), helpers.ADDR[0]['p2pkh']) self.assertEqual( addr.from_output_script(helpers.P2WPKH_ADDR['output']), helpers.P2WPKH_ADDR['address']) with self.assertRaises(ValueError) as context: addr.from_output_script(b'\x8e' * 34) self.assertIn( 'Cannot parse address from script.', str(context.exception)) def test_cashaddr_from_output_script(self): riemann.select_network('bitcoin_cash_main') self.assertEqual( addr.from_output_script(helpers.PK['ser'][0]['pkh_output']), helpers.ADDR[0]['p2pkh_cashaddr']) self.assertEqual( addr.from_output_script(helpers.OP_IF['output_script']), helpers.OP_IF['cashaddr']) def test_to_output_script(self): self.assertEqual( addr.to_output_script(helpers.OP_IF['p2sh']), helpers.OP_IF['output_script']) self.assertEqual( addr.to_output_script(helpers.P2WSH['human']['ins'][0]['addr']), helpers.P2WSH['ser']['ins'][0]['pk_script']) self.assertEqual( addr.to_output_script(helpers.ADDR[0]['p2pkh']), helpers.PK['ser'][0]['pkh_output']) self.assertEqual( addr.to_output_script(helpers.P2WPKH_ADDR['address']), helpers.P2WPKH_ADDR['output']) with self.assertRaises(ValueError) as context: # Junk B58 w valid checksum addr.to_output_script('1111111111111111111111111111111111177fdsQ') self.assertIn( 'Cannot parse output script from address.', str(context.exception)) def test_cashaddr_to_output_script(self): riemann.select_network('bitcoin_cash_main') self.assertEqual( addr.to_output_script(helpers.OP_IF['cashaddr']), helpers.OP_IF['output_script']) self.assertEqual( addr.to_output_script(helpers.ADDR[0]['p2pkh_cashaddr']), helpers.PK['ser'][0]['pkh_output']) ", https://github.com/summa-tx/riemann,0fda32e783487572c869aaabc6af7adba01cbd00,riemann/tests/encoding/test_addresses.py::TestAddresses::test_make_p2sh_address_msig,riemann/tests/encoding/test_addresses.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import riemann from riemann import utils from .. import helpers from ...encoding import addresses as addr # NB: # How to make more of these. # 1. Install python-bitcoinlib # 2. Compile a script to an array of ints (same as input to bytes class) # 3. Follow procedure below # # from bitcoin.core.script import CScript # from bitcoin.wallet import CBitcoinAddress # a = CScript(bytes.fromhex('HEX SCRIPT')) # CBitcoinAddress.from_scriptPubKey(a.to_p2sh_scriptPubKey() # # For more P2PKH addresses: # P2PKHBitcoinAddress.from_pubkey(PUBKEY_BYTES, accept_invalid=True) class TestAddresses(unittest.TestCase): def tearDown(self): riemann.select_network('bitcoin_main') def test_make_p2sh_address(self): a = addr.make_p2sh_address('OP_IF') self.assertEqual(a, helpers.OP_IF['p2sh']) def test_make_p2sh_address_msig(self): a = addr.make_p2sh_address(helpers.MSIG_2_2['redeem_script']) self.assertEqual(a, helpers.MSIG_2_2['p2sh']) def test_make_p2wsh_address(self): a = addr.make_p2wsh_address( helpers.P2WSH['human']['witnesses'][0]['wit_script']) self.assertEqual(a, helpers.P2WSH['human']['ins'][0]['addr']) def test_make_p2pkh_address(self): a = addr.make_p2pkh_address(b'\x00' * 65) self.assertEqual(a, helpers.ADDR[0]['p2pkh']) b = addr.make_p2pkh_address(b'\x11' * 65) self.assertEqual(b, helpers.ADDR[1]['p2pkh']) def test_make_p2wpkh_address(self): a = addr.make_p2wpkh_address(helpers.P2WPKH_ADDR['pubkey']) self.assertEqual(a, helpers.P2WPKH_ADDR['address']) def test_parse(self): self.assertEqual(addr.parse(helpers.OP_IF['p2sh']), b'\x05' + helpers.OP_IF['script_hash']) self.assertEqual(addr.parse(helpers.MSIG_2_2['p2sh']), b'\x05' + helpers.MSIG_2_2['script_hash']) self.assertEqual(addr.parse( helpers.P2WSH['human']['ins'][0]['addr']), b'\x00\x20' + helpers.P2WSH['ser']['ins'][0]['pk_script'][2:]) self.assertEqual(addr.parse(helpers.P2WPKH_ADDR['address']), b'\x00\x14' + helpers.P2WPKH_ADDR['pkh']) self.assertEqual(addr.parse(helpers.ADDR[0]['p2pkh']), b'\x00' + helpers.PK['ser'][0]['pkh']) with self.assertRaises(ValueError) as context: addr.parse('This is not a valid address.') self.assertIn('Unsupported address format. Got: ', str(context.exception)) def test_parse_hash(self): self.assertEqual(addr.parse_hash(helpers.OP_IF['p2sh']), helpers.OP_IF['script_hash']) self.assertEqual(addr.parse_hash(helpers.MSIG_2_2['p2sh']), helpers.MSIG_2_2['script_hash']) self.assertEqual( addr.parse_hash( helpers.P2WSH['human']['ins'][0]['addr']), helpers.P2WSH['ser']['ins'][0]['pk_script'][2:]) self.assertEqual(addr.parse_hash(helpers.P2WPKH_ADDR['address']), helpers.P2WPKH_ADDR['pkh']) self.assertEqual(addr.parse_hash(helpers.ADDR[0]['p2pkh']), helpers.PK['ser'][0]['pkh']) with self.assertRaises(ValueError) as context: addr.parse('bc1blahblahblah') self.assertIn('Unsupported address format. Got: ', str(context.exception)) # Test cash addr code riemann.select_network('bitcoin_cash_main') self.assertEqual( addr.parse_hash(helpers.OP_IF['p2sh']), helpers.OP_IF['script_hash']) self.assertEqual( addr.parse_hash(helpers.OP_IF['cashaddr']), helpers.OP_IF['script_hash']) self.assertEqual( addr.parse_hash(helpers.CASHADDR['p2pkh']), utils.hash160(helpers.CASHADDR['pubkey'])) def test_cashaddrs(self): riemann.select_network('bitcoin_cash_main') self.assertEqual( addr.make_legacy_p2sh_address('OP_IF'), helpers.OP_IF['p2sh']) self.assertEqual( addr.make_sh_address('OP_IF'), helpers.OP_IF['cashaddr']) self.assertEqual( addr.make_legacy_p2pkh_address(helpers.CASHADDR['pubkey']), helpers.CASHADDR['legacy_p2pkh']) self.assertEqual( addr.make_pkh_address(helpers.CASHADDR['pubkey']), helpers.CASHADDR['p2pkh']) def test_from_output_script(self): self.assertEqual( addr.from_output_script(helpers.OP_IF['output_script']), helpers.OP_IF['p2sh']) self.assertEqual( addr.from_output_script( helpers.P2WSH['ser']['ins'][0]['pk_script']), helpers.P2WSH['human']['ins'][0]['addr']) self.assertEqual( addr.from_output_script(helpers.PK['ser'][0]['pkh_output']), helpers.ADDR[0]['p2pkh']) self.assertEqual( addr.from_output_script(helpers.P2WPKH_ADDR['output']), helpers.P2WPKH_ADDR['address']) with self.assertRaises(ValueError) as context: addr.from_output_script(b'\x8e' * 34) self.assertIn( 'Cannot parse address from script.', str(context.exception)) def test_cashaddr_from_output_script(self): riemann.select_network('bitcoin_cash_main') self.assertEqual( addr.from_output_script(helpers.PK['ser'][0]['pkh_output']), helpers.ADDR[0]['p2pkh_cashaddr']) self.assertEqual( addr.from_output_script(helpers.OP_IF['output_script']), helpers.OP_IF['cashaddr']) def test_to_output_script(self): self.assertEqual( addr.to_output_script(helpers.OP_IF['p2sh']), helpers.OP_IF['output_script']) self.assertEqual( addr.to_output_script(helpers.P2WSH['human']['ins'][0]['addr']), helpers.P2WSH['ser']['ins'][0]['pk_script']) self.assertEqual( addr.to_output_script(helpers.ADDR[0]['p2pkh']), helpers.PK['ser'][0]['pkh_output']) self.assertEqual( addr.to_output_script(helpers.P2WPKH_ADDR['address']), helpers.P2WPKH_ADDR['output']) with self.assertRaises(ValueError) as context: # Junk B58 w valid checksum addr.to_output_script('1111111111111111111111111111111111177fdsQ') self.assertIn( 'Cannot parse output script from address.', str(context.exception)) def test_cashaddr_to_output_script(self): riemann.select_network('bitcoin_cash_main') self.assertEqual( addr.to_output_script(helpers.OP_IF['cashaddr']), helpers.OP_IF['output_script']) self.assertEqual( addr.to_output_script(helpers.ADDR[0]['p2pkh_cashaddr']), helpers.PK['ser'][0]['pkh_output']) ", https://github.com/summa-tx/riemann,0fda32e783487572c869aaabc6af7adba01cbd00,riemann/tests/encoding/test_addresses.py::TestAddresses::test_make_p2wsh_address,riemann/tests/encoding/test_addresses.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import unittest import riemann from riemann import utils from .. import helpers from ...encoding import addresses as addr # NB: # How to make more of these. # 1. Install python-bitcoinlib # 2. Compile a script to an array of ints (same as input to bytes class) # 3. Follow procedure below # # from bitcoin.core.script import CScript # from bitcoin.wallet import CBitcoinAddress # a = CScript(bytes.fromhex('HEX SCRIPT')) # CBitcoinAddress.from_scriptPubKey(a.to_p2sh_scriptPubKey() # # For more P2PKH addresses: # P2PKHBitcoinAddress.from_pubkey(PUBKEY_BYTES, accept_invalid=True) class TestAddresses(unittest.TestCase): def tearDown(self): riemann.select_network('bitcoin_main') def test_make_p2sh_address(self): a = addr.make_p2sh_address('OP_IF') self.assertEqual(a, helpers.OP_IF['p2sh']) def test_make_p2sh_address_msig(self): a = addr.make_p2sh_address(helpers.MSIG_2_2['redeem_script']) self.assertEqual(a, helpers.MSIG_2_2['p2sh']) def test_make_p2wsh_address(self): a = addr.make_p2wsh_address( helpers.P2WSH['human']['witnesses'][0]['wit_script']) self.assertEqual(a, helpers.P2WSH['human']['ins'][0]['addr']) def test_make_p2pkh_address(self): a = addr.make_p2pkh_address(b'\x00' * 65) self.assertEqual(a, helpers.ADDR[0]['p2pkh']) b = addr.make_p2pkh_address(b'\x11' * 65) self.assertEqual(b, helpers.ADDR[1]['p2pkh']) def test_make_p2wpkh_address(self): a = addr.make_p2wpkh_address(helpers.P2WPKH_ADDR['pubkey']) self.assertEqual(a, helpers.P2WPKH_ADDR['address']) def test_parse(self): self.assertEqual(addr.parse(helpers.OP_IF['p2sh']), b'\x05' + helpers.OP_IF['script_hash']) self.assertEqual(addr.parse(helpers.MSIG_2_2['p2sh']), b'\x05' + helpers.MSIG_2_2['script_hash']) self.assertEqual(addr.parse( helpers.P2WSH['human']['ins'][0]['addr']), b'\x00\x20' + helpers.P2WSH['ser']['ins'][0]['pk_script'][2:]) self.assertEqual(addr.parse(helpers.P2WPKH_ADDR['address']), b'\x00\x14' + helpers.P2WPKH_ADDR['pkh']) self.assertEqual(addr.parse(helpers.ADDR[0]['p2pkh']), b'\x00' + helpers.PK['ser'][0]['pkh']) with self.assertRaises(ValueError) as context: addr.parse('This is not a valid address.') self.assertIn('Unsupported address format. Got: ', str(context.exception)) def test_parse_hash(self): self.assertEqual(addr.parse_hash(helpers.OP_IF['p2sh']), helpers.OP_IF['script_hash']) self.assertEqual(addr.parse_hash(helpers.MSIG_2_2['p2sh']), helpers.MSIG_2_2['script_hash']) self.assertEqual( addr.parse_hash( helpers.P2WSH['human']['ins'][0]['addr']), helpers.P2WSH['ser']['ins'][0]['pk_script'][2:]) self.assertEqual(addr.parse_hash(helpers.P2WPKH_ADDR['address']), helpers.P2WPKH_ADDR['pkh']) self.assertEqual(addr.parse_hash(helpers.ADDR[0]['p2pkh']), helpers.PK['ser'][0]['pkh']) with self.assertRaises(ValueError) as context: addr.parse('bc1blahblahblah') self.assertIn('Unsupported address format. Got: ', str(context.exception)) # Test cash addr code riemann.select_network('bitcoin_cash_main') self.assertEqual( addr.parse_hash(helpers.OP_IF['p2sh']), helpers.OP_IF['script_hash']) self.assertEqual( addr.parse_hash(helpers.OP_IF['cashaddr']), helpers.OP_IF['script_hash']) self.assertEqual( addr.parse_hash(helpers.CASHADDR['p2pkh']), utils.hash160(helpers.CASHADDR['pubkey'])) def test_cashaddrs(self): riemann.select_network('bitcoin_cash_main') self.assertEqual( addr.make_legacy_p2sh_address('OP_IF'), helpers.OP_IF['p2sh']) self.assertEqual( addr.make_sh_address('OP_IF'), helpers.OP_IF['cashaddr']) self.assertEqual( addr.make_legacy_p2pkh_address(helpers.CASHADDR['pubkey']), helpers.CASHADDR['legacy_p2pkh']) self.assertEqual( addr.make_pkh_address(helpers.CASHADDR['pubkey']), helpers.CASHADDR['p2pkh']) def test_from_output_script(self): self.assertEqual( addr.from_output_script(helpers.OP_IF['output_script']), helpers.OP_IF['p2sh']) self.assertEqual( addr.from_output_script( helpers.P2WSH['ser']['ins'][0]['pk_script']), helpers.P2WSH['human']['ins'][0]['addr']) self.assertEqual( addr.from_output_script(helpers.PK['ser'][0]['pkh_output']), helpers.ADDR[0]['p2pkh']) self.assertEqual( addr.from_output_script(helpers.P2WPKH_ADDR['output']), helpers.P2WPKH_ADDR['address']) with self.assertRaises(ValueError) as context: addr.from_output_script(b'\x8e' * 34) self.assertIn( 'Cannot parse address from script.', str(context.exception)) def test_cashaddr_from_output_script(self): riemann.select_network('bitcoin_cash_main') self.assertEqual( addr.from_output_script(helpers.PK['ser'][0]['pkh_output']), helpers.ADDR[0]['p2pkh_cashaddr']) self.assertEqual( addr.from_output_script(helpers.OP_IF['output_script']), helpers.OP_IF['cashaddr']) def test_to_output_script(self): self.assertEqual( addr.to_output_script(helpers.OP_IF['p2sh']), helpers.OP_IF['output_script']) self.assertEqual( addr.to_output_script(helpers.P2WSH['human']['ins'][0]['addr']), helpers.P2WSH['ser']['ins'][0]['pk_script']) self.assertEqual( addr.to_output_script(helpers.ADDR[0]['p2pkh']), helpers.PK['ser'][0]['pkh_output']) self.assertEqual( addr.to_output_script(helpers.P2WPKH_ADDR['address']), helpers.P2WPKH_ADDR['output']) with self.assertRaises(ValueError) as context: # Junk B58 w valid checksum addr.to_output_script('1111111111111111111111111111111111177fdsQ') self.assertIn( 'Cannot parse output script from address.', str(context.exception)) def test_cashaddr_to_output_script(self): riemann.select_network('bitcoin_cash_main') self.assertEqual( addr.to_output_script(helpers.OP_IF['cashaddr']), helpers.OP_IF['output_script']) self.assertEqual( addr.to_output_script(helpers.ADDR[0]['p2pkh_cashaddr']), helpers.PK['ser'][0]['pkh_output']) ", https://github.com/summa-tx/riemann,0fda32e783487572c869aaabc6af7adba01cbd00,riemann/tests/test_simple.py::TestSimple::test_output,riemann/tests/test_simple.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import unittest import riemann from riemann import simple from riemann import tx as txn from riemann.tests import helpers class TestSimple(unittest.TestCase): def setUp(self): pass def tearDown(self): riemann.select_network('bitcoin_main') def test_output(self): for i in range(len(helpers.P2WSH['human']['outs'])): self.assertEqual( simple.output( value=helpers.P2WSH['human']['outs'][i]['value'], address=helpers.P2WSH['human']['outs'][i]['addr']), helpers.P2WSH['ser']['outs'][i]['output']) def test_outpoint(self): self.assertEqual( simple.outpoint( tx_id=helpers.P2PKH['human']['ins'][0]['hash'], index=helpers.P2PKH['human']['ins'][0]['index']), helpers.P2PKH['ser']['ins'][0]['outpoint']) def test_unsigned_input(self): outpoint = simple.outpoint( tx_id=helpers.P2PKH['human']['ins'][0]['hash'], index=helpers.P2PKH['human']['ins'][0]['index']) self.assertEqual( simple.unsigned_input( outpoint=outpoint), outpoint.to_bytes() + b'\x00' + b'\xFE\xFF\xFF\xFF') self.assertEqual( simple.unsigned_input( outpoint=outpoint, sequence=0x1234abcd), outpoint.to_bytes() + b'\x00' + b'\xcd\xab\x34\x12') def test_unsigned_legacy_tx(self): outpoint = simple.outpoint( tx_id=helpers.P2PKH['human']['ins'][0]['hash'], index=helpers.P2PKH['human']['ins'][0]['index']) tx_in = simple.unsigned_input( outpoint=outpoint, sequence=helpers.P2PKH['human']['ins'][0]['sequence']) tx_out = simple.output( helpers.P2PKH['human']['outs'][0]['value'], helpers.P2PKH['human']['outs'][0]['addr']) tx_return_output = txn.make_op_return_output( helpers.P2PKH['human']['outs'][1]['memo']) tx = simple.unsigned_legacy_tx( tx_ins=[tx_in], tx_outs=[tx_out, tx_return_output]) self.assertEqual(tx, helpers.P2PKH['ser']['tx']['unsigned']) def test_unsigned_witness_tx(self): outpoint = simple.outpoint( tx_id=helpers.P2WPKH['human']['ins'][0]['hash'], index=helpers.P2WPKH['human']['ins'][0]['index']) tx_in = simple.unsigned_input( outpoint=outpoint, sequence=helpers.P2WPKH['human']['ins'][0]['sequence']) tx_out = simple.output( helpers.P2WPKH['human']['outs'][0]['value'], helpers.P2WPKH['human']['outs'][0]['addr']) tx = simple.unsigned_witness_tx( tx_ins=[tx_in], tx_outs=[tx_out], lock_time=helpers.P2WPKH['human']['locktime']) self.assertEqual(tx, helpers.P2WPKH['ser']['tx']['unsigned']) ", https://github.com/summa-tx/riemann,0fda32e783487572c869aaabc6af7adba01cbd00,riemann/tests/test_simple.py::TestSimple::test_unsigned_legacy_tx,riemann/tests/test_simple.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import unittest import riemann from riemann import simple from riemann import tx as txn from riemann.tests import helpers class TestSimple(unittest.TestCase): def setUp(self): pass def tearDown(self): riemann.select_network('bitcoin_main') def test_output(self): for i in range(len(helpers.P2WSH['human']['outs'])): self.assertEqual( simple.output( value=helpers.P2WSH['human']['outs'][i]['value'], address=helpers.P2WSH['human']['outs'][i]['addr']), helpers.P2WSH['ser']['outs'][i]['output']) def test_outpoint(self): self.assertEqual( simple.outpoint( tx_id=helpers.P2PKH['human']['ins'][0]['hash'], index=helpers.P2PKH['human']['ins'][0]['index']), helpers.P2PKH['ser']['ins'][0]['outpoint']) def test_unsigned_input(self): outpoint = simple.outpoint( tx_id=helpers.P2PKH['human']['ins'][0]['hash'], index=helpers.P2PKH['human']['ins'][0]['index']) self.assertEqual( simple.unsigned_input( outpoint=outpoint), outpoint.to_bytes() + b'\x00' + b'\xFE\xFF\xFF\xFF') self.assertEqual( simple.unsigned_input( outpoint=outpoint, sequence=0x1234abcd), outpoint.to_bytes() + b'\x00' + b'\xcd\xab\x34\x12') def test_unsigned_legacy_tx(self): outpoint = simple.outpoint( tx_id=helpers.P2PKH['human']['ins'][0]['hash'], index=helpers.P2PKH['human']['ins'][0]['index']) tx_in = simple.unsigned_input( outpoint=outpoint, sequence=helpers.P2PKH['human']['ins'][0]['sequence']) tx_out = simple.output( helpers.P2PKH['human']['outs'][0]['value'], helpers.P2PKH['human']['outs'][0]['addr']) tx_return_output = txn.make_op_return_output( helpers.P2PKH['human']['outs'][1]['memo']) tx = simple.unsigned_legacy_tx( tx_ins=[tx_in], tx_outs=[tx_out, tx_return_output]) self.assertEqual(tx, helpers.P2PKH['ser']['tx']['unsigned']) def test_unsigned_witness_tx(self): outpoint = simple.outpoint( tx_id=helpers.P2WPKH['human']['ins'][0]['hash'], index=helpers.P2WPKH['human']['ins'][0]['index']) tx_in = simple.unsigned_input( outpoint=outpoint, sequence=helpers.P2WPKH['human']['ins'][0]['sequence']) tx_out = simple.output( helpers.P2WPKH['human']['outs'][0]['value'], helpers.P2WPKH['human']['outs'][0]['addr']) tx = simple.unsigned_witness_tx( tx_ins=[tx_in], tx_outs=[tx_out], lock_time=helpers.P2WPKH['human']['locktime']) self.assertEqual(tx, helpers.P2WPKH['ser']['tx']['unsigned']) ", https://github.com/summa-tx/riemann,0fda32e783487572c869aaabc6af7adba01cbd00,riemann/tests/test_simple.py::TestSimple::test_unsigned_witness_tx,riemann/tests/test_simple.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import riemann from riemann import simple from riemann import tx as txn from riemann.tests import helpers class TestSimple(unittest.TestCase): def setUp(self): pass def tearDown(self): riemann.select_network('bitcoin_main') def test_output(self): for i in range(len(helpers.P2WSH['human']['outs'])): self.assertEqual( simple.output( value=helpers.P2WSH['human']['outs'][i]['value'], address=helpers.P2WSH['human']['outs'][i]['addr']), helpers.P2WSH['ser']['outs'][i]['output']) def test_outpoint(self): self.assertEqual( simple.outpoint( tx_id=helpers.P2PKH['human']['ins'][0]['hash'], index=helpers.P2PKH['human']['ins'][0]['index']), helpers.P2PKH['ser']['ins'][0]['outpoint']) def test_unsigned_input(self): outpoint = simple.outpoint( tx_id=helpers.P2PKH['human']['ins'][0]['hash'], index=helpers.P2PKH['human']['ins'][0]['index']) self.assertEqual( simple.unsigned_input( outpoint=outpoint), outpoint.to_bytes() + b'\x00' + b'\xFE\xFF\xFF\xFF') self.assertEqual( simple.unsigned_input( outpoint=outpoint, sequence=0x1234abcd), outpoint.to_bytes() + b'\x00' + b'\xcd\xab\x34\x12') def test_unsigned_legacy_tx(self): outpoint = simple.outpoint( tx_id=helpers.P2PKH['human']['ins'][0]['hash'], index=helpers.P2PKH['human']['ins'][0]['index']) tx_in = simple.unsigned_input( outpoint=outpoint, sequence=helpers.P2PKH['human']['ins'][0]['sequence']) tx_out = simple.output( helpers.P2PKH['human']['outs'][0]['value'], helpers.P2PKH['human']['outs'][0]['addr']) tx_return_output = txn.make_op_return_output( helpers.P2PKH['human']['outs'][1]['memo']) tx = simple.unsigned_legacy_tx( tx_ins=[tx_in], tx_outs=[tx_out, tx_return_output]) self.assertEqual(tx, helpers.P2PKH['ser']['tx']['unsigned']) def test_unsigned_witness_tx(self): outpoint = simple.outpoint( tx_id=helpers.P2WPKH['human']['ins'][0]['hash'], index=helpers.P2WPKH['human']['ins'][0]['index']) tx_in = simple.unsigned_input( outpoint=outpoint, sequence=helpers.P2WPKH['human']['ins'][0]['sequence']) tx_out = simple.output( helpers.P2WPKH['human']['outs'][0]['value'], helpers.P2WPKH['human']['outs'][0]['addr']) tx = simple.unsigned_witness_tx( tx_ins=[tx_in], tx_outs=[tx_out], lock_time=helpers.P2WPKH['human']['locktime']) self.assertEqual(tx, helpers.P2WPKH['ser']['tx']['unsigned']) ", https://github.com/summa-tx/riemann,0fda32e783487572c869aaabc6af7adba01cbd00,riemann/tests/tx/test_tx_builder.py::TestTxBuilder::test_make_p2wpkh_output_script,riemann/tests/tx/test_tx_builder.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import unittest import riemann from unittest import mock from riemann.tests import helpers from riemann.tx import tx_builder as tb class TestTxBuilder(unittest.TestCase): def setUp(self): pass def tearDown(self): riemann.select_network('bitcoin_main') def test_make_sh_output_script(self): self.assertEqual( tb.make_sh_output_script('OP_IF'), helpers.OP_IF['output_script']) self.assertEqual( tb.make_sh_output_script( helpers.P2WSH['human']['witnesses'][0]['wit_script'], witness=True), helpers.P2WSH['ser']['ins'][0]['pk_script']) riemann.select_network('bitcoin_cash_main') with self.assertRaises(ValueError) as context: tb.make_sh_output_script( helpers.P2WSH['human']['witnesses'][0]['wit_script'], witness=True) self.assertIn( 'Network bitcoin_cash_main does not support witness scripts.', str(context.exception)) def test_make_pkh_output_script(self): self.assertEqual( tb.make_pkh_output_script(helpers.PK['ser'][0]['pk']), helpers.PK['ser'][0]['pkh_output']) self.assertEqual( tb.make_pkh_output_script( helpers.PK['ser'][0]['pk'], witness=True), helpers.PK['ser'][0]['pkh_p2wpkh_output']) riemann.select_network('bitcoin_cash_main') with self.assertRaises(ValueError) as context: tb.make_pkh_output_script(helpers.PK['ser'][0]['pk'], witness=True) self.assertIn( 'Network bitcoin_cash_main does not support witness scripts.', str(context.exception)) with self.assertRaises(ValueError) as context: tb.make_pkh_output_script('hello world') self.assertIn( 'Unknown pubkey format. Expected bytes. Got: ', str(context.exception)) def test_make_p2sh_output_script(self): self.assertEqual( tb.make_p2sh_output_script('OP_IF'), helpers.OP_IF['output_script']) def test_make_p2pkh_output_script(self): self.assertEqual( tb.make_p2pkh_output_script(helpers.PK['ser'][0]['pk']), helpers.PK['ser'][0]['pkh_output']) def test_make_p2wsh_output_script(self): self.assertEqual( tb.make_p2wsh_output_script( helpers.P2WSH['human']['witnesses'][0]['wit_script']), helpers.P2WSH['ser']['ins'][0]['pk_script']) def test_make_p2wpkh_output_script(self): self.assertEqual( tb.make_p2wpkh_output_script(helpers.PK['ser'][0]['pk']), helpers.PK['ser'][0]['pkh_p2wpkh_output']) def test_make_sh_output(self): pass # covered by next two def test_make_p2sh_output(self): self.assertEqual( tb.make_p2sh_output( value=helpers.P2PKH1['human']['outs'][0]['value'], output_script='OP_IF'), helpers.OP_IF['output']) def test_make_p2wsh_output(self): helper_witness = helpers.P2WSH['human']['witnesses'][0] self.assertEqual( tb.make_p2wsh_output( value=helpers.P2WSH['human']['outs'][3]['value'], output_script=helper_witness['wit_script']), helpers.P2WSH['ser']['outs'][3]['output']) def test_make_pkh_output(self): pass # covered by next 2 def test_make_p2pkh_output(self): self.assertEqual( tb.make_p2pkh_output( value=helpers.P2PKH1['human']['outs'][0]['value'], pubkey=helpers.PK['ser'][0]['pk']), helpers.PK['ser'][0]['pk_p2pkh_output']) def test_make_p2wpkh_output(self): self.assertEqual( tb.make_p2wpkh_output( value=helpers.P2PKH1['human']['outs'][0]['value'], pubkey=helpers.PK['ser'][0]['pk']), helpers.PK['ser'][0]['pk_p2wpkh_output']) def test_make_op_return_output_error(self): with self.assertRaises(ValueError) as context: tb.make_op_return_output(b'\x00' * 78) self.assertIn( 'Data is too long. Expected <= 77 bytes', str(context.exception)) def test_make_empty_witness(self): pass def test_make_witness_stack_item(self): pass def test_make_witness(self): pass def test_make_outpoint(self): outpoint = tb.make_outpoint( tx_id_le=helpers.P2PKH1['ser']['ins'][0]['hash'], index=helpers.P2PKH1['human']['ins'][0]['index']) self.assertEqual( outpoint, helpers.P2PKH1['ser']['ins'][0]['outpoint']) def test_make_script_sig(self): tx_in = helpers.P2SH_PD1['human']['ins'][0] self.assertEqual( tb.make_script_sig( stack_script=tx_in['stack_script'], redeem_script=tx_in['redeem_script']), helpers.P2SH_PD1['ser']['ins'][0]['script_sig']) def test_make_legacy_input(self): outpoint = tb.make_outpoint( tx_id_le=helpers.P2PKH1['ser']['ins'][0]['hash'], index=helpers.P2PKH1['human']['ins'][0]['index']) tx_in = tb.make_legacy_input( outpoint=outpoint, stack_script=helpers.P2PKH1['ser']['ins'][0]['stack_script'], redeem_script=helpers.P2PKH1['ser']['ins'][0]['redeem_script'], sequence=helpers.P2PKH1['human']['ins'][0]['sequence']) self.assertEqual(tx_in, helpers.P2PKH1['ser']['tx']['in']) def test_make_legacy_input_and_empty_witness(self): pass def test_make_witness_input_and_witness(self): pass def test_length_prepend(self): self.assertEqual( tb.length_prepend(b'\x00'), b'\x01\x00') self.assertEqual( tb.length_prepend(b'\x00' * 99), b'\x63' + b'\x00' * 99) self.assertEqual( tb.length_prepend(b'\x00' * 256), b'\xfd\x00\x01' + b'\x00' * 256) @mock.patch('riemann.tx.tx_builder.sprout') def test_sprout_snowflake(self, mock_tx): # TODO: Improve riemann.select_network('zcash_sprout_main') mock_tx.SproutTx.return_value = 0 self.assertEqual( tb.make_tx(0, 0, 0, 0, tx_joinsplits=[]), 0) @mock.patch('riemann.tx.tx_builder.overwinter') def test_overwinter_snowflake(self, mock_tx): # TODO: Improve riemann.select_network('zcash_overwinter_main') mock_tx.OverwinterTx.return_value = 0 self.assertEqual( tb.make_tx(0, 0, 0, 0, expiry=0), 0) ", https://github.com/summa-tx/riemann,0fda32e783487572c869aaabc6af7adba01cbd00,riemann/tests/tx/test_tx_builder.py::TestTxBuilder::test_make_pkh_output_script,riemann/tests/tx/test_tx_builder.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import riemann from unittest import mock from riemann.tests import helpers from riemann.tx import tx_builder as tb class TestTxBuilder(unittest.TestCase): def setUp(self): pass def tearDown(self): riemann.select_network('bitcoin_main') def test_make_sh_output_script(self): self.assertEqual( tb.make_sh_output_script('OP_IF'), helpers.OP_IF['output_script']) self.assertEqual( tb.make_sh_output_script( helpers.P2WSH['human']['witnesses'][0]['wit_script'], witness=True), helpers.P2WSH['ser']['ins'][0]['pk_script']) riemann.select_network('bitcoin_cash_main') with self.assertRaises(ValueError) as context: tb.make_sh_output_script( helpers.P2WSH['human']['witnesses'][0]['wit_script'], witness=True) self.assertIn( 'Network bitcoin_cash_main does not support witness scripts.', str(context.exception)) def test_make_pkh_output_script(self): self.assertEqual( tb.make_pkh_output_script(helpers.PK['ser'][0]['pk']), helpers.PK['ser'][0]['pkh_output']) self.assertEqual( tb.make_pkh_output_script( helpers.PK['ser'][0]['pk'], witness=True), helpers.PK['ser'][0]['pkh_p2wpkh_output']) riemann.select_network('bitcoin_cash_main') with self.assertRaises(ValueError) as context: tb.make_pkh_output_script(helpers.PK['ser'][0]['pk'], witness=True) self.assertIn( 'Network bitcoin_cash_main does not support witness scripts.', str(context.exception)) with self.assertRaises(ValueError) as context: tb.make_pkh_output_script('hello world') self.assertIn( 'Unknown pubkey format. Expected bytes. Got: ', str(context.exception)) def test_make_p2sh_output_script(self): self.assertEqual( tb.make_p2sh_output_script('OP_IF'), helpers.OP_IF['output_script']) def test_make_p2pkh_output_script(self): self.assertEqual( tb.make_p2pkh_output_script(helpers.PK['ser'][0]['pk']), helpers.PK['ser'][0]['pkh_output']) def test_make_p2wsh_output_script(self): self.assertEqual( tb.make_p2wsh_output_script( helpers.P2WSH['human']['witnesses'][0]['wit_script']), helpers.P2WSH['ser']['ins'][0]['pk_script']) def test_make_p2wpkh_output_script(self): self.assertEqual( tb.make_p2wpkh_output_script(helpers.PK['ser'][0]['pk']), helpers.PK['ser'][0]['pkh_p2wpkh_output']) def test_make_sh_output(self): pass # covered by next two def test_make_p2sh_output(self): self.assertEqual( tb.make_p2sh_output( value=helpers.P2PKH1['human']['outs'][0]['value'], output_script='OP_IF'), helpers.OP_IF['output']) def test_make_p2wsh_output(self): helper_witness = helpers.P2WSH['human']['witnesses'][0] self.assertEqual( tb.make_p2wsh_output( value=helpers.P2WSH['human']['outs'][3]['value'], output_script=helper_witness['wit_script']), helpers.P2WSH['ser']['outs'][3]['output']) def test_make_pkh_output(self): pass # covered by next 2 def test_make_p2pkh_output(self): self.assertEqual( tb.make_p2pkh_output( value=helpers.P2PKH1['human']['outs'][0]['value'], pubkey=helpers.PK['ser'][0]['pk']), helpers.PK['ser'][0]['pk_p2pkh_output']) def test_make_p2wpkh_output(self): self.assertEqual( tb.make_p2wpkh_output( value=helpers.P2PKH1['human']['outs'][0]['value'], pubkey=helpers.PK['ser'][0]['pk']), helpers.PK['ser'][0]['pk_p2wpkh_output']) def test_make_op_return_output_error(self): with self.assertRaises(ValueError) as context: tb.make_op_return_output(b'\x00' * 78) self.assertIn( 'Data is too long. Expected <= 77 bytes', str(context.exception)) def test_make_empty_witness(self): pass def test_make_witness_stack_item(self): pass def test_make_witness(self): pass def test_make_outpoint(self): outpoint = tb.make_outpoint( tx_id_le=helpers.P2PKH1['ser']['ins'][0]['hash'], index=helpers.P2PKH1['human']['ins'][0]['index']) self.assertEqual( outpoint, helpers.P2PKH1['ser']['ins'][0]['outpoint']) def test_make_script_sig(self): tx_in = helpers.P2SH_PD1['human']['ins'][0] self.assertEqual( tb.make_script_sig( stack_script=tx_in['stack_script'], redeem_script=tx_in['redeem_script']), helpers.P2SH_PD1['ser']['ins'][0]['script_sig']) def test_make_legacy_input(self): outpoint = tb.make_outpoint( tx_id_le=helpers.P2PKH1['ser']['ins'][0]['hash'], index=helpers.P2PKH1['human']['ins'][0]['index']) tx_in = tb.make_legacy_input( outpoint=outpoint, stack_script=helpers.P2PKH1['ser']['ins'][0]['stack_script'], redeem_script=helpers.P2PKH1['ser']['ins'][0]['redeem_script'], sequence=helpers.P2PKH1['human']['ins'][0]['sequence']) self.assertEqual(tx_in, helpers.P2PKH1['ser']['tx']['in']) def test_make_legacy_input_and_empty_witness(self): pass def test_make_witness_input_and_witness(self): pass def test_length_prepend(self): self.assertEqual( tb.length_prepend(b'\x00'), b'\x01\x00') self.assertEqual( tb.length_prepend(b'\x00' * 99), b'\x63' + b'\x00' * 99) self.assertEqual( tb.length_prepend(b'\x00' * 256), b'\xfd\x00\x01' + b'\x00' * 256) @mock.patch('riemann.tx.tx_builder.sprout') def test_sprout_snowflake(self, mock_tx): # TODO: Improve riemann.select_network('zcash_sprout_main') mock_tx.SproutTx.return_value = 0 self.assertEqual( tb.make_tx(0, 0, 0, 0, tx_joinsplits=[]), 0) @mock.patch('riemann.tx.tx_builder.overwinter') def test_overwinter_snowflake(self, mock_tx): # TODO: Improve riemann.select_network('zcash_overwinter_main') mock_tx.OverwinterTx.return_value = 0 self.assertEqual( tb.make_tx(0, 0, 0, 0, expiry=0), 0) ", https://github.com/SurrealAI/symphony,747b87eee20d48032b4a554950687d0b1209fd24,test/test_config.py::TestConfig::test_register,test/test_config.py,NIO,flaky,RepoArchived,nan,classify;root_cause,"from symphony.engine import SymphonyConfig import pytest class TestConfig: def test_creation(self): SymphonyConfig.reset() config = SymphonyConfig() config.update({'username': 'abc', 'experiment_folder': '~/kurreal'}) assert config.username == 'abc' assert config.experiment_folder == '~/kurreal' def test_singleton(self): SymphonyConfig.reset() config = SymphonyConfig() config.update({'username': 'abc', 'experiment_folder': '~/kurreal'}) SymphonyConfig().set_username('def') assert config.username == 'def' def test_no_double_register(self): SymphonyConfig.reset() with pytest.raises(ValueError): SymphonyConfig().register_handler('username', str) def test_register(self): class TmuxConfig: def __init__(self, di): self.max_windows = None if 'max_windows' in di: self.max_windows = di['max_windows'] def set_max_windows(self, x): self.max_windows = x SymphonyConfig().register_handler('tmux', TmuxConfig) SymphonyConfig().update({'tmux': {'max_windows': 5}}) assert SymphonyConfig().tmux.max_windows == 5", https://github.com/SuryaSankar/flask-sqlalchemy-booster,e43980e29a18e725d791b87ce1b677470bbaf22c,tests/test_view_boosters/test_todo_list_api/test_api_endpoints.py::test_multi_users_creation,tests/test_view_boosters/test_todo_list_api/test_api_endpoints.py,NIO,flaky,Opened,https://github.com/SuryaSankar/flask-sqlalchemy-booster/pull/18,classify;root_cause,"import requests import json def test_api_endpoint_existence(todolist_app): with todolist_app.test_client() as client: resp = client.get('/tasks') assert resp.status_code == 200 def test_user_creation(todolist_app): with todolist_app.test_client() as client: name = ""popeye"" resp = client.jpost( '/users', { ""name"": name, ""email"": ""popeye@acme.com"", ""gender"": ""male"" } ) assert resp['status'] == 'success' assert 'id' in resp['result'] assert resp['result']['name'] == name def test_multi_users_creation(todolist_app): with todolist_app.test_client() as client: resp = client.jpost( '/users', [{ ""name"": ""sylvester"", ""email"": ""sylvester@acme.com"", ""gender"": ""male"" }, { ""name"": ""tweety"", ""email"": ""tweety@acme.com"", ""gender"": ""female"" }] ) assert resp['status'] == 'success' assert len(resp['result']) == 2 def test_task_creation(todolist_app): with todolist_app.test_client() as client: title = ""Eat spinach"" resp = client.jpost( '/tasks', { ""title"": title, ""user_id"": 1 } ) assert resp['status'] == 'success' assert 'id' in resp['result'] assert resp['result']['title'] == title def test_multi_tasks_creation(todolist_app): with todolist_app.test_client() as client: resp = client.jpost( '/tasks', [{ ""title"": ""Eat tweety"", ""user_email"": ""sylvester@acme.com"" }, { ""title"": ""Solve a mystery"", ""user_email"": ""tweety@acme.com"" }] ) assert resp['status'] == 'success' def test_task_updation(todolist_app): with todolist_app.test_client() as client: modified_title = ""Eat spinach from tin"" resp = client.jput( '/tasks/1', { ""title"": modified_title } ) assert resp['status'] == 'success' assert 'id' in resp['result'] assert resp['result']['title'] == modified_title def test_tasks_index(todolist_app): with todolist_app.test_client() as client: resp = client.jget('/tasks') assert resp['status'] == 'success' def test_task_get(todolist_app): with todolist_app.test_client() as client: resp = client.jget('/tasks/1') assert resp['status'] == 'success' def test_task_filtering(todolist_app): with todolist_app.test_client() as client: resp = client.jget('/tasks?user_email=tweety@acme.com') assert resp['status'] == 'success' assert resp['result'][0]['title'] == ""Solve a mystery"" ", https://github.com/SuryaSankar/flask-sqlalchemy-booster,e43980e29a18e725d791b87ce1b677470bbaf22c,tests/test_view_boosters/test_todo_list_api/test_api_endpoints.py::test_task_get,tests/test_view_boosters/test_todo_list_api/test_api_endpoints.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import requests import json def test_api_endpoint_existence(todolist_app): with todolist_app.test_client() as client: resp = client.get('/tasks') assert resp.status_code == 200 def test_user_creation(todolist_app): with todolist_app.test_client() as client: name = ""popeye"" resp = client.jpost( '/users', { ""name"": name, ""email"": ""popeye@acme.com"", ""gender"": ""male"" } ) assert resp['status'] == 'success' assert 'id' in resp['result'] assert resp['result']['name'] == name def test_multi_users_creation(todolist_app): with todolist_app.test_client() as client: resp = client.jpost( '/users', [{ ""name"": ""sylvester"", ""email"": ""sylvester@acme.com"", ""gender"": ""male"" }, { ""name"": ""tweety"", ""email"": ""tweety@acme.com"", ""gender"": ""female"" }] ) assert resp['status'] == 'success' assert len(resp['result']) == 2 def test_task_creation(todolist_app): with todolist_app.test_client() as client: title = ""Eat spinach"" resp = client.jpost( '/tasks', { ""title"": title, ""user_id"": 1 } ) assert resp['status'] == 'success' assert 'id' in resp['result'] assert resp['result']['title'] == title def test_multi_tasks_creation(todolist_app): with todolist_app.test_client() as client: resp = client.jpost( '/tasks', [{ ""title"": ""Eat tweety"", ""user_email"": ""sylvester@acme.com"" }, { ""title"": ""Solve a mystery"", ""user_email"": ""tweety@acme.com"" }] ) assert resp['status'] == 'success' def test_task_updation(todolist_app): with todolist_app.test_client() as client: modified_title = ""Eat spinach from tin"" resp = client.jput( '/tasks/1', { ""title"": modified_title } ) assert resp['status'] == 'success' assert 'id' in resp['result'] assert resp['result']['title'] == modified_title def test_tasks_index(todolist_app): with todolist_app.test_client() as client: resp = client.jget('/tasks') assert resp['status'] == 'success' def test_task_get(todolist_app): with todolist_app.test_client() as client: resp = client.jget('/tasks/1') assert resp['status'] == 'success' def test_task_filtering(todolist_app): with todolist_app.test_client() as client: resp = client.jget('/tasks?user_email=tweety@acme.com') assert resp['status'] == 'success' assert resp['result'][0]['title'] == ""Solve a mystery"" ", https://github.com/SuryaSankar/flask-sqlalchemy-booster,e43980e29a18e725d791b87ce1b677470bbaf22c,tests/test_view_boosters/test_todo_list_api/test_api_endpoints.py::test_task_updation,tests/test_view_boosters/test_todo_list_api/test_api_endpoints.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import requests import json def test_api_endpoint_existence(todolist_app): with todolist_app.test_client() as client: resp = client.get('/tasks') assert resp.status_code == 200 def test_user_creation(todolist_app): with todolist_app.test_client() as client: name = ""popeye"" resp = client.jpost( '/users', { ""name"": name, ""email"": ""popeye@acme.com"", ""gender"": ""male"" } ) assert resp['status'] == 'success' assert 'id' in resp['result'] assert resp['result']['name'] == name def test_multi_users_creation(todolist_app): with todolist_app.test_client() as client: resp = client.jpost( '/users', [{ ""name"": ""sylvester"", ""email"": ""sylvester@acme.com"", ""gender"": ""male"" }, { ""name"": ""tweety"", ""email"": ""tweety@acme.com"", ""gender"": ""female"" }] ) assert resp['status'] == 'success' assert len(resp['result']) == 2 def test_task_creation(todolist_app): with todolist_app.test_client() as client: title = ""Eat spinach"" resp = client.jpost( '/tasks', { ""title"": title, ""user_id"": 1 } ) assert resp['status'] == 'success' assert 'id' in resp['result'] assert resp['result']['title'] == title def test_multi_tasks_creation(todolist_app): with todolist_app.test_client() as client: resp = client.jpost( '/tasks', [{ ""title"": ""Eat tweety"", ""user_email"": ""sylvester@acme.com"" }, { ""title"": ""Solve a mystery"", ""user_email"": ""tweety@acme.com"" }] ) assert resp['status'] == 'success' def test_task_updation(todolist_app): with todolist_app.test_client() as client: modified_title = ""Eat spinach from tin"" resp = client.jput( '/tasks/1', { ""title"": modified_title } ) assert resp['status'] == 'success' assert 'id' in resp['result'] assert resp['result']['title'] == modified_title def test_tasks_index(todolist_app): with todolist_app.test_client() as client: resp = client.jget('/tasks') assert resp['status'] == 'success' def test_task_get(todolist_app): with todolist_app.test_client() as client: resp = client.jget('/tasks/1') assert resp['status'] == 'success' def test_task_filtering(todolist_app): with todolist_app.test_client() as client: resp = client.jget('/tasks?user_email=tweety@acme.com') assert resp['status'] == 'success' assert resp['result'][0]['title'] == ""Solve a mystery"" ", https://github.com/SuryaSankar/flask-sqlalchemy-booster,e43980e29a18e725d791b87ce1b677470bbaf22c,tests/test_view_boosters/test_todo_list_api/test_api_endpoints.py::test_user_creation,tests/test_view_boosters/test_todo_list_api/test_api_endpoints.py,NIO,flaky,Opened,https://github.com/SuryaSankar/flask-sqlalchemy-booster/pull/18,classify;root_cause,"import requests import json def test_api_endpoint_existence(todolist_app): with todolist_app.test_client() as client: resp = client.get('/tasks') assert resp.status_code == 200 def test_user_creation(todolist_app): with todolist_app.test_client() as client: name = ""popeye"" resp = client.jpost( '/users', { ""name"": name, ""email"": ""popeye@acme.com"", ""gender"": ""male"" } ) assert resp['status'] == 'success' assert 'id' in resp['result'] assert resp['result']['name'] == name def test_multi_users_creation(todolist_app): with todolist_app.test_client() as client: resp = client.jpost( '/users', [{ ""name"": ""sylvester"", ""email"": ""sylvester@acme.com"", ""gender"": ""male"" }, { ""name"": ""tweety"", ""email"": ""tweety@acme.com"", ""gender"": ""female"" }] ) assert resp['status'] == 'success' assert len(resp['result']) == 2 def test_task_creation(todolist_app): with todolist_app.test_client() as client: title = ""Eat spinach"" resp = client.jpost( '/tasks', { ""title"": title, ""user_id"": 1 } ) assert resp['status'] == 'success' assert 'id' in resp['result'] assert resp['result']['title'] == title def test_multi_tasks_creation(todolist_app): with todolist_app.test_client() as client: resp = client.jpost( '/tasks', [{ ""title"": ""Eat tweety"", ""user_email"": ""sylvester@acme.com"" }, { ""title"": ""Solve a mystery"", ""user_email"": ""tweety@acme.com"" }] ) assert resp['status'] == 'success' def test_task_updation(todolist_app): with todolist_app.test_client() as client: modified_title = ""Eat spinach from tin"" resp = client.jput( '/tasks/1', { ""title"": modified_title } ) assert resp['status'] == 'success' assert 'id' in resp['result'] assert resp['result']['title'] == modified_title def test_tasks_index(todolist_app): with todolist_app.test_client() as client: resp = client.jget('/tasks') assert resp['status'] == 'success' def test_task_get(todolist_app): with todolist_app.test_client() as client: resp = client.jget('/tasks/1') assert resp['status'] == 'success' def test_task_filtering(todolist_app): with todolist_app.test_client() as client: resp = client.jget('/tasks?user_email=tweety@acme.com') assert resp['status'] == 'success' assert resp['result'][0]['title'] == ""Solve a mystery"" ", https://github.com/SuryaSankar/flask-sqlalchemy-booster,e43980e29a18e725d791b87ce1b677470bbaf22c,tests/test_view_boosters/test_todo_list_api/test_relationship_updates.py::test_list_relationship_new_item,tests/test_view_boosters/test_todo_list_api/test_relationship_updates.py,NIO,flaky,Opened,https://github.com/SuryaSankar/flask-sqlalchemy-booster/pull/18,classify;root_cause,"import requests import json def test_list_relationship_new_item(todolist_with_users_tasks): with todolist_with_users_tasks.test_client() as client: first_task_resp_before_update = client.jget(""/tasks/1"") resp = client.jpost( '/users?_ds={""rels"":{""tasks"":{}}}', { ""name"": ""Sheldon"", ""email"": ""sheldon@bbt.com"", ""gender"": ""male"", ""tasks"": [ { ""title"": ""Build a minature rocket"" } ] }) print(resp) assert resp['status'] == 'success' first_task_resp = client.jget(""/tasks/1"") print(first_task_resp) assert first_task_resp['result']['title'] == first_task_resp_before_update['result']['title'] def test_list_relationship_updation_of_existing_item(todolist_with_users_tasks): """""" Regression test case for https://github.com/SuryaSankar/flask-sqlalchemy-booster/issues/17 """""" with todolist_with_users_tasks.test_client() as client: first_task = client.jget(""/tasks/1"")[""result""] second_task = client.jget('/tasks/2')['result'] print(""second_task "", second_task) new_title = ""Modified second task title"" resp = client.jput( '/users/' + str(second_task['user_id'])+'?_ds={""rels"":{""tasks"":{}}}', { ""tasks"": [ { ""id"": second_task[""id""], ""title"": new_title } ] }) assert resp['status'] == 'success' first_task_after_update = client.jget(""/tasks/1"")[""result""] assert first_task['title'] == first_task_after_update['title'] second_task_after_update = client.jget(""/tasks/2"")[""result""] print(""second_task_after_update "", second_task_after_update) assert second_task_after_update['title'] == new_title ", https://github.com/sushobhit27/multiindex,b631e60bd47558eca9067a0dc87f0e713cddc6f2,multiindex/tests/test_multiindex.py::test_insert,multiindex/tests/test_multiindex.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..multiindex import MultiIndexContainer from ..indexed_by import OrderedUnique class Employee(object): def __init__(self,first_name, last_name, emp_id): self.first_name = first_name self.last_name = last_name self.emp_id = emp_id def __str__(self): return '{} {}, {}'.format(self.first_name, self.last_name, self.emp_id) @pytest.fixture(scope='session') def emp_seq(): seq = [Employee('Steve', 'Austin', 786), Employee('Bret', 'Hart', 345), Employee('Razor', 'Ramon', 8732), Employee('Shawn', 'Michaels', 1234)] return seq @pytest.fixture(scope='session') def mi(emp_seq): mi = MultiIndexContainer(OrderedUnique('first_name'), OrderedUnique('last_name'), OrderedUnique('emp_id') ) for emp in emp_seq: mi.insert(emp) return mi def test_insert(mi, emp_seq): assert emp_seq[3] == mi.get('first_name', 'Shawn') assert emp_seq[0] == mi.get('emp_id', 786) assert mi.get('emp_id', 321) is None assert emp_seq[0] == mi.get('first_name', 'Steve') assert emp_seq[1] == mi.get('last_name', 'Hart') assert emp_seq[2] == mi.get('last_name', 'Ramon') def test_insert_overwrite(mi, emp_seq): assert emp_seq[3] == mi.get('first_name', 'Shawn') assert emp_seq[0] == mi.get('emp_id', 786) assert mi.get('emp_id', 321) is None assert emp_seq[0] == mi.get('first_name', 'Steve') assert emp_seq[1] == mi.get('last_name', 'Hart') assert emp_seq[2] == mi.get('last_name', 'Ramon') mi.insert(Employee('Steve', 'Austin', 123), overwrite=True) assert mi.get('first_name', 'Steve').emp_id == 123 mi.insert(Employee('Razor', 'Topaz', 8732), overwrite=True) assert mi.get('emp_id', 8732).last_name == 'Topaz' assert mi.get('first_name', 'Razor').emp_id == 8732 def test_modify(mi): mi.modify('emp_id', 786, 666) assert mi.get('emp_id', 666).first_name == 'Steve' assert mi.get('emp_id', 666).last_name == 'Austin' assert mi.get('first_name', 'Steve').emp_id == 666 assert mi.get('last_name', 'Austin').emp_id == 666 def test_remove(mi): mi.remove('emp_id', 666) assert mi.get('emp_id', 666) is None def test_get_by(mi): assert mi.get_by_emp_id(1234).first_name == 'Shawn' assert mi.get_by_first_name('Shawn').emp_id == 1234 assert mi.get_by_last_name('Hart').first_name == 'Bret'", https://github.com/sushobhit27/multiindex,b631e60bd47558eca9067a0dc87f0e713cddc6f2,multiindex/tests/test_multiindex.py::test_insert_overwrite,multiindex/tests/test_multiindex.py,NIO,flaky,Opened,https://github.com/sushobhit27/multiindex/pull/1,classify;root_cause,"import pytest from ..multiindex import MultiIndexContainer from ..indexed_by import OrderedUnique class Employee(object): def __init__(self,first_name, last_name, emp_id): self.first_name = first_name self.last_name = last_name self.emp_id = emp_id def __str__(self): return '{} {}, {}'.format(self.first_name, self.last_name, self.emp_id) @pytest.fixture(scope='session') def emp_seq(): seq = [Employee('Steve', 'Austin', 786), Employee('Bret', 'Hart', 345), Employee('Razor', 'Ramon', 8732), Employee('Shawn', 'Michaels', 1234)] return seq @pytest.fixture(scope='session') def mi(emp_seq): mi = MultiIndexContainer(OrderedUnique('first_name'), OrderedUnique('last_name'), OrderedUnique('emp_id') ) for emp in emp_seq: mi.insert(emp) return mi def test_insert(mi, emp_seq): assert emp_seq[3] == mi.get('first_name', 'Shawn') assert emp_seq[0] == mi.get('emp_id', 786) assert mi.get('emp_id', 321) is None assert emp_seq[0] == mi.get('first_name', 'Steve') assert emp_seq[1] == mi.get('last_name', 'Hart') assert emp_seq[2] == mi.get('last_name', 'Ramon') def test_insert_overwrite(mi, emp_seq): assert emp_seq[3] == mi.get('first_name', 'Shawn') assert emp_seq[0] == mi.get('emp_id', 786) assert mi.get('emp_id', 321) is None assert emp_seq[0] == mi.get('first_name', 'Steve') assert emp_seq[1] == mi.get('last_name', 'Hart') assert emp_seq[2] == mi.get('last_name', 'Ramon') mi.insert(Employee('Steve', 'Austin', 123), overwrite=True) assert mi.get('first_name', 'Steve').emp_id == 123 mi.insert(Employee('Razor', 'Topaz', 8732), overwrite=True) assert mi.get('emp_id', 8732).last_name == 'Topaz' assert mi.get('first_name', 'Razor').emp_id == 8732 def test_modify(mi): mi.modify('emp_id', 786, 666) assert mi.get('emp_id', 666).first_name == 'Steve' assert mi.get('emp_id', 666).last_name == 'Austin' assert mi.get('first_name', 'Steve').emp_id == 666 assert mi.get('last_name', 'Austin').emp_id == 666 def test_remove(mi): mi.remove('emp_id', 666) assert mi.get('emp_id', 666) is None def test_get_by(mi): assert mi.get_by_emp_id(1234).first_name == 'Shawn' assert mi.get_by_first_name('Shawn').emp_id == 1234 assert mi.get_by_last_name('Hart').first_name == 'Bret'", https://github.com/sushobhit27/multiindex,b631e60bd47558eca9067a0dc87f0e713cddc6f2,multiindex/tests/test_multiindex.py::test_modify,multiindex/tests/test_multiindex.py,NIO,flaky,Opened,https://github.com/sushobhit27/multiindex/pull/2,classify;root_cause,"import pytest from ..multiindex import MultiIndexContainer from ..indexed_by import OrderedUnique class Employee(object): def __init__(self,first_name, last_name, emp_id): self.first_name = first_name self.last_name = last_name self.emp_id = emp_id def __str__(self): return '{} {}, {}'.format(self.first_name, self.last_name, self.emp_id) @pytest.fixture(scope='session') def emp_seq(): seq = [Employee('Steve', 'Austin', 786), Employee('Bret', 'Hart', 345), Employee('Razor', 'Ramon', 8732), Employee('Shawn', 'Michaels', 1234)] return seq @pytest.fixture(scope='session') def mi(emp_seq): mi = MultiIndexContainer(OrderedUnique('first_name'), OrderedUnique('last_name'), OrderedUnique('emp_id') ) for emp in emp_seq: mi.insert(emp) return mi def test_insert(mi, emp_seq): assert emp_seq[3] == mi.get('first_name', 'Shawn') assert emp_seq[0] == mi.get('emp_id', 786) assert mi.get('emp_id', 321) is None assert emp_seq[0] == mi.get('first_name', 'Steve') assert emp_seq[1] == mi.get('last_name', 'Hart') assert emp_seq[2] == mi.get('last_name', 'Ramon') def test_insert_overwrite(mi, emp_seq): assert emp_seq[3] == mi.get('first_name', 'Shawn') assert emp_seq[0] == mi.get('emp_id', 786) assert mi.get('emp_id', 321) is None assert emp_seq[0] == mi.get('first_name', 'Steve') assert emp_seq[1] == mi.get('last_name', 'Hart') assert emp_seq[2] == mi.get('last_name', 'Ramon') mi.insert(Employee('Steve', 'Austin', 123), overwrite=True) assert mi.get('first_name', 'Steve').emp_id == 123 mi.insert(Employee('Razor', 'Topaz', 8732), overwrite=True) assert mi.get('emp_id', 8732).last_name == 'Topaz' assert mi.get('first_name', 'Razor').emp_id == 8732 def test_modify(mi): mi.modify('emp_id', 786, 666) assert mi.get('emp_id', 666).first_name == 'Steve' assert mi.get('emp_id', 666).last_name == 'Austin' assert mi.get('first_name', 'Steve').emp_id == 666 assert mi.get('last_name', 'Austin').emp_id == 666 def test_remove(mi): mi.remove('emp_id', 666) assert mi.get('emp_id', 666) is None def test_get_by(mi): assert mi.get_by_emp_id(1234).first_name == 'Shawn' assert mi.get_by_first_name('Shawn').emp_id == 1234 assert mi.get_by_last_name('Hart').first_name == 'Bret'", https://github.com/sushobhit27/multiindex,b631e60bd47558eca9067a0dc87f0e713cddc6f2,multiindex/tests/test_multiindex.py::test_remove,multiindex/tests/test_multiindex.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..multiindex import MultiIndexContainer from ..indexed_by import OrderedUnique class Employee(object): def __init__(self,first_name, last_name, emp_id): self.first_name = first_name self.last_name = last_name self.emp_id = emp_id def __str__(self): return '{} {}, {}'.format(self.first_name, self.last_name, self.emp_id) @pytest.fixture(scope='session') def emp_seq(): seq = [Employee('Steve', 'Austin', 786), Employee('Bret', 'Hart', 345), Employee('Razor', 'Ramon', 8732), Employee('Shawn', 'Michaels', 1234)] return seq @pytest.fixture(scope='session') def mi(emp_seq): mi = MultiIndexContainer(OrderedUnique('first_name'), OrderedUnique('last_name'), OrderedUnique('emp_id') ) for emp in emp_seq: mi.insert(emp) return mi def test_insert(mi, emp_seq): assert emp_seq[3] == mi.get('first_name', 'Shawn') assert emp_seq[0] == mi.get('emp_id', 786) assert mi.get('emp_id', 321) is None assert emp_seq[0] == mi.get('first_name', 'Steve') assert emp_seq[1] == mi.get('last_name', 'Hart') assert emp_seq[2] == mi.get('last_name', 'Ramon') def test_insert_overwrite(mi, emp_seq): assert emp_seq[3] == mi.get('first_name', 'Shawn') assert emp_seq[0] == mi.get('emp_id', 786) assert mi.get('emp_id', 321) is None assert emp_seq[0] == mi.get('first_name', 'Steve') assert emp_seq[1] == mi.get('last_name', 'Hart') assert emp_seq[2] == mi.get('last_name', 'Ramon') mi.insert(Employee('Steve', 'Austin', 123), overwrite=True) assert mi.get('first_name', 'Steve').emp_id == 123 mi.insert(Employee('Razor', 'Topaz', 8732), overwrite=True) assert mi.get('emp_id', 8732).last_name == 'Topaz' assert mi.get('first_name', 'Razor').emp_id == 8732 def test_modify(mi): mi.modify('emp_id', 786, 666) assert mi.get('emp_id', 666).first_name == 'Steve' assert mi.get('emp_id', 666).last_name == 'Austin' assert mi.get('first_name', 'Steve').emp_id == 666 assert mi.get('last_name', 'Austin').emp_id == 666 def test_remove(mi): mi.remove('emp_id', 666) assert mi.get('emp_id', 666) is None def test_get_by(mi): assert mi.get_by_emp_id(1234).first_name == 'Shawn' assert mi.get_by_first_name('Shawn').emp_id == 1234 assert mi.get_by_last_name('Hart').first_name == 'Bret'", https://github.com/sushobhit27/multiindex,b631e60bd47558eca9067a0dc87f0e713cddc6f2,multiindex/tests/test_multiindex_hashed_non_unique.py::test_insert,multiindex/tests/test_multiindex_hashed_non_unique.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import pytest from ..multiindex import MultiIndexContainer from ..indexed_by import HashedNonUnique class Potus(object): def __init__(self,first_name, last_name, assumed_ofc_at): self.first_name = first_name self.last_name = last_name self.assumed_ofc_at = assumed_ofc_at def __str__(self): return '{} {}, {}'.format(self.first_name, self.last_name, self.assumed_ofc_at) @pytest.fixture(scope='session') def potus_seq(): seq = [Potus('George', 'Washington', 57), Potus('Thomas', 'Jefferson', 58), Potus('Theodore', 'Roosevelt', 43), Potus('George', 'Bush', 55), Potus('Barack', 'Obama', 48), Potus('Franklin', 'Roosevelt', 51), ] return seq @pytest.fixture(scope='session') def mi(potus_seq): mi = MultiIndexContainer(HashedNonUnique('first_name'), HashedNonUnique('last_name'), HashedNonUnique('assumed_ofc_at') ) for emp in potus_seq: mi.insert(emp) return mi def test_insert(mi, potus_seq): assert [potus_seq[4]] == mi.get('first_name', 'Barack') assert [potus_seq[2]] == mi.get('assumed_ofc_at', 43) assert None is mi.get('assumed_ofc_at', 0) assert None is mi.get('assumed_ofc_at', 786) assert [potus_seq[3]] == mi.get('last_name', 'Bush') assert [potus_seq[5]] == mi.get('first_name', 'Franklin') assert [potus_seq[1]] == mi.get('first_name', 'Thomas') for potus in mi.get('first_name', 'George'): assert potus in [potus_seq[3], potus_seq[0]] def test_modify(mi): mi.modify('last_name', 'Obama', 'Hussain') assert mi.get('assumed_ofc_at', 48)[0].last_name == 'Hussain' assert mi.get('first_name', 'Barack')[0].last_name == 'Hussain' assert mi.get('last_name', 'Hussain')[0].first_name == 'Barack' assert mi.get('last_name', 'Hussain')[0].assumed_ofc_at == 48 def test_remove(mi): mi.remove('first_name', 'George') assert len(mi.get('first_name', 'George')) == 1", https://github.com/sushobhit27/multiindex,b631e60bd47558eca9067a0dc87f0e713cddc6f2,multiindex/tests/test_multiindex_hashed_non_unique.py::test_modify,multiindex/tests/test_multiindex_hashed_non_unique.py,NIO,flaky,Opened,https://github.com/sushobhit27/multiindex/pull/3,classify;root_cause,"import pytest from ..multiindex import MultiIndexContainer from ..indexed_by import HashedNonUnique class Potus(object): def __init__(self,first_name, last_name, assumed_ofc_at): self.first_name = first_name self.last_name = last_name self.assumed_ofc_at = assumed_ofc_at def __str__(self): return '{} {}, {}'.format(self.first_name, self.last_name, self.assumed_ofc_at) @pytest.fixture(scope='session') def potus_seq(): seq = [Potus('George', 'Washington', 57), Potus('Thomas', 'Jefferson', 58), Potus('Theodore', 'Roosevelt', 43), Potus('George', 'Bush', 55), Potus('Barack', 'Obama', 48), Potus('Franklin', 'Roosevelt', 51), ] return seq @pytest.fixture(scope='session') def mi(potus_seq): mi = MultiIndexContainer(HashedNonUnique('first_name'), HashedNonUnique('last_name'), HashedNonUnique('assumed_ofc_at') ) for emp in potus_seq: mi.insert(emp) return mi def test_insert(mi, potus_seq): assert [potus_seq[4]] == mi.get('first_name', 'Barack') assert [potus_seq[2]] == mi.get('assumed_ofc_at', 43) assert None is mi.get('assumed_ofc_at', 0) assert None is mi.get('assumed_ofc_at', 786) assert [potus_seq[3]] == mi.get('last_name', 'Bush') assert [potus_seq[5]] == mi.get('first_name', 'Franklin') assert [potus_seq[1]] == mi.get('first_name', 'Thomas') for potus in mi.get('first_name', 'George'): assert potus in [potus_seq[3], potus_seq[0]] def test_modify(mi): mi.modify('last_name', 'Obama', 'Hussain') assert mi.get('assumed_ofc_at', 48)[0].last_name == 'Hussain' assert mi.get('first_name', 'Barack')[0].last_name == 'Hussain' assert mi.get('last_name', 'Hussain')[0].first_name == 'Barack' assert mi.get('last_name', 'Hussain')[0].assumed_ofc_at == 48 def test_remove(mi): mi.remove('first_name', 'George') assert len(mi.get('first_name', 'George')) == 1", https://github.com/sushobhit27/multiindex,b631e60bd47558eca9067a0dc87f0e713cddc6f2,multiindex/tests/test_multiindex_hashed_non_unique.py::test_remove,multiindex/tests/test_multiindex_hashed_non_unique.py,NIO,flaky,Opened,https://github.com/sushobhit27/multiindex/pull/3,classify;root_cause,"import pytest from ..multiindex import MultiIndexContainer from ..indexed_by import HashedNonUnique class Potus(object): def __init__(self,first_name, last_name, assumed_ofc_at): self.first_name = first_name self.last_name = last_name self.assumed_ofc_at = assumed_ofc_at def __str__(self): return '{} {}, {}'.format(self.first_name, self.last_name, self.assumed_ofc_at) @pytest.fixture(scope='session') def potus_seq(): seq = [Potus('George', 'Washington', 57), Potus('Thomas', 'Jefferson', 58), Potus('Theodore', 'Roosevelt', 43), Potus('George', 'Bush', 55), Potus('Barack', 'Obama', 48), Potus('Franklin', 'Roosevelt', 51), ] return seq @pytest.fixture(scope='session') def mi(potus_seq): mi = MultiIndexContainer(HashedNonUnique('first_name'), HashedNonUnique('last_name'), HashedNonUnique('assumed_ofc_at') ) for emp in potus_seq: mi.insert(emp) return mi def test_insert(mi, potus_seq): assert [potus_seq[4]] == mi.get('first_name', 'Barack') assert [potus_seq[2]] == mi.get('assumed_ofc_at', 43) assert None is mi.get('assumed_ofc_at', 0) assert None is mi.get('assumed_ofc_at', 786) assert [potus_seq[3]] == mi.get('last_name', 'Bush') assert [potus_seq[5]] == mi.get('first_name', 'Franklin') assert [potus_seq[1]] == mi.get('first_name', 'Thomas') for potus in mi.get('first_name', 'George'): assert potus in [potus_seq[3], potus_seq[0]] def test_modify(mi): mi.modify('last_name', 'Obama', 'Hussain') assert mi.get('assumed_ofc_at', 48)[0].last_name == 'Hussain' assert mi.get('first_name', 'Barack')[0].last_name == 'Hussain' assert mi.get('last_name', 'Hussain')[0].first_name == 'Barack' assert mi.get('last_name', 'Hussain')[0].assumed_ofc_at == 48 def test_remove(mi): mi.remove('first_name', 'George') assert len(mi.get('first_name', 'George')) == 1", https://github.com/sylhare/gitlab_stats,157de3b703031ef0de39e187314133624f1ac1bb,tests/test_utils.py::UtilsTest::test_056_enhance_project_info,tests/test_utils.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"import unittest from io import StringIO from unittest import mock import tests from gitlab_stats.utils import * class UtilsTest(unittest.TestCase): def test_001_check_proxy_format(self): result = format_proxy(""test"") self.assertEqual({'http': ""test"", 'https': ""test""}, result) def test_003_no_token_raise_exception(self): with mock.patch.dict('os.environ', clear=True): token = check_token(None) self.assertEqual(None, token) # -- Project name and id def test_030_get_name_and_id_in_a_list_of_dict(self): response = get_name_and_id(tests.PROJECT) self.assertEqual(type(response), list) self.assertEqual(type(response[0]), dict) def test_031_get_name_and_id_has_correct_fields(self): response = get_name_and_id(tests.PROJECT) self.assertEqual(len(response[0]), 2) self.assertEqual(response[0]['id'], tests.PROJECT_ID) self.assertEqual(response[0]['name'], tests.PROJECT_NAME) # -- Pipeline def test_040_get_pipeline_id_in_a_list(self): response = get_pipelines_id(tests.PIPELINES) self.assertEqual(type(response), list) def test_041_get_pipeline_id_in_a_dict_has_correct_fields(self): response = get_pipelines_id(tests.PIPELINES) self.assertEqual(type(response), list) self.assertEqual(len(response), 4) self.assertEqual(response[0], 33409) def test_042_get_pipeline_info_in_a_dict(self): response = get_pipeline_info(tests.PIPELINE_INFO) self.assertEqual(type(response), dict) def test_043_get_pipeline_info_with_correct_fields(self): response = get_pipeline_info(tests.PIPELINE_INFO) self.assertEqual(len(response), 4) self.assertTrue('id' in response) self.assertTrue('status' in response) self.assertTrue('duration' in response) self.assertTrue('date' in response) # -- Value added def test_050_get_moy_duration(self): duration_moy = get_duration_moy(tests.PROJECT_INFO) self.assertEqual(duration_moy, 58.9) def test_051_get_moy_duration_with_null(self): duration_moy = get_duration_moy(tests.PROJECT_INFO_WITH_ONE_DURATION_NONE) self.assertEqual(duration_moy, 57.7) def test_052_get_moy_duration_when_one_duration_null(self): duration_moy = get_duration_moy(tests.PROJECT_INFO_DURATION_NONE) self.assertEqual(duration_moy, None) def test_053_get_success_percentage(self): success_percentage = get_success_percentage(tests.PROJECT_INFO) self.assertEqual(success_percentage, 70) def test_054_get_success_percentage_when_no_pipeline(self): success_percentage = get_success_percentage(tests.PROJECT_INFO_EMPTY) self.assertEqual(success_percentage, None) def test_055_get_all_pipeline_info_from_2_weeks_ago(self): response = get_pipeline_info_from(tests.PROJECT_INFO_TIME, 15) self.assertEqual(len(response['pipelines']), 6) def test_056_enhance_project_info(self): response = enhance_project_info(tests.PROJECT_INFO) self.assertEqual(len(response), 5) self.assertTrue('id' in response) self.assertTrue('name' in response) self.assertTrue('duration_moy' in response) self.assertTrue('duration_in_minutes' in response) self.assertTrue('success_percentage' in response) def test_057_get_report(self): with mock.patch('sys.stdout', new=StringIO()) as output: print_cli_report(tests.PROJECT_INFO_ENHANCED) self.assertEqual(output.getvalue().strip()[0], tests.REPORT[0]) # -- Report def test_220_create_a_csv(self): create_dict_to_csv(tests.PROJECT_INFO_ENHANCED, 'output.csv') self.assertTrue(os.path.isfile('output.csv')) def test_221_write_into_a_created_report_do_not_had_headers(self): write_dict_to_csv(tests.PROJECT_INFO_ENHANCED, 'output.csv') with open('output.csv', 'r') as f: result = list(csv.reader(f)) self.assertNotEqual(result[0], result[2]) def test_222_report_regenerated(self): generate_report(tests.PROJECT_INFO_ENHANCED, 'output.csv') with open('output.csv', 'r') as f: result = list(csv.reader(f)) self.assertEqual(result[2], result[3]) os.remove('output.csv') def test_223_report_generated(self): generate_report(tests.PROJECT_INFO_ENHANCED, 'output.csv') with open('output.csv', 'r') as f: result = list(csv.reader(f)) self.assertNotEqual(result[0], result[1]) with self.assertRaises(IndexError): print(""report created so there's only two rows: {} should raise en error"".format(result[2])) os.remove('output.csv') if __name__ == ""__main__"": unittest.main() ", https://github.com/syrusakbary/promise,4627315476f6b9fc82818327ae09b04f89f9bda7,tests/test_dataloader.py::test_wrong_loader_return_type_does_not_block_async_instance,tests/test_dataloader.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from pytest import raises from promise import Promise, async_instance from promise.dataloader import DataLoader def id_loader(**options): load_calls = [] resolve = options.pop(""resolve"", Promise.resolve) def fn(keys): load_calls.append(keys) return resolve(keys) identity_loader = DataLoader(fn, **options) return identity_loader, load_calls def test_build_a_simple_data_loader(): def call_fn(keys): return Promise.resolve(keys) identity_loader = DataLoader(call_fn) promise1 = identity_loader.load(1) assert isinstance(promise1, Promise) value1 = promise1.get() assert value1 == 1 def test_supports_loading_multiple_keys_in_one_call(): def call_fn(keys): return Promise.resolve(keys) identity_loader = DataLoader(call_fn) promise_all = identity_loader.load_many([1, 2]) assert isinstance(promise_all, Promise) values = promise_all.get() assert values == [1, 2] promise_all = identity_loader.load_many([]) assert isinstance(promise_all, Promise) values = promise_all.get() assert values == [] def test_batches_multiple_requests(): @Promise.safe def do(): identity_loader, load_calls = id_loader() promise1 = identity_loader.load(1) promise2 = identity_loader.load(2) p = Promise.all([promise1, promise2]) value1, value2 = p.get() assert value1 == 1 assert value2 == 2 assert load_calls == [[1, 2]] do().get() def test_batches_multiple_requests_with_max_batch_sizes(): @Promise.safe def do(): identity_loader, load_calls = id_loader(max_batch_size=2) promise1 = identity_loader.load(1) promise2 = identity_loader.load(2) promise3 = identity_loader.load(3) p = Promise.all([promise1, promise2, promise3]) value1, value2, value3 = p.get() assert value1 == 1 assert value2 == 2 assert value3 == 3 assert load_calls == [[1, 2], [3]] do().get() def test_coalesces_identical_requests(): @Promise.safe def do(): identity_loader, load_calls = id_loader() promise1 = identity_loader.load(1) promise2 = identity_loader.load(1) assert promise1 == promise2 p = Promise.all([promise1, promise2]) value1, value2 = p.get() assert value1 == 1 assert value2 == 1 assert load_calls == [[1]] do().get() def test_caches_repeated_requests(): @Promise.safe def do(): identity_loader, load_calls = id_loader() a, b = Promise.all([identity_loader.load(""A""), identity_loader.load(""B"")]).get() assert a == ""A"" assert b == ""B"" assert load_calls == [[""A"", ""B""]] a2, c = Promise.all( [identity_loader.load(""A""), identity_loader.load(""C"")] ).get() assert a2 == ""A"" assert c == ""C"" assert load_calls == [[""A"", ""B""], [""C""]] a3, b2, c2 = Promise.all( [ identity_loader.load(""A""), identity_loader.load(""B""), identity_loader.load(""C""), ] ).get() assert a3 == ""A"" assert b2 == ""B"" assert c2 == ""C"" assert load_calls == [[""A"", ""B""], [""C""]] do().get() def test_clears_single_value_in_loader(): @Promise.safe def do(): identity_loader, load_calls = id_loader() a, b = Promise.all([identity_loader.load(""A""), identity_loader.load(""B"")]).get() assert a == ""A"" assert b == ""B"" assert load_calls == [[""A"", ""B""]] identity_loader.clear(""A"") a2, b2 = Promise.all( [identity_loader.load(""A""), identity_loader.load(""B"")] ).get() assert a2 == ""A"" assert b2 == ""B"" assert load_calls == [[""A"", ""B""], [""A""]] do().get() def test_clears_all_values_in_loader(): @Promise.safe def do(): identity_loader, load_calls = id_loader() a, b = Promise.all([identity_loader.load(""A""), identity_loader.load(""B"")]).get() assert a == ""A"" assert b == ""B"" assert load_calls == [[""A"", ""B""]] identity_loader.clear_all() a2, b2 = Promise.all( [identity_loader.load(""A""), identity_loader.load(""B"")] ).get() assert a2 == ""A"" assert b2 == ""B"" assert load_calls == [[""A"", ""B""], [""A"", ""B""]] do().get() def test_does_not_replace_cache_map(): @Promise.safe def do(): identity_loader, _ = id_loader() a, b = Promise.all([identity_loader.load(""A""), identity_loader.load(""B"")]).get() assert a == ""A"" assert b == ""B"" cache_map = identity_loader._promise_cache identity_loader.clear_all() assert id(identity_loader._promise_cache) == id(cache_map) do().get() def test_allows_priming_the_cache(): @Promise.safe def do(): identity_loader, load_calls = id_loader() identity_loader.prime(""A"", ""A"") a, b = Promise.all([identity_loader.load(""A""), identity_loader.load(""B"")]).get() assert a == ""A"" assert b == ""B"" assert load_calls == [[""B""]] do().get() def test_does_not_prime_keys_that_already_exist(): @Promise.safe def do(): identity_loader, load_calls = id_loader() identity_loader.prime(""A"", ""X"") a1 = identity_loader.load(""A"").get() b1 = identity_loader.load(""B"").get() assert a1 == ""X"" assert b1 == ""B"" identity_loader.prime(""A"", ""Y"") identity_loader.prime(""B"", ""Y"") a2 = identity_loader.load(""A"").get() b2 = identity_loader.load(""B"").get() assert a2 == ""X"" assert b2 == ""B"" assert load_calls == [[""B""]] do().get() # Represents Errors def test_resolves_to_error_to_indicate_failure(): @Promise.safe def do(): def resolve(keys): mapped_keys = [ key if key % 2 == 0 else Exception(""Odd: {}"".format(key)) for key in keys ] return Promise.resolve(mapped_keys) even_loader, load_calls = id_loader(resolve=resolve) with raises(Exception) as exc_info: even_loader.load(1).get() assert str(exc_info.value) == ""Odd: 1"" value2 = even_loader.load(2).get() assert value2 == 2 assert load_calls == [[1], [2]] do().get() def test_can_represent_failures_and_successes_simultaneously(): @Promise.safe def do(): def resolve(keys): mapped_keys = [ key if key % 2 == 0 else Exception(""Odd: {}"".format(key)) for key in keys ] return Promise.resolve(mapped_keys) even_loader, load_calls = id_loader(resolve=resolve) promise1 = even_loader.load(1) promise2 = even_loader.load(2) with raises(Exception) as exc_info: promise1.get() assert str(exc_info.value) == ""Odd: 1"" value2 = promise2.get() assert value2 == 2 assert load_calls == [[1, 2]] do().get() def test_caches_failed_fetches(): @Promise.safe def do(): def resolve(keys): mapped_keys = [Exception(""Error: {}"".format(key)) for key in keys] return Promise.resolve(mapped_keys) error_loader, load_calls = id_loader(resolve=resolve) with raises(Exception) as exc_info: error_loader.load(1).get() assert str(exc_info.value) == ""Error: 1"" with raises(Exception) as exc_info: error_loader.load(1).get() assert str(exc_info.value) == ""Error: 1"" assert load_calls == [[1]] do().get() def test_caches_failed_fetches(): @Promise.safe def do(): identity_loader, load_calls = id_loader() identity_loader.prime(1, Exception(""Error: 1"")) with raises(Exception) as exc_info: identity_loader.load(1).get() assert load_calls == [] do().get() # It is resilient to job queue ordering # def test_batches_loads_occuring_within_promises(): # @Promise.safe # def do(): # identity_loader, load_calls = id_loader() # values = Promise.all([ # identity_loader.load('A'), # Promise.resolve(None).then(lambda v: Promise.resolve(None)).then( # lambda v: identity_loader.load('B') # ) # ]).get() # assert values == ['A', 'B'] # assert load_calls == [['A', 'B']] # do().get() def test_catches_error_if_loader_resolver_fails(): @Promise.safe def do(): def do_resolve(x): raise Exception(""AOH!"") a_loader, a_load_calls = id_loader(resolve=do_resolve) with raises(Exception) as exc_info: a_loader.load(""A1"").get() assert str(exc_info.value) == ""AOH!"" do().get() def test_can_call_a_loader_from_a_loader(): @Promise.safe def do(): deep_loader, deep_load_calls = id_loader() a_loader, a_load_calls = id_loader( resolve=lambda keys: deep_loader.load(tuple(keys)) ) b_loader, b_load_calls = id_loader( resolve=lambda keys: deep_loader.load(tuple(keys)) ) a1, b1, a2, b2 = Promise.all( [ a_loader.load(""A1""), b_loader.load(""B1""), a_loader.load(""A2""), b_loader.load(""B2""), ] ).get() assert a1 == ""A1"" assert b1 == ""B1"" assert a2 == ""A2"" assert b2 == ""B2"" assert a_load_calls == [[""A1"", ""A2""]] assert b_load_calls == [[""B1"", ""B2""]] assert deep_load_calls == [[(""A1"", ""A2""), (""B1"", ""B2"")]] do().get() def test_dataloader_clear_with_missing_key_works(): @Promise.safe def do(): d", https://github.com/taxpon/pytidy,0a0a54b80b53dadd8ecc8ff2db42444741b75865,tests/test_dataclass.py::test_class_is_inserted_to_dataclass,tests/test_dataclass.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from pytidy.decorators import component, autowired_cls import sys if sys.version_info[1] >= 7: from dataclasses import dataclass @component class C: def get(self): return ""c"" @autowired_cls @dataclass class D: c: C def get(self): return self.c.get() def test_class_is_inserted_to_dataclass(): obj = D() another = D() assert obj.get() == ""c"" assert id(obj.c) == id(another.c) ", https://github.com/taxpon/pytidy,0a0a54b80b53dadd8ecc8ff2db42444741b75865,tests/test_function.py::test_object_insertion,tests/test_function.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from pytidy.decorators import autowired, component @component class A: def get(self): return ""a"" class B: @autowired def __init__(self, a: A): self.a = a def get(self): return self.a.get() class B2: @autowired def __init__(self, a: A) -> None: # Has return type ""None"" self.a = a def get(self): return self.a.get() def test_object_insertion(): obj = B() another = B() assert obj.get() == ""a"" assert id(obj.a) == id(another.a) def test_return_type_none(): obj = B2() another = B() assert obj.get() == ""a"" assert id(obj.a) == id(another.a) ", https://github.com/taxpon/pytidy,0a0a54b80b53dadd8ecc8ff2db42444741b75865,tests/test_function.py::test_return_type_none,tests/test_function.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from pytidy.decorators import autowired, component @component class A: def get(self): return ""a"" class B: @autowired def __init__(self, a: A): self.a = a def get(self): return self.a.get() class B2: @autowired def __init__(self, a: A) -> None: # Has return type ""None"" self.a = a def get(self): return self.a.get() def test_object_insertion(): obj = B() another = B() assert obj.get() == ""a"" assert id(obj.a) == id(another.a) def test_return_type_none(): obj = B2() another = B() assert obj.get() == ""a"" assert id(obj.a) == id(another.a) ", https://github.com/teamhide/pythondi,913e6a60fc5d02c3db385ad322f970ecec36969b,tests/test_configure.py::test_configure,tests/test_configure.py,NIO,flaky,Accepted,https://github.com/teamhide/pythondi/pull/23,classify;root_cause;fix_proposal,"from pytest import raises from pythondi import ( configure, configure_after_clear, Provider, InjectException, Container ) def test_configure(): provider = Provider() provider.bind(int, str) configure(provider=provider) assert isinstance(Container.get(), Provider) assert Container.get().bindings[int] == str with raises(InjectException): configure(provider=provider) def test_configure_after_clear(): provider = Provider() provider.bind(int, str) configure_after_clear(provider=provider) provider = Provider() provider.bind(str, int) configure_after_clear(provider=provider) assert isinstance(Container.get(), Provider) assert Container.get().bindings[str] == int ", https://github.com/Teamworksapp/pydbvolve,e18bb8cf98997c52de0cf30a8e106c480ba0bb57,tests/unittests/test_00_initialization.py::test_09_get_migration_table_schema,tests/unittests/test_00_initialization.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import sys import sqlite3 import os import sys import importlib # Set path to force the import of the local module sys.path.insert(1, os.path.abspath('.')) import pydbvolve TEST_CONFIG_FILE = os.path.abspath(os.path.join('tests', 'pydbvolve.conf')) TEST_DB_FILE = os.path.abspath(os.path.join('tests', 'test_db.sqlite')) def test_00_local_module(capsys): """"""Verify that we are using the local module."""""" with capsys.disabled(): #print(pydbvolve.__path__) assert('site-packages' not in '|'.join(pydbvolve.__path__)) assert('dist-packages' not in '|'.join(pydbvolve.__path__)) # End test_00_local_module def test_01_new_config(): """"""Verify that the return from new_config call is an instance of dict"""""" assert(isinstance(pydbvolve.new_config(), dict)) # End test_new_config def test_02_get_migration_user(): """"""Verify that the get_migration_user returns a non-empty string"""""" muser = pydbvolve.get_migration_user({}) assert(muser is not None) assert(isinstance(muser, str)) assert(len(muser) > 0) # End test_get_migration_user def test_03_get_base_dir(): """"""Verify that we get a non-Falsey return from get_base_dir."""""" bdir = pydbvolve.get_base_dir(TEST_CONFIG_FILE) assert(bdir is not None) assert(isinstance(bdir, str)) assert(len(bdir) > 0) # End test_03_get_base_dir def test_04_get_migration_base_dir(): """"""Verify that we get a non-Falsey return from get_migration_base_dir."""""" mdir = pydbvolve.get_migration_base_dir(pydbvolve.get_base_dir(TEST_CONFIG_FILE)) assert(mdir is not None) assert(isinstance(mdir, str)) assert(len(mdir) > 0) assert(mdir.startswith(pydbvolve.get_base_dir(TEST_CONFIG_FILE))) # End test_04_get_migration_base_dir def test_05_get_migration_upgrade_dir(): """"""Verify that we get a non-Falsey return from get_migration_upgrade_dir."""""" mdir = pydbvolve.get_migration_base_dir(pydbvolve.get_base_dir(TEST_CONFIG_FILE)) udir = pydbvolve.get_migration_upgrade_dir(mdir) assert(udir is not None) assert(isinstance(udir, str)) assert(len(udir) > 0) assert(udir.startswith(mdir)) # End test_05_get_migration_upgrade_dir def test_06_get_migration_downgrade_dir(): """"""Verify that we get a non-Falsey return from get_migration_downgrade_dir."""""" mdir = pydbvolve.get_migration_base_dir(pydbvolve.get_base_dir(TEST_CONFIG_FILE)) ddir = pydbvolve.get_migration_downgrade_dir(mdir) assert(ddir is not None) assert(isinstance(ddir, str)) assert(len(ddir) > 0) assert(ddir.startswith(mdir)) # End test_06_get_migration_downgrade_dir def test_07_get_log_dir(): """"""Verify that we get a non-Falsey return from get_log_dir."""""" bdir = pydbvolve.get_base_dir(TEST_CONFIG_FILE) ldir = pydbvolve.get_log_dir(bdir) assert(ldir is not None) assert(isinstance(ldir, str)) assert(len(ldir) > 0) assert(ldir.startswith(bdir)) # End test_07_get_log_dir def test_08_get_migration_table_name(): """"""Verify that we get a non-Falsey return from get_migration_table_name."""""" table_name = pydbvolve.get_migration_table_name() assert(table_name is not None) assert(isinstance(table_name, str)) assert(len(table_name) > 0) # End test_08_get_migration_table_name def test_09_get_migration_table_schema(): """"""Verify that we get a non-Falsey return from get_migration_table_schema."""""" schema = pydbvolve.get_migration_table_schema() assert(schema is not None) assert(isinstance(schema, str)) assert(len(schema) > 0) # End test_09_get_migration_table_name def test_10_get_positional_variable_marker(): """"""Verify that we get back a non-Falsey return from get_positional_variable_marker."""""" marker = pydbvolve.get_positional_variable_marker() assert(marker is not None) assert(isinstance(marker, str)) assert(len(marker) > 0) # End test_10_get_positional_variable_marker def test_11_log_file(capsys): """"""Verify that we can open a log file."""""" with capsys.disabled(): config = {'log_file_name': 'tmp.log', 'logger_name': 'f', 'version': 'r1.0.0', 'migration_action': 'info'} pydbvolve.setup_file_logger(config) f = config['logger'] config['log_file_name'] = None config['logger_name'] = 's' pydbvolve.setup_stream_logger(config) s = config['logger'] assert(f is not None) assert(s is not None) assert(f.__class__.__name__ == 'Logger') assert(s.__class__.__name__ == 'Logger') assert(f.name == 'f') assert(s.name == 's') assert(len(s.handlers) == 1) assert(len(f.handlers) >= 1) # End test_11_log_file def test_12_get_filename_regex(): """"""Verify that we get a compiled filename regex from get_filename_regex."""""" import re r = pydbvolve.get_filename_regex() assert(r is not None) assert(r.__class__.__name__ == 'SRE_Pattern') # End test_12_get_filename_regex def test_13_pre_config(): res = None try: res = pydbvolve.get_db_credentials(None) except Exception as e: assert(e.__class__.__name__ == 'NotImplementedError') assert(res is None) try: res = pydbvolve.get_db_connection(None, None) except Exception as f: assert(f.__class__.__name__ == 'NotImplementedError') assert(res is None) # End test_13_pre_config def test_14_load_config(): """"""Load the test config file and verify that it overrides the speficied functions."""""" try: os.unlink(TEST_DB_FILE) except: pass old_get_db_credentials = pydbvolve.get_db_credentials old_get_base_dir = pydbvolve.get_base_dir old_get_db_connection = pydbvolve.get_db_connection old_get_positional_variable_marker = pydbvolve.get_positional_variable_marker old_get_migration_table_schema = pydbvolve.get_migration_table_schema pydbvolve.load_config(TEST_CONFIG_FILE) assert(old_get_db_credentials != pydbvolve.get_db_credentials) assert(old_get_base_dir != pydbvolve.get_base_dir) assert(old_get_db_connection != pydbvolve.get_db_connection) assert(old_get_positional_variable_marker != pydbvolve.get_positional_variable_marker) assert(old_get_migration_table_schema != pydbvolve.get_migration_table_schema) # End test_load_config def test_15_run_config(): """"""Verify we get a config dict with all expected keys present and having no None values."""""" def get_migration_table_schema(): return ""public"" pydbvolve.load_config(TEST_CONFIG_FILE) pydbvolve.get_migration_table_schema = get_migration_table_schema config = pydbvolve.new_config() config.update({'migration_action': 'info', 'version': pydbvolve.LATEST_VERSION, 'migration_user': pydbvolve.get_migration_user(config), 'sequential': False, 'config_file_path': TEST_CONFIG_FILE, 'verbose': False}) pydbvolve.run_config(config) for key in ('base_dir', 'migration_dir', 'migration_upgrade_dir', 'migration_downgrade_dir', 'filename_regex', 'log_dir', 'migration_table_schema', 'migration_table_name', 'positional_variable_marker', 'migration_action', 'version', 'migration_user', 'sequential', 'verbose'): assert(key in config) assert(config[key] is not None) importlib.reload(pydbvolve) # End test_15_run_config def test_16_confirm_dirs(): pydbvolve.load_config(TEST_CONFIG_FILE) config = pydbvolve.new_config() config.update({'migration_action': 'info', 'version': pydbvolve.LATEST_VERSION, 'migration_user': pydbvolve.get_migration_user(config), 'sequential': False, 'config_file_path': TEST_CONFIG_FILE, 'verbose': False}) pydbvolve.run_config(config) pydbvolve.confirm_dirs(config) for dirname in (k for k in config.keys() if k.endswith('_dir')): assert(os.path.exists(config[dirname])) # End test_16_confirm_dirs def test_17_get_db_credentials(): """"""Test the return from get_db_credentials call"""""" pydbvolve.load_config(TEST_CONFIG_FILE) config = pydbvolve.new_config() config.update({'migration_action': 'info', 'version': pydbvolve.LATEST_VERSION, 'migration_user': pydbvolve.get_migration_user(config), 'sequential': False, 'config_file_path': TEST_CONFIG_FILE, 'verbose': False}) pydbvolve.run_config(config) cred = pydbvolve.get_db_credentials(config) assert(cred is not None) assert(isinstance(cred, dict)) assert(len(cred) > 0) # End test_17_get_db_credentials def test_18_get_database_user(): pydbvolve.load_config(TEST_CONFIG_FILE) config = pydbvolve.new_config() config.update({'migration_action': 'info', 'version': pydbvolve.LATEST_VERSION, 'migration_user': pydbvolve.get_migration_user(config), 'sequential': False, 'config_file_path': TEST_CONFIG_FILE, 'verbose': False}) pydbvolve.run_config(config) cred = pydbvolve.get_db_credentials(config) dbuser = pydbvolve.get_database_user(config, cred) assert(dbuser is not None) assert(isinstance(dbuser, str)) assert(len(dbuser) > 0) # End test_18_get_database_user def test_19_connect_db(): """"""Verify that we can get a connection based on the definition of the get_db_connection config function."""""" pydbvolve.load_config(TEST_CONFIG_FILE) config = pydbvolve.new_config() config.update({'migr", https://github.com/Teamworksapp/pydbvolve,e18bb8cf98997c52de0cf30a8e106c480ba0bb57,tests/unittests/test_00_initialization.py::test_11_log_file,tests/unittests/test_00_initialization.py,NIO,flaky,Opened,https://github.com/Teamworksapp/pydbvolve/pull/14,classify;root_cause,"import os import sys import sqlite3 import os import sys import importlib # Set path to force the import of the local module sys.path.insert(1, os.path.abspath('.')) import pydbvolve TEST_CONFIG_FILE = os.path.abspath(os.path.join('tests', 'pydbvolve.conf')) TEST_DB_FILE = os.path.abspath(os.path.join('tests', 'test_db.sqlite')) def test_00_local_module(capsys): """"""Verify that we are using the local module."""""" with capsys.disabled(): #print(pydbvolve.__path__) assert('site-packages' not in '|'.join(pydbvolve.__path__)) assert('dist-packages' not in '|'.join(pydbvolve.__path__)) # End test_00_local_module def test_01_new_config(): """"""Verify that the return from new_config call is an instance of dict"""""" assert(isinstance(pydbvolve.new_config(), dict)) # End test_new_config def test_02_get_migration_user(): """"""Verify that the get_migration_user returns a non-empty string"""""" muser = pydbvolve.get_migration_user({}) assert(muser is not None) assert(isinstance(muser, str)) assert(len(muser) > 0) # End test_get_migration_user def test_03_get_base_dir(): """"""Verify that we get a non-Falsey return from get_base_dir."""""" bdir = pydbvolve.get_base_dir(TEST_CONFIG_FILE) assert(bdir is not None) assert(isinstance(bdir, str)) assert(len(bdir) > 0) # End test_03_get_base_dir def test_04_get_migration_base_dir(): """"""Verify that we get a non-Falsey return from get_migration_base_dir."""""" mdir = pydbvolve.get_migration_base_dir(pydbvolve.get_base_dir(TEST_CONFIG_FILE)) assert(mdir is not None) assert(isinstance(mdir, str)) assert(len(mdir) > 0) assert(mdir.startswith(pydbvolve.get_base_dir(TEST_CONFIG_FILE))) # End test_04_get_migration_base_dir def test_05_get_migration_upgrade_dir(): """"""Verify that we get a non-Falsey return from get_migration_upgrade_dir."""""" mdir = pydbvolve.get_migration_base_dir(pydbvolve.get_base_dir(TEST_CONFIG_FILE)) udir = pydbvolve.get_migration_upgrade_dir(mdir) assert(udir is not None) assert(isinstance(udir, str)) assert(len(udir) > 0) assert(udir.startswith(mdir)) # End test_05_get_migration_upgrade_dir def test_06_get_migration_downgrade_dir(): """"""Verify that we get a non-Falsey return from get_migration_downgrade_dir."""""" mdir = pydbvolve.get_migration_base_dir(pydbvolve.get_base_dir(TEST_CONFIG_FILE)) ddir = pydbvolve.get_migration_downgrade_dir(mdir) assert(ddir is not None) assert(isinstance(ddir, str)) assert(len(ddir) > 0) assert(ddir.startswith(mdir)) # End test_06_get_migration_downgrade_dir def test_07_get_log_dir(): """"""Verify that we get a non-Falsey return from get_log_dir."""""" bdir = pydbvolve.get_base_dir(TEST_CONFIG_FILE) ldir = pydbvolve.get_log_dir(bdir) assert(ldir is not None) assert(isinstance(ldir, str)) assert(len(ldir) > 0) assert(ldir.startswith(bdir)) # End test_07_get_log_dir def test_08_get_migration_table_name(): """"""Verify that we get a non-Falsey return from get_migration_table_name."""""" table_name = pydbvolve.get_migration_table_name() assert(table_name is not None) assert(isinstance(table_name, str)) assert(len(table_name) > 0) # End test_08_get_migration_table_name def test_09_get_migration_table_schema(): """"""Verify that we get a non-Falsey return from get_migration_table_schema."""""" schema = pydbvolve.get_migration_table_schema() assert(schema is not None) assert(isinstance(schema, str)) assert(len(schema) > 0) # End test_09_get_migration_table_name def test_10_get_positional_variable_marker(): """"""Verify that we get back a non-Falsey return from get_positional_variable_marker."""""" marker = pydbvolve.get_positional_variable_marker() assert(marker is not None) assert(isinstance(marker, str)) assert(len(marker) > 0) # End test_10_get_positional_variable_marker def test_11_log_file(capsys): """"""Verify that we can open a log file."""""" with capsys.disabled(): config = {'log_file_name': 'tmp.log', 'logger_name': 'f', 'version': 'r1.0.0', 'migration_action': 'info'} pydbvolve.setup_file_logger(config) f = config['logger'] config['log_file_name'] = None config['logger_name'] = 's' pydbvolve.setup_stream_logger(config) s = config['logger'] assert(f is not None) assert(s is not None) assert(f.__class__.__name__ == 'Logger') assert(s.__class__.__name__ == 'Logger') assert(f.name == 'f') assert(s.name == 's') assert(len(s.handlers) == 1) assert(len(f.handlers) >= 1) # End test_11_log_file def test_12_get_filename_regex(): """"""Verify that we get a compiled filename regex from get_filename_regex."""""" import re r = pydbvolve.get_filename_regex() assert(r is not None) assert(r.__class__.__name__ == 'SRE_Pattern') # End test_12_get_filename_regex def test_13_pre_config(): res = None try: res = pydbvolve.get_db_credentials(None) except Exception as e: assert(e.__class__.__name__ == 'NotImplementedError') assert(res is None) try: res = pydbvolve.get_db_connection(None, None) except Exception as f: assert(f.__class__.__name__ == 'NotImplementedError') assert(res is None) # End test_13_pre_config def test_14_load_config(): """"""Load the test config file and verify that it overrides the speficied functions."""""" try: os.unlink(TEST_DB_FILE) except: pass old_get_db_credentials = pydbvolve.get_db_credentials old_get_base_dir = pydbvolve.get_base_dir old_get_db_connection = pydbvolve.get_db_connection old_get_positional_variable_marker = pydbvolve.get_positional_variable_marker old_get_migration_table_schema = pydbvolve.get_migration_table_schema pydbvolve.load_config(TEST_CONFIG_FILE) assert(old_get_db_credentials != pydbvolve.get_db_credentials) assert(old_get_base_dir != pydbvolve.get_base_dir) assert(old_get_db_connection != pydbvolve.get_db_connection) assert(old_get_positional_variable_marker != pydbvolve.get_positional_variable_marker) assert(old_get_migration_table_schema != pydbvolve.get_migration_table_schema) # End test_load_config def test_15_run_config(): """"""Verify we get a config dict with all expected keys present and having no None values."""""" def get_migration_table_schema(): return ""public"" pydbvolve.load_config(TEST_CONFIG_FILE) pydbvolve.get_migration_table_schema = get_migration_table_schema config = pydbvolve.new_config() config.update({'migration_action': 'info', 'version': pydbvolve.LATEST_VERSION, 'migration_user': pydbvolve.get_migration_user(config), 'sequential': False, 'config_file_path': TEST_CONFIG_FILE, 'verbose': False}) pydbvolve.run_config(config) for key in ('base_dir', 'migration_dir', 'migration_upgrade_dir', 'migration_downgrade_dir', 'filename_regex', 'log_dir', 'migration_table_schema', 'migration_table_name', 'positional_variable_marker', 'migration_action', 'version', 'migration_user', 'sequential', 'verbose'): assert(key in config) assert(config[key] is not None) importlib.reload(pydbvolve) # End test_15_run_config def test_16_confirm_dirs(): pydbvolve.load_config(TEST_CONFIG_FILE) config = pydbvolve.new_config() config.update({'migration_action': 'info', 'version': pydbvolve.LATEST_VERSION, 'migration_user': pydbvolve.get_migration_user(config), 'sequential': False, 'config_file_path': TEST_CONFIG_FILE, 'verbose': False}) pydbvolve.run_config(config) pydbvolve.confirm_dirs(config) for dirname in (k for k in config.keys() if k.endswith('_dir')): assert(os.path.exists(config[dirname])) # End test_16_confirm_dirs def test_17_get_db_credentials(): """"""Test the return from get_db_credentials call"""""" pydbvolve.load_config(TEST_CONFIG_FILE) config = pydbvolve.new_config() config.update({'migration_action': 'info', 'version': pydbvolve.LATEST_VERSION, 'migration_user': pydbvolve.get_migration_user(config), 'sequential': False, 'config_file_path': TEST_CONFIG_FILE, 'verbose': False}) pydbvolve.run_config(config) cred = pydbvolve.get_db_credentials(config) assert(cred is not None) assert(isinstance(cred, dict)) assert(len(cred) > 0) # End test_17_get_db_credentials def test_18_get_database_user(): pydbvolve.load_config(TEST_CONFIG_FILE) config = pydbvolve.new_config() config.update({'migration_action': 'info', 'version': pydbvolve.LATEST_VERSION, 'migration_user': pydbvolve.get_migration_user(config), 'sequential': False, 'config_file_path': TEST_CONFIG_FILE, 'verbose': False}) pydbvolve.run_config(config) cred = pydbvolve.get_db_credentials(config) dbuser = pydbvolve.get_database_user(config, cred) assert(dbuser is not None) assert(isinstance(dbuser, str)) assert(len(dbuser) > 0) # End test_18_get_database_user def test_19_connect_db(): """"""Verify that we can get a connection based on the definition of the get_db_connection config function."""""" pydbvolve.load_config(TEST_CONFIG_FILE) config = pydbvolve.new_config() config.update({'migr", https://github.com/Teamworksapp/pydbvolve,e18bb8cf98997c52de0cf30a8e106c480ba0bb57,tests/unittests/test_00_initialization.py::test_13_pre_config,tests/unittests/test_00_initialization.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import sys import sqlite3 import os import sys import importlib # Set path to force the import of the local module sys.path.insert(1, os.path.abspath('.')) import pydbvolve TEST_CONFIG_FILE = os.path.abspath(os.path.join('tests', 'pydbvolve.conf')) TEST_DB_FILE = os.path.abspath(os.path.join('tests', 'test_db.sqlite')) def test_00_local_module(capsys): """"""Verify that we are using the local module."""""" with capsys.disabled(): #print(pydbvolve.__path__) assert('site-packages' not in '|'.join(pydbvolve.__path__)) assert('dist-packages' not in '|'.join(pydbvolve.__path__)) # End test_00_local_module def test_01_new_config(): """"""Verify that the return from new_config call is an instance of dict"""""" assert(isinstance(pydbvolve.new_config(), dict)) # End test_new_config def test_02_get_migration_user(): """"""Verify that the get_migration_user returns a non-empty string"""""" muser = pydbvolve.get_migration_user({}) assert(muser is not None) assert(isinstance(muser, str)) assert(len(muser) > 0) # End test_get_migration_user def test_03_get_base_dir(): """"""Verify that we get a non-Falsey return from get_base_dir."""""" bdir = pydbvolve.get_base_dir(TEST_CONFIG_FILE) assert(bdir is not None) assert(isinstance(bdir, str)) assert(len(bdir) > 0) # End test_03_get_base_dir def test_04_get_migration_base_dir(): """"""Verify that we get a non-Falsey return from get_migration_base_dir."""""" mdir = pydbvolve.get_migration_base_dir(pydbvolve.get_base_dir(TEST_CONFIG_FILE)) assert(mdir is not None) assert(isinstance(mdir, str)) assert(len(mdir) > 0) assert(mdir.startswith(pydbvolve.get_base_dir(TEST_CONFIG_FILE))) # End test_04_get_migration_base_dir def test_05_get_migration_upgrade_dir(): """"""Verify that we get a non-Falsey return from get_migration_upgrade_dir."""""" mdir = pydbvolve.get_migration_base_dir(pydbvolve.get_base_dir(TEST_CONFIG_FILE)) udir = pydbvolve.get_migration_upgrade_dir(mdir) assert(udir is not None) assert(isinstance(udir, str)) assert(len(udir) > 0) assert(udir.startswith(mdir)) # End test_05_get_migration_upgrade_dir def test_06_get_migration_downgrade_dir(): """"""Verify that we get a non-Falsey return from get_migration_downgrade_dir."""""" mdir = pydbvolve.get_migration_base_dir(pydbvolve.get_base_dir(TEST_CONFIG_FILE)) ddir = pydbvolve.get_migration_downgrade_dir(mdir) assert(ddir is not None) assert(isinstance(ddir, str)) assert(len(ddir) > 0) assert(ddir.startswith(mdir)) # End test_06_get_migration_downgrade_dir def test_07_get_log_dir(): """"""Verify that we get a non-Falsey return from get_log_dir."""""" bdir = pydbvolve.get_base_dir(TEST_CONFIG_FILE) ldir = pydbvolve.get_log_dir(bdir) assert(ldir is not None) assert(isinstance(ldir, str)) assert(len(ldir) > 0) assert(ldir.startswith(bdir)) # End test_07_get_log_dir def test_08_get_migration_table_name(): """"""Verify that we get a non-Falsey return from get_migration_table_name."""""" table_name = pydbvolve.get_migration_table_name() assert(table_name is not None) assert(isinstance(table_name, str)) assert(len(table_name) > 0) # End test_08_get_migration_table_name def test_09_get_migration_table_schema(): """"""Verify that we get a non-Falsey return from get_migration_table_schema."""""" schema = pydbvolve.get_migration_table_schema() assert(schema is not None) assert(isinstance(schema, str)) assert(len(schema) > 0) # End test_09_get_migration_table_name def test_10_get_positional_variable_marker(): """"""Verify that we get back a non-Falsey return from get_positional_variable_marker."""""" marker = pydbvolve.get_positional_variable_marker() assert(marker is not None) assert(isinstance(marker, str)) assert(len(marker) > 0) # End test_10_get_positional_variable_marker def test_11_log_file(capsys): """"""Verify that we can open a log file."""""" with capsys.disabled(): config = {'log_file_name': 'tmp.log', 'logger_name': 'f', 'version': 'r1.0.0', 'migration_action': 'info'} pydbvolve.setup_file_logger(config) f = config['logger'] config['log_file_name'] = None config['logger_name'] = 's' pydbvolve.setup_stream_logger(config) s = config['logger'] assert(f is not None) assert(s is not None) assert(f.__class__.__name__ == 'Logger') assert(s.__class__.__name__ == 'Logger') assert(f.name == 'f') assert(s.name == 's') assert(len(s.handlers) == 1) assert(len(f.handlers) >= 1) # End test_11_log_file def test_12_get_filename_regex(): """"""Verify that we get a compiled filename regex from get_filename_regex."""""" import re r = pydbvolve.get_filename_regex() assert(r is not None) assert(r.__class__.__name__ == 'SRE_Pattern') # End test_12_get_filename_regex def test_13_pre_config(): res = None try: res = pydbvolve.get_db_credentials(None) except Exception as e: assert(e.__class__.__name__ == 'NotImplementedError') assert(res is None) try: res = pydbvolve.get_db_connection(None, None) except Exception as f: assert(f.__class__.__name__ == 'NotImplementedError') assert(res is None) # End test_13_pre_config def test_14_load_config(): """"""Load the test config file and verify that it overrides the speficied functions."""""" try: os.unlink(TEST_DB_FILE) except: pass old_get_db_credentials = pydbvolve.get_db_credentials old_get_base_dir = pydbvolve.get_base_dir old_get_db_connection = pydbvolve.get_db_connection old_get_positional_variable_marker = pydbvolve.get_positional_variable_marker old_get_migration_table_schema = pydbvolve.get_migration_table_schema pydbvolve.load_config(TEST_CONFIG_FILE) assert(old_get_db_credentials != pydbvolve.get_db_credentials) assert(old_get_base_dir != pydbvolve.get_base_dir) assert(old_get_db_connection != pydbvolve.get_db_connection) assert(old_get_positional_variable_marker != pydbvolve.get_positional_variable_marker) assert(old_get_migration_table_schema != pydbvolve.get_migration_table_schema) # End test_load_config def test_15_run_config(): """"""Verify we get a config dict with all expected keys present and having no None values."""""" def get_migration_table_schema(): return ""public"" pydbvolve.load_config(TEST_CONFIG_FILE) pydbvolve.get_migration_table_schema = get_migration_table_schema config = pydbvolve.new_config() config.update({'migration_action': 'info', 'version': pydbvolve.LATEST_VERSION, 'migration_user': pydbvolve.get_migration_user(config), 'sequential': False, 'config_file_path': TEST_CONFIG_FILE, 'verbose': False}) pydbvolve.run_config(config) for key in ('base_dir', 'migration_dir', 'migration_upgrade_dir', 'migration_downgrade_dir', 'filename_regex', 'log_dir', 'migration_table_schema', 'migration_table_name', 'positional_variable_marker', 'migration_action', 'version', 'migration_user', 'sequential', 'verbose'): assert(key in config) assert(config[key] is not None) importlib.reload(pydbvolve) # End test_15_run_config def test_16_confirm_dirs(): pydbvolve.load_config(TEST_CONFIG_FILE) config = pydbvolve.new_config() config.update({'migration_action': 'info', 'version': pydbvolve.LATEST_VERSION, 'migration_user': pydbvolve.get_migration_user(config), 'sequential': False, 'config_file_path': TEST_CONFIG_FILE, 'verbose': False}) pydbvolve.run_config(config) pydbvolve.confirm_dirs(config) for dirname in (k for k in config.keys() if k.endswith('_dir')): assert(os.path.exists(config[dirname])) # End test_16_confirm_dirs def test_17_get_db_credentials(): """"""Test the return from get_db_credentials call"""""" pydbvolve.load_config(TEST_CONFIG_FILE) config = pydbvolve.new_config() config.update({'migration_action': 'info', 'version': pydbvolve.LATEST_VERSION, 'migration_user': pydbvolve.get_migration_user(config), 'sequential': False, 'config_file_path': TEST_CONFIG_FILE, 'verbose': False}) pydbvolve.run_config(config) cred = pydbvolve.get_db_credentials(config) assert(cred is not None) assert(isinstance(cred, dict)) assert(len(cred) > 0) # End test_17_get_db_credentials def test_18_get_database_user(): pydbvolve.load_config(TEST_CONFIG_FILE) config = pydbvolve.new_config() config.update({'migration_action': 'info', 'version': pydbvolve.LATEST_VERSION, 'migration_user': pydbvolve.get_migration_user(config), 'sequential': False, 'config_file_path': TEST_CONFIG_FILE, 'verbose': False}) pydbvolve.run_config(config) cred = pydbvolve.get_db_credentials(config) dbuser = pydbvolve.get_database_user(config, cred) assert(dbuser is not None) assert(isinstance(dbuser, str)) assert(len(dbuser) > 0) # End test_18_get_database_user def test_19_connect_db(): """"""Verify that we can get a connection based on the definition of the get_db_connection config function."""""" pydbvolve.load_config(TEST_CONFIG_FILE) config = pydbvolve.new_config() config.update({'migr", https://github.com/Teamworksapp/pydbvolve,e18bb8cf98997c52de0cf30a8e106c480ba0bb57,tests/unittests/test_01_migration_table.py::test_02_check_migration_table_bad_structure,tests/unittests/test_01_migration_table.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import os import sys import datetime import os import sys # Set path to force the import of the local module sys.path.insert(1, os.path.abspath('.')) import pydbvolve TEST_CONFIG_FILE = os.path.join('tests', 'pydbvolve.conf') def _drop_migration_table(config): conn = config['conn'] # remove the table if it exists sql = """""" select * from sqlite_master where tbl_name = ?; """""" with conn.cursor() as cur: cur.execute(sql, (config['migration_table_name'], )) res = cur.fetchone() if res: sql = ''' drop table {}; '''.format(config['migration_table_name']) with config['conn'].cursor() as cur: cur.execute(sql) config['conn'].commit() # End _drop_migration_table def _create_bad_migration_table(config): conn = config['conn'] sql = ''' create table {}""{}"" (id integer, shooba text, dooba text); '''.format(config.get('migration_table_schema', ''), config['migration_table_name']) with conn.cursor() as cur: cur.execute(sql) conn.commit() # End _create_bad_migration_table def _empty_migation_table(config): conn = config['conn'] sql = ''' delete from {}""{}""; '''.format(config.get('migration_table_schema', ''), config['migration_table_name']) with conn.cursor() as cur: cur.execute(sql) conn.commit() # End _empty_migation_table def _add_migration_record(config, record): conn = config['conn'] sql = """""" insert into {}""{}"" ( {} ) values ( {} ) """""".format(config.get('migration_table_schema', ''), config['migration_table_name'], ', '.join(pydbvolve.VALID_COLUMNS), ', '.join([config['positional_variable_marker']] * len(pydbvolve.VALID_COLUMNS))) values = tuple(record.get(c, '') for c in pydbvolve.VALID_COLUMNS) with conn.cursor() as cur: cur.execute(sql, values) conn.commit() # End _add_migration_record def _count_current(config): conn = config['conn'] sql = """""" select count(*) as ""count"" from {}""{}"" where is_current = 1; """""".format(config.get('migration_table_schema', ''), config['migration_table_name']) with conn.cursor() as cur: cur.execute(sql) res = cur.fetchone().get('count') return res # End _count_current def _count_baseline(config): conn = config['conn'] sql = """""" select count(*) as ""count"" from {}""{}"" where is_baseline = 1; """""".format(config.get('migration_table_schema', ''), config['migration_table_name']) with conn.cursor() as cur: cur.execute(sql) res = cur.fetchone().get('count') return res # End _count_current def test_00_local_module(capsys): """"""Verify that we are using the local module."""""" with capsys.disabled(): #print(pydbvolve.__path__) assert('site-packages' not in '|'.join(pydbvolve.__path__)) assert('dist-packages' not in '|'.join(pydbvolve.__path__)) # End test_00_local_module def test_01_check_migration_table_init(): """"""Verify that the migration table can be detected"""""" config = pydbvolve.initialize(TEST_CONFIG_FILE, 'info', 'r1.1.10', True, False) _drop_migration_table(config) res = pydbvolve.check_migration_table(config) assert(res == False) # End test_01_check_migration_table_init def test_02_check_migration_table_bad_structure(): """"""Verify that an aberrant table structure can be detected"""""" config = pydbvolve.initialize(TEST_CONFIG_FILE, 'info', 'r1.1.10', True, False) _create_bad_migration_table(config) exc = None try: res = pydbvolve.check_migration_table(config) except Exception as e: exc = e assert(isinstance(exc, pydbvolve.MigrationTableOutOfSync)) # End test_02_check_migration_table_bad_structure def test_03_create_migration_table(): """"""Verify that the migration table can be created"""""" config = pydbvolve.initialize(TEST_CONFIG_FILE, 'info', 'r1.1.10', True, False) _drop_migration_table(config) res = pydbvolve.create_migration_table(config) assert(res == True) assert(pydbvolve.check_migration_table(config)) res = pydbvolve.create_migration_table(config) assert(res == False) # End test_03_create_migration_table def test_04_check_migration_table_bad_current(): """"""Verify that multiple current recurds are detected"""""" config = pydbvolve.initialize(TEST_CONFIG_FILE, 'info', 'r1.1.10', True, False) _drop_migration_table(config) res = pydbvolve.create_migration_table(config) assert(res) record = { 'version': 'r1.0.0', 'applied_ts': datetime.datetime.now(), 'migration_file': 'somefile.sql', 'migration_type': 'sql', 'migration_user': config['migration_user'], 'db_user': config['db_user'], 'is_current': 1, 'is_baseline': 0 } _add_migration_record(config, record) record['version'] = 'r1.0.1' _add_migration_record(config, record) try: res = pydbvolve.check_migration_table(config) except Exception as e: res = False assert(isinstance(e, pydbvolve.MigrationTableConstraintError)) assert(res == False) # End test_04_check_migration_table_bad_current def test_05_check_migration_table_bad_baseline(): """"""Verify that multiple baseline recurds are detected"""""" config = pydbvolve.initialize(TEST_CONFIG_FILE, 'info', 'r1.1.10', True, False) _drop_migration_table(config) res = pydbvolve.create_migration_table(config) assert(res) record = { 'version': 'r1.0.0', 'applied_ts': datetime.datetime.now(), 'migration_file': 'somefile.sql', 'migration_type': 'sql', 'migration_user': config['migration_user'], 'db_user': config['db_user'], 'is_current': 0, 'is_baseline': 1 } _add_migration_record(config, record) record['version'] = 'r1.0.1' _add_migration_record(config, record) try: res = pydbvolve.check_migration_table(config) except Exception as e: res = False assert(isinstance(e, pydbvolve.MigrationTableConstraintError)) assert(res == False) # End test_04_check_migration_table_bad_current def test_06_clear_current(): """"""Verify that the current record flag can be cleared"""""" config = pydbvolve.initialize(TEST_CONFIG_FILE, 'info', 'r1.1.10', True, False) _drop_migration_table(config) res = pydbvolve.create_migration_table(config) assert(res) record = { 'version': 'r1.0.0', 'applied_ts': datetime.datetime.now(), 'migration_file': 'somefile.sql', 'migration_type': 'sql', 'migration_user': config['migration_user'], 'db_user': config['db_user'], 'is_current': 1, 'is_baseline': 0 } _add_migration_record(config, record) assert(_count_current(config) == 1) res = pydbvolve.clear_current(config) assert(res) assert(_count_current(config) == 0) config['conn'].commit() _drop_migration_table(config) res = pydbvolve.clear_current(config) assert(res == False) # End test_05_clear_current def test_07_clear_baseline(): """"""Verify that the baseline record flag can be cleared"""""" config = pydbvolve.initialize(TEST_CONFIG_FILE, 'info', 'r1.1.10', True, False) _drop_migration_table(config) res = pydbvolve.create_migration_table(config) assert(res) record = { 'version': 'r1.0.0', 'applied_ts': datetime.datetime.now(), 'migration_file': 'somefile.sql', 'migration_type': 'sql', 'migration_user': config['migration_user'], 'db_user': config['db_user'], 'is_current': 0, 'is_baseline': 1 } _add_migration_record(config, record) assert(_count_baseline(config) == 1) res = pydbvolve.clear_baseline(config) assert(res) assert(_count_baseline(config) == 0) config['conn'].commit() _drop_migration_table(config) res = pydbvolve.clear_baseline(config) assert(res == False) # End test_05_clear_baseline def test_08_get_current(): """"""Verify that the current record can be retrieved"""""" config = pydbvolve.initialize(TEST_CONFIG_FILE, 'info', 'r1.1.10', True, False) _drop_migration_table(config) res = pydbvolve.create_migration_table(config) assert(res) record = { 'version': 'r1.0.0', 'applied_ts': datetime.datetime.now(), 'migration_file': 'somefile.sql', 'migration_type': 'sql', 'migration_user': config['migration_user'], 'db_user': config['db_user'], 'is_current': 1, 'is_baseline': 0 } res = pydbvolve.get_current(config) assert(res == {}) _add_migration_record(config, record) assert(_count_current(config) == 1) res = pydbvolve.get_current(config) assert(bool(res)) assert((res['version'], str(res['applied_ts'])) == (record['version'], str(record['applied_ts']))) _drop_migration_table(config) res = pydbvolve.get_current(config) assert(res == {}) # End test_07_get_current def test_09_get_baseline(): """"""Verify that the baseline record can be retrieved"""""" config = pydbvolve.initialize(TEST_CONFIG_FILE, 'info', 'r1.1.10', True, False) _drop_migration_table(config) res = pydbvolve.create_migration_table(config) assert(res) record = { 'version': 'r1.0.0', 'applied_ts': datetime.datetime.now(), 'migration_file': 'somefile.sql', 'migration_type': 'sql', 'migration_user': config['migration_user'], 'db_user': config['db_user'], 'is_current': 0, 'is_baseline': 1 } res = pydbvolve.get_baseline(config) assert(res == {}) _add_migration_record(config, record) assert(_count_baseline(config) == 1) res = pydbvolve.get_baseline(config) assert(bool(res)) assert((res['version'], str(res['applied_ts'])) == (record['v", https://github.com/Tedyst/HikLoad,b3eb7be65bd34040992e1b7b15f23ff4ce2e8dc4,tests/test_config.py::test_envvar,tests/test_config.py,OD-Brit,flaky,Deleted,nan,classify;root_cause,"import hikload.config as config def test_defaultvar(): config.CONFIG[""server""] = ""1"" assert config.CONFIG[""server""] == ""1"" def test_envvar(): import os os.environ[""server""] = ""1"" assert config.CONFIG[""server""] == ""1"" # Can't add a lot of tests since Hikvision's API is pretty unreliable ", https://github.com/Tencent/QTAF,c9141e8fbdfd89f5f13dd59dfab17f9e8193dcf8,tests/test_testbase/test_assert.py::AssertionTest::test_assert_failure,tests/test_testbase/test_assert.py,OD-Vic,flaky,nan,nan,classify;root_cause,"# -*- coding: utf-8 -*- # # Tencent is pleased to support the open source community by making QTA available. # Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. # Licensed under the BSD 3-Clause License (the ""License""); you may not use this # file except in compliance with the License. You may obtain a copy of the License at # # https://opensource.org/licenses/BSD-3-Clause # # Unless required by applicable law or agreed to in writing, software distributed # under the License is distributed on an ""AS IS"" basis, WITHOUT WARRANTIES OR CONDITIONS # OF ANY KIND, either express or implied. See the License for the specific language # governing permissions and limitations under the License. # """"""testcase for testbase.asserts """""" import unittest from testbase.testcase import TestCase from testbase.datadrive import DataDrive from testbase.test import modify_settings class AssertionFailureTest(TestCase): """"""dummy class for asserts test """""" owner = ""dummy"" timeout = 10 priority = TestCase.EnumPriority.High status = TestCase.EnumStatus.Ready def run_test(self): self.start_step(""step 1"") self.assert_(""assert1"", self.bar(1) in [1, 4]) self.start_step(""step 2"") self.assert_(""assert"", self.foo(self.bar(1)) in [2, 4]) def foo(self, a): return a + 1 def bar(self, b): return b + 1 class AssertionInnerInvokeTest(AssertionFailureTest): """"""xxxx """""" def run_test(self): self.assert_foo_bar() def assert_foo_bar(self): self.assert_(""assert"", self.foo(self.bar(1)) in [2, 4]) @DataDrive([1,2]) class AssertionDatadriveTest(AssertionFailureTest): """"""dummy class for asserts test """""" def run_test(self): self.assert_(""assert"", self.foo(self.bar(1)) in [2, 4]) class AssertionTest(unittest.TestCase): """"""unit test for assertion """""" def is_func_rewritten(self, new_func, old_code): new_code = new_func.__func__.__code__ return new_code != old_code def test_assert_failure(self): case = AssertionFailureTest() old_run_test_code = case.run_test.__func__.__code__ case.debug_run() self.assertEqual(case.test_result.passed, False, ""断言失败,用例没有失败"") self.assertEqual(len(case.test_result._step_results), 2, ""设置了断言失败继续执行,但是用例没有继续执行"") self.assertEqual(self.is_func_rewritten(case.run_test, old_run_test_code), True, ""重写assert失败,code对象没有改变"") def test_assert_inner_invoke(self): case = AssertionInnerInvokeTest() old_assert_foo_bar_code = case.assert_foo_bar.__func__.__code__ case.debug_run() self.assertEqual(case.test_result.passed, False, ""断言失败,用例没有失败"") self.assertEqual(self.is_func_rewritten(case.assert_foo_bar, old_assert_foo_bar_code), True, ""重写assert失败,code对象没有改变"") def test_assert_datadrive(self): case = AssertionDatadriveTest() old_run_test_code = case.run_test.__func__.__code__ report = case.debug_run_one(0) self.assertEqual(report.is_passed(), False, ""数据驱动断言失败,用例没有失败"") report = case.debug_run_one(1) self.assertEqual(report.is_passed(), False, ""数据驱动断言失败,用例没有失败"") self.assertEqual(self.is_func_rewritten(case.run_test, old_run_test_code), True, ""重写assert失败,code对象没有改变"") def test_disable_rewrite_assert(self): with modify_settings(QTAF_REWRITE_ASSERT=False): case = AssertionFailureTest() old_run_test_code = case.run_test.__func__.__code__ case.debug_run() self.assertEqual(case.test_result.passed, False, ""禁用重写assert,用例没有失败"") self.assertEqual(self.is_func_rewritten(case.run_test, old_run_test_code), False, ""禁用重写assert,assert_被重写了"") def test_disable_assert_failed_continue(self): with modify_settings(QTAF_ASSERT_CONTINUE=False): case = AssertionFailureTest() case.debug_run() self.assertEqual(case.test_result.passed, False, ""断言失败退出执行,用例没有失败"") self.assertEqual(len(case.test_result._step_results), 1, u""设置了断言失败退出执行,但是用例仍继续执行"") if __name__ == ""__main__"": unittest.main() ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_add_classification.py::TestAddClassification::test_add_classification,tests/test_add_classification.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause," import unittest try: from collections import OrderedDict except: # 2.6 from ordereddict import OrderedDict from cromulent import model, vocab from cromulent.model import factory class TestAddClassification(unittest.TestCase): def test_add_classification(self): amnt = model.MonetaryAmount(ident='') amnt.value = 7.0 self.assertNotIn('Asking Price', factory.toString(amnt)) vocab.add_classification(amnt, vocab.AskingPrice) self.assertIn('Asking Price', factory.toString(amnt)) if __name__ == '__main__': unittest.main() ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_currency.py::TestCurrencyExtraction::test_extract_est,tests/test_currency.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python3 import unittest try: from contextlib import suppress except: # Python 2.7 suppress = None import pprint from datetime import datetime from cromulent import model, vocab from cromulent.extract import extract_monetary_amount import cromulent.extract CUSTOM_MAPPING = { 'xxx': vocab.register_instance('xxx custom currency', {'parent': model.Currency, 'id': '999999999', 'label': 'My Dollars'}), 'zzz': 'us dollars' } class TestCurrencyExtraction(unittest.TestCase): ''' Test the ability to extract currency data. ''' def setUp(self): pass def tearDown(self): pass def test_extract_simple(self): e = extract_monetary_amount({ 'price': '10.0', 'currency': 'pounds' }) self.assertEqual(e.type, 'MonetaryAmount') self.assertEqual(e._label, '10.0 pounds') self.assertEqual(e.value, 10) c = e.currency self.assertEqual(c.type, 'Currency') self.assertEqual(c._label, 'British Pounds') def test_extract_comma_separated(self): e = extract_monetary_amount({ 'price': '1,280.5', 'currency': 'pounds' }) self.assertEqual(e.type, 'MonetaryAmount') self.assertEqual(e._label, '1,280.5 pounds') self.assertEqual(e.value, 1280.50) c = e.currency self.assertEqual(c.type, 'Currency') self.assertEqual(c._label, 'British Pounds') def test_extract_multiple_comma_separated(self): e = extract_monetary_amount({ 'price': '1,310,720.5', 'currency': 'pounds' }) self.assertEqual(e.type, 'MonetaryAmount') self.assertEqual(e._label, '1,310,720.5 pounds') self.assertEqual(e.value, 1310720.5) c = e.currency self.assertEqual(c.type, 'Currency') self.assertEqual(c._label, 'British Pounds') def test_extract_est(self): e = extract_monetary_amount({ 'est_price': '12.0', 'currency': 'pounds' }) self.assertEqual(e.value, 12) c = e.currency self.assertEqual(e.classified_as[0]._label, 'Estimated Price') self.assertEqual(e.currency._label, 'British Pounds') def test_extract_start(self): e = extract_monetary_amount({ 'start_price': '8.5', 'currency': 'pounds' }) self.assertEqual(e.value, 8.5) c = e.currency self.assertEqual(e.classified_as[0]._label, 'Starting Price') self.assertEqual(e.currency._label, 'British Pounds') def test_extract_custom_currency_key(self): d = { 'price': '7', 'currency': 'zzz' } with self.assertRaises(AttributeError): e = extract_monetary_amount(d) self.assertEqual(e.currency._label, 'Custom Currency') e = extract_monetary_amount(d, currency_mapping=CUSTOM_MAPPING) self.assertEqual(e.value, 7) self.assertEqual(e.currency._label, 'US Dollars') def test_extract_custom_currency_instance(self): d = { 'price': '7', 'currency': 'xxx' } with self.assertRaises(AttributeError): e = extract_monetary_amount(d) self.assertEqual(e.currency._label, 'Custom Currency') e = extract_monetary_amount(d, currency_mapping=CUSTOM_MAPPING) self.assertEqual(e.value, 7) self.assertEqual(e.currency._label, 'My Dollars') def test_extract_price_with_citation(self): d = { 'price': '7', 'currency': 'pounds', 'citation': 'crom test suite' } e = extract_monetary_amount(d, add_citations=True) self.assertEqual(e.value, 7) self.assertEqual(e.currency._label, 'British Pounds') self.assertEqual(e.referred_to_by[0].content, 'crom test suite') if __name__ == '__main__': unittest.main() ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_currency.py::TestCurrencyExtraction::test_extract_price_with_citation,tests/test_currency.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python3 import unittest try: from contextlib import suppress except: # Python 2.7 suppress = None import pprint from datetime import datetime from cromulent import model, vocab from cromulent.extract import extract_monetary_amount import cromulent.extract CUSTOM_MAPPING = { 'xxx': vocab.register_instance('xxx custom currency', {'parent': model.Currency, 'id': '999999999', 'label': 'My Dollars'}), 'zzz': 'us dollars' } class TestCurrencyExtraction(unittest.TestCase): ''' Test the ability to extract currency data. ''' def setUp(self): pass def tearDown(self): pass def test_extract_simple(self): e = extract_monetary_amount({ 'price': '10.0', 'currency': 'pounds' }) self.assertEqual(e.type, 'MonetaryAmount') self.assertEqual(e._label, '10.0 pounds') self.assertEqual(e.value, 10) c = e.currency self.assertEqual(c.type, 'Currency') self.assertEqual(c._label, 'British Pounds') def test_extract_comma_separated(self): e = extract_monetary_amount({ 'price': '1,280.5', 'currency': 'pounds' }) self.assertEqual(e.type, 'MonetaryAmount') self.assertEqual(e._label, '1,280.5 pounds') self.assertEqual(e.value, 1280.50) c = e.currency self.assertEqual(c.type, 'Currency') self.assertEqual(c._label, 'British Pounds') def test_extract_multiple_comma_separated(self): e = extract_monetary_amount({ 'price': '1,310,720.5', 'currency': 'pounds' }) self.assertEqual(e.type, 'MonetaryAmount') self.assertEqual(e._label, '1,310,720.5 pounds') self.assertEqual(e.value, 1310720.5) c = e.currency self.assertEqual(c.type, 'Currency') self.assertEqual(c._label, 'British Pounds') def test_extract_est(self): e = extract_monetary_amount({ 'est_price': '12.0', 'currency': 'pounds' }) self.assertEqual(e.value, 12) c = e.currency self.assertEqual(e.classified_as[0]._label, 'Estimated Price') self.assertEqual(e.currency._label, 'British Pounds') def test_extract_start(self): e = extract_monetary_amount({ 'start_price': '8.5', 'currency': 'pounds' }) self.assertEqual(e.value, 8.5) c = e.currency self.assertEqual(e.classified_as[0]._label, 'Starting Price') self.assertEqual(e.currency._label, 'British Pounds') def test_extract_custom_currency_key(self): d = { 'price': '7', 'currency': 'zzz' } with self.assertRaises(AttributeError): e = extract_monetary_amount(d) self.assertEqual(e.currency._label, 'Custom Currency') e = extract_monetary_amount(d, currency_mapping=CUSTOM_MAPPING) self.assertEqual(e.value, 7) self.assertEqual(e.currency._label, 'US Dollars') def test_extract_custom_currency_instance(self): d = { 'price': '7', 'currency': 'xxx' } with self.assertRaises(AttributeError): e = extract_monetary_amount(d) self.assertEqual(e.currency._label, 'Custom Currency') e = extract_monetary_amount(d, currency_mapping=CUSTOM_MAPPING) self.assertEqual(e.value, 7) self.assertEqual(e.currency._label, 'My Dollars') def test_extract_price_with_citation(self): d = { 'price': '7', 'currency': 'pounds', 'citation': 'crom test suite' } e = extract_monetary_amount(d, add_citations=True) self.assertEqual(e.value, 7) self.assertEqual(e.currency._label, 'British Pounds') self.assertEqual(e.referred_to_by[0].content, 'crom test suite') if __name__ == '__main__': unittest.main() ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_currency.py::TestCurrencyExtraction::test_extract_start,tests/test_currency.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python3 import unittest try: from contextlib import suppress except: # Python 2.7 suppress = None import pprint from datetime import datetime from cromulent import model, vocab from cromulent.extract import extract_monetary_amount import cromulent.extract CUSTOM_MAPPING = { 'xxx': vocab.register_instance('xxx custom currency', {'parent': model.Currency, 'id': '999999999', 'label': 'My Dollars'}), 'zzz': 'us dollars' } class TestCurrencyExtraction(unittest.TestCase): ''' Test the ability to extract currency data. ''' def setUp(self): pass def tearDown(self): pass def test_extract_simple(self): e = extract_monetary_amount({ 'price': '10.0', 'currency': 'pounds' }) self.assertEqual(e.type, 'MonetaryAmount') self.assertEqual(e._label, '10.0 pounds') self.assertEqual(e.value, 10) c = e.currency self.assertEqual(c.type, 'Currency') self.assertEqual(c._label, 'British Pounds') def test_extract_comma_separated(self): e = extract_monetary_amount({ 'price': '1,280.5', 'currency': 'pounds' }) self.assertEqual(e.type, 'MonetaryAmount') self.assertEqual(e._label, '1,280.5 pounds') self.assertEqual(e.value, 1280.50) c = e.currency self.assertEqual(c.type, 'Currency') self.assertEqual(c._label, 'British Pounds') def test_extract_multiple_comma_separated(self): e = extract_monetary_amount({ 'price': '1,310,720.5', 'currency': 'pounds' }) self.assertEqual(e.type, 'MonetaryAmount') self.assertEqual(e._label, '1,310,720.5 pounds') self.assertEqual(e.value, 1310720.5) c = e.currency self.assertEqual(c.type, 'Currency') self.assertEqual(c._label, 'British Pounds') def test_extract_est(self): e = extract_monetary_amount({ 'est_price': '12.0', 'currency': 'pounds' }) self.assertEqual(e.value, 12) c = e.currency self.assertEqual(e.classified_as[0]._label, 'Estimated Price') self.assertEqual(e.currency._label, 'British Pounds') def test_extract_start(self): e = extract_monetary_amount({ 'start_price': '8.5', 'currency': 'pounds' }) self.assertEqual(e.value, 8.5) c = e.currency self.assertEqual(e.classified_as[0]._label, 'Starting Price') self.assertEqual(e.currency._label, 'British Pounds') def test_extract_custom_currency_key(self): d = { 'price': '7', 'currency': 'zzz' } with self.assertRaises(AttributeError): e = extract_monetary_amount(d) self.assertEqual(e.currency._label, 'Custom Currency') e = extract_monetary_amount(d, currency_mapping=CUSTOM_MAPPING) self.assertEqual(e.value, 7) self.assertEqual(e.currency._label, 'US Dollars') def test_extract_custom_currency_instance(self): d = { 'price': '7', 'currency': 'xxx' } with self.assertRaises(AttributeError): e = extract_monetary_amount(d) self.assertEqual(e.currency._label, 'Custom Currency') e = extract_monetary_amount(d, currency_mapping=CUSTOM_MAPPING) self.assertEqual(e.value, 7) self.assertEqual(e.currency._label, 'My Dollars') def test_extract_price_with_citation(self): d = { 'price': '7', 'currency': 'pounds', 'citation': 'crom test suite' } e = extract_monetary_amount(d, add_citations=True) self.assertEqual(e.value, 7) self.assertEqual(e.currency._label, 'British Pounds') self.assertEqual(e.referred_to_by[0].content, 'crom test suite') if __name__ == '__main__': unittest.main() ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestAutoIdentifiers::test_no_ident,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestBaseResource::test_allows_multiple,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestBaseResource::test_check_prop,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestBaseResource::test_check_reference,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestBaseResource::test_init_params,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestBaseResource::test_list_my_props,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestBaseResource::test_multiplicity,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_breadth,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_collapse_json,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_ordering,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_pipe_scoped,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_production_mode,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_recursion,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_string_list,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_toFile,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_toJSON,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_toJSON_fast,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_toJSON_normal,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_toString,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestFactorySerialization::test_toString_fast,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestMagicMethods::test_not_multiple_instance,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestMagicMethods::test_validate_multiplicity,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestMagicMethods::test_validate_profile_off,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestMagicMethods::test_validation_wrong_type,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_model.py::TestPropertyCache::test_cache_hierarchy,tests/test_model.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os import shutil import json import pickle from collections import OrderedDict from cromulent import model, vocab from cromulent.model import override_okay class TestFactorySetup(unittest.TestCase): def setUp(self): model.factory.base_url = 'http://data.getty.edu/provenance/' model.factory.base_dir = 'tests/provenance_base_dir' model.factory.default_lang = 'en' #model.factory.context_uri = 'http://www.cidoc-crm.org/cidoc-crm/' def tearDown(self): model.factory.base_url = 'http://lod.example.org/museum/' model.factory.log_stream = sys.stderr model.factory.debug_level = 'warn' def test_base_url(self): self.assertEqual(model.factory.base_url, 'http://data.getty.edu/provenance/') def test_base_dir(self): self.assertEqual(model.factory.base_dir, 'tests/provenance_base_dir') def test_default_lang(self): self.assertEqual(model.factory.default_lang, 'en') def test_set_debug_stream(self): strm = open('err_output', 'w') model.factory.set_debug_stream(strm) self.assertEqual(model.factory.log_stream, strm) def test_set_debug(self): model.factory.set_debug('error_on_warning') self.assertEqual(model.factory.debug_level, 'error_on_warning') self.assertRaises(model.ConfigurationError, model.factory.set_debug, 'xxx') self.assertRaises(model.MetadataError, model.factory.maybe_warn, ""test"") def test_load_context(self): self.assertRaises(model.ConfigurationError, model.factory.load_context, ""foo"", {""foo"":""does_not_exist.txt""}) model.factory.load_context(""foo"", {""foo"":""tests/test_context.json""}) self.assertEqual(model.factory.context_json, {""@context"":{""id"":""@id""}}) self.assertRaises(model.ConfigurationError, model.factory.load_context, """", {}) def test_pickle(self): model.factory.log_stream = sys.stderr srlz = pickle.dumps(model.factory) newfac = pickle.loads(srlz) self.assertTrue(model.factory.log_stream is newfac.log_stream) class TestFactorySerialization(unittest.TestCase): def setUp(self): self.collection = model.InformationObject('collection') self.collection._label = ""Test Object"" def test_broken_unicode(self): model.factory.debug_level = ""error_on_warning"" try: badval = b""\xFF\xFE\x02"" except: badval = ""\xFF\xFE\x02"" badjs = {""_label"": badval} self.assertRaises(model.MetadataError, model.factory._buildString, js=badjs) def test_toJSON(self): # model.factory.context_uri = 'http://lod.getty.edu/context.json' expect = OrderedDict([ ('@context', model.factory.context_uri), ('id', u'http://lod.example.org/museum/InformationObject/collection'), ('type', 'InformationObject'), ('_label', 'Test Object')]) outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) def test_toJSON_fast(self): model.factory.json_serializer = ""fast"" expect = {'@context': model.factory.context_uri, 'id': 'http://lod.example.org/museum/InformationObject/collection', 'type': 'InformationObject', '_label': 'Test Object'} outj = model.factory.toJSON(self.collection) self.assertEqual(expect, outj) model.factory.json_serializer = ""normal"" def test_toJSON_normal(self): expect = OrderedDict([(u'@context', model.factory.context_uri), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.full_names = True p = model.Person(""1"") p._label = ""Test Person"" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False def test_toString(self): expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' outs = model.factory.toString(self.collection) self.assertEqual(expect, outs) def test_toString_fast(self): # Should only be trusted in python 3 if sys.version_info.major >= 3 and sys.version_info.minor >= 6: expect = u'{""@context"":""'+model.factory.context_uri+'"",""id"":""http://lod.example.org/museum/InformationObject/collection"",""type"":""InformationObject"",""_label"":""Test Object""}' model.factory.json_serializer = ""fast"" outs = model.factory.toString(self.collection) model.factory.json_serializer = ""normal"" self.assertEqual(expect, outs) else: print(""Skipping toString_fast test in Python 2.x"") def test_toFile(self): self.assertRaises(model.ConfigurationError, model.factory.toFile, self.collection) # Test auto filename determination model.factory.base_dir = 'tests' model.factory.toFile(self.collection) self.assertTrue(os.path.isfile('tests/InformationObject/collection.json')) # Test explicit filename setting model.factory.toFile(self.collection, filename='tests/fishbat.bar') self.assertTrue(os.path.isfile('tests/fishbat.bar')) # Tidy up shutil.rmtree('tests/InformationObject') def test_breadth(self): x = model.TransferOfCustody() e = model.Activity() fr = model.Group() to = model.Group() w = model.HumanMadeObject() fr._label = ""From"" to._label = ""To"" x.transferred_custody_of = w x.transferred_custody_from = fr x.transferred_custody_to = to e.used_specific_object = w e.carried_out_by = to w.current_owner = fr x.specific_purpose = e js = model.factory.toJSON(x) # Okay ... if we're breadth first, then custody_from is a resource # And now it's the first in the list self.assertTrue(isinstance(js['transferred_custody_from'][0], OrderedDict)) def test_string_list(self): x = model.Activity() x._label = [""Label 1"", ""Label 2""] js = model.factory.toJSON(x) self.assertTrue(js['_label'] == x._label) def test_external(self): x = model.ExternalResource(ident=""1"") model.factory.elasticsearch_compatible = 1 js = x._toJSON(done=None) self.assertTrue(type(js) == dict) model.factory.elasticsearch_compatible = 0 js = x._toJSON(done=None) # testing unicode in 2, str in 3 :( self.assertTrue(type(js) != dict) def test_recursion(self): x = model.Activity() x.part = x js = model.factory.toJSON(x) # If our recursion checks have regressed, this will barf right here self.assertTrue(1) def test_pipe_scoped(self): x = model.Activity() y = model.Activity() x.part = y model.factory.pipe_scoped_contexts = True js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' in js) model.factory.pipe_scoped_contexts = False js = model.factory.toJSON(x) self.assertTrue('part|crm:P9_consists_of' not in js) self.assertTrue('part' in js) def test_collapse_json(self): model.factory.auto_id_type = ""uuid"" model.factory.base_url = ""http://lod.example.org/museum/"" model.factory.context_uri = ""https://linked.art/ns/v1/linked-art.json"" p = model.Person() p.classified_as = model.Type(ident=""http://example.org/Type"", label=""Test"") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6) def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value=""abc"") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False) def test_ordering(self): p = model.Person(label=""Person"") p.classified_as = model.Type(ident=""type-uri"") p.referred_to_by = model.LinguisticObject(content=""text"") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index(""_label"") clsf = outstr.index(""classified_as"") r2b = outstr.index(""referred_to_by"") dim = outstr.index(""dimension"") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim) class TestProcessTSV(unittest.TestCase): def test_process_tsv(self): expect = {u'subs': [u'E84_Information_Carrier'], u'label': u'Human-Made Object', u'className': u'HumanMadeObject', u'subOf': u'E19_Physical_Object|E24_Physical_Human-Made_Thing', u'props': [], u'class': None, u'okay': u'1'} fn = 'cromulent/data/crm_vocab.tsv' vocabData = model.process_tsv(fn) man_made = vocabData['E22_Human-Made_Object'] del man_made['desc'] # too long and volatile # check subs specifically - could be coming from an extension if man_made['subs'] != expect['subs']: del man_made['subs'] del expect['subs'] self.assertEqual(expect, man_made) class TestBuildClasses(unittest.TestCase): def test_build_classes(self): tsv = ""\nClassName_full\tclass\tClassName_py\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() model.build_classes(""tests/temp.tsv"", ""ClassName_full"") from cromulent.model import ClassName_py self.assertEqual('Class Description', ClassName_py.__doc__) os.remove('tests/temp.tsv') class TestBuildClass(unittest.TestCase): def test_build_class(self): tsv = ""\nClassName_full\tclass\tClassName_py2\tClass Label\tClass Description\t\t1\t\n"" fh = open('tests/temp.tsv', 'w') fh.write(tsv) fh.close() vocabData = model.process_tsv('tests/temp.tsv') model.build_class('ClassName_full', model.BaseResource, vocabData) from cromulent.model import ClassName_py2 self.assertEqual('Class Description', ClassName_py2.__doc__) os.remove('tests/temp.tsv') class TestAutoIdentifiers(unittest.TestCase): def test_bad_autoid(self): model.factory.auto_assign_id = True model.factory.auto_id_type = ""broken"" self.assertRaises(model.ConfigurationError, model.factory.generate_id, ""irrelevant"") def test_int(self): model.factory.auto_assign_id = True model.", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_reader.py::TestReader::test_read,tests/test_reader.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause," import unittest try: from collections import OrderedDict except: # 2.6 from ordereddict import OrderedDict from cromulent import reader from cromulent.model import factory, Person, DataError, BaseResource, \ Dimension, override_okay, AttributeAssignment from cromulent import vocab class TestReader(unittest.TestCase): def setUp(self): self.reader = reader.Reader() # ensure we can use parent_of override_okay(Person, 'parent_of') # Person._properties['parent_of']['multiple'] = 1 def test_read(self): self.assertRaises(DataError, self.reader.read, """") self.assertRaises(DataError, self.reader.read, ""This is not JSON"") self.assertRaises(DataError, self.reader.read, ""{}"") whostr = '{""type"": ""Person"", ""_label"": ""me""}' self.assertTrue(isinstance(self.reader.read(whostr), Person)) whostr = '{""@context"": ""fishbat"", ""type"": ""Person"", ""_label"": ""me""}' self.assertTrue(isinstance(self.reader.read(whostr), Person)) levelstr = '{""type"": ""Person"", ""parent_of"": {""type"": ""Person"", ""_label"": ""child""}}' self.assertTrue(isinstance(self.reader.read(levelstr).parent_of[0], Person)) basestr = '{""_label"": ""base""}' self.assertTrue(isinstance(self.reader.read(basestr), BaseResource)) unknown = '{""type"":""FishBat""}' self.assertRaises(DataError, self.reader.read, unknown) unknown2 = '{""type"":""Person"", ""fishbat"": ""bob""}' self.assertRaises(DataError, self.reader.read, unknown) def test_attrib_assign(self): vocab.add_attribute_assignment_check() data = """""" { ""id"": ""https://linked.art/example/activity/12"", ""type"": ""AttributeAssignment"", ""assigned"": { ""id"": ""https://linked.art/example/name/10"", ""type"": ""Name"", ""content"": ""Exhibition Specific Name"" }, ""assigned_property"": ""identified_by"", ""assigned_to"": { ""id"": ""https://linked.art/example/object/12"", ""type"": ""HumanMadeObject"", ""_label"": ""Real Painting Name"" } } """""" d = self.reader.read(data) self.assertTrue(isinstance(d, AttributeAssignment)) def test_vocab_collision(self): # Test that the algorithm picks the right vocab instance # if multiple have the same AAT term but different base class data = """""" { ""type"": ""LinguisticObject"", ""_label"": ""Sale recorded in catalog: B-267 0003 (1817) (record number 22947)"", ""part_of"": [ { ""type"": ""LinguisticObject"", ""_label"": ""Sale Catalog B-267"", ""classified_as"": [ { ""id"": ""http://vocab.getty.edu/aat/300026068"", ""type"": ""Type"", ""_label"": ""Auction Catalog"" } ] } ] } """""" d = self.reader.read(data) self.assertTrue(isinstance(d.part_of[0], vocab.AuctionCatalogText)) ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_reader.py::TestReader::test_vocab_collision,tests/test_reader.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause," import unittest try: from collections import OrderedDict except: # 2.6 from ordereddict import OrderedDict from cromulent import reader from cromulent.model import factory, Person, DataError, BaseResource, \ Dimension, override_okay, AttributeAssignment from cromulent import vocab class TestReader(unittest.TestCase): def setUp(self): self.reader = reader.Reader() # ensure we can use parent_of override_okay(Person, 'parent_of') # Person._properties['parent_of']['multiple'] = 1 def test_read(self): self.assertRaises(DataError, self.reader.read, """") self.assertRaises(DataError, self.reader.read, ""This is not JSON"") self.assertRaises(DataError, self.reader.read, ""{}"") whostr = '{""type"": ""Person"", ""_label"": ""me""}' self.assertTrue(isinstance(self.reader.read(whostr), Person)) whostr = '{""@context"": ""fishbat"", ""type"": ""Person"", ""_label"": ""me""}' self.assertTrue(isinstance(self.reader.read(whostr), Person)) levelstr = '{""type"": ""Person"", ""parent_of"": {""type"": ""Person"", ""_label"": ""child""}}' self.assertTrue(isinstance(self.reader.read(levelstr).parent_of[0], Person)) basestr = '{""_label"": ""base""}' self.assertTrue(isinstance(self.reader.read(basestr), BaseResource)) unknown = '{""type"":""FishBat""}' self.assertRaises(DataError, self.reader.read, unknown) unknown2 = '{""type"":""Person"", ""fishbat"": ""bob""}' self.assertRaises(DataError, self.reader.read, unknown) def test_attrib_assign(self): vocab.add_attribute_assignment_check() data = """""" { ""id"": ""https://linked.art/example/activity/12"", ""type"": ""AttributeAssignment"", ""assigned"": { ""id"": ""https://linked.art/example/name/10"", ""type"": ""Name"", ""content"": ""Exhibition Specific Name"" }, ""assigned_property"": ""identified_by"", ""assigned_to"": { ""id"": ""https://linked.art/example/object/12"", ""type"": ""HumanMadeObject"", ""_label"": ""Real Painting Name"" } } """""" d = self.reader.read(data) self.assertTrue(isinstance(d, AttributeAssignment)) def test_vocab_collision(self): # Test that the algorithm picks the right vocab instance # if multiple have the same AAT term but different base class data = """""" { ""type"": ""LinguisticObject"", ""_label"": ""Sale recorded in catalog: B-267 0003 (1817) (record number 22947)"", ""part_of"": [ { ""type"": ""LinguisticObject"", ""_label"": ""Sale Catalog B-267"", ""classified_as"": [ { ""id"": ""http://vocab.getty.edu/aat/300026068"", ""type"": ""Type"", ""_label"": ""Auction Catalog"" } ] } ] } """""" d = self.reader.read(data) self.assertTrue(isinstance(d.part_of[0], vocab.AuctionCatalogText)) ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_vocab.py::TestClassBuilder::test_aa_check,tests/test_vocab.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os from cromulent import vocab, model from cromulent.model import factory class TestClassBuilder(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_class(self): vocab.register_aat_class(""TestObject1"", {""parent"": model.HumanMadeObject, ""id"": ""1"", ""label"": ""example 1""}) from cromulent.vocab import TestObject1 self.assertEqual(TestObject1._classification[0].id, 'http://vocab.getty.edu/aat/1') def test_instance(self): vocab.register_instance(""TestMaterial2"", {""parent"": model.Material, ""id"": ""2"", ""label"": ""example 2""}) self.assertTrue('TestMaterial2' in vocab.instances) tm2 = vocab.instances['TestMaterial2'] self.assertEqual(tm2.id, ""http://vocab.getty.edu/aat/2"") def test_metatype(self): vocab.register_instance(""example"", {""parent"": model.Type, ""id"": ""3"", ""label"": ""example type""}) vocab.register_aat_class(""TestObject2"", {""parent"": model.HumanMadeObject, ""id"": ""4"", ""label"": ""example typed object"", ""metatype"": ""example""}) from cromulent.vocab import TestObject2 self.assertEqual(TestObject2._classification[0].classified_as[0].id, 'http://vocab.getty.edu/aat/3') def test_multitype(self): from cromulent.vocab import make_multitype_obj, Painting, Drawing inst = make_multitype_obj(Painting, Drawing) self.assertTrue(isinstance(inst, Painting)) self.assertTrue(len(inst.classified_as) == 2) self.assertTrue(inst.classified_as[1].id == ""http://vocab.getty.edu/aat/300033973"") from cromulent.model import HumanMadeObject inst = make_multitype_obj(HumanMadeObject, Painting) self.assertTrue(len(inst.classified_as) == 1) self.assertTrue(inst.classified_as[0].id == ""http://vocab.getty.edu/aat/300033618"") def test_conceptual_parts(self): r = model.Right() r2 = model.Right() self.assertRaises(model.DataError, r.__setattr__, 'part', r2) r.c_part = r2 self.assertTrue(r2 in r.c_part) vocab.conceptual_only_parts() r3 = model.Right() r4 = model.Right() r3.part = r4 self.assertTrue(r4 in r3.c_part) self.assertTrue(""part"" in model.factory.toJSON(r3)) self.assertTrue(r4 in r3.part) def test_art_setter(self): p = model.HumanMadeObject(""a"", art=1) p._label = ""a"" pj = p._toJSON(done={}) self.assertFalse(pj.get('classified_as', None)) vocab.add_art_setter() p2 = vocab.Painting(""b"", art=1) p2j = p2._toJSON(done={}) def test_aa_check(self): # Make sure that some other test hasn't set it try: del model.AttributeAssignment.set_assigned_property except: pass t = model.Type() aa = model.AttributeAssignment() # First check that aa accepts a type aa.assigned_property = t # And will not accept a string self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") # Check we can set anything to assigned / assigned_to aa.assigned_property = None aa.assigned = aa aa.assigned_to = aa self.assertEqual(aa.assigned, aa) self.assertEqual(aa.assigned_to, aa) vocab.add_attribute_assignment_check() # This should fail right now as can't classify as an AA self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") aa.assigned = None aa.assigned_to = None aa.assigned = t aa.assigned_to = t aa.assigned_property = ""classified_as"" self.assertEqual(aa.assigned_property, 'classified_as') def test_boundary_setter(self): vocab.add_linked_art_boundary_check() p = model.Person() p2 = model.Person() n = model.Name() n.content = ""Test"" p2.identified_by = n p.exact_match = p2 # Now, Test should not appear in the resulting JSON of p factory.linked_art_boundaries = True js = factory.toJSON(p) self.assertTrue(not 'identified_by' in js['exact_match'][0]) factory.linked_art_boundaries = False js = factory.toJSON(p) self.assertTrue('identified_by' in js['exact_match'][0]) def test_procurement_boundary(self): vocab.add_linked_art_boundary_check() a = model.Activity() p = vocab.ProvenanceEntry() a.caused = p js = factory.toJSON(a) self.assertTrue(not 'classified_as' in js['caused'][0]) def test_linguistic_object_boundary(self): vocab.add_linked_art_boundary_check() jrnl = vocab.JournalText(label=""journal"") issue = vocab.IssueText(label=""issue"") issue.part_of = jrnl issue.referred_to_by = vocab.MaterialStatement(content=""Statement"") js = factory.toJSON(issue) # Have not embedded journal in issue self.assertTrue(not 'classified_as' in js['part_of'][0]) # Have embedded statement in issue self.assertTrue('content' in js['referred_to_by'][0]) self.assertTrue('type' in js['referred_to_by'][0]['classified_as'][0]['classified_as'][0]) ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_vocab.py::TestClassBuilder::test_boundary_setter,tests/test_vocab.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os from cromulent import vocab, model from cromulent.model import factory class TestClassBuilder(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_class(self): vocab.register_aat_class(""TestObject1"", {""parent"": model.HumanMadeObject, ""id"": ""1"", ""label"": ""example 1""}) from cromulent.vocab import TestObject1 self.assertEqual(TestObject1._classification[0].id, 'http://vocab.getty.edu/aat/1') def test_instance(self): vocab.register_instance(""TestMaterial2"", {""parent"": model.Material, ""id"": ""2"", ""label"": ""example 2""}) self.assertTrue('TestMaterial2' in vocab.instances) tm2 = vocab.instances['TestMaterial2'] self.assertEqual(tm2.id, ""http://vocab.getty.edu/aat/2"") def test_metatype(self): vocab.register_instance(""example"", {""parent"": model.Type, ""id"": ""3"", ""label"": ""example type""}) vocab.register_aat_class(""TestObject2"", {""parent"": model.HumanMadeObject, ""id"": ""4"", ""label"": ""example typed object"", ""metatype"": ""example""}) from cromulent.vocab import TestObject2 self.assertEqual(TestObject2._classification[0].classified_as[0].id, 'http://vocab.getty.edu/aat/3') def test_multitype(self): from cromulent.vocab import make_multitype_obj, Painting, Drawing inst = make_multitype_obj(Painting, Drawing) self.assertTrue(isinstance(inst, Painting)) self.assertTrue(len(inst.classified_as) == 2) self.assertTrue(inst.classified_as[1].id == ""http://vocab.getty.edu/aat/300033973"") from cromulent.model import HumanMadeObject inst = make_multitype_obj(HumanMadeObject, Painting) self.assertTrue(len(inst.classified_as) == 1) self.assertTrue(inst.classified_as[0].id == ""http://vocab.getty.edu/aat/300033618"") def test_conceptual_parts(self): r = model.Right() r2 = model.Right() self.assertRaises(model.DataError, r.__setattr__, 'part', r2) r.c_part = r2 self.assertTrue(r2 in r.c_part) vocab.conceptual_only_parts() r3 = model.Right() r4 = model.Right() r3.part = r4 self.assertTrue(r4 in r3.c_part) self.assertTrue(""part"" in model.factory.toJSON(r3)) self.assertTrue(r4 in r3.part) def test_art_setter(self): p = model.HumanMadeObject(""a"", art=1) p._label = ""a"" pj = p._toJSON(done={}) self.assertFalse(pj.get('classified_as', None)) vocab.add_art_setter() p2 = vocab.Painting(""b"", art=1) p2j = p2._toJSON(done={}) def test_aa_check(self): # Make sure that some other test hasn't set it try: del model.AttributeAssignment.set_assigned_property except: pass t = model.Type() aa = model.AttributeAssignment() # First check that aa accepts a type aa.assigned_property = t # And will not accept a string self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") # Check we can set anything to assigned / assigned_to aa.assigned_property = None aa.assigned = aa aa.assigned_to = aa self.assertEqual(aa.assigned, aa) self.assertEqual(aa.assigned_to, aa) vocab.add_attribute_assignment_check() # This should fail right now as can't classify as an AA self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") aa.assigned = None aa.assigned_to = None aa.assigned = t aa.assigned_to = t aa.assigned_property = ""classified_as"" self.assertEqual(aa.assigned_property, 'classified_as') def test_boundary_setter(self): vocab.add_linked_art_boundary_check() p = model.Person() p2 = model.Person() n = model.Name() n.content = ""Test"" p2.identified_by = n p.exact_match = p2 # Now, Test should not appear in the resulting JSON of p factory.linked_art_boundaries = True js = factory.toJSON(p) self.assertTrue(not 'identified_by' in js['exact_match'][0]) factory.linked_art_boundaries = False js = factory.toJSON(p) self.assertTrue('identified_by' in js['exact_match'][0]) def test_procurement_boundary(self): vocab.add_linked_art_boundary_check() a = model.Activity() p = vocab.ProvenanceEntry() a.caused = p js = factory.toJSON(a) self.assertTrue(not 'classified_as' in js['caused'][0]) def test_linguistic_object_boundary(self): vocab.add_linked_art_boundary_check() jrnl = vocab.JournalText(label=""journal"") issue = vocab.IssueText(label=""issue"") issue.part_of = jrnl issue.referred_to_by = vocab.MaterialStatement(content=""Statement"") js = factory.toJSON(issue) # Have not embedded journal in issue self.assertTrue(not 'classified_as' in js['part_of'][0]) # Have embedded statement in issue self.assertTrue('content' in js['referred_to_by'][0]) self.assertTrue('type' in js['referred_to_by'][0]['classified_as'][0]['classified_as'][0]) ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_vocab.py::TestClassBuilder::test_conceptual_parts,tests/test_vocab.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os from cromulent import vocab, model from cromulent.model import factory class TestClassBuilder(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_class(self): vocab.register_aat_class(""TestObject1"", {""parent"": model.HumanMadeObject, ""id"": ""1"", ""label"": ""example 1""}) from cromulent.vocab import TestObject1 self.assertEqual(TestObject1._classification[0].id, 'http://vocab.getty.edu/aat/1') def test_instance(self): vocab.register_instance(""TestMaterial2"", {""parent"": model.Material, ""id"": ""2"", ""label"": ""example 2""}) self.assertTrue('TestMaterial2' in vocab.instances) tm2 = vocab.instances['TestMaterial2'] self.assertEqual(tm2.id, ""http://vocab.getty.edu/aat/2"") def test_metatype(self): vocab.register_instance(""example"", {""parent"": model.Type, ""id"": ""3"", ""label"": ""example type""}) vocab.register_aat_class(""TestObject2"", {""parent"": model.HumanMadeObject, ""id"": ""4"", ""label"": ""example typed object"", ""metatype"": ""example""}) from cromulent.vocab import TestObject2 self.assertEqual(TestObject2._classification[0].classified_as[0].id, 'http://vocab.getty.edu/aat/3') def test_multitype(self): from cromulent.vocab import make_multitype_obj, Painting, Drawing inst = make_multitype_obj(Painting, Drawing) self.assertTrue(isinstance(inst, Painting)) self.assertTrue(len(inst.classified_as) == 2) self.assertTrue(inst.classified_as[1].id == ""http://vocab.getty.edu/aat/300033973"") from cromulent.model import HumanMadeObject inst = make_multitype_obj(HumanMadeObject, Painting) self.assertTrue(len(inst.classified_as) == 1) self.assertTrue(inst.classified_as[0].id == ""http://vocab.getty.edu/aat/300033618"") def test_conceptual_parts(self): r = model.Right() r2 = model.Right() self.assertRaises(model.DataError, r.__setattr__, 'part', r2) r.c_part = r2 self.assertTrue(r2 in r.c_part) vocab.conceptual_only_parts() r3 = model.Right() r4 = model.Right() r3.part = r4 self.assertTrue(r4 in r3.c_part) self.assertTrue(""part"" in model.factory.toJSON(r3)) self.assertTrue(r4 in r3.part) def test_art_setter(self): p = model.HumanMadeObject(""a"", art=1) p._label = ""a"" pj = p._toJSON(done={}) self.assertFalse(pj.get('classified_as', None)) vocab.add_art_setter() p2 = vocab.Painting(""b"", art=1) p2j = p2._toJSON(done={}) def test_aa_check(self): # Make sure that some other test hasn't set it try: del model.AttributeAssignment.set_assigned_property except: pass t = model.Type() aa = model.AttributeAssignment() # First check that aa accepts a type aa.assigned_property = t # And will not accept a string self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") # Check we can set anything to assigned / assigned_to aa.assigned_property = None aa.assigned = aa aa.assigned_to = aa self.assertEqual(aa.assigned, aa) self.assertEqual(aa.assigned_to, aa) vocab.add_attribute_assignment_check() # This should fail right now as can't classify as an AA self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") aa.assigned = None aa.assigned_to = None aa.assigned = t aa.assigned_to = t aa.assigned_property = ""classified_as"" self.assertEqual(aa.assigned_property, 'classified_as') def test_boundary_setter(self): vocab.add_linked_art_boundary_check() p = model.Person() p2 = model.Person() n = model.Name() n.content = ""Test"" p2.identified_by = n p.exact_match = p2 # Now, Test should not appear in the resulting JSON of p factory.linked_art_boundaries = True js = factory.toJSON(p) self.assertTrue(not 'identified_by' in js['exact_match'][0]) factory.linked_art_boundaries = False js = factory.toJSON(p) self.assertTrue('identified_by' in js['exact_match'][0]) def test_procurement_boundary(self): vocab.add_linked_art_boundary_check() a = model.Activity() p = vocab.ProvenanceEntry() a.caused = p js = factory.toJSON(a) self.assertTrue(not 'classified_as' in js['caused'][0]) def test_linguistic_object_boundary(self): vocab.add_linked_art_boundary_check() jrnl = vocab.JournalText(label=""journal"") issue = vocab.IssueText(label=""issue"") issue.part_of = jrnl issue.referred_to_by = vocab.MaterialStatement(content=""Statement"") js = factory.toJSON(issue) # Have not embedded journal in issue self.assertTrue(not 'classified_as' in js['part_of'][0]) # Have embedded statement in issue self.assertTrue('content' in js['referred_to_by'][0]) self.assertTrue('type' in js['referred_to_by'][0]['classified_as'][0]['classified_as'][0]) ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_vocab.py::TestClassBuilder::test_linguistic_object_boundary,tests/test_vocab.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os from cromulent import vocab, model from cromulent.model import factory class TestClassBuilder(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_class(self): vocab.register_aat_class(""TestObject1"", {""parent"": model.HumanMadeObject, ""id"": ""1"", ""label"": ""example 1""}) from cromulent.vocab import TestObject1 self.assertEqual(TestObject1._classification[0].id, 'http://vocab.getty.edu/aat/1') def test_instance(self): vocab.register_instance(""TestMaterial2"", {""parent"": model.Material, ""id"": ""2"", ""label"": ""example 2""}) self.assertTrue('TestMaterial2' in vocab.instances) tm2 = vocab.instances['TestMaterial2'] self.assertEqual(tm2.id, ""http://vocab.getty.edu/aat/2"") def test_metatype(self): vocab.register_instance(""example"", {""parent"": model.Type, ""id"": ""3"", ""label"": ""example type""}) vocab.register_aat_class(""TestObject2"", {""parent"": model.HumanMadeObject, ""id"": ""4"", ""label"": ""example typed object"", ""metatype"": ""example""}) from cromulent.vocab import TestObject2 self.assertEqual(TestObject2._classification[0].classified_as[0].id, 'http://vocab.getty.edu/aat/3') def test_multitype(self): from cromulent.vocab import make_multitype_obj, Painting, Drawing inst = make_multitype_obj(Painting, Drawing) self.assertTrue(isinstance(inst, Painting)) self.assertTrue(len(inst.classified_as) == 2) self.assertTrue(inst.classified_as[1].id == ""http://vocab.getty.edu/aat/300033973"") from cromulent.model import HumanMadeObject inst = make_multitype_obj(HumanMadeObject, Painting) self.assertTrue(len(inst.classified_as) == 1) self.assertTrue(inst.classified_as[0].id == ""http://vocab.getty.edu/aat/300033618"") def test_conceptual_parts(self): r = model.Right() r2 = model.Right() self.assertRaises(model.DataError, r.__setattr__, 'part', r2) r.c_part = r2 self.assertTrue(r2 in r.c_part) vocab.conceptual_only_parts() r3 = model.Right() r4 = model.Right() r3.part = r4 self.assertTrue(r4 in r3.c_part) self.assertTrue(""part"" in model.factory.toJSON(r3)) self.assertTrue(r4 in r3.part) def test_art_setter(self): p = model.HumanMadeObject(""a"", art=1) p._label = ""a"" pj = p._toJSON(done={}) self.assertFalse(pj.get('classified_as', None)) vocab.add_art_setter() p2 = vocab.Painting(""b"", art=1) p2j = p2._toJSON(done={}) def test_aa_check(self): # Make sure that some other test hasn't set it try: del model.AttributeAssignment.set_assigned_property except: pass t = model.Type() aa = model.AttributeAssignment() # First check that aa accepts a type aa.assigned_property = t # And will not accept a string self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") # Check we can set anything to assigned / assigned_to aa.assigned_property = None aa.assigned = aa aa.assigned_to = aa self.assertEqual(aa.assigned, aa) self.assertEqual(aa.assigned_to, aa) vocab.add_attribute_assignment_check() # This should fail right now as can't classify as an AA self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") aa.assigned = None aa.assigned_to = None aa.assigned = t aa.assigned_to = t aa.assigned_property = ""classified_as"" self.assertEqual(aa.assigned_property, 'classified_as') def test_boundary_setter(self): vocab.add_linked_art_boundary_check() p = model.Person() p2 = model.Person() n = model.Name() n.content = ""Test"" p2.identified_by = n p.exact_match = p2 # Now, Test should not appear in the resulting JSON of p factory.linked_art_boundaries = True js = factory.toJSON(p) self.assertTrue(not 'identified_by' in js['exact_match'][0]) factory.linked_art_boundaries = False js = factory.toJSON(p) self.assertTrue('identified_by' in js['exact_match'][0]) def test_procurement_boundary(self): vocab.add_linked_art_boundary_check() a = model.Activity() p = vocab.ProvenanceEntry() a.caused = p js = factory.toJSON(a) self.assertTrue(not 'classified_as' in js['caused'][0]) def test_linguistic_object_boundary(self): vocab.add_linked_art_boundary_check() jrnl = vocab.JournalText(label=""journal"") issue = vocab.IssueText(label=""issue"") issue.part_of = jrnl issue.referred_to_by = vocab.MaterialStatement(content=""Statement"") js = factory.toJSON(issue) # Have not embedded journal in issue self.assertTrue(not 'classified_as' in js['part_of'][0]) # Have embedded statement in issue self.assertTrue('content' in js['referred_to_by'][0]) self.assertTrue('type' in js['referred_to_by'][0]['classified_as'][0]['classified_as'][0]) ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_vocab.py::TestClassBuilder::test_metatype,tests/test_vocab.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os from cromulent import vocab, model from cromulent.model import factory class TestClassBuilder(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_class(self): vocab.register_aat_class(""TestObject1"", {""parent"": model.HumanMadeObject, ""id"": ""1"", ""label"": ""example 1""}) from cromulent.vocab import TestObject1 self.assertEqual(TestObject1._classification[0].id, 'http://vocab.getty.edu/aat/1') def test_instance(self): vocab.register_instance(""TestMaterial2"", {""parent"": model.Material, ""id"": ""2"", ""label"": ""example 2""}) self.assertTrue('TestMaterial2' in vocab.instances) tm2 = vocab.instances['TestMaterial2'] self.assertEqual(tm2.id, ""http://vocab.getty.edu/aat/2"") def test_metatype(self): vocab.register_instance(""example"", {""parent"": model.Type, ""id"": ""3"", ""label"": ""example type""}) vocab.register_aat_class(""TestObject2"", {""parent"": model.HumanMadeObject, ""id"": ""4"", ""label"": ""example typed object"", ""metatype"": ""example""}) from cromulent.vocab import TestObject2 self.assertEqual(TestObject2._classification[0].classified_as[0].id, 'http://vocab.getty.edu/aat/3') def test_multitype(self): from cromulent.vocab import make_multitype_obj, Painting, Drawing inst = make_multitype_obj(Painting, Drawing) self.assertTrue(isinstance(inst, Painting)) self.assertTrue(len(inst.classified_as) == 2) self.assertTrue(inst.classified_as[1].id == ""http://vocab.getty.edu/aat/300033973"") from cromulent.model import HumanMadeObject inst = make_multitype_obj(HumanMadeObject, Painting) self.assertTrue(len(inst.classified_as) == 1) self.assertTrue(inst.classified_as[0].id == ""http://vocab.getty.edu/aat/300033618"") def test_conceptual_parts(self): r = model.Right() r2 = model.Right() self.assertRaises(model.DataError, r.__setattr__, 'part', r2) r.c_part = r2 self.assertTrue(r2 in r.c_part) vocab.conceptual_only_parts() r3 = model.Right() r4 = model.Right() r3.part = r4 self.assertTrue(r4 in r3.c_part) self.assertTrue(""part"" in model.factory.toJSON(r3)) self.assertTrue(r4 in r3.part) def test_art_setter(self): p = model.HumanMadeObject(""a"", art=1) p._label = ""a"" pj = p._toJSON(done={}) self.assertFalse(pj.get('classified_as', None)) vocab.add_art_setter() p2 = vocab.Painting(""b"", art=1) p2j = p2._toJSON(done={}) def test_aa_check(self): # Make sure that some other test hasn't set it try: del model.AttributeAssignment.set_assigned_property except: pass t = model.Type() aa = model.AttributeAssignment() # First check that aa accepts a type aa.assigned_property = t # And will not accept a string self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") # Check we can set anything to assigned / assigned_to aa.assigned_property = None aa.assigned = aa aa.assigned_to = aa self.assertEqual(aa.assigned, aa) self.assertEqual(aa.assigned_to, aa) vocab.add_attribute_assignment_check() # This should fail right now as can't classify as an AA self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") aa.assigned = None aa.assigned_to = None aa.assigned = t aa.assigned_to = t aa.assigned_property = ""classified_as"" self.assertEqual(aa.assigned_property, 'classified_as') def test_boundary_setter(self): vocab.add_linked_art_boundary_check() p = model.Person() p2 = model.Person() n = model.Name() n.content = ""Test"" p2.identified_by = n p.exact_match = p2 # Now, Test should not appear in the resulting JSON of p factory.linked_art_boundaries = True js = factory.toJSON(p) self.assertTrue(not 'identified_by' in js['exact_match'][0]) factory.linked_art_boundaries = False js = factory.toJSON(p) self.assertTrue('identified_by' in js['exact_match'][0]) def test_procurement_boundary(self): vocab.add_linked_art_boundary_check() a = model.Activity() p = vocab.ProvenanceEntry() a.caused = p js = factory.toJSON(a) self.assertTrue(not 'classified_as' in js['caused'][0]) def test_linguistic_object_boundary(self): vocab.add_linked_art_boundary_check() jrnl = vocab.JournalText(label=""journal"") issue = vocab.IssueText(label=""issue"") issue.part_of = jrnl issue.referred_to_by = vocab.MaterialStatement(content=""Statement"") js = factory.toJSON(issue) # Have not embedded journal in issue self.assertTrue(not 'classified_as' in js['part_of'][0]) # Have embedded statement in issue self.assertTrue('content' in js['referred_to_by'][0]) self.assertTrue('type' in js['referred_to_by'][0]['classified_as'][0]['classified_as'][0]) ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_vocab.py::TestClassBuilder::test_multitype,tests/test_vocab.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os from cromulent import vocab, model from cromulent.model import factory class TestClassBuilder(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_class(self): vocab.register_aat_class(""TestObject1"", {""parent"": model.HumanMadeObject, ""id"": ""1"", ""label"": ""example 1""}) from cromulent.vocab import TestObject1 self.assertEqual(TestObject1._classification[0].id, 'http://vocab.getty.edu/aat/1') def test_instance(self): vocab.register_instance(""TestMaterial2"", {""parent"": model.Material, ""id"": ""2"", ""label"": ""example 2""}) self.assertTrue('TestMaterial2' in vocab.instances) tm2 = vocab.instances['TestMaterial2'] self.assertEqual(tm2.id, ""http://vocab.getty.edu/aat/2"") def test_metatype(self): vocab.register_instance(""example"", {""parent"": model.Type, ""id"": ""3"", ""label"": ""example type""}) vocab.register_aat_class(""TestObject2"", {""parent"": model.HumanMadeObject, ""id"": ""4"", ""label"": ""example typed object"", ""metatype"": ""example""}) from cromulent.vocab import TestObject2 self.assertEqual(TestObject2._classification[0].classified_as[0].id, 'http://vocab.getty.edu/aat/3') def test_multitype(self): from cromulent.vocab import make_multitype_obj, Painting, Drawing inst = make_multitype_obj(Painting, Drawing) self.assertTrue(isinstance(inst, Painting)) self.assertTrue(len(inst.classified_as) == 2) self.assertTrue(inst.classified_as[1].id == ""http://vocab.getty.edu/aat/300033973"") from cromulent.model import HumanMadeObject inst = make_multitype_obj(HumanMadeObject, Painting) self.assertTrue(len(inst.classified_as) == 1) self.assertTrue(inst.classified_as[0].id == ""http://vocab.getty.edu/aat/300033618"") def test_conceptual_parts(self): r = model.Right() r2 = model.Right() self.assertRaises(model.DataError, r.__setattr__, 'part', r2) r.c_part = r2 self.assertTrue(r2 in r.c_part) vocab.conceptual_only_parts() r3 = model.Right() r4 = model.Right() r3.part = r4 self.assertTrue(r4 in r3.c_part) self.assertTrue(""part"" in model.factory.toJSON(r3)) self.assertTrue(r4 in r3.part) def test_art_setter(self): p = model.HumanMadeObject(""a"", art=1) p._label = ""a"" pj = p._toJSON(done={}) self.assertFalse(pj.get('classified_as', None)) vocab.add_art_setter() p2 = vocab.Painting(""b"", art=1) p2j = p2._toJSON(done={}) def test_aa_check(self): # Make sure that some other test hasn't set it try: del model.AttributeAssignment.set_assigned_property except: pass t = model.Type() aa = model.AttributeAssignment() # First check that aa accepts a type aa.assigned_property = t # And will not accept a string self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") # Check we can set anything to assigned / assigned_to aa.assigned_property = None aa.assigned = aa aa.assigned_to = aa self.assertEqual(aa.assigned, aa) self.assertEqual(aa.assigned_to, aa) vocab.add_attribute_assignment_check() # This should fail right now as can't classify as an AA self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") aa.assigned = None aa.assigned_to = None aa.assigned = t aa.assigned_to = t aa.assigned_property = ""classified_as"" self.assertEqual(aa.assigned_property, 'classified_as') def test_boundary_setter(self): vocab.add_linked_art_boundary_check() p = model.Person() p2 = model.Person() n = model.Name() n.content = ""Test"" p2.identified_by = n p.exact_match = p2 # Now, Test should not appear in the resulting JSON of p factory.linked_art_boundaries = True js = factory.toJSON(p) self.assertTrue(not 'identified_by' in js['exact_match'][0]) factory.linked_art_boundaries = False js = factory.toJSON(p) self.assertTrue('identified_by' in js['exact_match'][0]) def test_procurement_boundary(self): vocab.add_linked_art_boundary_check() a = model.Activity() p = vocab.ProvenanceEntry() a.caused = p js = factory.toJSON(a) self.assertTrue(not 'classified_as' in js['caused'][0]) def test_linguistic_object_boundary(self): vocab.add_linked_art_boundary_check() jrnl = vocab.JournalText(label=""journal"") issue = vocab.IssueText(label=""issue"") issue.part_of = jrnl issue.referred_to_by = vocab.MaterialStatement(content=""Statement"") js = factory.toJSON(issue) # Have not embedded journal in issue self.assertTrue(not 'classified_as' in js['part_of'][0]) # Have embedded statement in issue self.assertTrue('content' in js['referred_to_by'][0]) self.assertTrue('type' in js['referred_to_by'][0]['classified_as'][0]['classified_as'][0]) ", https://github.com/thegetty/crom,98bb6be4e32b4c81eb7e0b5e841a915b015abaf0,tests/test_vocab.py::TestClassBuilder::test_procurement_boundary,tests/test_vocab.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import sys import os from cromulent import vocab, model from cromulent.model import factory class TestClassBuilder(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_class(self): vocab.register_aat_class(""TestObject1"", {""parent"": model.HumanMadeObject, ""id"": ""1"", ""label"": ""example 1""}) from cromulent.vocab import TestObject1 self.assertEqual(TestObject1._classification[0].id, 'http://vocab.getty.edu/aat/1') def test_instance(self): vocab.register_instance(""TestMaterial2"", {""parent"": model.Material, ""id"": ""2"", ""label"": ""example 2""}) self.assertTrue('TestMaterial2' in vocab.instances) tm2 = vocab.instances['TestMaterial2'] self.assertEqual(tm2.id, ""http://vocab.getty.edu/aat/2"") def test_metatype(self): vocab.register_instance(""example"", {""parent"": model.Type, ""id"": ""3"", ""label"": ""example type""}) vocab.register_aat_class(""TestObject2"", {""parent"": model.HumanMadeObject, ""id"": ""4"", ""label"": ""example typed object"", ""metatype"": ""example""}) from cromulent.vocab import TestObject2 self.assertEqual(TestObject2._classification[0].classified_as[0].id, 'http://vocab.getty.edu/aat/3') def test_multitype(self): from cromulent.vocab import make_multitype_obj, Painting, Drawing inst = make_multitype_obj(Painting, Drawing) self.assertTrue(isinstance(inst, Painting)) self.assertTrue(len(inst.classified_as) == 2) self.assertTrue(inst.classified_as[1].id == ""http://vocab.getty.edu/aat/300033973"") from cromulent.model import HumanMadeObject inst = make_multitype_obj(HumanMadeObject, Painting) self.assertTrue(len(inst.classified_as) == 1) self.assertTrue(inst.classified_as[0].id == ""http://vocab.getty.edu/aat/300033618"") def test_conceptual_parts(self): r = model.Right() r2 = model.Right() self.assertRaises(model.DataError, r.__setattr__, 'part', r2) r.c_part = r2 self.assertTrue(r2 in r.c_part) vocab.conceptual_only_parts() r3 = model.Right() r4 = model.Right() r3.part = r4 self.assertTrue(r4 in r3.c_part) self.assertTrue(""part"" in model.factory.toJSON(r3)) self.assertTrue(r4 in r3.part) def test_art_setter(self): p = model.HumanMadeObject(""a"", art=1) p._label = ""a"" pj = p._toJSON(done={}) self.assertFalse(pj.get('classified_as', None)) vocab.add_art_setter() p2 = vocab.Painting(""b"", art=1) p2j = p2._toJSON(done={}) def test_aa_check(self): # Make sure that some other test hasn't set it try: del model.AttributeAssignment.set_assigned_property except: pass t = model.Type() aa = model.AttributeAssignment() # First check that aa accepts a type aa.assigned_property = t # And will not accept a string self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") # Check we can set anything to assigned / assigned_to aa.assigned_property = None aa.assigned = aa aa.assigned_to = aa self.assertEqual(aa.assigned, aa) self.assertEqual(aa.assigned_to, aa) vocab.add_attribute_assignment_check() # This should fail right now as can't classify as an AA self.assertRaises(model.DataError, aa.__setattr__, ""assigned_property"", ""classified_as"") aa.assigned = None aa.assigned_to = None aa.assigned = t aa.assigned_to = t aa.assigned_property = ""classified_as"" self.assertEqual(aa.assigned_property, 'classified_as') def test_boundary_setter(self): vocab.add_linked_art_boundary_check() p = model.Person() p2 = model.Person() n = model.Name() n.content = ""Test"" p2.identified_by = n p.exact_match = p2 # Now, Test should not appear in the resulting JSON of p factory.linked_art_boundaries = True js = factory.toJSON(p) self.assertTrue(not 'identified_by' in js['exact_match'][0]) factory.linked_art_boundaries = False js = factory.toJSON(p) self.assertTrue('identified_by' in js['exact_match'][0]) def test_procurement_boundary(self): vocab.add_linked_art_boundary_check() a = model.Activity() p = vocab.ProvenanceEntry() a.caused = p js = factory.toJSON(a) self.assertTrue(not 'classified_as' in js['caused'][0]) def test_linguistic_object_boundary(self): vocab.add_linked_art_boundary_check() jrnl = vocab.JournalText(label=""journal"") issue = vocab.IssueText(label=""issue"") issue.part_of = jrnl issue.referred_to_by = vocab.MaterialStatement(content=""Statement"") js = factory.toJSON(issue) # Have not embedded journal in issue self.assertTrue(not 'classified_as' in js['part_of'][0]) # Have embedded statement in issue self.assertTrue('content' in js['referred_to_by'][0]) self.assertTrue('type' in js['referred_to_by'][0]['classified_as'][0]['classified_as'][0]) ", https://github.com/thesimj/envyaml,79a220cabb69d71397310d941de205610cf5e4cb,tests/test_envyaml.py::test_it_should_not_fail_when_try_load_non_exist_default_file,tests/test_envyaml.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"from __future__ import absolute_import import os import pytest from envyaml import EnvYAML # set os env os.environ[""TEST_ENV""] = ""test-env"" def test_it_should_read_env_file(): env = EnvYAML(""tests/env.test.yaml"", env_file=""tests/test.env"") assert env[""env_file.project.name""] == ""project-x-42"" assert env[""USERNAME""] == ""env-username"" assert env[""PASSWORD""] == ""env-password-with-escape"" assert env[""PASSWORD_WE""] == ""env-password-without-escape"" assert env[""EMPTY""] == """" # Test wrong names, should be not found and raise KeyErro with pytest.raises(KeyError): assert env[""01sre""] with pytest.raises(KeyError): assert env[""!dtdrthkj""] with pytest.raises(KeyError): assert env[""$WRONG_NAME""] # Config from comment with pytest.raises(KeyError): assert env[""comments""] def test_it_should_read_custom_file(): env = EnvYAML(""tests/env.test.yaml"") # access by property name assert env[""one.two.three.value""] == ""one-two-three-value"" # access by item name assert env[""one.two.three.value""] == ""one-two-three-value"" # access list item assert env[""list_test""][0] == ""one"" # access list item (array) assert env[""list_test.0""] == ""one"" assert isinstance(env[""keys""], dict) and len(env[""keys""]) == 2 def test_it_should_access_environment_variables(): os.environ[""ENV_VAR""] = ""test-env-var"" env = EnvYAML() assert env[""ENV_VAR""] == ""test-env-var"" def test_it_should_fail_when_access_environment_variables(): os.environ[""ENV_VAR""] = ""test-env-var"" env = EnvYAML(include_environment=False) with pytest.raises(KeyError): assert env[""ENV_VAR""] == ""test-env-var"" def test_it_should_access_environ(): os.environ[""ENV_VAR""] = ""test-env-var"" env = EnvYAML() assert env.environ() == os.environ assert env.environ()[""ENV_VAR""] == os.environ[""ENV_VAR""] def test_it_should_get_default_values(): env = EnvYAML(""tests/env.test.yaml"") assert env.get(""empty.novalues"", ""default"") is None assert env.get(""empty.noenvvalue"", ""env-value"") == ""env-value"" assert env[""empty.noenvvalue""] is None def test_it_should_raise_key_error_when_no_values(): env = EnvYAML(""tests/env.test.yaml"") with pytest.raises(KeyError): assert env[""empty.no-value-at-all""] def test_it_should_read_default_file(): env = EnvYAML() # access by property name assert env[""one.two.three.value""] == ""one-two-three-value"" # access by item name assert env[""one.two.three.value""] == ""one-two-three-value"" # access list item assert env[""list_test""][0] == ""one"" # access list item (array) assert env[""list_test.0""] == ""one"" assert isinstance(env[""keys""], dict) and len(env[""keys""]) == 2 def test_it_should_populate_env_variable(): env = EnvYAML(""tests/env.test.yaml"") assert env[""config.test_env""] == os.environ[""TEST_ENV""] def test_it_should_return_dict_on_export(): env = EnvYAML(""tests/env.test.yaml"") assert isinstance(env.export(), dict) and len(env.export()) >= 4 def test_it_should_convert_config_to_dict(): env = EnvYAML(""tests/env.test.yaml"") assert isinstance(dict(env[""test""]), dict) def test_it_should_access_all_keys_in_config(): env = EnvYAML(""tests/env.test.yaml"") assert len(env.keys()) > 10 def test_it_should_access_keys_and_lists(): env = EnvYAML(""tests/env.test.yaml"") assert isinstance(env[""keys_and_lists""], dict) assert isinstance(env[""keys_and_lists.one""], list) assert isinstance(env[""keys_and_lists.two""], list) assert env[""keys_and_lists.one.0""] == ""one"" assert env[""keys_and_lists.two.1.super.one""] == ""one"" def test_it_should_read_config_from_env_variable(): # Set env file os.environ[""ENV_YAML_FILE""] = ""tests/env.test.yaml"" os.environ[""ENV_FILE""] = ""tests/test.env"" env = EnvYAML() assert env[""env_file.project.name""] == ""project-x-42"" assert isinstance(env.export(), dict) and len(env.export()) >= 4 def test_it_should_raise_exception_when_file_not_found(): with pytest.raises(IOError): EnvYAML(""tests/env.notfound.yaml"") def test_it_should_use_default_value(): env = EnvYAML(""tests/env.test.yaml"") assert env.get(""not.exist.key"") is None assert env.get(""not.exist.key"", ""default"") == ""default"" assert env.get(""config.test"") == 100 def test_it_should_get_lists_values_by_number(): env = EnvYAML(""tests/env.test.yaml"") assert env[""list_test""][0] == ""one"" assert env[""list_test""][1] == ""two"" assert env[""list_test""][2] == ""tree"" assert env.get(""list_test.0"") == ""one"" assert env.get(""list_test.1"") == ""two"" assert env.get(""list_test.2"") == ""tree"" assert env[""list_test.0""] == ""one"" assert env[""list_test.1""] == ""two"" assert env[""list_test.2""] == ""tree"" def test_it_should_not_fail_when_try_load_non_exist_default_file(): del os.environ[""ENV_YAML_FILE""] del os.environ[""ENV_FILE""] env = EnvYAML() assert isinstance(env, EnvYAML) def test_it_should_not_fail_when_try_load_default_empty_yaml_file(): env = EnvYAML(""tests/env.empty.yaml"") assert isinstance(env, EnvYAML) def test_it_should_not_fail_when_try_load_default_empty_dotenv_file(): env = EnvYAML(env_file=""tests/test.empty.env"") assert isinstance(env, EnvYAML) def test_it_should_be_valid_in_check(): env = EnvYAML(env_file=""tests/env.test.yaml"") if 'test.one' in env: assert env['test.one'] == 123 assert 'test.not_exists' not in env ", https://github.com/thimic/nodal,261eeefd50298765c5722c5f7da72e6f2927bd3e,tests/nodal/nodes/test_noop.py::TestNoOp::test_name,tests/nodal/nodes/test_noop.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python3 # -*- coding: utf-8 -*- import nodal from nodal.core import MaxInputsExceededException from unittest import TestCase class TestNoOp(TestCase): def test___init__(self): self.assertRaises(TypeError, nodal.nodes.NoOp, 'foo') self.assertRaises(TypeError, nodal.nodes.NoOp, test='foo') def test_input(self): noop1 = nodal.nodes.NoOp() noop2 = nodal.nodes.NoOp() noop2.set_input(0, noop1) self.assertRaises(MaxInputsExceededException, noop1.input, 1) self.assertIsNone(noop1.input(0)) self.assertEqual(noop1, noop2.input(0)) self.assertIn(noop2, noop1.dependents) self.assertTrue(noop2.set_input(0, None)) self.assertIsNone(noop2.input(0)) self.assertFalse(noop1.dependents) def test_name(self): noop = nodal.nodes.NoOp() self.assertEqual('NoOp1', noop.name) noop.name = 'FooOp' self.assertEqual('FooOp', noop.name) def test_set_input(self): noop1 = nodal.nodes.NoOp() noop2 = nodal.nodes.NoOp() self.assertRaises(MaxInputsExceededException, noop1.set_input, 1, noop2) self.assertTrue(noop1.set_input(0, noop2)) def test_output_type(self): noop = nodal.nodes.NoOp() self.assertEqual(object, noop.output_type['type']) def test__execute(self): plus = nodal.nodes.Plus(5) noop = nodal.nodes.NoOp() self.assertEqual(noop.result, NotImplemented) noop.set_input(0, plus) noop.execute() self.assertEqual(5, noop.result) ", https://github.com/thimic/nodal,261eeefd50298765c5722c5f7da72e6f2927bd3e,tests/nodal/test_graph.py::TestGraph::test__on_node_create,tests/nodal/test_graph.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python3 # -*- coding: utf-8 -*- from unittest import TestCase import nodal from nodal import Graph from nodal.core.callbacks import Callbacks class TestGraph(TestCase): def setUp(self): self.graph = Graph() # def tearDown(self): # for node in self.graph.nodes: # node.delete() # self.graph.clear() # Callbacks.clear() def test_create_node(self): with self.graph: noop = self.graph.create_node('NoOp') self.assertTrue(noop in self.graph.nodes) output = nodal.nodes.Output() self.assertTrue(output in self.graph.nodes) def test_delete_node(self): with self.graph: noop = self.graph.create_node('NoOp') self.assertTrue(noop in self.graph.nodes) self.graph.delete_node(noop) self.assertFalse(self.graph.nodes) output = nodal.nodes.Output() self.assertTrue(output in self.graph.nodes) output.delete() self.assertFalse(self.graph.nodes) def test_nodes(self): with self.graph: self.assertFalse(self.graph.nodes) noop = self.graph.create_node('NoOp') self.assertListEqual(self.graph.nodes, [noop]) def test_to_node(self): with self.graph: noop = self.graph.create_node('NoOp') to_noop = self.graph.to_node(noop.name) self.assertEqual(noop, to_noop) self.assertIsNone(self.graph.to_node('Foo')) def test_clear(self): with self.graph: noop = self.graph.create_node('NoOp') self.assertEqual(self.graph.nodes, [noop]) self.graph.clear() self.assertFalse(self.graph.nodes) def test_execute(self): with self.graph: plus1 = self.graph.create_node('Plus') plus1.value = 5 plus2 = nodal.nodes.Plus() plus2.value = 2 plus2.set_input(0, plus1) output = self.graph.create_node('Output') output.set_input(0, plus2) result = self.graph.execute([plus1, plus2, output]) self.assertDictEqual( result, {plus1.name: 5, plus2.name: 7, output.name: 7} ) self.assertEqual({plus1.name: 5}, self.graph.execute(plus1)) def test__on_node_create(self): with self.graph: noop1 = self.graph.create_node('NoOp') self.assertEqual(noop1.name, 'NoOp1') noop2 = nodal.nodes.NoOp() self.assertEqual(noop2.name, 'NoOp2') noop3 = self.graph.create_node('NoOp') self.assertEqual(noop3.name, 'NoOp3') noop4 = self.graph.create_node('NoOp', name='FooOp') self.assertEqual(noop4.name, 'FooOp1') output = nodal.nodes.Output() self.assertEqual(output.name, 'Output1') def test__on_node_destroy(self): with self.graph: # Create 3 NoOps noop1 = self.graph.create_node('NoOp') noop2 = nodal.nodes.NoOp() noop3 = self.graph.create_node('NoOp') # Delete two NoOps and check that we are left with one self.graph.delete_node(noop3) noop2.delete() self.assertEqual(self.graph.nodes, [noop1]) # Create new NoOp and check that it gets named as expected noop2 = nodal.nodes.NoOp() self.assertEqual(noop2.name, 'NoOp2') def test_sort(self): with self.graph: # Create graph plus1 = self.graph.create_node('Plus', 5) plus2 = self.graph.create_node('Plus', 10) plus3 = self.graph.create_node('Plus', 15) sum_ = self.graph.create_node('Plus') sum_.name = 'Sum1' sum_.set_input(0, plus1) sum_.set_input(1, plus2) sum_.set_input(2, plus3) output1 = self.graph.create_node('Output') output1.set_input(0, sum_) noop = self.graph.create_node('NoOp') noop.set_input(0, sum_) plus4 = self.graph.create_node('Plus', 20) plus4.set_input(0, noop) output2 = self.graph.create_node('Output') output2.set_input(0, plus4) # Check that the graph has the right output self.assertEqual({'Output2': 50}, self.graph.execute(output2)) # Confirm sort order is correct self.assertListEqual( [plus1, plus2, plus3, sum_, noop, plus4, output2, output1], self.graph.sort() ) ", https://github.com/thimic/nodal,261eeefd50298765c5722c5f7da72e6f2927bd3e,tests/nodal/test_graph.py::TestGraph::test__on_node_destroy,tests/nodal/test_graph.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python3 # -*- coding: utf-8 -*- from unittest import TestCase import nodal from nodal import Graph from nodal.core.callbacks import Callbacks class TestGraph(TestCase): def setUp(self): self.graph = Graph() # def tearDown(self): # for node in self.graph.nodes: # node.delete() # self.graph.clear() # Callbacks.clear() def test_create_node(self): with self.graph: noop = self.graph.create_node('NoOp') self.assertTrue(noop in self.graph.nodes) output = nodal.nodes.Output() self.assertTrue(output in self.graph.nodes) def test_delete_node(self): with self.graph: noop = self.graph.create_node('NoOp') self.assertTrue(noop in self.graph.nodes) self.graph.delete_node(noop) self.assertFalse(self.graph.nodes) output = nodal.nodes.Output() self.assertTrue(output in self.graph.nodes) output.delete() self.assertFalse(self.graph.nodes) def test_nodes(self): with self.graph: self.assertFalse(self.graph.nodes) noop = self.graph.create_node('NoOp') self.assertListEqual(self.graph.nodes, [noop]) def test_to_node(self): with self.graph: noop = self.graph.create_node('NoOp') to_noop = self.graph.to_node(noop.name) self.assertEqual(noop, to_noop) self.assertIsNone(self.graph.to_node('Foo')) def test_clear(self): with self.graph: noop = self.graph.create_node('NoOp') self.assertEqual(self.graph.nodes, [noop]) self.graph.clear() self.assertFalse(self.graph.nodes) def test_execute(self): with self.graph: plus1 = self.graph.create_node('Plus') plus1.value = 5 plus2 = nodal.nodes.Plus() plus2.value = 2 plus2.set_input(0, plus1) output = self.graph.create_node('Output') output.set_input(0, plus2) result = self.graph.execute([plus1, plus2, output]) self.assertDictEqual( result, {plus1.name: 5, plus2.name: 7, output.name: 7} ) self.assertEqual({plus1.name: 5}, self.graph.execute(plus1)) def test__on_node_create(self): with self.graph: noop1 = self.graph.create_node('NoOp') self.assertEqual(noop1.name, 'NoOp1') noop2 = nodal.nodes.NoOp() self.assertEqual(noop2.name, 'NoOp2') noop3 = self.graph.create_node('NoOp') self.assertEqual(noop3.name, 'NoOp3') noop4 = self.graph.create_node('NoOp', name='FooOp') self.assertEqual(noop4.name, 'FooOp1') output = nodal.nodes.Output() self.assertEqual(output.name, 'Output1') def test__on_node_destroy(self): with self.graph: # Create 3 NoOps noop1 = self.graph.create_node('NoOp') noop2 = nodal.nodes.NoOp() noop3 = self.graph.create_node('NoOp') # Delete two NoOps and check that we are left with one self.graph.delete_node(noop3) noop2.delete() self.assertEqual(self.graph.nodes, [noop1]) # Create new NoOp and check that it gets named as expected noop2 = nodal.nodes.NoOp() self.assertEqual(noop2.name, 'NoOp2') def test_sort(self): with self.graph: # Create graph plus1 = self.graph.create_node('Plus', 5) plus2 = self.graph.create_node('Plus', 10) plus3 = self.graph.create_node('Plus', 15) sum_ = self.graph.create_node('Plus') sum_.name = 'Sum1' sum_.set_input(0, plus1) sum_.set_input(1, plus2) sum_.set_input(2, plus3) output1 = self.graph.create_node('Output') output1.set_input(0, sum_) noop = self.graph.create_node('NoOp') noop.set_input(0, sum_) plus4 = self.graph.create_node('Plus', 20) plus4.set_input(0, noop) output2 = self.graph.create_node('Output') output2.set_input(0, plus4) # Check that the graph has the right output self.assertEqual({'Output2': 50}, self.graph.execute(output2)) # Confirm sort order is correct self.assertListEqual( [plus1, plus2, plus3, sum_, noop, plus4, output2, output1], self.graph.sort() ) ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcQueueTest::test_queue_collection_types,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcQueueTest::test_queue_complex_types,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcQueueTest::test_queue_get_blocking,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcQueueTest::test_queue_get_nowait_timeout,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcQueueTest::test_queue_primitive_types,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcQueueTest::test_queue_put_get,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcRpcTest::test_echo_command_response_function,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcRpcTest::test_error_function,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcRpcTest::test_remote_decorator,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcRpcTest::test_simple_function,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcRpcTest::test_simple_list_param_function,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcRpcTest::test_simple_multiple_param_default_function,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcRpcTest::test_simple_multiple_param_function,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcRpcTest::test_stateful_rpc,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcRpcTest::test_timeout,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_ipc.py::IpcRpcTest::test_void_function_error,tests/test_provider_ipc.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.ipc import IpcEventBus, IpcConfig, IpcQueue from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class IpcEventBusTestBase(unittest.TestCase): bus: IpcEventBus def setUp(self) -> None: self.bus = pymq.init(IpcConfig()) def tearDown(self) -> None: pymq.shutdown() self.bus.close() class IpcQueueTest(IpcEventBusTestBase, AbstractQueueTest): @classmethod def tearDownClass(cls) -> None: IpcQueue('pymq_global_test_queue').free() IpcQueue('pymq_global_test_queue_1').free() IpcQueue('pymq_global_test_queue_2').free() class IpcRpcTest(IpcEventBusTestBase, AbstractRpcTest): def setUp(self) -> None: super().setUp() class IpcPubSubTest(IpcEventBusTestBase, AbstractPubSubTest): def test_publish_pattern(self): pass del IpcEventBusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_redis.py::RedisRpcTest::test_channel_expire,tests/test_provider_redis.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import queue import threading import time import unittest from timeout_decorator import timeout_decorator import pymq from pymq.provider.redis import RedisConfig, RedisEventBus from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest from tests.testutils import RedisResource class MyRedisEvent: pass class RedisEventbusTestBase(unittest.TestCase): redis: RedisResource = RedisResource() bus: RedisEventBus @classmethod def setUpClass(cls) -> None: cls.redis.setUp() @classmethod def tearDownClass(cls) -> None: cls.redis.tearDown() def setUp(self) -> None: self.bus = pymq.init(RedisConfig(self.redis.rds)) def tearDown(self) -> None: pymq.shutdown() self.redis.rds.flushall() class RedisQueueTest(RedisEventbusTestBase, AbstractQueueTest): pass class InitSubscribersTest(RedisEventbusTestBase): def setUp(self) -> None: pass def tearDown(self) -> None: pass def test_subscribe_before_init(self): invocations = queue.Queue() def handler(event: str): invocations.put(event) pymq.subscribe(handler, channel='early/subscription') pymq.publish('hello', channel='early/subscription') # doesn't do anything try: super().setUp() pymq.publish('hello', channel='early/subscription') self.assertEqual('hello', invocations.get(timeout=1)) self.assertEqual(0, invocations.qsize()) finally: super().tearDown() def test_unsubscribe_before_init(self): invocations = queue.Queue() def handler(event: str): invocations.put(event) pymq.subscribe(handler, channel='early/subscription') pymq.unsubscribe(handler, channel='early/subscription') try: super().setUp() pymq.publish('hello', channel='early/subscription') self.assertRaises(queue.Empty, invocations.get, timeout=0.25) finally: super().tearDown() class RedisPubSubTest(RedisEventbusTestBase, AbstractPubSubTest): def test_add_listener_creates_subscription_correctly(self): def listener(event: MyRedisEvent): pass self.assertEqual(0, len(self.redis.rds.pubsub_channels()), 'expected no subscribers, but got %s' % self.redis.rds.pubsub_channels()) pymq.subscribe(listener) channels = self.redis.rds.pubsub_channels() self.assertEqual(1, len(channels)) # event names are encoded in the channels self.assertTrue(channels[0].endswith('.MyRedisEvent')) def test_remove_listener_also_removes_redis_subscription(self): def listener1(event: MyRedisEvent): pass def listener2(event: MyRedisEvent): pass pymq.subscribe(listener1) pymq.subscribe(listener2) self.assertEqual(1, len(self.redis.rds.pubsub_channels())) pymq.unsubscribe(listener1) self.assertEqual(1, len(self.redis.rds.pubsub_channels())) pymq.unsubscribe(listener2) self.assertEqual(0, len(self.redis.rds.pubsub_channels())) class RedisRpcTest(RedisEventbusTestBase, AbstractRpcTest): @timeout_decorator.timeout(5) def test_channel_expire(self): self.bus.rpc_channel_expire = 1 called = threading.Event() def remotefn(): time.sleep(1.25) called.set() pymq.expose(remotefn) stub = pymq.stub(remotefn, timeout=1) stub.rpc() keys = self.redis.rds.keys('*rpc*') self.assertEqual(0, len(keys), 'Expected no rpc results yet %s' % keys) called.wait() keys = self.redis.rds.keys('*rpc*') self.assertEqual(1, len(keys)) # wait for key to expire time.sleep(1.25) keys = self.redis.rds.keys('*rpc*') self.assertEqual(0, len(keys), 'key did not expire') del RedisEventbusTestBase if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_simple.py::SimplePubSubTest::test_event_serialization,tests/test_provider_simple.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.simple import SimpleEventBus from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class SimpleQueueTest(unittest.TestCase, AbstractQueueTest): def setUp(self) -> None: super().setUp() pymq.init(SimpleEventBus) def tearDown(self) -> None: super().tearDown() pymq.shutdown() class SimpleRpcTest(unittest.TestCase, AbstractRpcTest): def setUp(self) -> None: super().setUp() pymq.init(SimpleEventBus) def tearDown(self) -> None: super().tearDown() pymq.shutdown() class SimplePubSubTest(unittest.TestCase, AbstractPubSubTest): def setUp(self) -> None: super().setUp() pymq.init(SimpleEventBus) def tearDown(self) -> None: super().tearDown() pymq.shutdown() def test_publish_pattern(self): # TODO: pattern matching not implemented for simple eventbus pass if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_simple.py::SimplePubSubTest::test_topic,tests/test_provider_simple.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.simple import SimpleEventBus from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class SimpleQueueTest(unittest.TestCase, AbstractQueueTest): def setUp(self) -> None: super().setUp() pymq.init(SimpleEventBus) def tearDown(self) -> None: super().tearDown() pymq.shutdown() class SimpleRpcTest(unittest.TestCase, AbstractRpcTest): def setUp(self) -> None: super().setUp() pymq.init(SimpleEventBus) def tearDown(self) -> None: super().tearDown() pymq.shutdown() class SimplePubSubTest(unittest.TestCase, AbstractPubSubTest): def setUp(self) -> None: super().setUp() pymq.init(SimpleEventBus) def tearDown(self) -> None: super().tearDown() pymq.shutdown() def test_publish_pattern(self): # TODO: pattern matching not implemented for simple eventbus pass if __name__ == '__main__': unittest.main() ", https://github.com/thrau/pymq,101857bca2b705c328d3bda3b26797b51e8ffb70,tests/test_provider_simple.py::SimpleRpcTest::test_stateful_rpc,tests/test_provider_simple.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"import unittest import pymq from pymq.provider.simple import SimpleEventBus from tests.base.pubsub import AbstractPubSubTest from tests.base.queue import AbstractQueueTest from tests.base.rpc import AbstractRpcTest class SimpleQueueTest(unittest.TestCase, AbstractQueueTest): def setUp(self) -> None: super().setUp() pymq.init(SimpleEventBus) def tearDown(self) -> None: super().tearDown() pymq.shutdown() class SimpleRpcTest(unittest.TestCase, AbstractRpcTest): def setUp(self) -> None: super().setUp() pymq.init(SimpleEventBus) def tearDown(self) -> None: super().tearDown() pymq.shutdown() class SimplePubSubTest(unittest.TestCase, AbstractPubSubTest): def setUp(self) -> None: super().setUp() pymq.init(SimpleEventBus) def tearDown(self) -> None: super().tearDown() pymq.shutdown() def test_publish_pattern(self): # TODO: pattern matching not implemented for simple eventbus pass if __name__ == '__main__': unittest.main() ", https://github.com/thundra-io/thundra-lambda-agent-python,fcd1c2b0b309721d89bd35511bd6645ebd62a5bc,tests/plugins/log/test_log_plugin.py::test_log_plugin_with_initialization,tests/plugins/log/test_log_plugin.py,NIO,flaky,Accepted,https://github.com/thundra-io/thundra-agent-python/pull/169,classify;root_cause;fix_proposal,"import logging from logging.config import fileConfig from thundra.plugins.log.thundra_log_handler import ThundraLogHandler, logs def test_when_thundra_log_handler_is_not_added_to_logger(handler, mock_context, mock_event): _, handler = handler handler(mock_event, mock_context) assert len(logs) == 0 def test_log_plugin_with_initialization(): logger = logging.getLogger('test_handler') log_handler = ThundraLogHandler() logger.addHandler(log_handler) logger.setLevel(logging.INFO) logger.info(""This is an info log"") assert len(logs) == 1 log = logs[0] assert log['logMessage'] == ""This is an info log"" assert log['logContextName'] == 'test_handler' assert log['logLevel'] == ""INFO"" assert log['logLevelCode'] == 2 del logs[:] def test_log_plugin_with_config_file(): # config file path. Make sure path is correct with respect to where test is invoked fileConfig('tests/plugins/log/test_log_config.ini') logger = logging.getLogger('test_config_handler') logger.debug(""This is a debug log"") assert len(logs) == 1 log = logs[0] assert log['logMessage'] == ""This is a debug log"" assert log['logContextName'] == 'test_config_handler' assert log['logLevel'] == ""DEBUG"" assert log['logLevelCode'] == 1 ", https://github.com/thundra-io/thundra-lambda-agent-python,fcd1c2b0b309721d89bd35511bd6645ebd62a5bc,tests/plugins/trace/test_traceable.py::test_trace_args,tests/plugins/trace/test_traceable.py,NIO,flaky,DeveloperFixed,5e19c017753a32cd0079dc7862177f1038761317,classify;root_cause,"from thundra.opentracing.tracer import ThundraTracer def test_trace_args(trace_args): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count=0 for key in nodes: if key.operation_name == 'func_args': count += 1 assert count == 0 traceable_trace_args, func_args = trace_args func_args('arg1', arg2='arg2') active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func_args': count += 1 active_span = key args = active_span.get_tag('method.args') assert len(args) == 2 assert args[0]['value'] == 'arg1' assert args[0]['name'] == 'arg-0' assert args[0]['type'] == 'str' assert args[1]['value'] == 'arg2' assert args[1]['name'] == 'arg2' assert args[1]['type'] == 'str' return_value = active_span.get_tag('method.return_value') assert return_value is None error = active_span.get_tag('error') assert error is None assert count == 1 assert traceable_trace_args.trace_args is True assert traceable_trace_args.trace_return_value is False assert traceable_trace_args.trace_error is True def test_trace_return_values(trace_return_val): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count = 0 for key in nodes: if key.operation_name == 'func_return_val': count += 1 assert count == 0 traceable_trace_return_val, func_return_val = trace_return_val response = func_return_val() active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func_return_val': count += 1 active_span = key args = active_span.get_tag('method.args') assert args is None return_value = active_span.get_tag('method.return_value') assert return_value['type'] == type(response).__name__ assert return_value['value'] == response error = active_span.get_tag('error') assert error is None assert count == 1 assert traceable_trace_return_val.trace_args is False assert traceable_trace_return_val.trace_return_value is True assert traceable_trace_return_val.trace_error is True def test_trace_error(trace_error): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count = 0 for key in nodes: if key.operation_name == 'func_with_error': count += 1 assert count == 0 traceable, func_with_error = trace_error try: func_with_error() except: active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func_with_error': count += 1 active_span = key args = active_span.get_tag('method.args') assert args is None return_value = active_span.get_tag('method.return_value') assert return_value is None thrown_error = active_span.get_tag('error.kind') assert thrown_error == 'Exception' assert count == 1 assert traceable.trace_args is False assert traceable.trace_return_value is False assert traceable.trace_error is True def test_trace_with_default_configs(trace): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count = 0 for key in nodes: if key.operation_name == 'func': count += 1 assert count == 0 traceable, func = trace func(arg='test') active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func': count += 1 active_span = key args = active_span.get_tag('method.args') assert args is None return_value = active_span.get_tag('method.return_value') assert return_value is None error = active_span.get_tag('error') assert error is None assert count == 1 assert traceable.trace_args is False assert traceable.trace_return_value is False assert traceable.trace_error is True ", https://github.com/thundra-io/thundra-lambda-agent-python,fcd1c2b0b309721d89bd35511bd6645ebd62a5bc,tests/plugins/trace/test_traceable.py::test_trace_error,tests/plugins/trace/test_traceable.py,NIO,flaky,DeveloperFixed,5e19c017753a32cd0079dc7862177f1038761318,classify;root_cause,"from thundra.opentracing.tracer import ThundraTracer def test_trace_args(trace_args): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count=0 for key in nodes: if key.operation_name == 'func_args': count += 1 assert count == 0 traceable_trace_args, func_args = trace_args func_args('arg1', arg2='arg2') active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func_args': count += 1 active_span = key args = active_span.get_tag('method.args') assert len(args) == 2 assert args[0]['value'] == 'arg1' assert args[0]['name'] == 'arg-0' assert args[0]['type'] == 'str' assert args[1]['value'] == 'arg2' assert args[1]['name'] == 'arg2' assert args[1]['type'] == 'str' return_value = active_span.get_tag('method.return_value') assert return_value is None error = active_span.get_tag('error') assert error is None assert count == 1 assert traceable_trace_args.trace_args is True assert traceable_trace_args.trace_return_value is False assert traceable_trace_args.trace_error is True def test_trace_return_values(trace_return_val): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count = 0 for key in nodes: if key.operation_name == 'func_return_val': count += 1 assert count == 0 traceable_trace_return_val, func_return_val = trace_return_val response = func_return_val() active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func_return_val': count += 1 active_span = key args = active_span.get_tag('method.args') assert args is None return_value = active_span.get_tag('method.return_value') assert return_value['type'] == type(response).__name__ assert return_value['value'] == response error = active_span.get_tag('error') assert error is None assert count == 1 assert traceable_trace_return_val.trace_args is False assert traceable_trace_return_val.trace_return_value is True assert traceable_trace_return_val.trace_error is True def test_trace_error(trace_error): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count = 0 for key in nodes: if key.operation_name == 'func_with_error': count += 1 assert count == 0 traceable, func_with_error = trace_error try: func_with_error() except: active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func_with_error': count += 1 active_span = key args = active_span.get_tag('method.args') assert args is None return_value = active_span.get_tag('method.return_value') assert return_value is None thrown_error = active_span.get_tag('error.kind') assert thrown_error == 'Exception' assert count == 1 assert traceable.trace_args is False assert traceable.trace_return_value is False assert traceable.trace_error is True def test_trace_with_default_configs(trace): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count = 0 for key in nodes: if key.operation_name == 'func': count += 1 assert count == 0 traceable, func = trace func(arg='test') active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func': count += 1 active_span = key args = active_span.get_tag('method.args') assert args is None return_value = active_span.get_tag('method.return_value') assert return_value is None error = active_span.get_tag('error') assert error is None assert count == 1 assert traceable.trace_args is False assert traceable.trace_return_value is False assert traceable.trace_error is True ", https://github.com/thundra-io/thundra-lambda-agent-python,fcd1c2b0b309721d89bd35511bd6645ebd62a5bc,tests/plugins/trace/test_traceable.py::test_trace_return_values,tests/plugins/trace/test_traceable.py,NIO,flaky,DeveloperFixed,5e19c017753a32cd0079dc7862177f1038761319,classify;root_cause,"from thundra.opentracing.tracer import ThundraTracer def test_trace_args(trace_args): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count=0 for key in nodes: if key.operation_name == 'func_args': count += 1 assert count == 0 traceable_trace_args, func_args = trace_args func_args('arg1', arg2='arg2') active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func_args': count += 1 active_span = key args = active_span.get_tag('method.args') assert len(args) == 2 assert args[0]['value'] == 'arg1' assert args[0]['name'] == 'arg-0' assert args[0]['type'] == 'str' assert args[1]['value'] == 'arg2' assert args[1]['name'] == 'arg2' assert args[1]['type'] == 'str' return_value = active_span.get_tag('method.return_value') assert return_value is None error = active_span.get_tag('error') assert error is None assert count == 1 assert traceable_trace_args.trace_args is True assert traceable_trace_args.trace_return_value is False assert traceable_trace_args.trace_error is True def test_trace_return_values(trace_return_val): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count = 0 for key in nodes: if key.operation_name == 'func_return_val': count += 1 assert count == 0 traceable_trace_return_val, func_return_val = trace_return_val response = func_return_val() active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func_return_val': count += 1 active_span = key args = active_span.get_tag('method.args') assert args is None return_value = active_span.get_tag('method.return_value') assert return_value['type'] == type(response).__name__ assert return_value['value'] == response error = active_span.get_tag('error') assert error is None assert count == 1 assert traceable_trace_return_val.trace_args is False assert traceable_trace_return_val.trace_return_value is True assert traceable_trace_return_val.trace_error is True def test_trace_error(trace_error): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count = 0 for key in nodes: if key.operation_name == 'func_with_error': count += 1 assert count == 0 traceable, func_with_error = trace_error try: func_with_error() except: active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func_with_error': count += 1 active_span = key args = active_span.get_tag('method.args') assert args is None return_value = active_span.get_tag('method.return_value') assert return_value is None thrown_error = active_span.get_tag('error.kind') assert thrown_error == 'Exception' assert count == 1 assert traceable.trace_args is False assert traceable.trace_return_value is False assert traceable.trace_error is True def test_trace_with_default_configs(trace): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count = 0 for key in nodes: if key.operation_name == 'func': count += 1 assert count == 0 traceable, func = trace func(arg='test') active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func': count += 1 active_span = key args = active_span.get_tag('method.args') assert args is None return_value = active_span.get_tag('method.return_value') assert return_value is None error = active_span.get_tag('error') assert error is None assert count == 1 assert traceable.trace_args is False assert traceable.trace_return_value is False assert traceable.trace_error is True ", https://github.com/thundra-io/thundra-lambda-agent-python,fcd1c2b0b309721d89bd35511bd6645ebd62a5bc,tests/plugins/trace/test_traceable.py::test_trace_with_default_configs,tests/plugins/trace/test_traceable.py,NIO,flaky,DeveloperFixed,5e19c017753a32cd0079dc7862177f1038761320,classify;root_cause,"from thundra.opentracing.tracer import ThundraTracer def test_trace_args(trace_args): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count=0 for key in nodes: if key.operation_name == 'func_args': count += 1 assert count == 0 traceable_trace_args, func_args = trace_args func_args('arg1', arg2='arg2') active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func_args': count += 1 active_span = key args = active_span.get_tag('method.args') assert len(args) == 2 assert args[0]['value'] == 'arg1' assert args[0]['name'] == 'arg-0' assert args[0]['type'] == 'str' assert args[1]['value'] == 'arg2' assert args[1]['name'] == 'arg2' assert args[1]['type'] == 'str' return_value = active_span.get_tag('method.return_value') assert return_value is None error = active_span.get_tag('error') assert error is None assert count == 1 assert traceable_trace_args.trace_args is True assert traceable_trace_args.trace_return_value is False assert traceable_trace_args.trace_error is True def test_trace_return_values(trace_return_val): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count = 0 for key in nodes: if key.operation_name == 'func_return_val': count += 1 assert count == 0 traceable_trace_return_val, func_return_val = trace_return_val response = func_return_val() active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func_return_val': count += 1 active_span = key args = active_span.get_tag('method.args') assert args is None return_value = active_span.get_tag('method.return_value') assert return_value['type'] == type(response).__name__ assert return_value['value'] == response error = active_span.get_tag('error') assert error is None assert count == 1 assert traceable_trace_return_val.trace_args is False assert traceable_trace_return_val.trace_return_value is True assert traceable_trace_return_val.trace_error is True def test_trace_error(trace_error): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count = 0 for key in nodes: if key.operation_name == 'func_with_error': count += 1 assert count == 0 traceable, func_with_error = trace_error try: func_with_error() except: active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func_with_error': count += 1 active_span = key args = active_span.get_tag('method.args') assert args is None return_value = active_span.get_tag('method.return_value') assert return_value is None thrown_error = active_span.get_tag('error.kind') assert thrown_error == 'Exception' assert count == 1 assert traceable.trace_args is False assert traceable.trace_return_value is False assert traceable.trace_error is True def test_trace_with_default_configs(trace): tracer = ThundraTracer.get_instance() nodes = tracer.get_spans() count = 0 for key in nodes: if key.operation_name == 'func': count += 1 assert count == 0 traceable, func = trace func(arg='test') active_span = None nodes = tracer.get_spans() for key in nodes: if key.operation_name == 'func': count += 1 active_span = key args = active_span.get_tag('method.args') assert args is None return_value = active_span.get_tag('method.return_value') assert return_value is None error = active_span.get_tag('error') assert error is None assert count == 1 assert traceable.trace_args is False assert traceable.trace_return_value is False assert traceable.trace_error is True ", https://github.com/tianocore/edk2-pytool-library,ab6bac82f297b4e8fe0a0d4dcde4711b3cb1bc88,edk2toollib/acpi/ivrs_parser_test.py::IvrsParserTest::test_ivrs_parser_ivrs_full,edk2toollib/acpi/ivrs_parser_test.py,OD-Brit,flaky,nan,nan,classify;root_cause,"## # Copyright (C) Microsoft Corporation. All rights reserved. # SPDX-License-Identifier: BSD-2-Clause-Patent # # Python script that converts a raw IVRS table into a struct ## # spell-checker:ignore IVMD, IOMMUEFR import unittest from edk2toollib.acpi.ivrs_parser import IVRS_TABLE class IvrsParserTest(unittest.TestCase): dte_00h = None dte_01h = None dte_02h = None dte_03h = None dte_42h = None dte_43h = None dte_46h = None dte_47h = None dte_48h = None dte_f0h_0 = None dte_f0h_1 = None dte_f0h_2 = None ivhd_10h = None ivhd_11h = None ivhd_40h = None ivmd_20h = None ivmd_21h = None ivmd_22h = None ivrs_table = None # All the dte types start with Type (1 byte), DeviceID (2 bytes), DTE Settings (1 byte) def test_ivrs_parser_dte_00h(self): # Reserved device dte_t_00h = bytes([0x00, 0x00, 0x00, 0x00]) IvrsParserTest.dte_00h = IVRS_TABLE.DEVICE_TABLE_ENTRY.Factory(dte_t_00h) self.assertNotEqual(IvrsParserTest.dte_00h.Encode(), None) self.assertEqual(IvrsParserTest.dte_00h.Type, IVRS_TABLE.DEVICE_TABLE_ENTRY.DTE_TYPE.RESERVED) self.assertEqual(IvrsParserTest.dte_00h.DeviceID, 0) self.assertEqual(IvrsParserTest.dte_00h.DTESetting, 0) def test_ivrs_parser_dte_01h(self): # All devices dte_t_01h = bytes([0x01, 0xFF, 0xFF, 0x00]) IvrsParserTest.dte_01h = IVRS_TABLE.DEVICE_TABLE_ENTRY.Factory(dte_t_01h) self.assertEqual(IvrsParserTest.dte_01h.Type, IVRS_TABLE.DEVICE_TABLE_ENTRY.DTE_TYPE.ALL) self.assertEqual(IvrsParserTest.dte_01h.DTESetting, 0) def test_ivrs_parser_dte_02h(self): # Select device dte_t_02h = bytes([0x02, 0x5A, 0x5A, 0x00]) IvrsParserTest.dte_02h = IVRS_TABLE.DEVICE_TABLE_ENTRY.Factory(dte_t_02h) self.assertNotEqual(IvrsParserTest.dte_02h.Encode(), None) self.assertEqual(IvrsParserTest.dte_02h.Type, IVRS_TABLE.DEVICE_TABLE_ENTRY.DTE_TYPE.SELECT) self.assertEqual(IvrsParserTest.dte_02h.DeviceID, 0x5A5A) self.assertEqual(IvrsParserTest.dte_02h.DTESetting, 0) def test_ivrs_parser_dte_03h(self): # Start of device range dte_t_03h = bytes([0x03, 0xBE, 0xBA, 0x00]) dte_t_04h = bytes([0x04, 0xFF, 0xFF, 0x00]) IvrsParserTest.dte_03h = IVRS_TABLE.DEVICE_TABLE_ENTRY.Factory(dte_t_03h + dte_t_04h) self.assertNotEqual(IvrsParserTest.dte_03h.Encode(), None) self.assertEqual(IvrsParserTest.dte_03h.Type, IVRS_TABLE.DEVICE_TABLE_ENTRY.DTE_TYPE.RANGE_START) self.assertEqual(IvrsParserTest.dte_03h.DeviceID, 0xBABE) self.assertEqual(IvrsParserTest.dte_03h.DTESetting, 0) self.assertEqual(IvrsParserTest.dte_03h.EndDeviceID, 0xFFFF) def test_ivrs_parser_dte_42h(self): # Alias select device dte_t_42h = bytes([0x42, 0xAD, 0xDE, 0x00, 0x00, 0xEF, 0xBE, 0x00]) IvrsParserTest.dte_42h = IVRS_TABLE.DEVICE_TABLE_ENTRY.Factory(dte_t_42h) self.assertNotEqual(IvrsParserTest.dte_42h.Encode(), None) self.assertEqual(IvrsParserTest.dte_42h.Type, IVRS_TABLE.DEVICE_TABLE_ENTRY.DTE_TYPE.ALIAS_SELECT) self.assertEqual(IvrsParserTest.dte_42h.DeviceID, 0xDEAD) self.assertEqual(IvrsParserTest.dte_42h.DTESetting, 0) self.assertEqual(IvrsParserTest.dte_42h.SourceDeviceID, 0xBEEF) def test_ivrs_parser_dte_43h(self): # Alias range device dte_t_43h = bytes([0x43, 0xED, 0xFE, 0x00, 0x00, 0x0D, 0xF0, 0x00]) dte_t_04h = bytes([0x04, 0xFF, 0xFF, 0x00]) IvrsParserTest.dte_43h = IVRS_TABLE.DEVICE_TABLE_ENTRY.Factory(dte_t_43h + dte_t_04h) self.assertNotEqual(IvrsParserTest.dte_43h.Encode(), None) self.assertEqual(IvrsParserTest.dte_43h.Type, IVRS_TABLE.DEVICE_TABLE_ENTRY.DTE_TYPE.ALIAS_RANGE_START) self.assertEqual(IvrsParserTest.dte_43h.DeviceID, 0xFEED) self.assertEqual(IvrsParserTest.dte_43h.DTESetting, 0) self.assertEqual(IvrsParserTest.dte_43h.SourceDeviceID, 0xF00D) self.assertEqual(IvrsParserTest.dte_43h.EndDeviceID, 0xFFFF) def test_ivrs_parser_dte_46h(self): # Extended select device dte_t_46h = bytes([0x46, 0x05, 0xB1, 0x00, 0xFE, 0xCA, 0xEF, 0xBE]) IvrsParserTest.dte_46h = IVRS_TABLE.DEVICE_TABLE_ENTRY.Factory(dte_t_46h) self.assertNotEqual(IvrsParserTest.dte_46h.Encode(), None) self.assertEqual(IvrsParserTest.dte_46h.Type, IVRS_TABLE.DEVICE_TABLE_ENTRY.DTE_TYPE.EX_SELECT) self.assertEqual(IvrsParserTest.dte_46h.DeviceID, 0xB105) self.assertEqual(IvrsParserTest.dte_46h.DTESetting, 0) self.assertEqual(IvrsParserTest.dte_46h.ExtendedDTESetting, 0xBEEFCAFE) def test_ivrs_parser_dte_47h(self): # Extended range of device dte_t_47h = bytes([0x47, 0xDE, 0xC0, 0x00, 0xBE, 0xBA, 0xAD, 0xAB]) dte_t_04h = bytes([0x04, 0xFF, 0xFF, 0x00]) IvrsParserTest.dte_47h = IVRS_TABLE.DEVICE_TABLE_ENTRY.Factory(dte_t_47h + dte_t_04h) self.assertNotEqual(IvrsParserTest.dte_47h.Encode(), None) self.assertEqual(IvrsParserTest.dte_47h.Type, IVRS_TABLE.DEVICE_TABLE_ENTRY.DTE_TYPE.EX_RANGE_START) self.assertEqual(IvrsParserTest.dte_47h.DeviceID, 0xC0DE) self.assertEqual(IvrsParserTest.dte_47h.DTESetting, 0) self.assertEqual(IvrsParserTest.dte_47h.ExtendedDTESetting, 0xABADBABE) self.assertEqual(IvrsParserTest.dte_47h.EndDeviceID, 0xFFFF) def test_ivrs_parser_dte_48h(self): # Special device (IOAPIC, HPET) dte_t_48h = bytes([0x48, 0x00, 0x00, 0x00, 0x15, 0xAD, 0xDE, 0x01]) IvrsParserTest.dte_48h = IVRS_TABLE.DEVICE_TABLE_ENTRY.Factory(dte_t_48h) self.assertNotEqual(IvrsParserTest.dte_48h.Encode(), None) self.assertEqual(IvrsParserTest.dte_48h.Type, IVRS_TABLE.DEVICE_TABLE_ENTRY.DTE_TYPE.SPECIAL) self.assertEqual(IvrsParserTest.dte_48h.DTESetting, 0) self.assertEqual(IvrsParserTest.dte_48h.Handle, 0x15) self.assertEqual(IvrsParserTest.dte_48h.SourceDeviceID, 0xDEAD) self.assertEqual(IvrsParserTest.dte_48h.Variety, 0x01) def test_ivrs_parser_dte_f0h_0(self): # ACPI device without UID dte_t_f0h_0 = bytes([0xF0, 0x11, 0x11, 0xF6, 0x46, 0x41, 0x4B, 0x45, 0x30, 0x30, 0x30, 0x30, # HID: 'FAKE0000' 0x43, 0x4F, 0x4D, 0x50, 0x30, 0x30, 0x30, 0x30, # CID: 'COMP0000' 0x00, # UID format 0x00]) # UID length IvrsParserTest.dte_f0h_0 = IVRS_TABLE.DEVICE_TABLE_ENTRY.Factory(dte_t_f0h_0) self.assertNotEqual(IvrsParserTest.dte_f0h_0.Encode(), None) self.assertEqual(IvrsParserTest.dte_f0h_0.Type, IVRS_TABLE.DEVICE_TABLE_ENTRY.DTE_TYPE.ACPI) self.assertEqual(IvrsParserTest.dte_f0h_0.DeviceID, 0x1111) self.assertEqual(IvrsParserTest.dte_f0h_0.DTESetting, 0xF6) self.assertEqual(IvrsParserTest.dte_f0h_0.HID, b'FAKE0000') self.assertEqual(IvrsParserTest.dte_f0h_0.CID, b'COMP0000') self.assertEqual(IvrsParserTest.dte_f0h_0.UIDFormat, 0x00) self.assertEqual(IvrsParserTest.dte_f0h_0.UIDLength, 0x00) def test_ivrs_parser_dte_f0h_1(self): # ACPI device with integer UID dte_t_f0h_1 = bytes([0xF0, 0x11, 0x11, 0xF6, 0x46, 0x41, 0x4B, 0x45, 0x30, 0x30, 0x30, 0x30, # HID: 'FAKE0000' 0x43, 0x4F, 0x4D, 0x50, 0x30, 0x30, 0x30, 0x30, # CID: 'COMP0000' 0x01, # UID format 0x08, # UID length 0x0D, 0xF0, 0xED, 0xFE, 0xEF, 0xBE, 0xAD, 0xDE]) # UID: 0xDEADBEEFFEEDF00D IvrsParserTest.dte_f0h_1 = IVRS_TABLE.DEVICE_TABLE_ENTRY.Factory(dte_t_f0h_1) self.assertNotEqual(IvrsParserTest.dte_f0h_1.Encode(), None) self.assertEqual(IvrsParserTest.dte_f0h_1.Type, IVRS_TABLE.DEVICE_TABLE_ENTRY.DTE_TYPE.ACPI) self.assertEqual(IvrsParserTest.dte_f0h_1.DeviceID, 0x1111) self.assertEqual(IvrsParserTest.dte_f0h_1.DTESetting, 0xF6) self.assertEqual(IvrsParserTest.dte_f0h_1.HID, b'FAKE0000') self.assertEqual(IvrsParserTest.dte_f0h_1.CID, b'COMP0000') self.assertEqual(IvrsParserTest.dte_f0h_1.UIDFormat, 0x01) self.assertEqual(IvrsParserTest.dte_f0h_1.UIDLength, 0x08) self.assertEqual(IvrsParserTest.dte_f0h_1.UID, 0xDEADBEEFFEEDF00D) def test_ivrs_parser_dte_f0h_2(self): # ACPI device with string UID dte_t_f0h_2 = bytes([0xF0, 0x11, 0x11, 0xF6, 0x46, 0x41, 0x4B, 0x45, 0x30, 0x30, 0x30, 0x30, # HID: 'FAKE0000' 0x43, 0x4F, 0x4D, 0x50, 0x30, 0x30, 0x30, 0x30, # CID: 'COMP0000' 0x02, # UID format 0x09, # UID length 0x5C, 0x5F, 0x53, 0x42, 0x2E, 0x46, 0x55, 0x52, 0x30]) # UID: '\_SB.FUR0' IvrsParserTest.dte_f0h_2 = IVRS_TABLE.DEVICE_TABLE_ENTRY.Factory(dte_t_f0h_2) self.assertNotEqual(IvrsParserTest.dte_f0h_2.Encode(), None) self.assertEqual(IvrsParserTest.dte_f0h_2.Type, IVRS_TABLE.DEVICE_TABLE_ENTRY.DTE_TYPE.ACPI) self.assertEqual(IvrsParserTest.dte_f0h_2.DeviceID, 0x1111) self.assertEqual(IvrsParserTest.dte_f0h_2.DTESetting, 0xF6) self.assertEqual(IvrsParserTest.dte_f0h_2.HID, b'FAKE0000') self.assertEqual(IvrsParserTest.dte_f0h_2.CID, b'COMP0000') self.assertEqual(IvrsParserTest.dte_f0h_2.UIDFormat, 0x02) self.assertEqual(IvrsParserTest.dte_f0h_2.UIDLength, 0x09) self.assertEqual(IvrsParserTest.dte_f0h_2.UID, b'\\_SB.FUR0') def test_ivrs_parser_ivhd_10h(self): # I/O Virtualization Hardware Definition (IVHD) Type 10h header ivhd_t_10h = bytes([0x10, # Type 0x90, # Fl", https://github.com/tim-fi/pyecs,5cc3645e3ee0bc0af02aea438092f00910976ec6,test/test_core.py::test_automatic_cleanup,test/test_core.py,NIO,flaky,Deleted,nan,classify;root_cause,"from uuid import uuid1 import pytest from pyecs import core class ComponentFullname(core.Component): ... class ComponentA(core.Component, identifier=""A""): ... class ComponentB(core.Component, identifier=""B""): ... def test_components(): assert ComponentFullname in core._COMPONENT_REGISTRY assert core._COMPONENT_REGISTRY[ComponentFullname] == ""ComponentFullname"" assert ""ComponentFullname"" in core._COMPONENT_REGISTRY_REV assert core._COMPONENT_REGISTRY_REV[""ComponentFullname""] == ComponentFullname assert ComponentA in core._COMPONENT_REGISTRY assert core._COMPONENT_REGISTRY[ComponentA] == ""A"" assert ""A"" in core._COMPONENT_REGISTRY_REV assert core._COMPONENT_REGISTRY_REV[""A""] == ComponentA assert ComponentB in core._COMPONENT_REGISTRY assert core._COMPONENT_REGISTRY[ComponentB] == ""B"" assert ""B"" in core._COMPONENT_REGISTRY_REV assert core._COMPONENT_REGISTRY_REV[""B""] == ComponentB def test_controller(): controller = core.ECSController() assert controller._components == { component_type: dict() for component_type in core._COMPONENT_REGISTRY_REV } uuid_a = controller.add_entity() assert uuid_a in controller._entities assert controller._entities[uuid_a] == [] componentA_a = ComponentA() controller.add_components(uuid_a, componentA_a) assert controller._entities[uuid_a] == [""A""] assert uuid_a in controller._components[""A""] assert controller._components[""A""][uuid_a] == componentA_a componentA_b = ComponentA() componentB_b = ComponentB() uuid_b = controller.add_entity(componentA_b, componentB_b) assert uuid_b in controller._entities assert len(controller._entities) == 2 assert controller._entities[uuid_b] == [""A"", ""B""] assert uuid_b in controller._components[""A""] assert controller._components[""A""][uuid_b] == componentA_b assert uuid_b in controller._components[""B""] assert controller._components[""B""][uuid_b] == componentB_b controller.delete_components(uuid_b, ComponentB, instant=True) assert controller._entities[uuid_b] == [""A""] assert uuid_b not in controller._components[""B""] controller.delete_entity(uuid_b, instant=True) assert uuid_b not in controller._entities assert uuid_b not in controller._components[""A""] uuid_c = uuid1() class UnregisteredComponent(core.Component): ... with pytest.raises(KeyError): # generate entity with unknown component type controller.add_entity(UnregisteredComponent()) with pytest.raises(KeyError): # add component to unknown entity id controller.add_components(uuid_c, ComponentA()) with pytest.raises(KeyError): # add component to entity that already has a component of that type controller.add_components(uuid_a, ComponentA()) with pytest.raises(KeyError): # delete unknown entity controller.get_components(uuid_c, ComponentA) with pytest.raises(KeyError): # get component of unknown type controller.get_components(uuid_a, UnregisteredComponent) with pytest.raises(KeyError): # get component which doesn't exist in entity controller.get_components(uuid_a, ComponentB) with pytest.raises(KeyError): # delete unknown entity controller.delete_entity(uuid_c) with pytest.raises(KeyError): # delete component in unknown entity controller.delete_components(uuid_c, ComponentA) with pytest.raises(KeyError): # delete component of unknown type controller.delete_components(uuid_a, UnregisteredComponent) with pytest.raises(KeyError): # delete component which doesn't exist in entity controller.delete_components(uuid_a, ComponentB) def test_persistant_data(): # Test persistant data on the controller @core.system() def system1(delta_t, data): data[""last_delta_t""] = delta_t @core.system() def system2(delta_t, data): assert data[""last_delta_t""] == delta_t controller = core.ECSController() controller.register_system(system1) controller.register_system(system2) controller._run_system(""system1"", 0) assert ""last_delta_t"" in controller.data assert controller.data[""last_delta_t""] == 0 controller._run_system(""system2"", 0) def test_systems(): @core.system((ComponentA,)) def system_a(delta_t, data, entities): # abuse delta_t to check if entities is of proper length assert len(entities) == delta_t assert all(isinstance(entity, core.EntityProxy) for entity in entities) assert all(len(entity.components) == 1 for entity in entities) assert all(""A"" in entity.components for entity in entities) assert all(isinstance(entity.A, ComponentA) for entity in entities) @core.system((ComponentA, ComponentB)) def system_ab(delta_t, data, entities): # abuse delta_t to check if entities is of proper length assert len(entities) == delta_t assert all(len(entity.components) == 2 for entity in entities) assert all( ""A"" in entity.components and ""B"" in entity.components for entity in entities ) assert all( isinstance(entity.A, ComponentA) and isinstance(entity.B, ComponentB) for entity in entities ) controller = core.ECSController() controller.register_system(system_a) controller.register_system(system_ab) def unknown_system(delta_t, data, entities): ... with pytest.raises(KeyError): controller.register_system(unknown_system) controller._run_system(""system_a"", 0) controller._run_system(""system_ab"", 0) uuid_a = controller.add_entity(ComponentA()) controller._run_system(""system_a"", 1) controller._run_system(""system_ab"", 0) uuid_b = controller.add_entity(ComponentA()) controller._run_system(""system_a"", 2) controller._run_system(""system_ab"", 0) controller.add_components(uuid_a, ComponentB()) controller._run_system(""system_a"", 2) controller._run_system(""system_ab"", 1) controller.add_components(uuid_b, ComponentB()) controller._run_system(""system_a"", 2) controller._run_system(""system_ab"", 2) _ = controller.add_entity(ComponentB()) controller._run_system(""system_a"", 2) controller._run_system(""system_ab"", 2) def test_system_precalculation(): controller = core.ECSController() uuid = controller.add_entity(ComponentA()) @core.system((ComponentA,)) def test_system(delta_t, data, entities): assert len(entities) == 1 assert entities[0].uuid == uuid controller.register_system(test_system) controller._run_system(""test_system"", 0) def test_quit_exception(): @core.system() def quit_timeout(delta_t, data): data[""tick""] += 1 if data[""tick""] == 4: raise core.Quit controller = core.ECSController() controller.register_system(quit_timeout) controller[""tick""] = 0 controller.run() assert controller[""tick""] == 4 def test_automatic_cleanup(): @core.system((ComponentA, ComponentB)) def delete_system(delta_t, data, entities): if len(entities) > 0: for entity in entities: del entity.A entity.delete() del entity.uuid assert not hasattr(entity, ""uuid"") else: raise core.Quit() controller = core.ECSController() controller.register_system(delete_system) uuid = controller.add_entity(ComponentA(), ComponentB()) controller.delete_components(uuid, ComponentB) assert controller._entities[uuid] == [""A"", ""B""] assert controller._to_be_delete == ([], [(uuid, ""B"")]) controller.delete_entity(uuid) assert uuid in controller._entities assert controller._to_be_delete == ([uuid], [(uuid, ""B"")]) controller._do_cleanup() assert controller._to_be_delete == ([], []) assert uuid not in controller._entities assert uuid not in controller._components[""A""] assert uuid not in controller._components[""B""] controller.add_entity(ComponentA(), ComponentB()) controller.run() assert len(controller._entities) == 0 ", https://github.com/tim-fi/pyecs,5cc3645e3ee0bc0af02aea438092f00910976ec6,test/test_core.py::test_controller,test/test_core.py,NIO,flaky,Deleted,nan,classify;root_cause,"from uuid import uuid1 import pytest from pyecs import core class ComponentFullname(core.Component): ... class ComponentA(core.Component, identifier=""A""): ... class ComponentB(core.Component, identifier=""B""): ... def test_components(): assert ComponentFullname in core._COMPONENT_REGISTRY assert core._COMPONENT_REGISTRY[ComponentFullname] == ""ComponentFullname"" assert ""ComponentFullname"" in core._COMPONENT_REGISTRY_REV assert core._COMPONENT_REGISTRY_REV[""ComponentFullname""] == ComponentFullname assert ComponentA in core._COMPONENT_REGISTRY assert core._COMPONENT_REGISTRY[ComponentA] == ""A"" assert ""A"" in core._COMPONENT_REGISTRY_REV assert core._COMPONENT_REGISTRY_REV[""A""] == ComponentA assert ComponentB in core._COMPONENT_REGISTRY assert core._COMPONENT_REGISTRY[ComponentB] == ""B"" assert ""B"" in core._COMPONENT_REGISTRY_REV assert core._COMPONENT_REGISTRY_REV[""B""] == ComponentB def test_controller(): controller = core.ECSController() assert controller._components == { component_type: dict() for component_type in core._COMPONENT_REGISTRY_REV } uuid_a = controller.add_entity() assert uuid_a in controller._entities assert controller._entities[uuid_a] == [] componentA_a = ComponentA() controller.add_components(uuid_a, componentA_a) assert controller._entities[uuid_a] == [""A""] assert uuid_a in controller._components[""A""] assert controller._components[""A""][uuid_a] == componentA_a componentA_b = ComponentA() componentB_b = ComponentB() uuid_b = controller.add_entity(componentA_b, componentB_b) assert uuid_b in controller._entities assert len(controller._entities) == 2 assert controller._entities[uuid_b] == [""A"", ""B""] assert uuid_b in controller._components[""A""] assert controller._components[""A""][uuid_b] == componentA_b assert uuid_b in controller._components[""B""] assert controller._components[""B""][uuid_b] == componentB_b controller.delete_components(uuid_b, ComponentB, instant=True) assert controller._entities[uuid_b] == [""A""] assert uuid_b not in controller._components[""B""] controller.delete_entity(uuid_b, instant=True) assert uuid_b not in controller._entities assert uuid_b not in controller._components[""A""] uuid_c = uuid1() class UnregisteredComponent(core.Component): ... with pytest.raises(KeyError): # generate entity with unknown component type controller.add_entity(UnregisteredComponent()) with pytest.raises(KeyError): # add component to unknown entity id controller.add_components(uuid_c, ComponentA()) with pytest.raises(KeyError): # add component to entity that already has a component of that type controller.add_components(uuid_a, ComponentA()) with pytest.raises(KeyError): # delete unknown entity controller.get_components(uuid_c, ComponentA) with pytest.raises(KeyError): # get component of unknown type controller.get_components(uuid_a, UnregisteredComponent) with pytest.raises(KeyError): # get component which doesn't exist in entity controller.get_components(uuid_a, ComponentB) with pytest.raises(KeyError): # delete unknown entity controller.delete_entity(uuid_c) with pytest.raises(KeyError): # delete component in unknown entity controller.delete_components(uuid_c, ComponentA) with pytest.raises(KeyError): # delete component of unknown type controller.delete_components(uuid_a, UnregisteredComponent) with pytest.raises(KeyError): # delete component which doesn't exist in entity controller.delete_components(uuid_a, ComponentB) def test_persistant_data(): # Test persistant data on the controller @core.system() def system1(delta_t, data): data[""last_delta_t""] = delta_t @core.system() def system2(delta_t, data): assert data[""last_delta_t""] == delta_t controller = core.ECSController() controller.register_system(system1) controller.register_system(system2) controller._run_system(""system1"", 0) assert ""last_delta_t"" in controller.data assert controller.data[""last_delta_t""] == 0 controller._run_system(""system2"", 0) def test_systems(): @core.system((ComponentA,)) def system_a(delta_t, data, entities): # abuse delta_t to check if entities is of proper length assert len(entities) == delta_t assert all(isinstance(entity, core.EntityProxy) for entity in entities) assert all(len(entity.components) == 1 for entity in entities) assert all(""A"" in entity.components for entity in entities) assert all(isinstance(entity.A, ComponentA) for entity in entities) @core.system((ComponentA, ComponentB)) def system_ab(delta_t, data, entities): # abuse delta_t to check if entities is of proper length assert len(entities) == delta_t assert all(len(entity.components) == 2 for entity in entities) assert all( ""A"" in entity.components and ""B"" in entity.components for entity in entities ) assert all( isinstance(entity.A, ComponentA) and isinstance(entity.B, ComponentB) for entity in entities ) controller = core.ECSController() controller.register_system(system_a) controller.register_system(system_ab) def unknown_system(delta_t, data, entities): ... with pytest.raises(KeyError): controller.register_system(unknown_system) controller._run_system(""system_a"", 0) controller._run_system(""system_ab"", 0) uuid_a = controller.add_entity(ComponentA()) controller._run_system(""system_a"", 1) controller._run_system(""system_ab"", 0) uuid_b = controller.add_entity(ComponentA()) controller._run_system(""system_a"", 2) controller._run_system(""system_ab"", 0) controller.add_components(uuid_a, ComponentB()) controller._run_system(""system_a"", 2) controller._run_system(""system_ab"", 1) controller.add_components(uuid_b, ComponentB()) controller._run_system(""system_a"", 2) controller._run_system(""system_ab"", 2) _ = controller.add_entity(ComponentB()) controller._run_system(""system_a"", 2) controller._run_system(""system_ab"", 2) def test_system_precalculation(): controller = core.ECSController() uuid = controller.add_entity(ComponentA()) @core.system((ComponentA,)) def test_system(delta_t, data, entities): assert len(entities) == 1 assert entities[0].uuid == uuid controller.register_system(test_system) controller._run_system(""test_system"", 0) def test_quit_exception(): @core.system() def quit_timeout(delta_t, data): data[""tick""] += 1 if data[""tick""] == 4: raise core.Quit controller = core.ECSController() controller.register_system(quit_timeout) controller[""tick""] = 0 controller.run() assert controller[""tick""] == 4 def test_automatic_cleanup(): @core.system((ComponentA, ComponentB)) def delete_system(delta_t, data, entities): if len(entities) > 0: for entity in entities: del entity.A entity.delete() del entity.uuid assert not hasattr(entity, ""uuid"") else: raise core.Quit() controller = core.ECSController() controller.register_system(delete_system) uuid = controller.add_entity(ComponentA(), ComponentB()) controller.delete_components(uuid, ComponentB) assert controller._entities[uuid] == [""A"", ""B""] assert controller._to_be_delete == ([], [(uuid, ""B"")]) controller.delete_entity(uuid) assert uuid in controller._entities assert controller._to_be_delete == ([uuid], [(uuid, ""B"")]) controller._do_cleanup() assert controller._to_be_delete == ([], []) assert uuid not in controller._entities assert uuid not in controller._components[""A""] assert uuid not in controller._components[""B""] controller.add_entity(ComponentA(), ComponentB()) controller.run() assert len(controller._entities) == 0 ", https://github.com/timkostka/ucal,df0ccbc5f8af54fc69df56191bd810481a17b0e3,tests/test_ucal.py::TestSyntax::test_function_ln,tests/test_ucal.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Perform unit tests on ucal.py. """""" import os import sys import unittest # add parent directory to path to ensure we test the correct ucal sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) try: import ucal except ModuleNotFoundError: print('ERROR: ucal not found in path') exit(1) print('Script is at', os.path.abspath(__file__)) print('Testing ucal at', os.path.abspath(ucal.__file__)) # ensure ucal is loaded from the correct place ucal_dir = os.path.dirname(os.path.dirname(os.path.abspath(ucal.__file__))) test_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) assert ucal_dir == test_dir class TestSyntax(unittest.TestCase): """"""Test for correct detection of various syntax error."""""" def test_string_conversion(self): """"""Test errors when converting to a string."""""" self.assertEqual(str(ucal.evaluate('1/m')), '1 m^-1') self.assertEqual(str(ucal.evaluate('m^2')), '1 m^2') self.assertEqual(str(ucal.evaluate('m^1.5')), '1 m^1.5') def test_invalid_equation(self): """"""Test errors when parsing invalid equations."""""" self.assertRaises(ucal.ParserError, ucal.evaluate, '%1') def test_quantity_equality(self): """"""Test Quantity.__eq__ function."""""" self.assertEqual(ucal.ucal.calculate('1m'), ucal.ucal.calculate('1m')) def test_quantity_addition(self): """"""Test for proper Quantity.__add__ functionality."""""" x = ucal.ucal.calculate('1m') self.assertEqual(str(x + x), '2 m') def test_quantity_subtraction(self): """"""Test for proper Quantity.__sub__ functionality."""""" x = ucal.ucal.calculate('1m') self.assertEqual(str(x - x), '0 m') def test_quantity_addition_error(self): """"""Test for proper Quantity.__add__ error checks."""""" x = ucal.ucal.calculate('1m') y = ucal.ucal.calculate('0') self.assertRaises(ucal.QuantityError, x.__add__, y) def test_quantity_subtraction_error(self): """"""Test for proper Quantity.__sub__ error checks."""""" x = ucal.ucal.calculate('1m') y = ucal.ucal.calculate('0') self.assertRaises(ucal.QuantityError, x.__sub__, y) def test_invalid_values(self): """"""Test errors when parsing invalid values."""""" self.assertRaises(ucal.ParserError, ucal.evaluate, '') self.assertRaises(ucal.ParserError, ucal.evaluate, '+++1') self.assertRaises(ucal.ParserError, ucal.evaluate, '+') self.assertRaises(ucal.ParserError, ucal.evaluate, '.') self.assertRaises(ucal.ParserError, ucal.evaluate, 'e10') self.assertRaises(ucal.ParserError, ucal.evaluate, '1ee10') self.assertRaises(ucal.ParserError, ucal.evaluate, '1E+-1') self.assertRaises(ucal.ParserError, ucal.evaluate, '!67') def test_basic_value_comprehension(self): """"""Test for valid value definitions."""""" self.assertEqual(ucal.evaluate('1'), '1') self.assertEqual(ucal.evaluate('123457890'), '123457890') self.assertEqual(ucal.evaluate('001'), '1') self.assertEqual(ucal.evaluate('.1'), '0.1') self.assertEqual(ucal.evaluate('+1'), '1') self.assertEqual(ucal.evaluate('-1'), '-1') self.assertEqual(ucal.evaluate('1e3'), '1000') def test_nested_prefix_comprehension(self): """"""Test for valid value definitions."""""" self.assertEqual(ucal.evaluate('1+-2'), '-1') def test_output_number_formatting(self): """"""Test for desired output format for numbers."""""" self.assertEqual(ucal.evaluate('0.1234567890'), '0.123456789') self.assertEqual(ucal.evaluate('1e9'), '1000000000') self.assertEqual(ucal.evaluate('+1e+9'), '1000000000') self.assertEqual(ucal.evaluate('-1e-3'), '-0.001') self.assertEqual(ucal.evaluate('1E6'), '1000000') def test_arithmetic(self): """"""Test basic arithmetic operations."""""" self.assertEqual(ucal.evaluate('1+2'), '3') self.assertEqual(ucal.evaluate('1-2'), '-1') self.assertEqual(ucal.evaluate('3*4'), '12') self.assertEqual(ucal.evaluate('8/2'), '4') self.assertEqual(ucal.evaluate('--1'), '1') def test_decimal_comprehension(self): """"""Test decimal number comprehension."""""" self.assertEqual(ucal.evaluate('0'), '0') self.assertEqual(ucal.evaluate('0123'), '123') self.assertEqual(ucal.evaluate('+1e555'), '1e555') self.assertEqual(ucal.evaluate('1e-67'), '1e-67') def test_binary_comprehension(self): """"""Test binary number comprehension."""""" self.assertEqual(ucal.evaluate('0B0'), '0') self.assertEqual(ucal.evaluate('0b0'), '0') self.assertEqual(ucal.evaluate('0b11'), '3') self.assertEqual(ucal.evaluate('0b1111'), '15') self.assertRaises(ucal.ParserError, ucal.evaluate, '0b') def test_hexadecimal_comprehension(self): """"""Test hexadecimal number comprehension."""""" self.assertEqual(ucal.evaluate('0x0'), '0') self.assertEqual(ucal.evaluate('0X0'), '0') self.assertEqual(ucal.evaluate('0x123'), '291') self.assertEqual(ucal.evaluate('0xAbCdEf'), '11259375') self.assertRaises(ucal.ParserError, ucal.evaluate, '0x') self.assertRaises(ucal.ParserError, ucal.evaluate, '0xAG') def test_hexadecimal_errors(self): """"""Test hexadecimal number error detection."""""" self.assertIn('only integers', ucal.interpret('1m in hex')) def test_binary_errors(self): """"""Test binary number error detection."""""" self.assertIn('only integers', ucal.interpret('1m in bin')) def test_hexadecimal_conversion(self): """"""Test conversion to hexademical numbers."""""" self.assertEqual(ucal.interpret('67 in hex'), '0x43') self.assertEqual(ucal.interpret('123 in hex'), '0x7B') def test_binary_conversion(self): """"""Test conversion to hexademical numbers."""""" self.assertEqual(ucal.interpret('67 in bin'), '0b1000011') def test_output_units(self): """"""Test automatic output conversions."""""" self.assertEqual(ucal.evaluate('1A*Ohm'), '1 V') self.assertEqual(ucal.evaluate('1/s'), '1 Hz') def test_target_simple_units(self): """"""Test conversion to specified units."""""" self.assertEqual(ucal.interpret('1m to mm'), '1000 mm') self.assertEqual(ucal.interpret('1m as mm'), '1000 mm') self.assertEqual(ucal.interpret('1m in mm'), '1000 mm') def test_target_compound_units(self): """"""Test conversion to specified units."""""" self.assertEqual(ucal.interpret('1in^2 to mm^2'), '645.16 mm^2') def test_target_units_fallback(self): """"""Test fallback conversion if target units are invalid."""""" self.assertEqual(ucal.interpret('1 in kg'), '0.0254 kg m') def test_factorial(self): """"""Test the factorial postfix operator."""""" self.assertEqual(ucal.evaluate('0!'), '1') self.assertEqual(ucal.evaluate('1!'), '1') self.assertEqual(ucal.evaluate('2!'), '2') self.assertEqual(ucal.evaluate('5!'), '120') self.assertEqual(ucal.evaluate('18!'), '6402373705728000') def test_factorial_error(self): """"""Test invalid uses of the factorial postfix operator."""""" self.assertRaises(ucal.QuantityError, ucal.evaluate, '(1m)!') self.assertRaises(ucal.QuantityError, ucal.evaluate, '1.5!') self.assertRaises(ucal.QuantityError, ucal.evaluate, '(-1)!') def test_percent(self): """"""Test percentage evaluation."""""" self.assertEqual(ucal.evaluate('100%'), '1') self.assertEqual(ucal.evaluate('50%'), '0.5') self.assertEqual(ucal.evaluate('25%'), '0.25') def test_unbalanced_parentheses(self): """"""Test for unbalanced parentheses."""""" self.assertRaises(ucal.ParserError, ucal.evaluate, '1)') self.assertRaises(ucal.ParserError, ucal.evaluate, '(1') self.assertRaises(ucal.ParserError, ucal.evaluate, '(1))') self.assertRaises(ucal.ParserError, ucal.evaluate, '((1)') self.assertRaises(ucal.ParserError, ucal.evaluate, ')1(') def test_balanced_parentheses(self): """"""Test for balanced parentheses."""""" self.assertEqual(ucal.evaluate('(1)'), '1') self.assertEqual(ucal.evaluate('((1))'), '1') self.assertEqual(ucal.evaluate('(((((1)))))'), '1') def test_invalid_implicit_multiplication(self): """"""Test invalid implicit multiplication."""""" # implicit multiplication not allowed between two values self.assertRaises(ucal.ParserError, ucal.evaluate, '1 2') # not allowed between left variable and right value self.assertRaises(ucal.ParserError, ucal.evaluate, 'm 1') self.assertRaises(ucal.ParserError, ucal.evaluate, '1 m 1') def test_implicit_multiplication(self): """"""Test for valid implicit multiplication."""""" self.assertEqual(ucal.evaluate('1 m'), '1 m') self.assertEqual(ucal.evaluate('m m'), '1 m^2') self.assertEqual(ucal.evaluate('(1) m'), '1 m') self.assertEqual(ucal.evaluate('(1)(1)'), '1') def test_power_evaluation_order(self): """"""Test order of power evaluation."""""" self.assertEqual(ucal.evaluate('3^3^3'), '7625597484987') def test_evaluation_order_2(self): """"""Test order of power/factorial evaluations."""""" self.assertEqual(ucal.evaluate('3^2!'), '9') def test_unrecognized_function(self): """"""Test for an unrecognized function function."""""" self.assertRaises(ucal.ParserError, ucal.evaluate, 'thisIsUnrecognized(1)') def test_function_sqrt(self): """"""Test sqrt function."""""" self.assertEqual(ucal.evaluate('sqrt(4)'), '2') def test_function_exp(self): """"""Test exp function."""""" self.assertEqual(ucal.evaluate('exp(0)'), '1", https://github.com/timkostka/ucal,df0ccbc5f8af54fc69df56191bd810481a17b0e3,tests/test_ucal.py::TestSyntax::test_function_log,tests/test_ucal.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,""""""" Perform unit tests on ucal.py. """""" import os import sys import unittest # add parent directory to path to ensure we test the correct ucal sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) try: import ucal except ModuleNotFoundError: print('ERROR: ucal not found in path') exit(1) print('Script is at', os.path.abspath(__file__)) print('Testing ucal at', os.path.abspath(ucal.__file__)) # ensure ucal is loaded from the correct place ucal_dir = os.path.dirname(os.path.dirname(os.path.abspath(ucal.__file__))) test_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) assert ucal_dir == test_dir class TestSyntax(unittest.TestCase): """"""Test for correct detection of various syntax error."""""" def test_string_conversion(self): """"""Test errors when converting to a string."""""" self.assertEqual(str(ucal.evaluate('1/m')), '1 m^-1') self.assertEqual(str(ucal.evaluate('m^2')), '1 m^2') self.assertEqual(str(ucal.evaluate('m^1.5')), '1 m^1.5') def test_invalid_equation(self): """"""Test errors when parsing invalid equations."""""" self.assertRaises(ucal.ParserError, ucal.evaluate, '%1') def test_quantity_equality(self): """"""Test Quantity.__eq__ function."""""" self.assertEqual(ucal.ucal.calculate('1m'), ucal.ucal.calculate('1m')) def test_quantity_addition(self): """"""Test for proper Quantity.__add__ functionality."""""" x = ucal.ucal.calculate('1m') self.assertEqual(str(x + x), '2 m') def test_quantity_subtraction(self): """"""Test for proper Quantity.__sub__ functionality."""""" x = ucal.ucal.calculate('1m') self.assertEqual(str(x - x), '0 m') def test_quantity_addition_error(self): """"""Test for proper Quantity.__add__ error checks."""""" x = ucal.ucal.calculate('1m') y = ucal.ucal.calculate('0') self.assertRaises(ucal.QuantityError, x.__add__, y) def test_quantity_subtraction_error(self): """"""Test for proper Quantity.__sub__ error checks."""""" x = ucal.ucal.calculate('1m') y = ucal.ucal.calculate('0') self.assertRaises(ucal.QuantityError, x.__sub__, y) def test_invalid_values(self): """"""Test errors when parsing invalid values."""""" self.assertRaises(ucal.ParserError, ucal.evaluate, '') self.assertRaises(ucal.ParserError, ucal.evaluate, '+++1') self.assertRaises(ucal.ParserError, ucal.evaluate, '+') self.assertRaises(ucal.ParserError, ucal.evaluate, '.') self.assertRaises(ucal.ParserError, ucal.evaluate, 'e10') self.assertRaises(ucal.ParserError, ucal.evaluate, '1ee10') self.assertRaises(ucal.ParserError, ucal.evaluate, '1E+-1') self.assertRaises(ucal.ParserError, ucal.evaluate, '!67') def test_basic_value_comprehension(self): """"""Test for valid value definitions."""""" self.assertEqual(ucal.evaluate('1'), '1') self.assertEqual(ucal.evaluate('123457890'), '123457890') self.assertEqual(ucal.evaluate('001'), '1') self.assertEqual(ucal.evaluate('.1'), '0.1') self.assertEqual(ucal.evaluate('+1'), '1') self.assertEqual(ucal.evaluate('-1'), '-1') self.assertEqual(ucal.evaluate('1e3'), '1000') def test_nested_prefix_comprehension(self): """"""Test for valid value definitions."""""" self.assertEqual(ucal.evaluate('1+-2'), '-1') def test_output_number_formatting(self): """"""Test for desired output format for numbers."""""" self.assertEqual(ucal.evaluate('0.1234567890'), '0.123456789') self.assertEqual(ucal.evaluate('1e9'), '1000000000') self.assertEqual(ucal.evaluate('+1e+9'), '1000000000') self.assertEqual(ucal.evaluate('-1e-3'), '-0.001') self.assertEqual(ucal.evaluate('1E6'), '1000000') def test_arithmetic(self): """"""Test basic arithmetic operations."""""" self.assertEqual(ucal.evaluate('1+2'), '3') self.assertEqual(ucal.evaluate('1-2'), '-1') self.assertEqual(ucal.evaluate('3*4'), '12') self.assertEqual(ucal.evaluate('8/2'), '4') self.assertEqual(ucal.evaluate('--1'), '1') def test_decimal_comprehension(self): """"""Test decimal number comprehension."""""" self.assertEqual(ucal.evaluate('0'), '0') self.assertEqual(ucal.evaluate('0123'), '123') self.assertEqual(ucal.evaluate('+1e555'), '1e555') self.assertEqual(ucal.evaluate('1e-67'), '1e-67') def test_binary_comprehension(self): """"""Test binary number comprehension."""""" self.assertEqual(ucal.evaluate('0B0'), '0') self.assertEqual(ucal.evaluate('0b0'), '0') self.assertEqual(ucal.evaluate('0b11'), '3') self.assertEqual(ucal.evaluate('0b1111'), '15') self.assertRaises(ucal.ParserError, ucal.evaluate, '0b') def test_hexadecimal_comprehension(self): """"""Test hexadecimal number comprehension."""""" self.assertEqual(ucal.evaluate('0x0'), '0') self.assertEqual(ucal.evaluate('0X0'), '0') self.assertEqual(ucal.evaluate('0x123'), '291') self.assertEqual(ucal.evaluate('0xAbCdEf'), '11259375') self.assertRaises(ucal.ParserError, ucal.evaluate, '0x') self.assertRaises(ucal.ParserError, ucal.evaluate, '0xAG') def test_hexadecimal_errors(self): """"""Test hexadecimal number error detection."""""" self.assertIn('only integers', ucal.interpret('1m in hex')) def test_binary_errors(self): """"""Test binary number error detection."""""" self.assertIn('only integers', ucal.interpret('1m in bin')) def test_hexadecimal_conversion(self): """"""Test conversion to hexademical numbers."""""" self.assertEqual(ucal.interpret('67 in hex'), '0x43') self.assertEqual(ucal.interpret('123 in hex'), '0x7B') def test_binary_conversion(self): """"""Test conversion to hexademical numbers."""""" self.assertEqual(ucal.interpret('67 in bin'), '0b1000011') def test_output_units(self): """"""Test automatic output conversions."""""" self.assertEqual(ucal.evaluate('1A*Ohm'), '1 V') self.assertEqual(ucal.evaluate('1/s'), '1 Hz') def test_target_simple_units(self): """"""Test conversion to specified units."""""" self.assertEqual(ucal.interpret('1m to mm'), '1000 mm') self.assertEqual(ucal.interpret('1m as mm'), '1000 mm') self.assertEqual(ucal.interpret('1m in mm'), '1000 mm') def test_target_compound_units(self): """"""Test conversion to specified units."""""" self.assertEqual(ucal.interpret('1in^2 to mm^2'), '645.16 mm^2') def test_target_units_fallback(self): """"""Test fallback conversion if target units are invalid."""""" self.assertEqual(ucal.interpret('1 in kg'), '0.0254 kg m') def test_factorial(self): """"""Test the factorial postfix operator."""""" self.assertEqual(ucal.evaluate('0!'), '1') self.assertEqual(ucal.evaluate('1!'), '1') self.assertEqual(ucal.evaluate('2!'), '2') self.assertEqual(ucal.evaluate('5!'), '120') self.assertEqual(ucal.evaluate('18!'), '6402373705728000') def test_factorial_error(self): """"""Test invalid uses of the factorial postfix operator."""""" self.assertRaises(ucal.QuantityError, ucal.evaluate, '(1m)!') self.assertRaises(ucal.QuantityError, ucal.evaluate, '1.5!') self.assertRaises(ucal.QuantityError, ucal.evaluate, '(-1)!') def test_percent(self): """"""Test percentage evaluation."""""" self.assertEqual(ucal.evaluate('100%'), '1') self.assertEqual(ucal.evaluate('50%'), '0.5') self.assertEqual(ucal.evaluate('25%'), '0.25') def test_unbalanced_parentheses(self): """"""Test for unbalanced parentheses."""""" self.assertRaises(ucal.ParserError, ucal.evaluate, '1)') self.assertRaises(ucal.ParserError, ucal.evaluate, '(1') self.assertRaises(ucal.ParserError, ucal.evaluate, '(1))') self.assertRaises(ucal.ParserError, ucal.evaluate, '((1)') self.assertRaises(ucal.ParserError, ucal.evaluate, ')1(') def test_balanced_parentheses(self): """"""Test for balanced parentheses."""""" self.assertEqual(ucal.evaluate('(1)'), '1') self.assertEqual(ucal.evaluate('((1))'), '1') self.assertEqual(ucal.evaluate('(((((1)))))'), '1') def test_invalid_implicit_multiplication(self): """"""Test invalid implicit multiplication."""""" # implicit multiplication not allowed between two values self.assertRaises(ucal.ParserError, ucal.evaluate, '1 2') # not allowed between left variable and right value self.assertRaises(ucal.ParserError, ucal.evaluate, 'm 1') self.assertRaises(ucal.ParserError, ucal.evaluate, '1 m 1') def test_implicit_multiplication(self): """"""Test for valid implicit multiplication."""""" self.assertEqual(ucal.evaluate('1 m'), '1 m') self.assertEqual(ucal.evaluate('m m'), '1 m^2') self.assertEqual(ucal.evaluate('(1) m'), '1 m') self.assertEqual(ucal.evaluate('(1)(1)'), '1') def test_power_evaluation_order(self): """"""Test order of power evaluation."""""" self.assertEqual(ucal.evaluate('3^3^3'), '7625597484987') def test_evaluation_order_2(self): """"""Test order of power/factorial evaluations."""""" self.assertEqual(ucal.evaluate('3^2!'), '9') def test_unrecognized_function(self): """"""Test for an unrecognized function function."""""" self.assertRaises(ucal.ParserError, ucal.evaluate, 'thisIsUnrecognized(1)') def test_function_sqrt(self): """"""Test sqrt function."""""" self.assertEqual(ucal.evaluate('sqrt(4)'), '2') def test_function_exp(self): """"""Test exp function."""""" self.assertEqual(ucal.evaluate('exp(0)'), '1", https://github.com/tjdevries/easy_python_requirements,2bf905f34065637cbc781d17d8e5806892fb66d5,test/test_mock_functions.py::test_mock_class_update,test/test_mock_functions.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python3 # -*- coding: utf-8 -*- from shutil import copyfile from os import remove, sep, walk import pathlib from easy_python_requirements.update import update_func, update_file, update_folder, update_class from easy_python_requirements.parsed import Parsed from easy_python_requirements.test_info import read_json_info class FileCleaner: def __init__(self, filename: str, temp_prefix='._tmp'): self.filename = filename self.temp_file = temp_prefix + filename.replace(sep, '') def __enter__(self): copyfile(self.filename, self.temp_file) def __exit__(self, type, value, traceback): copyfile(self.temp_file, self.filename) remove(self.temp_file) class ListFileCleaner: def __init__(self, filenames: list, temp_prefix='._tmp'): self.filenames = filenames self.filecleaners = [] for filename in self.filenames: self.filecleaners.append(FileCleaner(filename)) def __enter__(self): for filecleaner in self.filecleaners: filecleaner.__enter__() def __exit__(self, type, value, traceback): for filecleaner in self.filecleaners: filecleaner.__exit__(type, value, traceback) def test_mock_test_example_1(): with FileCleaner('./mock_functions/test_example_1.py'): from mock_functions.test_example_1 import test_feature_example_1 p = Parsed(test_feature_example_1) p.parse() desc = p.description assert(desc == 'This **shall** be caught') assert(p.requires_update is True) update_func(test_feature_example_1) with open('./mock_functions/test_example_1.py') as f: f_list = f.read().split('\n') info_line = '' for line in f_list: if 'TEST INFO' in line: info_line = line break assert('test_id' in info_line) def test_mock_function_in_module_1(): with FileCleaner('./mock_functions/test_example_2.py'): from mock_functions.test_example_2 import ExampleClass p = Parsed(ExampleClass.function_2) p.parse() desc = p.description assert(desc == 'Requirement info') assert(p.requires_update is True) update_func(ExampleClass.function_2) with open('./mock_functions/test_example_2.py') as f: f_list = f.read().split('\n') info_line = '' for line in f_list: if 'TEST INFO' in line: info_line = line break assert('test_id' in info_line) def test_mock_function_with_multiple_modules(): with FileCleaner('./mock_functions/test_example_3.py'): from mock_functions.test_example_3 import SecondClassExample p = Parsed(SecondClassExample.this_doc_string_should_change) p.parse() desc = p.description assert(desc == 'This is the only info that should be read or changed') assert(p.requires_update is True) update_func(SecondClassExample.this_doc_string_should_change) with open('./mock_functions/test_example_3.py') as f: f_list = f.read().split('\n') for line in f_list[SecondClassExample.this_doc_string_should_change.__code__.co_firstlineno:]: if 'TEST INFO' in line: assert('test_id' in line) break def test_mock_module_with_two_updates(): """""" This test should demonstrate adding two INFOs, one to the class and one to its function. It should add the module one first, and then the function. It should make sure that it uses the highest id found. """""" with FileCleaner('./mock_functions/test_module_stuff.py'): # Pretend we just ran into this when cycling through the files files_to_check = ['mock_functions/test_module_stuff.py'] for f in files_to_check: update_file(f) for f in files_to_check: index = 0 with open(f, 'r') as reader: for line in reader.readlines(): if index == 27: print(line) json_info = read_json_info(line) assert(json_info['test_id'] == 6) index += 1 def test_mock_folder_update(): """""" This test will take a directory and update all of the files there. Folder update does not check recursively if False is set. """""" effected_files = [] for path, subdirs, files in walk('./mock_functions/'): for name in files: if '__' in name: continue if 'pyc' in name: continue effected_files.append(str(pathlib.PurePath(path, name))) print(effected_files) with ListFileCleaner(effected_files): folder_to_check = './mock_functions/' update_folder(folder_to_check, False) for f in effected_files: with open(f, 'r') as reader: for line in reader.readlines(): if 'TEST INFO' in line: print(f, line) def test_mock_class_update(): with FileCleaner('./mock_functions/test_module_stuff.py'): from mock_functions.test_module_stuff import SecondClass update_class(SecondClass) with open('./mock_functions/test_module_stuff.py', 'r') as reader: desired_line = -1 index = 0 for line in reader.readlines(): if 'SecondClass' in line: desired_line = index + 2 if index == desired_line: print(line) json_info = read_json_info(line) assert(json_info['test_id'] > 1) index += 1 ", https://github.com/tjdevries/easy_python_requirements,2bf905f34065637cbc781d17d8e5806892fb66d5,test/test_mock_functions.py::test_mock_module_with_two_updates,test/test_mock_functions.py,OD,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python3 # -*- coding: utf-8 -*- from shutil import copyfile from os import remove, sep, walk import pathlib from easy_python_requirements.update import update_func, update_file, update_folder, update_class from easy_python_requirements.parsed import Parsed from easy_python_requirements.test_info import read_json_info class FileCleaner: def __init__(self, filename: str, temp_prefix='._tmp'): self.filename = filename self.temp_file = temp_prefix + filename.replace(sep, '') def __enter__(self): copyfile(self.filename, self.temp_file) def __exit__(self, type, value, traceback): copyfile(self.temp_file, self.filename) remove(self.temp_file) class ListFileCleaner: def __init__(self, filenames: list, temp_prefix='._tmp'): self.filenames = filenames self.filecleaners = [] for filename in self.filenames: self.filecleaners.append(FileCleaner(filename)) def __enter__(self): for filecleaner in self.filecleaners: filecleaner.__enter__() def __exit__(self, type, value, traceback): for filecleaner in self.filecleaners: filecleaner.__exit__(type, value, traceback) def test_mock_test_example_1(): with FileCleaner('./mock_functions/test_example_1.py'): from mock_functions.test_example_1 import test_feature_example_1 p = Parsed(test_feature_example_1) p.parse() desc = p.description assert(desc == 'This **shall** be caught') assert(p.requires_update is True) update_func(test_feature_example_1) with open('./mock_functions/test_example_1.py') as f: f_list = f.read().split('\n') info_line = '' for line in f_list: if 'TEST INFO' in line: info_line = line break assert('test_id' in info_line) def test_mock_function_in_module_1(): with FileCleaner('./mock_functions/test_example_2.py'): from mock_functions.test_example_2 import ExampleClass p = Parsed(ExampleClass.function_2) p.parse() desc = p.description assert(desc == 'Requirement info') assert(p.requires_update is True) update_func(ExampleClass.function_2) with open('./mock_functions/test_example_2.py') as f: f_list = f.read().split('\n') info_line = '' for line in f_list: if 'TEST INFO' in line: info_line = line break assert('test_id' in info_line) def test_mock_function_with_multiple_modules(): with FileCleaner('./mock_functions/test_example_3.py'): from mock_functions.test_example_3 import SecondClassExample p = Parsed(SecondClassExample.this_doc_string_should_change) p.parse() desc = p.description assert(desc == 'This is the only info that should be read or changed') assert(p.requires_update is True) update_func(SecondClassExample.this_doc_string_should_change) with open('./mock_functions/test_example_3.py') as f: f_list = f.read().split('\n') for line in f_list[SecondClassExample.this_doc_string_should_change.__code__.co_firstlineno:]: if 'TEST INFO' in line: assert('test_id' in line) break def test_mock_module_with_two_updates(): """""" This test should demonstrate adding two INFOs, one to the class and one to its function. It should add the module one first, and then the function. It should make sure that it uses the highest id found. """""" with FileCleaner('./mock_functions/test_module_stuff.py'): # Pretend we just ran into this when cycling through the files files_to_check = ['mock_functions/test_module_stuff.py'] for f in files_to_check: update_file(f) for f in files_to_check: index = 0 with open(f, 'r') as reader: for line in reader.readlines(): if index == 27: print(line) json_info = read_json_info(line) assert(json_info['test_id'] == 6) index += 1 def test_mock_folder_update(): """""" This test will take a directory and update all of the files there. Folder update does not check recursively if False is set. """""" effected_files = [] for path, subdirs, files in walk('./mock_functions/'): for name in files: if '__' in name: continue if 'pyc' in name: continue effected_files.append(str(pathlib.PurePath(path, name))) print(effected_files) with ListFileCleaner(effected_files): folder_to_check = './mock_functions/' update_folder(folder_to_check, False) for f in effected_files: with open(f, 'r') as reader: for line in reader.readlines(): if 'TEST INFO' in line: print(f, line) def test_mock_class_update(): with FileCleaner('./mock_functions/test_module_stuff.py'): from mock_functions.test_module_stuff import SecondClass update_class(SecondClass) with open('./mock_functions/test_module_stuff.py', 'r') as reader: desired_line = -1 index = 0 for line in reader.readlines(): if 'SecondClass' in line: desired_line = index + 2 if index == desired_line: print(line) json_info = read_json_info(line) assert(json_info['test_id'] > 1) index += 1 ", https://github.com/tmurph/todo-sync,32915e9a1509aa2c4083613e256cb0d502c441aa,tests/test_org_to_asana.py::test_o2a_behind_make_fn[a_node0-expected_node0],tests/test_org_to_asana.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest import unittest.mock as mock import todo_sync.backends.asana as a import todo_sync.backends.org as o import todo_sync.mappers.org_to_asana as o2a from .helpers import assert_trees_equal_p from .test_org import mock_headline_node, mock_filename_node from .test_org import mock_source as mock_org_source from .test_asana import mock_task_node, mock_project_node from .test_asana import mock_source as mock_asana_source MOCK_NAME_TITLE = MOCK_TITLE_NAME = 'REQUIRED' MOCK_TASK_ID = 1 MOCK_PROJECT_ID = 2 def mapped_mock_headline_node(info_dict=None): d = {'title': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_headline_node(d) def mapped_mock_task_node(info_dict=None): d = {'id': MOCK_TASK_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_task_node(d) def mapped_mock_filename_node(info_dict=None): d = {} if info_dict: d.update(info_dict) return mock_filename_node(d) def mapped_mock_project_node(info_dict=None): d = {'id': MOCK_PROJECT_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_project_node(d) def test_o2a_behind_source_creation(mocker): ""Can we get an Org Source?"" mocker.patch('pexpect.spawn') mocker.patch('pexpect.replwrap.REPLWrapper') s = o2a.behind_source(None, dry_run=True) assert isinstance(s, o.Source) def test_o2a_ahead_source_creation(mocker): ""Can we get an Asana Source?"" mocker.patch('asana.Client') s = o2a.ahead_source(None) assert isinstance(s, a.Source) @pytest.fixture def ahead_source(): with mock.patch('asana.Client'): mocked_source = mock.Mock(a.Source, wraps=mock_asana_source()) with mock.patch.object(a.Source, 'from_client', return_value=mocked_source): yield o2a.ahead_source(None) @pytest.fixture def behind_source(): with mock.patch('pexpect.spawn'): with mock.patch('pexpect.replwrap.REPLWrapper'): mocked_source = mock.Mock(o.Source, wraps=mock_org_source()) with mock.patch.object(o.Source, 'from_emacs_repl', return_value=mocked_source): yield o2a.behind_source(None) def test_o2a_behind_get_tree(behind_source): ""Does the Org Source have a special get_tree method?"" behind_source.get_tree() behind_source.get_all_items.assert_called_with( ['asana_id', 'asana_project_id']) def test_o2a_ahead_get_tree(ahead_source): ""Does the Asana Source have a special get_tree method?"" ahead_source.get_tree() ahead_source.get_all_items.assert_called_with() @pytest.mark.parametrize(""a_node, expected_node"", [ (mapped_mock_task_node(), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)})), (mapped_mock_task_node({'notes': 'my stuff'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': 'my stuff'})), (mapped_mock_task_node({'completed': True}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'DONE'})), (mapped_mock_task_node({'completed': False}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'TODO'})), (mapped_mock_task_node({'due_on': '2016-11-19'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-19'})), (mapped_mock_task_node({'due_at': '2016-11-20T03:34:00.000Z'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-20T03:34:00.000Z'})), (mapped_mock_task_node({'project_id': 261881552216474}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'asana_project_id': '261881552216474'})), (mapped_mock_project_node({'name': 'My Inbox'}), mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)})), (mapped_mock_task_node({'tags': {'@work'}}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@work'}})), (mapped_mock_task_node({'tags': set()}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': set()})), (mapped_mock_task_node({'notes': ''}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': ''}))]) def test_o2a_behind_make_fn(a_node, expected_node, behind_source): ""Does the Org Source have a special make_fn method?"" o_node = behind_source.make_fn(a_node) assert_trees_equal_p(o_node, expected_node, ['id']) @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node(), mapped_mock_task_node(), True), (mapped_mock_headline_node({'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_headline_node({'title': 'On the other hand ...', 'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node({'name': 'On the one hand ...'}), True), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_filename_node({'id': 'On the other hand ...'}), mapped_mock_project_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node({'id': 'On the other hand ...', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'On the one hand ...'}), True), (mapped_mock_headline_node, mapped_mock_project_node(), False), (mapped_mock_headline_node({'title': 'A ""project"" headline'}), mapped_mock_project_node({'name': 'A ""project"" headline'}), False), (mapped_mock_filename_node(), mapped_mock_task_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_task_node({'name': 'My Inbox'}), False)]) def test_o2a_map_fn(o_node, a_node, expected): ""Can we map from Org nodes to Asana nodes?"" assert o2a.map_fn(o_node, a_node) == expected @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node(), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node(), mapped_mock_project_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), False), (mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@home'}}), mapped_mock_task_node({'tags': {'@work'}}), False)]) def test_o2a_eql_fn(o_node, a_node, expected): ""Can we compare Org nodes to Asana nodes?"" assert o2a.eql_fn(o_node, a_node) == expected ", https://github.com/tmurph/todo-sync,32915e9a1509aa2c4083613e256cb0d502c441aa,tests/test_org_to_asana.py::test_o2a_behind_make_fn[a_node1-expected_node1],tests/test_org_to_asana.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest import unittest.mock as mock import todo_sync.backends.asana as a import todo_sync.backends.org as o import todo_sync.mappers.org_to_asana as o2a from .helpers import assert_trees_equal_p from .test_org import mock_headline_node, mock_filename_node from .test_org import mock_source as mock_org_source from .test_asana import mock_task_node, mock_project_node from .test_asana import mock_source as mock_asana_source MOCK_NAME_TITLE = MOCK_TITLE_NAME = 'REQUIRED' MOCK_TASK_ID = 1 MOCK_PROJECT_ID = 2 def mapped_mock_headline_node(info_dict=None): d = {'title': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_headline_node(d) def mapped_mock_task_node(info_dict=None): d = {'id': MOCK_TASK_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_task_node(d) def mapped_mock_filename_node(info_dict=None): d = {} if info_dict: d.update(info_dict) return mock_filename_node(d) def mapped_mock_project_node(info_dict=None): d = {'id': MOCK_PROJECT_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_project_node(d) def test_o2a_behind_source_creation(mocker): ""Can we get an Org Source?"" mocker.patch('pexpect.spawn') mocker.patch('pexpect.replwrap.REPLWrapper') s = o2a.behind_source(None, dry_run=True) assert isinstance(s, o.Source) def test_o2a_ahead_source_creation(mocker): ""Can we get an Asana Source?"" mocker.patch('asana.Client') s = o2a.ahead_source(None) assert isinstance(s, a.Source) @pytest.fixture def ahead_source(): with mock.patch('asana.Client'): mocked_source = mock.Mock(a.Source, wraps=mock_asana_source()) with mock.patch.object(a.Source, 'from_client', return_value=mocked_source): yield o2a.ahead_source(None) @pytest.fixture def behind_source(): with mock.patch('pexpect.spawn'): with mock.patch('pexpect.replwrap.REPLWrapper'): mocked_source = mock.Mock(o.Source, wraps=mock_org_source()) with mock.patch.object(o.Source, 'from_emacs_repl', return_value=mocked_source): yield o2a.behind_source(None) def test_o2a_behind_get_tree(behind_source): ""Does the Org Source have a special get_tree method?"" behind_source.get_tree() behind_source.get_all_items.assert_called_with( ['asana_id', 'asana_project_id']) def test_o2a_ahead_get_tree(ahead_source): ""Does the Asana Source have a special get_tree method?"" ahead_source.get_tree() ahead_source.get_all_items.assert_called_with() @pytest.mark.parametrize(""a_node, expected_node"", [ (mapped_mock_task_node(), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)})), (mapped_mock_task_node({'notes': 'my stuff'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': 'my stuff'})), (mapped_mock_task_node({'completed': True}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'DONE'})), (mapped_mock_task_node({'completed': False}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'TODO'})), (mapped_mock_task_node({'due_on': '2016-11-19'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-19'})), (mapped_mock_task_node({'due_at': '2016-11-20T03:34:00.000Z'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-20T03:34:00.000Z'})), (mapped_mock_task_node({'project_id': 261881552216474}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'asana_project_id': '261881552216474'})), (mapped_mock_project_node({'name': 'My Inbox'}), mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)})), (mapped_mock_task_node({'tags': {'@work'}}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@work'}})), (mapped_mock_task_node({'tags': set()}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': set()})), (mapped_mock_task_node({'notes': ''}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': ''}))]) def test_o2a_behind_make_fn(a_node, expected_node, behind_source): ""Does the Org Source have a special make_fn method?"" o_node = behind_source.make_fn(a_node) assert_trees_equal_p(o_node, expected_node, ['id']) @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node(), mapped_mock_task_node(), True), (mapped_mock_headline_node({'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_headline_node({'title': 'On the other hand ...', 'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node({'name': 'On the one hand ...'}), True), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_filename_node({'id': 'On the other hand ...'}), mapped_mock_project_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node({'id': 'On the other hand ...', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'On the one hand ...'}), True), (mapped_mock_headline_node, mapped_mock_project_node(), False), (mapped_mock_headline_node({'title': 'A ""project"" headline'}), mapped_mock_project_node({'name': 'A ""project"" headline'}), False), (mapped_mock_filename_node(), mapped_mock_task_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_task_node({'name': 'My Inbox'}), False)]) def test_o2a_map_fn(o_node, a_node, expected): ""Can we map from Org nodes to Asana nodes?"" assert o2a.map_fn(o_node, a_node) == expected @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node(), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node(), mapped_mock_project_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), False), (mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@home'}}), mapped_mock_task_node({'tags': {'@work'}}), False)]) def test_o2a_eql_fn(o_node, a_node, expected): ""Can we compare Org nodes to Asana nodes?"" assert o2a.eql_fn(o_node, a_node) == expected ", https://github.com/tmurph/todo-sync,32915e9a1509aa2c4083613e256cb0d502c441aa,tests/test_org_to_asana.py::test_o2a_behind_make_fn[a_node10-expected_node10],tests/test_org_to_asana.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest import unittest.mock as mock import todo_sync.backends.asana as a import todo_sync.backends.org as o import todo_sync.mappers.org_to_asana as o2a from .helpers import assert_trees_equal_p from .test_org import mock_headline_node, mock_filename_node from .test_org import mock_source as mock_org_source from .test_asana import mock_task_node, mock_project_node from .test_asana import mock_source as mock_asana_source MOCK_NAME_TITLE = MOCK_TITLE_NAME = 'REQUIRED' MOCK_TASK_ID = 1 MOCK_PROJECT_ID = 2 def mapped_mock_headline_node(info_dict=None): d = {'title': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_headline_node(d) def mapped_mock_task_node(info_dict=None): d = {'id': MOCK_TASK_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_task_node(d) def mapped_mock_filename_node(info_dict=None): d = {} if info_dict: d.update(info_dict) return mock_filename_node(d) def mapped_mock_project_node(info_dict=None): d = {'id': MOCK_PROJECT_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_project_node(d) def test_o2a_behind_source_creation(mocker): ""Can we get an Org Source?"" mocker.patch('pexpect.spawn') mocker.patch('pexpect.replwrap.REPLWrapper') s = o2a.behind_source(None, dry_run=True) assert isinstance(s, o.Source) def test_o2a_ahead_source_creation(mocker): ""Can we get an Asana Source?"" mocker.patch('asana.Client') s = o2a.ahead_source(None) assert isinstance(s, a.Source) @pytest.fixture def ahead_source(): with mock.patch('asana.Client'): mocked_source = mock.Mock(a.Source, wraps=mock_asana_source()) with mock.patch.object(a.Source, 'from_client', return_value=mocked_source): yield o2a.ahead_source(None) @pytest.fixture def behind_source(): with mock.patch('pexpect.spawn'): with mock.patch('pexpect.replwrap.REPLWrapper'): mocked_source = mock.Mock(o.Source, wraps=mock_org_source()) with mock.patch.object(o.Source, 'from_emacs_repl', return_value=mocked_source): yield o2a.behind_source(None) def test_o2a_behind_get_tree(behind_source): ""Does the Org Source have a special get_tree method?"" behind_source.get_tree() behind_source.get_all_items.assert_called_with( ['asana_id', 'asana_project_id']) def test_o2a_ahead_get_tree(ahead_source): ""Does the Asana Source have a special get_tree method?"" ahead_source.get_tree() ahead_source.get_all_items.assert_called_with() @pytest.mark.parametrize(""a_node, expected_node"", [ (mapped_mock_task_node(), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)})), (mapped_mock_task_node({'notes': 'my stuff'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': 'my stuff'})), (mapped_mock_task_node({'completed': True}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'DONE'})), (mapped_mock_task_node({'completed': False}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'TODO'})), (mapped_mock_task_node({'due_on': '2016-11-19'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-19'})), (mapped_mock_task_node({'due_at': '2016-11-20T03:34:00.000Z'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-20T03:34:00.000Z'})), (mapped_mock_task_node({'project_id': 261881552216474}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'asana_project_id': '261881552216474'})), (mapped_mock_project_node({'name': 'My Inbox'}), mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)})), (mapped_mock_task_node({'tags': {'@work'}}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@work'}})), (mapped_mock_task_node({'tags': set()}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': set()})), (mapped_mock_task_node({'notes': ''}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': ''}))]) def test_o2a_behind_make_fn(a_node, expected_node, behind_source): ""Does the Org Source have a special make_fn method?"" o_node = behind_source.make_fn(a_node) assert_trees_equal_p(o_node, expected_node, ['id']) @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node(), mapped_mock_task_node(), True), (mapped_mock_headline_node({'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_headline_node({'title': 'On the other hand ...', 'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node({'name': 'On the one hand ...'}), True), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_filename_node({'id': 'On the other hand ...'}), mapped_mock_project_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node({'id': 'On the other hand ...', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'On the one hand ...'}), True), (mapped_mock_headline_node, mapped_mock_project_node(), False), (mapped_mock_headline_node({'title': 'A ""project"" headline'}), mapped_mock_project_node({'name': 'A ""project"" headline'}), False), (mapped_mock_filename_node(), mapped_mock_task_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_task_node({'name': 'My Inbox'}), False)]) def test_o2a_map_fn(o_node, a_node, expected): ""Can we map from Org nodes to Asana nodes?"" assert o2a.map_fn(o_node, a_node) == expected @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node(), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node(), mapped_mock_project_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), False), (mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@home'}}), mapped_mock_task_node({'tags': {'@work'}}), False)]) def test_o2a_eql_fn(o_node, a_node, expected): ""Can we compare Org nodes to Asana nodes?"" assert o2a.eql_fn(o_node, a_node) == expected ", https://github.com/tmurph/todo-sync,32915e9a1509aa2c4083613e256cb0d502c441aa,tests/test_org_to_asana.py::test_o2a_behind_make_fn[a_node2-expected_node2],tests/test_org_to_asana.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest import unittest.mock as mock import todo_sync.backends.asana as a import todo_sync.backends.org as o import todo_sync.mappers.org_to_asana as o2a from .helpers import assert_trees_equal_p from .test_org import mock_headline_node, mock_filename_node from .test_org import mock_source as mock_org_source from .test_asana import mock_task_node, mock_project_node from .test_asana import mock_source as mock_asana_source MOCK_NAME_TITLE = MOCK_TITLE_NAME = 'REQUIRED' MOCK_TASK_ID = 1 MOCK_PROJECT_ID = 2 def mapped_mock_headline_node(info_dict=None): d = {'title': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_headline_node(d) def mapped_mock_task_node(info_dict=None): d = {'id': MOCK_TASK_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_task_node(d) def mapped_mock_filename_node(info_dict=None): d = {} if info_dict: d.update(info_dict) return mock_filename_node(d) def mapped_mock_project_node(info_dict=None): d = {'id': MOCK_PROJECT_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_project_node(d) def test_o2a_behind_source_creation(mocker): ""Can we get an Org Source?"" mocker.patch('pexpect.spawn') mocker.patch('pexpect.replwrap.REPLWrapper') s = o2a.behind_source(None, dry_run=True) assert isinstance(s, o.Source) def test_o2a_ahead_source_creation(mocker): ""Can we get an Asana Source?"" mocker.patch('asana.Client') s = o2a.ahead_source(None) assert isinstance(s, a.Source) @pytest.fixture def ahead_source(): with mock.patch('asana.Client'): mocked_source = mock.Mock(a.Source, wraps=mock_asana_source()) with mock.patch.object(a.Source, 'from_client', return_value=mocked_source): yield o2a.ahead_source(None) @pytest.fixture def behind_source(): with mock.patch('pexpect.spawn'): with mock.patch('pexpect.replwrap.REPLWrapper'): mocked_source = mock.Mock(o.Source, wraps=mock_org_source()) with mock.patch.object(o.Source, 'from_emacs_repl', return_value=mocked_source): yield o2a.behind_source(None) def test_o2a_behind_get_tree(behind_source): ""Does the Org Source have a special get_tree method?"" behind_source.get_tree() behind_source.get_all_items.assert_called_with( ['asana_id', 'asana_project_id']) def test_o2a_ahead_get_tree(ahead_source): ""Does the Asana Source have a special get_tree method?"" ahead_source.get_tree() ahead_source.get_all_items.assert_called_with() @pytest.mark.parametrize(""a_node, expected_node"", [ (mapped_mock_task_node(), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)})), (mapped_mock_task_node({'notes': 'my stuff'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': 'my stuff'})), (mapped_mock_task_node({'completed': True}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'DONE'})), (mapped_mock_task_node({'completed': False}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'TODO'})), (mapped_mock_task_node({'due_on': '2016-11-19'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-19'})), (mapped_mock_task_node({'due_at': '2016-11-20T03:34:00.000Z'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-20T03:34:00.000Z'})), (mapped_mock_task_node({'project_id': 261881552216474}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'asana_project_id': '261881552216474'})), (mapped_mock_project_node({'name': 'My Inbox'}), mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)})), (mapped_mock_task_node({'tags': {'@work'}}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@work'}})), (mapped_mock_task_node({'tags': set()}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': set()})), (mapped_mock_task_node({'notes': ''}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': ''}))]) def test_o2a_behind_make_fn(a_node, expected_node, behind_source): ""Does the Org Source have a special make_fn method?"" o_node = behind_source.make_fn(a_node) assert_trees_equal_p(o_node, expected_node, ['id']) @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node(), mapped_mock_task_node(), True), (mapped_mock_headline_node({'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_headline_node({'title': 'On the other hand ...', 'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node({'name': 'On the one hand ...'}), True), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_filename_node({'id': 'On the other hand ...'}), mapped_mock_project_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node({'id': 'On the other hand ...', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'On the one hand ...'}), True), (mapped_mock_headline_node, mapped_mock_project_node(), False), (mapped_mock_headline_node({'title': 'A ""project"" headline'}), mapped_mock_project_node({'name': 'A ""project"" headline'}), False), (mapped_mock_filename_node(), mapped_mock_task_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_task_node({'name': 'My Inbox'}), False)]) def test_o2a_map_fn(o_node, a_node, expected): ""Can we map from Org nodes to Asana nodes?"" assert o2a.map_fn(o_node, a_node) == expected @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node(), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node(), mapped_mock_project_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), False), (mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@home'}}), mapped_mock_task_node({'tags': {'@work'}}), False)]) def test_o2a_eql_fn(o_node, a_node, expected): ""Can we compare Org nodes to Asana nodes?"" assert o2a.eql_fn(o_node, a_node) == expected ", https://github.com/tmurph/todo-sync,32915e9a1509aa2c4083613e256cb0d502c441aa,tests/test_org_to_asana.py::test_o2a_behind_make_fn[a_node3-expected_node3],tests/test_org_to_asana.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest import unittest.mock as mock import todo_sync.backends.asana as a import todo_sync.backends.org as o import todo_sync.mappers.org_to_asana as o2a from .helpers import assert_trees_equal_p from .test_org import mock_headline_node, mock_filename_node from .test_org import mock_source as mock_org_source from .test_asana import mock_task_node, mock_project_node from .test_asana import mock_source as mock_asana_source MOCK_NAME_TITLE = MOCK_TITLE_NAME = 'REQUIRED' MOCK_TASK_ID = 1 MOCK_PROJECT_ID = 2 def mapped_mock_headline_node(info_dict=None): d = {'title': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_headline_node(d) def mapped_mock_task_node(info_dict=None): d = {'id': MOCK_TASK_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_task_node(d) def mapped_mock_filename_node(info_dict=None): d = {} if info_dict: d.update(info_dict) return mock_filename_node(d) def mapped_mock_project_node(info_dict=None): d = {'id': MOCK_PROJECT_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_project_node(d) def test_o2a_behind_source_creation(mocker): ""Can we get an Org Source?"" mocker.patch('pexpect.spawn') mocker.patch('pexpect.replwrap.REPLWrapper') s = o2a.behind_source(None, dry_run=True) assert isinstance(s, o.Source) def test_o2a_ahead_source_creation(mocker): ""Can we get an Asana Source?"" mocker.patch('asana.Client') s = o2a.ahead_source(None) assert isinstance(s, a.Source) @pytest.fixture def ahead_source(): with mock.patch('asana.Client'): mocked_source = mock.Mock(a.Source, wraps=mock_asana_source()) with mock.patch.object(a.Source, 'from_client', return_value=mocked_source): yield o2a.ahead_source(None) @pytest.fixture def behind_source(): with mock.patch('pexpect.spawn'): with mock.patch('pexpect.replwrap.REPLWrapper'): mocked_source = mock.Mock(o.Source, wraps=mock_org_source()) with mock.patch.object(o.Source, 'from_emacs_repl', return_value=mocked_source): yield o2a.behind_source(None) def test_o2a_behind_get_tree(behind_source): ""Does the Org Source have a special get_tree method?"" behind_source.get_tree() behind_source.get_all_items.assert_called_with( ['asana_id', 'asana_project_id']) def test_o2a_ahead_get_tree(ahead_source): ""Does the Asana Source have a special get_tree method?"" ahead_source.get_tree() ahead_source.get_all_items.assert_called_with() @pytest.mark.parametrize(""a_node, expected_node"", [ (mapped_mock_task_node(), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)})), (mapped_mock_task_node({'notes': 'my stuff'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': 'my stuff'})), (mapped_mock_task_node({'completed': True}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'DONE'})), (mapped_mock_task_node({'completed': False}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'TODO'})), (mapped_mock_task_node({'due_on': '2016-11-19'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-19'})), (mapped_mock_task_node({'due_at': '2016-11-20T03:34:00.000Z'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-20T03:34:00.000Z'})), (mapped_mock_task_node({'project_id': 261881552216474}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'asana_project_id': '261881552216474'})), (mapped_mock_project_node({'name': 'My Inbox'}), mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)})), (mapped_mock_task_node({'tags': {'@work'}}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@work'}})), (mapped_mock_task_node({'tags': set()}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': set()})), (mapped_mock_task_node({'notes': ''}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': ''}))]) def test_o2a_behind_make_fn(a_node, expected_node, behind_source): ""Does the Org Source have a special make_fn method?"" o_node = behind_source.make_fn(a_node) assert_trees_equal_p(o_node, expected_node, ['id']) @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node(), mapped_mock_task_node(), True), (mapped_mock_headline_node({'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_headline_node({'title': 'On the other hand ...', 'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node({'name': 'On the one hand ...'}), True), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_filename_node({'id': 'On the other hand ...'}), mapped_mock_project_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node({'id': 'On the other hand ...', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'On the one hand ...'}), True), (mapped_mock_headline_node, mapped_mock_project_node(), False), (mapped_mock_headline_node({'title': 'A ""project"" headline'}), mapped_mock_project_node({'name': 'A ""project"" headline'}), False), (mapped_mock_filename_node(), mapped_mock_task_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_task_node({'name': 'My Inbox'}), False)]) def test_o2a_map_fn(o_node, a_node, expected): ""Can we map from Org nodes to Asana nodes?"" assert o2a.map_fn(o_node, a_node) == expected @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node(), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node(), mapped_mock_project_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), False), (mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@home'}}), mapped_mock_task_node({'tags': {'@work'}}), False)]) def test_o2a_eql_fn(o_node, a_node, expected): ""Can we compare Org nodes to Asana nodes?"" assert o2a.eql_fn(o_node, a_node) == expected ", https://github.com/tmurph/todo-sync,32915e9a1509aa2c4083613e256cb0d502c441aa,tests/test_org_to_asana.py::test_o2a_behind_make_fn[a_node4-expected_node4],tests/test_org_to_asana.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest import unittest.mock as mock import todo_sync.backends.asana as a import todo_sync.backends.org as o import todo_sync.mappers.org_to_asana as o2a from .helpers import assert_trees_equal_p from .test_org import mock_headline_node, mock_filename_node from .test_org import mock_source as mock_org_source from .test_asana import mock_task_node, mock_project_node from .test_asana import mock_source as mock_asana_source MOCK_NAME_TITLE = MOCK_TITLE_NAME = 'REQUIRED' MOCK_TASK_ID = 1 MOCK_PROJECT_ID = 2 def mapped_mock_headline_node(info_dict=None): d = {'title': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_headline_node(d) def mapped_mock_task_node(info_dict=None): d = {'id': MOCK_TASK_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_task_node(d) def mapped_mock_filename_node(info_dict=None): d = {} if info_dict: d.update(info_dict) return mock_filename_node(d) def mapped_mock_project_node(info_dict=None): d = {'id': MOCK_PROJECT_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_project_node(d) def test_o2a_behind_source_creation(mocker): ""Can we get an Org Source?"" mocker.patch('pexpect.spawn') mocker.patch('pexpect.replwrap.REPLWrapper') s = o2a.behind_source(None, dry_run=True) assert isinstance(s, o.Source) def test_o2a_ahead_source_creation(mocker): ""Can we get an Asana Source?"" mocker.patch('asana.Client') s = o2a.ahead_source(None) assert isinstance(s, a.Source) @pytest.fixture def ahead_source(): with mock.patch('asana.Client'): mocked_source = mock.Mock(a.Source, wraps=mock_asana_source()) with mock.patch.object(a.Source, 'from_client', return_value=mocked_source): yield o2a.ahead_source(None) @pytest.fixture def behind_source(): with mock.patch('pexpect.spawn'): with mock.patch('pexpect.replwrap.REPLWrapper'): mocked_source = mock.Mock(o.Source, wraps=mock_org_source()) with mock.patch.object(o.Source, 'from_emacs_repl', return_value=mocked_source): yield o2a.behind_source(None) def test_o2a_behind_get_tree(behind_source): ""Does the Org Source have a special get_tree method?"" behind_source.get_tree() behind_source.get_all_items.assert_called_with( ['asana_id', 'asana_project_id']) def test_o2a_ahead_get_tree(ahead_source): ""Does the Asana Source have a special get_tree method?"" ahead_source.get_tree() ahead_source.get_all_items.assert_called_with() @pytest.mark.parametrize(""a_node, expected_node"", [ (mapped_mock_task_node(), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)})), (mapped_mock_task_node({'notes': 'my stuff'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': 'my stuff'})), (mapped_mock_task_node({'completed': True}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'DONE'})), (mapped_mock_task_node({'completed': False}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'TODO'})), (mapped_mock_task_node({'due_on': '2016-11-19'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-19'})), (mapped_mock_task_node({'due_at': '2016-11-20T03:34:00.000Z'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-20T03:34:00.000Z'})), (mapped_mock_task_node({'project_id': 261881552216474}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'asana_project_id': '261881552216474'})), (mapped_mock_project_node({'name': 'My Inbox'}), mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)})), (mapped_mock_task_node({'tags': {'@work'}}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@work'}})), (mapped_mock_task_node({'tags': set()}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': set()})), (mapped_mock_task_node({'notes': ''}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': ''}))]) def test_o2a_behind_make_fn(a_node, expected_node, behind_source): ""Does the Org Source have a special make_fn method?"" o_node = behind_source.make_fn(a_node) assert_trees_equal_p(o_node, expected_node, ['id']) @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node(), mapped_mock_task_node(), True), (mapped_mock_headline_node({'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_headline_node({'title': 'On the other hand ...', 'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node({'name': 'On the one hand ...'}), True), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_filename_node({'id': 'On the other hand ...'}), mapped_mock_project_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node({'id': 'On the other hand ...', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'On the one hand ...'}), True), (mapped_mock_headline_node, mapped_mock_project_node(), False), (mapped_mock_headline_node({'title': 'A ""project"" headline'}), mapped_mock_project_node({'name': 'A ""project"" headline'}), False), (mapped_mock_filename_node(), mapped_mock_task_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_task_node({'name': 'My Inbox'}), False)]) def test_o2a_map_fn(o_node, a_node, expected): ""Can we map from Org nodes to Asana nodes?"" assert o2a.map_fn(o_node, a_node) == expected @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node(), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node(), mapped_mock_project_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), False), (mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@home'}}), mapped_mock_task_node({'tags': {'@work'}}), False)]) def test_o2a_eql_fn(o_node, a_node, expected): ""Can we compare Org nodes to Asana nodes?"" assert o2a.eql_fn(o_node, a_node) == expected ", https://github.com/tmurph/todo-sync,32915e9a1509aa2c4083613e256cb0d502c441aa,tests/test_org_to_asana.py::test_o2a_behind_make_fn[a_node5-expected_node5],tests/test_org_to_asana.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest import unittest.mock as mock import todo_sync.backends.asana as a import todo_sync.backends.org as o import todo_sync.mappers.org_to_asana as o2a from .helpers import assert_trees_equal_p from .test_org import mock_headline_node, mock_filename_node from .test_org import mock_source as mock_org_source from .test_asana import mock_task_node, mock_project_node from .test_asana import mock_source as mock_asana_source MOCK_NAME_TITLE = MOCK_TITLE_NAME = 'REQUIRED' MOCK_TASK_ID = 1 MOCK_PROJECT_ID = 2 def mapped_mock_headline_node(info_dict=None): d = {'title': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_headline_node(d) def mapped_mock_task_node(info_dict=None): d = {'id': MOCK_TASK_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_task_node(d) def mapped_mock_filename_node(info_dict=None): d = {} if info_dict: d.update(info_dict) return mock_filename_node(d) def mapped_mock_project_node(info_dict=None): d = {'id': MOCK_PROJECT_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_project_node(d) def test_o2a_behind_source_creation(mocker): ""Can we get an Org Source?"" mocker.patch('pexpect.spawn') mocker.patch('pexpect.replwrap.REPLWrapper') s = o2a.behind_source(None, dry_run=True) assert isinstance(s, o.Source) def test_o2a_ahead_source_creation(mocker): ""Can we get an Asana Source?"" mocker.patch('asana.Client') s = o2a.ahead_source(None) assert isinstance(s, a.Source) @pytest.fixture def ahead_source(): with mock.patch('asana.Client'): mocked_source = mock.Mock(a.Source, wraps=mock_asana_source()) with mock.patch.object(a.Source, 'from_client', return_value=mocked_source): yield o2a.ahead_source(None) @pytest.fixture def behind_source(): with mock.patch('pexpect.spawn'): with mock.patch('pexpect.replwrap.REPLWrapper'): mocked_source = mock.Mock(o.Source, wraps=mock_org_source()) with mock.patch.object(o.Source, 'from_emacs_repl', return_value=mocked_source): yield o2a.behind_source(None) def test_o2a_behind_get_tree(behind_source): ""Does the Org Source have a special get_tree method?"" behind_source.get_tree() behind_source.get_all_items.assert_called_with( ['asana_id', 'asana_project_id']) def test_o2a_ahead_get_tree(ahead_source): ""Does the Asana Source have a special get_tree method?"" ahead_source.get_tree() ahead_source.get_all_items.assert_called_with() @pytest.mark.parametrize(""a_node, expected_node"", [ (mapped_mock_task_node(), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)})), (mapped_mock_task_node({'notes': 'my stuff'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': 'my stuff'})), (mapped_mock_task_node({'completed': True}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'DONE'})), (mapped_mock_task_node({'completed': False}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'TODO'})), (mapped_mock_task_node({'due_on': '2016-11-19'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-19'})), (mapped_mock_task_node({'due_at': '2016-11-20T03:34:00.000Z'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-20T03:34:00.000Z'})), (mapped_mock_task_node({'project_id': 261881552216474}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'asana_project_id': '261881552216474'})), (mapped_mock_project_node({'name': 'My Inbox'}), mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)})), (mapped_mock_task_node({'tags': {'@work'}}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@work'}})), (mapped_mock_task_node({'tags': set()}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': set()})), (mapped_mock_task_node({'notes': ''}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': ''}))]) def test_o2a_behind_make_fn(a_node, expected_node, behind_source): ""Does the Org Source have a special make_fn method?"" o_node = behind_source.make_fn(a_node) assert_trees_equal_p(o_node, expected_node, ['id']) @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node(), mapped_mock_task_node(), True), (mapped_mock_headline_node({'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_headline_node({'title': 'On the other hand ...', 'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node({'name': 'On the one hand ...'}), True), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_filename_node({'id': 'On the other hand ...'}), mapped_mock_project_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node({'id': 'On the other hand ...', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'On the one hand ...'}), True), (mapped_mock_headline_node, mapped_mock_project_node(), False), (mapped_mock_headline_node({'title': 'A ""project"" headline'}), mapped_mock_project_node({'name': 'A ""project"" headline'}), False), (mapped_mock_filename_node(), mapped_mock_task_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_task_node({'name': 'My Inbox'}), False)]) def test_o2a_map_fn(o_node, a_node, expected): ""Can we map from Org nodes to Asana nodes?"" assert o2a.map_fn(o_node, a_node) == expected @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node(), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node(), mapped_mock_project_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), False), (mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@home'}}), mapped_mock_task_node({'tags': {'@work'}}), False)]) def test_o2a_eql_fn(o_node, a_node, expected): ""Can we compare Org nodes to Asana nodes?"" assert o2a.eql_fn(o_node, a_node) == expected ", https://github.com/tmurph/todo-sync,32915e9a1509aa2c4083613e256cb0d502c441aa,tests/test_org_to_asana.py::test_o2a_behind_make_fn[a_node6-expected_node6],tests/test_org_to_asana.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest import unittest.mock as mock import todo_sync.backends.asana as a import todo_sync.backends.org as o import todo_sync.mappers.org_to_asana as o2a from .helpers import assert_trees_equal_p from .test_org import mock_headline_node, mock_filename_node from .test_org import mock_source as mock_org_source from .test_asana import mock_task_node, mock_project_node from .test_asana import mock_source as mock_asana_source MOCK_NAME_TITLE = MOCK_TITLE_NAME = 'REQUIRED' MOCK_TASK_ID = 1 MOCK_PROJECT_ID = 2 def mapped_mock_headline_node(info_dict=None): d = {'title': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_headline_node(d) def mapped_mock_task_node(info_dict=None): d = {'id': MOCK_TASK_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_task_node(d) def mapped_mock_filename_node(info_dict=None): d = {} if info_dict: d.update(info_dict) return mock_filename_node(d) def mapped_mock_project_node(info_dict=None): d = {'id': MOCK_PROJECT_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_project_node(d) def test_o2a_behind_source_creation(mocker): ""Can we get an Org Source?"" mocker.patch('pexpect.spawn') mocker.patch('pexpect.replwrap.REPLWrapper') s = o2a.behind_source(None, dry_run=True) assert isinstance(s, o.Source) def test_o2a_ahead_source_creation(mocker): ""Can we get an Asana Source?"" mocker.patch('asana.Client') s = o2a.ahead_source(None) assert isinstance(s, a.Source) @pytest.fixture def ahead_source(): with mock.patch('asana.Client'): mocked_source = mock.Mock(a.Source, wraps=mock_asana_source()) with mock.patch.object(a.Source, 'from_client', return_value=mocked_source): yield o2a.ahead_source(None) @pytest.fixture def behind_source(): with mock.patch('pexpect.spawn'): with mock.patch('pexpect.replwrap.REPLWrapper'): mocked_source = mock.Mock(o.Source, wraps=mock_org_source()) with mock.patch.object(o.Source, 'from_emacs_repl', return_value=mocked_source): yield o2a.behind_source(None) def test_o2a_behind_get_tree(behind_source): ""Does the Org Source have a special get_tree method?"" behind_source.get_tree() behind_source.get_all_items.assert_called_with( ['asana_id', 'asana_project_id']) def test_o2a_ahead_get_tree(ahead_source): ""Does the Asana Source have a special get_tree method?"" ahead_source.get_tree() ahead_source.get_all_items.assert_called_with() @pytest.mark.parametrize(""a_node, expected_node"", [ (mapped_mock_task_node(), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)})), (mapped_mock_task_node({'notes': 'my stuff'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': 'my stuff'})), (mapped_mock_task_node({'completed': True}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'DONE'})), (mapped_mock_task_node({'completed': False}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'TODO'})), (mapped_mock_task_node({'due_on': '2016-11-19'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-19'})), (mapped_mock_task_node({'due_at': '2016-11-20T03:34:00.000Z'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-20T03:34:00.000Z'})), (mapped_mock_task_node({'project_id': 261881552216474}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'asana_project_id': '261881552216474'})), (mapped_mock_project_node({'name': 'My Inbox'}), mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)})), (mapped_mock_task_node({'tags': {'@work'}}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@work'}})), (mapped_mock_task_node({'tags': set()}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': set()})), (mapped_mock_task_node({'notes': ''}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': ''}))]) def test_o2a_behind_make_fn(a_node, expected_node, behind_source): ""Does the Org Source have a special make_fn method?"" o_node = behind_source.make_fn(a_node) assert_trees_equal_p(o_node, expected_node, ['id']) @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node(), mapped_mock_task_node(), True), (mapped_mock_headline_node({'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_headline_node({'title': 'On the other hand ...', 'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node({'name': 'On the one hand ...'}), True), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_filename_node({'id': 'On the other hand ...'}), mapped_mock_project_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node({'id': 'On the other hand ...', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'On the one hand ...'}), True), (mapped_mock_headline_node, mapped_mock_project_node(), False), (mapped_mock_headline_node({'title': 'A ""project"" headline'}), mapped_mock_project_node({'name': 'A ""project"" headline'}), False), (mapped_mock_filename_node(), mapped_mock_task_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_task_node({'name': 'My Inbox'}), False)]) def test_o2a_map_fn(o_node, a_node, expected): ""Can we map from Org nodes to Asana nodes?"" assert o2a.map_fn(o_node, a_node) == expected @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node(), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node(), mapped_mock_project_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), False), (mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@home'}}), mapped_mock_task_node({'tags': {'@work'}}), False)]) def test_o2a_eql_fn(o_node, a_node, expected): ""Can we compare Org nodes to Asana nodes?"" assert o2a.eql_fn(o_node, a_node) == expected ", https://github.com/tmurph/todo-sync,32915e9a1509aa2c4083613e256cb0d502c441aa,tests/test_org_to_asana.py::test_o2a_behind_make_fn[a_node7-expected_node7],tests/test_org_to_asana.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest import unittest.mock as mock import todo_sync.backends.asana as a import todo_sync.backends.org as o import todo_sync.mappers.org_to_asana as o2a from .helpers import assert_trees_equal_p from .test_org import mock_headline_node, mock_filename_node from .test_org import mock_source as mock_org_source from .test_asana import mock_task_node, mock_project_node from .test_asana import mock_source as mock_asana_source MOCK_NAME_TITLE = MOCK_TITLE_NAME = 'REQUIRED' MOCK_TASK_ID = 1 MOCK_PROJECT_ID = 2 def mapped_mock_headline_node(info_dict=None): d = {'title': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_headline_node(d) def mapped_mock_task_node(info_dict=None): d = {'id': MOCK_TASK_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_task_node(d) def mapped_mock_filename_node(info_dict=None): d = {} if info_dict: d.update(info_dict) return mock_filename_node(d) def mapped_mock_project_node(info_dict=None): d = {'id': MOCK_PROJECT_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_project_node(d) def test_o2a_behind_source_creation(mocker): ""Can we get an Org Source?"" mocker.patch('pexpect.spawn') mocker.patch('pexpect.replwrap.REPLWrapper') s = o2a.behind_source(None, dry_run=True) assert isinstance(s, o.Source) def test_o2a_ahead_source_creation(mocker): ""Can we get an Asana Source?"" mocker.patch('asana.Client') s = o2a.ahead_source(None) assert isinstance(s, a.Source) @pytest.fixture def ahead_source(): with mock.patch('asana.Client'): mocked_source = mock.Mock(a.Source, wraps=mock_asana_source()) with mock.patch.object(a.Source, 'from_client', return_value=mocked_source): yield o2a.ahead_source(None) @pytest.fixture def behind_source(): with mock.patch('pexpect.spawn'): with mock.patch('pexpect.replwrap.REPLWrapper'): mocked_source = mock.Mock(o.Source, wraps=mock_org_source()) with mock.patch.object(o.Source, 'from_emacs_repl', return_value=mocked_source): yield o2a.behind_source(None) def test_o2a_behind_get_tree(behind_source): ""Does the Org Source have a special get_tree method?"" behind_source.get_tree() behind_source.get_all_items.assert_called_with( ['asana_id', 'asana_project_id']) def test_o2a_ahead_get_tree(ahead_source): ""Does the Asana Source have a special get_tree method?"" ahead_source.get_tree() ahead_source.get_all_items.assert_called_with() @pytest.mark.parametrize(""a_node, expected_node"", [ (mapped_mock_task_node(), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)})), (mapped_mock_task_node({'notes': 'my stuff'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': 'my stuff'})), (mapped_mock_task_node({'completed': True}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'DONE'})), (mapped_mock_task_node({'completed': False}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'TODO'})), (mapped_mock_task_node({'due_on': '2016-11-19'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-19'})), (mapped_mock_task_node({'due_at': '2016-11-20T03:34:00.000Z'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-20T03:34:00.000Z'})), (mapped_mock_task_node({'project_id': 261881552216474}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'asana_project_id': '261881552216474'})), (mapped_mock_project_node({'name': 'My Inbox'}), mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)})), (mapped_mock_task_node({'tags': {'@work'}}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@work'}})), (mapped_mock_task_node({'tags': set()}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': set()})), (mapped_mock_task_node({'notes': ''}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': ''}))]) def test_o2a_behind_make_fn(a_node, expected_node, behind_source): ""Does the Org Source have a special make_fn method?"" o_node = behind_source.make_fn(a_node) assert_trees_equal_p(o_node, expected_node, ['id']) @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node(), mapped_mock_task_node(), True), (mapped_mock_headline_node({'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_headline_node({'title': 'On the other hand ...', 'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node({'name': 'On the one hand ...'}), True), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_filename_node({'id': 'On the other hand ...'}), mapped_mock_project_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node({'id': 'On the other hand ...', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'On the one hand ...'}), True), (mapped_mock_headline_node, mapped_mock_project_node(), False), (mapped_mock_headline_node({'title': 'A ""project"" headline'}), mapped_mock_project_node({'name': 'A ""project"" headline'}), False), (mapped_mock_filename_node(), mapped_mock_task_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_task_node({'name': 'My Inbox'}), False)]) def test_o2a_map_fn(o_node, a_node, expected): ""Can we map from Org nodes to Asana nodes?"" assert o2a.map_fn(o_node, a_node) == expected @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node(), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node(), mapped_mock_project_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), False), (mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@home'}}), mapped_mock_task_node({'tags': {'@work'}}), False)]) def test_o2a_eql_fn(o_node, a_node, expected): ""Can we compare Org nodes to Asana nodes?"" assert o2a.eql_fn(o_node, a_node) == expected ", https://github.com/tmurph/todo-sync,32915e9a1509aa2c4083613e256cb0d502c441aa,tests/test_org_to_asana.py::test_o2a_behind_make_fn[a_node8-expected_node8],tests/test_org_to_asana.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest import unittest.mock as mock import todo_sync.backends.asana as a import todo_sync.backends.org as o import todo_sync.mappers.org_to_asana as o2a from .helpers import assert_trees_equal_p from .test_org import mock_headline_node, mock_filename_node from .test_org import mock_source as mock_org_source from .test_asana import mock_task_node, mock_project_node from .test_asana import mock_source as mock_asana_source MOCK_NAME_TITLE = MOCK_TITLE_NAME = 'REQUIRED' MOCK_TASK_ID = 1 MOCK_PROJECT_ID = 2 def mapped_mock_headline_node(info_dict=None): d = {'title': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_headline_node(d) def mapped_mock_task_node(info_dict=None): d = {'id': MOCK_TASK_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_task_node(d) def mapped_mock_filename_node(info_dict=None): d = {} if info_dict: d.update(info_dict) return mock_filename_node(d) def mapped_mock_project_node(info_dict=None): d = {'id': MOCK_PROJECT_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_project_node(d) def test_o2a_behind_source_creation(mocker): ""Can we get an Org Source?"" mocker.patch('pexpect.spawn') mocker.patch('pexpect.replwrap.REPLWrapper') s = o2a.behind_source(None, dry_run=True) assert isinstance(s, o.Source) def test_o2a_ahead_source_creation(mocker): ""Can we get an Asana Source?"" mocker.patch('asana.Client') s = o2a.ahead_source(None) assert isinstance(s, a.Source) @pytest.fixture def ahead_source(): with mock.patch('asana.Client'): mocked_source = mock.Mock(a.Source, wraps=mock_asana_source()) with mock.patch.object(a.Source, 'from_client', return_value=mocked_source): yield o2a.ahead_source(None) @pytest.fixture def behind_source(): with mock.patch('pexpect.spawn'): with mock.patch('pexpect.replwrap.REPLWrapper'): mocked_source = mock.Mock(o.Source, wraps=mock_org_source()) with mock.patch.object(o.Source, 'from_emacs_repl', return_value=mocked_source): yield o2a.behind_source(None) def test_o2a_behind_get_tree(behind_source): ""Does the Org Source have a special get_tree method?"" behind_source.get_tree() behind_source.get_all_items.assert_called_with( ['asana_id', 'asana_project_id']) def test_o2a_ahead_get_tree(ahead_source): ""Does the Asana Source have a special get_tree method?"" ahead_source.get_tree() ahead_source.get_all_items.assert_called_with() @pytest.mark.parametrize(""a_node, expected_node"", [ (mapped_mock_task_node(), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)})), (mapped_mock_task_node({'notes': 'my stuff'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': 'my stuff'})), (mapped_mock_task_node({'completed': True}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'DONE'})), (mapped_mock_task_node({'completed': False}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'TODO'})), (mapped_mock_task_node({'due_on': '2016-11-19'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-19'})), (mapped_mock_task_node({'due_at': '2016-11-20T03:34:00.000Z'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-20T03:34:00.000Z'})), (mapped_mock_task_node({'project_id': 261881552216474}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'asana_project_id': '261881552216474'})), (mapped_mock_project_node({'name': 'My Inbox'}), mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)})), (mapped_mock_task_node({'tags': {'@work'}}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@work'}})), (mapped_mock_task_node({'tags': set()}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': set()})), (mapped_mock_task_node({'notes': ''}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': ''}))]) def test_o2a_behind_make_fn(a_node, expected_node, behind_source): ""Does the Org Source have a special make_fn method?"" o_node = behind_source.make_fn(a_node) assert_trees_equal_p(o_node, expected_node, ['id']) @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node(), mapped_mock_task_node(), True), (mapped_mock_headline_node({'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_headline_node({'title': 'On the other hand ...', 'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node({'name': 'On the one hand ...'}), True), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_filename_node({'id': 'On the other hand ...'}), mapped_mock_project_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node({'id': 'On the other hand ...', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'On the one hand ...'}), True), (mapped_mock_headline_node, mapped_mock_project_node(), False), (mapped_mock_headline_node({'title': 'A ""project"" headline'}), mapped_mock_project_node({'name': 'A ""project"" headline'}), False), (mapped_mock_filename_node(), mapped_mock_task_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_task_node({'name': 'My Inbox'}), False)]) def test_o2a_map_fn(o_node, a_node, expected): ""Can we map from Org nodes to Asana nodes?"" assert o2a.map_fn(o_node, a_node) == expected @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node(), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node(), mapped_mock_project_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), False), (mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@home'}}), mapped_mock_task_node({'tags': {'@work'}}), False)]) def test_o2a_eql_fn(o_node, a_node, expected): ""Can we compare Org nodes to Asana nodes?"" assert o2a.eql_fn(o_node, a_node) == expected ", https://github.com/tmurph/todo-sync,32915e9a1509aa2c4083613e256cb0d502c441aa,tests/test_org_to_asana.py::test_o2a_behind_make_fn[a_node9-expected_node9],tests/test_org_to_asana.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest import unittest.mock as mock import todo_sync.backends.asana as a import todo_sync.backends.org as o import todo_sync.mappers.org_to_asana as o2a from .helpers import assert_trees_equal_p from .test_org import mock_headline_node, mock_filename_node from .test_org import mock_source as mock_org_source from .test_asana import mock_task_node, mock_project_node from .test_asana import mock_source as mock_asana_source MOCK_NAME_TITLE = MOCK_TITLE_NAME = 'REQUIRED' MOCK_TASK_ID = 1 MOCK_PROJECT_ID = 2 def mapped_mock_headline_node(info_dict=None): d = {'title': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_headline_node(d) def mapped_mock_task_node(info_dict=None): d = {'id': MOCK_TASK_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_task_node(d) def mapped_mock_filename_node(info_dict=None): d = {} if info_dict: d.update(info_dict) return mock_filename_node(d) def mapped_mock_project_node(info_dict=None): d = {'id': MOCK_PROJECT_ID, 'name': MOCK_TITLE_NAME} if info_dict: d.update(info_dict) return mock_project_node(d) def test_o2a_behind_source_creation(mocker): ""Can we get an Org Source?"" mocker.patch('pexpect.spawn') mocker.patch('pexpect.replwrap.REPLWrapper') s = o2a.behind_source(None, dry_run=True) assert isinstance(s, o.Source) def test_o2a_ahead_source_creation(mocker): ""Can we get an Asana Source?"" mocker.patch('asana.Client') s = o2a.ahead_source(None) assert isinstance(s, a.Source) @pytest.fixture def ahead_source(): with mock.patch('asana.Client'): mocked_source = mock.Mock(a.Source, wraps=mock_asana_source()) with mock.patch.object(a.Source, 'from_client', return_value=mocked_source): yield o2a.ahead_source(None) @pytest.fixture def behind_source(): with mock.patch('pexpect.spawn'): with mock.patch('pexpect.replwrap.REPLWrapper'): mocked_source = mock.Mock(o.Source, wraps=mock_org_source()) with mock.patch.object(o.Source, 'from_emacs_repl', return_value=mocked_source): yield o2a.behind_source(None) def test_o2a_behind_get_tree(behind_source): ""Does the Org Source have a special get_tree method?"" behind_source.get_tree() behind_source.get_all_items.assert_called_with( ['asana_id', 'asana_project_id']) def test_o2a_ahead_get_tree(ahead_source): ""Does the Asana Source have a special get_tree method?"" ahead_source.get_tree() ahead_source.get_all_items.assert_called_with() @pytest.mark.parametrize(""a_node, expected_node"", [ (mapped_mock_task_node(), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)})), (mapped_mock_task_node({'notes': 'my stuff'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': 'my stuff'})), (mapped_mock_task_node({'completed': True}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'DONE'})), (mapped_mock_task_node({'completed': False}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'todo_type': 'TODO'})), (mapped_mock_task_node({'due_on': '2016-11-19'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-19'})), (mapped_mock_task_node({'due_at': '2016-11-20T03:34:00.000Z'}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'deadline': '2016-11-20T03:34:00.000Z'})), (mapped_mock_task_node({'project_id': 261881552216474}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'asana_project_id': '261881552216474'})), (mapped_mock_project_node({'name': 'My Inbox'}), mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)})), (mapped_mock_task_node({'tags': {'@work'}}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@work'}})), (mapped_mock_task_node({'tags': set()}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': set()})), (mapped_mock_task_node({'notes': ''}), mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'paragraph': ''}))]) def test_o2a_behind_make_fn(a_node, expected_node, behind_source): ""Does the Org Source have a special make_fn method?"" o_node = behind_source.make_fn(a_node) assert_trees_equal_p(o_node, expected_node, ['id']) @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node(), mapped_mock_task_node(), True), (mapped_mock_headline_node({'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_headline_node({'title': 'On the other hand ...', 'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node({'name': 'On the one hand ...'}), True), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_filename_node({'id': 'On the other hand ...'}), mapped_mock_project_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node({'id': 'On the other hand ...', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'On the one hand ...'}), True), (mapped_mock_headline_node, mapped_mock_project_node(), False), (mapped_mock_headline_node({'title': 'A ""project"" headline'}), mapped_mock_project_node({'name': 'A ""project"" headline'}), False), (mapped_mock_filename_node(), mapped_mock_task_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_task_node({'name': 'My Inbox'}), False)]) def test_o2a_map_fn(o_node, a_node, expected): ""Can we map from Org nodes to Asana nodes?"" assert o2a.map_fn(o_node, a_node) == expected @pytest.mark.parametrize(""o_node, a_node, expected"", [ (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID)}), mapped_mock_task_node(), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'title': 'On the other hand ...'}), mapped_mock_task_node({'name': 'On the one hand ...'}), False), (mapped_mock_filename_node(), mapped_mock_project_node(), False), (mapped_mock_filename_node({'id': 'My Inbox'}), mapped_mock_project_node({'name': 'My Inbox'}), False), (mapped_mock_filename_node({'id': 'My Inbox', 'asana_project_id': str(MOCK_PROJECT_ID)}), mapped_mock_project_node({'name': 'My Inbox'}), True), (mapped_mock_headline_node({'asana_id': str(MOCK_TASK_ID), 'tags': {'@home'}}), mapped_mock_task_node({'tags': {'@work'}}), False)]) def test_o2a_eql_fn(o_node, a_node, expected): ""Can we compare Org nodes to Asana nodes?"" assert o2a.eql_fn(o_node, a_node) == expected ", https://github.com/tomerten/findatabroker,204c433d51e2d2de5c132a4607cd504809fd4a71,FinDataBroker/tests/test_mongo.py::test__load___ok,FinDataBroker/tests/test_mongo.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import pytest from pytest import raises import mongomock from pymongo import ASCENDING from FinDataBroker.DataBrokerMongoDb import DataBrokerMongoDb client = mongomock.MongoClient() broker = DataBrokerMongoDb(client) def test___databroker_creation___ok(): assert isinstance(broker, DataBrokerMongoDb) # not implemented in mongomock # def test___get_stat_non_existing_db___nog(): # with raises(AttributeError): # broker.get_stats('test') def test__save___ok(): db = 'testdb' col = 'testcol' objs = [ { 'name': 'boe', 'year': 2019 } ] index = [('name', ASCENDING), ('year', ASCENDING)] broker.save(objs, db, col, index, unique=True) assert broker.get_number_of_documents(db, col) == 1 def test__load___ok(): db = 'testdb' col = 'testcol' objs = [ { 'name': 'boe', 'year': 2019 } ] index = [('name', ASCENDING), ('year', ASCENDING)] broker.save(objs, db, col, index, unique=True) assert broker.get_number_of_documents(db, col) == 1 data = broker.load(db, col, {'year': 2019}) assert objs == data ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_classify.py::TestFindRain::test_1,tests/unit/test_classify.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,""""""" This module contains unit tests for classify-module. """""" import numpy as np import numpy.ma as ma from numpy.testing import assert_array_equal from cloudnetpy.categorize import classify class Obs: def __init__(self): self.beta = ma.array([[1, 1], [1, 1], [1, 1], [1, 1]], mask=[[0, 0], [0, 0], [1, 1], [1, 1]]) def test_find_aerosols(): obs = Obs() is_falling = np.array([[1, 0], [1, 0], [1, 0], [1, 0]]) is_liquid = np.array([[1, 0], [0, 1], [1, 0], [0, 1]]) result = np.array([[0, 1], [0, 0], [0, 0], [0, 0]]) assert_array_equal(classify._find_aerosols(obs, is_falling, is_liquid), result) def test_bits_to_integer(): b0 = [[1, 0, 0, 0, 1, 1, 1, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] b1 = [[0, 1, 0, 0, 1, 1, 1, 0, 1, 0], [1, 0, 1, 0, 0, 0, 0, 0, 0, 0]] b2 = [[0, 0, 1, 0, 0, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0, 0, 0]] b3 = [[0, 0, 0, 1, 0, 0, 1, 0, 1, 1], [0, 1, 1, 0, 0, 0, 0, 0, 0, 0]] bits = [b0, b1, b2, b3] re = [[1, 2, 4, 8, 3, 7, 15, 5, 10, 9], [6, 12, 14, 0, 0, 0, 0, 0, 0, 0]] assert_array_equal(classify._bits_to_integer(bits), re) class TestFindRain: time = np.linspace(0, 24, 2880) # 30 s resolution z = np.zeros((len(time), 10)) def test_1(self): result = np.zeros(len(self.time)) assert_array_equal(classify._find_rain(self.z, self.time), result) def test_2(self): self.z[:, 3] = 0.1 result = np.ones(len(self.time)) assert_array_equal(classify._find_rain(self.z, self.time), result) def test_3(self): self.z[5, 3] = 0.1 result = np.ones(len(self.time)) result[3:7] = 1 assert_array_equal(classify._find_rain(self.z, self.time, time_buffer=1), result) def test_4(self): self.z[1440, 3] = 0.1 result = np.ones(len(self.time)) assert_array_equal(classify._find_rain(self.z, self.time, time_buffer=1500), result) def test_find_clutter(): is_rain = np.array([0, 0, 0, 1, 1], dtype=bool) v = np.ones((5, 12)) * 0.1 v = ma.array(v) v[:, 5] = 0.04 result = np.zeros(v.shape) result[:3, 5] = 1 assert_array_equal(classify._find_clutter(v, is_rain), result) def test_find_drizzle_and_falling(): is_liquid = np.array([[0, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0]]) is_falling = np.array([[0, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1]]) is_freezing = np.array([[0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 1, 1]]) result = ma.array([[0, 2, 0, 0, 1, 0], [0, 0, 0, 2, 1, 1]], mask=[[1, 0, 1, 1, 0, 1], [1, 1, 1, 0, 0, 0]]) assert_array_equal(classify._find_drizzle_and_falling(is_liquid, is_falling, is_freezing), result) def test_find_profiles_with_undetected_melting(): is_liquid = np.array([[0, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]) is_falling = np.array([[0, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]) is_freezing = np.array([[0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 1, 1], [0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0]]) is_melting = np.array([[0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]) result = np.array([0, 1, 1, 0]) bits = [is_liquid, is_falling, is_freezing, is_melting] undetected = classify._find_profiles_with_undetected_melting(bits) assert_array_equal(undetected.data, result) ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_classify.py::TestFindRain::test_3,tests/unit/test_classify.py,OD-Brit,flaky,RepoArchived,nan,classify;root_cause,""""""" This module contains unit tests for classify-module. """""" import numpy as np import numpy.ma as ma from numpy.testing import assert_array_equal from cloudnetpy.categorize import classify class Obs: def __init__(self): self.beta = ma.array([[1, 1], [1, 1], [1, 1], [1, 1]], mask=[[0, 0], [0, 0], [1, 1], [1, 1]]) def test_find_aerosols(): obs = Obs() is_falling = np.array([[1, 0], [1, 0], [1, 0], [1, 0]]) is_liquid = np.array([[1, 0], [0, 1], [1, 0], [0, 1]]) result = np.array([[0, 1], [0, 0], [0, 0], [0, 0]]) assert_array_equal(classify._find_aerosols(obs, is_falling, is_liquid), result) def test_bits_to_integer(): b0 = [[1, 0, 0, 0, 1, 1, 1, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] b1 = [[0, 1, 0, 0, 1, 1, 1, 0, 1, 0], [1, 0, 1, 0, 0, 0, 0, 0, 0, 0]] b2 = [[0, 0, 1, 0, 0, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0, 0, 0]] b3 = [[0, 0, 0, 1, 0, 0, 1, 0, 1, 1], [0, 1, 1, 0, 0, 0, 0, 0, 0, 0]] bits = [b0, b1, b2, b3] re = [[1, 2, 4, 8, 3, 7, 15, 5, 10, 9], [6, 12, 14, 0, 0, 0, 0, 0, 0, 0]] assert_array_equal(classify._bits_to_integer(bits), re) class TestFindRain: time = np.linspace(0, 24, 2880) # 30 s resolution z = np.zeros((len(time), 10)) def test_1(self): result = np.zeros(len(self.time)) assert_array_equal(classify._find_rain(self.z, self.time), result) def test_2(self): self.z[:, 3] = 0.1 result = np.ones(len(self.time)) assert_array_equal(classify._find_rain(self.z, self.time), result) def test_3(self): self.z[5, 3] = 0.1 result = np.ones(len(self.time)) result[3:7] = 1 assert_array_equal(classify._find_rain(self.z, self.time, time_buffer=1), result) def test_4(self): self.z[1440, 3] = 0.1 result = np.ones(len(self.time)) assert_array_equal(classify._find_rain(self.z, self.time, time_buffer=1500), result) def test_find_clutter(): is_rain = np.array([0, 0, 0, 1, 1], dtype=bool) v = np.ones((5, 12)) * 0.1 v = ma.array(v) v[:, 5] = 0.04 result = np.zeros(v.shape) result[:3, 5] = 1 assert_array_equal(classify._find_clutter(v, is_rain), result) def test_find_drizzle_and_falling(): is_liquid = np.array([[0, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0]]) is_falling = np.array([[0, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1]]) is_freezing = np.array([[0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 1, 1]]) result = ma.array([[0, 2, 0, 0, 1, 0], [0, 0, 0, 2, 1, 1]], mask=[[1, 0, 1, 1, 0, 1], [1, 1, 1, 0, 0, 0]]) assert_array_equal(classify._find_drizzle_and_falling(is_liquid, is_falling, is_freezing), result) def test_find_profiles_with_undetected_melting(): is_liquid = np.array([[0, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]) is_falling = np.array([[0, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]) is_freezing = np.array([[0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 1, 1], [0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0]]) is_melting = np.array([[0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]) result = np.array([0, 1, 1, 0]) bits = [is_liquid, is_falling, is_freezing, is_melting] undetected = classify._find_profiles_with_undetected_melting(bits) assert_array_equal(undetected.data, result) ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[beta_corr],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[Do],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[Do_bias],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[Do_error],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[drizzle_lwc],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[drizzle_lwc_bias],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[drizzle_lwc_error],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[drizzle_lwf],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[drizzle_lwf_bias],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[drizzle_lwf_error],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[drizzle_N],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[drizzle_N_bias],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[drizzle_N_error],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[mu],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[mu_error],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[S],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[S_error],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[v_air],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[v_drizzle],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[v_drizzle_bias],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_append_data[v_drizzle_error],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_calc_beta_z_ratio,tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_calc_density,tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_calc_derived_products[drizzle_lwc],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_calc_derived_products[drizzle_lwf],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_calc_derived_products[drizzle_N],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_calc_derived_products[v_air],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_calc_derived_products[v_drizzle],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_calc_dia,tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_calc_fall_velocity,tests/unit/test_drizzle.py,OD-Brit,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_calc_lwc,tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_calc_lwf,tests/unit/test_drizzle.py,OD-Brit,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_calc_v_air,tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_find_indices,tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_find_lut_indices,tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_init_variables[beta_corr],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_init_variables[Do],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_init_variables[mu],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_init_variables[S],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_is_converged,tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_screen_rain,tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_update_result_tables[Do-10],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_update_result_tables[mu--1],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_drizzle.py::test_update_result_tables[S-93.7247943],tests/unit/test_drizzle.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from numpy import testing import pytest from pathlib import Path import cloudnetpy.products.drizzle as drizzle from cloudnetpy.products.drizzle import * from cloudnetpy.products.drizzle_error import get_drizzle_error DIMENSIONS_X = ('time', 'model_time') TEST_ARRAY_X = np.arange(2) DIMENSIONS_Y = ('height', 'model_height') TEST_ARRAY_Y = np.arange(3) @pytest.fixture(scope='session') def drizzle_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('beta', 'f8', ('time', 'height')) var[:] = [[0.1, 0.1, 0.1], [1, 0.2, 3]] var = root_grp.createVariable('beta_error', 'f8') var[:] = 0.1 var = root_grp.createVariable('beta_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('v', 'f8', ('time', 'height')) var[:] = [[1, 2, 3], [1, 2, 3]] var = root_grp.createVariable('Z', 'f8', ('time', 'height')) var[:] = [[1, 0.1, 0.2], [0.3, 2, 0.1]] var = root_grp.createVariable('Z_error', 'f8', ('time', 'height')) var[:] = [[.01, 0.1, 0.2], [0.3, 0.2, 0.1]] var = root_grp.createVariable('Z_bias', 'f8') var[:] = 0.1 var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('radar_frequency', 'f8') var[:] = 35.5 # TODO: How to check with multiple options root_grp.close() return file_name def _create_dimensions(root_grp, test_array, dimension): n_dim = len(test_array) for dim_name in dimension: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp, test_array, dimension): for dim_name in dimension: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = test_array if dim_name == 'height': x.units = 'm' def test_convert_z_units(drizzle_source_file): from cloudnetpy.utils import db2lin obj = DrizzleSource(drizzle_source_file) z = obj.getvar('Z') - 180 compare = db2lin(z) testing.assert_array_almost_equal(obj._convert_z_units(), compare) @pytest.mark.parametrize('key', [ 'Do', 'mu', 'S', 'lwf', 'termv', 'width', 'ray', 'v']) def test_read_mie_lut(drizzle_source_file, key): obj = DrizzleSource(drizzle_source_file) assert key in obj.mie.keys() def test_get_mie_file(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) obj.module_path = ''.join((str(Path(__file__).parents[2]), '/cloudnetpy/products')) obj._get_mie_file() compare = '/'.join((obj.module_path, 'mie_lu_tables.nc')) testing.assert_equal(obj._get_mie_file(), compare) def test_get_wl_band(drizzle_source_file): obj = DrizzleSource(drizzle_source_file) compare = '35' testing.assert_equal(obj._get_wl_band(), compare) @pytest.fixture(scope='session') def drizzle_cat_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimension_variables(root_grp, TEST_ARRAY_X, DIMENSIONS_X) _create_dimensions(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) _create_dimension_variables(root_grp, TEST_ARRAY_Y, DIMENSIONS_Y) var = root_grp.createVariable('dheight', 'f8') var[:] = 10 var = root_grp.createVariable('uwind', 'f8', ('model_time', 'model_height')) var[:] = [[2, 2, 1], [1, 3, 5]] var = root_grp.createVariable('vwind', 'f8', ('model_time', 'model_height')) var[:] = [[-2, -2, 1], [1, -3, 0]] var = root_grp.createVariable('category_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('quality_bits', 'i4', ('time', 'height')) var[:] = [[0, 1, 2], [4, 8, 16]] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('is_undetected_melting', 'i4', 'time') var[:] = [0, 1] var = root_grp.createVariable('v_sigma', 'f8', ('time', 'height')) var[:] = [[-2, np.nan, 2], [1, -1, 0]] var = root_grp.createVariable('width', 'f8', ('time', 'height')) var[:] = [[2, 0, 1], [1, 3, 0]] root_grp.close() return file_name def test_find_v_sigma(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([[1, 0, 1], [1, 1, 1]], dtype=bool) testing.assert_array_almost_equal(obj._find_v_sigma(drizzle_cat_file), compare) def test_find_warm_liquid(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) obj.category_bits['droplet'] = np.array([0, 0, 0, 1, 1, 1, 0], dtype=bool) obj.category_bits['cold'] = np.array([1, 1, 0, 0, 1, 0, 1], dtype=bool) compare = np.array([0, 0, 0, 1, 0, 1, 0], dtype=bool) testing.assert_array_almost_equal(obj._find_warm_liquid(), compare) @pytest.mark.parametrize(""is_rain, falling, droplet, cold, melting, insect, "" ""radar, lidar, clutter, molecular, attenuated, v_sigma"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([1, 1, 0, 1]))]) def test_find_drizzle(drizzle_cat_file, is_rain, falling, droplet, cold, melting, insect, radar, lidar, clutter, molecular, attenuated, v_sigma): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.category_bits['falling'] = falling obj.category_bits['droplet'] = droplet obj.category_bits['cold'] = cold obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['lidar'] = lidar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular obj.quality_bits['attenuated'] = attenuated obj.is_v_sigma = v_sigma compare = np.array([[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 0, 0]]) testing.assert_array_almost_equal(obj._find_drizzle(), compare) @pytest.mark.parametrize(""is_rain, warm, falling, melting, insect, "" ""radar, clutter, molecular"", [ (np.array([0, 0, 0, 0]), np.array([1, 1, 1, 1]), np.array([1, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]), np.array([0, 1, 1, 0]), np.array([0, 0, 0, 1]), np.array([0, 0, 0, 1]))]) def test_find_would_be_drizzle(drizzle_cat_file, is_rain, warm, falling, melting, insect, radar, clutter, molecular): obj = DrizzleClassification(drizzle_cat_file) obj.is_rain = is_rain obj.warm_liquid = warm obj.category_bits['falling'] = falling obj.category_bits['melting'] = melting obj.category_bits['insect'] = insect obj.quality_bits['radar'] = radar obj.quality_bits['clutter'] = clutter obj.quality_bits['molecular'] = molecular compare = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 1, 1, 0]]) testing.assert_array_almost_equal(obj._find_would_be_drizzle(), compare) def test_find_cold_rain(drizzle_cat_file): obj = DrizzleClassification(drizzle_cat_file) compare = np.array([0, 1]) testing.assert_array_almost_equal(obj._find_cold_rain(), compare) def test_calculate_spectral_width(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) width = netCDF4.Dataset(drizzle_cat_file).variables['width'][:] v_sigma = netCDF4.Dataset(drizzle_cat_file).variables['v_sigma'][:] factor = obj._calc_v_sigma_factor() compare = width - factor * v_sigma testing.assert_almost_equal(obj._calculate_spectral_width(), compare) def test_calc_beam_divergence(drizzle_cat_file): obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] compare = height * np.deg2rad(0.5) testing.assert_almost_equal(obj._calc_beam_divergence(), compare) def test_calc_v_sigma_factor(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) height = netCDF4.Dataset(drizzle_cat_file).variables['height'][:] uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] beam = height * np.deg2rad(0.5) wind = l2norm(uwind, vwind) a_wind = (wind + beam) ** (2 / 3) s_wind = (30 * wind + beam) ** (2 / 3) compare = a_wind / (s_wind - a_wind) testing.assert_array_almost_equal(obj._calc_v_sigma_factor(), compare) def test_calc_horizontal_wind(drizzle_cat_file): from cloudnetpy.utils import l2norm obj = CorrectSpectralWidth(drizzle_cat_file) uwind = netCDF4.Dataset(drizzle_cat_file).variables['uwind'][:] vwind = netCDF4.Dataset(drizzle_cat_file).variables['vwind'][:] compare = l2norm(uwind, vwind) testing.assert_array_almost_equal(obj._calc_horizontal_wind(), ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_lwc.py::test_init_status[0],tests/unit/test_lwc.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import numpy as np from numpy.testing import assert_array_equal, assert_array_almost_equal from collections import namedtuple import pytest import netCDF4 from cloudnetpy.products.lwc import LwcSource, Lwc, LwcStatus, LwcError from cloudnetpy.categorize import atmos DIMENSIONS = ('time', 'height', 'model_time', 'model_height') TEST_ARRAY = np.arange(3) CategorizeBits = namedtuple('CategorizeBits', ['category_bits', 'quality_bits']) @pytest.fixture(scope='session') def lwc_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp) _create_dimension_variables(root_grp) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('lwp', 'f8', 'time') var[:] = [1, 1, 0.5] var = root_grp.createVariable('lwp_error', 'f8', 'time') var[:] = [0.2, 0.2, 0.1] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1, 1] var = root_grp.createVariable('category_bits', 'i4', 'time') var[:] = [0, 1, 2] var = root_grp.createVariable('quality_bits', 'i4', 'time') var[:] = [8, 16, 32] var = root_grp.createVariable('temperature', 'f8', ('time', 'height')) var[:] = np.array([[282, 280, 278], [286, 284, 282], [284, 282, 280]]) var = root_grp.createVariable('pressure', 'f8', ('time', 'height')) var[:] = np.array([[1010, 1000, 990], [1020, 1010, 1000], [1030, 1020, 1010]]) root_grp.close() return file_name def _create_dimensions(root_grp): n_dim = len(TEST_ARRAY) for dim_name in DIMENSIONS: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp): for dim_name in DIMENSIONS: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = TEST_ARRAY if dim_name == 'height': x.units = 'm' def test_get_atmosphere_t(lwc_source_file): obj = LwcSource(lwc_source_file) compare = np.array([[282, 280, 278], [286, 284, 282], [284, 282, 280]]) assert_array_equal(compare, obj.atmosphere[0]) def test_get_atmosphere_p(lwc_source_file): obj = LwcSource(lwc_source_file) compare = np.array([[1010, 1000, 990], [1020, 1010, 1000], [1030, 1020, 1010]]) assert_array_equal(compare, obj.atmosphere[-1]) class LwcSourceObj: def __init__(self): self.dheight = 10 self.categorize_bits = \ CategorizeBits(category_bits={'droplet': np.asarray([[1, 0, 1], [0, 1, 1]], dtype=bool)}, quality_bits={'radar': np.asarray([[1, 0, 1], [0, 1, 1]], dtype=bool), 'lidar': np.asarray([[1, 0, 1], [0, 1, 1]], dtype=bool)}) self.atmosphere = (np.array([[282, 281, 280], [280, 279, 278]]), np.array([[101000, 100500, 100000], [100000, 99500, 99000]])) self.lwp = np.array([2, 0]) self.lwp_error = np.array([0.1, 0.2]) self.is_rain = np.array([0, 1]) LWC_OBJ = Lwc(LwcSourceObj()) STATUS_OBJ = LwcStatus(LwcSourceObj(), LWC_OBJ) ERROR_OBJ = LwcError(LwcSourceObj(), LWC_OBJ) @pytest.mark.parametrize(""value"", [0, 1]) def test_get_liquid(value): assert value in LWC_OBJ.is_liquid def test_init_lwc_adiabatic(lwc_source_file): lwc_source = LwcSourceObj() compare = atmos.fill_clouds_with_lwc_dz(lwc_source.atmosphere, LWC_OBJ.is_liquid) compare[0, 0] *= 10 compare[0, 2] *= 10 compare[1, 1] *= 10 compare[1, 2] *= 20 assert_array_almost_equal(LWC_OBJ._init_lwc_adiabatic(), compare) def test_screen_rain_lwc(): compare = np.ma.array([[5, 1, 2], [3, 6, 0]], mask=[[0, 0, 0], [1, 1, 1]]) assert_array_equal(compare.mask, LWC_OBJ.lwc.mask) @pytest.mark.parametrize(""value"", [0, 1]) def test_init_status(value): assert value in STATUS_OBJ._init_status() @pytest.mark.parametrize(""key"", ['radar', 'lidar']) def test_get_echo(key): assert key in STATUS_OBJ.echo.keys() @pytest.mark.parametrize(""value"", [0, 1, 2]) def test_update_status(value): time = np.array([0]) STATUS_OBJ._update_status(time) assert value in STATUS_OBJ.status @pytest.mark.parametrize(""value"", [0, 1, 2, 3]) def test_adjust_lwc(value): time = 0 base = 0 STATUS_OBJ.status = np.array([[1, 0, 2], [0, 0, 2]]) STATUS_OBJ._adjust_lwc(time, base) assert value in STATUS_OBJ.status def test_has_converged(): ind = 1 assert STATUS_OBJ._has_converged(ind) is True def test_out_of_bound(): ind = 2 assert STATUS_OBJ._out_of_bound(ind) is True def test_find_adjustable_clouds(): assert 1 not in STATUS_OBJ._find_adjustable_clouds() def test_find_topmost_clouds(): compare = np.asarray([[0, 0, 1], [0, 1, 1]], dtype=bool) assert_array_equal(STATUS_OBJ._find_topmost_clouds(), compare) def test_find_echo_combinations_in_liquid(): STATUS_OBJ.echo['lidar'] = np.array([[0, 1, 0], [1, 1, 0]]) STATUS_OBJ.echo['radar'] = np.array([[0, 0, 0], [0, 1, 1]]) STATUS_OBJ.is_liquid = np.array([[1, 1, 1], [0, 1, 1]]) compare = np.array([[0, 1, 0], [0, 3, 2]]) assert_array_equal(STATUS_OBJ._find_echo_combinations_in_liquid(), compare) def test_find_lidar_only_clouds(): inds = np.array([[1, 0, 0], [0, 1, 3]]) compare = np.array([True, False]) assert_array_equal(STATUS_OBJ._find_lidar_only_clouds(inds), compare) def test_remove_good_profiles(): top_c = np.asarray([[1, 1, 0], [1, 0, 1]], dtype=bool) compare = np.asarray([[1, 1, 0], [0, 0, 0]], dtype=bool) assert_array_equal(STATUS_OBJ._remove_good_profiles(top_c), compare) def test_find_lwp_difference(): STATUS_OBJ.lwc_adiabatic = np.array([[1, 8, 2], [2, 3, 7]]) STATUS_OBJ.lwc_source.lwp = np.array([50, 30]) compare = np.array([60, 90]) assert_array_equal(STATUS_OBJ._find_lwp_difference(), compare) @pytest.mark.parametrize(""value"", [0, 1, 2, 3, 4]) def test_screen_rain_status(value): STATUS_OBJ.lwc_source.is_rain = np.array([0, 1]) STATUS_OBJ.status = np.array([[0, 2, 2, 3, 1], [1, 3, 0, 2, 2]]) STATUS_OBJ.screen_rain() assert value in STATUS_OBJ.status def test_limit_error(): error = np.array([[0, 0, 1], [0.2, 0.4, 0.3]]) max_v = 0.5 compare = np.array([[0, 0, 0.5], [0.2, 0.4, 0.3]]) assert_array_equal(ERROR_OBJ._limit_error(error, max_v), compare) def test_calc_lwc_gradient(): from cloudnetpy.utils import l2norm ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.3], [0.1, 0.3, 0.6]]) compare = l2norm(*np.gradient(ERROR_OBJ.lwc)) assert_array_almost_equal(ERROR_OBJ._calc_lwc_gradient(), compare) def test_calc_lwc_relative_error(): from cloudnetpy.utils import l2norm ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.3], [0.1, 0.3, 0.6]]) x = l2norm(*np.gradient(ERROR_OBJ.lwc)) compare = x / ERROR_OBJ.lwc / 2 compare[compare > 5] = 5 assert_array_almost_equal(ERROR_OBJ._calc_lwc_relative_error(), compare) def test_calc_lwp_relative_error(): ERROR_OBJ.lwc_source.lwp = np.array([0.1, 0.5]) ERROR_OBJ.lwc_source.lwp_error = np.array([0.2, 5.5]) compare = ERROR_OBJ.lwc_source.lwp_error / ERROR_OBJ.lwc_source.lwp compare[compare > 10] = 10 assert_array_equal(ERROR_OBJ._calc_lwp_relative_error(), compare) def test_calc_combined_error(): from cloudnetpy.utils import transpose, l2norm err_2d = np.array([[0, 0.1, 0.1], [0.2, 0.4, 0.15]]) err_1d = np.array([0.3, 0.2]) compare = l2norm(err_2d, transpose(err_1d)) assert_array_equal(ERROR_OBJ._calc_combined_error(err_2d, err_1d), compare) def test_fill_error_array(): error_in = np.array([[0, 0.1, 0.1], [0.2, 0.4, 0.15]]) ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.1], [0.1, 0.2, 0.2]], mask=[[0, 1, 0], [1, 0, 0]]) compare = np.ma.array([[0, 0, 0], [0, 0, 0]], mask=[[0, 1, 0], [1, 0, 0]]) ERROR_OBJ._fill_error_array(error_in) error = ERROR_OBJ._fill_error_array(error_in) assert_array_almost_equal(error.mask, compare.mask) def test_screen_rain_error(): compare = np.ma.array([[0.709, 0, 0.709], [0, 0, 0]], mask=[[0, 1, 0], [1, 1, 1]]) assert_array_equal(ERROR_OBJ.lwc_error.mask, compare.mask) @pytest.mark.parametrize(""key"", [""lwc"", ""lwc_retrieval_status"", ""lwc_error""]) def test_append_data(lwc_source_file, key): from cloudnetpy.products.lwc import _append_data lwc_source = LwcSource(lwc_source_file) _append_data(lwc_source, LWC_OBJ, STATUS_OBJ, ERROR_OBJ) assert key in lwc_source.data.keys() ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_lwc.py::test_remove_good_profiles,tests/unit/test_lwc.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import numpy as np from numpy.testing import assert_array_equal, assert_array_almost_equal from collections import namedtuple import pytest import netCDF4 from cloudnetpy.products.lwc import LwcSource, Lwc, LwcStatus, LwcError from cloudnetpy.categorize import atmos DIMENSIONS = ('time', 'height', 'model_time', 'model_height') TEST_ARRAY = np.arange(3) CategorizeBits = namedtuple('CategorizeBits', ['category_bits', 'quality_bits']) @pytest.fixture(scope='session') def lwc_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp) _create_dimension_variables(root_grp) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('lwp', 'f8', 'time') var[:] = [1, 1, 0.5] var = root_grp.createVariable('lwp_error', 'f8', 'time') var[:] = [0.2, 0.2, 0.1] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1, 1] var = root_grp.createVariable('category_bits', 'i4', 'time') var[:] = [0, 1, 2] var = root_grp.createVariable('quality_bits', 'i4', 'time') var[:] = [8, 16, 32] var = root_grp.createVariable('temperature', 'f8', ('time', 'height')) var[:] = np.array([[282, 280, 278], [286, 284, 282], [284, 282, 280]]) var = root_grp.createVariable('pressure', 'f8', ('time', 'height')) var[:] = np.array([[1010, 1000, 990], [1020, 1010, 1000], [1030, 1020, 1010]]) root_grp.close() return file_name def _create_dimensions(root_grp): n_dim = len(TEST_ARRAY) for dim_name in DIMENSIONS: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp): for dim_name in DIMENSIONS: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = TEST_ARRAY if dim_name == 'height': x.units = 'm' def test_get_atmosphere_t(lwc_source_file): obj = LwcSource(lwc_source_file) compare = np.array([[282, 280, 278], [286, 284, 282], [284, 282, 280]]) assert_array_equal(compare, obj.atmosphere[0]) def test_get_atmosphere_p(lwc_source_file): obj = LwcSource(lwc_source_file) compare = np.array([[1010, 1000, 990], [1020, 1010, 1000], [1030, 1020, 1010]]) assert_array_equal(compare, obj.atmosphere[-1]) class LwcSourceObj: def __init__(self): self.dheight = 10 self.categorize_bits = \ CategorizeBits(category_bits={'droplet': np.asarray([[1, 0, 1], [0, 1, 1]], dtype=bool)}, quality_bits={'radar': np.asarray([[1, 0, 1], [0, 1, 1]], dtype=bool), 'lidar': np.asarray([[1, 0, 1], [0, 1, 1]], dtype=bool)}) self.atmosphere = (np.array([[282, 281, 280], [280, 279, 278]]), np.array([[101000, 100500, 100000], [100000, 99500, 99000]])) self.lwp = np.array([2, 0]) self.lwp_error = np.array([0.1, 0.2]) self.is_rain = np.array([0, 1]) LWC_OBJ = Lwc(LwcSourceObj()) STATUS_OBJ = LwcStatus(LwcSourceObj(), LWC_OBJ) ERROR_OBJ = LwcError(LwcSourceObj(), LWC_OBJ) @pytest.mark.parametrize(""value"", [0, 1]) def test_get_liquid(value): assert value in LWC_OBJ.is_liquid def test_init_lwc_adiabatic(lwc_source_file): lwc_source = LwcSourceObj() compare = atmos.fill_clouds_with_lwc_dz(lwc_source.atmosphere, LWC_OBJ.is_liquid) compare[0, 0] *= 10 compare[0, 2] *= 10 compare[1, 1] *= 10 compare[1, 2] *= 20 assert_array_almost_equal(LWC_OBJ._init_lwc_adiabatic(), compare) def test_screen_rain_lwc(): compare = np.ma.array([[5, 1, 2], [3, 6, 0]], mask=[[0, 0, 0], [1, 1, 1]]) assert_array_equal(compare.mask, LWC_OBJ.lwc.mask) @pytest.mark.parametrize(""value"", [0, 1]) def test_init_status(value): assert value in STATUS_OBJ._init_status() @pytest.mark.parametrize(""key"", ['radar', 'lidar']) def test_get_echo(key): assert key in STATUS_OBJ.echo.keys() @pytest.mark.parametrize(""value"", [0, 1, 2]) def test_update_status(value): time = np.array([0]) STATUS_OBJ._update_status(time) assert value in STATUS_OBJ.status @pytest.mark.parametrize(""value"", [0, 1, 2, 3]) def test_adjust_lwc(value): time = 0 base = 0 STATUS_OBJ.status = np.array([[1, 0, 2], [0, 0, 2]]) STATUS_OBJ._adjust_lwc(time, base) assert value in STATUS_OBJ.status def test_has_converged(): ind = 1 assert STATUS_OBJ._has_converged(ind) is True def test_out_of_bound(): ind = 2 assert STATUS_OBJ._out_of_bound(ind) is True def test_find_adjustable_clouds(): assert 1 not in STATUS_OBJ._find_adjustable_clouds() def test_find_topmost_clouds(): compare = np.asarray([[0, 0, 1], [0, 1, 1]], dtype=bool) assert_array_equal(STATUS_OBJ._find_topmost_clouds(), compare) def test_find_echo_combinations_in_liquid(): STATUS_OBJ.echo['lidar'] = np.array([[0, 1, 0], [1, 1, 0]]) STATUS_OBJ.echo['radar'] = np.array([[0, 0, 0], [0, 1, 1]]) STATUS_OBJ.is_liquid = np.array([[1, 1, 1], [0, 1, 1]]) compare = np.array([[0, 1, 0], [0, 3, 2]]) assert_array_equal(STATUS_OBJ._find_echo_combinations_in_liquid(), compare) def test_find_lidar_only_clouds(): inds = np.array([[1, 0, 0], [0, 1, 3]]) compare = np.array([True, False]) assert_array_equal(STATUS_OBJ._find_lidar_only_clouds(inds), compare) def test_remove_good_profiles(): top_c = np.asarray([[1, 1, 0], [1, 0, 1]], dtype=bool) compare = np.asarray([[1, 1, 0], [0, 0, 0]], dtype=bool) assert_array_equal(STATUS_OBJ._remove_good_profiles(top_c), compare) def test_find_lwp_difference(): STATUS_OBJ.lwc_adiabatic = np.array([[1, 8, 2], [2, 3, 7]]) STATUS_OBJ.lwc_source.lwp = np.array([50, 30]) compare = np.array([60, 90]) assert_array_equal(STATUS_OBJ._find_lwp_difference(), compare) @pytest.mark.parametrize(""value"", [0, 1, 2, 3, 4]) def test_screen_rain_status(value): STATUS_OBJ.lwc_source.is_rain = np.array([0, 1]) STATUS_OBJ.status = np.array([[0, 2, 2, 3, 1], [1, 3, 0, 2, 2]]) STATUS_OBJ.screen_rain() assert value in STATUS_OBJ.status def test_limit_error(): error = np.array([[0, 0, 1], [0.2, 0.4, 0.3]]) max_v = 0.5 compare = np.array([[0, 0, 0.5], [0.2, 0.4, 0.3]]) assert_array_equal(ERROR_OBJ._limit_error(error, max_v), compare) def test_calc_lwc_gradient(): from cloudnetpy.utils import l2norm ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.3], [0.1, 0.3, 0.6]]) compare = l2norm(*np.gradient(ERROR_OBJ.lwc)) assert_array_almost_equal(ERROR_OBJ._calc_lwc_gradient(), compare) def test_calc_lwc_relative_error(): from cloudnetpy.utils import l2norm ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.3], [0.1, 0.3, 0.6]]) x = l2norm(*np.gradient(ERROR_OBJ.lwc)) compare = x / ERROR_OBJ.lwc / 2 compare[compare > 5] = 5 assert_array_almost_equal(ERROR_OBJ._calc_lwc_relative_error(), compare) def test_calc_lwp_relative_error(): ERROR_OBJ.lwc_source.lwp = np.array([0.1, 0.5]) ERROR_OBJ.lwc_source.lwp_error = np.array([0.2, 5.5]) compare = ERROR_OBJ.lwc_source.lwp_error / ERROR_OBJ.lwc_source.lwp compare[compare > 10] = 10 assert_array_equal(ERROR_OBJ._calc_lwp_relative_error(), compare) def test_calc_combined_error(): from cloudnetpy.utils import transpose, l2norm err_2d = np.array([[0, 0.1, 0.1], [0.2, 0.4, 0.15]]) err_1d = np.array([0.3, 0.2]) compare = l2norm(err_2d, transpose(err_1d)) assert_array_equal(ERROR_OBJ._calc_combined_error(err_2d, err_1d), compare) def test_fill_error_array(): error_in = np.array([[0, 0.1, 0.1], [0.2, 0.4, 0.15]]) ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.1], [0.1, 0.2, 0.2]], mask=[[0, 1, 0], [1, 0, 0]]) compare = np.ma.array([[0, 0, 0], [0, 0, 0]], mask=[[0, 1, 0], [1, 0, 0]]) ERROR_OBJ._fill_error_array(error_in) error = ERROR_OBJ._fill_error_array(error_in) assert_array_almost_equal(error.mask, compare.mask) def test_screen_rain_error(): compare = np.ma.array([[0.709, 0, 0.709], [0, 0, 0]], mask=[[0, 1, 0], [1, 1, 1]]) assert_array_equal(ERROR_OBJ.lwc_error.mask, compare.mask) @pytest.mark.parametrize(""key"", [""lwc"", ""lwc_retrieval_status"", ""lwc_error""]) def test_append_data(lwc_source_file, key): from cloudnetpy.products.lwc import _append_data lwc_source = LwcSource(lwc_source_file) _append_data(lwc_source, LWC_OBJ, STATUS_OBJ, ERROR_OBJ) assert key in lwc_source.data.keys() ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_lwc.py::test_update_status[0],tests/unit/test_lwc.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import numpy as np from numpy.testing import assert_array_equal, assert_array_almost_equal from collections import namedtuple import pytest import netCDF4 from cloudnetpy.products.lwc import LwcSource, Lwc, LwcStatus, LwcError from cloudnetpy.categorize import atmos DIMENSIONS = ('time', 'height', 'model_time', 'model_height') TEST_ARRAY = np.arange(3) CategorizeBits = namedtuple('CategorizeBits', ['category_bits', 'quality_bits']) @pytest.fixture(scope='session') def lwc_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp) _create_dimension_variables(root_grp) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('lwp', 'f8', 'time') var[:] = [1, 1, 0.5] var = root_grp.createVariable('lwp_error', 'f8', 'time') var[:] = [0.2, 0.2, 0.1] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1, 1] var = root_grp.createVariable('category_bits', 'i4', 'time') var[:] = [0, 1, 2] var = root_grp.createVariable('quality_bits', 'i4', 'time') var[:] = [8, 16, 32] var = root_grp.createVariable('temperature', 'f8', ('time', 'height')) var[:] = np.array([[282, 280, 278], [286, 284, 282], [284, 282, 280]]) var = root_grp.createVariable('pressure', 'f8', ('time', 'height')) var[:] = np.array([[1010, 1000, 990], [1020, 1010, 1000], [1030, 1020, 1010]]) root_grp.close() return file_name def _create_dimensions(root_grp): n_dim = len(TEST_ARRAY) for dim_name in DIMENSIONS: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp): for dim_name in DIMENSIONS: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = TEST_ARRAY if dim_name == 'height': x.units = 'm' def test_get_atmosphere_t(lwc_source_file): obj = LwcSource(lwc_source_file) compare = np.array([[282, 280, 278], [286, 284, 282], [284, 282, 280]]) assert_array_equal(compare, obj.atmosphere[0]) def test_get_atmosphere_p(lwc_source_file): obj = LwcSource(lwc_source_file) compare = np.array([[1010, 1000, 990], [1020, 1010, 1000], [1030, 1020, 1010]]) assert_array_equal(compare, obj.atmosphere[-1]) class LwcSourceObj: def __init__(self): self.dheight = 10 self.categorize_bits = \ CategorizeBits(category_bits={'droplet': np.asarray([[1, 0, 1], [0, 1, 1]], dtype=bool)}, quality_bits={'radar': np.asarray([[1, 0, 1], [0, 1, 1]], dtype=bool), 'lidar': np.asarray([[1, 0, 1], [0, 1, 1]], dtype=bool)}) self.atmosphere = (np.array([[282, 281, 280], [280, 279, 278]]), np.array([[101000, 100500, 100000], [100000, 99500, 99000]])) self.lwp = np.array([2, 0]) self.lwp_error = np.array([0.1, 0.2]) self.is_rain = np.array([0, 1]) LWC_OBJ = Lwc(LwcSourceObj()) STATUS_OBJ = LwcStatus(LwcSourceObj(), LWC_OBJ) ERROR_OBJ = LwcError(LwcSourceObj(), LWC_OBJ) @pytest.mark.parametrize(""value"", [0, 1]) def test_get_liquid(value): assert value in LWC_OBJ.is_liquid def test_init_lwc_adiabatic(lwc_source_file): lwc_source = LwcSourceObj() compare = atmos.fill_clouds_with_lwc_dz(lwc_source.atmosphere, LWC_OBJ.is_liquid) compare[0, 0] *= 10 compare[0, 2] *= 10 compare[1, 1] *= 10 compare[1, 2] *= 20 assert_array_almost_equal(LWC_OBJ._init_lwc_adiabatic(), compare) def test_screen_rain_lwc(): compare = np.ma.array([[5, 1, 2], [3, 6, 0]], mask=[[0, 0, 0], [1, 1, 1]]) assert_array_equal(compare.mask, LWC_OBJ.lwc.mask) @pytest.mark.parametrize(""value"", [0, 1]) def test_init_status(value): assert value in STATUS_OBJ._init_status() @pytest.mark.parametrize(""key"", ['radar', 'lidar']) def test_get_echo(key): assert key in STATUS_OBJ.echo.keys() @pytest.mark.parametrize(""value"", [0, 1, 2]) def test_update_status(value): time = np.array([0]) STATUS_OBJ._update_status(time) assert value in STATUS_OBJ.status @pytest.mark.parametrize(""value"", [0, 1, 2, 3]) def test_adjust_lwc(value): time = 0 base = 0 STATUS_OBJ.status = np.array([[1, 0, 2], [0, 0, 2]]) STATUS_OBJ._adjust_lwc(time, base) assert value in STATUS_OBJ.status def test_has_converged(): ind = 1 assert STATUS_OBJ._has_converged(ind) is True def test_out_of_bound(): ind = 2 assert STATUS_OBJ._out_of_bound(ind) is True def test_find_adjustable_clouds(): assert 1 not in STATUS_OBJ._find_adjustable_clouds() def test_find_topmost_clouds(): compare = np.asarray([[0, 0, 1], [0, 1, 1]], dtype=bool) assert_array_equal(STATUS_OBJ._find_topmost_clouds(), compare) def test_find_echo_combinations_in_liquid(): STATUS_OBJ.echo['lidar'] = np.array([[0, 1, 0], [1, 1, 0]]) STATUS_OBJ.echo['radar'] = np.array([[0, 0, 0], [0, 1, 1]]) STATUS_OBJ.is_liquid = np.array([[1, 1, 1], [0, 1, 1]]) compare = np.array([[0, 1, 0], [0, 3, 2]]) assert_array_equal(STATUS_OBJ._find_echo_combinations_in_liquid(), compare) def test_find_lidar_only_clouds(): inds = np.array([[1, 0, 0], [0, 1, 3]]) compare = np.array([True, False]) assert_array_equal(STATUS_OBJ._find_lidar_only_clouds(inds), compare) def test_remove_good_profiles(): top_c = np.asarray([[1, 1, 0], [1, 0, 1]], dtype=bool) compare = np.asarray([[1, 1, 0], [0, 0, 0]], dtype=bool) assert_array_equal(STATUS_OBJ._remove_good_profiles(top_c), compare) def test_find_lwp_difference(): STATUS_OBJ.lwc_adiabatic = np.array([[1, 8, 2], [2, 3, 7]]) STATUS_OBJ.lwc_source.lwp = np.array([50, 30]) compare = np.array([60, 90]) assert_array_equal(STATUS_OBJ._find_lwp_difference(), compare) @pytest.mark.parametrize(""value"", [0, 1, 2, 3, 4]) def test_screen_rain_status(value): STATUS_OBJ.lwc_source.is_rain = np.array([0, 1]) STATUS_OBJ.status = np.array([[0, 2, 2, 3, 1], [1, 3, 0, 2, 2]]) STATUS_OBJ.screen_rain() assert value in STATUS_OBJ.status def test_limit_error(): error = np.array([[0, 0, 1], [0.2, 0.4, 0.3]]) max_v = 0.5 compare = np.array([[0, 0, 0.5], [0.2, 0.4, 0.3]]) assert_array_equal(ERROR_OBJ._limit_error(error, max_v), compare) def test_calc_lwc_gradient(): from cloudnetpy.utils import l2norm ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.3], [0.1, 0.3, 0.6]]) compare = l2norm(*np.gradient(ERROR_OBJ.lwc)) assert_array_almost_equal(ERROR_OBJ._calc_lwc_gradient(), compare) def test_calc_lwc_relative_error(): from cloudnetpy.utils import l2norm ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.3], [0.1, 0.3, 0.6]]) x = l2norm(*np.gradient(ERROR_OBJ.lwc)) compare = x / ERROR_OBJ.lwc / 2 compare[compare > 5] = 5 assert_array_almost_equal(ERROR_OBJ._calc_lwc_relative_error(), compare) def test_calc_lwp_relative_error(): ERROR_OBJ.lwc_source.lwp = np.array([0.1, 0.5]) ERROR_OBJ.lwc_source.lwp_error = np.array([0.2, 5.5]) compare = ERROR_OBJ.lwc_source.lwp_error / ERROR_OBJ.lwc_source.lwp compare[compare > 10] = 10 assert_array_equal(ERROR_OBJ._calc_lwp_relative_error(), compare) def test_calc_combined_error(): from cloudnetpy.utils import transpose, l2norm err_2d = np.array([[0, 0.1, 0.1], [0.2, 0.4, 0.15]]) err_1d = np.array([0.3, 0.2]) compare = l2norm(err_2d, transpose(err_1d)) assert_array_equal(ERROR_OBJ._calc_combined_error(err_2d, err_1d), compare) def test_fill_error_array(): error_in = np.array([[0, 0.1, 0.1], [0.2, 0.4, 0.15]]) ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.1], [0.1, 0.2, 0.2]], mask=[[0, 1, 0], [1, 0, 0]]) compare = np.ma.array([[0, 0, 0], [0, 0, 0]], mask=[[0, 1, 0], [1, 0, 0]]) ERROR_OBJ._fill_error_array(error_in) error = ERROR_OBJ._fill_error_array(error_in) assert_array_almost_equal(error.mask, compare.mask) def test_screen_rain_error(): compare = np.ma.array([[0.709, 0, 0.709], [0, 0, 0]], mask=[[0, 1, 0], [1, 1, 1]]) assert_array_equal(ERROR_OBJ.lwc_error.mask, compare.mask) @pytest.mark.parametrize(""key"", [""lwc"", ""lwc_retrieval_status"", ""lwc_error""]) def test_append_data(lwc_source_file, key): from cloudnetpy.products.lwc import _append_data lwc_source = LwcSource(lwc_source_file) _append_data(lwc_source, LWC_OBJ, STATUS_OBJ, ERROR_OBJ) assert key in lwc_source.data.keys() ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_lwc.py::test_update_status[1],tests/unit/test_lwc.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import numpy as np from numpy.testing import assert_array_equal, assert_array_almost_equal from collections import namedtuple import pytest import netCDF4 from cloudnetpy.products.lwc import LwcSource, Lwc, LwcStatus, LwcError from cloudnetpy.categorize import atmos DIMENSIONS = ('time', 'height', 'model_time', 'model_height') TEST_ARRAY = np.arange(3) CategorizeBits = namedtuple('CategorizeBits', ['category_bits', 'quality_bits']) @pytest.fixture(scope='session') def lwc_source_file(tmpdir_factory, file_metadata): file_name = tmpdir_factory.mktemp(""data"").join(""file.nc"") root_grp = netCDF4.Dataset(file_name, ""w"", format=""NETCDF4_CLASSIC"") _create_dimensions(root_grp) _create_dimension_variables(root_grp) var = root_grp.createVariable('altitude', 'f8') var[:] = 1 var.units = 'km' var = root_grp.createVariable('lwp', 'f8', 'time') var[:] = [1, 1, 0.5] var = root_grp.createVariable('lwp_error', 'f8', 'time') var[:] = [0.2, 0.2, 0.1] var = root_grp.createVariable('is_rain', 'i4', 'time') var[:] = [0, 1, 1] var = root_grp.createVariable('category_bits', 'i4', 'time') var[:] = [0, 1, 2] var = root_grp.createVariable('quality_bits', 'i4', 'time') var[:] = [8, 16, 32] var = root_grp.createVariable('temperature', 'f8', ('time', 'height')) var[:] = np.array([[282, 280, 278], [286, 284, 282], [284, 282, 280]]) var = root_grp.createVariable('pressure', 'f8', ('time', 'height')) var[:] = np.array([[1010, 1000, 990], [1020, 1010, 1000], [1030, 1020, 1010]]) root_grp.close() return file_name def _create_dimensions(root_grp): n_dim = len(TEST_ARRAY) for dim_name in DIMENSIONS: root_grp.createDimension(dim_name, n_dim) def _create_dimension_variables(root_grp): for dim_name in DIMENSIONS: x = root_grp.createVariable(dim_name, 'f8', (dim_name,)) x[:] = TEST_ARRAY if dim_name == 'height': x.units = 'm' def test_get_atmosphere_t(lwc_source_file): obj = LwcSource(lwc_source_file) compare = np.array([[282, 280, 278], [286, 284, 282], [284, 282, 280]]) assert_array_equal(compare, obj.atmosphere[0]) def test_get_atmosphere_p(lwc_source_file): obj = LwcSource(lwc_source_file) compare = np.array([[1010, 1000, 990], [1020, 1010, 1000], [1030, 1020, 1010]]) assert_array_equal(compare, obj.atmosphere[-1]) class LwcSourceObj: def __init__(self): self.dheight = 10 self.categorize_bits = \ CategorizeBits(category_bits={'droplet': np.asarray([[1, 0, 1], [0, 1, 1]], dtype=bool)}, quality_bits={'radar': np.asarray([[1, 0, 1], [0, 1, 1]], dtype=bool), 'lidar': np.asarray([[1, 0, 1], [0, 1, 1]], dtype=bool)}) self.atmosphere = (np.array([[282, 281, 280], [280, 279, 278]]), np.array([[101000, 100500, 100000], [100000, 99500, 99000]])) self.lwp = np.array([2, 0]) self.lwp_error = np.array([0.1, 0.2]) self.is_rain = np.array([0, 1]) LWC_OBJ = Lwc(LwcSourceObj()) STATUS_OBJ = LwcStatus(LwcSourceObj(), LWC_OBJ) ERROR_OBJ = LwcError(LwcSourceObj(), LWC_OBJ) @pytest.mark.parametrize(""value"", [0, 1]) def test_get_liquid(value): assert value in LWC_OBJ.is_liquid def test_init_lwc_adiabatic(lwc_source_file): lwc_source = LwcSourceObj() compare = atmos.fill_clouds_with_lwc_dz(lwc_source.atmosphere, LWC_OBJ.is_liquid) compare[0, 0] *= 10 compare[0, 2] *= 10 compare[1, 1] *= 10 compare[1, 2] *= 20 assert_array_almost_equal(LWC_OBJ._init_lwc_adiabatic(), compare) def test_screen_rain_lwc(): compare = np.ma.array([[5, 1, 2], [3, 6, 0]], mask=[[0, 0, 0], [1, 1, 1]]) assert_array_equal(compare.mask, LWC_OBJ.lwc.mask) @pytest.mark.parametrize(""value"", [0, 1]) def test_init_status(value): assert value in STATUS_OBJ._init_status() @pytest.mark.parametrize(""key"", ['radar', 'lidar']) def test_get_echo(key): assert key in STATUS_OBJ.echo.keys() @pytest.mark.parametrize(""value"", [0, 1, 2]) def test_update_status(value): time = np.array([0]) STATUS_OBJ._update_status(time) assert value in STATUS_OBJ.status @pytest.mark.parametrize(""value"", [0, 1, 2, 3]) def test_adjust_lwc(value): time = 0 base = 0 STATUS_OBJ.status = np.array([[1, 0, 2], [0, 0, 2]]) STATUS_OBJ._adjust_lwc(time, base) assert value in STATUS_OBJ.status def test_has_converged(): ind = 1 assert STATUS_OBJ._has_converged(ind) is True def test_out_of_bound(): ind = 2 assert STATUS_OBJ._out_of_bound(ind) is True def test_find_adjustable_clouds(): assert 1 not in STATUS_OBJ._find_adjustable_clouds() def test_find_topmost_clouds(): compare = np.asarray([[0, 0, 1], [0, 1, 1]], dtype=bool) assert_array_equal(STATUS_OBJ._find_topmost_clouds(), compare) def test_find_echo_combinations_in_liquid(): STATUS_OBJ.echo['lidar'] = np.array([[0, 1, 0], [1, 1, 0]]) STATUS_OBJ.echo['radar'] = np.array([[0, 0, 0], [0, 1, 1]]) STATUS_OBJ.is_liquid = np.array([[1, 1, 1], [0, 1, 1]]) compare = np.array([[0, 1, 0], [0, 3, 2]]) assert_array_equal(STATUS_OBJ._find_echo_combinations_in_liquid(), compare) def test_find_lidar_only_clouds(): inds = np.array([[1, 0, 0], [0, 1, 3]]) compare = np.array([True, False]) assert_array_equal(STATUS_OBJ._find_lidar_only_clouds(inds), compare) def test_remove_good_profiles(): top_c = np.asarray([[1, 1, 0], [1, 0, 1]], dtype=bool) compare = np.asarray([[1, 1, 0], [0, 0, 0]], dtype=bool) assert_array_equal(STATUS_OBJ._remove_good_profiles(top_c), compare) def test_find_lwp_difference(): STATUS_OBJ.lwc_adiabatic = np.array([[1, 8, 2], [2, 3, 7]]) STATUS_OBJ.lwc_source.lwp = np.array([50, 30]) compare = np.array([60, 90]) assert_array_equal(STATUS_OBJ._find_lwp_difference(), compare) @pytest.mark.parametrize(""value"", [0, 1, 2, 3, 4]) def test_screen_rain_status(value): STATUS_OBJ.lwc_source.is_rain = np.array([0, 1]) STATUS_OBJ.status = np.array([[0, 2, 2, 3, 1], [1, 3, 0, 2, 2]]) STATUS_OBJ.screen_rain() assert value in STATUS_OBJ.status def test_limit_error(): error = np.array([[0, 0, 1], [0.2, 0.4, 0.3]]) max_v = 0.5 compare = np.array([[0, 0, 0.5], [0.2, 0.4, 0.3]]) assert_array_equal(ERROR_OBJ._limit_error(error, max_v), compare) def test_calc_lwc_gradient(): from cloudnetpy.utils import l2norm ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.3], [0.1, 0.3, 0.6]]) compare = l2norm(*np.gradient(ERROR_OBJ.lwc)) assert_array_almost_equal(ERROR_OBJ._calc_lwc_gradient(), compare) def test_calc_lwc_relative_error(): from cloudnetpy.utils import l2norm ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.3], [0.1, 0.3, 0.6]]) x = l2norm(*np.gradient(ERROR_OBJ.lwc)) compare = x / ERROR_OBJ.lwc / 2 compare[compare > 5] = 5 assert_array_almost_equal(ERROR_OBJ._calc_lwc_relative_error(), compare) def test_calc_lwp_relative_error(): ERROR_OBJ.lwc_source.lwp = np.array([0.1, 0.5]) ERROR_OBJ.lwc_source.lwp_error = np.array([0.2, 5.5]) compare = ERROR_OBJ.lwc_source.lwp_error / ERROR_OBJ.lwc_source.lwp compare[compare > 10] = 10 assert_array_equal(ERROR_OBJ._calc_lwp_relative_error(), compare) def test_calc_combined_error(): from cloudnetpy.utils import transpose, l2norm err_2d = np.array([[0, 0.1, 0.1], [0.2, 0.4, 0.15]]) err_1d = np.array([0.3, 0.2]) compare = l2norm(err_2d, transpose(err_1d)) assert_array_equal(ERROR_OBJ._calc_combined_error(err_2d, err_1d), compare) def test_fill_error_array(): error_in = np.array([[0, 0.1, 0.1], [0.2, 0.4, 0.15]]) ERROR_OBJ.lwc = np.ma.array([[0.1, 0.2, 0.1], [0.1, 0.2, 0.2]], mask=[[0, 1, 0], [1, 0, 0]]) compare = np.ma.array([[0, 0, 0], [0, 0, 0]], mask=[[0, 1, 0], [1, 0, 0]]) ERROR_OBJ._fill_error_array(error_in) error = ERROR_OBJ._fill_error_array(error_in) assert_array_almost_equal(error.mask, compare.mask) def test_screen_rain_error(): compare = np.ma.array([[0.709, 0, 0.709], [0, 0, 0]], mask=[[0, 1, 0], [1, 1, 1]]) assert_array_equal(ERROR_OBJ.lwc_error.mask, compare.mask) @pytest.mark.parametrize(""key"", [""lwc"", ""lwc_retrieval_status"", ""lwc_error""]) def test_append_data(lwc_source_file, key): from cloudnetpy.products.lwc import _append_data lwc_source = LwcSource(lwc_source_file) _append_data(lwc_source, LWC_OBJ, STATUS_OBJ, ERROR_OBJ) assert key in lwc_source.data.keys() ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_meta_for_old_files.py::test_fix_old_data_2,tests/unit/test_meta_for_old_files.py,NIO,flaky,Opened,https://github.com/tukiains/cloudnetpy-legacy/pull/29,classify;root_cause,"import numpy as np import numpy.ma as ma from cloudnetpy.plotting import meta_for_old_files from numpy.testing import assert_array_equal data_orig = ma.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) def test_fix_old_data(): data, name = meta_for_old_files.fix_old_data(data_orig, 'specific_humidity') assert_array_equal(data_orig, data) assert_array_equal(name, 'q') def test_fix_old_data_2(): data, name = meta_for_old_files.fix_old_data(data_orig, 'detection_status') assert_array_equal(data_orig, data) assert ma.count(data) == 7 ", https://github.com/tukiains/cloudnetpy,26f2607b890630146469cfa410fce99438ceee3f,tests/unit/test_rpg.py::TestReduceHeader::test_1,tests/unit/test_rpg.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from numpy.testing import assert_array_equal from cloudnetpy.instruments import rpg @pytest.fixture def example_files(tmpdir): file_names = ['f.LV1', 'f.txt', 'f.LV0', 'f.lv1', 'g.LV1'] folder = tmpdir.mkdir('data/') for name in file_names: with open(folder.join(name), 'wb') as f: f.write(b'abc') return folder def test_get_rpg_files(example_files): dir_name = example_files.dirname + '/data/' result = [f""{dir_name}{x}"" for x in ('f.LV1', 'g.LV1')] assert rpg.get_rpg_files(dir_name) == result class TestReduceHeader: n_points = 100 header = {'a': n_points * [1], 'b': n_points * [2], 'c': n_points * [3]} def test_1(self): assert_array_equal(rpg._reduce_header(self.header), {'a': 1, 'b': 2, 'c': 3}) def test_2(self): self.header['a'][50] = 10 with pytest.raises(AssertionError): assert_array_equal(rpg._reduce_header(self.header), {'a': 1, 'b': 2, 'c': 3}) ", https://github.com/unfoldingWord-dev/python-resource-container,64d87f900af969f47b20b798cf00c2861435b0d8,tests/container_test.py::TestResourceContainer::test_should_fail_to_load_missing_rc,tests/container_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil from unittest import TestCase from resource_container import factory from general_tools.file_utils import write_file import pytest import yaml DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data') def make_rc(dir, multi=False): manifest = { 'dublin_core': { 'type': 'book', 'conformsto': 'rc0.2', 'format': 'text/usfm', 'identifier': 'en-ulb', 'title': 'Unlocked Literal Bible', 'subject': 'Bible translation', 'description': 'The Unlocked Literal Bible is an open-licensed version of the Bible that is intended to provide a form-centric translation of the Bible.', 'language': { 'identifier': 'en', 'title': 'English', 'direction': 'ltr' }, 'source': [{ 'language': 'en', 'identifier': 'en-asv', 'version': '1990' }], 'rights': 'CC BY-SA 4.0', 'creator': 'Wycliffe Associates', 'contributor': [ 'Wycliffe Associates' ], 'relation': [ 'en-udb', 'en-tn' ], 'publisher': 'Door43', 'issued': '2015-12-17', 'modified': '2015-12-22', 'version': '3' }, 'checking': { 'checking_entity': [ 'Wycliffe Associates', ], 'checking_level': '3' }, 'projects': [{ 'identifier': 'gen', 'title': 'Genesis', 'versification': 'kjv', 'sort': 1, 'path': './gen', 'categories': [ 'bible-ot' ] }] } if multi is True: manifest['projects'].append({ 'identifier': 'exo', 'title': 'Exodus', 'versification': 'kjv', 'sort': 2, 'path': './exo', 'categories': [ 'bible-ot' ] }) rc = factory.create(dir, manifest) write_file(os.path.join(dir, 'gen', '01', '01.usfm'), 'gen 1:1') write_file(os.path.join(dir, 'gen', '01', '02.usfm'), 'gen 1:2') write_file(os.path.join(dir, 'gen', '01', '03.usfm'), 'gen 1:3') write_file(os.path.join(dir, 'gen', '02', '01.usfm'), 'gen 2:1') write_file(os.path.join(dir, 'gen', '02', '02.usfm'), 'gen 2:2') if multi is True: write_file(os.path.join(dir, 'exo', '01', '01.usfm'), 'exo 1:1') write_file(os.path.join(dir, 'exo', '01', '02.usfm'), 'exo 1:2') write_file(os.path.join(dir, 'exo', '01', '03.usfm'), 'exo 1:3') write_file(os.path.join(dir, 'exo', '02', '01.usfm'), 'exo 2:1') write_file(os.path.join(dir, 'exo', '02', '02.usfm'), 'exo 2:2') class TestResourceContainer(TestCase): @classmethod def setUpClass(cls): directory = os.path.join(DATA_DIR, 'temp') if os.path.exists(directory): shutil.rmtree(directory) def test_load_a_single_book_rc(self): directory = os.path.join(DATA_DIR, 'temp', 'single') make_rc(directory) rc = factory.load(directory) assert rc.path == directory assert len(rc.chapters()) == 2 assert len(rc.chunks('01')) == 3 assert len(rc.chunks('02')) == 2 assert rc.read_chunk('01', '03') == 'gen 1:3' def test_load_a_multi_book_rc(self): directory = os.path.join(DATA_DIR, 'temp', 'multi') make_rc(directory, True) rc = factory.load(directory) assert rc.project_count == 2 assert rc.path == directory assert len(rc.chapters('gen')) == 2 assert len(rc.chunks('gen', '01')) == 3 assert len(rc.chunks('gen', '02')) == 2 assert rc.read_chunk('gen', '01', '03') == 'gen 1:3' assert len(rc.chapters('exo')) == 2 assert len(rc.chunks('exo', '01')) == 3 assert len(rc.chunks('exo', '02')) == 2 assert rc.read_chunk('exo', '01', '03') == 'exo 1:3' def test_should_fail_to_load_missing_rc(self): directory = os.path.join(DATA_DIR, 'temp', 'missing') os.mkdir(directory) rc = None with pytest.raises(Exception) as my_error: rc = factory.load(directory) assert 'Not a resource container. Missing manifest.yaml' in str(my_error.value) assert rc is None def test_should_load_a_missing_rc_when_not_in_strict_mode(self): directory = os.path.join(DATA_DIR, 'temp', 'missing') if os.path.isdir(directory): os.rmdir(directory) os.mkdir(directory) rc = factory.load(directory, False) assert rc is not None def test_updating_a_rc(self): directory = os.path.join(DATA_DIR, 'temp', 'updated_container') chunk_text = 'Hello world!' make_rc(directory) rc = factory.load(directory) rc.write_chunk('02', '03', chunk_text) rc.write_chunk('03', '01', chunk_text) assert rc.read_chunk('02', '03') == chunk_text assert rc.read_chunk('03', '01') == chunk_text def test_creating_a_rc(self): directory = os.path.join(DATA_DIR, 'temp', 'new_rc') manifest = { 'dublin_core': { 'type': 'book', 'format': 'text/usfm', 'identifier': 'en-me', 'language': { 'identifier': 'en', 'title': 'English', 'direction': 'ltr' }, 'rights': 'CC BY-SA 4.0' } } rc = factory.create(directory, manifest) assert rc.conforms_to == factory.current_version assert rc.type == 'book' def test_not_opening_a_rc_that_is_too_old(self): directory = os.path.join(DATA_DIR, 'temp', 'old_rc') manifest = { 'dublin_core': { 'type': 'book', 'conformsto': 'rc0.1', 'format': 'text/usfm', 'identifier': 'en-ulb', 'language': { 'identifier': 'en', 'title': 'English', 'direction': 'ltr' }, }, 'projects': [] } write_file(os.path.join(directory, 'manifest.yaml'), yaml.dump(manifest, default_flow_style=False)) rc = None with pytest.raises(Exception) as my_error: rc = factory.load(directory) assert 'Unsupported resource container version. Found 0.1 but expected 0.2' in str(my_error.value) assert rc is None def test_not_opening_rc_that_is_too_new(self): directory = os.path.join(DATA_DIR, 'temp', 'too_new_rc') manifest = { 'dublin_core': { 'type': 'book', 'conformsto': 'rc999.1', 'format': 'text/usfm', 'identifier': 'en-ulb', 'language': { 'identifier': 'en', 'title': 'English', 'direction': 'ltr' }, }, 'projects': [] } write_file(os.path.join(directory, 'manifest.yaml'), yaml.dump(manifest, default_flow_style=False)) rc = None with pytest.raises(Exception) as my_error: rc = factory.load(directory) assert 'Unsupported resource container version. Found 999.1 but expected 0.2' in str(my_error.value) assert rc is None def test_thowing_an_error_when_not_specifying_project_in_multi_project_rc(self): directory = os.path.join(DATA_DIR, 'temp', 'big_container') make_rc(directory, True) rc = factory.load(directory) with pytest.raises(Exception) as my_error: rc.chapters() assert 'Multiple projects found. Specify the project identifier.' in str(my_error.value) with pytest.raises(Exception) as my_error2: rc.chunks('01') assert 'Multiple projects found. Specify the project identifier.' in str(my_error2.value) with pytest.raises(Exception) as my_error3: rc.read_chunk('01', '01') assert 'Multiple projects found. Specify the project identifier.' in str(my_error3.value) with pytest.raises(Exception) as my_error4: rc.write_chunk('01', '01', 'test') assert 'Multiple projects found. Specify the project identifier.' in str(my_error4.value) def test_open_project(self): dir = os.path.join(DATA_DIR, 'existing-rc', 'en-obs') rc = factory.load(dir) project = rc.project() assert project['identifier'] == 'obs' assert rc.conforms_to == '0.2' assert type(rc.chapters()) == list assert rc.language['identifier'] == 'en' assert rc.chunks('01') == ['01.md'] assert rc.read_chunk('01', '01').split('\n', 1)[0] == '# 1. The Creation' assert rc.type == 'book' assert rc.config()['content']['10']['10']['dict'][0] == '/en/tw/bible/god' assert rc.toc() is None def test_write_and_remove_chunk(self): dir = os.path.join(DATA_DIR, 'existing-rc', 'en-obs') rc = factory.load(dir) my_chunk = 'this is a test' rc.write_chunk('test', 'test', my_chunk) assert rc.read_chunk('test', 'test') == my_chunk rc.write_chunk('test', 'test', '') assert rc.read_chunk('test', 'test') is None def test_create_resource_container(self): dir = os.path.join(DATA_DIR, 'temp', 'new-rc', 'en-obs') container = factory.create(dir, { 'dublin_core': { 'type': 'book', 'format': 'text/markdown', 'identifier': 'obs', 'language': { 'direction': 'ltr', 'identifier': 'en', 'title'", https://github.com/unfoldingWord-dev/python-resource-container,64d87f900af969f47b20b798cf00c2861435b0d8,tests/container_test.py::TestResourceContainer::test_should_load_a_missing_rc_when_not_in_strict_mode,tests/container_test.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import os import shutil from unittest import TestCase from resource_container import factory from general_tools.file_utils import write_file import pytest import yaml DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data') def make_rc(dir, multi=False): manifest = { 'dublin_core': { 'type': 'book', 'conformsto': 'rc0.2', 'format': 'text/usfm', 'identifier': 'en-ulb', 'title': 'Unlocked Literal Bible', 'subject': 'Bible translation', 'description': 'The Unlocked Literal Bible is an open-licensed version of the Bible that is intended to provide a form-centric translation of the Bible.', 'language': { 'identifier': 'en', 'title': 'English', 'direction': 'ltr' }, 'source': [{ 'language': 'en', 'identifier': 'en-asv', 'version': '1990' }], 'rights': 'CC BY-SA 4.0', 'creator': 'Wycliffe Associates', 'contributor': [ 'Wycliffe Associates' ], 'relation': [ 'en-udb', 'en-tn' ], 'publisher': 'Door43', 'issued': '2015-12-17', 'modified': '2015-12-22', 'version': '3' }, 'checking': { 'checking_entity': [ 'Wycliffe Associates', ], 'checking_level': '3' }, 'projects': [{ 'identifier': 'gen', 'title': 'Genesis', 'versification': 'kjv', 'sort': 1, 'path': './gen', 'categories': [ 'bible-ot' ] }] } if multi is True: manifest['projects'].append({ 'identifier': 'exo', 'title': 'Exodus', 'versification': 'kjv', 'sort': 2, 'path': './exo', 'categories': [ 'bible-ot' ] }) rc = factory.create(dir, manifest) write_file(os.path.join(dir, 'gen', '01', '01.usfm'), 'gen 1:1') write_file(os.path.join(dir, 'gen', '01', '02.usfm'), 'gen 1:2') write_file(os.path.join(dir, 'gen', '01', '03.usfm'), 'gen 1:3') write_file(os.path.join(dir, 'gen', '02', '01.usfm'), 'gen 2:1') write_file(os.path.join(dir, 'gen', '02', '02.usfm'), 'gen 2:2') if multi is True: write_file(os.path.join(dir, 'exo', '01', '01.usfm'), 'exo 1:1') write_file(os.path.join(dir, 'exo', '01', '02.usfm'), 'exo 1:2') write_file(os.path.join(dir, 'exo', '01', '03.usfm'), 'exo 1:3') write_file(os.path.join(dir, 'exo', '02', '01.usfm'), 'exo 2:1') write_file(os.path.join(dir, 'exo', '02', '02.usfm'), 'exo 2:2') class TestResourceContainer(TestCase): @classmethod def setUpClass(cls): directory = os.path.join(DATA_DIR, 'temp') if os.path.exists(directory): shutil.rmtree(directory) def test_load_a_single_book_rc(self): directory = os.path.join(DATA_DIR, 'temp', 'single') make_rc(directory) rc = factory.load(directory) assert rc.path == directory assert len(rc.chapters()) == 2 assert len(rc.chunks('01')) == 3 assert len(rc.chunks('02')) == 2 assert rc.read_chunk('01', '03') == 'gen 1:3' def test_load_a_multi_book_rc(self): directory = os.path.join(DATA_DIR, 'temp', 'multi') make_rc(directory, True) rc = factory.load(directory) assert rc.project_count == 2 assert rc.path == directory assert len(rc.chapters('gen')) == 2 assert len(rc.chunks('gen', '01')) == 3 assert len(rc.chunks('gen', '02')) == 2 assert rc.read_chunk('gen', '01', '03') == 'gen 1:3' assert len(rc.chapters('exo')) == 2 assert len(rc.chunks('exo', '01')) == 3 assert len(rc.chunks('exo', '02')) == 2 assert rc.read_chunk('exo', '01', '03') == 'exo 1:3' def test_should_fail_to_load_missing_rc(self): directory = os.path.join(DATA_DIR, 'temp', 'missing') os.mkdir(directory) rc = None with pytest.raises(Exception) as my_error: rc = factory.load(directory) assert 'Not a resource container. Missing manifest.yaml' in str(my_error.value) assert rc is None def test_should_load_a_missing_rc_when_not_in_strict_mode(self): directory = os.path.join(DATA_DIR, 'temp', 'missing') if os.path.isdir(directory): os.rmdir(directory) os.mkdir(directory) rc = factory.load(directory, False) assert rc is not None def test_updating_a_rc(self): directory = os.path.join(DATA_DIR, 'temp', 'updated_container') chunk_text = 'Hello world!' make_rc(directory) rc = factory.load(directory) rc.write_chunk('02', '03', chunk_text) rc.write_chunk('03', '01', chunk_text) assert rc.read_chunk('02', '03') == chunk_text assert rc.read_chunk('03', '01') == chunk_text def test_creating_a_rc(self): directory = os.path.join(DATA_DIR, 'temp', 'new_rc') manifest = { 'dublin_core': { 'type': 'book', 'format': 'text/usfm', 'identifier': 'en-me', 'language': { 'identifier': 'en', 'title': 'English', 'direction': 'ltr' }, 'rights': 'CC BY-SA 4.0' } } rc = factory.create(directory, manifest) assert rc.conforms_to == factory.current_version assert rc.type == 'book' def test_not_opening_a_rc_that_is_too_old(self): directory = os.path.join(DATA_DIR, 'temp', 'old_rc') manifest = { 'dublin_core': { 'type': 'book', 'conformsto': 'rc0.1', 'format': 'text/usfm', 'identifier': 'en-ulb', 'language': { 'identifier': 'en', 'title': 'English', 'direction': 'ltr' }, }, 'projects': [] } write_file(os.path.join(directory, 'manifest.yaml'), yaml.dump(manifest, default_flow_style=False)) rc = None with pytest.raises(Exception) as my_error: rc = factory.load(directory) assert 'Unsupported resource container version. Found 0.1 but expected 0.2' in str(my_error.value) assert rc is None def test_not_opening_rc_that_is_too_new(self): directory = os.path.join(DATA_DIR, 'temp', 'too_new_rc') manifest = { 'dublin_core': { 'type': 'book', 'conformsto': 'rc999.1', 'format': 'text/usfm', 'identifier': 'en-ulb', 'language': { 'identifier': 'en', 'title': 'English', 'direction': 'ltr' }, }, 'projects': [] } write_file(os.path.join(directory, 'manifest.yaml'), yaml.dump(manifest, default_flow_style=False)) rc = None with pytest.raises(Exception) as my_error: rc = factory.load(directory) assert 'Unsupported resource container version. Found 999.1 but expected 0.2' in str(my_error.value) assert rc is None def test_thowing_an_error_when_not_specifying_project_in_multi_project_rc(self): directory = os.path.join(DATA_DIR, 'temp', 'big_container') make_rc(directory, True) rc = factory.load(directory) with pytest.raises(Exception) as my_error: rc.chapters() assert 'Multiple projects found. Specify the project identifier.' in str(my_error.value) with pytest.raises(Exception) as my_error2: rc.chunks('01') assert 'Multiple projects found. Specify the project identifier.' in str(my_error2.value) with pytest.raises(Exception) as my_error3: rc.read_chunk('01', '01') assert 'Multiple projects found. Specify the project identifier.' in str(my_error3.value) with pytest.raises(Exception) as my_error4: rc.write_chunk('01', '01', 'test') assert 'Multiple projects found. Specify the project identifier.' in str(my_error4.value) def test_open_project(self): dir = os.path.join(DATA_DIR, 'existing-rc', 'en-obs') rc = factory.load(dir) project = rc.project() assert project['identifier'] == 'obs' assert rc.conforms_to == '0.2' assert type(rc.chapters()) == list assert rc.language['identifier'] == 'en' assert rc.chunks('01') == ['01.md'] assert rc.read_chunk('01', '01').split('\n', 1)[0] == '# 1. The Creation' assert rc.type == 'book' assert rc.config()['content']['10']['10']['dict'][0] == '/en/tw/bible/god' assert rc.toc() is None def test_write_and_remove_chunk(self): dir = os.path.join(DATA_DIR, 'existing-rc', 'en-obs') rc = factory.load(dir) my_chunk = 'this is a test' rc.write_chunk('test', 'test', my_chunk) assert rc.read_chunk('test', 'test') == my_chunk rc.write_chunk('test', 'test', '') assert rc.read_chunk('test', 'test') is None def test_create_resource_container(self): dir = os.path.join(DATA_DIR, 'temp', 'new-rc', 'en-obs') container = factory.create(dir, { 'dublin_core': { 'type': 'book', 'format': 'text/markdown', 'identifier': 'obs', 'language': { 'direction': 'ltr', 'identifier': 'en', 'title'", https://github.com/uriyyo/instapi,6f972e2ae1ccc9a21eb6b18136ee677667cd4ba5,tests/unit_tests/models/test_media.py::test_media_info,tests/unit_tests/models/test_media.py,NOD,flaky,Opened,https://github.com/uriyyo/instapi/pull/139,classify;root_cause,"from ..conftest import random_string class TestMedia: """"""Tests for Media class"""""" def test_media_info(self, media, mocker): items = [[*range(100)]] data = {""items"": items} media_info_mock = mocker.patch(""instapi.client.client.media_info"", return_value=data) assert media._media_info() == items[0] media_info_mock.assert_called_once_with(media.pk) def test_comment(self, mocker, media): comment_mock = mocker.patch(""instapi.client.client.post_comment"") text = random_string() media.comment(text) comment_mock.assert_called_once_with(media.pk, text) ", https://github.com/Varkal/chuda,bd4d30c0f809d5349752ab2f322f30b01ab6b4a4,tests/test_app.py::test_output[argv0],tests/test_app.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause," import pytest from chuda.app import App from .utils import cli_args TEST_STRING = ""Test"" class BasicApp(App): app_name = ""basic_app"" def main(self): self.logger.debug(TEST_STRING) self.logger.info(TEST_STRING) @cli_args( [TEST_STRING] ) def test_run_call_main(mocker, argv): app = BasicApp() mocker.spy(app, ""main"") app.run() assert getattr(app.main, ""call_count"") == 1 @cli_args( [TEST_STRING] ) def test_output(capsys, argv): app = BasicApp() app.run() stdout, _ = capsys.readouterr() assert stdout == TEST_STRING+""\n"" @cli_args( [TEST_STRING, ""--quiet""], [TEST_STRING, ""-q""] ) def test_quiet(capsys, argv): app = BasicApp() app.run() stdout, _ = capsys.readouterr() assert stdout == """" @cli_args([TEST_STRING, ""--version""]) def test_version(capsys, argv): app = BasicApp() app.run() stdout, _ = capsys.readouterr() assert stdout == ""{}: {}\n"".format(app.app_name, app.version) @cli_args( [TEST_STRING, ""--verbose""], [TEST_STRING, ""-v""] ) def test_verbose(capsys, argv): app = BasicApp() app.run() stdout, _ = capsys.readouterr() assert stdout == ""{}\n{}\n"".format(TEST_STRING, TEST_STRING) @cli_args( [TEST_STRING, ""--help""], [TEST_STRING, ""-h""] ) def test_help(argv): with pytest.raises(SystemExit): app = BasicApp() app.run() @cli_args( [TEST_STRING, ""--verbose""], [TEST_STRING, ""-v""], [TEST_STRING, ""--quiet""], [TEST_STRING, ""-q""], [TEST_STRING, ""--version""], ) def test_no_default_args(argv): class NoDefaultArgsApp(App): override_default_arguments = True def main(self): self.logger.info(TEST_STRING) with pytest.raises(SystemExit): app = NoDefaultArgsApp() app.run() ", https://github.com/verdan/pyatlasclient,6c3ef3926eef059f00730b6bcc1a1787f6bbcd9c,tests/test_models.py::TestDiscoveryREST::test_search_attribute_get,tests/test_models.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from pytest_mock import mocker from pkg_resources import resource_filename import json import pytest from atlasclient import client from atlasclient import exceptions GUID = '8bbea92b-d98c-4613-ae6e-1a9d0b4f344b' RESPONSE_JSON_DIR = 'response_json' QUERY_JSON_DIR = 'query_json' @pytest.fixture(scope='class') def entity_post_response(): with open('{}/entity_post.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def entity_bulk_response(): with open('{}/entitybulk_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def entity_bulk_classification_response(): with open('{}/entitybulkclassification_post.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def entity_guid_response(): with open('{}/entityguid_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def entity_guid_delete_response(): with open('{}/entityguid_delete.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='function') def entity_guid(atlas_client): entity_guid = atlas_client.entity_guid(GUID) return(entity_guid) @pytest.fixture(scope='class') def entity_guid_classification_response(): with open('{}/entityguid_classification_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def entity_guid_classifications_post(): with open('{}/entityguid_classifications_post.json'.format(resource_filename(__name__, QUERY_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def typedefs_response(): with open('{}/typedefs_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def typedefs_headers_response(): with open('{}/typedefs_headers_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def relationshipdef_guid_response(): with open('{}/relationshipdef_guid_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def lineage_guid_response(): with open('{}/lineage_guid_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def relationship_guid_response(): with open('{}/relationship_guid_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def search_attribute_response(): with open('{}/search_attribute_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def search_saved_response(): with open('{}/search_saved_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def search_saved_name_response(): with open('{}/search_saved_name_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def search_saved_guid_response(): with open('{}/search_saved_guid_get.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def search_saved_update(): with open('{}/search_saved_update.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return(response) @pytest.fixture(scope='class') def admin_metrics_response(): with open('{}/admin_metrics.json'.format(resource_filename(__name__, RESPONSE_JSON_DIR))) as json_data: response = json.load(json_data) return response class TestEntityREST(): def test_entity_post(self, mocker, atlas_client, entity_guid_response, entity_post_response): mocker.patch.object(atlas_client.client, 'get') atlas_client.client.get.return_value = entity_guid_response mocker.patch.object(atlas_client.entity_post.client, 'post') atlas_client.entity_post.client.post.return_value = entity_post_response atlas_client.entity_post.create(data=entity_guid_response) atlas_client.entity_post.client.post.assert_called_with(atlas_client.entity_post.url, data=entity_guid_response) def test_entity_bulk_get(self, mocker, atlas_client, entity_bulk_response): mocker.patch.object(atlas_client.client, 'get') atlas_client.entity_bulk.client.get.return_value = entity_bulk_response params = {'guid': [GUID, '92b3a92b-d98c-4613-ae6e-1a9d0b4f344b']} bulk_collection = atlas_client.entity_bulk(**params) for bulk in bulk_collection: atlas_client.entity_bulk.client.get.assert_called_with(bulk_collection.url, params=params) for entity in bulk.entities: assert entity.version == 12345 def test_entity_bulk_get_with_relationships(self, mocker, atlas_client, entity_bulk_response): mocker.patch.object(atlas_client.client, 'get') atlas_client.entity_bulk.client.get.return_value = entity_bulk_response params = {'guid': [GUID, '92b3a92b-d98c-4613-ae6e-1a9d0b4f344b']} bulk_collection = atlas_client.entity_bulk(**params) for bulk in bulk_collection: atlas_client.entity_bulk.client.get.assert_called_with(bulk_collection.url, params=params) for entity in bulk.entities_with_relationships(): assert entity.version == 12345 def test_entity_bulk_delete(self, mocker, atlas_client, entity_bulk_response): mocker.patch.object(atlas_client.client, 'get') atlas_client.entity_bulk.client.get.return_value = entity_bulk_response mocker.patch.object(atlas_client.client, 'delete') atlas_client.entity_bulk.client.delete.return_value = entity_bulk_response params = {'guid': [GUID, '92b3a92b-d98c-4613-ae6e-1a9d0b4f344b']} bulk_collection = atlas_client.entity_bulk(**params) for bulk in bulk_collection: bulk.delete(**params) atlas_client.entity_bulk.client.delete.assert_called_with(bulk_collection.url, params=params) def test_entity_bulk_create(self, mocker, atlas_client, entity_bulk_response): mocker.patch.object(atlas_client.client, 'get') atlas_client.entity_bulk.client.get.return_value = entity_bulk_response mocker.patch.object(atlas_client.client, 'post') atlas_client.entity_bulk.client.post.return_value = entity_bulk_response params = {'guid': [GUID, '92b3a92b-d98c-4613-ae6e-1a9d0b4f344b']} bulk_collection = atlas_client.entity_bulk(**params) for bulk in bulk_collection: bulk.create() atlas_client.entity_bulk.client.post.assert_called_with(bulk_collection.url, data=bulk._data) def test_entity_bulk_classification_create(self, mocker, atlas_client, entity_bulk_classification_response): mocker.patch.object(atlas_client.entity_bulk_classification.client, 'post') atlas_client.entity_bulk_classification.client.post.return_value = entity_bulk_classification_response params = {'guid': [GUID, '92b3a92b-d98c-4613-ae6e-1a9d0b4f344b']} atlas_client.entity_bulk_classification.create(data=entity_bulk_classification_response) atlas_client.entity_bulk_classification.client.post.assert_called_with(atlas_client.entity_bulk_classification.url, data=entity_bulk_classification_response) def test_get_entity_by_guid(self, mocker, entity_guid_response, entity_guid): mocker.patch.object(entity_guid.client.client, 'request') entity_guid.client.client.request.return_value = entity_guid_response assert '/entity/guid/{}'.format(GUID) in entity_guid._href # Loading the data because before that no data were actually loaded (lazy loading) entity_guid.entity assert entity_guid._data['entity']['guid'] == GUID entity_guid.entity['status'] = 'ACTIVE' assert entity_guid._data['entity']['status'] == 'ACTIVE' def test_update_entity_by_guid(self, mocker, entity_guid_response, entity_guid): mocker.patch.object(entity_guid.client, 'request') entity_guid.client.request.return_value = entity_guid_response mocker.patch.object(entity_guid.client, 'put') attribute = 'description' entity_guid.update(attribute=attribute) entity_guid.client.put.assert_called_with(entity_guid._href + '?name={}'.format(attribute), data=entity_guid.entity['attributes'][attribute]) def test_create_entity_by_guid(self, mocker, entity_guid_response, entity_guid): mocker.patch.object(entity_guid.client, 'post') entity_guid.create() entity_guid.client.post.assert_called_with(entit", https://github.com/vilkasgroup/Verifone,5509c08767a9b482cfe5a9572854cf4c73ad6c02,tests/test_verifone.py::TestVerifone::test_001_create_object_with_defaults,tests/test_verifone.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Tests for `verifone` package."""""" import unittest import logging import os from datetime import datetime from verifone import verifone try: import http.client as http_client except ImportError: import httplib as http_client # logging http_client.HTTPConnection.debuglevel = 1 logging.basicConfig() logging.getLogger().setLevel(logging.DEBUG) requests_log = logging.getLogger(""requests.packages.urllib3"") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True class TestVerifone(unittest.TestCase): """""" Test for Verifone package. """""" @classmethod def setUpClass(cls): """""" Set up our Verifone client for tests. It requires the following environment variables: AGREEMENTCODE, RSAPRIVATEKEY and EMAIL """""" cls._verifone_client = verifone.Verifone(os.environ.get('AGREEMENTCODE'), os.environ.get('RSAPRIVATEKEY'), os.environ.get('RSAVERIFONEPUBLICKEY'), ""IntegrationTest"", ""6.0.37"") cls._test_requests = os.environ.get('TESTSENDINGREQUEST') cls._verifone_client_2 = verifone.Verifone(os.environ.get('AGREEMENTCODE'), os.environ.get('RSAPRIVATEKEY'), os.environ.get('RSAVERIFONEPUBLICKEY'), ""IntegrationTest"", ""6.0.37"", return_error_dict=1) def test_001_create_object_with_defaults(self): """""" Test creating a new object with default values """""" self.assertTrue(self._verifone_client._currency == ""EUR"") self.assertTrue(self._verifone_client._test_mode == 0) def test_002_get_endpoint(self): """""" Test to get endpoint url and change to test mode """""" self.assertEqual(self._verifone_client.endpoint, 'https://epayment1.point.fi/pw/serverinterface') self._verifone_client._test_mode = 1 self.assertEqual(self._verifone_client.endpoint, 'https://epayment.test.point.fi/pw/serverinterface') self.assertTrue(self._verifone_client._test_mode == 1) def test_003_create_object_wrong_currency(self): """""" Test creating a new object with wrong currency, so default currency should be used """""" default_currency = 'EUR' verifone_cl = verifone.Verifone('test_apikey', '1234', 'Test', 'IntegrationTest', '6.0.37', 'euro') self.assertEqual(verifone_cl._currency, default_currency) verifone_cl = verifone.Verifone('test_apikey', '1234', 'Test', 'IntegrationTest', '6.0.37', 'eu1') self.assertEqual(verifone_cl._currency, default_currency) verifone_cl = verifone.Verifone('test_apikey', '1234', 'Test', 'IntegrationTest', '6.0.37', '€') self.assertEqual(verifone_cl._currency, default_currency) verifone_cl = verifone.Verifone('test_apikey', '1234', 'Test', 'IntegrationTest', '6.0.37', 'abc') self.assertNotEqual(verifone_cl._currency, default_currency) def test_004_create_object_currency_lower(self): """""" Test creating a new object with currency in lower case """""" verifone_cl = verifone.Verifone('test_apikey', '1234', 'Test', 'IntegrationTest', '6.0.37', 'eur') self.assertEqual(verifone_cl._currency, 'EUR') def test_005_update_currency(self): """""" Test that currency is updated """""" new_value = ""SEK"" self._verifone_client.currency = new_value self.assertEqual(self._verifone_client._currency, new_value) with self.assertRaises(ValueError): self._verifone_client.currency = ""Euro"" with self.assertRaises(ValueError): self._verifone_client.currency = """" with self.assertRaises(ValueError): self._verifone_client.currency = ""kr3"" with self.assertRaises(ValueError): self._verifone_client.currency = ""€"" new_value = ""abc"" self._verifone_client.currency = new_value self.assertEqual(self._verifone_client._currency, new_value.upper()) self._verifone_client._currency = ""abc"" with self.assertRaises(ValueError): self._verifone_client.currency new_value = ""eur"" self._verifone_client.currency = new_value self.assertEqual(self._verifone_client._currency, new_value.upper()) self.assertEqual(self._verifone_client.currency, '978') def test_006_is_available(self): """""" Test connection to Verifone server """""" if (self._test_requests == ""1""): response = self._verifone_client.is_available() self.assertTrue(response['i-f-1-1_availability'] == '2') self._verifone_client._test_mode = 1 response = self._verifone_client.is_available() self.assertTrue(response['i-f-1-1_availability'] == '2') def test_007_get_payment_methods(self): """""" Test to get all available payment methods """""" if (self._test_requests == ""1""): response = self._verifone_client.list_payment_methods() self.assertIsNotNone(response['s-t-1-30_payment-method-code-0']) def test_008_list_saved_payment_methods(self): """""" Test to get saved payment methods """""" if (self._test_requests == ""1""): params = { 's-f-1-30_buyer-first-name': 'Test', 's-f-1-30_buyer-last-name': 'Tester', 's-f-1-100_buyer-email-address': os.environ.get('EMAIL'), 's-t-1-30_buyer-phone-number': '123456789', 's-t-1-255_buyer-external-id': os.environ.get('EXTERNALID'), } response = self._verifone_client.list_saved_payment_methods(params) self.assertTrue('l-t-1-20_payment-method-id-0' in response) def test_009_remove_saved_payment_method(self): """""" Test to remove saved payment method when saved payment method is wrong """""" if (self._test_requests == ""1""): response = self._verifone_client.remove_saved_payment_method(123456) self.assertEqual(response['l-t-1-10_removed-count'], '0') def test_010_generate_payment_data(self): """""" Test to generate payment data """""" customer_id = '1234567890asdfghjklp-1234567890zxcvbnmklo' note = ""Note"" params = { 'order_number': '58459', 'order_timestamp': '2018-08-02 09:14:12', 'payment_timestamp': '2018-08-02 11:59:16', 'locale': 'fi_FI', 'amount_gross': 1.51, 'amount_net': 1.22, 'vat_amount': 0.29, 'first_name': 'Test', 'last_name': 'Tester', 'email': 'test@test.test', 'phone': '1212121212121212', 'address': 'Test Street 4', 'postal_code': 33200, 'city': 'Tampere', 'country': 'fi', 'style': '', 'cancel_url': 'https://cancel.url', 'error_url': 'https://error.url', 'expired_url': 'https://expired.url', 'rejected_url': 'https://rejected.url', 'success_url': 'https://success.url', 'success_url_server': 'https://server.success.url', 'save_method': 3, 'payment_method': 'nordea-e-payment', 'customer_id': customer_id, 'note': note, 'products': [ { 'name': 'er_7142303001', 'pieces': 1, 'discount': 0, 'vat': 24.00, 'amount_gross': 1.51, 'amount_net': 1.22, 'unit_cost_gross': 1.51, }, ], 'dynamic_feedback': 's-t-1-4_error-code,i-t-6-6_card-pan-first6,i-t-4-4_card-pan-last4', } data = self._verifone_client.generate_payment_data(params) self.assertTrue('s-t-256-256_signature-one' in data) self.assertIsNotNone(data['s-t-256-256_signature-one']) self.assertTrue('s-t-256-256_signature-two' in data) self.assertIsNotNone(data['s-t-256-256_signature-two']) self.assertEqual(data['l-f-1-20_order-gross-amount'], 151) self.assertEqual(data['l-f-1-20_order-net-amount'], 122) self.assertEqual(data['l-f-1-20_order-vat-amount'], 29) self.assertEqual(data['s-t-1-255_buyer-external-id'], customer_id) self.assertEqual(data['s-t-1-36_order-note'], note) self.assertIsNotNone(data['s-t-1-1024_dynamic-feedback']) self.assertEqual(data['s-t-1-30_bi-name-0'], 'er_7142303001') self.assertEqual(len(data['s-t-1-30_bi-name-0']), 13) def test_011_generate_payment_data(self): """""" Test to generate payment data when all data is not defined """""" params = { 'order_number': '58459', 'locale': 'fi_FI', 'first_name': 'Test', 'last_name': 'Tester', 'email': 'test@test.test', 'cancel_url': 'https://cancel.url', 'error_url': 'https://error.url', 'expired_url': 'https://expired.url', 'rejected_url': 'https://rejected.url', 'success_url': 'https://success.url', 'success_url_server': 'https://server.success.url', 'skip_confirmation': 1, 'country': '246', 'products': [ { 'name': 'er_7142303001', 'pieces': 1, 'vat': 24.00, }, ] } data = self._verifone_client.generate_payment_data(params) self.assertTrue('s-t-1-30_style-code' in data) self.assertTrue('i-t-1-1_skip-confirmation-page' in data) self.assertEqual(data['i-t-1-1_skip-confirmation-page'], 1) self.assertEqual(data['i-t-1-3_delivery-address-country-code'], '246') def test_012_generate_payment_link(self): """""" Test to generate payment link """""" if (self._test_requests == ""1""): params = { 'locale-f-2-5_payment-locale': 'fi_FI', 't-f-14-19_order-expiry-timestamp': '2018-10-02 09:14:12', ", https://github.com/vitalk/flask-apify,f486d2616575be0e460ccf354d22badb7d68b387,tests/test_apify.py::test_apify_init,tests/test_apify.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import pytest import logging from flask import url_for from flask_apify.fy import ( catch_errors, guess_best_mimetype, set_best_serializer ) from flask_apify.exc import ( ApiError, ApiUnauthorized, ApiNotAcceptable ) from flask_apify.serializers import ( get_default_serializer, get_serializer, to_javascript, to_json, to_html ) from .conftest import accept_mimetypes PY2 = sys.version_info[0] == 2 if PY2: text_type = unicode else: text_type = str def b(s): return s.encode('utf-8') if isinstance(s, text_type) else s def test_apify_init(app, apify): assert 'apify' in app.extensions assert apify.blueprint is not None assert apify.logger is logging.getLogger('flask-apify') assert apify.preprocessor_funcs == [set_best_serializer,] assert apify.postprocessor_funcs == [] assert apify.finalizer_funcs == [] assert apify.serializers['text/html'] is to_html assert apify.serializers['application/json'] is to_json assert apify.serializers['application/javascript'] is to_javascript assert apify.serializers['application/json-p'] is to_javascript assert apify.serializers['text/json-p'] is to_javascript def test_apify_does_not_require_app_object_while_instantiated(client, accept_mimetypes): res = client.get(url_for('api.ping'), headers=accept_mimetypes) assert res.status_code == 200 def test_apify_register_serializer_for_mimetype(app, apify): @apify.serializer('application/xml') def to_xml(x): return x mimetype, serializer = get_serializer('application/xml') assert mimetype == 'application/xml' assert serializer is to_xml def test_apify_get_serializer(app, mimetype): mime, fn = get_serializer(mimetype) assert mime == mimetype assert callable(fn) def test_apify_get_serializer_may_raise_error(app): with pytest.raises(ApiNotAcceptable): get_serializer('nosuch/mimetype') def test_apify_default_response_mimetype_is_application_javascript(app): mimetype, fn = get_default_serializer() assert mimetype == 'application/javascript' assert callable(fn) def test_apify_get_default_serializer_may_raise_error_if_nosuch_serializer(app): app.config['APIFY_DEFAULT_MIMETYPE'] = 'nosuch/mimetype' with pytest.raises(RuntimeError): get_default_serializer() def test_apify_call_require_explicit_mimetype(app, client): res = client.get(url_for('api.ping')) assert res.status_code == 406 assert res.mimetype == 'application/javascript' def test_apify_handle_custom_errors(client, accept_mimetypes): res = client.get(url_for('api.error'), headers=accept_mimetypes) assert res.status_code == 418 assert b('This server is a teapot, not a coffee machine') in res.data def test_apify_handle_http_exceptions(client, accept_mimetypes): res = client.get(url_for('api.forbidden'), headers=accept_mimetypes) assert res.status_code == 403 assert b('the permission to access the requested resource') in res.data def test_returns_server_error_if_exception_has_no_status_code(client, accept_mimetypes): res = client.get(url_for('api.bomb'), headers=accept_mimetypes) assert res.status_code == 500 assert b('boom!') in res.data def test_apify_allow_apply_route_decorator_multiple_times(app, client, accept_json): res = client.get(url_for('api.ping'), headers=accept_json) assert res.status_code == 200 assert res.json == {'value': 200} res = client.get(url_for('api.ping', value=404), headers=accept_json) assert res.status_code == 200 assert res.json == {'value': 404} @pytest.fixture def stdout(): import sys if sys.version_info[0] == 2: from cStringIO import StringIO else: from io import StringIO return StringIO() @pytest.fixture def app_logger(apify, stdout): apify.logger.level = logging.ERROR apify.logger.addHandler(logging.StreamHandler(stdout)) @pytest.mark.usefixtures('app_logger') class TestLogging(object): def test_log_exception(self, app, apify, stdout): with app.test_request_context('/foo'): try: 1 // 0 except ZeroDivisionError as exc: apify.log_exception(exc) err = stdout.getvalue() # Request path and method assert 'Exception on GET /foo' in err # Exception traceback assert 'Traceback (most recent call last):' in err # Erroneous expression assert '1 // 0' in err # Exception itself assert 'ZeroDivisionError:' in err def test_http_exception_logging(self, apify, client, stdout, accept_any): apify.logger.level = logging.INFO client.get(url_for('api.forbidden'), headers=accept_any) err = stdout.getvalue() assert 'Exception on GET /forbidden' in err assert '403: Forbidden' in err def test_exception_logging(self, client, stdout, accept_any): res = client.get(url_for('api.bomb'), headers=accept_any) err = stdout.getvalue() assert 'Exception on GET /bomb' in err assert 'Bomb:' in err class TestCatchErrorsDecorator(object): def test_catch_error(self): @catch_errors(ValueError, errorhandler=lambda x: x) def raise_error(value): raise ValueError assert raise_error('What is the meaning of the Life?') def test_may_catch_multiple_errors(self): @catch_errors((ValueError, ZeroDivisionError), lambda x: x) def raise_error(value): if value == 0: raise ZeroDivisionError raise ValueError assert raise_error(0) assert raise_error(42) def test_exec_errorhandler(self): @catch_errors(ValueError, errorhandler=lambda x: 42) def raise_error(value): raise ValueError assert raise_error('What is the meaning of the Life?') == 42 class TestMimetypeDetection(object): def test_support_wildcards(self, app, accept_any): with app.test_request_context(headers=accept_any): assert guess_best_mimetype() == 'application/javascript' @pytest.mark.options(apify_default_mimetype='text/xml') def test_returns_default_mimetype_if_client_may_accept_any_mimetype(self, app, accept_any): with app.test_request_context(headers=accept_any): assert guess_best_mimetype() == 'text/xml' @pytest.mark.options(apify_default_mimetype='application/xml') def test_wildcard_in_subtype(self, app): accept_headers = accept_mimetypes('application/*; q=0.1,' 'application/json; q=1,' 'application/javascript') with app.test_request_context(headers=accept_headers): assert guess_best_mimetype() == 'application/xml' def test_select_mimetype_with_better_quality_when_multiple_choices(self, app): accept_headers = accept_mimetypes('application/javascript; q=0.9,' 'application/json; q=1') with app.test_request_context(headers=accept_headers): assert guess_best_mimetype() == 'application/json' def test_invalid_accept_header(self, app): with app.test_request_context(headers=accept_mimetypes('*/json')): assert guess_best_mimetype() is None def test_not_acceptable_mimetype(self, app): with app.test_request_context(headers=accept_mimetypes('text/xml')): assert guess_best_mimetype() is None def test_respect_available_mimetypes(self, app, apify): @apify.serializer('text/xml') def to_xml(x): return x with app.test_request_context(headers=accept_mimetypes('text/xml')): assert guess_best_mimetype() == 'text/xml' def test_apify_add_preprocessor(apify): @apify.preprocessor def my_preprocessor(): pass assert my_preprocessor in apify.preprocessor_funcs def test_default_function_argument_in_preprocessor(apify): @apify.preprocessor() def my_preprocessor(): pass assert my_preprocessor in apify.preprocessor_funcs def test_apify_add_function_to_set_best_serializer_as_default_preprocessor(apify): assert set_best_serializer in apify.preprocessor_funcs def test_apify_exec_preprocessors(apify, client, accept_mimetypes): @apify.preprocessor def login_required(fn): raise ApiUnauthorized() res = client.get(url_for('api.ping'), headers=accept_mimetypes) assert res.status_code == 401 assert b('The server could not verify that you are ' 'authorized to access the requested URL.') in res.data def test_view_callable_may_rewrite_response_object(client, accept_mimetypes): res = client.get(url_for('api.rewrite_response'), headers=accept_mimetypes) assert res.mimetype == 'custom/mimetype' assert res.data == b('response has been rewritten') def test_preprocessor_may_rewrite_view_response(app, apify, client, accept_mimetypes): @apify.preprocessor def rewrite_response(fn): def wrapper(): return app.response_class('response has been rewritten', mimetype='custom/mimetype') return wrapper res = client.get(url_for('api.ping'), headers=accept_mimetypes) assert res.mimetype == 'custom/mimetype' assert res.data == b('response has been rewritten') def test_apify_register_postprocessor(apify): @apify.postprocessor def my_postprocessor(): pass assert my_postprocessor in apify.postprocessor_funcs def test_default_function_argument_in_postprocessor(apify): @apify.postprocessor() def my_postprocessor(): pass assert my_postprocessor in apify.postprocessor_funcs def test_apify_exec_postprocessor(apify, client, accept", https://github.com/vitalk/flask-apify,f486d2616575be0e460ccf354d22badb7d68b387,tests/test_serializers.py::TestJSONPSerializer::test_support_custom_callback_name,tests/test_serializers.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- import pytest from flask_apify.serializers import Serializer from flask_apify.serializers.debug import DebugSerializer from flask_apify.serializers.json import JSONSerializer from flask_apify.serializers.jsonp import JSONPSerializer from flask_apify.serializers.jsonp import jsonp class TestSerializer(object): def setup(self): self.serializer = Serializer() def test_base_class_raises_not_implementent_error_on_call(self): with pytest.raises(NotImplementedError): self.serializer({}) class TestDebugSerializer(object): def setup(self): self.serializer = DebugSerializer() def test_dump(self, app): assert self.serializer(42) == '
42
' class TestJSONSerializer(object): def setup(self): self.serializer = JSONSerializer() def test_dump(self): assert self.serializer({'ping': 'pong'}) == '{""ping"": ""pong""}' class TestJSONPSerializer(object): def setup(self): self.serializer = JSONPSerializer() def test_add_padding_to_string(self): assert jsonp('{""ping"": ""pong""}', 'console.log') == \ 'console.log({""ping"": ""pong""});' def test_returns_string_as_is_if_no_padding(self): assert jsonp('hello') == 'hello' def test_use_previously_registered_serializer_to_dump_json(self, app, apify): @apify.serializer('application/json') def my_json(raw): return '42' assert self.serializer('What is the meaning of the Life?') == '42' def test_use_callback_function_from_request_arguments_to_wrap_output(self, app): with app.test_request_context('?callback=console.log'): assert self.serializer('42') == 'console.log(42);' def test_support_custom_callback_name(self, app): serializer = JSONPSerializer(callback_name='jsonp') with app.test_request_context('?jsonp=console.log'): assert serializer('42') == 'console.log(42);' ", https://github.com/vitalk/flask-apify,f486d2616575be0e460ccf354d22badb7d68b387,tests/test_serializers.py::TestJSONPSerializer::test_use_callback_function_from_request_arguments_to_wrap_output,tests/test_serializers.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- import pytest from flask_apify.serializers import Serializer from flask_apify.serializers.debug import DebugSerializer from flask_apify.serializers.json import JSONSerializer from flask_apify.serializers.jsonp import JSONPSerializer from flask_apify.serializers.jsonp import jsonp class TestSerializer(object): def setup(self): self.serializer = Serializer() def test_base_class_raises_not_implementent_error_on_call(self): with pytest.raises(NotImplementedError): self.serializer({}) class TestDebugSerializer(object): def setup(self): self.serializer = DebugSerializer() def test_dump(self, app): assert self.serializer(42) == '
42
' class TestJSONSerializer(object): def setup(self): self.serializer = JSONSerializer() def test_dump(self): assert self.serializer({'ping': 'pong'}) == '{""ping"": ""pong""}' class TestJSONPSerializer(object): def setup(self): self.serializer = JSONPSerializer() def test_add_padding_to_string(self): assert jsonp('{""ping"": ""pong""}', 'console.log') == \ 'console.log({""ping"": ""pong""});' def test_returns_string_as_is_if_no_padding(self): assert jsonp('hello') == 'hello' def test_use_previously_registered_serializer_to_dump_json(self, app, apify): @apify.serializer('application/json') def my_json(raw): return '42' assert self.serializer('What is the meaning of the Life?') == '42' def test_use_callback_function_from_request_arguments_to_wrap_output(self, app): with app.test_request_context('?callback=console.log'): assert self.serializer('42') == 'console.log(42);' def test_support_custom_callback_name(self, app): serializer = JSONPSerializer(callback_name='jsonp') with app.test_request_context('?jsonp=console.log'): assert serializer('42') == 'console.log(42);' ", https://github.com/vkbo/novelWriter,559f2e53e410f9793ab08c2ef7ed308f75c92985,tests/test_project.py::testProjectNewCustom,tests/test_project.py,NIO,flaky,DeveloperFixed,nan,classify;root_cause,"# -*- coding: utf-8 -*- """"""novelWriter Project Class Tester """""" import nw import pytest from os import path from nwtools import * from nwdummy import DummyMain from nw.config import Config from nw.core.project import NWProject from nw.core.index import NWIndex from nw.constants import nwItemClass theConf = Config() theMain = DummyMain() theMain.mainConf = theConf theProject = NWProject(theMain) theProject.projTree.setSeed(42) @pytest.mark.project def testProjectNewMinimal(nwTempProj, nwRef, nwTemp): projFile = path.join(nwTempProj,""nwProject.nwx"") refFile = path.join(nwRef,""proj"", ""1_nwProject.nwx"") assert theConf.initConfig(nwRef, nwTemp) assert theProject.newProject({""projPath"": nwTempProj}) assert theProject.setProjectPath(nwTempProj) assert theProject.saveProject() assert theProject.closeProject() assert cmpFiles(projFile, refFile, [2, 6, 7, 8]) @pytest.mark.project def testProjectOpen(nwTempProj): projFile = path.join(nwTempProj,""nwProject.nwx"") assert theProject.openProject(projFile) @pytest.mark.project def testProjectSave(nwTempProj,nwRef): projFile = path.join(nwTempProj,""nwProject.nwx"") refFile = path.join(nwRef,""proj"",""1_nwProject.nwx"") assert theProject.saveProject() assert theProject.closeProject() assert cmpFiles(projFile, refFile, [2, 6, 7, 8]) assert not theProject.projChanged @pytest.mark.project def testProjectOpenTwice(nwTempProj,nwRef): projFile = path.join(nwTempProj,""nwProject.nwx"") refFile = path.join(nwRef,""proj"",""1_nwProject.nwx"") assert theProject.openProject(projFile) assert not theProject.openProject(projFile) assert theProject.openProject(projFile, overrideLock=True) assert theProject.saveProject() assert theProject.closeProject() assert cmpFiles(projFile, refFile, [2, 6, 7, 8]) @pytest.mark.project def testProjectNewRoot(nwTempProj,nwRef): projFile = path.join(nwTempProj,""nwProject.nwx"") refFile = path.join(nwRef,""proj"",""2_nwProject.nwx"") assert theProject.openProject(projFile) assert isinstance(theProject.newRoot(""Novel"", nwItemClass.NOVEL), type(None)) assert isinstance(theProject.newRoot(""Plot"", nwItemClass.PLOT), type(None)) assert isinstance(theProject.newRoot(""Character"", nwItemClass.CHARACTER), type(None)) assert isinstance(theProject.newRoot(""World"", nwItemClass.WORLD), type(None)) assert isinstance(theProject.newRoot(""Timeline"", nwItemClass.TIMELINE), str) assert isinstance(theProject.newRoot(""Object"", nwItemClass.OBJECT), str) assert isinstance(theProject.newRoot(""Custom1"", nwItemClass.CUSTOM), str) assert isinstance(theProject.newRoot(""Custom2"", nwItemClass.CUSTOM), str) assert theProject.projChanged assert theProject.saveProject() assert theProject.closeProject() assert cmpFiles(projFile, refFile, [2, 6, 7, 8]) assert not theProject.projChanged @pytest.mark.project def testProjectNewFile(nwTempProj,nwRef): projFile = path.join(nwTempProj,""nwProject.nwx"") refFile = path.join(nwRef,""proj"",""3_nwProject.nwx"") assert theProject.openProject(projFile) assert isinstance(theProject.newFile(""Hello"", nwItemClass.NOVEL, ""73475cb40a568""), str) assert isinstance(theProject.newFile(""Jane"", nwItemClass.CHARACTER, ""71ee45a3c0db9""), str) assert theProject.projChanged assert theProject.saveProject() assert theProject.closeProject() assert cmpFiles(projFile, refFile, [2, 6, 7, 8]) assert not theProject.projChanged @pytest.mark.project def testIndexScanThis(nwTempProj): projFile = path.join(nwTempProj,""nwProject.nwx"") assert theProject.openProject(projFile) theIndex = NWIndex(theProject, theMain) tHandle = ""31489056e0916"" isValid, theBits, thePos = theIndex.scanThis(""tag: this, and this"") assert not isValid isValid, theBits, thePos = theIndex.scanThis(""@"") assert not isValid isValid, theBits, thePos = theIndex.scanThis(""@:"") assert not isValid isValid, theBits, thePos = theIndex.scanThis("" @a: b"") assert not isValid isValid, theBits, thePos = theIndex.scanThis(""@a:"") assert isValid assert str(theBits) == ""['@a']"" assert str(thePos) == ""[0]"" isValid, theBits, thePos = theIndex.scanThis(""@a:b"") assert isValid assert str(theBits) == ""['@a', 'b']"" assert str(thePos) == ""[0, 3]"" isValid, theBits, thePos = theIndex.scanThis(""@a:b,c,d"") assert isValid assert str(theBits) == ""['@a', 'b', 'c', 'd']"" assert str(thePos) == ""[0, 3, 5, 7]"" isValid, theBits, thePos = theIndex.scanThis(""@a : b , c , d"") assert isValid assert str(theBits) == ""['@a', 'b', 'c', 'd']"" assert str(thePos) == ""[0, 5, 9, 13]"" isValid, theBits, thePos = theIndex.scanThis(""@tag: this, and this"") assert isValid assert str(theBits) == ""['@tag', 'this', 'and this']"" assert str(thePos) == ""[0, 6, 12]"" assert theProject.closeProject() @pytest.mark.project def testIndexCheckThese(nwTempProj): projFile = path.join(nwTempProj,""nwProject.nwx"") assert theProject.openProject(projFile) theIndex = NWIndex(theProject, theMain) nHandle = ""0e17daca5f3e1"" nItem = theProject.projTree[nHandle] cHandle = ""02d20bbd7e394"" cItem = theProject.projTree[cHandle] assert theIndex.scanText(cHandle, ( ""# Jane Smith\n"" ""@tag: Jane"" )) assert theIndex.scanText(nHandle, ( ""# Hello World!\n"" ""@pov: Jane"" )) assert str(theIndex.tagIndex) == ""{'Jane': [2, '02d20bbd7e394', 'CHARACTER', 'T000001']}"" assert theIndex.novelIndex[nHandle][""T000001""][""title""] == ""Hello World!"" assert str(theIndex.checkThese([""@tag"", ""Jane""], cItem)) == ""[True, True]"" assert str(theIndex.checkThese([""@tag"", ""John""], cItem)) == ""[True, True]"" assert str(theIndex.checkThese([""@tag"", ""Jane""], nItem)) == ""[True, False]"" assert str(theIndex.checkThese([""@tag"", ""John""], nItem)) == ""[True, True]"" assert str(theIndex.checkThese([""@pov"", ""John""], nItem)) == ""[True, False]"" assert str(theIndex.checkThese([""@pov"", ""Jane""], nItem)) == ""[True, True]"" assert str(theIndex.checkThese([""@ pov"", ""Jane""], nItem)) == ""[False, False]"" assert str(theIndex.checkThese([""@what"", ""Jane""], nItem)) == ""[False, False]"" assert theProject.closeProject() @pytest.mark.project def testIndexMeta(nwTempProj): projFile = path.join(nwTempProj,""nwProject.nwx"") assert theProject.openProject(projFile) theIndex = NWIndex(theProject, theMain) nHandle = ""0e17daca5f3e1"" nItem = theProject.projTree[nHandle] cHandle = ""02d20bbd7e394"" cItem = theProject.projTree[cHandle] assert theIndex.scanText(cHandle, ( ""# Jane Smith\n"" ""@tag: Jane\n"" )) assert theIndex.scanText(nHandle, ( ""# Hello World!\n"" ""@pov: Jane\n"" ""@char: Jane\n"" ""\n"" ""% this is a comment\n"" ""\n"" ""This is a story about Jane Smith.\n"" ""\n"" ""Well, not really.\n"" )) assert str(theIndex.tagIndex) == ""{'Jane': [2, '02d20bbd7e394', 'CHARACTER', 'T000001']}"" assert theIndex.novelIndex[nHandle][""T000001""][""title""] == ""Hello World!"" # The novel structure should contain the pointer to the novel file header assert str(theIndex.getNovelStructure()) == ""['0e17daca5f3e1:T000001']"" # The novel file should have the correct counts cC, wC, pC = theIndex.getCounts(nHandle) assert cC == 62 # Characters in text and title only assert wC == 12 # Words in text and title only assert pC == 2 # Paragraphs in text only # The novel file should now refer to Jane as @pov and @char theRefs = theIndex.getReferences(nHandle) assert str(theRefs[""@pov""]) == ""['Jane']"" assert str(theRefs[""@char""]) == ""['Jane']"" # The character file should have a record of the reference from the novel file theRefs = theIndex.getBackReferenceList(cHandle) assert str(theRefs) == ""{'0e17daca5f3e1': 'T000001'}"" assert theProject.closeProject() # The two following tests must be at the end as they mess up the config object # and the handle seed. They go into their own folders, but use the same project # object as the test above. @pytest.mark.project def testProjectNewCustom(nwTempCustom, nwRef, nwTemp): projData = { ""projName"": ""Test Custom"", ""projTitle"": ""Test Novel"", ""projAuthors"": ""Jane Doe\nJohn Doh\n"", ""projPath"": nwTempCustom, ""popSample"": False, ""popMinimal"": False, ""popCustom"": True, ""addRoots"": [ nwItemClass.PLOT, nwItemClass.CHARACTER, nwItemClass.WORLD, nwItemClass.TIMELINE, nwItemClass.OBJECT, nwItemClass.ENTITY, ], ""numChapters"": 3, ""numScenes"": 3, ""chFolders"": True, } theProject.mainConf = theConf theProject.projTree.setSeed(42) assert theProject.newProject(projData) assert theProject.saveProject() assert theProject.closeProject() projFile = path.join(nwTempCustom, ""nwProject.nwx"") refFile = path.join(nwRef, ""proj"", ""4_nwProject.nwx"") assert cmpFiles(projFile, refFile, [2, 6, 7, 8]) @pytest.mark.project def testProjectNewSample(nwTempSample, nwLipsum, nwRef, nwTemp): projData = { ""projName"": ""Test Sample"", ""projTitle"": ""Test Novel"", ""projAuthors"": ""Jane Doe\nJohn Doh\n"", ""projPath"": nwTempSample, ""popSample"": True, ""popMinimal"": False, ""popCustom"": False, } theProject.mainConf = theConf assert theProject.newProject(projData) assert theProject.openProject(nwTempSample) assert theProject.projName == ""Sample Project"" assert theProject.saveProject() assert theProject.closeProject() ", https://github.com/vkbo/novelWriter,559f2e53e410f9793ab08c2ef7ed308f75c92985,tests/test_project.py::testProjectNewMinimal,tests/test_project.py,NIO,flaky,DeveloperFixed,nan,classify;root_cause,"# -*- coding: utf-8 -*- """"""novelWriter Project Class Tester """""" import nw import pytest from os import path from nwtools import * from nwdummy import DummyMain from nw.config import Config from nw.core.project import NWProject from nw.core.index import NWIndex from nw.constants import nwItemClass theConf = Config() theMain = DummyMain() theMain.mainConf = theConf theProject = NWProject(theMain) theProject.projTree.setSeed(42) @pytest.mark.project def testProjectNewMinimal(nwTempProj, nwRef, nwTemp): projFile = path.join(nwTempProj,""nwProject.nwx"") refFile = path.join(nwRef,""proj"", ""1_nwProject.nwx"") assert theConf.initConfig(nwRef, nwTemp) assert theProject.newProject({""projPath"": nwTempProj}) assert theProject.setProjectPath(nwTempProj) assert theProject.saveProject() assert theProject.closeProject() assert cmpFiles(projFile, refFile, [2, 6, 7, 8]) @pytest.mark.project def testProjectOpen(nwTempProj): projFile = path.join(nwTempProj,""nwProject.nwx"") assert theProject.openProject(projFile) @pytest.mark.project def testProjectSave(nwTempProj,nwRef): projFile = path.join(nwTempProj,""nwProject.nwx"") refFile = path.join(nwRef,""proj"",""1_nwProject.nwx"") assert theProject.saveProject() assert theProject.closeProject() assert cmpFiles(projFile, refFile, [2, 6, 7, 8]) assert not theProject.projChanged @pytest.mark.project def testProjectOpenTwice(nwTempProj,nwRef): projFile = path.join(nwTempProj,""nwProject.nwx"") refFile = path.join(nwRef,""proj"",""1_nwProject.nwx"") assert theProject.openProject(projFile) assert not theProject.openProject(projFile) assert theProject.openProject(projFile, overrideLock=True) assert theProject.saveProject() assert theProject.closeProject() assert cmpFiles(projFile, refFile, [2, 6, 7, 8]) @pytest.mark.project def testProjectNewRoot(nwTempProj,nwRef): projFile = path.join(nwTempProj,""nwProject.nwx"") refFile = path.join(nwRef,""proj"",""2_nwProject.nwx"") assert theProject.openProject(projFile) assert isinstance(theProject.newRoot(""Novel"", nwItemClass.NOVEL), type(None)) assert isinstance(theProject.newRoot(""Plot"", nwItemClass.PLOT), type(None)) assert isinstance(theProject.newRoot(""Character"", nwItemClass.CHARACTER), type(None)) assert isinstance(theProject.newRoot(""World"", nwItemClass.WORLD), type(None)) assert isinstance(theProject.newRoot(""Timeline"", nwItemClass.TIMELINE), str) assert isinstance(theProject.newRoot(""Object"", nwItemClass.OBJECT), str) assert isinstance(theProject.newRoot(""Custom1"", nwItemClass.CUSTOM), str) assert isinstance(theProject.newRoot(""Custom2"", nwItemClass.CUSTOM), str) assert theProject.projChanged assert theProject.saveProject() assert theProject.closeProject() assert cmpFiles(projFile, refFile, [2, 6, 7, 8]) assert not theProject.projChanged @pytest.mark.project def testProjectNewFile(nwTempProj,nwRef): projFile = path.join(nwTempProj,""nwProject.nwx"") refFile = path.join(nwRef,""proj"",""3_nwProject.nwx"") assert theProject.openProject(projFile) assert isinstance(theProject.newFile(""Hello"", nwItemClass.NOVEL, ""73475cb40a568""), str) assert isinstance(theProject.newFile(""Jane"", nwItemClass.CHARACTER, ""71ee45a3c0db9""), str) assert theProject.projChanged assert theProject.saveProject() assert theProject.closeProject() assert cmpFiles(projFile, refFile, [2, 6, 7, 8]) assert not theProject.projChanged @pytest.mark.project def testIndexScanThis(nwTempProj): projFile = path.join(nwTempProj,""nwProject.nwx"") assert theProject.openProject(projFile) theIndex = NWIndex(theProject, theMain) tHandle = ""31489056e0916"" isValid, theBits, thePos = theIndex.scanThis(""tag: this, and this"") assert not isValid isValid, theBits, thePos = theIndex.scanThis(""@"") assert not isValid isValid, theBits, thePos = theIndex.scanThis(""@:"") assert not isValid isValid, theBits, thePos = theIndex.scanThis("" @a: b"") assert not isValid isValid, theBits, thePos = theIndex.scanThis(""@a:"") assert isValid assert str(theBits) == ""['@a']"" assert str(thePos) == ""[0]"" isValid, theBits, thePos = theIndex.scanThis(""@a:b"") assert isValid assert str(theBits) == ""['@a', 'b']"" assert str(thePos) == ""[0, 3]"" isValid, theBits, thePos = theIndex.scanThis(""@a:b,c,d"") assert isValid assert str(theBits) == ""['@a', 'b', 'c', 'd']"" assert str(thePos) == ""[0, 3, 5, 7]"" isValid, theBits, thePos = theIndex.scanThis(""@a : b , c , d"") assert isValid assert str(theBits) == ""['@a', 'b', 'c', 'd']"" assert str(thePos) == ""[0, 5, 9, 13]"" isValid, theBits, thePos = theIndex.scanThis(""@tag: this, and this"") assert isValid assert str(theBits) == ""['@tag', 'this', 'and this']"" assert str(thePos) == ""[0, 6, 12]"" assert theProject.closeProject() @pytest.mark.project def testIndexCheckThese(nwTempProj): projFile = path.join(nwTempProj,""nwProject.nwx"") assert theProject.openProject(projFile) theIndex = NWIndex(theProject, theMain) nHandle = ""0e17daca5f3e1"" nItem = theProject.projTree[nHandle] cHandle = ""02d20bbd7e394"" cItem = theProject.projTree[cHandle] assert theIndex.scanText(cHandle, ( ""# Jane Smith\n"" ""@tag: Jane"" )) assert theIndex.scanText(nHandle, ( ""# Hello World!\n"" ""@pov: Jane"" )) assert str(theIndex.tagIndex) == ""{'Jane': [2, '02d20bbd7e394', 'CHARACTER', 'T000001']}"" assert theIndex.novelIndex[nHandle][""T000001""][""title""] == ""Hello World!"" assert str(theIndex.checkThese([""@tag"", ""Jane""], cItem)) == ""[True, True]"" assert str(theIndex.checkThese([""@tag"", ""John""], cItem)) == ""[True, True]"" assert str(theIndex.checkThese([""@tag"", ""Jane""], nItem)) == ""[True, False]"" assert str(theIndex.checkThese([""@tag"", ""John""], nItem)) == ""[True, True]"" assert str(theIndex.checkThese([""@pov"", ""John""], nItem)) == ""[True, False]"" assert str(theIndex.checkThese([""@pov"", ""Jane""], nItem)) == ""[True, True]"" assert str(theIndex.checkThese([""@ pov"", ""Jane""], nItem)) == ""[False, False]"" assert str(theIndex.checkThese([""@what"", ""Jane""], nItem)) == ""[False, False]"" assert theProject.closeProject() @pytest.mark.project def testIndexMeta(nwTempProj): projFile = path.join(nwTempProj,""nwProject.nwx"") assert theProject.openProject(projFile) theIndex = NWIndex(theProject, theMain) nHandle = ""0e17daca5f3e1"" nItem = theProject.projTree[nHandle] cHandle = ""02d20bbd7e394"" cItem = theProject.projTree[cHandle] assert theIndex.scanText(cHandle, ( ""# Jane Smith\n"" ""@tag: Jane\n"" )) assert theIndex.scanText(nHandle, ( ""# Hello World!\n"" ""@pov: Jane\n"" ""@char: Jane\n"" ""\n"" ""% this is a comment\n"" ""\n"" ""This is a story about Jane Smith.\n"" ""\n"" ""Well, not really.\n"" )) assert str(theIndex.tagIndex) == ""{'Jane': [2, '02d20bbd7e394', 'CHARACTER', 'T000001']}"" assert theIndex.novelIndex[nHandle][""T000001""][""title""] == ""Hello World!"" # The novel structure should contain the pointer to the novel file header assert str(theIndex.getNovelStructure()) == ""['0e17daca5f3e1:T000001']"" # The novel file should have the correct counts cC, wC, pC = theIndex.getCounts(nHandle) assert cC == 62 # Characters in text and title only assert wC == 12 # Words in text and title only assert pC == 2 # Paragraphs in text only # The novel file should now refer to Jane as @pov and @char theRefs = theIndex.getReferences(nHandle) assert str(theRefs[""@pov""]) == ""['Jane']"" assert str(theRefs[""@char""]) == ""['Jane']"" # The character file should have a record of the reference from the novel file theRefs = theIndex.getBackReferenceList(cHandle) assert str(theRefs) == ""{'0e17daca5f3e1': 'T000001'}"" assert theProject.closeProject() # The two following tests must be at the end as they mess up the config object # and the handle seed. They go into their own folders, but use the same project # object as the test above. @pytest.mark.project def testProjectNewCustom(nwTempCustom, nwRef, nwTemp): projData = { ""projName"": ""Test Custom"", ""projTitle"": ""Test Novel"", ""projAuthors"": ""Jane Doe\nJohn Doh\n"", ""projPath"": nwTempCustom, ""popSample"": False, ""popMinimal"": False, ""popCustom"": True, ""addRoots"": [ nwItemClass.PLOT, nwItemClass.CHARACTER, nwItemClass.WORLD, nwItemClass.TIMELINE, nwItemClass.OBJECT, nwItemClass.ENTITY, ], ""numChapters"": 3, ""numScenes"": 3, ""chFolders"": True, } theProject.mainConf = theConf theProject.projTree.setSeed(42) assert theProject.newProject(projData) assert theProject.saveProject() assert theProject.closeProject() projFile = path.join(nwTempCustom, ""nwProject.nwx"") refFile = path.join(nwRef, ""proj"", ""4_nwProject.nwx"") assert cmpFiles(projFile, refFile, [2, 6, 7, 8]) @pytest.mark.project def testProjectNewSample(nwTempSample, nwLipsum, nwRef, nwTemp): projData = { ""projName"": ""Test Sample"", ""projTitle"": ""Test Novel"", ""projAuthors"": ""Jane Doe\nJohn Doh\n"", ""projPath"": nwTempSample, ""popSample"": True, ""popMinimal"": False, ""popCustom"": False, } theProject.mainConf = theConf assert theProject.newProject(projData) assert theProject.openProject(nwTempSample) assert theProject.projName == ""Sample Project"" assert theProject.saveProject() assert theProject.closeProject() ", https://github.com/Vnet-as/myr-base,56e347b4f4d70f53a6afe55e96c1f9e35f5454eb,tests/test_base.py::TestApp::test_announcing_no_tasks,tests/test_base.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from myr.base.app import ( MyrApp, announce, get_function_spec, ENV ) class TestApp: def test_basic(self): app = MyrApp() assert 'myr.base.app.announce' in app.tasks.regular() assert 'announce' in app.conf.beat_schedule def test_announcing_no_tasks(self, mocker): class Task: app = MyrApp() mocker.patch.object(Task.app, 'send_task') announce(Task) Task.app.send_task.assert_called_with( ENV.get('MYR_ANNOUNCE_TASK'), args=[{}], queue=ENV.get('MYR_ANNOUNCE_QUEUE')) def test_announcing_user_tasks(self, mocker): app = MyrApp() @app.task def test_task(a1, a2='2'): pass mocker.patch.object(app, 'send_task') announce(test_task) tasks_spec = { test_task.name: { 'signature': get_function_spec(test_task.run)._asdict(), 'routing': { 'queue': 'myr.tasks', 'routing_key': 'myr.tasks', 'exchange': '' } } } app.send_task.assert_called_with( ENV.get('MYR_ANNOUNCE_TASK'), args=[tasks_spec], queue=ENV.get('MYR_ANNOUNCE_QUEUE')) def test_custom_task_routing(self, mocker): app = MyrApp() @app.task def test_task(): pass app.conf.task_routes = { test_task.name: { 'queue': 'test_queue', 'exchange': 'test_exchange', 'routing_key': 'test_key' } } mocker.patch.object(app, 'send_task') announce(test_task) tasks_spec = { test_task.name: { 'signature': get_function_spec(test_task.run)._asdict(), 'routing': { 'queue': 'test_queue', 'exchange': 'test_exchange', 'routing_key': 'test_key' } } } app.send_task.assert_called_with( ENV.get('MYR_ANNOUNCE_TASK'), args=[tasks_spec], queue=ENV.get('MYR_ANNOUNCE_QUEUE')) ", https://github.com/voytekresearch/fooof,7a520c4f8ac3a44f4f18dc025b5e5d40049c9f2b,fooof/tests/core/test_io.py::test_load_file_contents,fooof/tests/core/test_io.py,OD-Brit,flaky,Rejected,https://github.com/fooof-tools/fooof/pull/243,classify;root_cause,"""""""Tests for fooof.core.io."""""" import os from fooof.core.items import OBJ_DESC from fooof.tests.settings import TEST_DATA_PATH from fooof.core.io import * ################################################################################################### ################################################################################################### def test_fname(): """"""Check that the file name checker helper function properly checks / adds file extensions."""""" assert fname('data', 'json') == 'data.json' assert fname('data.json', 'json') == 'data.json' assert fname('pic', 'png') == 'pic.png' assert fname('pic.png', 'png') == 'pic.png' assert fname('report.pdf', 'pdf') == 'report.pdf' assert fname('report.png', 'pdf') == 'report.png' def test_fpath(): """"""Check that the file path checker helper function properly checks / combines file paths."""""" assert fpath(None, 'data.json') == 'data.json' assert fpath('/path/', 'data.json') == '/path/data.json' def test_save_fm_str(tfm): """"""Check saving fm data, with file specifiers as strings."""""" # Test saving out each set of save elements file_name_res = 'test_fooof_res' file_name_set = 'test_fooof_set' file_name_dat = 'test_fooof_dat' save_fm(tfm, file_name_res, TEST_DATA_PATH, False, True, False, False) save_fm(tfm, file_name_set, TEST_DATA_PATH, False, False, True, False) save_fm(tfm, file_name_dat, TEST_DATA_PATH, False, False, False, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_res + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_set + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_dat + '.json')) # Test saving out all save elements file_name_all = 'test_fooof_all' save_fm(tfm, file_name_all, TEST_DATA_PATH, False, True, True, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_all + '.json')) def test_save_fm_append(tfm): """"""Check saving fm data, appending to a file."""""" file_name = 'test_fooof_append' save_fm(tfm, file_name, TEST_DATA_PATH, True, True, True, True) save_fm(tfm, file_name, TEST_DATA_PATH, True, True, True, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_save_fm_fobj(tfm): """"""Check saving fm data, with file object file specifier."""""" file_name = 'test_fooof_fileobj' # Save, using file-object: three successive lines with three possible save settings with open(os.path.join(TEST_DATA_PATH, file_name + '.json'), 'w') as f_obj: save_fm(tfm, f_obj, TEST_DATA_PATH, False, True, False, False) save_fm(tfm, f_obj, TEST_DATA_PATH, False, False, True, False) save_fm(tfm, f_obj, TEST_DATA_PATH, False, False, False, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_save_fg(tfg): """"""Check saving fg data."""""" res_file_name = 'test_fooofgroup_res' set_file_name = 'test_fooofgroup_set' dat_file_name = 'test_fooofgroup_dat' save_fg(tfg, file_name=res_file_name, file_path=TEST_DATA_PATH, save_results=True) save_fg(tfg, file_name=set_file_name, file_path=TEST_DATA_PATH, save_settings=True) save_fg(tfg, file_name=dat_file_name, file_path=TEST_DATA_PATH, save_data=True) assert os.path.exists(os.path.join(TEST_DATA_PATH, res_file_name + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, set_file_name + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, dat_file_name + '.json')) # Test saving out all save elements file_name_all = 'test_fooofgroup_all' save_fg(tfg, file_name_all, TEST_DATA_PATH, False, True, True, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_all + '.json')) def test_save_fg_append(tfg): """"""Check saving fg data, appending to file."""""" file_name = 'test_fooofgroup_append' save_fg(tfg, file_name, TEST_DATA_PATH, True, save_results=True) save_fg(tfg, file_name, TEST_DATA_PATH, True, save_results=True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_save_fg_fobj(tfg): """"""Check saving fg data, with file object file specifier."""""" file_name = 'test_fooof_fileobj' with open(os.path.join(TEST_DATA_PATH, file_name + '.json'), 'w') as f_obj: save_fg(tfg, f_obj, TEST_DATA_PATH, False, True, False, False) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_load_json_str(): """"""Test loading JSON file, with str file specifier. Loads files from test_save_fm_str. """""" file_name = 'test_fooof_all' data = load_json(file_name, TEST_DATA_PATH) assert data def test_load_json_fobj(): """"""Test loading JSON file, with file object file specifier. Loads files from test_save_fm_str. """""" file_name = 'test_fooof_all' with open(os.path.join(TEST_DATA_PATH, file_name + '.json'), 'r') as f_obj: data = load_json(f_obj, '') assert data def test_load_jsonlines(): """"""Test loading JSONlines file. Loads files from test_save_fg. """""" res_file_name = 'test_fooofgroup_res' for data in load_jsonlines(res_file_name, TEST_DATA_PATH): assert data def test_load_file_contents(): """"""Check that loaded files contain the contents they should. Note that is this test fails, it likely stems from an issue from saving. """""" file_name = 'test_fooof_all' loaded_data = load_json(file_name, TEST_DATA_PATH) # Check settings for setting in OBJ_DESC['settings']: assert setting in loaded_data.keys() # Check results for result in OBJ_DESC['results']: assert result in loaded_data.keys() # Check results for datum in OBJ_DESC['data']: assert datum in loaded_data.keys() ", https://github.com/voytekresearch/fooof,7a520c4f8ac3a44f4f18dc025b5e5d40049c9f2b,fooof/tests/core/test_io.py::test_load_jsonlines,fooof/tests/core/test_io.py,OD-Brit,flaky,Rejected,https://github.com/fooof-tools/fooof/pull/243,classify;root_cause,"""""""Tests for fooof.core.io."""""" import os from fooof.core.items import OBJ_DESC from fooof.tests.settings import TEST_DATA_PATH from fooof.core.io import * ################################################################################################### ################################################################################################### def test_fname(): """"""Check that the file name checker helper function properly checks / adds file extensions."""""" assert fname('data', 'json') == 'data.json' assert fname('data.json', 'json') == 'data.json' assert fname('pic', 'png') == 'pic.png' assert fname('pic.png', 'png') == 'pic.png' assert fname('report.pdf', 'pdf') == 'report.pdf' assert fname('report.png', 'pdf') == 'report.png' def test_fpath(): """"""Check that the file path checker helper function properly checks / combines file paths."""""" assert fpath(None, 'data.json') == 'data.json' assert fpath('/path/', 'data.json') == '/path/data.json' def test_save_fm_str(tfm): """"""Check saving fm data, with file specifiers as strings."""""" # Test saving out each set of save elements file_name_res = 'test_fooof_res' file_name_set = 'test_fooof_set' file_name_dat = 'test_fooof_dat' save_fm(tfm, file_name_res, TEST_DATA_PATH, False, True, False, False) save_fm(tfm, file_name_set, TEST_DATA_PATH, False, False, True, False) save_fm(tfm, file_name_dat, TEST_DATA_PATH, False, False, False, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_res + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_set + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_dat + '.json')) # Test saving out all save elements file_name_all = 'test_fooof_all' save_fm(tfm, file_name_all, TEST_DATA_PATH, False, True, True, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_all + '.json')) def test_save_fm_append(tfm): """"""Check saving fm data, appending to a file."""""" file_name = 'test_fooof_append' save_fm(tfm, file_name, TEST_DATA_PATH, True, True, True, True) save_fm(tfm, file_name, TEST_DATA_PATH, True, True, True, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_save_fm_fobj(tfm): """"""Check saving fm data, with file object file specifier."""""" file_name = 'test_fooof_fileobj' # Save, using file-object: three successive lines with three possible save settings with open(os.path.join(TEST_DATA_PATH, file_name + '.json'), 'w') as f_obj: save_fm(tfm, f_obj, TEST_DATA_PATH, False, True, False, False) save_fm(tfm, f_obj, TEST_DATA_PATH, False, False, True, False) save_fm(tfm, f_obj, TEST_DATA_PATH, False, False, False, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_save_fg(tfg): """"""Check saving fg data."""""" res_file_name = 'test_fooofgroup_res' set_file_name = 'test_fooofgroup_set' dat_file_name = 'test_fooofgroup_dat' save_fg(tfg, file_name=res_file_name, file_path=TEST_DATA_PATH, save_results=True) save_fg(tfg, file_name=set_file_name, file_path=TEST_DATA_PATH, save_settings=True) save_fg(tfg, file_name=dat_file_name, file_path=TEST_DATA_PATH, save_data=True) assert os.path.exists(os.path.join(TEST_DATA_PATH, res_file_name + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, set_file_name + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, dat_file_name + '.json')) # Test saving out all save elements file_name_all = 'test_fooofgroup_all' save_fg(tfg, file_name_all, TEST_DATA_PATH, False, True, True, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_all + '.json')) def test_save_fg_append(tfg): """"""Check saving fg data, appending to file."""""" file_name = 'test_fooofgroup_append' save_fg(tfg, file_name, TEST_DATA_PATH, True, save_results=True) save_fg(tfg, file_name, TEST_DATA_PATH, True, save_results=True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_save_fg_fobj(tfg): """"""Check saving fg data, with file object file specifier."""""" file_name = 'test_fooof_fileobj' with open(os.path.join(TEST_DATA_PATH, file_name + '.json'), 'w') as f_obj: save_fg(tfg, f_obj, TEST_DATA_PATH, False, True, False, False) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_load_json_str(): """"""Test loading JSON file, with str file specifier. Loads files from test_save_fm_str. """""" file_name = 'test_fooof_all' data = load_json(file_name, TEST_DATA_PATH) assert data def test_load_json_fobj(): """"""Test loading JSON file, with file object file specifier. Loads files from test_save_fm_str. """""" file_name = 'test_fooof_all' with open(os.path.join(TEST_DATA_PATH, file_name + '.json'), 'r') as f_obj: data = load_json(f_obj, '') assert data def test_load_jsonlines(): """"""Test loading JSONlines file. Loads files from test_save_fg. """""" res_file_name = 'test_fooofgroup_res' for data in load_jsonlines(res_file_name, TEST_DATA_PATH): assert data def test_load_file_contents(): """"""Check that loaded files contain the contents they should. Note that is this test fails, it likely stems from an issue from saving. """""" file_name = 'test_fooof_all' loaded_data = load_json(file_name, TEST_DATA_PATH) # Check settings for setting in OBJ_DESC['settings']: assert setting in loaded_data.keys() # Check results for result in OBJ_DESC['results']: assert result in loaded_data.keys() # Check results for datum in OBJ_DESC['data']: assert datum in loaded_data.keys() ", https://github.com/voytekresearch/fooof,7a520c4f8ac3a44f4f18dc025b5e5d40049c9f2b,fooof/tests/core/test_io.py::test_load_json_fobj,fooof/tests/core/test_io.py,OD-Brit,flaky,Rejected,https://github.com/fooof-tools/fooof/pull/243,classify;root_cause,"""""""Tests for fooof.core.io."""""" import os from fooof.core.items import OBJ_DESC from fooof.tests.settings import TEST_DATA_PATH from fooof.core.io import * ################################################################################################### ################################################################################################### def test_fname(): """"""Check that the file name checker helper function properly checks / adds file extensions."""""" assert fname('data', 'json') == 'data.json' assert fname('data.json', 'json') == 'data.json' assert fname('pic', 'png') == 'pic.png' assert fname('pic.png', 'png') == 'pic.png' assert fname('report.pdf', 'pdf') == 'report.pdf' assert fname('report.png', 'pdf') == 'report.png' def test_fpath(): """"""Check that the file path checker helper function properly checks / combines file paths."""""" assert fpath(None, 'data.json') == 'data.json' assert fpath('/path/', 'data.json') == '/path/data.json' def test_save_fm_str(tfm): """"""Check saving fm data, with file specifiers as strings."""""" # Test saving out each set of save elements file_name_res = 'test_fooof_res' file_name_set = 'test_fooof_set' file_name_dat = 'test_fooof_dat' save_fm(tfm, file_name_res, TEST_DATA_PATH, False, True, False, False) save_fm(tfm, file_name_set, TEST_DATA_PATH, False, False, True, False) save_fm(tfm, file_name_dat, TEST_DATA_PATH, False, False, False, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_res + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_set + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_dat + '.json')) # Test saving out all save elements file_name_all = 'test_fooof_all' save_fm(tfm, file_name_all, TEST_DATA_PATH, False, True, True, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_all + '.json')) def test_save_fm_append(tfm): """"""Check saving fm data, appending to a file."""""" file_name = 'test_fooof_append' save_fm(tfm, file_name, TEST_DATA_PATH, True, True, True, True) save_fm(tfm, file_name, TEST_DATA_PATH, True, True, True, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_save_fm_fobj(tfm): """"""Check saving fm data, with file object file specifier."""""" file_name = 'test_fooof_fileobj' # Save, using file-object: three successive lines with three possible save settings with open(os.path.join(TEST_DATA_PATH, file_name + '.json'), 'w') as f_obj: save_fm(tfm, f_obj, TEST_DATA_PATH, False, True, False, False) save_fm(tfm, f_obj, TEST_DATA_PATH, False, False, True, False) save_fm(tfm, f_obj, TEST_DATA_PATH, False, False, False, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_save_fg(tfg): """"""Check saving fg data."""""" res_file_name = 'test_fooofgroup_res' set_file_name = 'test_fooofgroup_set' dat_file_name = 'test_fooofgroup_dat' save_fg(tfg, file_name=res_file_name, file_path=TEST_DATA_PATH, save_results=True) save_fg(tfg, file_name=set_file_name, file_path=TEST_DATA_PATH, save_settings=True) save_fg(tfg, file_name=dat_file_name, file_path=TEST_DATA_PATH, save_data=True) assert os.path.exists(os.path.join(TEST_DATA_PATH, res_file_name + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, set_file_name + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, dat_file_name + '.json')) # Test saving out all save elements file_name_all = 'test_fooofgroup_all' save_fg(tfg, file_name_all, TEST_DATA_PATH, False, True, True, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_all + '.json')) def test_save_fg_append(tfg): """"""Check saving fg data, appending to file."""""" file_name = 'test_fooofgroup_append' save_fg(tfg, file_name, TEST_DATA_PATH, True, save_results=True) save_fg(tfg, file_name, TEST_DATA_PATH, True, save_results=True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_save_fg_fobj(tfg): """"""Check saving fg data, with file object file specifier."""""" file_name = 'test_fooof_fileobj' with open(os.path.join(TEST_DATA_PATH, file_name + '.json'), 'w') as f_obj: save_fg(tfg, f_obj, TEST_DATA_PATH, False, True, False, False) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_load_json_str(): """"""Test loading JSON file, with str file specifier. Loads files from test_save_fm_str. """""" file_name = 'test_fooof_all' data = load_json(file_name, TEST_DATA_PATH) assert data def test_load_json_fobj(): """"""Test loading JSON file, with file object file specifier. Loads files from test_save_fm_str. """""" file_name = 'test_fooof_all' with open(os.path.join(TEST_DATA_PATH, file_name + '.json'), 'r') as f_obj: data = load_json(f_obj, '') assert data def test_load_jsonlines(): """"""Test loading JSONlines file. Loads files from test_save_fg. """""" res_file_name = 'test_fooofgroup_res' for data in load_jsonlines(res_file_name, TEST_DATA_PATH): assert data def test_load_file_contents(): """"""Check that loaded files contain the contents they should. Note that is this test fails, it likely stems from an issue from saving. """""" file_name = 'test_fooof_all' loaded_data = load_json(file_name, TEST_DATA_PATH) # Check settings for setting in OBJ_DESC['settings']: assert setting in loaded_data.keys() # Check results for result in OBJ_DESC['results']: assert result in loaded_data.keys() # Check results for datum in OBJ_DESC['data']: assert datum in loaded_data.keys() ", https://github.com/voytekresearch/fooof,7a520c4f8ac3a44f4f18dc025b5e5d40049c9f2b,fooof/tests/core/test_io.py::test_load_json_str,fooof/tests/core/test_io.py,OD-Brit,flaky,Rejected,https://github.com/fooof-tools/fooof/pull/243,classify;root_cause,"""""""Tests for fooof.core.io."""""" import os from fooof.core.items import OBJ_DESC from fooof.tests.settings import TEST_DATA_PATH from fooof.core.io import * ################################################################################################### ################################################################################################### def test_fname(): """"""Check that the file name checker helper function properly checks / adds file extensions."""""" assert fname('data', 'json') == 'data.json' assert fname('data.json', 'json') == 'data.json' assert fname('pic', 'png') == 'pic.png' assert fname('pic.png', 'png') == 'pic.png' assert fname('report.pdf', 'pdf') == 'report.pdf' assert fname('report.png', 'pdf') == 'report.png' def test_fpath(): """"""Check that the file path checker helper function properly checks / combines file paths."""""" assert fpath(None, 'data.json') == 'data.json' assert fpath('/path/', 'data.json') == '/path/data.json' def test_save_fm_str(tfm): """"""Check saving fm data, with file specifiers as strings."""""" # Test saving out each set of save elements file_name_res = 'test_fooof_res' file_name_set = 'test_fooof_set' file_name_dat = 'test_fooof_dat' save_fm(tfm, file_name_res, TEST_DATA_PATH, False, True, False, False) save_fm(tfm, file_name_set, TEST_DATA_PATH, False, False, True, False) save_fm(tfm, file_name_dat, TEST_DATA_PATH, False, False, False, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_res + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_set + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_dat + '.json')) # Test saving out all save elements file_name_all = 'test_fooof_all' save_fm(tfm, file_name_all, TEST_DATA_PATH, False, True, True, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_all + '.json')) def test_save_fm_append(tfm): """"""Check saving fm data, appending to a file."""""" file_name = 'test_fooof_append' save_fm(tfm, file_name, TEST_DATA_PATH, True, True, True, True) save_fm(tfm, file_name, TEST_DATA_PATH, True, True, True, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_save_fm_fobj(tfm): """"""Check saving fm data, with file object file specifier."""""" file_name = 'test_fooof_fileobj' # Save, using file-object: three successive lines with three possible save settings with open(os.path.join(TEST_DATA_PATH, file_name + '.json'), 'w') as f_obj: save_fm(tfm, f_obj, TEST_DATA_PATH, False, True, False, False) save_fm(tfm, f_obj, TEST_DATA_PATH, False, False, True, False) save_fm(tfm, f_obj, TEST_DATA_PATH, False, False, False, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_save_fg(tfg): """"""Check saving fg data."""""" res_file_name = 'test_fooofgroup_res' set_file_name = 'test_fooofgroup_set' dat_file_name = 'test_fooofgroup_dat' save_fg(tfg, file_name=res_file_name, file_path=TEST_DATA_PATH, save_results=True) save_fg(tfg, file_name=set_file_name, file_path=TEST_DATA_PATH, save_settings=True) save_fg(tfg, file_name=dat_file_name, file_path=TEST_DATA_PATH, save_data=True) assert os.path.exists(os.path.join(TEST_DATA_PATH, res_file_name + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, set_file_name + '.json')) assert os.path.exists(os.path.join(TEST_DATA_PATH, dat_file_name + '.json')) # Test saving out all save elements file_name_all = 'test_fooofgroup_all' save_fg(tfg, file_name_all, TEST_DATA_PATH, False, True, True, True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name_all + '.json')) def test_save_fg_append(tfg): """"""Check saving fg data, appending to file."""""" file_name = 'test_fooofgroup_append' save_fg(tfg, file_name, TEST_DATA_PATH, True, save_results=True) save_fg(tfg, file_name, TEST_DATA_PATH, True, save_results=True) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_save_fg_fobj(tfg): """"""Check saving fg data, with file object file specifier."""""" file_name = 'test_fooof_fileobj' with open(os.path.join(TEST_DATA_PATH, file_name + '.json'), 'w') as f_obj: save_fg(tfg, f_obj, TEST_DATA_PATH, False, True, False, False) assert os.path.exists(os.path.join(TEST_DATA_PATH, file_name + '.json')) def test_load_json_str(): """"""Test loading JSON file, with str file specifier. Loads files from test_save_fm_str. """""" file_name = 'test_fooof_all' data = load_json(file_name, TEST_DATA_PATH) assert data def test_load_json_fobj(): """"""Test loading JSON file, with file object file specifier. Loads files from test_save_fm_str. """""" file_name = 'test_fooof_all' with open(os.path.join(TEST_DATA_PATH, file_name + '.json'), 'r') as f_obj: data = load_json(f_obj, '') assert data def test_load_jsonlines(): """"""Test loading JSONlines file. Loads files from test_save_fg. """""" res_file_name = 'test_fooofgroup_res' for data in load_jsonlines(res_file_name, TEST_DATA_PATH): assert data def test_load_file_contents(): """"""Check that loaded files contain the contents they should. Note that is this test fails, it likely stems from an issue from saving. """""" file_name = 'test_fooof_all' loaded_data = load_json(file_name, TEST_DATA_PATH) # Check settings for setting in OBJ_DESC['settings']: assert setting in loaded_data.keys() # Check results for result in OBJ_DESC['results']: assert result in loaded_data.keys() # Check results for datum in OBJ_DESC['data']: assert datum in loaded_data.keys() ", https://github.com/voytekresearch/fooof,7a520c4f8ac3a44f4f18dc025b5e5d40049c9f2b,fooof/tests/objs/test_fit.py::test_fooof_fit_knee,fooof/tests/objs/test_fit.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Tests for fooof.objs.fit, including the FOOOF object and it's methods. NOTES ----- The tests here are not strong tests for accuracy. They serve rather as 'smoke tests', for if anything fails completely. """""" import numpy as np from py.test import raises from fooof.core.items import OBJ_DESC from fooof.core.errors import FitError from fooof.core.utils import group_three from fooof.sim import gen_freqs, gen_power_spectrum from fooof.data import FOOOFSettings, FOOOFMetaData, FOOOFResults from fooof.core.errors import DataError, NoDataError, InconsistentDataError from fooof.tests.settings import TEST_DATA_PATH from fooof.tests.tutils import get_tfm, plot_test from fooof.objs.fit import * ################################################################################################### ################################################################################################### def test_fooof(): """"""Check FOOOF object initializes properly."""""" assert FOOOF(verbose=False) def test_fooof_has_data(tfm): """"""Test the has_data property attribute, with and without model fits."""""" assert tfm.has_data ntfm = FOOOF() assert not ntfm.has_data def test_fooof_has_model(tfm): """"""Test the has_model property attribute, with and without model fits."""""" assert tfm.has_model ntfm = FOOOF() assert not ntfm.has_model def test_fooof_n_peaks(tfm): """"""Test the n_peaks property attribute."""""" assert tfm.n_peaks_ def test_fooof_fit_nk(): """"""Test FOOOF fit, no knee."""""" ap_params = [50, 2] gauss_params = [10, 0.5, 2, 20, 0.3, 4] nlv = 0.0025 xs, ys = gen_power_spectrum([3, 50], ap_params, gauss_params, nlv) tfm = FOOOF(verbose=False) tfm.fit(xs, ys) # Check model results - aperiodic parameters assert np.allclose(ap_params, tfm.aperiodic_params_, [0.5, 0.1]) # Check model results - gaussian parameters for ii, gauss in enumerate(group_three(gauss_params)): assert np.allclose(gauss, tfm.gaussian_params_[ii], [2.0, 0.5, 1.0]) def test_fooof_fit_nk_noise(): """"""Test FOOOF fit on noisy data, to make sure nothing breaks."""""" ap_params = [50, 2] gauss_params = [10, 0.5, 2, 20, 0.3, 4] nlv = 1.0 xs, ys = gen_power_spectrum([3, 50], ap_params, gauss_params, nlv) tfm = FOOOF(max_n_peaks=8, verbose=False) tfm.fit(xs, ys) # No accuracy checking here - just checking that it ran assert tfm.has_model def test_fooof_fit_knee(): """"""Test FOOOF fit, with a knee."""""" ap_params = [50, 10, 1] gauss_params = [10, 0.3, 2, 20, 0.1, 4, 60, 0.3, 1] nlv = 0.0025 xs, ys = gen_power_spectrum([1, 150], ap_params, gauss_params, nlv) tfm = FOOOF(aperiodic_mode='knee', verbose=False) tfm.fit(xs, ys) # Check model results - aperiodic parameters assert np.allclose(ap_params, tfm.aperiodic_params_, [1, 2, 0.2]) # Check model results - gaussian parameters for ii, gauss in enumerate(group_three(gauss_params)): assert np.allclose(gauss, tfm.gaussian_params_[ii], [2.0, 0.5, 1.0]) def test_fooof_fit_measures(): """"""Test goodness of fit & error metrics, post model fitting."""""" tfm = FOOOF(verbose=False) # Hack fake data with known properties: total error magnitude 2 tfm.power_spectrum = np.array([1, 2, 3, 4, 5]) tfm.fooofed_spectrum_ = np.array([1, 2, 5, 4, 5]) # Check default goodness of fit and error measures tfm._calc_r_squared() assert np.isclose(tfm.r_squared_, 0.75757575) tfm._calc_error() assert np.isclose(tfm.error_, 0.4) # Check with alternative error fit approach tfm._calc_error(metric='MSE') assert np.isclose(tfm.error_, 0.8) tfm._calc_error(metric='RMSE') assert np.isclose(tfm.error_, np.sqrt(0.8)) with raises(ValueError): tfm._calc_error(metric='BAD') def test_fooof_checks(): """"""Test various checks, errors and edge cases in FOOOF. This tests all the input checking done in `_prepare_data`. """""" xs, ys = gen_power_spectrum([3, 50], [50, 2], [10, 0.5, 2]) tfm = FOOOF(verbose=False) ## Check checks & errors done in `_prepare_data` # Check wrong data type error with raises(DataError): tfm.fit(list(xs), list(ys)) # Check dimension error with raises(DataError): tfm.fit(xs, np.reshape(ys, [1, len(ys)])) # Check shape mismatch error with raises(InconsistentDataError): tfm.fit(xs[:-1], ys) # Check complex inputs error with raises(DataError): tfm.fit(xs, ys.astype('complex')) # Check trim_spectrum range tfm.fit(xs, ys, [3, 40]) # Check freq of 0 issue xs, ys = gen_power_spectrum([3, 50], [50, 2], [10, 0.5, 2]) tfm.fit(xs, ys) assert tfm.freqs[0] != 0 # Check error if there is a post-logging inf or nan with raises(DataError): # Double log (1) -> -inf tfm.fit(np.array([1, 2, 3]), np.log10(np.array([1, 2, 3]))) with raises(DataError): # Log (-1) -> NaN tfm.fit(np.array([1, 2, 3]), np.array([-1, 2, 3])) ## Check errors & errors done in `fit` # Check fit, and string report model error (no data / model fit) tfm = FOOOF(verbose=False) with raises(NoDataError): tfm.fit() def test_fooof_load(): """"""Test load into FOOOF. Note: loads files from test_core_io."""""" # Test loading just results tfm = FOOOF(verbose=False) file_name_res = 'test_fooof_res' tfm.load(file_name_res, TEST_DATA_PATH) # Check that result attributes get filled for result in OBJ_DESC['results']: assert not np.all(np.isnan(getattr(tfm, result))) # Test that settings and data are None # Except for aperiodic mode, which can be inferred from the data for setting in OBJ_DESC['settings']: if setting is not 'aperiodic_mode': assert getattr(tfm, setting) is None assert getattr(tfm, 'power_spectrum') is None # Test loading just settings tfm = FOOOF(verbose=False) file_name_set = 'test_fooof_set' tfm.load(file_name_set, TEST_DATA_PATH) for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) is not None # Test that results and data are None for result in OBJ_DESC['results']: assert np.all(np.isnan(getattr(tfm, result))) assert tfm.power_spectrum is None # Test loading just data tfm = FOOOF(verbose=False) file_name_dat = 'test_fooof_dat' tfm.load(file_name_dat, TEST_DATA_PATH) assert tfm.power_spectrum is not None # Test that settings and results are None for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) is None for result in OBJ_DESC['results']: assert np.all(np.isnan(getattr(tfm, result))) # Test loading all elements tfm = FOOOF(verbose=False) file_name_all = 'test_fooof_all' tfm.load(file_name_all, TEST_DATA_PATH) for result in OBJ_DESC['results']: assert not np.all(np.isnan(getattr(tfm, result))) for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) is not None for data in OBJ_DESC['data']: assert getattr(tfm, data) is not None for meta_dat in OBJ_DESC['meta_data']: assert getattr(tfm, meta_dat) is not None def test_add_data(): """"""Tests method to add data to FOOOF objects."""""" # This test uses it's own FOOOF object, to not add stuff to the global one tfm = get_tfm() # Test data for adding freqs, pows = np.array([1, 2, 3]), np.array([10, 10, 10]) # Test adding data tfm.add_data(freqs, pows) assert tfm.has_data assert np.all(tfm.freqs == freqs) assert np.all(tfm.power_spectrum == np.log10(pows)) # Test that prior data does not get cleared, when requesting not to clear tfm._reset_data_results(True, True, True) tfm.add_results(FOOOFResults([1, 1], [10, 0.5, 0.5], 0.95, 0.02, [10, 0.5, 0.25])) tfm.add_data(freqs, pows, clear_results=False) assert tfm.has_data assert tfm.has_model # Test that prior data does get cleared, when requesting not to clear tfm._reset_data_results(True, True, True) tfm.add_data(freqs, pows, clear_results=True) assert tfm.has_data assert not tfm.has_model def test_add_settings(): """"""Tests method to add settings to FOOOF objects."""""" # This test uses it's own FOOOF object, to not add stuff to the global one tfm = get_tfm() # Test adding settings fooof_settings = FOOOFSettings([1, 4], 6, 0, 2, 'fixed') tfm.add_settings(fooof_settings) for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) == getattr(fooof_settings, setting) def test_add_meta_data(): """"""Tests method to add meta data to FOOOF objects."""""" # This test uses it's own FOOOF object, to not add stuff to the global one tfm = get_tfm() # Test adding meta data fooof_meta_data = FOOOFMetaData([3, 40], 0.5) tfm.add_meta_data(fooof_meta_data) for meta_dat in OBJ_DESC['meta_data']: assert getattr(tfm, meta_dat) == getattr(fooof_meta_data, meta_dat) def test_add_results(): """"""Tests method to add results to FOOOF objects."""""" # This test uses it's own FOOOF object, to not add stuff to the global one tfm = get_tfm() # Test adding results fooof_results = FOOOFResults([1, 1], [10, 0.5, 0.5], 0.95, 0.02, [10, 0.5, 0.25]) tfm.add_results(fooof_results) assert tfm.has_model for setting in OBJ_DESC['results']: assert getattr(tfm, setting) == getattr(fooof_results, setting.strip('_')) def test_obj_gets(tfm): """"""Tests methods that return FOOOF data objects. Checks: get_settings, get_meta_data, get_results """""" settings = tfm.get_settings() assert isinstance(settings, FOOOFSettings) meta_data = tfm.get_meta_data() assert isinstance(meta_data, FOOOFMetaData) results = tfm.get_results() assert isinstance(results, FOOOFResults) def test_get_params(tfm): """"""Test the get_params method."""""" ", https://github.com/voytekresearch/fooof,7a520c4f8ac3a44f4f18dc025b5e5d40049c9f2b,fooof/tests/objs/test_fit.py::test_fooof_fit_nk,fooof/tests/objs/test_fit.py,OD-Vic,flaky,nan,nan,classify;root_cause,"""""""Tests for fooof.objs.fit, including the FOOOF object and it's methods. NOTES ----- The tests here are not strong tests for accuracy. They serve rather as 'smoke tests', for if anything fails completely. """""" import numpy as np from py.test import raises from fooof.core.items import OBJ_DESC from fooof.core.errors import FitError from fooof.core.utils import group_three from fooof.sim import gen_freqs, gen_power_spectrum from fooof.data import FOOOFSettings, FOOOFMetaData, FOOOFResults from fooof.core.errors import DataError, NoDataError, InconsistentDataError from fooof.tests.settings import TEST_DATA_PATH from fooof.tests.tutils import get_tfm, plot_test from fooof.objs.fit import * ################################################################################################### ################################################################################################### def test_fooof(): """"""Check FOOOF object initializes properly."""""" assert FOOOF(verbose=False) def test_fooof_has_data(tfm): """"""Test the has_data property attribute, with and without model fits."""""" assert tfm.has_data ntfm = FOOOF() assert not ntfm.has_data def test_fooof_has_model(tfm): """"""Test the has_model property attribute, with and without model fits."""""" assert tfm.has_model ntfm = FOOOF() assert not ntfm.has_model def test_fooof_n_peaks(tfm): """"""Test the n_peaks property attribute."""""" assert tfm.n_peaks_ def test_fooof_fit_nk(): """"""Test FOOOF fit, no knee."""""" ap_params = [50, 2] gauss_params = [10, 0.5, 2, 20, 0.3, 4] nlv = 0.0025 xs, ys = gen_power_spectrum([3, 50], ap_params, gauss_params, nlv) tfm = FOOOF(verbose=False) tfm.fit(xs, ys) # Check model results - aperiodic parameters assert np.allclose(ap_params, tfm.aperiodic_params_, [0.5, 0.1]) # Check model results - gaussian parameters for ii, gauss in enumerate(group_three(gauss_params)): assert np.allclose(gauss, tfm.gaussian_params_[ii], [2.0, 0.5, 1.0]) def test_fooof_fit_nk_noise(): """"""Test FOOOF fit on noisy data, to make sure nothing breaks."""""" ap_params = [50, 2] gauss_params = [10, 0.5, 2, 20, 0.3, 4] nlv = 1.0 xs, ys = gen_power_spectrum([3, 50], ap_params, gauss_params, nlv) tfm = FOOOF(max_n_peaks=8, verbose=False) tfm.fit(xs, ys) # No accuracy checking here - just checking that it ran assert tfm.has_model def test_fooof_fit_knee(): """"""Test FOOOF fit, with a knee."""""" ap_params = [50, 10, 1] gauss_params = [10, 0.3, 2, 20, 0.1, 4, 60, 0.3, 1] nlv = 0.0025 xs, ys = gen_power_spectrum([1, 150], ap_params, gauss_params, nlv) tfm = FOOOF(aperiodic_mode='knee', verbose=False) tfm.fit(xs, ys) # Check model results - aperiodic parameters assert np.allclose(ap_params, tfm.aperiodic_params_, [1, 2, 0.2]) # Check model results - gaussian parameters for ii, gauss in enumerate(group_three(gauss_params)): assert np.allclose(gauss, tfm.gaussian_params_[ii], [2.0, 0.5, 1.0]) def test_fooof_fit_measures(): """"""Test goodness of fit & error metrics, post model fitting."""""" tfm = FOOOF(verbose=False) # Hack fake data with known properties: total error magnitude 2 tfm.power_spectrum = np.array([1, 2, 3, 4, 5]) tfm.fooofed_spectrum_ = np.array([1, 2, 5, 4, 5]) # Check default goodness of fit and error measures tfm._calc_r_squared() assert np.isclose(tfm.r_squared_, 0.75757575) tfm._calc_error() assert np.isclose(tfm.error_, 0.4) # Check with alternative error fit approach tfm._calc_error(metric='MSE') assert np.isclose(tfm.error_, 0.8) tfm._calc_error(metric='RMSE') assert np.isclose(tfm.error_, np.sqrt(0.8)) with raises(ValueError): tfm._calc_error(metric='BAD') def test_fooof_checks(): """"""Test various checks, errors and edge cases in FOOOF. This tests all the input checking done in `_prepare_data`. """""" xs, ys = gen_power_spectrum([3, 50], [50, 2], [10, 0.5, 2]) tfm = FOOOF(verbose=False) ## Check checks & errors done in `_prepare_data` # Check wrong data type error with raises(DataError): tfm.fit(list(xs), list(ys)) # Check dimension error with raises(DataError): tfm.fit(xs, np.reshape(ys, [1, len(ys)])) # Check shape mismatch error with raises(InconsistentDataError): tfm.fit(xs[:-1], ys) # Check complex inputs error with raises(DataError): tfm.fit(xs, ys.astype('complex')) # Check trim_spectrum range tfm.fit(xs, ys, [3, 40]) # Check freq of 0 issue xs, ys = gen_power_spectrum([3, 50], [50, 2], [10, 0.5, 2]) tfm.fit(xs, ys) assert tfm.freqs[0] != 0 # Check error if there is a post-logging inf or nan with raises(DataError): # Double log (1) -> -inf tfm.fit(np.array([1, 2, 3]), np.log10(np.array([1, 2, 3]))) with raises(DataError): # Log (-1) -> NaN tfm.fit(np.array([1, 2, 3]), np.array([-1, 2, 3])) ## Check errors & errors done in `fit` # Check fit, and string report model error (no data / model fit) tfm = FOOOF(verbose=False) with raises(NoDataError): tfm.fit() def test_fooof_load(): """"""Test load into FOOOF. Note: loads files from test_core_io."""""" # Test loading just results tfm = FOOOF(verbose=False) file_name_res = 'test_fooof_res' tfm.load(file_name_res, TEST_DATA_PATH) # Check that result attributes get filled for result in OBJ_DESC['results']: assert not np.all(np.isnan(getattr(tfm, result))) # Test that settings and data are None # Except for aperiodic mode, which can be inferred from the data for setting in OBJ_DESC['settings']: if setting is not 'aperiodic_mode': assert getattr(tfm, setting) is None assert getattr(tfm, 'power_spectrum') is None # Test loading just settings tfm = FOOOF(verbose=False) file_name_set = 'test_fooof_set' tfm.load(file_name_set, TEST_DATA_PATH) for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) is not None # Test that results and data are None for result in OBJ_DESC['results']: assert np.all(np.isnan(getattr(tfm, result))) assert tfm.power_spectrum is None # Test loading just data tfm = FOOOF(verbose=False) file_name_dat = 'test_fooof_dat' tfm.load(file_name_dat, TEST_DATA_PATH) assert tfm.power_spectrum is not None # Test that settings and results are None for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) is None for result in OBJ_DESC['results']: assert np.all(np.isnan(getattr(tfm, result))) # Test loading all elements tfm = FOOOF(verbose=False) file_name_all = 'test_fooof_all' tfm.load(file_name_all, TEST_DATA_PATH) for result in OBJ_DESC['results']: assert not np.all(np.isnan(getattr(tfm, result))) for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) is not None for data in OBJ_DESC['data']: assert getattr(tfm, data) is not None for meta_dat in OBJ_DESC['meta_data']: assert getattr(tfm, meta_dat) is not None def test_add_data(): """"""Tests method to add data to FOOOF objects."""""" # This test uses it's own FOOOF object, to not add stuff to the global one tfm = get_tfm() # Test data for adding freqs, pows = np.array([1, 2, 3]), np.array([10, 10, 10]) # Test adding data tfm.add_data(freqs, pows) assert tfm.has_data assert np.all(tfm.freqs == freqs) assert np.all(tfm.power_spectrum == np.log10(pows)) # Test that prior data does not get cleared, when requesting not to clear tfm._reset_data_results(True, True, True) tfm.add_results(FOOOFResults([1, 1], [10, 0.5, 0.5], 0.95, 0.02, [10, 0.5, 0.25])) tfm.add_data(freqs, pows, clear_results=False) assert tfm.has_data assert tfm.has_model # Test that prior data does get cleared, when requesting not to clear tfm._reset_data_results(True, True, True) tfm.add_data(freqs, pows, clear_results=True) assert tfm.has_data assert not tfm.has_model def test_add_settings(): """"""Tests method to add settings to FOOOF objects."""""" # This test uses it's own FOOOF object, to not add stuff to the global one tfm = get_tfm() # Test adding settings fooof_settings = FOOOFSettings([1, 4], 6, 0, 2, 'fixed') tfm.add_settings(fooof_settings) for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) == getattr(fooof_settings, setting) def test_add_meta_data(): """"""Tests method to add meta data to FOOOF objects."""""" # This test uses it's own FOOOF object, to not add stuff to the global one tfm = get_tfm() # Test adding meta data fooof_meta_data = FOOOFMetaData([3, 40], 0.5) tfm.add_meta_data(fooof_meta_data) for meta_dat in OBJ_DESC['meta_data']: assert getattr(tfm, meta_dat) == getattr(fooof_meta_data, meta_dat) def test_add_results(): """"""Tests method to add results to FOOOF objects."""""" # This test uses it's own FOOOF object, to not add stuff to the global one tfm = get_tfm() # Test adding results fooof_results = FOOOFResults([1, 1], [10, 0.5, 0.5], 0.95, 0.02, [10, 0.5, 0.25]) tfm.add_results(fooof_results) assert tfm.has_model for setting in OBJ_DESC['results']: assert getattr(tfm, setting) == getattr(fooof_results, setting.strip('_')) def test_obj_gets(tfm): """"""Tests methods that return FOOOF data objects. Checks: get_settings, get_meta_data, get_results """""" settings = tfm.get_settings() assert isinstance(settings, FOOOFSettings) meta_data = tfm.get_meta_data() assert isinstance(meta_data, FOOOFMetaData) results = tfm.get_results() assert isinstance(results, FOOOFResults) def test_get_params(tfm): """"""Test the get_params method."""""" ", https://github.com/voytekresearch/fooof,7a520c4f8ac3a44f4f18dc025b5e5d40049c9f2b,fooof/tests/objs/test_fit.py::test_fooof_load,fooof/tests/objs/test_fit.py,OD-Brit,flaky,nan,nan,classify;root_cause,"""""""Tests for fooof.objs.fit, including the FOOOF object and it's methods. NOTES ----- The tests here are not strong tests for accuracy. They serve rather as 'smoke tests', for if anything fails completely. """""" import numpy as np from py.test import raises from fooof.core.items import OBJ_DESC from fooof.core.errors import FitError from fooof.core.utils import group_three from fooof.sim import gen_freqs, gen_power_spectrum from fooof.data import FOOOFSettings, FOOOFMetaData, FOOOFResults from fooof.core.errors import DataError, NoDataError, InconsistentDataError from fooof.tests.settings import TEST_DATA_PATH from fooof.tests.tutils import get_tfm, plot_test from fooof.objs.fit import * ################################################################################################### ################################################################################################### def test_fooof(): """"""Check FOOOF object initializes properly."""""" assert FOOOF(verbose=False) def test_fooof_has_data(tfm): """"""Test the has_data property attribute, with and without model fits."""""" assert tfm.has_data ntfm = FOOOF() assert not ntfm.has_data def test_fooof_has_model(tfm): """"""Test the has_model property attribute, with and without model fits."""""" assert tfm.has_model ntfm = FOOOF() assert not ntfm.has_model def test_fooof_n_peaks(tfm): """"""Test the n_peaks property attribute."""""" assert tfm.n_peaks_ def test_fooof_fit_nk(): """"""Test FOOOF fit, no knee."""""" ap_params = [50, 2] gauss_params = [10, 0.5, 2, 20, 0.3, 4] nlv = 0.0025 xs, ys = gen_power_spectrum([3, 50], ap_params, gauss_params, nlv) tfm = FOOOF(verbose=False) tfm.fit(xs, ys) # Check model results - aperiodic parameters assert np.allclose(ap_params, tfm.aperiodic_params_, [0.5, 0.1]) # Check model results - gaussian parameters for ii, gauss in enumerate(group_three(gauss_params)): assert np.allclose(gauss, tfm.gaussian_params_[ii], [2.0, 0.5, 1.0]) def test_fooof_fit_nk_noise(): """"""Test FOOOF fit on noisy data, to make sure nothing breaks."""""" ap_params = [50, 2] gauss_params = [10, 0.5, 2, 20, 0.3, 4] nlv = 1.0 xs, ys = gen_power_spectrum([3, 50], ap_params, gauss_params, nlv) tfm = FOOOF(max_n_peaks=8, verbose=False) tfm.fit(xs, ys) # No accuracy checking here - just checking that it ran assert tfm.has_model def test_fooof_fit_knee(): """"""Test FOOOF fit, with a knee."""""" ap_params = [50, 10, 1] gauss_params = [10, 0.3, 2, 20, 0.1, 4, 60, 0.3, 1] nlv = 0.0025 xs, ys = gen_power_spectrum([1, 150], ap_params, gauss_params, nlv) tfm = FOOOF(aperiodic_mode='knee', verbose=False) tfm.fit(xs, ys) # Check model results - aperiodic parameters assert np.allclose(ap_params, tfm.aperiodic_params_, [1, 2, 0.2]) # Check model results - gaussian parameters for ii, gauss in enumerate(group_three(gauss_params)): assert np.allclose(gauss, tfm.gaussian_params_[ii], [2.0, 0.5, 1.0]) def test_fooof_fit_measures(): """"""Test goodness of fit & error metrics, post model fitting."""""" tfm = FOOOF(verbose=False) # Hack fake data with known properties: total error magnitude 2 tfm.power_spectrum = np.array([1, 2, 3, 4, 5]) tfm.fooofed_spectrum_ = np.array([1, 2, 5, 4, 5]) # Check default goodness of fit and error measures tfm._calc_r_squared() assert np.isclose(tfm.r_squared_, 0.75757575) tfm._calc_error() assert np.isclose(tfm.error_, 0.4) # Check with alternative error fit approach tfm._calc_error(metric='MSE') assert np.isclose(tfm.error_, 0.8) tfm._calc_error(metric='RMSE') assert np.isclose(tfm.error_, np.sqrt(0.8)) with raises(ValueError): tfm._calc_error(metric='BAD') def test_fooof_checks(): """"""Test various checks, errors and edge cases in FOOOF. This tests all the input checking done in `_prepare_data`. """""" xs, ys = gen_power_spectrum([3, 50], [50, 2], [10, 0.5, 2]) tfm = FOOOF(verbose=False) ## Check checks & errors done in `_prepare_data` # Check wrong data type error with raises(DataError): tfm.fit(list(xs), list(ys)) # Check dimension error with raises(DataError): tfm.fit(xs, np.reshape(ys, [1, len(ys)])) # Check shape mismatch error with raises(InconsistentDataError): tfm.fit(xs[:-1], ys) # Check complex inputs error with raises(DataError): tfm.fit(xs, ys.astype('complex')) # Check trim_spectrum range tfm.fit(xs, ys, [3, 40]) # Check freq of 0 issue xs, ys = gen_power_spectrum([3, 50], [50, 2], [10, 0.5, 2]) tfm.fit(xs, ys) assert tfm.freqs[0] != 0 # Check error if there is a post-logging inf or nan with raises(DataError): # Double log (1) -> -inf tfm.fit(np.array([1, 2, 3]), np.log10(np.array([1, 2, 3]))) with raises(DataError): # Log (-1) -> NaN tfm.fit(np.array([1, 2, 3]), np.array([-1, 2, 3])) ## Check errors & errors done in `fit` # Check fit, and string report model error (no data / model fit) tfm = FOOOF(verbose=False) with raises(NoDataError): tfm.fit() def test_fooof_load(): """"""Test load into FOOOF. Note: loads files from test_core_io."""""" # Test loading just results tfm = FOOOF(verbose=False) file_name_res = 'test_fooof_res' tfm.load(file_name_res, TEST_DATA_PATH) # Check that result attributes get filled for result in OBJ_DESC['results']: assert not np.all(np.isnan(getattr(tfm, result))) # Test that settings and data are None # Except for aperiodic mode, which can be inferred from the data for setting in OBJ_DESC['settings']: if setting is not 'aperiodic_mode': assert getattr(tfm, setting) is None assert getattr(tfm, 'power_spectrum') is None # Test loading just settings tfm = FOOOF(verbose=False) file_name_set = 'test_fooof_set' tfm.load(file_name_set, TEST_DATA_PATH) for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) is not None # Test that results and data are None for result in OBJ_DESC['results']: assert np.all(np.isnan(getattr(tfm, result))) assert tfm.power_spectrum is None # Test loading just data tfm = FOOOF(verbose=False) file_name_dat = 'test_fooof_dat' tfm.load(file_name_dat, TEST_DATA_PATH) assert tfm.power_spectrum is not None # Test that settings and results are None for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) is None for result in OBJ_DESC['results']: assert np.all(np.isnan(getattr(tfm, result))) # Test loading all elements tfm = FOOOF(verbose=False) file_name_all = 'test_fooof_all' tfm.load(file_name_all, TEST_DATA_PATH) for result in OBJ_DESC['results']: assert not np.all(np.isnan(getattr(tfm, result))) for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) is not None for data in OBJ_DESC['data']: assert getattr(tfm, data) is not None for meta_dat in OBJ_DESC['meta_data']: assert getattr(tfm, meta_dat) is not None def test_add_data(): """"""Tests method to add data to FOOOF objects."""""" # This test uses it's own FOOOF object, to not add stuff to the global one tfm = get_tfm() # Test data for adding freqs, pows = np.array([1, 2, 3]), np.array([10, 10, 10]) # Test adding data tfm.add_data(freqs, pows) assert tfm.has_data assert np.all(tfm.freqs == freqs) assert np.all(tfm.power_spectrum == np.log10(pows)) # Test that prior data does not get cleared, when requesting not to clear tfm._reset_data_results(True, True, True) tfm.add_results(FOOOFResults([1, 1], [10, 0.5, 0.5], 0.95, 0.02, [10, 0.5, 0.25])) tfm.add_data(freqs, pows, clear_results=False) assert tfm.has_data assert tfm.has_model # Test that prior data does get cleared, when requesting not to clear tfm._reset_data_results(True, True, True) tfm.add_data(freqs, pows, clear_results=True) assert tfm.has_data assert not tfm.has_model def test_add_settings(): """"""Tests method to add settings to FOOOF objects."""""" # This test uses it's own FOOOF object, to not add stuff to the global one tfm = get_tfm() # Test adding settings fooof_settings = FOOOFSettings([1, 4], 6, 0, 2, 'fixed') tfm.add_settings(fooof_settings) for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) == getattr(fooof_settings, setting) def test_add_meta_data(): """"""Tests method to add meta data to FOOOF objects."""""" # This test uses it's own FOOOF object, to not add stuff to the global one tfm = get_tfm() # Test adding meta data fooof_meta_data = FOOOFMetaData([3, 40], 0.5) tfm.add_meta_data(fooof_meta_data) for meta_dat in OBJ_DESC['meta_data']: assert getattr(tfm, meta_dat) == getattr(fooof_meta_data, meta_dat) def test_add_results(): """"""Tests method to add results to FOOOF objects."""""" # This test uses it's own FOOOF object, to not add stuff to the global one tfm = get_tfm() # Test adding results fooof_results = FOOOFResults([1, 1], [10, 0.5, 0.5], 0.95, 0.02, [10, 0.5, 0.25]) tfm.add_results(fooof_results) assert tfm.has_model for setting in OBJ_DESC['results']: assert getattr(tfm, setting) == getattr(fooof_results, setting.strip('_')) def test_obj_gets(tfm): """"""Tests methods that return FOOOF data objects. Checks: get_settings, get_meta_data, get_results """""" settings = tfm.get_settings() assert isinstance(settings, FOOOFSettings) meta_data = tfm.get_meta_data() assert isinstance(meta_data, FOOOFMetaData) results = tfm.get_results() assert isinstance(results, FOOOFResults) def test_get_params(tfm): """"""Test the get_params method."""""" ", https://github.com/voytekresearch/fooof,7a520c4f8ac3a44f4f18dc025b5e5d40049c9f2b,fooof/tests/objs/test_group.py::test_fg_load,fooof/tests/objs/test_group.py,OD-Brit,flaky,nan,nan,classify;root_cause,"""""""Tests for the fooof.objs.group, including the FOOOFGroup object and it's methods. NOTES ----- The tests here are not strong tests for accuracy. They serve rather as 'smoke tests', for if anything fails completely. """""" import numpy as np from numpy.testing import assert_equal from fooof.data import FOOOFResults from fooof.core.items import OBJ_DESC from fooof.sim import gen_group_power_spectra from fooof.tests.settings import TEST_DATA_PATH from fooof.tests.tutils import default_group_params, plot_test from fooof.objs.group import * ################################################################################################### ################################################################################################### def test_fg(): """"""Check FOOOFGroup object initializes properly."""""" # Note: doesn't assert fg itself, as it return false when group_results are empty # This is due to the __len__ used in FOOOFGroup fg = FOOOFGroup(verbose=False) assert isinstance(fg, FOOOFGroup) def test_fg_iter(tfg): """"""Check iterating through FOOOFGroup."""""" for res in tfg: assert res def test_fg_getitem(tfg): """"""Check indexing, from custom __getitem__, in FOOOFGroup."""""" assert tfg[0] def test_fg_has_data(tfg): """"""Test the has_data property attribute, with and without data."""""" assert tfg.has_model ntfg = FOOOFGroup() assert not ntfg.has_data def test_fg_has_model(tfg): """"""Test the has_model property attribute, with and without model fits."""""" assert tfg.has_model ntfg = FOOOFGroup() assert not ntfg.has_model def test_fooof_n_peaks(tfg): """"""Test the n_peaks property attribute."""""" assert tfg.n_peaks_ def test_n_null(tfg): """"""Test the n_null_ property attribute."""""" # Since there should have been no failed fits, this should return 0 assert tfg.n_null_ == 0 def test_null_inds(tfg): """"""Test the null_inds_ property attribute."""""" # Since there should be no failed fits, this should return an empty list assert tfg.null_inds_ == [] def test_fg_fit_nk(): """"""Test FOOOFGroup fit, no knee."""""" n_spectra = 2 xs, ys = gen_group_power_spectra(n_spectra, *default_group_params(), nlvs=0) tfg = FOOOFGroup(verbose=False) tfg.fit(xs, ys) out = tfg.get_results() assert out assert len(out) == n_spectra assert isinstance(out[0], FOOOFResults) assert np.all(out[1].aperiodic_params) def test_fg_fit_nk_noise(): """"""Test FOOOFGroup fit, no knee, on noisy data, to make sure nothing breaks."""""" n_spectra = 5 xs, ys = gen_group_power_spectra(n_spectra, *default_group_params(), nlvs=1.0) tfg = FOOOFGroup(max_n_peaks=8, verbose=False) tfg.fit(xs, ys) # No accuracy checking here - just checking that it ran assert tfg.has_model def test_fg_fit_knee(): """"""Test FOOOFGroup fit, with a knee."""""" n_spectra = 2 ap_params = [50, 2, 1] gaussian_params = [10, 0.5, 2, 20, 0.3, 4] xs, ys = gen_group_power_spectra(n_spectra, [1, 150], ap_params, gaussian_params, nlvs=0) tfg = FOOOFGroup(aperiodic_mode='knee', verbose=False) tfg.fit(xs, ys) # No accuracy checking here - just checking that it ran assert tfg.has_model def test_fg_fit_progress(tfg): """"""Test running FOOOFGroup fitting, with a progress bar."""""" tfg.fit(progress='tqdm') def test_fg_fail(): """"""Test FOOOFGroup fit, in a way that some fits will fail. Also checks that model failures don't cause errors. """""" # Create some noisy spectra that will be hard to fit fs, ps = gen_group_power_spectra(10, [3, 6], [1, 1], [10, 1, 1], nlvs=10) # Use a fg with the max iterations set so low that it will fail to converge ntfg = FOOOFGroup() ntfg._maxfev = 5 # Fit models, where some will fail, to see if it completes cleanly ntfg.fit(fs, ps) # Check that results are all for res in ntfg.get_results(): assert res # Test that get_params works with failed model fits outs1 = ntfg.get_params('aperiodic_params') outs2 = ntfg.get_params('aperiodic_params', 'exponent') outs3 = ntfg.get_params('peak_params') outs4 = ntfg.get_params('peak_params', 0) outs5 = ntfg.get_params('gaussian_params', 2) # Test shortcut labels outs6 = ntfg.get_params('aperiodic') outs6 = ntfg.get_params('peak', 'CF') # Test the property attributes related to null model fits # This checks that they do the right thing when there are null fits (failed fits) assert ntfg.n_null_ > 0 assert ntfg.null_inds_ def test_fg_drop(): """"""Test function to drop results from FOOOFGroup."""""" n_spectra = 3 xs, ys = gen_group_power_spectra(n_spectra, *default_group_params()) tfg = FOOOFGroup(verbose=False) # Test dropping one ind tfg.fit(xs, ys) tfg.drop(0) dropped_fres = tfg.group_results[0] for field in dropped_fres._fields: assert np.all(np.isnan(getattr(dropped_fres, field))) # Test dropping multiple inds tfg.fit(xs, ys) drop_inds = [0, 2] tfg.drop(drop_inds) for drop_ind in drop_inds: dropped_fres = tfg.group_results[drop_ind] for field in dropped_fres._fields: assert np.all(np.isnan(getattr(dropped_fres, field))) # Test that a FOOOFGroup that has had inds dropped still works with `get_params` cfs = tfg.get_params('peak_params', 1) exps = tfg.get_params('aperiodic_params', 'exponent') assert np.all(np.isnan(exps[drop_inds])) assert np.all(np.invert(np.isnan(np.delete(exps, drop_inds)))) def test_fg_fit_par(): """"""Test FOOOFGroup fit, running in parallel."""""" n_spectra = 2 xs, ys = gen_group_power_spectra(n_spectra, *default_group_params()) tfg = FOOOFGroup(verbose=False) tfg.fit(xs, ys, n_jobs=2) out = tfg.get_results() assert out assert len(out) == n_spectra assert isinstance(out[0], FOOOFResults) assert np.all(out[1].aperiodic_params) def test_fg_print(tfg): """"""Check print method (alias)."""""" tfg.print_results() assert True def test_get_results(tfg): """"""Check get results method."""""" assert tfg.get_results() def test_get_params(tfg): """"""Check get_params method."""""" for dname in ['aperiodic_params', 'peak_params', 'error', 'r_squared', 'gaussian_params']: assert np.any(tfg.get_params(dname)) if dname == 'aperiodic_params': for dtype in ['offset', 'exponent']: assert np.any(tfg.get_params(dname, dtype)) if dname == 'peak_params': for dtype in ['CF', 'PW', 'BW']: assert np.any(tfg.get_params(dname, dtype)) @plot_test def test_fg_plot(tfg, skip_if_no_mpl): """"""Check alias method for plot."""""" tfg.plot() def test_fg_load(): """"""Test load into FOOOFGroup. Note: loads files from test_core_io."""""" file_name_res = 'test_fooofgroup_res' file_name_set = 'test_fooofgroup_set' file_name_dat = 'test_fooofgroup_dat' # Test loading just results tfg = FOOOFGroup(verbose=False) tfg.load(file_name_res, TEST_DATA_PATH) assert len(tfg.group_results) > 0 # Test that settings and data are None # Except for aperiodic mode, which can be inferred from the data for setting in OBJ_DESC['settings']: if setting is not 'aperiodic_mode': assert getattr(tfg, setting) is None assert tfg.power_spectra is None # Test loading just settings tfg = FOOOFGroup(verbose=False) tfg.load(file_name_set, TEST_DATA_PATH) for setting in OBJ_DESC['settings']: assert getattr(tfg, setting) is not None # Test that results and data are None for result in OBJ_DESC['results']: assert np.all(np.isnan(getattr(tfg, result))) assert tfg.power_spectra is None # Test loading just data tfg = FOOOFGroup(verbose=False) tfg.load(file_name_dat, TEST_DATA_PATH) assert tfg.power_spectra is not None # Test that settings and results are None for setting in OBJ_DESC['settings']: assert getattr(tfg, setting) is None for result in OBJ_DESC['results']: assert np.all(np.isnan(getattr(tfg, result))) # Test loading all elements tfg = FOOOFGroup(verbose=False) file_name_all = 'test_fooofgroup_all' tfg.load(file_name_all, TEST_DATA_PATH) assert len(tfg.group_results) > 0 for setting in OBJ_DESC['settings']: assert getattr(tfg, setting) is not None assert tfg.power_spectra is not None for meta_dat in OBJ_DESC['meta_data']: assert getattr(tfg, meta_dat) is not None def test_fg_report(skip_if_no_mpl): """"""Check that running the top level model method runs."""""" n_spectra = 2 xs, ys = gen_group_power_spectra(n_spectra, *default_group_params()) tfg = FOOOFGroup(verbose=False) tfg.report(xs, ys) assert tfg def test_fg_get_fooof(tfg): """"""Check return of an individual model fit to a FOOOF object from FOOOFGroup."""""" # Check without regenerating tfm0 = tfg.get_fooof(0, False) assert tfm0 # Check that settings are copied over properly for setting in OBJ_DESC['settings']: assert getattr(tfg, setting) == getattr(tfm0, setting) # Check with regenerating tfm1 = tfg.get_fooof(1, True) assert tfm1 # Check that regenerated model is created for result in OBJ_DESC['results']: assert np.all(getattr(tfm1, result)) # Test when object has no data (clear a copy of tfg) new_tfg = tfg.copy() new_tfg._reset_data_results(False, True, True, True) tfm2 = new_tfg.get_fooof(0, True) assert tfm2 # Check that data info is copied over properly for meta_dat in OBJ_DESC['meta_data']: assert getattr(tfm2, meta_dat) def test_fg_get_group(tfg): """"""Check the return of a sub-sampled FOOOFGroup."""""" # Check with list index inds1 = [1, 2] nfg1 = tfg.get_group(inds1) assert isinstance(nfg1, FOOOFGroup) # ", https://github.com/voytekresearch/fooof,7a520c4f8ac3a44f4f18dc025b5e5d40049c9f2b,fooof/tests/utils/test_download.py::test_check_data_file,fooof/tests/utils/test_download.py,OD-Brit,flaky,nan,nan,classify;root_cause,"""""""Test functions for fooof.utils.download."""""" import os import shutil import numpy as np from fooof.utils.download import * ################################################################################################### ################################################################################################### TEST_FOLDER = 'test_data' def clean_up_downloads(): shutil.rmtree(TEST_FOLDER) ################################################################################################### ################################################################################################### def test_check_data_folder(): check_data_folder(TEST_FOLDER) assert os.path.isdir(TEST_FOLDER) def test_check_data_file(): filename = 'freqs.npy' check_data_file(filename, TEST_FOLDER) assert os.path.isfile(os.path.join(TEST_FOLDER, filename)) def test_fetch_fooof_data(): filename = 'spectrum.npy' fetch_fooof_data(filename, folder=TEST_FOLDER) assert os.path.isfile(os.path.join(TEST_FOLDER, filename)) clean_up_downloads() def test_load_fooof_data(): filename = 'freqs.npy' data = load_fooof_data(filename, folder=TEST_FOLDER) assert isinstance(data, np.ndarray) clean_up_downloads() ", https://github.com/voytekresearch/fooof,7a520c4f8ac3a44f4f18dc025b5e5d40049c9f2b,fooof/tests/utils/test_io.py::test_load_fooof,fooof/tests/utils/test_io.py,OD-Brit,flaky,nan,nan,classify;root_cause,"""""""Test functions for fooof.utils.io."""""" import numpy as np from fooof.core.items import OBJ_DESC from fooof.objs import FOOOF, FOOOFGroup from fooof.tests.settings import TEST_DATA_PATH from fooof.utils.io import * ################################################################################################### ################################################################################################### def test_load_fooof(): file_name = 'test_fooof_all' tfm = load_fooof(file_name, TEST_DATA_PATH) assert isinstance(tfm, FOOOF) # Check that all elements get loaded for result in OBJ_DESC['results']: assert not np.all(np.isnan(getattr(tfm, result))) for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) is not None for data in OBJ_DESC['data']: assert getattr(tfm, data) is not None for meta_dat in OBJ_DESC['meta_data']: assert getattr(tfm, meta_dat) is not None def test_load_fooofgroup(): file_name = 'test_fooofgroup_all' tfg = load_fooofgroup(file_name, TEST_DATA_PATH) assert isinstance(tfg, FOOOFGroup) # Check that all elements get loaded assert len(tfg.group_results) > 0 for setting in OBJ_DESC['settings']: assert getattr(tfg, setting) is not None assert tfg.power_spectra is not None for meta_dat in OBJ_DESC['meta_data']: assert getattr(tfg, meta_dat) is not None ", https://github.com/voytekresearch/fooof,7a520c4f8ac3a44f4f18dc025b5e5d40049c9f2b,fooof/tests/utils/test_io.py::test_load_fooofgroup,fooof/tests/utils/test_io.py,OD-Brit,flaky,nan,nan,classify;root_cause,"""""""Test functions for fooof.utils.io."""""" import numpy as np from fooof.core.items import OBJ_DESC from fooof.objs import FOOOF, FOOOFGroup from fooof.tests.settings import TEST_DATA_PATH from fooof.utils.io import * ################################################################################################### ################################################################################################### def test_load_fooof(): file_name = 'test_fooof_all' tfm = load_fooof(file_name, TEST_DATA_PATH) assert isinstance(tfm, FOOOF) # Check that all elements get loaded for result in OBJ_DESC['results']: assert not np.all(np.isnan(getattr(tfm, result))) for setting in OBJ_DESC['settings']: assert getattr(tfm, setting) is not None for data in OBJ_DESC['data']: assert getattr(tfm, data) is not None for meta_dat in OBJ_DESC['meta_data']: assert getattr(tfm, meta_dat) is not None def test_load_fooofgroup(): file_name = 'test_fooofgroup_all' tfg = load_fooofgroup(file_name, TEST_DATA_PATH) assert isinstance(tfg, FOOOFGroup) # Check that all elements get loaded assert len(tfg.group_results) > 0 for setting in OBJ_DESC['settings']: assert getattr(tfg, setting) is not None assert tfg.power_spectra is not None for meta_dat in OBJ_DESC['meta_data']: assert getattr(tfg, meta_dat) is not None ", https://github.com/waqasbhatti/authnzerver,bc735a1c9e1c084c40854a539a000d69a50ed486,authnzerver/tests/test_auth_creation.py::test_create_user,authnzerver/tests/test_auth_creation.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"'''test_auth_actions.py - Waqas Bhatti (wbhatti@astro.princeton.edu) - Sep 2018 License: MIT. See the LICENSE file for details. This contains tests for the auth functions in authnzerver.actions. ''' from authnzerver import authdb, actions import os.path import os from datetime import datetime, timedelta import time def get_test_authdb(): '''This just makes a new test auth DB for each test function. ''' authdb.create_sqlite_authdb('test-creation.authdb.sqlite') authdb.initial_authdb_inserts('sqlite:///test-creation.authdb.sqlite') def test_create_user(): ''' This runs through various iterations of creating a user. ''' try: os.remove('test-creation.authdb.sqlite') except Exception: pass try: os.remove('test-creation.authdb.sqlite-shm') except Exception: pass try: os.remove('test-creation.authdb.sqlite-wal') except Exception: pass get_test_authdb() # 1. dumb password payload = {'full_name':'Test User', 'email':'testuser@test.org', 'password':'password', 'reqid':1, 'pii_salt':'super-random-salt'} user_created = actions.create_new_user( payload, override_authdb_path='sqlite:///test-creation.authdb.sqlite' ) assert user_created['success'] is False assert user_created['user_email'] == 'testuser@test.org' assert user_created['user_id'] is None assert user_created['send_verification'] is False assert ('Your password is too short. It must have at least 12 characters.' in user_created['messages']) assert ('Your password is too similar to either ' 'the domain name of this server or your ' 'own name or email address.' in user_created['messages']) assert ('Your password is not complex enough. ' 'One or more characters appear appear too frequently.' in user_created['messages']) assert ('Your password is on the list of the most common ' 'passwords and is vulnerable to guessing.' in user_created['messages']) # 2. all numeric password payload = {'full_name':'Test User', 'email':'testuser@test.org', 'password':'239420349823904802398402375025', 'reqid':1, 'pii_salt':'super-random-salt'} user_created = actions.create_new_user( payload, override_authdb_path='sqlite:///test-creation.authdb.sqlite' ) assert user_created['success'] is False assert user_created['user_email'] == 'testuser@test.org' assert user_created['user_id'] is None assert user_created['send_verification'] is False assert ('Your password cannot be all numbers.' in user_created['messages']) # 3a. password ~= email address payload = {'full_name': 'Test User', 'email':'testuser@test.org', 'password':'testuser', 'reqid':1, 'pii_salt':'super-random-salt'} user_created = actions.create_new_user( payload, override_authdb_path='sqlite:///test-creation.authdb.sqlite' ) assert user_created['success'] is False assert user_created['user_email'] == 'testuser@test.org' assert user_created['user_id'] is None assert user_created['send_verification'] is False assert ('Your password is not complex enough. ' 'One or more characters appear appear too frequently.' in user_created['messages']) assert ('Your password is too similar to either ' 'the domain name of this server or your ' 'own name or email address.' in user_created['messages']) # 3b. password ~= full name payload = {'full_name': 'Test User', 'email':'testuser@test.org', 'password':'TestUser123', 'reqid':1, 'pii_salt':'super-random-salt'} user_created = actions.create_new_user( payload, override_authdb_path='sqlite:///test-creation.authdb.sqlite' ) assert user_created['success'] is False assert user_created['user_email'] == 'testuser@test.org' assert user_created['user_id'] is None assert user_created['send_verification'] is False assert ('Your password is too similar to either ' 'the domain name of this server or your ' 'own name or email address.' in user_created['messages']) # 4. password is OK payload = {'full_name': 'Test User', 'email':'testuser@test.org', 'password':'aROwQin9L8nNtPTEMLXd', 'reqid':1, 'pii_salt':'super-random-salt'} user_created = actions.create_new_user( payload, override_authdb_path='sqlite:///test-creation.authdb.sqlite' ) assert user_created['success'] is True assert user_created['user_email'] == 'testuser@test.org' assert user_created['user_id'] == 4 assert user_created['send_verification'] is True assert ('User account created. Please verify your email address to log in.' in user_created['messages']) # 5. try to create a new user with an existing email address payload = {'full_name': 'Test User', 'email':'testuser@test.org', 'password':'aROwQin9L8nNtPTEMLXd', 'reqid':1, 'pii_salt':'super-random-salt'} user_created = actions.create_new_user( payload, override_authdb_path='sqlite:///test-creation.authdb.sqlite' ) assert user_created['success'] is False assert user_created['user_email'] == 'testuser@test.org' assert user_created['user_id'] == 4 # we should not send a verification email because the user already has an # account or if the account is not active yet, the last verification email # was sent less than 24 hours ago assert user_created['send_verification'] is False assert ('User account created. Please verify your email address to log in.' in user_created['messages']) try: os.remove('test-creation.authdb.sqlite') except Exception: pass try: os.remove('test-creation.authdb.sqlite-shm') except Exception: pass try: os.remove('test-creation.authdb.sqlite-wal') except Exception: pass def test_sessions(): ''' This tests session token generation, readback, deletion, and expiry. ''' try: os.remove('test-creation.authdb.sqlite') except Exception: pass try: os.remove('test-creation.authdb.sqlite-shm') except Exception: pass try: os.remove('test-creation.authdb.sqlite-wal') except Exception: pass get_test_authdb() # session token payload session_payload = { 'user_id':2, 'user_agent':'Mozzarella Killerwhale', 'expires':datetime.utcnow()+timedelta(hours=1), 'ip_address': '1.2.3.4', 'extra_info_json':{'pref_datasets_always_private':True}, 'reqid':1, 'pii_salt':'super-random-salt' } # check creation session_token1 = actions.auth_session_new( session_payload, override_authdb_path='sqlite:///test-creation.authdb.sqlite' ) assert session_token1['success'] is True assert session_token1['session_token'] is not None # check deletion deleted = actions.auth_session_delete( {'session_token':session_token1['session_token'], 'reqid':1, 'pii_salt':'super-random-salt'}, override_authdb_path='sqlite:///test-creation.authdb.sqlite' ) assert deleted['success'] is True # check readback of deleted check = actions.auth_session_exists( {'session_token':session_token1['session_token'], 'reqid':1, 'pii_salt':'super-random-salt'}, override_authdb_path='sqlite:///test-creation.authdb.sqlite' ) assert check['success'] is False # new session token payload session_payload = { 'user_id':2, 'user_agent':'Mozzarella Killerwhale', 'expires':datetime.utcnow()+timedelta(hours=1), 'ip_address': '1.2.3.4', 'extra_info_json':{'pref_datasets_always_private':True}, 'reqid':1, 'pii_salt':'super-random-salt' } # check creation session_token2 = actions.auth_session_new( session_payload, override_authdb_path='sqlite:///test-creation.authdb.sqlite' ) assert session_token2['success'] is True assert session_token2['session_token'] is not None assert session_token1['session_token'] != session_token2['session_token'] # get items for session_token check = actions.auth_session_exists( {'session_token':session_token2['session_token'], 'reqid':1, 'pii_salt':'super-random-salt'}, override_authdb_path='sqlite:///test-creation.authdb.sqlite' ) assert check['success'] is True for key in ('user_id','full_name','email','email_verified', 'emailverify_sent_datetime','is_active', 'last_login_try','last_login_success','created_on', 'user_role','session_token','ip_address', 'user_agent','created','expires', 'extra_info_json'): assert key in check['session_info'] assert check['session_info']['user_id'] == 2 assert check['session_info']['full_name'] == 'The systemwide anonymous user' assert check['session_info']['email'] == 'anonuser@localhost' assert check['session_info']['email_verified'] is True assert check['session_info']['is_active'] is True assert check['session_info']['last_login_try'] is None assert check['session_info']['last_login_success'] is None assert check['session_info']['user_role'] == 'anonymous' assert check['session_info'][ 'session_token' ] == session_token2['session_token'] assert check['session", https://github.com/waqasbhatti/authnzerver,bc735a1c9e1c084c40854a539a000d69a50ed486,authnzerver/tests/test_auth_permissions.py::test_role_permissions,authnzerver/tests/test_auth_permissions.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"'''test_auth_actions.py - Waqas Bhatti (wbhatti@astro.princeton.edu) - Sep 2018 License: MIT. See the LICENSE file for details. This contains tests for the auth functions in authnzerver.actions. ''' from .. import authdb, actions import os.path import os import multiprocessing as mp def get_test_authdb(): '''This just makes a new test auth DB for each test function. ''' authdb.create_sqlite_authdb('test-permcheck.authdb.sqlite') authdb.initial_authdb_inserts('sqlite:///test-permcheck.authdb.sqlite') def test_role_permissions(): ''' This tests if we can check the permissions for a logged-in user. ''' try: os.remove('test-permcheck.authdb.sqlite') except Exception: pass try: os.remove('test-permcheck.authdb.sqlite-shm') except Exception: pass try: os.remove('test-permcheck.authdb.sqlite-wal') except Exception: pass get_test_authdb() # create the user user_payload = {'full_name': 'Test User', 'email':'testuser-permcheck@test.org', 'password':'aROwQin9L8nNtPTEMLXd', 'pii_salt':'super-secret-salt', 'reqid':1} user_created = actions.create_new_user( user_payload, override_authdb_path='sqlite:///test-permcheck.authdb.sqlite' ) assert user_created['success'] is True assert user_created['user_email'] == 'testuser-permcheck@test.org' assert ('User account created. Please verify your email address to log in.' in user_created['messages']) # verify our email emailverify = ( actions.set_user_emailaddr_verified( {'email':user_payload['email'], 'user_id': user_created['user_id'], 'pii_salt':'super-secret-salt', 'reqid':1}, override_authdb_path='sqlite:///test-permcheck.authdb.sqlite' ) ) # make a non-verified user user_payload2 = {'full_name': 'Test User', 'email':'testuser-permcheck2@test.org', 'password':'aROwQin9L8nNtPTEMLXd', 'pii_salt':'super-secret-salt', 'reqid':1} user_created2 = actions.create_new_user( user_payload2, override_authdb_path='sqlite:///test-permcheck.authdb.sqlite' ) assert user_created2['success'] is True assert user_created2['user_email'] == 'testuser-permcheck2@test.org' assert ('User account created. Please verify your email address to log in.' in user_created2['messages']) # # now run the permissions checks # # get the permissions JSON thisdir = os.path.dirname(__file__) permissions_json = os.path.abspath( os.path.join(thisdir, '..', 'default-permissions-model.json') ) # 1. view a non-owned public object access_check = actions.check_user_access( {'user_id':emailverify['user_id'], 'user_role':'authenticated', 'action':'view', 'target_name':'object', 'target_owner':1, 'target_visibility':'public', 'target_sharedwith':'', 'reqid':1, 'pii_salt':'dummy-pii-salt'}, override_authdb_path='sqlite:///test-permcheck.authdb.sqlite', override_permissions_json=permissions_json, raiseonfail=True, ) assert access_check['success'] is True assert ( ""Access request check successful. Access granted: True."" in access_check['messages'] ) # 2. delete a non-owned public object access_check = actions.check_user_access( {'user_id':emailverify['user_id'], 'user_role':'authenticated', 'action':'delete', 'target_name':'object', 'target_owner':1, 'target_visibility':'public', 'target_sharedwith':'', 'reqid':1, 'pii_salt':'dummy-pii-salt'}, override_authdb_path='sqlite:///test-permcheck.authdb.sqlite', override_permissions_json=permissions_json, raiseonfail=True, ) assert access_check['success'] is False assert ( ""Access request check successful. Access granted: False."" in access_check['messages'] ) # 3. edit a self owned dataset access_check = actions.check_user_access( {'user_id':emailverify['user_id'], 'user_role':'authenticated', 'action':'edit', 'target_name':'dataset', 'target_owner':emailverify['user_id'], 'target_visibility':'private', 'target_sharedwith':'', 'reqid':1, 'pii_salt':'dummy-pii-salt'}, override_authdb_path='sqlite:///test-permcheck.authdb.sqlite', override_permissions_json=permissions_json, raiseonfail=True, ) assert access_check['success'] is True assert ( ""Access request check successful. Access granted: True."" in access_check['messages'] ) # 3. as superuser, delete someone else's private dataset access_check = actions.check_user_access( {'user_id':1, 'user_role':'superuser', 'action':'delete', 'target_name':'dataset', 'target_owner':4, 'target_visibility':'private', 'target_sharedwith':'', 'reqid':1, 'pii_salt':'dummy-pii-salt'}, override_authdb_path='sqlite:///test-permcheck.authdb.sqlite', override_permissions_json=permissions_json, raiseonfail=True, ) assert access_check['success'] is True assert ( ""Access request check successful. Access granted: True."" in access_check['messages'] ) # 4. as locked user, try to view a public collection access_check = actions.check_user_access( {'user_id': 3, 'user_role':'locked', 'action':'view', 'target_name':'collection', 'target_owner':1, 'target_visibility':'public', 'target_sharedwith':'', 'reqid':1, 'pii_salt':'dummy-pii-salt'}, override_authdb_path='sqlite:///test-permcheck.authdb.sqlite', override_permissions_json=permissions_json, raiseonfail=True, ) assert access_check['success'] is False assert ( ""Access request check successful. Access granted: False."" in access_check['messages'] ) # 5. as an unknown user with superuser privileges, try to edit a private # dataset access_check = actions.check_user_access( {'user_id':10, 'user_role':'superuser', 'action':'edit', 'target_name':'dataset', 'target_owner':1, 'target_visibility':'private', 'target_sharedwith':'', 'reqid':1, 'pii_salt':'dummy-pii-salt'}, override_authdb_path='sqlite:///test-permcheck.authdb.sqlite', override_permissions_json=permissions_json, raiseonfail=True, ) assert access_check['success'] is False assert ( ""Access request check successful. Access granted: False."" in access_check['messages'] ) # 6. as a known user but non-activated account, try to view a collection access_check = actions.check_user_access( {'user_id':5, 'user_role':'authenticated', 'action':'view', 'target_name':'collection', 'target_owner':1, 'target_visibility':'public', 'target_sharedwith':'', 'reqid':1, 'pii_salt':'dummy-pii-salt'}, override_authdb_path='sqlite:///test-permcheck.authdb.sqlite', override_permissions_json=permissions_json, raiseonfail=True, ) assert access_check['success'] is False assert ( ""Access request check successful. Access granted: False."" in access_check['messages'] ) # # teardown # currproc = mp.current_process() if getattr(currproc, 'authdb_meta', None): del currproc.authdb_meta if getattr(currproc, 'connection', None): currproc.authdb_conn.close() del currproc.authdb_conn if getattr(currproc, 'authdb_engine', None): currproc.authdb_engine.dispose() del currproc.authdb_engine try: os.remove('test-permcheck.authdb.sqlite') except Exception: pass try: os.remove('test-permcheck.authdb.sqlite-shm') except Exception: pass try: os.remove('test-permcheck.authdb.sqlite-wal') except Exception: pass def test_role_limits(): ''' This tests if we can check the permissions for a logged-in user. ''' try: os.remove('test-permcheck.authdb.sqlite') except Exception: pass try: os.remove('test-permcheck.authdb.sqlite-shm') except Exception: pass try: os.remove('test-permcheck.authdb.sqlite-wal') except Exception: pass get_test_authdb() # create the user user_payload = {'full_name': 'Test User', 'email':'testuser-permcheck@test.org', 'password':'aROwQin9L8nNtPTEMLXd', 'pii_salt':'super-secret-salt', 'reqid':1} user_created = actions.create_new_user( user_payload, override_authdb_path='sqlite:///test-permcheck.authdb.sqlite' ) assert user_created['success'] is True assert user_created['user_email'] == 'testuser-permcheck@test.org' assert ('User account created. Please verify your email address to log in.' in user_created['messages']) # verify our email emailverify = ( actions.set_user_emailaddr_verified( {'email':user_payload['email'], 'user_id': user_created['user_id'], 'pii_salt':'super-secret-salt', 'reqid':1}, override_authdb_path='sqlite:///test-permcheck.authdb.sqlite' ) ) # make a non-verified user user_payload2 = {'full_name': 'Tes", https://github.com/WCraaS/wcraas_common,af209ab412c18473edfaaf2871eef9c0357b91e9,tests/test_wcraas_common.py::test_default_env_load,tests/test_wcraas_common.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Tests for `wcraas_common` package."""""" import os import pytest from collections import namedtuple from wcraas_common.config import AMQPConfig from wcraas_common.wcraas_common import WcraasWorker from wcraas_common.decorator import is_rpc, consume def test_default_env_load(): conf = AMQPConfig.fromenv() assert isinstance(conf, tuple) assert conf._fields == (""host"", ""port"", ""user"", ""password"") assert conf.host == ""localhost"" assert conf.port == 5672 assert conf.user == ""guest"" assert conf.password == ""guest"" def test_custom_env_load(): os.environ[""COTTONTAIL_HOST""] = ""127.0.0.1"" os.environ[""COTTONTAIL_PORT""] = ""5673"" os.environ[""COTTONTAIL_USER""] = ""admin"" os.environ[""COTTONTAIL_PASS""] = ""admin"" conf = AMQPConfig.fromenv() assert conf.host == ""127.0.0.1"" assert conf.port == 5673 assert conf.user == ""admin"" assert conf.password == ""admin"" def test_subclass_worker_creation(): # WcraasWorker is now an abstruct class and cannot be instantiated # without subclassing it to implement the start coroutine. class WcraasWorkerTest(WcraasWorker): async def start(self): pass conf = AMQPConfig.fromenv() worker = WcraasWorkerTest(conf, 20) assert str(worker) == f""{worker.__class__.__name__}(amqp={worker.amqp}, loglevel={worker.loglevel})"" assert worker._amqp_pool is not None def test_direct_worker_creation(): conf = AMQPConfig.fromenv() # Ensure direct creation of WcraasWorker is not allowed with pytest.raises(TypeError): worker = WcraasWorker(conf, 20) def test_decorator_is_rpc(): command_name = ""awesome_command"" def fun(): pass assert getattr(fun, ""is_rpc"", False) is False @is_rpc(command_name) def fun(): pass assert getattr(fun, ""is_rpc"", False) is True assert getattr(fun, ""rpc_command"", None) == command_name def test_decorator_consume(): queue_name = ""awesome_queue"" def fun(): pass assert getattr(fun, ""is_consume"", False) is False @consume(queue_name) def fun(): pass assert getattr(fun, ""is_consume"", False) is True assert getattr(fun, ""consume_queue"", None) == queue_name def test_worker_func_discover(): # WcraasWorker is now an abstruct class and cannot be instantiated # without subclassing it to implement the start coroutine. class WcraasWorkerTest(WcraasWorker): async def start(self): pass conf = AMQPConfig.fromenv() w = WcraasWorkerTest(conf, 20) assert w._discover(""is_rpc"") == [] assert w._discover(""is_consume"") == [] @is_rpc(""awesome_command"") def foo(self): pass @consume(""awesome_queue"") def bar(self): pass WcraasWorker.foo = foo WcraasWorker.bar = bar assert w._discover(""is_consume"") == [w.bar] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_caching.py::test_resilient_between_timecaches,tests/test_caching.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import os import time from logging import getLogger from uuid import uuid4 import random import weakref import gc import pytest from easypy.bunch import Bunch from easypy.caching import timecache, PersistentCache, cached_property, locking_cache from easypy.units import DAY from easypy.resilience import resilient _logger = getLogger(__name__) def test_timecache(): ts = 0 data = Bunch(a=0, b=0) def get_ts(): return ts @timecache(expiration=1, get_ts_func=get_ts, key_func=lambda k: k) def inc(k, x): x += 1 data[k] += 1 assert data.a == data.b == 0 inc('a', random.random()) assert (data.a, data.b) == (1, 0) inc('a', x=random.random()) assert (data.a, data.b) == (1, 0) ts += 1 inc('a', random.random()) assert (data.a, data.b) == (2, 0) inc('b', x=random.random()) assert (data.a, data.b) == (2, 1) inc('b', random.random()) assert (data.a, data.b) == (2, 1) ts += 1 inc('b', x=random.random()) assert (data.a, data.b) == (2, 2) inc.cache_clear() inc('a', x=random.random()) assert (data.a, data.b) == (3, 2) inc('b', x=random.random()) assert (data.a, data.b) == (3, 3) inc.cache_clear() inc('a', x=random.random()) inc('b', x=random.random()) inc('a', x=random.random()) inc('b', x=random.random()) assert (data.a, data.b) == (4, 4) inc.cache_pop('a', x=random.random()) inc('a', x=random.random()) inc('b', x=random.random()) def test_timecache_method(): ts = 0 def get_ts(): return ts class Foo: def __init__(self, prefix): self.prefix = prefix @timecache(expiration=1, get_ts_func=get_ts, key_func=lambda args: args) def foo(self, *args): return [self.prefix] + list(args) foo1 = Foo(1) foo2 = Foo(2) assert foo1.foo(1, 2, 3) == foo1.foo(1, 2, 3) assert foo1.foo(1, 2, 3) != foo1.foo(1, 2, 4) assert foo1.foo(1, 2, 3) != foo2.foo(1, 2, 3) foo1_1 = foo1.foo(1) foo1_2 = foo1.foo(2) foo2_1 = foo2.foo(1) foo2_2 = foo2.foo(2) assert foo1_1 == [1, 1] assert foo1_2 == [1, 2] assert foo2_1 == [2, 1] assert foo2_2 == [2, 2] assert foo1_1 is foo1.foo(1) assert foo1_2 is foo1.foo(2) assert foo2_1 is foo2.foo(1) assert foo2_2 is foo2.foo(2) assert foo1_1 is foo1.foo(1) assert foo1_2 is foo1.foo(2) assert foo2_1 is foo2.foo(1) assert foo2_2 is foo2.foo(2) foo1.foo.cache_clear() foo2.foo.cache_pop(1) assert foo1_1 is not foo1.foo(1) assert foo1_2 is not foo1.foo(2) assert foo2_1 is not foo2.foo(1) assert foo2_2 is foo2.foo(2) def test_timecache_getattr(): ts = 0 def get_ts(): return ts class Foo: def __init__(self): self.count = 0 @timecache(expiration=1, get_ts_func=get_ts) def __getattr__(self, name): self.count += 1 return [self.count, name] foo = Foo() assert foo.bar == [1, 'bar'] assert foo.bar == [1, 'bar'] assert foo.baz == [2, 'baz'] ts += 1 assert foo.baz == [3, 'baz'] assert foo.bar == [4, 'bar'] @pytest.yield_fixture() def persistent_cache_path(): cache_path = '/tmp/test_pcache_%s' % uuid4() try: yield cache_path finally: try: os.unlink(""%s.db"" % cache_path) except: # noqa pass def test_persistent_cache(persistent_cache_path): ps = PersistentCache(persistent_cache_path, version=1) TEST_KEY = ""test_key"" TEST_VALUE = ""test_value"" ps.set(TEST_KEY, TEST_VALUE) assert ps.get(TEST_KEY) == TEST_VALUE, ""Value does not match set value"" ps = PersistentCache(persistent_cache_path, version=1) assert ps.get(TEST_KEY) == TEST_VALUE, ""Value does not match set value after reopen"" ps = PersistentCache(persistent_cache_path, version=2) with pytest.raises(KeyError): # Changed version should invalidate cache ps.get(TEST_KEY) # Default values assert ps.get(TEST_KEY, default=None) is None, ""Wrong default value returnen(not None)"" assert ps.get(TEST_KEY, default=""1"") == ""1"", ""Wrong default value returned"" # Cached func should be called only once value_generated = False use_cache = True class UnnecessaryFunctionCall(Exception): pass ps = PersistentCache(persistent_cache_path, version=2, ignored_keywords=""x"") @ps(validator=lambda _, **__: use_cache) def cached_func(x): nonlocal value_generated if value_generated: raise UnnecessaryFunctionCall() value_generated = True return True assert cached_func(x=random.random()) is cached_func(x=random.random()) assert value_generated # Testing validator use_cache = False with pytest.raises(UnnecessaryFunctionCall): cached_func(x=random.random()) # Removing data ps.clear() assert ps.get(TEST_KEY, default=None) is None, ""Database was not cleared properly"" # Expiration ps = PersistentCache(persistent_cache_path, version=3, expiration=.01) ps.set(TEST_KEY, TEST_VALUE) time.sleep(0.011) assert ps.get(TEST_KEY, None) is None, ""Database was not cleaned up on expiration"" def test_locking_timecache(): from easypy.concurrency import MultiObject # Cached func should be called only once value_generated = False class UnnecessaryFunctionCall(Exception): pass @timecache(ignored_keywords='x') def test(x): nonlocal value_generated if value_generated: raise UnnecessaryFunctionCall() value_generated = True return True MultiObject(range(10)).call(lambda x: test(x=x)) @pytest.mark.parametrize('cache_decorator', [cached_property, timecache()]) def test_caching_gc_leaks(cache_decorator): """""" Make sure that the cache does not prevent GC collection once the original objects die """""" class Leaked(): pass class Foo: @cache_decorator def cached_method(self): return Leaked() def get(self): """"""Generalize property type and function type caches"""""" result = self.cached_method if callable(result): result = result() assert isinstance(result, Leaked), 'cache not used properly - got wrong value %s' % (result,) return result foo = Foo() leaked = weakref.ref(foo.get()) gc.collect() assert leaked() == foo.get() del foo gc.collect() assert leaked() is None def test_resilient_between_timecaches(): class ExceptionLeakedThroughResilient(Exception): pass @timecache(1) @resilient(acceptable=ExceptionLeakedThroughResilient, default='default') @timecache(1) def foo(): raise ExceptionLeakedThroughResilient() assert foo() == 'default' ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_concurrency.py::test_concurrent_done_status[False],tests/test_concurrency.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch import pytest from time import sleep from easypy.threadtree import get_thread_stacks, ThreadContexts from easypy.concurrency import concurrent, MultiObject, MultiException @pytest.yield_fixture(params=[True, False], ids=['concurrent', 'nonconcurrent']) def concurrency_enabled_and_disabled(request): if request.param: # concurrency enabled yield else: # concurrency disabled from easypy.concurrency import disable, enable try: disable() yield finally: enable() def test_thread_stacks(): with concurrent(sleep, .1, threadname='sleep'): print(get_thread_stacks().render()) def test_thread_contexts_counters(): TC = ThreadContexts(counters=('i', 'j')) assert TC.i == TC.j == 0 with TC(i=1): def check1(): assert TC.i == 1 assert TC.j == 0 with TC(i=1, j=1): def check2(): assert TC.i == 2 assert TC.j == 1 with concurrent(check2): pass with concurrent(check1): pass def test_thread_contexts_counters_multiobject(): TC = ThreadContexts(counters=('i',)) assert TC.i == 0 print(""---"") @TC(i=True) def test(n): print(n, TC._context_data) sleep(.1) return TC.i test(0) ret = MultiObject(range(10)).call(test) assert set(ret) == {1} def test_thread_context_stacks(): TC = ThreadContexts(stacks=('i', 'j')) assert TC.i == TC.j == [] with TC(i='a'): def check1(): assert TC.i == ['a'] assert TC.j == [] with TC(i='i', j='j'): def check2(): assert TC.i == ['a', 'i'] assert TC.j == ['j'] with concurrent(check2): pass with concurrent(check1): pass def test_multiobject_0(): x = MultiObject([]).foo() assert len(x) == 0 assert x.__class__.CONCESTOR is object def test_multiobject_1(): m = MultiObject(range(10)) def mul(a, b, *c): return a * b + sum(c) assert sum(m.call(mul, 2)) == 90 assert sum(m.call(mul, b=10)) == 450 assert sum(m.call(mul, 1, 1, 1)) == 65 assert m.filter(None).T == (1, 2, 3, 4, 5, 6, 7, 8, 9) assert sum(m.denominator) == 10 with pytest.raises(MultiException) as info: m.call(lambda i: 1 / (i % 2)) assert info.value.count == 5 assert info.value.common_type == ZeroDivisionError assert not info.value.complete def test_multiobject_exceptions(): assert MultiException[ValueError] is MultiException[ValueError] assert issubclass(MultiException[UnicodeDecodeError], MultiException[UnicodeError]) assert issubclass(MultiException[UnicodeDecodeError], MultiException[ValueError]) with pytest.raises(AssertionError): MultiException[0] with pytest.raises(MultiException): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[Exception]): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[ZeroDivisionError]): MultiObject(range(5)).call(lambda n: 1 / n) try: MultiObject(range(5)).call(lambda n: 1 / n) except MultiException[ValueError] as exc: assert False except MultiException[ZeroDivisionError] as exc: assert len(exc.actual) == 1 assert isinstance(exc.one, ZeroDivisionError) else: assert False with pytest.raises(MultiException[ArithmeticError]): try: MultiObject(range(5)).call(lambda n: 1 / n) except ZeroDivisionError: assert False # shouldn't be here except MultiException[ValueError]: assert False # shouldn't be here class ExceptionForPicklingTest(ArithmeticError): pass def test_multiexception_pickling(): import pickle import multiprocessing def throw(n): if not n: raise ExceptionForPicklingTest(n) def fail_and_dump(queue): try: MultiObject(range(5)).call(throw) except MultiException[ArithmeticError] as exc: p = pickle.dumps(exc) queue.put_nowait(p) queue = multiprocessing.Queue(1) process = multiprocessing.Process(target=fail_and_dump, args=(queue,)) process.start() process.join() p = queue.get_nowait() exc = pickle.loads(p) assert isinstance(exc, MultiException[ExceptionForPicklingTest]) assert exc.common_type is ExceptionForPicklingTest assert exc.exceptions[0].args == (0,) assert exc.exceptions[1:] == [None] * 4 def test_multiobject_concurrent_find_found(): m = MultiObject(range(10)) from time import sleep ret = m.concurrent_find(lambda n: sleep(n / 10) or n) # n==0 is not nonzero, so it's not eligible assert ret == 1 def test_multiobject_concurrent_find_not_found(): m = MultiObject(range(10)) ret = m.concurrent_find(lambda n: n < 0) assert ret is False m = MultiObject([0] * 5) ret = m.concurrent_find(lambda n: n) assert ret == 0 def test_multiobject_concurrent_find_proper_shutdown(): executed = [] m = MultiObject(range(10), workers=1) ret = m.concurrent_find(lambda n: [print(n) or executed.append(n) or sleep(.01)]) assert ret sleep(1) # wait for potential stragglers assert max(executed) <= 2 def test_multiobject_zip_with(): m = MultiObject(range(4)) with pytest.raises(AssertionError): m.zip_with(range(3), range(5)) # too few objects m.zip_with(range(5), range(6)) # too many objects ret = m.zip_with(range(1, 5)).call(lambda a, b: a + b).T assert ret == (1, 3, 5, 7) def test_multiobject_enumerate(): m = MultiObject(range(5), log_ctx=""abcd"") def check(i, j): assert i == j + 1 e = m.enumerate(1) assert e._log_ctx == tuple(""abcd"") e.call(check) def test_multiobject_logging(): m = MultiObject(range(4), log_ctx=""abcd"", initial_log_interval=0.1) def check(i): sleep(.2) # we'll mock the logger so we can ensure it logged with patch(""easypy.concurrency._logger"") as _logger: m.call(check) args_list = (c[0] for c in _logger.info.call_args_list) for args in args_list: assert ""test_multiobject_logging..check"" == args[2] assert ""easypy/tests/test_concurrency.py"" in args[4] def test_multiobject_types(): assert isinstance(MultiObject(range(5)), MultiObject[int]) assert not isinstance(MultiObject(range(5)), MultiObject[str]) class A(): ... class B(A): ... assert issubclass(MultiObject[A], MultiObject) assert not issubclass(MultiObject[A], A) assert issubclass(MultiObject[B], MultiObject[A]) assert not issubclass(MultiObject[A], MultiObject[B]) assert isinstance(MultiObject([B()]), MultiObject[A]) assert not isinstance(MultiObject([A()]), MultiObject[B]) assert isinstance(MultiObject[A]([B()]), MultiObject[A]) assert isinstance(MultiObject[A]([B()]), MultiObject[B]) assert isinstance(MultiObject[int](range(5)), MultiObject[int]) with pytest.raises(TypeError): assert MultiObject[str](range(5)) assert isinstance(MultiObject[str](""123"").call(int), MultiObject[int]) def test_multiobject_namedtuples(): from collections import namedtuple class Something(namedtuple(""Something"", ""a b"")): pass def ensure_not_expanded(something): # This will probably fail before these asserts assert hasattr(something, 'a') assert hasattr(something, 'b') objects = [Something(1, 2), Something(2, 3), Something(3, 4)] MultiObject(objects).call(ensure_not_expanded) @pytest.mark.usefixtures('concurrency_enabled_and_disabled') def test_multiexception_api(): with pytest.raises(MultiException) as exc: MultiObject([0, 5]).call(lambda i: 10 // i) failed, sucsessful = exc.value.futures assert failed.done() with pytest.raises(ZeroDivisionError): failed.result() assert isinstance(failed.exception(), ZeroDivisionError) assert sucsessful.done() assert sucsessful.result() == 2 assert sucsessful.exception() is None def test_multiexception_types(): class OK(Exception): pass class BAD(object): pass class OKBAD(OK, BAD): pass with pytest.raises(AssertionError): MultiException[BAD] def raise_it(typ): raise typ() with pytest.raises(MultiException[OK]): MultiObject([OK]).call(raise_it) with pytest.raises(MultiException[OKBAD]): MultiObject([OKBAD]).call(raise_it) with pytest.raises(MultiException[OK]): MultiObject([OKBAD]).call(raise_it) @pytest.mark.parametrize('throw', [False, True]) def test_concurrent_done_status(throw): from threading import Event continue_func = Event() def func(): continue_func.wait() if throw: raise Exception() with concurrent(func, throw=False) as c: assert not c.done() continue_func.set() sleep(0.1) assert c.done() assert c.done() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_concurrency.py::test_concurrent_done_status[True],tests/test_concurrency.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch import pytest from time import sleep from easypy.threadtree import get_thread_stacks, ThreadContexts from easypy.concurrency import concurrent, MultiObject, MultiException @pytest.yield_fixture(params=[True, False], ids=['concurrent', 'nonconcurrent']) def concurrency_enabled_and_disabled(request): if request.param: # concurrency enabled yield else: # concurrency disabled from easypy.concurrency import disable, enable try: disable() yield finally: enable() def test_thread_stacks(): with concurrent(sleep, .1, threadname='sleep'): print(get_thread_stacks().render()) def test_thread_contexts_counters(): TC = ThreadContexts(counters=('i', 'j')) assert TC.i == TC.j == 0 with TC(i=1): def check1(): assert TC.i == 1 assert TC.j == 0 with TC(i=1, j=1): def check2(): assert TC.i == 2 assert TC.j == 1 with concurrent(check2): pass with concurrent(check1): pass def test_thread_contexts_counters_multiobject(): TC = ThreadContexts(counters=('i',)) assert TC.i == 0 print(""---"") @TC(i=True) def test(n): print(n, TC._context_data) sleep(.1) return TC.i test(0) ret = MultiObject(range(10)).call(test) assert set(ret) == {1} def test_thread_context_stacks(): TC = ThreadContexts(stacks=('i', 'j')) assert TC.i == TC.j == [] with TC(i='a'): def check1(): assert TC.i == ['a'] assert TC.j == [] with TC(i='i', j='j'): def check2(): assert TC.i == ['a', 'i'] assert TC.j == ['j'] with concurrent(check2): pass with concurrent(check1): pass def test_multiobject_0(): x = MultiObject([]).foo() assert len(x) == 0 assert x.__class__.CONCESTOR is object def test_multiobject_1(): m = MultiObject(range(10)) def mul(a, b, *c): return a * b + sum(c) assert sum(m.call(mul, 2)) == 90 assert sum(m.call(mul, b=10)) == 450 assert sum(m.call(mul, 1, 1, 1)) == 65 assert m.filter(None).T == (1, 2, 3, 4, 5, 6, 7, 8, 9) assert sum(m.denominator) == 10 with pytest.raises(MultiException) as info: m.call(lambda i: 1 / (i % 2)) assert info.value.count == 5 assert info.value.common_type == ZeroDivisionError assert not info.value.complete def test_multiobject_exceptions(): assert MultiException[ValueError] is MultiException[ValueError] assert issubclass(MultiException[UnicodeDecodeError], MultiException[UnicodeError]) assert issubclass(MultiException[UnicodeDecodeError], MultiException[ValueError]) with pytest.raises(AssertionError): MultiException[0] with pytest.raises(MultiException): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[Exception]): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[ZeroDivisionError]): MultiObject(range(5)).call(lambda n: 1 / n) try: MultiObject(range(5)).call(lambda n: 1 / n) except MultiException[ValueError] as exc: assert False except MultiException[ZeroDivisionError] as exc: assert len(exc.actual) == 1 assert isinstance(exc.one, ZeroDivisionError) else: assert False with pytest.raises(MultiException[ArithmeticError]): try: MultiObject(range(5)).call(lambda n: 1 / n) except ZeroDivisionError: assert False # shouldn't be here except MultiException[ValueError]: assert False # shouldn't be here class ExceptionForPicklingTest(ArithmeticError): pass def test_multiexception_pickling(): import pickle import multiprocessing def throw(n): if not n: raise ExceptionForPicklingTest(n) def fail_and_dump(queue): try: MultiObject(range(5)).call(throw) except MultiException[ArithmeticError] as exc: p = pickle.dumps(exc) queue.put_nowait(p) queue = multiprocessing.Queue(1) process = multiprocessing.Process(target=fail_and_dump, args=(queue,)) process.start() process.join() p = queue.get_nowait() exc = pickle.loads(p) assert isinstance(exc, MultiException[ExceptionForPicklingTest]) assert exc.common_type is ExceptionForPicklingTest assert exc.exceptions[0].args == (0,) assert exc.exceptions[1:] == [None] * 4 def test_multiobject_concurrent_find_found(): m = MultiObject(range(10)) from time import sleep ret = m.concurrent_find(lambda n: sleep(n / 10) or n) # n==0 is not nonzero, so it's not eligible assert ret == 1 def test_multiobject_concurrent_find_not_found(): m = MultiObject(range(10)) ret = m.concurrent_find(lambda n: n < 0) assert ret is False m = MultiObject([0] * 5) ret = m.concurrent_find(lambda n: n) assert ret == 0 def test_multiobject_concurrent_find_proper_shutdown(): executed = [] m = MultiObject(range(10), workers=1) ret = m.concurrent_find(lambda n: [print(n) or executed.append(n) or sleep(.01)]) assert ret sleep(1) # wait for potential stragglers assert max(executed) <= 2 def test_multiobject_zip_with(): m = MultiObject(range(4)) with pytest.raises(AssertionError): m.zip_with(range(3), range(5)) # too few objects m.zip_with(range(5), range(6)) # too many objects ret = m.zip_with(range(1, 5)).call(lambda a, b: a + b).T assert ret == (1, 3, 5, 7) def test_multiobject_enumerate(): m = MultiObject(range(5), log_ctx=""abcd"") def check(i, j): assert i == j + 1 e = m.enumerate(1) assert e._log_ctx == tuple(""abcd"") e.call(check) def test_multiobject_logging(): m = MultiObject(range(4), log_ctx=""abcd"", initial_log_interval=0.1) def check(i): sleep(.2) # we'll mock the logger so we can ensure it logged with patch(""easypy.concurrency._logger"") as _logger: m.call(check) args_list = (c[0] for c in _logger.info.call_args_list) for args in args_list: assert ""test_multiobject_logging..check"" == args[2] assert ""easypy/tests/test_concurrency.py"" in args[4] def test_multiobject_types(): assert isinstance(MultiObject(range(5)), MultiObject[int]) assert not isinstance(MultiObject(range(5)), MultiObject[str]) class A(): ... class B(A): ... assert issubclass(MultiObject[A], MultiObject) assert not issubclass(MultiObject[A], A) assert issubclass(MultiObject[B], MultiObject[A]) assert not issubclass(MultiObject[A], MultiObject[B]) assert isinstance(MultiObject([B()]), MultiObject[A]) assert not isinstance(MultiObject([A()]), MultiObject[B]) assert isinstance(MultiObject[A]([B()]), MultiObject[A]) assert isinstance(MultiObject[A]([B()]), MultiObject[B]) assert isinstance(MultiObject[int](range(5)), MultiObject[int]) with pytest.raises(TypeError): assert MultiObject[str](range(5)) assert isinstance(MultiObject[str](""123"").call(int), MultiObject[int]) def test_multiobject_namedtuples(): from collections import namedtuple class Something(namedtuple(""Something"", ""a b"")): pass def ensure_not_expanded(something): # This will probably fail before these asserts assert hasattr(something, 'a') assert hasattr(something, 'b') objects = [Something(1, 2), Something(2, 3), Something(3, 4)] MultiObject(objects).call(ensure_not_expanded) @pytest.mark.usefixtures('concurrency_enabled_and_disabled') def test_multiexception_api(): with pytest.raises(MultiException) as exc: MultiObject([0, 5]).call(lambda i: 10 // i) failed, sucsessful = exc.value.futures assert failed.done() with pytest.raises(ZeroDivisionError): failed.result() assert isinstance(failed.exception(), ZeroDivisionError) assert sucsessful.done() assert sucsessful.result() == 2 assert sucsessful.exception() is None def test_multiexception_types(): class OK(Exception): pass class BAD(object): pass class OKBAD(OK, BAD): pass with pytest.raises(AssertionError): MultiException[BAD] def raise_it(typ): raise typ() with pytest.raises(MultiException[OK]): MultiObject([OK]).call(raise_it) with pytest.raises(MultiException[OKBAD]): MultiObject([OKBAD]).call(raise_it) with pytest.raises(MultiException[OK]): MultiObject([OKBAD]).call(raise_it) @pytest.mark.parametrize('throw', [False, True]) def test_concurrent_done_status(throw): from threading import Event continue_func = Event() def func(): continue_func.wait() if throw: raise Exception() with concurrent(func, throw=False) as c: assert not c.done() continue_func.set() sleep(0.1) assert c.done() assert c.done() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_concurrency.py::test_multiexception_api[concurrent],tests/test_concurrency.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch import pytest from time import sleep from easypy.threadtree import get_thread_stacks, ThreadContexts from easypy.concurrency import concurrent, MultiObject, MultiException @pytest.yield_fixture(params=[True, False], ids=['concurrent', 'nonconcurrent']) def concurrency_enabled_and_disabled(request): if request.param: # concurrency enabled yield else: # concurrency disabled from easypy.concurrency import disable, enable try: disable() yield finally: enable() def test_thread_stacks(): with concurrent(sleep, .1, threadname='sleep'): print(get_thread_stacks().render()) def test_thread_contexts_counters(): TC = ThreadContexts(counters=('i', 'j')) assert TC.i == TC.j == 0 with TC(i=1): def check1(): assert TC.i == 1 assert TC.j == 0 with TC(i=1, j=1): def check2(): assert TC.i == 2 assert TC.j == 1 with concurrent(check2): pass with concurrent(check1): pass def test_thread_contexts_counters_multiobject(): TC = ThreadContexts(counters=('i',)) assert TC.i == 0 print(""---"") @TC(i=True) def test(n): print(n, TC._context_data) sleep(.1) return TC.i test(0) ret = MultiObject(range(10)).call(test) assert set(ret) == {1} def test_thread_context_stacks(): TC = ThreadContexts(stacks=('i', 'j')) assert TC.i == TC.j == [] with TC(i='a'): def check1(): assert TC.i == ['a'] assert TC.j == [] with TC(i='i', j='j'): def check2(): assert TC.i == ['a', 'i'] assert TC.j == ['j'] with concurrent(check2): pass with concurrent(check1): pass def test_multiobject_0(): x = MultiObject([]).foo() assert len(x) == 0 assert x.__class__.CONCESTOR is object def test_multiobject_1(): m = MultiObject(range(10)) def mul(a, b, *c): return a * b + sum(c) assert sum(m.call(mul, 2)) == 90 assert sum(m.call(mul, b=10)) == 450 assert sum(m.call(mul, 1, 1, 1)) == 65 assert m.filter(None).T == (1, 2, 3, 4, 5, 6, 7, 8, 9) assert sum(m.denominator) == 10 with pytest.raises(MultiException) as info: m.call(lambda i: 1 / (i % 2)) assert info.value.count == 5 assert info.value.common_type == ZeroDivisionError assert not info.value.complete def test_multiobject_exceptions(): assert MultiException[ValueError] is MultiException[ValueError] assert issubclass(MultiException[UnicodeDecodeError], MultiException[UnicodeError]) assert issubclass(MultiException[UnicodeDecodeError], MultiException[ValueError]) with pytest.raises(AssertionError): MultiException[0] with pytest.raises(MultiException): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[Exception]): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[ZeroDivisionError]): MultiObject(range(5)).call(lambda n: 1 / n) try: MultiObject(range(5)).call(lambda n: 1 / n) except MultiException[ValueError] as exc: assert False except MultiException[ZeroDivisionError] as exc: assert len(exc.actual) == 1 assert isinstance(exc.one, ZeroDivisionError) else: assert False with pytest.raises(MultiException[ArithmeticError]): try: MultiObject(range(5)).call(lambda n: 1 / n) except ZeroDivisionError: assert False # shouldn't be here except MultiException[ValueError]: assert False # shouldn't be here class ExceptionForPicklingTest(ArithmeticError): pass def test_multiexception_pickling(): import pickle import multiprocessing def throw(n): if not n: raise ExceptionForPicklingTest(n) def fail_and_dump(queue): try: MultiObject(range(5)).call(throw) except MultiException[ArithmeticError] as exc: p = pickle.dumps(exc) queue.put_nowait(p) queue = multiprocessing.Queue(1) process = multiprocessing.Process(target=fail_and_dump, args=(queue,)) process.start() process.join() p = queue.get_nowait() exc = pickle.loads(p) assert isinstance(exc, MultiException[ExceptionForPicklingTest]) assert exc.common_type is ExceptionForPicklingTest assert exc.exceptions[0].args == (0,) assert exc.exceptions[1:] == [None] * 4 def test_multiobject_concurrent_find_found(): m = MultiObject(range(10)) from time import sleep ret = m.concurrent_find(lambda n: sleep(n / 10) or n) # n==0 is not nonzero, so it's not eligible assert ret == 1 def test_multiobject_concurrent_find_not_found(): m = MultiObject(range(10)) ret = m.concurrent_find(lambda n: n < 0) assert ret is False m = MultiObject([0] * 5) ret = m.concurrent_find(lambda n: n) assert ret == 0 def test_multiobject_concurrent_find_proper_shutdown(): executed = [] m = MultiObject(range(10), workers=1) ret = m.concurrent_find(lambda n: [print(n) or executed.append(n) or sleep(.01)]) assert ret sleep(1) # wait for potential stragglers assert max(executed) <= 2 def test_multiobject_zip_with(): m = MultiObject(range(4)) with pytest.raises(AssertionError): m.zip_with(range(3), range(5)) # too few objects m.zip_with(range(5), range(6)) # too many objects ret = m.zip_with(range(1, 5)).call(lambda a, b: a + b).T assert ret == (1, 3, 5, 7) def test_multiobject_enumerate(): m = MultiObject(range(5), log_ctx=""abcd"") def check(i, j): assert i == j + 1 e = m.enumerate(1) assert e._log_ctx == tuple(""abcd"") e.call(check) def test_multiobject_logging(): m = MultiObject(range(4), log_ctx=""abcd"", initial_log_interval=0.1) def check(i): sleep(.2) # we'll mock the logger so we can ensure it logged with patch(""easypy.concurrency._logger"") as _logger: m.call(check) args_list = (c[0] for c in _logger.info.call_args_list) for args in args_list: assert ""test_multiobject_logging..check"" == args[2] assert ""easypy/tests/test_concurrency.py"" in args[4] def test_multiobject_types(): assert isinstance(MultiObject(range(5)), MultiObject[int]) assert not isinstance(MultiObject(range(5)), MultiObject[str]) class A(): ... class B(A): ... assert issubclass(MultiObject[A], MultiObject) assert not issubclass(MultiObject[A], A) assert issubclass(MultiObject[B], MultiObject[A]) assert not issubclass(MultiObject[A], MultiObject[B]) assert isinstance(MultiObject([B()]), MultiObject[A]) assert not isinstance(MultiObject([A()]), MultiObject[B]) assert isinstance(MultiObject[A]([B()]), MultiObject[A]) assert isinstance(MultiObject[A]([B()]), MultiObject[B]) assert isinstance(MultiObject[int](range(5)), MultiObject[int]) with pytest.raises(TypeError): assert MultiObject[str](range(5)) assert isinstance(MultiObject[str](""123"").call(int), MultiObject[int]) def test_multiobject_namedtuples(): from collections import namedtuple class Something(namedtuple(""Something"", ""a b"")): pass def ensure_not_expanded(something): # This will probably fail before these asserts assert hasattr(something, 'a') assert hasattr(something, 'b') objects = [Something(1, 2), Something(2, 3), Something(3, 4)] MultiObject(objects).call(ensure_not_expanded) @pytest.mark.usefixtures('concurrency_enabled_and_disabled') def test_multiexception_api(): with pytest.raises(MultiException) as exc: MultiObject([0, 5]).call(lambda i: 10 // i) failed, sucsessful = exc.value.futures assert failed.done() with pytest.raises(ZeroDivisionError): failed.result() assert isinstance(failed.exception(), ZeroDivisionError) assert sucsessful.done() assert sucsessful.result() == 2 assert sucsessful.exception() is None def test_multiexception_types(): class OK(Exception): pass class BAD(object): pass class OKBAD(OK, BAD): pass with pytest.raises(AssertionError): MultiException[BAD] def raise_it(typ): raise typ() with pytest.raises(MultiException[OK]): MultiObject([OK]).call(raise_it) with pytest.raises(MultiException[OKBAD]): MultiObject([OKBAD]).call(raise_it) with pytest.raises(MultiException[OK]): MultiObject([OKBAD]).call(raise_it) @pytest.mark.parametrize('throw', [False, True]) def test_concurrent_done_status(throw): from threading import Event continue_func = Event() def func(): continue_func.wait() if throw: raise Exception() with concurrent(func, throw=False) as c: assert not c.done() continue_func.set() sleep(0.1) assert c.done() assert c.done() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_concurrency.py::test_multiexception_api[nonconcurrent],tests/test_concurrency.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch import pytest from time import sleep from easypy.threadtree import get_thread_stacks, ThreadContexts from easypy.concurrency import concurrent, MultiObject, MultiException @pytest.yield_fixture(params=[True, False], ids=['concurrent', 'nonconcurrent']) def concurrency_enabled_and_disabled(request): if request.param: # concurrency enabled yield else: # concurrency disabled from easypy.concurrency import disable, enable try: disable() yield finally: enable() def test_thread_stacks(): with concurrent(sleep, .1, threadname='sleep'): print(get_thread_stacks().render()) def test_thread_contexts_counters(): TC = ThreadContexts(counters=('i', 'j')) assert TC.i == TC.j == 0 with TC(i=1): def check1(): assert TC.i == 1 assert TC.j == 0 with TC(i=1, j=1): def check2(): assert TC.i == 2 assert TC.j == 1 with concurrent(check2): pass with concurrent(check1): pass def test_thread_contexts_counters_multiobject(): TC = ThreadContexts(counters=('i',)) assert TC.i == 0 print(""---"") @TC(i=True) def test(n): print(n, TC._context_data) sleep(.1) return TC.i test(0) ret = MultiObject(range(10)).call(test) assert set(ret) == {1} def test_thread_context_stacks(): TC = ThreadContexts(stacks=('i', 'j')) assert TC.i == TC.j == [] with TC(i='a'): def check1(): assert TC.i == ['a'] assert TC.j == [] with TC(i='i', j='j'): def check2(): assert TC.i == ['a', 'i'] assert TC.j == ['j'] with concurrent(check2): pass with concurrent(check1): pass def test_multiobject_0(): x = MultiObject([]).foo() assert len(x) == 0 assert x.__class__.CONCESTOR is object def test_multiobject_1(): m = MultiObject(range(10)) def mul(a, b, *c): return a * b + sum(c) assert sum(m.call(mul, 2)) == 90 assert sum(m.call(mul, b=10)) == 450 assert sum(m.call(mul, 1, 1, 1)) == 65 assert m.filter(None).T == (1, 2, 3, 4, 5, 6, 7, 8, 9) assert sum(m.denominator) == 10 with pytest.raises(MultiException) as info: m.call(lambda i: 1 / (i % 2)) assert info.value.count == 5 assert info.value.common_type == ZeroDivisionError assert not info.value.complete def test_multiobject_exceptions(): assert MultiException[ValueError] is MultiException[ValueError] assert issubclass(MultiException[UnicodeDecodeError], MultiException[UnicodeError]) assert issubclass(MultiException[UnicodeDecodeError], MultiException[ValueError]) with pytest.raises(AssertionError): MultiException[0] with pytest.raises(MultiException): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[Exception]): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[ZeroDivisionError]): MultiObject(range(5)).call(lambda n: 1 / n) try: MultiObject(range(5)).call(lambda n: 1 / n) except MultiException[ValueError] as exc: assert False except MultiException[ZeroDivisionError] as exc: assert len(exc.actual) == 1 assert isinstance(exc.one, ZeroDivisionError) else: assert False with pytest.raises(MultiException[ArithmeticError]): try: MultiObject(range(5)).call(lambda n: 1 / n) except ZeroDivisionError: assert False # shouldn't be here except MultiException[ValueError]: assert False # shouldn't be here class ExceptionForPicklingTest(ArithmeticError): pass def test_multiexception_pickling(): import pickle import multiprocessing def throw(n): if not n: raise ExceptionForPicklingTest(n) def fail_and_dump(queue): try: MultiObject(range(5)).call(throw) except MultiException[ArithmeticError] as exc: p = pickle.dumps(exc) queue.put_nowait(p) queue = multiprocessing.Queue(1) process = multiprocessing.Process(target=fail_and_dump, args=(queue,)) process.start() process.join() p = queue.get_nowait() exc = pickle.loads(p) assert isinstance(exc, MultiException[ExceptionForPicklingTest]) assert exc.common_type is ExceptionForPicklingTest assert exc.exceptions[0].args == (0,) assert exc.exceptions[1:] == [None] * 4 def test_multiobject_concurrent_find_found(): m = MultiObject(range(10)) from time import sleep ret = m.concurrent_find(lambda n: sleep(n / 10) or n) # n==0 is not nonzero, so it's not eligible assert ret == 1 def test_multiobject_concurrent_find_not_found(): m = MultiObject(range(10)) ret = m.concurrent_find(lambda n: n < 0) assert ret is False m = MultiObject([0] * 5) ret = m.concurrent_find(lambda n: n) assert ret == 0 def test_multiobject_concurrent_find_proper_shutdown(): executed = [] m = MultiObject(range(10), workers=1) ret = m.concurrent_find(lambda n: [print(n) or executed.append(n) or sleep(.01)]) assert ret sleep(1) # wait for potential stragglers assert max(executed) <= 2 def test_multiobject_zip_with(): m = MultiObject(range(4)) with pytest.raises(AssertionError): m.zip_with(range(3), range(5)) # too few objects m.zip_with(range(5), range(6)) # too many objects ret = m.zip_with(range(1, 5)).call(lambda a, b: a + b).T assert ret == (1, 3, 5, 7) def test_multiobject_enumerate(): m = MultiObject(range(5), log_ctx=""abcd"") def check(i, j): assert i == j + 1 e = m.enumerate(1) assert e._log_ctx == tuple(""abcd"") e.call(check) def test_multiobject_logging(): m = MultiObject(range(4), log_ctx=""abcd"", initial_log_interval=0.1) def check(i): sleep(.2) # we'll mock the logger so we can ensure it logged with patch(""easypy.concurrency._logger"") as _logger: m.call(check) args_list = (c[0] for c in _logger.info.call_args_list) for args in args_list: assert ""test_multiobject_logging..check"" == args[2] assert ""easypy/tests/test_concurrency.py"" in args[4] def test_multiobject_types(): assert isinstance(MultiObject(range(5)), MultiObject[int]) assert not isinstance(MultiObject(range(5)), MultiObject[str]) class A(): ... class B(A): ... assert issubclass(MultiObject[A], MultiObject) assert not issubclass(MultiObject[A], A) assert issubclass(MultiObject[B], MultiObject[A]) assert not issubclass(MultiObject[A], MultiObject[B]) assert isinstance(MultiObject([B()]), MultiObject[A]) assert not isinstance(MultiObject([A()]), MultiObject[B]) assert isinstance(MultiObject[A]([B()]), MultiObject[A]) assert isinstance(MultiObject[A]([B()]), MultiObject[B]) assert isinstance(MultiObject[int](range(5)), MultiObject[int]) with pytest.raises(TypeError): assert MultiObject[str](range(5)) assert isinstance(MultiObject[str](""123"").call(int), MultiObject[int]) def test_multiobject_namedtuples(): from collections import namedtuple class Something(namedtuple(""Something"", ""a b"")): pass def ensure_not_expanded(something): # This will probably fail before these asserts assert hasattr(something, 'a') assert hasattr(something, 'b') objects = [Something(1, 2), Something(2, 3), Something(3, 4)] MultiObject(objects).call(ensure_not_expanded) @pytest.mark.usefixtures('concurrency_enabled_and_disabled') def test_multiexception_api(): with pytest.raises(MultiException) as exc: MultiObject([0, 5]).call(lambda i: 10 // i) failed, sucsessful = exc.value.futures assert failed.done() with pytest.raises(ZeroDivisionError): failed.result() assert isinstance(failed.exception(), ZeroDivisionError) assert sucsessful.done() assert sucsessful.result() == 2 assert sucsessful.exception() is None def test_multiexception_types(): class OK(Exception): pass class BAD(object): pass class OKBAD(OK, BAD): pass with pytest.raises(AssertionError): MultiException[BAD] def raise_it(typ): raise typ() with pytest.raises(MultiException[OK]): MultiObject([OK]).call(raise_it) with pytest.raises(MultiException[OKBAD]): MultiObject([OKBAD]).call(raise_it) with pytest.raises(MultiException[OK]): MultiObject([OKBAD]).call(raise_it) @pytest.mark.parametrize('throw', [False, True]) def test_concurrent_done_status(throw): from threading import Event continue_func = Event() def func(): continue_func.wait() if throw: raise Exception() with concurrent(func, throw=False) as c: assert not c.done() continue_func.set() sleep(0.1) assert c.done() assert c.done() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_concurrency.py::test_multiexception_pickling,tests/test_concurrency.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch import pytest from time import sleep from easypy.threadtree import get_thread_stacks, ThreadContexts from easypy.concurrency import concurrent, MultiObject, MultiException @pytest.yield_fixture(params=[True, False], ids=['concurrent', 'nonconcurrent']) def concurrency_enabled_and_disabled(request): if request.param: # concurrency enabled yield else: # concurrency disabled from easypy.concurrency import disable, enable try: disable() yield finally: enable() def test_thread_stacks(): with concurrent(sleep, .1, threadname='sleep'): print(get_thread_stacks().render()) def test_thread_contexts_counters(): TC = ThreadContexts(counters=('i', 'j')) assert TC.i == TC.j == 0 with TC(i=1): def check1(): assert TC.i == 1 assert TC.j == 0 with TC(i=1, j=1): def check2(): assert TC.i == 2 assert TC.j == 1 with concurrent(check2): pass with concurrent(check1): pass def test_thread_contexts_counters_multiobject(): TC = ThreadContexts(counters=('i',)) assert TC.i == 0 print(""---"") @TC(i=True) def test(n): print(n, TC._context_data) sleep(.1) return TC.i test(0) ret = MultiObject(range(10)).call(test) assert set(ret) == {1} def test_thread_context_stacks(): TC = ThreadContexts(stacks=('i', 'j')) assert TC.i == TC.j == [] with TC(i='a'): def check1(): assert TC.i == ['a'] assert TC.j == [] with TC(i='i', j='j'): def check2(): assert TC.i == ['a', 'i'] assert TC.j == ['j'] with concurrent(check2): pass with concurrent(check1): pass def test_multiobject_0(): x = MultiObject([]).foo() assert len(x) == 0 assert x.__class__.CONCESTOR is object def test_multiobject_1(): m = MultiObject(range(10)) def mul(a, b, *c): return a * b + sum(c) assert sum(m.call(mul, 2)) == 90 assert sum(m.call(mul, b=10)) == 450 assert sum(m.call(mul, 1, 1, 1)) == 65 assert m.filter(None).T == (1, 2, 3, 4, 5, 6, 7, 8, 9) assert sum(m.denominator) == 10 with pytest.raises(MultiException) as info: m.call(lambda i: 1 / (i % 2)) assert info.value.count == 5 assert info.value.common_type == ZeroDivisionError assert not info.value.complete def test_multiobject_exceptions(): assert MultiException[ValueError] is MultiException[ValueError] assert issubclass(MultiException[UnicodeDecodeError], MultiException[UnicodeError]) assert issubclass(MultiException[UnicodeDecodeError], MultiException[ValueError]) with pytest.raises(AssertionError): MultiException[0] with pytest.raises(MultiException): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[Exception]): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[ZeroDivisionError]): MultiObject(range(5)).call(lambda n: 1 / n) try: MultiObject(range(5)).call(lambda n: 1 / n) except MultiException[ValueError] as exc: assert False except MultiException[ZeroDivisionError] as exc: assert len(exc.actual) == 1 assert isinstance(exc.one, ZeroDivisionError) else: assert False with pytest.raises(MultiException[ArithmeticError]): try: MultiObject(range(5)).call(lambda n: 1 / n) except ZeroDivisionError: assert False # shouldn't be here except MultiException[ValueError]: assert False # shouldn't be here class ExceptionForPicklingTest(ArithmeticError): pass def test_multiexception_pickling(): import pickle import multiprocessing def throw(n): if not n: raise ExceptionForPicklingTest(n) def fail_and_dump(queue): try: MultiObject(range(5)).call(throw) except MultiException[ArithmeticError] as exc: p = pickle.dumps(exc) queue.put_nowait(p) queue = multiprocessing.Queue(1) process = multiprocessing.Process(target=fail_and_dump, args=(queue,)) process.start() process.join() p = queue.get_nowait() exc = pickle.loads(p) assert isinstance(exc, MultiException[ExceptionForPicklingTest]) assert exc.common_type is ExceptionForPicklingTest assert exc.exceptions[0].args == (0,) assert exc.exceptions[1:] == [None] * 4 def test_multiobject_concurrent_find_found(): m = MultiObject(range(10)) from time import sleep ret = m.concurrent_find(lambda n: sleep(n / 10) or n) # n==0 is not nonzero, so it's not eligible assert ret == 1 def test_multiobject_concurrent_find_not_found(): m = MultiObject(range(10)) ret = m.concurrent_find(lambda n: n < 0) assert ret is False m = MultiObject([0] * 5) ret = m.concurrent_find(lambda n: n) assert ret == 0 def test_multiobject_concurrent_find_proper_shutdown(): executed = [] m = MultiObject(range(10), workers=1) ret = m.concurrent_find(lambda n: [print(n) or executed.append(n) or sleep(.01)]) assert ret sleep(1) # wait for potential stragglers assert max(executed) <= 2 def test_multiobject_zip_with(): m = MultiObject(range(4)) with pytest.raises(AssertionError): m.zip_with(range(3), range(5)) # too few objects m.zip_with(range(5), range(6)) # too many objects ret = m.zip_with(range(1, 5)).call(lambda a, b: a + b).T assert ret == (1, 3, 5, 7) def test_multiobject_enumerate(): m = MultiObject(range(5), log_ctx=""abcd"") def check(i, j): assert i == j + 1 e = m.enumerate(1) assert e._log_ctx == tuple(""abcd"") e.call(check) def test_multiobject_logging(): m = MultiObject(range(4), log_ctx=""abcd"", initial_log_interval=0.1) def check(i): sleep(.2) # we'll mock the logger so we can ensure it logged with patch(""easypy.concurrency._logger"") as _logger: m.call(check) args_list = (c[0] for c in _logger.info.call_args_list) for args in args_list: assert ""test_multiobject_logging..check"" == args[2] assert ""easypy/tests/test_concurrency.py"" in args[4] def test_multiobject_types(): assert isinstance(MultiObject(range(5)), MultiObject[int]) assert not isinstance(MultiObject(range(5)), MultiObject[str]) class A(): ... class B(A): ... assert issubclass(MultiObject[A], MultiObject) assert not issubclass(MultiObject[A], A) assert issubclass(MultiObject[B], MultiObject[A]) assert not issubclass(MultiObject[A], MultiObject[B]) assert isinstance(MultiObject([B()]), MultiObject[A]) assert not isinstance(MultiObject([A()]), MultiObject[B]) assert isinstance(MultiObject[A]([B()]), MultiObject[A]) assert isinstance(MultiObject[A]([B()]), MultiObject[B]) assert isinstance(MultiObject[int](range(5)), MultiObject[int]) with pytest.raises(TypeError): assert MultiObject[str](range(5)) assert isinstance(MultiObject[str](""123"").call(int), MultiObject[int]) def test_multiobject_namedtuples(): from collections import namedtuple class Something(namedtuple(""Something"", ""a b"")): pass def ensure_not_expanded(something): # This will probably fail before these asserts assert hasattr(something, 'a') assert hasattr(something, 'b') objects = [Something(1, 2), Something(2, 3), Something(3, 4)] MultiObject(objects).call(ensure_not_expanded) @pytest.mark.usefixtures('concurrency_enabled_and_disabled') def test_multiexception_api(): with pytest.raises(MultiException) as exc: MultiObject([0, 5]).call(lambda i: 10 // i) failed, sucsessful = exc.value.futures assert failed.done() with pytest.raises(ZeroDivisionError): failed.result() assert isinstance(failed.exception(), ZeroDivisionError) assert sucsessful.done() assert sucsessful.result() == 2 assert sucsessful.exception() is None def test_multiexception_types(): class OK(Exception): pass class BAD(object): pass class OKBAD(OK, BAD): pass with pytest.raises(AssertionError): MultiException[BAD] def raise_it(typ): raise typ() with pytest.raises(MultiException[OK]): MultiObject([OK]).call(raise_it) with pytest.raises(MultiException[OKBAD]): MultiObject([OKBAD]).call(raise_it) with pytest.raises(MultiException[OK]): MultiObject([OKBAD]).call(raise_it) @pytest.mark.parametrize('throw', [False, True]) def test_concurrent_done_status(throw): from threading import Event continue_func = Event() def func(): continue_func.wait() if throw: raise Exception() with concurrent(func, throw=False) as c: assert not c.done() continue_func.set() sleep(0.1) assert c.done() assert c.done() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_concurrency.py::test_multiexception_types,tests/test_concurrency.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch import pytest from time import sleep from easypy.threadtree import get_thread_stacks, ThreadContexts from easypy.concurrency import concurrent, MultiObject, MultiException @pytest.yield_fixture(params=[True, False], ids=['concurrent', 'nonconcurrent']) def concurrency_enabled_and_disabled(request): if request.param: # concurrency enabled yield else: # concurrency disabled from easypy.concurrency import disable, enable try: disable() yield finally: enable() def test_thread_stacks(): with concurrent(sleep, .1, threadname='sleep'): print(get_thread_stacks().render()) def test_thread_contexts_counters(): TC = ThreadContexts(counters=('i', 'j')) assert TC.i == TC.j == 0 with TC(i=1): def check1(): assert TC.i == 1 assert TC.j == 0 with TC(i=1, j=1): def check2(): assert TC.i == 2 assert TC.j == 1 with concurrent(check2): pass with concurrent(check1): pass def test_thread_contexts_counters_multiobject(): TC = ThreadContexts(counters=('i',)) assert TC.i == 0 print(""---"") @TC(i=True) def test(n): print(n, TC._context_data) sleep(.1) return TC.i test(0) ret = MultiObject(range(10)).call(test) assert set(ret) == {1} def test_thread_context_stacks(): TC = ThreadContexts(stacks=('i', 'j')) assert TC.i == TC.j == [] with TC(i='a'): def check1(): assert TC.i == ['a'] assert TC.j == [] with TC(i='i', j='j'): def check2(): assert TC.i == ['a', 'i'] assert TC.j == ['j'] with concurrent(check2): pass with concurrent(check1): pass def test_multiobject_0(): x = MultiObject([]).foo() assert len(x) == 0 assert x.__class__.CONCESTOR is object def test_multiobject_1(): m = MultiObject(range(10)) def mul(a, b, *c): return a * b + sum(c) assert sum(m.call(mul, 2)) == 90 assert sum(m.call(mul, b=10)) == 450 assert sum(m.call(mul, 1, 1, 1)) == 65 assert m.filter(None).T == (1, 2, 3, 4, 5, 6, 7, 8, 9) assert sum(m.denominator) == 10 with pytest.raises(MultiException) as info: m.call(lambda i: 1 / (i % 2)) assert info.value.count == 5 assert info.value.common_type == ZeroDivisionError assert not info.value.complete def test_multiobject_exceptions(): assert MultiException[ValueError] is MultiException[ValueError] assert issubclass(MultiException[UnicodeDecodeError], MultiException[UnicodeError]) assert issubclass(MultiException[UnicodeDecodeError], MultiException[ValueError]) with pytest.raises(AssertionError): MultiException[0] with pytest.raises(MultiException): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[Exception]): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[ZeroDivisionError]): MultiObject(range(5)).call(lambda n: 1 / n) try: MultiObject(range(5)).call(lambda n: 1 / n) except MultiException[ValueError] as exc: assert False except MultiException[ZeroDivisionError] as exc: assert len(exc.actual) == 1 assert isinstance(exc.one, ZeroDivisionError) else: assert False with pytest.raises(MultiException[ArithmeticError]): try: MultiObject(range(5)).call(lambda n: 1 / n) except ZeroDivisionError: assert False # shouldn't be here except MultiException[ValueError]: assert False # shouldn't be here class ExceptionForPicklingTest(ArithmeticError): pass def test_multiexception_pickling(): import pickle import multiprocessing def throw(n): if not n: raise ExceptionForPicklingTest(n) def fail_and_dump(queue): try: MultiObject(range(5)).call(throw) except MultiException[ArithmeticError] as exc: p = pickle.dumps(exc) queue.put_nowait(p) queue = multiprocessing.Queue(1) process = multiprocessing.Process(target=fail_and_dump, args=(queue,)) process.start() process.join() p = queue.get_nowait() exc = pickle.loads(p) assert isinstance(exc, MultiException[ExceptionForPicklingTest]) assert exc.common_type is ExceptionForPicklingTest assert exc.exceptions[0].args == (0,) assert exc.exceptions[1:] == [None] * 4 def test_multiobject_concurrent_find_found(): m = MultiObject(range(10)) from time import sleep ret = m.concurrent_find(lambda n: sleep(n / 10) or n) # n==0 is not nonzero, so it's not eligible assert ret == 1 def test_multiobject_concurrent_find_not_found(): m = MultiObject(range(10)) ret = m.concurrent_find(lambda n: n < 0) assert ret is False m = MultiObject([0] * 5) ret = m.concurrent_find(lambda n: n) assert ret == 0 def test_multiobject_concurrent_find_proper_shutdown(): executed = [] m = MultiObject(range(10), workers=1) ret = m.concurrent_find(lambda n: [print(n) or executed.append(n) or sleep(.01)]) assert ret sleep(1) # wait for potential stragglers assert max(executed) <= 2 def test_multiobject_zip_with(): m = MultiObject(range(4)) with pytest.raises(AssertionError): m.zip_with(range(3), range(5)) # too few objects m.zip_with(range(5), range(6)) # too many objects ret = m.zip_with(range(1, 5)).call(lambda a, b: a + b).T assert ret == (1, 3, 5, 7) def test_multiobject_enumerate(): m = MultiObject(range(5), log_ctx=""abcd"") def check(i, j): assert i == j + 1 e = m.enumerate(1) assert e._log_ctx == tuple(""abcd"") e.call(check) def test_multiobject_logging(): m = MultiObject(range(4), log_ctx=""abcd"", initial_log_interval=0.1) def check(i): sleep(.2) # we'll mock the logger so we can ensure it logged with patch(""easypy.concurrency._logger"") as _logger: m.call(check) args_list = (c[0] for c in _logger.info.call_args_list) for args in args_list: assert ""test_multiobject_logging..check"" == args[2] assert ""easypy/tests/test_concurrency.py"" in args[4] def test_multiobject_types(): assert isinstance(MultiObject(range(5)), MultiObject[int]) assert not isinstance(MultiObject(range(5)), MultiObject[str]) class A(): ... class B(A): ... assert issubclass(MultiObject[A], MultiObject) assert not issubclass(MultiObject[A], A) assert issubclass(MultiObject[B], MultiObject[A]) assert not issubclass(MultiObject[A], MultiObject[B]) assert isinstance(MultiObject([B()]), MultiObject[A]) assert not isinstance(MultiObject([A()]), MultiObject[B]) assert isinstance(MultiObject[A]([B()]), MultiObject[A]) assert isinstance(MultiObject[A]([B()]), MultiObject[B]) assert isinstance(MultiObject[int](range(5)), MultiObject[int]) with pytest.raises(TypeError): assert MultiObject[str](range(5)) assert isinstance(MultiObject[str](""123"").call(int), MultiObject[int]) def test_multiobject_namedtuples(): from collections import namedtuple class Something(namedtuple(""Something"", ""a b"")): pass def ensure_not_expanded(something): # This will probably fail before these asserts assert hasattr(something, 'a') assert hasattr(something, 'b') objects = [Something(1, 2), Something(2, 3), Something(3, 4)] MultiObject(objects).call(ensure_not_expanded) @pytest.mark.usefixtures('concurrency_enabled_and_disabled') def test_multiexception_api(): with pytest.raises(MultiException) as exc: MultiObject([0, 5]).call(lambda i: 10 // i) failed, sucsessful = exc.value.futures assert failed.done() with pytest.raises(ZeroDivisionError): failed.result() assert isinstance(failed.exception(), ZeroDivisionError) assert sucsessful.done() assert sucsessful.result() == 2 assert sucsessful.exception() is None def test_multiexception_types(): class OK(Exception): pass class BAD(object): pass class OKBAD(OK, BAD): pass with pytest.raises(AssertionError): MultiException[BAD] def raise_it(typ): raise typ() with pytest.raises(MultiException[OK]): MultiObject([OK]).call(raise_it) with pytest.raises(MultiException[OKBAD]): MultiObject([OKBAD]).call(raise_it) with pytest.raises(MultiException[OK]): MultiObject([OKBAD]).call(raise_it) @pytest.mark.parametrize('throw', [False, True]) def test_concurrent_done_status(throw): from threading import Event continue_func = Event() def func(): continue_func.wait() if throw: raise Exception() with concurrent(func, throw=False) as c: assert not c.done() continue_func.set() sleep(0.1) assert c.done() assert c.done() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_concurrency.py::test_multiobject_1,tests/test_concurrency.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch import pytest from time import sleep from easypy.threadtree import get_thread_stacks, ThreadContexts from easypy.concurrency import concurrent, MultiObject, MultiException @pytest.yield_fixture(params=[True, False], ids=['concurrent', 'nonconcurrent']) def concurrency_enabled_and_disabled(request): if request.param: # concurrency enabled yield else: # concurrency disabled from easypy.concurrency import disable, enable try: disable() yield finally: enable() def test_thread_stacks(): with concurrent(sleep, .1, threadname='sleep'): print(get_thread_stacks().render()) def test_thread_contexts_counters(): TC = ThreadContexts(counters=('i', 'j')) assert TC.i == TC.j == 0 with TC(i=1): def check1(): assert TC.i == 1 assert TC.j == 0 with TC(i=1, j=1): def check2(): assert TC.i == 2 assert TC.j == 1 with concurrent(check2): pass with concurrent(check1): pass def test_thread_contexts_counters_multiobject(): TC = ThreadContexts(counters=('i',)) assert TC.i == 0 print(""---"") @TC(i=True) def test(n): print(n, TC._context_data) sleep(.1) return TC.i test(0) ret = MultiObject(range(10)).call(test) assert set(ret) == {1} def test_thread_context_stacks(): TC = ThreadContexts(stacks=('i', 'j')) assert TC.i == TC.j == [] with TC(i='a'): def check1(): assert TC.i == ['a'] assert TC.j == [] with TC(i='i', j='j'): def check2(): assert TC.i == ['a', 'i'] assert TC.j == ['j'] with concurrent(check2): pass with concurrent(check1): pass def test_multiobject_0(): x = MultiObject([]).foo() assert len(x) == 0 assert x.__class__.CONCESTOR is object def test_multiobject_1(): m = MultiObject(range(10)) def mul(a, b, *c): return a * b + sum(c) assert sum(m.call(mul, 2)) == 90 assert sum(m.call(mul, b=10)) == 450 assert sum(m.call(mul, 1, 1, 1)) == 65 assert m.filter(None).T == (1, 2, 3, 4, 5, 6, 7, 8, 9) assert sum(m.denominator) == 10 with pytest.raises(MultiException) as info: m.call(lambda i: 1 / (i % 2)) assert info.value.count == 5 assert info.value.common_type == ZeroDivisionError assert not info.value.complete def test_multiobject_exceptions(): assert MultiException[ValueError] is MultiException[ValueError] assert issubclass(MultiException[UnicodeDecodeError], MultiException[UnicodeError]) assert issubclass(MultiException[UnicodeDecodeError], MultiException[ValueError]) with pytest.raises(AssertionError): MultiException[0] with pytest.raises(MultiException): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[Exception]): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[ZeroDivisionError]): MultiObject(range(5)).call(lambda n: 1 / n) try: MultiObject(range(5)).call(lambda n: 1 / n) except MultiException[ValueError] as exc: assert False except MultiException[ZeroDivisionError] as exc: assert len(exc.actual) == 1 assert isinstance(exc.one, ZeroDivisionError) else: assert False with pytest.raises(MultiException[ArithmeticError]): try: MultiObject(range(5)).call(lambda n: 1 / n) except ZeroDivisionError: assert False # shouldn't be here except MultiException[ValueError]: assert False # shouldn't be here class ExceptionForPicklingTest(ArithmeticError): pass def test_multiexception_pickling(): import pickle import multiprocessing def throw(n): if not n: raise ExceptionForPicklingTest(n) def fail_and_dump(queue): try: MultiObject(range(5)).call(throw) except MultiException[ArithmeticError] as exc: p = pickle.dumps(exc) queue.put_nowait(p) queue = multiprocessing.Queue(1) process = multiprocessing.Process(target=fail_and_dump, args=(queue,)) process.start() process.join() p = queue.get_nowait() exc = pickle.loads(p) assert isinstance(exc, MultiException[ExceptionForPicklingTest]) assert exc.common_type is ExceptionForPicklingTest assert exc.exceptions[0].args == (0,) assert exc.exceptions[1:] == [None] * 4 def test_multiobject_concurrent_find_found(): m = MultiObject(range(10)) from time import sleep ret = m.concurrent_find(lambda n: sleep(n / 10) or n) # n==0 is not nonzero, so it's not eligible assert ret == 1 def test_multiobject_concurrent_find_not_found(): m = MultiObject(range(10)) ret = m.concurrent_find(lambda n: n < 0) assert ret is False m = MultiObject([0] * 5) ret = m.concurrent_find(lambda n: n) assert ret == 0 def test_multiobject_concurrent_find_proper_shutdown(): executed = [] m = MultiObject(range(10), workers=1) ret = m.concurrent_find(lambda n: [print(n) or executed.append(n) or sleep(.01)]) assert ret sleep(1) # wait for potential stragglers assert max(executed) <= 2 def test_multiobject_zip_with(): m = MultiObject(range(4)) with pytest.raises(AssertionError): m.zip_with(range(3), range(5)) # too few objects m.zip_with(range(5), range(6)) # too many objects ret = m.zip_with(range(1, 5)).call(lambda a, b: a + b).T assert ret == (1, 3, 5, 7) def test_multiobject_enumerate(): m = MultiObject(range(5), log_ctx=""abcd"") def check(i, j): assert i == j + 1 e = m.enumerate(1) assert e._log_ctx == tuple(""abcd"") e.call(check) def test_multiobject_logging(): m = MultiObject(range(4), log_ctx=""abcd"", initial_log_interval=0.1) def check(i): sleep(.2) # we'll mock the logger so we can ensure it logged with patch(""easypy.concurrency._logger"") as _logger: m.call(check) args_list = (c[0] for c in _logger.info.call_args_list) for args in args_list: assert ""test_multiobject_logging..check"" == args[2] assert ""easypy/tests/test_concurrency.py"" in args[4] def test_multiobject_types(): assert isinstance(MultiObject(range(5)), MultiObject[int]) assert not isinstance(MultiObject(range(5)), MultiObject[str]) class A(): ... class B(A): ... assert issubclass(MultiObject[A], MultiObject) assert not issubclass(MultiObject[A], A) assert issubclass(MultiObject[B], MultiObject[A]) assert not issubclass(MultiObject[A], MultiObject[B]) assert isinstance(MultiObject([B()]), MultiObject[A]) assert not isinstance(MultiObject([A()]), MultiObject[B]) assert isinstance(MultiObject[A]([B()]), MultiObject[A]) assert isinstance(MultiObject[A]([B()]), MultiObject[B]) assert isinstance(MultiObject[int](range(5)), MultiObject[int]) with pytest.raises(TypeError): assert MultiObject[str](range(5)) assert isinstance(MultiObject[str](""123"").call(int), MultiObject[int]) def test_multiobject_namedtuples(): from collections import namedtuple class Something(namedtuple(""Something"", ""a b"")): pass def ensure_not_expanded(something): # This will probably fail before these asserts assert hasattr(something, 'a') assert hasattr(something, 'b') objects = [Something(1, 2), Something(2, 3), Something(3, 4)] MultiObject(objects).call(ensure_not_expanded) @pytest.mark.usefixtures('concurrency_enabled_and_disabled') def test_multiexception_api(): with pytest.raises(MultiException) as exc: MultiObject([0, 5]).call(lambda i: 10 // i) failed, sucsessful = exc.value.futures assert failed.done() with pytest.raises(ZeroDivisionError): failed.result() assert isinstance(failed.exception(), ZeroDivisionError) assert sucsessful.done() assert sucsessful.result() == 2 assert sucsessful.exception() is None def test_multiexception_types(): class OK(Exception): pass class BAD(object): pass class OKBAD(OK, BAD): pass with pytest.raises(AssertionError): MultiException[BAD] def raise_it(typ): raise typ() with pytest.raises(MultiException[OK]): MultiObject([OK]).call(raise_it) with pytest.raises(MultiException[OKBAD]): MultiObject([OKBAD]).call(raise_it) with pytest.raises(MultiException[OK]): MultiObject([OKBAD]).call(raise_it) @pytest.mark.parametrize('throw', [False, True]) def test_concurrent_done_status(throw): from threading import Event continue_func = Event() def func(): continue_func.wait() if throw: raise Exception() with concurrent(func, throw=False) as c: assert not c.done() continue_func.set() sleep(0.1) assert c.done() assert c.done() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_concurrency.py::test_multiobject_exceptions,tests/test_concurrency.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch import pytest from time import sleep from easypy.threadtree import get_thread_stacks, ThreadContexts from easypy.concurrency import concurrent, MultiObject, MultiException @pytest.yield_fixture(params=[True, False], ids=['concurrent', 'nonconcurrent']) def concurrency_enabled_and_disabled(request): if request.param: # concurrency enabled yield else: # concurrency disabled from easypy.concurrency import disable, enable try: disable() yield finally: enable() def test_thread_stacks(): with concurrent(sleep, .1, threadname='sleep'): print(get_thread_stacks().render()) def test_thread_contexts_counters(): TC = ThreadContexts(counters=('i', 'j')) assert TC.i == TC.j == 0 with TC(i=1): def check1(): assert TC.i == 1 assert TC.j == 0 with TC(i=1, j=1): def check2(): assert TC.i == 2 assert TC.j == 1 with concurrent(check2): pass with concurrent(check1): pass def test_thread_contexts_counters_multiobject(): TC = ThreadContexts(counters=('i',)) assert TC.i == 0 print(""---"") @TC(i=True) def test(n): print(n, TC._context_data) sleep(.1) return TC.i test(0) ret = MultiObject(range(10)).call(test) assert set(ret) == {1} def test_thread_context_stacks(): TC = ThreadContexts(stacks=('i', 'j')) assert TC.i == TC.j == [] with TC(i='a'): def check1(): assert TC.i == ['a'] assert TC.j == [] with TC(i='i', j='j'): def check2(): assert TC.i == ['a', 'i'] assert TC.j == ['j'] with concurrent(check2): pass with concurrent(check1): pass def test_multiobject_0(): x = MultiObject([]).foo() assert len(x) == 0 assert x.__class__.CONCESTOR is object def test_multiobject_1(): m = MultiObject(range(10)) def mul(a, b, *c): return a * b + sum(c) assert sum(m.call(mul, 2)) == 90 assert sum(m.call(mul, b=10)) == 450 assert sum(m.call(mul, 1, 1, 1)) == 65 assert m.filter(None).T == (1, 2, 3, 4, 5, 6, 7, 8, 9) assert sum(m.denominator) == 10 with pytest.raises(MultiException) as info: m.call(lambda i: 1 / (i % 2)) assert info.value.count == 5 assert info.value.common_type == ZeroDivisionError assert not info.value.complete def test_multiobject_exceptions(): assert MultiException[ValueError] is MultiException[ValueError] assert issubclass(MultiException[UnicodeDecodeError], MultiException[UnicodeError]) assert issubclass(MultiException[UnicodeDecodeError], MultiException[ValueError]) with pytest.raises(AssertionError): MultiException[0] with pytest.raises(MultiException): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[Exception]): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[ZeroDivisionError]): MultiObject(range(5)).call(lambda n: 1 / n) try: MultiObject(range(5)).call(lambda n: 1 / n) except MultiException[ValueError] as exc: assert False except MultiException[ZeroDivisionError] as exc: assert len(exc.actual) == 1 assert isinstance(exc.one, ZeroDivisionError) else: assert False with pytest.raises(MultiException[ArithmeticError]): try: MultiObject(range(5)).call(lambda n: 1 / n) except ZeroDivisionError: assert False # shouldn't be here except MultiException[ValueError]: assert False # shouldn't be here class ExceptionForPicklingTest(ArithmeticError): pass def test_multiexception_pickling(): import pickle import multiprocessing def throw(n): if not n: raise ExceptionForPicklingTest(n) def fail_and_dump(queue): try: MultiObject(range(5)).call(throw) except MultiException[ArithmeticError] as exc: p = pickle.dumps(exc) queue.put_nowait(p) queue = multiprocessing.Queue(1) process = multiprocessing.Process(target=fail_and_dump, args=(queue,)) process.start() process.join() p = queue.get_nowait() exc = pickle.loads(p) assert isinstance(exc, MultiException[ExceptionForPicklingTest]) assert exc.common_type is ExceptionForPicklingTest assert exc.exceptions[0].args == (0,) assert exc.exceptions[1:] == [None] * 4 def test_multiobject_concurrent_find_found(): m = MultiObject(range(10)) from time import sleep ret = m.concurrent_find(lambda n: sleep(n / 10) or n) # n==0 is not nonzero, so it's not eligible assert ret == 1 def test_multiobject_concurrent_find_not_found(): m = MultiObject(range(10)) ret = m.concurrent_find(lambda n: n < 0) assert ret is False m = MultiObject([0] * 5) ret = m.concurrent_find(lambda n: n) assert ret == 0 def test_multiobject_concurrent_find_proper_shutdown(): executed = [] m = MultiObject(range(10), workers=1) ret = m.concurrent_find(lambda n: [print(n) or executed.append(n) or sleep(.01)]) assert ret sleep(1) # wait for potential stragglers assert max(executed) <= 2 def test_multiobject_zip_with(): m = MultiObject(range(4)) with pytest.raises(AssertionError): m.zip_with(range(3), range(5)) # too few objects m.zip_with(range(5), range(6)) # too many objects ret = m.zip_with(range(1, 5)).call(lambda a, b: a + b).T assert ret == (1, 3, 5, 7) def test_multiobject_enumerate(): m = MultiObject(range(5), log_ctx=""abcd"") def check(i, j): assert i == j + 1 e = m.enumerate(1) assert e._log_ctx == tuple(""abcd"") e.call(check) def test_multiobject_logging(): m = MultiObject(range(4), log_ctx=""abcd"", initial_log_interval=0.1) def check(i): sleep(.2) # we'll mock the logger so we can ensure it logged with patch(""easypy.concurrency._logger"") as _logger: m.call(check) args_list = (c[0] for c in _logger.info.call_args_list) for args in args_list: assert ""test_multiobject_logging..check"" == args[2] assert ""easypy/tests/test_concurrency.py"" in args[4] def test_multiobject_types(): assert isinstance(MultiObject(range(5)), MultiObject[int]) assert not isinstance(MultiObject(range(5)), MultiObject[str]) class A(): ... class B(A): ... assert issubclass(MultiObject[A], MultiObject) assert not issubclass(MultiObject[A], A) assert issubclass(MultiObject[B], MultiObject[A]) assert not issubclass(MultiObject[A], MultiObject[B]) assert isinstance(MultiObject([B()]), MultiObject[A]) assert not isinstance(MultiObject([A()]), MultiObject[B]) assert isinstance(MultiObject[A]([B()]), MultiObject[A]) assert isinstance(MultiObject[A]([B()]), MultiObject[B]) assert isinstance(MultiObject[int](range(5)), MultiObject[int]) with pytest.raises(TypeError): assert MultiObject[str](range(5)) assert isinstance(MultiObject[str](""123"").call(int), MultiObject[int]) def test_multiobject_namedtuples(): from collections import namedtuple class Something(namedtuple(""Something"", ""a b"")): pass def ensure_not_expanded(something): # This will probably fail before these asserts assert hasattr(something, 'a') assert hasattr(something, 'b') objects = [Something(1, 2), Something(2, 3), Something(3, 4)] MultiObject(objects).call(ensure_not_expanded) @pytest.mark.usefixtures('concurrency_enabled_and_disabled') def test_multiexception_api(): with pytest.raises(MultiException) as exc: MultiObject([0, 5]).call(lambda i: 10 // i) failed, sucsessful = exc.value.futures assert failed.done() with pytest.raises(ZeroDivisionError): failed.result() assert isinstance(failed.exception(), ZeroDivisionError) assert sucsessful.done() assert sucsessful.result() == 2 assert sucsessful.exception() is None def test_multiexception_types(): class OK(Exception): pass class BAD(object): pass class OKBAD(OK, BAD): pass with pytest.raises(AssertionError): MultiException[BAD] def raise_it(typ): raise typ() with pytest.raises(MultiException[OK]): MultiObject([OK]).call(raise_it) with pytest.raises(MultiException[OKBAD]): MultiObject([OKBAD]).call(raise_it) with pytest.raises(MultiException[OK]): MultiObject([OKBAD]).call(raise_it) @pytest.mark.parametrize('throw', [False, True]) def test_concurrent_done_status(throw): from threading import Event continue_func = Event() def func(): continue_func.wait() if throw: raise Exception() with concurrent(func, throw=False) as c: assert not c.done() continue_func.set() sleep(0.1) assert c.done() assert c.done() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_concurrency.py::test_multiobject_logging,tests/test_concurrency.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch import pytest from time import sleep from easypy.threadtree import get_thread_stacks, ThreadContexts from easypy.concurrency import concurrent, MultiObject, MultiException @pytest.yield_fixture(params=[True, False], ids=['concurrent', 'nonconcurrent']) def concurrency_enabled_and_disabled(request): if request.param: # concurrency enabled yield else: # concurrency disabled from easypy.concurrency import disable, enable try: disable() yield finally: enable() def test_thread_stacks(): with concurrent(sleep, .1, threadname='sleep'): print(get_thread_stacks().render()) def test_thread_contexts_counters(): TC = ThreadContexts(counters=('i', 'j')) assert TC.i == TC.j == 0 with TC(i=1): def check1(): assert TC.i == 1 assert TC.j == 0 with TC(i=1, j=1): def check2(): assert TC.i == 2 assert TC.j == 1 with concurrent(check2): pass with concurrent(check1): pass def test_thread_contexts_counters_multiobject(): TC = ThreadContexts(counters=('i',)) assert TC.i == 0 print(""---"") @TC(i=True) def test(n): print(n, TC._context_data) sleep(.1) return TC.i test(0) ret = MultiObject(range(10)).call(test) assert set(ret) == {1} def test_thread_context_stacks(): TC = ThreadContexts(stacks=('i', 'j')) assert TC.i == TC.j == [] with TC(i='a'): def check1(): assert TC.i == ['a'] assert TC.j == [] with TC(i='i', j='j'): def check2(): assert TC.i == ['a', 'i'] assert TC.j == ['j'] with concurrent(check2): pass with concurrent(check1): pass def test_multiobject_0(): x = MultiObject([]).foo() assert len(x) == 0 assert x.__class__.CONCESTOR is object def test_multiobject_1(): m = MultiObject(range(10)) def mul(a, b, *c): return a * b + sum(c) assert sum(m.call(mul, 2)) == 90 assert sum(m.call(mul, b=10)) == 450 assert sum(m.call(mul, 1, 1, 1)) == 65 assert m.filter(None).T == (1, 2, 3, 4, 5, 6, 7, 8, 9) assert sum(m.denominator) == 10 with pytest.raises(MultiException) as info: m.call(lambda i: 1 / (i % 2)) assert info.value.count == 5 assert info.value.common_type == ZeroDivisionError assert not info.value.complete def test_multiobject_exceptions(): assert MultiException[ValueError] is MultiException[ValueError] assert issubclass(MultiException[UnicodeDecodeError], MultiException[UnicodeError]) assert issubclass(MultiException[UnicodeDecodeError], MultiException[ValueError]) with pytest.raises(AssertionError): MultiException[0] with pytest.raises(MultiException): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[Exception]): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[ZeroDivisionError]): MultiObject(range(5)).call(lambda n: 1 / n) try: MultiObject(range(5)).call(lambda n: 1 / n) except MultiException[ValueError] as exc: assert False except MultiException[ZeroDivisionError] as exc: assert len(exc.actual) == 1 assert isinstance(exc.one, ZeroDivisionError) else: assert False with pytest.raises(MultiException[ArithmeticError]): try: MultiObject(range(5)).call(lambda n: 1 / n) except ZeroDivisionError: assert False # shouldn't be here except MultiException[ValueError]: assert False # shouldn't be here class ExceptionForPicklingTest(ArithmeticError): pass def test_multiexception_pickling(): import pickle import multiprocessing def throw(n): if not n: raise ExceptionForPicklingTest(n) def fail_and_dump(queue): try: MultiObject(range(5)).call(throw) except MultiException[ArithmeticError] as exc: p = pickle.dumps(exc) queue.put_nowait(p) queue = multiprocessing.Queue(1) process = multiprocessing.Process(target=fail_and_dump, args=(queue,)) process.start() process.join() p = queue.get_nowait() exc = pickle.loads(p) assert isinstance(exc, MultiException[ExceptionForPicklingTest]) assert exc.common_type is ExceptionForPicklingTest assert exc.exceptions[0].args == (0,) assert exc.exceptions[1:] == [None] * 4 def test_multiobject_concurrent_find_found(): m = MultiObject(range(10)) from time import sleep ret = m.concurrent_find(lambda n: sleep(n / 10) or n) # n==0 is not nonzero, so it's not eligible assert ret == 1 def test_multiobject_concurrent_find_not_found(): m = MultiObject(range(10)) ret = m.concurrent_find(lambda n: n < 0) assert ret is False m = MultiObject([0] * 5) ret = m.concurrent_find(lambda n: n) assert ret == 0 def test_multiobject_concurrent_find_proper_shutdown(): executed = [] m = MultiObject(range(10), workers=1) ret = m.concurrent_find(lambda n: [print(n) or executed.append(n) or sleep(.01)]) assert ret sleep(1) # wait for potential stragglers assert max(executed) <= 2 def test_multiobject_zip_with(): m = MultiObject(range(4)) with pytest.raises(AssertionError): m.zip_with(range(3), range(5)) # too few objects m.zip_with(range(5), range(6)) # too many objects ret = m.zip_with(range(1, 5)).call(lambda a, b: a + b).T assert ret == (1, 3, 5, 7) def test_multiobject_enumerate(): m = MultiObject(range(5), log_ctx=""abcd"") def check(i, j): assert i == j + 1 e = m.enumerate(1) assert e._log_ctx == tuple(""abcd"") e.call(check) def test_multiobject_logging(): m = MultiObject(range(4), log_ctx=""abcd"", initial_log_interval=0.1) def check(i): sleep(.2) # we'll mock the logger so we can ensure it logged with patch(""easypy.concurrency._logger"") as _logger: m.call(check) args_list = (c[0] for c in _logger.info.call_args_list) for args in args_list: assert ""test_multiobject_logging..check"" == args[2] assert ""easypy/tests/test_concurrency.py"" in args[4] def test_multiobject_types(): assert isinstance(MultiObject(range(5)), MultiObject[int]) assert not isinstance(MultiObject(range(5)), MultiObject[str]) class A(): ... class B(A): ... assert issubclass(MultiObject[A], MultiObject) assert not issubclass(MultiObject[A], A) assert issubclass(MultiObject[B], MultiObject[A]) assert not issubclass(MultiObject[A], MultiObject[B]) assert isinstance(MultiObject([B()]), MultiObject[A]) assert not isinstance(MultiObject([A()]), MultiObject[B]) assert isinstance(MultiObject[A]([B()]), MultiObject[A]) assert isinstance(MultiObject[A]([B()]), MultiObject[B]) assert isinstance(MultiObject[int](range(5)), MultiObject[int]) with pytest.raises(TypeError): assert MultiObject[str](range(5)) assert isinstance(MultiObject[str](""123"").call(int), MultiObject[int]) def test_multiobject_namedtuples(): from collections import namedtuple class Something(namedtuple(""Something"", ""a b"")): pass def ensure_not_expanded(something): # This will probably fail before these asserts assert hasattr(something, 'a') assert hasattr(something, 'b') objects = [Something(1, 2), Something(2, 3), Something(3, 4)] MultiObject(objects).call(ensure_not_expanded) @pytest.mark.usefixtures('concurrency_enabled_and_disabled') def test_multiexception_api(): with pytest.raises(MultiException) as exc: MultiObject([0, 5]).call(lambda i: 10 // i) failed, sucsessful = exc.value.futures assert failed.done() with pytest.raises(ZeroDivisionError): failed.result() assert isinstance(failed.exception(), ZeroDivisionError) assert sucsessful.done() assert sucsessful.result() == 2 assert sucsessful.exception() is None def test_multiexception_types(): class OK(Exception): pass class BAD(object): pass class OKBAD(OK, BAD): pass with pytest.raises(AssertionError): MultiException[BAD] def raise_it(typ): raise typ() with pytest.raises(MultiException[OK]): MultiObject([OK]).call(raise_it) with pytest.raises(MultiException[OKBAD]): MultiObject([OKBAD]).call(raise_it) with pytest.raises(MultiException[OK]): MultiObject([OKBAD]).call(raise_it) @pytest.mark.parametrize('throw', [False, True]) def test_concurrent_done_status(throw): from threading import Event continue_func = Event() def func(): continue_func.wait() if throw: raise Exception() with concurrent(func, throw=False) as c: assert not c.done() continue_func.set() sleep(0.1) assert c.done() assert c.done() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_concurrency.py::test_thread_contexts_counters,tests/test_concurrency.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch import pytest from time import sleep from easypy.threadtree import get_thread_stacks, ThreadContexts from easypy.concurrency import concurrent, MultiObject, MultiException @pytest.yield_fixture(params=[True, False], ids=['concurrent', 'nonconcurrent']) def concurrency_enabled_and_disabled(request): if request.param: # concurrency enabled yield else: # concurrency disabled from easypy.concurrency import disable, enable try: disable() yield finally: enable() def test_thread_stacks(): with concurrent(sleep, .1, threadname='sleep'): print(get_thread_stacks().render()) def test_thread_contexts_counters(): TC = ThreadContexts(counters=('i', 'j')) assert TC.i == TC.j == 0 with TC(i=1): def check1(): assert TC.i == 1 assert TC.j == 0 with TC(i=1, j=1): def check2(): assert TC.i == 2 assert TC.j == 1 with concurrent(check2): pass with concurrent(check1): pass def test_thread_contexts_counters_multiobject(): TC = ThreadContexts(counters=('i',)) assert TC.i == 0 print(""---"") @TC(i=True) def test(n): print(n, TC._context_data) sleep(.1) return TC.i test(0) ret = MultiObject(range(10)).call(test) assert set(ret) == {1} def test_thread_context_stacks(): TC = ThreadContexts(stacks=('i', 'j')) assert TC.i == TC.j == [] with TC(i='a'): def check1(): assert TC.i == ['a'] assert TC.j == [] with TC(i='i', j='j'): def check2(): assert TC.i == ['a', 'i'] assert TC.j == ['j'] with concurrent(check2): pass with concurrent(check1): pass def test_multiobject_0(): x = MultiObject([]).foo() assert len(x) == 0 assert x.__class__.CONCESTOR is object def test_multiobject_1(): m = MultiObject(range(10)) def mul(a, b, *c): return a * b + sum(c) assert sum(m.call(mul, 2)) == 90 assert sum(m.call(mul, b=10)) == 450 assert sum(m.call(mul, 1, 1, 1)) == 65 assert m.filter(None).T == (1, 2, 3, 4, 5, 6, 7, 8, 9) assert sum(m.denominator) == 10 with pytest.raises(MultiException) as info: m.call(lambda i: 1 / (i % 2)) assert info.value.count == 5 assert info.value.common_type == ZeroDivisionError assert not info.value.complete def test_multiobject_exceptions(): assert MultiException[ValueError] is MultiException[ValueError] assert issubclass(MultiException[UnicodeDecodeError], MultiException[UnicodeError]) assert issubclass(MultiException[UnicodeDecodeError], MultiException[ValueError]) with pytest.raises(AssertionError): MultiException[0] with pytest.raises(MultiException): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[Exception]): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[ZeroDivisionError]): MultiObject(range(5)).call(lambda n: 1 / n) try: MultiObject(range(5)).call(lambda n: 1 / n) except MultiException[ValueError] as exc: assert False except MultiException[ZeroDivisionError] as exc: assert len(exc.actual) == 1 assert isinstance(exc.one, ZeroDivisionError) else: assert False with pytest.raises(MultiException[ArithmeticError]): try: MultiObject(range(5)).call(lambda n: 1 / n) except ZeroDivisionError: assert False # shouldn't be here except MultiException[ValueError]: assert False # shouldn't be here class ExceptionForPicklingTest(ArithmeticError): pass def test_multiexception_pickling(): import pickle import multiprocessing def throw(n): if not n: raise ExceptionForPicklingTest(n) def fail_and_dump(queue): try: MultiObject(range(5)).call(throw) except MultiException[ArithmeticError] as exc: p = pickle.dumps(exc) queue.put_nowait(p) queue = multiprocessing.Queue(1) process = multiprocessing.Process(target=fail_and_dump, args=(queue,)) process.start() process.join() p = queue.get_nowait() exc = pickle.loads(p) assert isinstance(exc, MultiException[ExceptionForPicklingTest]) assert exc.common_type is ExceptionForPicklingTest assert exc.exceptions[0].args == (0,) assert exc.exceptions[1:] == [None] * 4 def test_multiobject_concurrent_find_found(): m = MultiObject(range(10)) from time import sleep ret = m.concurrent_find(lambda n: sleep(n / 10) or n) # n==0 is not nonzero, so it's not eligible assert ret == 1 def test_multiobject_concurrent_find_not_found(): m = MultiObject(range(10)) ret = m.concurrent_find(lambda n: n < 0) assert ret is False m = MultiObject([0] * 5) ret = m.concurrent_find(lambda n: n) assert ret == 0 def test_multiobject_concurrent_find_proper_shutdown(): executed = [] m = MultiObject(range(10), workers=1) ret = m.concurrent_find(lambda n: [print(n) or executed.append(n) or sleep(.01)]) assert ret sleep(1) # wait for potential stragglers assert max(executed) <= 2 def test_multiobject_zip_with(): m = MultiObject(range(4)) with pytest.raises(AssertionError): m.zip_with(range(3), range(5)) # too few objects m.zip_with(range(5), range(6)) # too many objects ret = m.zip_with(range(1, 5)).call(lambda a, b: a + b).T assert ret == (1, 3, 5, 7) def test_multiobject_enumerate(): m = MultiObject(range(5), log_ctx=""abcd"") def check(i, j): assert i == j + 1 e = m.enumerate(1) assert e._log_ctx == tuple(""abcd"") e.call(check) def test_multiobject_logging(): m = MultiObject(range(4), log_ctx=""abcd"", initial_log_interval=0.1) def check(i): sleep(.2) # we'll mock the logger so we can ensure it logged with patch(""easypy.concurrency._logger"") as _logger: m.call(check) args_list = (c[0] for c in _logger.info.call_args_list) for args in args_list: assert ""test_multiobject_logging..check"" == args[2] assert ""easypy/tests/test_concurrency.py"" in args[4] def test_multiobject_types(): assert isinstance(MultiObject(range(5)), MultiObject[int]) assert not isinstance(MultiObject(range(5)), MultiObject[str]) class A(): ... class B(A): ... assert issubclass(MultiObject[A], MultiObject) assert not issubclass(MultiObject[A], A) assert issubclass(MultiObject[B], MultiObject[A]) assert not issubclass(MultiObject[A], MultiObject[B]) assert isinstance(MultiObject([B()]), MultiObject[A]) assert not isinstance(MultiObject([A()]), MultiObject[B]) assert isinstance(MultiObject[A]([B()]), MultiObject[A]) assert isinstance(MultiObject[A]([B()]), MultiObject[B]) assert isinstance(MultiObject[int](range(5)), MultiObject[int]) with pytest.raises(TypeError): assert MultiObject[str](range(5)) assert isinstance(MultiObject[str](""123"").call(int), MultiObject[int]) def test_multiobject_namedtuples(): from collections import namedtuple class Something(namedtuple(""Something"", ""a b"")): pass def ensure_not_expanded(something): # This will probably fail before these asserts assert hasattr(something, 'a') assert hasattr(something, 'b') objects = [Something(1, 2), Something(2, 3), Something(3, 4)] MultiObject(objects).call(ensure_not_expanded) @pytest.mark.usefixtures('concurrency_enabled_and_disabled') def test_multiexception_api(): with pytest.raises(MultiException) as exc: MultiObject([0, 5]).call(lambda i: 10 // i) failed, sucsessful = exc.value.futures assert failed.done() with pytest.raises(ZeroDivisionError): failed.result() assert isinstance(failed.exception(), ZeroDivisionError) assert sucsessful.done() assert sucsessful.result() == 2 assert sucsessful.exception() is None def test_multiexception_types(): class OK(Exception): pass class BAD(object): pass class OKBAD(OK, BAD): pass with pytest.raises(AssertionError): MultiException[BAD] def raise_it(typ): raise typ() with pytest.raises(MultiException[OK]): MultiObject([OK]).call(raise_it) with pytest.raises(MultiException[OKBAD]): MultiObject([OKBAD]).call(raise_it) with pytest.raises(MultiException[OK]): MultiObject([OKBAD]).call(raise_it) @pytest.mark.parametrize('throw', [False, True]) def test_concurrent_done_status(throw): from threading import Event continue_func = Event() def func(): continue_func.wait() if throw: raise Exception() with concurrent(func, throw=False) as c: assert not c.done() continue_func.set() sleep(0.1) assert c.done() assert c.done() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_concurrency.py::test_thread_context_stacks,tests/test_concurrency.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch import pytest from time import sleep from easypy.threadtree import get_thread_stacks, ThreadContexts from easypy.concurrency import concurrent, MultiObject, MultiException @pytest.yield_fixture(params=[True, False], ids=['concurrent', 'nonconcurrent']) def concurrency_enabled_and_disabled(request): if request.param: # concurrency enabled yield else: # concurrency disabled from easypy.concurrency import disable, enable try: disable() yield finally: enable() def test_thread_stacks(): with concurrent(sleep, .1, threadname='sleep'): print(get_thread_stacks().render()) def test_thread_contexts_counters(): TC = ThreadContexts(counters=('i', 'j')) assert TC.i == TC.j == 0 with TC(i=1): def check1(): assert TC.i == 1 assert TC.j == 0 with TC(i=1, j=1): def check2(): assert TC.i == 2 assert TC.j == 1 with concurrent(check2): pass with concurrent(check1): pass def test_thread_contexts_counters_multiobject(): TC = ThreadContexts(counters=('i',)) assert TC.i == 0 print(""---"") @TC(i=True) def test(n): print(n, TC._context_data) sleep(.1) return TC.i test(0) ret = MultiObject(range(10)).call(test) assert set(ret) == {1} def test_thread_context_stacks(): TC = ThreadContexts(stacks=('i', 'j')) assert TC.i == TC.j == [] with TC(i='a'): def check1(): assert TC.i == ['a'] assert TC.j == [] with TC(i='i', j='j'): def check2(): assert TC.i == ['a', 'i'] assert TC.j == ['j'] with concurrent(check2): pass with concurrent(check1): pass def test_multiobject_0(): x = MultiObject([]).foo() assert len(x) == 0 assert x.__class__.CONCESTOR is object def test_multiobject_1(): m = MultiObject(range(10)) def mul(a, b, *c): return a * b + sum(c) assert sum(m.call(mul, 2)) == 90 assert sum(m.call(mul, b=10)) == 450 assert sum(m.call(mul, 1, 1, 1)) == 65 assert m.filter(None).T == (1, 2, 3, 4, 5, 6, 7, 8, 9) assert sum(m.denominator) == 10 with pytest.raises(MultiException) as info: m.call(lambda i: 1 / (i % 2)) assert info.value.count == 5 assert info.value.common_type == ZeroDivisionError assert not info.value.complete def test_multiobject_exceptions(): assert MultiException[ValueError] is MultiException[ValueError] assert issubclass(MultiException[UnicodeDecodeError], MultiException[UnicodeError]) assert issubclass(MultiException[UnicodeDecodeError], MultiException[ValueError]) with pytest.raises(AssertionError): MultiException[0] with pytest.raises(MultiException): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[Exception]): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[ZeroDivisionError]): MultiObject(range(5)).call(lambda n: 1 / n) try: MultiObject(range(5)).call(lambda n: 1 / n) except MultiException[ValueError] as exc: assert False except MultiException[ZeroDivisionError] as exc: assert len(exc.actual) == 1 assert isinstance(exc.one, ZeroDivisionError) else: assert False with pytest.raises(MultiException[ArithmeticError]): try: MultiObject(range(5)).call(lambda n: 1 / n) except ZeroDivisionError: assert False # shouldn't be here except MultiException[ValueError]: assert False # shouldn't be here class ExceptionForPicklingTest(ArithmeticError): pass def test_multiexception_pickling(): import pickle import multiprocessing def throw(n): if not n: raise ExceptionForPicklingTest(n) def fail_and_dump(queue): try: MultiObject(range(5)).call(throw) except MultiException[ArithmeticError] as exc: p = pickle.dumps(exc) queue.put_nowait(p) queue = multiprocessing.Queue(1) process = multiprocessing.Process(target=fail_and_dump, args=(queue,)) process.start() process.join() p = queue.get_nowait() exc = pickle.loads(p) assert isinstance(exc, MultiException[ExceptionForPicklingTest]) assert exc.common_type is ExceptionForPicklingTest assert exc.exceptions[0].args == (0,) assert exc.exceptions[1:] == [None] * 4 def test_multiobject_concurrent_find_found(): m = MultiObject(range(10)) from time import sleep ret = m.concurrent_find(lambda n: sleep(n / 10) or n) # n==0 is not nonzero, so it's not eligible assert ret == 1 def test_multiobject_concurrent_find_not_found(): m = MultiObject(range(10)) ret = m.concurrent_find(lambda n: n < 0) assert ret is False m = MultiObject([0] * 5) ret = m.concurrent_find(lambda n: n) assert ret == 0 def test_multiobject_concurrent_find_proper_shutdown(): executed = [] m = MultiObject(range(10), workers=1) ret = m.concurrent_find(lambda n: [print(n) or executed.append(n) or sleep(.01)]) assert ret sleep(1) # wait for potential stragglers assert max(executed) <= 2 def test_multiobject_zip_with(): m = MultiObject(range(4)) with pytest.raises(AssertionError): m.zip_with(range(3), range(5)) # too few objects m.zip_with(range(5), range(6)) # too many objects ret = m.zip_with(range(1, 5)).call(lambda a, b: a + b).T assert ret == (1, 3, 5, 7) def test_multiobject_enumerate(): m = MultiObject(range(5), log_ctx=""abcd"") def check(i, j): assert i == j + 1 e = m.enumerate(1) assert e._log_ctx == tuple(""abcd"") e.call(check) def test_multiobject_logging(): m = MultiObject(range(4), log_ctx=""abcd"", initial_log_interval=0.1) def check(i): sleep(.2) # we'll mock the logger so we can ensure it logged with patch(""easypy.concurrency._logger"") as _logger: m.call(check) args_list = (c[0] for c in _logger.info.call_args_list) for args in args_list: assert ""test_multiobject_logging..check"" == args[2] assert ""easypy/tests/test_concurrency.py"" in args[4] def test_multiobject_types(): assert isinstance(MultiObject(range(5)), MultiObject[int]) assert not isinstance(MultiObject(range(5)), MultiObject[str]) class A(): ... class B(A): ... assert issubclass(MultiObject[A], MultiObject) assert not issubclass(MultiObject[A], A) assert issubclass(MultiObject[B], MultiObject[A]) assert not issubclass(MultiObject[A], MultiObject[B]) assert isinstance(MultiObject([B()]), MultiObject[A]) assert not isinstance(MultiObject([A()]), MultiObject[B]) assert isinstance(MultiObject[A]([B()]), MultiObject[A]) assert isinstance(MultiObject[A]([B()]), MultiObject[B]) assert isinstance(MultiObject[int](range(5)), MultiObject[int]) with pytest.raises(TypeError): assert MultiObject[str](range(5)) assert isinstance(MultiObject[str](""123"").call(int), MultiObject[int]) def test_multiobject_namedtuples(): from collections import namedtuple class Something(namedtuple(""Something"", ""a b"")): pass def ensure_not_expanded(something): # This will probably fail before these asserts assert hasattr(something, 'a') assert hasattr(something, 'b') objects = [Something(1, 2), Something(2, 3), Something(3, 4)] MultiObject(objects).call(ensure_not_expanded) @pytest.mark.usefixtures('concurrency_enabled_and_disabled') def test_multiexception_api(): with pytest.raises(MultiException) as exc: MultiObject([0, 5]).call(lambda i: 10 // i) failed, sucsessful = exc.value.futures assert failed.done() with pytest.raises(ZeroDivisionError): failed.result() assert isinstance(failed.exception(), ZeroDivisionError) assert sucsessful.done() assert sucsessful.result() == 2 assert sucsessful.exception() is None def test_multiexception_types(): class OK(Exception): pass class BAD(object): pass class OKBAD(OK, BAD): pass with pytest.raises(AssertionError): MultiException[BAD] def raise_it(typ): raise typ() with pytest.raises(MultiException[OK]): MultiObject([OK]).call(raise_it) with pytest.raises(MultiException[OKBAD]): MultiObject([OKBAD]).call(raise_it) with pytest.raises(MultiException[OK]): MultiObject([OKBAD]).call(raise_it) @pytest.mark.parametrize('throw', [False, True]) def test_concurrent_done_status(throw): from threading import Event continue_func = Event() def func(): continue_func.wait() if throw: raise Exception() with concurrent(func, throw=False) as c: assert not c.done() continue_func.set() sleep(0.1) assert c.done() assert c.done() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_concurrency.py::test_thread_stacks,tests/test_concurrency.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch import pytest from time import sleep from easypy.threadtree import get_thread_stacks, ThreadContexts from easypy.concurrency import concurrent, MultiObject, MultiException @pytest.yield_fixture(params=[True, False], ids=['concurrent', 'nonconcurrent']) def concurrency_enabled_and_disabled(request): if request.param: # concurrency enabled yield else: # concurrency disabled from easypy.concurrency import disable, enable try: disable() yield finally: enable() def test_thread_stacks(): with concurrent(sleep, .1, threadname='sleep'): print(get_thread_stacks().render()) def test_thread_contexts_counters(): TC = ThreadContexts(counters=('i', 'j')) assert TC.i == TC.j == 0 with TC(i=1): def check1(): assert TC.i == 1 assert TC.j == 0 with TC(i=1, j=1): def check2(): assert TC.i == 2 assert TC.j == 1 with concurrent(check2): pass with concurrent(check1): pass def test_thread_contexts_counters_multiobject(): TC = ThreadContexts(counters=('i',)) assert TC.i == 0 print(""---"") @TC(i=True) def test(n): print(n, TC._context_data) sleep(.1) return TC.i test(0) ret = MultiObject(range(10)).call(test) assert set(ret) == {1} def test_thread_context_stacks(): TC = ThreadContexts(stacks=('i', 'j')) assert TC.i == TC.j == [] with TC(i='a'): def check1(): assert TC.i == ['a'] assert TC.j == [] with TC(i='i', j='j'): def check2(): assert TC.i == ['a', 'i'] assert TC.j == ['j'] with concurrent(check2): pass with concurrent(check1): pass def test_multiobject_0(): x = MultiObject([]).foo() assert len(x) == 0 assert x.__class__.CONCESTOR is object def test_multiobject_1(): m = MultiObject(range(10)) def mul(a, b, *c): return a * b + sum(c) assert sum(m.call(mul, 2)) == 90 assert sum(m.call(mul, b=10)) == 450 assert sum(m.call(mul, 1, 1, 1)) == 65 assert m.filter(None).T == (1, 2, 3, 4, 5, 6, 7, 8, 9) assert sum(m.denominator) == 10 with pytest.raises(MultiException) as info: m.call(lambda i: 1 / (i % 2)) assert info.value.count == 5 assert info.value.common_type == ZeroDivisionError assert not info.value.complete def test_multiobject_exceptions(): assert MultiException[ValueError] is MultiException[ValueError] assert issubclass(MultiException[UnicodeDecodeError], MultiException[UnicodeError]) assert issubclass(MultiException[UnicodeDecodeError], MultiException[ValueError]) with pytest.raises(AssertionError): MultiException[0] with pytest.raises(MultiException): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[Exception]): MultiObject(range(5)).call(lambda n: 1 / n) with pytest.raises(MultiException[ZeroDivisionError]): MultiObject(range(5)).call(lambda n: 1 / n) try: MultiObject(range(5)).call(lambda n: 1 / n) except MultiException[ValueError] as exc: assert False except MultiException[ZeroDivisionError] as exc: assert len(exc.actual) == 1 assert isinstance(exc.one, ZeroDivisionError) else: assert False with pytest.raises(MultiException[ArithmeticError]): try: MultiObject(range(5)).call(lambda n: 1 / n) except ZeroDivisionError: assert False # shouldn't be here except MultiException[ValueError]: assert False # shouldn't be here class ExceptionForPicklingTest(ArithmeticError): pass def test_multiexception_pickling(): import pickle import multiprocessing def throw(n): if not n: raise ExceptionForPicklingTest(n) def fail_and_dump(queue): try: MultiObject(range(5)).call(throw) except MultiException[ArithmeticError] as exc: p = pickle.dumps(exc) queue.put_nowait(p) queue = multiprocessing.Queue(1) process = multiprocessing.Process(target=fail_and_dump, args=(queue,)) process.start() process.join() p = queue.get_nowait() exc = pickle.loads(p) assert isinstance(exc, MultiException[ExceptionForPicklingTest]) assert exc.common_type is ExceptionForPicklingTest assert exc.exceptions[0].args == (0,) assert exc.exceptions[1:] == [None] * 4 def test_multiobject_concurrent_find_found(): m = MultiObject(range(10)) from time import sleep ret = m.concurrent_find(lambda n: sleep(n / 10) or n) # n==0 is not nonzero, so it's not eligible assert ret == 1 def test_multiobject_concurrent_find_not_found(): m = MultiObject(range(10)) ret = m.concurrent_find(lambda n: n < 0) assert ret is False m = MultiObject([0] * 5) ret = m.concurrent_find(lambda n: n) assert ret == 0 def test_multiobject_concurrent_find_proper_shutdown(): executed = [] m = MultiObject(range(10), workers=1) ret = m.concurrent_find(lambda n: [print(n) or executed.append(n) or sleep(.01)]) assert ret sleep(1) # wait for potential stragglers assert max(executed) <= 2 def test_multiobject_zip_with(): m = MultiObject(range(4)) with pytest.raises(AssertionError): m.zip_with(range(3), range(5)) # too few objects m.zip_with(range(5), range(6)) # too many objects ret = m.zip_with(range(1, 5)).call(lambda a, b: a + b).T assert ret == (1, 3, 5, 7) def test_multiobject_enumerate(): m = MultiObject(range(5), log_ctx=""abcd"") def check(i, j): assert i == j + 1 e = m.enumerate(1) assert e._log_ctx == tuple(""abcd"") e.call(check) def test_multiobject_logging(): m = MultiObject(range(4), log_ctx=""abcd"", initial_log_interval=0.1) def check(i): sleep(.2) # we'll mock the logger so we can ensure it logged with patch(""easypy.concurrency._logger"") as _logger: m.call(check) args_list = (c[0] for c in _logger.info.call_args_list) for args in args_list: assert ""test_multiobject_logging..check"" == args[2] assert ""easypy/tests/test_concurrency.py"" in args[4] def test_multiobject_types(): assert isinstance(MultiObject(range(5)), MultiObject[int]) assert not isinstance(MultiObject(range(5)), MultiObject[str]) class A(): ... class B(A): ... assert issubclass(MultiObject[A], MultiObject) assert not issubclass(MultiObject[A], A) assert issubclass(MultiObject[B], MultiObject[A]) assert not issubclass(MultiObject[A], MultiObject[B]) assert isinstance(MultiObject([B()]), MultiObject[A]) assert not isinstance(MultiObject([A()]), MultiObject[B]) assert isinstance(MultiObject[A]([B()]), MultiObject[A]) assert isinstance(MultiObject[A]([B()]), MultiObject[B]) assert isinstance(MultiObject[int](range(5)), MultiObject[int]) with pytest.raises(TypeError): assert MultiObject[str](range(5)) assert isinstance(MultiObject[str](""123"").call(int), MultiObject[int]) def test_multiobject_namedtuples(): from collections import namedtuple class Something(namedtuple(""Something"", ""a b"")): pass def ensure_not_expanded(something): # This will probably fail before these asserts assert hasattr(something, 'a') assert hasattr(something, 'b') objects = [Something(1, 2), Something(2, 3), Something(3, 4)] MultiObject(objects).call(ensure_not_expanded) @pytest.mark.usefixtures('concurrency_enabled_and_disabled') def test_multiexception_api(): with pytest.raises(MultiException) as exc: MultiObject([0, 5]).call(lambda i: 10 // i) failed, sucsessful = exc.value.futures assert failed.done() with pytest.raises(ZeroDivisionError): failed.result() assert isinstance(failed.exception(), ZeroDivisionError) assert sucsessful.done() assert sucsessful.result() == 2 assert sucsessful.exception() is None def test_multiexception_types(): class OK(Exception): pass class BAD(object): pass class OKBAD(OK, BAD): pass with pytest.raises(AssertionError): MultiException[BAD] def raise_it(typ): raise typ() with pytest.raises(MultiException[OK]): MultiObject([OK]).call(raise_it) with pytest.raises(MultiException[OKBAD]): MultiObject([OKBAD]).call(raise_it) with pytest.raises(MultiException[OK]): MultiObject([OKBAD]).call(raise_it) @pytest.mark.parametrize('throw', [False, True]) def test_concurrent_done_status(throw): from threading import Event continue_func = Event() def func(): continue_func.wait() if throw: raise Exception() with concurrent(func, throw=False) as c: assert not c.done() continue_func.set() sleep(0.1) assert c.done() assert c.done() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_exceptions.py::test_pickle_texception,tests/test_exceptions.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from easypy.exceptions import TException from easypy.bunch import Bunch class T(TException): template = ""The happened: {what}"" def test_pickle_texception(): import pickle t1 = T(what=""happened"", a=1, b=Bunch(x=[1, 2, 3], y=range(5))) t2 = pickle.loads(pickle.dumps(t1)) assert t1.render() == t2.render() assert t1._params == t2._params ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_logging.py::test_indent_around_ctx,tests/test_logging.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from io import StringIO from logging import getLogger, Formatter, StreamHandler from easypy.colors import uncolored logger = getLogger(__name__) @pytest.yield_fixture def get_log(): level = logger.root.level stream = StringIO() handler = StreamHandler(stream) handler.setFormatter(Formatter(fmt=""%(message)s"")) logger.root.addHandler(handler) logger.root.setLevel(0) def get(): return uncolored(stream.getvalue()) yield get logger.root.setLevel(level) logger.root.removeHandler(handler) def test_indent_around_generator(get_log): @logger.indented(""hey"") def gen(): logger.info(""000"") yield 1 yield 2 for i in gen(): logger.info(""%03d"", i) break assert get_log() == ""hey\n000\n001\nDONE in no-time (hey)\n"" def test_indent_around_function(get_log): @logger.indented(""hey"") def foo(): logger.info(""001"") foo() assert get_log() == ""hey\n001\nDONE in no-time (hey)\n"" def test_indent_around_ctx(get_log): @logger.indented(""hey"") @contextmanager def ctx(): logger.info(""001"") yield logger.info(""003"") with ctx(): logger.info(""002"") assert get_log() == ""hey\n001\n002\n003\nDONE in no-time (hey)\n"" ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_logging.py::test_indent_around_function,tests/test_logging.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from io import StringIO from logging import getLogger, Formatter, StreamHandler from easypy.colors import uncolored logger = getLogger(__name__) @pytest.yield_fixture def get_log(): level = logger.root.level stream = StringIO() handler = StreamHandler(stream) handler.setFormatter(Formatter(fmt=""%(message)s"")) logger.root.addHandler(handler) logger.root.setLevel(0) def get(): return uncolored(stream.getvalue()) yield get logger.root.setLevel(level) logger.root.removeHandler(handler) def test_indent_around_generator(get_log): @logger.indented(""hey"") def gen(): logger.info(""000"") yield 1 yield 2 for i in gen(): logger.info(""%03d"", i) break assert get_log() == ""hey\n000\n001\nDONE in no-time (hey)\n"" def test_indent_around_function(get_log): @logger.indented(""hey"") def foo(): logger.info(""001"") foo() assert get_log() == ""hey\n001\nDONE in no-time (hey)\n"" def test_indent_around_ctx(get_log): @logger.indented(""hey"") @contextmanager def ctx(): logger.info(""001"") yield logger.info(""003"") with ctx(): logger.info(""002"") assert get_log() == ""hey\n001\n002\n003\nDONE in no-time (hey)\n"" ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_logging.py::test_indent_around_generator,tests/test_logging.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from io import StringIO from logging import getLogger, Formatter, StreamHandler from easypy.colors import uncolored logger = getLogger(__name__) @pytest.yield_fixture def get_log(): level = logger.root.level stream = StringIO() handler = StreamHandler(stream) handler.setFormatter(Formatter(fmt=""%(message)s"")) logger.root.addHandler(handler) logger.root.setLevel(0) def get(): return uncolored(stream.getvalue()) yield get logger.root.setLevel(level) logger.root.removeHandler(handler) def test_indent_around_generator(get_log): @logger.indented(""hey"") def gen(): logger.info(""000"") yield 1 yield 2 for i in gen(): logger.info(""%03d"", i) break assert get_log() == ""hey\n000\n001\nDONE in no-time (hey)\n"" def test_indent_around_function(get_log): @logger.indented(""hey"") def foo(): logger.info(""001"") foo() assert get_log() == ""hey\n001\nDONE in no-time (hey)\n"" def test_indent_around_ctx(get_log): @logger.indented(""hey"") @contextmanager def ctx(): logger.info(""001"") yield logger.info(""003"") with ctx(): logger.info(""002"") assert get_log() == ""hey\n001\n002\n003\nDONE in no-time (hey)\n"" ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_rwlock.py::test_rwlock,tests/test_rwlock.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import threading import logging from easypy.concurrency import concurrent from easypy.sync import RWLock from easypy.bunch import Bunch def test_rwlock(): main_ctrl = threading.Event() reader_ctrl = threading.Event() writer_ctrl = threading.Event() lock = RWLock(""test"") state = Bunch(reading=False, writing=False) def read(): logging.info(""Before read"") reader_ctrl.wait() reader_ctrl.clear() with lock: logging.info(""Reading..."") state.reading = True main_ctrl.set() reader_ctrl.wait() reader_ctrl.clear() state.reading = False logging.info(""Done reading"") logging.info(""After read"") def write(): logging.info(""Before write"") writer_ctrl.wait() writer_ctrl.clear() with lock.exclusive(): logging.info(""Writing..."") state.writing = True main_ctrl.set() writer_ctrl.wait() writer_ctrl.clear() state.writing = False logging.info(""Done writing"") logging.info(""After write"") main_ctrl.set() reader = concurrent(read, threadname='read') writer = concurrent(write, threadname='write') with reader, writer: assert not state.reading and not state.writing reader_ctrl.set() main_ctrl.wait() logging.info(""lock acquired non-exclusively"") main_ctrl.clear() assert state.reading and not state.writing writer_ctrl.set() logging.info(""writer awaits exclusivity"") with lock: assert state.reading and not state.writing reader_ctrl.set() main_ctrl.wait() main_ctrl.clear() logging.info(""read lock released"") assert not state.reading and state.writing writer_ctrl.set() main_ctrl.wait() main_ctrl.clear() logging.info(""write lock released"") assert not state.reading and not state.writing ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_async,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_ctx,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_ctx_with_identifier,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_priorities,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_priorities_async,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_registration_context,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_signal_weakref,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_signal_weakref_complex_descriptors,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_signal_weakref_context_manager_delete_after,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_signal_weakref_context_manager_delete_after_with_identifier,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_signal_weakref_context_manager_delete_during,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_signal_weakref_context_manager_delete_during_with_identifier,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_signal_weakref_with_identifier,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_simple_signal_object_identifier,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_simple_signal_object_identifier_attribute,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_simple_signal_object_registration,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_simple_signal_object_wo_identifier,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_signals.py::test_simple_signal_registration,tests/test_signals.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest from contextlib import contextmanager from easypy.signals import register_object, unregister_object, MissingIdentifier, PRIORITIES from easypy.signals import on_test, on_async_test from easypy.signals import on_test_identifier from easypy.signals import on_ctx_test from easypy.signals import on_ctx_test_identifier on_test_identifier.identifier = 'obj' on_ctx_test_identifier.identifier = 'obj' on_async_test.asynchronous = True def test_simple_signal_registration(): @on_test.register def foo(a, b): a / b with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test.unregister(foo) on_test(a=5, b=0, c='c') def test_simple_signal_object_registration(): class Foo(): def on_test(self, a, b): a / b __init__ = register_object f = Foo() with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') unregister_object(f) def test_simple_signal_object_identifier(): on_test_identifier(obj='xxx') class Foo(): def on_test_identifier(self, a): a / self.b __init__ = register_object f1 = Foo() f2 = Foo() f1.b = 1 f2.b = 0 with pytest.raises(MissingIdentifier): on_test_identifier(a=5, b=0, c='c') on_test_identifier(a=5, b=0, obj=f1) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) unregister_object(f1) unregister_object(f2) with on_test_identifier.registered(f2.on_test_identifier): with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj=f2) def test_simple_signal_object_wo_identifier(): class Foo(): def on_test_identifier(self, obj): 1 / 0 __init__ = register_object f1 = Foo() with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj1') on_test_identifier(a=5, b=0, obj='obj2') unregister_object(f1) def test_simple_signal_object_identifier_attribute(): class Foo(): def on_test_identifier(self): 1 / 0 __init__ = register_object f1 = Foo() f1.obj = 'obj1' on_test_identifier(obj='xxx') with pytest.raises(ZeroDivisionError): on_test_identifier(obj='obj1') unregister_object(f1) def test_registration_context(): def foo(a, b): a / b with on_test.registered(foo): with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') on_test(a=5, b=0, c='c') def test_priorities(): l = [] @on_test.register(priority=PRIORITIES.LAST) def last(): l.append(3) @on_test.register(priority=PRIORITIES.FIRST) def first(): l.append(1) @on_test.register() def mid(): l.append(2) on_test() assert l == [1, 2, 3] def test_priorities_async(): l = [] import time @on_test.register(asynchronous=True, priority=PRIORITIES.FIRST) def first(): time.sleep(.05) l.append(1) @on_test.register(asynchronous=True, priority=PRIORITIES.LAST) def last(): l.append(2) on_test() assert l == [1, 2] def test_async(): from threading import get_ident main = get_ident() @on_async_test.register(asynchronous=True) def a1(): assert get_ident() != main @on_async_test.register() # follows the current setting def a2(): assert get_ident() != main with pytest.raises(AssertionError): @on_async_test.register(asynchronous=False) def xx(): assert get_ident() == main on_async_test() def test_ctx(): result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) assert result == [] with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] unregister_object(foo) with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_ctx_with_identifier(): class Foo(): @contextmanager def on_ctx_test_identifier(self, before, after): self.result.append(before) yield self.result.append(after) f1 = Foo() f1.obj = 'obj1' f1.result = [] f2 = Foo() f2.obj = 'obj2' f2.result = [] register_object(f1) register_object(f2) with on_ctx_test_identifier(before=1, after=2, obj='obj1'): assert f1.result == [1] assert f2.result == [] assert f1.result == [1, 2] assert f2.result == [] with on_ctx_test_identifier(before=3, after=4, obj='obj2'): assert f1.result == [1, 2] assert f2.result == [3] assert f1.result == [1, 2] assert f2.result == [3, 4] def test_signal_weakref(): """""" Test that signals handlers of methods are deleted when their objects get collected """""" import gc class Foo: def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_complex_descriptors(): import gc from easypy.lockstep import lockstep class Foo: @lockstep def on_test(self, a, b): a / b foo = Foo() register_object(foo) with pytest.raises(ZeroDivisionError): on_test(a=5, b=0, c='c') del foo gc.collect() on_test(a=5, b=0, c='c') def test_signal_weakref_context_manager_delete_after(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] del foo gc.collect() with on_ctx_test(before=3, after=4): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during(): import gc result = [] class Foo: @contextmanager def on_ctx_test(self, before, after): result.append(before) yield result.append(after) foo = Foo() register_object(foo) with on_ctx_test(before=1, after=2): assert result == [1] assert result == [1, 2] with on_ctx_test(before=3, after=4): assert result == [1, 2, 3] del foo gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] def test_signal_weakref_with_identifier(): import gc class Foo: def on_test_identifier(self, a, b): a / b foo = Foo() foo.obj = 'obj' register_object(foo) with pytest.raises(ZeroDivisionError): on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') del foo gc.collect() on_test_identifier(a=5, b=0, obj='obj') on_test_identifier(a=5, b=0, obj='noobj') def test_signal_weakref_context_manager_delete_after_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] del foo del foo2 gc.collect() with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2] assert result == [1, 2] def test_signal_weakref_context_manager_delete_during_with_identifier(): import gc result = [] class Foo: @contextmanager def on_ctx_test_identifier(self, before, after): result.append(before) yield result.append(after) foo = Foo() foo.obj = 'obj' register_object(foo) foo2 = Foo() foo2.obj = 'otherobj' register_object(foo2) with on_ctx_test_identifier(before=1, after=2, obj='obj'): assert result == [1] assert result == [1, 2] with on_ctx_test_identifier(before=3, after=4, obj='obj'): assert result == [1, 2, 3] del foo del foo2 gc.collect() # The context manager should keep the signal handler alive assert result == [1, 2, 3, 4] ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_sync.py::test_logged_condition,tests/test_sync.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch, call import pytest from time import sleep import threading from contextlib import contextmanager, ExitStack import random import re from easypy.concurrency import MultiObject, MultiException, concurrent from easypy.timing import repeat, timing from easypy.bunch import Bunch from easypy.units import Duration from easypy.sync import iter_wait, wait, iter_wait_progress, Timer, TimeoutException, PredicateNotSatisfied from easypy.sync import SynchronizationCoordinator, SYNC from easypy.sync import shared_contextmanager from easypy.sync import TagAlongThread from easypy.sync import LoggedRLock, LockLeaseExpired from easypy.sync import SynchronizedSingleton from easypy.sync import LoggedCondition from .test_logging import get_log # noqa; pytest fixture def test_shared_contextmanager(): data = [] @shared_contextmanager def foo(a): data.append(a) yield a data.append(-a) with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] with foo(1): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] data.clear() with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] def test_shared_contextmanager_method(): class Foo(object): def __init__(self): self.data = [] @shared_contextmanager def foo(self, a): self.data.append(a) yield a self.data.append(-a) f = Foo() g = Foo() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] f.data.clear() g.data.clear() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] def test_shared_contextmanager_method_does_not_keep_object_alive_after_done(): import weakref import gc class Foo: @shared_contextmanager def foo(self): yield foo = Foo() weak_foo = weakref.ref(foo) with foo.foo(): del foo gc.collect() assert weak_foo() is not None, 'Object collected but contextmanager is active' gc.collect() assert weak_foo() is None, 'Object not collected but contextmanager has exited' def verify_concurrent_order(executed, *expected): look_at_index = 0 for expected_group in expected: executed_group = set(executed[look_at_index:look_at_index + len(expected_group)]) assert executed_group == expected_group, 'wrong execution order' look_at_index += len(executed_group) assert look_at_index == len(executed), 'executed list is shorted than expected' def test_synchronization_coordinator_wait_for_everyone(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) execute('after sleep') sync.wait_for_everyone() execute('after wait') sync.wait_for_everyone() sleep(i / 10) execute('after sleep 2') sync.wait_for_everyone() execute('after wait 2') mo.call(foo) verify_concurrent_order( executed, {(i, 'after sleep') for i in range(3)}, {(i, 'after wait') for i in range(3)}, {(i, 'after sleep 2') for i in range(3)}, {(i, 'after wait 2') for i in range(3)}) def test_synchronization_coordinator_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) assert sync.collect_and_call_once(i, len) == 3, 'parameters remain from previous call' mo.call(foo) verify_concurrent_order( executed, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_abandon(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sync.wait_for_everyone() execute('after wait 1') if i == 2: sync.abandon() return # Only two waiters should reach here sync.wait_for_everyone() execute('after wait 2') # Even without explicit call to abandon, sync should only wait for two waiters sync.wait_for_everyone() execute('after wait 3') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait 1') for i in range(3)}, {(i, 'after wait 2') for i in range(2)}, {(i, 'after wait 3') for i in range(2)}) def test_synchronization_coordinator_exception_in_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) times_called = 0 class MyException(Exception): pass def foo(i): def func_to_call_once(_): nonlocal times_called times_called += 1 raise MyException with pytest.raises(MyException): sync.collect_and_call_once(i, func_to_call_once) assert sync.collect_and_call_once(i + 1, sum) == 6 mo.call(foo) assert times_called == 1, 'collect_and_call_once with exception called the function more than once' def test_synchronization_coordinator_with_multiobject(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) sleep(i / 10) _sync.wait_for_everyone() execute('after wait') def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = _sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) foo(10) assert executed == [ (10, 'after wait'), 'params = [11]', (10, 'result is 11')] executed.clear() mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_with_multiobject_exception(): mo = MultiObject(range(3)) executed = [] class MyException(Exception): pass def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: raise MyException _sync.wait_for_everyone() execute('after wait/abandon') with pytest.raises(MultiException) as exc: mo.call(foo) assert exc.value.count == 1 assert exc.value.common_type is MyException verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_early_return(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: return _sync.wait_for_everyone() execute('after wait/abandon') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_method(): class Foo: def __init__(self, i): self.i = i def foo(self, _sync=SYNC): return (self.i, _sync.collect_and_call_once(self.i, lambda i_values: sorted(i_values))) mo = MultiObject(Foo(i) for i in range(3)) assert mo.foo().T == ( (0, [0, 1, 2]), (1, [0, 1, 2]), (2, [0, 1, 2])) def test_synchronization_coordinator_failing_context_manager(): class MyException(Exception): pass @contextmanager def foo(should_fail, _sync=SYNC): if should_fail: raise MyException() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_sync.py::test_logged_condition_exception,tests/test_sync.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch, call import pytest from time import sleep import threading from contextlib import contextmanager, ExitStack import random import re from easypy.concurrency import MultiObject, MultiException, concurrent from easypy.timing import repeat, timing from easypy.bunch import Bunch from easypy.units import Duration from easypy.sync import iter_wait, wait, iter_wait_progress, Timer, TimeoutException, PredicateNotSatisfied from easypy.sync import SynchronizationCoordinator, SYNC from easypy.sync import shared_contextmanager from easypy.sync import TagAlongThread from easypy.sync import LoggedRLock, LockLeaseExpired from easypy.sync import SynchronizedSingleton from easypy.sync import LoggedCondition from .test_logging import get_log # noqa; pytest fixture def test_shared_contextmanager(): data = [] @shared_contextmanager def foo(a): data.append(a) yield a data.append(-a) with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] with foo(1): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] data.clear() with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] def test_shared_contextmanager_method(): class Foo(object): def __init__(self): self.data = [] @shared_contextmanager def foo(self, a): self.data.append(a) yield a self.data.append(-a) f = Foo() g = Foo() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] f.data.clear() g.data.clear() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] def test_shared_contextmanager_method_does_not_keep_object_alive_after_done(): import weakref import gc class Foo: @shared_contextmanager def foo(self): yield foo = Foo() weak_foo = weakref.ref(foo) with foo.foo(): del foo gc.collect() assert weak_foo() is not None, 'Object collected but contextmanager is active' gc.collect() assert weak_foo() is None, 'Object not collected but contextmanager has exited' def verify_concurrent_order(executed, *expected): look_at_index = 0 for expected_group in expected: executed_group = set(executed[look_at_index:look_at_index + len(expected_group)]) assert executed_group == expected_group, 'wrong execution order' look_at_index += len(executed_group) assert look_at_index == len(executed), 'executed list is shorted than expected' def test_synchronization_coordinator_wait_for_everyone(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) execute('after sleep') sync.wait_for_everyone() execute('after wait') sync.wait_for_everyone() sleep(i / 10) execute('after sleep 2') sync.wait_for_everyone() execute('after wait 2') mo.call(foo) verify_concurrent_order( executed, {(i, 'after sleep') for i in range(3)}, {(i, 'after wait') for i in range(3)}, {(i, 'after sleep 2') for i in range(3)}, {(i, 'after wait 2') for i in range(3)}) def test_synchronization_coordinator_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) assert sync.collect_and_call_once(i, len) == 3, 'parameters remain from previous call' mo.call(foo) verify_concurrent_order( executed, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_abandon(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sync.wait_for_everyone() execute('after wait 1') if i == 2: sync.abandon() return # Only two waiters should reach here sync.wait_for_everyone() execute('after wait 2') # Even without explicit call to abandon, sync should only wait for two waiters sync.wait_for_everyone() execute('after wait 3') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait 1') for i in range(3)}, {(i, 'after wait 2') for i in range(2)}, {(i, 'after wait 3') for i in range(2)}) def test_synchronization_coordinator_exception_in_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) times_called = 0 class MyException(Exception): pass def foo(i): def func_to_call_once(_): nonlocal times_called times_called += 1 raise MyException with pytest.raises(MyException): sync.collect_and_call_once(i, func_to_call_once) assert sync.collect_and_call_once(i + 1, sum) == 6 mo.call(foo) assert times_called == 1, 'collect_and_call_once with exception called the function more than once' def test_synchronization_coordinator_with_multiobject(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) sleep(i / 10) _sync.wait_for_everyone() execute('after wait') def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = _sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) foo(10) assert executed == [ (10, 'after wait'), 'params = [11]', (10, 'result is 11')] executed.clear() mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_with_multiobject_exception(): mo = MultiObject(range(3)) executed = [] class MyException(Exception): pass def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: raise MyException _sync.wait_for_everyone() execute('after wait/abandon') with pytest.raises(MultiException) as exc: mo.call(foo) assert exc.value.count == 1 assert exc.value.common_type is MyException verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_early_return(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: return _sync.wait_for_everyone() execute('after wait/abandon') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_method(): class Foo: def __init__(self, i): self.i = i def foo(self, _sync=SYNC): return (self.i, _sync.collect_and_call_once(self.i, lambda i_values: sorted(i_values))) mo = MultiObject(Foo(i) for i in range(3)) assert mo.foo().T == ( (0, [0, 1, 2]), (1, [0, 1, 2]), (2, [0, 1, 2])) def test_synchronization_coordinator_failing_context_manager(): class MyException(Exception): pass @contextmanager def foo(should_fail, _sync=SYNC): if should_fail: raise MyException() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_sync.py::test_logged_condition_waited_for,tests/test_sync.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch, call import pytest from time import sleep import threading from contextlib import contextmanager, ExitStack import random import re from easypy.concurrency import MultiObject, MultiException, concurrent from easypy.timing import repeat, timing from easypy.bunch import Bunch from easypy.units import Duration from easypy.sync import iter_wait, wait, iter_wait_progress, Timer, TimeoutException, PredicateNotSatisfied from easypy.sync import SynchronizationCoordinator, SYNC from easypy.sync import shared_contextmanager from easypy.sync import TagAlongThread from easypy.sync import LoggedRLock, LockLeaseExpired from easypy.sync import SynchronizedSingleton from easypy.sync import LoggedCondition from .test_logging import get_log # noqa; pytest fixture def test_shared_contextmanager(): data = [] @shared_contextmanager def foo(a): data.append(a) yield a data.append(-a) with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] with foo(1): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] data.clear() with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] def test_shared_contextmanager_method(): class Foo(object): def __init__(self): self.data = [] @shared_contextmanager def foo(self, a): self.data.append(a) yield a self.data.append(-a) f = Foo() g = Foo() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] f.data.clear() g.data.clear() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] def test_shared_contextmanager_method_does_not_keep_object_alive_after_done(): import weakref import gc class Foo: @shared_contextmanager def foo(self): yield foo = Foo() weak_foo = weakref.ref(foo) with foo.foo(): del foo gc.collect() assert weak_foo() is not None, 'Object collected but contextmanager is active' gc.collect() assert weak_foo() is None, 'Object not collected but contextmanager has exited' def verify_concurrent_order(executed, *expected): look_at_index = 0 for expected_group in expected: executed_group = set(executed[look_at_index:look_at_index + len(expected_group)]) assert executed_group == expected_group, 'wrong execution order' look_at_index += len(executed_group) assert look_at_index == len(executed), 'executed list is shorted than expected' def test_synchronization_coordinator_wait_for_everyone(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) execute('after sleep') sync.wait_for_everyone() execute('after wait') sync.wait_for_everyone() sleep(i / 10) execute('after sleep 2') sync.wait_for_everyone() execute('after wait 2') mo.call(foo) verify_concurrent_order( executed, {(i, 'after sleep') for i in range(3)}, {(i, 'after wait') for i in range(3)}, {(i, 'after sleep 2') for i in range(3)}, {(i, 'after wait 2') for i in range(3)}) def test_synchronization_coordinator_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) assert sync.collect_and_call_once(i, len) == 3, 'parameters remain from previous call' mo.call(foo) verify_concurrent_order( executed, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_abandon(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sync.wait_for_everyone() execute('after wait 1') if i == 2: sync.abandon() return # Only two waiters should reach here sync.wait_for_everyone() execute('after wait 2') # Even without explicit call to abandon, sync should only wait for two waiters sync.wait_for_everyone() execute('after wait 3') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait 1') for i in range(3)}, {(i, 'after wait 2') for i in range(2)}, {(i, 'after wait 3') for i in range(2)}) def test_synchronization_coordinator_exception_in_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) times_called = 0 class MyException(Exception): pass def foo(i): def func_to_call_once(_): nonlocal times_called times_called += 1 raise MyException with pytest.raises(MyException): sync.collect_and_call_once(i, func_to_call_once) assert sync.collect_and_call_once(i + 1, sum) == 6 mo.call(foo) assert times_called == 1, 'collect_and_call_once with exception called the function more than once' def test_synchronization_coordinator_with_multiobject(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) sleep(i / 10) _sync.wait_for_everyone() execute('after wait') def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = _sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) foo(10) assert executed == [ (10, 'after wait'), 'params = [11]', (10, 'result is 11')] executed.clear() mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_with_multiobject_exception(): mo = MultiObject(range(3)) executed = [] class MyException(Exception): pass def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: raise MyException _sync.wait_for_everyone() execute('after wait/abandon') with pytest.raises(MultiException) as exc: mo.call(foo) assert exc.value.count == 1 assert exc.value.common_type is MyException verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_early_return(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: return _sync.wait_for_everyone() execute('after wait/abandon') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_method(): class Foo: def __init__(self, i): self.i = i def foo(self, _sync=SYNC): return (self.i, _sync.collect_and_call_once(self.i, lambda i_values: sorted(i_values))) mo = MultiObject(Foo(i) for i in range(3)) assert mo.foo().T == ( (0, [0, 1, 2]), (1, [0, 1, 2]), (2, [0, 1, 2])) def test_synchronization_coordinator_failing_context_manager(): class MyException(Exception): pass @contextmanager def foo(should_fail, _sync=SYNC): if should_fail: raise MyException() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_sync.py::test_logged_lock,tests/test_sync.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch, call import pytest from time import sleep import threading from contextlib import contextmanager, ExitStack import random import re from easypy.concurrency import MultiObject, MultiException, concurrent from easypy.timing import repeat, timing from easypy.bunch import Bunch from easypy.units import Duration from easypy.sync import iter_wait, wait, iter_wait_progress, Timer, TimeoutException, PredicateNotSatisfied from easypy.sync import SynchronizationCoordinator, SYNC from easypy.sync import shared_contextmanager from easypy.sync import TagAlongThread from easypy.sync import LoggedRLock, LockLeaseExpired from easypy.sync import SynchronizedSingleton from easypy.sync import LoggedCondition from .test_logging import get_log # noqa; pytest fixture def test_shared_contextmanager(): data = [] @shared_contextmanager def foo(a): data.append(a) yield a data.append(-a) with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] with foo(1): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] data.clear() with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] def test_shared_contextmanager_method(): class Foo(object): def __init__(self): self.data = [] @shared_contextmanager def foo(self, a): self.data.append(a) yield a self.data.append(-a) f = Foo() g = Foo() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] f.data.clear() g.data.clear() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] def test_shared_contextmanager_method_does_not_keep_object_alive_after_done(): import weakref import gc class Foo: @shared_contextmanager def foo(self): yield foo = Foo() weak_foo = weakref.ref(foo) with foo.foo(): del foo gc.collect() assert weak_foo() is not None, 'Object collected but contextmanager is active' gc.collect() assert weak_foo() is None, 'Object not collected but contextmanager has exited' def verify_concurrent_order(executed, *expected): look_at_index = 0 for expected_group in expected: executed_group = set(executed[look_at_index:look_at_index + len(expected_group)]) assert executed_group == expected_group, 'wrong execution order' look_at_index += len(executed_group) assert look_at_index == len(executed), 'executed list is shorted than expected' def test_synchronization_coordinator_wait_for_everyone(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) execute('after sleep') sync.wait_for_everyone() execute('after wait') sync.wait_for_everyone() sleep(i / 10) execute('after sleep 2') sync.wait_for_everyone() execute('after wait 2') mo.call(foo) verify_concurrent_order( executed, {(i, 'after sleep') for i in range(3)}, {(i, 'after wait') for i in range(3)}, {(i, 'after sleep 2') for i in range(3)}, {(i, 'after wait 2') for i in range(3)}) def test_synchronization_coordinator_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) assert sync.collect_and_call_once(i, len) == 3, 'parameters remain from previous call' mo.call(foo) verify_concurrent_order( executed, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_abandon(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sync.wait_for_everyone() execute('after wait 1') if i == 2: sync.abandon() return # Only two waiters should reach here sync.wait_for_everyone() execute('after wait 2') # Even without explicit call to abandon, sync should only wait for two waiters sync.wait_for_everyone() execute('after wait 3') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait 1') for i in range(3)}, {(i, 'after wait 2') for i in range(2)}, {(i, 'after wait 3') for i in range(2)}) def test_synchronization_coordinator_exception_in_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) times_called = 0 class MyException(Exception): pass def foo(i): def func_to_call_once(_): nonlocal times_called times_called += 1 raise MyException with pytest.raises(MyException): sync.collect_and_call_once(i, func_to_call_once) assert sync.collect_and_call_once(i + 1, sum) == 6 mo.call(foo) assert times_called == 1, 'collect_and_call_once with exception called the function more than once' def test_synchronization_coordinator_with_multiobject(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) sleep(i / 10) _sync.wait_for_everyone() execute('after wait') def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = _sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) foo(10) assert executed == [ (10, 'after wait'), 'params = [11]', (10, 'result is 11')] executed.clear() mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_with_multiobject_exception(): mo = MultiObject(range(3)) executed = [] class MyException(Exception): pass def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: raise MyException _sync.wait_for_everyone() execute('after wait/abandon') with pytest.raises(MultiException) as exc: mo.call(foo) assert exc.value.count == 1 assert exc.value.common_type is MyException verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_early_return(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: return _sync.wait_for_everyone() execute('after wait/abandon') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_method(): class Foo: def __init__(self, i): self.i = i def foo(self, _sync=SYNC): return (self.i, _sync.collect_and_call_once(self.i, lambda i_values: sorted(i_values))) mo = MultiObject(Foo(i) for i in range(3)) assert mo.foo().T == ( (0, [0, 1, 2]), (1, [0, 1, 2]), (2, [0, 1, 2])) def test_synchronization_coordinator_failing_context_manager(): class MyException(Exception): pass @contextmanager def foo(should_fail, _sync=SYNC): if should_fail: raise MyException() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_sync.py::test_synchronization_coordinator_failing_context_manager,tests/test_sync.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch, call import pytest from time import sleep import threading from contextlib import contextmanager, ExitStack import random import re from easypy.concurrency import MultiObject, MultiException, concurrent from easypy.timing import repeat, timing from easypy.bunch import Bunch from easypy.units import Duration from easypy.sync import iter_wait, wait, iter_wait_progress, Timer, TimeoutException, PredicateNotSatisfied from easypy.sync import SynchronizationCoordinator, SYNC from easypy.sync import shared_contextmanager from easypy.sync import TagAlongThread from easypy.sync import LoggedRLock, LockLeaseExpired from easypy.sync import SynchronizedSingleton from easypy.sync import LoggedCondition from .test_logging import get_log # noqa; pytest fixture def test_shared_contextmanager(): data = [] @shared_contextmanager def foo(a): data.append(a) yield a data.append(-a) with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] with foo(1): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] data.clear() with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] def test_shared_contextmanager_method(): class Foo(object): def __init__(self): self.data = [] @shared_contextmanager def foo(self, a): self.data.append(a) yield a self.data.append(-a) f = Foo() g = Foo() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] f.data.clear() g.data.clear() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] def test_shared_contextmanager_method_does_not_keep_object_alive_after_done(): import weakref import gc class Foo: @shared_contextmanager def foo(self): yield foo = Foo() weak_foo = weakref.ref(foo) with foo.foo(): del foo gc.collect() assert weak_foo() is not None, 'Object collected but contextmanager is active' gc.collect() assert weak_foo() is None, 'Object not collected but contextmanager has exited' def verify_concurrent_order(executed, *expected): look_at_index = 0 for expected_group in expected: executed_group = set(executed[look_at_index:look_at_index + len(expected_group)]) assert executed_group == expected_group, 'wrong execution order' look_at_index += len(executed_group) assert look_at_index == len(executed), 'executed list is shorted than expected' def test_synchronization_coordinator_wait_for_everyone(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) execute('after sleep') sync.wait_for_everyone() execute('after wait') sync.wait_for_everyone() sleep(i / 10) execute('after sleep 2') sync.wait_for_everyone() execute('after wait 2') mo.call(foo) verify_concurrent_order( executed, {(i, 'after sleep') for i in range(3)}, {(i, 'after wait') for i in range(3)}, {(i, 'after sleep 2') for i in range(3)}, {(i, 'after wait 2') for i in range(3)}) def test_synchronization_coordinator_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) assert sync.collect_and_call_once(i, len) == 3, 'parameters remain from previous call' mo.call(foo) verify_concurrent_order( executed, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_abandon(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sync.wait_for_everyone() execute('after wait 1') if i == 2: sync.abandon() return # Only two waiters should reach here sync.wait_for_everyone() execute('after wait 2') # Even without explicit call to abandon, sync should only wait for two waiters sync.wait_for_everyone() execute('after wait 3') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait 1') for i in range(3)}, {(i, 'after wait 2') for i in range(2)}, {(i, 'after wait 3') for i in range(2)}) def test_synchronization_coordinator_exception_in_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) times_called = 0 class MyException(Exception): pass def foo(i): def func_to_call_once(_): nonlocal times_called times_called += 1 raise MyException with pytest.raises(MyException): sync.collect_and_call_once(i, func_to_call_once) assert sync.collect_and_call_once(i + 1, sum) == 6 mo.call(foo) assert times_called == 1, 'collect_and_call_once with exception called the function more than once' def test_synchronization_coordinator_with_multiobject(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) sleep(i / 10) _sync.wait_for_everyone() execute('after wait') def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = _sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) foo(10) assert executed == [ (10, 'after wait'), 'params = [11]', (10, 'result is 11')] executed.clear() mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_with_multiobject_exception(): mo = MultiObject(range(3)) executed = [] class MyException(Exception): pass def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: raise MyException _sync.wait_for_everyone() execute('after wait/abandon') with pytest.raises(MultiException) as exc: mo.call(foo) assert exc.value.count == 1 assert exc.value.common_type is MyException verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_early_return(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: return _sync.wait_for_everyone() execute('after wait/abandon') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_method(): class Foo: def __init__(self, i): self.i = i def foo(self, _sync=SYNC): return (self.i, _sync.collect_and_call_once(self.i, lambda i_values: sorted(i_values))) mo = MultiObject(Foo(i) for i in range(3)) assert mo.foo().T == ( (0, [0, 1, 2]), (1, [0, 1, 2]), (2, [0, 1, 2])) def test_synchronization_coordinator_failing_context_manager(): class MyException(Exception): pass @contextmanager def foo(should_fail, _sync=SYNC): if should_fail: raise MyException() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_sync.py::test_synchronization_coordinator_timeout,tests/test_sync.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch, call import pytest from time import sleep import threading from contextlib import contextmanager, ExitStack import random import re from easypy.concurrency import MultiObject, MultiException, concurrent from easypy.timing import repeat, timing from easypy.bunch import Bunch from easypy.units import Duration from easypy.sync import iter_wait, wait, iter_wait_progress, Timer, TimeoutException, PredicateNotSatisfied from easypy.sync import SynchronizationCoordinator, SYNC from easypy.sync import shared_contextmanager from easypy.sync import TagAlongThread from easypy.sync import LoggedRLock, LockLeaseExpired from easypy.sync import SynchronizedSingleton from easypy.sync import LoggedCondition from .test_logging import get_log # noqa; pytest fixture def test_shared_contextmanager(): data = [] @shared_contextmanager def foo(a): data.append(a) yield a data.append(-a) with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] with foo(1): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] data.clear() with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] def test_shared_contextmanager_method(): class Foo(object): def __init__(self): self.data = [] @shared_contextmanager def foo(self, a): self.data.append(a) yield a self.data.append(-a) f = Foo() g = Foo() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] f.data.clear() g.data.clear() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] def test_shared_contextmanager_method_does_not_keep_object_alive_after_done(): import weakref import gc class Foo: @shared_contextmanager def foo(self): yield foo = Foo() weak_foo = weakref.ref(foo) with foo.foo(): del foo gc.collect() assert weak_foo() is not None, 'Object collected but contextmanager is active' gc.collect() assert weak_foo() is None, 'Object not collected but contextmanager has exited' def verify_concurrent_order(executed, *expected): look_at_index = 0 for expected_group in expected: executed_group = set(executed[look_at_index:look_at_index + len(expected_group)]) assert executed_group == expected_group, 'wrong execution order' look_at_index += len(executed_group) assert look_at_index == len(executed), 'executed list is shorted than expected' def test_synchronization_coordinator_wait_for_everyone(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) execute('after sleep') sync.wait_for_everyone() execute('after wait') sync.wait_for_everyone() sleep(i / 10) execute('after sleep 2') sync.wait_for_everyone() execute('after wait 2') mo.call(foo) verify_concurrent_order( executed, {(i, 'after sleep') for i in range(3)}, {(i, 'after wait') for i in range(3)}, {(i, 'after sleep 2') for i in range(3)}, {(i, 'after wait 2') for i in range(3)}) def test_synchronization_coordinator_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) assert sync.collect_and_call_once(i, len) == 3, 'parameters remain from previous call' mo.call(foo) verify_concurrent_order( executed, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_abandon(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sync.wait_for_everyone() execute('after wait 1') if i == 2: sync.abandon() return # Only two waiters should reach here sync.wait_for_everyone() execute('after wait 2') # Even without explicit call to abandon, sync should only wait for two waiters sync.wait_for_everyone() execute('after wait 3') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait 1') for i in range(3)}, {(i, 'after wait 2') for i in range(2)}, {(i, 'after wait 3') for i in range(2)}) def test_synchronization_coordinator_exception_in_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) times_called = 0 class MyException(Exception): pass def foo(i): def func_to_call_once(_): nonlocal times_called times_called += 1 raise MyException with pytest.raises(MyException): sync.collect_and_call_once(i, func_to_call_once) assert sync.collect_and_call_once(i + 1, sum) == 6 mo.call(foo) assert times_called == 1, 'collect_and_call_once with exception called the function more than once' def test_synchronization_coordinator_with_multiobject(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) sleep(i / 10) _sync.wait_for_everyone() execute('after wait') def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = _sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) foo(10) assert executed == [ (10, 'after wait'), 'params = [11]', (10, 'result is 11')] executed.clear() mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_with_multiobject_exception(): mo = MultiObject(range(3)) executed = [] class MyException(Exception): pass def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: raise MyException _sync.wait_for_everyone() execute('after wait/abandon') with pytest.raises(MultiException) as exc: mo.call(foo) assert exc.value.count == 1 assert exc.value.common_type is MyException verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_early_return(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: return _sync.wait_for_everyone() execute('after wait/abandon') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_method(): class Foo: def __init__(self, i): self.i = i def foo(self, _sync=SYNC): return (self.i, _sync.collect_and_call_once(self.i, lambda i_values: sorted(i_values))) mo = MultiObject(Foo(i) for i in range(3)) assert mo.foo().T == ( (0, [0, 1, 2]), (1, [0, 1, 2]), (2, [0, 1, 2])) def test_synchronization_coordinator_failing_context_manager(): class MyException(Exception): pass @contextmanager def foo(should_fail, _sync=SYNC): if should_fail: raise MyException() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_sync.py::test_synchronization_coordinator_with_multiobject_exception,tests/test_sync.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch, call import pytest from time import sleep import threading from contextlib import contextmanager, ExitStack import random import re from easypy.concurrency import MultiObject, MultiException, concurrent from easypy.timing import repeat, timing from easypy.bunch import Bunch from easypy.units import Duration from easypy.sync import iter_wait, wait, iter_wait_progress, Timer, TimeoutException, PredicateNotSatisfied from easypy.sync import SynchronizationCoordinator, SYNC from easypy.sync import shared_contextmanager from easypy.sync import TagAlongThread from easypy.sync import LoggedRLock, LockLeaseExpired from easypy.sync import SynchronizedSingleton from easypy.sync import LoggedCondition from .test_logging import get_log # noqa; pytest fixture def test_shared_contextmanager(): data = [] @shared_contextmanager def foo(a): data.append(a) yield a data.append(-a) with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] with foo(1): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] data.clear() with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] def test_shared_contextmanager_method(): class Foo(object): def __init__(self): self.data = [] @shared_contextmanager def foo(self, a): self.data.append(a) yield a self.data.append(-a) f = Foo() g = Foo() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] f.data.clear() g.data.clear() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] def test_shared_contextmanager_method_does_not_keep_object_alive_after_done(): import weakref import gc class Foo: @shared_contextmanager def foo(self): yield foo = Foo() weak_foo = weakref.ref(foo) with foo.foo(): del foo gc.collect() assert weak_foo() is not None, 'Object collected but contextmanager is active' gc.collect() assert weak_foo() is None, 'Object not collected but contextmanager has exited' def verify_concurrent_order(executed, *expected): look_at_index = 0 for expected_group in expected: executed_group = set(executed[look_at_index:look_at_index + len(expected_group)]) assert executed_group == expected_group, 'wrong execution order' look_at_index += len(executed_group) assert look_at_index == len(executed), 'executed list is shorted than expected' def test_synchronization_coordinator_wait_for_everyone(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) execute('after sleep') sync.wait_for_everyone() execute('after wait') sync.wait_for_everyone() sleep(i / 10) execute('after sleep 2') sync.wait_for_everyone() execute('after wait 2') mo.call(foo) verify_concurrent_order( executed, {(i, 'after sleep') for i in range(3)}, {(i, 'after wait') for i in range(3)}, {(i, 'after sleep 2') for i in range(3)}, {(i, 'after wait 2') for i in range(3)}) def test_synchronization_coordinator_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) assert sync.collect_and_call_once(i, len) == 3, 'parameters remain from previous call' mo.call(foo) verify_concurrent_order( executed, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_abandon(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sync.wait_for_everyone() execute('after wait 1') if i == 2: sync.abandon() return # Only two waiters should reach here sync.wait_for_everyone() execute('after wait 2') # Even without explicit call to abandon, sync should only wait for two waiters sync.wait_for_everyone() execute('after wait 3') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait 1') for i in range(3)}, {(i, 'after wait 2') for i in range(2)}, {(i, 'after wait 3') for i in range(2)}) def test_synchronization_coordinator_exception_in_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) times_called = 0 class MyException(Exception): pass def foo(i): def func_to_call_once(_): nonlocal times_called times_called += 1 raise MyException with pytest.raises(MyException): sync.collect_and_call_once(i, func_to_call_once) assert sync.collect_and_call_once(i + 1, sum) == 6 mo.call(foo) assert times_called == 1, 'collect_and_call_once with exception called the function more than once' def test_synchronization_coordinator_with_multiobject(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) sleep(i / 10) _sync.wait_for_everyone() execute('after wait') def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = _sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) foo(10) assert executed == [ (10, 'after wait'), 'params = [11]', (10, 'result is 11')] executed.clear() mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_with_multiobject_exception(): mo = MultiObject(range(3)) executed = [] class MyException(Exception): pass def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: raise MyException _sync.wait_for_everyone() execute('after wait/abandon') with pytest.raises(MultiException) as exc: mo.call(foo) assert exc.value.count == 1 assert exc.value.common_type is MyException verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_early_return(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: return _sync.wait_for_everyone() execute('after wait/abandon') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_method(): class Foo: def __init__(self, i): self.i = i def foo(self, _sync=SYNC): return (self.i, _sync.collect_and_call_once(self.i, lambda i_values: sorted(i_values))) mo = MultiObject(Foo(i) for i in range(3)) assert mo.foo().T == ( (0, [0, 1, 2]), (1, [0, 1, 2]), (2, [0, 1, 2])) def test_synchronization_coordinator_failing_context_manager(): class MyException(Exception): pass @contextmanager def foo(should_fail, _sync=SYNC): if should_fail: raise MyException() ", https://github.com/weka-io/easypy,9501c3ee03dcb1630e58cbb73b1647056907bcea,tests/test_sync.py::test_wait_log_predicate,tests/test_sync.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from unittest.mock import patch, call import pytest from time import sleep import threading from contextlib import contextmanager, ExitStack import random import re from easypy.concurrency import MultiObject, MultiException, concurrent from easypy.timing import repeat, timing from easypy.bunch import Bunch from easypy.units import Duration from easypy.sync import iter_wait, wait, iter_wait_progress, Timer, TimeoutException, PredicateNotSatisfied from easypy.sync import SynchronizationCoordinator, SYNC from easypy.sync import shared_contextmanager from easypy.sync import TagAlongThread from easypy.sync import LoggedRLock, LockLeaseExpired from easypy.sync import SynchronizedSingleton from easypy.sync import LoggedCondition from .test_logging import get_log # noqa; pytest fixture def test_shared_contextmanager(): data = [] @shared_contextmanager def foo(a): data.append(a) yield a data.append(-a) with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] with foo(1): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] data.clear() with foo(1): assert data == [1] with foo(2): assert data == [1, 2] with foo(2): assert data == [1, 2] assert data == [1, 2] assert data == [1, 2, -2] assert data == [1, 2, -2, -1] def test_shared_contextmanager_method(): class Foo(object): def __init__(self): self.data = [] @shared_contextmanager def foo(self, a): self.data.append(a) yield a self.data.append(-a) f = Foo() g = Foo() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] f.data.clear() g.data.clear() with f.foo(1), g.foo(5): assert f.data == [1] assert g.data == [5] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(2), g.foo(6): assert f.data == [1, 2] assert g.data == [5, 6] with f.foo(1), g.foo(5): assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2] assert g.data == [5, 6] assert f.data == [1, 2, -2] assert g.data == [5, 6, -6] assert f.data == [1, 2, -2, -1] assert g.data == [5, 6, -6, -5] def test_shared_contextmanager_method_does_not_keep_object_alive_after_done(): import weakref import gc class Foo: @shared_contextmanager def foo(self): yield foo = Foo() weak_foo = weakref.ref(foo) with foo.foo(): del foo gc.collect() assert weak_foo() is not None, 'Object collected but contextmanager is active' gc.collect() assert weak_foo() is None, 'Object not collected but contextmanager has exited' def verify_concurrent_order(executed, *expected): look_at_index = 0 for expected_group in expected: executed_group = set(executed[look_at_index:look_at_index + len(expected_group)]) assert executed_group == expected_group, 'wrong execution order' look_at_index += len(executed_group) assert look_at_index == len(executed), 'executed list is shorted than expected' def test_synchronization_coordinator_wait_for_everyone(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) execute('after sleep') sync.wait_for_everyone() execute('after wait') sync.wait_for_everyone() sleep(i / 10) execute('after sleep 2') sync.wait_for_everyone() execute('after wait 2') mo.call(foo) verify_concurrent_order( executed, {(i, 'after sleep') for i in range(3)}, {(i, 'after wait') for i in range(3)}, {(i, 'after sleep 2') for i in range(3)}, {(i, 'after wait 2') for i in range(3)}) def test_synchronization_coordinator_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sleep(i / 10) def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) assert sync.collect_and_call_once(i, len) == 3, 'parameters remain from previous call' mo.call(foo) verify_concurrent_order( executed, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_abandon(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) executed = [] def foo(i): def execute(caption): executed.append((i, caption)) sync.wait_for_everyone() execute('after wait 1') if i == 2: sync.abandon() return # Only two waiters should reach here sync.wait_for_everyone() execute('after wait 2') # Even without explicit call to abandon, sync should only wait for two waiters sync.wait_for_everyone() execute('after wait 3') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait 1') for i in range(3)}, {(i, 'after wait 2') for i in range(2)}, {(i, 'after wait 3') for i in range(2)}) def test_synchronization_coordinator_exception_in_collect_and_call_once(): mo = MultiObject(range(3)) sync = SynchronizationCoordinator(len(mo)) times_called = 0 class MyException(Exception): pass def foo(i): def func_to_call_once(_): nonlocal times_called times_called += 1 raise MyException with pytest.raises(MyException): sync.collect_and_call_once(i, func_to_call_once) assert sync.collect_and_call_once(i + 1, sum) == 6 mo.call(foo) assert times_called == 1, 'collect_and_call_once with exception called the function more than once' def test_synchronization_coordinator_with_multiobject(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) sleep(i / 10) _sync.wait_for_everyone() execute('after wait') def func_to_call_once(param): executed.append('params = %s' % sorted(param)) return sum(param) result = _sync.collect_and_call_once(i + 1, func_to_call_once) execute('result is %s' % result) foo(10) assert executed == [ (10, 'after wait'), 'params = [11]', (10, 'result is 11')] executed.clear() mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {'params = [1, 2, 3]'}, {(i, 'result is 6') for i in range(3)}) def test_synchronization_coordinator_with_multiobject_exception(): mo = MultiObject(range(3)) executed = [] class MyException(Exception): pass def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: raise MyException _sync.wait_for_everyone() execute('after wait/abandon') with pytest.raises(MultiException) as exc: mo.call(foo) assert exc.value.count == 1 assert exc.value.common_type is MyException verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_early_return(): mo = MultiObject(range(3)) executed = [] def foo(i, _sync=SYNC): def execute(caption): executed.append((i, caption)) _sync.wait_for_everyone() execute('after wait') if i == 2: return _sync.wait_for_everyone() execute('after wait/abandon') mo.call(foo) verify_concurrent_order( executed, {(i, 'after wait') for i in range(3)}, {(i, 'after wait/abandon') for i in range(2)}) def test_synchronization_coordinator_with_multiobject_method(): class Foo: def __init__(self, i): self.i = i def foo(self, _sync=SYNC): return (self.i, _sync.collect_and_call_once(self.i, lambda i_values: sorted(i_values))) mo = MultiObject(Foo(i) for i in range(3)) assert mo.foo().T == ( (0, [0, 1, 2]), (1, [0, 1, 2]), (2, [0, 1, 2])) def test_synchronization_coordinator_failing_context_manager(): class MyException(Exception): pass @contextmanager def foo(should_fail, _sync=SYNC): if should_fail: raise MyException() ", https://github.com/whisller/pyeventdispatcher,5382e5e8578dea89a76481e15d410d516999055a,test/test_pyeventdispatcher.py::TestRegister::test_it_allows_to_register[],test/test_pyeventdispatcher.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest import pyeventdispatcher from pyeventdispatcher import ( EventDispatcher, Event, EventDispatcherException, EventSubscriber, listen, ) from pyeventdispatcher.event_dispatcher import ( MemoryRegistry, register_global_listener, register_event_subscribers, ) class TestRegister: class MyListener: def call_on_event(self, event): print(event.data) @pytest.mark.parametrize( ""registered"", [MyListener().call_on_event, lambda event: print(event.data)] ) def test_it_allows_to_register(self, registered, capsys): py_event_dispatcher = EventDispatcher() py_event_dispatcher.register(""foo.bar"", registered) py_event_dispatcher.dispatch(Event(""foo.bar"", {""a"": ""b""})) captured = capsys.readouterr() assert captured.out == ""{'a': 'b'}\n"" @pytest.mark.parametrize( ""to_register, output"", [ # With default ""priority"" - in order they were added ( ( {""lambda"": lambda event: print(""First""), ""priority"": 0}, {""lambda"": lambda event: print(""Second""), ""priority"": 0}, ), ""First\nSecond\n"", ), # Based on priority ( ( {""lambda"": lambda event: print(""First""), ""priority"": 0}, {""lambda"": lambda event: print(""Second""), ""priority"": -100}, ), ""Second\nFirst\n"", ), ], ) def test_listeners_executed_in_order(self, to_register, output, capsys): py_event_dispatcher = EventDispatcher() for register in to_register: py_event_dispatcher.register( ""foo.bar"", register[""lambda""], register[""priority""] ) py_event_dispatcher.dispatch(Event(""foo.bar"", {""a"": ""b""})) captured = capsys.readouterr() assert captured.out == output def test_it_raises_an_exception_when_non_callable_is_trying_to_be_registered(self): py_event_dispatcher = EventDispatcher() with pytest.raises(EventDispatcherException): py_event_dispatcher.register(""foo.bar"", """") @pytest.mark.parametrize(""priority"", [None, """"]) def test_it_raises_an_exception_when_priority_is_not_integer(self, priority): py_event_dispatcher = EventDispatcher() with pytest.raises(EventDispatcherException): py_event_dispatcher.register( ""foo.bar"", lambda event: print(event), priority ) class TestRegisterGlobal: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_it_allows_to_register_listener_globally(self, capsys): def my_listener(event): print(""my_listener"") def global_listener(event): print(""global"") register_global_listener(""foo.bar"", global_listener) py_event_dispatcher_1 = EventDispatcher() py_event_dispatcher_1.register(""foo.bar"", my_listener) py_event_dispatcher_2 = EventDispatcher() py_event_dispatcher_2.register(""foo.bar"", my_listener) py_event_dispatcher_1.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""my_listener\nglobal\n"" class TestRegisterSubscribers: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() class MySubscriber1(EventSubscriber): EVENTS = {""foo.bar"": ""execute_one"", ""bar.foo"": (""execute_two"", -10)} @staticmethod def execute_one(event): print(""MySubscriber1::execute_one"") @staticmethod def execute_two(event): print(""MySubscriber1::execute_two"") def test_register_global_listeners_by_subscriber(self, capsys): register_event_subscribers() py_event_dispatcher = EventDispatcher() py_event_dispatcher.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""MySubscriber1::execute_one\n"" class TestRegisterThroughDecorator: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_register_global_listener_by_decorator(self, capsys): @listen(""foo.bar"") def my_test_function(event): print(event.name) py_event_dispatcher = EventDispatcher() py_event_dispatcher.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""foo.bar\n"" class TestStopPropagation: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_it_stops_propagation(self, capsys): def first_listener(event): event.stop = True print(""first_listener"") def second_listener(event): print(""first_listener"") py_event_dispatcher = EventDispatcher() py_event_dispatcher.register(""foo.bar"", first_listener) py_event_dispatcher.register(""foo.bar"", second_listener) py_event_dispatcher.dispatch(Event(""foo.bar"", {})) captured = capsys.readouterr() assert captured.out == ""first_listener\n"" ", https://github.com/whisller/pyeventdispatcher,5382e5e8578dea89a76481e15d410d516999055a,test/test_pyeventdispatcher.py::TestRegister::test_it_allows_to_register[call_on_event],test/test_pyeventdispatcher.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest import pyeventdispatcher from pyeventdispatcher import ( EventDispatcher, Event, EventDispatcherException, EventSubscriber, listen, ) from pyeventdispatcher.event_dispatcher import ( MemoryRegistry, register_global_listener, register_event_subscribers, ) class TestRegister: class MyListener: def call_on_event(self, event): print(event.data) @pytest.mark.parametrize( ""registered"", [MyListener().call_on_event, lambda event: print(event.data)] ) def test_it_allows_to_register(self, registered, capsys): py_event_dispatcher = EventDispatcher() py_event_dispatcher.register(""foo.bar"", registered) py_event_dispatcher.dispatch(Event(""foo.bar"", {""a"": ""b""})) captured = capsys.readouterr() assert captured.out == ""{'a': 'b'}\n"" @pytest.mark.parametrize( ""to_register, output"", [ # With default ""priority"" - in order they were added ( ( {""lambda"": lambda event: print(""First""), ""priority"": 0}, {""lambda"": lambda event: print(""Second""), ""priority"": 0}, ), ""First\nSecond\n"", ), # Based on priority ( ( {""lambda"": lambda event: print(""First""), ""priority"": 0}, {""lambda"": lambda event: print(""Second""), ""priority"": -100}, ), ""Second\nFirst\n"", ), ], ) def test_listeners_executed_in_order(self, to_register, output, capsys): py_event_dispatcher = EventDispatcher() for register in to_register: py_event_dispatcher.register( ""foo.bar"", register[""lambda""], register[""priority""] ) py_event_dispatcher.dispatch(Event(""foo.bar"", {""a"": ""b""})) captured = capsys.readouterr() assert captured.out == output def test_it_raises_an_exception_when_non_callable_is_trying_to_be_registered(self): py_event_dispatcher = EventDispatcher() with pytest.raises(EventDispatcherException): py_event_dispatcher.register(""foo.bar"", """") @pytest.mark.parametrize(""priority"", [None, """"]) def test_it_raises_an_exception_when_priority_is_not_integer(self, priority): py_event_dispatcher = EventDispatcher() with pytest.raises(EventDispatcherException): py_event_dispatcher.register( ""foo.bar"", lambda event: print(event), priority ) class TestRegisterGlobal: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_it_allows_to_register_listener_globally(self, capsys): def my_listener(event): print(""my_listener"") def global_listener(event): print(""global"") register_global_listener(""foo.bar"", global_listener) py_event_dispatcher_1 = EventDispatcher() py_event_dispatcher_1.register(""foo.bar"", my_listener) py_event_dispatcher_2 = EventDispatcher() py_event_dispatcher_2.register(""foo.bar"", my_listener) py_event_dispatcher_1.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""my_listener\nglobal\n"" class TestRegisterSubscribers: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() class MySubscriber1(EventSubscriber): EVENTS = {""foo.bar"": ""execute_one"", ""bar.foo"": (""execute_two"", -10)} @staticmethod def execute_one(event): print(""MySubscriber1::execute_one"") @staticmethod def execute_two(event): print(""MySubscriber1::execute_two"") def test_register_global_listeners_by_subscriber(self, capsys): register_event_subscribers() py_event_dispatcher = EventDispatcher() py_event_dispatcher.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""MySubscriber1::execute_one\n"" class TestRegisterThroughDecorator: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_register_global_listener_by_decorator(self, capsys): @listen(""foo.bar"") def my_test_function(event): print(event.name) py_event_dispatcher = EventDispatcher() py_event_dispatcher.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""foo.bar\n"" class TestStopPropagation: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_it_stops_propagation(self, capsys): def first_listener(event): event.stop = True print(""first_listener"") def second_listener(event): print(""first_listener"") py_event_dispatcher = EventDispatcher() py_event_dispatcher.register(""foo.bar"", first_listener) py_event_dispatcher.register(""foo.bar"", second_listener) py_event_dispatcher.dispatch(Event(""foo.bar"", {})) captured = capsys.readouterr() assert captured.out == ""first_listener\n"" ", https://github.com/whisller/pyeventdispatcher,433accc6e5682280189d8a584b87d4c3f815e200,test/test_pyeventdispatcher.py::TestRegister::test_listeners_executed_in_order test/test_pyeventdispatcher.py::TestRegister::test_it_allows_to_register,test/test_pyeventdispatcher.py,OD,flaky,RepoArchived,nan,classify;root_cause,"import pytest import pyeventdispatcher from pyeventdispatcher import ( EventDispatcher, Event, EventDispatcherException, EventSubscriber, listen, ) from pyeventdispatcher.event_dispatcher import ( MemoryRegistry, register_global_listener, register_event_subscribers, ) class TestRegister: class MyListener: def call_on_event(self, event): print(event.data) @pytest.mark.parametrize( ""registered"", [MyListener().call_on_event, lambda event: print(event.data)] ) def test_it_allows_to_register(self, registered, capsys): py_event_dispatcher = EventDispatcher() py_event_dispatcher.register(""foo.bar"", registered) py_event_dispatcher.dispatch(Event(""foo.bar"", {""a"": ""b""})) captured = capsys.readouterr() assert captured.out == ""{'a': 'b'}\n"" @pytest.mark.parametrize( ""to_register, output"", [ # With default ""priority"" - in order they were added ( ( {""lambda"": lambda event: print(""First""), ""priority"": 0}, {""lambda"": lambda event: print(""Second""), ""priority"": 0}, ), ""First\nSecond\n"", ), # Based on priority ( ( {""lambda"": lambda event: print(""First""), ""priority"": 0}, {""lambda"": lambda event: print(""Second""), ""priority"": -100}, ), ""Second\nFirst\n"", ), ], ) def test_listeners_executed_in_order(self, to_register, output, capsys): py_event_dispatcher = EventDispatcher() for register in to_register: py_event_dispatcher.register( ""foo.bar"", register[""lambda""], register[""priority""] ) py_event_dispatcher.dispatch(Event(""foo.bar"", {""a"": ""b""})) captured = capsys.readouterr() assert captured.out == output def test_it_raises_an_exception_when_non_callable_is_trying_to_be_registered(self): py_event_dispatcher = EventDispatcher() with pytest.raises(EventDispatcherException): py_event_dispatcher.register(""foo.bar"", """") @pytest.mark.parametrize(""priority"", [None, """"]) def test_it_raises_an_exception_when_priority_is_not_integer(self, priority): py_event_dispatcher = EventDispatcher() with pytest.raises(EventDispatcherException): py_event_dispatcher.register( ""foo.bar"", lambda event: print(event), priority ) class TestRegisterGlobal: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_it_allows_to_register_listener_globally(self, capsys): def my_listener(event): print(""my_listener"") def global_listener(event): print(""global"") register_global_listener(""foo.bar"", global_listener) py_event_dispatcher_1 = EventDispatcher() py_event_dispatcher_1.register(""foo.bar"", my_listener) py_event_dispatcher_2 = EventDispatcher() py_event_dispatcher_2.register(""foo.bar"", my_listener) py_event_dispatcher_1.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""my_listener\nglobal\n"" class TestRegisterSubscribers: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() class MySubscriber1(EventSubscriber): EVENTS = {""foo.bar"": ""execute_one"", ""bar.foo"": (""execute_two"", -10)} @staticmethod def execute_one(event): print(""MySubscriber1::execute_one"") @staticmethod def execute_two(event): print(""MySubscriber1::execute_two"") def test_register_global_listeners_by_subscriber(self, capsys): register_event_subscribers() py_event_dispatcher = EventDispatcher() py_event_dispatcher.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""MySubscriber1::execute_one\n"" class TestRegisterThroughDecorator: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_register_global_listener_by_decorator(self, capsys): @listen(""foo.bar"") def my_test_function(event): print(event.name) py_event_dispatcher = EventDispatcher() py_event_dispatcher.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""foo.bar\n"" class TestStopPropagation: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_it_stops_propagation(self, capsys): def first_listener(event): event.stop = True print(""first_listener"") def second_listener(event): print(""first_listener"") py_event_dispatcher = EventDispatcher() py_event_dispatcher.register(""foo.bar"", first_listener) py_event_dispatcher.register(""foo.bar"", second_listener) py_event_dispatcher.dispatch(Event(""foo.bar"", {})) captured = capsys.readouterr() assert captured.out == ""first_listener\n"" ", https://github.com/whisller/pyeventdispatcher,5382e5e8578dea89a76481e15d410d516999055a,test/test_pyeventdispatcher.py::TestRegister::test_listeners_executed_in_order[to_register0-First\nSecond\n],test/test_pyeventdispatcher.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest import pyeventdispatcher from pyeventdispatcher import ( EventDispatcher, Event, EventDispatcherException, EventSubscriber, listen, ) from pyeventdispatcher.event_dispatcher import ( MemoryRegistry, register_global_listener, register_event_subscribers, ) class TestRegister: class MyListener: def call_on_event(self, event): print(event.data) @pytest.mark.parametrize( ""registered"", [MyListener().call_on_event, lambda event: print(event.data)] ) def test_it_allows_to_register(self, registered, capsys): py_event_dispatcher = EventDispatcher() py_event_dispatcher.register(""foo.bar"", registered) py_event_dispatcher.dispatch(Event(""foo.bar"", {""a"": ""b""})) captured = capsys.readouterr() assert captured.out == ""{'a': 'b'}\n"" @pytest.mark.parametrize( ""to_register, output"", [ # With default ""priority"" - in order they were added ( ( {""lambda"": lambda event: print(""First""), ""priority"": 0}, {""lambda"": lambda event: print(""Second""), ""priority"": 0}, ), ""First\nSecond\n"", ), # Based on priority ( ( {""lambda"": lambda event: print(""First""), ""priority"": 0}, {""lambda"": lambda event: print(""Second""), ""priority"": -100}, ), ""Second\nFirst\n"", ), ], ) def test_listeners_executed_in_order(self, to_register, output, capsys): py_event_dispatcher = EventDispatcher() for register in to_register: py_event_dispatcher.register( ""foo.bar"", register[""lambda""], register[""priority""] ) py_event_dispatcher.dispatch(Event(""foo.bar"", {""a"": ""b""})) captured = capsys.readouterr() assert captured.out == output def test_it_raises_an_exception_when_non_callable_is_trying_to_be_registered(self): py_event_dispatcher = EventDispatcher() with pytest.raises(EventDispatcherException): py_event_dispatcher.register(""foo.bar"", """") @pytest.mark.parametrize(""priority"", [None, """"]) def test_it_raises_an_exception_when_priority_is_not_integer(self, priority): py_event_dispatcher = EventDispatcher() with pytest.raises(EventDispatcherException): py_event_dispatcher.register( ""foo.bar"", lambda event: print(event), priority ) class TestRegisterGlobal: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_it_allows_to_register_listener_globally(self, capsys): def my_listener(event): print(""my_listener"") def global_listener(event): print(""global"") register_global_listener(""foo.bar"", global_listener) py_event_dispatcher_1 = EventDispatcher() py_event_dispatcher_1.register(""foo.bar"", my_listener) py_event_dispatcher_2 = EventDispatcher() py_event_dispatcher_2.register(""foo.bar"", my_listener) py_event_dispatcher_1.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""my_listener\nglobal\n"" class TestRegisterSubscribers: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() class MySubscriber1(EventSubscriber): EVENTS = {""foo.bar"": ""execute_one"", ""bar.foo"": (""execute_two"", -10)} @staticmethod def execute_one(event): print(""MySubscriber1::execute_one"") @staticmethod def execute_two(event): print(""MySubscriber1::execute_two"") def test_register_global_listeners_by_subscriber(self, capsys): register_event_subscribers() py_event_dispatcher = EventDispatcher() py_event_dispatcher.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""MySubscriber1::execute_one\n"" class TestRegisterThroughDecorator: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_register_global_listener_by_decorator(self, capsys): @listen(""foo.bar"") def my_test_function(event): print(event.name) py_event_dispatcher = EventDispatcher() py_event_dispatcher.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""foo.bar\n"" class TestStopPropagation: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_it_stops_propagation(self, capsys): def first_listener(event): event.stop = True print(""first_listener"") def second_listener(event): print(""first_listener"") py_event_dispatcher = EventDispatcher() py_event_dispatcher.register(""foo.bar"", first_listener) py_event_dispatcher.register(""foo.bar"", second_listener) py_event_dispatcher.dispatch(Event(""foo.bar"", {})) captured = capsys.readouterr() assert captured.out == ""first_listener\n"" ", https://github.com/whisller/pyeventdispatcher,5382e5e8578dea89a76481e15d410d516999055a,test/test_pyeventdispatcher.py::TestRegister::test_listeners_executed_in_order[to_register1-Second\nFirst\n],test/test_pyeventdispatcher.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"import pytest import pyeventdispatcher from pyeventdispatcher import ( EventDispatcher, Event, EventDispatcherException, EventSubscriber, listen, ) from pyeventdispatcher.event_dispatcher import ( MemoryRegistry, register_global_listener, register_event_subscribers, ) class TestRegister: class MyListener: def call_on_event(self, event): print(event.data) @pytest.mark.parametrize( ""registered"", [MyListener().call_on_event, lambda event: print(event.data)] ) def test_it_allows_to_register(self, registered, capsys): py_event_dispatcher = EventDispatcher() py_event_dispatcher.register(""foo.bar"", registered) py_event_dispatcher.dispatch(Event(""foo.bar"", {""a"": ""b""})) captured = capsys.readouterr() assert captured.out == ""{'a': 'b'}\n"" @pytest.mark.parametrize( ""to_register, output"", [ # With default ""priority"" - in order they were added ( ( {""lambda"": lambda event: print(""First""), ""priority"": 0}, {""lambda"": lambda event: print(""Second""), ""priority"": 0}, ), ""First\nSecond\n"", ), # Based on priority ( ( {""lambda"": lambda event: print(""First""), ""priority"": 0}, {""lambda"": lambda event: print(""Second""), ""priority"": -100}, ), ""Second\nFirst\n"", ), ], ) def test_listeners_executed_in_order(self, to_register, output, capsys): py_event_dispatcher = EventDispatcher() for register in to_register: py_event_dispatcher.register( ""foo.bar"", register[""lambda""], register[""priority""] ) py_event_dispatcher.dispatch(Event(""foo.bar"", {""a"": ""b""})) captured = capsys.readouterr() assert captured.out == output def test_it_raises_an_exception_when_non_callable_is_trying_to_be_registered(self): py_event_dispatcher = EventDispatcher() with pytest.raises(EventDispatcherException): py_event_dispatcher.register(""foo.bar"", """") @pytest.mark.parametrize(""priority"", [None, """"]) def test_it_raises_an_exception_when_priority_is_not_integer(self, priority): py_event_dispatcher = EventDispatcher() with pytest.raises(EventDispatcherException): py_event_dispatcher.register( ""foo.bar"", lambda event: print(event), priority ) class TestRegisterGlobal: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_it_allows_to_register_listener_globally(self, capsys): def my_listener(event): print(""my_listener"") def global_listener(event): print(""global"") register_global_listener(""foo.bar"", global_listener) py_event_dispatcher_1 = EventDispatcher() py_event_dispatcher_1.register(""foo.bar"", my_listener) py_event_dispatcher_2 = EventDispatcher() py_event_dispatcher_2.register(""foo.bar"", my_listener) py_event_dispatcher_1.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""my_listener\nglobal\n"" class TestRegisterSubscribers: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() class MySubscriber1(EventSubscriber): EVENTS = {""foo.bar"": ""execute_one"", ""bar.foo"": (""execute_two"", -10)} @staticmethod def execute_one(event): print(""MySubscriber1::execute_one"") @staticmethod def execute_two(event): print(""MySubscriber1::execute_two"") def test_register_global_listeners_by_subscriber(self, capsys): register_event_subscribers() py_event_dispatcher = EventDispatcher() py_event_dispatcher.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""MySubscriber1::execute_one\n"" class TestRegisterThroughDecorator: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_register_global_listener_by_decorator(self, capsys): @listen(""foo.bar"") def my_test_function(event): print(event.name) py_event_dispatcher = EventDispatcher() py_event_dispatcher.dispatch(Event(""foo.bar"", None)) captured = capsys.readouterr() assert captured.out == ""foo.bar\n"" class TestStopPropagation: def setup_method(self): pyeventdispatcher.event_dispatcher.global_registry = MemoryRegistry() def test_it_stops_propagation(self, capsys): def first_listener(event): event.stop = True print(""first_listener"") def second_listener(event): print(""first_listener"") py_event_dispatcher = EventDispatcher() py_event_dispatcher.register(""foo.bar"", first_listener) py_event_dispatcher.register(""foo.bar"", second_listener) py_event_dispatcher.dispatch(Event(""foo.bar"", {})) captured = capsys.readouterr() assert captured.out == ""first_listener\n"" ", https://github.com/wilfredinni/coo,da9e75d46ed65599cd2223b029ac30c08496a80b,tests/test_coo.py::test_schedule_time_zone_media,tests/test_coo.py,NIO,flaky,Accepted,https://github.com/wilfredinni/coo/pull/1,classify;root_cause;fix_proposal,"from pathlib import Path import pytest from twitter.error import TwitterError from coo import Coo from coo._exceptions import TweetTypeError, ScheduleError # Mock Update list m_updates = [""mock1"", ""mock2"", ""mock3"", ""mock4"", ""mock5""] def test_input_types(): with pytest.raises(TypeError): Coo([1], [1], [1], [1]) with pytest.raises(TypeError): Coo({1: 5}, {1: 5}, {1: 5}, {1: 5}) with pytest.raises(TypeError): Coo(1, 2, 3, 4) # correct construction. no error Coo(""mock"", ""mock"", ""mock"", ""mock"") @pytest.fixture def coo_preview_instance(): yield Coo(""mock"", ""mock"", ""mock"", ""mock"", preview=True) @pytest.fixture def coo_mock_instance(): yield Coo(""mock1"", ""mock2"", ""mock3"", ""mock4"") # API def test_wrong_credentials_TwitterError(coo_mock_instance): with pytest.raises(TwitterError): coo_mock_instance.verify # TWEET @pytest.mark.parametrize( ""updates, delay, interval, template, time_zone"", [ (m_updates, None, None, None, None), # One None (m_updates, None, ""now"", ""$message"", ""local""), (m_updates, ""now"", None, ""$message"", ""local""), (m_updates, ""now"", ""now"", None, ""local""), (m_updates, ""now"", ""now"", ""$message"", None), # Two None (m_updates, None, None, ""$message"", ""local""), (m_updates, ""now"", None, None, ""local""), (m_updates, ""now"", ""now"", None, None), (m_updates, None, ""now"", ""$message"", None), # _delay (m_updates, ""now"", None, None, None), (m_updates, 0, None, None, None), # _interval (m_updates, None, ""now"", None, None), (m_updates, None, 0, None, None), # Template (m_updates, None, None, ""$message"", None), # Time zone (m_updates, None, None, None, ""local""), (m_updates, None, None, None, ""America/Santiago ""), ], ) def test_tweet(coo_preview_instance, updates, delay, interval, template, time_zone): coo_preview_instance.tweet(updates, delay, interval, template, time_zone) @pytest.mark.parametrize( ""tz"", [ (""Canada/Yukon""), (""Brazil/Acre""), (""Australia/Tasmania""), (""America/Santiago""), (""America/Detroit""), (""Asia/Atyrau""), ], ) def test_tweet_time_zone(coo_preview_instance, tz): coo_preview_instance.tweet([""mock""], time_zone=tz) assert coo_preview_instance.time_zone == tz def test_tweet_random(coo_preview_instance): updates = [""mock1"", ""mock2"", ""mock3"", ""mock4"", ""mock5""] coo_preview_instance.tweet(m_updates, aleatory=True) assert updates != m_updates def test_tweet_media_update(coo_preview_instance): coo_preview_instance.tweet([""mock""], media=""../coo.png"") assert coo_preview_instance.media == Path(""../coo.png"") @pytest.mark.parametrize( ""updates"", [ # update is not a instance of list: ((1, 2, 3)), ({1, 2, 3}), (123), (""string""), # The instances 'in' the list are no strings: ([(1, 2, 3)]), ([{1, 2, 3}]), ([[1, 2, 3]]), ([1, 2, 3]), ], ) def test_tweet_TweetTypeError(coo_preview_instance, updates): with pytest.raises(TweetTypeError): coo_preview_instance.tweet(updates) def test_tweet_media_FileNotFoundError(coo_mock_instance): with pytest.raises(FileNotFoundError): coo_mock_instance.tweet([""mock""], media=""coo_.png"") def test_tweet_media_TwitterError(coo_mock_instance): with pytest.raises(TwitterError): coo_mock_instance.tweet([""mock""], media=""coo.png"") def test_tweet_none_media_TwitterError(coo_mock_instance): with pytest.raises(TwitterError): coo_mock_instance.tweet([""mock""], media=None) # SCHEDULE def test_schedule_time_zone_media(coo_preview_instance): updates = [ (""now"", ""template"", ""update""), (0, ""template"", ""update""), (""now"", None, ""update""), (0, None, ""update""), (0, None, ""update"", ""../coo.png""), ] coo_preview_instance.schedule(updates, time_zone=""Canada/Yukon"", media=""../coo.png"") assert coo_preview_instance.time_zone == ""Canada/Yukon"" assert coo_preview_instance.media == Path(""../coo.png"") assert coo_preview_instance.global_media == Path(""../coo.png"") @pytest.mark.parametrize( ""updates"", [ ([[""update1"", ""update2""]]), ([{""update1"", ""update2""}]), ([""update1"", ""update2""]), ([123, 456, 789]), # len tuple ([(""now"")]), ], ) def test_schedule_ScheduleError(coo_preview_instance, updates): with pytest.raises(ScheduleError): coo_preview_instance.schedule(updates) # STR UPDATE @pytest.mark.parametrize( ""update, template"", [(""My Twitter Update"", None), (""My Twitter Update"", ""$message"")] ) def test__str_update(coo_preview_instance, update, template): coo_preview_instance._str_update(update, template) # _delay @pytest.mark.parametrize(""delay"", [(0), (""now"")]) def test_delay(coo_preview_instance, delay): coo_preview_instance._delay(delay) # _interval @pytest.mark.parametrize(""_interval"", [(0), (""now"")]) def test__interval(coo_preview_instance, _interval): coo_preview_instance._interval(_interval) ", https://github.com/wilfredinni/coo,da9e75d46ed65599cd2223b029ac30c08496a80b,tests/test_coo.py::test_tweet_none_media_TwitterError,tests/test_coo.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"from pathlib import Path import pytest from twitter.error import TwitterError from coo import Coo from coo._exceptions import TweetTypeError, ScheduleError # Mock Update list m_updates = [""mock1"", ""mock2"", ""mock3"", ""mock4"", ""mock5""] def test_input_types(): with pytest.raises(TypeError): Coo([1], [1], [1], [1]) with pytest.raises(TypeError): Coo({1: 5}, {1: 5}, {1: 5}, {1: 5}) with pytest.raises(TypeError): Coo(1, 2, 3, 4) # correct construction. no error Coo(""mock"", ""mock"", ""mock"", ""mock"") @pytest.fixture def coo_preview_instance(): yield Coo(""mock"", ""mock"", ""mock"", ""mock"", preview=True) @pytest.fixture def coo_mock_instance(): yield Coo(""mock1"", ""mock2"", ""mock3"", ""mock4"") # API def test_wrong_credentials_TwitterError(coo_mock_instance): with pytest.raises(TwitterError): coo_mock_instance.verify # TWEET @pytest.mark.parametrize( ""updates, delay, interval, template, time_zone"", [ (m_updates, None, None, None, None), # One None (m_updates, None, ""now"", ""$message"", ""local""), (m_updates, ""now"", None, ""$message"", ""local""), (m_updates, ""now"", ""now"", None, ""local""), (m_updates, ""now"", ""now"", ""$message"", None), # Two None (m_updates, None, None, ""$message"", ""local""), (m_updates, ""now"", None, None, ""local""), (m_updates, ""now"", ""now"", None, None), (m_updates, None, ""now"", ""$message"", None), # _delay (m_updates, ""now"", None, None, None), (m_updates, 0, None, None, None), # _interval (m_updates, None, ""now"", None, None), (m_updates, None, 0, None, None), # Template (m_updates, None, None, ""$message"", None), # Time zone (m_updates, None, None, None, ""local""), (m_updates, None, None, None, ""America/Santiago ""), ], ) def test_tweet(coo_preview_instance, updates, delay, interval, template, time_zone): coo_preview_instance.tweet(updates, delay, interval, template, time_zone) @pytest.mark.parametrize( ""tz"", [ (""Canada/Yukon""), (""Brazil/Acre""), (""Australia/Tasmania""), (""America/Santiago""), (""America/Detroit""), (""Asia/Atyrau""), ], ) def test_tweet_time_zone(coo_preview_instance, tz): coo_preview_instance.tweet([""mock""], time_zone=tz) assert coo_preview_instance.time_zone == tz def test_tweet_random(coo_preview_instance): updates = [""mock1"", ""mock2"", ""mock3"", ""mock4"", ""mock5""] coo_preview_instance.tweet(m_updates, aleatory=True) assert updates != m_updates def test_tweet_media_update(coo_preview_instance): coo_preview_instance.tweet([""mock""], media=""../coo.png"") assert coo_preview_instance.media == Path(""../coo.png"") @pytest.mark.parametrize( ""updates"", [ # update is not a instance of list: ((1, 2, 3)), ({1, 2, 3}), (123), (""string""), # The instances 'in' the list are no strings: ([(1, 2, 3)]), ([{1, 2, 3}]), ([[1, 2, 3]]), ([1, 2, 3]), ], ) def test_tweet_TweetTypeError(coo_preview_instance, updates): with pytest.raises(TweetTypeError): coo_preview_instance.tweet(updates) def test_tweet_media_FileNotFoundError(coo_mock_instance): with pytest.raises(FileNotFoundError): coo_mock_instance.tweet([""mock""], media=""coo_.png"") def test_tweet_media_TwitterError(coo_mock_instance): with pytest.raises(TwitterError): coo_mock_instance.tweet([""mock""], media=""coo.png"") def test_tweet_none_media_TwitterError(coo_mock_instance): with pytest.raises(TwitterError): coo_mock_instance.tweet([""mock""], media=None) # SCHEDULE def test_schedule_time_zone_media(coo_preview_instance): updates = [ (""now"", ""template"", ""update""), (0, ""template"", ""update""), (""now"", None, ""update""), (0, None, ""update""), (0, None, ""update"", ""../coo.png""), ] coo_preview_instance.schedule(updates, time_zone=""Canada/Yukon"", media=""../coo.png"") assert coo_preview_instance.time_zone == ""Canada/Yukon"" assert coo_preview_instance.media == Path(""../coo.png"") assert coo_preview_instance.global_media == Path(""../coo.png"") @pytest.mark.parametrize( ""updates"", [ ([[""update1"", ""update2""]]), ([{""update1"", ""update2""}]), ([""update1"", ""update2""]), ([123, 456, 789]), # len tuple ([(""now"")]), ], ) def test_schedule_ScheduleError(coo_preview_instance, updates): with pytest.raises(ScheduleError): coo_preview_instance.schedule(updates) # STR UPDATE @pytest.mark.parametrize( ""update, template"", [(""My Twitter Update"", None), (""My Twitter Update"", ""$message"")] ) def test__str_update(coo_preview_instance, update, template): coo_preview_instance._str_update(update, template) # _delay @pytest.mark.parametrize(""delay"", [(0), (""now"")]) def test_delay(coo_preview_instance, delay): coo_preview_instance._delay(delay) # _interval @pytest.mark.parametrize(""_interval"", [(0), (""now"")]) def test__interval(coo_preview_instance, _interval): coo_preview_instance._interval(_interval) ", https://github.com/wilfredinni/marlin,85f4ae0080ee9a25c1b8d2e481725ac9c42ca99f,tests/test_marlin.py::test_read_bookmark,tests/test_marlin.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from marlin.manage import ManageBookmark from pathlib import Path # new test path marlin_path = Path((Path.home()) / '.test_marlin') mock_object = ManageBookmark('mock', 'mock_path', marlin_path) def test_create_marlin_folder(): mock_object.create_marlin_folder() exists = Path(marlin_path).exists() # assert exists is True if not exists: raise AssertionError() def test_add_bookmark(): mock_object.add_bookmark() bookmark = Path((marlin_path) / 'mock') exists = Path(bookmark).exists() assert exists is True def test_read_bookmark(): mock_path = mock_object.read_bookmark('mock') assert mock_path == 'mock_path' def test_list_bookmark(): list_bookmark = mock_object.list_bookmark() assert isinstance(list_bookmark, list) def test_remove_bookmark(): mock_object.remove_bookmark() bookmark = Path((marlin_path) / 'mock') exists = Path(bookmark).exists() # assert exists is False if exists is not False: raise AssertionError() ", https://github.com/wilfredinni/marlin,85f4ae0080ee9a25c1b8d2e481725ac9c42ca99f,tests/test_marlin.py::test_remove_bookmark,tests/test_marlin.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from marlin.manage import ManageBookmark from pathlib import Path # new test path marlin_path = Path((Path.home()) / '.test_marlin') mock_object = ManageBookmark('mock', 'mock_path', marlin_path) def test_create_marlin_folder(): mock_object.create_marlin_folder() exists = Path(marlin_path).exists() # assert exists is True if not exists: raise AssertionError() def test_add_bookmark(): mock_object.add_bookmark() bookmark = Path((marlin_path) / 'mock') exists = Path(bookmark).exists() assert exists is True def test_read_bookmark(): mock_path = mock_object.read_bookmark('mock') assert mock_path == 'mock_path' def test_list_bookmark(): list_bookmark = mock_object.list_bookmark() assert isinstance(list_bookmark, list) def test_remove_bookmark(): mock_object.remove_bookmark() bookmark = Path((marlin_path) / 'mock') exists = Path(bookmark).exists() # assert exists is False if exists is not False: raise AssertionError() ", https://github.com/willforde/urlquick,db765372b73800cfa056047a3f3331820abd4ae8,tests/test_urlquick.py::TestCacheHandler::test_cache_check_no_cache,tests/test_urlquick.py,OD-Vic,flaky,nan,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- import unittest import urlquick import types import socket import base64 import json import sys import os import io import time import zlib import logging import ssl from random import random from functools import wraps from collections import OrderedDict, defaultdict if urlquick.py3: unicode = str from gzip import compress as gzip_compress else: from gzip import GzipFile def gzip_compress(data, compresslevel=4): """"""Compress data in one shot and return the compressed string. Optional argument is the compression level, in range of 0-9. """""" buf = io.BytesIO() with GzipFile(fileobj=buf, mode='wb', compresslevel=compresslevel) as f: f.write(data) return buf.getvalue() logger = urlquick.logger logger.setLevel(logging.DEBUG) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.DEBUG) logger.addHandler(ch) class TestMisc(unittest.TestCase): def test_cache_property(self): class Cache(object): @urlquick.CachedProperty def worker(self): return random() work = Cache() # Test that working return a random float ret = work.worker self.assertTrue(0 < ret < 1) # Test the a seccond request returns the same value self.assertEqual(work.worker, ret) # Test that a new value is return when cache is deleted del work.worker self.assertNotEqual(work.worker, ret) # Test that the setter failes as setter was not enabled with self.assertRaises(AttributeError): work.worker = 321 def test_cache_property_setter(self): class Cache(object): @urlquick.CachedProperty def worker(self): return random() # Enable setter worker.allow_setter = True work = Cache() # Test the setter works and does not raise AttributeError work.worker = 321 # Now test that the value that is returned is what was set and not a random one. self.assertEqual(work.worker, 321) # Test that a new value is return when cache is deleted del work.worker self.assertNotEqual(work.worker, 321) def test_UnicodeDict(self): test_dict1 = {u""test1"": u""work1"", ""test2"": ""work2"", ""num"": 3} test_dict2 = {""test3"": u""work3"", u""test4"": ""work4""} test_dict3 = {""test5"": None} test_dict = urlquick.UnicodeDict(test_dict1, test_dict2, test_dict3) # Check that all items are unicode for key, value in test_dict.items(): self.assertIsInstance(key, unicode) self.assertIsInstance(value, unicode) def test_httperror(self): test = urlquick.HTTPError(""https://httpbin.org/get"", 404, ""Not Found"", {""testing"": ""yes""}) self.assertEqual(str(test), ""HTTP Client Error 404: Not Found"") class TestPy2Functions(unittest.TestCase): def test_quote(self): text = urlquick.quote(u""testing&testing"") self.assertEqual(text, u""testing%26testing"") def test_unquote(self): text = urlquick.unquote(u""testing%26testing"") self.assertEqual(text, u""testing&testing"") def test_parse_qsl(self): ret = urlquick.parse_qsl(u""test=yes&work=true"") self.assertEqual(ret, [(u""test"", u""yes""), (u""work"", u""true"")]) def test_urlencode_list(self): test_list = [(u""test"", u""yes""), (u""work"", u""true"")] ret = urlquick.urlencode(test_list) self.assertEqual(ret, u""test=yes&work=true"") def test_urlencode_dict(self): test_dict = OrderedDict([(u""test"", u""yes""), (u""work"", u""true"")]) ret = urlquick.urlencode(test_dict) self.assertEqual(ret, u""test=yes&work=true"") def test_urlencode_list_with_list(self): test_list = [(u""test"", [u""test1"", u""test2""]), (u""work"", u""true"")] ret = urlquick.urlencode(test_list) self.assertEqual(ret, u""test=%5B%27test1%27%2C+%27test2%27%5D&work=true"") def test_urlencode_dict_with_list(self): test_dict = OrderedDict([(u""test"", [u""test1"", u""test2""]), (u""work"", u""true"")]) ret = urlquick.urlencode(test_dict) self.assertEqual(ret, u""test=%5B%27test1%27%2C+%27test2%27%5D&work=true"") def test_urlencode_list_with_list_doseq(self): test_list = [(u""test"", [u""test1"", u""test2""]), (u""work"", u""true"")] ret = urlquick.urlencode(test_list, doseq=True) self.assertEqual(ret, u""test=test1&test=test2&work=true"") def test_urlencode_dict_with_list_doseq(self): test_dict = OrderedDict([(u""test"", [u""test1"", u""test2""]), (u""work"", u""true"")]) ret = urlquick.urlencode(test_dict, doseq=True) self.assertEqual(ret, u""test=test1&test=test2&work=true"") class TestCaseInsensitiveDict(unittest.TestCase): def test_init(self): headers = urlquick.CaseInsensitiveDict() self.assertTrue(not headers) def test_init_args(self): headers = urlquick.CaseInsensitiveDict({u""key1"": u""value1""}, {""key2"": ""value2""}) self.assertTrue(u""key1"" in headers) self.assertTrue(u""key2"" in headers) self.assertEqual(headers[u""key1""], u""value1"") self.assertEqual(headers[u""key2""], u""value2"") def test_assign_unicode(self): headers = urlquick.CaseInsensitiveDict() headers[u""key""] = u""value"" self.assertTrue(u""key"" in headers) self.assertEqual(headers[u""key""], u""value"") def test_assign_bytes(self): headers = urlquick.CaseInsensitiveDict() headers[b""key""] = b""value"" self.assertTrue(u""key"" in headers) self.assertEqual(headers[u""key""], u""value"") def test_assign_none(self): headers = urlquick.CaseInsensitiveDict() headers[b""key""] = None self.assertFalse(u""key"" in headers) def test_len(self): headers = urlquick.CaseInsensitiveDict({u""key1"": u""value1""}) self.assertEqual(len(headers), 1) def test_copy(self): headers = urlquick.CaseInsensitiveDict({u""key1"": u""value1""}).copy() self.assertIsInstance(headers, urlquick.CaseInsensitiveDict) def test_iter(self): headers = urlquick.CaseInsensitiveDict({u""key1"": u""value1"", ""key2"": ""value2""}) ret = list(iter(headers)) self.assertIsInstance(ret, list) self.assertTrue(u""key1"" in ret) self.assertTrue(u""key2"" in ret) def test_repr(self): headers = urlquick.CaseInsensitiveDict({u""key1"": u""value1"", ""key2"": ""value2""}) test = repr(headers) self.assertTrue(""key1"" in test) self.assertTrue(""value1"" in test) self.assertTrue(""key2"" in test) self.assertTrue(""value2"" in test) class TestCacheHandler(unittest.TestCase): class create(object): def __init__(self, url, max_age=14400, body=u"""", headers=None, status=200, reason=u""OK""): response = {u""body"": body, u""headers"": headers, u""status"": status, u""reason"": reason, u""version"": 11, u""strict"": True} response[u""body""] = response[u""body""].encode(""utf8"") # Base64 encode the body to make it json serializable response[u""body""] = base64.b64encode(response[u""body""]).decode(""ascii"") if response[u""headers""]: response[u""headers""] = dict(response[u""headers""]) if isinstance(url, unicode): url = url.encode(""utf8"") hash_url = urlquick.CacheHandler.hash_url(url) path = urlquick.CacheHandler.cache_dir() cache_file = os.path.join(path, hash_url) # Save the response to disk using json Serialization with open(cache_file, ""w"") as stream: json.dump(response, stream, indent=4, separators=("","", "":"")) print(cache_file) self.cache = urlquick.CacheHandler(hash_url, max_age) def __enter__(self): return self.cache def __exit__(self, exc_type, exc_val, exc_tb): self.cache.delete(self.cache.cache_file) def test_cache_dir(self): def exists(*args): return False def makedirs(*args): pass _exists = os.path.exists _makedirs = os.makedirs os.path.exists = exists os.makedirs = makedirs try: path = urlquick.CacheHandler.cache_dir() self.assertTrue(bool(path)) finally: os.path.exists = _exists os.makedirs = _makedirs def test_isfilefresh(self): fresh = urlquick.CacheHandler.isfilefresh(__file__, 0) self.assertFalse(fresh) fresh = urlquick.CacheHandler.isfilefresh(__file__, 999999999) self.assertTrue(fresh) def test_init(self): with self.create(""https://httpbin.org/get""): pass def test_always_fresh_status(self): with self.create(""https://httpbin.org/get"", status=301) as cache: self.assertTrue(cache.isfresh()) def test_always_fresh_maxage(self): with self.create(""https://httpbin.org/get"", -1) as cache: self.assertTrue(cache.isfresh()) def test_isfilefresh_yes(self): with self.create(""https://httpbin.org/get"", 999999999) as cache: self.assertTrue(cache.isfresh()) def test_isfilefresh_no(self): with self.create(""https://httpbin.org/get"", 0) as cache: self.assertFalse(cache.isfresh()) def test_reset_timestamp(self): with self.create(""https://httpbin.org/get"") as cache: before_reset = os.stat(cache.cache_file).st_mtime time.sleep(.1) cache.reset_timestamp() after_reset = os.stat(cache.cache_file).st_mtime self.assertNotEqual(before_reset, after_reset) def test_conditional_headers_etag(self): headers = urlquick.CaseInsensitiveDict({""Etag"": ""lksjdfoiwjlksh""}) with ", https://github.com/willvousden/ptemcee,a411be2e29e39585212c92acea1893f955aa9be8,ptemcee/tests.py::Tests::test_inf_logprob,ptemcee/tests.py,OD-Vic,flaky,RepoArchived,nan,classify;root_cause,"#!/usr/bin/env python # encoding: utf-8 """""" Defines various pytest unit tests. """""" from __future__ import absolute_import, print_function, division import itertools import numpy as np from numpy.random.mtrand import RandomState from .sampler import Sampler, make_ladder from .interruptible_pool import Pool logprecision = -4 def logprob_gaussian(x, icov): return -np.dot(x, np.dot(icov, x)) / 2 def logprob_gaussian_nan(x, icov): # If any of walker's parameters are zeros, return NaN. if not (np.array(x)).any(): return np.nan else: return logprob_gaussian(x, icov) def logprob_gaussian_inf(x, icov): # If any of walker's parameters are negative, return -inf. if (np.array(x) < 0).any(): return -np.inf else: return logprob_gaussian(x, icov) def log_unit_sphere_volume(ndim): if ndim % 2 == 0: logfactorial = 0 for i in range(1, ndim // 2 + 1): logfactorial += np.log(i) return ndim / 2 * np.log(np.pi) - logfactorial else: logfactorial = 0 for i in range(1, ndim + 1, 2): logfactorial += np.log(i) return (ndim + 1) / 2 * np.log(2) \ + (ndim - 1) / 2 * np.log(np.pi) - logfactorial class LogLikeGaussian(object): def __init__(self, icov, test_nan=False, test_inf=False): """""" Initialize a gaussian PDF with the given inverse covariance matrix. If not ``None``, ``cutoff`` truncates the PDF at the given number of sigma from the origin (i.e., the PDF is non-zero only on an ellipse aligned with the principal axes of the distribution). Without this cutoff, thermodynamic integration with a flat prior is logarithmically divergent. """""" self.icov = icov self.test_nan = test_nan self.test_inf = test_inf def __call__(self, x): if self.test_nan: f = logprob_gaussian_nan elif self.test_inf: f = logprob_gaussian_inf else: f = logprob_gaussian return f(x, self.icov) class LogPriorGaussian(object): def __init__(self, icov, cutoff=None): self.icov = icov self.cutoff = cutoff def __call__(self, x): contour = logprob_gaussian(x, self.icov) if self.cutoff is not None: if -contour > self.cutoff * self.cutoff / 2: return -np.inf else: return 0 else: return 0 class Tests(object): sampler = None # type: Sampler @classmethod def setup_class(cls): np.seterr(all='raise') cls.nwalkers = 100 # type: int cls.ndim = 5 # type: int cls.ntemps = 10 # type: int cls.Tmax = 250 # type: float cls.cutoff = 10 # type: float cls.N = 1000 # type: int cls.mean = np.zeros(cls.ndim) # type: np.ndarray sqrtcov = 0.5 - np.random.rand(cls.ndim, cls.ndim) sqrtcov = np.triu(sqrtcov) sqrtcov += sqrtcov.T - np.diag(sqrtcov.diagonal()) cls.cov = np.dot(sqrtcov, sqrtcov) # type: np.ndarray cls.icov = np.linalg.inv(cls.cov) # type: np.ndarray cls.icov_unit = np.eye(cls.ndim) # type: np.ndarray # Draw samples from unit ball. nsamples = cls.ntemps * cls.nwalkers x = np.random.randn(nsamples, cls.ndim) x /= np.linalg.norm(x, axis=-1).reshape((nsamples, 1)) x *= np.random.rand(nsamples).reshape((nsamples, 1)) ** (1 / cls.ndim) # Now transform them to cover the prior volume. cls.p0_unit = x * cls.cutoff # type: np.ndarray cls.p0 = np.dot(x, sqrtcov) # type: np.ndarray cls.p0_unit = cls.p0_unit.reshape(cls.ntemps, cls.nwalkers, cls.ndim) # type: np.ndarray cls.p0 = cls.p0.reshape(cls.ntemps, cls.nwalkers, cls.ndim) # type: np.ndarray def check_sampler(self, sampler, p0, weak=False, fail=False): """""" Check that the sampler is behaving itself. Parameters ---------- sampler : Sampler The sampler to check. p0 : float, optional The initial positions at which to start the sampler's walkers. weak : bool, optional If ``True``, just check that the sampler ran without errors; don't check any of the results. fail : :class:`Exception`, optional If specified, assert that the sampler fails with the given exception type. """""" if fail: # Require the sampler to fail before it even starts. try: chain = sampler.chain(p0) for x in chain.iterate(self.N): assert False, \ 'Sampler should have failed by now.' except Exception as e: # If a type was specified, require that the sampler fail with this exception type. if type(e) is fail: return else: raise else: chain = sampler.chain(p0) for ensemble in chain.iterate(self.N): ratios = ensemble.swaps_accepted / ensemble.swaps_proposed assert np.all(ensemble.logP > -np.inf) and np.all(ensemble.logP > -np.inf), \ 'Invalid posterior/likelihood values; outside posterior support.' assert np.all(ratios >= 0) and np.all(ratios <= 1), \ 'Invalid swap ratios.' assert ensemble.logP.shape == ensemble.logP.shape == ensemble.x.shape[:-1], \ 'Sampler output shapes invalid.' assert ensemble.x.shape[-1] == self.ndim, \ 'Sampler output shapes invalid.' assert ratios.shape[0] == ensemble.logP.shape[0] - 1 and len(ratios.shape) == 1, \ 'Sampler output shapes invalid.' assert np.all(ensemble.betas >= 0), \ 'Negative temperatures!' assert np.all(np.diff(ensemble.betas) != 0), \ 'Temperatures have coalesced.' assert np.all(np.diff(ensemble.betas) < 0), \ 'Temperatures incorrectly ordered.' assert np.all(chain.get_acts() > 0), \ 'Invalid autocorrelation lengths.' if not weak: # Weaker assertions on acceptance fraction assert np.mean(chain.jump_acceptance_ratio) > 0.1, \ 'Acceptance fraction < 0.1' assert np.mean(chain.swap_acceptance_ratio) > 0.1, \ 'Temperature swap acceptance fraction < 0.1.' # TODO: Why doesn't this work? # if sampler.adaptive: # assert abs(chain.swap_acceptance_ratio[0] - 0.25) < 0.05, \ # 'Swap acceptance ratio != 0.25' data = np.reshape(chain.x[0, ...], (-1, chain.x.shape[-1])) log_volume = self.ndim * np.log(self.cutoff) \ + log_unit_sphere_volume(self.ndim) \ + 0.5 * np.log(np.linalg.det(self.cov)) gaussian_integral = self.ndim / 2 * np.log(2 * np.pi) \ + 0.5 * np.log(np.linalg.det(self.cov)) logZ, dlogZ = chain.log_evidence_estimate() assert np.abs(logZ - (gaussian_integral - log_volume)) < 3 * dlogZ, \ 'Evidence incorrect: {:g}+/{:g} versus correct {:g}.' \ .format(logZ, gaussian_integral - log_volume, dlogZ) maxdiff = 10 ** logprecision assert np.all((np.mean(data, axis=0) - self.mean) ** 2 / self.N ** 2 < maxdiff), 'Mean incorrect.' assert np.all((np.cov(data, rowvar=False) - self.cov) ** 2 / self.N ** 2 < maxdiff), 'Covariance incorrect.' def test_prior_support(self): sampler = Sampler(self.nwalkers, self.ndim, LogLikeGaussian(self.icov_unit), LogPriorGaussian(self.icov_unit, cutoff=self.cutoff), betas=make_ladder(self.ndim, self.ntemps, Tmax=self.Tmax)) # What happens when we start the sampler outside our prior support? self.p0_unit[0][0][0] = 1e6 * self.cutoff self.check_sampler(sampler, p0=self.p0_unit, fail=ValueError) def test_likelihood_support(self): sampler = Sampler(self.nwalkers, self.ndim, LogLikeGaussian(self.icov_unit, test_inf=True), LogPriorGaussian(self.icov_unit, cutoff=self.cutoff), betas=make_ladder(self.ndim, self.ntemps, Tmax=self.Tmax)) # What happens when we start the sampler outside our likelihood # support? Give some walkers a negative parameter value, where the # likelihood is unsupported. self.p0_unit[0][0][0] = -1 self.check_sampler(sampler, p0=self.p0_unit, fail=ValueError) def test_nan_logprob(self): sampler = Sampler(self.nwalkers, self.ndim, LogLikeGaussian(self.icov_unit, test_nan=True), LogPriorGaussian(self.icov_unit, cutoff=self.cutoff), betas=make_ladder(self.ndim, self.ntemps, Tmax=self.Tmax)) # If a walker is right at zero, ``logprobfn`` returns ``np.nan``; # sampler should fail with a ``ValueError``. self.p0_unit[-1][0][:] = 0 self.check_sampler(sampler, p0=self.p0_unit, fail=ValueError) def test_inf_logprob(self): """""" If a walker has any parameter negative, ``logprobfn`` returns ``-np.inf``. Start the ensembles in the all-positive part of the parameter space, then run for long enough for sampler to migrate into negative parts. (We can't start outside the posterior support, or the sampler will fail). The sampler should be happy with this; otherwise, a Flo", https://github.com/Windsooon/cherry,bc6ae1f0284d837fb95279098538530312313534,tests/test_classify.py::ClassifyTest::test_init,tests/test_classify.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import unittest import cherry from unittest import mock from cherry import classify from sklearn.exceptions import NotFittedError class ClassifyTest(unittest.TestCase): def setUp(self): pass # __init__() @mock.patch('cherry.classifyer.Classify._classify') @mock.patch('cherry.classifyer.Classify._load_cache') def test_init(self, mock_load, mock_classify): mock_load.return_value = ('foo', 'bar') cherry.classifyer.Classify(model='random', text=['random text']) mock_load.assert_called_once_with('random') mock_classify.assert_called_once_with(['random text']) # _load_cache() @mock.patch('cherry.classifyer.Classify._classify') @mock.patch('cherry.classifyer.load_cache') def test_load_cache(self, mock_load, mock_classify): res = cherry.classifyer.Classify(model='foo', text=['random text']) mock_load.assert_not_called() ", https://github.com/Windsooon/cherry,bc6ae1f0284d837fb95279098538530312313534,tests/test_classify.py::ClassifyTest::test_load_cache,tests/test_classify.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"import os import unittest import cherry from unittest import mock from cherry import classify from sklearn.exceptions import NotFittedError class ClassifyTest(unittest.TestCase): def setUp(self): pass # __init__() @mock.patch('cherry.classifyer.Classify._classify') @mock.patch('cherry.classifyer.Classify._load_cache') def test_init(self, mock_load, mock_classify): mock_load.return_value = ('foo', 'bar') cherry.classifyer.Classify(model='random', text=['random text']) mock_load.assert_called_once_with('random') mock_classify.assert_called_once_with(['random text']) # _load_cache() @mock.patch('cherry.classifyer.Classify._classify') @mock.patch('cherry.classifyer.load_cache') def test_load_cache(self, mock_load, mock_classify): res = cherry.classifyer.Classify(model='foo', text=['random text']) mock_load.assert_not_called() ", https://github.com/Windsooon/cherry,bc6ae1f0284d837fb95279098538530312313534,tests/test_classify.py::test_init,tests/test_classify.py,NIO,flaky,Opened,https://github.com/Windsooon/cherry/pull/19,classify;root_cause,"import os import unittest import cherry from unittest import mock from cherry import classify from sklearn.exceptions import NotFittedError class ClassifyTest(unittest.TestCase): def setUp(self): pass # __init__() @mock.patch('cherry.classifyer.Classify._classify') @mock.patch('cherry.classifyer.Classify._load_cache') def test_init(self, mock_load, mock_classify): mock_load.return_value = ('foo', 'bar') cherry.classifyer.Classify(model='random', text=['random text']) mock_load.assert_called_once_with('random') mock_classify.assert_called_once_with(['random text']) # _load_cache() @mock.patch('cherry.classifyer.Classify._classify') @mock.patch('cherry.classifyer.load_cache') def test_load_cache(self, mock_load, mock_classify): res = cherry.classifyer.Classify(model='foo', text=['random text']) mock_load.assert_not_called() ", https://github.com/workenvoy/firestore,4933f1a9f307539a8cef4ea80869c8821a92bc8d,tests/containers/collection_test.py::DocumentTest::test_collection_name,tests/containers/collection_test.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from unittest import TestCase from pytest import mark from firestore import Collection as Document, String from firestore.errors import InvalidDocumentError, ValidationError, UnknownFieldError from firestore.containers.collection import Cache MOUTHFUL = ""supercalifragiexpialiantidocious"" YDS = ""yabba/dooda/speca"" class DuplicateDoc(Document): field = String(pk=True) field2 = String(pk=True) class ConstructorDocument(Document): __collection__ = ""yabba.dooda.speca"" name = String(required=True, unique=True, pk=True) class TheDocument(Document): yimu = String(required=True) class RequiredDocument(Document): email = String(required=True) first_name = String(required=True) last_name = String(required=True) class DocumentTest(TestCase): def setUp(self): self.td = TheDocument() self.dd = DuplicateDoc() self.rd = RequiredDocument() self.cd = ConstructorDocument(name=""Yessiree"") def tearDown(self): pass def test_collection_name(self): self.assertEqual(self.cd.collection, YDS) def test_set_collection_name(self): self.cd.collection = ""booting"" # here we are testing the instance proxied the value to the # class correctly thus we access the class variable directly # to check self.assertEqual(type(self.cd).__collection__, ""booting"") def test_cache(self): cache = Cache() cache.name = MOUTHFUL self.assertEqual(cache.name, MOUTHFUL) self.assertLessEqual({""_pk"": False}.items(), cache.items()) def test_cache_persists_correctly(self): self.td.yimu = ""true"" self.assertEqual(self.td._data.yimu, self.td.yimu) def test_recursive_cache_access(self): pass def test_document_has_instance(self): self.td.yimu = ""true"" expected = { ""_pk"": False, ""yimu"": ""true"" } self.assertEqual(self.td._data, expected) def test_duplicate_pk(self): with self.assertRaises(InvalidDocumentError): self.dd.field = ""You"" self.dd.field2 = ""You"" def test_dbpath(self): _ = ""gherkin"" self.cd.name = _ self.assertEqual(self.cd.dbpath, ""{}/{}"".format(YDS, _)) def test_document_constructor_error_for_unknown_key(self): with self.assertRaises(UnknownFieldError): _ = RequiredDocument(shoe_size=45) def test_document_constructor_initialization(self): self.assertEqual(self.cd.name, ""Yessiree"") def test_document_save(self): with self.assertRaises(ValidationError): self.rd = RequiredDocument() self.rd.save() ", https://github.com/workenvoy/firestore,4933f1a9f307539a8cef4ea80869c8821a92bc8d,tests/containers/collection_test.py::DocumentTest::test_dbpath,tests/containers/collection_test.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from unittest import TestCase from pytest import mark from firestore import Collection as Document, String from firestore.errors import InvalidDocumentError, ValidationError, UnknownFieldError from firestore.containers.collection import Cache MOUTHFUL = ""supercalifragiexpialiantidocious"" YDS = ""yabba/dooda/speca"" class DuplicateDoc(Document): field = String(pk=True) field2 = String(pk=True) class ConstructorDocument(Document): __collection__ = ""yabba.dooda.speca"" name = String(required=True, unique=True, pk=True) class TheDocument(Document): yimu = String(required=True) class RequiredDocument(Document): email = String(required=True) first_name = String(required=True) last_name = String(required=True) class DocumentTest(TestCase): def setUp(self): self.td = TheDocument() self.dd = DuplicateDoc() self.rd = RequiredDocument() self.cd = ConstructorDocument(name=""Yessiree"") def tearDown(self): pass def test_collection_name(self): self.assertEqual(self.cd.collection, YDS) def test_set_collection_name(self): self.cd.collection = ""booting"" # here we are testing the instance proxied the value to the # class correctly thus we access the class variable directly # to check self.assertEqual(type(self.cd).__collection__, ""booting"") def test_cache(self): cache = Cache() cache.name = MOUTHFUL self.assertEqual(cache.name, MOUTHFUL) self.assertLessEqual({""_pk"": False}.items(), cache.items()) def test_cache_persists_correctly(self): self.td.yimu = ""true"" self.assertEqual(self.td._data.yimu, self.td.yimu) def test_recursive_cache_access(self): pass def test_document_has_instance(self): self.td.yimu = ""true"" expected = { ""_pk"": False, ""yimu"": ""true"" } self.assertEqual(self.td._data, expected) def test_duplicate_pk(self): with self.assertRaises(InvalidDocumentError): self.dd.field = ""You"" self.dd.field2 = ""You"" def test_dbpath(self): _ = ""gherkin"" self.cd.name = _ self.assertEqual(self.cd.dbpath, ""{}/{}"".format(YDS, _)) def test_document_constructor_error_for_unknown_key(self): with self.assertRaises(UnknownFieldError): _ = RequiredDocument(shoe_size=45) def test_document_constructor_initialization(self): self.assertEqual(self.cd.name, ""Yessiree"") def test_document_save(self): with self.assertRaises(ValidationError): self.rd = RequiredDocument() self.rd.save() ", https://github.com/wsilva32/poly_decomp.py,cb1a47f218e076772b9df137c1928023882df5e5,tests/test_poly_decomp.py::TestPoly_Decomp::test_not_polygonCanSee,tests/test_poly_decomp.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python from poly_decomp import poly_decomp line1 = [[0, 0],[5, 5]] line2 = [[5, 0],[0, 5]] line3 = [[-1, -1],[-5, -5]] poly = [[0, 0], [5, 0], [5, 5], [2.5, 2.5], [0, 5]] class TestPoly_Decomp: def test_line_intersection(self): assert poly_decomp.lineInt(line1, line2) == [2.5, 2.5] def test_no_line_intersection(self): assert poly_decomp.lineInt(line1, line3) == [0, 0] def test_line_segments_intersect(self): assert poly_decomp.lineSegmentsIntersect(line1[0], line1[1], line2[0], line2[1]) == True def test_no_line_segments_intersect(self): assert poly_decomp.lineSegmentsIntersect(line1[0], line1[1], line3[0], line3[1]) == False def test_collinear(self): assert poly_decomp.collinear([0, 0], [5, 0], [10, 0]) == True def test_not_collinear(self): assert poly_decomp.collinear([0, 0], [5, 1], [10, 0]) == False def test_polygonAt(self): index = 2 assert poly_decomp.polygonAt(poly, index) == [5, 5] def test_polygonAt_negative_index(self): index = -2 assert poly_decomp.polygonAt(poly, index) == [2.5, 2.5] def test_polygonClear(self): poly_decomp.polygonClear(poly) assert len(poly) == 0 def test_polygonAppend(self): source = [[0, 5], [2.5, 2.5], [5, 5], [5, 0], [0, 0]] poly_decomp.polygonAppend(poly, source, 0, 5) assert poly == source def test_polygonMakeCCW(self): poly_decomp.polygonMakeCCW(poly) assert poly == [[0, 0], [5, 0], [5, 5], [2.5, 2.5], [0, 5]] def test_polygonIsReflex(self): assert poly_decomp.polygonIsReflex(poly, 3) == True def test_not_polygonIsReflex(self): assert poly_decomp.polygonIsReflex(poly, 4) == False def test_polygonCanSee(self): assert poly_decomp.polygonCanSee(poly, 0, 4) == True def test_not_polygonCanSee(self): assert poly_decomp.polygonCanSee(poly, 3, 4) == False def test_polygonCopy(self): assert poly_decomp.polygonCopy(poly, 2, 3) == [[5, 5], [2.5, 2.5]] def test_polygonCutEdges(self): assert poly_decomp.polygonGetCutEdges(poly) == [[[2.5, 2.5], [0, 0]]] def test_polygonDecomp(self): assert poly_decomp.polygonDecomp(poly) == [[[0, 0], [2.5, 2.5], [0, 5]], [[0, 0], [5, 0], [5, 5], [2.5, 2.5]]] def test_not_polygonIsSimple(self): nonsimple_poly = [[0, 0], [5, 0], [0, 5], [5, 5]] assert poly_decomp.polygonIsSimple(nonsimple_poly) == False def test_polygonIsSimple(self): assert poly_decomp.polygonIsSimple(poly) == True def test_getIntersectionPoint(self): assert poly_decomp.getIntersectionPoint(line1[0], line1[1], line2[0], line2[1]) == [2.5, 2.5] def test_polygonQuickDecomp(self): assert poly_decomp.polygonQuickDecomp(poly) == [[[5, 0], [5, 5], [2.5, 2.5]], [[2.5, 2.5], [0, 5], [0, 0], [5, 0]]] def test_polygonRemoveCollinearPoints(self): collinear_poly = [[0, 0], [5, 0], [10, 0], [5, 5]] poly_decomp.polygonRemoveCollinearPoints(collinear_poly) assert collinear_poly == [[0, 0], [10, 0], [5, 5]] ", https://github.com/wsilva32/poly_decomp.py,cb1a47f218e076772b9df137c1928023882df5e5,tests/test_poly_decomp.py::TestPoly_Decomp::test_not_polygonIsReflex,tests/test_poly_decomp.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python from poly_decomp import poly_decomp line1 = [[0, 0],[5, 5]] line2 = [[5, 0],[0, 5]] line3 = [[-1, -1],[-5, -5]] poly = [[0, 0], [5, 0], [5, 5], [2.5, 2.5], [0, 5]] class TestPoly_Decomp: def test_line_intersection(self): assert poly_decomp.lineInt(line1, line2) == [2.5, 2.5] def test_no_line_intersection(self): assert poly_decomp.lineInt(line1, line3) == [0, 0] def test_line_segments_intersect(self): assert poly_decomp.lineSegmentsIntersect(line1[0], line1[1], line2[0], line2[1]) == True def test_no_line_segments_intersect(self): assert poly_decomp.lineSegmentsIntersect(line1[0], line1[1], line3[0], line3[1]) == False def test_collinear(self): assert poly_decomp.collinear([0, 0], [5, 0], [10, 0]) == True def test_not_collinear(self): assert poly_decomp.collinear([0, 0], [5, 1], [10, 0]) == False def test_polygonAt(self): index = 2 assert poly_decomp.polygonAt(poly, index) == [5, 5] def test_polygonAt_negative_index(self): index = -2 assert poly_decomp.polygonAt(poly, index) == [2.5, 2.5] def test_polygonClear(self): poly_decomp.polygonClear(poly) assert len(poly) == 0 def test_polygonAppend(self): source = [[0, 5], [2.5, 2.5], [5, 5], [5, 0], [0, 0]] poly_decomp.polygonAppend(poly, source, 0, 5) assert poly == source def test_polygonMakeCCW(self): poly_decomp.polygonMakeCCW(poly) assert poly == [[0, 0], [5, 0], [5, 5], [2.5, 2.5], [0, 5]] def test_polygonIsReflex(self): assert poly_decomp.polygonIsReflex(poly, 3) == True def test_not_polygonIsReflex(self): assert poly_decomp.polygonIsReflex(poly, 4) == False def test_polygonCanSee(self): assert poly_decomp.polygonCanSee(poly, 0, 4) == True def test_not_polygonCanSee(self): assert poly_decomp.polygonCanSee(poly, 3, 4) == False def test_polygonCopy(self): assert poly_decomp.polygonCopy(poly, 2, 3) == [[5, 5], [2.5, 2.5]] def test_polygonCutEdges(self): assert poly_decomp.polygonGetCutEdges(poly) == [[[2.5, 2.5], [0, 0]]] def test_polygonDecomp(self): assert poly_decomp.polygonDecomp(poly) == [[[0, 0], [2.5, 2.5], [0, 5]], [[0, 0], [5, 0], [5, 5], [2.5, 2.5]]] def test_not_polygonIsSimple(self): nonsimple_poly = [[0, 0], [5, 0], [0, 5], [5, 5]] assert poly_decomp.polygonIsSimple(nonsimple_poly) == False def test_polygonIsSimple(self): assert poly_decomp.polygonIsSimple(poly) == True def test_getIntersectionPoint(self): assert poly_decomp.getIntersectionPoint(line1[0], line1[1], line2[0], line2[1]) == [2.5, 2.5] def test_polygonQuickDecomp(self): assert poly_decomp.polygonQuickDecomp(poly) == [[[5, 0], [5, 5], [2.5, 2.5]], [[2.5, 2.5], [0, 5], [0, 0], [5, 0]]] def test_polygonRemoveCollinearPoints(self): collinear_poly = [[0, 0], [5, 0], [10, 0], [5, 5]] poly_decomp.polygonRemoveCollinearPoints(collinear_poly) assert collinear_poly == [[0, 0], [10, 0], [5, 5]] ", https://github.com/wsilva32/poly_decomp.py,cb1a47f218e076772b9df137c1928023882df5e5,tests/test_poly_decomp.py::TestPoly_Decomp::test_polygonAt,tests/test_poly_decomp.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python from poly_decomp import poly_decomp line1 = [[0, 0],[5, 5]] line2 = [[5, 0],[0, 5]] line3 = [[-1, -1],[-5, -5]] poly = [[0, 0], [5, 0], [5, 5], [2.5, 2.5], [0, 5]] class TestPoly_Decomp: def test_line_intersection(self): assert poly_decomp.lineInt(line1, line2) == [2.5, 2.5] def test_no_line_intersection(self): assert poly_decomp.lineInt(line1, line3) == [0, 0] def test_line_segments_intersect(self): assert poly_decomp.lineSegmentsIntersect(line1[0], line1[1], line2[0], line2[1]) == True def test_no_line_segments_intersect(self): assert poly_decomp.lineSegmentsIntersect(line1[0], line1[1], line3[0], line3[1]) == False def test_collinear(self): assert poly_decomp.collinear([0, 0], [5, 0], [10, 0]) == True def test_not_collinear(self): assert poly_decomp.collinear([0, 0], [5, 1], [10, 0]) == False def test_polygonAt(self): index = 2 assert poly_decomp.polygonAt(poly, index) == [5, 5] def test_polygonAt_negative_index(self): index = -2 assert poly_decomp.polygonAt(poly, index) == [2.5, 2.5] def test_polygonClear(self): poly_decomp.polygonClear(poly) assert len(poly) == 0 def test_polygonAppend(self): source = [[0, 5], [2.5, 2.5], [5, 5], [5, 0], [0, 0]] poly_decomp.polygonAppend(poly, source, 0, 5) assert poly == source def test_polygonMakeCCW(self): poly_decomp.polygonMakeCCW(poly) assert poly == [[0, 0], [5, 0], [5, 5], [2.5, 2.5], [0, 5]] def test_polygonIsReflex(self): assert poly_decomp.polygonIsReflex(poly, 3) == True def test_not_polygonIsReflex(self): assert poly_decomp.polygonIsReflex(poly, 4) == False def test_polygonCanSee(self): assert poly_decomp.polygonCanSee(poly, 0, 4) == True def test_not_polygonCanSee(self): assert poly_decomp.polygonCanSee(poly, 3, 4) == False def test_polygonCopy(self): assert poly_decomp.polygonCopy(poly, 2, 3) == [[5, 5], [2.5, 2.5]] def test_polygonCutEdges(self): assert poly_decomp.polygonGetCutEdges(poly) == [[[2.5, 2.5], [0, 0]]] def test_polygonDecomp(self): assert poly_decomp.polygonDecomp(poly) == [[[0, 0], [2.5, 2.5], [0, 5]], [[0, 0], [5, 0], [5, 5], [2.5, 2.5]]] def test_not_polygonIsSimple(self): nonsimple_poly = [[0, 0], [5, 0], [0, 5], [5, 5]] assert poly_decomp.polygonIsSimple(nonsimple_poly) == False def test_polygonIsSimple(self): assert poly_decomp.polygonIsSimple(poly) == True def test_getIntersectionPoint(self): assert poly_decomp.getIntersectionPoint(line1[0], line1[1], line2[0], line2[1]) == [2.5, 2.5] def test_polygonQuickDecomp(self): assert poly_decomp.polygonQuickDecomp(poly) == [[[5, 0], [5, 5], [2.5, 2.5]], [[2.5, 2.5], [0, 5], [0, 0], [5, 0]]] def test_polygonRemoveCollinearPoints(self): collinear_poly = [[0, 0], [5, 0], [10, 0], [5, 5]] poly_decomp.polygonRemoveCollinearPoints(collinear_poly) assert collinear_poly == [[0, 0], [10, 0], [5, 5]] ", https://github.com/wsilva32/poly_decomp.py,cb1a47f218e076772b9df137c1928023882df5e5,tests/test_poly_decomp.py::TestPoly_Decomp::test_polygonAt_negative_index,tests/test_poly_decomp.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python from poly_decomp import poly_decomp line1 = [[0, 0],[5, 5]] line2 = [[5, 0],[0, 5]] line3 = [[-1, -1],[-5, -5]] poly = [[0, 0], [5, 0], [5, 5], [2.5, 2.5], [0, 5]] class TestPoly_Decomp: def test_line_intersection(self): assert poly_decomp.lineInt(line1, line2) == [2.5, 2.5] def test_no_line_intersection(self): assert poly_decomp.lineInt(line1, line3) == [0, 0] def test_line_segments_intersect(self): assert poly_decomp.lineSegmentsIntersect(line1[0], line1[1], line2[0], line2[1]) == True def test_no_line_segments_intersect(self): assert poly_decomp.lineSegmentsIntersect(line1[0], line1[1], line3[0], line3[1]) == False def test_collinear(self): assert poly_decomp.collinear([0, 0], [5, 0], [10, 0]) == True def test_not_collinear(self): assert poly_decomp.collinear([0, 0], [5, 1], [10, 0]) == False def test_polygonAt(self): index = 2 assert poly_decomp.polygonAt(poly, index) == [5, 5] def test_polygonAt_negative_index(self): index = -2 assert poly_decomp.polygonAt(poly, index) == [2.5, 2.5] def test_polygonClear(self): poly_decomp.polygonClear(poly) assert len(poly) == 0 def test_polygonAppend(self): source = [[0, 5], [2.5, 2.5], [5, 5], [5, 0], [0, 0]] poly_decomp.polygonAppend(poly, source, 0, 5) assert poly == source def test_polygonMakeCCW(self): poly_decomp.polygonMakeCCW(poly) assert poly == [[0, 0], [5, 0], [5, 5], [2.5, 2.5], [0, 5]] def test_polygonIsReflex(self): assert poly_decomp.polygonIsReflex(poly, 3) == True def test_not_polygonIsReflex(self): assert poly_decomp.polygonIsReflex(poly, 4) == False def test_polygonCanSee(self): assert poly_decomp.polygonCanSee(poly, 0, 4) == True def test_not_polygonCanSee(self): assert poly_decomp.polygonCanSee(poly, 3, 4) == False def test_polygonCopy(self): assert poly_decomp.polygonCopy(poly, 2, 3) == [[5, 5], [2.5, 2.5]] def test_polygonCutEdges(self): assert poly_decomp.polygonGetCutEdges(poly) == [[[2.5, 2.5], [0, 0]]] def test_polygonDecomp(self): assert poly_decomp.polygonDecomp(poly) == [[[0, 0], [2.5, 2.5], [0, 5]], [[0, 0], [5, 0], [5, 5], [2.5, 2.5]]] def test_not_polygonIsSimple(self): nonsimple_poly = [[0, 0], [5, 0], [0, 5], [5, 5]] assert poly_decomp.polygonIsSimple(nonsimple_poly) == False def test_polygonIsSimple(self): assert poly_decomp.polygonIsSimple(poly) == True def test_getIntersectionPoint(self): assert poly_decomp.getIntersectionPoint(line1[0], line1[1], line2[0], line2[1]) == [2.5, 2.5] def test_polygonQuickDecomp(self): assert poly_decomp.polygonQuickDecomp(poly) == [[[5, 0], [5, 5], [2.5, 2.5]], [[2.5, 2.5], [0, 5], [0, 0], [5, 0]]] def test_polygonRemoveCollinearPoints(self): collinear_poly = [[0, 0], [5, 0], [10, 0], [5, 5]] poly_decomp.polygonRemoveCollinearPoints(collinear_poly) assert collinear_poly == [[0, 0], [10, 0], [5, 5]] ", https://github.com/wsilva32/poly_decomp.py,cb1a47f218e076772b9df137c1928023882df5e5,tests/test_poly_decomp.py::TestPoly_Decomp::test_polygonIsReflex,tests/test_poly_decomp.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python from poly_decomp import poly_decomp line1 = [[0, 0],[5, 5]] line2 = [[5, 0],[0, 5]] line3 = [[-1, -1],[-5, -5]] poly = [[0, 0], [5, 0], [5, 5], [2.5, 2.5], [0, 5]] class TestPoly_Decomp: def test_line_intersection(self): assert poly_decomp.lineInt(line1, line2) == [2.5, 2.5] def test_no_line_intersection(self): assert poly_decomp.lineInt(line1, line3) == [0, 0] def test_line_segments_intersect(self): assert poly_decomp.lineSegmentsIntersect(line1[0], line1[1], line2[0], line2[1]) == True def test_no_line_segments_intersect(self): assert poly_decomp.lineSegmentsIntersect(line1[0], line1[1], line3[0], line3[1]) == False def test_collinear(self): assert poly_decomp.collinear([0, 0], [5, 0], [10, 0]) == True def test_not_collinear(self): assert poly_decomp.collinear([0, 0], [5, 1], [10, 0]) == False def test_polygonAt(self): index = 2 assert poly_decomp.polygonAt(poly, index) == [5, 5] def test_polygonAt_negative_index(self): index = -2 assert poly_decomp.polygonAt(poly, index) == [2.5, 2.5] def test_polygonClear(self): poly_decomp.polygonClear(poly) assert len(poly) == 0 def test_polygonAppend(self): source = [[0, 5], [2.5, 2.5], [5, 5], [5, 0], [0, 0]] poly_decomp.polygonAppend(poly, source, 0, 5) assert poly == source def test_polygonMakeCCW(self): poly_decomp.polygonMakeCCW(poly) assert poly == [[0, 0], [5, 0], [5, 5], [2.5, 2.5], [0, 5]] def test_polygonIsReflex(self): assert poly_decomp.polygonIsReflex(poly, 3) == True def test_not_polygonIsReflex(self): assert poly_decomp.polygonIsReflex(poly, 4) == False def test_polygonCanSee(self): assert poly_decomp.polygonCanSee(poly, 0, 4) == True def test_not_polygonCanSee(self): assert poly_decomp.polygonCanSee(poly, 3, 4) == False def test_polygonCopy(self): assert poly_decomp.polygonCopy(poly, 2, 3) == [[5, 5], [2.5, 2.5]] def test_polygonCutEdges(self): assert poly_decomp.polygonGetCutEdges(poly) == [[[2.5, 2.5], [0, 0]]] def test_polygonDecomp(self): assert poly_decomp.polygonDecomp(poly) == [[[0, 0], [2.5, 2.5], [0, 5]], [[0, 0], [5, 0], [5, 5], [2.5, 2.5]]] def test_not_polygonIsSimple(self): nonsimple_poly = [[0, 0], [5, 0], [0, 5], [5, 5]] assert poly_decomp.polygonIsSimple(nonsimple_poly) == False def test_polygonIsSimple(self): assert poly_decomp.polygonIsSimple(poly) == True def test_getIntersectionPoint(self): assert poly_decomp.getIntersectionPoint(line1[0], line1[1], line2[0], line2[1]) == [2.5, 2.5] def test_polygonQuickDecomp(self): assert poly_decomp.polygonQuickDecomp(poly) == [[[5, 0], [5, 5], [2.5, 2.5]], [[2.5, 2.5], [0, 5], [0, 0], [5, 0]]] def test_polygonRemoveCollinearPoints(self): collinear_poly = [[0, 0], [5, 0], [10, 0], [5, 5]] poly_decomp.polygonRemoveCollinearPoints(collinear_poly) assert collinear_poly == [[0, 0], [10, 0], [5, 5]] ", https://github.com/xlcnd/isbnlib,4dd24ceb7b686e78ee872d667014c6b3935b718c,isbnlib/test/test_cache.py::test_cache_del,isbnlib/test/test_cache.py,NIO,flaky,Accepted,https://github.com/xlcnd/isbnlib/pull/100,classify;root_cause;fix_proposal,"# -*- coding: utf-8 -*- # flake8: noqa # pylint: skip-file """"""Tests for the cache."""""" from nose.tools import assert_equals from .._imcache import IMCache cache = IMCache() def setup_module(): cache['123'] = 'abc' # <-- set def teardown_module(): del cache['123'] def test_cache_set(): """"""Test 'cache' operations (set)."""""" cache['567'] = 'jkl' assert_equals('jkl' == cache['567'], True) def test_cache_get(): """"""Test 'cache' operations (get)."""""" assert_equals(cache.get('123'), cache['123']) assert_equals(cache.get('000'), None) assert_equals(cache.get('000', ''), '') def test_cache_contains(): """"""Test 'cache' operations (contains)."""""" assert_equals('123' in cache, True) def test_cache_del(): """"""Test 'cache' operations (del)."""""" del cache['567'] assert_equals('567' not in cache, True) ", https://github.com/Yelp/kafka-utils,bde091515d1dcdd56b9272a3ab01b54742a60253,tests/kafka_rolling_restart/test_main.py::test_get_broker_list3,tests/kafka_rolling_restart/test_main.py,NIO,flaky,RepoArchived,nan,classify;root_cause,"# -*- coding: utf-8 -*- # Copyright 2016 Yelp Inc. # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import import mock import pytest import requests from requests.exceptions import RequestException from kafka_utils.kafka_rolling_restart import main from kafka_utils.util.config import ClusterConfig from kafka_utils.util.zookeeper import ZK @mock.patch.object(main.FuturesSession, 'get', autospec=True) def test_read_cluster_value_partitions(mock_get): response = mock.Mock(status_code=200, spec=requests.Response) response.json.return_value = {'value': 1} request = mock_get.return_value request.result.return_value = response p, b = main.read_cluster_status([""host1"", ""host2"", ""host3""], 80, ""jolokia"", None, None) assert p == 3 # 3 missing partitions assert b == 0 # 0 missing brokers @mock.patch.object(main.FuturesSession, 'get', autospec=True) def test_read_cluster_value_exit(mock_get): response = mock.Mock(status_code=404, spec=requests.Response) request = mock_get.return_value request.result.return_value = response with pytest.raises(SystemExit): p, b = main.read_cluster_status([""host1""], 80, ""jolokia"", None, None) @mock.patch.object(main.FuturesSession, 'get', autospec=True) def test_read_cluster_value_no_key(mock_get): response = mock.Mock(status_code=200, spec=requests.Response) response.json.return_value = {'wrong_key': 1} request = mock_get.return_value request.result.return_value = response p, b = main.read_cluster_status([""host1""], 80, ""jolokia"", None, None) assert p == 0 # 0 missing partitions assert b == 1 # 1 missing brokers @mock.patch.object(main.FuturesSession, 'get', autospec=True) def test_read_cluster_value_server_down(mock_get): request = mock_get.return_value request.result.side_effect = RequestException p, b = main.read_cluster_status([""host1""], 80, ""jolokia"", None, None) assert p == 0 # 0 missing partitions assert b == 1 # 1 missing brokers def read_cluster_state_values(first_part, repeat): for value in first_part: yield value while True: yield repeat @mock.patch.object( main, 'read_cluster_status', side_effect=read_cluster_state_values([(100, 1), (0, 1), (100, 0)], (0, 0)), autospec=True, ) @mock.patch('time.sleep', autospec=True) def test_wait_for_stable_cluster_success(mock_sleep, mock_read): main.wait_for_stable_cluster([], 1, """", None, None, 5, 3, 100) assert mock_read.call_count == 6 assert mock_sleep.mock_calls == [mock.call(5)] * 5 @mock.patch.object( main, 'read_cluster_status', side_effect=read_cluster_state_values([], (100, 0)), autospec=True, ) @mock.patch('time.sleep', autospec=True) def test_wait_for_stable_cluster_timeout(mock_sleep, mock_read): with pytest.raises(main.WaitTimeoutException): main.wait_for_stable_cluster([], 1, """", None, None, 5, 3, 100) assert mock_read.call_count == 21 assert mock_sleep.mock_calls == [mock.call(5)] * 20 cluster_config = ClusterConfig( type='mytype', name='some_cluster', broker_list='some_list', zookeeper='some_ip' ) @mock.patch( 'kafka_utils.util.zookeeper.KazooClient', autospec=True ) @mock.patch.object( ZK, 'get_brokers', side_effect=[{2: {'host': 'broker2'}, 3: {'host': 'broker3'}, 1: {'host': 'broker1'}}], autospec=True ) def test_get_broker_list1(mock_client, mock_get_broker): p = main.get_broker_list(cluster_config, active_controller_for_last=False) assert p == [(1, 'broker1'), (2, 'broker2'), (3, 'broker3')] @mock.patch( 'kafka_utils.util.zookeeper.KazooClient', autospec=True ) @mock.patch.object( ZK, 'get_brokers', side_effect=[{2: {'host': 'broker2'}, 3: {'host': 'broker3'}, 1: {'host': 'broker1'}}], autospec=True ) @mock.patch.object( ZK, 'get_json', side_effect=[{}], autospec=True ) def test_get_broker_list2(mock_client, mock_get_broker, mock_get_json): with pytest.raises(SystemExit) as pytest_wrapped_e: main.get_broker_list(cluster_config, active_controller_for_last=True) assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 1 @mock.patch( 'kafka_utils.util.zookeeper.KazooClient', autospec=True ) @mock.patch.object( ZK, 'get_brokers', side_effect=[{2: {'host': 'broker2'}, 3: {'host': 'broker3'}, 1: {'host': 'broker1'}}], autospec=True ) @mock.patch.object( ZK, 'get_json', side_effect=[{'brokerid': 2}], autospec=True ) def test_get_broker_list3(mock_client, mock_get_broker, mock_get_json): p = main.get_broker_list(cluster_config, active_controller_for_last=True) assert p == [(1, 'broker1'), (3, 'broker3'), (2, 'broker2')] ", https://github.com/Yelp/kafka-utils,bde091515d1dcdd56b9272a3ab01b54742a60253,tests/kafka_rolling_restart/test_main.py::test_wait_for_stable_cluster_success,tests/kafka_rolling_restart/test_main.py,NIO,flaky,RepoArchived,nan,classify;root_cause,"# -*- coding: utf-8 -*- # Copyright 2016 Yelp Inc. # # Licensed under the Apache License, Version 2.0 (the ""License""); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an ""AS IS"" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import import mock import pytest import requests from requests.exceptions import RequestException from kafka_utils.kafka_rolling_restart import main from kafka_utils.util.config import ClusterConfig from kafka_utils.util.zookeeper import ZK @mock.patch.object(main.FuturesSession, 'get', autospec=True) def test_read_cluster_value_partitions(mock_get): response = mock.Mock(status_code=200, spec=requests.Response) response.json.return_value = {'value': 1} request = mock_get.return_value request.result.return_value = response p, b = main.read_cluster_status([""host1"", ""host2"", ""host3""], 80, ""jolokia"", None, None) assert p == 3 # 3 missing partitions assert b == 0 # 0 missing brokers @mock.patch.object(main.FuturesSession, 'get', autospec=True) def test_read_cluster_value_exit(mock_get): response = mock.Mock(status_code=404, spec=requests.Response) request = mock_get.return_value request.result.return_value = response with pytest.raises(SystemExit): p, b = main.read_cluster_status([""host1""], 80, ""jolokia"", None, None) @mock.patch.object(main.FuturesSession, 'get', autospec=True) def test_read_cluster_value_no_key(mock_get): response = mock.Mock(status_code=200, spec=requests.Response) response.json.return_value = {'wrong_key': 1} request = mock_get.return_value request.result.return_value = response p, b = main.read_cluster_status([""host1""], 80, ""jolokia"", None, None) assert p == 0 # 0 missing partitions assert b == 1 # 1 missing brokers @mock.patch.object(main.FuturesSession, 'get', autospec=True) def test_read_cluster_value_server_down(mock_get): request = mock_get.return_value request.result.side_effect = RequestException p, b = main.read_cluster_status([""host1""], 80, ""jolokia"", None, None) assert p == 0 # 0 missing partitions assert b == 1 # 1 missing brokers def read_cluster_state_values(first_part, repeat): for value in first_part: yield value while True: yield repeat @mock.patch.object( main, 'read_cluster_status', side_effect=read_cluster_state_values([(100, 1), (0, 1), (100, 0)], (0, 0)), autospec=True, ) @mock.patch('time.sleep', autospec=True) def test_wait_for_stable_cluster_success(mock_sleep, mock_read): main.wait_for_stable_cluster([], 1, """", None, None, 5, 3, 100) assert mock_read.call_count == 6 assert mock_sleep.mock_calls == [mock.call(5)] * 5 @mock.patch.object( main, 'read_cluster_status', side_effect=read_cluster_state_values([], (100, 0)), autospec=True, ) @mock.patch('time.sleep', autospec=True) def test_wait_for_stable_cluster_timeout(mock_sleep, mock_read): with pytest.raises(main.WaitTimeoutException): main.wait_for_stable_cluster([], 1, """", None, None, 5, 3, 100) assert mock_read.call_count == 21 assert mock_sleep.mock_calls == [mock.call(5)] * 20 cluster_config = ClusterConfig( type='mytype', name='some_cluster', broker_list='some_list', zookeeper='some_ip' ) @mock.patch( 'kafka_utils.util.zookeeper.KazooClient', autospec=True ) @mock.patch.object( ZK, 'get_brokers', side_effect=[{2: {'host': 'broker2'}, 3: {'host': 'broker3'}, 1: {'host': 'broker1'}}], autospec=True ) def test_get_broker_list1(mock_client, mock_get_broker): p = main.get_broker_list(cluster_config, active_controller_for_last=False) assert p == [(1, 'broker1'), (2, 'broker2'), (3, 'broker3')] @mock.patch( 'kafka_utils.util.zookeeper.KazooClient', autospec=True ) @mock.patch.object( ZK, 'get_brokers', side_effect=[{2: {'host': 'broker2'}, 3: {'host': 'broker3'}, 1: {'host': 'broker1'}}], autospec=True ) @mock.patch.object( ZK, 'get_json', side_effect=[{}], autospec=True ) def test_get_broker_list2(mock_client, mock_get_broker, mock_get_json): with pytest.raises(SystemExit) as pytest_wrapped_e: main.get_broker_list(cluster_config, active_controller_for_last=True) assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code == 1 @mock.patch( 'kafka_utils.util.zookeeper.KazooClient', autospec=True ) @mock.patch.object( ZK, 'get_brokers', side_effect=[{2: {'host': 'broker2'}, 3: {'host': 'broker3'}, 1: {'host': 'broker1'}}], autospec=True ) @mock.patch.object( ZK, 'get_json', side_effect=[{'brokerid': 2}], autospec=True ) def test_get_broker_list3(mock_client, mock_get_broker, mock_get_json): p = main.get_broker_list(cluster_config, active_controller_for_last=True) assert p == [(1, 'broker1'), (3, 'broker3'), (2, 'broker2')] ", https://github.com/ylaizet/alphanum_code,fb5cd313dc41f046aac01ff01d6e1561d17894a6,tests/test_alphanum_code.py::test_init_code,tests/test_alphanum_code.py,NIO,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Test AlphanumCOde. Usage: python -m pytest tests/ """""" import datetime import pytest import os from alphanum_code import AlphaNumCodeManager INIT_CODE = ""52Z9Z"" @pytest.fixture(scope=""module"") def coder(): manager = AlphaNumCodeManager(""sqlite:///tests/codes.sqlite"", code_size=5, init_code=INIT_CODE) yield manager os.remove(""tests/codes.sqlite"") def test_init_code(coder): code = coder.next_code(""init code test"") assert code == INIT_CODE def test_next_code(coder): coder.next_code() code = coder.next_code(""next code test"") assert code == ""52ZA1"" ", https://github.com/ylaizet/alphanum_code,fb5cd313dc41f046aac01ff01d6e1561d17894a6,tests/test_alphanum_code.py::test_next_code,tests/test_alphanum_code.py,OD-Brit,flaky,Unmaintained,nan,classify;root_cause,"#!/usr/bin/env python # -*- coding: utf-8 -*- """"""Test AlphanumCOde. Usage: python -m pytest tests/ """""" import datetime import pytest import os from alphanum_code import AlphaNumCodeManager INIT_CODE = ""52Z9Z"" @pytest.fixture(scope=""module"") def coder(): manager = AlphaNumCodeManager(""sqlite:///tests/codes.sqlite"", code_size=5, init_code=INIT_CODE) yield manager os.remove(""tests/codes.sqlite"") def test_init_code(coder): code = coder.next_code(""init code test"") assert code == INIT_CODE def test_next_code(coder): coder.next_code() code = coder.next_code(""next code test"") assert code == ""52ZA1"" ", https://github.com/yukinotenshi/pydeploy,e3ddf907b293c9da28503b3d72414a303c5dfbed,pydeploy/tests/test_command_chain.py::TestCommandChain::test_all,pydeploy/tests/test_command_chain.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import json import unittest from pydeploy.util import generate_random_string from pydeploy.command_chain import CommandChain class TestCommandChain(unittest.TestCase): def setUp(self): self.filename = generate_random_string() self.first_out = generate_random_string() self.second_out = generate_random_string() def tearDown(self): os.remove(self.filename) CommandChain.instance = None def test_empty(self): data = { 'pre_script': [], 'post_script': [], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) self.assertEqual(len(chain.commands), 1) self.assertEqual(chain.commands[0].cmd, 'git pull origin master') def test_prescript_only(self): data = { 'pre_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'post_script': [], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) chain.commands[0].execute() chain.commands[1].execute() self.assertEqual(len(chain.commands), 3) self.assertEqual(chain.commands[2].cmd, 'git pull origin master') self.assertIn(self.first_out, chain.commands[0].out) self.assertIn(self.second_out, chain.commands[1].out) def test_postscript_only(self): data = { 'post_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'pre_script': [], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) chain.commands[1].execute() chain.commands[2].execute() self.assertEqual(len(chain.commands), 3) self.assertEqual(chain.commands[0].cmd, 'git pull origin master') self.assertIn(self.first_out, chain.commands[1].out) self.assertIn(self.second_out, chain.commands[2].out) def test_all(self): data = { 'post_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'pre_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) chain.commands[0].execute() chain.commands[1].execute() chain.commands[3].execute() chain.commands[4].execute() self.assertEqual(len(chain.commands), 5) self.assertEqual(chain.commands[2].cmd, 'git pull origin master') self.assertIn(self.first_out, chain.commands[0].out) self.assertIn(self.second_out, chain.commands[1].out) self.assertIn(self.first_out, chain.commands[3].out) self.assertIn(self.second_out, chain.commands[4].out) def test_notifier_loaded(self): data = { 'pre_script': [], 'post_script': [], 'remote': 'origin', 'branch': 'master', 'notifier': { 'type': 'discord', 'receiver': 'something' } } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) self.assertIsNotNone(chain.notifier) ", https://github.com/yukinotenshi/pydeploy,e3ddf907b293c9da28503b3d72414a303c5dfbed,pydeploy/tests/test_command_chain.py::TestCommandChain::test_notifier_loaded,pydeploy/tests/test_command_chain.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import json import unittest from pydeploy.util import generate_random_string from pydeploy.command_chain import CommandChain class TestCommandChain(unittest.TestCase): def setUp(self): self.filename = generate_random_string() self.first_out = generate_random_string() self.second_out = generate_random_string() def tearDown(self): os.remove(self.filename) CommandChain.instance = None def test_empty(self): data = { 'pre_script': [], 'post_script': [], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) self.assertEqual(len(chain.commands), 1) self.assertEqual(chain.commands[0].cmd, 'git pull origin master') def test_prescript_only(self): data = { 'pre_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'post_script': [], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) chain.commands[0].execute() chain.commands[1].execute() self.assertEqual(len(chain.commands), 3) self.assertEqual(chain.commands[2].cmd, 'git pull origin master') self.assertIn(self.first_out, chain.commands[0].out) self.assertIn(self.second_out, chain.commands[1].out) def test_postscript_only(self): data = { 'post_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'pre_script': [], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) chain.commands[1].execute() chain.commands[2].execute() self.assertEqual(len(chain.commands), 3) self.assertEqual(chain.commands[0].cmd, 'git pull origin master') self.assertIn(self.first_out, chain.commands[1].out) self.assertIn(self.second_out, chain.commands[2].out) def test_all(self): data = { 'post_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'pre_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) chain.commands[0].execute() chain.commands[1].execute() chain.commands[3].execute() chain.commands[4].execute() self.assertEqual(len(chain.commands), 5) self.assertEqual(chain.commands[2].cmd, 'git pull origin master') self.assertIn(self.first_out, chain.commands[0].out) self.assertIn(self.second_out, chain.commands[1].out) self.assertIn(self.first_out, chain.commands[3].out) self.assertIn(self.second_out, chain.commands[4].out) def test_notifier_loaded(self): data = { 'pre_script': [], 'post_script': [], 'remote': 'origin', 'branch': 'master', 'notifier': { 'type': 'discord', 'receiver': 'something' } } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) self.assertIsNotNone(chain.notifier) ", https://github.com/yukinotenshi/pydeploy,e3ddf907b293c9da28503b3d72414a303c5dfbed,pydeploy/tests/test_command_chain.py::TestCommandChain::test_postscript_only,pydeploy/tests/test_command_chain.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import json import unittest from pydeploy.util import generate_random_string from pydeploy.command_chain import CommandChain class TestCommandChain(unittest.TestCase): def setUp(self): self.filename = generate_random_string() self.first_out = generate_random_string() self.second_out = generate_random_string() def tearDown(self): os.remove(self.filename) CommandChain.instance = None def test_empty(self): data = { 'pre_script': [], 'post_script': [], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) self.assertEqual(len(chain.commands), 1) self.assertEqual(chain.commands[0].cmd, 'git pull origin master') def test_prescript_only(self): data = { 'pre_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'post_script': [], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) chain.commands[0].execute() chain.commands[1].execute() self.assertEqual(len(chain.commands), 3) self.assertEqual(chain.commands[2].cmd, 'git pull origin master') self.assertIn(self.first_out, chain.commands[0].out) self.assertIn(self.second_out, chain.commands[1].out) def test_postscript_only(self): data = { 'post_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'pre_script': [], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) chain.commands[1].execute() chain.commands[2].execute() self.assertEqual(len(chain.commands), 3) self.assertEqual(chain.commands[0].cmd, 'git pull origin master') self.assertIn(self.first_out, chain.commands[1].out) self.assertIn(self.second_out, chain.commands[2].out) def test_all(self): data = { 'post_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'pre_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) chain.commands[0].execute() chain.commands[1].execute() chain.commands[3].execute() chain.commands[4].execute() self.assertEqual(len(chain.commands), 5) self.assertEqual(chain.commands[2].cmd, 'git pull origin master') self.assertIn(self.first_out, chain.commands[0].out) self.assertIn(self.second_out, chain.commands[1].out) self.assertIn(self.first_out, chain.commands[3].out) self.assertIn(self.second_out, chain.commands[4].out) def test_notifier_loaded(self): data = { 'pre_script': [], 'post_script': [], 'remote': 'origin', 'branch': 'master', 'notifier': { 'type': 'discord', 'receiver': 'something' } } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) self.assertIsNotNone(chain.notifier) ", https://github.com/yukinotenshi/pydeploy,e3ddf907b293c9da28503b3d72414a303c5dfbed,pydeploy/tests/test_command_chain.py::TestCommandChain::test_prescript_only,pydeploy/tests/test_command_chain.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"import os import json import unittest from pydeploy.util import generate_random_string from pydeploy.command_chain import CommandChain class TestCommandChain(unittest.TestCase): def setUp(self): self.filename = generate_random_string() self.first_out = generate_random_string() self.second_out = generate_random_string() def tearDown(self): os.remove(self.filename) CommandChain.instance = None def test_empty(self): data = { 'pre_script': [], 'post_script': [], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) self.assertEqual(len(chain.commands), 1) self.assertEqual(chain.commands[0].cmd, 'git pull origin master') def test_prescript_only(self): data = { 'pre_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'post_script': [], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) chain.commands[0].execute() chain.commands[1].execute() self.assertEqual(len(chain.commands), 3) self.assertEqual(chain.commands[2].cmd, 'git pull origin master') self.assertIn(self.first_out, chain.commands[0].out) self.assertIn(self.second_out, chain.commands[1].out) def test_postscript_only(self): data = { 'post_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'pre_script': [], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) chain.commands[1].execute() chain.commands[2].execute() self.assertEqual(len(chain.commands), 3) self.assertEqual(chain.commands[0].cmd, 'git pull origin master') self.assertIn(self.first_out, chain.commands[1].out) self.assertIn(self.second_out, chain.commands[2].out) def test_all(self): data = { 'post_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'pre_script': [ ""echo "" + self.first_out, ""echo "" + self.second_out, ], 'remote': 'origin', 'branch': 'master' } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) chain.commands[0].execute() chain.commands[1].execute() chain.commands[3].execute() chain.commands[4].execute() self.assertEqual(len(chain.commands), 5) self.assertEqual(chain.commands[2].cmd, 'git pull origin master') self.assertIn(self.first_out, chain.commands[0].out) self.assertIn(self.second_out, chain.commands[1].out) self.assertIn(self.first_out, chain.commands[3].out) self.assertIn(self.second_out, chain.commands[4].out) def test_notifier_loaded(self): data = { 'pre_script': [], 'post_script': [], 'remote': 'origin', 'branch': 'master', 'notifier': { 'type': 'discord', 'receiver': 'something' } } with open(self.filename, 'w') as f: json.dump(data, f) chain = CommandChain.load_from_config(self.filename) self.assertIsNotNone(chain.notifier) ", https://github.com/Zabamund/wellpathpy,f29b14a9a1c46e77ea98b141e3cdc43674654ea2,wellpathpy/test/test_location.py::test_wellhead,wellpathpy/test/test_location.py,OD-Vic,flaky,nan,nan,classify;root_cause,"import pytest import numpy as np import pandas as pd from ..mincurve import minimum_curvature from ..location import loc_to_wellhead, loc_to_zero, loc_to_tvdss # import test well data well9 = pd.read_csv('./wellpathpy/test/fixtures/well9.csv', sep="","") well10 = pd.read_csv('./wellpathpy/test/fixtures/well10.csv', sep="","") # get data series well9_true_md_m = well9['Measured Depth ( ft )'].values * 0.3048 # converting feet to meters well9_true_inc = well9['Inclination ( deg )'].values well9_true_azi = well9['Azimuth Grid ( deg )'].values well9_true_tvd_m = well9['TVD ( ft )'].values * 0.3048 # converting feet to meters well9_true_northing = well9['Northing ( m )'].values well9_true_easting = well9['Easting ( m )'].values well9_true_surface_northing = 39998.454 well9_true_surface_easting = 655701.278 well9_true_datum_elevation = 100 * 0.3048 # converting feet to meters well10_true_md_m = well10['Measured Depth ( ft )'].values * 0.3048 # converting feet to meters well10_true_inc = well10['Inclination ( deg )'].values well10_true_azi = well10['Azimuth Grid ( deg )'].values well10_true_tvd_m = well10['TVD ( ft )'].values * 0.3048 # converting feet to meters well10_true_northing = well10['Northing ( m )'].values well10_true_easting = well10['Easting ( m )'].values well10_true_surface_northing = 40004.564 well10_true_surface_easting = 655701.377 well10_true_datum_elevation = 100 * 0.3048 # converting feet to meters def test_wellhead(): # test well9 well9_tvd, well9_northing, well9_easting, _ = minimum_curvature(well9_true_md_m, well9_true_inc, well9_true_azi) tvd, mN, mE = loc_to_wellhead( well9_tvd, well9_northing, well9_easting, well9_true_surface_northing, well9_true_surface_easting ) np.testing.assert_equal(tvd, well9_tvd) np.testing.assert_allclose(mN, well9_true_northing, atol=1) np.testing.assert_allclose(mE, well9_true_easting, atol=1) # test well10 well10_tvd, well10_northing, well10_easting, _ = minimum_curvature(well10_true_md_m, well10_true_inc, well10_true_azi) tvd, mN, mE = loc_to_wellhead( well10_tvd, well10_northing, well10_easting, well10_true_surface_northing, well10_true_surface_easting ) np.testing.assert_equal(tvd, well10_tvd) np.testing.assert_allclose(mN, well10_true_northing, atol=1) np.testing.assert_allclose(mE, well10_true_easting, atol=1) def test_zero(): # test well9 _, well9_northing, well9_easting, _ = minimum_curvature(well9_true_md_m, well9_true_inc, well9_true_azi) tvd, mN, mE = loc_to_zero( well9_true_tvd_m, well9_true_northing, well9_true_easting, well9_true_surface_northing, well9_true_surface_easting ) np.testing.assert_equal(tvd, well9_true_tvd_m) np.testing.assert_allclose(mN, well9_northing, atol=1) np.testing.assert_allclose(mE, well9_easting, atol=1) # test well10 _, well10_northing, well10_easting, _ = minimum_curvature(well10_true_md_m, well10_true_inc, well10_true_azi) tvd, mN, mE = loc_to_zero( well10_true_tvd_m, well10_true_northing, well10_true_easting, well10_true_surface_northing, well10_true_surface_easting ) np.testing.assert_equal(tvd, well10_true_tvd_m) np.testing.assert_allclose(mN, well10_northing, atol=1) np.testing.assert_allclose(mE, well10_easting, atol=1) def test_tvdss(): # test well9 well9_tvd, well9_northing, well9_easting, _ = minimum_curvature(well9_true_md_m, well9_true_inc, well9_true_azi) tvdss, mN, mE = loc_to_tvdss( well9_tvd, well9_northing, well9_easting, well9_true_datum_elevation ) np.testing.assert_allclose(tvdss, well9_true_datum_elevation - well9_true_tvd_m) np.testing.assert_equal(mN, well9_northing) np.testing.assert_equal(mE, well9_easting) # test well10 well10_tvd, well10_northing, well10_easting, _ = minimum_curvature(well10_true_md_m, well10_true_inc, well10_true_azi) tvdss, mN, mE = loc_to_tvdss( well10_tvd, well10_northing, well10_easting, well10_true_datum_elevation ) np.testing.assert_allclose(tvdss, well10_true_datum_elevation - well10_true_tvd_m) np.testing.assert_equal(mN, well10_northing) np.testing.assert_equal(mE, well10_easting)", https://github.com/Zabamund/wellpathpy,f29b14a9a1c46e77ea98b141e3cdc43674654ea2,wellpathpy/test/test_location.py::test_zero,wellpathpy/test/test_location.py,NIO,flaky,Accepted,https://github.com/Zabamund/wellpathpy/pull/50,classify;root_cause;fix_proposal,"import pytest import numpy as np import pandas as pd from ..mincurve import minimum_curvature from ..location import loc_to_wellhead, loc_to_zero, loc_to_tvdss # import test well data well9 = pd.read_csv('./wellpathpy/test/fixtures/well9.csv', sep="","") well10 = pd.read_csv('./wellpathpy/test/fixtures/well10.csv', sep="","") # get data series well9_true_md_m = well9['Measured Depth ( ft )'].values * 0.3048 # converting feet to meters well9_true_inc = well9['Inclination ( deg )'].values well9_true_azi = well9['Azimuth Grid ( deg )'].values well9_true_tvd_m = well9['TVD ( ft )'].values * 0.3048 # converting feet to meters well9_true_northing = well9['Northing ( m )'].values well9_true_easting = well9['Easting ( m )'].values well9_true_surface_northing = 39998.454 well9_true_surface_easting = 655701.278 well9_true_datum_elevation = 100 * 0.3048 # converting feet to meters well10_true_md_m = well10['Measured Depth ( ft )'].values * 0.3048 # converting feet to meters well10_true_inc = well10['Inclination ( deg )'].values well10_true_azi = well10['Azimuth Grid ( deg )'].values well10_true_tvd_m = well10['TVD ( ft )'].values * 0.3048 # converting feet to meters well10_true_northing = well10['Northing ( m )'].values well10_true_easting = well10['Easting ( m )'].values well10_true_surface_northing = 40004.564 well10_true_surface_easting = 655701.377 well10_true_datum_elevation = 100 * 0.3048 # converting feet to meters def test_wellhead(): # test well9 well9_tvd, well9_northing, well9_easting, _ = minimum_curvature(well9_true_md_m, well9_true_inc, well9_true_azi) tvd, mN, mE = loc_to_wellhead( well9_tvd, well9_northing, well9_easting, well9_true_surface_northing, well9_true_surface_easting ) np.testing.assert_equal(tvd, well9_tvd) np.testing.assert_allclose(mN, well9_true_northing, atol=1) np.testing.assert_allclose(mE, well9_true_easting, atol=1) # test well10 well10_tvd, well10_northing, well10_easting, _ = minimum_curvature(well10_true_md_m, well10_true_inc, well10_true_azi) tvd, mN, mE = loc_to_wellhead( well10_tvd, well10_northing, well10_easting, well10_true_surface_northing, well10_true_surface_easting ) np.testing.assert_equal(tvd, well10_tvd) np.testing.assert_allclose(mN, well10_true_northing, atol=1) np.testing.assert_allclose(mE, well10_true_easting, atol=1) def test_zero(): # test well9 _, well9_northing, well9_easting, _ = minimum_curvature(well9_true_md_m, well9_true_inc, well9_true_azi) tvd, mN, mE = loc_to_zero( well9_true_tvd_m, well9_true_northing, well9_true_easting, well9_true_surface_northing, well9_true_surface_easting ) np.testing.assert_equal(tvd, well9_true_tvd_m) np.testing.assert_allclose(mN, well9_northing, atol=1) np.testing.assert_allclose(mE, well9_easting, atol=1) # test well10 _, well10_northing, well10_easting, _ = minimum_curvature(well10_true_md_m, well10_true_inc, well10_true_azi) tvd, mN, mE = loc_to_zero( well10_true_tvd_m, well10_true_northing, well10_true_easting, well10_true_surface_northing, well10_true_surface_easting ) np.testing.assert_equal(tvd, well10_true_tvd_m) np.testing.assert_allclose(mN, well10_northing, atol=1) np.testing.assert_allclose(mE, well10_easting, atol=1) def test_tvdss(): # test well9 well9_tvd, well9_northing, well9_easting, _ = minimum_curvature(well9_true_md_m, well9_true_inc, well9_true_azi) tvdss, mN, mE = loc_to_tvdss( well9_tvd, well9_northing, well9_easting, well9_true_datum_elevation ) np.testing.assert_allclose(tvdss, well9_true_datum_elevation - well9_true_tvd_m) np.testing.assert_equal(mN, well9_northing) np.testing.assert_equal(mE, well9_easting) # test well10 well10_tvd, well10_northing, well10_easting, _ = minimum_curvature(well10_true_md_m, well10_true_inc, well10_true_azi) tvdss, mN, mE = loc_to_tvdss( well10_tvd, well10_northing, well10_easting, well10_true_datum_elevation ) np.testing.assert_allclose(tvdss, well10_true_datum_elevation - well10_true_tvd_m) np.testing.assert_equal(mN, well10_northing) np.testing.assert_equal(mE, well10_easting)", https://github.com/zdhoward/aud,2dfc81dad3da78537e1b1c6f5cc5630ba5b837f9,aud/test_aud.py::test_afx_2,aud/test_aud.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from aud import aud from os.path import abspath, exists, join, isfile, basename, dirname from os import mkdir, listdir from shutil import rmtree, copy2 import zipfile def checkdir(target_directory): target_directory = abspath(target_directory) if not (exists(target_directory)): try: mkdir(target_directory) except: assert False return True def checkfile(target_directory, file_name): if not exists(join(target_directory, file_name)): try: file = open(join(target_directory, file_name), ""w"") file.close() except: assert False return True ### START TESTS ### dir = abspath(""mock"") def test_cleanup(): global dir if exists(dir): rmtree(dir) assert not exists(dir) def test_init(): global dir print(""SETTING UP FOR TEST"") test_cleanup() checkdir(dir) checkfile(dir, ""test.txt"") checkfile(dir, ""abc.txt"") copy2(join(abspath(""mock_assets""), ""bloop.wav""), join(abspath(""mock""), ""bloop.wav"")) copy2(join(abspath(""mock_assets""), ""song.wav""), join(abspath(""mock""), ""song.wav"")) assert True def test_dir(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) a.config_set_log_file(""mock/test.log"") assert a.log(""TESTING LOG"") assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert a.get_single(0) == ""bloop.wav"" # doesn't work because its not sorted assert a.backup(join(dir, ""backup"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""backup""))) == [""bloop.wav"", ""song.wav""] assert a.copy(join(dir, ""copy"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""copy""))) == [""bloop.wav"", ""song.wav""] assert a.move(join(dir, ""move"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""move""))) == [""bloop.wav"", ""song.wav""] assert a.move(dir) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(dir)) == [ ""abc.txt"", ""backup"", ""bloop.wav"", ""copy"", ""move"", ""song.wav"", ""test.log"", ""test.txt"", ] assert a.zip(""mock/test.zip"") assert isfile(""mock/test.zip"") with zipfile.ZipFile(""mock/test.zip"") as file: assert sorted(file.namelist()) == [""bloop.wav"", ""song.wav""] def test_config(): global dir a = aud.Dir(dir) print(""SETTING EXTENSIONS"") assert a.config_set_extensions([""txt""]) assert a.config_get_extensions() == ["".txt""] print(""SETTING denylist"") assert a.config_set_denylist([""test.txt""]) assert a.config_get_denylist() == [""test.txt""] assert sorted(a.get_all()) == [""abc.txt""] assert a.config_set_allowlist(regex=""test.txt"") assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.config_set_allowlist([]) assert a.config_set_denylist(regex=""test.txt"") assert sorted(a.get_all()) == [""abc.txt""] print(""SETTING allowlist"") assert a.config_set_allowlist([""test.txt""]) assert a.config_get_allowlist() == [""test.txt""] assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] def test_name(): global dir a = aud.Dir(dir) a.config_set_extensions([""txt""]) assert a.config_get_extensions() == ["".txt""] assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.name_upper() assert sorted(a.get_all()) == [""ABC.txt"", ""TEST.txt""] assert a.name_lower() assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.name_prepend(""abc_"") assert sorted(a.get_all()) == [""abc_abc.txt"", ""abc_test.txt""] assert a.name_append(""_test"") assert sorted(a.get_all()) == [""abc_abc_test.txt"", ""abc_test_test.txt""] assert a.name_replace(""_"", ""-"") assert sorted(a.get_all()) == [""abc-abc-test.txt"", ""abc-test-test.txt""] # doesn't work because its not sorted assert a.name_iterate(4, "" "") assert sorted(a.get_all()) == [""0001 abc-abc-test.txt"", ""0002 abc-test-test.txt""] assert a.name_replace_spaces(""_"") assert sorted(a.get_all()) == [""0001__abc-abc-test.txt"", ""0002__abc-test-test.txt""] def test_afx_1(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_fade(2, 2) assert a.afx_pad(1, 1) assert a.afx_prepend(""mock/bloop.wav"") assert a.afx_append(""mock/bloop.wav"") assert a.afx_watermark(""mock/bloop.wav"", 1, 10) def test_afx_2(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_normalize(passes=2) assert a.afx_invert_stereo_phase(""both"") assert a.afx_hpf(80) assert a.afx_lpf(12000) def test_afx_3(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_strip_silence() assert a.afx_join(""mock/joined.wav"", ""wav"") assert a.afx_gain(3) assert a.afx_gain(-3) def test_convert(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.convert_to_mp3() assert a.convert_to_raw() assert len(a.get_all()) == 3 a.config_set_extensions([""mp3""]) assert len(a.get_all()) == 3 assert a.convert_to_wav() a.config_set_extensions([""wav""]) assert a.convert_to(""ogg"") a.config_set_extensions([""ogg""]) assert len(a.get_all()) == 3 a.config_set_extensions([""wav""]) assert a.convert_to(""flac"") a.config_set_extensions([""flac""]) assert len(a.get_all()) == 3 a.config_set_extensions([""wav"", ""mp3""]) assert a.convert_to_mono() assert a.convert_to_stereo() def test_export(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.export_for(""amuse"", ""mock/amuse"") assert sorted(listdir(join(dir, ""amuse""))) == [ ""bloop.wav"", ""joined.wav"", ""song.wav"", ] ", https://github.com/zdhoward/aud,2dfc81dad3da78537e1b1c6f5cc5630ba5b837f9,aud/test_aud.py::test_afx_3,aud/test_aud.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from aud import aud from os.path import abspath, exists, join, isfile, basename, dirname from os import mkdir, listdir from shutil import rmtree, copy2 import zipfile def checkdir(target_directory): target_directory = abspath(target_directory) if not (exists(target_directory)): try: mkdir(target_directory) except: assert False return True def checkfile(target_directory, file_name): if not exists(join(target_directory, file_name)): try: file = open(join(target_directory, file_name), ""w"") file.close() except: assert False return True ### START TESTS ### dir = abspath(""mock"") def test_cleanup(): global dir if exists(dir): rmtree(dir) assert not exists(dir) def test_init(): global dir print(""SETTING UP FOR TEST"") test_cleanup() checkdir(dir) checkfile(dir, ""test.txt"") checkfile(dir, ""abc.txt"") copy2(join(abspath(""mock_assets""), ""bloop.wav""), join(abspath(""mock""), ""bloop.wav"")) copy2(join(abspath(""mock_assets""), ""song.wav""), join(abspath(""mock""), ""song.wav"")) assert True def test_dir(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) a.config_set_log_file(""mock/test.log"") assert a.log(""TESTING LOG"") assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert a.get_single(0) == ""bloop.wav"" # doesn't work because its not sorted assert a.backup(join(dir, ""backup"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""backup""))) == [""bloop.wav"", ""song.wav""] assert a.copy(join(dir, ""copy"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""copy""))) == [""bloop.wav"", ""song.wav""] assert a.move(join(dir, ""move"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""move""))) == [""bloop.wav"", ""song.wav""] assert a.move(dir) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(dir)) == [ ""abc.txt"", ""backup"", ""bloop.wav"", ""copy"", ""move"", ""song.wav"", ""test.log"", ""test.txt"", ] assert a.zip(""mock/test.zip"") assert isfile(""mock/test.zip"") with zipfile.ZipFile(""mock/test.zip"") as file: assert sorted(file.namelist()) == [""bloop.wav"", ""song.wav""] def test_config(): global dir a = aud.Dir(dir) print(""SETTING EXTENSIONS"") assert a.config_set_extensions([""txt""]) assert a.config_get_extensions() == ["".txt""] print(""SETTING denylist"") assert a.config_set_denylist([""test.txt""]) assert a.config_get_denylist() == [""test.txt""] assert sorted(a.get_all()) == [""abc.txt""] assert a.config_set_allowlist(regex=""test.txt"") assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.config_set_allowlist([]) assert a.config_set_denylist(regex=""test.txt"") assert sorted(a.get_all()) == [""abc.txt""] print(""SETTING allowlist"") assert a.config_set_allowlist([""test.txt""]) assert a.config_get_allowlist() == [""test.txt""] assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] def test_name(): global dir a = aud.Dir(dir) a.config_set_extensions([""txt""]) assert a.config_get_extensions() == ["".txt""] assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.name_upper() assert sorted(a.get_all()) == [""ABC.txt"", ""TEST.txt""] assert a.name_lower() assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.name_prepend(""abc_"") assert sorted(a.get_all()) == [""abc_abc.txt"", ""abc_test.txt""] assert a.name_append(""_test"") assert sorted(a.get_all()) == [""abc_abc_test.txt"", ""abc_test_test.txt""] assert a.name_replace(""_"", ""-"") assert sorted(a.get_all()) == [""abc-abc-test.txt"", ""abc-test-test.txt""] # doesn't work because its not sorted assert a.name_iterate(4, "" "") assert sorted(a.get_all()) == [""0001 abc-abc-test.txt"", ""0002 abc-test-test.txt""] assert a.name_replace_spaces(""_"") assert sorted(a.get_all()) == [""0001__abc-abc-test.txt"", ""0002__abc-test-test.txt""] def test_afx_1(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_fade(2, 2) assert a.afx_pad(1, 1) assert a.afx_prepend(""mock/bloop.wav"") assert a.afx_append(""mock/bloop.wav"") assert a.afx_watermark(""mock/bloop.wav"", 1, 10) def test_afx_2(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_normalize(passes=2) assert a.afx_invert_stereo_phase(""both"") assert a.afx_hpf(80) assert a.afx_lpf(12000) def test_afx_3(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_strip_silence() assert a.afx_join(""mock/joined.wav"", ""wav"") assert a.afx_gain(3) assert a.afx_gain(-3) def test_convert(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.convert_to_mp3() assert a.convert_to_raw() assert len(a.get_all()) == 3 a.config_set_extensions([""mp3""]) assert len(a.get_all()) == 3 assert a.convert_to_wav() a.config_set_extensions([""wav""]) assert a.convert_to(""ogg"") a.config_set_extensions([""ogg""]) assert len(a.get_all()) == 3 a.config_set_extensions([""wav""]) assert a.convert_to(""flac"") a.config_set_extensions([""flac""]) assert len(a.get_all()) == 3 a.config_set_extensions([""wav"", ""mp3""]) assert a.convert_to_mono() assert a.convert_to_stereo() def test_export(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.export_for(""amuse"", ""mock/amuse"") assert sorted(listdir(join(dir, ""amuse""))) == [ ""bloop.wav"", ""joined.wav"", ""song.wav"", ] ", https://github.com/zdhoward/aud,2dfc81dad3da78537e1b1c6f5cc5630ba5b837f9,aud/test_aud.py::test_config,aud/test_aud.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from aud import aud from os.path import abspath, exists, join, isfile, basename, dirname from os import mkdir, listdir from shutil import rmtree, copy2 import zipfile def checkdir(target_directory): target_directory = abspath(target_directory) if not (exists(target_directory)): try: mkdir(target_directory) except: assert False return True def checkfile(target_directory, file_name): if not exists(join(target_directory, file_name)): try: file = open(join(target_directory, file_name), ""w"") file.close() except: assert False return True ### START TESTS ### dir = abspath(""mock"") def test_cleanup(): global dir if exists(dir): rmtree(dir) assert not exists(dir) def test_init(): global dir print(""SETTING UP FOR TEST"") test_cleanup() checkdir(dir) checkfile(dir, ""test.txt"") checkfile(dir, ""abc.txt"") copy2(join(abspath(""mock_assets""), ""bloop.wav""), join(abspath(""mock""), ""bloop.wav"")) copy2(join(abspath(""mock_assets""), ""song.wav""), join(abspath(""mock""), ""song.wav"")) assert True def test_dir(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) a.config_set_log_file(""mock/test.log"") assert a.log(""TESTING LOG"") assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert a.get_single(0) == ""bloop.wav"" # doesn't work because its not sorted assert a.backup(join(dir, ""backup"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""backup""))) == [""bloop.wav"", ""song.wav""] assert a.copy(join(dir, ""copy"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""copy""))) == [""bloop.wav"", ""song.wav""] assert a.move(join(dir, ""move"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""move""))) == [""bloop.wav"", ""song.wav""] assert a.move(dir) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(dir)) == [ ""abc.txt"", ""backup"", ""bloop.wav"", ""copy"", ""move"", ""song.wav"", ""test.log"", ""test.txt"", ] assert a.zip(""mock/test.zip"") assert isfile(""mock/test.zip"") with zipfile.ZipFile(""mock/test.zip"") as file: assert sorted(file.namelist()) == [""bloop.wav"", ""song.wav""] def test_config(): global dir a = aud.Dir(dir) print(""SETTING EXTENSIONS"") assert a.config_set_extensions([""txt""]) assert a.config_get_extensions() == ["".txt""] print(""SETTING denylist"") assert a.config_set_denylist([""test.txt""]) assert a.config_get_denylist() == [""test.txt""] assert sorted(a.get_all()) == [""abc.txt""] assert a.config_set_allowlist(regex=""test.txt"") assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.config_set_allowlist([]) assert a.config_set_denylist(regex=""test.txt"") assert sorted(a.get_all()) == [""abc.txt""] print(""SETTING allowlist"") assert a.config_set_allowlist([""test.txt""]) assert a.config_get_allowlist() == [""test.txt""] assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] def test_name(): global dir a = aud.Dir(dir) a.config_set_extensions([""txt""]) assert a.config_get_extensions() == ["".txt""] assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.name_upper() assert sorted(a.get_all()) == [""ABC.txt"", ""TEST.txt""] assert a.name_lower() assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.name_prepend(""abc_"") assert sorted(a.get_all()) == [""abc_abc.txt"", ""abc_test.txt""] assert a.name_append(""_test"") assert sorted(a.get_all()) == [""abc_abc_test.txt"", ""abc_test_test.txt""] assert a.name_replace(""_"", ""-"") assert sorted(a.get_all()) == [""abc-abc-test.txt"", ""abc-test-test.txt""] # doesn't work because its not sorted assert a.name_iterate(4, "" "") assert sorted(a.get_all()) == [""0001 abc-abc-test.txt"", ""0002 abc-test-test.txt""] assert a.name_replace_spaces(""_"") assert sorted(a.get_all()) == [""0001__abc-abc-test.txt"", ""0002__abc-test-test.txt""] def test_afx_1(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_fade(2, 2) assert a.afx_pad(1, 1) assert a.afx_prepend(""mock/bloop.wav"") assert a.afx_append(""mock/bloop.wav"") assert a.afx_watermark(""mock/bloop.wav"", 1, 10) def test_afx_2(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_normalize(passes=2) assert a.afx_invert_stereo_phase(""both"") assert a.afx_hpf(80) assert a.afx_lpf(12000) def test_afx_3(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_strip_silence() assert a.afx_join(""mock/joined.wav"", ""wav"") assert a.afx_gain(3) assert a.afx_gain(-3) def test_convert(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.convert_to_mp3() assert a.convert_to_raw() assert len(a.get_all()) == 3 a.config_set_extensions([""mp3""]) assert len(a.get_all()) == 3 assert a.convert_to_wav() a.config_set_extensions([""wav""]) assert a.convert_to(""ogg"") a.config_set_extensions([""ogg""]) assert len(a.get_all()) == 3 a.config_set_extensions([""wav""]) assert a.convert_to(""flac"") a.config_set_extensions([""flac""]) assert len(a.get_all()) == 3 a.config_set_extensions([""wav"", ""mp3""]) assert a.convert_to_mono() assert a.convert_to_stereo() def test_export(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.export_for(""amuse"", ""mock/amuse"") assert sorted(listdir(join(dir, ""amuse""))) == [ ""bloop.wav"", ""joined.wav"", ""song.wav"", ] ", https://github.com/zdhoward/aud,2dfc81dad3da78537e1b1c6f5cc5630ba5b837f9,aud/test_aud.py::test_dir,aud/test_aud.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from aud import aud from os.path import abspath, exists, join, isfile, basename, dirname from os import mkdir, listdir from shutil import rmtree, copy2 import zipfile def checkdir(target_directory): target_directory = abspath(target_directory) if not (exists(target_directory)): try: mkdir(target_directory) except: assert False return True def checkfile(target_directory, file_name): if not exists(join(target_directory, file_name)): try: file = open(join(target_directory, file_name), ""w"") file.close() except: assert False return True ### START TESTS ### dir = abspath(""mock"") def test_cleanup(): global dir if exists(dir): rmtree(dir) assert not exists(dir) def test_init(): global dir print(""SETTING UP FOR TEST"") test_cleanup() checkdir(dir) checkfile(dir, ""test.txt"") checkfile(dir, ""abc.txt"") copy2(join(abspath(""mock_assets""), ""bloop.wav""), join(abspath(""mock""), ""bloop.wav"")) copy2(join(abspath(""mock_assets""), ""song.wav""), join(abspath(""mock""), ""song.wav"")) assert True def test_dir(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) a.config_set_log_file(""mock/test.log"") assert a.log(""TESTING LOG"") assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert a.get_single(0) == ""bloop.wav"" # doesn't work because its not sorted assert a.backup(join(dir, ""backup"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""backup""))) == [""bloop.wav"", ""song.wav""] assert a.copy(join(dir, ""copy"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""copy""))) == [""bloop.wav"", ""song.wav""] assert a.move(join(dir, ""move"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""move""))) == [""bloop.wav"", ""song.wav""] assert a.move(dir) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(dir)) == [ ""abc.txt"", ""backup"", ""bloop.wav"", ""copy"", ""move"", ""song.wav"", ""test.log"", ""test.txt"", ] assert a.zip(""mock/test.zip"") assert isfile(""mock/test.zip"") with zipfile.ZipFile(""mock/test.zip"") as file: assert sorted(file.namelist()) == [""bloop.wav"", ""song.wav""] def test_config(): global dir a = aud.Dir(dir) print(""SETTING EXTENSIONS"") assert a.config_set_extensions([""txt""]) assert a.config_get_extensions() == ["".txt""] print(""SETTING denylist"") assert a.config_set_denylist([""test.txt""]) assert a.config_get_denylist() == [""test.txt""] assert sorted(a.get_all()) == [""abc.txt""] assert a.config_set_allowlist(regex=""test.txt"") assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.config_set_allowlist([]) assert a.config_set_denylist(regex=""test.txt"") assert sorted(a.get_all()) == [""abc.txt""] print(""SETTING allowlist"") assert a.config_set_allowlist([""test.txt""]) assert a.config_get_allowlist() == [""test.txt""] assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] def test_name(): global dir a = aud.Dir(dir) a.config_set_extensions([""txt""]) assert a.config_get_extensions() == ["".txt""] assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.name_upper() assert sorted(a.get_all()) == [""ABC.txt"", ""TEST.txt""] assert a.name_lower() assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.name_prepend(""abc_"") assert sorted(a.get_all()) == [""abc_abc.txt"", ""abc_test.txt""] assert a.name_append(""_test"") assert sorted(a.get_all()) == [""abc_abc_test.txt"", ""abc_test_test.txt""] assert a.name_replace(""_"", ""-"") assert sorted(a.get_all()) == [""abc-abc-test.txt"", ""abc-test-test.txt""] # doesn't work because its not sorted assert a.name_iterate(4, "" "") assert sorted(a.get_all()) == [""0001 abc-abc-test.txt"", ""0002 abc-test-test.txt""] assert a.name_replace_spaces(""_"") assert sorted(a.get_all()) == [""0001__abc-abc-test.txt"", ""0002__abc-test-test.txt""] def test_afx_1(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_fade(2, 2) assert a.afx_pad(1, 1) assert a.afx_prepend(""mock/bloop.wav"") assert a.afx_append(""mock/bloop.wav"") assert a.afx_watermark(""mock/bloop.wav"", 1, 10) def test_afx_2(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_normalize(passes=2) assert a.afx_invert_stereo_phase(""both"") assert a.afx_hpf(80) assert a.afx_lpf(12000) def test_afx_3(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_strip_silence() assert a.afx_join(""mock/joined.wav"", ""wav"") assert a.afx_gain(3) assert a.afx_gain(-3) def test_convert(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.convert_to_mp3() assert a.convert_to_raw() assert len(a.get_all()) == 3 a.config_set_extensions([""mp3""]) assert len(a.get_all()) == 3 assert a.convert_to_wav() a.config_set_extensions([""wav""]) assert a.convert_to(""ogg"") a.config_set_extensions([""ogg""]) assert len(a.get_all()) == 3 a.config_set_extensions([""wav""]) assert a.convert_to(""flac"") a.config_set_extensions([""flac""]) assert len(a.get_all()) == 3 a.config_set_extensions([""wav"", ""mp3""]) assert a.convert_to_mono() assert a.convert_to_stereo() def test_export(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.export_for(""amuse"", ""mock/amuse"") assert sorted(listdir(join(dir, ""amuse""))) == [ ""bloop.wav"", ""joined.wav"", ""song.wav"", ] ", https://github.com/zdhoward/aud,2dfc81dad3da78537e1b1c6f5cc5630ba5b837f9,aud/test_aud.py::test_export,aud/test_aud.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from aud import aud from os.path import abspath, exists, join, isfile, basename, dirname from os import mkdir, listdir from shutil import rmtree, copy2 import zipfile def checkdir(target_directory): target_directory = abspath(target_directory) if not (exists(target_directory)): try: mkdir(target_directory) except: assert False return True def checkfile(target_directory, file_name): if not exists(join(target_directory, file_name)): try: file = open(join(target_directory, file_name), ""w"") file.close() except: assert False return True ### START TESTS ### dir = abspath(""mock"") def test_cleanup(): global dir if exists(dir): rmtree(dir) assert not exists(dir) def test_init(): global dir print(""SETTING UP FOR TEST"") test_cleanup() checkdir(dir) checkfile(dir, ""test.txt"") checkfile(dir, ""abc.txt"") copy2(join(abspath(""mock_assets""), ""bloop.wav""), join(abspath(""mock""), ""bloop.wav"")) copy2(join(abspath(""mock_assets""), ""song.wav""), join(abspath(""mock""), ""song.wav"")) assert True def test_dir(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) a.config_set_log_file(""mock/test.log"") assert a.log(""TESTING LOG"") assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert a.get_single(0) == ""bloop.wav"" # doesn't work because its not sorted assert a.backup(join(dir, ""backup"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""backup""))) == [""bloop.wav"", ""song.wav""] assert a.copy(join(dir, ""copy"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""copy""))) == [""bloop.wav"", ""song.wav""] assert a.move(join(dir, ""move"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""move""))) == [""bloop.wav"", ""song.wav""] assert a.move(dir) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(dir)) == [ ""abc.txt"", ""backup"", ""bloop.wav"", ""copy"", ""move"", ""song.wav"", ""test.log"", ""test.txt"", ] assert a.zip(""mock/test.zip"") assert isfile(""mock/test.zip"") with zipfile.ZipFile(""mock/test.zip"") as file: assert sorted(file.namelist()) == [""bloop.wav"", ""song.wav""] def test_config(): global dir a = aud.Dir(dir) print(""SETTING EXTENSIONS"") assert a.config_set_extensions([""txt""]) assert a.config_get_extensions() == ["".txt""] print(""SETTING denylist"") assert a.config_set_denylist([""test.txt""]) assert a.config_get_denylist() == [""test.txt""] assert sorted(a.get_all()) == [""abc.txt""] assert a.config_set_allowlist(regex=""test.txt"") assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.config_set_allowlist([]) assert a.config_set_denylist(regex=""test.txt"") assert sorted(a.get_all()) == [""abc.txt""] print(""SETTING allowlist"") assert a.config_set_allowlist([""test.txt""]) assert a.config_get_allowlist() == [""test.txt""] assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] def test_name(): global dir a = aud.Dir(dir) a.config_set_extensions([""txt""]) assert a.config_get_extensions() == ["".txt""] assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.name_upper() assert sorted(a.get_all()) == [""ABC.txt"", ""TEST.txt""] assert a.name_lower() assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.name_prepend(""abc_"") assert sorted(a.get_all()) == [""abc_abc.txt"", ""abc_test.txt""] assert a.name_append(""_test"") assert sorted(a.get_all()) == [""abc_abc_test.txt"", ""abc_test_test.txt""] assert a.name_replace(""_"", ""-"") assert sorted(a.get_all()) == [""abc-abc-test.txt"", ""abc-test-test.txt""] # doesn't work because its not sorted assert a.name_iterate(4, "" "") assert sorted(a.get_all()) == [""0001 abc-abc-test.txt"", ""0002 abc-test-test.txt""] assert a.name_replace_spaces(""_"") assert sorted(a.get_all()) == [""0001__abc-abc-test.txt"", ""0002__abc-test-test.txt""] def test_afx_1(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_fade(2, 2) assert a.afx_pad(1, 1) assert a.afx_prepend(""mock/bloop.wav"") assert a.afx_append(""mock/bloop.wav"") assert a.afx_watermark(""mock/bloop.wav"", 1, 10) def test_afx_2(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_normalize(passes=2) assert a.afx_invert_stereo_phase(""both"") assert a.afx_hpf(80) assert a.afx_lpf(12000) def test_afx_3(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_strip_silence() assert a.afx_join(""mock/joined.wav"", ""wav"") assert a.afx_gain(3) assert a.afx_gain(-3) def test_convert(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.convert_to_mp3() assert a.convert_to_raw() assert len(a.get_all()) == 3 a.config_set_extensions([""mp3""]) assert len(a.get_all()) == 3 assert a.convert_to_wav() a.config_set_extensions([""wav""]) assert a.convert_to(""ogg"") a.config_set_extensions([""ogg""]) assert len(a.get_all()) == 3 a.config_set_extensions([""wav""]) assert a.convert_to(""flac"") a.config_set_extensions([""flac""]) assert len(a.get_all()) == 3 a.config_set_extensions([""wav"", ""mp3""]) assert a.convert_to_mono() assert a.convert_to_stereo() def test_export(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.export_for(""amuse"", ""mock/amuse"") assert sorted(listdir(join(dir, ""amuse""))) == [ ""bloop.wav"", ""joined.wav"", ""song.wav"", ] ", https://github.com/zdhoward/aud,2dfc81dad3da78537e1b1c6f5cc5630ba5b837f9,aud/test_aud.py::test_name,aud/test_aud.py,NOD,flaky,Unmaintained,nan,classify;root_cause,"from aud import aud from os.path import abspath, exists, join, isfile, basename, dirname from os import mkdir, listdir from shutil import rmtree, copy2 import zipfile def checkdir(target_directory): target_directory = abspath(target_directory) if not (exists(target_directory)): try: mkdir(target_directory) except: assert False return True def checkfile(target_directory, file_name): if not exists(join(target_directory, file_name)): try: file = open(join(target_directory, file_name), ""w"") file.close() except: assert False return True ### START TESTS ### dir = abspath(""mock"") def test_cleanup(): global dir if exists(dir): rmtree(dir) assert not exists(dir) def test_init(): global dir print(""SETTING UP FOR TEST"") test_cleanup() checkdir(dir) checkfile(dir, ""test.txt"") checkfile(dir, ""abc.txt"") copy2(join(abspath(""mock_assets""), ""bloop.wav""), join(abspath(""mock""), ""bloop.wav"")) copy2(join(abspath(""mock_assets""), ""song.wav""), join(abspath(""mock""), ""song.wav"")) assert True def test_dir(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) a.config_set_log_file(""mock/test.log"") assert a.log(""TESTING LOG"") assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert a.get_single(0) == ""bloop.wav"" # doesn't work because its not sorted assert a.backup(join(dir, ""backup"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""backup""))) == [""bloop.wav"", ""song.wav""] assert a.copy(join(dir, ""copy"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""copy""))) == [""bloop.wav"", ""song.wav""] assert a.move(join(dir, ""move"")) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(join(dir, ""move""))) == [""bloop.wav"", ""song.wav""] assert a.move(dir) assert sorted(a.get_all()) == [""bloop.wav"", ""song.wav""] assert sorted(listdir(dir)) == [ ""abc.txt"", ""backup"", ""bloop.wav"", ""copy"", ""move"", ""song.wav"", ""test.log"", ""test.txt"", ] assert a.zip(""mock/test.zip"") assert isfile(""mock/test.zip"") with zipfile.ZipFile(""mock/test.zip"") as file: assert sorted(file.namelist()) == [""bloop.wav"", ""song.wav""] def test_config(): global dir a = aud.Dir(dir) print(""SETTING EXTENSIONS"") assert a.config_set_extensions([""txt""]) assert a.config_get_extensions() == ["".txt""] print(""SETTING denylist"") assert a.config_set_denylist([""test.txt""]) assert a.config_get_denylist() == [""test.txt""] assert sorted(a.get_all()) == [""abc.txt""] assert a.config_set_allowlist(regex=""test.txt"") assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.config_set_allowlist([]) assert a.config_set_denylist(regex=""test.txt"") assert sorted(a.get_all()) == [""abc.txt""] print(""SETTING allowlist"") assert a.config_set_allowlist([""test.txt""]) assert a.config_get_allowlist() == [""test.txt""] assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] def test_name(): global dir a = aud.Dir(dir) a.config_set_extensions([""txt""]) assert a.config_get_extensions() == ["".txt""] assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.name_upper() assert sorted(a.get_all()) == [""ABC.txt"", ""TEST.txt""] assert a.name_lower() assert sorted(a.get_all()) == [""abc.txt"", ""test.txt""] assert a.name_prepend(""abc_"") assert sorted(a.get_all()) == [""abc_abc.txt"", ""abc_test.txt""] assert a.name_append(""_test"") assert sorted(a.get_all()) == [""abc_abc_test.txt"", ""abc_test_test.txt""] assert a.name_replace(""_"", ""-"") assert sorted(a.get_all()) == [""abc-abc-test.txt"", ""abc-test-test.txt""] # doesn't work because its not sorted assert a.name_iterate(4, "" "") assert sorted(a.get_all()) == [""0001 abc-abc-test.txt"", ""0002 abc-test-test.txt""] assert a.name_replace_spaces(""_"") assert sorted(a.get_all()) == [""0001__abc-abc-test.txt"", ""0002__abc-test-test.txt""] def test_afx_1(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_fade(2, 2) assert a.afx_pad(1, 1) assert a.afx_prepend(""mock/bloop.wav"") assert a.afx_append(""mock/bloop.wav"") assert a.afx_watermark(""mock/bloop.wav"", 1, 10) def test_afx_2(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_normalize(passes=2) assert a.afx_invert_stereo_phase(""both"") assert a.afx_hpf(80) assert a.afx_lpf(12000) def test_afx_3(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.afx_strip_silence() assert a.afx_join(""mock/joined.wav"", ""wav"") assert a.afx_gain(3) assert a.afx_gain(-3) def test_convert(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.convert_to_mp3() assert a.convert_to_raw() assert len(a.get_all()) == 3 a.config_set_extensions([""mp3""]) assert len(a.get_all()) == 3 assert a.convert_to_wav() a.config_set_extensions([""wav""]) assert a.convert_to(""ogg"") a.config_set_extensions([""ogg""]) assert len(a.get_all()) == 3 a.config_set_extensions([""wav""]) assert a.convert_to(""flac"") a.config_set_extensions([""flac""]) assert len(a.get_all()) == 3 a.config_set_extensions([""wav"", ""mp3""]) assert a.convert_to_mono() assert a.convert_to_stereo() def test_export(): global dir a = aud.Dir(dir) a.config_set_extensions([""wav""]) assert a.export_for(""amuse"", ""mock/amuse"") assert sorted(listdir(join(dir, ""amuse""))) == [ ""bloop.wav"", ""joined.wav"", ""song.wav"", ] ", https://github.com/zerotosingularity/pdl,cb22f929bddb51d8db16a4efede5ceaaf6fcd906,tests/test_pdl.py::pdl_test_helper,tests/test_pdl.py,NIO,flaky,Accepted,https://github.com/zerotosingularity/pdl/pull/6,classify;root_cause;fix_proposal,""""""" Tests for PDL """""" import http.server import threading import os import shutil import pytest from pdl import pdl DATA_DIR = ""data/"" TEST_FILE = ""1_hello_tensorflow.py"" MAT_FILE = ""data.mat"" PORT = 7972 URL = ""http://localhost:{port}"".format(port=PORT) ZIP_URL = ""{url}/tests/fixtures/hello_tensorflow.zip"".format(url=URL) TAR_GZ_URL = ""{url}/tests/fixtures/hello_tensorflow.tar.gz"".format(url=URL) TAR_URL = ""{url}/tests/fixtures/hello_tensorflow.tar"".format(url=URL) TGZ_URL = ""{url}/tests/fixtures/hello_tensorflow.tgz"".format(url=URL) GZ_URL = ""{url}/tests/fixtures/1_hello_tensorflow.py.gz"".format(url=URL) NON_ARCHIVE_URL = ""{url}/tests/fixtures/{mat_file}"".format( url=URL, mat_file=MAT_FILE) EMPTY_URL = """" URL_WITHOUT_FILE = URL + ""/"" # pylint: disable=C0103 httpd = http.server.HTTPServer( ('', PORT), http.server.SimpleHTTPRequestHandler) # pylint: disable=C0103 thread = threading.Thread(target=httpd.serve_forever) def setup_module(module): # pylint: disable=W0612,W0613 """""" Setup function for test framework """""" print(""setting up module"") thread.start() def teardown_module(module): # pylint: disable=W0612,W0613 """""" Tear down module """""" print(""tearing down module"") stop_server() print(os.getcwd()) shutil.rmtree(DATA_DIR) def test_zip_download(): """""" Test zip downlaod """""" pdl_test_helper(ZIP_URL) def test_tar_gz_download(): """""" Test .tar.gz download """""" pdl_test_helper(TAR_GZ_URL) def test_tar_download(): """""" Test tar download """""" pdl_test_helper(TAR_URL) def test_tgz_download(): """""" Test .tgz download """""" pdl_test_helper(TGZ_URL) def test_gz_download(): """""" Test .gz download """""" pdl_test_helper(GZ_URL) def test_empty_url(): """""" Test an empty url """""" with pytest.raises(Exception): pdl_test_helper(EMPTY_URL) def test_no_file_url(): """""" Test a url without a file """""" with pytest.raises(Exception): pdl_test_helper(URL_WITHOUT_FILE) def test_no_archive_url(): """""" Test a non-archive url """""" pdl_test_helper(NON_ARCHIVE_URL, False) @pytest.mark.skip(reason=""only used for attribute error fixing"") def test_movie_lens_latest(): """""" Skipped test for movie lens; advised use for debugging only """""" try: pdl.movie_lens_latest() except AttributeError: pytest.fail(""Unexpected error"") def pdl_test_helper(url, archive=True): """""" Test for PDL """""" filename = pdl.get_filename(url) file_location = pdl.get_file_location(DATA_DIR, filename) print(file_location) assert not os.path.exists(file_location) pdl.download(url, DATA_DIR) test_file_location = pdl.get_file_location(DATA_DIR, TEST_FILE) assert os.path.exists(test_file_location) if archive: assert not os.path.exists(file_location) else: os.unlink(file_location) def stop_server(): """""" Stop HTTP server """""" httpd.shutdown() ", https://github.com/zerotosingularity/pdl,fd39c391101b3fe315631565a0f4eb7c99890789,tests/test_pdl.py::test_no_archive_url,tests/test_pdl.py,OD-Brit,flaky,nan,nan,classify;root_cause,""""""" Tests for PDL """""" import http.server import threading import os import shutil import pytest from pdl import pdl DATA_DIR = ""data/"" TEST_FILE = ""1_hello_tensorflow.py"" MAT_FILE = ""data.mat"" PORT = 7972 URL = ""http://localhost:{port}"".format(port=PORT) ZIP_URL = ""{url}/tests/fixtures/hello_tensorflow.zip"".format(url=URL) TAR_GZ_URL = ""{url}/tests/fixtures/hello_tensorflow.tar.gz"".format(url=URL) TAR_URL = ""{url}/tests/fixtures/hello_tensorflow.tar"".format(url=URL) TGZ_URL = ""{url}/tests/fixtures/hello_tensorflow.tgz"".format(url=URL) GZ_URL = ""{url}/tests/fixtures/1_hello_tensorflow.py.gz"".format(url=URL) NON_ARCHIVE_URL = ""{url}/tests/fixtures/{mat_file}"".format( url=URL, mat_file=MAT_FILE) EMPTY_URL = """" URL_WITHOUT_FILE = URL + ""/"" # pylint: disable=C0103 httpd = http.server.HTTPServer( ('', PORT), http.server.SimpleHTTPRequestHandler) # pylint: disable=C0103 thread = threading.Thread(target=httpd.serve_forever) def setup_module(module): # pylint: disable=W0612,W0613 """""" Setup function for test framework """""" print(""setting up module"") thread.start() def teardown_module(module): # pylint: disable=W0612,W0613 """""" Tear down module """""" print(""tearing down module"") stop_server() print(os.getcwd()) shutil.rmtree(DATA_DIR) def test_zip_download(): """""" Test zip downlaod """""" pdl_test_helper(ZIP_URL) def test_tar_gz_download(): """""" Test .tar.gz download """""" pdl_test_helper(TAR_GZ_URL) def test_tar_download(): """""" Test tar download """""" pdl_test_helper(TAR_URL) def test_tgz_download(): """""" Test .tgz download """""" pdl_test_helper(TGZ_URL) def test_gz_download(): """""" Test .gz download """""" pdl_test_helper(GZ_URL) def test_empty_url(): """""" Test an empty url """""" with pytest.raises(Exception): pdl_test_helper(EMPTY_URL) def test_no_file_url(): """""" Test a url without a file """""" with pytest.raises(Exception): pdl_test_helper(URL_WITHOUT_FILE) def test_no_archive_url(): """""" Test a non-archive url """""" pdl_test_helper(NON_ARCHIVE_URL, False) @pytest.mark.skip(reason=""only used for attribute error fixing"") def test_movie_lens_latest(): """""" Skipped test for movie lens; advised use for debugging only """""" try: pdl.movie_lens_latest() except AttributeError: pytest.fail(""Unexpected error"") def pdl_test_helper(url, archive=True): """""" Test for PDL """""" filename = pdl.get_filename(url) file_location = pdl.get_file_location(DATA_DIR, filename) print(file_location) assert not os.path.exists(file_location) pdl.download(url, DATA_DIR) test_file_location = pdl.get_file_location(DATA_DIR, TEST_FILE) assert os.path.exists(test_file_location) if archive: assert not os.path.exists(file_location) def stop_server(): """""" Stop HTTP server """""" httpd.shutdown() ", https://github.com/zzzzlzzzz/jackfruit,350a41aa28e2bdc9367f9dd5e686b21125828589,tests/test_jackfruit.py::test_single_view,tests/test_jackfruit.py,OD-Vic,flaky,Unmaintained,nan,classify;root_cause,"from copy import deepcopy from pytest import fixture from jackfruit import * from telegram.ext import CallbackQueryHandler, CommandHandler, MessageHandler, Filters, CallbackContext @fixture() def dispatcher(): class DispatcherMock: HANDLERS = [] def add_handler(self, h): self.HANDLERS.append(h) yield DispatcherMock() @fixture() def view(): class GenericViewMock: def __init__(self): self.name = 'name' self.show_call = False def get_name(self): return self.name def process(self, state, update, context): return 'new_state' def show(self, update, context, msg_id=None): self.show_call = True yield GenericViewMock() def test_single_view(dispatcher, view): j = Jackfruit(dispatcher, view) assert len(j.STATE) == 1 assert j.STATE[view.get_name()] == view assert len(dispatcher.HANDLERS) == 2 for h in dispatcher.HANDLERS: if isinstance(h, CallbackQueryHandler): assert h.callback == j._dispatch elif isinstance(h, MessageHandler): assert h.callback == j._dispatch else: assert False def test_single_view_with_command(dispatcher, view): j = Jackfruit(dispatcher, view, [('start', view), ]) assert len(dispatcher.HANDLERS) == 3 for h in dispatcher.HANDLERS: if isinstance(h, CallbackQueryHandler): pass elif isinstance(h, MessageHandler): pass elif isinstance(h, CommandHandler): assert h.callback.func == j._dispatch_command else: assert False def test_register_view(dispatcher, view): view2 = deepcopy(view) view2.name = 'name2' view3 = deepcopy(view) view3.name = 'name3' j = Jackfruit(dispatcher, view, [('start', view), ]) j.register(view2, view3) assert len(j.STATE) == 3 assert j.STATE[view.get_name()] == view assert j.STATE[view2.get_name()] == view2 assert j.STATE[view3.get_name()] == view3 def get_dispatch_jackfruit(d, v): class JackfruitMock(Jackfruit): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._call_before_dispatch = False self._call_after_dispatch = False def before_dispatch(self, update, context) -> None: self._call_before_dispatch = True def after_dispatch(self, update, context) -> None: self._call_after_dispatch = True return JackfruitMock(d, v) def test_dispatch(dispatcher, view, update, context): j = get_dispatch_jackfruit(dispatcher, view) j._dispatch(update, context) assert context.chat_data[j._state_key] == 'new_state' assert j._call_before_dispatch assert j._call_after_dispatch def test_dispatch_command(dispatcher, view, update, context): j = get_dispatch_jackfruit(dispatcher, view) j._dispatch_command('name', update, context) assert context.chat_data[j._state_key] == 'name' assert view.show_call assert j._call_before_dispatch assert j._call_after_dispatch ",