def test_recording_clears(self): from example_class import ExampleClass # pylint: disable=import-error with appmap.Recording(): ExampleClass.static_method() # fresh recording shouldn't contain previous traces rec = appmap.Recording() with rec: ExampleClass.class_method() assert rec.events[0].method_id == 'class_method' # but it can be added to with rec: ExampleClass().instance_method() assert rec.events[2].method_id == 'instance_method'
def test_recursion_protection(self): r = appmap.Recording() with r: from example_class import ExampleClass ExampleClass().instance_method() # If we get here, recursion protection for rendering the receiver # is working assert True
def test_recording_shallow(self): from example_class import ExampleClass # pylint: disable=import-error rec = appmap.Recording() with rec: ExampleClass.class_method() ExampleClass().instance_method() ExampleClass.class_method() ExampleClass().instance_method() assert len(rec.events) == 8
def _generate(check_fn, method_name): monkeypatch.setattr(generation.FuncEntry, 'to_dict', partialmethod( check_fn, generation.FuncEntry.to_dict)) from example_class import ExampleClass # pylint: disable=import-error rec = appmap.Recording() with rec: m = getattr(ExampleClass(), method_name) m() return generation.dump(rec)
def test_when_both_raise(self, mocker): r = appmap.Recording() with r: from example_class import ExampleClass param = mocker.Mock() param.__str__ = mocker.Mock(side_effect=Exception) param.__repr__ = mocker.Mock(side_effect=Exception) ExampleClass().instance_with_param(param) expected_re = r'<.*? object at .*?>' actual_value = r.events[0].parameters[0]['value'] assert re.fullmatch(expected_re, actual_value)
def test_when_str_raises(self, mocker): r = appmap.Recording() with r: from example_class import ExampleClass param = mocker.Mock() param.__str__ = mocker.Mock(side_effect=Exception) param.__repr__ = mocker.Mock(return_value='param.__repr__') ExampleClass().instance_with_param(param) expected_value = 'param.__repr__' actual_value = r.events[0].parameters[0]['value'] assert expected_value == actual_value
def test_recording_wrapped(self): from example_class import ExampleClass # pylint: disable=import-error rec = appmap.Recording() with rec: ExampleClass.wrapped_class_method() ExampleClass.wrapped_static_method() ExampleClass().wrapped_instance_method() assert len(rec.events) == 6 evt = rec.events[-2] assert evt.event == 'call' assert evt.method_id == 'wrapped_instance_method'
def test_recording_works(self, with_data_dir): with open(os.path.join(with_data_dir, 'expected.appmap.json')) as f: expected_appmap = json.load(f) r = appmap.Recording() with r: from example_class import ExampleClass # pylint: disable=import-error ExampleClass.static_method() ExampleClass.class_method() ExampleClass().instance_method() ExampleClass.what_time_is_it() try: ExampleClass().test_exception() except: # pylint: disable=bare-except # noqa: E722 pass ExampleClass.call_yaml() generated_appmap = normalize_appmap(appmap.generation.dump(r)) assert remove_line_numbers(generated_appmap) == expected_appmap
def test_when_display_disabled(self, mocker): Env.current.set("APPMAP_DISPLAY_PARAMS", "false") r = appmap.Recording() with r: from example_class import ExampleClass param = mocker.MagicMock() # unittest.mock.MagicMock doesn't mock __repr__ by default param.__repr__ = mocker.Mock() ExampleClass().instance_with_param(param) param.__str__.assert_not_called() # The reason MagicMock doesn't mock __repr__ is because it # uses it. If APPMAP_DISPLAY_PARAMS is functioning # correctly, __repr__ will only be called once, by # MagicMock. (If it's broken, we may not get here at all, # because the assertion above may fail.) param.__repr__.assert_called_once_with()
def test_labeled_function(self, monkeypatch): def check_labels(self, to_dict): if self.name == 'labeled_method': assert list(self.labels) == ['super', 'important'] elif self.name == 'instance_method': assert not self.labels ret = to_dict(self) return ret monkeypatch.setattr( generation.FuncEntry, 'to_dict', partialmethod(check_labels, generation.FuncEntry.to_dict)) from example_class import ExampleClass # pylint: disable=import-error rec = appmap.Recording() with rec: ExampleClass().labeled_method() ExampleClass().instance_method() generation.dump(rec)
def test_recording_works(self, data_dir): with open(os.path.join(data_dir, 'expected.appmap.json')) as f: expected_appmap = json.load(f) import yaml appmap.instrument_module(yaml) r = appmap.Recording() with r: from example_class import ExampleClass # pylint: disable=import-error ExampleClass.static_method() ExampleClass.class_method() ExampleClass().instance_method() ExampleClass.what_time_is_it() try: ExampleClass().test_exception() except: # pylint: disable=bare-except # noqa: E722 pass ExampleClass.call_yaml() generated_appmap = self.normalize_appmap(appmap.generation.dump(r)) print(json.dumps(generated_appmap, indent=2)) assert generated_appmap == expected_appmap
def test_cant_start_twice(self): rec = appmap.Recording() rec.start() with pytest.raises(RuntimeError): rec.start()