def test_error_on_positional_constructor_args(self): with self.assertRaises(TypeError): SimpleNamespace('a') with self.assertRaises(TypeError): SimpleNamespace('a', 'bb') with self.assertRaises(TypeError): SimpleNamespace('xyz', a=sentinel.a, bb=sentinel.bb)
def test_initialization_with_default_binding_key( self, binding_key, mocked_conf_from_files, expected_config, expected_config_full, custom_config_spec_pattern=None): class SomeParser(BaseParser): default_binding_key = binding_key # => it's a concrete class if custom_config_spec_pattern is not None: SomeParser.config_spec_pattern = custom_config_spec_pattern unready_instance = SomeParser.__new__(SomeParser) self._asserts_of_proper__new__instance_adjustment(unready_instance) self._asserts_of_proper_preinit_hook_instance_adjustment( unready_instance, binding_key) super_cls_mock = SimpleNamespace(__init__=Mock()) with patch_always('n6.parsers.generic.super', return_value=super_cls_mock) as super_mock, \ patch('n6.parsers.generic.Config._load_n6_config_files', return_value=mocked_conf_from_files): # instantiation instance = SomeParser(a=sentinel.a, bb=sentinel.bb) self._asserts_of_proper__new__instance_adjustment(instance) self._asserts_of_proper_preinit_hook_instance_adjustment( instance, binding_key) self._basic_init_related_asserts(instance, SomeParser, super_mock, super_cls_mock, expected_config, expected_config_full)
def dict_to_obj(self, value): obj = SimpleNamespace(**value) for k in ['source', 'confidence', 'category', 'time']: assert hasattr(obj, k) # NOTE: `restriction`, athough it is required to exist in the database, is being # discarded by N6DataSpec.clean_result_dict() for non-full-access clients for k in [ 'restriction', 'name', 'address', 'url', 'fqdn', 'proto', 'sport', 'dport', 'dip', 'adip' ]: if not hasattr(obj, k): setattr(obj, k, None) return obj
def stub_of__QueuedBase_run(self): self._connection = SimpleNamespace( add_timeout=_add_timeout, outbound_buffer=collections.deque()) self.output_ready = True try: try: self.start_publishing() for callback in _consume_timeout_callbacks(): callback() finally: _clear_timeout_callbacks() finally: self._ensure_publishing_generator_closed()
def test_ne(self): obj2 = SimpleNamespace(a=sentinel.a, bb=sentinel.bb, self=sentinel.self) obj3 = SimpleNamespace(a=sentinel.a, bb=sentinel.bb) obj_empty = SimpleNamespace() self.assertFalse(self.obj != self.obj) self.assertFalse(obj2 != obj2) self.assertFalse(obj3 != obj3) self.assertFalse(obj_empty != obj_empty) self.assertFalse(self.obj != obj2) self.assertFalse(obj2 != self.obj) self.assertTrue(self.obj != obj3) self.assertTrue(obj3 != self.obj) self.assertTrue(self.obj != obj_empty) self.assertTrue(obj_empty != self.obj) self.assertTrue(obj2 != obj3) self.assertTrue(obj3 != obj2) self.assertTrue(obj2 != obj_empty) self.assertTrue(obj_empty != obj2) self.assertTrue(obj3 != obj_empty) self.assertTrue(obj_empty != obj3) del obj2.self self.assertFalse(obj2 != obj2) self.assertFalse(obj2 != obj3) self.assertFalse(obj3 != obj2) self.assertTrue(self.obj != obj2) self.assertTrue(obj2 != self.obj) self.assertTrue(obj2 != obj_empty) self.assertTrue(obj_empty != obj2) del obj2.a, obj2.bb self.assertFalse(obj2 != obj2) self.assertFalse(obj2 != obj_empty) self.assertFalse(obj_empty != obj2) self.assertTrue(self.obj != obj2) self.assertTrue(obj2 != self.obj) self.assertTrue(obj2 != obj3) self.assertTrue(obj3 != obj2)
def test__prepare_data__rk__with_raw_format_version_tag(self): data = self.meth.prepare_data( routing_key='ham.spam.33', body=sentinel.body, properties=SimpleNamespace(foo=sentinel.foo, bar=sentinel.bar, timestamp=1389348840, headers={'a': sentinel.a})) self.assertEqual(data, { 'a': sentinel.a, 'properties.foo': sentinel.foo, 'properties.bar': sentinel.bar, 'source': 'ham.spam', 'properties.timestamp': '2014-01-10 10:14:00', 'raw_format_version_tag': '33', 'raw': sentinel.body, })
def test__init(self): super_cls_mock = SimpleNamespace(__init__=Mock()) with patch_always('n6.collectors.generic.super', return_value=super_cls_mock) as super_mock: # instantiation instance = BaseOneShotCollector(input_data=SAMPLE_INPUT_DATA, a=SAMPLE_ARG_A, bb=SAMPLE_ARG_B) # assertions self.assertIsInstance(instance, BaseOneShotCollector) super_mock.assert_called_once_with(BaseOneShotCollector, instance) super_cls_mock.__init__.assert_called_once_with(a=SAMPLE_ARG_A, bb=SAMPLE_ARG_B) self.assertIs(instance.input_data, SAMPLE_INPUT_DATA) # instantiation without the `input_data` argument instance_2 = BaseOneShotCollector(a=SAMPLE_ARG_A, bb=SAMPLE_ARG_B) self.assertEqual(instance_2.input_data, {})
def test_repr(self): self.obj.xxxx = 'xxxx' self.assertEqual( repr(self.obj), "SimpleNamespace(a=sentinel.a, bb=sentinel.bb, " "self=sentinel.self, xxxx='xxxx')") self.assertEqual(repr(SimpleNamespace()), "SimpleNamespace()")
def setUp(self): self.obj = SimpleNamespace(a=sentinel.a, bb=sentinel.bb) self.obj.self = sentinel.self
SAMPLE_OTHER_CONFIG_SECTION = "other_section" SAMPLE_CONFIG_REQUIRED = ('required_opt', ) SAMPLE_REQUIRED_VALUE = "some option which is required" SAMPLE_OTHER_REQUIRED_VALUE = "123.89" CONFIG_WITH_NO_SECTION_DECLARED = ConfigSection('<no section declared>') MOCKED_CONFIG = { 'some_section': { 'required_opt': SAMPLE_REQUIRED_VALUE, 'some_opt': "ABc dd", }, 'other_section': { 'required_opt': SAMPLE_OTHER_REQUIRED_VALUE, 'some_opt': '[{"a": "bcd"}]', }, } MOCKED_SUPER_CLS = SimpleNamespace(__init__=Mock()) SAMPLE_EMAIL_MESSAGE = sentinel.email_msg SAMPLE_INPUT_DATA = sentinel.input_data SAMPLE_MESSAGE_ID = sentinel.message_id SAMPLE_SOURCE = sentinel.source SAMPLE_SOURCE_CHANNEL = sentinel.source_channel SAMPLE_TYPE = sentinel.type SAMPLE_CONTENT_TYPE = 'text/csv' SAMPLE_OUTPUT_COMPONENTS = sentinel.output_components SAMPLE_OUTPUT_RK = sentinel.output_rk SAMPLE_OUTPUT_DATA_BODY = sentinel.output_data_body SAMPLE_OUTPUT_PROP_KWARGS = sentinel.output_prop_kwargs CONFIG_PATCHER = patch('n6lib.config.Config._load_n6_config_files', return_value=MOCKED_CONFIG) SUPER_PATCHER = patch('n6.collectors.generic.super',